1 //===--- RestrictSystemIncludesCheck.h - clang-tidy --------------*- C++-*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_RESTRICTINCLUDESSCHECK_H 10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_RESTRICTINCLUDESSCHECK_H 11 12 #include "../ClangTidyCheck.h" 13 #include "../GlobList.h" 14 #include "clang/Lex/PPCallbacks.h" 15 16 namespace clang { 17 namespace tidy { 18 namespace portability { 19 20 /// Checks for allowed includes and suggests removal of any others. If no 21 /// includes are specified, the check will exit without issuing any warnings. 22 /// 23 /// For the user-facing documentation see: 24 /// http://clang.llvm.org/extra/clang-tidy/checks/portability-restrict-system-includes.html 25 class RestrictSystemIncludesCheck : public ClangTidyCheck { 26 public: 27 RestrictSystemIncludesCheck(StringRef Name, ClangTidyContext *Context, 28 std::string DefaultAllowedIncludes = "*") ClangTidyCheck(Name,Context)29 : ClangTidyCheck(Name, Context), 30 AllowedIncludes(Options.get("Includes", DefaultAllowedIncludes)), 31 AllowedIncludesGlobList(AllowedIncludes) {} 32 33 void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, 34 Preprocessor *ModuleExpanderPP) override; 35 void storeOptions(ClangTidyOptions::OptionMap &Opts) override; contains(StringRef FileName)36 bool contains(StringRef FileName) { 37 return AllowedIncludesGlobList.contains(FileName); 38 } 39 40 private: 41 std::string AllowedIncludes; 42 GlobList AllowedIncludesGlobList; 43 }; 44 45 class RestrictedIncludesPPCallbacks : public PPCallbacks { 46 public: RestrictedIncludesPPCallbacks(RestrictSystemIncludesCheck & Check,const SourceManager & SM)47 explicit RestrictedIncludesPPCallbacks(RestrictSystemIncludesCheck &Check, 48 const SourceManager &SM) 49 : Check(Check), SM(SM) {} 50 51 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, 52 StringRef FileName, bool IsAngled, 53 CharSourceRange FilenameRange, const FileEntry *File, 54 StringRef SearchPath, StringRef RelativePath, 55 const Module *Imported, 56 SrcMgr::CharacteristicKind FileType) override; 57 void EndOfMainFile() override; 58 59 private: 60 struct IncludeDirective { 61 IncludeDirective() = default; IncludeDirectiveIncludeDirective62 IncludeDirective(SourceLocation Loc, CharSourceRange Range, 63 StringRef Filename, StringRef FullPath, bool IsInMainFile) 64 : Loc(Loc), Range(Range), IncludeFile(Filename), IncludePath(FullPath), 65 IsInMainFile(IsInMainFile) {} 66 67 SourceLocation Loc; // '#' location in the include directive 68 CharSourceRange Range; // SourceRange for the file name 69 std::string IncludeFile; // Filename as a string 70 std::string IncludePath; // Full file path as a string 71 bool IsInMainFile; // Whether or not the include is in the main file 72 }; 73 74 using FileIncludes = llvm::SmallVector<IncludeDirective, 8>; 75 llvm::SmallDenseMap<FileID, FileIncludes> IncludeDirectives; 76 77 RestrictSystemIncludesCheck &Check; 78 const SourceManager &SM; 79 }; 80 81 } // namespace portability 82 } // namespace tidy 83 } // namespace clang 84 85 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_RESTRICTINCLUDESSCHECK_H 86