53 MatchFinder *Finder) {
54 const auto IsDefinitionInAnonymousNamespace = allOf(
55 isLexicallyInAnonymousNamespace(), unless(isInMacro()), isDefinition());
57 if (AllowMemberFunctionsInClass) {
59 functionDecl(IsDefinitionInAnonymousNamespace,
60 unless(anyOf(hasParent(cxxRecordDecl()),
61 hasParent(functionTemplateDecl(
62 hasParent(cxxRecordDecl()))))))
67 functionDecl(IsDefinitionInAnonymousNamespace).bind(
"function"),
this);
70 if (!AllowVariableDeclarations)
71 Finder->addMatcher(varDecl(IsDefinitionInAnonymousNamespace,
72 unless(isLocalVariable()), unless(parmVarDecl()))
78 const MatchFinder::MatchResult &Result) {
79 if (
const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>(
"function")) {
80 if (Func->isCXXClassMember())
81 diag(Func->getLocation(),
82 "place definition of method %0 outside of an anonymous namespace")
84 else if (Func->isStatic())
85 diag(Func->getLocation(),
86 "place static function %0 outside of an anonymous namespace")
89 diag(Func->getLocation(),
90 "function %0 is declared in an anonymous namespace; "
91 "prefer using 'static' for restricting visibility")
96 if (
const auto *Var = Result.Nodes.getNodeAs<VarDecl>(
"var")) {
97 if (Var->getStorageClass() == SC_Static)
98 diag(Var->getLocation(),
99 "place static variable %0 outside of an anonymous namespace")
102 diag(Var->getLocation(),
103 "variable %0 is declared in an anonymous namespace; "
104 "prefer using 'static' for restricting visibility")
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.