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 {
24 GlobalNamesInHeadersCheck::registerMatchers(ast_matchers::MatchFinder *
Finder) {
26 decl(anyOf(usingDecl(), usingDirectiveDecl()),
27 hasDeclContext(translationUnitDecl())).bind(
"using_decl"),
31 void GlobalNamesInHeadersCheck::check(
const MatchFinder::MatchResult &
Result) {
32 const auto *D = Result.Nodes.getNodeAs<Decl>(
"using_decl");
34 if (D->getLocStart().isMacroID())
38 if (Result.SourceManager->isInMainFile(
39 Result.SourceManager->getExpansionLoc(D->getLocStart()))) {
41 StringRef
Filename = Result.SourceManager->getFilename(
42 Result.SourceManager->getSpellingLoc(D->getLocStart()));
44 if (!Filename.endswith(
".h"))
48 if (
const auto* UsingDirective = dyn_cast<UsingDirectiveDecl>(D)) {
49 if (UsingDirective->getNominatedNamespace()->isAnonymousNamespace()) {
58 diag(D->getLocStart(),
59 "using declarations in the global namespace in headers are prohibited");
std::unique_ptr< ast_matchers::MatchFinder > Finder
std::string Filename
Filename as a string.