11 #include "clang/Frontend/CompilerInstance.h" 12 #include "clang/Lex/HeaderSearch.h" 13 #include "clang/Lex/PPCallbacks.h" 14 #include "clang/Lex/Preprocessor.h" 15 #include "llvm/ADT/DenseMap.h" 16 #include "llvm/ADT/SmallVector.h" 17 #include "llvm/Support/Path.h" 28 : Check(Check), SM(SM) {}
32 CharSourceRange FilenameRange,
const FileEntry *File,
33 StringRef SearchPath, StringRef RelativePath,
34 const Module *Imported,
35 SrcMgr::CharacteristicKind FileType)
override;
39 struct IncludeDirective {
40 IncludeDirective() =
default;
41 IncludeDirective(SourceLocation
Loc, CharSourceRange
Range,
42 StringRef
Filename, StringRef FullPath,
bool IsInMainFile)
43 :
Loc(Loc),
Range(Range), IncludeFile(Filename), IncludePath(FullPath),
44 IsInMainFile(IsInMainFile) {}
47 CharSourceRange
Range;
48 std::string IncludeFile;
49 std::string IncludePath;
53 using FileIncludes = llvm::SmallVector<IncludeDirective, 8>;
54 llvm::SmallDenseMap<FileID, FileIncludes> IncludeDirectives;
61 SourceLocation HashLoc,
const Token &IncludeTok, StringRef
FileName,
62 bool IsAngled, CharSourceRange FilenameRange,
const FileEntry *File,
63 StringRef SearchPath, StringRef RelativePath,
const Module *Imported,
64 SrcMgr::CharacteristicKind FileType) {
65 if (!Check.contains(FileName) && SrcMgr::isSystem(FileType)) {
66 SmallString<256> FullPath;
67 llvm::sys::path::append(FullPath, SearchPath);
68 llvm::sys::path::append(FullPath, RelativePath);
71 IncludeDirectives[SM.getFileID(HashLoc)].emplace_back(
72 HashLoc, FilenameRange, FileName, FullPath.str(),
73 SM.isInMainFile(HashLoc));
78 for (
const auto &Bucket : IncludeDirectives) {
79 const FileIncludes &FileDirectives = Bucket.second;
82 for (
const auto &Include : FileDirectives) {
85 unsigned ToLen = std::strcspn(SM.getCharacterData(Include.Loc),
"\n") + 1;
86 CharSourceRange ToRange = CharSourceRange::getCharRange(
87 Include.Loc, Include.Loc.getLocWithOffset(ToLen));
89 if (!Include.IsInMainFile) {
92 "system include %0 not allowed, transitively included from %1");
93 D << Include.IncludeFile << SM.getFilename(Include.Loc);
94 D << FixItHint::CreateRemoval(ToRange);
97 auto D = Check.diag(Include.Loc,
"system include %0 not allowed");
98 D << Include.IncludeFile;
99 D << FixItHint::CreateRemoval(ToRange);
105 CompilerInstance &Compiler) {
106 Compiler.getPreprocessor().addPPCallbacks(
107 llvm::make_unique<RestrictedIncludesPPCallbacks>(
108 *
this, Compiler.getSourceManager()));
113 Options.store(Opts,
"Includes", AllowedIncludes);
SourceLocation Loc
'#' location in the include directive
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.
void EndOfMainFile() override
std::string Filename
Filename as a string.
RestrictedIncludesPPCallbacks(RestrictSystemIncludesCheck &Check, SourceManager &SM)
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++ -*-===//
void registerPPCallbacks(CompilerInstance &Compiler) override
Override this to register PPCallbacks with Compiler.
CharSourceRange Range
SourceRange for the file name.