10#include "../utils/FileExtensionsUtils.h"
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,
27 CharSourceRange FilenameRange,
28 OptionalFileEntryRef File, StringRef SearchPath,
29 StringRef RelativePath,
const Module *Imported,
30 SrcMgr::CharacteristicKind FileType)
override;
33 SuspiciousIncludeCheck &Check;
41 std::optional<StringRef> ImplementationFileExtensionsOption =
43 RawStringImplementationFileExtensions =
44 ImplementationFileExtensionsOption.value_or(
46 if (ImplementationFileExtensionsOption) {
51 << RawStringImplementationFileExtensions;
56 std::optional<StringRef> HeaderFileExtensionsOption =
58 RawStringHeaderFileExtensions =
60 if (HeaderFileExtensionsOption) {
65 << RawStringHeaderFileExtensions;
73 RawStringImplementationFileExtensions);
74 Options.
store(Opts,
"HeaderFileExtensions", RawStringHeaderFileExtensions);
78 const SourceManager &SM, Preprocessor *
PP, Preprocessor *ModuleExpanderPP) {
80 ::std::make_unique<SuspiciousIncludePPCallbacks>(*
this, SM,
PP));
83void SuspiciousIncludePPCallbacks::InclusionDirective(
84 SourceLocation HashLoc,
const Token &IncludeTok, StringRef
FileName,
85 bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File,
86 StringRef SearchPath, StringRef RelativePath,
const Module *Imported,
87 SrcMgr::CharacteristicKind FileType) {
88 if (IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import)
91 SourceLocation DiagLoc = FilenameRange.getBegin().getLocWithOffset(1);
93 const std::optional<StringRef> IFE =
98 Check.diag(DiagLoc,
"suspicious #%0 of file with '%1' extension")
99 << IncludeTok.getIdentifierInfo()->getName() << *IFE;
101 for (
const auto &HFE : Check.HeaderFileExtensions) {
102 SmallString<128> GuessedFileName(
FileName);
103 llvm::sys::path::replace_extension(GuessedFileName,
104 (!HFE.empty() ?
"." :
"") + HFE);
106 OptionalFileEntryRef File =
107 PP->LookupFile(DiagLoc, GuessedFileName,
IsAngled,
nullptr,
nullptr,
108 nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr);
110 Check.diag(DiagLoc,
"did you mean to include '%0'?", DiagnosticIDs::Note)
bool IsAngled
true if this was an include with angle brackets
std::optional< StringRef > get(StringRef LocalName) const
Read a named option from the Context.
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder configurationDiag(StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning) const
Adds a diagnostic to report errors in the check's configuration.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
SuspiciousIncludeCheck(StringRef Name, ClangTidyContext *Context)
FileExtensionsSet ImplementationFileExtensions
FileExtensionsSet HeaderFileExtensions
std::optional< StringRef > getFileExtension(StringRef FileName, const FileExtensionsSet &FileExtensions)
Decides whether a file has a header file extension.
StringRef defaultHeaderFileExtensions()
Returns recommended default value for the list of header file extensions.
StringRef defaultFileExtensionDelimiters()
Returns recommended default value for the list of file extension delimiters.
bool parseFileExtensions(StringRef AllFileExtensions, FileExtensionsSet &FileExtensions, StringRef Delimiters)
Parses header file extensions from a semicolon-separated list.
StringRef defaultImplementationFileExtensions()
Returns recommended default value for the list of implementation file extensions.
llvm::StringMap< ClangTidyValue > OptionMap