[clang-format] Fix a bug in aligning comments above PPDirective (#72791)
[llvm-project.git] / clang / test / Refactor / Extract / ExtractionSemicolonPolicy.cpp
blob865e8290923a0144e2eefe420541a938f650dc97
1 // RUN: clang-refactor extract -selection=test:%s %s -- -std=c++11 -fcxx-exceptions | grep -v CHECK | FileCheck %s
3 struct Rectangle { int width, height; };
5 void extractStatement(const Rectangle &r) {
6 /*range adeclstmt=->+0:59*/int area = r.width * r.height;
8 // CHECK: 1 'adeclstmt' results:
9 // CHECK: static void extracted() {
10 // CHECK-NEXT: int area = r.width * r.height;{{$}}
11 // CHECK-NEXT: }{{[[:space:]].*}}
12 // CHECK-NEXT: void extractStatement(const Rectangle &r) {
13 // CHECK-NEXT: /*range adeclstmt=->+0:59*/extracted();{{$}}
14 // CHECK-NEXT: }
16 void extractStatementNoSemiIf(const Rectangle &r) {
17 /*range bextractif=->+2:4*/if (r.width) {
18 int x = r.height;
21 // CHECK: 1 'bextractif' results:
22 // CHECK: static void extracted() {
23 // CHECK-NEXT: if (r.width) {
24 // CHECK-NEXT: int x = r.height;
25 // CHECK-NEXT: }{{$}}
26 // CHECK-NEXT: }{{[[:space:]].*}}
27 // CHECK-NEXT: void extractStatementNoSemiIf(const Rectangle &r) {
28 // CHECK-NEXT: /*range bextractif=->+2:4*/extracted();{{$}}
29 // CHECK-NEXT: }
31 void extractStatementDontExtraneousSemi(const Rectangle &r) {
32 /*range cextractif=->+2:4*/if (r.width) {
33 int x = r.height;
34 } ;
35 } //^ This semicolon shouldn't be extracted.
36 // CHECK: 1 'cextractif' results:
37 // CHECK: static void extracted() {
38 // CHECK-NEXT: if (r.width) {
39 // CHECK-NEXT: int x = r.height;
40 // CHECK-NEXT: }{{$}}
41 // CHECK-NEXT: }{{[[:space:]].*}}
42 // CHECK-NEXT: void extractStatementDontExtraneousSemi(const Rectangle &r) {
43 // CHECK-NEXT: extracted(); ;{{$}}
44 // CHECK-NEXT: }
46 void extractStatementNotSemiSwitch() {
47 /*range dextract=->+5:4*/switch (2) {
48 case 1:
49 break;
50 case 2:
51 break;
54 // CHECK: 1 'dextract' results:
55 // CHECK: static void extracted() {
56 // CHECK-NEXT: switch (2) {
57 // CHECK-NEXT: case 1:
58 // CHECK-NEXT: break;
59 // CHECK-NEXT: case 2:
60 // CHECK-NEXT: break;
61 // CHECK-NEXT: }{{$}}
62 // CHECK-NEXT: }{{[[:space:]].*}}
63 // CHECK-NEXT: void extractStatementNotSemiSwitch() {
64 // CHECK-NEXT: extracted();{{$}}
65 // CHECK-NEXT: }
68 void extractStatementNotSemiWhile() {
69 /*range eextract=->+2:4*/while (true) {
70 int x = 0;
73 // CHECK: 1 'eextract' results:
74 // CHECK: static void extracted() {
75 // CHECK-NEXT: while (true) {
76 // CHECK-NEXT: int x = 0;
77 // CHECK-NEXT: }{{$}}
78 // CHECK-NEXT: }{{[[:space:]].*}}
79 // CHECK-NEXT: void extractStatementNotSemiWhile() {
80 // CHECK-NEXT: extracted();{{$}}
81 // CHECK-NEXT: }
83 void extractStatementNotSemiFor() {
84 /*range fextract=->+1:4*/for (int i = 0; i < 10; ++i) {
87 // CHECK: 1 'fextract' results:
88 // CHECK: static void extracted() {
89 // CHECK-NEXT: for (int i = 0; i < 10; ++i) {
90 // CHECK-NEXT: }{{$}}
91 // CHECK-NEXT: }{{[[:space:]].*}}
92 // CHECK-NEXT: void extractStatementNotSemiFor() {
93 // CHECK-NEXT: extracted();{{$}}
94 // CHECK-NEXT: }
96 struct XS {
97 int *begin() { return 0; }
98 int *end() { return 0; }
101 void extractStatementNotSemiRangedFor(XS xs) {
102 /*range gextract=->+1:4*/for (int i : xs) {
105 // CHECK: 1 'gextract' results:
106 // CHECK: static void extracted() {
107 // CHECK-NEXT: for (int i : xs) {
108 // CHECK-NEXT: }{{$}}
109 // CHECK-NEXT: }{{[[:space:]].*}}
110 // CHECK-NEXT: void extractStatementNotSemiRangedFor(XS xs) {
111 // CHECK-NEXT: extracted();{{$}}
112 // CHECK-NEXT: }
114 void extractStatementNotSemiRangedTryCatch() {
115 /*range hextract=->+3:4*/try { int x = 0; }
116 catch (const int &i) {
117 int y = i;
120 // CHECK: 1 'hextract' results:
121 // CHECK: static void extracted() {
122 // CHECK-NEXT: try { int x = 0; }
123 // CHECK-NEXT: catch (const int &i) {
124 // CHECK-NEXT: int y = i;
125 // CHECK-NEXT: }{{$}}
126 // CHECK-NEXT: }{{[[:space:]].*}}
127 // CHECK-NEXT: void extractStatementNotSemiRangedTryCatch() {
128 // CHECK-NEXT: extracted();{{$}}
129 // CHECK-NEXT: }
131 void extractCantFindSemicolon() {
132 /*range iextract=->+1:17*/do {
133 } while (true)
134 // Add a semicolon in both the extracted and original function as we don't
135 // want to extract the semicolon below.
138 // CHECK: 1 'iextract' results:
139 // CHECK: static void extracted() {
140 // CHECK-NEXT: do {
141 // CHECK-NEXT: } while (true);{{$}}
142 // CHECK-NEXT: }{{[[:space:]].*}}
143 // CHECK-NEXT: void extractCantFindSemicolon() {
144 // CHECK-NEXT: extracted();{{$}}
145 // CHECK-NEXT: //
146 // CHECK-NEXT: //
147 // CHECK-NEXT: ;
148 // CHECK-NEXT: }
150 void extractFindSemicolon() {
151 /*range jextract=->+1:17*/do {
152 } while (true) /*grab*/ ;
154 // CHECK: 1 'jextract' results:
155 // CHECK: static void extracted() {
156 // CHECK-NEXT: do {
157 // CHECK-NEXT: } while (true) /*grab*/ ;{{$}}
158 // CHECK-NEXT: }{{[[:space:]].*}}
159 // CHECK-NEXT: void extractFindSemicolon() {
160 // CHECK-NEXT: extracted();{{$}}
161 // CHECK-NEXT: }
163 void call();
165 void careForNonCompoundSemicolons1() {
166 /*range kextract=->+1:11*/if (true)
167 call();
169 // CHECK: 1 'kextract' results:
170 // CHECK: static void extracted() {
171 // CHECK-NEXT: if (true)
172 // CHECK-NEXT: call();{{$}}
173 // CHECK-NEXT: }{{[[:space:]].*}}
174 // CHECK-NEXT: void careForNonCompoundSemicolons1() {
175 // CHECK-NEXT: extracted();{{$}}
176 // CHECK-NEXT: }
178 void careForNonCompoundSemicolons2() {
179 /*range lextract=->+3:1*/for (int i = 0; i < 10; ++i)
180 while (i != 0)
182 // end right here111!
184 // CHECK: 1 'lextract' results:
185 // CHECK: static void extracted() {
186 // CHECK-NEXT: for (int i = 0; i < 10; ++i)
187 // CHECK-NEXT: while (i != 0)
188 // CHECK-NEXT: ;{{$}}
189 // CHECK-NEXT: }{{[[:space:]].*}}
190 // CHECK-NEXT: void careForNonCompoundSemicolons2() {
191 // CHECK-NEXT: extracted();{{$}}
192 // CHECK-NEXT: //
193 // CHECK-NEXT: }
195 void careForSwitchSemicolon() {
196 /*range mextract=->+0:53*/switch(0) default: break;
198 // CHECK: 1 'mextract' results:
199 // CHECK: static void extracted() {
200 // CHECK-NEXT: switch(0) default: break;{{$}}
201 // CHECK-NEXT: }{{[[:space:]].*}}
202 // CHECK-NEXT: void careForSwitchSemicolon() {
203 // CHECK-NEXT: extracted();{{$}}
204 // CHECK-NEXT: }
206 void extractStatementNotSemiDecl() {
207 /*range nextract=->+0:38*/int x = 5;
209 // CHECK: 1 'nextract' results:
210 // CHECK: static void extracted() {
211 // CHECK-NEXT: int x = 5;{{$}}
212 // CHECK-NEXT: }{{[[:space:]].*}}
213 // CHECK-NEXT: void extractStatementNotSemiDecl() {
214 // CHECK-NEXT: extracted();{{$}}
215 // CHECK-NEXT: }