c99-variable-length-array.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // RUN: %clang_cc1 -fsyntax-only -verify -Wvla %s
  2. struct NonPOD {
  3. NonPOD();
  4. };
  5. struct NonPOD2 {
  6. NonPOD np;
  7. };
  8. struct POD {
  9. int x;
  10. int y;
  11. };
  12. // We allow VLAs of POD types, only.
  13. void vla(int N) {
  14. int array1[N]; // expected-warning{{variable length arrays are a C99 feature}}
  15. POD array2[N]; // expected-warning{{variable length arrays are a C99 feature}}
  16. NonPOD array3[N]; // expected-error{{variable length array of non-POD element type 'NonPOD'}}
  17. NonPOD2 array4[N][3]; // expected-error{{variable length array of non-POD element type 'NonPOD2'}}
  18. }
  19. /// Warn about VLAs in templates.
  20. template<typename T>
  21. void vla_in_template(int N, T t) {
  22. int array1[N]; // expected-warning{{variable length arrays are a C99 feature}}
  23. }
  24. struct HasConstantValue {
  25. static const unsigned int value = 2;
  26. };
  27. struct HasNonConstantValue {
  28. static unsigned int value;
  29. };
  30. template<typename T>
  31. void vla_in_template(T t) {
  32. int array2[T::value]; // expected-warning{{variable length arrays are a C99 feature}}
  33. }
  34. template void vla_in_template<HasConstantValue>(HasConstantValue);
  35. template void vla_in_template<HasNonConstantValue>(HasNonConstantValue); // expected-note{{instantiation of}}
  36. template<typename T> struct X0 { };
  37. // Cannot use any variably-modified type with a template parameter or
  38. // argument.
  39. void inst_with_vla(int N) {
  40. int array[N]; // expected-warning{{variable length arrays are a C99 feature}}
  41. X0<__typeof__(array)> x0a; // expected-error{{variably modified type 'typeof (array)' (aka 'int [N]') cannot be used as a template argument}}
  42. }
  43. template<typename T>
  44. struct X1 {
  45. template<int (&Array)[T::value]> // expected-error{{non-type template parameter of variably modified type 'int (&)[HasNonConstantValue::value]'}} \
  46. // expected-warning{{variable length arrays are a C99 feature}}
  47. struct Inner {
  48. };
  49. };
  50. X1<HasConstantValue> x1a;
  51. X1<HasNonConstantValue> x1b; // expected-note{{in instantiation of}}
  52. // Template argument deduction does not allow deducing a size from a VLA.
  53. template<typename T, unsigned N>
  54. void accept_array(T (&array)[N]); // expected-note{{candidate template ignored: failed template argument deduction}}
  55. void test_accept_array(int N) {
  56. int array[N]; // expected-warning{{variable length arrays are a C99 feature}}
  57. accept_array(array); // expected-error{{no matching function for call to 'accept_array'}}
  58. }
  59. // Variably-modified types cannot be used in local classes.
  60. void local_classes(int N) {
  61. struct X {
  62. int size;
  63. int array[N]; // expected-error{{fields must have a constant size: 'variable length array in structure' extension will never be supported}} \
  64. // expected-warning{{variable length arrays are a C99 feature}}
  65. };
  66. }
  67. namespace PR7206 {
  68. void f(int x) {
  69. struct edge_info {
  70. float left;
  71. float right;
  72. };
  73. struct edge_info edgeInfo[x]; // expected-warning{{variable length arrays are a C99 feature}}
  74. }
  75. }
  76. namespace rdar8020206 {
  77. template<typename T>
  78. void f(int i) {
  79. const unsigned value = i;
  80. int array[value * i]; // expected-warning 2{{variable length arrays are a C99 feature}}
  81. }
  82. template void f<int>(int); // expected-note{{instantiation of}}
  83. }
  84. namespace rdar8021385 {
  85. typedef int my_int;
  86. struct A { typedef int my_int; };
  87. template<typename T>
  88. struct B {
  89. typedef typename T::my_int my_int;
  90. void f0() {
  91. int M = 4;
  92. my_int a[M]; // expected-warning{{variable length arrays are a C99 feature}}
  93. }
  94. };
  95. B<A> a;
  96. }
  97. namespace PR8209 {
  98. void f(int n) {
  99. typedef int vla_type[n]; // expected-warning{{variable length arrays are a C99 feature}}
  100. (void)new vla_type; // expected-error{{variably}}
  101. }
  102. }
  103. namespace rdar8733881 { // rdar://8733881
  104. static const int k_cVal3 = (int)(1000*0.2f);
  105. int f() {
  106. // Ok, fold to a constant size array as an extension.
  107. char rgch[k_cVal3] = {0};
  108. }
  109. }