Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:21

0001 //===--- RestrictSystemIncludesCheck.h - clang-tidy --------------*- C++-*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
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 /// Checks for allowed includes and suggests removal of any others. If no
0019 /// includes are specified, the check will exit without issuing any warnings.
0020 ///
0021 /// For the user-facing documentation see:
0022 /// http://clang.llvm.org/extra/clang-tidy/checks/portability/restrict-system-includes.html
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;      // '#' location in the include directive
0067     CharSourceRange Range;   // SourceRange for the file name
0068     std::string IncludeFile; // Filename as a string
0069     std::string IncludePath; // Full file path as a string
0070     bool IsInMainFile;       // Whether or not the include is in the main file
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 } // namespace clang::tidy::portability
0081 
0082 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_RESTRICTINCLUDESSCHECK_H