[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / Sema / exprs.c
blob31c6d1e01491a74d1cb00377d884bdf8c4daaeb4
1 // RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only
3 // 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)
10 #define PR8876_pos(err_ptr) do {\
11 if (!err_ptr) *(int*)(err_ptr) = 1;\
12 } while (0)
15 // 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.
17 void radar9171946(void) {
18 if (0) {
19 0 / (0 ? 1 : 0); // no-warning
23 int test_pr8876(void) {
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;
29 // 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.
31 void pr8183(unsigned long long test)
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'}}
37 // PR1966
38 _Complex double test1(void) {
39 return __extension__ 1.0if;
42 _Complex double test2(void) {
43 return 1.0if; // expected-warning {{imaginary constants are a GNU extension}}
46 // rdar://6097308
47 void test3(void) {
48 int x;
49 (__extension__ x) = 10;
52 // rdar://6162726
53 void test4(void) {
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 (-=)}}
57 var = +5; // no warning when space between the = and +.
58 var = -5;
60 var =+5; // no warning when the subexpr of the unary op has no space before it.
61 var =-5;
63 #define FIVE 5
64 var=-FIVE; // no warning with macros.
65 var=-FIVE;
68 // rdar://6319320
69 void test5(int *X, float *P) {
70 (float*)X = P; // expected-error {{assignment to cast is illegal, lvalue casts are not supported}}
71 #define FOO ((float*) X)
72 FOO = P; // expected-error {{assignment to cast is illegal, lvalue casts are not supported}}
75 void test6(void) {
76 int X;
77 X(); // expected-error {{called object type 'int' is not a function or function pointer}}
80 void test7(int *P, _Complex float Gamma) {
81 P = (P-42) + Gamma*4; // expected-error {{invalid operands to binary expression ('int *' and '_Complex float')}}
85 // rdar://6095061
86 int test8(void) {
87 int i;
88 __builtin_choose_expr (0, 42, i) = 10;
89 return i;
93 // PR3386
94 struct f { int x : 4; float y[]; };
95 int test9(struct f *P) {
96 int R;
97 R = __alignof(P->x); // expected-error {{invalid application of 'alignof' to bit-field}}
98 R = __alignof(P->y); // ok.
99 R = sizeof(P->x); // expected-error {{invalid application of 'sizeof' to bit-field}}
100 __extension__ ({ R = (__typeof__(P->x)) 2; }); // expected-error {{invalid application of 'typeof' to bit-field}}
101 return R;
104 // PR3562
105 void test10(int n,...) {
106 struct S {
107 double a[n]; // expected-error {{fields must have a constant size}}
108 } s;
109 double x = s.a[0]; // should not get another error here.
113 #define MYMAX(A,B) __extension__ ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __b : __a; })
115 struct mystruct {int A; };
116 void test11(struct mystruct P, float F) {
117 MYMAX(P, F); // expected-error {{invalid operands to binary expression ('typeof (P)' (aka 'struct mystruct') and 'typeof (F)' (aka 'float'))}}
120 // PR3753
121 int test12(const char *X) {
122 return X == "foo"; // expected-warning {{comparison against a string literal is unspecified (use an explicit string comparison function instead)}}
125 int test12b(const char *X) {
126 return sizeof(X == "foo"); // no-warning
129 // rdar://6719156
130 void test13(
131 void (^P)(void)) { // expected-error {{blocks support disabled - compile with -fblocks}}
132 P();
133 P = ^(void){}; // expected-error {{blocks support disabled - compile with -fblocks}}
136 void test14(void) {
137 typedef long long __m64 __attribute__((__vector_size__(8)));
138 typedef short __v4hi __attribute__((__vector_size__(8)));
140 // Ok.
141 __v4hi a;
142 __m64 mask = (__m64)((__v4hi)a > (__v4hi)a);
146 // PR5242
147 typedef unsigned long *test15_t;
149 test15_t test15(void) {
150 return (test15_t)0 + (test15_t)0; // expected-error {{invalid operands to binary expression ('test15_t' (aka 'unsigned long *') and 'test15_t')}}
153 // rdar://7446395
154 void test16(float x) { x == ((void*) 0); } // expected-error {{invalid operands to binary expression}}
156 // PR6004
157 void test17(int x) {
158 x = x / 0; // expected-warning {{division by zero is undefined}}
159 x = x % 0; // expected-warning {{remainder by zero is undefined}}
160 x /= 0; // expected-warning {{division by zero is undefined}}
161 x %= 0; // expected-warning {{remainder by zero is undefined}}
163 x = sizeof(x/0); // no warning.
166 // PR6501, PR11857, and PR23564
167 void test18_a(int a); // expected-note 2 {{'test18_a' declared here}}
168 void test18_b(int); // expected-note {{'test18_b' declared here}}
169 void test18_c(int a, int b); // expected-note 2 {{'test18_c' declared here}}
170 void test18_d(int a, ...); // expected-note {{'test18_d' declared here}}
171 void test18_e(int a, int b, ...); // expected-note {{'test18_e' declared here}}
172 #define MY_EXPORT __attribute__((visibility("default")))
173 MY_EXPORT void // (no "declared here" notes on this line, no "expanded from MY_EXPORT" notes either)
174 test18_f(int a, int b); // expected-note 2 {{'test18_f' declared here}}
175 void test18(int b) {
176 test18_a(b, b); // expected-error {{too many arguments to function call, expected single argument 'a', have 2}}
177 test18_a(); // expected-error {{too few arguments to function call, single argument 'a' was not specified}}
178 test18_b(); // expected-error {{too few arguments to function call, expected 1, have 0}}
179 test18_c(b); // expected-error {{too few arguments to function call, expected 2, have 1}}
180 test18_c(b, b, b); // expected-error {{too many arguments to function call, expected 2, have 3}}
181 test18_d(); // expected-error {{too few arguments to function call, at least argument 'a' must be specified}}
182 test18_e(); // expected-error {{too few arguments to function call, expected at least 2, have 0}}
183 test18_f(b); // expected-error {{too few arguments to function call, expected 2, have 1}}
184 test18_f(b, b, b); // expected-error {{too many arguments to function call, expected 2, have 3}}
187 typedef int __attribute__((address_space(256))) int_AS256;
188 // PR7569
189 void test19(void) {
190 *(int *)0 = 0; // expected-warning {{indirection of non-volatile null pointer}} \
191 // expected-note {{consider using __builtin_trap}}
192 *(volatile int *)0 = 0; // Ok.
193 *(int __attribute__((address_space(256))) *)0 = 0; // Ok.
194 *(int __attribute__((address_space(0))) *)0 = 0; // expected-warning {{indirection of non-volatile null pointer}} \
195 // expected-note {{consider using __builtin_trap}}
196 *(int_AS256 *)0 = 0; // Ok.
198 // rdar://9269271
199 int x = *(int *)0; // expected-warning {{indirection of non-volatile null pointer}} \
200 // expected-note {{consider using __builtin_trap}}
201 int x2 = *(volatile int *)0; // Ok.
202 int x3 = *(int __attribute__((address_space(0))) *)0; // expected-warning {{indirection of non-volatile null pointer}} \
203 // expected-note {{consider using __builtin_trap}}
204 int x4 = *(int_AS256 *)0; // Ok.
205 int *p = &(*(int *)0); // Ok.
206 int_AS256 *p1 = &(*(int __attribute__((address_space(256))) *)0); // Ok.
207 int __attribute__((address_space(0))) *p2 = &(*(int __attribute__((address_space(0))) *)0); // Ok.
210 int test20(int x) {
211 return x && 4; // expected-warning {{use of logical '&&' with constant operand}} \
212 // expected-note {{use '&' for a bitwise operation}} \
213 // expected-note {{remove constant to silence this warning}}
215 return x && sizeof(int) == 4; // no warning, RHS is logical op.
217 // no warning, this is an idiom for "true" in old C style.
218 return x && (signed char)1;
220 return x || 0;
221 return x || 1;
222 return x || -1; // expected-warning {{use of logical '||' with constant operand}} \
223 // expected-note {{use '|' for a bitwise operation}}
224 return x || 5; // expected-warning {{use of logical '||' with constant operand}} \
225 // expected-note {{use '|' for a bitwise operation}}
226 return x && 0;
227 return x && 1;
228 return x && -1; // expected-warning {{use of logical '&&' with constant operand}} \
229 // expected-note {{use '&' for a bitwise operation}} \
230 // expected-note {{remove constant to silence this warning}}
231 return x && 5; // expected-warning {{use of logical '&&' with constant operand}} \
232 // expected-note {{use '&' for a bitwise operation}} \
233 // expected-note {{remove constant to silence this warning}}
234 return x || (0);
235 return x || (1);
236 return x || (-1); // expected-warning {{use of logical '||' with constant operand}} \
237 // expected-note {{use '|' for a bitwise operation}}
238 return x || (5); // expected-warning {{use of logical '||' with constant operand}} \
239 // expected-note {{use '|' for a bitwise operation}}
240 return x && (0);
241 return x && (1);
242 return x && (-1); // expected-warning {{use of logical '&&' with constant operand}} \
243 // expected-note {{use '&' for a bitwise operation}} \
244 // expected-note {{remove constant to silence this warning}}
245 return x && (5); // expected-warning {{use of logical '&&' with constant operand}} \
246 // expected-note {{use '&' for a bitwise operation}} \
247 // expected-note {{remove constant to silence this warning}}
251 struct Test21; // expected-note 2 {{forward declaration}}
252 void test21(volatile struct Test21 *ptr) {
253 void test21_help(void);
254 (test21_help(), *ptr); // expected-error {{incomplete type 'struct Test21' where a complete type is required}}
255 (*ptr, test21_help()); // expected-error {{incomplete type 'struct Test21' where a complete type is required}}
258 // Make sure we do function/array decay.
259 void test22(void) {
260 if ("help")
261 (void) 0;
263 if (test22) // expected-warning {{address of function 'test22' will always evaluate to 'true'}} \
264 // expected-note {{prefix with the address-of operator to silence this warning}}
265 (void) 0;
267 if (&test22)
268 (void) 0;