Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only |
Chris Lattner | 3508084 | 2008-02-02 20:20:10 +0000 | [diff] [blame] | 2 | |
Ted Kremenek | 377e215 | 2011-02-23 02:15:19 +0000 | [diff] [blame] | 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 | |
Ted Kremenek | a8d459e | 2011-03-23 21:33:21 +0000 | [diff] [blame] | 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() { |
| 18 | if (0) { |
| 19 | 0 / (0 ? 1 : 0); // expected-warning {{expression result unused}} |
| 20 | } |
| 21 | } |
| 22 | |
Ted Kremenek | 377e215 | 2011-02-23 02:15:19 +0000 | [diff] [blame] | 23 | int 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 | |
Ted Kremenek | c56c004 | 2011-02-23 05:11:46 +0000 | [diff] [blame] | 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) |
| 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 | |
Chris Lattner | 3508084 | 2008-02-02 20:20:10 +0000 | [diff] [blame] | 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 | |
Chris Lattner | baf0d66 | 2008-07-25 18:07:19 +0000 | [diff] [blame] | 46 | // rdar://6097308 |
| 47 | void test3() { |
| 48 | int x; |
| 49 | (__extension__ x) = 10; |
| 50 | } |
| 51 | |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 52 | // rdar://6162726 |
| 53 | void 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 (-=)}} |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 57 | var = +5; // no warning when space between the = and +. |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 58 | var = -5; |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 59 | |
| 60 | var =+5; // no warning when the subexpr of the unary op has no space before it. |
| 61 | var =-5; |
Chris Lattner | 3e87209 | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 62 | |
| 63 | #define FIVE 5 |
| 64 | var=-FIVE; // no warning with macros. |
| 65 | var=-FIVE; |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 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}} |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 71 | #define FOO ((float*) X) |
| 72 | FOO = P; // expected-error {{assignment to cast is illegal, lvalue casts are not supported}} |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Chris Lattner | b1b4d33 | 2008-11-21 18:27:34 +0000 | [diff] [blame] | 75 | void test6() { |
| 76 | int X; |
| 77 | X(); // expected-error {{called object type 'int' is not a function or function pointer}} |
| 78 | } |
Chris Lattner | 7ca1425 | 2008-11-22 19:57:03 +0000 | [diff] [blame] | 79 | |
| 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')}} |
| 82 | } |
| 83 | |
Chris Lattner | 670a62c | 2008-12-12 05:35:08 +0000 | [diff] [blame] | 84 | |
| 85 | // rdar://6095061 |
| 86 | int test8(void) { |
| 87 | int i; |
Eli Friedman | eb32fde | 2009-04-28 03:13:54 +0000 | [diff] [blame] | 88 | __builtin_choose_expr (0, 42, i) = 10; |
Chris Lattner | 670a62c | 2008-12-12 05:35:08 +0000 | [diff] [blame] | 89 | return i; |
| 90 | } |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 91 | |
| 92 | |
| 93 | // PR3386 |
| 94 | struct f { int x : 4; float y[]; }; |
| 95 | int test9(struct f *P) { |
Chris Lattner | da02747 | 2009-01-24 21:29:22 +0000 | [diff] [blame] | 96 | int R; |
Anders Carlsson | 31fddcc | 2009-09-14 22:00:20 +0000 | [diff] [blame] | 97 | R = __alignof(P->x); // expected-error {{invalid application of '__alignof' to bit-field}} |
Eli Friedman | eb32fde | 2009-04-28 03:13:54 +0000 | [diff] [blame] | 98 | R = __alignof(P->y); // ok. |
Anders Carlsson | 31fddcc | 2009-09-14 22:00:20 +0000 | [diff] [blame] | 99 | R = sizeof(P->x); // expected-error {{invalid application of 'sizeof' to bit-field}} |
Chris Lattner | da02747 | 2009-01-24 21:29:22 +0000 | [diff] [blame] | 100 | return R; |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Chris Lattner | 56cd21b | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 103 | // PR3562 |
| 104 | void 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 | } |
Chris Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 110 | |
| 111 | |
| 112 | #define MYMAX(A,B) __extension__ ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __b : __a; }) |
| 113 | |
| 114 | struct mystruct {int A; }; |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 115 | void test11(struct mystruct P, float F) { |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 116 | MYMAX(P, F); // expected-error {{invalid operands to binary expression ('typeof (P)' (aka 'struct mystruct') and 'typeof (F)' (aka 'float'))}} |
Chris Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 119 | // PR3753 |
| 120 | int test12(const char *X) { |
Ted Kremenek | 03a4bee | 2010-04-09 20:26:53 +0000 | [diff] [blame] | 121 | return X == "foo"; // expected-warning {{comparison against a string literal is unspecified (use strncmp instead)}} |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Douglas Gregor | d1e4d9b | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 124 | int test12b(const char *X) { |
| 125 | return sizeof(X == "foo"); // no-warning |
| 126 | } |
| 127 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 128 | // rdar://6719156 |
| 129 | void 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 | } |
Chris Lattner | d013aa1 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 134 | |
Chris Lattner | d013aa1 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 135 | void test14() { |
Eli Friedman | eb32fde | 2009-04-28 03:13:54 +0000 | [diff] [blame] | 136 | typedef long long __m64 __attribute__((__vector_size__(8))); |
| 137 | typedef short __v4hi __attribute__((__vector_size__(8))); |
Chris Lattner | d013aa1 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 138 | |
Chris Lattner | 9c10fcf | 2009-07-08 01:08:03 +0000 | [diff] [blame] | 139 | // Ok. |
Chris Lattner | d013aa1 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 140 | __v4hi a; |
Chris Lattner | 9c10fcf | 2009-07-08 01:08:03 +0000 | [diff] [blame] | 141 | __m64 mask = (__m64)((__v4hi)a > (__v4hi)a); |
Chris Lattner | d013aa1 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Chris Lattner | 0a026af | 2009-10-20 05:36:05 +0000 | [diff] [blame] | 144 | |
| 145 | // PR5242 |
| 146 | typedef unsigned long *test15_t; |
| 147 | |
| 148 | test15_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 | |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 152 | // rdar://7446395 |
| 153 | void test16(float x) { x == ((void*) 0); } // expected-error {{invalid operands to binary expression}} |
Chris Lattner | 0a026af | 2009-10-20 05:36:05 +0000 | [diff] [blame] | 154 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 155 | // PR6004 |
| 156 | void 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}} |
Chris Lattner | cb329c5 | 2010-01-12 21:30:55 +0000 | [diff] [blame] | 161 | |
| 162 | x = sizeof(x/0); // no warning. |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Richard Smith | f7b8056 | 2012-05-11 05:16:41 +0000 | [diff] [blame^] | 165 | // PR6501 & PR11857 |
Peter Collingbourne | 9aab148 | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 166 | void test18_a(int a); // expected-note 2 {{'test18_a' declared here}} |
Richard Smith | f7b8056 | 2012-05-11 05:16:41 +0000 | [diff] [blame^] | 167 | void test18_b(int); // expected-note {{'test18_b' declared here}} |
| 168 | void test18_c(int a, int b); // expected-note {{'test18_c' declared here}} |
| 169 | void test18_d(int a, ...); // expected-note {{'test18_d' declared here}} |
Eric Christopher | c5e869b | 2010-04-19 18:39:43 +0000 | [diff] [blame] | 170 | void test18(int b) { |
| 171 | test18_a(b, b); // expected-error {{too many arguments to function call, expected 1, have 2}} |
Richard Smith | f7b8056 | 2012-05-11 05:16:41 +0000 | [diff] [blame^] | 172 | test18_a(); // expected-error {{too few arguments to function call, argument 'a' was not specified}} |
| 173 | test18_b(); // expected-error {{too few arguments to function call, expected 1, have 0}} |
| 174 | test18_c(b); // expected-error {{too few arguments to function call, expected 2, have 1}} |
| 175 | test18_d(); // expected-error {{too few arguments to function call, at least argument 'a' must be specified}} |
Eric Christopher | c5e869b | 2010-04-19 18:39:43 +0000 | [diff] [blame] | 176 | } |
Chris Lattner | 8b5dec3 | 2010-07-07 06:14:23 +0000 | [diff] [blame] | 177 | |
| 178 | // PR7569 |
| 179 | void test19() { |
| 180 | *(int*)0 = 0; // expected-warning {{indirection of non-volatile null pointer}} \ |
| 181 | // expected-note {{consider using __builtin_trap}} |
| 182 | *(volatile int*)0 = 0; // Ok. |
Argyrios Kyrtzidis | 8a285ae | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 183 | |
| 184 | // rdar://9269271 |
| 185 | int x = *(int*)0; // expected-warning {{indirection of non-volatile null pointer}} \ |
| 186 | // expected-note {{consider using __builtin_trap}} |
| 187 | int x2 = *(volatile int*)0; // Ok. |
| 188 | int *p = &(*(int*)0); // Ok; |
Chris Lattner | 8b5dec3 | 2010-07-07 06:14:23 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 191 | int test20(int x) { |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 192 | return x && 4; // expected-warning {{use of logical '&&' with constant operand}} \ |
| 193 | // expected-note {{use '&' for a bitwise operation}} \ |
| 194 | // expected-note {{remove constant to silence this warning}} |
Chris Lattner | 23ef3e4 | 2010-07-15 00:26:43 +0000 | [diff] [blame] | 195 | |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 196 | return x && sizeof(int) == 4; // no warning, RHS is logical op. |
| 197 | |
| 198 | // no warning, this is an idiom for "true" in old C style. |
| 199 | return x && (signed char)1; |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 200 | |
| 201 | return x || 0; |
| 202 | return x || 1; |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 203 | return x || -1; // expected-warning {{use of logical '||' with constant operand}} \ |
| 204 | // expected-note {{use '|' for a bitwise operation}} |
| 205 | return x || 5; // expected-warning {{use of logical '||' with constant operand}} \ |
| 206 | // expected-note {{use '|' for a bitwise operation}} |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 207 | return x && 0; |
| 208 | return x && 1; |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 209 | return x && -1; // expected-warning {{use of logical '&&' with constant operand}} \ |
| 210 | // expected-note {{use '&' for a bitwise operation}} \ |
| 211 | // expected-note {{remove constant to silence this warning}} |
| 212 | return x && 5; // expected-warning {{use of logical '&&' with constant operand}} \ |
| 213 | // expected-note {{use '&' for a bitwise operation}} \ |
| 214 | // expected-note {{remove constant to silence this warning}} |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 215 | return x || (0); |
| 216 | return x || (1); |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 217 | return x || (-1); // expected-warning {{use of logical '||' with constant operand}} \ |
| 218 | // expected-note {{use '|' for a bitwise operation}} |
| 219 | return x || (5); // expected-warning {{use of logical '||' with constant operand}} \ |
| 220 | // expected-note {{use '|' for a bitwise operation}} |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 221 | return x && (0); |
| 222 | return x && (1); |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 223 | return x && (-1); // 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 && (5); // expected-warning {{use of logical '&&' with constant operand}} \ |
| 227 | // expected-note {{use '&' for a bitwise operation}} \ |
| 228 | // expected-note {{remove constant to silence this warning}} |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 229 | |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 230 | } |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 231 | |
| 232 | struct Test21; // expected-note 2 {{forward declaration}} |
| 233 | void test21(volatile struct Test21 *ptr) { |
| 234 | void test21_help(void); |
| 235 | (test21_help(), *ptr); // expected-error {{incomplete type 'struct Test21' where a complete type is required}} |
| 236 | (*ptr, test21_help()); // expected-error {{incomplete type 'struct Test21' where a complete type is required}} |
| 237 | } |
John McCall | abc56c7 | 2010-12-04 06:09:13 +0000 | [diff] [blame] | 238 | |
| 239 | // Make sure we do function/array decay. |
| 240 | void test22() { |
| 241 | if ("help") |
| 242 | (void) 0; |
| 243 | |
| 244 | if (test22) |
| 245 | (void) 0; |
| 246 | } |