Debian Codegraph @

llvm-project/clang/unittests/Format/BracesRemoverTest.cpp

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