[PATCH] D153340: [include-cleaner] Add an IgnoreHeaders flag to the command-line tool.
Haojian Wu via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 20 06:36:57 PDT 2023
hokein created this revision.
hokein added a reviewer: kadircet.
Herald added a project: All.
hokein requested review of this revision.
Herald added a project: clang-tools-extra.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D153340
Files:
clang-tools-extra/include-cleaner/test/tool.cpp
clang-tools-extra/include-cleaner/tool/IncludeCleaner.cpp
Index: clang-tools-extra/include-cleaner/tool/IncludeCleaner.cpp
===================================================================
--- clang-tools-extra/include-cleaner/tool/IncludeCleaner.cpp
+++ clang-tools-extra/include-cleaner/tool/IncludeCleaner.cpp
@@ -14,8 +14,10 @@
#include "clang/Lex/Preprocessor.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
+#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/raw_ostream.h"
@@ -47,6 +49,13 @@
cl::cat(IncludeCleaner),
};
+cl::opt<std::string> IgnoreHeaders{
+ "ignore-headers",
+ cl::desc("A comma-separated list of headers to ignore."),
+ cl::init(""),
+ cl::cat(IncludeCleaner),
+};
+
enum class PrintStyle { Changes, Final };
cl::opt<PrintStyle> Print{
"print",
@@ -117,6 +126,27 @@
return;
}
+ std::vector<llvm::Regex> HeaderFilters;
+ llvm::SmallVector<StringRef> Headers;
+ llvm::StringRef(IgnoreHeaders).split(Headers, ',', -1, /*KeepEmpty*/ false);
+ for (auto HeaderPattern : Headers) {
+ std::string AnchoredPattern = "(" + HeaderPattern.str() + ")$";
+ llvm::Regex CompiledRegex(AnchoredPattern);
+ std::string RegexError;
+ if (!CompiledRegex.isValid(RegexError)) {
+ llvm::errs() << llvm::formatv("Invalid regular expression '{0}': {1}\n",
+ HeaderPattern, RegexError);
+ return;
+ }
+ HeaderFilters.push_back(std::move(CompiledRegex));
+ }
+ auto Filter = [&HeaderFilters](llvm::StringRef Path) {
+ for (const auto &F : HeaderFilters)
+ if (F.match(Path))
+ return true;
+ return false;
+ };
+
if (!HTMLReportPath.empty())
writeHTML();
@@ -137,10 +167,16 @@
if (Print.getNumOccurrences()) {
switch (Print) {
case PrintStyle::Changes:
- for (const Include *I : Results.Unused)
+ for (const Include *I : Results.Unused) {
+ if (Filter(I->Resolved->tryGetRealPathName()))
+ continue;
llvm::outs() << "- " << I->quote() << " @Line:" << I->Line << "\n";
- for (const auto &I : Results.Missing)
+ }
+ for (const auto &I : Results.Missing) {
+ if (Filter(llvm::StringRef(I).trim("<>\"")))
+ continue;
llvm::outs() << "+ " << I << "\n";
+ }
break;
case PrintStyle::Final:
llvm::outs() << Final;
Index: clang-tools-extra/include-cleaner/test/tool.cpp
===================================================================
--- clang-tools-extra/include-cleaner/test/tool.cpp
+++ clang-tools-extra/include-cleaner/test/tool.cpp
@@ -14,6 +14,10 @@
// REMOVE: - "foobar.h"
// REMOVE-NOT: + "foo.h"
+// RUN: clang-include-cleaner -print=changes %s --ignore-headers="foobar\.h,foo\.h" -- -I%S/Inputs/ | FileCheck --match-full-lines --allow-empty --check-prefix=IGNORE %s
+// IGNORE-NOT: - "foobar.h"
+// IGNORE-NOT: + "foo.h"
+
// RUN: clang-include-cleaner -print %s -- -I%S/Inputs/ | FileCheck --match-full-lines --check-prefix=PRINT %s
// PRINT: #include "foo.h"
// PRINT-NOT: {{^}}#include "foobar.h"{{$}}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153340.532894.patch
Type: text/x-patch
Size: 3316 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230620/adb91083/attachment-0001.bin>
More information about the cfe-commits
mailing list