clang-tools 20.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
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
24public:
26 std::string DefaultAllowedIncludes = "*")
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;
34 bool contains(StringRef FileName) {
35 return AllowedIncludesGlobList.contains(FileName);
36 }
37
38private:
39 std::string AllowedIncludes;
40 GlobList AllowedIncludesGlobList;
41};
42
43class RestrictedIncludesPPCallbacks : public PPCallbacks {
44public:
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
58private:
59 struct IncludeDirective {
60 IncludeDirective() = default;
61 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
llvm::SmallString< 256U > Name
CharSourceRange Range
SourceRange for the file name.
std::string Filename
Filename as a string.
bool IsAngled
true if this was an include with angle brackets
StringRef FileName
SourceLocation Loc
Base class for all clang-tidy checks.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Read-only set of strings represented as a list of positive and negative globs.
Definition: GlobList.h:25
virtual bool contains(StringRef S) const
Returns true if the pattern matches S.
Definition: GlobList.cpp:58
Checks for allowed includes and suggests removal of any others.
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
RestrictSystemIncludesCheck(StringRef Name, ClangTidyContext *Context, std::string DefaultAllowedIncludes="*")
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File, StringRef SearchPath, StringRef RelativePath, const Module *SuggestedModule, bool ModuleImported, SrcMgr::CharacteristicKind FileType) override
RestrictedIncludesPPCallbacks(RestrictSystemIncludesCheck &Check, const SourceManager &SM)
llvm::StringMap< ClangTidyValue > OptionMap