236
|
1 //===- unittest/Format/BracesRemoverTest.cpp ------------------------------===//
|
|
2 //
|
|
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
4 // See https://llvm.org/LICENSE.txt for license information.
|
|
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
6 //
|
|
7 //===----------------------------------------------------------------------===//
|
|
8
|
252
|
9 #include "FormatTestBase.h"
|
236
|
10
|
|
11 #define DEBUG_TYPE "braces-remover-test"
|
|
12
|
|
13 namespace clang {
|
|
14 namespace format {
|
252
|
15 namespace test {
|
236
|
16 namespace {
|
|
17
|
252
|
18 class BracesRemoverTest : public FormatTestBase {};
|
236
|
19
|
|
20 TEST_F(BracesRemoverTest, RemoveBraces) {
|
|
21 FormatStyle Style = getLLVMStyle();
|
|
22 Style.RemoveBracesLLVM = true;
|
|
23
|
|
24 // The following test cases are fully-braced versions of the examples at
|
|
25 // "llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single-
|
|
26 // statement-bodies-of-if-else-loop-statements".
|
|
27
|
|
28 // Omit the braces since the body is simple and clearly associated with the
|
|
29 // `if`.
|
|
30 verifyFormat("if (isa<FunctionDecl>(D))\n"
|
|
31 " handleFunctionDecl(D);\n"
|
|
32 "else if (isa<VarDecl>(D))\n"
|
|
33 " handleVarDecl(D);",
|
|
34 "if (isa<FunctionDecl>(D)) {\n"
|
|
35 " handleFunctionDecl(D);\n"
|
|
36 "} else if (isa<VarDecl>(D)) {\n"
|
|
37 " handleVarDecl(D);\n"
|
|
38 "}",
|
|
39 Style);
|
|
40
|
|
41 // Here we document the condition itself and not the body.
|
|
42 verifyFormat("if (isa<VarDecl>(D)) {\n"
|
|
43 " // It is necessary that we explain the situation with this\n"
|
|
44 " // surprisingly long comment, so it would be unclear\n"
|
|
45 " // without the braces whether the following statement is in\n"
|
|
46 " // the scope of the `if`.\n"
|
|
47 " // Because the condition is documented, we can't really\n"
|
|
48 " // hoist this comment that applies to the body above the\n"
|
|
49 " // `if`.\n"
|
|
50 " handleOtherDecl(D);\n"
|
|
51 "}",
|
|
52 Style);
|
|
53
|
|
54 // Use braces on the outer `if` to avoid a potential dangling `else`
|
|
55 // situation.
|
|
56 verifyFormat("if (isa<VarDecl>(D)) {\n"
|
|
57 " if (shouldProcessAttr(A))\n"
|
|
58 " handleAttr(A);\n"
|
|
59 "}",
|
|
60 "if (isa<VarDecl>(D)) {\n"
|
|
61 " if (shouldProcessAttr(A)) {\n"
|
|
62 " handleAttr(A);\n"
|
|
63 " }\n"
|
|
64 "}",
|
|
65 Style);
|
|
66
|
|
67 // Use braces for the `if` block to keep it uniform with the `else` block.
|
|
68 verifyFormat("if (isa<FunctionDecl>(D)) {\n"
|
|
69 " handleFunctionDecl(D);\n"
|
|
70 "} else {\n"
|
|
71 " // In this `else` case, it is necessary that we explain the\n"
|
|
72 " // situation with this surprisingly long comment, so it\n"
|
|
73 " // would be unclear without the braces whether the\n"
|
|
74 " // following statement is in the scope of the `if`.\n"
|
|
75 " handleOtherDecl(D);\n"
|
|
76 "}",
|
|
77 Style);
|
|
78
|
|
79 // This should also omit braces. The `for` loop contains only a single
|
|
80 // statement, so it shouldn't have braces. The `if` also only contains a
|
|
81 // single simple statement (the `for` loop), so it also should omit braces.
|
|
82 verifyFormat("if (isa<FunctionDecl>(D))\n"
|
|
83 " for (auto *A : D.attrs())\n"
|
|
84 " handleAttr(A);",
|
|
85 "if (isa<FunctionDecl>(D)) {\n"
|
|
86 " for (auto *A : D.attrs()) {\n"
|
|
87 " handleAttr(A);\n"
|
|
88 " }\n"
|
|
89 "}",
|
|
90 Style);
|
|
91
|
|
92 // Use braces for a `do-while` loop and its enclosing statement.
|
|
93 verifyFormat("if (Tok->is(tok::l_brace)) {\n"
|
|
94 " do {\n"
|
|
95 " Tok = Tok->Next;\n"
|
|
96 " } while (Tok);\n"
|
|
97 "}",
|
|
98 Style);
|
|
99
|
|
100 // Use braces for the outer `if` since the nested `for` is braced.
|
|
101 verifyFormat("if (isa<FunctionDecl>(D)) {\n"
|
|
102 " for (auto *A : D.attrs()) {\n"
|
|
103 " // In this `for` loop body, it is necessary that we\n"
|
|
104 " // explain the situation with this surprisingly long\n"
|
|
105 " // comment, forcing braces on the `for` block.\n"
|
|
106 " handleAttr(A);\n"
|
|
107 " }\n"
|
|
108 "}",
|
|
109 Style);
|
|
110
|
|
111 // Use braces on the outer block because there are more than two levels of
|
|
112 // nesting.
|
|
113 verifyFormat("if (isa<FunctionDecl>(D)) {\n"
|
|
114 " for (auto *A : D.attrs())\n"
|
|
115 " for (ssize_t i : llvm::seq<ssize_t>(count))\n"
|
|
116 " handleAttrOnDecl(D, A, i);\n"
|
|
117 "}",
|
|
118 "if (isa<FunctionDecl>(D)) {\n"
|
|
119 " for (auto *A : D.attrs()) {\n"
|
|
120 " for (ssize_t i : llvm::seq<ssize_t>(count)) {\n"
|
|
121 " handleAttrOnDecl(D, A, i);\n"
|
|
122 " }\n"
|
|
123 " }\n"
|
|
124 "}",
|
|
125 Style);
|
|
126
|
|
127 // Use braces on the outer block because of a nested `if`; otherwise the
|
|
128 // compiler would warn: `add explicit braces to avoid dangling else`
|
|
129 verifyFormat("if (auto *D = dyn_cast<FunctionDecl>(D)) {\n"
|
|
130 " if (shouldProcess(D))\n"
|
|
131 " handleVarDecl(D);\n"
|
|
132 " else\n"
|
|
133 " markAsIgnored(D);\n"
|
|
134 "}",
|
|
135 "if (auto *D = dyn_cast<FunctionDecl>(D)) {\n"
|
|
136 " if (shouldProcess(D)) {\n"
|
|
137 " handleVarDecl(D);\n"
|
|
138 " } else {\n"
|
|
139 " markAsIgnored(D);\n"
|
|
140 " }\n"
|
|
141 "}",
|
|
142 Style);
|
|
143
|
|
144 verifyFormat("// clang-format off\n"
|
|
145 "// comment\n"
|
|
146 "while (i > 0) { --i; }\n"
|
|
147 "// clang-format on\n"
|
|
148 "while (j < 0)\n"
|
|
149 " ++j;",
|
|
150 "// clang-format off\n"
|
|
151 "// comment\n"
|
|
152 "while (i > 0) { --i; }\n"
|
|
153 "// clang-format on\n"
|
|
154 "while (j < 0) { ++j; }",
|
|
155 Style);
|
|
156
|
252
|
157 verifyFormat("for (;;) {\n"
|
|
158 " for (;;)\n"
|
|
159 " for (;;)\n"
|
|
160 " a;\n"
|
|
161 "}",
|
|
162 "for (;;) {\n"
|
|
163 " for (;;) {\n"
|
|
164 " for (;;) {\n"
|
|
165 " a;\n"
|
|
166 " }\n"
|
|
167 " }\n"
|
|
168 "}",
|
|
169 Style);
|
|
170
|
236
|
171 verifyFormat("if (a)\n"
|
|
172 " b; // comment\n"
|
|
173 "else if (c)\n"
|
|
174 " d; /* comment */\n"
|
|
175 "else\n"
|
|
176 " e;",
|
|
177 "if (a) {\n"
|
|
178 " b; // comment\n"
|
|
179 "} else if (c) {\n"
|
|
180 " d; /* comment */\n"
|
|
181 "} else {\n"
|
|
182 " e;\n"
|
|
183 "}",
|
|
184 Style);
|
|
185
|
|
186 verifyFormat("if (a) {\n"
|
|
187 " b;\n"
|
|
188 " c;\n"
|
|
189 "} else if (d) {\n"
|
|
190 " e;\n"
|
|
191 "}",
|
|
192 Style);
|
|
193
|
|
194 verifyFormat("if (a) {\n"
|
|
195 "#undef NDEBUG\n"
|
|
196 " b;\n"
|
|
197 "} else {\n"
|
|
198 " c;\n"
|
|
199 "}",
|
|
200 Style);
|
|
201
|
|
202 verifyFormat("if (a) {\n"
|
|
203 " // comment\n"
|
|
204 "} else if (b) {\n"
|
|
205 " c;\n"
|
|
206 "}",
|
|
207 Style);
|
|
208
|
|
209 verifyFormat("if (a) {\n"
|
|
210 " b;\n"
|
|
211 "} else {\n"
|
|
212 " { c; }\n"
|
|
213 "}",
|
|
214 Style);
|
|
215
|
|
216 verifyFormat("if (a) {\n"
|
|
217 " if (b) // comment\n"
|
|
218 " c;\n"
|
|
219 "} else if (d) {\n"
|
|
220 " e;\n"
|
|
221 "}",
|
|
222 "if (a) {\n"
|
|
223 " if (b) { // comment\n"
|
|
224 " c;\n"
|
|
225 " }\n"
|
|
226 "} else if (d) {\n"
|
|
227 " e;\n"
|
|
228 "}",
|
|
229 Style);
|
|
230
|
|
231 verifyFormat("if (a) {\n"
|
|
232 " if (b) {\n"
|
|
233 " c;\n"
|
|
234 " // comment\n"
|
|
235 " } else if (d) {\n"
|
|
236 " e;\n"
|
|
237 " }\n"
|
|
238 "}",
|
|
239 Style);
|
|
240
|
|
241 verifyFormat("if (a) {\n"
|
|
242 " if (b)\n"
|
|
243 " c;\n"
|
|
244 "}",
|
|
245 "if (a) {\n"
|
|
246 " if (b) {\n"
|
|
247 " c;\n"
|
|
248 " }\n"
|
|
249 "}",
|
|
250 Style);
|
|
251
|
|
252 verifyFormat("if (a)\n"
|
|
253 " if (b)\n"
|
|
254 " c;\n"
|
|
255 " else\n"
|
|
256 " d;\n"
|
|
257 "else\n"
|
|
258 " e;",
|
|
259 "if (a) {\n"
|
|
260 " if (b) {\n"
|
|
261 " c;\n"
|
|
262 " } else {\n"
|
|
263 " d;\n"
|
|
264 " }\n"
|
|
265 "} else {\n"
|
|
266 " e;\n"
|
|
267 "}",
|
|
268 Style);
|
|
269
|
|
270 verifyFormat("if (a) {\n"
|
|
271 " // comment\n"
|
|
272 " if (b)\n"
|
|
273 " c;\n"
|
|
274 " else if (d)\n"
|
|
275 " e;\n"
|
|
276 "} else {\n"
|
|
277 " g;\n"
|
|
278 "}",
|
|
279 "if (a) {\n"
|
|
280 " // comment\n"
|
|
281 " if (b) {\n"
|
|
282 " c;\n"
|
|
283 " } else if (d) {\n"
|
|
284 " e;\n"
|
|
285 " }\n"
|
|
286 "} else {\n"
|
|
287 " g;\n"
|
|
288 "}",
|
|
289 Style);
|
|
290
|
|
291 verifyFormat("if (a)\n"
|
|
292 " b;\n"
|
|
293 "else if (c)\n"
|
|
294 " d;\n"
|
|
295 "else\n"
|
|
296 " e;",
|
|
297 "if (a) {\n"
|
|
298 " b;\n"
|
|
299 "} else {\n"
|
|
300 " if (c) {\n"
|
|
301 " d;\n"
|
|
302 " } else {\n"
|
|
303 " e;\n"
|
|
304 " }\n"
|
|
305 "}",
|
|
306 Style);
|
|
307
|
|
308 verifyFormat("if (a) {\n"
|
|
309 " if (b)\n"
|
|
310 " c;\n"
|
|
311 " else if (d)\n"
|
|
312 " e;\n"
|
|
313 "} else {\n"
|
|
314 " g;\n"
|
|
315 "}",
|
|
316 "if (a) {\n"
|
|
317 " if (b)\n"
|
|
318 " c;\n"
|
|
319 " else {\n"
|
|
320 " if (d)\n"
|
|
321 " e;\n"
|
|
322 " }\n"
|
|
323 "} else {\n"
|
|
324 " g;\n"
|
|
325 "}",
|
|
326 Style);
|
|
327
|
|
328 verifyFormat("if (isa<VarDecl>(D)) {\n"
|
|
329 " for (auto *A : D.attrs())\n"
|
|
330 " if (shouldProcessAttr(A))\n"
|
|
331 " handleAttr(A);\n"
|
|
332 "}",
|
|
333 "if (isa<VarDecl>(D)) {\n"
|
|
334 " for (auto *A : D.attrs()) {\n"
|
|
335 " if (shouldProcessAttr(A)) {\n"
|
|
336 " handleAttr(A);\n"
|
|
337 " }\n"
|
|
338 " }\n"
|
|
339 "}",
|
|
340 Style);
|
|
341
|
|
342 verifyFormat("do {\n"
|
|
343 " ++I;\n"
|
|
344 "} while (hasMore() && Filter(*I));",
|
|
345 "do { ++I; } while (hasMore() && Filter(*I));", Style);
|
|
346
|
|
347 verifyFormat("if (a)\n"
|
|
348 " if (b)\n"
|
|
349 " c;\n"
|
|
350 " else {\n"
|
|
351 " if (d)\n"
|
|
352 " e;\n"
|
|
353 " }\n"
|
|
354 "else\n"
|
|
355 " f;",
|
|
356 Style);
|
|
357
|
|
358 verifyFormat("if (a)\n"
|
|
359 " if (b)\n"
|
|
360 " c;\n"
|
|
361 " else {\n"
|
|
362 " if (d)\n"
|
|
363 " e;\n"
|
|
364 " else if (f)\n"
|
|
365 " g;\n"
|
|
366 " }\n"
|
|
367 "else\n"
|
|
368 " h;",
|
|
369 Style);
|
|
370
|
|
371 verifyFormat("if (a) {\n"
|
|
372 " b;\n"
|
|
373 "} else if (c) {\n"
|
|
374 " d;\n"
|
|
375 " e;\n"
|
|
376 "}",
|
|
377 "if (a) {\n"
|
|
378 " b;\n"
|
|
379 "} else {\n"
|
|
380 " if (c) {\n"
|
|
381 " d;\n"
|
|
382 " e;\n"
|
|
383 " }\n"
|
|
384 "}",
|
|
385 Style);
|
|
386
|
|
387 verifyFormat("if (a) {\n"
|
|
388 " b;\n"
|
|
389 " c;\n"
|
|
390 "} else if (d) {\n"
|
|
391 " e;\n"
|
|
392 " f;\n"
|
|
393 "}",
|
|
394 "if (a) {\n"
|
|
395 " b;\n"
|
|
396 " c;\n"
|
|
397 "} else {\n"
|
|
398 " if (d) {\n"
|
|
399 " e;\n"
|
|
400 " f;\n"
|
|
401 " }\n"
|
|
402 "}",
|
|
403 Style);
|
|
404
|
|
405 verifyFormat("if (a) {\n"
|
|
406 " b;\n"
|
|
407 "} else if (c) {\n"
|
|
408 " d;\n"
|
|
409 "} else {\n"
|
|
410 " e;\n"
|
|
411 " f;\n"
|
|
412 "}",
|
|
413 "if (a) {\n"
|
|
414 " b;\n"
|
|
415 "} else {\n"
|
|
416 " if (c) {\n"
|
|
417 " d;\n"
|
|
418 " } else {\n"
|
|
419 " e;\n"
|
|
420 " f;\n"
|
|
421 " }\n"
|
|
422 "}",
|
|
423 Style);
|
|
424
|
|
425 verifyFormat("if (a) {\n"
|
|
426 " b;\n"
|
|
427 "} else if (c) {\n"
|
|
428 " d;\n"
|
|
429 "} else if (e) {\n"
|
|
430 " f;\n"
|
|
431 " g;\n"
|
|
432 "}",
|
|
433 "if (a) {\n"
|
|
434 " b;\n"
|
|
435 "} else {\n"
|
|
436 " if (c) {\n"
|
|
437 " d;\n"
|
|
438 " } else if (e) {\n"
|
|
439 " f;\n"
|
|
440 " g;\n"
|
|
441 " }\n"
|
|
442 "}",
|
|
443 Style);
|
|
444
|
|
445 verifyFormat("if (a) {\n"
|
|
446 " if (b)\n"
|
|
447 " c;\n"
|
|
448 " else if (d) {\n"
|
|
449 " e;\n"
|
|
450 " f;\n"
|
|
451 " }\n"
|
|
452 "} else {\n"
|
|
453 " g;\n"
|
|
454 "}",
|
|
455 "if (a) {\n"
|
|
456 " if (b)\n"
|
|
457 " c;\n"
|
|
458 " else {\n"
|
|
459 " if (d) {\n"
|
|
460 " e;\n"
|
|
461 " f;\n"
|
|
462 " }\n"
|
|
463 " }\n"
|
|
464 "} else {\n"
|
|
465 " g;\n"
|
|
466 "}",
|
|
467 Style);
|
|
468
|
|
469 verifyFormat("if (a)\n"
|
|
470 " if (b)\n"
|
|
471 " c;\n"
|
|
472 " else {\n"
|
|
473 " if (d) {\n"
|
|
474 " e;\n"
|
|
475 " f;\n"
|
|
476 " }\n"
|
|
477 " }\n"
|
|
478 "else\n"
|
|
479 " g;",
|
|
480 Style);
|
|
481
|
|
482 verifyFormat("if (a) {\n"
|
|
483 " b;\n"
|
|
484 " c;\n"
|
|
485 "} else { // comment\n"
|
|
486 " if (d) {\n"
|
|
487 " e;\n"
|
|
488 " f;\n"
|
|
489 " }\n"
|
|
490 "}",
|
|
491 Style);
|
|
492
|
|
493 verifyFormat("if (a)\n"
|
|
494 " b;\n"
|
|
495 "else if (c)\n"
|
|
496 " while (d)\n"
|
|
497 " e;\n"
|
|
498 "// comment",
|
|
499 "if (a)\n"
|
|
500 "{\n"
|
|
501 " b;\n"
|
|
502 "} else if (c) {\n"
|
|
503 " while (d) {\n"
|
|
504 " e;\n"
|
|
505 " }\n"
|
|
506 "}\n"
|
|
507 "// comment",
|
|
508 Style);
|
|
509
|
|
510 verifyFormat("if (a) {\n"
|
|
511 " b;\n"
|
|
512 "} else if (c) {\n"
|
|
513 " d;\n"
|
|
514 "} else {\n"
|
|
515 " e;\n"
|
|
516 " g;\n"
|
|
517 "}",
|
|
518 Style);
|
|
519
|
|
520 verifyFormat("if (a) {\n"
|
|
521 " b;\n"
|
|
522 "} else if (c) {\n"
|
|
523 " d;\n"
|
|
524 "} else {\n"
|
|
525 " e;\n"
|
|
526 "} // comment",
|
|
527 Style);
|
|
528
|
|
529 verifyFormat("int abs = [](int i) {\n"
|
|
530 " if (i >= 0)\n"
|
|
531 " return i;\n"
|
|
532 " return -i;\n"
|
|
533 "};",
|
|
534 "int abs = [](int i) {\n"
|
|
535 " if (i >= 0) {\n"
|
|
536 " return i;\n"
|
|
537 " }\n"
|
|
538 " return -i;\n"
|
|
539 "};",
|
|
540 Style);
|
|
541
|
|
542 verifyFormat("if (a)\n"
|
|
543 " foo();\n"
|
|
544 "else\n"
|
|
545 " bar();",
|
|
546 "if (a)\n"
|
|
547 "{\n"
|
|
548 " foo();\n"
|
|
549 "}\n"
|
|
550 "else\n"
|
|
551 "{\n"
|
|
552 " bar();\n"
|
|
553 "}",
|
|
554 Style);
|
|
555
|
|
556 verifyFormat("if (a)\n"
|
|
557 " foo();\n"
|
|
558 "// comment\n"
|
|
559 "else\n"
|
|
560 " bar();",
|
|
561 "if (a) {\n"
|
|
562 " foo();\n"
|
|
563 "}\n"
|
|
564 "// comment\n"
|
|
565 "else {\n"
|
|
566 " bar();\n"
|
|
567 "}",
|
|
568 Style);
|
|
569
|
|
570 verifyFormat("if (a) {\n"
|
|
571 " if (b)\n"
|
|
572 " c = 1; // comment\n"
|
|
573 "}",
|
|
574 "if (a) {\n"
|
|
575 " if (b) {\n"
|
|
576 " c = 1; // comment\n"
|
|
577 " }\n"
|
|
578 "}",
|
|
579 Style);
|
|
580
|
|
581 verifyFormat("if (a) // comment\n"
|
|
582 " b = 1;",
|
|
583 "if (a) // comment\n"
|
|
584 "{\n"
|
|
585 " b = 1;\n"
|
|
586 "}",
|
|
587 Style);
|
|
588
|
|
589 verifyFormat("if (a) {\n"
|
|
590 "Label:\n"
|
|
591 "}",
|
|
592 Style);
|
|
593
|
|
594 verifyFormat("if (a) {\n"
|
|
595 "Label:\n"
|
|
596 " f();\n"
|
|
597 "}",
|
|
598 Style);
|
|
599
|
|
600 verifyFormat("if (a) {\n"
|
|
601 " f();\n"
|
|
602 "Label:\n"
|
|
603 "}",
|
|
604 Style);
|
|
605
|
|
606 verifyFormat("if consteval {\n"
|
|
607 " f();\n"
|
|
608 "} else {\n"
|
|
609 " g();\n"
|
|
610 "}",
|
|
611 Style);
|
|
612
|
|
613 verifyFormat("if not consteval {\n"
|
|
614 " f();\n"
|
|
615 "} else if (a) {\n"
|
|
616 " g();\n"
|
|
617 "}",
|
|
618 Style);
|
|
619
|
|
620 verifyFormat("if !consteval {\n"
|
|
621 " g();\n"
|
|
622 "}",
|
|
623 Style);
|
|
624
|
|
625 verifyFormat("while (0)\n"
|
|
626 " if (a)\n"
|
|
627 " return b;\n"
|
|
628 "return a;",
|
|
629 "while (0) {\n"
|
|
630 " if (a) {\n"
|
|
631 " return b;\n"
|
|
632 "}}\n"
|
|
633 "return a;",
|
|
634 Style);
|
|
635
|
252
|
636 verifyFormat("if (a)\n"
|
|
637 "#ifdef FOO\n"
|
|
638 " if (b)\n"
|
|
639 " bar = c;\n"
|
|
640 " else\n"
|
|
641 "#endif\n"
|
|
642 " {\n"
|
|
643 " foo = d;\n"
|
|
644 "#ifdef FOO\n"
|
|
645 " bar = e;\n"
|
|
646 "#else\n"
|
|
647 " bar = f;\n" // FIXME: should be indented 1 more level.
|
|
648 "#endif\n"
|
|
649 " }\n"
|
|
650 "else\n"
|
|
651 " bar = g;",
|
|
652 "if (a)\n"
|
|
653 "#ifdef FOO\n"
|
|
654 " if (b)\n"
|
|
655 " bar = c;\n"
|
|
656 " else\n"
|
|
657 "#endif\n"
|
|
658 " {\n"
|
|
659 " foo = d;\n"
|
|
660 "#ifdef FOO\n"
|
|
661 " bar = e;\n"
|
|
662 "#else\n"
|
|
663 " bar = f;\n"
|
|
664 "#endif\n"
|
|
665 " }\n"
|
|
666 "else {\n"
|
|
667 " bar = g;\n"
|
|
668 "}",
|
|
669 Style);
|
|
670
|
236
|
671 Style.ColumnLimit = 65;
|
|
672 verifyFormat("if (condition) {\n"
|
|
673 " ff(Indices,\n"
|
|
674 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
|
|
675 "} else {\n"
|
|
676 " ff(Indices,\n"
|
|
677 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
|
|
678 "}",
|
|
679 Style);
|
|
680
|
|
681 Style.ColumnLimit = 20;
|
|
682
|
|
683 verifyFormat("int i;\n"
|
|
684 "#define FOO(a, b) \\\n"
|
|
685 " while (a) { \\\n"
|
|
686 " b; \\\n"
|
|
687 " }",
|
|
688 Style);
|
|
689
|
|
690 verifyFormat("int ab = [](int i) {\n"
|
|
691 " if (i > 0) {\n"
|
|
692 " i = 12345678 -\n"
|
|
693 " i;\n"
|
|
694 " }\n"
|
|
695 " return i;\n"
|
|
696 "};",
|
|
697 Style);
|
|
698
|
|
699 verifyFormat("if (a) {\n"
|
|
700 " b = c + // 1 -\n"
|
|
701 " d;\n"
|
|
702 "}",
|
|
703 Style);
|
|
704
|
|
705 verifyFormat("if (a) {\n"
|
|
706 " b = c >= 0 ? d\n"
|
|
707 " : e;\n"
|
|
708 "}",
|
|
709 "if (a) {\n"
|
|
710 " b = c >= 0 ? d : e;\n"
|
|
711 "}",
|
|
712 Style);
|
|
713
|
|
714 verifyFormat("if (a)\n"
|
|
715 " b = c > 0 ? d : e;",
|
|
716 "if (a) {\n"
|
|
717 " b = c > 0 ? d : e;\n"
|
|
718 "}",
|
|
719 Style);
|
|
720
|
|
721 verifyFormat("if (-b >=\n"
|
|
722 " c) { // Keep.\n"
|
|
723 " foo();\n"
|
|
724 "} else {\n"
|
|
725 " bar();\n"
|
|
726 "}",
|
|
727 "if (-b >= c) { // Keep.\n"
|
|
728 " foo();\n"
|
|
729 "} else {\n"
|
|
730 " bar();\n"
|
|
731 "}",
|
|
732 Style);
|
|
733
|
|
734 verifyFormat("if (a) /* Remove. */\n"
|
|
735 " f();\n"
|
|
736 "else\n"
|
|
737 " g();",
|
|
738 "if (a) <% /* Remove. */\n"
|
|
739 " f();\n"
|
|
740 "%> else <%\n"
|
|
741 " g();\n"
|
|
742 "%>",
|
|
743 Style);
|
|
744
|
|
745 verifyFormat("while (\n"
|
|
746 " !i--) <% // Keep.\n"
|
|
747 " foo();\n"
|
|
748 "%>",
|
|
749 "while (!i--) <% // Keep.\n"
|
|
750 " foo();\n"
|
|
751 "%>",
|
|
752 Style);
|
|
753
|
|
754 verifyFormat("for (int &i : chars)\n"
|
|
755 " ++i;",
|
|
756 "for (int &i :\n"
|
|
757 " chars) {\n"
|
|
758 " ++i;\n"
|
|
759 "}",
|
|
760 Style);
|
|
761
|
|
762 verifyFormat("if (a)\n"
|
|
763 " b;\n"
|
|
764 "else if (c) {\n"
|
|
765 " d;\n"
|
|
766 " e;\n"
|
|
767 "} else\n"
|
|
768 " f = g(foo, bar,\n"
|
|
769 " baz);",
|
|
770 "if (a)\n"
|
|
771 " b;\n"
|
|
772 "else {\n"
|
|
773 " if (c) {\n"
|
|
774 " d;\n"
|
|
775 " e;\n"
|
|
776 " } else\n"
|
|
777 " f = g(foo, bar, baz);\n"
|
|
778 "}",
|
|
779 Style);
|
|
780
|
252
|
781 verifyFormat("if (foo)\n"
|
|
782 " f();\n"
|
|
783 "else if (bar || baz)\n"
|
|
784 " g();",
|
|
785 "if (foo) {\n"
|
|
786 " f();\n"
|
|
787 "} else if (bar || baz) {\n"
|
|
788 " g();\n"
|
|
789 "}",
|
|
790 Style);
|
|
791
|
236
|
792 Style.ColumnLimit = 0;
|
|
793 verifyFormat("if (a)\n"
|
|
794 " b234567890223456789032345678904234567890 = "
|
|
795 "c234567890223456789032345678904234567890;",
|
|
796 "if (a) {\n"
|
|
797 " b234567890223456789032345678904234567890 = "
|
|
798 "c234567890223456789032345678904234567890;\n"
|
|
799 "}",
|
|
800 Style);
|
|
801
|
|
802 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
|
|
803 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
|
|
804 Style.BraceWrapping.BeforeElse = true;
|
|
805
|
|
806 Style.ColumnLimit = 65;
|
|
807
|
|
808 verifyFormat("if (condition)\n"
|
|
809 "{\n"
|
|
810 " ff(Indices,\n"
|
|
811 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
|
|
812 "}\n"
|
|
813 "else\n"
|
|
814 "{\n"
|
|
815 " ff(Indices,\n"
|
|
816 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
|
|
817 "}",
|
|
818 "if (condition) {\n"
|
|
819 " ff(Indices,\n"
|
|
820 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
|
|
821 "} else {\n"
|
|
822 " ff(Indices,\n"
|
|
823 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
|
|
824 "}",
|
|
825 Style);
|
|
826
|
|
827 verifyFormat("if (a)\n"
|
|
828 "{ //\n"
|
|
829 " foo();\n"
|
|
830 "}",
|
|
831 "if (a) { //\n"
|
|
832 " foo();\n"
|
|
833 "}",
|
|
834 Style);
|
|
835
|
|
836 verifyFormat("if (a) // comment\n"
|
|
837 " b = 1;",
|
|
838 "if (a) // comment\n"
|
|
839 "{\n"
|
|
840 " b = 1;\n"
|
|
841 "}",
|
|
842 Style);
|
|
843
|
|
844 Style.ColumnLimit = 20;
|
|
845
|
|
846 verifyFormat("int ab = [](int i) {\n"
|
|
847 " if (i > 0)\n"
|
|
848 " {\n"
|
|
849 " i = 12345678 -\n"
|
|
850 " i;\n"
|
|
851 " }\n"
|
|
852 " return i;\n"
|
|
853 "};",
|
|
854 "int ab = [](int i) {\n"
|
|
855 " if (i > 0) {\n"
|
|
856 " i = 12345678 -\n"
|
|
857 " i;\n"
|
|
858 " }\n"
|
|
859 " return i;\n"
|
|
860 "};",
|
|
861 Style);
|
|
862
|
|
863 verifyFormat("if (a)\n"
|
|
864 "{\n"
|
|
865 " b = c + // 1 -\n"
|
|
866 " d;\n"
|
|
867 "}",
|
|
868 "if (a) {\n"
|
|
869 " b = c + // 1 -\n"
|
|
870 " d;\n"
|
|
871 "}",
|
|
872 Style);
|
|
873
|
|
874 verifyFormat("if (a)\n"
|
|
875 "{\n"
|
|
876 " b = c >= 0 ? d\n"
|
|
877 " : e;\n"
|
|
878 "}",
|
|
879 "if (a) {\n"
|
|
880 " b = c >= 0 ? d : e;\n"
|
|
881 "}",
|
|
882 Style);
|
|
883
|
|
884 verifyFormat("if (a)\n"
|
|
885 " b = c > 0 ? d : e;",
|
|
886 "if (a)\n"
|
|
887 "{\n"
|
|
888 " b = c > 0 ? d : e;\n"
|
|
889 "}",
|
|
890 Style);
|
|
891
|
|
892 verifyFormat("if (foo + bar <=\n"
|
|
893 " baz)\n"
|
|
894 "{\n"
|
|
895 " func(arg1, arg2);\n"
|
|
896 "}",
|
|
897 "if (foo + bar <= baz) {\n"
|
|
898 " func(arg1, arg2);\n"
|
|
899 "}",
|
|
900 Style);
|
|
901
|
|
902 verifyFormat("if (foo + bar < baz)\n"
|
|
903 " func(arg1, arg2);\n"
|
|
904 "else\n"
|
|
905 " func();",
|
|
906 "if (foo + bar < baz)\n"
|
|
907 "<%\n"
|
|
908 " func(arg1, arg2);\n"
|
|
909 "%>\n"
|
|
910 "else\n"
|
|
911 "<%\n"
|
|
912 " func();\n"
|
|
913 "%>",
|
|
914 Style);
|
|
915
|
|
916 verifyFormat("while (i--)\n"
|
|
917 "<% // Keep.\n"
|
|
918 " foo();\n"
|
|
919 "%>",
|
|
920 "while (i--) <% // Keep.\n"
|
|
921 " foo();\n"
|
|
922 "%>",
|
|
923 Style);
|
|
924
|
|
925 verifyFormat("for (int &i : chars)\n"
|
|
926 " ++i;",
|
|
927 "for (int &i : chars)\n"
|
|
928 "{\n"
|
|
929 " ++i;\n"
|
|
930 "}",
|
|
931 Style);
|
|
932 }
|
|
933
|
|
934 } // namespace
|
252
|
935 } // namespace test
|
236
|
936 } // namespace format
|
|
937 } // namespace clang
|