xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/c99-variable-length-array.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify -Wvla-extension %s
2f4a2713aSLionel Sambuc struct NonPOD {
3f4a2713aSLionel Sambuc   NonPOD();
4f4a2713aSLionel Sambuc };
5f4a2713aSLionel Sambuc 
6f4a2713aSLionel Sambuc struct NonPOD2 {
7f4a2713aSLionel Sambuc   NonPOD np;
8f4a2713aSLionel Sambuc };
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc struct POD {
11f4a2713aSLionel Sambuc   int x;
12f4a2713aSLionel Sambuc   int y;
13f4a2713aSLionel Sambuc };
14f4a2713aSLionel Sambuc 
15f4a2713aSLionel Sambuc // We allow VLAs of POD types, only.
vla(int N)16f4a2713aSLionel Sambuc void vla(int N) {
17f4a2713aSLionel Sambuc   int array1[N]; // expected-warning{{variable length arrays are a C99 feature}}
18f4a2713aSLionel Sambuc   POD array2[N]; // expected-warning{{variable length arrays are a C99 feature}}
19f4a2713aSLionel Sambuc   NonPOD array3[N]; // expected-error{{variable length array of non-POD element type 'NonPOD'}}
20f4a2713aSLionel Sambuc   NonPOD2 array4[N][3]; // expected-error{{variable length array of non-POD element type 'NonPOD2'}}
21f4a2713aSLionel Sambuc }
22f4a2713aSLionel Sambuc 
23f4a2713aSLionel Sambuc /// Warn about VLAs in templates.
24f4a2713aSLionel Sambuc template<typename T>
vla_in_template(int N,T t)25f4a2713aSLionel Sambuc void vla_in_template(int N, T t) {
26f4a2713aSLionel Sambuc   int array1[N]; // expected-warning{{variable length arrays are a C99 feature}}
27f4a2713aSLionel Sambuc }
28f4a2713aSLionel Sambuc 
29f4a2713aSLionel Sambuc struct HasConstantValue {
30f4a2713aSLionel Sambuc   static const unsigned int value = 2;
31f4a2713aSLionel Sambuc };
32f4a2713aSLionel Sambuc 
33f4a2713aSLionel Sambuc struct HasNonConstantValue {
34f4a2713aSLionel Sambuc   static unsigned int value;
35f4a2713aSLionel Sambuc };
36f4a2713aSLionel Sambuc 
37f4a2713aSLionel Sambuc template<typename T>
vla_in_template(T t)38f4a2713aSLionel Sambuc void vla_in_template(T t) {
39f4a2713aSLionel Sambuc   int array2[T::value]; // expected-warning{{variable length arrays are a C99 feature}}
40f4a2713aSLionel Sambuc }
41f4a2713aSLionel Sambuc 
42f4a2713aSLionel Sambuc template void vla_in_template<HasConstantValue>(HasConstantValue);
43f4a2713aSLionel Sambuc template void vla_in_template<HasNonConstantValue>(HasNonConstantValue); // expected-note{{instantiation of}}
44f4a2713aSLionel Sambuc 
45f4a2713aSLionel Sambuc template<typename T> struct X0 { };
46f4a2713aSLionel Sambuc 
47f4a2713aSLionel Sambuc // Cannot use any variably-modified type with a template parameter or
48f4a2713aSLionel Sambuc // argument.
inst_with_vla(int N)49f4a2713aSLionel Sambuc void inst_with_vla(int N) {
50f4a2713aSLionel Sambuc   int array[N]; // expected-warning{{variable length arrays are a C99 feature}}
51f4a2713aSLionel Sambuc   X0<__typeof__(array)> x0a; // expected-error{{variably modified type 'typeof (array)' (aka 'int [N]') cannot be used as a template argument}}
52f4a2713aSLionel Sambuc }
53f4a2713aSLionel Sambuc 
54f4a2713aSLionel Sambuc template<typename T>
55f4a2713aSLionel Sambuc struct X1 {
56f4a2713aSLionel Sambuc   template<int (&Array)[T::value]> // expected-error{{non-type template parameter of variably modified type 'int (&)[HasNonConstantValue::value]'}}  \
57f4a2713aSLionel Sambuc   // expected-warning{{variable length arrays are a C99 feature}}
58f4a2713aSLionel Sambuc   struct Inner {
59f4a2713aSLionel Sambuc 
60f4a2713aSLionel Sambuc   };
61f4a2713aSLionel Sambuc };
62f4a2713aSLionel Sambuc 
63f4a2713aSLionel Sambuc X1<HasConstantValue> x1a;
64f4a2713aSLionel Sambuc X1<HasNonConstantValue> x1b; // expected-note{{in instantiation of}}
65f4a2713aSLionel Sambuc 
66f4a2713aSLionel Sambuc // Template argument deduction does not allow deducing a size from a VLA.
67f4a2713aSLionel Sambuc // FIXME: This diagnostic should make it clear that the two 'N's are different entities!
68f4a2713aSLionel Sambuc template<typename T, unsigned N>
69f4a2713aSLionel Sambuc void accept_array(T (&array)[N]); // expected-note{{candidate template ignored: could not match 'T [N]' against 'int [N]'}}
70f4a2713aSLionel Sambuc 
test_accept_array(int N)71f4a2713aSLionel Sambuc void test_accept_array(int N) {
72f4a2713aSLionel Sambuc   int array[N]; // expected-warning{{variable length arrays are a C99 feature}}
73f4a2713aSLionel Sambuc   accept_array(array); // expected-error{{no matching function for call to 'accept_array'}}
74f4a2713aSLionel Sambuc }
75f4a2713aSLionel Sambuc 
76f4a2713aSLionel Sambuc // Variably-modified types cannot be used in local classes.
local_classes(int N)77f4a2713aSLionel Sambuc void local_classes(int N) { // expected-note {{declared here}}
78f4a2713aSLionel Sambuc   struct X {
79f4a2713aSLionel Sambuc     int size;
80f4a2713aSLionel Sambuc     int array[N]; // expected-error{{fields must have a constant size: 'variable length array in structure' extension will never be supported}} \
81f4a2713aSLionel Sambuc                   // expected-error{{reference to local variable 'N' declared in enclosing function 'local_classes'}} \
82f4a2713aSLionel Sambuc                   // expected-warning{{variable length arrays are a C99 feature}}
83f4a2713aSLionel Sambuc   };
84f4a2713aSLionel Sambuc }
85f4a2713aSLionel Sambuc 
86f4a2713aSLionel Sambuc namespace PR7206 {
f(int x)87f4a2713aSLionel Sambuc   void f(int x) {
88f4a2713aSLionel Sambuc     struct edge_info {
89f4a2713aSLionel Sambuc       float left;
90f4a2713aSLionel Sambuc       float right;
91f4a2713aSLionel Sambuc     };
92f4a2713aSLionel Sambuc     struct edge_info edgeInfo[x]; // expected-warning{{variable length arrays are a C99 feature}}
93f4a2713aSLionel Sambuc   }
94f4a2713aSLionel Sambuc }
95f4a2713aSLionel Sambuc 
96f4a2713aSLionel Sambuc namespace rdar8020206 {
97f4a2713aSLionel Sambuc   template<typename T>
f(int i)98f4a2713aSLionel Sambuc   void f(int i) {
99f4a2713aSLionel Sambuc     const unsigned value = i;
100f4a2713aSLionel Sambuc     int array[value * i]; // expected-warning 2{{variable length arrays are a C99 feature}}
101f4a2713aSLionel Sambuc   }
102f4a2713aSLionel Sambuc 
103f4a2713aSLionel Sambuc   template void f<int>(int); // expected-note{{instantiation of}}
104f4a2713aSLionel Sambuc }
105f4a2713aSLionel Sambuc 
106f4a2713aSLionel Sambuc namespace rdar8021385 {
107f4a2713aSLionel Sambuc   typedef int my_int;
108f4a2713aSLionel Sambuc   struct A { typedef int my_int; };
109f4a2713aSLionel Sambuc   template<typename T>
110f4a2713aSLionel Sambuc   struct B {
111f4a2713aSLionel Sambuc     typedef typename T::my_int my_int;
f0rdar8021385::B112f4a2713aSLionel Sambuc     void f0() {
113f4a2713aSLionel Sambuc       int M = 4;
114f4a2713aSLionel Sambuc       my_int a[M]; // expected-warning{{variable length arrays are a C99 feature}}
115f4a2713aSLionel Sambuc     }
116f4a2713aSLionel Sambuc   };
117f4a2713aSLionel Sambuc   B<A> a;
118f4a2713aSLionel Sambuc }
119f4a2713aSLionel Sambuc 
120f4a2713aSLionel Sambuc namespace PR8209 {
f(int n)121f4a2713aSLionel Sambuc   void f(int n) {
122f4a2713aSLionel Sambuc     typedef int vla_type[n]; // expected-warning{{variable length arrays are a C99 feature}}
123f4a2713aSLionel Sambuc     (void)new vla_type; // expected-error{{variably}}
124f4a2713aSLionel Sambuc   }
125f4a2713aSLionel Sambuc }
126f4a2713aSLionel Sambuc 
127f4a2713aSLionel Sambuc namespace rdar8733881 { // rdar://8733881
128f4a2713aSLionel Sambuc 
129f4a2713aSLionel Sambuc static const int k_cVal3 = (int)(1000*0.2f);
f()130f4a2713aSLionel Sambuc   int f() {
131f4a2713aSLionel Sambuc     // Ok, fold to a constant size array as an extension.
132f4a2713aSLionel Sambuc     char rgch[k_cVal3] = {0};
133f4a2713aSLionel Sambuc   }
134f4a2713aSLionel Sambuc }
135f4a2713aSLionel Sambuc 
136f4a2713aSLionel Sambuc namespace PR11744 {
f(int n)137f4a2713aSLionel Sambuc   template<typename T> int f(int n) {
138f4a2713aSLionel Sambuc     T arr[3][n]; // expected-warning 3 {{variable length arrays are a C99 feature}}
139f4a2713aSLionel Sambuc     return 3;
140f4a2713aSLionel Sambuc   }
141f4a2713aSLionel Sambuc   int test = f<int>(0); // expected-note {{instantiation of}}
142f4a2713aSLionel Sambuc }
143*0a6a1f1dSLionel Sambuc 
144*0a6a1f1dSLionel Sambuc namespace pr18633 {
145*0a6a1f1dSLionel Sambuc   struct A1 {
146*0a6a1f1dSLionel Sambuc     static const int sz;
147*0a6a1f1dSLionel Sambuc     static const int sz2;
148*0a6a1f1dSLionel Sambuc   };
149*0a6a1f1dSLionel Sambuc   const int A1::sz2 = 11;
150*0a6a1f1dSLionel Sambuc   template<typename T>
func()151*0a6a1f1dSLionel Sambuc   void func () {
152*0a6a1f1dSLionel Sambuc     int arr[A1::sz]; // expected-warning{{variable length arrays are a C99 feature}}
153*0a6a1f1dSLionel Sambuc   }
154*0a6a1f1dSLionel Sambuc   template<typename T>
func2()155*0a6a1f1dSLionel Sambuc   void func2 () {
156*0a6a1f1dSLionel Sambuc     int arr[A1::sz2];
157*0a6a1f1dSLionel Sambuc   }
158*0a6a1f1dSLionel Sambuc   const int A1::sz = 12;
func2()159*0a6a1f1dSLionel Sambuc   void func2() {
160*0a6a1f1dSLionel Sambuc     func<int>();
161*0a6a1f1dSLionel Sambuc     func2<int>();
162*0a6a1f1dSLionel Sambuc   }
163*0a6a1f1dSLionel Sambuc }
164