Skip to content

Commit 3de5d8e

Browse files
authored
[include-cleaner] Add --only-headers flag, opposite of --ignore-headers (#78714)
1 parent ff1cde5 commit 3de5d8e

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

clang-tools-extra/include-cleaner/test/tool.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ int x = foo();
2222
// IGNORE2-NOT: - "foobar.h"
2323
// IGNORE2: + "foo.h"
2424

25+
// RUN: clang-include-cleaner -print=changes %s --only-headers="foo\.h" -- -I%S/Inputs/ | FileCheck --match-full-lines --allow-empty --check-prefix=ONLY %s
26+
// ONLY-NOT: - "foobar.h"
27+
// ONLY: + "foo.h"
28+
2529
// RUN: clang-include-cleaner -print %s -- -I%S/Inputs/ | FileCheck --match-full-lines --check-prefix=PRINT %s
2630
// PRINT: #include "foo.h"
2731
// PRINT-NOT: {{^}}#include "foobar.h"{{$}}

clang-tools-extra/include-cleaner/tool/IncludeCleaner.cpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ cl::opt<std::string> HTMLReportPath{
5757
cl::cat(IncludeCleaner),
5858
};
5959

60+
cl::opt<std::string> OnlyHeaders{
61+
"only-headers",
62+
cl::desc("A comma-separated list of regexes to match against suffix of a "
63+
"header. Only headers that match will be analyzed."),
64+
cl::init(""),
65+
cl::cat(IncludeCleaner),
66+
};
67+
6068
cl::opt<std::string> IgnoreHeaders{
6169
"ignore-headers",
6270
cl::desc("A comma-separated list of regexes to match against suffix of a "
@@ -221,11 +229,12 @@ class ActionFactory : public tooling::FrontendActionFactory {
221229
llvm::StringMap<std::string> EditedFiles;
222230
};
223231

224-
std::function<bool(llvm::StringRef)> headerFilter() {
232+
// Compiles a regex list into a function that return true if any match a header.
233+
// Prints and returns nullptr if any regexes are invalid.
234+
std::function<bool(llvm::StringRef)> matchesAny(llvm::StringRef RegexFlag) {
225235
auto FilterRegs = std::make_shared<std::vector<llvm::Regex>>();
226-
227236
llvm::SmallVector<llvm::StringRef> Headers;
228-
llvm::StringRef(IgnoreHeaders).split(Headers, ',', -1, /*KeepEmpty=*/false);
237+
RegexFlag.split(Headers, ',', -1, /*KeepEmpty=*/false);
229238
for (auto HeaderPattern : Headers) {
230239
std::string AnchoredPattern = "(" + HeaderPattern.str() + ")$";
231240
llvm::Regex CompiledRegex(AnchoredPattern);
@@ -246,6 +255,21 @@ std::function<bool(llvm::StringRef)> headerFilter() {
246255
};
247256
}
248257

258+
std::function<bool(llvm::StringRef)> headerFilter() {
259+
auto OnlyMatches = matchesAny(OnlyHeaders);
260+
auto IgnoreMatches = matchesAny(IgnoreHeaders);
261+
if (!OnlyMatches || !IgnoreMatches)
262+
return nullptr;
263+
264+
return [OnlyMatches, IgnoreMatches](llvm::StringRef Header) {
265+
if (OnlyHeaders.getNumOccurrences() && !OnlyMatches(Header))
266+
return true;
267+
if (IgnoreHeaders.getNumOccurrences() && IgnoreMatches(Header))
268+
return true;
269+
return false;
270+
};
271+
}
272+
249273
} // namespace
250274
} // namespace include_cleaner
251275
} // namespace clang

0 commit comments

Comments
 (0)