[clang-tidy] Move fuchsia-restrict-system-includes to portability module for general use.
Summary:
Created a general check for restrict-system-includes under portability as recommend in the comments under D75332. I also fleshed out the user facing documentation to show examples for common use-cases such as allow-list, block-list, and wild carding.
Removed fuchsia's check as per phosek sugguestion.
Reviewers: aaron.ballman, phosek, alexfh, hokein, njames93
Reviewed By: phosek
Subscribers: Eugene.Zelenko, mgorny, xazax.hun, phosek, cfe-commits, MaskRay
Tags: #clang-tools-extra, #clang
Differential Revision: https://reviews.llvm.org/D75786
2020-03-11 01:28:23 +08:00
|
|
|
//===--- RestrictSystemIncludesCheck.h - clang-tidy --------------*- C++-*-===//
|
2018-05-12 05:08:59 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2018-05-12 05:08:59 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[clang-tidy] Move fuchsia-restrict-system-includes to portability module for general use.
Summary:
Created a general check for restrict-system-includes under portability as recommend in the comments under D75332. I also fleshed out the user facing documentation to show examples for common use-cases such as allow-list, block-list, and wild carding.
Removed fuchsia's check as per phosek sugguestion.
Reviewers: aaron.ballman, phosek, alexfh, hokein, njames93
Reviewed By: phosek
Subscribers: Eugene.Zelenko, mgorny, xazax.hun, phosek, cfe-commits, MaskRay
Tags: #clang-tools-extra, #clang
Differential Revision: https://reviews.llvm.org/D75786
2020-03-11 01:28:23 +08:00
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_RESTRICTINCLUDESSCHECK_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_RESTRICTINCLUDESSCHECK_H
|
2018-05-12 05:08:59 +08:00
|
|
|
|
2019-03-25 20:38:26 +08:00
|
|
|
#include "../ClangTidyCheck.h"
|
2019-08-26 23:44:32 +08:00
|
|
|
#include "../GlobList.h"
|
2020-06-29 23:05:51 +08:00
|
|
|
#include "clang/Lex/PPCallbacks.h"
|
2018-05-12 05:08:59 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
[clang-tidy] Move fuchsia-restrict-system-includes to portability module for general use.
Summary:
Created a general check for restrict-system-includes under portability as recommend in the comments under D75332. I also fleshed out the user facing documentation to show examples for common use-cases such as allow-list, block-list, and wild carding.
Removed fuchsia's check as per phosek sugguestion.
Reviewers: aaron.ballman, phosek, alexfh, hokein, njames93
Reviewed By: phosek
Subscribers: Eugene.Zelenko, mgorny, xazax.hun, phosek, cfe-commits, MaskRay
Tags: #clang-tools-extra, #clang
Differential Revision: https://reviews.llvm.org/D75786
2020-03-11 01:28:23 +08:00
|
|
|
namespace portability {
|
2018-05-12 05:08:59 +08:00
|
|
|
|
|
|
|
/// Checks for allowed includes and suggests removal of any others. If no
|
|
|
|
/// includes are specified, the check will exit without issuing any warnings.
|
|
|
|
///
|
|
|
|
/// For the user-facing documentation see:
|
[clang-tidy] Move fuchsia-restrict-system-includes to portability module for general use.
Summary:
Created a general check for restrict-system-includes under portability as recommend in the comments under D75332. I also fleshed out the user facing documentation to show examples for common use-cases such as allow-list, block-list, and wild carding.
Removed fuchsia's check as per phosek sugguestion.
Reviewers: aaron.ballman, phosek, alexfh, hokein, njames93
Reviewed By: phosek
Subscribers: Eugene.Zelenko, mgorny, xazax.hun, phosek, cfe-commits, MaskRay
Tags: #clang-tools-extra, #clang
Differential Revision: https://reviews.llvm.org/D75786
2020-03-11 01:28:23 +08:00
|
|
|
/// http://clang.llvm.org/extra/clang-tidy/checks/portability-restrict-system-includes.html
|
2018-05-12 05:08:59 +08:00
|
|
|
class RestrictSystemIncludesCheck : public ClangTidyCheck {
|
|
|
|
public:
|
2020-03-21 06:52:39 +08:00
|
|
|
RestrictSystemIncludesCheck(StringRef Name, ClangTidyContext *Context,
|
|
|
|
std::string DefaultAllowedIncludes = "*")
|
2018-05-12 05:08:59 +08:00
|
|
|
: ClangTidyCheck(Name, Context),
|
2020-03-21 06:52:39 +08:00
|
|
|
AllowedIncludes(Options.get("Includes", DefaultAllowedIncludes)),
|
2018-05-12 05:08:59 +08:00
|
|
|
AllowedIncludesGlobList(AllowedIncludes) {}
|
|
|
|
|
2019-03-23 02:58:12 +08:00
|
|
|
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
|
|
|
|
Preprocessor *ModuleExpanderPP) override;
|
2018-05-12 05:08:59 +08:00
|
|
|
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
|
|
|
|
bool contains(StringRef FileName) {
|
|
|
|
return AllowedIncludesGlobList.contains(FileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string AllowedIncludes;
|
|
|
|
GlobList AllowedIncludesGlobList;
|
|
|
|
};
|
|
|
|
|
2020-03-21 06:52:39 +08:00
|
|
|
class RestrictedIncludesPPCallbacks : public PPCallbacks {
|
|
|
|
public:
|
|
|
|
explicit RestrictedIncludesPPCallbacks(RestrictSystemIncludesCheck &Check,
|
|
|
|
const SourceManager &SM)
|
|
|
|
: Check(Check), SM(SM) {}
|
|
|
|
|
|
|
|
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
|
|
|
|
StringRef FileName, bool IsAngled,
|
|
|
|
CharSourceRange FilenameRange, const FileEntry *File,
|
|
|
|
StringRef SearchPath, StringRef RelativePath,
|
|
|
|
const Module *Imported,
|
|
|
|
SrcMgr::CharacteristicKind FileType) override;
|
|
|
|
void EndOfMainFile() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct IncludeDirective {
|
|
|
|
IncludeDirective() = default;
|
|
|
|
IncludeDirective(SourceLocation Loc, CharSourceRange Range,
|
|
|
|
StringRef Filename, StringRef FullPath, bool IsInMainFile)
|
|
|
|
: Loc(Loc), Range(Range), IncludeFile(Filename), IncludePath(FullPath),
|
|
|
|
IsInMainFile(IsInMainFile) {}
|
|
|
|
|
|
|
|
SourceLocation Loc; // '#' location in the include directive
|
|
|
|
CharSourceRange Range; // SourceRange for the file name
|
|
|
|
std::string IncludeFile; // Filename as a string
|
|
|
|
std::string IncludePath; // Full file path as a string
|
|
|
|
bool IsInMainFile; // Whether or not the include is in the main file
|
|
|
|
};
|
|
|
|
|
|
|
|
using FileIncludes = llvm::SmallVector<IncludeDirective, 8>;
|
|
|
|
llvm::SmallDenseMap<FileID, FileIncludes> IncludeDirectives;
|
|
|
|
|
|
|
|
RestrictSystemIncludesCheck &Check;
|
|
|
|
const SourceManager &SM;
|
|
|
|
};
|
|
|
|
|
[clang-tidy] Move fuchsia-restrict-system-includes to portability module for general use.
Summary:
Created a general check for restrict-system-includes under portability as recommend in the comments under D75332. I also fleshed out the user facing documentation to show examples for common use-cases such as allow-list, block-list, and wild carding.
Removed fuchsia's check as per phosek sugguestion.
Reviewers: aaron.ballman, phosek, alexfh, hokein, njames93
Reviewed By: phosek
Subscribers: Eugene.Zelenko, mgorny, xazax.hun, phosek, cfe-commits, MaskRay
Tags: #clang-tools-extra, #clang
Differential Revision: https://reviews.llvm.org/D75786
2020-03-11 01:28:23 +08:00
|
|
|
} // namespace portability
|
2018-05-12 05:08:59 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|
|
|
|
|
2020-03-21 06:52:39 +08:00
|
|
|
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_RESTRICTINCLUDESSCHECK_H
|