10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/Lexer.h"
18 namespace readability {
20 void StaticDefinitionInAnonymousNamespaceCheck::registerMatchers(
21 MatchFinder *Finder) {
23 namedDecl(anyOf(functionDecl(isDefinition(), isStaticStorageClass()),
24 varDecl(isDefinition(), isStaticStorageClass())),
30 void StaticDefinitionInAnonymousNamespaceCheck::check(
31 const MatchFinder::MatchResult &Result) {
32 const auto *Def = Result.Nodes.getNodeAs<NamedDecl>(
"static-def");
34 if (Def->getLocation().isMacroID())
38 const DeclContext *DC = Def->getDeclContext();
39 if (DC->getDeclKind() != Decl::Namespace)
43 diag(Def->getLocation(),
"%0 is a static definition in "
44 "anonymous namespace; static is redundant here")
47 SourceLocation
Loc = Def->getSourceRange().getBegin();
48 while (Loc < Def->getSourceRange().getEnd() &&
49 !Lexer::getRawToken(
Loc, Tok, *Result.SourceManager, getLangOpts(),
51 SourceRange TokenRange(Tok.getLocation(), Tok.getEndLoc());
52 StringRef SourceText =
53 Lexer::getSourceText(CharSourceRange::getTokenRange(TokenRange),
54 *Result.SourceManager, getLangOpts());
55 if (SourceText ==
"static") {
56 Diag << FixItHint::CreateRemoval(TokenRange);
59 Loc = Tok.getEndLoc();