10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Lex/Lexer.h"
19 MatchFinder *Finder) {
21 namedDecl(anyOf(functionDecl(isDefinition(), isStaticStorageClass()),
22 varDecl(isDefinition(), isStaticStorageClass())),
23 isInAnonymousNamespace())
29 const MatchFinder::MatchResult &Result) {
30 const auto *Def = Result.Nodes.getNodeAs<NamedDecl>(
"static-def");
32 if (Def->getLocation().isMacroID())
36 const DeclContext *DC = Def->getDeclContext();
37 if (DC->getDeclKind() != Decl::Namespace)
41 diag(Def->getLocation(),
"%0 is a static definition in "
42 "anonymous namespace; static is redundant here")
45 SourceLocation
Loc = Def->getSourceRange().getBegin();
46 while (Loc < Def->getSourceRange().getEnd() &&
47 !Lexer::getRawToken(
Loc, Tok, *Result.SourceManager,
getLangOpts(),
49 SourceRange TokenRange(Tok.getLocation(), Tok.getEndLoc());
50 StringRef SourceText =
51 Lexer::getSourceText(CharSourceRange::getTokenRange(TokenRange),
53 if (SourceText ==
"static") {
54 Diag << FixItHint::CreateRemoval(TokenRange);
57 Loc = Tok.getEndLoc();
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.