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"
218 verifyFormat("if (a) {\n"
219 " if (b) // comment\n"
225 " if (b) { // comment\n"
233 verifyFormat("if (a) {\n"
243 verifyFormat("if (a) {\n"
254 verifyFormat("if (a)\n"
272 verifyFormat("if (a) {\n"
293 verifyFormat("if (a)\n"
310 verifyFormat("if (a) {\n"
330 verifyFormat("if (isa<VarDecl>(D)) {\n"
331 " for (auto *A : D.attrs())\n"
332 " if (shouldProcessAttr(A))\n"
335 "if (isa<VarDecl>(D)) {\n"
336 " for (auto *A : D.attrs()) {\n"
337 " if (shouldProcessAttr(A)) {\n"
344 verifyFormat("do {\n"
346 "} while (hasMore() && Filter(*I));",
347 "do { ++I; } while (hasMore() && Filter(*I));", Style
);
349 verifyFormat("if (a)\n"
360 verifyFormat("if (a)\n"
373 verifyFormat("if (a) {\n"
389 verifyFormat("if (a) {\n"
407 verifyFormat("if (a) {\n"
427 verifyFormat("if (a) {\n"
447 verifyFormat("if (a) {\n"
471 verifyFormat("if (a)\n"
484 verifyFormat("if (a) {\n"
487 "} else { // comment\n"
495 verifyFormat("if (a)\n"
512 verifyFormat("if (a) {\n"
522 verifyFormat("if (a) {\n"
531 verifyFormat("int abs = [](int i) {\n"
536 "int abs = [](int i) {\n"
544 verifyFormat("if (a)\n"
558 verifyFormat("if (a)\n"
572 verifyFormat("if (a) {\n"
574 " c = 1; // comment\n"
578 " c = 1; // comment\n"
583 verifyFormat("if (a) // comment\n"
585 "if (a) // comment\n"
591 verifyFormat("if (a) {\n"
596 verifyFormat("if (a) {\n"
602 verifyFormat("if (a) {\n"
608 verifyFormat("if consteval {\n"
615 verifyFormat("if not consteval {\n"
622 verifyFormat("if !consteval {\n"
627 verifyFormat("while (0)\n"
638 verifyFormat("if (a)\n"
649 " bar = f;\n" // FIXME: should be indented 1 more level.
673 Style
.ColumnLimit
= 65;
674 verifyFormat("if (condition) {\n"
676 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
679 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
683 Style
.ColumnLimit
= 20;
685 verifyFormat("int i;\n"
686 "#define FOO(a, b) \\\n"
692 verifyFormat("int ab = [](int i) {\n"
701 verifyFormat("if (a) {\n"
707 verifyFormat("if (a) {\n"
712 " b = c >= 0 ? d : e;\n"
716 verifyFormat("if (a)\n"
717 " b = c > 0 ? d : e;",
719 " b = c > 0 ? d : e;\n"
723 verifyFormat("if (-b >=\n"
729 "if (-b >= c) { // Keep.\n"
736 verifyFormat("if (a) /* Remove. */\n"
740 "if (a) <% /* Remove. */\n"
747 verifyFormat("while (\n"
748 " !i--) <% // Keep.\n"
751 "while (!i--) <% // Keep.\n"
756 verifyFormat("for (int &i : chars)\n"
764 verifyFormat("if (a)\n"
779 " f = g(foo, bar, baz);\n"
783 verifyFormat("if (foo)\n"
785 "else if (bar || baz)\n"
789 "} else if (bar || baz) {\n"
794 Style
.ColumnLimit
= 0;
795 verifyFormat("if (a)\n"
796 " b234567890223456789032345678904234567890 = "
797 "c234567890223456789032345678904234567890;",
799 " b234567890223456789032345678904234567890 = "
800 "c234567890223456789032345678904234567890;\n"
804 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
805 Style
.BraceWrapping
.AfterControlStatement
= FormatStyle::BWACS_Always
;
806 Style
.BraceWrapping
.BeforeElse
= true;
808 Style
.ColumnLimit
= 65;
810 verifyFormat("if (condition)\n"
813 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
818 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
822 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
825 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
829 verifyFormat("if (a)\n"
838 verifyFormat("if (a) // comment\n"
840 "if (a) // comment\n"
846 Style
.ColumnLimit
= 20;
848 verifyFormat("int ab = [](int i) {\n"
856 "int ab = [](int i) {\n"
865 verifyFormat("if (a)\n"
876 verifyFormat("if (a)\n"
882 " b = c >= 0 ? d : e;\n"
886 verifyFormat("if (a)\n"
887 " b = c > 0 ? d : e;",
890 " b = c > 0 ? d : e;\n"
894 verifyFormat("if (foo + bar <=\n"
897 " func(arg1, arg2);\n"
899 "if (foo + bar <= baz) {\n"
900 " func(arg1, arg2);\n"
904 verifyFormat("if (foo + bar < baz)\n"
905 " func(arg1, arg2);\n"
908 "if (foo + bar < baz)\n"
910 " func(arg1, arg2);\n"
918 verifyFormat("while (i--)\n"
922 "while (i--) <% // Keep.\n"
927 verifyFormat("for (int &i : chars)\n"
929 "for (int &i : chars)\n"
938 } // namespace format