brackets.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // RUN: %clang_cc1 -fsyntax-only -verify %s
  2. // RUN: cp %s %t
  3. // RUN: not %clang_cc1 -fixit %t -x c++ -DFIXIT
  4. // RUN: %clang_cc1 -fsyntax-only %t -x c++ -DFIXIT
  5. // RUN: not %clang_cc1 -fsyntax-only -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s -strict-whitespace
  6. void test1() {
  7. int a[] = {0,1,1,2,3};
  8. int []b = {0,1,4,9,16};
  9. // expected-error@-1{{brackets are not allowed here; to declare an array, place the brackets after the name}}
  10. // CHECK: {{^}} int []b = {0,1,4,9,16};
  11. // CHECK: {{^}} ~~ ^
  12. // CHECK: {{^}} []
  13. // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:7-[[@LINE-5]]:9}:""
  14. // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:10-[[@LINE-6]]:10}:"[]"
  15. int c = a[0];
  16. int d = b[0]; // No undeclared identifer error here.
  17. int *e = a;
  18. int *f = b; // No undeclared identifer error here.
  19. int[1] g[2];
  20. // expected-error@-1{{brackets are not allowed here; to declare an array, place the brackets after the name}}
  21. // CHECK: {{^}} int[1] g[2];
  22. // CHECK: {{^}} ~~~ ^
  23. // CHECK: {{^}} [1]
  24. // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:6-[[@LINE-5]]:9}:""
  25. // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:14-[[@LINE-6]]:14}:"[1]"
  26. }
  27. void test2() {
  28. int [3] (*a) = 0;
  29. // expected-error@-1{{brackets are not allowed here; to declare an array, place the brackets after the name}}
  30. // CHECK: {{^}} int [3] (*a) = 0;
  31. // CHECK: {{^}} ~~~~ ^
  32. // CHECK: {{^}} [3]
  33. // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:7-[[@LINE-5]]:11}:""
  34. // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:15-[[@LINE-6]]:15}:"[3]"
  35. #ifndef FIXIT
  36. // Make sure a is corrected to be like type y, instead of like type z.
  37. int (*b)[3] = a;
  38. int (*c[3]) = a; // expected-error{{}}
  39. #endif
  40. }
  41. struct A {
  42. static int [1][1]x;
  43. // expected-error@-1{{brackets are not allowed here; to declare an array, place the brackets after the name}}
  44. // CHECK: {{^}} static int [1][1]x;
  45. // CHECK: {{^}} ~~~~~~ ^
  46. // CHECK: {{^}} [1][1]
  47. // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:14-[[@LINE-5]]:20}:""
  48. // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:21-[[@LINE-6]]:21}:"[1][1]"
  49. };
  50. int [1][1]A::x = { {42} };
  51. // expected-error@-1{{brackets are not allowed here; to declare an array, place the brackets after the name}}
  52. // CHECK: {{^}}int [1][1]A::x = { {42} };
  53. // CHECK: {{^}} ~~~~~~ ^
  54. // CHECK: {{^}} [1][1]
  55. // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:5-[[@LINE-5]]:11}:""
  56. // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:15-[[@LINE-6]]:15}:"[1][1]"
  57. struct B { static int (*x)[5]; };
  58. int [5] *B::x = 0;
  59. // expected-error@-1{{brackets are not allowed here; to declare an array, place the brackets after the name}}
  60. // CHECK: {{^}}int [5] *B::x = 0;
  61. // CHECK: {{^}} ~~~~ ^
  62. // CHECK: {{^}} ( )[5]
  63. // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:5-[[@LINE-5]]:9}:""
  64. // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:9-[[@LINE-6]]:9}:"("
  65. // CHECK: fix-it:{{.*}}:{[[@LINE-7]]:14-[[@LINE-7]]:14}:")[5]"
  66. void test3() {
  67. int [3] *a;
  68. // expected-error@-1{{brackets are not allowed here; to declare an array, place the brackets after the name}}
  69. // CHECK: {{^}} int [3] *a;
  70. // CHECK: {{^}} ~~~~ ^
  71. // CHECK: {{^}} ( )[3]
  72. // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:7-[[@LINE-5]]:11}:""
  73. // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:11-[[@LINE-6]]:11}:"("
  74. // CHECK: fix-it:{{.*}}:{[[@LINE-7]]:13-[[@LINE-7]]:13}:")[3]"
  75. int (*b)[3] = a; // no error
  76. }
  77. void test4() {
  78. int [2] a;
  79. // expected-error@-1{{brackets are not allowed here; to declare an array, place the brackets after the name}}
  80. // CHECK: {{^}} int [2] a;
  81. // CHECK: {{^}} ~~~~ ^
  82. // CHECK: {{^}} [2]
  83. // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:7-[[@LINE-5]]:11}:""
  84. // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:12-[[@LINE-6]]:12}:"[2]"
  85. int [2] &b = a;
  86. // expected-error@-1{{brackets are not allowed here; to declare an array, place the brackets after the name}}
  87. // CHECK: {{^}} int [2] &b = a;
  88. // CHECK: {{^}} ~~~~ ^
  89. // CHECK: {{^}} ( )[2]
  90. // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:7-[[@LINE-5]]:11}:""
  91. // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:11-[[@LINE-6]]:11}:"("
  92. // CHECK: fix-it:{{.*}}:{[[@LINE-7]]:13-[[@LINE-7]]:13}:")[2]"
  93. }
  94. namespace test5 {
  95. #ifndef FIXIT
  96. int [][][];
  97. // expected-error@-1{{expected unqualified-id}}
  98. // CHECK: {{^}}int [][][];
  99. // CHECK: {{^}} ^
  100. struct C {
  101. int [];
  102. // expected-error@-1{{expected member name or ';' after declaration specifiers}}
  103. // CHECK: {{^}} int [];
  104. // CHECK: {{^}} ~~~ ^
  105. };
  106. #endif
  107. }
  108. namespace test6 {
  109. struct A {
  110. static int arr[3];
  111. };
  112. int [3] ::test6::A::arr = {1,2,3};
  113. // expected-error@-1{{brackets are not allowed here; to declare an array, place the brackets after the name}}
  114. // CHECK: {{^}}int [3] ::test6::A::arr = {1,2,3};
  115. // CHECK: {{^}} ~~~~ ^
  116. // CHECK: {{^}} [3]
  117. // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:5-[[@LINE-5]]:9}:""
  118. // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:24-[[@LINE-6]]:24}:"[3]"
  119. }
  120. namespace test7 {
  121. class A{};
  122. void test() {
  123. int [3] A::*a;
  124. // expected-error@-1{{brackets are not allowed here; to declare an array, place the brackets after the name}}
  125. // CHECK: {{^}} int [3] A::*a;
  126. // CHECK: {{^}} ~~~~ ^
  127. // CHECK: {{^}} ( )[3]
  128. // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:7-[[@LINE-5]]:11}:""
  129. // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:11-[[@LINE-6]]:11}:"("
  130. // CHECK: fix-it:{{.*}}:{[[@LINE-7]]:16-[[@LINE-7]]:16}:")[3]"
  131. }
  132. }
  133. namespace test8 {
  134. struct A {
  135. static const char f[];
  136. };
  137. const char[] A::f = "f";
  138. // expected-error@-1{{brackets are not allowed here; to declare an array, place the brackets after the name}}
  139. }
  140. // CHECK: 15 errors generated.