[PATCH] D44293: [clangd] Fix irrelevant declaratations in goto definition (on macros).

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 9 02:01:34 PST 2018


hokein created this revision.
hokein added a reviewer: ilya-biryukov.
Herald added subscribers: ioeric, jkorous-apple, klimek.

DeclrationAndMacrosFinder will find some declarations (not macro!) that are
referened inside the macro somehow, isSearchedLocation() is not sufficient, we
don't know whether the searched source location is macro or not.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D44293

Files:
  clangd/XRefs.cpp
  unittests/clangd/XRefsTests.cpp


Index: unittests/clangd/XRefsTests.cpp
===================================================================
--- unittests/clangd/XRefsTests.cpp
+++ unittests/clangd/XRefsTests.cpp
@@ -213,6 +213,15 @@
         #undef macro
       )cpp",
 
+      R"cpp(// Macro
+       class TTT { public: int a; };
+       #define [[FF(S) if (int b = S.a) {}]]
+       void f() {
+         TTT t;
+         F^F(t);
+       }
+      )cpp",
+
       R"cpp(// Forward class declaration
         class Foo;
         [[class Foo {}]];
Index: clangd/XRefs.cpp
===================================================================
--- clangd/XRefs.cpp
+++ clangd/XRefs.cpp
@@ -197,19 +197,21 @@
   indexTopLevelDecls(AST.getASTContext(), AST.getTopLevelDecls(),
                      DeclMacrosFinder, IndexOpts);
 
-  std::vector<const Decl *> Decls = DeclMacrosFinder->takeDecls();
   std::vector<MacroDecl> MacroInfos = DeclMacrosFinder->takeMacroInfos();
+  if (!MacroInfos.empty()) {
+    for (auto Item : MacroInfos) {
+      SourceRange SR(Item.Info->getDefinitionLoc(),
+                     Item.Info->getDefinitionEndLoc());
+      auto L = getDeclarationLocation(AST, SR);
+      if (L)
+        Result.push_back(*L);
+    }
 
-  for (auto Item : Decls) {
-    auto L = getDeclarationLocation(AST, Item->getSourceRange());
-    if (L)
-      Result.push_back(*L);
+    return Result;
   }
 
-  for (auto Item : MacroInfos) {
-    SourceRange SR(Item.Info->getDefinitionLoc(),
-                   Item.Info->getDefinitionEndLoc());
-    auto L = getDeclarationLocation(AST, SR);
+  for (auto Item : DeclMacrosFinder->takeDecls()) {
+    auto L = getDeclarationLocation(AST, Item->getSourceRange());
     if (L)
       Result.push_back(*L);
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D44293.137707.patch
Type: text/x-patch
Size: 1729 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180309/dfb7ce6a/attachment.bin>


More information about the cfe-commits mailing list