[clang-tools-extra] [include-cleaner][NFC] record macro refs in direct includes (PR #189366)
unterumarmung wrote: Moved to: https://github.com/llvm/llvm-project/pull/196761 https://github.com/llvm/llvm-project/pull/189366 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [include-cleaner][NFC] record macro refs in direct includes (PR #189366)
https://github.com/unterumarmung closed https://github.com/llvm/llvm-project/pull/189366 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [include-cleaner][NFC] record macro refs in direct includes (PR #189366)
unterumarmung wrote: @zeyi2 @localspook @vbvictor could you please take a look? https://github.com/llvm/llvm-project/pull/189366 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [include-cleaner][NFC] record macro refs in direct includes (PR #189366)
https://github.com/unterumarmung edited https://github.com/llvm/llvm-project/pull/189366 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [include-cleaner][NFC] record macro refs in direct includes (PR #189366)
llvmbot wrote:
@llvm/pr-subscribers-clang-tools-extra
Author: Daniil Dudkin (unterumarmung)
Changes
---
Full diff: https://github.com/llvm/llvm-project/pull/189366.diff
3 Files Affected:
- (modified)
clang-tools-extra/include-cleaner/include/clang-include-cleaner/Record.h (+3-2)
- (modified) clang-tools-extra/include-cleaner/lib/Record.cpp (+18-8)
- (modified) clang-tools-extra/include-cleaner/unittests/RecordTest.cpp (+69-2)
``diff
diff --git
a/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Record.h
b/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Record.h
index 2dcb5ea2555c5..98c2b63489f41 100644
--- a/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Record.h
+++ b/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Record.h
@@ -132,7 +132,7 @@ struct RecordedAST {
std::vector Roots;
};
-/// Recorded main-file preprocessor events relevant to include-cleaner.
+/// Recorded preprocessor events relevant to include-cleaner.
///
/// This doesn't include facts that we record globally for the whole TU, even
/// when they occur in the main file (e.g. IWYU pragmas).
@@ -140,7 +140,8 @@ struct RecordedPP {
/// The callback (when installed into clang) tracks macros/includes in this.
std::unique_ptr record(const Preprocessor &PP);
- /// Describes where macros were used in the main file.
+ /// Describes where macros were used in the main file or in files directly
+ /// included by the main file.
std::vector MacroReferences;
/// The include directives seen in the main file.
diff --git a/clang-tools-extra/include-cleaner/lib/Record.cpp
b/clang-tools-extra/include-cleaner/lib/Record.cpp
index 0284d6842e2d2..a19fcf525919d 100644
--- a/clang-tools-extra/include-cleaner/lib/Record.cpp
+++ b/clang-tools-extra/include-cleaner/lib/Record.cpp
@@ -84,13 +84,13 @@ class PPRecorder : public PPCallbacks {
void MacroExpands(const Token &MacroName, const MacroDefinition &MD,
SourceRange Range, const MacroArgs *Args) override {
-if (!Active)
+if (!shouldRecordMacroRef(MacroName.getLocation()))
return;
recordMacroRef(MacroName, *MD.getMacroInfo());
}
void MacroDefined(const Token &MacroName, const MacroDirective *MD) override
{
-if (!Active)
+if (!shouldRecordMacroRef(MacroName.getLocation()))
return;
const auto *MI = MD->getMacroInfo();
@@ -110,7 +110,7 @@ class PPRecorder : public PPCallbacks {
void MacroUndefined(const Token &MacroName, const MacroDefinition &MD,
const MacroDirective *) override {
-if (!Active)
+if (!shouldRecordMacroRef(MacroName.getLocation()))
return;
if (const auto *MI = MD.getMacroInfo())
recordMacroRef(MacroName, *MI);
@@ -118,7 +118,7 @@ class PPRecorder : public PPCallbacks {
void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
const MacroDefinition &MD) override {
-if (!Active)
+if (!shouldRecordMacroRef(MacroNameTok.getLocation()))
return;
if (const auto *MI = MD.getMacroInfo())
recordMacroRef(MacroNameTok, *MI, RefType::Ambiguous);
@@ -126,7 +126,7 @@ class PPRecorder : public PPCallbacks {
void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
const MacroDefinition &MD) override {
-if (!Active)
+if (!shouldRecordMacroRef(MacroNameTok.getLocation()))
return;
if (const auto *MI = MD.getMacroInfo())
recordMacroRef(MacroNameTok, *MI, RefType::Ambiguous);
@@ -136,14 +136,14 @@ class PPRecorder : public PPCallbacks {
using PPCallbacks::Elifndef;
void Elifdef(SourceLocation Loc, const Token &MacroNameTok,
const MacroDefinition &MD) override {
-if (!Active)
+if (!shouldRecordMacroRef(MacroNameTok.getLocation()))
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)
+if (!shouldRecordMacroRef(MacroNameTok.getLocation()))
return;
if (const auto *MI = MD.getMacroInfo())
recordMacroRef(MacroNameTok, *MI, RefType::Ambiguous);
@@ -151,13 +151,23 @@ class PPRecorder : public PPCallbacks {
void Defined(const Token &MacroNameTok, const MacroDefinition &MD,
SourceRange Range) override {
-if (!Active)
+if (!shouldRecordMacroRef(MacroNameTok.getLocation()))
return;
if (const auto *MI = MD.getMacroInfo())
recordMacroRef(MacroNameTok, *MI, RefType::Ambiguous);
}
private:
+ bool shouldRecordMacroRef(SourceLocation Loc) const {
+const SourceLocation ExpandedLoc = SM.getExpansionLoc(Loc);
+const FileID FID = SM.getFileID(ExpandedLoc);
+if (FID == SM.getMainFileID())
+ return true;
+const SourceLocation IncludeLoc = SM.getIncludeLoc(FID);
+retur
[clang-tools-extra] [include-cleaner][NFC] record macro refs in direct includes (PR #189366)
https://github.com/unterumarmung created
https://github.com/llvm/llvm-project/pull/189366
None
>From 9aecffdea019a2ad2a44356066a55a4ea1aa6e63 Mon Sep 17 00:00:00 2001
From: Daniil Dudkin
Date: Mon, 30 Mar 2026 14:25:56 +0300
Subject: [PATCH] [include-cleaner][NFC] record macro refs in direct includes
---
.../include/clang-include-cleaner/Record.h| 5 +-
.../include-cleaner/lib/Record.cpp| 26 ---
.../include-cleaner/unittests/RecordTest.cpp | 71 ++-
3 files changed, 90 insertions(+), 12 deletions(-)
diff --git
a/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Record.h
b/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Record.h
index 2dcb5ea2555c5..98c2b63489f41 100644
--- a/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Record.h
+++ b/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Record.h
@@ -132,7 +132,7 @@ struct RecordedAST {
std::vector Roots;
};
-/// Recorded main-file preprocessor events relevant to include-cleaner.
+/// Recorded preprocessor events relevant to include-cleaner.
///
/// This doesn't include facts that we record globally for the whole TU, even
/// when they occur in the main file (e.g. IWYU pragmas).
@@ -140,7 +140,8 @@ struct RecordedPP {
/// The callback (when installed into clang) tracks macros/includes in this.
std::unique_ptr record(const Preprocessor &PP);
- /// Describes where macros were used in the main file.
+ /// Describes where macros were used in the main file or in files directly
+ /// included by the main file.
std::vector MacroReferences;
/// The include directives seen in the main file.
diff --git a/clang-tools-extra/include-cleaner/lib/Record.cpp
b/clang-tools-extra/include-cleaner/lib/Record.cpp
index 0284d6842e2d2..a19fcf525919d 100644
--- a/clang-tools-extra/include-cleaner/lib/Record.cpp
+++ b/clang-tools-extra/include-cleaner/lib/Record.cpp
@@ -84,13 +84,13 @@ class PPRecorder : public PPCallbacks {
void MacroExpands(const Token &MacroName, const MacroDefinition &MD,
SourceRange Range, const MacroArgs *Args) override {
-if (!Active)
+if (!shouldRecordMacroRef(MacroName.getLocation()))
return;
recordMacroRef(MacroName, *MD.getMacroInfo());
}
void MacroDefined(const Token &MacroName, const MacroDirective *MD) override
{
-if (!Active)
+if (!shouldRecordMacroRef(MacroName.getLocation()))
return;
const auto *MI = MD->getMacroInfo();
@@ -110,7 +110,7 @@ class PPRecorder : public PPCallbacks {
void MacroUndefined(const Token &MacroName, const MacroDefinition &MD,
const MacroDirective *) override {
-if (!Active)
+if (!shouldRecordMacroRef(MacroName.getLocation()))
return;
if (const auto *MI = MD.getMacroInfo())
recordMacroRef(MacroName, *MI);
@@ -118,7 +118,7 @@ class PPRecorder : public PPCallbacks {
void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
const MacroDefinition &MD) override {
-if (!Active)
+if (!shouldRecordMacroRef(MacroNameTok.getLocation()))
return;
if (const auto *MI = MD.getMacroInfo())
recordMacroRef(MacroNameTok, *MI, RefType::Ambiguous);
@@ -126,7 +126,7 @@ class PPRecorder : public PPCallbacks {
void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
const MacroDefinition &MD) override {
-if (!Active)
+if (!shouldRecordMacroRef(MacroNameTok.getLocation()))
return;
if (const auto *MI = MD.getMacroInfo())
recordMacroRef(MacroNameTok, *MI, RefType::Ambiguous);
@@ -136,14 +136,14 @@ class PPRecorder : public PPCallbacks {
using PPCallbacks::Elifndef;
void Elifdef(SourceLocation Loc, const Token &MacroNameTok,
const MacroDefinition &MD) override {
-if (!Active)
+if (!shouldRecordMacroRef(MacroNameTok.getLocation()))
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)
+if (!shouldRecordMacroRef(MacroNameTok.getLocation()))
return;
if (const auto *MI = MD.getMacroInfo())
recordMacroRef(MacroNameTok, *MI, RefType::Ambiguous);
@@ -151,13 +151,23 @@ class PPRecorder : public PPCallbacks {
void Defined(const Token &MacroNameTok, const MacroDefinition &MD,
SourceRange Range) override {
-if (!Active)
+if (!shouldRecordMacroRef(MacroNameTok.getLocation()))
return;
if (const auto *MI = MD.getMacroInfo())
recordMacroRef(MacroNameTok, *MI, RefType::Ambiguous);
}
private:
+ bool shouldRecordMacroRef(SourceLocation Loc) const {
+const SourceLocation ExpandedLoc = SM.getExpansionLoc(Loc);
+const FileID FID = SM.getFileID(ExpandedLoc);
+if (FID == SM.getMainFileID())
+
