Skip to content

Commit

Permalink
[clangd] Remove calls to getFileLoc() in declToSym() (#83532)
Browse files Browse the repository at this point in the history
toHalfOpenFileRange() already handles translating macro locations to
file locations, and it can provide a better result by knowing about both
ends of the range.

Fixes clangd/clangd#1941
  • Loading branch information
HighCommander4 committed Mar 6, 2024
1 parent 5549b01 commit d1aec79
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
4 changes: 2 additions & 2 deletions clang-tools-extra/clangd/FindSymbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ std::string getSymbolDetail(ASTContext &Ctx, const NamedDecl &ND) {
std::optional<DocumentSymbol> declToSym(ASTContext &Ctx, const NamedDecl &ND) {
auto &SM = Ctx.getSourceManager();

SourceLocation BeginLoc = SM.getFileLoc(ND.getBeginLoc());
SourceLocation EndLoc = SM.getFileLoc(ND.getEndLoc());
SourceLocation BeginLoc = ND.getBeginLoc();
SourceLocation EndLoc = ND.getEndLoc();
const auto SymbolRange =
toHalfOpenFileRange(SM, Ctx.getLangOpts(), {BeginLoc, EndLoc});
if (!SymbolRange)
Expand Down
9 changes: 8 additions & 1 deletion clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,9 @@ TEST(DocumentSymbols, RangeFromMacro) {
$fullDef[[FF3() {
int var = 42;
}]]
#define FF4(name) int name = 0
$FooRange[[FF4($FooSelectionRange[[foo]])]];
)");
TU.Code = Main.code().str();
EXPECT_THAT(
Expand All @@ -766,7 +769,11 @@ TEST(DocumentSymbols, RangeFromMacro) {
AllOf(withName("FF3"), withDetail("()"),
symRange(Main.range("fullDef")),
children(AllOf(withName("waldo"), withDetail("void ()"),
symRange(Main.range("fullDef")))))));
symRange(Main.range("fullDef"))))),
AllOf(
withName("FF4"), withDetail("(foo)"),
children(AllOf(withName("foo"), symRange(Main.range("FooRange")),
symNameRange(Main.range("FooSelectionRange")))))));
}

TEST(DocumentSymbols, FuncTemplates) {
Expand Down

0 comments on commit d1aec79

Please sign in to comment.