[clang-tools-extra] 6ebd0aa - [include-cleaner] Record macro references in #ifdef clause.
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 24 05:48:39 PST 2022
Author: Viktoriia Bakalova
Date: 2022-11-24T14:48:25+01:00
New Revision: 6ebd0aa42066dd1879217b260704571e440ce5ef
URL: https://github.com/llvm/llvm-project/commit/6ebd0aa42066dd1879217b260704571e440ce5ef
DIFF: https://github.com/llvm/llvm-project/commit/6ebd0aa42066dd1879217b260704571e440ce5ef.diff
LOG: [include-cleaner] Record macro references in #ifdef clause.
Records macro references in #ifdef clauses as ambiguous.
Reviewed By: hokein
Differential Revision: https://reviews.llvm.org/D138559
Added:
Modified:
clang-tools-extra/include-cleaner/lib/Record.cpp
clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/include-cleaner/lib/Record.cpp b/clang-tools-extra/include-cleaner/lib/Record.cpp
index 16956b45198b6..618ec89e82ab7 100644
--- a/clang-tools-extra/include-cleaner/lib/Record.cpp
+++ b/clang-tools-extra/include-cleaner/lib/Record.cpp
@@ -83,14 +83,54 @@ class PPRecorder : public PPCallbacks {
recordMacroRef(MacroName, *MI);
}
+ 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);
+ }
+
+ void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
+ const MacroDefinition &MD) override {
+ if (!Active)
+ return;
+ if (const auto *MI = MD.getMacroInfo())
+ recordMacroRef(MacroNameTok, *MI, RefType::Ambiguous);
+ }
+
+ void Elifdef(SourceLocation Loc, const Token &MacroNameTok,
+ const MacroDefinition &MD) override {
+ if (!Active)
+ return;
+ if (const auto *MI = MD.getMacroInfo())
+ recordMacroRef(MacroNameTok, *MI, RefType::Ambiguous);
+ }
+
+ void Elifndef(SourceLocation Loc, const Token &MacroNameTok,
+ const MacroDefinition &MD) override {
+ if (!Active)
+ return;
+ if (const auto *MI = MD.getMacroInfo())
+ recordMacroRef(MacroNameTok, *MI, RefType::Ambiguous);
+ }
+
+ void Defined(const Token &MacroNameTok, const MacroDefinition &MD,
+ SourceRange Range) 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 = RefType::Explicit) {
if (MI.isBuiltinMacro())
return; // __FILE__ is not a reference.
- Recorded.MacroReferences.push_back(
- SymbolReference{Tok.getLocation(),
- Macro{Tok.getIdentifierInfo(), MI.getDefinitionLoc()},
- RefType::Explicit});
+ Recorded.MacroReferences.push_back(SymbolReference{
+ Tok.getLocation(),
+ Macro{Tok.getIdentifierInfo(), MI.getDefinitionLoc()}, RT});
}
bool Active = false;
diff --git a/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp b/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
index efe8e405102bc..fb4e0632fd242 100644
--- a/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
+++ b/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"
@@ -217,6 +218,44 @@ TEST_F(RecordPPTest, CapturesMacroRefs) {
EXPECT_THAT(ExpOffsets, ElementsAreArray(MainFile.points("exp")));
}
+TEST_F(RecordPPTest, CapturesConditionalMacroRefs) {
+ llvm::Annotations MainFile(R"cpp(
+ #define X 1
+
+ #ifdef ^X
+ #endif
+
+ #if defined(^X)
+ #endif
+
+ #ifndef ^X
+ #endif
+
+ #ifdef Y
+ #elifdef ^X
+ #endif
+
+ #ifndef ^X
+ #elifndef ^X
+ #endif
+ )cpp");
+
+ Inputs.Code = MainFile.code();
+ Inputs.ExtraArgs.push_back("-std=c++2b");
+ auto AST = build();
+
+ std::vector<unsigned> RefOffsets;
+ SourceManager &SM = AST.sourceManager();
+ for (const auto &Ref : Recorded.MacroReferences) {
+ auto [FID, Off] = SM.getDecomposedLoc(Ref.RefLocation);
+ ASSERT_EQ(FID, SM.getMainFileID());
+ EXPECT_EQ(Ref.RT, RefType::Ambiguous);
+ EXPECT_EQ("X", Ref.Target.macro().Name->getName());
+ RefOffsets.push_back(Off);
+ }
+ EXPECT_THAT(RefOffsets, ElementsAreArray(MainFile.points()));
+}
+
// Matches an Include* on the specified line;
MATCHER_P(line, N, "") { return arg->Line == (unsigned)N; }
More information about the cfe-commits
mailing list