11#include "clang/AST/ASTContext.h"
12#include "clang/Lex/Preprocessor.h"
18class SuspiciousIncludePPCallbacks :
public PPCallbacks {
20 explicit SuspiciousIncludePPCallbacks(SuspiciousIncludeCheck &Check,
21 const SourceManager &SM,
23 : Check(Check), PP(PP) {}
25 void InclusionDirective(SourceLocation HashLoc,
const Token &IncludeTok,
26 StringRef FileName,
bool IsAngled,
27 CharSourceRange FilenameRange,
28 OptionalFileEntryRef File, StringRef SearchPath,
29 StringRef RelativePath,
const Module *SuggestedModule,
31 SrcMgr::CharacteristicKind FileType)
override;
34 SuspiciousIncludeCheck &Check;
46 const SourceManager &SM, Preprocessor *
PP, Preprocessor *ModuleExpanderPP) {
48 std::make_unique<SuspiciousIncludePPCallbacks>(*
this, SM,
PP));
56void SuspiciousIncludePPCallbacks::InclusionDirective(
57 SourceLocation HashLoc,
const Token &IncludeTok, StringRef FileName,
58 bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File,
59 StringRef SearchPath, StringRef RelativePath,
const Module *SuggestedModule,
60 bool ModuleImported, SrcMgr::CharacteristicKind FileType) {
61 if (IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import)
67 const SourceLocation DiagLoc = FilenameRange.getBegin().getLocWithOffset(1);
70 FileName, Check.getImplementationFileExtensions());
74 Check.diag(DiagLoc,
"suspicious #%0 of file with '%1' extension")
75 << IncludeTok.getIdentifierInfo()->getName() << *IFE;
77 for (
const auto &HFE : Check.getHeaderFileExtensions()) {
78 SmallString<128> GuessedFileName(FileName);
79 llvm::sys::path::replace_extension(GuessedFileName,
80 (!HFE.empty() ?
"." :
"") + HFE);
82 const OptionalFileEntryRef File =
83 PP->LookupFile(DiagLoc, GuessedFileName, IsAngled,
nullptr,
nullptr,
84 nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr);
86 Check.diag(DiagLoc,
"did you mean to include '%0'?", DiagnosticIDs::Note)
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
StringRef IgnoredRegexString
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
SuspiciousIncludeCheck(StringRef Name, ClangTidyContext *Context)
std::optional< StringRef > getFileExtension(StringRef FileName, const FileExtensionsSet &FileExtensions)
Decides whether a file has a header file extension.
llvm::StringMap< ClangTidyValue > OptionMap