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}} |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 6 | int *p = &y[2]; // no-warning |
| 7 | (void) sizeof(x[2]); // no-warning |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 8 | y[2] = 2; // expected-warning {{array index of '2' indexes past the end of an array (that contains 2 elements)}} |
| 9 | return x[2] + // expected-warning {{array index of '2' indexes past the end of an array (that contains 2 elements)}} |
| 10 | y[-1] + // expected-warning {{array index of '-1' indexes before the beginning of the array}} |
| 11 | x[sizeof(x)] + // expected-warning {{array index of '8' indexes past the end of an array (that contains 2 elements)}} |
| 12 | 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] | 13 | x[sizeof(x) / sizeof(x[0]) - 1] + // no-warning |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 14 | 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] | 15 | } |
| 16 | |
Ted Kremenek | c71a2c0 | 2011-02-16 23:39:09 +0000 | [diff] [blame] | 17 | // This code example tests that -Warray-bounds works with arrays that |
| 18 | // are template parameters. |
| 19 | template <char *sz> class Qux { |
| 20 | bool test() { return sz[0] == 'a'; } |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 21 | }; |
| 22 | |
| 23 | void f1(int a[1]) { |
| 24 | int val = a[3]; // no warning for function argumnet |
| 25 | } |
| 26 | |
| 27 | void f2(const int (&a)[1]) { // expected-note {{declared here}} |
| 28 | int val = a[3]; // expected-warning {{array index of '3' indexes past the end of an array (that contains 1 elements)}} |
| 29 | } |
| 30 | |
| 31 | void test() { |
| 32 | struct { |
| 33 | int a[0]; |
| 34 | } s2; |
| 35 | s2.a[3] = 0; // no warning for 0-sized array |
| 36 | |
| 37 | union { |
| 38 | short a[2]; // expected-note {{declared here}} |
| 39 | char c[4]; |
| 40 | } u; |
| 41 | u.a[3] = 1; // expected-warning {{array index of '3' indexes past the end of an array (that contains 2 elements)}} |
| 42 | u.c[3] = 1; // no warning |
| 43 | |
| 44 | const int const_subscript = 3; |
| 45 | int array[1]; // expected-note {{declared here}} |
| 46 | array[const_subscript] = 0; // expected-warning {{array index of '3' indexes past the end of an array (that contains 1 elements)}} |
| 47 | |
| 48 | int *ptr; |
| 49 | ptr[3] = 0; // no warning for pointer references |
| 50 | int array2[] = { 0, 1, 2 }; // expected-note 2 {{declared here}} |
| 51 | |
| 52 | array2[3] = 0; // expected-warning {{array index of '3' indexes past the end of an array (that contains 3 elements)}} |
| 53 | array2[2+2] = 0; // expected-warning {{array index of '4' indexes past the end of an array (that contains 3 elements)}} |
| 54 | |
| 55 | const char *str1 = "foo"; |
| 56 | char c1 = str1[5]; // no warning for pointers |
| 57 | |
| 58 | const char str2[] = "foo"; // expected-note {{declared here}} |
| 59 | char c2 = str2[5]; // expected-warning {{array index of '5' indexes past the end of an array (that contains 4 elements)}} |
| 60 | |
| 61 | int (*array_ptr)[1]; |
| 62 | (*array_ptr)[3] = 1; // expected-warning {{array index of '3' indexes past the end of an array (that contains 1 elements)}} |
| 63 | } |
| 64 | |
| 65 | template <int I> struct S { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 66 | char arr[I]; // expected-note 2 {{declared here}} |
Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 67 | }; |
| 68 | template <int I> void f() { |
| 69 | S<3> s; |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 70 | 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] | 71 | s.arr[I] = 0; // expected-warning {{array index of '5' indexes past the end of an array (that contains 3 elements)}} |
| 72 | } |
| 73 | |
| 74 | void test_templates() { |
| 75 | f<5>(); // expected-note {{in instantiation}} |
| 76 | } |
Ted Kremenek | a85f528 | 2011-02-17 21:40:51 +0000 | [diff] [blame] | 77 | |
| 78 | #define SIZE 10 |
| 79 | #define ARR_IN_MACRO(flag, arr, idx) flag ? arr[idx] : 1 |
| 80 | |
| 81 | int test_no_warn_macro_unreachable() { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 82 | int arr[SIZE]; // expected-note {{array 'arr' declared here}} |
| 83 | return ARR_IN_MACRO(0, arr, SIZE) + // no-warning |
Ted Kremenek | a85f528 | 2011-02-17 21:40:51 +0000 | [diff] [blame] | 84 | ARR_IN_MACRO(1, arr, SIZE); // expected-warning{{array index of '10' indexes past the end of an array (that contains 10 elements)}} |
| 85 | } |
| 86 | |
Ted Kremenek | 25b3b84 | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 87 | // This exhibited an assertion failure for a 32-bit build of Clang. |
| 88 | int test_pr9240() { |
| 89 | short array[100]; // expected-note {{array 'array' declared here}} |
| 90 | return array[(unsigned long long) 100]; // expected-warning {{array index of '100' indexes past the end of an array (that contains 100 elements)}} |
| 91 | } |
| 92 | |
Ted Kremenek | 3bcc2be | 2011-02-23 01:52:07 +0000 | [diff] [blame] | 93 | // PR 9284 - a template parameter can cause an array bounds access to be |
| 94 | // infeasible. |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 95 | template <bool extendArray> |
Ted Kremenek | 3bcc2be | 2011-02-23 01:52:07 +0000 | [diff] [blame] | 96 | void pr9284() { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 97 | int arr[3 + (extendArray ? 1 : 0)]; |
| 98 | |
| 99 | if (extendArray) |
Ted Kremenek | 3bcc2be | 2011-02-23 01:52:07 +0000 | [diff] [blame] | 100 | arr[3] = 42; // no-warning |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Ted Kremenek | 3bcc2be | 2011-02-23 01:52:07 +0000 | [diff] [blame] | 103 | template <bool extendArray> |
| 104 | void pr9284b() { |
| 105 | int arr[3 + (extendArray ? 1 : 0)]; // expected-note {{array 'arr' declared here}} |
| 106 | |
| 107 | if (!extendArray) |
| 108 | arr[3] = 42; // expected-warning{{array index of '3' indexes past the end of an array (that contains 3 elements)}} |
| 109 | } |
| 110 | |
| 111 | void test_pr9284() { |
| 112 | pr9284<true>(); |
| 113 | pr9284<false>(); |
| 114 | pr9284b<true>(); |
| 115 | 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] | 116 | } |
| 117 | |
Ted Kremenek | 9e060ca | 2011-02-23 23:06:04 +0000 | [diff] [blame] | 118 | int test_pr9296() { |
| 119 | int array[2]; |
| 120 | return array[true]; // no-warning |
| 121 | } |
| 122 | |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 123 | int test_sizeof_as_condition(int flag) { |
| 124 | int arr[2] = { 0, 0 }; // expected-note {{array 'arr' declared here}} |
| 125 | if (flag) |
| 126 | return sizeof(char) != sizeof(char) ? arr[2] : arr[1]; |
| 127 | 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)}} |
| 128 | } |
| 129 | |
Ted Kremenek | e71f3d5 | 2011-03-01 23:12:55 +0000 | [diff] [blame^] | 130 | void test_switch() { |
| 131 | switch (4) { |
| 132 | case 1: { |
| 133 | int arr[2]; |
| 134 | arr[2] = 1; // no-warning |
| 135 | break; |
| 136 | } |
| 137 | case 4: { |
| 138 | int arr[2]; // expected-note {{array 'arr' declared here}} |
| 139 | arr[2] = 1; // expected-warning {{array index of '2' indexes past the end of an array (that contains 2 elements)}} |
| 140 | break; |
| 141 | } |
| 142 | default: { |
| 143 | int arr[2]; |
| 144 | arr[2] = 1; // no-warning |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | } |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 149 | |