This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG6585dd3b8373: [clangd] Replace the hacky include-cleaner 
macro-reference implementation. (authored by hokein).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147034/new/

https://reviews.llvm.org/D147034

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


Index: clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
+++ clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
@@ -186,6 +186,10 @@
 #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]]();
 
Index: clang-tools-extra/clangd/IncludeCleaner.cpp
===================================================================
--- clang-tools-extra/clangd/IncludeCleaner.cpp
+++ clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -353,20 +353,29 @@
 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;
 }
 


Index: clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
+++ clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
@@ -186,6 +186,10 @@
 #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]]();
 
Index: clang-tools-extra/clangd/IncludeCleaner.cpp
===================================================================
--- clang-tools-extra/clangd/IncludeCleaner.cpp
+++ clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -353,20 +353,29 @@
 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;
 }
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to