[clang-tools-extra] 7d668ae - [clangd] Mark macros from preamble for code completion

Kadir Cetinkaya via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 12 05:07:04 PST 2021


Author: Kadir Cetinkaya
Date: 2021-11-12T14:01:14+01:00
New Revision: 7d668ae38d2d54ebd5eca5f66a8cf353c3526dc3

URL: https://github.com/llvm/llvm-project/commit/7d668ae38d2d54ebd5eca5f66a8cf353c3526dc3
DIFF: https://github.com/llvm/llvm-project/commit/7d668ae38d2d54ebd5eca5f66a8cf353c3526dc3.diff

LOG: [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

Added: 
    

Modified: 
    clang-tools-extra/clangd/index/SymbolCollector.cpp
    clang-tools-extra/clangd/unittests/FileIndexTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/index/SymbolCollector.cpp b/clang-tools-extra/clangd/index/SymbolCollector.cpp
index 0591c5113a23a..dfcd4cb50a130 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 5f1998af3c21f..5b429d61a9e46 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


        


More information about the cfe-commits mailing list