brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.8 KiB · ecd8e19 Raw
68 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 "RestrictSystemLibcHeadersCheck.h"10#include "clang/AST/ASTContext.h"11#include "clang/Lex/HeaderSearch.h"12#include "clang/Lex/HeaderSearchOptions.h"13#include "clang/Lex/Preprocessor.h"14 15// FixItHint - Hint to check documentation script to mark this check as16// providing a FixIt.17 18namespace clang::tidy::llvm_libc {19 20namespace {21 22class RestrictedIncludesPPCallbacks23    : public portability::RestrictedIncludesPPCallbacks {24public:25  explicit RestrictedIncludesPPCallbacks(RestrictSystemLibcHeadersCheck &Check,26                                         const SourceManager &SM,27                                         SmallString<128> CompilerIncudeDir)28      : portability::RestrictedIncludesPPCallbacks(Check, SM),29        CompilerIncudeDir(std::move(CompilerIncudeDir)) {}30 31  void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,32                          StringRef FileName, bool IsAngled,33                          CharSourceRange FilenameRange,34                          OptionalFileEntryRef File, StringRef SearchPath,35                          StringRef RelativePath, const Module *SuggestedModule,36                          bool ModuleImported,37                          SrcMgr::CharacteristicKind FileType) override;38 39private:40  const SmallString<128> CompilerIncudeDir;41};42 43} // namespace44 45void RestrictedIncludesPPCallbacks::InclusionDirective(46    SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,47    bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File,48    StringRef SearchPath, StringRef RelativePath, const Module *SuggestedModule,49    bool ModuleImported, SrcMgr::CharacteristicKind FileType) {50  // Compiler provided headers are allowed (e.g stddef.h).51  if (SrcMgr::isSystem(FileType) && SearchPath == CompilerIncudeDir)52    return;53  portability::RestrictedIncludesPPCallbacks::InclusionDirective(54      HashLoc, IncludeTok, FileName, IsAngled, FilenameRange, File, SearchPath,55      RelativePath, SuggestedModule, ModuleImported, FileType);56}57 58void RestrictSystemLibcHeadersCheck::registerPPCallbacks(59    const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {60  SmallString<128> CompilerIncudeDir =61      StringRef(PP->getHeaderSearchInfo().getHeaderSearchOpts().ResourceDir);62  llvm::sys::path::append(CompilerIncudeDir, "include");63  PP->addPPCallbacks(std::make_unique<RestrictedIncludesPPCallbacks>(64      *this, SM, std::move(CompilerIncudeDir)));65}66 67} // namespace clang::tidy::llvm_libc68