10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
20 return std::distance(Node.method_begin(), Node.method_end()) != 0;
23AST_MATCHER(CXXRecordDecl, hasNonStaticNonImplicitMethod) {
24 return hasMethod(unless(anyOf(isStaticStorageClass(), isImplicit())))
25 .matches(Node, Finder, Builder);
28AST_MATCHER(CXXRecordDecl, hasNonPublicMemberVariable) {
29 return cxxRecordDecl(has(fieldDecl(unless(isPublic()))))
30 .matches(Node, Finder, Builder);
33AST_POLYMORPHIC_MATCHER_P(
boolean, AST_POLYMORPHIC_SUPPORTED_TYPES(Stmt, Decl),
44 IgnoreClassesWithAllMemberVariablesBeingPublic(
45 Options.get(
"IgnoreClassesWithAllMemberVariablesBeingPublic", false)),
46 IgnorePublicMemberVariables(
47 Options.get(
"IgnorePublicMemberVariables", false)) {}
51 Options.store(Opts,
"IgnoreClassesWithAllMemberVariablesBeingPublic",
52 IgnoreClassesWithAllMemberVariablesBeingPublic);
53 Options.store(Opts,
"IgnorePublicMemberVariables",
54 IgnorePublicMemberVariables);
58 MatchFinder *Finder) {
60 auto ShouldIgnoreRecord =
61 allOf(
boolean(IgnoreClassesWithAllMemberVariablesBeingPublic),
62 unless(hasNonPublicMemberVariable()));
68 auto InterestingField = IgnorePublicMemberVariables
69 ? fieldDecl(isProtected())
70 : fieldDecl(unless(isPrivate()));
76 Finder->addMatcher(cxxRecordDecl(anyOf(isStruct(), isClass()), hasMethods(),
77 hasNonStaticNonImplicitMethod(),
78 unless(ShouldIgnoreRecord),
79 forEach(InterestingField.bind(
"field")))
85 const MatchFinder::MatchResult &Result) {
86 const auto *Field = Result.Nodes.getNodeAs<FieldDecl>(
"field");
87 assert(Field &&
"We should have the field we are going to complain about");
89 diag(Field->getLocation(),
"member variable %0 has %1 visibility")
90 << Field << Field->getAccess();
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
NonPrivateMemberVariablesInClassesCheck(StringRef Name, ClangTidyContext *Context)
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
AST_MATCHER(BinaryOperator, isRelationalOperator)
llvm::StringMap< ClangTidyValue > OptionMap