clang-tools  8.0.0
NonPrivateMemberVariablesInClassesCheck.cpp
Go to the documentation of this file.
1 //===--- NonPrivateMemberVariablesInClassesCheck.cpp - clang-tidy ---------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace tidy {
18 namespace misc {
19 
20 namespace {
21 
22 AST_MATCHER(CXXRecordDecl, hasMethods) {
23  return std::distance(Node.method_begin(), Node.method_end()) != 0;
24 }
25 
26 AST_MATCHER(CXXRecordDecl, hasNonStaticNonImplicitMethod) {
27  return hasMethod(unless(anyOf(isStaticStorageClass(), isImplicit())))
28  .matches(Node, Finder, Builder);
29 }
30 
31 AST_MATCHER(CXXRecordDecl, hasNonPublicMemberVariable) {
32  return cxxRecordDecl(has(fieldDecl(unless(isPublic()))))
33  .matches(Node, Finder, Builder);
34 }
35 
36 AST_POLYMORPHIC_MATCHER_P(boolean, AST_POLYMORPHIC_SUPPORTED_TYPES(Stmt, Decl),
37  bool, Boolean) {
38  return Boolean;
39 }
40 
41 } // namespace
42 
43 NonPrivateMemberVariablesInClassesCheck::
44  NonPrivateMemberVariablesInClassesCheck(StringRef Name,
45  ClangTidyContext *Context)
46  : ClangTidyCheck(Name, Context),
47  IgnoreClassesWithAllMemberVariablesBeingPublic(
48  Options.get("IgnoreClassesWithAllMemberVariablesBeingPublic", false)),
49  IgnorePublicMemberVariables(
50  Options.get("IgnorePublicMemberVariables", false)) {}
51 
53  MatchFinder *Finder) {
54  if (!getLangOpts().CPlusPlus)
55  return;
56 
57  // We can ignore structs/classes with all member variables being public.
58  auto ShouldIgnoreRecord =
59  allOf(boolean(IgnoreClassesWithAllMemberVariablesBeingPublic),
60  unless(hasNonPublicMemberVariable()));
61 
62  // There are three visibility types: public, protected, private.
63  // If we are ok with public fields, then we only want to complain about
64  // protected fields, else we want to complain about all non-private fields.
65  // We can ignore public member variables in structs/classes, in unions.
66  auto InterestingField = fieldDecl(
67  IgnorePublicMemberVariables ? isProtected() : unless(isPrivate()));
68 
69  // We only want the records that not only contain the mutable data (non-static
70  // member variables), but also have some logic (non-static, non-implicit
71  // member functions). We may optionally ignore records where all the member
72  // variables are public.
73  Finder->addMatcher(cxxRecordDecl(anyOf(isStruct(), isClass()), hasMethods(),
74  hasNonStaticNonImplicitMethod(),
75  unless(ShouldIgnoreRecord),
76  forEach(InterestingField.bind("field")))
77  .bind("record"),
78  this);
79 }
80 
82  const MatchFinder::MatchResult &Result) {
83  const auto *Field = Result.Nodes.getNodeAs<FieldDecl>("field");
84  assert(Field && "We should have the field we are going to complain about");
85 
86  diag(Field->getLocation(), "member variable %0 has %1 visibility")
87  << Field << Field->getAccess();
88 }
89 
90 } // namespace misc
91 } // namespace tidy
92 } // namespace clang
AST_MATCHER(BinaryOperator, isAssignmentOperator)
Definition: Matchers.h:20
LangOptions getLangOpts() const
Returns the language options from the context.
Definition: ClangTidy.h:187
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Base class for all clang-tidy checks.
Definition: ClangTidy.h:127
static constexpr llvm::StringLiteral Name
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
static bool isPublic(const clang::AccessSpecifier AS, const clang::Linkage Link)
Definition: Serialize.cpp:189
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidy.cpp:438