clang-tools  14.0.0git
RestrictSystemIncludesCheck.h
Go to the documentation of this file.
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
26 public:
28  std::string DefaultAllowedIncludes = "*")
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;
36  bool contains(StringRef FileName) {
37  return AllowedIncludesGlobList.contains(FileName);
38  }
39 
40 private:
41  std::string AllowedIncludes;
42  GlobList AllowedIncludesGlobList;
43 };
44 
46 public:
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;
62  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
Range
CharSourceRange Range
SourceRange for the file name.
Definition: IncludeOrderCheck.cpp:38
Loc
SourceLocation Loc
Definition: KernelNameRestrictionCheck.cpp:45
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
Filename
std::string Filename
Filename as a string.
Definition: IncludeOrderCheck.cpp:39
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:54
clang::tidy::portability::RestrictSystemIncludesCheck
Checks for allowed includes and suggests removal of any others.
Definition: RestrictSystemIncludesCheck.h:25
clang::tidy::GlobList
Read-only set of strings represented as a list of positive and negative globs.
Definition: GlobList.h:25
clang::tidy::portability::RestrictSystemIncludesCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: RestrictSystemIncludesCheck.cpp:73
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:416
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:71
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::tidy::portability::RestrictedIncludesPPCallbacks
Definition: RestrictSystemIncludesCheck.h:45
FileName
StringRef FileName
Definition: KernelNameRestrictionCheck.cpp:46
clang::tidy::portability::RestrictSystemIncludesCheck::contains
bool contains(StringRef FileName)
Definition: RestrictSystemIncludesCheck.h:36
clang::tidy::portability::RestrictSystemIncludesCheck::registerPPCallbacks
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
Definition: RestrictSystemIncludesCheck.cpp:67
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition: BadSignalToKillThreadCheck.cpp:29
IsAngled
bool IsAngled
true if this was an include with angle brackets
Definition: IncludeOrderCheck.cpp:40
PPCallbacks
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
SM
const SourceManager & SM
Definition: IncludeCleaner.cpp:140
clang::tidy::portability::RestrictSystemIncludesCheck::RestrictSystemIncludesCheck
RestrictSystemIncludesCheck(StringRef Name, ClangTidyContext *Context, std::string DefaultAllowedIncludes="*")
Definition: RestrictSystemIncludesCheck.h:27
clang::tidy::portability::RestrictedIncludesPPCallbacks::InclusionDirective
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File, StringRef SearchPath, StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override
Definition: RestrictSystemLibcHeadersCheck.cpp:47
clang::tidy::portability::RestrictedIncludesPPCallbacks::EndOfMainFile
void EndOfMainFile() override
Definition: RestrictSystemIncludesCheck.cpp:40
clang::tidy::portability::RestrictedIncludesPPCallbacks::RestrictedIncludesPPCallbacks
RestrictedIncludesPPCallbacks(RestrictSystemIncludesCheck &Check, const SourceManager &SM)
Definition: RestrictSystemIncludesCheck.h:47
clang::tidy::GlobList::contains
bool contains(StringRef S) const
Returns true if the pattern matches S.
Definition: GlobList.cpp:56