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