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
|
|
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("if (a)\n"
|
|
208 " b; // comment\n"
|
|
209 "else if (c)\n"
|
|
210 " d; /* comment */\n"
|
|
211 "else\n"
|
|
212 " e;",
|
|
213 "if (a) {\n"
|
|
214 " b; // comment\n"
|
|
215 "} else if (c) {\n"
|
|
216 " d; /* comment */\n"
|
|
217 "} else {\n"
|
|
218 " e;\n"
|
|
219 "}",
|
|
220 Style);
|
|
221
|
|
222 verifyFormat("if (a) {\n"
|
|
223 " b;\n"
|
|
224 " c;\n"
|
|
225 "} else if (d) {\n"
|
|
226 " e;\n"
|
|
227 "}",
|
|
228 Style);
|
|
229
|
|
230 verifyFormat("if (a) {\n"
|
|
231 "#undef NDEBUG\n"
|
|
232 " b;\n"
|
|
233 "} else {\n"
|
|
234 " c;\n"
|
|
235 "}",
|
|
236 Style);
|
|
237
|
|
238 verifyFormat("if (a) {\n"
|
|
239 " // comment\n"
|
|
240 "} else if (b) {\n"
|
|
241 " c;\n"
|
|
242 "}",
|
|
243 Style);
|
|
244
|
|
245 verifyFormat("if (a) {\n"
|
|
246 " b;\n"
|
|
247 "} else {\n"
|
|
248 " { c; }\n"
|
|
249 "}",
|
|
250 Style);
|
|
251
|
|
252 verifyFormat("if (a) {\n"
|
|
253 " if (b) // comment\n"
|
|
254 " c;\n"
|
|
255 "} else if (d) {\n"
|
|
256 " e;\n"
|
|
257 "}",
|
|
258 "if (a) {\n"
|
|
259 " if (b) { // comment\n"
|
|
260 " c;\n"
|
|
261 " }\n"
|
|
262 "} else if (d) {\n"
|
|
263 " e;\n"
|
|
264 "}",
|
|
265 Style);
|
|
266
|
|
267 verifyFormat("if (a) {\n"
|
|
268 " if (b) {\n"
|
|
269 " c;\n"
|
|
270 " // comment\n"
|
|
271 " } else if (d) {\n"
|
|
272 " e;\n"
|
|
273 " }\n"
|
|
274 "}",
|
|
275 Style);
|
|
276
|
|
277 verifyFormat("if (a) {\n"
|
|
278 " if (b)\n"
|
|
279 " c;\n"
|
|
280 "}",
|
|
281 "if (a) {\n"
|
|
282 " if (b) {\n"
|
|
283 " c;\n"
|
|
284 " }\n"
|
|
285 "}",
|
|
286 Style);
|
|
287
|
|
288 verifyFormat("if (a)\n"
|
|
289 " if (b)\n"
|
|
290 " c;\n"
|
|
291 " else\n"
|
|
292 " d;\n"
|
|
293 "else\n"
|
|
294 " e;",
|
|
295 "if (a) {\n"
|
|
296 " if (b) {\n"
|
|
297 " c;\n"
|
|
298 " } else {\n"
|
|
299 " d;\n"
|
|
300 " }\n"
|
|
301 "} else {\n"
|
|
302 " e;\n"
|
|
303 "}",
|
|
304 Style);
|
|
305
|
|
306 verifyFormat("if (a) {\n"
|
|
307 " // comment\n"
|
|
308 " if (b)\n"
|
|
309 " c;\n"
|
|
310 " else if (d)\n"
|
|
311 " e;\n"
|
|
312 "} else {\n"
|
|
313 " g;\n"
|
|
314 "}",
|
|
315 "if (a) {\n"
|
|
316 " // comment\n"
|
|
317 " if (b) {\n"
|
|
318 " c;\n"
|
|
319 " } else if (d) {\n"
|
|
320 " e;\n"
|
|
321 " }\n"
|
|
322 "} else {\n"
|
|
323 " g;\n"
|
|
324 "}",
|
|
325 Style);
|
|
326
|
|
327 verifyFormat("if (a)\n"
|
|
328 " b;\n"
|
|
329 "else if (c)\n"
|
|
330 " d;\n"
|
|
331 "else\n"
|
|
332 " e;",
|
|
333 "if (a) {\n"
|
|
334 " b;\n"
|
|
335 "} else {\n"
|
|
336 " if (c) {\n"
|
|
337 " d;\n"
|
|
338 " } else {\n"
|
|
339 " e;\n"
|
|
340 " }\n"
|
|
341 "}",
|
|
342 Style);
|
|
343
|
|
344 verifyFormat("if (a) {\n"
|
|
345 " if (b)\n"
|
|
346 " c;\n"
|
|
347 " else if (d)\n"
|
|
348 " e;\n"
|
|
349 "} else {\n"
|
|
350 " g;\n"
|
|
351 "}",
|
|
352 "if (a) {\n"
|
|
353 " if (b)\n"
|
|
354 " c;\n"
|
|
355 " else {\n"
|
|
356 " if (d)\n"
|
|
357 " e;\n"
|
|
358 " }\n"
|
|
359 "} else {\n"
|
|
360 " g;\n"
|
|
361 "}",
|
|
362 Style);
|
|
363
|
|
364 verifyFormat("if (isa<VarDecl>(D)) {\n"
|
|
365 " for (auto *A : D.attrs())\n"
|
|
366 " if (shouldProcessAttr(A))\n"
|
|
367 " handleAttr(A);\n"
|
|
368 "}",
|
|
369 "if (isa<VarDecl>(D)) {\n"
|
|
370 " for (auto *A : D.attrs()) {\n"
|
|
371 " if (shouldProcessAttr(A)) {\n"
|
|
372 " handleAttr(A);\n"
|
|
373 " }\n"
|
|
374 " }\n"
|
|
375 "}",
|
|
376 Style);
|
|
377
|
|
378 verifyFormat("do {\n"
|
|
379 " ++I;\n"
|
|
380 "} while (hasMore() && Filter(*I));",
|
|
381 "do { ++I; } while (hasMore() && Filter(*I));", Style);
|
|
382
|
|
383 verifyFormat("if (a)\n"
|
|
384 " if (b)\n"
|
|
385 " c;\n"
|
|
386 " else {\n"
|
|
387 " if (d)\n"
|
|
388 " e;\n"
|
|
389 " }\n"
|
|
390 "else\n"
|
|
391 " f;",
|
|
392 Style);
|
|
393
|
|
394 verifyFormat("if (a)\n"
|
|
395 " if (b)\n"
|
|
396 " c;\n"
|
|
397 " else {\n"
|
|
398 " if (d)\n"
|
|
399 " e;\n"
|
|
400 " else if (f)\n"
|
|
401 " g;\n"
|
|
402 " }\n"
|
|
403 "else\n"
|
|
404 " h;",
|
|
405 Style);
|
|
406
|
|
407 verifyFormat("if (a) {\n"
|
|
408 " b;\n"
|
|
409 "} else if (c) {\n"
|
|
410 " d;\n"
|
|
411 " e;\n"
|
|
412 "}",
|
|
413 "if (a) {\n"
|
|
414 " b;\n"
|
|
415 "} else {\n"
|
|
416 " if (c) {\n"
|
|
417 " d;\n"
|
|
418 " e;\n"
|
|
419 " }\n"
|
|
420 "}",
|
|
421 Style);
|
|
422
|
|
423 verifyFormat("if (a) {\n"
|
|
424 " b;\n"
|
|
425 " c;\n"
|
|
426 "} else if (d) {\n"
|
|
427 " e;\n"
|
|
428 " f;\n"
|
|
429 "}",
|
|
430 "if (a) {\n"
|
|
431 " b;\n"
|
|
432 " c;\n"
|
|
433 "} else {\n"
|
|
434 " if (d) {\n"
|
|
435 " e;\n"
|
|
436 " f;\n"
|
|
437 " }\n"
|
|
438 "}",
|
|
439 Style);
|
|
440
|
|
441 verifyFormat("if (a) {\n"
|
|
442 " b;\n"
|
|
443 "} else if (c) {\n"
|
|
444 " d;\n"
|
|
445 "} else {\n"
|
|
446 " e;\n"
|
|
447 " f;\n"
|
|
448 "}",
|
|
449 "if (a) {\n"
|
|
450 " b;\n"
|
|
451 "} else {\n"
|
|
452 " if (c) {\n"
|
|
453 " d;\n"
|
|
454 " } else {\n"
|
|
455 " e;\n"
|
|
456 " f;\n"
|
|
457 " }\n"
|
|
458 "}",
|
|
459 Style);
|
|
460
|
|
461 verifyFormat("if (a) {\n"
|
|
462 " b;\n"
|
|
463 "} else if (c) {\n"
|
|
464 " d;\n"
|
|
465 "} else if (e) {\n"
|
|
466 " f;\n"
|
|
467 " g;\n"
|
|
468 "}",
|
|
469 "if (a) {\n"
|
|
470 " b;\n"
|
|
471 "} else {\n"
|
|
472 " if (c) {\n"
|
|
473 " d;\n"
|
|
474 " } else if (e) {\n"
|
|
475 " f;\n"
|
|
476 " g;\n"
|
|
477 " }\n"
|
|
478 "}",
|
|
479 Style);
|
|
480
|
|
481 verifyFormat("if (a) {\n"
|
|
482 " if (b)\n"
|
|
483 " c;\n"
|
|
484 " else if (d) {\n"
|
|
485 " e;\n"
|
|
486 " f;\n"
|
|
487 " }\n"
|
|
488 "} else {\n"
|
|
489 " g;\n"
|
|
490 "}",
|
|
491 "if (a) {\n"
|
|
492 " if (b)\n"
|
|
493 " c;\n"
|
|
494 " else {\n"
|
|
495 " if (d) {\n"
|
|
496 " e;\n"
|
|
497 " f;\n"
|
|
498 " }\n"
|
|
499 " }\n"
|
|
500 "} else {\n"
|
|
501 " g;\n"
|
|
502 "}",
|
|
503 Style);
|
|
504
|
|
505 verifyFormat("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;",
|
|
516 Style);
|
|
517
|
|
518 verifyFormat("if (a) {\n"
|
|
519 " b;\n"
|
|
520 " c;\n"
|
|
521 "} else { // comment\n"
|
|
522 " if (d) {\n"
|
|
523 " e;\n"
|
|
524 " f;\n"
|
|
525 " }\n"
|
|
526 "}",
|
|
527 Style);
|
|
528
|
|
529 verifyFormat("if (a)\n"
|
|
530 " b;\n"
|
|
531 "else if (c)\n"
|
|
532 " while (d)\n"
|
|
533 " e;\n"
|
|
534 "// comment",
|
|
535 "if (a)\n"
|
|
536 "{\n"
|
|
537 " b;\n"
|
|
538 "} else if (c) {\n"
|
|
539 " while (d) {\n"
|
|
540 " e;\n"
|
|
541 " }\n"
|
|
542 "}\n"
|
|
543 "// comment",
|
|
544 Style);
|
|
545
|
|
546 verifyFormat("if (a) {\n"
|
|
547 " b;\n"
|
|
548 "} else if (c) {\n"
|
|
549 " d;\n"
|
|
550 "} else {\n"
|
|
551 " e;\n"
|
|
552 " g;\n"
|
|
553 "}",
|
|
554 Style);
|
|
555
|
|
556 verifyFormat("if (a) {\n"
|
|
557 " b;\n"
|
|
558 "} else if (c) {\n"
|
|
559 " d;\n"
|
|
560 "} else {\n"
|
|
561 " e;\n"
|
|
562 "} // comment",
|
|
563 Style);
|
|
564
|
|
565 verifyFormat("int abs = [](int i) {\n"
|
|
566 " if (i >= 0)\n"
|
|
567 " return i;\n"
|
|
568 " return -i;\n"
|
|
569 "};",
|
|
570 "int abs = [](int i) {\n"
|
|
571 " if (i >= 0) {\n"
|
|
572 " return i;\n"
|
|
573 " }\n"
|
|
574 " return -i;\n"
|
|
575 "};",
|
|
576 Style);
|
|
577
|
|
578 verifyFormat("if (a)\n"
|
|
579 " foo();\n"
|
|
580 "else\n"
|
|
581 " bar();",
|
|
582 "if (a)\n"
|
|
583 "{\n"
|
|
584 " foo();\n"
|
|
585 "}\n"
|
|
586 "else\n"
|
|
587 "{\n"
|
|
588 " bar();\n"
|
|
589 "}",
|
|
590 Style);
|
|
591
|
|
592 verifyFormat("if (a)\n"
|
|
593 " foo();\n"
|
|
594 "// comment\n"
|
|
595 "else\n"
|
|
596 " bar();",
|
|
597 "if (a) {\n"
|
|
598 " foo();\n"
|
|
599 "}\n"
|
|
600 "// comment\n"
|
|
601 "else {\n"
|
|
602 " bar();\n"
|
|
603 "}",
|
|
604 Style);
|
|
605
|
|
606 verifyFormat("if (a) {\n"
|
|
607 " if (b)\n"
|
|
608 " c = 1; // comment\n"
|
|
609 "}",
|
|
610 "if (a) {\n"
|
|
611 " if (b) {\n"
|
|
612 " c = 1; // comment\n"
|
|
613 " }\n"
|
|
614 "}",
|
|
615 Style);
|
|
616
|
|
617 verifyFormat("if (a) // comment\n"
|
|
618 " b = 1;",
|
|
619 "if (a) // comment\n"
|
|
620 "{\n"
|
|
621 " b = 1;\n"
|
|
622 "}",
|
|
623 Style);
|
|
624
|
|
625 verifyFormat("if (a) {\n"
|
|
626 "Label:\n"
|
|
627 "}",
|
|
628 Style);
|
|
629
|
|
630 verifyFormat("if (a) {\n"
|
|
631 "Label:\n"
|
|
632 " f();\n"
|
|
633 "}",
|
|
634 Style);
|
|
635
|
|
636 verifyFormat("if (a) {\n"
|
|
637 " f();\n"
|
|
638 "Label:\n"
|
|
639 "}",
|
|
640 Style);
|
|
641
|
|
642 verifyFormat("if consteval {\n"
|
|
643 " f();\n"
|
|
644 "} else {\n"
|
|
645 " g();\n"
|
|
646 "}",
|
|
647 Style);
|
|
648
|
|
649 verifyFormat("if not consteval {\n"
|
|
650 " f();\n"
|
|
651 "} else if (a) {\n"
|
|
652 " g();\n"
|
|
653 "}",
|
|
654 Style);
|
|
655
|
|
656 verifyFormat("if !consteval {\n"
|
|
657 " g();\n"
|
|
658 "}",
|
|
659 Style);
|
|
660
|
|
661 verifyFormat("while (0)\n"
|
|
662 " if (a)\n"
|
|
663 " return b;\n"
|
|
664 "return a;",
|
|
665 "while (0) {\n"
|
|
666 " if (a) {\n"
|
|
667 " return b;\n"
|
|
668 "}}\n"
|
|
669 "return a;",
|
|
670 Style);
|
|
671
|
|
672 Style.ColumnLimit = 65;
|
|
673 verifyFormat("if (condition) {\n"
|
|
674 " ff(Indices,\n"
|
|
675 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
|
|
676 "} else {\n"
|
|
677 " ff(Indices,\n"
|
|
678 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
|
|
679 "}",
|
|
680 Style);
|
|
681
|
|
682 Style.ColumnLimit = 20;
|
|
683
|
|
684 verifyFormat("int i;\n"
|
|
685 "#define FOO(a, b) \\\n"
|
|
686 " while (a) { \\\n"
|
|
687 " b; \\\n"
|
|
688 " }",
|
|
689 Style);
|
|
690
|
|
691 verifyFormat("int ab = [](int i) {\n"
|
|
692 " if (i > 0) {\n"
|
|
693 " i = 12345678 -\n"
|
|
694 " i;\n"
|
|
695 " }\n"
|
|
696 " return i;\n"
|
|
697 "};",
|
|
698 Style);
|
|
699
|
|
700 verifyFormat("if (a) {\n"
|
|
701 " b = c + // 1 -\n"
|
|
702 " d;\n"
|
|
703 "}",
|
|
704 Style);
|
|
705
|
|
706 verifyFormat("if (a) {\n"
|
|
707 " b = c >= 0 ? d\n"
|
|
708 " : e;\n"
|
|
709 "}",
|
|
710 "if (a) {\n"
|
|
711 " b = c >= 0 ? d : e;\n"
|
|
712 "}",
|
|
713 Style);
|
|
714
|
|
715 verifyFormat("if (a)\n"
|
|
716 " b = c > 0 ? d : e;",
|
|
717 "if (a) {\n"
|
|
718 " b = c > 0 ? d : e;\n"
|
|
719 "}",
|
|
720 Style);
|
|
721
|
|
722 verifyFormat("if (-b >=\n"
|
|
723 " c) { // Keep.\n"
|
|
724 " foo();\n"
|
|
725 "} else {\n"
|
|
726 " bar();\n"
|
|
727 "}",
|
|
728 "if (-b >= c) { // Keep.\n"
|
|
729 " foo();\n"
|
|
730 "} else {\n"
|
|
731 " bar();\n"
|
|
732 "}",
|
|
733 Style);
|
|
734
|
|
735 verifyFormat("if (a) /* Remove. */\n"
|
|
736 " f();\n"
|
|
737 "else\n"
|
|
738 " g();",
|
|
739 "if (a) <% /* Remove. */\n"
|
|
740 " f();\n"
|
|
741 "%> else <%\n"
|
|
742 " g();\n"
|
|
743 "%>",
|
|
744 Style);
|
|
745
|
|
746 verifyFormat("while (\n"
|
|
747 " !i--) <% // Keep.\n"
|
|
748 " foo();\n"
|
|
749 "%>",
|
|
750 "while (!i--) <% // Keep.\n"
|
|
751 " foo();\n"
|
|
752 "%>",
|
|
753 Style);
|
|
754
|
|
755 verifyFormat("for (int &i : chars)\n"
|
|
756 " ++i;",
|
|
757 "for (int &i :\n"
|
|
758 " chars) {\n"
|
|
759 " ++i;\n"
|
|
760 "}",
|
|
761 Style);
|
|
762
|
|
763 verifyFormat("if (a)\n"
|
|
764 " b;\n"
|
|
765 "else if (c) {\n"
|
|
766 " d;\n"
|
|
767 " e;\n"
|
|
768 "} else\n"
|
|
769 " f = g(foo, bar,\n"
|
|
770 " baz);",
|
|
771 "if (a)\n"
|
|
772 " b;\n"
|
|
773 "else {\n"
|
|
774 " if (c) {\n"
|
|
775 " d;\n"
|
|
776 " e;\n"
|
|
777 " } else\n"
|
|
778 " f = g(foo, bar, baz);\n"
|
|
779 "}",
|
|
780 Style);
|
|
781
|
|
782 Style.ColumnLimit = 0;
|
|
783 verifyFormat("if (a)\n"
|
|
784 " b234567890223456789032345678904234567890 = "
|
|
785 "c234567890223456789032345678904234567890;",
|
|
786 "if (a) {\n"
|
|
787 " b234567890223456789032345678904234567890 = "
|
|
788 "c234567890223456789032345678904234567890;\n"
|
|
789 "}",
|
|
790 Style);
|
|
791
|
|
792 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
|
|
793 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
|
|
794 Style.BraceWrapping.BeforeElse = true;
|
|
795
|
|
796 Style.ColumnLimit = 65;
|
|
797
|
|
798 verifyFormat("if (condition)\n"
|
|
799 "{\n"
|
|
800 " ff(Indices,\n"
|
|
801 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
|
|
802 "}\n"
|
|
803 "else\n"
|
|
804 "{\n"
|
|
805 " ff(Indices,\n"
|
|
806 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
|
|
807 "}",
|
|
808 "if (condition) {\n"
|
|
809 " ff(Indices,\n"
|
|
810 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
|
|
811 "} else {\n"
|
|
812 " ff(Indices,\n"
|
|
813 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
|
|
814 "}",
|
|
815 Style);
|
|
816
|
|
817 verifyFormat("if (a)\n"
|
|
818 "{ //\n"
|
|
819 " foo();\n"
|
|
820 "}",
|
|
821 "if (a) { //\n"
|
|
822 " foo();\n"
|
|
823 "}",
|
|
824 Style);
|
|
825
|
|
826 verifyFormat("if (a) // comment\n"
|
|
827 " b = 1;",
|
|
828 "if (a) // comment\n"
|
|
829 "{\n"
|
|
830 " b = 1;\n"
|
|
831 "}",
|
|
832 Style);
|
|
833
|
|
834 Style.ColumnLimit = 20;
|
|
835
|
|
836 verifyFormat("int ab = [](int i) {\n"
|
|
837 " if (i > 0)\n"
|
|
838 " {\n"
|
|
839 " i = 12345678 -\n"
|
|
840 " i;\n"
|
|
841 " }\n"
|
|
842 " return i;\n"
|
|
843 "};",
|
|
844 "int ab = [](int i) {\n"
|
|
845 " if (i > 0) {\n"
|
|
846 " i = 12345678 -\n"
|
|
847 " i;\n"
|
|
848 " }\n"
|
|
849 " return i;\n"
|
|
850 "};",
|
|
851 Style);
|
|
852
|
|
853 verifyFormat("if (a)\n"
|
|
854 "{\n"
|
|
855 " b = c + // 1 -\n"
|
|
856 " d;\n"
|
|
857 "}",
|
|
858 "if (a) {\n"
|
|
859 " b = c + // 1 -\n"
|
|
860 " d;\n"
|
|
861 "}",
|
|
862 Style);
|
|
863
|
|
864 verifyFormat("if (a)\n"
|
|
865 "{\n"
|
|
866 " b = c >= 0 ? d\n"
|
|
867 " : e;\n"
|
|
868 "}",
|
|
869 "if (a) {\n"
|
|
870 " b = c >= 0 ? d : e;\n"
|
|
871 "}",
|
|
872 Style);
|
|
873
|
|
874 verifyFormat("if (a)\n"
|
|
875 " b = c > 0 ? d : e;",
|
|
876 "if (a)\n"
|
|
877 "{\n"
|
|
878 " b = c > 0 ? d : e;\n"
|
|
879 "}",
|
|
880 Style);
|
|
881
|
|
882 verifyFormat("if (foo + bar <=\n"
|
|
883 " baz)\n"
|
|
884 "{\n"
|
|
885 " func(arg1, arg2);\n"
|
|
886 "}",
|
|
887 "if (foo + bar <= baz) {\n"
|
|
888 " func(arg1, arg2);\n"
|
|
889 "}",
|
|
890 Style);
|
|
891
|
|
892 verifyFormat("if (foo + bar < baz)\n"
|
|
893 " func(arg1, arg2);\n"
|
|
894 "else\n"
|
|
895 " func();",
|
|
896 "if (foo + bar < baz)\n"
|
|
897 "<%\n"
|
|
898 " func(arg1, arg2);\n"
|
|
899 "%>\n"
|
|
900 "else\n"
|
|
901 "<%\n"
|
|
902 " func();\n"
|
|
903 "%>",
|
|
904 Style);
|
|
905
|
|
906 verifyFormat("while (i--)\n"
|
|
907 "<% // Keep.\n"
|
|
908 " foo();\n"
|
|
909 "%>",
|
|
910 "while (i--) <% // Keep.\n"
|
|
911 " foo();\n"
|
|
912 "%>",
|
|
913 Style);
|
|
914
|
|
915 verifyFormat("for (int &i : chars)\n"
|
|
916 " ++i;",
|
|
917 "for (int &i : chars)\n"
|
|
918 "{\n"
|
|
919 " ++i;\n"
|
|
920 "}",
|
|
921 Style);
|
|
922 }
|
|
923
|
|
924 } // namespace
|
|
925 } // namespace format
|
|
926 } // namespace clang
|