xref: /minix3/external/bsd/llvm/dist/clang/test/Sema/exprs.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc // PR 8876 - don't warn about trivially unreachable null derefs.  Note that
4f4a2713aSLionel Sambuc // we put this here because the reachability analysis only kicks in for
5f4a2713aSLionel Sambuc // suppressing false positives when code has no errors.
6f4a2713aSLionel Sambuc #define PR8876(err_ptr) do {\
7f4a2713aSLionel Sambuc     if (err_ptr) *(int*)(err_ptr) = 1;\
8f4a2713aSLionel Sambuc   } while (0)
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc #define PR8876_pos(err_ptr) do {\
11f4a2713aSLionel Sambuc     if (!err_ptr) *(int*)(err_ptr) = 1;\
12f4a2713aSLionel Sambuc   } while (0)
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc 
15f4a2713aSLionel Sambuc // Test that we don't report divide-by-zero errors in unreachable code.
16f4a2713aSLionel Sambuc // This test should be left as is, as it also tests CFG functionality.
radar9171946()17f4a2713aSLionel Sambuc void radar9171946() {
18f4a2713aSLionel Sambuc   if (0) {
19f4a2713aSLionel Sambuc     0 / (0 ? 1 : 0); // expected-warning {{expression result unused}}
20f4a2713aSLionel Sambuc   }
21f4a2713aSLionel Sambuc }
22f4a2713aSLionel Sambuc 
test_pr8876()23f4a2713aSLionel Sambuc int test_pr8876() {
24f4a2713aSLionel Sambuc   PR8876(0); // no-warning
25f4a2713aSLionel Sambuc   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'}}
26f4a2713aSLionel Sambuc   return 0;
27f4a2713aSLionel Sambuc }
28f4a2713aSLionel Sambuc 
29f4a2713aSLionel Sambuc // PR 8183 - Handle null pointer constants on the left-side of the '&&', and reason about
30f4a2713aSLionel Sambuc // this when determining the reachability of the null pointer dereference on the right side.
pr8183(unsigned long long test)31f4a2713aSLionel Sambuc void pr8183(unsigned long long test)
32f4a2713aSLionel Sambuc {
33f4a2713aSLionel Sambuc   (void)((((void*)0)) && (*((unsigned long long*)(((void*)0))) = ((unsigned long long)((test)) % (unsigned long long)((1000000000)))));  // no-warning
34f4a2713aSLionel Sambuc   (*((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'}}
35f4a2713aSLionel Sambuc }
36f4a2713aSLionel Sambuc 
37f4a2713aSLionel Sambuc // PR1966
test1()38f4a2713aSLionel Sambuc _Complex double test1() {
39f4a2713aSLionel Sambuc   return __extension__ 1.0if;
40f4a2713aSLionel Sambuc }
41f4a2713aSLionel Sambuc 
test2()42f4a2713aSLionel Sambuc _Complex double test2() {
43f4a2713aSLionel Sambuc   return 1.0if;    // expected-warning {{imaginary constants are a GNU extension}}
44f4a2713aSLionel Sambuc }
45f4a2713aSLionel Sambuc 
46f4a2713aSLionel Sambuc // rdar://6097308
test3()47f4a2713aSLionel Sambuc void test3() {
48f4a2713aSLionel Sambuc   int x;
49f4a2713aSLionel Sambuc   (__extension__ x) = 10;
50f4a2713aSLionel Sambuc }
51f4a2713aSLionel Sambuc 
52f4a2713aSLionel Sambuc // rdar://6162726
test4()53f4a2713aSLionel Sambuc void test4() {
54f4a2713aSLionel Sambuc       static int var;
55f4a2713aSLionel Sambuc       var =+ 5;  // expected-warning {{use of unary operator that may be intended as compound assignment (+=)}}
56f4a2713aSLionel Sambuc       var =- 5;  // expected-warning {{use of unary operator that may be intended as compound assignment (-=)}}
57f4a2713aSLionel Sambuc       var = +5;  // no warning when space between the = and +.
58f4a2713aSLionel Sambuc       var = -5;
59f4a2713aSLionel Sambuc 
60f4a2713aSLionel Sambuc       var =+5;  // no warning when the subexpr of the unary op has no space before it.
61f4a2713aSLionel Sambuc       var =-5;
62f4a2713aSLionel Sambuc 
63f4a2713aSLionel Sambuc #define FIVE 5
64f4a2713aSLionel Sambuc       var=-FIVE;  // no warning with macros.
65f4a2713aSLionel Sambuc       var=-FIVE;
66f4a2713aSLionel Sambuc }
67f4a2713aSLionel Sambuc 
68f4a2713aSLionel Sambuc // rdar://6319320
test5(int * X,float * P)69f4a2713aSLionel Sambuc void test5(int *X, float *P) {
70f4a2713aSLionel Sambuc   (float*)X = P;   // expected-error {{assignment to cast is illegal, lvalue casts are not supported}}
71f4a2713aSLionel Sambuc #define FOO ((float*) X)
72f4a2713aSLionel Sambuc   FOO = P;   // expected-error {{assignment to cast is illegal, lvalue casts are not supported}}
73f4a2713aSLionel Sambuc }
74f4a2713aSLionel Sambuc 
test6()75f4a2713aSLionel Sambuc void test6() {
76f4a2713aSLionel Sambuc   int X;
77f4a2713aSLionel Sambuc   X();  // expected-error {{called object type 'int' is not a function or function pointer}}
78f4a2713aSLionel Sambuc }
79f4a2713aSLionel Sambuc 
test7(int * P,_Complex float Gamma)80f4a2713aSLionel Sambuc void test7(int *P, _Complex float Gamma) {
81f4a2713aSLionel Sambuc    P = (P-42) + Gamma*4;  // expected-error {{invalid operands to binary expression ('int *' and '_Complex float')}}
82f4a2713aSLionel Sambuc }
83f4a2713aSLionel Sambuc 
84f4a2713aSLionel Sambuc 
85f4a2713aSLionel Sambuc // rdar://6095061
test8(void)86f4a2713aSLionel Sambuc int test8(void) {
87f4a2713aSLionel Sambuc   int i;
88f4a2713aSLionel Sambuc   __builtin_choose_expr (0, 42, i) = 10;
89f4a2713aSLionel Sambuc   return i;
90f4a2713aSLionel Sambuc }
91f4a2713aSLionel Sambuc 
92f4a2713aSLionel Sambuc 
93f4a2713aSLionel Sambuc // PR3386
94f4a2713aSLionel Sambuc struct f { int x : 4;  float y[]; };
test9(struct f * P)95f4a2713aSLionel Sambuc int test9(struct f *P) {
96f4a2713aSLionel Sambuc   int R;
97f4a2713aSLionel Sambuc   R = __alignof(P->x);  // expected-error {{invalid application of 'alignof' to bit-field}}
98f4a2713aSLionel Sambuc   R = __alignof(P->y);   // ok.
99f4a2713aSLionel Sambuc   R = sizeof(P->x); // expected-error {{invalid application of 'sizeof' to bit-field}}
100f4a2713aSLionel Sambuc   return R;
101f4a2713aSLionel Sambuc }
102f4a2713aSLionel Sambuc 
103f4a2713aSLionel Sambuc // PR3562
test10(int n,...)104f4a2713aSLionel Sambuc void test10(int n,...) {
105f4a2713aSLionel Sambuc   struct S {
106f4a2713aSLionel Sambuc     double          a[n];  // expected-error {{fields must have a constant size}}
107f4a2713aSLionel Sambuc   }               s;
108f4a2713aSLionel Sambuc   double x = s.a[0];  // should not get another error here.
109f4a2713aSLionel Sambuc }
110f4a2713aSLionel Sambuc 
111f4a2713aSLionel Sambuc 
112f4a2713aSLionel Sambuc #define MYMAX(A,B)    __extension__ ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __b : __a; })
113f4a2713aSLionel Sambuc 
114f4a2713aSLionel Sambuc struct mystruct {int A; };
test11(struct mystruct P,float F)115f4a2713aSLionel Sambuc void test11(struct mystruct P, float F) {
116f4a2713aSLionel Sambuc   MYMAX(P, F);  // expected-error {{invalid operands to binary expression ('typeof (P)' (aka 'struct mystruct') and 'typeof (F)' (aka 'float'))}}
117f4a2713aSLionel Sambuc }
118f4a2713aSLionel Sambuc 
119f4a2713aSLionel Sambuc // PR3753
test12(const char * X)120f4a2713aSLionel Sambuc int test12(const char *X) {
121f4a2713aSLionel Sambuc   return X == "foo";  // expected-warning {{comparison against a string literal is unspecified (use strncmp instead)}}
122f4a2713aSLionel Sambuc }
123f4a2713aSLionel Sambuc 
test12b(const char * X)124f4a2713aSLionel Sambuc int test12b(const char *X) {
125f4a2713aSLionel Sambuc   return sizeof(X == "foo"); // no-warning
126f4a2713aSLionel Sambuc }
127f4a2713aSLionel Sambuc 
128f4a2713aSLionel Sambuc // rdar://6719156
129f4a2713aSLionel Sambuc void test13(
130f4a2713aSLionel Sambuc             void (^P)()) { // expected-error {{blocks support disabled - compile with -fblocks}}
131f4a2713aSLionel Sambuc   P();
132f4a2713aSLionel Sambuc   P = ^(){}; // expected-error {{blocks support disabled - compile with -fblocks}}
133f4a2713aSLionel Sambuc }
134f4a2713aSLionel Sambuc 
test14()135f4a2713aSLionel Sambuc void test14() {
136f4a2713aSLionel Sambuc   typedef long long __m64 __attribute__((__vector_size__(8)));
137f4a2713aSLionel Sambuc   typedef short __v4hi __attribute__((__vector_size__(8)));
138f4a2713aSLionel Sambuc 
139f4a2713aSLionel Sambuc   // Ok.
140f4a2713aSLionel Sambuc   __v4hi a;
141f4a2713aSLionel Sambuc   __m64 mask = (__m64)((__v4hi)a > (__v4hi)a);
142f4a2713aSLionel Sambuc }
143f4a2713aSLionel Sambuc 
144f4a2713aSLionel Sambuc 
145f4a2713aSLionel Sambuc // PR5242
146f4a2713aSLionel Sambuc typedef unsigned long *test15_t;
147f4a2713aSLionel Sambuc 
test15(void)148f4a2713aSLionel Sambuc test15_t test15(void) {
149f4a2713aSLionel Sambuc   return (test15_t)0 + (test15_t)0;  // expected-error {{invalid operands to binary expression ('test15_t' (aka 'unsigned long *') and 'test15_t')}}
150f4a2713aSLionel Sambuc }
151f4a2713aSLionel Sambuc 
152f4a2713aSLionel Sambuc // rdar://7446395
test16(float x)153f4a2713aSLionel Sambuc void test16(float x) { x == ((void*) 0); }  // expected-error {{invalid operands to binary expression}}
154f4a2713aSLionel Sambuc 
155f4a2713aSLionel Sambuc // PR6004
test17(int x)156f4a2713aSLionel Sambuc void test17(int x) {
157f4a2713aSLionel Sambuc   x = x / 0;  // expected-warning {{division by zero is undefined}}
158f4a2713aSLionel Sambuc   x = x % 0;  // expected-warning {{remainder by zero is undefined}}
159f4a2713aSLionel Sambuc   x /= 0;  // expected-warning {{division by zero is undefined}}
160f4a2713aSLionel Sambuc   x %= 0;  // expected-warning {{remainder by zero is undefined}}
161f4a2713aSLionel Sambuc 
162f4a2713aSLionel Sambuc   x = sizeof(x/0);  // no warning.
163f4a2713aSLionel Sambuc }
164f4a2713aSLionel Sambuc 
165f4a2713aSLionel Sambuc // PR6501 & PR11857
166f4a2713aSLionel Sambuc void test18_a(int a); // expected-note 2 {{'test18_a' declared here}}
167f4a2713aSLionel Sambuc void test18_b(int); // expected-note {{'test18_b' declared here}}
168f4a2713aSLionel Sambuc void test18_c(int a, int b); // expected-note 2 {{'test18_c' declared here}}
169f4a2713aSLionel Sambuc void test18_d(int a, ...); // expected-note {{'test18_d' declared here}}
170f4a2713aSLionel Sambuc void test18_e(int a, int b, ...); // expected-note {{'test18_e' declared here}}
test18(int b)171f4a2713aSLionel Sambuc void test18(int b) {
172f4a2713aSLionel Sambuc   test18_a(b, b); // expected-error {{too many arguments to function call, expected single argument 'a', have 2}}
173f4a2713aSLionel Sambuc   test18_a(); // expected-error {{too few arguments to function call, single argument 'a' was not specified}}
174f4a2713aSLionel Sambuc   test18_b(); // expected-error {{too few arguments to function call, expected 1, have 0}}
175f4a2713aSLionel Sambuc   test18_c(b); // expected-error {{too few arguments to function call, expected 2, have 1}}
176f4a2713aSLionel Sambuc   test18_c(b, b, b); // expected-error {{too many arguments to function call, expected 2, have 3}}
177f4a2713aSLionel Sambuc   test18_d(); // expected-error {{too few arguments to function call, at least argument 'a' must be specified}}
178f4a2713aSLionel Sambuc   test18_e(); // expected-error {{too few arguments to function call, expected at least 2, have 0}}
179f4a2713aSLionel Sambuc }
180f4a2713aSLionel Sambuc 
181f4a2713aSLionel Sambuc // PR7569
test19()182f4a2713aSLionel Sambuc void test19() {
183f4a2713aSLionel Sambuc   *(int*)0 = 0;   // expected-warning {{indirection of non-volatile null pointer}} \
184f4a2713aSLionel Sambuc                   // expected-note {{consider using __builtin_trap}}
185f4a2713aSLionel Sambuc   *(volatile int*)0 = 0;  // Ok.
186f4a2713aSLionel Sambuc 
187f4a2713aSLionel Sambuc   // rdar://9269271
188f4a2713aSLionel Sambuc   int x = *(int*)0;  // expected-warning {{indirection of non-volatile null pointer}} \
189f4a2713aSLionel Sambuc                      // expected-note {{consider using __builtin_trap}}
190f4a2713aSLionel Sambuc   int x2 = *(volatile int*)0; // Ok.
191f4a2713aSLionel Sambuc   int *p = &(*(int*)0); // Ok;
192f4a2713aSLionel Sambuc }
193f4a2713aSLionel Sambuc 
test20(int x)194f4a2713aSLionel Sambuc int test20(int x) {
195f4a2713aSLionel Sambuc   return x && 4; // expected-warning {{use of logical '&&' with constant operand}} \
196f4a2713aSLionel Sambuc                  // expected-note {{use '&' for a bitwise operation}} \
197f4a2713aSLionel Sambuc                  // expected-note {{remove constant to silence this warning}}
198f4a2713aSLionel Sambuc 
199f4a2713aSLionel Sambuc   return x && sizeof(int) == 4;  // no warning, RHS is logical op.
200f4a2713aSLionel Sambuc 
201f4a2713aSLionel Sambuc   // no warning, this is an idiom for "true" in old C style.
202f4a2713aSLionel Sambuc   return x && (signed char)1;
203f4a2713aSLionel Sambuc 
204f4a2713aSLionel Sambuc   return x || 0;
205f4a2713aSLionel Sambuc   return x || 1;
206f4a2713aSLionel Sambuc   return x || -1; // expected-warning {{use of logical '||' with constant operand}} \
207f4a2713aSLionel Sambuc                   // expected-note {{use '|' for a bitwise operation}}
208f4a2713aSLionel Sambuc   return x || 5; // expected-warning {{use of logical '||' with constant operand}} \
209f4a2713aSLionel Sambuc                  // expected-note {{use '|' for a bitwise operation}}
210f4a2713aSLionel Sambuc   return x && 0;
211f4a2713aSLionel Sambuc   return x && 1;
212f4a2713aSLionel Sambuc   return x && -1; // expected-warning {{use of logical '&&' with constant operand}} \
213f4a2713aSLionel Sambuc                   // expected-note {{use '&' for a bitwise operation}} \
214f4a2713aSLionel Sambuc                   // expected-note {{remove constant to silence this warning}}
215f4a2713aSLionel Sambuc   return x && 5; // expected-warning {{use of logical '&&' with constant operand}} \
216f4a2713aSLionel Sambuc                  // expected-note {{use '&' for a bitwise operation}} \
217f4a2713aSLionel Sambuc                  // expected-note {{remove constant to silence this warning}}
218f4a2713aSLionel Sambuc   return x || (0);
219f4a2713aSLionel Sambuc   return x || (1);
220f4a2713aSLionel Sambuc   return x || (-1); // expected-warning {{use of logical '||' with constant operand}} \
221f4a2713aSLionel Sambuc                     // expected-note {{use '|' for a bitwise operation}}
222f4a2713aSLionel Sambuc   return x || (5); // expected-warning {{use of logical '||' with constant operand}} \
223f4a2713aSLionel Sambuc                    // expected-note {{use '|' for a bitwise operation}}
224f4a2713aSLionel Sambuc   return x && (0);
225f4a2713aSLionel Sambuc   return x && (1);
226f4a2713aSLionel Sambuc   return x && (-1); // expected-warning {{use of logical '&&' with constant operand}} \
227f4a2713aSLionel Sambuc                     // expected-note {{use '&' for a bitwise operation}} \
228f4a2713aSLionel Sambuc                     // expected-note {{remove constant to silence this warning}}
229f4a2713aSLionel Sambuc   return x && (5); // expected-warning {{use of logical '&&' with constant operand}} \
230f4a2713aSLionel Sambuc                    // expected-note {{use '&' for a bitwise operation}} \
231f4a2713aSLionel Sambuc                    // expected-note {{remove constant to silence this warning}}
232f4a2713aSLionel Sambuc 
233f4a2713aSLionel Sambuc }
234f4a2713aSLionel Sambuc 
235f4a2713aSLionel Sambuc struct Test21; // expected-note 2 {{forward declaration}}
test21(volatile struct Test21 * ptr)236f4a2713aSLionel Sambuc void test21(volatile struct Test21 *ptr) {
237f4a2713aSLionel Sambuc   void test21_help(void);
238f4a2713aSLionel Sambuc   (test21_help(), *ptr); // expected-error {{incomplete type 'struct Test21' where a complete type is required}}
239f4a2713aSLionel Sambuc   (*ptr, test21_help()); // expected-error {{incomplete type 'struct Test21' where a complete type is required}}
240f4a2713aSLionel Sambuc }
241f4a2713aSLionel Sambuc 
242f4a2713aSLionel Sambuc // Make sure we do function/array decay.
test22()243f4a2713aSLionel Sambuc void test22() {
244f4a2713aSLionel Sambuc   if ("help")
245f4a2713aSLionel Sambuc     (void) 0;
246f4a2713aSLionel Sambuc 
247*0a6a1f1dSLionel Sambuc   if (test22) // expected-warning {{address of function 'test22' will always evaluate to 'true'}} \
248*0a6a1f1dSLionel Sambuc 	      // expected-note {{prefix with the address-of operator to silence this warning}}
249*0a6a1f1dSLionel Sambuc     (void) 0;
250*0a6a1f1dSLionel Sambuc 
251*0a6a1f1dSLionel Sambuc   if (&test22)
252f4a2713aSLionel Sambuc     (void) 0;
253f4a2713aSLionel Sambuc }
254