brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.0 KiB · 4225c3e Raw
78 lines · cpp
1//===----------------------------------------------------------------------===//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-exception6//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::tidy::portability {20 21void 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(FileType)) {27    SmallString<256> FullPath;28    llvm::sys::path::append(FullPath, SearchPath);29    llvm::sys::path::append(FullPath, RelativePath);30    // Bucket the allowed include directives by the id of the file they were31    // declared in.32    IncludeDirectives[SM.getFileID(HashLoc)].emplace_back(33        HashLoc, FilenameRange, FileName, FullPath.str(),34        SM.isInMainFile(HashLoc));35  }36}37 38void 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 after45      // the newline, for finding the end (including the newline).46      const unsigned ToLen =47          std::strcspn(SM.getCharacterData(Include.Loc), "\n") + 1;48      const CharSourceRange ToRange = CharSourceRange::getCharRange(49          Include.Loc, Include.Loc.getLocWithOffset(ToLen));50 51      if (!Include.IsInMainFile) {52        auto D = Check.diag(53            Include.Loc,54            "system include %0 not allowed, transitively included from %1");55        D << Include.IncludeFile << SM.getFilename(Include.Loc);56        D << FixItHint::CreateRemoval(ToRange);57        continue;58      }59      auto D = Check.diag(Include.Loc, "system include %0 not allowed");60      D << Include.IncludeFile;61      D << FixItHint::CreateRemoval(ToRange);62    }63  }64}65 66void RestrictSystemIncludesCheck::registerPPCallbacks(67    const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {68  PP->addPPCallbacks(69      std::make_unique<RestrictedIncludesPPCallbacks>(*this, SM));70}71 72void RestrictSystemIncludesCheck::storeOptions(73    ClangTidyOptions::OptionMap &Opts) {74  Options.store(Opts, "Includes", AllowedIncludes);75}76 77} // namespace clang::tidy::portability78