clang-tools  14.0.0git
NonPrivateMemberVariablesInClassesCheck.cpp
Go to the documentation of this file.
1 //===--- NonPrivateMemberVariablesInClassesCheck.cpp - clang-tidy ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 
13 using namespace clang::ast_matchers;
14 
15 namespace clang {
16 namespace tidy {
17 namespace misc {
18 
19 namespace {
20 
21 AST_MATCHER(CXXRecordDecl, hasMethods) {
22  return std::distance(Node.method_begin(), Node.method_end()) != 0;
23 }
24 
25 AST_MATCHER(CXXRecordDecl, hasNonStaticNonImplicitMethod) {
26  return hasMethod(unless(anyOf(isStaticStorageClass(), isImplicit())))
27  .matches(Node, Finder, Builder);
28 }
29 
30 AST_MATCHER(CXXRecordDecl, hasNonPublicMemberVariable) {
31  return cxxRecordDecl(has(fieldDecl(unless(isPublic()))))
32  .matches(Node, Finder, Builder);
33 }
34 
35 AST_POLYMORPHIC_MATCHER_P(boolean, AST_POLYMORPHIC_SUPPORTED_TYPES(Stmt, Decl),
36  bool, Boolean) {
37  return Boolean;
38 }
39 
40 } // namespace
41 
42 NonPrivateMemberVariablesInClassesCheck::
43  NonPrivateMemberVariablesInClassesCheck(StringRef Name,
44  ClangTidyContext *Context)
45  : ClangTidyCheck(Name, Context),
46  IgnoreClassesWithAllMemberVariablesBeingPublic(
47  Options.get("IgnoreClassesWithAllMemberVariablesBeingPublic", false)),
48  IgnorePublicMemberVariables(
49  Options.get("IgnorePublicMemberVariables", false)) {}
50 
53  Options.store(Opts, "IgnoreClassesWithAllMemberVariablesBeingPublic",
54  IgnoreClassesWithAllMemberVariablesBeingPublic);
55  Options.store(Opts, "IgnorePublicMemberVariables",
56  IgnorePublicMemberVariables);
57 }
58 
60  MatchFinder *Finder) {
61  // We can ignore structs/classes with all member variables being public.
62  auto ShouldIgnoreRecord =
63  allOf(boolean(IgnoreClassesWithAllMemberVariablesBeingPublic),
64  unless(hasNonPublicMemberVariable()));
65 
66  // There are three visibility types: public, protected, private.
67  // If we are ok with public fields, then we only want to complain about
68  // protected fields, else we want to complain about all non-private fields.
69  // We can ignore public member variables in structs/classes, in unions.
70  auto InterestingField = IgnorePublicMemberVariables
71  ? fieldDecl(isProtected())
72  : fieldDecl(unless(isPrivate()));
73 
74  // We only want the records that not only contain the mutable data (non-static
75  // member variables), but also have some logic (non-static, non-implicit
76  // member functions). We may optionally ignore records where all the member
77  // variables are public.
78  Finder->addMatcher(cxxRecordDecl(anyOf(isStruct(), isClass()), hasMethods(),
79  hasNonStaticNonImplicitMethod(),
80  unless(ShouldIgnoreRecord),
81  forEach(InterestingField.bind("field")))
82  .bind("record"),
83  this);
84 }
85 
87  const MatchFinder::MatchResult &Result) {
88  const auto *Field = Result.Nodes.getNodeAs<FieldDecl>("field");
89  assert(Field && "We should have the field we are going to complain about");
90 
91  diag(Field->getLocation(), "member variable %0 has %1 visibility")
92  << Field << Field->getAccess();
93 }
94 
95 } // namespace misc
96 } // namespace tidy
97 } // namespace clang
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
NonPrivateMemberVariablesInClassesCheck.h
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:54
clang::tidy::misc::NonPrivateMemberVariablesInClassesCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: NonPrivateMemberVariablesInClassesCheck.cpp:59
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::clangd::SymbolKind::Boolean
@ Boolean
Decl
const FunctionDecl * Decl
Definition: AvoidBindCheck.cpp:100
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:416
Builder
CodeCompletionBuilder Builder
Definition: CodeCompletionStringsTests.cpp:36
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:72
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidyCheck.cpp:25
clang::ast_matchers::AST_MATCHER
AST_MATCHER(Expr, isMacroID)
Definition: PreferIsaOrDynCastInConditionalsCheck.cpp:19
clang::tidy::misc::NonPrivateMemberVariablesInClassesCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: NonPrivateMemberVariablesInClassesCheck.cpp:86
clang::doc::serialize::isPublic
static bool isPublic(const clang::AccessSpecifier AS, const clang::Linkage Link)
Definition: Serialize.cpp:223
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::misc::NonPrivateMemberVariablesInClassesCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: NonPrivateMemberVariablesInClassesCheck.cpp:51
clang::tidy::ClangTidyCheck::OptionsView::store
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Definition: ClangTidyCheck.cpp:120