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