clang-tools 10.0.0

RestrictSystemIncludesCheck.cpp
Go to the documentation of this file.
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
Loc
SourceLocation Loc
&#39;#&#39; location in the include directive
Definition: IncludeOrderCheck.cpp:37
clang::tidy::fuchsia::RestrictedIncludesPPCallbacks
Definition: RestrictSystemIncludesCheck.cpp:23
clang::tidy::fuchsia::RestrictSystemIncludesCheck::registerPPCallbacks
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
Definition: RestrictSystemIncludesCheck.cpp:103
clang::tidy::fuchsia::RestrictSystemIncludesCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: RestrictSystemIncludesCheck.cpp:109
clang::tidy::fuchsia::RestrictSystemIncludesCheck
Checks for allowed includes and suggests removal of any others.
Definition: RestrictSystemIncludesCheck.h:24
clang::tidy::fuchsia::RestrictedIncludesPPCallbacks::RestrictedIncludesPPCallbacks
RestrictedIncludesPPCallbacks(RestrictSystemIncludesCheck &Check, const SourceManager &SM)
Definition: RestrictSystemIncludesCheck.cpp:25
clang::tidy::fuchsia::RestrictedIncludesPPCallbacks::EndOfMainFile
void EndOfMainFile() override
Definition: RestrictSystemIncludesCheck.cpp:76
RestrictSystemIncludesCheck.h
Filename
std::string Filename
Filename as a string.
Definition: IncludeOrderCheck.cpp:39
IsAngled
bool IsAngled
true if this was an include with angle brackets
Definition: IncludeOrderCheck.cpp:40
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:97
FileName
PathRef FileName
Definition: CodeComplete.cpp:1018
PPCallbacks
clang::tidy::fuchsia::RestrictedIncludesPPCallbacks::InclusionDirective
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File, StringRef SearchPath, StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override
Definition: RestrictSystemIncludesCheck.cpp:59
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition: BadSignalToKillThreadCheck.cpp:28
Range
CharSourceRange Range
SourceRange for the file name.
Definition: IncludeOrderCheck.cpp:38