xref: /llvm-project/clang/test/Parser/brackets.cpp (revision 692d6bb544338c65550a46d76b81db668d4901f5)
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 // REQUIRES: rewriter
7 
8 void test1() {
9   int a[] = {0,1,1,2,3};
10   int []b = {0,1,4,9,16};
11   // expected-error@-1{{brackets go after the unqualified-id}}
12   // CHECK: {{^}}  int []b = {0,1,4,9,16};
13   // CHECK: {{^}}      ~~ ^
14   // CHECK: {{^}}         []
15   // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:7-[[@LINE-5]]:9}:""
16   // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:10-[[@LINE-6]]:10}:"[]"
17 
18   int c = a[0];
19   int d = b[0];  // No undeclared identifer error here.
20 
21   int *e = a;
22   int *f = b;  // No undeclared identifer error here.
23 
24   int[1] g[2];
25   // expected-error@-1{{brackets go after the unqualified-id}}
26   // CHECK: {{^}}  int[1] g[2];
27   // CHECK: {{^}}     ~~~     ^
28   // CHECK: {{^}}             [1]
29   // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:6-[[@LINE-5]]:9}:""
30   // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:14-[[@LINE-6]]:14}:"[1]"
31 }
32 
33 void test2() {
34   int [3] (*a) = 0;
35   // expected-error@-1{{brackets go after the unqualified-id}}
36   // CHECK: {{^}}  int [3] (*a) = 0;
37   // CHECK: {{^}}      ~~~~    ^
38   // CHECK: {{^}}              [3]
39   // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:7-[[@LINE-5]]:11}:""
40   // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:15-[[@LINE-6]]:15}:"[3]"
41 
42 #ifndef FIXIT
43   // Make sure a is corrected to be like type y, instead of like type z.
44   int (*b)[3] = a;
45   int (*c[3]) = a;  // expected-error{{}}
46 #endif
47 }
48 
49 struct A {
50   static int [1][1]x;
51   // expected-error@-1{{brackets go after the unqualified-id}}
52   // CHECK: {{^}}  static int [1][1]x;
53   // CHECK: {{^}}             ~~~~~~ ^
54   // CHECK: {{^}}                    [1][1]
55   // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:14-[[@LINE-5]]:20}:""
56   // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:21-[[@LINE-6]]:21}:"[1][1]"
57 };
58 
59 int [1][1]A::x = { {42} };
60 // expected-error@-1{{brackets go after the unqualified-id}}
61 // CHECK: {{^}}int [1][1]A::x = { {42} };
62 // CHECK: {{^}}    ~~~~~~    ^
63 // CHECK: {{^}}              [1][1]
64 // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:5-[[@LINE-5]]:11}:""
65 // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:15-[[@LINE-6]]:15}:"[1][1]"
66 
67 struct B { static int (*x)[5]; };
68 int [5] *B::x = 0;
69 // expected-error@-1{{brackets go after the unqualified-id}}
70 // CHECK: {{^}}int [5] *B::x = 0;
71 // CHECK: {{^}}    ~~~~     ^
72 // CHECK: {{^}}        (    )[5]
73 // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:5-[[@LINE-5]]:9}:""
74 // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:9-[[@LINE-6]]:9}:"("
75 // CHECK: fix-it:{{.*}}:{[[@LINE-7]]:14-[[@LINE-7]]:14}:")[5]"
76 
77 void test3() {
78   int [3] *a;
79   // expected-error@-1{{brackets go after the unqualified-id}}
80   // CHECK: {{^}}  int [3] *a;
81   // CHECK: {{^}}      ~~~~  ^
82   // CHECK: {{^}}          ( )[3]
83   // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:7-[[@LINE-5]]:11}:""
84   // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:11-[[@LINE-6]]:11}:"("
85   // CHECK: fix-it:{{.*}}:{[[@LINE-7]]:13-[[@LINE-7]]:13}:")[3]"
86 
87   int (*b)[3] = a;  // no error
88 }
89 
90 void test4() {
91   int [2] a;
92   // expected-error@-1{{brackets go after the unqualified-id}}
93   // CHECK: {{^}}  int [2] a;
94   // CHECK: {{^}}      ~~~~ ^
95   // CHECK: {{^}}           [2]
96   // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:7-[[@LINE-5]]:11}:""
97   // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:12-[[@LINE-6]]:12}:"[2]"
98 
99   int [2] &b = a;
100   // expected-error@-1{{brackets go after the unqualified-id}}
101   // CHECK: {{^}}  int [2] &b = a;
102   // CHECK: {{^}}      ~~~~  ^
103   // CHECK: {{^}}          ( )[2]
104   // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:7-[[@LINE-5]]:11}:""
105   // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:11-[[@LINE-6]]:11}:"("
106   // CHECK: fix-it:{{.*}}:{[[@LINE-7]]:13-[[@LINE-7]]:13}:")[2]"
107 
108 }
109 
110 namespace test5 {
111 #ifndef FIXIT
112 int [][][];
113 // expected-error@-1{{expected unqualified-id}}
114 // CHECK: {{^}}int [][][];
115 // CHECK: {{^}}    ^
116 
117 struct C {
118   int [];
119   // expected-error@-1{{expected member name or ';' after declaration specifiers}}
120   // CHECK: {{^}}  int [];
121   // CHECK: {{^}}  ~~~ ^
122 };
123 
124 #endif
125 }
126 
127 namespace test6 {
128 struct A {
129   static int arr[3];
130 };
131 int [3] ::test6::A::arr = {1,2,3};
132 // expected-error@-1{{brackets go after the unqualified-id}}
133 // CHECK: {{^}}int [3] ::test6::A::arr = {1,2,3};
134 // CHECK: {{^}}    ~~~~               ^
135 // CHECK: {{^}}                       [3]
136 // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:5-[[@LINE-5]]:9}:""
137 // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:24-[[@LINE-6]]:24}:"[3]"
138 
139 }
140 
141 namespace test7 {
142 class A{};
143 void test() {
144   int [3] A::*a;
145   // expected-error@-1{{brackets go after the unqualified-id}}
146   // CHECK: {{^}}  int [3] A::*a;
147   // CHECK: {{^}}      ~~~~     ^
148   // CHECK: {{^}}          (    )[3]
149   // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:7-[[@LINE-5]]:11}:""
150   // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:11-[[@LINE-6]]:11}:"("
151   // CHECK: fix-it:{{.*}}:{[[@LINE-7]]:16-[[@LINE-7]]:16}:")[3]"
152 }
153 }
154 // CHECK: 14 errors generated.
155