CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
78 lines
3.0 KiB
C++
78 lines
3.0 KiB
C++
//===----------------------------------------------------------------------===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#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>
|
|
|
|
namespace clang::tidy::portability {
|
|
|
|
void RestrictedIncludesPPCallbacks::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) {
|
|
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).
|
|
const unsigned ToLen =
|
|
std::strcspn(SM.getCharacterData(Include.Loc), "\n") + 1;
|
|
const 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(
|
|
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
|
|
PP->addPPCallbacks(
|
|
std::make_unique<RestrictedIncludesPPCallbacks>(*this, SM));
|
|
}
|
|
|
|
void RestrictSystemIncludesCheck::storeOptions(
|
|
ClangTidyOptions::OptionMap &Opts) {
|
|
Options.store(Opts, "Includes", AllowedIncludes);
|
|
}
|
|
|
|
} // namespace clang::tidy::portability
|