262 lines · c
1// RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only2 3// PR 8876 - don't warn about trivially unreachable null derefs. Note that4// we put this here because the reachability analysis only kicks in for5// 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(void) {18 if (0) {19 0 / (0 ? 1 : 0); // no-warning20 }21}22 23int test_pr8876(void) {24 PR8876(0); // no-warning25 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 about30// 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-warning34 (*((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// PR196638_Complex double test1(void) {39 return __extension__ 1.0if;40}41 42_Complex double test2(void) {43 return 1.0if; // expected-warning {{imaginary constants are a C2y extension}}44}45 46void test3(void) {47 int x;48 (__extension__ x) = 10;49}50 51void test4(void) {52 static int var;53 var =+ 5; // expected-warning {{use of unary operator that may be intended as compound assignment (+=)}}54 var =- 5; // expected-warning {{use of unary operator that may be intended as compound assignment (-=)}}55 var = +5; // no warning when space between the = and +.56 var = -5;57 58 var =+5; // no warning when the subexpr of the unary op has no space before it.59 var =-5;60 61#define FIVE 562 var=-FIVE; // no warning with macros.63 var=-FIVE;64}65 66void test5(int *X, float *P) {67 (float*)X = P; // expected-error {{assignment to cast is illegal, lvalue casts are not supported}}68#define FOO ((float*) X)69 FOO = P; // expected-error {{assignment to cast is illegal, lvalue casts are not supported}}70}71 72void test6(void) {73 int X;74 X(); // expected-error {{called object type 'int' is not a function or function pointer}}75}76 77void test7(int *P, _Complex float Gamma) {78 P = (P-42) + Gamma*4; // expected-error {{invalid operands to binary expression ('int *' and '_Complex float')}}79}80 81int test8(void) {82 int i;83 __builtin_choose_expr (0, 42, i) = 10;84 return i;85}86 87 88// PR338689struct f { int x : 4; float y[]; };90int test9(struct f *P) {91 int R;92 R = __alignof(P->x); // expected-error {{invalid application of 'alignof' to bit-field}}93 R = __alignof(P->y); // ok.94 R = sizeof(P->x); // expected-error {{invalid application of 'sizeof' to bit-field}}95 __extension__ ({ R = (__typeof__(P->x)) 2; }); // expected-error {{invalid application of 'typeof' to bit-field}}96 return R;97}98 99// PR3562100void test10(int n,...) {101 struct S {102 double a[n]; // expected-error {{fields must have a constant size}}103 } s;104 double x = s.a[0]; // should not get another error here.105}106 107 108#define MYMAX(A,B) __extension__ ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __b : __a; })109 110struct mystruct {int A; };111void test11(struct mystruct P, float F) {112 MYMAX(P, F); // expected-error {{invalid operands to binary expression ('typeof (P)' (aka 'struct mystruct') and 'typeof (F)' (aka 'float'))}}113}114 115// PR3753116int test12(const char *X) {117 return X == "foo"; // expected-warning {{comparison against a string literal is unspecified (use an explicit string comparison function instead)}}118}119 120int test12b(const char *X) {121 return sizeof(X == "foo"); // no-warning122}123 124void test13(125 void (^P)(void)) { // expected-error {{blocks support disabled - compile with -fblocks}}126 P();127 P = ^(void){}; // expected-error {{blocks support disabled - compile with -fblocks}}128}129 130void test14(void) {131 typedef long long __m64 __attribute__((__vector_size__(8)));132 typedef short __v4hi __attribute__((__vector_size__(8)));133 134 // Ok.135 __v4hi a;136 __m64 mask = (__m64)((__v4hi)a > (__v4hi)a);137}138 139 140// PR5242141typedef unsigned long *test15_t;142 143test15_t test15(void) {144 return (test15_t)0 + (test15_t)0; // expected-error {{invalid operands to binary expression ('test15_t' (aka 'unsigned long *') and 'test15_t')}}145}146 147void test16(float x) { x == ((void*) 0); } // expected-error {{invalid operands to binary expression}}148 149// PR6004150void test17(int x) {151 x = x / 0; // expected-warning {{division by zero is undefined}}152 x = x % 0; // expected-warning {{remainder by zero is undefined}}153 x /= 0; // expected-warning {{division by zero is undefined}}154 x %= 0; // expected-warning {{remainder by zero is undefined}}155 156 x = sizeof(x/0); // no warning.157}158 159// PR6501, PR11857, and PR23564160void test18_a(int a); // expected-note 2 {{'test18_a' declared here}}161void test18_b(int); // expected-note {{'test18_b' declared here}}162void test18_c(int a, int b); // expected-note 2 {{'test18_c' declared here}}163void test18_d(int a, ...); // expected-note {{'test18_d' declared here}}164void test18_e(int a, int b, ...); // expected-note {{'test18_e' declared here}}165#define MY_EXPORT __attribute__((visibility("default")))166MY_EXPORT void // (no "declared here" notes on this line, no "expanded from MY_EXPORT" notes either)167test18_f(int a, int b); // expected-note 2 {{'test18_f' declared here}}168void test18(int b) {169 test18_a(b, b); // expected-error {{too many arguments to function call, expected single argument 'a', have 2}}170 test18_a(); // expected-error {{too few arguments to function call, single argument 'a' was not specified}}171 test18_b(); // expected-error {{too few arguments to function call, expected 1, have 0}}172 test18_c(b); // expected-error {{too few arguments to function call, expected 2, have 1}}173 test18_c(b, b, b); // expected-error {{too many arguments to function call, expected 2, have 3}}174 test18_d(); // expected-error {{too few arguments to function call, at least argument 'a' must be specified}}175 test18_e(); // expected-error {{too few arguments to function call, expected at least 2, have 0}}176 test18_f(b); // expected-error {{too few arguments to function call, expected 2, have 1}}177 test18_f(b, b, b); // expected-error {{too many arguments to function call, expected 2, have 3}}178}179 180typedef int __attribute__((address_space(256))) int_AS256;181// PR7569182void test19(void) {183 *(int *)0 = 0; // expected-warning {{indirection of non-volatile null pointer}} \184 // expected-note {{consider using __builtin_trap}}185 *(volatile int *)0 = 0; // Ok.186 *(int __attribute__((address_space(256))) *)0 = 0; // Ok.187 *(int __attribute__((address_space(0))) *)0 = 0; // expected-warning {{indirection of non-volatile null pointer}} \188 // expected-note {{consider using __builtin_trap}}189 *(int_AS256 *)0 = 0; // Ok.190 191 int x = *(int *)0; // expected-warning {{indirection of non-volatile null pointer}} \192 // expected-note {{consider using __builtin_trap}}193 int x2 = *(volatile int *)0; // Ok.194 int x3 = *(int __attribute__((address_space(0))) *)0; // expected-warning {{indirection of non-volatile null pointer}} \195 // expected-note {{consider using __builtin_trap}}196 int x4 = *(int_AS256 *)0; // Ok.197 int *p = &(*(int *)0); // Ok.198 int_AS256 *p1 = &(*(int __attribute__((address_space(256))) *)0); // Ok.199 int __attribute__((address_space(0))) *p2 = &(*(int __attribute__((address_space(0))) *)0); // Ok.200}201 202int test20(int x) {203 return x && 4; // 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 207 return x && sizeof(int) == 4; // no warning, RHS is logical op.208 209 // no warning, this is an idiom for "true" in old C style.210 return x && (signed char)1;211 212 return x || 0;213 return x || 1;214 return x || -1; // expected-warning {{use of logical '||' with constant operand}} \215 // expected-note {{use '|' for a bitwise operation}}216 return x || 5; // expected-warning {{use of logical '||' with constant operand}} \217 // expected-note {{use '|' for a bitwise operation}}218 return x && 0;219 return x && 1;220 return x && -1; // 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 return x && 5; // expected-warning {{use of logical '&&' with constant operand}} \224 // expected-note {{use '&' for a bitwise operation}} \225 // expected-note {{remove constant to silence this warning}}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 return x || (5); // expected-warning {{use of logical '||' with constant operand}} \231 // expected-note {{use '|' for a bitwise operation}}232 return x && (0);233 return x && (1);234 return x && (-1); // expected-warning {{use of logical '&&' with constant operand}} \235 // expected-note {{use '&' for a bitwise operation}} \236 // expected-note {{remove constant to silence this warning}}237 return x && (5); // expected-warning {{use of logical '&&' with constant operand}} \238 // expected-note {{use '&' for a bitwise operation}} \239 // expected-note {{remove constant to silence this warning}}240 241}242 243struct Test21; // expected-note 2 {{forward declaration}}244void test21(volatile struct Test21 *ptr) {245 void test21_help(void);246 (test21_help(), *ptr); // expected-error {{incomplete type 'struct Test21' where a complete type is required}}247 (*ptr, test21_help()); // expected-error {{incomplete type 'struct Test21' where a complete type is required}}248}249 250// Make sure we do function/array decay.251void test22(void) {252 if ("help")253 (void) 0;254 255 if (test22) // expected-warning {{address of function 'test22' will always evaluate to 'true'}} \256 // expected-note {{prefix with the address-of operator to silence this warning}}257 (void) 0;258 259 if (&test22)260 (void) 0;261}262