brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.4 KiB · d66149a Raw
83 lines · c
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_RESTRICTSYSTEMINCLUDESCHECK_H10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_RESTRICTSYSTEMINCLUDESCHECK_H11 12#include "../ClangTidyCheck.h"13#include "../GlobList.h"14#include "clang/Lex/PPCallbacks.h"15 16namespace clang::tidy::portability {17 18/// Checks for allowed includes and suggests removal of any others. If no19/// includes are specified, the check will exit without issuing any warnings.20///21/// For the user-facing documentation see:22/// https://clang.llvm.org/extra/clang-tidy/checks/portability/restrict-system-includes.html23class RestrictSystemIncludesCheck : public ClangTidyCheck {24public:25  RestrictSystemIncludesCheck(StringRef Name, ClangTidyContext *Context,26                              StringRef 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  StringRef AllowedIncludes;40  GlobList AllowedIncludesGlobList;41};42 43class RestrictedIncludesPPCallbacks : public PPCallbacks {44public: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 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 directive67    CharSourceRange Range;   // SourceRange for the file name68    std::string IncludeFile; // Filename as a string69    std::string IncludePath; // Full file path as a string70    bool IsInMainFile;       // Whether or not the include is in the main file71  };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::portability81 82#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_RESTRICTSYSTEMINCLUDESCHECK_H83