warn-comma-operator.cpp
8.84 KB
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
// RUN: %clang_cc1 -fsyntax-only -Wcomma -std=c++11 -verify %s
// RUN: %clang_cc1 -fsyntax-only -Wcomma -std=c++11 -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
// RUN: %clang_cc1 -fsyntax-only -Wcomma -x c -std=c89 -verify %s
// RUN: %clang_cc1 -fsyntax-only -Wcomma -x c -std=c99 -verify %s
// RUN: %clang_cc1 -fsyntax-only -Wcomma -x c -std=c11 -verify %s
// RUN: %clang_cc1 -fsyntax-only -Wcomma -x c -std=c17 -verify %s
// int returning function
int return_four() { return 5; }
// Test builtin operators
void test_builtin() {
int x = 0, y = 0;
for (; y < 10; x++, y++) {}
for (; y < 10; ++x, y++) {}
for (; y < 10; x++, ++y) {}
for (; y < 10; ++x, ++y) {}
for (; y < 10; x--, ++y) {}
for (; y < 10; --x, ++y) {}
for (; y < 10; x = 5, ++y) {}
for (; y < 10; x *= 5, ++y) {}
for (; y < 10; x /= 5, ++y) {}
for (; y < 10; x %= 5, ++y) {}
for (; y < 10; x += 5, ++y) {}
for (; y < 10; x -= 5, ++y) {}
for (; y < 10; x <<= 5, ++y) {}
for (; y < 10; x >>= 5, ++y) {}
for (; y < 10; x &= 5, ++y) {}
for (; y < 10; x |= 5, ++y) {}
for (; y < 10; x ^= 5, ++y) {}
}
// Test nested comma operators
void test_nested() {
int x1, x2, x3;
int y1, *y2 = 0, y3 = 5;
#if __STDC_VERSION >= 199901L
for (int z1 = 5, z2 = 4, z3 = 3; x1 <4; ++x1) {}
#endif
}
// Confusing "," for "=="
void test_compare() {
if (return_four(), 5) {}
// expected-warning@-1{{comma operator}}
// expected-note@-2{{cast expression to void}}
// CHECK: fix-it:{{.*}}:{[[@LINE-3]]:7-[[@LINE-3]]:7}:"static_cast<void>("
// CHECK: fix-it:{{.*}}:{[[@LINE-4]]:20-[[@LINE-4]]:20}:")"
if (return_four() == 5) {}
}
// Confusing "," for "+"
int test_plus() {
return return_four(), return_four();
// expected-warning@-1{{comma operator}}
// expected-note@-2{{cast expression to void}}
// CHECK: fix-it:{{.*}}:{[[@LINE-3]]:10-[[@LINE-3]]:10}:"static_cast<void>("
// CHECK: fix-it:{{.*}}:{[[@LINE-4]]:23-[[@LINE-4]]:23}:")"
return return_four() + return_four();
}
// Be sure to look through parentheses
void test_parentheses() {
int x, y;
for (x = 0; return_four(), x;) {}
// expected-warning@-1{{comma operator}}
// expected-note@-2{{cast expression to void}}
// CHECK: fix-it:{{.*}}:{[[@LINE-3]]:15-[[@LINE-3]]:15}:"static_cast<void>("
// CHECK: fix-it:{{.*}}:{[[@LINE-4]]:28-[[@LINE-4]]:28}:")"
for (x = 0; (return_four()), (x) ;) {}
// expected-warning@-1{{comma operator}}
// expected-note@-2{{cast expression to void}}
// CHECK: fix-it:{{.*}}:{[[@LINE-3]]:15-[[@LINE-3]]:15}:"static_cast<void>("
// CHECK: fix-it:{{.*}}:{[[@LINE-4]]:30-[[@LINE-4]]:30}:")"
}
void test_increment() {
int x, y;
++x, ++y;
// expected-warning@-1{{comma operator}}
// expected-note@-2{{cast expression to void}}
// CHECK: fix-it:{{.*}}:{[[@LINE-3]]:3-[[@LINE-3]]:3}:"static_cast<void>("
// CHECK: fix-it:{{.*}}:{[[@LINE-4]]:6-[[@LINE-4]]:6}:")"
}
// Check for comma operator in conditions.
void test_conditions(int x) {
x = (return_four(), x);
// expected-warning@-1{{comma operator}}
// expected-note@-2{{cast expression to void}}
// CHECK: fix-it:{{.*}}:{[[@LINE-3]]:8-[[@LINE-3]]:8}:"static_cast<void>("
// CHECK: fix-it:{{.*}}:{[[@LINE-4]]:21-[[@LINE-4]]:21}:")"
int y = (return_four(), x);
// expected-warning@-1{{comma operator}}
// expected-note@-2{{cast expression to void}}
// CHECK: fix-it:{{.*}}:{[[@LINE-3]]:12-[[@LINE-3]]:12}:"static_cast<void>("
// CHECK: fix-it:{{.*}}:{[[@LINE-4]]:25-[[@LINE-4]]:25}:")"
for (; return_four(), x;) {}
// expected-warning@-1{{comma operator}}
// expected-note@-2{{cast expression to void}}
// CHECK: fix-it:{{.*}}:{[[@LINE-3]]:10-[[@LINE-3]]:10}:"static_cast<void>("
// CHECK: fix-it:{{.*}}:{[[@LINE-4]]:23-[[@LINE-4]]:23}:")"
while (return_four(), x) {}
// expected-warning@-1{{comma operator}}
// expected-note@-2{{cast expression to void}}
// CHECK: fix-it:{{.*}}:{[[@LINE-3]]:10-[[@LINE-3]]:10}:"static_cast<void>("
// CHECK: fix-it:{{.*}}:{[[@LINE-4]]:23-[[@LINE-4]]:23}:")"
if (return_four(), x) {}
// expected-warning@-1{{comma operator}}
// expected-note@-2{{cast expression to void}}
// CHECK: fix-it:{{.*}}:{[[@LINE-3]]:7-[[@LINE-3]]:7}:"static_cast<void>("
// CHECK: fix-it:{{.*}}:{[[@LINE-4]]:20-[[@LINE-4]]:20}:")"
do { } while (return_four(), x);
// expected-warning@-1{{comma operator}}
// expected-note@-2{{cast expression to void}}
// CHECK: fix-it:{{.*}}:{[[@LINE-3]]:17-[[@LINE-3]]:17}:"static_cast<void>("
// CHECK: fix-it:{{.*}}:{[[@LINE-4]]:30-[[@LINE-4]]:30}:")"
}
// Nested comma operator with fix-its.
void test_nested_fixits() {
return_four(), return_four(), return_four(), return_four();
// expected-warning@-1 3{{comma operator}}
// expected-note@-2 3{{cast expression to void}}
// CHECK: fix-it:{{.*}}:{[[@LINE-3]]:3-[[@LINE-3]]:3}:"static_cast<void>("
// CHECK: fix-it:{{.*}}:{[[@LINE-4]]:16-[[@LINE-4]]:16}:")"
// CHECK: fix-it:{{.*}}:{[[@LINE-5]]:18-[[@LINE-5]]:18}:"static_cast<void>("
// CHECK: fix-it:{{.*}}:{[[@LINE-6]]:31-[[@LINE-6]]:31}:")"
// CHECK: fix-it:{{.*}}:{[[@LINE-7]]:33-[[@LINE-7]]:33}:"static_cast<void>("
// CHECK: fix-it:{{.*}}:{[[@LINE-8]]:46-[[@LINE-8]]:46}:")"
}
#ifdef __cplusplus
class S2 {
public:
void advance();
S2 operator++();
S2 operator++(int);
S2 operator--();
S2 operator--(int);
S2 operator=(int);
S2 operator*=(int);
S2 operator/=(int);
S2 operator%=(int);
S2 operator+=(int);
S2 operator-=(int);
S2 operator<<=(int);
S2 operator>>=(int);
S2 operator&=(int);
S2 operator|=(int);
S2 operator^=(int);
};
// Test overloaded operators
void test_overloaded_operator() {
S2 x;
int y;
for (; y < 10; x++, y++) {}
for (; y < 10; ++x, y++) {}
for (; y < 10; x++, ++y) {}
for (; y < 10; ++x, ++y) {}
for (; y < 10; x--, ++y) {}
for (; y < 10; --x, ++y) {}
for (; y < 10; x = 5, ++y) {}
for (; y < 10; x *= 5, ++y) {}
for (; y < 10; x /= 5, ++y) {}
for (; y < 10; x %= 5, ++y) {}
for (; y < 10; x += 5, ++y) {}
for (; y < 10; x -= 5, ++y) {}
for (; y < 10; x <<= 5, ++y) {}
for (; y < 10; x >>= 5, ++y) {}
for (; y < 10; x &= 5, ++y) {}
for (; y < 10; x |= 5, ++y) {}
for (; y < 10; x ^= 5, ++y) {}
}
class Stream {
public:
Stream& operator<<(int);
} cout;
// Confusing "," for "<<"
void test_stream() {
cout << 5 << return_four();
cout << 5, return_four();
// expected-warning@-1{{comma operator}}
// expected-note@-2{{cast expression to void}}
// CHECK: fix-it:{{.*}}:{[[@LINE-3]]:3-[[@LINE-3]]:3}:"static_cast<void>("
// CHECK: fix-it:{{.*}}:{[[@LINE-4]]:12-[[@LINE-4]]:12}:")"
}
void Concat(int);
void Concat(int, int);
// Testing extra parentheses in function call
void test_overloaded_function() {
Concat((return_four() , 5));
// expected-warning@-1{{comma operator}}
// expected-note@-2{{cast expression to void}}
// CHECK: fix-it:{{.*}}:{[[@LINE-3]]:11-[[@LINE-3]]:11}:"static_cast<void>("
// CHECK: fix-it:{{.*}}:{[[@LINE-4]]:24-[[@LINE-4]]:24}:")"
Concat(return_four() , 5);
}
bool DoStuff();
class S9 {
public:
bool Advance();
bool More();
};
// Ignore comma operator in for-loop initializations and increments.
void test_for_loop() {
int x, y;
for (x = 0, y = 5; x < y; ++x) {}
for (x = 0; x < 10; DoStuff(), ++x) {}
for (S9 s; s.More(); s.Advance(), ++x) {}
}
// Ignore comma operator in templates.
namespace test_template {
template <bool T>
struct B { static const bool value = T; };
typedef B<true> true_type;
typedef B<false> false_type;
template <bool...>
struct bool_seq;
template <typename... xs>
class Foo {
typedef bool_seq<(xs::value, true)...> all_true;
typedef bool_seq<(xs::value, false)...> all_false;
typedef bool_seq<xs::value...> seq;
};
const auto X = Foo<true_type>();
}
namespace test_mutex {
class Mutex {
public:
Mutex();
~Mutex();
};
class MutexLock {
public:
MutexLock(Mutex &);
MutexLock();
~MutexLock();
};
class BuiltinMutex {
Mutex M;
};
Mutex StatusMutex;
bool Status;
bool get_status() {
return (MutexLock(StatusMutex), Status);
// expected-warning@-1{{comma operator}}
// expected-note@-2{{cast expression to void}}
// CHECK: fix-it:{{.*}}:{[[@LINE-3]]:11-[[@LINE-3]]:11}:"static_cast<void>("
// CHECK: fix-it:{{.*}}:{[[@LINE-4]]:33-[[@LINE-4]]:33}:")"
return (MutexLock(), Status);
// expected-warning@-1{{comma operator}}
// expected-note@-2{{cast expression to void}}
// CHECK: fix-it:{{.*}}:{[[@LINE-3]]:11-[[@LINE-3]]:11}:"static_cast<void>("
// CHECK: fix-it:{{.*}}:{[[@LINE-4]]:22-[[@LINE-4]]:22}:")"
return (BuiltinMutex(), Status);
// expected-warning@-1{{comma operator}}
// expected-note@-2{{cast expression to void}}
// CHECK: fix-it:{{.*}}:{[[@LINE-3]]:11-[[@LINE-3]]:11}:"static_cast<void>("
// CHECK: fix-it:{{.*}}:{[[@LINE-4]]:25-[[@LINE-4]]:25}:")"
}
}
// PR39375 - test cast to void to silence warnings
template <typename T>
void test_dependent_cast() {
(void)42, 0;
static_cast<void>(42), 0;
(void)T{}, 0;
static_cast<void>(T{}), 0;
}
#endif // ifdef __cplusplus