Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clangd] Remove calls to getFileLoc() in declToSym() #83532

Merged
merged 1 commit into from
Mar 6, 2024

Conversation

HighCommander4
Copy link
Collaborator

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

toHalfOpenFileRange() already handles translating macro locations
to file locations, and it can provide a better result by knowing
about both ends of the range.

clangd/clangd#1941
@llvmbot
Copy link
Collaborator

llvmbot commented Mar 1, 2024

@llvm/pr-subscribers-clangd

@llvm/pr-subscribers-clang-tools-extra

Author: Nathan Ridge (HighCommander4)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/83532.diff

2 Files Affected:

  • (modified) clang-tools-extra/clangd/FindSymbols.cpp (+2-2)
  • (modified) clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp (+8-1)
diff --git a/clang-tools-extra/clangd/FindSymbols.cpp b/clang-tools-extra/clangd/FindSymbols.cpp
index 5b3e46a7b4dc16..5244a4e893769e 100644
--- a/clang-tools-extra/clangd/FindSymbols.cpp
+++ b/clang-tools-extra/clangd/FindSymbols.cpp
@@ -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)
diff --git a/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp b/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp
index b1b8b4ccd184c2..4276a44275f535 100644
--- a/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp
@@ -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(
@@ -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) {

@HighCommander4 HighCommander4 merged commit d1aec79 into llvm:main Mar 6, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suboptimal selection range for variable defined using a macro
3 participants