summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKadir Cetinkaya <kadircet@google.com>2021-11-10 12:10:33 +0100
committerKadir Cetinkaya <kadircet@google.com>2021-11-12 14:01:14 +0100
commit7d668ae38d2d54ebd5eca5f66a8cf353c3526dc3 (patch)
treee269b80caa9cfe92ac39d6b5f449432c77783fa4
parent1d7fdbbc183aee2f1168425ae21e961164495466 (diff)
[clangd] Mark macros from preamble for code completion
If the main file is a header, mark the marcos defined in its preamble section as code-completion ready. Fixes https://github.com/clangd/clangd/issues/921. Differential Revision: https://reviews.llvm.org/D113555
-rw-r--r--clang-tools-extra/clangd/index/SymbolCollector.cpp6
-rw-r--r--clang-tools-extra/clangd/unittests/FileIndexTests.cpp13
2 files changed, 19 insertions, 0 deletions
diff --git a/clang-tools-extra/clangd/index/SymbolCollector.cpp b/clang-tools-extra/clangd/index/SymbolCollector.cpp
index 0591c5113a23..dfcd4cb50a13 100644
--- a/clang-tools-extra/clangd/index/SymbolCollector.cpp
+++ b/clang-tools-extra/clangd/index/SymbolCollector.cpp
@@ -564,6 +564,12 @@ void SymbolCollector::handleMacros(const MainFileMacros &MacroRefsToIndex) {
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
index 5f1998af3c21..5b429d61a9e4 100644
--- a/clang-tools-extra/clangd/unittests/FileIndexTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FileIndexTests.cpp
@@ -727,6 +727,19 @@ TEST(FileSymbolsTest, Profile) {
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