-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang-tidy] Add IgnoredRegex to 'bugprone-suspicious-include' #160958
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
The use case is shown in the test: Qt's moc output not to trigger a warning.
@llvm/pr-subscribers-clang-tools-extra Author: Björn Schäpers (HazardyKnusperkeks) ChangesThe use case is shown in the test: Qt's moc output not to trigger a warning. Full diff: https://github.com/llvm/llvm-project/pull/160958.diff 6 Files Affected:
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"
|
@llvm/pr-subscribers-clang-tidy Author: Björn Schäpers (HazardyKnusperkeks) ChangesThe use case is shown in the test: Qt's moc output not to trigger a warning. Full diff: https://github.com/llvm/llvm-project/pull/160958.diff 6 Files Affected:
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"
|
clang-tools-extra/docs/clang-tidy/checks/bugprone/suspicious-include.rst
Outdated
Show resolved
Hide resolved
clang-tools-extra/docs/clang-tidy/checks/bugprone/suspicious-include.rst
Outdated
Show resolved
Hide resolved
clang-tools-extra/docs/clang-tidy/checks/bugprone/suspicious-include.rst
Outdated
Show resolved
Hide resolved
…nclude.rst Co-authored-by: EugeneZelenko <[email protected]>
clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.cpp
Outdated
Show resolved
Hide resolved
clang-tools-extra/test/clang-tidy/checkers/bugprone/suspicious-include.cpp
Outdated
Show resolved
Hide resolved
|
||
FileExtensionsSet HeaderFileExtensions; | ||
FileExtensionsSet ImplementationFileExtensions; | ||
std::optional<StringRef> IgnoredRegexString; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make all data members private.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You said that before, I answered with a question, and got no answer. @vbvictor came with an answer and I went with that.
So if you can't react on my questions/answers, I'll certainly will not do anything.
The use case is shown in the test: Qt's moc output not to trigger a warning.