Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -verify %s |
| 2 | |
| 3 | int foo() { |
Ted Kremenek | bac7737 | 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 | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame^] | 6 | int z[1]; // expected-note {{array 'z' declared here}} |
Ted Kremenek | a0125d8 | 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 | 35001ca | 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 | c268434 | 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 | 35001ca | 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 | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 15 | x[sizeof(x) / sizeof(x[0]) - 1] + // no-warning |
Chandler Carruth | 35001ca | 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 | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 17 | } |
| 18 | |
Ted Kremenek | c71a2c0 | 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 | 35001ca | 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 | 9e6a1ca | 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 | 35001ca | 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 | 2760455 | 2011-07-26 18:36:36 +0000 | [diff] [blame] | 40 | short a[2]; // expected-note {{declared here}} |
Chandler Carruth | 35001ca | 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 |
| 45 | |
| 46 | const int const_subscript = 3; |
Chris Lattner | 9e6a1ca | 2011-08-02 21:44:23 +0000 | [diff] [blame] | 47 | int array[2]; // expected-note {{declared here}} |
| 48 | array[const_subscript] = 0; // expected-warning {{array index of '3' indexes past the end of an array (that contains 2 elements)}} |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 49 | |
| 50 | int *ptr; |
| 51 | ptr[3] = 0; // no warning for pointer references |
| 52 | int array2[] = { 0, 1, 2 }; // expected-note 2 {{declared here}} |
| 53 | |
| 54 | array2[3] = 0; // expected-warning {{array index of '3' indexes past the end of an array (that contains 3 elements)}} |
| 55 | array2[2+2] = 0; // expected-warning {{array index of '4' indexes past the end of an array (that contains 3 elements)}} |
| 56 | |
| 57 | const char *str1 = "foo"; |
| 58 | char c1 = str1[5]; // no warning for pointers |
| 59 | |
| 60 | const char str2[] = "foo"; // expected-note {{declared here}} |
| 61 | char c2 = str2[5]; // expected-warning {{array index of '5' indexes past the end of an array (that contains 4 elements)}} |
| 62 | |
Chris Lattner | 9e6a1ca | 2011-08-02 21:44:23 +0000 | [diff] [blame] | 63 | int (*array_ptr)[2]; |
| 64 | (*array_ptr)[3] = 1; // expected-warning {{array index of '3' indexes past the end of an array (that contains 2 elements)}} |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | template <int I> struct S { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 68 | char arr[I]; // expected-note 2 {{declared here}} |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 69 | }; |
| 70 | template <int I> void f() { |
| 71 | S<3> s; |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 72 | s.arr[4] = 0; // expected-warning {{array index of '4' indexes past the end of an array (that contains 3 elements)}} |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 73 | s.arr[I] = 0; // expected-warning {{array index of '5' indexes past the end of an array (that contains 3 elements)}} |
| 74 | } |
| 75 | |
| 76 | void test_templates() { |
| 77 | f<5>(); // expected-note {{in instantiation}} |
| 78 | } |
Ted Kremenek | a85f528 | 2011-02-17 21:40:51 +0000 | [diff] [blame] | 79 | |
| 80 | #define SIZE 10 |
| 81 | #define ARR_IN_MACRO(flag, arr, idx) flag ? arr[idx] : 1 |
| 82 | |
| 83 | int test_no_warn_macro_unreachable() { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 84 | int arr[SIZE]; // expected-note {{array 'arr' declared here}} |
| 85 | return ARR_IN_MACRO(0, arr, SIZE) + // no-warning |
Ted Kremenek | a85f528 | 2011-02-17 21:40:51 +0000 | [diff] [blame] | 86 | ARR_IN_MACRO(1, arr, SIZE); // expected-warning{{array index of '10' indexes past the end of an array (that contains 10 elements)}} |
| 87 | } |
| 88 | |
Ted Kremenek | 25b3b84 | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 89 | // This exhibited an assertion failure for a 32-bit build of Clang. |
| 90 | int test_pr9240() { |
| 91 | short array[100]; // expected-note {{array 'array' declared here}} |
| 92 | return array[(unsigned long long) 100]; // expected-warning {{array index of '100' indexes past the end of an array (that contains 100 elements)}} |
| 93 | } |
| 94 | |
Ted Kremenek | 3bcc2be | 2011-02-23 01:52:07 +0000 | [diff] [blame] | 95 | // PR 9284 - a template parameter can cause an array bounds access to be |
| 96 | // infeasible. |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 97 | template <bool extendArray> |
Ted Kremenek | 3bcc2be | 2011-02-23 01:52:07 +0000 | [diff] [blame] | 98 | void pr9284() { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 99 | int arr[3 + (extendArray ? 1 : 0)]; |
| 100 | |
| 101 | if (extendArray) |
Ted Kremenek | 3bcc2be | 2011-02-23 01:52:07 +0000 | [diff] [blame] | 102 | arr[3] = 42; // no-warning |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Ted Kremenek | 3bcc2be | 2011-02-23 01:52:07 +0000 | [diff] [blame] | 105 | template <bool extendArray> |
| 106 | void pr9284b() { |
| 107 | int arr[3 + (extendArray ? 1 : 0)]; // expected-note {{array 'arr' declared here}} |
| 108 | |
| 109 | if (!extendArray) |
| 110 | arr[3] = 42; // expected-warning{{array index of '3' indexes past the end of an array (that contains 3 elements)}} |
| 111 | } |
| 112 | |
| 113 | void test_pr9284() { |
| 114 | pr9284<true>(); |
| 115 | pr9284<false>(); |
| 116 | pr9284b<true>(); |
| 117 | pr9284b<false>(); // expected-note{{in instantiation of function template specialization 'pr9284b<false>' requested here}} |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Ted Kremenek | 9e060ca | 2011-02-23 23:06:04 +0000 | [diff] [blame] | 120 | int test_pr9296() { |
| 121 | int array[2]; |
| 122 | return array[true]; // no-warning |
| 123 | } |
| 124 | |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 125 | int test_sizeof_as_condition(int flag) { |
| 126 | int arr[2] = { 0, 0 }; // expected-note {{array 'arr' declared here}} |
| 127 | if (flag) |
| 128 | return sizeof(char) != sizeof(char) ? arr[2] : arr[1]; |
| 129 | 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)}} |
| 130 | } |
| 131 | |
Ted Kremenek | e71f3d5 | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 132 | void test_switch() { |
| 133 | switch (4) { |
| 134 | case 1: { |
| 135 | int arr[2]; |
| 136 | arr[2] = 1; // no-warning |
| 137 | break; |
| 138 | } |
| 139 | case 4: { |
| 140 | int arr[2]; // expected-note {{array 'arr' declared here}} |
| 141 | arr[2] = 1; // expected-warning {{array index of '2' indexes past the end of an array (that contains 2 elements)}} |
| 142 | break; |
| 143 | } |
| 144 | default: { |
| 145 | int arr[2]; |
| 146 | arr[2] = 1; // no-warning |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | } |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 151 | |
Ted Kremenek | 0498247 | 2011-03-04 01:03:41 +0000 | [diff] [blame] | 152 | // Test nested switch statements. |
| 153 | enum enumA { enumA_A, enumA_B, enumA_C, enumA_D, enumA_E }; |
| 154 | enum enumB { enumB_X, enumB_Y, enumB_Z }; |
| 155 | static enum enumB myVal = enumB_X; |
Kaelyn Uhrain | 2760455 | 2011-07-26 18:36:36 +0000 | [diff] [blame] | 156 | void test_nested_switch() |
| 157 | { |
Ted Kremenek | 0498247 | 2011-03-04 01:03:41 +0000 | [diff] [blame] | 158 | switch (enumA_E) { // expected-warning {{no case matching constant}} |
| 159 | switch (myVal) { // expected-warning {{enumeration values 'enumB_X' and 'enumB_Z' not handled in switch}} |
| 160 | case enumB_Y: ; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
Ted Kremenek | 432c478 | 2011-03-16 04:32:01 +0000 | [diff] [blame] | 165 | // Test that if all the values of an enum covered, that the 'default' branch |
| 166 | // is unreachable. |
| 167 | enum Values { A, B, C, D }; |
| 168 | void test_all_enums_covered(enum Values v) { |
| 169 | int x[2]; |
| 170 | switch (v) { |
| 171 | case A: return; |
| 172 | case B: return; |
| 173 | case C: return; |
| 174 | case D: return; |
| 175 | } |
| 176 | x[2] = 0; // no-warning |
| 177 | } |
Chris Lattner | 9e6a1ca | 2011-08-02 21:44:23 +0000 | [diff] [blame] | 178 | |
| 179 | namespace tailpad { |
| 180 | struct foo { |
Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame^] | 181 | char c1[1]; // expected-note {{declared here}} |
Chris Lattner | 9e6a1ca | 2011-08-02 21:44:23 +0000 | [diff] [blame] | 182 | int x; |
Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame^] | 183 | char c2[1]; |
Chris Lattner | 9e6a1ca | 2011-08-02 21:44:23 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | char bar(struct foo *F) { |
Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame^] | 187 | return F->c1[3]; // expected-warning {{array index of '3' indexes past the end of an array (that contains 1 element)}} |
| 188 | return F->c2[3]; // no warning, foo could have tail padding allocated. |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | namespace metaprogramming { |
| 193 | #define ONE 1 |
| 194 | struct foo { char c[ONE]; }; // expected-note {{declared here}} |
| 195 | template <int N> struct bar { char c[N]; }; // expected-note {{declared here}} |
| 196 | |
| 197 | char test(foo *F, bar<1> *B) { |
| 198 | return F->c[3] + // expected-warning {{array index of '3' indexes past the end of an array (that contains 1 element)}} |
| 199 | B->c[3]; // expected-warning {{array index of '3' indexes past the end of an array (that contains 1 element)}} |
Chris Lattner | 9e6a1ca | 2011-08-02 21:44:23 +0000 | [diff] [blame] | 200 | } |
| 201 | } |