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" 26 const SourceManager &SM)
27 : Check(Check), SM(SM) {}
31 CharSourceRange FilenameRange,
const FileEntry *File,
32 StringRef SearchPath, StringRef RelativePath,
33 const Module *Imported,
34 SrcMgr::CharacteristicKind FileType)
override;
38 struct IncludeDirective {
39 IncludeDirective() =
default;
40 IncludeDirective(SourceLocation
Loc, CharSourceRange
Range,
41 StringRef
Filename, StringRef FullPath,
bool IsInMainFile)
42 :
Loc(Loc),
Range(Range), IncludeFile(Filename), IncludePath(FullPath),
43 IsInMainFile(IsInMainFile) {}
46 CharSourceRange
Range;
47 std::string IncludeFile;
48 std::string IncludePath;
52 using FileIncludes = llvm::SmallVector<IncludeDirective, 8>;
53 llvm::SmallDenseMap<FileID, FileIncludes> IncludeDirectives;
56 const SourceManager &SM;
60 SourceLocation HashLoc,
const Token &IncludeTok, StringRef
FileName,
61 bool IsAngled, CharSourceRange FilenameRange,
const FileEntry *File,
62 StringRef SearchPath, StringRef RelativePath,
const Module *Imported,
63 SrcMgr::CharacteristicKind FileType) {
64 if (!Check.contains(FileName) && SrcMgr::isSystem(FileType)) {
65 SmallString<256> FullPath;
66 llvm::sys::path::append(FullPath, SearchPath);
67 llvm::sys::path::append(FullPath, RelativePath);
70 IncludeDirectives[SM.getFileID(HashLoc)].emplace_back(
71 HashLoc, FilenameRange, FileName, FullPath.str(),
72 SM.isInMainFile(HashLoc));
77 for (
const auto &Bucket : IncludeDirectives) {
78 const FileIncludes &FileDirectives = Bucket.second;
81 for (
const auto &Include : FileDirectives) {
84 unsigned ToLen = std::strcspn(SM.getCharacterData(Include.Loc),
"\n") + 1;
85 CharSourceRange ToRange = CharSourceRange::getCharRange(
86 Include.Loc, Include.Loc.getLocWithOffset(ToLen));
88 if (!Include.IsInMainFile) {
91 "system include %0 not allowed, transitively included from %1");
92 D << Include.IncludeFile << SM.getFilename(Include.Loc);
93 D << FixItHint::CreateRemoval(ToRange);
96 auto D = Check.diag(Include.Loc,
"system include %0 not allowed");
97 D << Include.IncludeFile;
98 D << FixItHint::CreateRemoval(ToRange);
104 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
106 llvm::make_unique<RestrictedIncludesPPCallbacks>(*
this, SM));
111 Options.store(Opts,
"Includes", AllowedIncludes);
SourceLocation Loc
'#' location in the include directive
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...
Checks for allowed includes and suggests removal of any others.
RestrictedIncludesPPCallbacks(RestrictSystemIncludesCheck &Check, const SourceManager &SM)
void EndOfMainFile() override
std::string Filename
Filename as a string.
bool IsAngled
true if this was an include with angle brackets
std::map< std::string, std::string > OptionMap
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File, StringRef SearchPath, StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
CharSourceRange Range
SourceRange for the file name.