blob: 7773c0849b4af606655771f57da3b6105100a062 [file] [log] [blame]
Douglas Gregordb4da822010-05-23 16:51:27 +00001// RUN: %clang_cc1 -fsyntax-only -verify -Wvla %s
Douglas Gregor0fddb972010-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 Smithd7c56e12011-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}}
Douglas Gregor0fddb972010-05-22 16:17:30 +000019 NonPOD array3[N]; // expected-error{{variable length array of non-POD element type 'NonPOD'}}
20 NonPOD2 array4[N][3]; // expected-error{{variable length array of non-POD element type 'NonPOD2'}}
21}
22
Douglas Gregora481ec42010-05-23 19:57:01 +000023/// Warn about VLAs in templates.
Douglas Gregor0fddb972010-05-22 16:17:30 +000024template<typename T>
25void vla_in_template(int N, T t) {
Richard Smithd7c56e12011-12-29 21:57:33 +000026 int array1[N]; // expected-warning{{variable length arrays are a C99 feature}}
Douglas Gregor0fddb972010-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 Smithd7c56e12011-12-29 21:57:33 +000039 int array2[T::value]; // expected-warning{{variable length arrays are a C99 feature}}
Douglas Gregor0fddb972010-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 Smithd7c56e12011-12-29 21:57:33 +000050 int array[N]; // expected-warning{{variable length arrays are a C99 feature}}
Douglas Gregor0fddb972010-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 Gregora481ec42010-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 Smithd7c56e12011-12-29 21:57:33 +000057 // expected-warning{{variable length arrays are a C99 feature}}
Douglas Gregor0fddb972010-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.
67template<typename T, unsigned N>
68void accept_array(T (&array)[N]); // expected-note{{candidate template ignored: failed template argument deduction}}
69
70void test_accept_array(int N) {
Richard Smithd7c56e12011-12-29 21:57:33 +000071 int array[N]; // expected-warning{{variable length arrays are a C99 feature}}
Douglas Gregor0fddb972010-05-22 16:17:30 +000072 accept_array(array); // expected-error{{no matching function for call to 'accept_array'}}
73}
74
75// Variably-modified types cannot be used in local classes.
Eli Friedman0a294222012-02-07 00:15:00 +000076void local_classes(int N) { // expected-note {{declared here}}
Douglas Gregor0fddb972010-05-22 16:17:30 +000077 struct X {
78 int size;
Douglas Gregordb4da822010-05-23 16:51:27 +000079 int array[N]; // expected-error{{fields must have a constant size: 'variable length array in structure' extension will never be supported}} \
Eli Friedman0a294222012-02-07 00:15:00 +000080 // expected-error{{reference to local variable 'N' declared in enclosing function 'local_classes'}} \
Richard Smithd7c56e12011-12-29 21:57:33 +000081 // expected-warning{{variable length arrays are a C99 feature}}
Douglas Gregor0fddb972010-05-22 16:17:30 +000082 };
83}
Douglas Gregor715e9c82010-05-23 16:10:32 +000084
85namespace PR7206 {
86 void f(int x) {
87 struct edge_info {
88 float left;
89 float right;
90 };
Richard Smithd7c56e12011-12-29 21:57:33 +000091 struct edge_info edgeInfo[x]; // expected-warning{{variable length arrays are a C99 feature}}
Douglas Gregor715e9c82010-05-23 16:10:32 +000092 }
93}
Douglas Gregor836adf62010-05-24 17:22:01 +000094
95namespace rdar8020206 {
96 template<typename T>
97 void f(int i) {
98 const unsigned value = i;
Richard Smithd7c56e12011-12-29 21:57:33 +000099 int array[value * i]; // expected-warning 2{{variable length arrays are a C99 feature}}
Douglas Gregor836adf62010-05-24 17:22:01 +0000100 }
101
102 template void f<int>(int); // expected-note{{instantiation of}}
103}
Douglas Gregor204ce172010-05-24 20:42:30 +0000104
105namespace rdar8021385 {
106 typedef int my_int;
107 struct A { typedef int my_int; };
108 template<typename T>
109 struct B {
110 typedef typename T::my_int my_int;
111 void f0() {
112 int M = 4;
Richard Smithd7c56e12011-12-29 21:57:33 +0000113 my_int a[M]; // expected-warning{{variable length arrays are a C99 feature}}
Douglas Gregor204ce172010-05-24 20:42:30 +0000114 }
115 };
116 B<A> a;
117}
Douglas Gregora0750762010-10-06 16:00:31 +0000118
119namespace PR8209 {
120 void f(int n) {
Richard Smithd7c56e12011-12-29 21:57:33 +0000121 typedef int vla_type[n]; // expected-warning{{variable length arrays are a C99 feature}}
Douglas Gregora0750762010-10-06 16:00:31 +0000122 (void)new vla_type; // expected-error{{variably}}
123 }
124}
Chris Lattnere1eed382011-06-14 06:38:10 +0000125
126namespace rdar8733881 { // rdar://8733881
127
128static const int k_cVal3 = (int)(1000*0.2f);
129 int f() {
130 // Ok, fold to a constant size array as an extension.
131 char rgch[k_cVal3] = {0};
132 }
133}
Eli Friedman457a3772012-01-25 22:19:07 +0000134
135namespace PR11744 {
136 template<typename T> int f(int n) {
137 T arr[3][n]; // expected-warning 3 {{variable length arrays are a C99 feature}}
138 return 3;
139 }
140 int test = f<int>(0); // expected-note {{instantiation of}}
141}