11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/ASTMatchers/ASTMatchers.h"
14 #include "clang/Lex/Lexer.h"
16 using namespace clang::ast_matchers;
21 namespace readability {
23 GlobalNamesInHeadersCheck::GlobalNamesInHeadersCheck(StringRef
Name,
26 RawStringHeaderFileExtensions(
27 Options.getLocalOrGlobal(
"HeaderFileExtensions",
"h")) {
31 llvm::errs() <<
"Invalid header file extension: "
32 << RawStringHeaderFileExtensions <<
"\n";
38 Options.
store(Opts,
"HeaderFileExtensions", RawStringHeaderFileExtensions);
44 decl(anyOf(usingDecl(), usingDirectiveDecl()),
45 hasDeclContext(translationUnitDecl())).bind(
"using_decl"),
50 const auto *D = Result.Nodes.getNodeAs<Decl>(
"using_decl");
52 if (D->getLocStart().isMacroID())
56 if (Result.SourceManager->isInMainFile(
57 Result.SourceManager->getExpansionLoc(D->getLocStart()))) {
60 D->getLocStart(), *Result.SourceManager, HeaderFileExtensions))
64 if (
const auto* UsingDirective = dyn_cast<UsingDirectiveDecl>(D)) {
65 if (UsingDirective->getNominatedNamespace()->isAnonymousNamespace()) {
74 diag(D->getLocStart(),
75 "using declarations in the global namespace in headers are prohibited");
bool parseHeaderFileExtensions(StringRef AllHeaderFileExtensions, HeaderFileExtensionsSet &HeaderFileExtensions, char delimiter)
Parses header file extensions from a semicolon-separated list.
std::unique_ptr< ast_matchers::MatchFinder > Finder
bool isSpellingLocInHeaderFile(SourceLocation Loc, SourceManager &SM, const HeaderFileExtensionsSet &HeaderFileExtensions)
Checks whether spelling location of Loc is in header file.
Base class for all clang-tidy checks.
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
std::map< std::string, std::string > OptionMap
ClangTidyContext & Context
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.