|
| 1 | +//===--- RestrictSystemLibcHeadersCheck.cpp - clang-tidy ------------------===// |
| 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-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "RestrictSystemLibcHeadersCheck.h" |
| 10 | +#include "clang/AST/ASTContext.h" |
| 11 | +#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 12 | +#include "clang/Lex/HeaderSearch.h" |
| 13 | +#include "clang/Lex/HeaderSearchOptions.h" |
| 14 | + |
| 15 | +namespace clang { |
| 16 | +namespace tidy { |
| 17 | +namespace llvm_libc { |
| 18 | + |
| 19 | +namespace { |
| 20 | + |
| 21 | +class RestrictedIncludesPPCallbacks : public PPCallbacks { |
| 22 | +public: |
| 23 | + explicit RestrictedIncludesPPCallbacks( |
| 24 | + RestrictSystemLibcHeadersCheck &Check, const SourceManager &SM, |
| 25 | + const SmallString<128> CompilerIncudeDir) |
| 26 | + : Check(Check), SM(SM), CompilerIncudeDir(CompilerIncudeDir) {} |
| 27 | + |
| 28 | + void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, |
| 29 | + StringRef FileName, bool IsAngled, |
| 30 | + CharSourceRange FilenameRange, const FileEntry *File, |
| 31 | + StringRef SearchPath, StringRef RelativePath, |
| 32 | + const Module *Imported, |
| 33 | + SrcMgr::CharacteristicKind FileType) override; |
| 34 | + |
| 35 | +private: |
| 36 | + RestrictSystemLibcHeadersCheck &Check; |
| 37 | + const SourceManager &SM; |
| 38 | + const SmallString<128> CompilerIncudeDir; |
| 39 | +}; |
| 40 | + |
| 41 | +} // namespace |
| 42 | + |
| 43 | +void RestrictedIncludesPPCallbacks::InclusionDirective( |
| 44 | + SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, |
| 45 | + bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File, |
| 46 | + StringRef SearchPath, StringRef RelativePath, const Module *Imported, |
| 47 | + SrcMgr::CharacteristicKind FileType) { |
| 48 | + if (SrcMgr::isSystem(FileType)) { |
| 49 | + // Compiler provided headers are allowed (e.g stddef.h). |
| 50 | + if (SearchPath == CompilerIncudeDir) return; |
| 51 | + if (!SM.isInMainFile(HashLoc)) { |
| 52 | + Check.diag( |
| 53 | + HashLoc, |
| 54 | + "system libc header %0 not allowed, transitively included from %1") |
| 55 | + << FileName << SM.getFilename(HashLoc); |
| 56 | + } else { |
| 57 | + Check.diag(HashLoc, "system libc header %0 not allowed") << FileName; |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +void RestrictSystemLibcHeadersCheck::registerPPCallbacks( |
| 63 | + const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) { |
| 64 | + SmallString<128> CompilerIncudeDir = |
| 65 | + StringRef(PP->getHeaderSearchInfo().getHeaderSearchOpts().ResourceDir); |
| 66 | + llvm::sys::path::append(CompilerIncudeDir, "include"); |
| 67 | + PP->addPPCallbacks(std::make_unique<RestrictedIncludesPPCallbacks>( |
| 68 | + *this, SM, CompilerIncudeDir)); |
| 69 | +} |
| 70 | + |
| 71 | +} // namespace llvm_libc |
| 72 | +} // namespace tidy |
| 73 | +} // namespace clang |
0 commit comments