blob: 8d0b1e4e30b06070963cdd78a02eafaf7e45aefb [file] [log] [blame]
Ted Kremeneka0125d82011-02-16 01:57:07 +00001// RUN: %clang_cc1 -verify %s
2
3int foo() {
Ted Kremenekbac77372011-02-16 22:08:28 +00004 int x[2]; // expected-note 4 {{array 'x' declared here}}
5 int y[2]; // expected-note 2 {{array 'y' declared here}}
Chandler Carruthc2684342011-08-05 09:10:50 +00006 int z[1]; // expected-note {{array 'z' declared here}}
Matt Beaumont-Gaycfbc5b52011-11-29 19:27:11 +00007 int w[1][1]; // expected-note {{array 'w' declared here}}
8 int v[1][1][1]; // expected-note {{array 'v' declared here}}
Ted Kremeneka0125d82011-02-16 01:57:07 +00009 int *p = &y[2]; // no-warning
10 (void) sizeof(x[2]); // no-warning
Matt Beaumont-Gaya5aa96d2011-11-24 00:27:38 +000011 y[2] = 2; // expected-warning {{array index 2 is past the end of the array (which contains 2 elements)}}
12 z[1] = 'x'; // expected-warning {{array index 1 is past the end of the array (which contains 1 element)}}
Matt Beaumont-Gaycfbc5b52011-11-29 19:27:11 +000013 w[0][2] = 0; // expected-warning {{array index 2 is past the end of the array (which contains 1 element)}}
14 v[0][0][2] = 0; // expected-warning {{array index 2 is past the end of the array (which contains 1 element)}}
Matt Beaumont-Gaya5aa96d2011-11-24 00:27:38 +000015 return x[2] + // expected-warning {{array index 2 is past the end of the array (which contains 2 elements)}}
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 (which contains 2 elements)}}
18 x[sizeof(x) / sizeof(x[0])] + // expected-warning {{array index 2 is past the end of the array (which contains 2 elements)}}
Ted Kremeneka0125d82011-02-16 01:57:07 +000019 x[sizeof(x) / sizeof(x[0]) - 1] + // no-warning
Matt Beaumont-Gaya5aa96d2011-11-24 00:27:38 +000020 x[sizeof(x[2])]; // expected-warning {{array index 4 is past the end of the array (which contains 2 elements)}}
Ted Kremeneka0125d82011-02-16 01:57:07 +000021}
22
Ted Kremenekc71a2c02011-02-16 23:39:09 +000023// This code example tests that -Warray-bounds works with arrays that
24// are template parameters.
25template <char *sz> class Qux {
26 bool test() { return sz[0] == 'a'; }
Chandler Carruth35001ca2011-02-17 21:10:52 +000027};
28
29void f1(int a[1]) {
30 int val = a[3]; // no warning for function argumnet
31}
32
Chris Lattner9e6a1ca2011-08-02 21:44:23 +000033void f2(const int (&a)[2]) { // expected-note {{declared here}}
Matt Beaumont-Gaya5aa96d2011-11-24 00:27:38 +000034 int val = a[3]; // expected-warning {{array index 3 is past the end of the array (which contains 2 elements)}}
Chandler Carruth35001ca2011-02-17 21:10:52 +000035}
36
37void test() {
38 struct {
39 int a[0];
40 } s2;
41 s2.a[3] = 0; // no warning for 0-sized array
42
43 union {
Kaelyn Uhraind6c88652011-08-05 23:18:04 +000044 short a[2]; // expected-note 4 {{declared here}}
Chandler Carruth35001ca2011-02-17 21:10:52 +000045 char c[4];
46 } u;
Matt Beaumont-Gaya5aa96d2011-11-24 00:27:38 +000047 u.a[3] = 1; // expected-warning {{array index 3 is past the end of the array (which contains 2 elements)}}
Chandler Carruth35001ca2011-02-17 21:10:52 +000048 u.c[3] = 1; // no warning
Kaelyn Uhraind6c88652011-08-05 23:18:04 +000049 short *p = &u.a[2]; // no warning
Matt Beaumont-Gaya5aa96d2011-11-24 00:27:38 +000050 p = &u.a[3]; // expected-warning {{array index 3 is past the end of the array (which contains 2 elements)}}
51 *(&u.a[2]) = 1; // expected-warning {{array index 2 is past the end of the array (which contains 2 elements)}}
52 *(&u.a[3]) = 1; // expected-warning {{array index 3 is past the end of the array (which contains 2 elements)}}
Kaelyn Uhraind6c88652011-08-05 23:18:04 +000053 *(&u.c[3]) = 1; // no warning
Chandler Carruth35001ca2011-02-17 21:10:52 +000054
55 const int const_subscript = 3;
Chris Lattner9e6a1ca2011-08-02 21:44:23 +000056 int array[2]; // expected-note {{declared here}}
Matt Beaumont-Gaya5aa96d2011-11-24 00:27:38 +000057 array[const_subscript] = 0; // expected-warning {{array index 3 is past the end of the array (which contains 2 elements)}}
Chandler Carruth35001ca2011-02-17 21:10:52 +000058
59 int *ptr;
60 ptr[3] = 0; // no warning for pointer references
61 int array2[] = { 0, 1, 2 }; // expected-note 2 {{declared here}}
62
Matt Beaumont-Gaya5aa96d2011-11-24 00:27:38 +000063 array2[3] = 0; // expected-warning {{array index 3 is past the end of the array (which contains 3 elements)}}
64 array2[2+2] = 0; // expected-warning {{array index 4 is past the end of the array (which contains 3 elements)}}
Chandler Carruth35001ca2011-02-17 21:10:52 +000065
66 const char *str1 = "foo";
67 char c1 = str1[5]; // no warning for pointers
68
69 const char str2[] = "foo"; // expected-note {{declared here}}
Matt Beaumont-Gaya5aa96d2011-11-24 00:27:38 +000070 char c2 = str2[5]; // expected-warning {{array index 5 is past the end of the array (which contains 4 elements)}}
Chandler Carruth35001ca2011-02-17 21:10:52 +000071
Chris Lattner9e6a1ca2011-08-02 21:44:23 +000072 int (*array_ptr)[2];
Matt Beaumont-Gaya5aa96d2011-11-24 00:27:38 +000073 (*array_ptr)[3] = 1; // expected-warning {{array index 3 is past the end of the array (which contains 2 elements)}}
Chandler Carruth35001ca2011-02-17 21:10:52 +000074}
75
David Blaikie36b7c632012-01-24 04:29:18 +000076// FIXME: we should see the next note only 3 times and the following warning once, not twice
77// since it is independent of the template parameter 'I'.
Chandler Carruth35001ca2011-02-17 21:10:52 +000078template <int I> struct S {
David Blaikie36b7c632012-01-24 04:29:18 +000079 char arr[I]; // expected-note 4 {{declared here}}
Chandler Carruth35001ca2011-02-17 21:10:52 +000080};
81template <int I> void f() {
82 S<3> s;
David Blaikie36b7c632012-01-24 04:29:18 +000083 s.arr[4] = 0; // expected-warning 2 {{array index 4 is past the end of the array (which contains 3 elements)}}
84 s.arr[I] = 0; // expected-warning {{array index 5 is past the end of the array (which contains 3 elements)}} \
85 expected-warning {{array index 3 is past the end of the array (which contains 3 elements)}}
Chandler Carruth35001ca2011-02-17 21:10:52 +000086}
87
88void test_templates() {
89 f<5>(); // expected-note {{in instantiation}}
David Blaikie36b7c632012-01-24 04:29:18 +000090 f<3>(); // expected-note {{in instantiation}}
Chandler Carruth35001ca2011-02-17 21:10:52 +000091}
Ted Kremeneka85f5282011-02-17 21:40:51 +000092
93#define SIZE 10
94#define ARR_IN_MACRO(flag, arr, idx) flag ? arr[idx] : 1
95
96int test_no_warn_macro_unreachable() {
Ted Kremenek351ba912011-02-23 01:52:04 +000097 int arr[SIZE]; // expected-note {{array 'arr' declared here}}
98 return ARR_IN_MACRO(0, arr, SIZE) + // no-warning
Matt Beaumont-Gaya5aa96d2011-11-24 00:27:38 +000099 ARR_IN_MACRO(1, arr, SIZE); // expected-warning{{array index 10 is past the end of the array (which contains 10 elements)}}
Ted Kremeneka85f5282011-02-17 21:40:51 +0000100}
101
Ted Kremenek25b3b842011-02-18 02:27:00 +0000102// This exhibited an assertion failure for a 32-bit build of Clang.
103int test_pr9240() {
104 short array[100]; // expected-note {{array 'array' declared here}}
Matt Beaumont-Gaya5aa96d2011-11-24 00:27:38 +0000105 return array[(unsigned long long) 100]; // expected-warning {{array index 100 is past the end of the array (which contains 100 elements)}}
Ted Kremenek25b3b842011-02-18 02:27:00 +0000106}
107
Ted Kremenek3bcc2be2011-02-23 01:52:07 +0000108// PR 9284 - a template parameter can cause an array bounds access to be
109// infeasible.
Ted Kremenek351ba912011-02-23 01:52:04 +0000110template <bool extendArray>
Ted Kremenek3bcc2be2011-02-23 01:52:07 +0000111void pr9284() {
Ted Kremenek351ba912011-02-23 01:52:04 +0000112 int arr[3 + (extendArray ? 1 : 0)];
113
114 if (extendArray)
Ted Kremenek3bcc2be2011-02-23 01:52:07 +0000115 arr[3] = 42; // no-warning
Ted Kremenek351ba912011-02-23 01:52:04 +0000116}
117
Ted Kremenek3bcc2be2011-02-23 01:52:07 +0000118template <bool extendArray>
119void pr9284b() {
120 int arr[3 + (extendArray ? 1 : 0)]; // expected-note {{array 'arr' declared here}}
121
122 if (!extendArray)
Matt Beaumont-Gaya5aa96d2011-11-24 00:27:38 +0000123 arr[3] = 42; // expected-warning{{array index 3 is past the end of the array (which contains 3 elements)}}
Ted Kremenek3bcc2be2011-02-23 01:52:07 +0000124}
125
126void test_pr9284() {
127 pr9284<true>();
128 pr9284<false>();
129 pr9284b<true>();
130 pr9284b<false>(); // expected-note{{in instantiation of function template specialization 'pr9284b<false>' requested here}}
Ted Kremenek351ba912011-02-23 01:52:04 +0000131}
132
Ted Kremenek9e060ca2011-02-23 23:06:04 +0000133int test_pr9296() {
134 int array[2];
135 return array[true]; // no-warning
136}
137
Ted Kremenek3aea4da2011-03-01 18:41:00 +0000138int test_sizeof_as_condition(int flag) {
139 int arr[2] = { 0, 0 }; // expected-note {{array 'arr' declared here}}
140 if (flag)
141 return sizeof(char) != sizeof(char) ? arr[2] : arr[1];
Matt Beaumont-Gaya5aa96d2011-11-24 00:27:38 +0000142 return sizeof(char) == sizeof(char) ? arr[2] : arr[1]; // expected-warning {{array index 2 is past the end of the array (which contains 2 elements)}}
Ted Kremenek3aea4da2011-03-01 18:41:00 +0000143}
144
Ted Kremeneke71f3d52011-03-01 23:12:55 +0000145void test_switch() {
146 switch (4) {
147 case 1: {
148 int arr[2];
149 arr[2] = 1; // no-warning
150 break;
151 }
152 case 4: {
153 int arr[2]; // expected-note {{array 'arr' declared here}}
Matt Beaumont-Gaya5aa96d2011-11-24 00:27:38 +0000154 arr[2] = 1; // expected-warning {{array index 2 is past the end of the array (which contains 2 elements)}}
Ted Kremeneke71f3d52011-03-01 23:12:55 +0000155 break;
156 }
157 default: {
158 int arr[2];
159 arr[2] = 1; // no-warning
160 break;
161 }
162 }
163}
Ted Kremenek3aea4da2011-03-01 18:41:00 +0000164
Ted Kremenek04982472011-03-04 01:03:41 +0000165// Test nested switch statements.
166enum enumA { enumA_A, enumA_B, enumA_C, enumA_D, enumA_E };
167enum enumB { enumB_X, enumB_Y, enumB_Z };
168static enum enumB myVal = enumB_X;
Kaelyn Uhraind6c88652011-08-05 23:18:04 +0000169void test_nested_switch() {
Ted Kremenek04982472011-03-04 01:03:41 +0000170 switch (enumA_E) { // expected-warning {{no case matching constant}}
171 switch (myVal) { // expected-warning {{enumeration values 'enumB_X' and 'enumB_Z' not handled in switch}}
172 case enumB_Y: ;
173 }
174 }
175}
176
Ted Kremenek432c4782011-03-16 04:32:01 +0000177// Test that if all the values of an enum covered, that the 'default' branch
178// is unreachable.
179enum Values { A, B, C, D };
180void test_all_enums_covered(enum Values v) {
181 int x[2];
182 switch (v) {
183 case A: return;
184 case B: return;
185 case C: return;
186 case D: return;
187 }
188 x[2] = 0; // no-warning
189}
Chris Lattner9e6a1ca2011-08-02 21:44:23 +0000190
191namespace tailpad {
192 struct foo {
Chandler Carruthc2684342011-08-05 09:10:50 +0000193 char c1[1]; // expected-note {{declared here}}
Chris Lattner9e6a1ca2011-08-02 21:44:23 +0000194 int x;
Chandler Carruthc2684342011-08-05 09:10:50 +0000195 char c2[1];
Chris Lattner9e6a1ca2011-08-02 21:44:23 +0000196 };
Matt Beaumont-Gay381711c2011-11-29 22:43:53 +0000197
198 class baz {
199 public:
200 char c1[1]; // expected-note {{declared here}}
201 int x;
202 char c2[1];
203 };
204
205 char bar(struct foo *F, baz *B) {
206 return F->c1[3] + // expected-warning {{array index 3 is past the end of the array (which contains 1 element)}}
207 F->c2[3] + // no warning, foo could have tail padding allocated.
208 B->c1[3] + // expected-warning {{array index 3 is past the end of the array (which contains 1 element)}}
209 B->c2[3]; // no warning, baz could have tail padding allocated.
Chandler Carruthc2684342011-08-05 09:10:50 +0000210 }
211}
212
213namespace metaprogramming {
214#define ONE 1
215 struct foo { char c[ONE]; }; // expected-note {{declared here}}
216 template <int N> struct bar { char c[N]; }; // expected-note {{declared here}}
217
218 char test(foo *F, bar<1> *B) {
Matt Beaumont-Gaya5aa96d2011-11-24 00:27:38 +0000219 return F->c[3] + // expected-warning {{array index 3 is past the end of the array (which contains 1 element)}}
220 B->c[3]; // expected-warning {{array index 3 is past the end of the array (which contains 1 element)}}
Chris Lattner9e6a1ca2011-08-02 21:44:23 +0000221 }
222}
Kaelyn Uhraind6c88652011-08-05 23:18:04 +0000223
224void bar(int x) {}
225int test_more() {
226 int foo[5]; // expected-note 5 {{array 'foo' declared here}}
Matt Beaumont-Gaya5aa96d2011-11-24 00:27:38 +0000227 bar(foo[5]); // expected-warning {{array index 5 is past the end of the array (which contains 5 elements)}}
228 ++foo[5]; // expected-warning {{array index 5 is past the end of the array (which contains 5 elements)}}
229 if (foo[6]) // expected-warning {{array index 6 is past the end of the array (which contains 5 elements)}}
230 return --foo[6]; // expected-warning {{array index 6 is past the end of the array (which contains 5 elements)}}
Kaelyn Uhraind6c88652011-08-05 23:18:04 +0000231 else
Matt Beaumont-Gaya5aa96d2011-11-24 00:27:38 +0000232 return foo[5]; // expected-warning {{array index 5 is past the end of the array (which contains 5 elements)}}
Kaelyn Uhraind6c88652011-08-05 23:18:04 +0000233}
Nico Weberde5998f2011-09-17 22:59:41 +0000234
235void test_pr10771() {
236 double foo[4096]; // expected-note {{array 'foo' declared here}}
237
238 ((char*)foo)[sizeof(foo) - 1] = '\0'; // no-warning
239 *(((char*)foo) + sizeof(foo) - 1) = '\0'; // no-warning
240
Matt Beaumont-Gaya5aa96d2011-11-24 00:27:38 +0000241 ((char*)foo)[sizeof(foo)] = '\0'; // expected-warning {{array index 32768 is past the end of the array (which contains 32768 elements)}}
Nico Weberde5998f2011-09-17 22:59:41 +0000242
243 // TODO: This should probably warn, too.
244 *(((char*)foo) + sizeof(foo)) = '\0'; // no-warning
245}
Ted Kremenek615eb7c2011-09-26 23:36:13 +0000246
247int test_pr11007_aux(const char * restrict, ...);
248
249// Test checking with varargs.
250void test_pr11007() {
251 double a[5]; // expected-note {{array 'a' declared here}}
Matt Beaumont-Gaya5aa96d2011-11-24 00:27:38 +0000252 test_pr11007_aux("foo", a[1000]); // expected-warning {{array index 1000 is past the end of the array}}
Ted Kremenek615eb7c2011-09-26 23:36:13 +0000253}