[Heikki Kultala] This patch contains the ABI changes for the TCE target.
[clang.git] / test / SemaCXX / array-bounds.cpp
blob0286c01d85ffddaede0e8ec899cfd81b152159df
1 // RUN: %clang_cc1 -verify %s
3 int foo() {
4 int x[2]; // expected-note 4 {{array 'x' declared here}}
5 int y[2]; // expected-note 2 {{array 'y' declared here}}
6 int *p = &y[2]; // no-warning
7 (void) sizeof(x[2]); // no-warning
8 y[2] = 2; // expected-warning {{array index of '2' indexes past the end of an array (that contains 2 elements)}}
9 return x[2] + // expected-warning {{array index of '2' indexes past the end of an array (that contains 2 elements)}}
10 y[-1] + // expected-warning {{array index of '-1' indexes before the beginning of the array}}
11 x[sizeof(x)] + // expected-warning {{array index of '8' indexes past the end of an array (that contains 2 elements)}}
12 x[sizeof(x) / sizeof(x[0])] + // expected-warning {{array index of '2' indexes past the end of an array (that contains 2 elements)}}
13 x[sizeof(x) / sizeof(x[0]) - 1] + // no-warning
14 x[sizeof(x[2])]; // expected-warning {{array index of '4' indexes past the end of an array (that contains 2 elements)}}
17 // This code example tests that -Warray-bounds works with arrays that
18 // are template parameters.
19 template <char *sz> class Qux {
20 bool test() { return sz[0] == 'a'; }
23 void f1(int a[1]) {
24 int val = a[3]; // no warning for function argumnet
27 void f2(const int (&a)[1]) { // expected-note {{declared here}}
28 int val = a[3]; // expected-warning {{array index of '3' indexes past the end of an array (that contains 1 elements)}}
31 void test() {
32 struct {
33 int a[0];
34 } s2;
35 s2.a[3] = 0; // no warning for 0-sized array
37 union {
38 short a[2]; // expected-note {{declared here}}
39 char c[4];
40 } u;
41 u.a[3] = 1; // expected-warning {{array index of '3' indexes past the end of an array (that contains 2 elements)}}
42 u.c[3] = 1; // no warning
44 const int const_subscript = 3;
45 int array[1]; // expected-note {{declared here}}
46 array[const_subscript] = 0; // expected-warning {{array index of '3' indexes past the end of an array (that contains 1 elements)}}
48 int *ptr;
49 ptr[3] = 0; // no warning for pointer references
50 int array2[] = { 0, 1, 2 }; // expected-note 2 {{declared here}}
52 array2[3] = 0; // expected-warning {{array index of '3' indexes past the end of an array (that contains 3 elements)}}
53 array2[2+2] = 0; // expected-warning {{array index of '4' indexes past the end of an array (that contains 3 elements)}}
55 const char *str1 = "foo";
56 char c1 = str1[5]; // no warning for pointers
58 const char str2[] = "foo"; // expected-note {{declared here}}
59 char c2 = str2[5]; // expected-warning {{array index of '5' indexes past the end of an array (that contains 4 elements)}}
61 int (*array_ptr)[1];
62 (*array_ptr)[3] = 1; // expected-warning {{array index of '3' indexes past the end of an array (that contains 1 elements)}}
65 template <int I> struct S {
66 char arr[I]; // expected-note 3 {{declared here}}
68 template <int I> void f() {
69 S<3> s;
70 s.arr[4] = 0; // expected-warning 2 {{array index of '4' indexes past the end of an array (that contains 3 elements)}}
71 s.arr[I] = 0; // expected-warning {{array index of '5' indexes past the end of an array (that contains 3 elements)}}
74 void test_templates() {
75 f<5>(); // expected-note {{in instantiation}}
78 #define SIZE 10
79 #define ARR_IN_MACRO(flag, arr, idx) flag ? arr[idx] : 1
81 int test_no_warn_macro_unreachable() {
82 int arr[SIZE]; // expected-note 2 {{array 'arr' declared here}}
83 // FIXME: We don't want to warn for the first case.
84 return ARR_IN_MACRO(0, arr, SIZE) + // expected-warning{{array index of '10' indexes past the end of an array (that contains 10 elements)}}
85 ARR_IN_MACRO(1, arr, SIZE); // expected-warning{{array index of '10' indexes past the end of an array (that contains 10 elements)}}
88 // This exhibited an assertion failure for a 32-bit build of Clang.
89 int test_pr9240() {
90 short array[100]; // expected-note {{array 'array' declared here}}
91 return array[(unsigned long long) 100]; // expected-warning {{array index of '100' indexes past the end of an array (that contains 100 elements)}}