[PATCH] D138559: Record macro references in #ifdef clause.

Viktoriia Bakalova via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 23 04:44:23 PST 2022


VitaNuo updated this revision to Diff 477461.
VitaNuo added a comment.

Update


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D138559

Files:
  clang-tools-extra/include-cleaner/lib/Record.cpp
  clang-tools-extra/include-cleaner/unittests/RecordTest.cpp


Index: clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
===================================================================
--- clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
@@ -7,12 +7,13 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang-include-cleaner/Record.h"
+#include "clang/Basic/SourceLocation.h"
 #include "clang/Frontend/FrontendAction.h"
 #include "clang/Frontend/FrontendActions.h"
 #include "clang/Testing/TestAST.h"
 #include "clang/Tooling/Inclusions/StandardLibrary.h"
-#include "llvm/Support/VirtualFileSystem.h"
 #include "llvm/ADT/ArrayRef.h"
+#include "llvm/Support/VirtualFileSystem.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Testing/Support/Annotations.h"
 #include "gmock/gmock.h"
@@ -218,6 +219,27 @@
   EXPECT_THAT(ExpOffsets, ElementsAreArray(MainFile.points("exp")));
 }
 
+TEST_F(RecordPPTest, CapturesIfDefMacroRefs) {
+  llvm::Annotations MainFile(R"cpp(
+    #define X 1
+    #ifdef $def^X
+     #define Y 2
+    #endif
+  )cpp");
+
+  Inputs.Code = MainFile.code();
+  auto AST = build();
+  ASSERT_THAT(Recorded.MacroReferences, Not(IsEmpty()));
+
+  SourceManager &SM = AST.sourceManager();
+  SourceLocation Def = SM.getComposedLoc(SM.getMainFileID(), MainFile.point("def"));
+
+  SymbolReference XRef = Recorded.MacroReferences.front();
+  EXPECT_EQ(XRef.RT, RefType::Ambiguous);
+  EXPECT_EQ("X", XRef.Target.macro().Name->getName());
+  EXPECT_EQ(XRef.RefLocation, Def);
+}
+
 // Matches an Include* on the specified line;
 MATCHER_P(line, N, "") { return arg->Line == (unsigned)N; }
 
Index: clang-tools-extra/include-cleaner/lib/Record.cpp
===================================================================
--- clang-tools-extra/include-cleaner/lib/Record.cpp
+++ clang-tools-extra/include-cleaner/lib/Record.cpp
@@ -53,7 +53,7 @@
                     SourceRange Range, const MacroArgs *Args) override {
     if (!Active)
       return;
-    recordMacroRef(MacroName, *MD.getMacroInfo());
+    recordMacroRef(MacroName, *MD.getMacroInfo(), RefType::Explicit);
   }
 
   void MacroDefined(const Token &MacroName, const MacroDirective *MD) override {
@@ -71,7 +71,7 @@
           llvm::is_contained(MI->params(), II))
         continue;
       if (const MacroInfo *MI = PP.getMacroInfo(II))
-        recordMacroRef(Tok, *MI);
+        recordMacroRef(Tok, *MI, RefType::Explicit);
     }
   }
 
@@ -80,17 +80,25 @@
     if (!Active)
       return;
     if (const auto *MI = MD.getMacroInfo())
-      recordMacroRef(MacroName, *MI);
+      recordMacroRef(MacroName, *MI, RefType::Explicit);
+  }
+
+  void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
+                     const MacroDefinition &MD) override {
+    if (!Active)
+      return;
+    if (const auto *MI = MD.getMacroInfo())
+      recordMacroRef(MacroNameTok, *MI, RefType::Ambiguous);
   }
 
 private:
-  void recordMacroRef(const Token &Tok, const MacroInfo &MI) {
+  void recordMacroRef(const Token &Tok, const MacroInfo &MI, RefType RT) {
     if (MI.isBuiltinMacro())
       return; // __FILE__ is not a reference.
     Recorded.MacroReferences.push_back(
         SymbolReference{Tok.getLocation(),
                         Macro{Tok.getIdentifierInfo(), MI.getDefinitionLoc()},
-                        RefType::Explicit});
+                        RT});
   }
 
   bool Active = false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138559.477461.patch
Type: text/x-patch
Size: 3460 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221123/68db8439/attachment.bin>


More information about the cfe-commits mailing list