summaryrefslogtreecommitdiffstats
path: root/test/clang-tidy/misc-non-private-member-variables-in-classes.cpp
blob: 2a93ff6a18c38d7a192d208932ad7eb6167b8237 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// RUN: %check_clang_tidy -check-suffixes=PUBLIC,NONPRIVATE,PROTECTED %s misc-non-private-member-variables-in-classes %t
// 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}]}' --
// 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}]}' --
// RUN: %check_clang_tidy -check-suffixes=PUBLIC,PROTECTED %s cppcoreguidelines-non-private-member-variables-in-classes %t -- --
// 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}]}' --
// 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}]}' --

//----------------------------------------------------------------------------//

// Only data, do not warn

struct S0 {
  int S0_v0;

public:
  int S0_v1;

protected:
  int S0_v2;

private:
  int S0_v3;
};

class S1 {
  int S1_v0;

public:
  int S1_v1;

protected:
  int S1_v2;

private:
  int S1_v3;
};

// Only data and implicit or static methods, do not warn

class C {
public:
  C() {}
  ~C() {}
};

struct S1Implicit {
  C S1Implicit_v0;
};

struct S1ImplicitAndStatic {
  C S1Implicit_v0;
  static void s() {}
};

//----------------------------------------------------------------------------//

// All functions are static, do not warn.

struct S2 {
  static void S2_m0();
  int S2_v0;

public:
  static void S2_m1();
  int S2_v1;

protected:
  static void S2_m3();
  int S2_v2;

private:
  static void S2_m4();
  int S2_v3;
};

class S3 {
  static void S3_m0();
  int S3_v0;

public:
  static void S3_m1();
  int S3_v1;

protected:
  static void S3_m3();
  int S3_v2;

private:
  static void S3_m4();
  int S3_v3;
};

//============================================================================//

// union != struct/class. do not diagnose.

union U0 {
  void U0_m0();
  int U0_v0;

public:
  void U0_m1();
  int U0_v1;

protected:
  void U0_m2();
  int U0_v2;

private:
  void U0_m3();
  int U0_v3;
};

//============================================================================//

// Has non-static method with default visibility.

struct S4 {
  void S4_m0();

  int S4_v0;
  // CHECK-MESSAGES-PUBLIC: :[[@LINE-1]]:7: warning: member variable 'S4_v0' has public visibility
public:
  int S4_v1;
  // CHECK-MESSAGES-PUBLIC: :[[@LINE-1]]:7: warning: member variable 'S4_v1' has public visibility
protected:
  int S4_v2;
  // CHECK-MESSAGES-PROTECTED: :[[@LINE-1]]:7: warning: member variable 'S4_v2' has protected visibility
private:
  int S4_v3;
};

class S5 {
  void S5_m0();

  int S5_v0;

public:
  int S5_v1;
  // CHECK-MESSAGES-PUBLIC: :[[@LINE-1]]:7: warning: member variable 'S5_v1' has public visibility
protected:
  int S5_v2;
  // CHECK-MESSAGES-PROTECTED: :[[@LINE-1]]:7: warning: member variable 'S5_v2' has protected visibility
private:
  int S5_v3;
};

//----------------------------------------------------------------------------//

// Has non-static method with public visibility.

struct S6 {
  int S6_v0;
  // CHECK-MESSAGES-PUBLIC: :[[@LINE-1]]:7: warning: member variable 'S6_v0' has public visibility
public:
  void S6_m0();
  int S6_v1;
  // CHECK-MESSAGES-PUBLIC: :[[@LINE-1]]:7: warning: member variable 'S6_v1' has public visibility
protected:
  int S6_v2;
  // CHECK-MESSAGES-PROTECTED: :[[@LINE-1]]:7: warning: member variable 'S6_v2' has protected visibility
private:
  int S6_v3;
};

class S7 {
  int S7_v0;

public:
  void S7_m0();
  int S7_v1;
  // CHECK-MESSAGES-PUBLIC: :[[@LINE-1]]:7: warning: member variable 'S7_v1' has public visibility
protected:
  int S7_v2;
  // CHECK-MESSAGES-PROTECTED: :[[@LINE-1]]:7: warning: member variable 'S7_v2' has protected visibility
private:
  int S7_v3;
};

//----------------------------------------------------------------------------//

// Has non-static method with protected visibility.

struct S8 {
  int S8_v0;
  // CHECK-MESSAGES-PUBLIC: :[[@LINE-1]]:7: warning: member variable 'S8_v0' has public visibility
public:
  int S8_v1;
  // CHECK-MESSAGES-PUBLIC: :[[@LINE-1]]:7: warning: member variable 'S8_v1' has public visibility
protected:
  void S8_m0();
  int S8_v2;
  // CHECK-MESSAGES-PROTECTED: :[[@LINE-1]]:7: warning: member variable 'S8_v2' has protected visibility
private:
  int S8_v3;
};

class S9 {
  int S9_v0;

public:
  int S9_v1;
  // CHECK-MESSAGES-PUBLIC: :[[@LINE-1]]:7: warning: member variable 'S9_v1' has public visibility
protected:
  void S9_m0();
  int S9_v2;
  // CHECK-MESSAGES-PROTECTED: :[[@LINE-1]]:7: warning: member variable 'S9_v2' has protected visibility
private:
  int S9_v3;
};

//----------------------------------------------------------------------------//

// Has non-static method with private visibility.

struct S10 {
  int S10_v0;
  // CHECK-MESSAGES-PUBLIC: :[[@LINE-1]]:7: warning: member variable 'S10_v0' has public visibility
public:
  int S10_v1;
  // CHECK-MESSAGES-PUBLIC: :[[@LINE-1]]:7: warning: member variable 'S10_v1' has public visibility
protected:
  int S10_v2;
  // CHECK-MESSAGES-PROTECTED: :[[@LINE-1]]:7: warning: member variable 'S10_v2' has protected visibility
private:
  void S10_m0();
  int S10_v3;
};

class S11 {
  int S11_v0;

public:
  int S11_v1;
  // CHECK-MESSAGES-PUBLIC: :[[@LINE-1]]:7: warning: member variable 'S11_v1' has public visibility
protected:
  int S11_v2;
  // CHECK-MESSAGES-PROTECTED: :[[@LINE-1]]:7: warning: member variable 'S11_v2' has protected visibility
private:
  void S11_m0();
  int S11_v3;
};

//============================================================================//

// Static variables are ignored.
// Has non-static methods and static variables.

struct S12 {
  void S12_m0();
  static int S12_v0;

public:
  void S12_m1();
  static int S12_v1;

protected:
  void S12_m2();
  static int S12_v2;

private:
  void S12_m3();
  static int S12_v3;
};

class S13 {
  void S13_m0();
  static int S13_v0;

public:
  void S13_m1();
  static int S13_v1;

protected:
  void S13_m2();
  static int S13_v2;

private:
  void S13_m3();
  static int S13_v3;
};

struct S14 {
  void S14_m0();
  int S14_v0;
  // CHECK-MESSAGES-NONPRIVATE: :[[@LINE-1]]:7: warning: member variable 'S14_v0' has public visibility

public:
  void S14_m1();
  int S14_v1;
  // CHECK-MESSAGES-NONPRIVATE: :[[@LINE-1]]:7: warning: member variable 'S14_v1' has public visibility

protected:
  void S14_m2();

private:
  void S14_m3();
};

class S15 {
  void S15_m0();

public:
  void S15_m1();
  int S15_v1;
  // CHECK-MESSAGES-NONPRIVATE: :[[@LINE-1]]:7: warning: member variable 'S15_v1' has public visibility

protected:
  void S15_m2();

private:
  void S15_m3();
};

//----------------------------------------------------------------------------//

template <typename T>
struct S97 {
  void method();
  T S97_v0;
  // CHECK-MESSAGES-NONPRIVATE: :[[@LINE-1]]:5: warning: member variable 'S97_v0' has public visibility
};

template struct S97<char *>;

template <>
struct S97<double> {
  void method();
  double S97d_v0;
  // CHECK-MESSAGES-NONPRIVATE: :[[@LINE-1]]:10: warning: member variable 'S97d_v0' has public visibility
};

//----------------------------------------------------------------------------//

#define FIELD(x) int x;

// Do diagnose fields originating from macros.
struct S98 {
  void method();
  FIELD(S98_v0);
  // CHECK-MESSAGES-NONPRIVATE: :[[@LINE-1]]:9: warning: member variable 'S98_v0' has public visibility
};

//----------------------------------------------------------------------------//

// Don't look in descendant classes.
class S99 {
  void method();

  struct S99_0 {
    int S99_S0_v0;
  };

public:
  struct S99_1 {
    int S99_S0_v0;
  };

protected:
  struct S99_2 {
    int S99_S0_v0;
  };

private:
  struct S99_3 {
    int S99_S0_v0;
  };
};

//----------------------------------------------------------------------------//

// Only diagnose once, don't let the inheritance fool you.
struct S100 {
  int S100_v0;
  // CHECK-MESSAGES-NONPRIVATE: :[[@LINE-1]]:7: warning: member variable 'S100_v0' has public visibility
  void m0();
};
struct S101_default_inheritance : S100 {
  int S101_v0;
  // CHECK-MESSAGES-NONPRIVATE: :[[@LINE-1]]:7: warning: member variable 'S101_v0' has public visibility
  void m1();
};
struct S102_public_inheritance : public S100 {
  int S102_v0;
  // CHECK-MESSAGES-NONPRIVATE: :[[@LINE-1]]:7: warning: member variable 'S102_v0' has public visibility
  void m1();
};
struct S103_protected_inheritance : protected S100 {
  int S103_v0;
  // CHECK-MESSAGES-NONPRIVATE: :[[@LINE-1]]:7: warning: member variable 'S103_v0' has public visibility
  void m1();
};
struct S104_private_inheritance : private S100 {
  int S104_v0;
  // CHECK-MESSAGES-NONPRIVATE: :[[@LINE-1]]:7: warning: member variable 'S104_v0' has public visibility
  void m1();
};