Index: clang-tools-extra/clangd/index/SymbolCollector.cpp =================================================================== --- clang-tools-extra/clangd/index/SymbolCollector.cpp +++ clang-tools-extra/clangd/index/SymbolCollector.cpp @@ -374,13 +374,17 @@ const auto &SM = PP->getSourceManager(); auto DefLoc = MI->getDefinitionLoc(); - if (SM.isInMainFile(SM.getExpansionLoc(DefLoc))) - return true; + bool IsMainFileSymbol = SM.isInMainFile(SM.getExpansionLoc(DefLoc)); + // Header guards are not interesting in index. Builtin macros don't have // useful locations and are not needed for code completions. if (MI->isUsedForHeaderGuard() || MI->isBuiltinMacro()) return true; + // Also avoid storing predefined macros like __DBL_MIN__. + if (IsMainFileSymbol && SM.isWrittenInBuiltinFile(DefLoc)) + return true; + // Mark the macro as referenced if this is a reference coming from the main // file. The macro may not be an interesting symbol, but it's cheaper to check // at the end. @@ -405,7 +409,8 @@ Symbol S; S.ID = std::move(*ID); S.Name = Name->getName(); - S.Flags |= Symbol::IndexedForCodeCompletion; + if (!IsMainFileSymbol) + S.Flags |= Symbol::IndexedForCodeCompletion; S.SymInfo = index::getSymbolInfoForMacro(*MI); std::string FileURI; // FIXME: use the result to filter out symbols. Index: clang-tools-extra/unittests/clangd/FindSymbolsTests.cpp =================================================================== --- clang-tools-extra/unittests/clangd/FindSymbolsTests.cpp +++ clang-tools-extra/unittests/clangd/FindSymbolsTests.cpp @@ -88,13 +88,16 @@ } // namespace -TEST_F(WorkspaceSymbolsTest, NoMacro) { +TEST_F(WorkspaceSymbolsTest, Macros) { addFile("foo.cpp", R"cpp( #define MACRO X )cpp"); - // Macros are not in the index. - EXPECT_THAT(getSymbols("macro"), IsEmpty()); + // LSP's SymbolKind doesn't have a "Macro" kind, and + // indexSymbolKindToSymbolKind() currently maps macros + // to SymbolKind::String. + EXPECT_THAT(getSymbols("macro"), + ElementsAre(AllOf(QName("MACRO"), WithKind(SymbolKind::String)))); } TEST_F(WorkspaceSymbolsTest, NoLocals) { Index: clang-tools-extra/unittests/clangd/SymbolCollectorTests.cpp =================================================================== --- clang-tools-extra/unittests/clangd/SymbolCollectorTests.cpp +++ clang-tools-extra/unittests/clangd/SymbolCollectorTests.cpp @@ -1053,13 +1053,13 @@ MAC(p); )"); - const std::string Main = R"( - #define MAIN 1 // not indexed + Annotations Main(R"( + #define $main[[MAIN]] 1 USED(t); - )"; + )"); CollectorOpts.CountReferences = true; CollectorOpts.CollectMacro = true; - runSymbolCollector(Header.code(), Main); + runSymbolCollector(Header.code(), Main.code()); EXPECT_THAT(Symbols, UnorderedElementsAre(QName("p"), QName("t"), AllOf(QName("X"), DeclURI(TestHeaderURI), @@ -1067,7 +1067,9 @@ AllOf(Labeled("MAC(x)"), RefCount(0), DeclRange(Header.range("mac"))), AllOf(Labeled("USED(y)"), RefCount(1), - DeclRange(Header.range("used"))))); + DeclRange(Header.range("used"))), + AllOf(Labeled("MAIN"), RefCount(0), + DeclRange(Main.range("main"))))); } TEST_F(SymbolCollectorTest, DeprecatedSymbols) {