From 47fcb11c78f88f4318253c2a78daccbb413d6df2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= Date: Fri, 26 Sep 2025 23:24:56 +0200 Subject: [PATCH 1/5] [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(*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 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 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 ` check to recognize literal suffixes added in C++23 and C23. +- Improved :doc:`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 // 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 + +// CHECK-MESSAGES-NOT: warning: +#include "moc_foo.cpp" From 4d87bc6ff6feb5b27e4716b5e55f19c9f7dfa19f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= Date: Sat, 27 Sep 2025 22:04:10 +0200 Subject: [PATCH 2/5] Adapt review comments --- clang-tools-extra/docs/ReleaseNotes.rst | 8 ++++---- .../clang-tidy/checks/bugprone/suspicious-include.rst | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 2e73bf7e42748..c0104ccaa82a0 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -259,6 +259,10 @@ Changes in existing checks ` check by fixing a crash on ``sizeof`` of an array of dependent type. +- Improved :doc:`bugprone-suspicious-include + ` check by adding + `IgnoredRegex` option. + - Improved :doc:`bugprone-tagged-union-member-count ` by fixing a false positive when enums or unions from system header files or the ``std`` @@ -364,10 +368,6 @@ Changes in existing checks ` check to recognize literal suffixes added in C++23 and C23. -- Improved :doc:`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 a8d36be70f673..5457e8fac35f7 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 @@ -21,5 +21,5 @@ Options .. option:: IgnoredRegex A regular expression for the file name to be ignored by the check. Default - is empty. + is no regular expression. From ef6de3f6abc92a23d8de92241eab1d993045e403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= Date: Sun, 28 Sep 2025 22:25:53 +0200 Subject: [PATCH 3/5] Adapt documentation --- .../docs/clang-tidy/checks/bugprone/suspicious-include.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 5457e8fac35f7..cf3df29f9c026 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 @@ -21,5 +21,5 @@ Options .. option:: IgnoredRegex A regular expression for the file name to be ignored by the check. Default - is no regular expression. + is empty string. From 46bf9cb1a4c3ba9684b58466137043cf2aec67a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= Date: Mon, 29 Sep 2025 00:06:46 +0200 Subject: [PATCH 4/5] Update clang-tools-extra/docs/clang-tidy/checks/bugprone/suspicious-include.rst Co-authored-by: EugeneZelenko --- .../docs/clang-tidy/checks/bugprone/suspicious-include.rst | 1 - 1 file changed, 1 deletion(-) 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 cf3df29f9c026..4fbfa259f3d09 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 @@ -22,4 +22,3 @@ Options A regular expression for the file name to be ignored by the check. Default is empty string. - From 50ff6d6913ade490a3050fd0e2ff8e0c7bb92fd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= Date: Sun, 5 Oct 2025 00:07:06 +0200 Subject: [PATCH 5/5] Incorporate Review Comments --- .../clang-tidy/bugprone/SuspiciousIncludeCheck.cpp | 14 ++++++++------ .../clang-tidy/bugprone/SuspiciousIncludeCheck.h | 3 ++- .../checkers/bugprone/suspicious-include.cpp | 4 +++- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.cpp index d4fec8049f72f..c8e88b1209ded 100644 --- a/clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.cpp @@ -41,7 +41,8 @@ SuspiciousIncludeCheck::SuspiciousIncludeCheck(StringRef Name, : ClangTidyCheck(Name, Context), HeaderFileExtensions(Context->getHeaderFileExtensions()), ImplementationFileExtensions(Context->getImplementationFileExtensions()), - IgnoredRegex(Options.get("IgnoredRegex")) {} + IgnoredRegexString(Options.get("IgnoredRegex")), + IgnoredRegex(IgnoredRegexString.value_or(StringRef{})) {} void SuspiciousIncludeCheck::registerPPCallbacks( const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) { @@ -50,8 +51,8 @@ void SuspiciousIncludeCheck::registerPPCallbacks( } void SuspiciousIncludeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { - if (IgnoredRegex.has_value()) - Options.store(Opts, "IgnoredRegex", IgnoredRegex.value()); + if (IgnoredRegexString.has_value()) + Options.store(Opts, "IgnoredRegex", IgnoredRegexString.value()); } void SuspiciousIncludePPCallbacks::InclusionDirective( @@ -62,9 +63,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; + if (Check.IgnoredRegexString.has_value() && + Check.IgnoredRegex.match(FileName)) { + return; + } SourceLocation DiagLoc = FilenameRange.getBegin().getLocWithOffset(1); diff --git a/clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.h b/clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.h index f4435e3e2041a..05c32b26af046 100644 --- a/clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.h +++ b/clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.h @@ -31,7 +31,8 @@ class SuspiciousIncludeCheck : public ClangTidyCheck { FileExtensionsSet HeaderFileExtensions; FileExtensionsSet ImplementationFileExtensions; - std::optional IgnoredRegex; + std::optional IgnoredRegexString; + llvm::Regex IgnoredRegex; }; } // namespace clang::tidy::bugprone 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 bd81e1aeb8789..4f2acbc2fe7c9 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,6 @@ -// RUN: %check_clang_tidy %s bugprone-suspicious-include %t -- -config="{CheckOptions: {bugprone-suspicious-include.IgnoredRegex: 'moc_.*'}"} -- -isystem %clang_tidy_headers -fmodules +// RUN: %check_clang_tidy %s bugprone-suspicious-include %t -- \ +// RUN: -config="{CheckOptions: {bugprone-suspicious-include.IgnoredRegex: 'moc_.*'}"} -- \ +// RUN: -isystem %clang_tidy_headers -fmodules // clang-format off