summaryrefslogtreecommitdiffstats
path: root/test/SemaCXX/array-bounds.cpp
diff options
context:
space:
mode:
authordim <dim@FreeBSD.org>2011-10-20 21:14:49 +0000
committerdim <dim@FreeBSD.org>2011-10-20 21:14:49 +0000
commit3963a48221351c61c17fb3f382341ab04809a3d3 (patch)
treeee2483e98b09cac943dc93a6969d83ca737ff139 /test/SemaCXX/array-bounds.cpp
parent611ba3ea3300b71eb95dc4e45f20eee5dddd32e1 (diff)
downloadFreeBSD-src-3963a48221351c61c17fb3f382341ab04809a3d3.zip
FreeBSD-src-3963a48221351c61c17fb3f382341ab04809a3d3.tar.gz
Vendor import of clang release_30 branch r142614:
http://llvm.org/svn/llvm-project/cfe/branches/release_30@142614
Diffstat (limited to 'test/SemaCXX/array-bounds.cpp')
-rw-r--r--test/SemaCXX/array-bounds.cpp80
1 files changed, 71 insertions, 9 deletions
diff --git a/test/SemaCXX/array-bounds.cpp b/test/SemaCXX/array-bounds.cpp
index 3bd6c354..555ac33 100644
--- a/test/SemaCXX/array-bounds.cpp
+++ b/test/SemaCXX/array-bounds.cpp
@@ -3,9 +3,11 @@
int foo() {
int x[2]; // expected-note 4 {{array 'x' declared here}}
int y[2]; // expected-note 2 {{array 'y' declared here}}
+ int z[1]; // expected-note {{array 'z' declared here}}
int *p = &y[2]; // no-warning
(void) sizeof(x[2]); // no-warning
y[2] = 2; // expected-warning {{array index of '2' indexes past the end of an array (that contains 2 elements)}}
+ z[1] = 'x'; // expected-warning {{array index of '1' indexes past the end of an array (that contains 1 element)}}
return x[2] + // expected-warning {{array index of '2' indexes past the end of an array (that contains 2 elements)}}
y[-1] + // expected-warning {{array index of '-1' indexes before the beginning of the array}}
x[sizeof(x)] + // expected-warning {{array index of '8' indexes past the end of an array (that contains 2 elements)}}
@@ -24,8 +26,8 @@ void f1(int a[1]) {
int val = a[3]; // no warning for function argumnet
}
-void f2(const int (&a)[1]) { // expected-note {{declared here}}
- int val = a[3]; // expected-warning {{array index of '3' indexes past the end of an array (that contains 1 elements)}}
+void f2(const int (&a)[2]) { // expected-note {{declared here}}
+ int val = a[3]; // expected-warning {{array index of '3' indexes past the end of an array (that contains 2 elements)}}
}
void test() {
@@ -35,15 +37,20 @@ void test() {
s2.a[3] = 0; // no warning for 0-sized array
union {
- short a[2]; // expected-note {{declared here}}
+ short a[2]; // expected-note 4 {{declared here}}
char c[4];
} u;
u.a[3] = 1; // expected-warning {{array index of '3' indexes past the end of an array (that contains 2 elements)}}
u.c[3] = 1; // no warning
+ short *p = &u.a[2]; // no warning
+ p = &u.a[3]; // expected-warning {{array index of '3' indexes past the end of an array (that contains 2 elements)}}
+ *(&u.a[2]) = 1; // expected-warning {{array index of '2' indexes past the end of an array (that contains 2 elements)}}
+ *(&u.a[3]) = 1; // expected-warning {{array index of '3' indexes past the end of an array (that contains 2 elements)}}
+ *(&u.c[3]) = 1; // no warning
const int const_subscript = 3;
- int array[1]; // expected-note {{declared here}}
- array[const_subscript] = 0; // expected-warning {{array index of '3' indexes past the end of an array (that contains 1 elements)}}
+ int array[2]; // expected-note {{declared here}}
+ array[const_subscript] = 0; // expected-warning {{array index of '3' indexes past the end of an array (that contains 2 elements)}}
int *ptr;
ptr[3] = 0; // no warning for pointer references
@@ -58,8 +65,8 @@ void test() {
const char str2[] = "foo"; // expected-note {{declared here}}
char c2 = str2[5]; // expected-warning {{array index of '5' indexes past the end of an array (that contains 4 elements)}}
- int (*array_ptr)[1];
- (*array_ptr)[3] = 1; // expected-warning {{array index of '3' indexes past the end of an array (that contains 1 elements)}}
+ int (*array_ptr)[2];
+ (*array_ptr)[3] = 1; // expected-warning {{array index of '3' indexes past the end of an array (that contains 2 elements)}}
}
template <int I> struct S {
@@ -151,8 +158,7 @@ void test_switch() {
enum enumA { enumA_A, enumA_B, enumA_C, enumA_D, enumA_E };
enum enumB { enumB_X, enumB_Y, enumB_Z };
static enum enumB myVal = enumB_X;
-void test_nested_switch()
-{
+void test_nested_switch() {
switch (enumA_E) { // expected-warning {{no case matching constant}}
switch (myVal) { // expected-warning {{enumeration values 'enumB_X' and 'enumB_Z' not handled in switch}}
case enumB_Y: ;
@@ -173,3 +179,59 @@ void test_all_enums_covered(enum Values v) {
}
x[2] = 0; // no-warning
}
+
+namespace tailpad {
+ struct foo {
+ char c1[1]; // expected-note {{declared here}}
+ int x;
+ char c2[1];
+ };
+
+ char bar(struct foo *F) {
+ return F->c1[3]; // expected-warning {{array index of '3' indexes past the end of an array (that contains 1 element)}}
+ return F->c2[3]; // no warning, foo could have tail padding allocated.
+ }
+}
+
+namespace metaprogramming {
+#define ONE 1
+ struct foo { char c[ONE]; }; // expected-note {{declared here}}
+ template <int N> struct bar { char c[N]; }; // expected-note {{declared here}}
+
+ char test(foo *F, bar<1> *B) {
+ return F->c[3] + // expected-warning {{array index of '3' indexes past the end of an array (that contains 1 element)}}
+ B->c[3]; // expected-warning {{array index of '3' indexes past the end of an array (that contains 1 element)}}
+ }
+}
+
+void bar(int x) {}
+int test_more() {
+ int foo[5]; // expected-note 5 {{array 'foo' declared here}}
+ bar(foo[5]); // expected-warning {{array index of '5' indexes past the end of an array (that contains 5 elements)}}
+ ++foo[5]; // expected-warning {{array index of '5' indexes past the end of an array (that contains 5 elements)}}
+ if (foo[6]) // expected-warning {{array index of '6' indexes past the end of an array (that contains 5 elements)}}
+ return --foo[6]; // expected-warning {{array index of '6' indexes past the end of an array (that contains 5 elements)}}
+ else
+ return foo[5]; // expected-warning {{array index of '5' indexes past the end of an array (that contains 5 elements)}}
+}
+
+void test_pr10771() {
+ double foo[4096]; // expected-note {{array 'foo' declared here}}
+
+ ((char*)foo)[sizeof(foo) - 1] = '\0'; // no-warning
+ *(((char*)foo) + sizeof(foo) - 1) = '\0'; // no-warning
+
+ ((char*)foo)[sizeof(foo)] = '\0'; // expected-warning {{array index of '32768' indexes past the end of an array (that contains 32768 elements)}}
+
+ // TODO: This should probably warn, too.
+ *(((char*)foo) + sizeof(foo)) = '\0'; // no-warning
+}
+
+int test_pr11007_aux(const char * restrict, ...);
+
+// Test checking with varargs.
+void test_pr11007() {
+ double a[5]; // expected-note {{array 'a' declared here}}
+ test_pr11007_aux("foo", a[1000]); // expected-warning {{array index of '1000' indexes past the end of an array}}
+}
+
OpenPOWER on IntegriCloud