1 //===- unittest/Format/BracesRemoverTest.cpp ------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "FormatTestBase.h"
11 #define DEBUG_TYPE "braces-remover-test"
18 class BracesRemoverTest
: public FormatTestBase
{};
20 TEST_F(BracesRemoverTest
, RemoveBraces
) {
21 FormatStyle Style
= getLLVMStyle();
22 Style
.RemoveBracesLLVM
= true;
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".
28 // Omit the braces since the body is simple and clearly associated with the
30 verifyFormat("if (isa<FunctionDecl>(D))\n"
31 " handleFunctionDecl(D);\n"
32 "else if (isa<VarDecl>(D))\n"
34 "if (isa<FunctionDecl>(D)) {\n"
35 " handleFunctionDecl(D);\n"
36 "} else if (isa<VarDecl>(D)) {\n"
37 " handleVarDecl(D);\n"
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"
50 " handleOtherDecl(D);\n"
54 // Use braces on the outer `if` to avoid a potential dangling `else`
56 verifyFormat("if (isa<VarDecl>(D)) {\n"
57 " if (shouldProcessAttr(A))\n"
60 "if (isa<VarDecl>(D)) {\n"
61 " if (shouldProcessAttr(A)) {\n"
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"
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"
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"
85 "if (isa<FunctionDecl>(D)) {\n"
86 " for (auto *A : D.attrs()) {\n"
92 // Use braces for a `do-while` loop and its enclosing statement.
93 verifyFormat("if (Tok->is(tok::l_brace)) {\n"
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"
111 // Use braces on the outer block because there are more than two levels of
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"
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"
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"
133 " markAsIgnored(D);\n"
135 "if (auto *D = dyn_cast<FunctionDecl>(D)) {\n"
136 " if (shouldProcess(D)) {\n"
137 " handleVarDecl(D);\n"
139 " markAsIgnored(D);\n"
144 verifyFormat("// clang-format off\n"
146 "while (i > 0) { --i; }\n"
147 "// clang-format on\n"
150 "// clang-format off\n"
152 "while (i > 0) { --i; }\n"
153 "// clang-format on\n"
154 "while (j < 0) { ++j; }",
157 verifyFormat("for (;;) {\n"
171 verifyFormat("if (a)\n"
174 " d; /* comment */\n"
180 " d; /* comment */\n"
186 verifyFormat("if (a) {\n"
194 verifyFormat("if (a) {\n"
202 verifyFormat("if (a) {\n"
209 verifyFormat("if (a) {\n"
216 verifyFormat("if (a) {\n"
217 " if (b) // comment\n"
223 " if (b) { // comment\n"
231 verifyFormat("if (a) {\n"
241 verifyFormat("if (a) {\n"
252 verifyFormat("if (a)\n"
270 verifyFormat("if (a) {\n"
291 verifyFormat("if (a)\n"
308 verifyFormat("if (a) {\n"
328 verifyFormat("if (isa<VarDecl>(D)) {\n"
329 " for (auto *A : D.attrs())\n"
330 " if (shouldProcessAttr(A))\n"
333 "if (isa<VarDecl>(D)) {\n"
334 " for (auto *A : D.attrs()) {\n"
335 " if (shouldProcessAttr(A)) {\n"
342 verifyFormat("do {\n"
344 "} while (hasMore() && Filter(*I));",
345 "do { ++I; } while (hasMore() && Filter(*I));", Style
);
347 verifyFormat("if (a)\n"
358 verifyFormat("if (a)\n"
371 verifyFormat("if (a) {\n"
387 verifyFormat("if (a) {\n"
405 verifyFormat("if (a) {\n"
425 verifyFormat("if (a) {\n"
445 verifyFormat("if (a) {\n"
469 verifyFormat("if (a)\n"
482 verifyFormat("if (a) {\n"
485 "} else { // comment\n"
493 verifyFormat("if (a)\n"
510 verifyFormat("if (a) {\n"
520 verifyFormat("if (a) {\n"
529 verifyFormat("int abs = [](int i) {\n"
534 "int abs = [](int i) {\n"
542 verifyFormat("if (a)\n"
556 verifyFormat("if (a)\n"
570 verifyFormat("if (a) {\n"
572 " c = 1; // comment\n"
576 " c = 1; // comment\n"
581 verifyFormat("if (a) // comment\n"
583 "if (a) // comment\n"
589 verifyFormat("if (a) {\n"
594 verifyFormat("if (a) {\n"
600 verifyFormat("if (a) {\n"
606 verifyFormat("if consteval {\n"
613 verifyFormat("if not consteval {\n"
620 verifyFormat("if !consteval {\n"
625 verifyFormat("while (0)\n"
636 verifyFormat("if (a)\n"
647 " bar = f;\n" // FIXME: should be indented 1 more level.
671 Style
.ColumnLimit
= 65;
672 verifyFormat("if (condition) {\n"
674 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
677 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
681 Style
.ColumnLimit
= 20;
683 verifyFormat("int i;\n"
684 "#define FOO(a, b) \\\n"
690 verifyFormat("int ab = [](int i) {\n"
699 verifyFormat("if (a) {\n"
705 verifyFormat("if (a) {\n"
710 " b = c >= 0 ? d : e;\n"
714 verifyFormat("if (a)\n"
715 " b = c > 0 ? d : e;",
717 " b = c > 0 ? d : e;\n"
721 verifyFormat("if (-b >=\n"
727 "if (-b >= c) { // Keep.\n"
734 verifyFormat("if (a) /* Remove. */\n"
738 "if (a) <% /* Remove. */\n"
745 verifyFormat("while (\n"
746 " !i--) <% // Keep.\n"
749 "while (!i--) <% // Keep.\n"
754 verifyFormat("for (int &i : chars)\n"
762 verifyFormat("if (a)\n"
777 " f = g(foo, bar, baz);\n"
781 verifyFormat("if (foo)\n"
783 "else if (bar || baz)\n"
787 "} else if (bar || baz) {\n"
792 Style
.ColumnLimit
= 0;
793 verifyFormat("if (a)\n"
794 " b234567890223456789032345678904234567890 = "
795 "c234567890223456789032345678904234567890;",
797 " b234567890223456789032345678904234567890 = "
798 "c234567890223456789032345678904234567890;\n"
802 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
803 Style
.BraceWrapping
.AfterControlStatement
= FormatStyle::BWACS_Always
;
804 Style
.BraceWrapping
.BeforeElse
= true;
806 Style
.ColumnLimit
= 65;
808 verifyFormat("if (condition)\n"
811 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
816 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
820 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
823 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
827 verifyFormat("if (a)\n"
836 verifyFormat("if (a) // comment\n"
838 "if (a) // comment\n"
844 Style
.ColumnLimit
= 20;
846 verifyFormat("int ab = [](int i) {\n"
854 "int ab = [](int i) {\n"
863 verifyFormat("if (a)\n"
874 verifyFormat("if (a)\n"
880 " b = c >= 0 ? d : e;\n"
884 verifyFormat("if (a)\n"
885 " b = c > 0 ? d : e;",
888 " b = c > 0 ? d : e;\n"
892 verifyFormat("if (foo + bar <=\n"
895 " func(arg1, arg2);\n"
897 "if (foo + bar <= baz) {\n"
898 " func(arg1, arg2);\n"
902 verifyFormat("if (foo + bar < baz)\n"
903 " func(arg1, arg2);\n"
906 "if (foo + bar < baz)\n"
908 " func(arg1, arg2);\n"
916 verifyFormat("while (i--)\n"
920 "while (i--) <% // Keep.\n"
925 verifyFormat("for (int &i : chars)\n"
927 "for (int &i : chars)\n"
936 } // namespace format