11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Lex/Lexer.h"
15 using namespace clang::ast_matchers;
19 namespace readability {
21 void StaticDefinitionInAnonymousNamespaceCheck::registerMatchers(
24 namedDecl(anyOf(functionDecl(isDefinition(), isStaticStorageClass()),
25 varDecl(isDefinition(), isStaticStorageClass())),
26 hasParent(namespaceDecl(isAnonymous())))
31 void StaticDefinitionInAnonymousNamespaceCheck::check(
32 const MatchFinder::MatchResult &Result) {
33 const auto *Def = Result.Nodes.getNodeAs<NamedDecl>(
"static-def");
35 if (Def->getLocation().isMacroID())
39 const DeclContext *DC = Def->getDeclContext();
40 if (DC->getDeclKind() != Decl::Namespace)
44 diag(Def->getLocation(),
"%0 is a static definition in "
45 "anonymous namespace; static is redundant here")
48 SourceLocation
Loc = Def->getSourceRange().getBegin();
49 while (Loc < Def->getSourceRange().getEnd() &&
50 !Lexer::getRawToken(Loc, Tok, *Result.SourceManager, getLangOpts(),
52 SourceRange TokenRange(Tok.getLocation(), Tok.getEndLoc());
53 StringRef SourceText =
54 Lexer::getSourceText(CharSourceRange::getTokenRange(TokenRange),
55 *Result.SourceManager, getLangOpts());
56 if (SourceText ==
"static") {
57 Diag << FixItHint::CreateRemoval(TokenRange);
60 Loc = Tok.getEndLoc();
SourceLocation Loc
'#' location in the include directive
std::unique_ptr< ast_matchers::MatchFinder > Finder