[clang-tidy] Add module for llvm-libc and restrict-system-libc-header-check.
Summary: This adds a new module to enforce standards specific to the llvm-libc project. This change also adds the first check which restricts user from including system libc headers accidentally which can lead to subtle bugs that would be a challenge to detect.
Reviewers: alexfh, hokein, aaron.ballman
Reviewed By: aaron.ballman
Subscribers: juliehockett, arphaman, jfb, abrachet, sivachandra, Eugene.Zelenko, njames93, mgorny, xazax.hun, MaskRay, cfe-commits
Tags: #clang-tools-extra, #libc-project, #clang
Differential Revision: https://reviews.llvm.org/D75332
2020-03-12 11:38:05 -07:00
|
|
|
//===--- RestrictSystemLibcHeadersCheck.cpp - clang-tidy ------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "RestrictSystemLibcHeadersCheck.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
|
|
|
#include "clang/Lex/HeaderSearchOptions.h"
|
2020-06-29 16:05:51 +01:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
[clang-tidy] Add module for llvm-libc and restrict-system-libc-header-check.
Summary: This adds a new module to enforce standards specific to the llvm-libc project. This change also adds the first check which restricts user from including system libc headers accidentally which can lead to subtle bugs that would be a challenge to detect.
Reviewers: alexfh, hokein, aaron.ballman
Reviewed By: aaron.ballman
Subscribers: juliehockett, arphaman, jfb, abrachet, sivachandra, Eugene.Zelenko, njames93, mgorny, xazax.hun, MaskRay, cfe-commits
Tags: #clang-tools-extra, #libc-project, #clang
Differential Revision: https://reviews.llvm.org/D75332
2020-03-12 11:38:05 -07:00
|
|
|
|
2020-06-22 11:07:21 +01:00
|
|
|
// FixItHint - Hint to check documentation script to mark this check as
|
|
|
|
// providing a FixIt.
|
|
|
|
|
2023-01-14 17:40:54 +00:00
|
|
|
namespace clang::tidy::llvm_libc {
|
[clang-tidy] Add module for llvm-libc and restrict-system-libc-header-check.
Summary: This adds a new module to enforce standards specific to the llvm-libc project. This change also adds the first check which restricts user from including system libc headers accidentally which can lead to subtle bugs that would be a challenge to detect.
Reviewers: alexfh, hokein, aaron.ballman
Reviewed By: aaron.ballman
Subscribers: juliehockett, arphaman, jfb, abrachet, sivachandra, Eugene.Zelenko, njames93, mgorny, xazax.hun, MaskRay, cfe-commits
Tags: #clang-tools-extra, #libc-project, #clang
Differential Revision: https://reviews.llvm.org/D75332
2020-03-12 11:38:05 -07:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-03-20 15:52:39 -07:00
|
|
|
class RestrictedIncludesPPCallbacks
|
|
|
|
: public portability::RestrictedIncludesPPCallbacks {
|
[clang-tidy] Add module for llvm-libc and restrict-system-libc-header-check.
Summary: This adds a new module to enforce standards specific to the llvm-libc project. This change also adds the first check which restricts user from including system libc headers accidentally which can lead to subtle bugs that would be a challenge to detect.
Reviewers: alexfh, hokein, aaron.ballman
Reviewed By: aaron.ballman
Subscribers: juliehockett, arphaman, jfb, abrachet, sivachandra, Eugene.Zelenko, njames93, mgorny, xazax.hun, MaskRay, cfe-commits
Tags: #clang-tools-extra, #libc-project, #clang
Differential Revision: https://reviews.llvm.org/D75332
2020-03-12 11:38:05 -07:00
|
|
|
public:
|
|
|
|
explicit RestrictedIncludesPPCallbacks(
|
|
|
|
RestrictSystemLibcHeadersCheck &Check, const SourceManager &SM,
|
|
|
|
const SmallString<128> CompilerIncudeDir)
|
2020-03-20 15:52:39 -07:00
|
|
|
: portability::RestrictedIncludesPPCallbacks(Check, SM),
|
|
|
|
CompilerIncudeDir(CompilerIncudeDir) {}
|
[clang-tidy] Add module for llvm-libc and restrict-system-libc-header-check.
Summary: This adds a new module to enforce standards specific to the llvm-libc project. This change also adds the first check which restricts user from including system libc headers accidentally which can lead to subtle bugs that would be a challenge to detect.
Reviewers: alexfh, hokein, aaron.ballman
Reviewed By: aaron.ballman
Subscribers: juliehockett, arphaman, jfb, abrachet, sivachandra, Eugene.Zelenko, njames93, mgorny, xazax.hun, MaskRay, cfe-commits
Tags: #clang-tools-extra, #libc-project, #clang
Differential Revision: https://reviews.llvm.org/D75332
2020-03-12 11:38:05 -07:00
|
|
|
|
|
|
|
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
|
|
|
|
StringRef FileName, bool IsAngled,
|
2022-04-11 12:10:05 +02:00
|
|
|
CharSourceRange FilenameRange,
|
2022-12-20 00:15:11 +01:00
|
|
|
OptionalFileEntryRef File, StringRef SearchPath,
|
2024-02-08 19:19:18 +01:00
|
|
|
StringRef RelativePath, const Module *SuggestedModule,
|
|
|
|
bool ModuleImported,
|
[clang-tidy] Add module for llvm-libc and restrict-system-libc-header-check.
Summary: This adds a new module to enforce standards specific to the llvm-libc project. This change also adds the first check which restricts user from including system libc headers accidentally which can lead to subtle bugs that would be a challenge to detect.
Reviewers: alexfh, hokein, aaron.ballman
Reviewed By: aaron.ballman
Subscribers: juliehockett, arphaman, jfb, abrachet, sivachandra, Eugene.Zelenko, njames93, mgorny, xazax.hun, MaskRay, cfe-commits
Tags: #clang-tools-extra, #libc-project, #clang
Differential Revision: https://reviews.llvm.org/D75332
2020-03-12 11:38:05 -07:00
|
|
|
SrcMgr::CharacteristicKind FileType) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
const SmallString<128> CompilerIncudeDir;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void RestrictedIncludesPPCallbacks::InclusionDirective(
|
|
|
|
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
|
2022-12-20 00:15:11 +01:00
|
|
|
bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File,
|
2024-02-08 19:19:18 +01:00
|
|
|
StringRef SearchPath, StringRef RelativePath, const Module *SuggestedModule,
|
|
|
|
bool ModuleImported, SrcMgr::CharacteristicKind FileType) {
|
2020-03-20 15:52:39 -07:00
|
|
|
// Compiler provided headers are allowed (e.g stddef.h).
|
|
|
|
if (SrcMgr::isSystem(FileType) && SearchPath == CompilerIncudeDir)
|
|
|
|
return;
|
|
|
|
portability::RestrictedIncludesPPCallbacks::InclusionDirective(
|
|
|
|
HashLoc, IncludeTok, FileName, IsAngled, FilenameRange, File, SearchPath,
|
2024-02-08 19:19:18 +01:00
|
|
|
RelativePath, SuggestedModule, ModuleImported, FileType);
|
[clang-tidy] Add module for llvm-libc and restrict-system-libc-header-check.
Summary: This adds a new module to enforce standards specific to the llvm-libc project. This change also adds the first check which restricts user from including system libc headers accidentally which can lead to subtle bugs that would be a challenge to detect.
Reviewers: alexfh, hokein, aaron.ballman
Reviewed By: aaron.ballman
Subscribers: juliehockett, arphaman, jfb, abrachet, sivachandra, Eugene.Zelenko, njames93, mgorny, xazax.hun, MaskRay, cfe-commits
Tags: #clang-tools-extra, #libc-project, #clang
Differential Revision: https://reviews.llvm.org/D75332
2020-03-12 11:38:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void RestrictSystemLibcHeadersCheck::registerPPCallbacks(
|
|
|
|
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
|
|
|
|
SmallString<128> CompilerIncudeDir =
|
|
|
|
StringRef(PP->getHeaderSearchInfo().getHeaderSearchOpts().ResourceDir);
|
|
|
|
llvm::sys::path::append(CompilerIncudeDir, "include");
|
|
|
|
PP->addPPCallbacks(std::make_unique<RestrictedIncludesPPCallbacks>(
|
|
|
|
*this, SM, CompilerIncudeDir));
|
|
|
|
}
|
|
|
|
|
2023-01-14 17:40:54 +00:00
|
|
|
} // namespace clang::tidy::llvm_libc
|