10#include "clang/Frontend/CompilerInstance.h"
11#include "clang/Lex/HeaderSearch.h"
12#include "clang/Lex/PPCallbacks.h"
13#include "clang/Lex/Preprocessor.h"
14#include "llvm/ADT/DenseMap.h"
15#include "llvm/ADT/SmallVector.h"
16#include "llvm/Support/Path.h"
22 SourceLocation HashLoc,
const Token &IncludeTok, StringRef
FileName,
23 bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File,
24 StringRef SearchPath, StringRef RelativePath,
const Module *SuggestedModule,
25 bool ModuleImported, SrcMgr::CharacteristicKind FileType) {
27 SmallString<256> FullPath;
28 llvm::sys::path::append(FullPath, SearchPath);
29 llvm::sys::path::append(FullPath, RelativePath);
32 IncludeDirectives[SM.getFileID(HashLoc)].emplace_back(
33 HashLoc, FilenameRange,
FileName, FullPath.str(),
34 SM.isInMainFile(HashLoc));
39 for (
const auto &Bucket : IncludeDirectives) {
40 const FileIncludes &FileDirectives = Bucket.second;
43 for (
const auto &Include : FileDirectives) {
46 unsigned ToLen = std::strcspn(SM.getCharacterData(Include.Loc),
"\n") + 1;
47 CharSourceRange ToRange = CharSourceRange::getCharRange(
48 Include.Loc, Include.Loc.getLocWithOffset(ToLen));
50 if (!Include.IsInMainFile) {
53 "system include %0 not allowed, transitively included from %1");
54 D << Include.IncludeFile << SM.getFilename(Include.Loc);
55 D << FixItHint::CreateRemoval(ToRange);
58 auto D = Check.
diag(Include.Loc,
"system include %0 not allowed");
59 D << Include.IncludeFile;
60 D << FixItHint::CreateRemoval(ToRange);
66 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
68 std::make_unique<RestrictedIncludesPPCallbacks>(*
this, SM));
bool IsAngled
true if this was an include with angle brackets
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.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
bool contains(StringRef FileName)
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File, StringRef SearchPath, StringRef RelativePath, const Module *SuggestedModule, bool ModuleImported, SrcMgr::CharacteristicKind FileType) override
void EndOfMainFile() override
llvm::StringMap< ClangTidyValue > OptionMap