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/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 as
16// providing a FixIt.
17
18namespace clang::tidy::llvm_libc {
19
20namespace {
21
22class RestrictedIncludesPPCallbacks
23 : public portability::RestrictedIncludesPPCallbacks {
24public:
25 explicit RestrictedIncludesPPCallbacks(
26 RestrictSystemLibcHeadersCheck &Check, const SourceManager &SM,
27 const SmallString<128> CompilerIncudeDir)
28 : portability::RestrictedIncludesPPCallbacks(Check, SM),
29 CompilerIncudeDir(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} // namespace
44
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(CK: 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(path&: CompilerIncudeDir, a: "include");
63 PP->addPPCallbacks(C: std::make_unique<RestrictedIncludesPPCallbacks>(
64 args&: *this, args: SM, args&: CompilerIncudeDir));
65}
66
67} // namespace clang::tidy::llvm_libc
68

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

source code of clang-tools-extra/clang-tidy/llvmlibc/RestrictSystemLibcHeadersCheck.cpp