[clang-tidy] Non-private member variables in classes (MISRA, CppCoreGuidelines, HICPP)
authorRoman Lebedev <lebedev.ri@gmail.com>
Thu, 18 Oct 2018 20:16:44 +0000 (20:16 +0000)
committerRoman Lebedev <lebedev.ri@gmail.com>
Thu, 18 Oct 2018 20:16:44 +0000 (20:16 +0000)
commit6cfa38f1f1f84851af6b53b894e36defa6c8cb27
treeed7aeb13368694a5e0ad09a7ce15b263ae2a5075
parenta9b271b32035f90f3efba9074b7906df5c9256dc
[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
clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
clang-tools-extra/clang-tidy/misc/CMakeLists.txt
clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
clang-tools-extra/clang-tidy/misc/NonPrivateMemberVariablesInClassesCheck.cpp [new file with mode: 0644]
clang-tools-extra/clang-tidy/misc/NonPrivateMemberVariablesInClassesCheck.h [new file with mode: 0644]
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines-non-private-member-variables-in-classes.rst [new file with mode: 0644]
clang-tools-extra/docs/clang-tidy/checks/list.rst
clang-tools-extra/docs/clang-tidy/checks/misc-non-private-member-variables-in-classes.rst [new file with mode: 0644]
clang-tools-extra/test/clang-tidy/misc-non-private-member-variables-in-classes.cpp [new file with mode: 0644]