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