10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/HeaderSearch.h"
13 #include "clang/Lex/HeaderSearchOptions.h"
14 #include "clang/Lex/Preprocessor.h"
25 class RestrictedIncludesPPCallbacks
26 :
public portability::RestrictedIncludesPPCallbacks {
28 explicit RestrictedIncludesPPCallbacks(
29 RestrictSystemLibcHeadersCheck &Check,
const SourceManager &SM,
30 const SmallString<128> CompilerIncudeDir)
31 : portability::RestrictedIncludesPPCallbacks(Check, SM),
32 CompilerIncudeDir(CompilerIncudeDir) {}
34 void InclusionDirective(SourceLocation HashLoc,
const Token &IncludeTok,
36 CharSourceRange FilenameRange,
37 Optional<FileEntryRef> File, StringRef SearchPath,
38 StringRef RelativePath,
const Module *Imported,
39 SrcMgr::CharacteristicKind FileType)
override;
42 const SmallString<128> CompilerIncudeDir;
47 void RestrictedIncludesPPCallbacks::InclusionDirective(
48 SourceLocation HashLoc,
const Token &IncludeTok, StringRef
FileName,
49 bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File,
50 StringRef SearchPath, StringRef RelativePath,
const Module *Imported,
51 SrcMgr::CharacteristicKind FileType) {
53 if (SrcMgr::isSystem(FileType) && SearchPath == CompilerIncudeDir)
57 RelativePath, Imported, FileType);
61 const SourceManager &SM, Preprocessor *
PP, Preprocessor *ModuleExpanderPP) {
62 SmallString<128> CompilerIncudeDir =
63 StringRef(
PP->getHeaderSearchInfo().getHeaderSearchOpts().ResourceDir);
64 llvm::sys::path::append(CompilerIncudeDir,
"include");
65 PP->addPPCallbacks(std::make_unique<RestrictedIncludesPPCallbacks>(
66 *
this, SM, CompilerIncudeDir));