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