[clang-tidy] Move fuchsia-restrict-system-includes to portability module for general use.
Summary:
Created a general check for restrict-system-includes under portability as recommend in the comments under D75332. I also fleshed out the user facing documentation to show examples for common use-cases such as allow-list, block-list, and wild carding.
Removed fuchsia's check as per phosek sugguestion.
Reviewers: aaron.ballman, phosek, alexfh, hokein, njames93
Reviewed By: phosek
Subscribers: Eugene.Zelenko, mgorny, xazax.hun, phosek, cfe-commits, MaskRay
Tags: #clang-tools-extra, #clang
Differential Revision: https://reviews.llvm.org/D75786
2020-03-10 10:28:23 -07:00
|
|
|
//===--- RestrictSystemIncludesCheck.cpp - clang-tidy ---------------------===//
|
2018-05-11 21:08:59 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-05-11 21:08:59 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "RestrictSystemIncludesCheck.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
|
|
|
#include "clang/Lex/PPCallbacks.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include <cstring>
|
|
|
|
|
2023-01-14 17:40:54 +00:00
|
|
|
namespace clang::tidy::portability {
|
2018-05-11 21:08:59 +00:00
|
|
|
|
|
|
|
void RestrictedIncludesPPCallbacks::InclusionDirective(
|
|
|
|
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
|
2022-12-20 00:15:11 +01:00
|
|
|
bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File,
|
2024-02-08 19:19:18 +01:00
|
|
|
StringRef SearchPath, StringRef RelativePath, const Module *SuggestedModule,
|
|
|
|
bool ModuleImported, SrcMgr::CharacteristicKind FileType) {
|
2018-05-11 21:08:59 +00:00
|
|
|
if (!Check.contains(FileName) && SrcMgr::isSystem(FileType)) {
|
|
|
|
SmallString<256> FullPath;
|
|
|
|
llvm::sys::path::append(FullPath, SearchPath);
|
|
|
|
llvm::sys::path::append(FullPath, RelativePath);
|
|
|
|
// Bucket the allowed include directives by the id of the file they were
|
|
|
|
// declared in.
|
|
|
|
IncludeDirectives[SM.getFileID(HashLoc)].emplace_back(
|
|
|
|
HashLoc, FilenameRange, FileName, FullPath.str(),
|
|
|
|
SM.isInMainFile(HashLoc));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RestrictedIncludesPPCallbacks::EndOfMainFile() {
|
|
|
|
for (const auto &Bucket : IncludeDirectives) {
|
|
|
|
const FileIncludes &FileDirectives = Bucket.second;
|
|
|
|
|
|
|
|
// Emit fixits for all restricted includes.
|
|
|
|
for (const auto &Include : FileDirectives) {
|
|
|
|
// Fetch the length of the include statement from the start to just after
|
|
|
|
// the newline, for finding the end (including the newline).
|
|
|
|
unsigned ToLen = std::strcspn(SM.getCharacterData(Include.Loc), "\n") + 1;
|
|
|
|
CharSourceRange ToRange = CharSourceRange::getCharRange(
|
|
|
|
Include.Loc, Include.Loc.getLocWithOffset(ToLen));
|
|
|
|
|
|
|
|
if (!Include.IsInMainFile) {
|
|
|
|
auto D = Check.diag(
|
|
|
|
Include.Loc,
|
|
|
|
"system include %0 not allowed, transitively included from %1");
|
|
|
|
D << Include.IncludeFile << SM.getFilename(Include.Loc);
|
|
|
|
D << FixItHint::CreateRemoval(ToRange);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto D = Check.diag(Include.Loc, "system include %0 not allowed");
|
|
|
|
D << Include.IncludeFile;
|
|
|
|
D << FixItHint::CreateRemoval(ToRange);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RestrictSystemIncludesCheck::registerPPCallbacks(
|
2019-03-22 18:58:12 +00:00
|
|
|
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
|
|
|
|
PP->addPPCallbacks(
|
2019-08-14 23:52:23 +00:00
|
|
|
std::make_unique<RestrictedIncludesPPCallbacks>(*this, SM));
|
2018-05-11 21:08:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RestrictSystemIncludesCheck::storeOptions(
|
|
|
|
ClangTidyOptions::OptionMap &Opts) {
|
|
|
|
Options.store(Opts, "Includes", AllowedIncludes);
|
|
|
|
}
|
|
|
|
|
2023-01-14 17:40:54 +00:00
|
|
|
} // namespace clang::tidy::portability
|