RestrictSystemIncludesCheck.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_RESTRICTINCLUDESSCHECK_H
  9. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_RESTRICTINCLUDESSCHECK_H
  10. #include "../ClangTidyCheck.h"
  11. #include "../GlobList.h"
  12. #include "clang/Lex/PPCallbacks.h"
  13. namespace clang::tidy::portability {
  14. /// Checks for allowed includes and suggests removal of any others. If no
  15. /// includes are specified, the check will exit without issuing any warnings.
  16. ///
  17. /// For the user-facing documentation see:
  18. /// http://clang.llvm.org/extra/clang-tidy/checks/portability/restrict-system-includes.html
  19. class RestrictSystemIncludesCheck : public ClangTidyCheck {
  20. public:
  21. RestrictSystemIncludesCheck(StringRef Name, ClangTidyContext *Context,
  22. std::string DefaultAllowedIncludes = "*")
  23. : ClangTidyCheck(Name, Context),
  24. AllowedIncludes(Options.get("Includes", DefaultAllowedIncludes)),
  25. AllowedIncludesGlobList(AllowedIncludes) {}
  26. void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
  27. Preprocessor *ModuleExpanderPP) override;
  28. void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
  29. bool contains(StringRef FileName) {
  30. return AllowedIncludesGlobList.contains(FileName);
  31. }
  32. private:
  33. std::string AllowedIncludes;
  34. GlobList AllowedIncludesGlobList;
  35. };
  36. class RestrictedIncludesPPCallbacks : public PPCallbacks {
  37. public:
  38. explicit RestrictedIncludesPPCallbacks(RestrictSystemIncludesCheck &Check,
  39. const SourceManager &SM)
  40. : Check(Check), SM(SM) {}
  41. void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
  42. StringRef FileName, bool IsAngled,
  43. CharSourceRange FilenameRange,
  44. OptionalFileEntryRef File, StringRef SearchPath,
  45. StringRef RelativePath, const Module *Imported,
  46. SrcMgr::CharacteristicKind FileType) override;
  47. void EndOfMainFile() override;
  48. private:
  49. struct IncludeDirective {
  50. IncludeDirective() = default;
  51. IncludeDirective(SourceLocation Loc, CharSourceRange Range,
  52. StringRef Filename, StringRef FullPath, bool IsInMainFile)
  53. : Loc(Loc), Range(Range), IncludeFile(Filename), IncludePath(FullPath),
  54. IsInMainFile(IsInMainFile) {}
  55. SourceLocation Loc; // '#' location in the include directive
  56. CharSourceRange Range; // SourceRange for the file name
  57. std::string IncludeFile; // Filename as a string
  58. std::string IncludePath; // Full file path as a string
  59. bool IsInMainFile; // Whether or not the include is in the main file
  60. };
  61. using FileIncludes = llvm::SmallVector<IncludeDirective, 8>;
  62. llvm::SmallDenseMap<FileID, FileIncludes> IncludeDirectives;
  63. RestrictSystemIncludesCheck &Check;
  64. const SourceManager &SM;
  65. };
  66. } // namespace clang::tidy::portability
  67. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_RESTRICTINCLUDESSCHECK_H