blob: e8156320db4a3ee34e6e4d4cd89f5ae05cfcc9b1 [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}}
Ted Kremeneka0125d82011-02-16 01:57:07 +00007 int *p = &y[2]; // no-warning
8 (void) sizeof(x[2]); // no-warning
Chandler Carruth35001ca2011-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 Carruthc2684342011-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 Carruth35001ca2011-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 Kremeneka0125d82011-02-16 01:57:07 +000015 x[sizeof(x) / sizeof(x[0]) - 1] + // no-warning
Chandler Carruth35001ca2011-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 Kremeneka0125d82011-02-16 01:57:07 +000017}
18
Ted Kremenekc71a2c02011-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 Carruth35001ca2011-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 Lattner9e6a1ca2011-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 Carruth35001ca2011-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 Uhrain27604552011-07-26 18:36:36 +000040 short a[2]; // expected-note {{declared here}}
Chandler Carruth35001ca2011-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
45
46 const int const_subscript = 3;
Chris Lattner9e6a1ca2011-08-02 21:44:23 +000047 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 Carruth35001ca2011-02-17 21:10:52 +000049
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 Lattner9e6a1ca2011-08-02 21:44:23 +000063 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 Carruth35001ca2011-02-17 21:10:52 +000065}
66
67template <int I> struct S {
Ted Kremenek351ba912011-02-23 01:52:04 +000068 char arr[I]; // expected-note 2 {{declared here}}
Chandler Carruth35001ca2011-02-17 21:10:52 +000069};
70template <int I> void f() {
71 S<3> s;
Ted Kremenek351ba912011-02-23 01:52:04 +000072 s.arr[4] = 0; // expected-warning {{array index of '4' indexes past the end of an array (that contains 3 elements)}}
Chandler Carruth35001ca2011-02-17 21:10:52 +000073 s.arr[I] = 0; // expected-warning {{array index of '5' indexes past the end of an array (that contains 3 elements)}}
74}
75
76void test_templates() {
77 f<5>(); // expected-note {{in instantiation}}
78}
Ted Kremeneka85f5282011-02-17 21:40:51 +000079
80#define SIZE 10
81#define ARR_IN_MACRO(flag, arr, idx) flag ? arr[idx] : 1
82
83int test_no_warn_macro_unreachable() {
Ted Kremenek351ba912011-02-23 01:52:04 +000084 int arr[SIZE]; // expected-note {{array 'arr' declared here}}
85 return ARR_IN_MACRO(0, arr, SIZE) + // no-warning
Ted Kremeneka85f5282011-02-17 21:40:51 +000086 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 Kremenek25b3b842011-02-18 02:27:00 +000089// This exhibited an assertion failure for a 32-bit build of Clang.
90int 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 Kremenek3bcc2be2011-02-23 01:52:07 +000095// PR 9284 - a template parameter can cause an array bounds access to be
96// infeasible.
Ted Kremenek351ba912011-02-23 01:52:04 +000097template <bool extendArray>
Ted Kremenek3bcc2be2011-02-23 01:52:07 +000098void pr9284() {
Ted Kremenek351ba912011-02-23 01:52:04 +000099 int arr[3 + (extendArray ? 1 : 0)];
100
101 if (extendArray)
Ted Kremenek3bcc2be2011-02-23 01:52:07 +0000102 arr[3] = 42; // no-warning
Ted Kremenek351ba912011-02-23 01:52:04 +0000103}
104
Ted Kremenek3bcc2be2011-02-23 01:52:07 +0000105template <bool extendArray>
106void 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
113void 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 Kremenek351ba912011-02-23 01:52:04 +0000118}
119
Ted Kremenek9e060ca2011-02-23 23:06:04 +0000120int test_pr9296() {
121 int array[2];
122 return array[true]; // no-warning
123}
124
Ted Kremenek3aea4da2011-03-01 18:41:00 +0000125int 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 Kremeneke71f3d52011-03-01 23:12:55 +0000132void 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 Kremenek3aea4da2011-03-01 18:41:00 +0000151
Ted Kremenek04982472011-03-04 01:03:41 +0000152// Test nested switch statements.
153enum enumA { enumA_A, enumA_B, enumA_C, enumA_D, enumA_E };
154enum enumB { enumB_X, enumB_Y, enumB_Z };
155static enum enumB myVal = enumB_X;
Kaelyn Uhrain27604552011-07-26 18:36:36 +0000156void test_nested_switch()
157{
Ted Kremenek04982472011-03-04 01:03:41 +0000158 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 Kremenek432c4782011-03-16 04:32:01 +0000165// Test that if all the values of an enum covered, that the 'default' branch
166// is unreachable.
167enum Values { A, B, C, D };
168void 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 Lattner9e6a1ca2011-08-02 21:44:23 +0000178
179namespace tailpad {
180 struct foo {
Chandler Carruthc2684342011-08-05 09:10:50 +0000181 char c1[1]; // expected-note {{declared here}}
Chris Lattner9e6a1ca2011-08-02 21:44:23 +0000182 int x;
Chandler Carruthc2684342011-08-05 09:10:50 +0000183 char c2[1];
Chris Lattner9e6a1ca2011-08-02 21:44:23 +0000184 };
185
186 char bar(struct foo *F) {
Chandler Carruthc2684342011-08-05 09:10:50 +0000187 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
192namespace 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 Lattner9e6a1ca2011-08-02 21:44:23 +0000200 }
201}