Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,20 @@ 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) {
PP->addPPCallbacks(
::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,
Expand All @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ Changes in existing checks
<clang-tidy/checks/bugprone/sizeof-expression>` check by fixing
a crash on ``sizeof`` of an array of dependent type.

- Improved :doc:`bugprone-suspicious-include
<clang-tidy/checks/bugprone/suspicious-include>` check by adding
`IgnoredRegex` option.

- Improved :doc:`bugprone-tagged-union-member-count
<clang-tidy/checks/bugprone/tagged-union-member-count>` by fixing a false
positive when enums or unions from system header files or the ``std``
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 string.

Empty file.
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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"
Loading