blob: 0d6c5488de5f25695e22728c0232b68d46187b73 [file] [log] [blame]
Daniel Dunbar8fbe78f2009-12-15 20:14:24 +00001// RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only
Chris Lattnerc43926f2008-02-02 20:20:10 +00002
Ted Kremenekef59fe72011-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
15int test_pr8876() {
16 PR8876(0); // no-warning
17 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'}}
18 return 0;
19}
20
Ted Kremenek1a241d12011-02-23 05:11:46 +000021// PR 8183 - Handle null pointer constants on the left-side of the '&&', and reason about
22// this when determining the reachability of the null pointer dereference on the right side.
23void pr8183(unsigned long long test)
24{
25 (void)((((void*)0)) && (*((unsigned long long*)(((void*)0))) = ((unsigned long long)((test)) % (unsigned long long)((1000000000))))); // no-warning
26 (*((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'}}
27}
28
Chris Lattnerc43926f2008-02-02 20:20:10 +000029// PR1966
30_Complex double test1() {
31 return __extension__ 1.0if;
32}
33
34_Complex double test2() {
35 return 1.0if; // expected-warning {{imaginary constants are an extension}}
36}
37
Chris Lattnerec8996d2008-07-25 18:07:19 +000038// rdar://6097308
39void test3() {
40 int x;
41 (__extension__ x) = 10;
42}
43
Chris Lattnerea714382008-08-21 18:04:13 +000044// rdar://6162726
45void test4() {
46 static int var;
47 var =+ 5; // expected-warning {{use of unary operator that may be intended as compound assignment (+=)}}
48 var =- 5; // expected-warning {{use of unary operator that may be intended as compound assignment (-=)}}
Chris Lattner36c39c92009-03-08 06:51:10 +000049 var = +5; // no warning when space between the = and +.
Chris Lattnerea714382008-08-21 18:04:13 +000050 var = -5;
Chris Lattner36c39c92009-03-08 06:51:10 +000051
52 var =+5; // no warning when the subexpr of the unary op has no space before it.
53 var =-5;
Chris Lattnered9f14c2009-03-09 07:11:10 +000054
55#define FIVE 5
56 var=-FIVE; // no warning with macros.
57 var=-FIVE;
Chris Lattnerea714382008-08-21 18:04:13 +000058}
59
Chris Lattner9b3bbe92008-11-17 19:51:54 +000060// rdar://6319320
61void test5(int *X, float *P) {
62 (float*)X = P; // expected-error {{assignment to cast is illegal, lvalue casts are not supported}}
Daniel Dunbarc2223ab2009-04-15 00:08:05 +000063#define FOO ((float*) X)
64 FOO = P; // expected-error {{assignment to cast is illegal, lvalue casts are not supported}}
Chris Lattner9b3bbe92008-11-17 19:51:54 +000065}
66
Chris Lattner40bb0c82008-11-21 18:27:34 +000067void test6() {
68 int X;
69 X(); // expected-error {{called object type 'int' is not a function or function pointer}}
70}
Chris Lattner8cd9b962008-11-22 19:57:03 +000071
72void test7(int *P, _Complex float Gamma) {
73 P = (P-42) + Gamma*4; // expected-error {{invalid operands to binary expression ('int *' and '_Complex float')}}
74}
75
Chris Lattner053441f2008-12-12 05:35:08 +000076
77// rdar://6095061
78int test8(void) {
79 int i;
Eli Friedman2b680b42009-04-28 03:13:54 +000080 __builtin_choose_expr (0, 42, i) = 10;
Chris Lattner053441f2008-12-12 05:35:08 +000081 return i;
82}
Chris Lattner8dff0172009-01-24 20:17:12 +000083
84
85// PR3386
86struct f { int x : 4; float y[]; };
87int test9(struct f *P) {
Chris Lattner5eca6ad2009-01-24 21:29:22 +000088 int R;
Anders Carlsson2898af52009-09-14 22:00:20 +000089 R = __alignof(P->x); // expected-error {{invalid application of '__alignof' to bit-field}}
Eli Friedman2b680b42009-04-28 03:13:54 +000090 R = __alignof(P->y); // ok.
Anders Carlsson2898af52009-09-14 22:00:20 +000091 R = sizeof(P->x); // expected-error {{invalid application of 'sizeof' to bit-field}}
Chris Lattner5eca6ad2009-01-24 21:29:22 +000092 return R;
Chris Lattner8dff0172009-01-24 20:17:12 +000093}
94
Chris Lattner303284a2009-02-13 22:08:30 +000095// PR3562
96void test10(int n,...) {
97 struct S {
98 double a[n]; // expected-error {{fields must have a constant size}}
99 } s;
100 double x = s.a[0]; // should not get another error here.
101}
Chris Lattner810d3302009-02-19 23:45:49 +0000102
103
104#define MYMAX(A,B) __extension__ ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __b : __a; })
105
106struct mystruct {int A; };
Chris Lattner222b8bd2009-03-08 19:39:53 +0000107void test11(struct mystruct P, float F) {
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +0000108 MYMAX(P, F); // expected-error {{invalid operands to binary expression ('typeof (P)' (aka 'struct mystruct') and 'typeof (F)' (aka 'float'))}}
Chris Lattner810d3302009-02-19 23:45:49 +0000109}
110
Chris Lattner222b8bd2009-03-08 19:39:53 +0000111// PR3753
112int test12(const char *X) {
Ted Kremenek800b66b2010-04-09 20:26:53 +0000113 return X == "foo"; // expected-warning {{comparison against a string literal is unspecified (use strncmp instead)}}
Chris Lattner222b8bd2009-03-08 19:39:53 +0000114}
115
Douglas Gregor49862b82010-01-12 23:18:54 +0000116int test12b(const char *X) {
117 return sizeof(X == "foo"); // no-warning
118}
119
Chris Lattner9eac9312009-03-27 04:18:06 +0000120// rdar://6719156
121void test13(
122 void (^P)()) { // expected-error {{blocks support disabled - compile with -fblocks}}
123 P();
124 P = ^(){}; // expected-error {{blocks support disabled - compile with -fblocks}}
125}
Chris Lattner5d688962009-03-31 07:46:52 +0000126
Chris Lattner5d688962009-03-31 07:46:52 +0000127void test14() {
Eli Friedman2b680b42009-04-28 03:13:54 +0000128 typedef long long __m64 __attribute__((__vector_size__(8)));
129 typedef short __v4hi __attribute__((__vector_size__(8)));
Chris Lattner5d688962009-03-31 07:46:52 +0000130
Chris Lattner2a7deb62009-07-08 01:08:03 +0000131 // Ok.
Chris Lattner5d688962009-03-31 07:46:52 +0000132 __v4hi a;
Chris Lattner2a7deb62009-07-08 01:08:03 +0000133 __m64 mask = (__m64)((__v4hi)a > (__v4hi)a);
Chris Lattner5d688962009-03-31 07:46:52 +0000134}
135
Chris Lattnerbd19b182009-10-20 05:36:05 +0000136
137// PR5242
138typedef unsigned long *test15_t;
139
140test15_t test15(void) {
141 return (test15_t)0 + (test15_t)0; // expected-error {{invalid operands to binary expression ('test15_t' (aka 'unsigned long *') and 'test15_t')}}
142}
143
Chris Lattner9a152e22009-12-05 05:40:13 +0000144// rdar://7446395
145void test16(float x) { x == ((void*) 0); } // expected-error {{invalid operands to binary expression}}
Chris Lattnerbd19b182009-10-20 05:36:05 +0000146
Chris Lattnerfaa54172010-01-12 21:23:57 +0000147// PR6004
148void test17(int x) {
149 x = x / 0; // expected-warning {{division by zero is undefined}}
150 x = x % 0; // expected-warning {{remainder by zero is undefined}}
151 x /= 0; // expected-warning {{division by zero is undefined}}
152 x %= 0; // expected-warning {{remainder by zero is undefined}}
Chris Lattner70117952010-01-12 21:30:55 +0000153
154 x = sizeof(x/0); // no warning.
Chris Lattnerfaa54172010-01-12 21:23:57 +0000155}
156
Eric Christopher5c6525f2010-04-19 18:39:43 +0000157// PR6501
158void test18_a(int a);
159void test18(int b) {
160 test18_a(b, b); // expected-error {{too many arguments to function call, expected 1, have 2}}
161 test18_a(); // expected-error {{too few arguments to function call, expected 1, have 0}}
162}
Chris Lattner39561062010-07-07 06:14:23 +0000163
164// PR7569
165void test19() {
166 *(int*)0 = 0; // expected-warning {{indirection of non-volatile null pointer}} \
167 // expected-note {{consider using __builtin_trap}}
168 *(volatile int*)0 = 0; // Ok.
169}
170
Chris Lattner8406c512010-07-13 19:41:32 +0000171int test20(int x) {
172 return x && 4; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
Chris Lattnerdeee7a32010-07-15 00:26:43 +0000173
Chris Lattner938533d2010-07-24 01:10:11 +0000174 return x && sizeof(int) == 4; // no warning, RHS is logical op.
175
176 // no warning, this is an idiom for "true" in old C style.
177 return x && (signed char)1;
Chris Lattner8406c512010-07-13 19:41:32 +0000178}
John McCall73d36182010-10-12 07:14:40 +0000179
180struct Test21; // expected-note 2 {{forward declaration}}
181void test21(volatile struct Test21 *ptr) {
182 void test21_help(void);
183 (test21_help(), *ptr); // expected-error {{incomplete type 'struct Test21' where a complete type is required}}
184 (*ptr, test21_help()); // expected-error {{incomplete type 'struct Test21' where a complete type is required}}
185}
John McCall29cb2fd2010-12-04 06:09:13 +0000186
187// Make sure we do function/array decay.
188void test22() {
189 if ("help")
190 (void) 0;
191
192 if (test22)
193 (void) 0;
194}