Skip to content

Commit 47fcb11

Browse files
[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.
1 parent 1e302e9 commit 47fcb11

File tree

6 files changed

+30
-4
lines changed

6 files changed

+30
-4
lines changed

clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,20 @@ SuspiciousIncludeCheck::SuspiciousIncludeCheck(StringRef Name,
4040
ClangTidyContext *Context)
4141
: ClangTidyCheck(Name, Context),
4242
HeaderFileExtensions(Context->getHeaderFileExtensions()),
43-
ImplementationFileExtensions(Context->getImplementationFileExtensions()) {
44-
}
43+
ImplementationFileExtensions(Context->getImplementationFileExtensions()),
44+
IgnoredRegex(Options.get("IgnoredRegex")) {}
4545

4646
void SuspiciousIncludeCheck::registerPPCallbacks(
4747
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
4848
PP->addPPCallbacks(
4949
::std::make_unique<SuspiciousIncludePPCallbacks>(*this, SM, PP));
5050
}
5151

52+
void SuspiciousIncludeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
53+
if (IgnoredRegex.has_value())
54+
Options.store(Opts, "IgnoredRegex", IgnoredRegex.value());
55+
}
56+
5257
void SuspiciousIncludePPCallbacks::InclusionDirective(
5358
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
5459
bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File,
@@ -57,6 +62,10 @@ void SuspiciousIncludePPCallbacks::InclusionDirective(
5762
if (IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import)
5863
return;
5964

65+
if (Check.IgnoredRegex.has_value())
66+
if (llvm::Regex Regex{Check.IgnoredRegex.value()}; Regex.match(FileName))
67+
return;
68+
6069
SourceLocation DiagLoc = FilenameRange.getBegin().getLocWithOffset(1);
6170

6271
const std::optional<StringRef> IFE =

clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_SUSPICIOUSINCLUDECHECK_H
1111

1212
#include "../ClangTidyCheck.h"
13-
#include "../utils/FileExtensionsUtils.h"
1413

1514
namespace clang::tidy::bugprone {
1615

@@ -28,9 +27,11 @@ class SuspiciousIncludeCheck : public ClangTidyCheck {
2827
SuspiciousIncludeCheck(StringRef Name, ClangTidyContext *Context);
2928
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
3029
Preprocessor *ModuleExpanderPP) override;
30+
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
3131

3232
FileExtensionsSet HeaderFileExtensions;
3333
FileExtensionsSet ImplementationFileExtensions;
34+
std::optional<StringRef> IgnoredRegex;
3435
};
3536

3637
} // namespace clang::tidy::bugprone

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ Changes in existing checks
364364
<clang-tidy/checks/readability/uppercase-literal-suffix>` check to recognize
365365
literal suffixes added in C++23 and C23.
366366

367+
- Improved :doc:`bugprone-suspicious-include
368+
<clang-tidy/checks/bugprone/suspicious-include>` check by adding
369+
``IgnoredRegex`` option.
370+
367371
Removed checks
368372
^^^^^^^^^^^^^^
369373

clang-tools-extra/docs/clang-tidy/checks/bugprone/suspicious-include.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,12 @@ Examples:
1414
#include "Pterodactyl.h" // OK, .h files tend not to have definitions.
1515
#include "Velociraptor.cpp" // Warning, filename is suspicious.
1616
#include_next <stdio.c> // Warning, filename is suspicious.
17+
18+
Options
19+
-------
20+
21+
.. option:: IgnoredRegex
22+
23+
A regular expression for the file name to be ignored by the check. Default
24+
is empty.
25+

clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/moc_foo.cpp

Whitespace-only changes.

clang-tools-extra/test/clang-tidy/checkers/bugprone/suspicious-include.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %check_clang_tidy %s bugprone-suspicious-include %t -- -- -isystem %clang_tidy_headers -fmodules
1+
// RUN: %check_clang_tidy %s bugprone-suspicious-include %t -- -config="{CheckOptions: {bugprone-suspicious-include.IgnoredRegex: 'moc_.*'}"} -- -isystem %clang_tidy_headers -fmodules
22

33
// clang-format off
44

@@ -22,3 +22,6 @@
2222

2323
// CHECK-MESSAGES: [[@LINE+1]]:14: warning: suspicious #include of file with '.cxx' extension
2424
# include <c.cxx>
25+
26+
// CHECK-MESSAGES-NOT: warning:
27+
#include "moc_foo.cpp"

0 commit comments

Comments
 (0)