1// RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only
2
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)
9
10#define PR8876_pos(err_ptr) do {\
11    if (!err_ptr) *(int*)(err_ptr) = 1;\
12  } while (0)
13
14
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.
17void radar9171946() {
18  if (0) {
19    0 / (0 ? 1 : 0); // expected-warning {{expression result unused}}
20  }
21}
22
23int 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
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.
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
37// PR1966
38_Complex double test1() {
39  return __extension__ 1.0if;
40}
41
42_Complex double test2() {
43  return 1.0if;    // expected-warning {{imaginary constants are an extension}}
44}
45
46// rdar://6097308
47void test3() {
48  int x;
49  (__extension__ x) = 10;
50}
51
52// 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 (-=)}}
57      var = +5;  // no warning when space between the = and +.
58      var = -5;
59
60      var =+5;  // no warning when the subexpr of the unary op has no space before it.
61      var =-5;
62
63#define FIVE 5
64      var=-FIVE;  // no warning with macros.
65      var=-FIVE;
66}
67
68// rdar://6319320
69void 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}}
73}
74
75void test6() {
76  int X;
77  X();  // expected-error {{called object type 'int' is not a function or function pointer}}
78}
79
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
84
85// rdar://6095061
86int test8(void) {
87  int i;
88  __builtin_choose_expr (0, 42, i) = 10;
89  return i;
90}
91
92
93// PR3386
94struct f { int x : 4;  float y[]; };
95int 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  return R;
101}
102
103// 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}
110
111
112#define MYMAX(A,B)    __extension__ ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __b : __a; })
113
114struct mystruct {int A; };
115void test11(struct mystruct P, float F) {
116  MYMAX(P, F);  // expected-error {{invalid operands to binary expression ('typeof (P)' (aka 'struct mystruct') and 'typeof (F)' (aka 'float'))}}
117}
118
119// PR3753
120int test12(const char *X) {
121  return X == "foo";  // expected-warning {{comparison against a string literal is unspecified (use strncmp instead)}}
122}
123
124int test12b(const char *X) {
125  return sizeof(X == "foo"); // no-warning
126}
127
128// 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}
134
135void test14() {
136  typedef long long __m64 __attribute__((__vector_size__(8)));
137  typedef short __v4hi __attribute__((__vector_size__(8)));
138
139  // Ok.
140  __v4hi a;
141  __m64 mask = (__m64)((__v4hi)a > (__v4hi)a);
142}
143
144
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
152// rdar://7446395
153void test16(float x) { x == ((void*) 0); }  // expected-error {{invalid operands to binary expression}}
154
155// 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}}
161
162  x = sizeof(x/0);  // no warning.
163}
164
165// PR6501
166void test18_a(int a); // expected-note 2 {{'test18_a' declared here}}
167void test18(int b) {
168  test18_a(b, b); // expected-error {{too many arguments to function call, expected 1, have 2}}
169  test18_a(); // expected-error {{too few arguments to function call, expected 1, have 0}}
170}
171
172// PR7569
173void test19() {
174  *(int*)0 = 0;   // expected-warning {{indirection of non-volatile null pointer}} \
175                  // expected-note {{consider using __builtin_trap}}
176  *(volatile int*)0 = 0;  // Ok.
177
178  // rdar://9269271
179  int x = *(int*)0;  // expected-warning {{indirection of non-volatile null pointer}} \
180                     // expected-note {{consider using __builtin_trap}}
181  int x2 = *(volatile int*)0; // Ok.
182  int *p = &(*(int*)0); // Ok;
183}
184
185int test20(int x) {
186  return x && 4; // expected-warning {{use of logical '&&' with constant operand}} \
187                 // expected-note {{use '&' for a bitwise operation}} \
188                 // expected-note {{remove constant to silence this warning}}
189
190  return x && sizeof(int) == 4;  // no warning, RHS is logical op.
191
192  // no warning, this is an idiom for "true" in old C style.
193  return x && (signed char)1;
194
195  return x || 0;
196  return x || 1;
197  return x || -1; // expected-warning {{use of logical '||' with constant operand}} \
198                  // expected-note {{use '|' for a bitwise operation}}
199  return x || 5; // expected-warning {{use of logical '||' with constant operand}} \
200                 // expected-note {{use '|' for a bitwise operation}}
201  return x && 0;
202  return x && 1;
203  return x && -1; // expected-warning {{use of logical '&&' with constant operand}} \
204                  // expected-note {{use '&' for a bitwise operation}} \
205                  // expected-note {{remove constant to silence this warning}}
206  return x && 5; // expected-warning {{use of logical '&&' with constant operand}} \
207                 // expected-note {{use '&' for a bitwise operation}} \
208                 // expected-note {{remove constant to silence this warning}}
209  return x || (0);
210  return x || (1);
211  return x || (-1); // expected-warning {{use of logical '||' with constant operand}} \
212                    // expected-note {{use '|' for a bitwise operation}}
213  return x || (5); // expected-warning {{use of logical '||' with constant operand}} \
214                   // expected-note {{use '|' for a bitwise operation}}
215  return x && (0);
216  return x && (1);
217  return x && (-1); // expected-warning {{use of logical '&&' with constant operand}} \
218                    // expected-note {{use '&' for a bitwise operation}} \
219                    // expected-note {{remove constant to silence this warning}}
220  return x && (5); // expected-warning {{use of logical '&&' with constant operand}} \
221                   // expected-note {{use '&' for a bitwise operation}} \
222                   // expected-note {{remove constant to silence this warning}}
223
224}
225
226struct Test21; // expected-note 2 {{forward declaration}}
227void test21(volatile struct Test21 *ptr) {
228  void test21_help(void);
229  (test21_help(), *ptr); // expected-error {{incomplete type 'struct Test21' where a complete type is required}}
230  (*ptr, test21_help()); // expected-error {{incomplete type 'struct Test21' where a complete type is required}}
231}
232
233// Make sure we do function/array decay.
234void test22() {
235  if ("help")
236    (void) 0;
237
238  if (test22)
239    (void) 0;
240}
241