| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 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) {} |
| 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 |
|
| 38 |
private: |
| 39 |
std::string AllowedIncludes; |
| 40 |
GlobList AllowedIncludesGlobList; |
| 41 |
}; |
| 42 |
|
| 43 |
class RestrictedIncludesPPCallbacks : public PPCallbacks { |
| 44 |
public: |
| 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 *Imported, |
| 54 |
SrcMgr::CharacteristicKind FileType) override; |
| 55 |
void EndOfMainFile() override; |
| 56 |
|
| 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) {} |
| 64 |
|
| 65 |
SourceLocation Loc; |
| 66 |
CharSourceRange Range; |
| 67 |
std::string IncludeFile; |
| 68 |
std::string IncludePath; |
| 69 |
bool IsInMainFile; |
| 70 |
}; |
| 71 |
|
| 72 |
using FileIncludes = llvm::SmallVector<IncludeDirective, 8>; |
| 73 |
llvm::SmallDenseMap<FileID, FileIncludes> IncludeDirectives; |
| 74 |
|
| 75 |
RestrictSystemIncludesCheck &Check; |
| 76 |
const SourceManager &SM; |
| 77 |
}; |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
#endif |