blob: e42e32cd72fd3b29eef76f4822e59b1d3c8d63cf [file] [log] [blame]
Julie Hockettb2b9ed82018-05-11 21:08:59 +00001//===--- RestrictSystemIncludesCheck.cpp - clang-tidy----------------------===//
2//
Chandler Carruthb1ace232019-01-19 08:50:56 +00003// 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
Julie Hockettb2b9ed82018-05-11 21:08:59 +00006//
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
19namespace clang {
20namespace tidy {
21namespace fuchsia {
22
23class RestrictedIncludesPPCallbacks : public PPCallbacks {
24public:
25 explicit RestrictedIncludesPPCallbacks(RestrictSystemIncludesCheck &Check,
26 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
37private:
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 SourceManager &SM;
57};
58
59void 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
76void 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
103void RestrictSystemIncludesCheck::registerPPCallbacks(
104 CompilerInstance &Compiler) {
105 Compiler.getPreprocessor().addPPCallbacks(
106 llvm::make_unique<RestrictedIncludesPPCallbacks>(
107 *this, Compiler.getSourceManager()));
108}
109
110void RestrictSystemIncludesCheck::storeOptions(
111 ClangTidyOptions::OptionMap &Opts) {
112 Options.store(Opts, "Includes", AllowedIncludes);
113}
114
115} // namespace fuchsia
116} // namespace tidy
117} // namespace clang