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