comparison clang-tools-extra/clang-tidy/llvmlibc/RestrictSystemLibcHeadersCheck.cpp @ 173:0572611fdcc8 llvm10 llvm12

reorgnization done
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 11:55:54 +0900
parents
children 2e18cbf3894f
comparison
equal deleted inserted replaced
172:9fbae9c8bf63 173:0572611fdcc8
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
22 : public portability::RestrictedIncludesPPCallbacks {
23 public:
24 explicit RestrictedIncludesPPCallbacks(
25 RestrictSystemLibcHeadersCheck &Check, const SourceManager &SM,
26 const SmallString<128> CompilerIncudeDir)
27 : portability::RestrictedIncludesPPCallbacks(Check, SM),
28 CompilerIncudeDir(CompilerIncudeDir) {}
29
30 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
31 StringRef FileName, bool IsAngled,
32 CharSourceRange FilenameRange, const FileEntry *File,
33 StringRef SearchPath, StringRef RelativePath,
34 const Module *Imported,
35 SrcMgr::CharacteristicKind FileType) override;
36
37 private:
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 // Compiler provided headers are allowed (e.g stddef.h).
49 if (SrcMgr::isSystem(FileType) && SearchPath == CompilerIncudeDir)
50 return;
51 portability::RestrictedIncludesPPCallbacks::InclusionDirective(
52 HashLoc, IncludeTok, FileName, IsAngled, FilenameRange, File, SearchPath,
53 RelativePath, Imported, FileType);
54 }
55
56 void RestrictSystemLibcHeadersCheck::registerPPCallbacks(
57 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
58 SmallString<128> CompilerIncudeDir =
59 StringRef(PP->getHeaderSearchInfo().getHeaderSearchOpts().ResourceDir);
60 llvm::sys::path::append(CompilerIncudeDir, "include");
61 PP->addPPCallbacks(std::make_unique<RestrictedIncludesPPCallbacks>(
62 *this, SM, CompilerIncudeDir));
63 }
64
65 } // namespace llvm_libc
66 } // namespace tidy
67 } // namespace clang