398 lines · cpp
1// RUN: %check_clang_tidy -check-suffixes=PUBLIC,NONPRIVATE,PROTECTED %s misc-non-private-member-variables-in-classes %t2// RUN: %check_clang_tidy -check-suffixes=PUBLIC,NONPRIVATE,PROTECTED %s misc-non-private-member-variables-in-classes %t -- -config='{CheckOptions: {misc-non-private-member-variables-in-classes.IgnorePublicMemberVariables: false, misc-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic: false}}' --3// RUN: %check_clang_tidy -check-suffixes=PUBLIC,PROTECTED %s misc-non-private-member-variables-in-classes %t -- -config='{CheckOptions: {misc-non-private-member-variables-in-classes.IgnorePublicMemberVariables: false, misc-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic: true}}' --4// RUN: %check_clang_tidy -check-suffixes=PUBLIC,PROTECTED %s cppcoreguidelines-non-private-member-variables-in-classes %t -- --5// RUN: %check_clang_tidy -check-suffixes=PROTECTED %s misc-non-private-member-variables-in-classes %t -- -config='{CheckOptions: {misc-non-private-member-variables-in-classes.IgnorePublicMemberVariables: true, misc-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic: false}}' --6// RUN: %check_clang_tidy -check-suffixes=PROTECTED %s misc-non-private-member-variables-in-classes %t -- -config='{CheckOptions: {misc-non-private-member-variables-in-classes.IgnorePublicMemberVariables: true, misc-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic: true}}' --7 8//----------------------------------------------------------------------------//9 10// Only data, do not warn11 12struct S0 {13 int S0_v0;14 15public:16 int S0_v1;17 18protected:19 int S0_v2;20 21private:22 int S0_v3;23};24 25class S1 {26 int S1_v0;27 28public:29 int S1_v1;30 31protected:32 int S1_v2;33 34private:35 int S1_v3;36};37 38// Only data and implicit or static methods, do not warn39 40class C {41public:42 C() {}43 ~C() {}44};45 46struct S1Implicit {47 C S1Implicit_v0;48};49 50struct S1ImplicitAndStatic {51 C S1Implicit_v0;52 static void s() {}53};54 55//----------------------------------------------------------------------------//56 57// All functions are static, do not warn.58 59struct S2 {60 static void S2_m0();61 int S2_v0;62 63public:64 static void S2_m1();65 int S2_v1;66 67protected:68 static void S2_m3();69 int S2_v2;70 71private:72 static void S2_m4();73 int S2_v3;74};75 76class S3 {77 static void S3_m0();78 int S3_v0;79 80public:81 static void S3_m1();82 int S3_v1;83 84protected:85 static void S3_m3();86 int S3_v2;87 88private:89 static void S3_m4();90 int S3_v3;91};92 93//============================================================================//94 95// union != struct/class. do not diagnose.96 97union U0 {98 void U0_m0();99 int U0_v0;100 101public:102 void U0_m1();103 int U0_v1;104 105protected:106 void U0_m2();107 int U0_v2;108 109private:110 void U0_m3();111 int U0_v3;112};113 114//============================================================================//115 116// Has non-static method with default visibility.117 118struct S4 {119 void S4_m0();120 121 int S4_v0;122 // CHECK-MESSAGES-PUBLIC: :[[@LINE-1]]:7: warning: member variable 'S4_v0' has public visibility123public:124 int S4_v1;125 // CHECK-MESSAGES-PUBLIC: :[[@LINE-1]]:7: warning: member variable 'S4_v1' has public visibility126protected:127 int S4_v2;128 // CHECK-MESSAGES-PROTECTED: :[[@LINE-1]]:7: warning: member variable 'S4_v2' has protected visibility129private:130 int S4_v3;131};132 133class S5 {134 void S5_m0();135 136 int S5_v0;137 138public:139 int S5_v1;140 // CHECK-MESSAGES-PUBLIC: :[[@LINE-1]]:7: warning: member variable 'S5_v1' has public visibility141protected:142 int S5_v2;143 // CHECK-MESSAGES-PROTECTED: :[[@LINE-1]]:7: warning: member variable 'S5_v2' has protected visibility144private:145 int S5_v3;146};147 148//----------------------------------------------------------------------------//149 150// Has non-static method with public visibility.151 152struct S6 {153 int S6_v0;154 // CHECK-MESSAGES-PUBLIC: :[[@LINE-1]]:7: warning: member variable 'S6_v0' has public visibility155public:156 void S6_m0();157 int S6_v1;158 // CHECK-MESSAGES-PUBLIC: :[[@LINE-1]]:7: warning: member variable 'S6_v1' has public visibility159protected:160 int S6_v2;161 // CHECK-MESSAGES-PROTECTED: :[[@LINE-1]]:7: warning: member variable 'S6_v2' has protected visibility162private:163 int S6_v3;164};165 166class S7 {167 int S7_v0;168 169public:170 void S7_m0();171 int S7_v1;172 // CHECK-MESSAGES-PUBLIC: :[[@LINE-1]]:7: warning: member variable 'S7_v1' has public visibility173protected:174 int S7_v2;175 // CHECK-MESSAGES-PROTECTED: :[[@LINE-1]]:7: warning: member variable 'S7_v2' has protected visibility176private:177 int S7_v3;178};179 180//----------------------------------------------------------------------------//181 182// Has non-static method with protected visibility.183 184struct S8 {185 int S8_v0;186 // CHECK-MESSAGES-PUBLIC: :[[@LINE-1]]:7: warning: member variable 'S8_v0' has public visibility187public:188 int S8_v1;189 // CHECK-MESSAGES-PUBLIC: :[[@LINE-1]]:7: warning: member variable 'S8_v1' has public visibility190protected:191 void S8_m0();192 int S8_v2;193 // CHECK-MESSAGES-PROTECTED: :[[@LINE-1]]:7: warning: member variable 'S8_v2' has protected visibility194private:195 int S8_v3;196};197 198class S9 {199 int S9_v0;200 201public:202 int S9_v1;203 // CHECK-MESSAGES-PUBLIC: :[[@LINE-1]]:7: warning: member variable 'S9_v1' has public visibility204protected:205 void S9_m0();206 int S9_v2;207 // CHECK-MESSAGES-PROTECTED: :[[@LINE-1]]:7: warning: member variable 'S9_v2' has protected visibility208private:209 int S9_v3;210};211 212//----------------------------------------------------------------------------//213 214// Has non-static method with private visibility.215 216struct S10 {217 int S10_v0;218 // CHECK-MESSAGES-PUBLIC: :[[@LINE-1]]:7: warning: member variable 'S10_v0' has public visibility219public:220 int S10_v1;221 // CHECK-MESSAGES-PUBLIC: :[[@LINE-1]]:7: warning: member variable 'S10_v1' has public visibility222protected:223 int S10_v2;224 // CHECK-MESSAGES-PROTECTED: :[[@LINE-1]]:7: warning: member variable 'S10_v2' has protected visibility225private:226 void S10_m0();227 int S10_v3;228};229 230class S11 {231 int S11_v0;232 233public:234 int S11_v1;235 // CHECK-MESSAGES-PUBLIC: :[[@LINE-1]]:7: warning: member variable 'S11_v1' has public visibility236protected:237 int S11_v2;238 // CHECK-MESSAGES-PROTECTED: :[[@LINE-1]]:7: warning: member variable 'S11_v2' has protected visibility239private:240 void S11_m0();241 int S11_v3;242};243 244//============================================================================//245 246// Static variables are ignored.247// Has non-static methods and static variables.248 249struct S12 {250 void S12_m0();251 static int S12_v0;252 253public:254 void S12_m1();255 static int S12_v1;256 257protected:258 void S12_m2();259 static int S12_v2;260 261private:262 void S12_m3();263 static int S12_v3;264};265 266class S13 {267 void S13_m0();268 static int S13_v0;269 270public:271 void S13_m1();272 static int S13_v1;273 274protected:275 void S13_m2();276 static int S13_v2;277 278private:279 void S13_m3();280 static int S13_v3;281};282 283struct S14 {284 void S14_m0();285 int S14_v0;286 // CHECK-MESSAGES-NONPRIVATE: :[[@LINE-1]]:7: warning: member variable 'S14_v0' has public visibility287 288public:289 void S14_m1();290 int S14_v1;291 // CHECK-MESSAGES-NONPRIVATE: :[[@LINE-1]]:7: warning: member variable 'S14_v1' has public visibility292 293protected:294 void S14_m2();295 296private:297 void S14_m3();298};299 300class S15 {301 void S15_m0();302 303public:304 void S15_m1();305 int S15_v1;306 // CHECK-MESSAGES-NONPRIVATE: :[[@LINE-1]]:7: warning: member variable 'S15_v1' has public visibility307 308protected:309 void S15_m2();310 311private:312 void S15_m3();313};314 315//----------------------------------------------------------------------------//316 317template <typename T>318struct S97 {319 void method();320 T S97_v0;321 // CHECK-MESSAGES-NONPRIVATE: :[[@LINE-1]]:5: warning: member variable 'S97_v0' has public visibility322};323 324template struct S97<char *>;325 326template <>327struct S97<double> {328 void method();329 double S97d_v0;330 // CHECK-MESSAGES-NONPRIVATE: :[[@LINE-1]]:10: warning: member variable 'S97d_v0' has public visibility331};332 333//----------------------------------------------------------------------------//334 335#define FIELD(x) int x;336 337// Do diagnose fields originating from macros.338struct S98 {339 void method();340 FIELD(S98_v0);341 // CHECK-MESSAGES-NONPRIVATE: :[[@LINE-1]]:9: warning: member variable 'S98_v0' has public visibility342};343 344//----------------------------------------------------------------------------//345 346// Don't look in descendant classes.347class S99 {348 void method();349 350 struct S99_0 {351 int S99_S0_v0;352 };353 354public:355 struct S99_1 {356 int S99_S0_v0;357 };358 359protected:360 struct S99_2 {361 int S99_S0_v0;362 };363 364private:365 struct S99_3 {366 int S99_S0_v0;367 };368};369 370//----------------------------------------------------------------------------//371 372// Only diagnose once, don't let the inheritance fool you.373struct S100 {374 int S100_v0;375 // CHECK-MESSAGES-NONPRIVATE: :[[@LINE-1]]:7: warning: member variable 'S100_v0' has public visibility376 void m0();377};378struct S101_default_inheritance : S100 {379 int S101_v0;380 // CHECK-MESSAGES-NONPRIVATE: :[[@LINE-1]]:7: warning: member variable 'S101_v0' has public visibility381 void m1();382};383struct S102_public_inheritance : public S100 {384 int S102_v0;385 // CHECK-MESSAGES-NONPRIVATE: :[[@LINE-1]]:7: warning: member variable 'S102_v0' has public visibility386 void m1();387};388struct S103_protected_inheritance : protected S100 {389 int S103_v0;390 // CHECK-MESSAGES-NONPRIVATE: :[[@LINE-1]]:7: warning: member variable 'S103_v0' has public visibility391 void m1();392};393struct S104_private_inheritance : private S100 {394 int S104_v0;395 // CHECK-MESSAGES-NONPRIVATE: :[[@LINE-1]]:7: warning: member variable 'S104_v0' has public visibility396 void m1();397};398