blob: 2bbb4776ada8b3162f276d827d94ed5902bd3015 [file] [log] [blame]
Ted Kremenek64699be2011-02-16 01:57:07 +00001// RUN: %clang_cc1 -verify %s
2
3int foo() {
Ted Kremenek3a854f82011-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 Carruth28389f02011-08-05 09:10:50 +00006 int z[1]; // expected-note {{array 'z' declared here}}
Ted Kremenek64699be2011-02-16 01:57:07 +00007 int *p = &y[2]; // no-warning
8 (void) sizeof(x[2]); // no-warning
Chandler Carruth1af88f12011-02-17 21:10:52 +00009 y[2] = 2; // expected-warning {{array index of '2' indexes past the end of an array (that contains 2 elements)}}
Chandler Carruth28389f02011-08-05 09:10:50 +000010 z[1] = 'x'; // expected-warning {{array index of '1' indexes past the end of an array (that contains 1 element)}}
Chandler Carruth1af88f12011-02-17 21:10:52 +000011 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 Kremenek64699be2011-02-16 01:57:07 +000015 x[sizeof(x) / sizeof(x[0]) - 1] + // no-warning
Chandler Carruth1af88f12011-02-17 21:10:52 +000016 x[sizeof(x[2])]; // expected-warning {{array index of '4' indexes past the end of an array (that contains 2 elements)}}
Ted Kremenek64699be2011-02-16 01:57:07 +000017}
18
Ted Kremenek197fcd42011-02-16 23:39:09 +000019// This code example tests that -Warray-bounds works with arrays that
20// are template parameters.
21template <char *sz> class Qux {
22 bool test() { return sz[0] == 'a'; }
Chandler Carruth1af88f12011-02-17 21:10:52 +000023};
24
25void f1(int a[1]) {
26 int val = a[3]; // no warning for function argumnet
27}
28
Chris Lattnerf51dae02011-08-02 21:44:23 +000029void 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 Carruth1af88f12011-02-17 21:10:52 +000031}
32
33void test() {
34 struct {
35 int a[0];
36 } s2;
37 s2.a[3] = 0; // no warning for 0-sized array
38
39 union {
Kaelyn Uhrain2e7aa5a2011-08-05 23:18:04 +000040 short a[2]; // expected-note 4 {{declared here}}
Chandler Carruth1af88f12011-02-17 21:10:52 +000041 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 Uhrain2e7aa5a2011-08-05 23:18:04 +000045 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 Carruth1af88f12011-02-17 21:10:52 +000050
51 const int const_subscript = 3;
Chris Lattnerf51dae02011-08-02 21:44:23 +000052 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 Carruth1af88f12011-02-17 21:10:52 +000054
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 Lattnerf51dae02011-08-02 21:44:23 +000068 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 Carruth1af88f12011-02-17 21:10:52 +000070}
71
72template <int I> struct S {
Ted Kremenek3427fac2011-02-23 01:52:04 +000073 char arr[I]; // expected-note 2 {{declared here}}
Chandler Carruth1af88f12011-02-17 21:10:52 +000074};
75template <int I> void f() {
76 S<3> s;
Ted Kremenek3427fac2011-02-23 01:52:04 +000077 s.arr[4] = 0; // expected-warning {{array index of '4' indexes past the end of an array (that contains 3 elements)}}
Chandler Carruth1af88f12011-02-17 21:10:52 +000078 s.arr[I] = 0; // expected-warning {{array index of '5' indexes past the end of an array (that contains 3 elements)}}
79}
80
81void test_templates() {
82 f<5>(); // expected-note {{in instantiation}}
83}
Ted Kremenek48d96262011-02-17 21:40:51 +000084
85#define SIZE 10
86#define ARR_IN_MACRO(flag, arr, idx) flag ? arr[idx] : 1
87
88int test_no_warn_macro_unreachable() {
Ted Kremenek3427fac2011-02-23 01:52:04 +000089 int arr[SIZE]; // expected-note {{array 'arr' declared here}}
90 return ARR_IN_MACRO(0, arr, SIZE) + // no-warning
Ted Kremenek48d96262011-02-17 21:40:51 +000091 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 Kremeneka7ced2c2011-02-18 02:27:00 +000094// This exhibited an assertion failure for a 32-bit build of Clang.
95int 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 Kremenek477c8f52011-02-23 01:52:07 +0000100// PR 9284 - a template parameter can cause an array bounds access to be
101// infeasible.
Ted Kremenek3427fac2011-02-23 01:52:04 +0000102template <bool extendArray>
Ted Kremenek477c8f52011-02-23 01:52:07 +0000103void pr9284() {
Ted Kremenek3427fac2011-02-23 01:52:04 +0000104 int arr[3 + (extendArray ? 1 : 0)];
105
106 if (extendArray)
Ted Kremenek477c8f52011-02-23 01:52:07 +0000107 arr[3] = 42; // no-warning
Ted Kremenek3427fac2011-02-23 01:52:04 +0000108}
109
Ted Kremenek477c8f52011-02-23 01:52:07 +0000110template <bool extendArray>
111void 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
118void 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 Kremenek3427fac2011-02-23 01:52:04 +0000123}
124
Ted Kremeneke4b316c2011-02-23 23:06:04 +0000125int test_pr9296() {
126 int array[2];
127 return array[true]; // no-warning
128}
129
Ted Kremenekdf26df72011-03-01 18:41:00 +0000130int 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 Kremenekeff9a7f2011-03-01 23:12:55 +0000137void 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 Kremenekdf26df72011-03-01 18:41:00 +0000156
Ted Kremenekbe528712011-03-04 01:03:41 +0000157// Test nested switch statements.
158enum enumA { enumA_A, enumA_B, enumA_C, enumA_D, enumA_E };
159enum enumB { enumB_X, enumB_Y, enumB_Z };
160static enum enumB myVal = enumB_X;
Kaelyn Uhrain2e7aa5a2011-08-05 23:18:04 +0000161void test_nested_switch() {
Ted Kremenekbe528712011-03-04 01:03:41 +0000162 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 Kremenek35c70f62011-03-16 04:32:01 +0000169// Test that if all the values of an enum covered, that the 'default' branch
170// is unreachable.
171enum Values { A, B, C, D };
172void 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 Lattnerf51dae02011-08-02 21:44:23 +0000182
183namespace tailpad {
184 struct foo {
Chandler Carruth28389f02011-08-05 09:10:50 +0000185 char c1[1]; // expected-note {{declared here}}
Chris Lattnerf51dae02011-08-02 21:44:23 +0000186 int x;
Chandler Carruth28389f02011-08-05 09:10:50 +0000187 char c2[1];
Chris Lattnerf51dae02011-08-02 21:44:23 +0000188 };
189
190 char bar(struct foo *F) {
Chandler Carruth28389f02011-08-05 09:10:50 +0000191 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
196namespace 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 Lattnerf51dae02011-08-02 21:44:23 +0000204 }
205}
Kaelyn Uhrain2e7aa5a2011-08-05 23:18:04 +0000206
207void bar(int x) {}
208int 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}