Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -verify %s |
| 2 | |
| 3 | int foo() { |
Ted Kremenek | 3a854f8 | 2011-02-16 22:08:28 +0000 | [diff] [blame] | 4 | int x[2]; // expected-note 4 {{array 'x' declared here}} |
| 5 | int y[2]; // expected-note 2 {{array 'y' declared here}} |
Chandler Carruth | 28389f0 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 6 | int z[1]; // expected-note {{array 'z' declared here}} |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 7 | int *p = &y[2]; // no-warning |
| 8 | (void) sizeof(x[2]); // no-warning |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 9 | y[2] = 2; // expected-warning {{array index of '2' indexes past the end of an array (that contains 2 elements)}} |
Chandler Carruth | 28389f0 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 10 | z[1] = 'x'; // expected-warning {{array index of '1' indexes past the end of an array (that contains 1 element)}} |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 11 | return x[2] + // expected-warning {{array index of '2' indexes past the end of an array (that contains 2 elements)}} |
| 12 | y[-1] + // expected-warning {{array index of '-1' indexes before the beginning of the array}} |
| 13 | x[sizeof(x)] + // expected-warning {{array index of '8' indexes past the end of an array (that contains 2 elements)}} |
| 14 | x[sizeof(x) / sizeof(x[0])] + // expected-warning {{array index of '2' indexes past the end of an array (that contains 2 elements)}} |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 15 | x[sizeof(x) / sizeof(x[0]) - 1] + // no-warning |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 16 | x[sizeof(x[2])]; // expected-warning {{array index of '4' indexes past the end of an array (that contains 2 elements)}} |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 17 | } |
| 18 | |
Ted Kremenek | 197fcd4 | 2011-02-16 23:39:09 +0000 | [diff] [blame] | 19 | // This code example tests that -Warray-bounds works with arrays that |
| 20 | // are template parameters. |
| 21 | template <char *sz> class Qux { |
| 22 | bool test() { return sz[0] == 'a'; } |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | void f1(int a[1]) { |
| 26 | int val = a[3]; // no warning for function argumnet |
| 27 | } |
| 28 | |
Chris Lattner | f51dae0 | 2011-08-02 21:44:23 +0000 | [diff] [blame] | 29 | void f2(const int (&a)[2]) { // expected-note {{declared here}} |
| 30 | int val = a[3]; // expected-warning {{array index of '3' indexes past the end of an array (that contains 2 elements)}} |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void test() { |
| 34 | struct { |
| 35 | int a[0]; |
| 36 | } s2; |
| 37 | s2.a[3] = 0; // no warning for 0-sized array |
| 38 | |
| 39 | union { |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 40 | short a[2]; // expected-note 4 {{declared here}} |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 41 | char c[4]; |
| 42 | } u; |
| 43 | u.a[3] = 1; // expected-warning {{array index of '3' indexes past the end of an array (that contains 2 elements)}} |
| 44 | u.c[3] = 1; // no warning |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 45 | short *p = &u.a[2]; // no warning |
| 46 | p = &u.a[3]; // expected-warning {{array index of '3' indexes past the end of an array (that contains 2 elements)}} |
| 47 | *(&u.a[2]) = 1; // expected-warning {{array index of '2' indexes past the end of an array (that contains 2 elements)}} |
| 48 | *(&u.a[3]) = 1; // expected-warning {{array index of '3' indexes past the end of an array (that contains 2 elements)}} |
| 49 | *(&u.c[3]) = 1; // no warning |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 50 | |
| 51 | const int const_subscript = 3; |
Chris Lattner | f51dae0 | 2011-08-02 21:44:23 +0000 | [diff] [blame] | 52 | int array[2]; // expected-note {{declared here}} |
| 53 | array[const_subscript] = 0; // expected-warning {{array index of '3' indexes past the end of an array (that contains 2 elements)}} |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 54 | |
| 55 | int *ptr; |
| 56 | ptr[3] = 0; // no warning for pointer references |
| 57 | int array2[] = { 0, 1, 2 }; // expected-note 2 {{declared here}} |
| 58 | |
| 59 | array2[3] = 0; // expected-warning {{array index of '3' indexes past the end of an array (that contains 3 elements)}} |
| 60 | array2[2+2] = 0; // expected-warning {{array index of '4' indexes past the end of an array (that contains 3 elements)}} |
| 61 | |
| 62 | const char *str1 = "foo"; |
| 63 | char c1 = str1[5]; // no warning for pointers |
| 64 | |
| 65 | const char str2[] = "foo"; // expected-note {{declared here}} |
| 66 | char c2 = str2[5]; // expected-warning {{array index of '5' indexes past the end of an array (that contains 4 elements)}} |
| 67 | |
Chris Lattner | f51dae0 | 2011-08-02 21:44:23 +0000 | [diff] [blame] | 68 | int (*array_ptr)[2]; |
| 69 | (*array_ptr)[3] = 1; // expected-warning {{array index of '3' indexes past the end of an array (that contains 2 elements)}} |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | template <int I> struct S { |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 73 | char arr[I]; // expected-note 2 {{declared here}} |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 74 | }; |
| 75 | template <int I> void f() { |
| 76 | S<3> s; |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 77 | s.arr[4] = 0; // expected-warning {{array index of '4' indexes past the end of an array (that contains 3 elements)}} |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 78 | s.arr[I] = 0; // expected-warning {{array index of '5' indexes past the end of an array (that contains 3 elements)}} |
| 79 | } |
| 80 | |
| 81 | void test_templates() { |
| 82 | f<5>(); // expected-note {{in instantiation}} |
| 83 | } |
Ted Kremenek | 48d9626 | 2011-02-17 21:40:51 +0000 | [diff] [blame] | 84 | |
| 85 | #define SIZE 10 |
| 86 | #define ARR_IN_MACRO(flag, arr, idx) flag ? arr[idx] : 1 |
| 87 | |
| 88 | int test_no_warn_macro_unreachable() { |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 89 | int arr[SIZE]; // expected-note {{array 'arr' declared here}} |
| 90 | return ARR_IN_MACRO(0, arr, SIZE) + // no-warning |
Ted Kremenek | 48d9626 | 2011-02-17 21:40:51 +0000 | [diff] [blame] | 91 | ARR_IN_MACRO(1, arr, SIZE); // expected-warning{{array index of '10' indexes past the end of an array (that contains 10 elements)}} |
| 92 | } |
| 93 | |
Ted Kremenek | a7ced2c | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 94 | // This exhibited an assertion failure for a 32-bit build of Clang. |
| 95 | int test_pr9240() { |
| 96 | short array[100]; // expected-note {{array 'array' declared here}} |
| 97 | return array[(unsigned long long) 100]; // expected-warning {{array index of '100' indexes past the end of an array (that contains 100 elements)}} |
| 98 | } |
| 99 | |
Ted Kremenek | 477c8f5 | 2011-02-23 01:52:07 +0000 | [diff] [blame] | 100 | // PR 9284 - a template parameter can cause an array bounds access to be |
| 101 | // infeasible. |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 102 | template <bool extendArray> |
Ted Kremenek | 477c8f5 | 2011-02-23 01:52:07 +0000 | [diff] [blame] | 103 | void pr9284() { |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 104 | int arr[3 + (extendArray ? 1 : 0)]; |
| 105 | |
| 106 | if (extendArray) |
Ted Kremenek | 477c8f5 | 2011-02-23 01:52:07 +0000 | [diff] [blame] | 107 | arr[3] = 42; // no-warning |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Ted Kremenek | 477c8f5 | 2011-02-23 01:52:07 +0000 | [diff] [blame] | 110 | template <bool extendArray> |
| 111 | void pr9284b() { |
| 112 | int arr[3 + (extendArray ? 1 : 0)]; // expected-note {{array 'arr' declared here}} |
| 113 | |
| 114 | if (!extendArray) |
| 115 | arr[3] = 42; // expected-warning{{array index of '3' indexes past the end of an array (that contains 3 elements)}} |
| 116 | } |
| 117 | |
| 118 | void test_pr9284() { |
| 119 | pr9284<true>(); |
| 120 | pr9284<false>(); |
| 121 | pr9284b<true>(); |
| 122 | pr9284b<false>(); // expected-note{{in instantiation of function template specialization 'pr9284b<false>' requested here}} |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Ted Kremenek | e4b316c | 2011-02-23 23:06:04 +0000 | [diff] [blame] | 125 | int test_pr9296() { |
| 126 | int array[2]; |
| 127 | return array[true]; // no-warning |
| 128 | } |
| 129 | |
Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 130 | int test_sizeof_as_condition(int flag) { |
| 131 | int arr[2] = { 0, 0 }; // expected-note {{array 'arr' declared here}} |
| 132 | if (flag) |
| 133 | return sizeof(char) != sizeof(char) ? arr[2] : arr[1]; |
| 134 | return sizeof(char) == sizeof(char) ? arr[2] : arr[1]; // expected-warning {{array index of '2' indexes past the end of an array (that contains 2 elements)}} |
| 135 | } |
| 136 | |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 137 | void test_switch() { |
| 138 | switch (4) { |
| 139 | case 1: { |
| 140 | int arr[2]; |
| 141 | arr[2] = 1; // no-warning |
| 142 | break; |
| 143 | } |
| 144 | case 4: { |
| 145 | int arr[2]; // expected-note {{array 'arr' declared here}} |
| 146 | arr[2] = 1; // expected-warning {{array index of '2' indexes past the end of an array (that contains 2 elements)}} |
| 147 | break; |
| 148 | } |
| 149 | default: { |
| 150 | int arr[2]; |
| 151 | arr[2] = 1; // no-warning |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | } |
Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 156 | |
Ted Kremenek | be52871 | 2011-03-04 01:03:41 +0000 | [diff] [blame] | 157 | // Test nested switch statements. |
| 158 | enum enumA { enumA_A, enumA_B, enumA_C, enumA_D, enumA_E }; |
| 159 | enum enumB { enumB_X, enumB_Y, enumB_Z }; |
| 160 | static enum enumB myVal = enumB_X; |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 161 | void test_nested_switch() { |
Ted Kremenek | be52871 | 2011-03-04 01:03:41 +0000 | [diff] [blame] | 162 | switch (enumA_E) { // expected-warning {{no case matching constant}} |
| 163 | switch (myVal) { // expected-warning {{enumeration values 'enumB_X' and 'enumB_Z' not handled in switch}} |
| 164 | case enumB_Y: ; |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
Ted Kremenek | 35c70f6 | 2011-03-16 04:32:01 +0000 | [diff] [blame] | 169 | // Test that if all the values of an enum covered, that the 'default' branch |
| 170 | // is unreachable. |
| 171 | enum Values { A, B, C, D }; |
| 172 | void test_all_enums_covered(enum Values v) { |
| 173 | int x[2]; |
| 174 | switch (v) { |
| 175 | case A: return; |
| 176 | case B: return; |
| 177 | case C: return; |
| 178 | case D: return; |
| 179 | } |
| 180 | x[2] = 0; // no-warning |
| 181 | } |
Chris Lattner | f51dae0 | 2011-08-02 21:44:23 +0000 | [diff] [blame] | 182 | |
| 183 | namespace tailpad { |
| 184 | struct foo { |
Chandler Carruth | 28389f0 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 185 | char c1[1]; // expected-note {{declared here}} |
Chris Lattner | f51dae0 | 2011-08-02 21:44:23 +0000 | [diff] [blame] | 186 | int x; |
Chandler Carruth | 28389f0 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 187 | char c2[1]; |
Chris Lattner | f51dae0 | 2011-08-02 21:44:23 +0000 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | char bar(struct foo *F) { |
Chandler Carruth | 28389f0 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 191 | return F->c1[3]; // expected-warning {{array index of '3' indexes past the end of an array (that contains 1 element)}} |
| 192 | return F->c2[3]; // no warning, foo could have tail padding allocated. |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | namespace metaprogramming { |
| 197 | #define ONE 1 |
| 198 | struct foo { char c[ONE]; }; // expected-note {{declared here}} |
| 199 | template <int N> struct bar { char c[N]; }; // expected-note {{declared here}} |
| 200 | |
| 201 | char test(foo *F, bar<1> *B) { |
| 202 | return F->c[3] + // expected-warning {{array index of '3' indexes past the end of an array (that contains 1 element)}} |
| 203 | B->c[3]; // expected-warning {{array index of '3' indexes past the end of an array (that contains 1 element)}} |
Chris Lattner | f51dae0 | 2011-08-02 21:44:23 +0000 | [diff] [blame] | 204 | } |
| 205 | } |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 206 | |
| 207 | void bar(int x) {} |
| 208 | int test_more() { |
| 209 | int foo[5]; // expected-note 5 {{array 'foo' declared here}} |
| 210 | bar(foo[5]); // expected-warning {{array index of '5' indexes past the end of an array (that contains 5 elements)}} |
| 211 | ++foo[5]; // expected-warning {{array index of '5' indexes past the end of an array (that contains 5 elements)}} |
| 212 | if (foo[6]) // expected-warning {{array index of '6' indexes past the end of an array (that contains 5 elements)}} |
| 213 | return --foo[6]; // expected-warning {{array index of '6' indexes past the end of an array (that contains 5 elements)}} |
| 214 | else |
| 215 | return foo[5]; // expected-warning {{array index of '5' indexes past the end of an array (that contains 5 elements)}} |
| 216 | } |