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