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"
21 namespace portability {
24 SourceLocation HashLoc,
const Token &IncludeTok, StringRef
FileName,
25 bool IsAngled, CharSourceRange FilenameRange,
const FileEntry *File,
26 StringRef SearchPath, StringRef RelativePath,
const Module *Imported,
27 SrcMgr::CharacteristicKind FileType) {
29 SmallString<256> FullPath;
30 llvm::sys::path::append(FullPath, SearchPath);
31 llvm::sys::path::append(FullPath, RelativePath);
34 IncludeDirectives[SM.getFileID(HashLoc)].emplace_back(
35 HashLoc, FilenameRange,
FileName, FullPath.str(),
36 SM.isInMainFile(HashLoc));
41 for (
const auto &Bucket : IncludeDirectives) {
42 const FileIncludes &FileDirectives = Bucket.second;
45 for (
const auto &Include : FileDirectives) {
48 unsigned ToLen = std::strcspn(SM.getCharacterData(Include.Loc),
"\n") + 1;
49 CharSourceRange ToRange = CharSourceRange::getCharRange(
50 Include.Loc, Include.Loc.getLocWithOffset(ToLen));
52 if (!Include.IsInMainFile) {
55 "system include %0 not allowed, transitively included from %1");
56 D << Include.IncludeFile << SM.getFilename(Include.Loc);
57 D << FixItHint::CreateRemoval(ToRange);
60 auto D = Check.
diag(Include.Loc,
"system include %0 not allowed");
61 D << Include.IncludeFile;
62 D << FixItHint::CreateRemoval(ToRange);
68 const SourceManager &SM, Preprocessor *
PP, Preprocessor *ModuleExpanderPP) {
70 std::make_unique<RestrictedIncludesPPCallbacks>(*
this, SM));