[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / clang-tools-extra / clang-tidy / portability / RestrictSystemIncludesCheck.h
blobad18e6f411dbbd07dfd13005f387132b1ea7f13a
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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_RESTRICTINCLUDESSCHECK_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_RESTRICTINCLUDESSCHECK_H
12 #include "../ClangTidyCheck.h"
13 #include "../GlobList.h"
14 #include "clang/Lex/PPCallbacks.h"
16 namespace clang::tidy::portability {
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 = "*")
27 : ClangTidyCheck(Name, Context),
28 AllowedIncludes(Options.get("Includes", DefaultAllowedIncludes)),
29 AllowedIncludesGlobList(AllowedIncludes) {}
31 void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
32 Preprocessor *ModuleExpanderPP) override;
33 void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
34 bool contains(StringRef FileName) {
35 return AllowedIncludesGlobList.contains(FileName);
38 private:
39 std::string AllowedIncludes;
40 GlobList AllowedIncludesGlobList;
43 class RestrictedIncludesPPCallbacks : public PPCallbacks {
44 public:
45 explicit RestrictedIncludesPPCallbacks(RestrictSystemIncludesCheck &Check,
46 const SourceManager &SM)
47 : Check(Check), SM(SM) {}
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 *Imported,
54 SrcMgr::CharacteristicKind FileType) override;
55 void EndOfMainFile() override;
57 private:
58 struct IncludeDirective {
59 IncludeDirective() = default;
60 IncludeDirective(SourceLocation Loc, CharSourceRange Range,
61 StringRef Filename, StringRef FullPath, bool IsInMainFile)
62 : Loc(Loc), Range(Range), IncludeFile(Filename), IncludePath(FullPath),
63 IsInMainFile(IsInMainFile) {}
65 SourceLocation Loc; // '#' location in the include directive
66 CharSourceRange Range; // SourceRange for the file name
67 std::string IncludeFile; // Filename as a string
68 std::string IncludePath; // Full file path as a string
69 bool IsInMainFile; // Whether or not the include is in the main file
72 using FileIncludes = llvm::SmallVector<IncludeDirective, 8>;
73 llvm::SmallDenseMap<FileID, FileIncludes> IncludeDirectives;
75 RestrictSystemIncludesCheck &Check;
76 const SourceManager &SM;
79 } // namespace clang::tidy::portability
81 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_RESTRICTINCLUDESSCHECK_H