2013-01-24 04:02:51 +08:00
|
|
|
// RUN: %clang_cc1 -fsyntax-only -verify -Wvla-extension %s
|
2010-05-23 00:17:30 +08:00
|
|
|
struct NonPOD {
|
|
|
|
NonPOD();
|
|
|
|
};
|
|
|
|
|
|
|
|
struct NonPOD2 {
|
|
|
|
NonPOD np;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct POD {
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
};
|
|
|
|
|
|
|
|
// We allow VLAs of POD types, only.
|
|
|
|
void vla(int N) {
|
2011-12-30 05:57:33 +08:00
|
|
|
int array1[N]; // expected-warning{{variable length arrays are a C99 feature}}
|
|
|
|
POD array2[N]; // expected-warning{{variable length arrays are a C99 feature}}
|
2016-04-29 17:39:50 +08:00
|
|
|
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}}
|
2010-05-23 00:17:30 +08:00
|
|
|
}
|
|
|
|
|
2010-05-24 03:57:01 +08:00
|
|
|
/// Warn about VLAs in templates.
|
2010-05-23 00:17:30 +08:00
|
|
|
template<typename T>
|
|
|
|
void vla_in_template(int N, T t) {
|
2011-12-30 05:57:33 +08:00
|
|
|
int array1[N]; // expected-warning{{variable length arrays are a C99 feature}}
|
2010-05-23 00:17:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct HasConstantValue {
|
|
|
|
static const unsigned int value = 2;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct HasNonConstantValue {
|
|
|
|
static unsigned int value;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void vla_in_template(T t) {
|
2011-12-30 05:57:33 +08:00
|
|
|
int array2[T::value]; // expected-warning{{variable length arrays are a C99 feature}}
|
2010-05-23 00:17:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2011-12-30 05:57:33 +08:00
|
|
|
int array[N]; // expected-warning{{variable length arrays are a C99 feature}}
|
2010-05-23 00:17:30 +08:00
|
|
|
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 {
|
2010-05-24 03:57:01 +08:00
|
|
|
template<int (&Array)[T::value]> // expected-error{{non-type template parameter of variably modified type 'int (&)[HasNonConstantValue::value]'}} \
|
2011-12-30 05:57:33 +08:00
|
|
|
// expected-warning{{variable length arrays are a C99 feature}}
|
2010-05-23 00:17:30 +08:00
|
|
|
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.
|
2013-01-31 13:19:49 +08:00
|
|
|
// FIXME: This diagnostic should make it clear that the two 'N's are different entities!
|
2010-05-23 00:17:30 +08:00
|
|
|
template<typename T, unsigned N>
|
2013-01-31 13:19:49 +08:00
|
|
|
void accept_array(T (&array)[N]); // expected-note{{candidate template ignored: could not match 'T [N]' against 'int [N]'}}
|
2010-05-23 00:17:30 +08:00
|
|
|
|
|
|
|
void test_accept_array(int N) {
|
2011-12-30 05:57:33 +08:00
|
|
|
int array[N]; // expected-warning{{variable length arrays are a C99 feature}}
|
2010-05-23 00:17:30 +08:00
|
|
|
accept_array(array); // expected-error{{no matching function for call to 'accept_array'}}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Variably-modified types cannot be used in local classes.
|
2012-02-07 08:15:00 +08:00
|
|
|
void local_classes(int N) { // expected-note {{declared here}}
|
2010-05-23 00:17:30 +08:00
|
|
|
struct X {
|
|
|
|
int size;
|
2010-05-24 00:51:27 +08:00
|
|
|
int array[N]; // expected-error{{fields must have a constant size: 'variable length array in structure' extension will never be supported}} \
|
2012-02-07 08:15:00 +08:00
|
|
|
// expected-error{{reference to local variable 'N' declared in enclosing function 'local_classes'}} \
|
2011-12-30 05:57:33 +08:00
|
|
|
// expected-warning{{variable length arrays are a C99 feature}}
|
2010-05-23 00:17:30 +08:00
|
|
|
};
|
|
|
|
}
|
2010-05-24 00:10:32 +08:00
|
|
|
|
|
|
|
namespace PR7206 {
|
|
|
|
void f(int x) {
|
|
|
|
struct edge_info {
|
|
|
|
float left;
|
|
|
|
float right;
|
|
|
|
};
|
2011-12-30 05:57:33 +08:00
|
|
|
struct edge_info edgeInfo[x]; // expected-warning{{variable length arrays are a C99 feature}}
|
2010-05-24 00:10:32 +08:00
|
|
|
}
|
|
|
|
}
|
2010-05-25 01:22:01 +08:00
|
|
|
|
|
|
|
namespace rdar8020206 {
|
|
|
|
template<typename T>
|
|
|
|
void f(int i) {
|
|
|
|
const unsigned value = i;
|
2011-12-30 05:57:33 +08:00
|
|
|
int array[value * i]; // expected-warning 2{{variable length arrays are a C99 feature}}
|
2010-05-25 01:22:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template void f<int>(int); // expected-note{{instantiation of}}
|
|
|
|
}
|
2010-05-25 04:42:30 +08:00
|
|
|
|
|
|
|
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;
|
2011-12-30 05:57:33 +08:00
|
|
|
my_int a[M]; // expected-warning{{variable length arrays are a C99 feature}}
|
2010-05-25 04:42:30 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
B<A> a;
|
|
|
|
}
|
2010-10-07 00:00:31 +08:00
|
|
|
|
|
|
|
namespace PR8209 {
|
|
|
|
void f(int n) {
|
2011-12-30 05:57:33 +08:00
|
|
|
typedef int vla_type[n]; // expected-warning{{variable length arrays are a C99 feature}}
|
2010-10-07 00:00:31 +08:00
|
|
|
(void)new vla_type; // expected-error{{variably}}
|
|
|
|
}
|
|
|
|
}
|
2011-06-14 14:38:10 +08:00
|
|
|
|
|
|
|
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};
|
|
|
|
}
|
|
|
|
}
|
2012-01-26 06:19:07 +08:00
|
|
|
|
|
|
|
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}}
|
|
|
|
}
|
2014-02-06 11:49:11 +08:00
|
|
|
|
|
|
|
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>();
|
|
|
|
}
|
|
|
|
}
|