[clang-tools-extra] [clangd] Remove calls to getFileLoc() in declToSym() (PR #83532)

Nathan Ridge via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 29 22:38:17 PST 2024


https://github.com/HighCommander4 created https://github.com/llvm/llvm-project/pull/83532

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 https://github.com/clangd/clangd/issues/1941

>From b419bcb01b406321af2cf226b3d30390d7231487 Mon Sep 17 00:00:00 2001
From: Nathan Ridge <zeratul976 at hotmail.com>
Date: Wed, 28 Feb 2024 03:26:07 -0500
Subject: [PATCH] [clangd] Remove calls to getFileLoc() in declToSym()

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

https://github.com/clangd/clangd/issues/1941
---
 clang-tools-extra/clangd/FindSymbols.cpp                | 4 ++--
 clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp | 9 ++++++++-
 2 files changed, 10 insertions(+), 3 deletions(-)

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) {



More information about the cfe-commits mailing list