150
|
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
|
|
9 #include "RestrictSystemIncludesCheck.h"
|
|
10 #include "clang/Frontend/CompilerInstance.h"
|
|
11 #include "clang/Lex/HeaderSearch.h"
|
|
12 #include "clang/Lex/PPCallbacks.h"
|
|
13 #include "clang/Lex/Preprocessor.h"
|
|
14 #include "llvm/ADT/DenseMap.h"
|
|
15 #include "llvm/ADT/SmallVector.h"
|
|
16 #include "llvm/Support/Path.h"
|
|
17 #include <cstring>
|
|
18
|
|
19 namespace clang {
|
|
20 namespace tidy {
|
|
21 namespace fuchsia {
|
|
22
|
|
23 class RestrictedIncludesPPCallbacks : public PPCallbacks {
|
|
24 public:
|
|
25 explicit RestrictedIncludesPPCallbacks(RestrictSystemIncludesCheck &Check,
|
|
26 const SourceManager &SM)
|
|
27 : Check(Check), SM(SM) {}
|
|
28
|
|
29 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
|
|
30 StringRef FileName, bool IsAngled,
|
|
31 CharSourceRange FilenameRange, const FileEntry *File,
|
|
32 StringRef SearchPath, StringRef RelativePath,
|
|
33 const Module *Imported,
|
|
34 SrcMgr::CharacteristicKind FileType) override;
|
|
35 void EndOfMainFile() override;
|
|
36
|
|
37 private:
|
|
38 struct IncludeDirective {
|
|
39 IncludeDirective() = default;
|
|
40 IncludeDirective(SourceLocation Loc, CharSourceRange Range,
|
|
41 StringRef Filename, StringRef FullPath, bool IsInMainFile)
|
|
42 : Loc(Loc), Range(Range), IncludeFile(Filename), IncludePath(FullPath),
|
|
43 IsInMainFile(IsInMainFile) {}
|
|
44
|
|
45 SourceLocation Loc; // '#' location in the include directive
|
|
46 CharSourceRange Range; // SourceRange for the file name
|
|
47 std::string IncludeFile; // Filename as a string
|
|
48 std::string IncludePath; // Full file path as a string
|
|
49 bool IsInMainFile; // Whether or not the include is in the main file
|
|
50 };
|
|
51
|
|
52 using FileIncludes = llvm::SmallVector<IncludeDirective, 8>;
|
|
53 llvm::SmallDenseMap<FileID, FileIncludes> IncludeDirectives;
|
|
54
|
|
55 RestrictSystemIncludesCheck &Check;
|
|
56 const SourceManager &SM;
|
|
57 };
|
|
58
|
|
59 void RestrictedIncludesPPCallbacks::InclusionDirective(
|
|
60 SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
|
|
61 bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File,
|
|
62 StringRef SearchPath, StringRef RelativePath, const Module *Imported,
|
|
63 SrcMgr::CharacteristicKind FileType) {
|
|
64 if (!Check.contains(FileName) && SrcMgr::isSystem(FileType)) {
|
|
65 SmallString<256> FullPath;
|
|
66 llvm::sys::path::append(FullPath, SearchPath);
|
|
67 llvm::sys::path::append(FullPath, RelativePath);
|
|
68 // Bucket the allowed include directives by the id of the file they were
|
|
69 // declared in.
|
|
70 IncludeDirectives[SM.getFileID(HashLoc)].emplace_back(
|
|
71 HashLoc, FilenameRange, FileName, FullPath.str(),
|
|
72 SM.isInMainFile(HashLoc));
|
|
73 }
|
|
74 }
|
|
75
|
|
76 void RestrictedIncludesPPCallbacks::EndOfMainFile() {
|
|
77 for (const auto &Bucket : IncludeDirectives) {
|
|
78 const FileIncludes &FileDirectives = Bucket.second;
|
|
79
|
|
80 // Emit fixits for all restricted includes.
|
|
81 for (const auto &Include : FileDirectives) {
|
|
82 // Fetch the length of the include statement from the start to just after
|
|
83 // the newline, for finding the end (including the newline).
|
|
84 unsigned ToLen = std::strcspn(SM.getCharacterData(Include.Loc), "\n") + 1;
|
|
85 CharSourceRange ToRange = CharSourceRange::getCharRange(
|
|
86 Include.Loc, Include.Loc.getLocWithOffset(ToLen));
|
|
87
|
|
88 if (!Include.IsInMainFile) {
|
|
89 auto D = Check.diag(
|
|
90 Include.Loc,
|
|
91 "system include %0 not allowed, transitively included from %1");
|
|
92 D << Include.IncludeFile << SM.getFilename(Include.Loc);
|
|
93 D << FixItHint::CreateRemoval(ToRange);
|
|
94 continue;
|
|
95 }
|
|
96 auto D = Check.diag(Include.Loc, "system include %0 not allowed");
|
|
97 D << Include.IncludeFile;
|
|
98 D << FixItHint::CreateRemoval(ToRange);
|
|
99 }
|
|
100 }
|
|
101 }
|
|
102
|
|
103 void RestrictSystemIncludesCheck::registerPPCallbacks(
|
|
104 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
|
|
105 PP->addPPCallbacks(
|
|
106 std::make_unique<RestrictedIncludesPPCallbacks>(*this, SM));
|
|
107 }
|
|
108
|
|
109 void RestrictSystemIncludesCheck::storeOptions(
|
|
110 ClangTidyOptions::OptionMap &Opts) {
|
|
111 Options.store(Opts, "Includes", AllowedIncludes);
|
|
112 }
|
|
113
|
|
114 } // namespace fuchsia
|
|
115 } // namespace tidy
|
|
116 } // namespace clang
|