1 // RUN: %clang_cc1 -verify -std=c++14 %s
4 int x
[2]; // expected-note 4 {{array 'x' declared here}}
5 int y
[2]; // expected-note 2 {{array 'y' declared here}}
6 int z
[1]; // expected-note {{array 'z' declared here}}
7 int w
[1][1]; // expected-note {{array 'w' declared here}}
8 int v
[1][1][1]; // expected-note {{array 'v' declared here}}
9 int *p
= &y
[2]; // no-warning
10 (void) sizeof(x
[2]); // no-warning
11 y
[2] = 2; // expected-warning {{array index 2 is past the end of the array (that has type 'int[2]')}}
12 z
[1] = 'x'; // expected-warning {{array index 1 is past the end of the array (that has type 'int[1]')}}
13 w
[0][2] = 0; // expected-warning {{array index 2 is past the end of the array (that has type 'int[1]')}}
14 v
[0][0][2] = 0; // expected-warning {{array index 2 is past the end of the array (that has type 'int[1]')}}
15 return x
[2] + // expected-warning {{array index 2 is past the end of the array (that has type 'int[2]')}}
16 y
[-1] + // expected-warning {{array index -1 is before the beginning of the array}}
17 x
[sizeof(x
)] + // expected-warning {{array index 8 is past the end of the array (that has type 'int[2]')}}
18 x
[sizeof(x
) / sizeof(x
[0])] + // expected-warning {{array index 2 is past the end of the array (that has type 'int[2]')}}
19 x
[sizeof(x
) / sizeof(x
[0]) - 1] + // no-warning
20 x
[sizeof(x
[2])]; // expected-warning {{array index 4 is past the end of the array (that has type 'int[2]')}}
23 // This code example tests that -Warray-bounds works with arrays that
24 // are template parameters.
25 template <char *sz
> class Qux
{
26 bool test() { return sz
[0] == 'a'; }
30 int val
= a
[3]; // no warning for function argumnet
33 void f2(const int (&a
)[2]) { // expected-note {{declared here}}
34 int val
= a
[3]; // expected-warning {{array index 3 is past the end of the array (that has type 'const int[2]')}}
41 s2
.a
[3] = 0; // no warning for 0-sized array
44 short a
[2]; // expected-note 4 {{declared here}}
47 u
.a
[3] = 1; // expected-warning {{array index 3 is past the end of the array (that has type 'short[2]')}}
48 u
.c
[3] = 1; // no warning
49 short *p
= &u
.a
[2]; // no warning
50 p
= &u
.a
[3]; // expected-warning {{array index 3 is past the end of the array (that has type 'short[2]')}}
51 *(&u
.a
[2]) = 1; // expected-warning {{array index 2 is past the end of the array (that has type 'short[2]')}}
52 *(&u
.a
[3]) = 1; // expected-warning {{array index 3 is past the end of the array (that has type 'short[2]')}}
53 *(&u
.c
[3]) = 1; // no warning
55 const int const_subscript
= 3;
56 int array
[2]; // expected-note {{declared here}}
57 array
[const_subscript
] = 0; // expected-warning {{array index 3 is past the end of the array (that has type 'int[2]')}}
60 ptr
[3] = 0; // no warning for pointer references
61 int array2
[] = { 0, 1, 2 }; // expected-note 2 {{declared here}}
63 array2
[3] = 0; // expected-warning {{array index 3 is past the end of the array (that has type 'int[3]')}}
64 array2
[2+2] = 0; // expected-warning {{array index 4 is past the end of the array (that has type 'int[3]')}}
66 const char *str1
= "foo";
67 char c1
= str1
[5]; // no warning for pointers
69 const char str2
[] = "foo"; // expected-note {{declared here}}
70 char c2
= str2
[5]; // expected-warning {{array index 5 is past the end of the array (that has type 'const char[4]')}}
73 (*array_ptr
)[3] = 1; // expected-warning {{array index 3 is past the end of the array (that has type 'int[2]')}}
76 template <int I
> struct S
{
77 char arr
[I
]; // expected-note 3 {{declared here}}
79 template <int I
> void f() {
81 s
.arr
[4] = 0; // expected-warning 2 {{array index 4 is past the end of the array (that has type 'char[3]')}}
82 s
.arr
[I
] = 0; // expected-warning {{array index 5 is past the end of the array (that has type 'char[3]')}}
85 void test_templates() {
86 f
<5>(); // expected-note {{in instantiation}}
90 #define ARR_IN_MACRO(flag, arr, idx) flag ? arr[idx] : 1
92 int test_no_warn_macro_unreachable() {
93 int arr
[SIZE
]; // expected-note {{array 'arr' declared here}}
94 return ARR_IN_MACRO(0, arr
, SIZE
) + // no-warning
95 ARR_IN_MACRO(1, arr
, SIZE
); // expected-warning{{array index 10 is past the end of the array (that has type 'int[10]')}}
98 // This exhibited an assertion failure for a 32-bit build of Clang.
100 short array
[100]; // expected-note {{array 'array' declared here}}
101 return array
[(unsigned long long) 100]; // expected-warning {{array index 100 is past the end of the array (that has type 'short[100]')}}
104 // PR 9284 - a template parameter can cause an array bounds access to be
106 template <bool extendArray
>
108 int arr
[3 + (extendArray
? 1 : 0)];
111 arr
[3] = 42; // no-warning
114 template <bool extendArray
>
116 int arr
[3 + (extendArray
? 1 : 0)]; // expected-note {{array 'arr' declared here}}
119 arr
[3] = 42; // expected-warning{{array index 3 is past the end of the array (that has type 'int[3]')}}
126 pr9284b
<false>(); // expected-note{{in instantiation of function template specialization 'pr9284b<false>' requested here}}
131 return array
[true]; // no-warning
134 int test_sizeof_as_condition(int flag
) {
135 int arr
[2] = { 0, 0 }; // expected-note {{array 'arr' declared here}}
137 return sizeof(char) != sizeof(char) ? arr
[2] : arr
[1];
138 return sizeof(char) == sizeof(char) ? arr
[2] : arr
[1]; // expected-warning {{array index 2 is past the end of the array (that has type 'int[2]')}}
145 arr
[2] = 1; // no-warning
149 int arr
[2]; // expected-note {{array 'arr' declared here}}
150 arr
[2] = 1; // expected-warning {{array index 2 is past the end of the array (that has type 'int[2]')}}
155 arr
[2] = 1; // no-warning
161 // Test nested switch statements.
162 enum enumA
{ enumA_A
, enumA_B
, enumA_C
, enumA_D
, enumA_E
};
163 enum enumB
{ enumB_X
, enumB_Y
, enumB_Z
};
164 static enum enumB myVal
= enumB_X
;
165 void test_nested_switch() {
166 switch (enumA_E
) { // expected-warning {{no case matching constant}}
167 switch (myVal
) { // expected-warning {{enumeration values 'enumB_X' and 'enumB_Z' not handled in switch}}
173 // Test that if all the values of an enum covered, that the 'default' branch
175 enum Values
{ A
, B
, C
, D
};
176 void test_all_enums_covered(enum Values v
) {
184 x
[2] = 0; // no-warning
189 char c1
[1]; // expected-note {{declared here}}
196 char c1
[1]; // expected-note {{declared here}}
201 char bar(struct foo
*F
, baz
*B
) {
202 return F
->c1
[3] + // expected-warning {{array index 3 is past the end of the array (that has type 'char[1]')}}
203 F
->c2
[3] + // no warning, foo could have tail padding allocated.
204 B
->c1
[3] + // expected-warning {{array index 3 is past the end of the array (that has type 'char[1]')}}
205 B
->c2
[3]; // no warning, baz could have tail padding allocated.
209 namespace metaprogramming
{
212 char c
[ONE
]; // expected-note {{array 'c' declared here}}
215 template <int N
> struct bar
{ char c
[N
]; }; // expected-note {{declared here}}
217 char test(foo
*F
, bar
<1> *B
) {
218 return F
->c
[3] + // expected-warning {{array index 3 is past the end of the array (that has type 'char[1]')}}
219 B
->c
[3]; // expected-warning {{array index 3 is past the end of the array (that has type 'char[1]')}}
225 int foo
[5]; // expected-note 5 {{array 'foo' declared here}}
226 bar(foo
[5]); // expected-warning {{array index 5 is past the end of the array (that has type 'int[5]')}}
227 ++foo
[5]; // expected-warning {{array index 5 is past the end of the array (that has type 'int[5]')}}
228 if (foo
[6]) // expected-warning {{array index 6 is past the end of the array (that has type 'int[5]')}}
229 return --foo
[6]; // expected-warning {{array index 6 is past the end of the array (that has type 'int[5]')}}
231 return foo
[5]; // expected-warning {{array index 5 is past the end of the array (that has type 'int[5]')}}
234 void test_pr10771() {
235 double foo
[4096]; // expected-note {{array 'foo' declared here}}
237 ((char*)foo
)[sizeof(foo
) - 1] = '\0'; // no-warning
238 *(((char*)foo
) + sizeof(foo
) - 1) = '\0'; // no-warning
240 ((char*)foo
)[sizeof(foo
)] = '\0'; // expected-warning {{array index 32768 is past the end of the array (that has type 'double[4096]', cast to 'char *')}}
242 // TODO: This should probably warn, too.
243 *(((char*)foo
) + sizeof(foo
)) = '\0'; // no-warning
246 int test_pr11007_aux(const char * restrict
, ...);
248 // Test checking with varargs.
249 void test_pr11007() {
250 double a
[5]; // expected-note {{array 'a' declared here}}
251 test_pr11007_aux("foo", a
[1000]); // expected-warning {{array index 1000 is past the end of the array (that has type 'double[5]')}}
254 void test_rdar10916006(void)
256 int a
[128]; // expected-note {{array 'a' declared here}}
257 a
[(unsigned char)'\xA1'] = 1; // expected-warning {{array index 161 is past the end of the array (that has type 'int[128]')}}
265 void test_struct_array_index() {
266 struct P p
[10]; // expected-note {{array 'p' declared here}}
267 p
[11] = {0, 1}; // expected-warning {{array index 11 is past the end of the array (that has type 'struct P[10]')}}
270 int operator+(const struct P
&s1
, const struct P
&s2
);
271 int test_operator_overload_struct_array_index() {
272 struct P x
[10] = {0}; // expected-note {{array 'x' declared here}}
273 return x
[1] + x
[11]; // expected-warning {{array index 11 is past the end of the array (that has type 'struct P[10]')}}
276 int multi
[2][2][2]; // expected-note 3 {{array 'multi' declared here}}
277 int test_multiarray() {
278 return multi
[2][0][0] + // expected-warning {{array index 2 is past the end of the array (that has type 'int[2][2][2]')}}
279 multi
[0][2][0] + // expected-warning {{array index 2 is past the end of the array (that has type 'int[2][2]')}}
280 multi
[0][0][2]; // expected-warning {{array index 2 is past the end of the array (that has type 'int[2]')}}
286 struct multi_s multi2
[4]; // expected-note {{array 'multi2' declared here}}
287 int test_struct_multiarray() {
288 return multi2
[4].arr
[0]; // expected-warning {{array index 4 is past the end of the array (that has type 'struct multi_s[4]')}}
293 extern S xxx
[2]; // expected-note {{array 'xxx' declared here}}
296 C
&f() { return reinterpret_cast<C
*>(xxx
)[1]; } // no-warning
297 // We have no info on whether this is out-of-bounds.
298 C
&g() { return reinterpret_cast<C
*>(xxx
)[2]; } // no-warning
299 // We can still diagnose this.
300 C
&h() { return reinterpret_cast<C
*>(xxx
)[-1]; } // expected-warning {{array index -1 is before the beginning of the array}}
304 template <typename Ty
> void foo() {
305 Ty buffer
[2]; // expected-note 3{{array 'buffer' declared here}}
306 ((char *)buffer
)[2] = 'A'; // expected-warning 1{{array index 2 is past the end of the array (that has type 'char[2]', cast to 'char *')}}
307 ((char *)buffer
)[-1] = 'A'; // expected-warning 2{{array index -1 is before the beginning of the array}}
311 foo
<char>(); // expected-note 1{{in instantiation of function template specialization}}
312 foo
<int>(); // expected-note 1{{in instantiation of function template specialization}}
316 namespace var_template_array
{
317 template <typename T
> int arr
[2]; // expected-note {{array 'arr<int>' declared here}}
318 template <> int arr
<float>[1]; // expected-note {{array 'arr<float>' declared here}}
321 arr
<int>[1] = 0; // ok
322 arr
<int>[2] = 0; // expected-warning {{array index 2 is past the end of the array (that has type 'int[2]')}}
323 arr
<float>[1] = 0; // expected-warning {{array index 1 is past the end of the array (that has type 'int[1]')}}
325 } // namespace var_template_array