xref: /llvm-project/clang-tools-extra/clang-tidy/portability/RestrictSystemIncludesCheck.h (revision da95d926f6fce4ed9707c77908ad96624268f134)
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::tidy::portability {
17 
18 /// Checks for allowed includes and suggests removal of any others. If no
19 /// includes are specified, the check will exit without issuing any warnings.
20 ///
21 /// For the user-facing documentation see:
22 /// http://clang.llvm.org/extra/clang-tidy/checks/portability/restrict-system-includes.html
23 class RestrictSystemIncludesCheck : public ClangTidyCheck {
24 public:
25   RestrictSystemIncludesCheck(StringRef Name, ClangTidyContext *Context,
26                               std::string DefaultAllowedIncludes = "*")
ClangTidyCheck(Name,Context)27       : ClangTidyCheck(Name, Context),
28         AllowedIncludes(Options.get("Includes", DefaultAllowedIncludes)),
29         AllowedIncludesGlobList(AllowedIncludes) {}
30 
31   void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
32                            Preprocessor *ModuleExpanderPP) override;
33   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
contains(StringRef FileName)34   bool contains(StringRef FileName) {
35     return AllowedIncludesGlobList.contains(FileName);
36   }
37 
38 private:
39   std::string AllowedIncludes;
40   GlobList AllowedIncludesGlobList;
41 };
42 
43 class RestrictedIncludesPPCallbacks : public PPCallbacks {
44 public:
RestrictedIncludesPPCallbacks(RestrictSystemIncludesCheck & Check,const SourceManager & SM)45   explicit RestrictedIncludesPPCallbacks(RestrictSystemIncludesCheck &Check,
46                                          const SourceManager &SM)
47       : Check(Check), SM(SM) {}
48 
49   void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
50                           StringRef FileName, bool IsAngled,
51                           CharSourceRange FilenameRange,
52                           OptionalFileEntryRef File, StringRef SearchPath,
53                           StringRef RelativePath, const Module *SuggestedModule,
54                           bool ModuleImported,
55                           SrcMgr::CharacteristicKind FileType) override;
56   void EndOfMainFile() override;
57 
58 private:
59   struct IncludeDirective {
60     IncludeDirective() = default;
IncludeDirectiveIncludeDirective61     IncludeDirective(SourceLocation Loc, CharSourceRange Range,
62                      StringRef Filename, StringRef FullPath, bool IsInMainFile)
63         : Loc(Loc), Range(Range), IncludeFile(Filename), IncludePath(FullPath),
64           IsInMainFile(IsInMainFile) {}
65 
66     SourceLocation Loc;      // '#' location in the include directive
67     CharSourceRange Range;   // SourceRange for the file name
68     std::string IncludeFile; // Filename as a string
69     std::string IncludePath; // Full file path as a string
70     bool IsInMainFile;       // Whether or not the include is in the main file
71   };
72 
73   using FileIncludes = llvm::SmallVector<IncludeDirective, 8>;
74   llvm::SmallDenseMap<FileID, FileIncludes> IncludeDirectives;
75 
76   RestrictSystemIncludesCheck &Check;
77   const SourceManager &SM;
78 };
79 
80 } // namespace clang::tidy::portability
81 
82 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_RESTRICTINCLUDESSCHECK_H
83