diff --git a/clang-tools-extra/clangd/index/SymbolCollector.cpp b/clang-tools-extra/clangd/index/SymbolCollector.cpp --- a/clang-tools-extra/clangd/index/SymbolCollector.cpp +++ b/clang-tools-extra/clangd/index/SymbolCollector.cpp @@ -564,6 +564,12 @@ S.SymInfo.Lang = index::SymbolLanguage::C; S.Origin = Opts.Origin; S.CanonicalDeclaration = R.Location; + // Make the macro visible for code completion if main file is an + // include-able header. + if (!HeaderFileURIs->getIncludeHeader(SM.getMainFileID()).empty()) { + S.Flags |= Symbol::IndexedForCodeCompletion; + S.Flags |= Symbol::VisibleOutsideFile; + } Symbols.insert(S); } } diff --git a/clang-tools-extra/clangd/unittests/FileIndexTests.cpp b/clang-tools-extra/clangd/unittests/FileIndexTests.cpp --- a/clang-tools-extra/clangd/unittests/FileIndexTests.cpp +++ b/clang-tools-extra/clangd/unittests/FileIndexTests.cpp @@ -727,6 +727,19 @@ EXPECT_THAT(MT.child("f3").children(), ElementsAre(Pair("relations", _))); EXPECT_THAT(MT.child("f3").total(), Gt(0U)); } + +TEST(FileIndexTest, MacrosFromMainFile) { + FileIndex Idx; + TestTU TU; + TU.Code = "#pragma once\n#define FOO"; + TU.Filename = "foo.h"; + auto AST = TU.build(); + Idx.updateMain(testPath(TU.Filename), AST); + + auto &FooSymbol = findSymbol(runFuzzyFind(Idx, ""), "FOO"); + EXPECT_TRUE(FooSymbol.Flags & Symbol::IndexedForCodeCompletion); +} + } // namespace } // namespace clangd } // namespace clang