File indexing completed on 2026-05-10 08:36:21
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_RESTRICTINCLUDESSCHECK_H
0010 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_RESTRICTINCLUDESSCHECK_H
0011
0012 #include "../ClangTidyCheck.h"
0013 #include "../GlobList.h"
0014 #include "clang/Lex/PPCallbacks.h"
0015
0016 namespace clang::tidy::portability {
0017
0018
0019
0020
0021
0022
0023 class RestrictSystemIncludesCheck : public ClangTidyCheck {
0024 public:
0025 RestrictSystemIncludesCheck(StringRef Name, ClangTidyContext *Context,
0026 std::string DefaultAllowedIncludes = "*")
0027 : ClangTidyCheck(Name, Context),
0028 AllowedIncludes(Options.get("Includes", DefaultAllowedIncludes)),
0029 AllowedIncludesGlobList(AllowedIncludes) {}
0030
0031 void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
0032 Preprocessor *ModuleExpanderPP) override;
0033 void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
0034 bool contains(StringRef FileName) {
0035 return AllowedIncludesGlobList.contains(FileName);
0036 }
0037
0038 private:
0039 std::string AllowedIncludes;
0040 GlobList AllowedIncludesGlobList;
0041 };
0042
0043 class RestrictedIncludesPPCallbacks : public PPCallbacks {
0044 public:
0045 explicit RestrictedIncludesPPCallbacks(RestrictSystemIncludesCheck &Check,
0046 const SourceManager &SM)
0047 : Check(Check), SM(SM) {}
0048
0049 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
0050 StringRef FileName, bool IsAngled,
0051 CharSourceRange FilenameRange,
0052 OptionalFileEntryRef File, StringRef SearchPath,
0053 StringRef RelativePath, const Module *SuggestedModule,
0054 bool ModuleImported,
0055 SrcMgr::CharacteristicKind FileType) override;
0056 void EndOfMainFile() override;
0057
0058 private:
0059 struct IncludeDirective {
0060 IncludeDirective() = default;
0061 IncludeDirective(SourceLocation Loc, CharSourceRange Range,
0062 StringRef Filename, StringRef FullPath, bool IsInMainFile)
0063 : Loc(Loc), Range(Range), IncludeFile(Filename), IncludePath(FullPath),
0064 IsInMainFile(IsInMainFile) {}
0065
0066 SourceLocation Loc;
0067 CharSourceRange Range;
0068 std::string IncludeFile;
0069 std::string IncludePath;
0070 bool IsInMainFile;
0071 };
0072
0073 using FileIncludes = llvm::SmallVector<IncludeDirective, 8>;
0074 llvm::SmallDenseMap<FileID, FileIncludes> IncludeDirectives;
0075
0076 RestrictSystemIncludesCheck &Check;
0077 const SourceManager &SM;
0078 };
0079
0080 }
0081
0082 #endif