Index: clangd/XRefs.cpp =================================================================== --- clangd/XRefs.cpp +++ clangd/XRefs.cpp @@ -197,19 +197,21 @@ indexTopLevelDecls(AST.getASTContext(), AST.getTopLevelDecls(), DeclMacrosFinder, IndexOpts); - std::vector Decls = DeclMacrosFinder->takeDecls(); std::vector 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); } 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 {}]];