blob: 17b1aa2851b067cd47c7fbae7a68ff7dda4cf216 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only
Chris Lattner35080842008-02-02 20:20:10 +00002
Ted Kremenek377e2152011-02-23 02:15:19 +00003// PR 8876 - don't warn about trivially unreachable null derefs. Note that
4// we put this here because the reachability analysis only kicks in for
5// suppressing false positives when code has no errors.
6#define PR8876(err_ptr) do {\
7 if (err_ptr) *(int*)(err_ptr) = 1;\
8 } while (0)
9
10#define PR8876_pos(err_ptr) do {\
11 if (!err_ptr) *(int*)(err_ptr) = 1;\
12 } while (0)
13
14
Ted Kremeneka8d459e2011-03-23 21:33:21 +000015// Test that we don't report divide-by-zero errors in unreachable code.
16// This test should be left as is, as it also tests CFG functionality.
17void radar9171946() {
18 if (0) {
19 0 / (0 ? 1 : 0); // expected-warning {{expression result unused}}
20 }
21}
22
Ted Kremenek377e2152011-02-23 02:15:19 +000023int test_pr8876() {
24 PR8876(0); // no-warning
25 PR8876_pos(0); // expected-warning{{indirection of non-volatile null pointer will be deleted, not trap}} expected-note{{consider using __builtin_trap() or qualifying pointer with 'volatile'}}
26 return 0;
27}
28
Ted Kremenekc56c0042011-02-23 05:11:46 +000029// PR 8183 - Handle null pointer constants on the left-side of the '&&', and reason about
30// this when determining the reachability of the null pointer dereference on the right side.
31void pr8183(unsigned long long test)
32{
33 (void)((((void*)0)) && (*((unsigned long long*)(((void*)0))) = ((unsigned long long)((test)) % (unsigned long long)((1000000000))))); // no-warning
34 (*((unsigned long long*)(((void*)0))) = ((unsigned long long)((test)) % (unsigned long long)((1000000000)))); // expected-warning {{indirection of non-volatile null pointer will be deleted, not trap}} expected-note {{consider using __builtin_trap() or qualifying pointer with 'volatile'}}
35}
36
Chris Lattner35080842008-02-02 20:20:10 +000037// PR1966
38_Complex double test1() {
39 return __extension__ 1.0if;
40}
41
42_Complex double test2() {
Eli Friedman80a8eb72012-11-02 01:40:23 +000043 return 1.0if; // expected-warning {{imaginary constants are a GNU extension}}
Chris Lattner35080842008-02-02 20:20:10 +000044}
45
Chris Lattnerbaf0d662008-07-25 18:07:19 +000046// rdar://6097308
47void test3() {
48 int x;
49 (__extension__ x) = 10;
50}
51
Chris Lattner2c156472008-08-21 18:04:13 +000052// rdar://6162726
53void test4() {
54 static int var;
55 var =+ 5; // expected-warning {{use of unary operator that may be intended as compound assignment (+=)}}
56 var =- 5; // expected-warning {{use of unary operator that may be intended as compound assignment (-=)}}
Chris Lattner399bd1b2009-03-08 06:51:10 +000057 var = +5; // no warning when space between the = and +.
Chris Lattner2c156472008-08-21 18:04:13 +000058 var = -5;
Chris Lattner399bd1b2009-03-08 06:51:10 +000059
60 var =+5; // no warning when the subexpr of the unary op has no space before it.
61 var =-5;
Chris Lattner3e872092009-03-09 07:11:10 +000062
63#define FIVE 5
64 var=-FIVE; // no warning with macros.
65 var=-FIVE;
Chris Lattner2c156472008-08-21 18:04:13 +000066}
67
Chris Lattnerca354fa2008-11-17 19:51:54 +000068// rdar://6319320
69void test5(int *X, float *P) {
70 (float*)X = P; // expected-error {{assignment to cast is illegal, lvalue casts are not supported}}
Daniel Dunbar44e35f72009-04-15 00:08:05 +000071#define FOO ((float*) X)
72 FOO = P; // expected-error {{assignment to cast is illegal, lvalue casts are not supported}}
Chris Lattnerca354fa2008-11-17 19:51:54 +000073}
74
Chris Lattnerb1b4d332008-11-21 18:27:34 +000075void test6() {
76 int X;
77 X(); // expected-error {{called object type 'int' is not a function or function pointer}}
78}
Chris Lattner7ca14252008-11-22 19:57:03 +000079
80void test7(int *P, _Complex float Gamma) {
81 P = (P-42) + Gamma*4; // expected-error {{invalid operands to binary expression ('int *' and '_Complex float')}}
82}
83
Chris Lattner670a62c2008-12-12 05:35:08 +000084
85// rdar://6095061
86int test8(void) {
87 int i;
Eli Friedmaneb32fde2009-04-28 03:13:54 +000088 __builtin_choose_expr (0, 42, i) = 10;
Chris Lattner670a62c2008-12-12 05:35:08 +000089 return i;
90}
Chris Lattner31e21e02009-01-24 20:17:12 +000091
92
93// PR3386
94struct f { int x : 4; float y[]; };
95int test9(struct f *P) {
Chris Lattnerda027472009-01-24 21:29:22 +000096 int R;
Richard Smith7132be12013-03-18 23:37:25 +000097 R = __alignof(P->x); // expected-error {{invalid application of 'alignof' to bit-field}}
Eli Friedmaneb32fde2009-04-28 03:13:54 +000098 R = __alignof(P->y); // ok.
Anders Carlsson31fddcc2009-09-14 22:00:20 +000099 R = sizeof(P->x); // expected-error {{invalid application of 'sizeof' to bit-field}}
Chris Lattnerda027472009-01-24 21:29:22 +0000100 return R;
Chris Lattner31e21e02009-01-24 20:17:12 +0000101}
102
Chris Lattner56cd21b2009-02-13 22:08:30 +0000103// PR3562
104void test10(int n,...) {
105 struct S {
106 double a[n]; // expected-error {{fields must have a constant size}}
107 } s;
108 double x = s.a[0]; // should not get another error here.
109}
Chris Lattnerd0344a42009-02-19 23:45:49 +0000110
111
112#define MYMAX(A,B) __extension__ ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __b : __a; })
113
114struct mystruct {int A; };
Chris Lattner55660a72009-03-08 19:39:53 +0000115void test11(struct mystruct P, float F) {
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +0000116 MYMAX(P, F); // expected-error {{invalid operands to binary expression ('typeof (P)' (aka 'struct mystruct') and 'typeof (F)' (aka 'float'))}}
Chris Lattnerd0344a42009-02-19 23:45:49 +0000117}
118
Chris Lattner55660a72009-03-08 19:39:53 +0000119// PR3753
120int test12(const char *X) {
Ted Kremenek03a4bee2010-04-09 20:26:53 +0000121 return X == "foo"; // expected-warning {{comparison against a string literal is unspecified (use strncmp instead)}}
Chris Lattner55660a72009-03-08 19:39:53 +0000122}
123
Douglas Gregord1e4d9b2010-01-12 23:18:54 +0000124int test12b(const char *X) {
125 return sizeof(X == "foo"); // no-warning
126}
127
Chris Lattner9af55002009-03-27 04:18:06 +0000128// rdar://6719156
129void test13(
130 void (^P)()) { // expected-error {{blocks support disabled - compile with -fblocks}}
131 P();
132 P = ^(){}; // expected-error {{blocks support disabled - compile with -fblocks}}
133}
Chris Lattnerd013aa12009-03-31 07:46:52 +0000134
Chris Lattnerd013aa12009-03-31 07:46:52 +0000135void test14() {
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000136 typedef long long __m64 __attribute__((__vector_size__(8)));
137 typedef short __v4hi __attribute__((__vector_size__(8)));
Chris Lattnerd013aa12009-03-31 07:46:52 +0000138
Chris Lattner9c10fcf2009-07-08 01:08:03 +0000139 // Ok.
Chris Lattnerd013aa12009-03-31 07:46:52 +0000140 __v4hi a;
Chris Lattner9c10fcf2009-07-08 01:08:03 +0000141 __m64 mask = (__m64)((__v4hi)a > (__v4hi)a);
Chris Lattnerd013aa12009-03-31 07:46:52 +0000142}
143
Chris Lattner0a026af2009-10-20 05:36:05 +0000144
145// PR5242
146typedef unsigned long *test15_t;
147
148test15_t test15(void) {
149 return (test15_t)0 + (test15_t)0; // expected-error {{invalid operands to binary expression ('test15_t' (aka 'unsigned long *') and 'test15_t')}}
150}
151
Chris Lattner02dd4b12009-12-05 05:40:13 +0000152// rdar://7446395
153void test16(float x) { x == ((void*) 0); } // expected-error {{invalid operands to binary expression}}
Chris Lattner0a026af2009-10-20 05:36:05 +0000154
Chris Lattner7ef655a2010-01-12 21:23:57 +0000155// PR6004
156void test17(int x) {
157 x = x / 0; // expected-warning {{division by zero is undefined}}
158 x = x % 0; // expected-warning {{remainder by zero is undefined}}
159 x /= 0; // expected-warning {{division by zero is undefined}}
160 x %= 0; // expected-warning {{remainder by zero is undefined}}
Chris Lattnercb329c52010-01-12 21:30:55 +0000161
162 x = sizeof(x/0); // no warning.
Chris Lattner7ef655a2010-01-12 21:23:57 +0000163}
164
Richard Smithf7b80562012-05-11 05:16:41 +0000165// PR6501 & PR11857
Peter Collingbourne9aab1482011-07-29 00:24:42 +0000166void test18_a(int a); // expected-note 2 {{'test18_a' declared here}}
Richard Smithf7b80562012-05-11 05:16:41 +0000167void test18_b(int); // expected-note {{'test18_b' declared here}}
Richard Smithc608c3c2012-05-15 06:21:54 +0000168void test18_c(int a, int b); // expected-note 2 {{'test18_c' declared here}}
Richard Smithf7b80562012-05-11 05:16:41 +0000169void test18_d(int a, ...); // expected-note {{'test18_d' declared here}}
Richard Smithc608c3c2012-05-15 06:21:54 +0000170void test18_e(int a, int b, ...); // expected-note {{'test18_e' declared here}}
Eric Christopherc5e869b2010-04-19 18:39:43 +0000171void test18(int b) {
Richard Smithc608c3c2012-05-15 06:21:54 +0000172 test18_a(b, b); // expected-error {{too many arguments to function call, expected single argument 'a', have 2}}
173 test18_a(); // expected-error {{too few arguments to function call, single argument 'a' was not specified}}
Richard Smithf7b80562012-05-11 05:16:41 +0000174 test18_b(); // expected-error {{too few arguments to function call, expected 1, have 0}}
175 test18_c(b); // expected-error {{too few arguments to function call, expected 2, have 1}}
Richard Smithc608c3c2012-05-15 06:21:54 +0000176 test18_c(b, b, b); // expected-error {{too many arguments to function call, expected 2, have 3}}
Richard Smithf7b80562012-05-11 05:16:41 +0000177 test18_d(); // expected-error {{too few arguments to function call, at least argument 'a' must be specified}}
Richard Smithc608c3c2012-05-15 06:21:54 +0000178 test18_e(); // expected-error {{too few arguments to function call, expected at least 2, have 0}}
Eric Christopherc5e869b2010-04-19 18:39:43 +0000179}
Chris Lattner8b5dec32010-07-07 06:14:23 +0000180
181// PR7569
182void test19() {
183 *(int*)0 = 0; // expected-warning {{indirection of non-volatile null pointer}} \
184 // expected-note {{consider using __builtin_trap}}
185 *(volatile int*)0 = 0; // Ok.
Argyrios Kyrtzidis8a285ae2011-04-26 17:41:22 +0000186
187 // rdar://9269271
188 int x = *(int*)0; // expected-warning {{indirection of non-volatile null pointer}} \
189 // expected-note {{consider using __builtin_trap}}
190 int x2 = *(volatile int*)0; // Ok.
191 int *p = &(*(int*)0); // Ok;
Chris Lattner8b5dec32010-07-07 06:14:23 +0000192}
193
Chris Lattner90a8f272010-07-13 19:41:32 +0000194int test20(int x) {
Matt Beaumont-Gay9b127f32011-08-15 17:50:06 +0000195 return x && 4; // expected-warning {{use of logical '&&' with constant operand}} \
196 // expected-note {{use '&' for a bitwise operation}} \
197 // expected-note {{remove constant to silence this warning}}
Chris Lattner23ef3e42010-07-15 00:26:43 +0000198
Chris Lattnerb7690b42010-07-24 01:10:11 +0000199 return x && sizeof(int) == 4; // no warning, RHS is logical op.
200
201 // no warning, this is an idiom for "true" in old C style.
202 return x && (signed char)1;
Chandler Carruth0683a142011-05-31 05:41:42 +0000203
204 return x || 0;
205 return x || 1;
Matt Beaumont-Gay9b127f32011-08-15 17:50:06 +0000206 return x || -1; // expected-warning {{use of logical '||' with constant operand}} \
207 // expected-note {{use '|' for a bitwise operation}}
208 return x || 5; // expected-warning {{use of logical '||' with constant operand}} \
209 // expected-note {{use '|' for a bitwise operation}}
Chandler Carruth0683a142011-05-31 05:41:42 +0000210 return x && 0;
211 return x && 1;
Matt Beaumont-Gay9b127f32011-08-15 17:50:06 +0000212 return x && -1; // expected-warning {{use of logical '&&' with constant operand}} \
213 // expected-note {{use '&' for a bitwise operation}} \
214 // expected-note {{remove constant to silence this warning}}
215 return x && 5; // expected-warning {{use of logical '&&' with constant operand}} \
216 // expected-note {{use '&' for a bitwise operation}} \
217 // expected-note {{remove constant to silence this warning}}
Chandler Carruth0683a142011-05-31 05:41:42 +0000218 return x || (0);
219 return x || (1);
Matt Beaumont-Gay9b127f32011-08-15 17:50:06 +0000220 return x || (-1); // expected-warning {{use of logical '||' with constant operand}} \
221 // expected-note {{use '|' for a bitwise operation}}
222 return x || (5); // expected-warning {{use of logical '||' with constant operand}} \
223 // expected-note {{use '|' for a bitwise operation}}
Chandler Carruth0683a142011-05-31 05:41:42 +0000224 return x && (0);
225 return x && (1);
Matt Beaumont-Gay9b127f32011-08-15 17:50:06 +0000226 return x && (-1); // expected-warning {{use of logical '&&' with constant operand}} \
227 // expected-note {{use '&' for a bitwise operation}} \
228 // expected-note {{remove constant to silence this warning}}
229 return x && (5); // expected-warning {{use of logical '&&' with constant operand}} \
230 // expected-note {{use '&' for a bitwise operation}} \
231 // expected-note {{remove constant to silence this warning}}
Chandler Carruth0683a142011-05-31 05:41:42 +0000232
Chris Lattner90a8f272010-07-13 19:41:32 +0000233}
John McCallcf2e5062010-10-12 07:14:40 +0000234
235struct Test21; // expected-note 2 {{forward declaration}}
236void test21(volatile struct Test21 *ptr) {
237 void test21_help(void);
238 (test21_help(), *ptr); // expected-error {{incomplete type 'struct Test21' where a complete type is required}}
239 (*ptr, test21_help()); // expected-error {{incomplete type 'struct Test21' where a complete type is required}}
240}
John McCallabc56c72010-12-04 06:09:13 +0000241
242// Make sure we do function/array decay.
243void test22() {
244 if ("help")
245 (void) 0;
246
Stephen Hines176edba2014-12-01 14:53:08 -0800247 if (test22) // expected-warning {{address of function 'test22' will always evaluate to 'true'}} \
248 // expected-note {{prefix with the address-of operator to silence this warning}}
249 (void) 0;
250
251 if (&test22)
John McCallabc56c72010-12-04 06:09:13 +0000252 (void) 0;
253}