[clang-tools-extra] 6585dd3 - [clangd] Replace the hacky include-cleaner macro-reference implementation.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 23 05:09:04 PDT 2023


Author: Haojian Wu
Date: 2023-06-23T14:08:55+02:00
New Revision: 6585dd3b83738789dff5ca82008efdf84c9b922c

URL: https://github.com/llvm/llvm-project/commit/6585dd3b83738789dff5ca82008efdf84c9b922c
DIFF: https://github.com/llvm/llvm-project/commit/6585dd3b83738789dff5ca82008efdf84c9b922c.diff

LOG: [clangd] Replace the hacky include-cleaner macro-reference implementation.

Now MainFileMacros preserves enough information, we perform a just-in-time
convertion to interop with include-cleaner::Macro for include-cleaer features.

Differential Revision: https://reviews.llvm.org/D147034

Added: 
    

Modified: 
    clang-tools-extra/clangd/IncludeCleaner.cpp
    clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/IncludeCleaner.cpp b/clang-tools-extra/clangd/IncludeCleaner.cpp
index e04e28bd30f18..dd8b5afc2a24f 100644
--- a/clang-tools-extra/clangd/IncludeCleaner.cpp
+++ b/clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -353,20 +353,29 @@ getUnused(ParsedAST &AST,
 std::vector<include_cleaner::SymbolReference>
 collectMacroReferences(ParsedAST &AST) {
   const auto &SM = AST.getSourceManager();
-  //  FIXME: !!this is a hacky way to collect macro references.
-  std::vector<include_cleaner::SymbolReference> Macros;
   auto &PP = AST.getPreprocessor();
-  for (const syntax::Token &Tok :
-       AST.getTokens().spelledTokens(SM.getMainFileID())) {
-    auto Macro = locateMacroAt(Tok, PP);
-    if (!Macro)
-      continue;
-    if (auto DefLoc = Macro->Info->getDefinitionLoc(); DefLoc.isValid())
+  std::vector<include_cleaner::SymbolReference> Macros;
+  for (const auto &[_, Refs] : AST.getMacros().MacroRefs) {
+    for (const auto &Ref : Refs) {
+      auto Loc = SM.getComposedLoc(SM.getMainFileID(), Ref.StartOffset);
+      const auto *Tok = AST.getTokens().spelledTokenAt(Loc);
+      if (!Tok)
+        continue;
+      auto Macro = locateMacroAt(*Tok, PP);
+      if (!Macro)
+        continue;
+      auto DefLoc = Macro->NameLoc;
+      if (!DefLoc.isValid())
+        continue;
       Macros.push_back(
-          {include_cleaner::Macro{/*Name=*/PP.getIdentifierInfo(Tok.text(SM)),
+          {include_cleaner::Macro{/*Name=*/PP.getIdentifierInfo(Tok->text(SM)),
                                   DefLoc},
-           Tok.location(), include_cleaner::RefType::Explicit});
+           Tok->location(),
+           Ref.InConditionalDirective ? include_cleaner::RefType::Ambiguous
+                                      : include_cleaner::RefType::Explicit});
+    }
   }
+
   return Macros;
 }
 

diff  --git a/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp b/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
index 33e3960689505..6397f32c525f1 100644
--- a/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
+++ b/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
@@ -186,6 +186,10 @@ TEST(IncludeCleaner, GenerateMissingHeaderDiags) {
 #define DEF(X) const Foo *X;
 #define BAZ(X) const X x
 
+// No missing include insertion for ambiguous macro refs.
+#if defined(FOO)
+#endif
+
   void foo() {
     $b[[b]]();
 


        


More information about the cfe-commits mailing list