Skip to content

Commit 799b97c

Browse files
HazardyKnusperkeksDharuniRAcharya
authored andcommitted
[clang-tidy] Add IgnoredRegex to 'bugprone-suspicious-include' (llvm#160958)
The use case is shown in the test: Qt's moc output not to trigger a warning.
1 parent eefd072 commit 799b97c

File tree

6 files changed

+32
-4
lines changed

6 files changed

+32
-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,21 @@ 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+
IgnoredRegexString(Options.get("IgnoredRegex").value_or(StringRef{})),
45+
IgnoredRegex(IgnoredRegexString) {}
4546

4647
void SuspiciousIncludeCheck::registerPPCallbacks(
4748
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
4849
PP->addPPCallbacks(
4950
::std::make_unique<SuspiciousIncludePPCallbacks>(*this, SM, PP));
5051
}
5152

53+
void SuspiciousIncludeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
54+
if (!IgnoredRegexString.empty())
55+
Options.store(Opts, "IgnoredRegex", IgnoredRegexString);
56+
}
57+
5258
void SuspiciousIncludePPCallbacks::InclusionDirective(
5359
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
5460
bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File,
@@ -57,6 +63,9 @@ void SuspiciousIncludePPCallbacks::InclusionDirective(
5763
if (IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import)
5864
return;
5965

66+
if (!Check.IgnoredRegexString.empty() && Check.IgnoredRegex.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: 3 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,12 @@ 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+
StringRef IgnoredRegexString;
35+
llvm::Regex IgnoredRegex;
3436
};
3537

3638
} // namespace clang::tidy::bugprone

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,10 @@ Changes in existing checks
286286
<clang-tidy/checks/bugprone/sizeof-expression>` check by fixing
287287
a crash on ``sizeof`` of an array of dependent type.
288288

289+
- Improved :doc:`bugprone-suspicious-include
290+
<clang-tidy/checks/bugprone/suspicious-include>` check by adding
291+
`IgnoredRegex` option.
292+
289293
- Improved :doc:`bugprone-tagged-union-member-count
290294
<clang-tidy/checks/bugprone/tagged-union-member-count>` by fixing a false
291295
positive when enums or unions from system header files or the ``std``

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@ 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 string.

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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
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 -- \
2+
// RUN: -config="{CheckOptions: {bugprone-suspicious-include.IgnoredRegex: 'moc_.*'}"} -- \
3+
// RUN: -isystem %clang_tidy_headers -fmodules
24

35
// clang-format off
46

@@ -22,3 +24,6 @@
2224

2325
// CHECK-MESSAGES: [[@LINE+1]]:14: warning: suspicious #include of file with '.cxx' extension
2426
# include <c.cxx>
27+
28+
// CHECK-MESSAGES-NOT: warning:
29+
#include "moc_foo.cpp"

0 commit comments

Comments
 (0)