blob: 8a2e35b012fc055dbf149434dae87b7c1ce77266 [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) {
Douglas Gregordb4da822010-05-23 16:51:27 +000017 int array1[N]; // expected-warning{{variable length arrays are a C99 feature, accepted as an extension}}
18 POD array2[N]; // expected-warning{{variable length arrays are a C99 feature, accepted as an extension}}
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
23// We disallow VLAs in templates
24template<typename T>
25void vla_in_template(int N, T t) {
26 int array1[N]; // expected-error{{variable length array cannot be used in a template definition}}
27}
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) {
39 int array2[T::value]; // expected-error{{variable length array cannot be used in a template instantiation}}
40}
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) {
Douglas Gregordb4da822010-05-23 16:51:27 +000050 int array[N]; // expected-warning{{variable length arrays are a C99 feature, accepted as an extension}}
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 {
56 template<int (&Array)[T::value]> // expected-error{{variable length array cannot be used in a template instantiation}}
57 struct Inner {
58
59 };
60};
61
62X1<HasConstantValue> x1a;
63X1<HasNonConstantValue> x1b; // expected-note{{in instantiation of}}
64
65// Template argument deduction does not allow deducing a size from a VLA.
66template<typename T, unsigned N>
67void accept_array(T (&array)[N]); // expected-note{{candidate template ignored: failed template argument deduction}}
68
69void test_accept_array(int N) {
Douglas Gregordb4da822010-05-23 16:51:27 +000070 int array[N]; // expected-warning{{variable length arrays are a C99 feature, accepted as an extension}}
Douglas Gregor0fddb972010-05-22 16:17:30 +000071 accept_array(array); // expected-error{{no matching function for call to 'accept_array'}}
72}
73
74// Variably-modified types cannot be used in local classes.
75void local_classes(int N) {
76 struct X {
77 int size;
Douglas Gregordb4da822010-05-23 16:51:27 +000078 int array[N]; // expected-error{{fields must have a constant size: 'variable length array in structure' extension will never be supported}} \
79 // expected-warning{{variable length arrays are a C99 feature, accepted as an extension}}
Douglas Gregor0fddb972010-05-22 16:17:30 +000080 };
81}
Douglas Gregor715e9c82010-05-23 16:10:32 +000082
83namespace PR7206 {
84 void f(int x) {
85 struct edge_info {
86 float left;
87 float right;
88 };
Douglas Gregordb4da822010-05-23 16:51:27 +000089 struct edge_info edgeInfo[x]; // expected-warning{{variable length arrays are a C99 feature, accepted as an extension}}
Douglas Gregor715e9c82010-05-23 16:10:32 +000090 }
91}