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