forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tagged-union-member-count.cpp
263 lines (226 loc) · 6.04 KB
/
tagged-union-member-count.cpp
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
// RUN: %check_clang_tidy -std=c++98-or-later %s bugprone-tagged-union-member-count %t
// Test check with C++ features
typedef enum Tags3 {
tags3_1,
tags3_2,
tags3_3,
} tags3;
typedef enum Tags4 {
tags4_1,
tags4_2,
tags4_3,
tags4_4,
} tags4;
enum class Classtags3 {
classtags3_1,
classtags3_2,
classtags3_3,
};
enum class Typedtags3 : unsigned int {
typedtags3_1,
typedtags3_2,
typedtags3_3,
};
typedef union Union3 {
short *Shorts;
int *Ints;
float *Floats;
} union3;
typedef union Union4 {
short *Shorts;
double *Doubles;
int *Ints;
float *Floats;
} union4;
// It is not obvious which enum is the tag for the union.
class MaybeTaggedUnion1 { // No warnings expected.
enum Tags3 TagA;
enum Tags4 TagB;
union Union4 Data;
};
// It is not obvious which union does the tag belong to.
class MaybeTaggedUnion2 { // No warnings expected.
enum Tags3 Tag;
union Union3 DataB;
union Union3 DataA;
};
// It is not obvious which union does the tag belong to.
class MaybeTaggedUnion3 { // No warnings expected.
enum Tags3 Tag;
union {
int I1;
int I2;
int I3;
};
union {
float F1;
float F2;
float F3;
};
};
// CHECK-MESSAGES: :[[@LINE+1]]:7: warning: Tagged union has more data members (4) than tags (3)
class TaggedUnionClassPredefinedTagAndPredefinedUnion {
enum Tags3 Tag;
union Union4 Data;
};
// CHECK-MESSAGES: :[[@LINE+1]]:7: warning: Tagged union has more data members (4) than tags (3)
class TaggedUnionClassPredefinedTagAndInlineUnion {
enum Tags3 Tag;
union {
int *Ints;
char Characters[13];
class {
double Re;
double Im;
} Complex;
long L;
} Data;
};
// CHECK-MESSAGES: :[[@LINE+1]]:7: warning: Tagged union has more data members (4) than tags (3)
class TaggedUnionClassInlineTagAndPredefinedUnion {
enum {
tag1,
tag2,
tag3,
} Tag;
union Union4 Data;
};
// CHECK-MESSAGES: :[[@LINE+1]]:7: warning: Tagged union has more data members (4) than tags (3)
class TaggedUnionClassInlineTagAndInlineUnion {
enum {
tag1,
tag2,
tag3,
} Tag;
union {
int *Ints;
char Characters[13];
class {
double Re;
double Im;
} Complex;
long L;
} Data;
};
// CHECK-MESSAGES: :[[@LINE+1]]:7: warning: Tagged union has more data members (4) than tags (3)
class TaggedUnionClassWithNestedTaggedUnionClass {
enum Tags3 Tag;
union {
float F;
int I;
long L;
// CHECK-MESSAGES: :[[@LINE+1]]:11: warning: Tagged union has more data members (4) than tags (3)
class Innerdecl {
enum Tags3 Tag;
union Union4 Data;
} Inner;
} Data;
};
// CHECK-MESSAGES: :[[@LINE+1]]:7: warning: Tagged union has more data members (4) than tags (3)
class TaggedUnionClassWithTypedefedTag {
tags3 Tag;
union4 Data;
};
// CHECK-MESSAGES: :[[@LINE+1]]:8: warning: Tagged union has more data members (4) than tags (3)
struct TaggedUnionStructWithEnumClass {
enum Classtags3 Tag;
union4 Data;
};
// CHECK-MESSAGES: :[[@LINE+1]]:7: warning: Tagged union has more data members (4) than tags (3)
class TaggedUnionClasswithEnumClass {
enum Classtags3 Tag;
union4 Data;
};
// CHECK-MESSAGES: :[[@LINE+1]]:8: warning: Tagged union has more data members (4) than tags (3)
struct TaggedUnionStructWithTypedEnum {
Typedtags3 Tag;
union4 Data;
};
// CHECK-MESSAGES: :[[@LINE+1]]:7: warning: Tagged union has more data members (4) than tags (3)
class TaggedUnionClassWithTypedEnum {
Typedtags3 Tag;
union4 Data;
};
// CHECK-MESSAGES: :[[@LINE+1]]:8: warning: Tagged union has more data members (4) than tags (3)
struct AnonymousTaggedUnionStruct {
tags3 Tag;
union {
char A;
short B;
int C;
long D;
};
};
// CHECK-MESSAGES: :[[@LINE+1]]:7: warning: Tagged union has more data members (4) than tags (3)
class TaggedUnionClassWithAnonymousUnion {
tags3 Tag;
union {
char A;
short B;
int C;
long D;
};
};
namespace testnamespace {
enum Tags3 {
tags3_1,
tags3_2,
tags3_3,
};
union Union4 {
short *Shorts;
double *Doubles;
int *Ints;
float *Floats;
};
// CHECK-MESSAGES: :[[@LINE+1]]:8: warning: Tagged union has more data members (4) than tags (3)
struct TaggedUnionStructInNamespace {
Tags3 Tags;
Union4 Data;
};
// CHECK-MESSAGES: :[[@LINE+1]]:7: warning: Tagged union has more data members (4) than tags (3)
class TaggedUnionClassInNamespace {
Tags3 Tags;
Union4 Data;
};
} // namespace testnamespace
// CHECK-MESSAGES: :[[@LINE+1]]:8: warning: Tagged union has more data members (4) than tags (3)
struct TaggedUnionStructWithNamespacedTagAndUnion {
testnamespace::Tags3 Tags;
testnamespace::Union4 Data;
};
// CHECK-MESSAGES: :[[@LINE+1]]:7: warning: Tagged union has more data members (4) than tags (3)
class TaggedUnionClassWithNamespacedTagAndUnion {
testnamespace::Tags3 Tags;
testnamespace::Union4 Data;
};
// CHECK-MESSAGES: :[[@LINE+2]]:8: warning: Tagged union has more data members (4) than tags (3)
template <typename Union, typename Tag>
struct TemplatedStructWithNamespacedTagAndUnion {
Tag Kind;
Union Data;
};
TemplatedStructWithNamespacedTagAndUnion<testnamespace::Union4, testnamespace::Tags3> TemplatedStruct3;
// CHECK-MESSAGES: :[[@LINE+2]]:7: warning: Tagged union has more data members (4) than tags (3)
template <typename Union, typename Tag>
class TemplatedClassWithNamespacedTagAndUnion {
Tag Kind;
Union Data;
};
TemplatedClassWithNamespacedTagAndUnion<testnamespace::Union4, testnamespace::Tags3> TemplatedClass3;
// CHECK-MESSAGES: :[[@LINE+2]]:8: warning: Tagged union has more data members (4) than tags (3)
template <typename Union, typename Tag>
struct TemplatedStruct {
Tag Kind;
Union Data;
};
TemplatedStruct<union3, tags3> TemplatedStruct1; // No warning expected
TemplatedStruct<union4, tags3> TemplatedStruct2;
// CHECK-MESSAGES: :[[@LINE+2]]:7: warning: Tagged union has more data members (4) than tags (3)
template <typename Union, typename Tag>
class TemplatedClass {
Tag Kind;
Union Data;
};
TemplatedClass<union3, tags3> TemplatedClass1; // No warning expected
TemplatedClass<union4, tags3> TemplatedClass2;