Index: clang-tools-extra/trunk/clangd/AST.cpp =================================================================== --- clang-tools-extra/trunk/clangd/AST.cpp +++ clang-tools-extra/trunk/clangd/AST.cpp @@ -23,9 +23,11 @@ namespace clangd { // Returns true if the complete name of decl \p D is spelled in the source code. -// This is not the case for symbols formed via macro concatenation. -// (We used to attempt to treat names spelled on the command-line this way too, -// but the preamble doesn't preserve the required information). +// This is not the case for: +// * symbols formed via macro concatenation, the spelling location will +// be "" +// * symbols controlled and defined by a compile command-line option +// `-DName=foo`, the spelling location will be "". bool isSpelledInSourceCode(const Decl *D) { const auto &SM = D->getASTContext().getSourceManager(); auto Loc = D->getLocation(); @@ -33,7 +35,8 @@ // macros, we should use the location where the whole definition occurs. if (Loc.isMacroID()) { std::string PrintLoc = SM.getSpellingLoc(Loc).printToString(SM); - if (StringRef(PrintLoc).startswith("")) return false; } return true; Index: clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp =================================================================== --- clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp +++ clang-tools-extra/trunk/unittests/clangd/SymbolCollectorTests.cpp @@ -614,6 +614,18 @@ DeclURI(TestHeaderURI)))); } +TEST_F(SymbolCollectorTest, SymbolFormedByCLI) { + Annotations Header(R"( + #ifdef NAME + class $expansion[[NAME]] {}; + #endif + )"); + runSymbolCollector(Header.code(), /*Main=*/"", /*ExtraArgs=*/{"-DNAME=name"}); + EXPECT_THAT(Symbols, UnorderedElementsAre(AllOf( + QName("name"), DeclRange(Header.range("expansion")), + DeclURI(TestHeaderURI)))); +} + TEST_F(SymbolCollectorTest, IgnoreSymbolsInMainFile) { const std::string Header = R"( class Foo {};