diff --git a/clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.cpp index 843368e723f1f..c8e88b1209ded 100644 --- a/clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.cpp @@ -40,8 +40,9 @@ SuspiciousIncludeCheck::SuspiciousIncludeCheck(StringRef Name, ClangTidyContext *Context) : ClangTidyCheck(Name, Context), HeaderFileExtensions(Context->getHeaderFileExtensions()), - ImplementationFileExtensions(Context->getImplementationFileExtensions()) { -} + ImplementationFileExtensions(Context->getImplementationFileExtensions()), + IgnoredRegexString(Options.get("IgnoredRegex")), + IgnoredRegex(IgnoredRegexString.value_or(StringRef{})) {} void SuspiciousIncludeCheck::registerPPCallbacks( const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) { @@ -49,6 +50,11 @@ void SuspiciousIncludeCheck::registerPPCallbacks( ::std::make_unique(*this, SM, PP)); } +void SuspiciousIncludeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { + if (IgnoredRegexString.has_value()) + Options.store(Opts, "IgnoredRegex", IgnoredRegexString.value()); +} + void SuspiciousIncludePPCallbacks::InclusionDirective( SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File, @@ -57,6 +63,11 @@ void SuspiciousIncludePPCallbacks::InclusionDirective( if (IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import) return; + if (Check.IgnoredRegexString.has_value() && + Check.IgnoredRegex.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..05c32b26af046 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,12 @@ 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 IgnoredRegexString; + llvm::Regex IgnoredRegex; }; } // namespace clang::tidy::bugprone diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 8c7426c33d13b..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`` 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..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 @@ -14,3 +14,11 @@ 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 string. 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..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 -- -- -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 @@ -22,3 +24,6 @@ // CHECK-MESSAGES: [[@LINE+1]]:14: warning: suspicious #include of file with '.cxx' extension # include + +// CHECK-MESSAGES-NOT: warning: +#include "moc_foo.cpp"