xref: /llvm-project/clang-tools-extra/clang-tidy/portability/RestrictSystemIncludesCheck.cpp (revision da95d926f6fce4ed9707c77908ad96624268f134)
1ebdb98f2SPaula Toth //===--- RestrictSystemIncludesCheck.cpp - clang-tidy ---------------------===//
2ebdb98f2SPaula Toth //
3ebdb98f2SPaula Toth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4ebdb98f2SPaula Toth // See https://llvm.org/LICENSE.txt for license information.
5ebdb98f2SPaula Toth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ebdb98f2SPaula Toth //
7ebdb98f2SPaula Toth //===----------------------------------------------------------------------===//
8ebdb98f2SPaula Toth 
9ebdb98f2SPaula Toth #include "RestrictSystemIncludesCheck.h"
10ebdb98f2SPaula Toth #include "clang/Frontend/CompilerInstance.h"
11ebdb98f2SPaula Toth #include "clang/Lex/HeaderSearch.h"
12ebdb98f2SPaula Toth #include "clang/Lex/PPCallbacks.h"
13ebdb98f2SPaula Toth #include "clang/Lex/Preprocessor.h"
14ebdb98f2SPaula Toth #include "llvm/ADT/DenseMap.h"
15ebdb98f2SPaula Toth #include "llvm/ADT/SmallVector.h"
16ebdb98f2SPaula Toth #include "llvm/Support/Path.h"
17ebdb98f2SPaula Toth #include <cstring>
18ebdb98f2SPaula Toth 
197d2ea6c4SCarlos Galvez namespace clang::tidy::portability {
20ebdb98f2SPaula Toth 
InclusionDirective(SourceLocation HashLoc,const Token & IncludeTok,StringRef FileName,bool IsAngled,CharSourceRange FilenameRange,OptionalFileEntryRef File,StringRef SearchPath,StringRef RelativePath,const Module * SuggestedModule,bool ModuleImported,SrcMgr::CharacteristicKind FileType)21ebdb98f2SPaula Toth void RestrictedIncludesPPCallbacks::InclusionDirective(
22ebdb98f2SPaula Toth     SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
23854c10f8SBenjamin Kramer     bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File,
24*da95d926SJan Svoboda     StringRef SearchPath, StringRef RelativePath, const Module *SuggestedModule,
25*da95d926SJan Svoboda     bool ModuleImported, SrcMgr::CharacteristicKind FileType) {
26ebdb98f2SPaula Toth   if (!Check.contains(FileName) && SrcMgr::isSystem(FileType)) {
27ebdb98f2SPaula Toth     SmallString<256> FullPath;
28ebdb98f2SPaula Toth     llvm::sys::path::append(FullPath, SearchPath);
29ebdb98f2SPaula Toth     llvm::sys::path::append(FullPath, RelativePath);
30ebdb98f2SPaula Toth     // Bucket the allowed include directives by the id of the file they were
31ebdb98f2SPaula Toth     // declared in.
32ebdb98f2SPaula Toth     IncludeDirectives[SM.getFileID(HashLoc)].emplace_back(
33ebdb98f2SPaula Toth         HashLoc, FilenameRange, FileName, FullPath.str(),
34ebdb98f2SPaula Toth         SM.isInMainFile(HashLoc));
35ebdb98f2SPaula Toth   }
36ebdb98f2SPaula Toth }
37ebdb98f2SPaula Toth 
EndOfMainFile()38ebdb98f2SPaula Toth void RestrictedIncludesPPCallbacks::EndOfMainFile() {
39ebdb98f2SPaula Toth   for (const auto &Bucket : IncludeDirectives) {
40ebdb98f2SPaula Toth     const FileIncludes &FileDirectives = Bucket.second;
41ebdb98f2SPaula Toth 
42ebdb98f2SPaula Toth     // Emit fixits for all restricted includes.
43ebdb98f2SPaula Toth     for (const auto &Include : FileDirectives) {
44ebdb98f2SPaula Toth       // Fetch the length of the include statement from the start to just after
45ebdb98f2SPaula Toth       // the newline, for finding the end (including the newline).
46ebdb98f2SPaula Toth       unsigned ToLen = std::strcspn(SM.getCharacterData(Include.Loc), "\n") + 1;
47ebdb98f2SPaula Toth       CharSourceRange ToRange = CharSourceRange::getCharRange(
48ebdb98f2SPaula Toth           Include.Loc, Include.Loc.getLocWithOffset(ToLen));
49ebdb98f2SPaula Toth 
50ebdb98f2SPaula Toth       if (!Include.IsInMainFile) {
51ebdb98f2SPaula Toth         auto D = Check.diag(
52ebdb98f2SPaula Toth             Include.Loc,
53ebdb98f2SPaula Toth             "system include %0 not allowed, transitively included from %1");
54ebdb98f2SPaula Toth         D << Include.IncludeFile << SM.getFilename(Include.Loc);
55ebdb98f2SPaula Toth         D << FixItHint::CreateRemoval(ToRange);
56ebdb98f2SPaula Toth         continue;
57ebdb98f2SPaula Toth       }
58ebdb98f2SPaula Toth       auto D = Check.diag(Include.Loc, "system include %0 not allowed");
59ebdb98f2SPaula Toth       D << Include.IncludeFile;
60ebdb98f2SPaula Toth       D << FixItHint::CreateRemoval(ToRange);
61ebdb98f2SPaula Toth     }
62ebdb98f2SPaula Toth   }
63ebdb98f2SPaula Toth }
64ebdb98f2SPaula Toth 
registerPPCallbacks(const SourceManager & SM,Preprocessor * PP,Preprocessor * ModuleExpanderPP)65ebdb98f2SPaula Toth void RestrictSystemIncludesCheck::registerPPCallbacks(
66ebdb98f2SPaula Toth     const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
67ebdb98f2SPaula Toth   PP->addPPCallbacks(
68ebdb98f2SPaula Toth       std::make_unique<RestrictedIncludesPPCallbacks>(*this, SM));
69ebdb98f2SPaula Toth }
70ebdb98f2SPaula Toth 
storeOptions(ClangTidyOptions::OptionMap & Opts)71ebdb98f2SPaula Toth void RestrictSystemIncludesCheck::storeOptions(
72ebdb98f2SPaula Toth     ClangTidyOptions::OptionMap &Opts) {
73ebdb98f2SPaula Toth   Options.store(Opts, "Includes", AllowedIncludes);
74ebdb98f2SPaula Toth }
75ebdb98f2SPaula Toth 
767d2ea6c4SCarlos Galvez } // namespace clang::tidy::portability
77