[clang-tools-extra] [clang-tidy] Add IgnoredRegex to 'bugprone-suspicious-include' (PR #160958)
Björn Schäpers via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 26 15:23:43 PDT 2025
https://github.com/HazardyKnusperkeks created https://github.com/llvm/llvm-project/pull/160958
The use case is shown in the test: Qt's moc output not to trigger a warning.
>From 47fcb11c78f88f4318253c2a78daccbb413d6df2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= <bjoern at hazardy.de>
Date: Fri, 26 Sep 2025 23:24:56 +0200
Subject: [PATCH] [clang-tidy] Add IgnoredRegex to
'bugprone-suspicious-include'
The use case is shown in the test: Qt's moc output not to trigger a
warning.
---
.../clang-tidy/bugprone/SuspiciousIncludeCheck.cpp | 13 +++++++++++--
.../clang-tidy/bugprone/SuspiciousIncludeCheck.h | 3 ++-
clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++
.../checks/bugprone/suspicious-include.rst | 9 +++++++++
.../clang-tidy/checkers/Inputs/Headers/moc_foo.cpp | 0
.../checkers/bugprone/suspicious-include.cpp | 5 ++++-
6 files changed, 30 insertions(+), 4 deletions(-)
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/moc_foo.cpp
diff --git a/clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.cpp
index 843368e723f1f..d4fec8049f72f 100644
--- a/clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.cpp
@@ -40,8 +40,8 @@ SuspiciousIncludeCheck::SuspiciousIncludeCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
HeaderFileExtensions(Context->getHeaderFileExtensions()),
- ImplementationFileExtensions(Context->getImplementationFileExtensions()) {
-}
+ ImplementationFileExtensions(Context->getImplementationFileExtensions()),
+ IgnoredRegex(Options.get("IgnoredRegex")) {}
void SuspiciousIncludeCheck::registerPPCallbacks(
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
@@ -49,6 +49,11 @@ void SuspiciousIncludeCheck::registerPPCallbacks(
::std::make_unique<SuspiciousIncludePPCallbacks>(*this, SM, PP));
}
+void SuspiciousIncludeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
+ if (IgnoredRegex.has_value())
+ Options.store(Opts, "IgnoredRegex", IgnoredRegex.value());
+}
+
void SuspiciousIncludePPCallbacks::InclusionDirective(
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File,
@@ -57,6 +62,10 @@ void SuspiciousIncludePPCallbacks::InclusionDirective(
if (IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import)
return;
+ if (Check.IgnoredRegex.has_value())
+ if (llvm::Regex Regex{Check.IgnoredRegex.value()}; Regex.match(FileName))
+ return;
+
SourceLocation DiagLoc = FilenameRange.getBegin().getLocWithOffset(1);
const std::optional<StringRef> IFE =
diff --git a/clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.h b/clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.h
index 3aa9491ef0e3b..f4435e3e2041a 100644
--- a/clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.h
+++ b/clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.h
@@ -10,7 +10,6 @@
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_SUSPICIOUSINCLUDECHECK_H
#include "../ClangTidyCheck.h"
-#include "../utils/FileExtensionsUtils.h"
namespace clang::tidy::bugprone {
@@ -28,9 +27,11 @@ class SuspiciousIncludeCheck : public ClangTidyCheck {
SuspiciousIncludeCheck(StringRef Name, ClangTidyContext *Context);
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
Preprocessor *ModuleExpanderPP) override;
+ void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
FileExtensionsSet HeaderFileExtensions;
FileExtensionsSet ImplementationFileExtensions;
+ std::optional<StringRef> IgnoredRegex;
};
} // namespace clang::tidy::bugprone
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 8c7426c33d13b..2e73bf7e42748 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -364,6 +364,10 @@ Changes in existing checks
<clang-tidy/checks/readability/uppercase-literal-suffix>` check to recognize
literal suffixes added in C++23 and C23.
+- Improved :doc:`bugprone-suspicious-include
+ <clang-tidy/checks/bugprone/suspicious-include>` check by adding
+ ``IgnoredRegex`` option.
+
Removed checks
^^^^^^^^^^^^^^
diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/suspicious-include.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/suspicious-include.rst
index 669654fdd435f..a8d36be70f673 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/suspicious-include.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/suspicious-include.rst
@@ -14,3 +14,12 @@ Examples:
#include "Pterodactyl.h" // OK, .h files tend not to have definitions.
#include "Velociraptor.cpp" // Warning, filename is suspicious.
#include_next <stdio.c> // Warning, filename is suspicious.
+
+Options
+-------
+
+.. option:: IgnoredRegex
+
+ A regular expression for the file name to be ignored by the check. Default
+ is empty.
+
diff --git a/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/moc_foo.cpp b/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/moc_foo.cpp
new file mode 100644
index 0000000000000..e69de29bb2d1d
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/suspicious-include.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/suspicious-include.cpp
index 969d0bfdf7ed0..bd81e1aeb8789 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/suspicious-include.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/suspicious-include.cpp
@@ -1,4 +1,4 @@
-// RUN: %check_clang_tidy %s bugprone-suspicious-include %t -- -- -isystem %clang_tidy_headers -fmodules
+// RUN: %check_clang_tidy %s bugprone-suspicious-include %t -- -config="{CheckOptions: {bugprone-suspicious-include.IgnoredRegex: 'moc_.*'}"} -- -isystem %clang_tidy_headers -fmodules
// clang-format off
@@ -22,3 +22,6 @@
// CHECK-MESSAGES: [[@LINE+1]]:14: warning: suspicious #include of file with '.cxx' extension
# include <c.cxx>
+
+// CHECK-MESSAGES-NOT: warning:
+#include "moc_foo.cpp"
More information about the cfe-commits
mailing list