RestrictSystemIncludesCheck.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //===--- RestrictSystemIncludesCheck.cpp - clang-tidy ---------------------===//
  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. #include "RestrictSystemIncludesCheck.h"
  9. #include "clang/Frontend/CompilerInstance.h"
  10. #include "clang/Lex/HeaderSearch.h"
  11. #include "clang/Lex/PPCallbacks.h"
  12. #include "clang/Lex/Preprocessor.h"
  13. #include "llvm/ADT/DenseMap.h"
  14. #include "llvm/ADT/SmallVector.h"
  15. #include "llvm/Support/Path.h"
  16. #include <cstring>
  17. namespace clang::tidy::portability {
  18. void RestrictedIncludesPPCallbacks::InclusionDirective(
  19. SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
  20. bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File,
  21. StringRef SearchPath, StringRef RelativePath, const Module *Imported,
  22. SrcMgr::CharacteristicKind FileType) {
  23. if (!Check.contains(FileName) && SrcMgr::isSystem(FileType)) {
  24. SmallString<256> FullPath;
  25. llvm::sys::path::append(FullPath, SearchPath);
  26. llvm::sys::path::append(FullPath, RelativePath);
  27. // Bucket the allowed include directives by the id of the file they were
  28. // declared in.
  29. IncludeDirectives[SM.getFileID(HashLoc)].emplace_back(
  30. HashLoc, FilenameRange, FileName, FullPath.str(),
  31. SM.isInMainFile(HashLoc));
  32. }
  33. }
  34. void RestrictedIncludesPPCallbacks::EndOfMainFile() {
  35. for (const auto &Bucket : IncludeDirectives) {
  36. const FileIncludes &FileDirectives = Bucket.second;
  37. // Emit fixits for all restricted includes.
  38. for (const auto &Include : FileDirectives) {
  39. // Fetch the length of the include statement from the start to just after
  40. // the newline, for finding the end (including the newline).
  41. unsigned ToLen = std::strcspn(SM.getCharacterData(Include.Loc), "\n") + 1;
  42. CharSourceRange ToRange = CharSourceRange::getCharRange(
  43. Include.Loc, Include.Loc.getLocWithOffset(ToLen));
  44. if (!Include.IsInMainFile) {
  45. auto D = Check.diag(
  46. Include.Loc,
  47. "system include %0 not allowed, transitively included from %1");
  48. D << Include.IncludeFile << SM.getFilename(Include.Loc);
  49. D << FixItHint::CreateRemoval(ToRange);
  50. continue;
  51. }
  52. auto D = Check.diag(Include.Loc, "system include %0 not allowed");
  53. D << Include.IncludeFile;
  54. D << FixItHint::CreateRemoval(ToRange);
  55. }
  56. }
  57. }
  58. void RestrictSystemIncludesCheck::registerPPCallbacks(
  59. const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
  60. PP->addPPCallbacks(
  61. std::make_unique<RestrictedIncludesPPCallbacks>(*this, SM));
  62. }
  63. void RestrictSystemIncludesCheck::storeOptions(
  64. ClangTidyOptions::OptionMap &Opts) {
  65. Options.store(Opts, "Includes", AllowedIncludes);
  66. }
  67. } // namespace clang::tidy::portability