10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
17AST_POLYMORPHIC_MATCHER_P(isInHeaderFile,
18 AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
22 Node.getBeginLoc(), Finder->getASTContext().getSourceManager(),
23 HeaderFileExtensions);
27 return llvm::isa<CXXMethodDecl>(&Node);
29AST_MATCHER(VarDecl, isStaticDataMember) {
return Node.isStaticDataMember(); }
35 HeaderFileExtensions(Context->getHeaderFileExtensions()) {}
39 functionDecl(isStaticStorageClass(),
40 unless(anyOf(isInHeaderFile(HeaderFileExtensions),
41 isInAnonymousNamespace(), isMemberFunction())))
45 varDecl(isStaticStorageClass(),
46 unless(anyOf(isInHeaderFile(HeaderFileExtensions),
47 isInAnonymousNamespace(), isStaticLocal(),
48 isStaticDataMember(), hasType(isConstQualified()))))
54 if (
const auto *MatchedDecl = Result.Nodes.getNodeAs<NamedDecl>(
"x")) {
55 StringRef
Type = llvm::isa<VarDecl>(MatchedDecl) ?
"variable" :
"function";
56 diag(MatchedDecl->getLocation(),
57 "%0 %1 declared 'static', move to anonymous namespace instead")
58 <<
Type << MatchedDecl;
llvm::SmallString< 256U > Name
::clang::DynTypedNode Node
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this 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.
UseAnonymousNamespaceCheck(StringRef Name, ClangTidyContext *Context)
AST_MATCHER(Expr, isMacroID)
bool isExpansionLocInHeaderFile(SourceLocation Loc, const SourceManager &SM, const FileExtensionsSet &HeaderFileExtensions)
Checks whether expansion location of Loc is in header file.
llvm::SmallSet< llvm::StringRef, 5 > FileExtensionsSet