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::tidy::portability { |
20 | |
21 | void RestrictedIncludesPPCallbacks::InclusionDirective( |
22 | SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, |
23 | bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File, |
24 | StringRef SearchPath, StringRef RelativePath, const Module *SuggestedModule, |
25 | bool ModuleImported, SrcMgr::CharacteristicKind FileType) { |
26 | if (!Check.contains(FileName) && SrcMgr::isSystem(CK: FileType)) { |
27 | SmallString<256> FullPath; |
28 | llvm::sys::path::append(path&: FullPath, a: SearchPath); |
29 | llvm::sys::path::append(path&: FullPath, a: RelativePath); |
30 | // Bucket the allowed include directives by the id of the file they were |
31 | // declared in. |
32 | IncludeDirectives[SM.getFileID(SpellingLoc: HashLoc)].emplace_back( |
33 | Args&: HashLoc, Args&: FilenameRange, Args&: FileName, Args: FullPath.str(), |
34 | Args: SM.isInMainFile(Loc: HashLoc)); |
35 | } |
36 | } |
37 | |
38 | void RestrictedIncludesPPCallbacks::EndOfMainFile() { |
39 | for (const auto &Bucket : IncludeDirectives) { |
40 | const FileIncludes &FileDirectives = Bucket.second; |
41 | |
42 | // Emit fixits for all restricted includes. |
43 | for (const auto &Include : FileDirectives) { |
44 | // Fetch the length of the include statement from the start to just after |
45 | // the newline, for finding the end (including the newline). |
46 | unsigned ToLen = std::strcspn(s: SM.getCharacterData(SL: Include.Loc), reject: "\n" ) + 1; |
47 | CharSourceRange ToRange = CharSourceRange::getCharRange( |
48 | B: Include.Loc, E: Include.Loc.getLocWithOffset(Offset: ToLen)); |
49 | |
50 | if (!Include.IsInMainFile) { |
51 | auto D = Check.diag( |
52 | Loc: Include.Loc, |
53 | Description: "system include %0 not allowed, transitively included from %1" ); |
54 | D << Include.IncludeFile << SM.getFilename(SpellingLoc: Include.Loc); |
55 | D << FixItHint::CreateRemoval(RemoveRange: ToRange); |
56 | continue; |
57 | } |
58 | auto D = Check.diag(Loc: Include.Loc, Description: "system include %0 not allowed" ); |
59 | D << Include.IncludeFile; |
60 | D << FixItHint::CreateRemoval(RemoveRange: ToRange); |
61 | } |
62 | } |
63 | } |
64 | |
65 | void RestrictSystemIncludesCheck::registerPPCallbacks( |
66 | const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) { |
67 | PP->addPPCallbacks( |
68 | C: std::make_unique<RestrictedIncludesPPCallbacks>(args&: *this, args: SM)); |
69 | } |
70 | |
71 | void RestrictSystemIncludesCheck::storeOptions( |
72 | ClangTidyOptions::OptionMap &Opts) { |
73 | Options.store(Options&: Opts, LocalName: "Includes" , Value: AllowedIncludes); |
74 | } |
75 | |
76 | } // namespace clang::tidy::portability |
77 | |