[clang-tidy] Non-private member variables in classes (MISRA, CppCoreGuidelines, HICPP)
Summary:
Finds classes that not only contain the data (non-static member variables),
but also have logic (non-static member functions), and diagnoses all member
variables that have any other scope other than `private`. They should be
made `private`, and manipulated exclusively via the member functions.
Optionally, classes with all member variables being `public` could be
ignored, and optionally all `public` member variables could be ignored.
Options
-------
* IgnoreClassesWithAllMemberVariablesBeingPublic
Allows to completely ignore classes if **all** the member variables in that
class have `public` visibility.
* IgnorePublicMemberVariables
Allows to ignore (not diagnose) **all** the member variables with `public`
visibility scope.
References:
* MISRA 11-0-1 Member data in non-POD class types shall be private.
* https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c2-use-class-if-the-class-has-an-invariant-use-struct-if-the-data-members-can-vary-independently
* https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rc-private
* https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rh-protected
Reviewers: JonasToth, aaron.ballman, alexfh, hokein, xazax.hun
Reviewed By: aaron.ballman
Subscribers: Eugene.Zelenko, zinovy.nis, cfe-commits, rnkovacs, nemanjai, mgorny, xazax.hun, kbarton
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D52771
llvm-svn: 344757
2018-10-19 04:16:44 +08:00
|
|
|
//===--- NonPrivateMemberVariablesInClassesCheck.cpp - clang-tidy ---------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
[clang-tidy] Non-private member variables in classes (MISRA, CppCoreGuidelines, HICPP)
Summary:
Finds classes that not only contain the data (non-static member variables),
but also have logic (non-static member functions), and diagnoses all member
variables that have any other scope other than `private`. They should be
made `private`, and manipulated exclusively via the member functions.
Optionally, classes with all member variables being `public` could be
ignored, and optionally all `public` member variables could be ignored.
Options
-------
* IgnoreClassesWithAllMemberVariablesBeingPublic
Allows to completely ignore classes if **all** the member variables in that
class have `public` visibility.
* IgnorePublicMemberVariables
Allows to ignore (not diagnose) **all** the member variables with `public`
visibility scope.
References:
* MISRA 11-0-1 Member data in non-POD class types shall be private.
* https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c2-use-class-if-the-class-has-an-invariant-use-struct-if-the-data-members-can-vary-independently
* https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rc-private
* https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rh-protected
Reviewers: JonasToth, aaron.ballman, alexfh, hokein, xazax.hun
Reviewed By: aaron.ballman
Subscribers: Eugene.Zelenko, zinovy.nis, cfe-commits, rnkovacs, nemanjai, mgorny, xazax.hun, kbarton
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D52771
llvm-svn: 344757
2018-10-19 04:16:44 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "NonPrivateMemberVariablesInClassesCheck.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace misc {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
AST_MATCHER(CXXRecordDecl, hasMethods) {
|
|
|
|
return std::distance(Node.method_begin(), Node.method_end()) != 0;
|
|
|
|
}
|
|
|
|
|
2019-01-20 22:28:27 +08:00
|
|
|
AST_MATCHER(CXXRecordDecl, hasNonStaticNonImplicitMethod) {
|
|
|
|
return hasMethod(unless(anyOf(isStaticStorageClass(), isImplicit())))
|
[clang-tidy] Non-private member variables in classes (MISRA, CppCoreGuidelines, HICPP)
Summary:
Finds classes that not only contain the data (non-static member variables),
but also have logic (non-static member functions), and diagnoses all member
variables that have any other scope other than `private`. They should be
made `private`, and manipulated exclusively via the member functions.
Optionally, classes with all member variables being `public` could be
ignored, and optionally all `public` member variables could be ignored.
Options
-------
* IgnoreClassesWithAllMemberVariablesBeingPublic
Allows to completely ignore classes if **all** the member variables in that
class have `public` visibility.
* IgnorePublicMemberVariables
Allows to ignore (not diagnose) **all** the member variables with `public`
visibility scope.
References:
* MISRA 11-0-1 Member data in non-POD class types shall be private.
* https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c2-use-class-if-the-class-has-an-invariant-use-struct-if-the-data-members-can-vary-independently
* https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rc-private
* https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rh-protected
Reviewers: JonasToth, aaron.ballman, alexfh, hokein, xazax.hun
Reviewed By: aaron.ballman
Subscribers: Eugene.Zelenko, zinovy.nis, cfe-commits, rnkovacs, nemanjai, mgorny, xazax.hun, kbarton
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D52771
llvm-svn: 344757
2018-10-19 04:16:44 +08:00
|
|
|
.matches(Node, Finder, Builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
AST_MATCHER(CXXRecordDecl, hasNonPublicMemberVariable) {
|
|
|
|
return cxxRecordDecl(has(fieldDecl(unless(isPublic()))))
|
|
|
|
.matches(Node, Finder, Builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
AST_POLYMORPHIC_MATCHER_P(boolean, AST_POLYMORPHIC_SUPPORTED_TYPES(Stmt, Decl),
|
|
|
|
bool, Boolean) {
|
|
|
|
return Boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
NonPrivateMemberVariablesInClassesCheck::
|
|
|
|
NonPrivateMemberVariablesInClassesCheck(StringRef Name,
|
|
|
|
ClangTidyContext *Context)
|
|
|
|
: ClangTidyCheck(Name, Context),
|
|
|
|
IgnoreClassesWithAllMemberVariablesBeingPublic(
|
|
|
|
Options.get("IgnoreClassesWithAllMemberVariablesBeingPublic", false)),
|
|
|
|
IgnorePublicMemberVariables(
|
|
|
|
Options.get("IgnorePublicMemberVariables", false)) {}
|
|
|
|
|
|
|
|
void NonPrivateMemberVariablesInClassesCheck::registerMatchers(
|
|
|
|
MatchFinder *Finder) {
|
|
|
|
if (!getLangOpts().CPlusPlus)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// We can ignore structs/classes with all member variables being public.
|
|
|
|
auto ShouldIgnoreRecord =
|
|
|
|
allOf(boolean(IgnoreClassesWithAllMemberVariablesBeingPublic),
|
|
|
|
unless(hasNonPublicMemberVariable()));
|
|
|
|
|
|
|
|
// There are three visibility types: public, protected, private.
|
|
|
|
// If we are ok with public fields, then we only want to complain about
|
|
|
|
// protected fields, else we want to complain about all non-private fields.
|
|
|
|
// We can ignore public member variables in structs/classes, in unions.
|
|
|
|
auto InterestingField = fieldDecl(
|
|
|
|
IgnorePublicMemberVariables ? isProtected() : unless(isPrivate()));
|
|
|
|
|
2018-11-25 10:41:01 +08:00
|
|
|
// We only want the records that not only contain the mutable data (non-static
|
2019-01-20 22:28:27 +08:00
|
|
|
// member variables), but also have some logic (non-static, non-implicit
|
|
|
|
// member functions). We may optionally ignore records where all the member
|
|
|
|
// variables are public.
|
2018-11-25 10:41:01 +08:00
|
|
|
Finder->addMatcher(cxxRecordDecl(anyOf(isStruct(), isClass()), hasMethods(),
|
2019-01-20 22:28:27 +08:00
|
|
|
hasNonStaticNonImplicitMethod(),
|
2018-11-25 10:41:01 +08:00
|
|
|
unless(ShouldIgnoreRecord),
|
[clang-tidy] Non-private member variables in classes (MISRA, CppCoreGuidelines, HICPP)
Summary:
Finds classes that not only contain the data (non-static member variables),
but also have logic (non-static member functions), and diagnoses all member
variables that have any other scope other than `private`. They should be
made `private`, and manipulated exclusively via the member functions.
Optionally, classes with all member variables being `public` could be
ignored, and optionally all `public` member variables could be ignored.
Options
-------
* IgnoreClassesWithAllMemberVariablesBeingPublic
Allows to completely ignore classes if **all** the member variables in that
class have `public` visibility.
* IgnorePublicMemberVariables
Allows to ignore (not diagnose) **all** the member variables with `public`
visibility scope.
References:
* MISRA 11-0-1 Member data in non-POD class types shall be private.
* https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c2-use-class-if-the-class-has-an-invariant-use-struct-if-the-data-members-can-vary-independently
* https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rc-private
* https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rh-protected
Reviewers: JonasToth, aaron.ballman, alexfh, hokein, xazax.hun
Reviewed By: aaron.ballman
Subscribers: Eugene.Zelenko, zinovy.nis, cfe-commits, rnkovacs, nemanjai, mgorny, xazax.hun, kbarton
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D52771
llvm-svn: 344757
2018-10-19 04:16:44 +08:00
|
|
|
forEach(InterestingField.bind("field")))
|
|
|
|
.bind("record"),
|
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NonPrivateMemberVariablesInClassesCheck::check(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
|
|
|
const auto *Field = Result.Nodes.getNodeAs<FieldDecl>("field");
|
|
|
|
assert(Field && "We should have the field we are going to complain about");
|
|
|
|
|
|
|
|
diag(Field->getLocation(), "member variable %0 has %1 visibility")
|
|
|
|
<< Field << Field->getAccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace misc
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|