1 | //===- unittest/Format/BracesInserterTest.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 "FormatTestBase.h" |
10 | |
11 | #define DEBUG_TYPE "braces-inserter-test" |
12 | |
13 | namespace clang { |
14 | namespace format { |
15 | namespace test { |
16 | namespace { |
17 | |
18 | class BracesInserterTest : public FormatTestBase {}; |
19 | |
20 | TEST_F(BracesInserterTest, InsertBraces) { |
21 | FormatStyle Style = getLLVMStyle(); |
22 | Style.InsertBraces = true; |
23 | |
24 | verifyFormat("// clang-format off\n" |
25 | "// comment\n" |
26 | "if (a) f();\n" |
27 | "// clang-format on\n" |
28 | "if (b) {\n" |
29 | " g();\n" |
30 | "}" , |
31 | "// clang-format off\n" |
32 | "// comment\n" |
33 | "if (a) f();\n" |
34 | "// clang-format on\n" |
35 | "if (b) g();" , |
36 | Style); |
37 | |
38 | verifyFormat("if (a) {\n" |
39 | " switch (b) {\n" |
40 | " case 1:\n" |
41 | " c = 0;\n" |
42 | " break;\n" |
43 | " default:\n" |
44 | " c = 1;\n" |
45 | " }\n" |
46 | "}" , |
47 | "if (a)\n" |
48 | " switch (b) {\n" |
49 | " case 1:\n" |
50 | " c = 0;\n" |
51 | " break;\n" |
52 | " default:\n" |
53 | " c = 1;\n" |
54 | " }" , |
55 | Style); |
56 | |
57 | verifyFormat("for (auto node : nodes) {\n" |
58 | " if (node) {\n" |
59 | " break;\n" |
60 | " }\n" |
61 | "}" , |
62 | "for (auto node : nodes)\n" |
63 | " if (node)\n" |
64 | " break;" , |
65 | Style); |
66 | |
67 | verifyFormat("for (auto node : nodes) {\n" |
68 | " if (node)\n" |
69 | "}" , |
70 | "for (auto node : nodes)\n" |
71 | " if (node)" , |
72 | Style); |
73 | |
74 | verifyFormat("do {\n" |
75 | " --a;\n" |
76 | "} while (a);" , |
77 | "do\n" |
78 | " --a;\n" |
79 | "while (a);" , |
80 | Style); |
81 | |
82 | verifyFormat("if (i) {\n" |
83 | " ++i;\n" |
84 | "} else {\n" |
85 | " --i;\n" |
86 | "}" , |
87 | "if (i)\n" |
88 | " ++i;\n" |
89 | "else {\n" |
90 | " --i;\n" |
91 | "}" , |
92 | Style); |
93 | |
94 | verifyFormat("void f() {\n" |
95 | " while (j--) {\n" |
96 | " while (i) {\n" |
97 | " --i;\n" |
98 | " }\n" |
99 | " }\n" |
100 | "}" , |
101 | "void f() {\n" |
102 | " while (j--)\n" |
103 | " while (i)\n" |
104 | " --i;\n" |
105 | "}" , |
106 | Style); |
107 | |
108 | verifyFormat("f({\n" |
109 | " if (a) {\n" |
110 | " g();\n" |
111 | " }\n" |
112 | "});" , |
113 | "f({\n" |
114 | " if (a)\n" |
115 | " g();\n" |
116 | "});" , |
117 | Style); |
118 | |
119 | verifyFormat("if (a) {\n" |
120 | " f();\n" |
121 | "} else if (b) {\n" |
122 | " g();\n" |
123 | "} else {\n" |
124 | " h();\n" |
125 | "}" , |
126 | "if (a)\n" |
127 | " f();\n" |
128 | "else if (b)\n" |
129 | " g();\n" |
130 | "else\n" |
131 | " h();" , |
132 | Style); |
133 | |
134 | verifyFormat("if (a) {\n" |
135 | " f();\n" |
136 | "}\n" |
137 | "// comment\n" |
138 | "/* comment */" , |
139 | "if (a)\n" |
140 | " f();\n" |
141 | "// comment\n" |
142 | "/* comment */" , |
143 | Style); |
144 | |
145 | verifyFormat("if (a) {\n" |
146 | " // foo\n" |
147 | " // bar\n" |
148 | " f();\n" |
149 | "}" , |
150 | "if (a)\n" |
151 | " // foo\n" |
152 | " // bar\n" |
153 | " f();" , |
154 | Style); |
155 | |
156 | verifyFormat("if (a) { //\n" |
157 | " b = 1;\n" |
158 | "}" , |
159 | "if (a) //\n" |
160 | " b = 1;" , |
161 | Style); |
162 | |
163 | verifyFormat("if (a) { // comment\n" |
164 | " // comment\n" |
165 | " f();\n" |
166 | "}" , |
167 | "if (a) // comment\n" |
168 | " // comment\n" |
169 | " f();" , |
170 | Style); |
171 | |
172 | verifyFormat("if (a) {\n" |
173 | " f(); // comment\n" |
174 | "}" , |
175 | "if (a)\n" |
176 | " f(); // comment" , |
177 | Style); |
178 | |
179 | verifyFormat("if (a) {\n" |
180 | " f();\n" |
181 | "}\n" |
182 | "#undef A\n" |
183 | "#undef B" , |
184 | "if (a)\n" |
185 | " f();\n" |
186 | "#undef A\n" |
187 | "#undef B" , |
188 | Style); |
189 | |
190 | verifyFormat("if (a)\n" |
191 | "#ifdef A\n" |
192 | " f();\n" |
193 | "#else\n" |
194 | " g();\n" |
195 | "#endif" , |
196 | Style); |
197 | |
198 | verifyFormat("#if 0\n" |
199 | "#elif 1\n" |
200 | "#endif\n" |
201 | "void f() {\n" |
202 | " if (a) {\n" |
203 | " g();\n" |
204 | " }\n" |
205 | "}" , |
206 | "#if 0\n" |
207 | "#elif 1\n" |
208 | "#endif\n" |
209 | "void f() {\n" |
210 | " if (a) g();\n" |
211 | "}" , |
212 | Style); |
213 | |
214 | verifyFormat("do {\n" |
215 | "#if 0\n" |
216 | "#else\n" |
217 | " if (b) {\n" |
218 | "#endif\n" |
219 | " }\n" |
220 | "} while (0);" , |
221 | Style); |
222 | |
223 | Style.RemoveBracesLLVM = true; |
224 | verifyFormat("if (a) //\n" |
225 | " return b;" , |
226 | Style); |
227 | Style.RemoveBracesLLVM = false; |
228 | |
229 | Style.ColumnLimit = 15; |
230 | |
231 | verifyFormat("#define A \\\n" |
232 | " if (a) \\\n" |
233 | " f();" , |
234 | Style); |
235 | |
236 | verifyFormat("if (a + b >\n" |
237 | " c) {\n" |
238 | " f();\n" |
239 | "}" , |
240 | "if (a + b > c)\n" |
241 | " f();" , |
242 | Style); |
243 | |
244 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
245 | Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; |
246 | |
247 | verifyFormat("if (a) //\n" |
248 | "{\n" |
249 | " b = 1;\n" |
250 | "}" , |
251 | "if (a) //\n" |
252 | " b = 1;" , |
253 | Style); |
254 | } |
255 | |
256 | TEST_F(BracesInserterTest, InsertBracesRange) { |
257 | FormatStyle Style = getLLVMStyle(); |
258 | Style.InsertBraces = true; |
259 | |
260 | const StringRef Code("while (a)\n" |
261 | " if (b)\n" |
262 | " return;" ); |
263 | |
264 | verifyFormat("while (a) {\n" |
265 | " if (b)\n" |
266 | " return;\n" |
267 | "}" , |
268 | Code, Style, {tooling::Range(0, 9)}); // line 1 |
269 | |
270 | verifyFormat("while (a) {\n" |
271 | " if (b) {\n" |
272 | " return;\n" |
273 | " }\n" |
274 | "}" , |
275 | Code, Style, {tooling::Range(0, 18)}); // lines 1-2 |
276 | |
277 | verifyFormat("while (a)\n" |
278 | " if (b) {\n" |
279 | " return;\n" |
280 | " }" , |
281 | Code, Style, {tooling::Range(10, 8)}); // line 2 |
282 | |
283 | verifyFormat(Code, Code, Style, {tooling::Range(19, 11)}); // line 3 |
284 | } |
285 | |
286 | } // namespace |
287 | } // namespace test |
288 | } // namespace format |
289 | } // namespace clang |
290 | |