blob: 5fd7e3749f7d1f5181fc166ac733a1312a7faad6 [file] [log] [blame]
Dmitri Gribenko6c926cc2013-01-23 20:02:51 +00001// RUN: %clang_cc1 -fsyntax-only -verify -Wvla-extension %s
Douglas Gregor959d5a02010-05-22 16:17:30 +00002struct NonPOD {
3 NonPOD();
4};
5
6struct NonPOD2 {
7 NonPOD np;
8};
9
10struct POD {
11 int x;
12 int y;
13};
14
15// We allow VLAs of POD types, only.
16void vla(int N) {
Richard Smithe4345902011-12-29 21:57:33 +000017 int array1[N]; // expected-warning{{variable length arrays are a C99 feature}}
18 POD array2[N]; // expected-warning{{variable length arrays are a C99 feature}}
Alexey Bataeve7545b32016-04-29 09:39:50 +000019 NonPOD array3[N]; // expected-warning{{variable length arrays are a C99 feature}}
20 NonPOD2 array4[N][3]; // expected-warning{{variable length arrays are a C99 feature}}
Douglas Gregor959d5a02010-05-22 16:17:30 +000021}
22
Douglas Gregora09387d2010-05-23 19:57:01 +000023/// Warn about VLAs in templates.
Douglas Gregor959d5a02010-05-22 16:17:30 +000024template<typename T>
25void vla_in_template(int N, T t) {
Richard Smithe4345902011-12-29 21:57:33 +000026 int array1[N]; // expected-warning{{variable length arrays are a C99 feature}}
Douglas Gregor959d5a02010-05-22 16:17:30 +000027}
28
29struct HasConstantValue {
30 static const unsigned int value = 2;
31};
32
33struct HasNonConstantValue {
34 static unsigned int value;
35};
36
37template<typename T>
38void vla_in_template(T t) {
Richard Smithe4345902011-12-29 21:57:33 +000039 int array2[T::value]; // expected-warning{{variable length arrays are a C99 feature}}
Douglas Gregor959d5a02010-05-22 16:17:30 +000040}
41
42template void vla_in_template<HasConstantValue>(HasConstantValue);
43template void vla_in_template<HasNonConstantValue>(HasNonConstantValue); // expected-note{{instantiation of}}
44
45template<typename T> struct X0 { };
46
47// Cannot use any variably-modified type with a template parameter or
48// argument.
49void inst_with_vla(int N) {
Richard Smithe4345902011-12-29 21:57:33 +000050 int array[N]; // expected-warning{{variable length arrays are a C99 feature}}
Douglas Gregor959d5a02010-05-22 16:17:30 +000051 X0<__typeof__(array)> x0a; // expected-error{{variably modified type 'typeof (array)' (aka 'int [N]') cannot be used as a template argument}}
52}
53
54template<typename T>
55struct X1 {
Douglas Gregora09387d2010-05-23 19:57:01 +000056 template<int (&Array)[T::value]> // expected-error{{non-type template parameter of variably modified type 'int (&)[HasNonConstantValue::value]'}} \
Richard Smithe4345902011-12-29 21:57:33 +000057 // expected-warning{{variable length arrays are a C99 feature}}
Douglas Gregor959d5a02010-05-22 16:17:30 +000058 struct Inner {
59
60 };
61};
62
63X1<HasConstantValue> x1a;
64X1<HasNonConstantValue> x1b; // expected-note{{in instantiation of}}
65
66// Template argument deduction does not allow deducing a size from a VLA.
Richard Smith44ecdbd2013-01-31 05:19:49 +000067// FIXME: This diagnostic should make it clear that the two 'N's are different entities!
Douglas Gregor959d5a02010-05-22 16:17:30 +000068template<typename T, unsigned N>
Richard Smith44ecdbd2013-01-31 05:19:49 +000069void accept_array(T (&array)[N]); // expected-note{{candidate template ignored: could not match 'T [N]' against 'int [N]'}}
Douglas Gregor959d5a02010-05-22 16:17:30 +000070
71void test_accept_array(int N) {
Richard Smithe4345902011-12-29 21:57:33 +000072 int array[N]; // expected-warning{{variable length arrays are a C99 feature}}
Douglas Gregor959d5a02010-05-22 16:17:30 +000073 accept_array(array); // expected-error{{no matching function for call to 'accept_array'}}
74}
75
76// Variably-modified types cannot be used in local classes.
Eli Friedmandd053f62012-02-07 00:15:00 +000077void local_classes(int N) { // expected-note {{declared here}}
Douglas Gregor959d5a02010-05-22 16:17:30 +000078 struct X {
79 int size;
Douglas Gregor4b636a72010-05-23 16:51:27 +000080 int array[N]; // expected-error{{fields must have a constant size: 'variable length array in structure' extension will never be supported}} \
Eli Friedmandd053f62012-02-07 00:15:00 +000081 // expected-error{{reference to local variable 'N' declared in enclosing function 'local_classes'}} \
Richard Smithe4345902011-12-29 21:57:33 +000082 // expected-warning{{variable length arrays are a C99 feature}}
Douglas Gregor959d5a02010-05-22 16:17:30 +000083 };
84}
Douglas Gregor5e8c8c02010-05-23 16:10:32 +000085
86namespace PR7206 {
87 void f(int x) {
88 struct edge_info {
89 float left;
90 float right;
91 };
Richard Smithe4345902011-12-29 21:57:33 +000092 struct edge_info edgeInfo[x]; // expected-warning{{variable length arrays are a C99 feature}}
Douglas Gregor5e8c8c02010-05-23 16:10:32 +000093 }
94}
Douglas Gregor5a5073e2010-05-24 17:22:01 +000095
96namespace rdar8020206 {
97 template<typename T>
98 void f(int i) {
99 const unsigned value = i;
Richard Smithe4345902011-12-29 21:57:33 +0000100 int array[value * i]; // expected-warning 2{{variable length arrays are a C99 feature}}
Douglas Gregor5a5073e2010-05-24 17:22:01 +0000101 }
102
103 template void f<int>(int); // expected-note{{instantiation of}}
104}
Douglas Gregor9a414452010-05-24 20:42:30 +0000105
106namespace rdar8021385 {
107 typedef int my_int;
108 struct A { typedef int my_int; };
109 template<typename T>
110 struct B {
111 typedef typename T::my_int my_int;
112 void f0() {
113 int M = 4;
Richard Smithe4345902011-12-29 21:57:33 +0000114 my_int a[M]; // expected-warning{{variable length arrays are a C99 feature}}
Douglas Gregor9a414452010-05-24 20:42:30 +0000115 }
116 };
117 B<A> a;
118}
Douglas Gregor3999e152010-10-06 16:00:31 +0000119
120namespace PR8209 {
121 void f(int n) {
Richard Smithe4345902011-12-29 21:57:33 +0000122 typedef int vla_type[n]; // expected-warning{{variable length arrays are a C99 feature}}
Douglas Gregor3999e152010-10-06 16:00:31 +0000123 (void)new vla_type; // expected-error{{variably}}
124 }
125}
Chris Lattnerf35de482011-06-14 06:38:10 +0000126
127namespace rdar8733881 { // rdar://8733881
128
129static const int k_cVal3 = (int)(1000*0.2f);
130 int f() {
131 // Ok, fold to a constant size array as an extension.
132 char rgch[k_cVal3] = {0};
133 }
134}
Eli Friedmanf7f102f2012-01-25 22:19:07 +0000135
136namespace PR11744 {
137 template<typename T> int f(int n) {
138 T arr[3][n]; // expected-warning 3 {{variable length arrays are a C99 feature}}
139 return 3;
140 }
141 int test = f<int>(0); // expected-note {{instantiation of}}
142}
Serge Pavlov774c6d02014-02-06 03:49:11 +0000143
144namespace pr18633 {
145 struct A1 {
146 static const int sz;
147 static const int sz2;
148 };
149 const int A1::sz2 = 11;
150 template<typename T>
151 void func () {
152 int arr[A1::sz]; // expected-warning{{variable length arrays are a C99 feature}}
153 }
154 template<typename T>
155 void func2 () {
156 int arr[A1::sz2];
157 }
158 const int A1::sz = 12;
159 void func2() {
160 func<int>();
161 func2<int>();
162 }
163}