Debian Codegraph @

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

raw
1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
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 #include "llvm/Support/Debug.h"
15 #include "gtest/gtest.h"
16
17 #define DEBUG_TYPE "format-test"
18
19 using clang::tooling::ReplacementTest;
20 using clang::tooling::toReplacements;
21 using testing::ScopedTrace;
22
23 namespace clang {
24 namespace format {
25 namespace {
26
27 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
28
29 class FormatTest : public ::testing::Test {
30 protected:
31 enum StatusCheck { SC_ExpectComplete, SC_ExpectIncomplete, SC_DoNotCheck };
32
33 std::string format(llvm::StringRef Code,
34 const FormatStyle &Style = getLLVMStyle(),
35 StatusCheck CheckComplete = SC_ExpectComplete) {
36 LLVM_DEBUG(llvm::errs() << "---\n");
37 LLVM_DEBUG(llvm::errs() << Code << "\n\n");
38 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
39 FormattingAttemptStatus Status;
40 tooling::Replacements Replaces =
41 reformat(Style, Code, Ranges, "<stdin>", &Status);
42 if (CheckComplete != SC_DoNotCheck) {
43 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
44 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
45 << Code << "\n\n";
46 }
47 ReplacementCount = Replaces.size();
48 auto Result = applyAllReplacements(Code, Replaces);
49 EXPECT_TRUE(static_cast<bool>(Result));
50 LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
51 return *Result;
52 }
53
54 FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) {
55 Style.ColumnLimit = ColumnLimit;
56 return Style;
57 }
58
59 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
60 return getStyleWithColumns(getLLVMStyle(), ColumnLimit);
61 }
62
63 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
64 return getStyleWithColumns(getGoogleStyle(), ColumnLimit);
65 }
66
67 void _verifyFormat(const char *File, int Line, llvm::StringRef Expected,
68 llvm::StringRef Code,
69 const FormatStyle &Style = getLLVMStyle()) {
70 ScopedTrace t(File, Line, ::testing::Message() << Code.str());
71 EXPECT_EQ(Expected.str(), format(Expected, Style))
72 << "Expected code is not stable";
73 EXPECT_EQ(Expected.str(), format(Code, Style));
74 if (Style.Language == FormatStyle::LK_Cpp) {
75 // Objective-C++ is a superset of C++, so everything checked for C++
76 // needs to be checked for Objective-C++ as well.
77 FormatStyle ObjCStyle = Style;
78 ObjCStyle.Language = FormatStyle::LK_ObjC;
79 EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle));
80 }
81 }
82
83 void _verifyFormat(const char *File, int Line, llvm::StringRef Code,
84 const FormatStyle &Style = getLLVMStyle()) {
85 _verifyFormat(File, Line, Code, test::messUp(Code), Style);
86 }
87
88 void _verifyIncompleteFormat(const char *File, int Line, llvm::StringRef Code,
89 const FormatStyle &Style = getLLVMStyle()) {
90 ScopedTrace t(File, Line, ::testing::Message() << Code.str());
91 EXPECT_EQ(Code.str(),
92 format(test::messUp(Code), Style, SC_ExpectIncomplete));
93 }
94
95 void _verifyIndependentOfContext(const char *File, int Line,
96 llvm::StringRef Text,
97 const FormatStyle &Style = getLLVMStyle()) {
98 _verifyFormat(File, Line, Text, Style);
99 _verifyFormat(File, Line, llvm::Twine("void f() { " + Text + " }").str(),
100 Style);
101 }
102
103 /// \brief Verify that clang-format does not crash on the given input.
104 void verifyNoCrash(llvm::StringRef Code,
105 const FormatStyle &Style = getLLVMStyle()) {
106 format(Code, Style, SC_DoNotCheck);
107 }
108
109 int ReplacementCount;
110 };
111
112 #define verifyIndependentOfContext(...) \
113 _verifyIndependentOfContext(__FILE__, __LINE__, __VA_ARGS__)
114 #define verifyIncompleteFormat(...) \
115 _verifyIncompleteFormat(__FILE__, __LINE__, __VA_ARGS__)
116 #define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__)
117 #define verifyGoogleFormat(Code) verifyFormat(Code, getGoogleStyle())
118
119 TEST_F(FormatTest, MessUp) {
120 EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
121 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
122 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
123 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
124 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
125 }
126
127 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) {
128 EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language);
129 }
130
131 TEST_F(FormatTest, LLVMStyleOverride) {
132 EXPECT_EQ(FormatStyle::LK_Proto,
133 getLLVMStyle(FormatStyle::LK_Proto).Language);
134 }
135
136 //===----------------------------------------------------------------------===//
137 // Basic function tests.
138 //===----------------------------------------------------------------------===//
139
140 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
141 EXPECT_EQ(";", format(";"));
142 }
143
144 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
145 EXPECT_EQ("int i;", format(" int i;"));
146 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;"));
147 EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
148 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
149 }
150
151 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
152 EXPECT_EQ("int i;", format("int\ni;"));
153 }
154
155 TEST_F(FormatTest, FormatsNestedBlockStatements) {
156 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
157 }
158
159 TEST_F(FormatTest, FormatsNestedCall) {
160 verifyFormat("Method(f1, f2(f3));");
161 verifyFormat("Method(f1(f2, f3()));");
162 verifyFormat("Method(f1(f2, (f3())));");
163 }
164
165 TEST_F(FormatTest, NestedNameSpecifiers) {
166 verifyFormat("vector<::Type> v;");
167 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
168 verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
169 verifyFormat("static constexpr bool Bar = typeof(bar())::value;");
170 verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;");
171 verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;");
172 verifyFormat("bool a = 2 < ::SomeFunction();");
173 verifyFormat("ALWAYS_INLINE ::std::string getName();");
174 verifyFormat("some::string getName();");
175 }
176
177 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
178 EXPECT_EQ("if (a) {\n"
179 " f();\n"
180 "}",
181 format("if(a){f();}"));
182 EXPECT_EQ(4, ReplacementCount);
183 EXPECT_EQ("if (a) {\n"
184 " f();\n"
185 "}",
186 format("if (a) {\n"
187 " f();\n"
188 "}"));
189 EXPECT_EQ(0, ReplacementCount);
190 EXPECT_EQ("/*\r\n"
191 "\r\n"
192 "*/\r\n",
193 format("/*\r\n"
194 "\r\n"
195 "*/\r\n"));
196 EXPECT_EQ(0, ReplacementCount);
197 }
198
199 TEST_F(FormatTest, RemovesEmptyLines) {
200 EXPECT_EQ("class C {\n"
201 " int i;\n"
202 "};",
203 format("class C {\n"
204 " int i;\n"
205 "\n"
206 "};"));
207
208 // Don't remove empty lines at the start of namespaces or extern "C" blocks.
209 EXPECT_EQ("namespace N {\n"
210 "\n"
211 "int i;\n"
212 "}",
213 format("namespace N {\n"
214 "\n"
215 "int i;\n"
216 "}",
217 getGoogleStyle()));
218 EXPECT_EQ("/* something */ namespace N {\n"
219 "\n"
220 "int i;\n"
221 "}",
222 format("/* something */ namespace N {\n"
223 "\n"
224 "int i;\n"
225 "}",
226 getGoogleStyle()));
227 EXPECT_EQ("inline namespace N {\n"
228 "\n"
229 "int i;\n"
230 "}",
231 format("inline namespace N {\n"
232 "\n"
233 "int i;\n"
234 "}",
235 getGoogleStyle()));
236 EXPECT_EQ("/* something */ inline namespace N {\n"
237 "\n"
238 "int i;\n"
239 "}",
240 format("/* something */ inline namespace N {\n"
241 "\n"
242 "int i;\n"
243 "}",
244 getGoogleStyle()));
245 EXPECT_EQ("export namespace N {\n"
246 "\n"
247 "int i;\n"
248 "}",
249 format("export namespace N {\n"
250 "\n"
251 "int i;\n"
252 "}",
253 getGoogleStyle()));
254 EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
255 "\n"
256 "int i;\n"
257 "}",
258 format("extern /**/ \"C\" /**/ {\n"
259 "\n"
260 "int i;\n"
261 "}",
262 getGoogleStyle()));
263
264 auto CustomStyle = getLLVMStyle();
265 CustomStyle.BreakBeforeBraces = FormatStyle::BS_Custom;
266 CustomStyle.BraceWrapping.AfterNamespace = true;
267 CustomStyle.KeepEmptyLinesAtTheStartOfBlocks = false;
268 EXPECT_EQ("namespace N\n"
269 "{\n"
270 "\n"
271 "int i;\n"
272 "}",
273 format("namespace N\n"
274 "{\n"
275 "\n"
276 "\n"
277 "int i;\n"
278 "}",
279 CustomStyle));
280 EXPECT_EQ("/* something */ namespace N\n"
281 "{\n"
282 "\n"
283 "int i;\n"
284 "}",
285 format("/* something */ namespace N {\n"
286 "\n"
287 "\n"
288 "int i;\n"
289 "}",
290 CustomStyle));
291 EXPECT_EQ("inline namespace N\n"
292 "{\n"
293 "\n"
294 "int i;\n"
295 "}",
296 format("inline namespace N\n"
297 "{\n"
298 "\n"
299 "\n"
300 "int i;\n"
301 "}",
302 CustomStyle));
303 EXPECT_EQ("/* something */ inline namespace N\n"
304 "{\n"
305 "\n"
306 "int i;\n"
307 "}",
308 format("/* something */ inline namespace N\n"
309 "{\n"
310 "\n"
311 "int i;\n"
312 "}",
313 CustomStyle));
314 EXPECT_EQ("export namespace N\n"
315 "{\n"
316 "\n"
317 "int i;\n"
318 "}",
319 format("export namespace N\n"
320 "{\n"
321 "\n"
322 "int i;\n"
323 "}",
324 CustomStyle));
325 EXPECT_EQ("namespace a\n"
326 "{\n"
327 "namespace b\n"
328 "{\n"
329 "\n"
330 "class AA {};\n"
331 "\n"
332 "} // namespace b\n"
333 "} // namespace a\n",
334 format("namespace a\n"
335 "{\n"
336 "namespace b\n"
337 "{\n"
338 "\n"
339 "\n"
340 "class AA {};\n"
341 "\n"
342 "\n"
343 "}\n"
344 "}\n",
345 CustomStyle));
346 EXPECT_EQ("namespace A /* comment */\n"
347 "{\n"
348 "class B {}\n"
349 "} // namespace A",
350 format("namespace A /* comment */ { class B {} }", CustomStyle));
351 EXPECT_EQ("namespace A\n"
352 "{ /* comment */\n"
353 "class B {}\n"
354 "} // namespace A",
355 format("namespace A {/* comment */ class B {} }", CustomStyle));
356 EXPECT_EQ("namespace A\n"
357 "{ /* comment */\n"
358 "\n"
359 "class B {}\n"
360 "\n"
361 ""
362 "} // namespace A",
363 format("namespace A { /* comment */\n"
364 "\n"
365 "\n"
366 "class B {}\n"
367 "\n"
368 "\n"
369 "}",
370 CustomStyle));
371 EXPECT_EQ("namespace A /* comment */\n"
372 "{\n"
373 "\n"
374 "class B {}\n"
375 "\n"
376 "} // namespace A",
377 format("namespace A/* comment */ {\n"
378 "\n"
379 "\n"
380 "class B {}\n"
381 "\n"
382 "\n"
383 "}",
384 CustomStyle));
385
386 // ...but do keep inlining and removing empty lines for non-block extern "C"
387 // functions.
388 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
389 EXPECT_EQ("extern \"C\" int f() {\n"
390 " int i = 42;\n"
391 " return i;\n"
392 "}",
393 format("extern \"C\" int f() {\n"
394 "\n"
395 " int i = 42;\n"
396 " return i;\n"
397 "}",
398 getGoogleStyle()));
399
400 // Remove empty lines at the beginning and end of blocks.
401 EXPECT_EQ("void f() {\n"
402 "\n"
403 " if (a) {\n"
404 "\n"
405 " f();\n"
406 " }\n"
407 "}",
408 format("void f() {\n"
409 "\n"
410 " if (a) {\n"
411 "\n"
412 " f();\n"
413 "\n"
414 " }\n"
415 "\n"
416 "}",
417 getLLVMStyle()));
418 EXPECT_EQ("void f() {\n"
419 " if (a) {\n"
420 " f();\n"
421 " }\n"
422 "}",
423 format("void f() {\n"
424 "\n"
425 " if (a) {\n"
426 "\n"
427 " f();\n"
428 "\n"
429 " }\n"
430 "\n"
431 "}",
432 getGoogleStyle()));
433
434 // Don't remove empty lines in more complex control statements.
435 EXPECT_EQ("void f() {\n"
436 " if (a) {\n"
437 " f();\n"
438 "\n"
439 " } else if (b) {\n"
440 " f();\n"
441 " }\n"
442 "}",
443 format("void f() {\n"
444 " if (a) {\n"
445 " f();\n"
446 "\n"
447 " } else if (b) {\n"
448 " f();\n"
449 "\n"
450 " }\n"
451 "\n"
452 "}"));
453
454 // Don't remove empty lines before namespace endings.
455 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
456 LLVMWithNoNamespaceFix.FixNamespaceComments = false;
457 EXPECT_EQ("namespace {\n"
458 "int i;\n"
459 "\n"
460 "}",
461 format("namespace {\n"
462 "int i;\n"
463 "\n"
464 "}",
465 LLVMWithNoNamespaceFix));
466 EXPECT_EQ("namespace {\n"
467 "int i;\n"
468 "}",
469 format("namespace {\n"
470 "int i;\n"
471 "}",
472 LLVMWithNoNamespaceFix));
473 EXPECT_EQ("namespace {\n"
474 "int i;\n"
475 "\n"
476 "};",
477 format("namespace {\n"
478 "int i;\n"
479 "\n"
480 "};",
481 LLVMWithNoNamespaceFix));
482 EXPECT_EQ("namespace {\n"
483 "int i;\n"
484 "};",
485 format("namespace {\n"
486 "int i;\n"
487 "};",
488 LLVMWithNoNamespaceFix));
489 EXPECT_EQ("namespace {\n"
490 "int i;\n"
491 "\n"
492 "}",
493 format("namespace {\n"
494 "int i;\n"
495 "\n"
496 "}"));
497 EXPECT_EQ("namespace {\n"
498 "int i;\n"
499 "\n"
500 "} // namespace",
501 format("namespace {\n"
502 "int i;\n"
503 "\n"
504 "} // namespace"));
505
506 FormatStyle Style = getLLVMStyle();
507 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
508 Style.MaxEmptyLinesToKeep = 2;
509 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
510 Style.BraceWrapping.AfterClass = true;
511 Style.BraceWrapping.AfterFunction = true;
512 Style.KeepEmptyLinesAtTheStartOfBlocks = false;
513
514 EXPECT_EQ("class Foo\n"
515 "{\n"
516 " Foo() {}\n"
517 "\n"
518 " void funk() {}\n"
519 "};",
520 format("class Foo\n"
521 "{\n"
522 " Foo()\n"
523 " {\n"
524 " }\n"
525 "\n"
526 " void funk() {}\n"
527 "};",
528 Style));
529 }
530
531 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
532 verifyFormat("x = (a) and (b);");
533 verifyFormat("x = (a) or (b);");
534 verifyFormat("x = (a) bitand (b);");
535 verifyFormat("x = (a) bitor (b);");
536 verifyFormat("x = (a) not_eq (b);");
537 verifyFormat("x = (a) and_eq (b);");
538 verifyFormat("x = (a) or_eq (b);");
539 verifyFormat("x = (a) xor (b);");
540 }
541
542 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) {
543 verifyFormat("x = compl(a);");
544 verifyFormat("x = not(a);");
545 verifyFormat("x = bitand(a);");
546 // Unary operator must not be merged with the next identifier
547 verifyFormat("x = compl a;");
548 verifyFormat("x = not a;");
549 verifyFormat("x = bitand a;");
550 }
551
552 //===----------------------------------------------------------------------===//
553 // Tests for control statements.
554 //===----------------------------------------------------------------------===//
555
556 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
557 verifyFormat("if (true)\n f();\ng();");
558 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
559 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
560 verifyFormat("if constexpr (true)\n"
561 " f();\ng();");
562 verifyFormat("if CONSTEXPR (true)\n"
563 " f();\ng();");
564 verifyFormat("if constexpr (a)\n"
565 " if constexpr (b)\n"
566 " if constexpr (c)\n"
567 " g();\n"
568 "h();");
569 verifyFormat("if CONSTEXPR (a)\n"
570 " if CONSTEXPR (b)\n"
571 " if CONSTEXPR (c)\n"
572 " g();\n"
573 "h();");
574 verifyFormat("if constexpr (a)\n"
575 " if constexpr (b) {\n"
576 " f();\n"
577 " }\n"
578 "g();");
579 verifyFormat("if CONSTEXPR (a)\n"
580 " if CONSTEXPR (b) {\n"
581 " f();\n"
582 " }\n"
583 "g();");
584
585 verifyFormat("if consteval {\n}");
586 verifyFormat("if !consteval {\n}");
587 verifyFormat("if not consteval {\n}");
588 verifyFormat("if consteval {\n} else {\n}");
589 verifyFormat("if !consteval {\n} else {\n}");
590 verifyFormat("if consteval {\n"
591 " f();\n"
592 "}");
593 verifyFormat("if !consteval {\n"
594 " f();\n"
595 "}");
596 verifyFormat("if consteval {\n"
597 " f();\n"
598 "} else {\n"
599 " g();\n"
600 "}");
601 verifyFormat("if CONSTEVAL {\n"
602 " f();\n"
603 "}");
604 verifyFormat("if !CONSTEVAL {\n"
605 " f();\n"
606 "}");
607
608 verifyFormat("if (a)\n"
609 " g();");
610 verifyFormat("if (a) {\n"
611 " g()\n"
612 "};");
613 verifyFormat("if (a)\n"
614 " g();\n"
615 "else\n"
616 " g();");
617 verifyFormat("if (a) {\n"
618 " g();\n"
619 "} else\n"
620 " g();");
621 verifyFormat("if (a)\n"
622 " g();\n"
623 "else {\n"
624 " g();\n"
625 "}");
626 verifyFormat("if (a) {\n"
627 " g();\n"
628 "} else {\n"
629 " g();\n"
630 "}");
631 verifyFormat("if (a)\n"
632 " g();\n"
633 "else if (b)\n"
634 " g();\n"
635 "else\n"
636 " g();");
637 verifyFormat("if (a) {\n"
638 " g();\n"
639 "} else if (b)\n"
640 " g();\n"
641 "else\n"
642 " g();");
643 verifyFormat("if (a)\n"
644 " g();\n"
645 "else if (b) {\n"
646 " g();\n"
647 "} else\n"
648 " g();");
649 verifyFormat("if (a)\n"
650 " g();\n"
651 "else if (b)\n"
652 " g();\n"
653 "else {\n"
654 " g();\n"
655 "}");
656 verifyFormat("if (a)\n"
657 " g();\n"
658 "else if (b) {\n"
659 " g();\n"
660 "} else {\n"
661 " g();\n"
662 "}");
663 verifyFormat("if (a) {\n"
664 " g();\n"
665 "} else if (b) {\n"
666 " g();\n"
667 "} else {\n"
668 " g();\n"
669 "}");
670
671 FormatStyle AllowsMergedIf = getLLVMStyle();
672 AllowsMergedIf.IfMacros.push_back("MYIF");
673 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
674 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
675 FormatStyle::SIS_WithoutElse;
676 verifyFormat("if (a)\n"
677 " // comment\n"
678 " f();",
679 AllowsMergedIf);
680 verifyFormat("{\n"
681 " if (a)\n"
682 " label:\n"
683 " f();\n"
684 "}",
685 AllowsMergedIf);
686 verifyFormat("#define A \\\n"
687 " if (a) \\\n"
688 " label: \\\n"
689 " f()",
690 AllowsMergedIf);
691 verifyFormat("if (a)\n"
692 " ;",
693 AllowsMergedIf);
694 verifyFormat("if (a)\n"
695 " if (b) return;",
696 AllowsMergedIf);
697
698 verifyFormat("if (a) // Can't merge this\n"
699 " f();\n",
700 AllowsMergedIf);
701 verifyFormat("if (a) /* still don't merge */\n"
702 " f();",
703 AllowsMergedIf);
704 verifyFormat("if (a) { // Never merge this\n"
705 " f();\n"
706 "}",
707 AllowsMergedIf);
708 verifyFormat("if (a) { /* Never merge this */\n"
709 " f();\n"
710 "}",
711 AllowsMergedIf);
712 verifyFormat("MYIF (a)\n"
713 " // comment\n"
714 " f();",
715 AllowsMergedIf);
716 verifyFormat("{\n"
717 " MYIF (a)\n"
718 " label:\n"
719 " f();\n"
720 "}",
721 AllowsMergedIf);
722 verifyFormat("#define A \\\n"
723 " MYIF (a) \\\n"
724 " label: \\\n"
725 " f()",
726 AllowsMergedIf);
727 verifyFormat("MYIF (a)\n"
728 " ;",
729 AllowsMergedIf);
730 verifyFormat("MYIF (a)\n"
731 " MYIF (b) return;",
732 AllowsMergedIf);
733
734 verifyFormat("MYIF (a) // Can't merge this\n"
735 " f();\n",
736 AllowsMergedIf);
737 verifyFormat("MYIF (a) /* still don't merge */\n"
738 " f();",
739 AllowsMergedIf);
740 verifyFormat("MYIF (a) { // Never merge this\n"
741 " f();\n"
742 "}",
743 AllowsMergedIf);
744 verifyFormat("MYIF (a) { /* Never merge this */\n"
745 " f();\n"
746 "}",
747 AllowsMergedIf);
748
749 AllowsMergedIf.ColumnLimit = 14;
750 // Where line-lengths matter, a 2-letter synonym that maintains line length.
751 // Not IF to avoid any confusion that IF is somehow special.
752 AllowsMergedIf.IfMacros.push_back("FI");
753 verifyFormat("if (a) return;", AllowsMergedIf);
754 verifyFormat("if (aaaaaaaaa)\n"
755 " return;",
756 AllowsMergedIf);
757 verifyFormat("FI (a) return;", AllowsMergedIf);
758 verifyFormat("FI (aaaaaaaaa)\n"
759 " return;",
760 AllowsMergedIf);
761
762 AllowsMergedIf.ColumnLimit = 13;
763 verifyFormat("if (a)\n return;", AllowsMergedIf);
764 verifyFormat("FI (a)\n return;", AllowsMergedIf);
765
766 FormatStyle AllowsMergedIfElse = getLLVMStyle();
767 AllowsMergedIfElse.IfMacros.push_back("MYIF");
768 AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine =
769 FormatStyle::SIS_AllIfsAndElse;
770 verifyFormat("if (a)\n"
771 " // comment\n"
772 " f();\n"
773 "else\n"
774 " // comment\n"
775 " f();",
776 AllowsMergedIfElse);
777 verifyFormat("{\n"
778 " if (a)\n"
779 " label:\n"
780 " f();\n"
781 " else\n"
782 " label:\n"
783 " f();\n"
784 "}",
785 AllowsMergedIfElse);
786 verifyFormat("if (a)\n"
787 " ;\n"
788 "else\n"
789 " ;",
790 AllowsMergedIfElse);
791 verifyFormat("if (a) {\n"
792 "} else {\n"
793 "}",
794 AllowsMergedIfElse);
795 verifyFormat("if (a) return;\n"
796 "else if (b) return;\n"
797 "else return;",
798 AllowsMergedIfElse);
799 verifyFormat("if (a) {\n"
800 "} else return;",
801 AllowsMergedIfElse);
802 verifyFormat("if (a) {\n"
803 "} else if (b) return;\n"
804 "else return;",
805 AllowsMergedIfElse);
806 verifyFormat("if (a) return;\n"
807 "else if (b) {\n"
808 "} else return;",
809 AllowsMergedIfElse);
810 verifyFormat("if (a)\n"
811 " if (b) return;\n"
812 " else return;",
813 AllowsMergedIfElse);
814 verifyFormat("if constexpr (a)\n"
815 " if constexpr (b) return;\n"
816 " else if constexpr (c) return;\n"
817 " else return;",
818 AllowsMergedIfElse);
819 verifyFormat("MYIF (a)\n"
820 " // comment\n"
821 " f();\n"
822 "else\n"
823 " // comment\n"
824 " f();",
825 AllowsMergedIfElse);
826 verifyFormat("{\n"
827 " MYIF (a)\n"
828 " label:\n"
829 " f();\n"
830 " else\n"
831 " label:\n"
832 " f();\n"
833 "}",
834 AllowsMergedIfElse);
835 verifyFormat("MYIF (a)\n"
836 " ;\n"
837 "else\n"
838 " ;",
839 AllowsMergedIfElse);
840 verifyFormat("MYIF (a) {\n"
841 "} else {\n"
842 "}",
843 AllowsMergedIfElse);
844 verifyFormat("MYIF (a) return;\n"
845 "else MYIF (b) return;\n"
846 "else return;",
847 AllowsMergedIfElse);
848 verifyFormat("MYIF (a) {\n"
849 "} else return;",
850 AllowsMergedIfElse);
851 verifyFormat("MYIF (a) {\n"
852 "} else MYIF (b) return;\n"
853 "else return;",
854 AllowsMergedIfElse);
855 verifyFormat("MYIF (a) return;\n"
856 "else MYIF (b) {\n"
857 "} else return;",
858 AllowsMergedIfElse);
859 verifyFormat("MYIF (a)\n"
860 " MYIF (b) return;\n"
861 " else return;",
862 AllowsMergedIfElse);
863 verifyFormat("MYIF constexpr (a)\n"
864 " MYIF constexpr (b) return;\n"
865 " else MYIF constexpr (c) return;\n"
866 " else return;",
867 AllowsMergedIfElse);
868 }
869
870 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) {
871 FormatStyle AllowsMergedIf = getLLVMStyle();
872 AllowsMergedIf.IfMacros.push_back("MYIF");
873 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
874 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
875 FormatStyle::SIS_WithoutElse;
876 verifyFormat("if (a)\n"
877 " f();\n"
878 "else {\n"
879 " g();\n"
880 "}",
881 AllowsMergedIf);
882 verifyFormat("if (a)\n"
883 " f();\n"
884 "else\n"
885 " g();\n",
886 AllowsMergedIf);
887
888 verifyFormat("if (a) g();", AllowsMergedIf);
889 verifyFormat("if (a) {\n"
890 " g()\n"
891 "};",
892 AllowsMergedIf);
893 verifyFormat("if (a)\n"
894 " g();\n"
895 "else\n"
896 " g();",
897 AllowsMergedIf);
898 verifyFormat("if (a) {\n"
899 " g();\n"
900 "} else\n"
901 " g();",
902 AllowsMergedIf);
903 verifyFormat("if (a)\n"
904 " g();\n"
905 "else {\n"
906 " g();\n"
907 "}",
908 AllowsMergedIf);
909 verifyFormat("if (a) {\n"
910 " g();\n"
911 "} else {\n"
912 " g();\n"
913 "}",
914 AllowsMergedIf);
915 verifyFormat("if (a)\n"
916 " g();\n"
917 "else if (b)\n"
918 " g();\n"
919 "else\n"
920 " g();",
921 AllowsMergedIf);
922 verifyFormat("if (a) {\n"
923 " g();\n"
924 "} else if (b)\n"
925 " g();\n"
926 "else\n"
927 " g();",
928 AllowsMergedIf);
929 verifyFormat("if (a)\n"
930 " g();\n"
931 "else if (b) {\n"
932 " g();\n"
933 "} else\n"
934 " g();",
935 AllowsMergedIf);
936 verifyFormat("if (a)\n"
937 " g();\n"
938 "else if (b)\n"
939 " g();\n"
940 "else {\n"
941 " g();\n"
942 "}",
943 AllowsMergedIf);
944 verifyFormat("if (a)\n"
945 " g();\n"
946 "else if (b) {\n"
947 " g();\n"
948 "} else {\n"
949 " g();\n"
950 "}",
951 AllowsMergedIf);
952 verifyFormat("if (a) {\n"
953 " g();\n"
954 "} else if (b) {\n"
955 " g();\n"
956 "} else {\n"
957 " g();\n"
958 "}",
959 AllowsMergedIf);
960 verifyFormat("MYIF (a)\n"
961 " f();\n"
962 "else {\n"
963 " g();\n"
964 "}",
965 AllowsMergedIf);
966 verifyFormat("MYIF (a)\n"
967 " f();\n"
968 "else\n"
969 " g();\n",
970 AllowsMergedIf);
971
972 verifyFormat("MYIF (a) g();", AllowsMergedIf);
973 verifyFormat("MYIF (a) {\n"
974 " g()\n"
975 "};",
976 AllowsMergedIf);
977 verifyFormat("MYIF (a)\n"
978 " g();\n"
979 "else\n"
980 " g();",
981 AllowsMergedIf);
982 verifyFormat("MYIF (a) {\n"
983 " g();\n"
984 "} else\n"
985 " g();",
986 AllowsMergedIf);
987 verifyFormat("MYIF (a)\n"
988 " g();\n"
989 "else {\n"
990 " g();\n"
991 "}",
992 AllowsMergedIf);
993 verifyFormat("MYIF (a) {\n"
994 " g();\n"
995 "} else {\n"
996 " g();\n"
997 "}",
998 AllowsMergedIf);
999 verifyFormat("MYIF (a)\n"
1000 " g();\n"
1001 "else MYIF (b)\n"
1002 " g();\n"
1003 "else\n"
1004 " g();",
1005 AllowsMergedIf);
1006 verifyFormat("MYIF (a)\n"
1007 " g();\n"
1008 "else if (b)\n"
1009 " g();\n"
1010 "else\n"
1011 " g();",
1012 AllowsMergedIf);
1013 verifyFormat("MYIF (a) {\n"
1014 " g();\n"
1015 "} else MYIF (b)\n"
1016 " g();\n"
1017 "else\n"
1018 " g();",
1019 AllowsMergedIf);
1020 verifyFormat("MYIF (a) {\n"
1021 " g();\n"
1022 "} else if (b)\n"
1023 " g();\n"
1024 "else\n"
1025 " g();",
1026 AllowsMergedIf);
1027 verifyFormat("MYIF (a)\n"
1028 " g();\n"
1029 "else MYIF (b) {\n"
1030 " g();\n"
1031 "} else\n"
1032 " g();",
1033 AllowsMergedIf);
1034 verifyFormat("MYIF (a)\n"
1035 " g();\n"
1036 "else if (b) {\n"
1037 " g();\n"
1038 "} else\n"
1039 " g();",
1040 AllowsMergedIf);
1041 verifyFormat("MYIF (a)\n"
1042 " g();\n"
1043 "else MYIF (b)\n"
1044 " g();\n"
1045 "else {\n"
1046 " g();\n"
1047 "}",
1048 AllowsMergedIf);
1049 verifyFormat("MYIF (a)\n"
1050 " g();\n"
1051 "else if (b)\n"
1052 " g();\n"
1053 "else {\n"
1054 " g();\n"
1055 "}",
1056 AllowsMergedIf);
1057 verifyFormat("MYIF (a)\n"
1058 " g();\n"
1059 "else MYIF (b) {\n"
1060 " g();\n"
1061 "} else {\n"
1062 " g();\n"
1063 "}",
1064 AllowsMergedIf);
1065 verifyFormat("MYIF (a)\n"
1066 " g();\n"
1067 "else if (b) {\n"
1068 " g();\n"
1069 "} else {\n"
1070 " g();\n"
1071 "}",
1072 AllowsMergedIf);
1073 verifyFormat("MYIF (a) {\n"
1074 " g();\n"
1075 "} else MYIF (b) {\n"
1076 " g();\n"
1077 "} else {\n"
1078 " g();\n"
1079 "}",
1080 AllowsMergedIf);
1081 verifyFormat("MYIF (a) {\n"
1082 " g();\n"
1083 "} else if (b) {\n"
1084 " g();\n"
1085 "} else {\n"
1086 " g();\n"
1087 "}",
1088 AllowsMergedIf);
1089
1090 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
1091 FormatStyle::SIS_OnlyFirstIf;
1092
1093 verifyFormat("if (a) f();\n"
1094 "else {\n"
1095 " g();\n"
1096 "}",
1097 AllowsMergedIf);
1098 verifyFormat("if (a) f();\n"
1099 "else {\n"
1100 " if (a) f();\n"
1101 " else {\n"
1102 " g();\n"
1103 " }\n"
1104 " g();\n"
1105 "}",
1106 AllowsMergedIf);
1107
1108 verifyFormat("if (a) g();", AllowsMergedIf);
1109 verifyFormat("if (a) {\n"
1110 " g()\n"
1111 "};",
1112 AllowsMergedIf);
1113 verifyFormat("if (a) g();\n"
1114 "else\n"
1115 " g();",
1116 AllowsMergedIf);
1117 verifyFormat("if (a) {\n"
1118 " g();\n"
1119 "} else\n"
1120 " g();",
1121 AllowsMergedIf);
1122 verifyFormat("if (a) g();\n"
1123 "else {\n"
1124 " g();\n"
1125 "}",
1126 AllowsMergedIf);
1127 verifyFormat("if (a) {\n"
1128 " g();\n"
1129 "} else {\n"
1130 " g();\n"
1131 "}",
1132 AllowsMergedIf);
1133 verifyFormat("if (a) g();\n"
1134 "else if (b)\n"
1135 " g();\n"
1136 "else\n"
1137 " g();",
1138 AllowsMergedIf);
1139 verifyFormat("if (a) {\n"
1140 " g();\n"
1141 "} else if (b)\n"
1142 " g();\n"
1143 "else\n"
1144 " g();",
1145 AllowsMergedIf);
1146 verifyFormat("if (a) g();\n"
1147 "else if (b) {\n"
1148 " g();\n"
1149 "} else\n"
1150 " g();",
1151 AllowsMergedIf);
1152 verifyFormat("if (a) g();\n"
1153 "else if (b)\n"
1154 " g();\n"
1155 "else {\n"
1156 " g();\n"
1157 "}",
1158 AllowsMergedIf);
1159 verifyFormat("if (a) g();\n"
1160 "else if (b) {\n"
1161 " g();\n"
1162 "} else {\n"
1163 " g();\n"
1164 "}",
1165 AllowsMergedIf);
1166 verifyFormat("if (a) {\n"
1167 " g();\n"
1168 "} else if (b) {\n"
1169 " g();\n"
1170 "} else {\n"
1171 " g();\n"
1172 "}",
1173 AllowsMergedIf);
1174 verifyFormat("MYIF (a) f();\n"
1175 "else {\n"
1176 " g();\n"
1177 "}",
1178 AllowsMergedIf);
1179 verifyFormat("MYIF (a) f();\n"
1180 "else {\n"
1181 " if (a) f();\n"
1182 " else {\n"
1183 " g();\n"
1184 " }\n"
1185 " g();\n"
1186 "}",
1187 AllowsMergedIf);
1188
1189 verifyFormat("MYIF (a) g();", AllowsMergedIf);
1190 verifyFormat("MYIF (a) {\n"
1191 " g()\n"
1192 "};",
1193 AllowsMergedIf);
1194 verifyFormat("MYIF (a) g();\n"
1195 "else\n"
1196 " g();",
1197 AllowsMergedIf);
1198 verifyFormat("MYIF (a) {\n"
1199 " g();\n"
1200 "} else\n"
1201 " g();",
1202 AllowsMergedIf);
1203 verifyFormat("MYIF (a) g();\n"
1204 "else {\n"
1205 " g();\n"
1206 "}",
1207 AllowsMergedIf);
1208 verifyFormat("MYIF (a) {\n"
1209 " g();\n"
1210 "} else {\n"
1211 " g();\n"
1212 "}",
1213 AllowsMergedIf);
1214 verifyFormat("MYIF (a) g();\n"
1215 "else MYIF (b)\n"
1216 " g();\n"
1217 "else\n"
1218 " g();",
1219 AllowsMergedIf);
1220 verifyFormat("MYIF (a) g();\n"
1221 "else if (b)\n"
1222 " g();\n"
1223 "else\n"
1224 " g();",
1225 AllowsMergedIf);
1226 verifyFormat("MYIF (a) {\n"
1227 " g();\n"
1228 "} else MYIF (b)\n"
1229 " g();\n"
1230 "else\n"
1231 " g();",
1232 AllowsMergedIf);
1233 verifyFormat("MYIF (a) {\n"
1234 " g();\n"
1235 "} else if (b)\n"
1236 " g();\n"
1237 "else\n"
1238 " g();",
1239 AllowsMergedIf);
1240 verifyFormat("MYIF (a) g();\n"
1241 "else MYIF (b) {\n"
1242 " g();\n"
1243 "} else\n"
1244 " g();",
1245 AllowsMergedIf);
1246 verifyFormat("MYIF (a) g();\n"
1247 "else if (b) {\n"
1248 " g();\n"
1249 "} else\n"
1250 " g();",
1251 AllowsMergedIf);
1252 verifyFormat("MYIF (a) g();\n"
1253 "else MYIF (b)\n"
1254 " g();\n"
1255 "else {\n"
1256 " g();\n"
1257 "}",
1258 AllowsMergedIf);
1259 verifyFormat("MYIF (a) g();\n"
1260 "else if (b)\n"
1261 " g();\n"
1262 "else {\n"
1263 " g();\n"
1264 "}",
1265 AllowsMergedIf);
1266 verifyFormat("MYIF (a) g();\n"
1267 "else MYIF (b) {\n"
1268 " g();\n"
1269 "} else {\n"
1270 " g();\n"
1271 "}",
1272 AllowsMergedIf);
1273 verifyFormat("MYIF (a) g();\n"
1274 "else if (b) {\n"
1275 " g();\n"
1276 "} else {\n"
1277 " g();\n"
1278 "}",
1279 AllowsMergedIf);
1280 verifyFormat("MYIF (a) {\n"
1281 " g();\n"
1282 "} else MYIF (b) {\n"
1283 " g();\n"
1284 "} else {\n"
1285 " g();\n"
1286 "}",
1287 AllowsMergedIf);
1288 verifyFormat("MYIF (a) {\n"
1289 " g();\n"
1290 "} else if (b) {\n"
1291 " g();\n"
1292 "} else {\n"
1293 " g();\n"
1294 "}",
1295 AllowsMergedIf);
1296
1297 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
1298 FormatStyle::SIS_AllIfsAndElse;
1299
1300 verifyFormat("if (a) f();\n"
1301 "else {\n"
1302 " g();\n"
1303 "}",
1304 AllowsMergedIf);
1305 verifyFormat("if (a) f();\n"
1306 "else {\n"
1307 " if (a) f();\n"
1308 " else {\n"
1309 " g();\n"
1310 " }\n"
1311 " g();\n"
1312 "}",
1313 AllowsMergedIf);
1314
1315 verifyFormat("if (a) g();", AllowsMergedIf);
1316 verifyFormat("if (a) {\n"
1317 " g()\n"
1318 "};",
1319 AllowsMergedIf);
1320 verifyFormat("if (a) g();\n"
1321 "else g();",
1322 AllowsMergedIf);
1323 verifyFormat("if (a) {\n"
1324 " g();\n"
1325 "} else g();",
1326 AllowsMergedIf);
1327 verifyFormat("if (a) g();\n"
1328 "else {\n"
1329 " g();\n"
1330 "}",
1331 AllowsMergedIf);
1332 verifyFormat("if (a) {\n"
1333 " g();\n"
1334 "} else {\n"
1335 " g();\n"
1336 "}",
1337 AllowsMergedIf);
1338 verifyFormat("if (a) g();\n"
1339 "else if (b) g();\n"
1340 "else g();",
1341 AllowsMergedIf);
1342 verifyFormat("if (a) {\n"
1343 " g();\n"
1344 "} else if (b) g();\n"
1345 "else g();",
1346 AllowsMergedIf);
1347 verifyFormat("if (a) g();\n"
1348 "else if (b) {\n"
1349 " g();\n"
1350 "} else g();",
1351 AllowsMergedIf);
1352 verifyFormat("if (a) g();\n"
1353 "else if (b) g();\n"
1354 "else {\n"
1355 " g();\n"
1356 "}",
1357 AllowsMergedIf);
1358 verifyFormat("if (a) g();\n"
1359 "else if (b) {\n"
1360 " g();\n"
1361 "} else {\n"
1362 " g();\n"
1363 "}",
1364 AllowsMergedIf);
1365 verifyFormat("if (a) {\n"
1366 " g();\n"
1367 "} else if (b) {\n"
1368 " g();\n"
1369 "} else {\n"
1370 " g();\n"
1371 "}",
1372 AllowsMergedIf);
1373 verifyFormat("MYIF (a) f();\n"
1374 "else {\n"
1375 " g();\n"
1376 "}",
1377 AllowsMergedIf);
1378 verifyFormat("MYIF (a) f();\n"
1379 "else {\n"
1380 " if (a) f();\n"
1381 " else {\n"
1382 " g();\n"
1383 " }\n"
1384 " g();\n"
1385 "}",
1386 AllowsMergedIf);
1387
1388 verifyFormat("MYIF (a) g();", AllowsMergedIf);
1389 verifyFormat("MYIF (a) {\n"
1390 " g()\n"
1391 "};",
1392 AllowsMergedIf);
1393 verifyFormat("MYIF (a) g();\n"
1394 "else g();",
1395 AllowsMergedIf);
1396 verifyFormat("MYIF (a) {\n"
1397 " g();\n"
1398 "} else g();",
1399 AllowsMergedIf);
1400 verifyFormat("MYIF (a) g();\n"
1401 "else {\n"
1402 " g();\n"
1403 "}",
1404 AllowsMergedIf);
1405 verifyFormat("MYIF (a) {\n"
1406 " g();\n"
1407 "} else {\n"
1408 " g();\n"
1409 "}",
1410 AllowsMergedIf);
1411 verifyFormat("MYIF (a) g();\n"
1412 "else MYIF (b) g();\n"
1413 "else g();",
1414 AllowsMergedIf);
1415 verifyFormat("MYIF (a) g();\n"
1416 "else if (b) g();\n"
1417 "else g();",
1418 AllowsMergedIf);
1419 verifyFormat("MYIF (a) {\n"
1420 " g();\n"
1421 "} else MYIF (b) g();\n"
1422 "else g();",
1423 AllowsMergedIf);
1424 verifyFormat("MYIF (a) {\n"
1425 " g();\n"
1426 "} else if (b) g();\n"
1427 "else g();",
1428 AllowsMergedIf);
1429 verifyFormat("MYIF (a) g();\n"
1430 "else MYIF (b) {\n"
1431 " g();\n"
1432 "} else g();",
1433 AllowsMergedIf);
1434 verifyFormat("MYIF (a) g();\n"
1435 "else if (b) {\n"
1436 " g();\n"
1437 "} else g();",
1438 AllowsMergedIf);
1439 verifyFormat("MYIF (a) g();\n"
1440 "else MYIF (b) g();\n"
1441 "else {\n"
1442 " g();\n"
1443 "}",
1444 AllowsMergedIf);
1445 verifyFormat("MYIF (a) g();\n"
1446 "else if (b) g();\n"
1447 "else {\n"
1448 " g();\n"
1449 "}",
1450 AllowsMergedIf);
1451 verifyFormat("MYIF (a) g();\n"
1452 "else MYIF (b) {\n"
1453 " g();\n"
1454 "} else {\n"
1455 " g();\n"
1456 "}",
1457 AllowsMergedIf);
1458 verifyFormat("MYIF (a) g();\n"
1459 "else if (b) {\n"
1460 " g();\n"
1461 "} else {\n"
1462 " g();\n"
1463 "}",
1464 AllowsMergedIf);
1465 verifyFormat("MYIF (a) {\n"
1466 " g();\n"
1467 "} else MYIF (b) {\n"
1468 " g();\n"
1469 "} else {\n"
1470 " g();\n"
1471 "}",
1472 AllowsMergedIf);
1473 verifyFormat("MYIF (a) {\n"
1474 " g();\n"
1475 "} else if (b) {\n"
1476 " g();\n"
1477 "} else {\n"
1478 " g();\n"
1479 "}",
1480 AllowsMergedIf);
1481 }
1482
1483 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
1484 FormatStyle AllowsMergedLoops = getLLVMStyle();
1485 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
1486 verifyFormat("while (true) continue;", AllowsMergedLoops);
1487 verifyFormat("for (;;) continue;", AllowsMergedLoops);
1488 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
1489 verifyFormat("BOOST_FOREACH (int &v, vec) v *= 2;", AllowsMergedLoops);
1490 verifyFormat("while (true)\n"
1491 " ;",
1492 AllowsMergedLoops);
1493 verifyFormat("for (;;)\n"
1494 " ;",
1495 AllowsMergedLoops);
1496 verifyFormat("for (;;)\n"
1497 " for (;;) continue;",
1498 AllowsMergedLoops);
1499 verifyFormat("for (;;)\n"
1500 " while (true) continue;",
1501 AllowsMergedLoops);
1502 verifyFormat("while (true)\n"
1503 " for (;;) continue;",
1504 AllowsMergedLoops);
1505 verifyFormat("BOOST_FOREACH (int &v, vec)\n"
1506 " for (;;) continue;",
1507 AllowsMergedLoops);
1508 verifyFormat("for (;;)\n"
1509 " BOOST_FOREACH (int &v, vec) continue;",
1510 AllowsMergedLoops);
1511 verifyFormat("for (;;) // Can't merge this\n"
1512 " continue;",
1513 AllowsMergedLoops);
1514 verifyFormat("for (;;) /* still don't merge */\n"
1515 " continue;",
1516 AllowsMergedLoops);
1517 verifyFormat("do a++;\n"
1518 "while (true);",
1519 AllowsMergedLoops);
1520 verifyFormat("do /* Don't merge */\n"
1521 " a++;\n"
1522 "while (true);",
1523 AllowsMergedLoops);
1524 verifyFormat("do // Don't merge\n"
1525 " a++;\n"
1526 "while (true);",
1527 AllowsMergedLoops);
1528 verifyFormat("do\n"
1529 " // Don't merge\n"
1530 " a++;\n"
1531 "while (true);",
1532 AllowsMergedLoops);
1533 // Without braces labels are interpreted differently.
1534 verifyFormat("{\n"
1535 " do\n"
1536 " label:\n"
1537 " a++;\n"
1538 " while (true);\n"
1539 "}",
1540 AllowsMergedLoops);
1541 }
1542
1543 TEST_F(FormatTest, FormatShortBracedStatements) {
1544 FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
1545 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine, false);
1546 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine,
1547 FormatStyle::SIS_Never);
1548 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine, false);
1549 EXPECT_EQ(AllowSimpleBracedStatements.BraceWrapping.AfterFunction, false);
1550 verifyFormat("for (;;) {\n"
1551 " f();\n"
1552 "}");
1553 verifyFormat("/*comment*/ for (;;) {\n"
1554 " f();\n"
1555 "}");
1556 verifyFormat("BOOST_FOREACH (int v, vec) {\n"
1557 " f();\n"
1558 "}");
1559 verifyFormat("/*comment*/ BOOST_FOREACH (int v, vec) {\n"
1560 " f();\n"
1561 "}");
1562 verifyFormat("while (true) {\n"
1563 " f();\n"
1564 "}");
1565 verifyFormat("/*comment*/ while (true) {\n"
1566 " f();\n"
1567 "}");
1568 verifyFormat("if (true) {\n"
1569 " f();\n"
1570 "}");
1571 verifyFormat("/*comment*/ if (true) {\n"
1572 " f();\n"
1573 "}");
1574
1575 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
1576 FormatStyle::SBS_Empty;
1577 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1578 FormatStyle::SIS_WithoutElse;
1579 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1580 verifyFormat("if (i) break;", AllowSimpleBracedStatements);
1581 verifyFormat("if (i > 0) {\n"
1582 " return i;\n"
1583 "}",
1584 AllowSimpleBracedStatements);
1585
1586 AllowSimpleBracedStatements.IfMacros.push_back("MYIF");
1587 // Where line-lengths matter, a 2-letter synonym that maintains line length.
1588 // Not IF to avoid any confusion that IF is somehow special.
1589 AllowSimpleBracedStatements.IfMacros.push_back("FI");
1590 AllowSimpleBracedStatements.ColumnLimit = 40;
1591 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
1592 FormatStyle::SBS_Always;
1593 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1594 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
1595 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
1596 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
1597
1598 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1599 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1600 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1601 verifyFormat("if consteval {}", AllowSimpleBracedStatements);
1602 verifyFormat("if !consteval {}", AllowSimpleBracedStatements);
1603 verifyFormat("if CONSTEVAL {}", AllowSimpleBracedStatements);
1604 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1605 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1606 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1607 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1608 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1609 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1610 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1611 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1612 verifyFormat("if consteval { f(); }", AllowSimpleBracedStatements);
1613 verifyFormat("if CONSTEVAL { f(); }", AllowSimpleBracedStatements);
1614 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1615 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1616 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1617 verifyFormat("MYIF consteval { f(); }", AllowSimpleBracedStatements);
1618 verifyFormat("MYIF CONSTEVAL { f(); }", AllowSimpleBracedStatements);
1619 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1620 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1621 verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1622 AllowSimpleBracedStatements);
1623 verifyFormat("if (true) {\n"
1624 " ffffffffffffffffffffffff();\n"
1625 "}",
1626 AllowSimpleBracedStatements);
1627 verifyFormat("if (true) {\n"
1628 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1629 "}",
1630 AllowSimpleBracedStatements);
1631 verifyFormat("if (true) { //\n"
1632 " f();\n"
1633 "}",
1634 AllowSimpleBracedStatements);
1635 verifyFormat("if (true) {\n"
1636 " f();\n"
1637 " f();\n"
1638 "}",
1639 AllowSimpleBracedStatements);
1640 verifyFormat("if (true) {\n"
1641 " f();\n"
1642 "} else {\n"
1643 " f();\n"
1644 "}",
1645 AllowSimpleBracedStatements);
1646 verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1647 AllowSimpleBracedStatements);
1648 verifyFormat("MYIF (true) {\n"
1649 " ffffffffffffffffffffffff();\n"
1650 "}",
1651 AllowSimpleBracedStatements);
1652 verifyFormat("MYIF (true) {\n"
1653 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1654 "}",
1655 AllowSimpleBracedStatements);
1656 verifyFormat("MYIF (true) { //\n"
1657 " f();\n"
1658 "}",
1659 AllowSimpleBracedStatements);
1660 verifyFormat("MYIF (true) {\n"
1661 " f();\n"
1662 " f();\n"
1663 "}",
1664 AllowSimpleBracedStatements);
1665 verifyFormat("MYIF (true) {\n"
1666 " f();\n"
1667 "} else {\n"
1668 " f();\n"
1669 "}",
1670 AllowSimpleBracedStatements);
1671
1672 verifyFormat("struct A2 {\n"
1673 " int X;\n"
1674 "};",
1675 AllowSimpleBracedStatements);
1676 verifyFormat("typedef struct A2 {\n"
1677 " int X;\n"
1678 "} A2_t;",
1679 AllowSimpleBracedStatements);
1680 verifyFormat("template <int> struct A2 {\n"
1681 " struct B {};\n"
1682 "};",
1683 AllowSimpleBracedStatements);
1684
1685 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1686 FormatStyle::SIS_Never;
1687 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1688 verifyFormat("if (true) {\n"
1689 " f();\n"
1690 "}",
1691 AllowSimpleBracedStatements);
1692 verifyFormat("if (true) {\n"
1693 " f();\n"
1694 "} else {\n"
1695 " f();\n"
1696 "}",
1697 AllowSimpleBracedStatements);
1698 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1699 verifyFormat("MYIF (true) {\n"
1700 " f();\n"
1701 "}",
1702 AllowSimpleBracedStatements);
1703 verifyFormat("MYIF (true) {\n"
1704 " f();\n"
1705 "} else {\n"
1706 " f();\n"
1707 "}",
1708 AllowSimpleBracedStatements);
1709
1710 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1711 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1712 verifyFormat("while (true) {\n"
1713 " f();\n"
1714 "}",
1715 AllowSimpleBracedStatements);
1716 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1717 verifyFormat("for (;;) {\n"
1718 " f();\n"
1719 "}",
1720 AllowSimpleBracedStatements);
1721 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements);
1722 verifyFormat("BOOST_FOREACH (int v, vec) {\n"
1723 " f();\n"
1724 "}",
1725 AllowSimpleBracedStatements);
1726
1727 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1728 FormatStyle::SIS_WithoutElse;
1729 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1730 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement =
1731 FormatStyle::BWACS_Always;
1732
1733 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1734 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1735 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1736 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1737 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1738 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1739 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1740 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1741 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1742 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1743 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1744 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1745 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1746 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1747 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1748 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1749 verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1750 AllowSimpleBracedStatements);
1751 verifyFormat("if (true)\n"
1752 "{\n"
1753 " ffffffffffffffffffffffff();\n"
1754 "}",
1755 AllowSimpleBracedStatements);
1756 verifyFormat("if (true)\n"
1757 "{\n"
1758 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1759 "}",
1760 AllowSimpleBracedStatements);
1761 verifyFormat("if (true)\n"
1762 "{ //\n"
1763 " f();\n"
1764 "}",
1765 AllowSimpleBracedStatements);
1766 verifyFormat("if (true)\n"
1767 "{\n"
1768 " f();\n"
1769 " f();\n"
1770 "}",
1771 AllowSimpleBracedStatements);
1772 verifyFormat("if (true)\n"
1773 "{\n"
1774 " f();\n"
1775 "} else\n"
1776 "{\n"
1777 " f();\n"
1778 "}",
1779 AllowSimpleBracedStatements);
1780 verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1781 AllowSimpleBracedStatements);
1782 verifyFormat("MYIF (true)\n"
1783 "{\n"
1784 " ffffffffffffffffffffffff();\n"
1785 "}",
1786 AllowSimpleBracedStatements);
1787 verifyFormat("MYIF (true)\n"
1788 "{\n"
1789 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1790 "}",
1791 AllowSimpleBracedStatements);
1792 verifyFormat("MYIF (true)\n"
1793 "{ //\n"
1794 " f();\n"
1795 "}",
1796 AllowSimpleBracedStatements);
1797 verifyFormat("MYIF (true)\n"
1798 "{\n"
1799 " f();\n"
1800 " f();\n"
1801 "}",
1802 AllowSimpleBracedStatements);
1803 verifyFormat("MYIF (true)\n"
1804 "{\n"
1805 " f();\n"
1806 "} else\n"
1807 "{\n"
1808 " f();\n"
1809 "}",
1810 AllowSimpleBracedStatements);
1811
1812 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1813 FormatStyle::SIS_Never;
1814 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1815 verifyFormat("if (true)\n"
1816 "{\n"
1817 " f();\n"
1818 "}",
1819 AllowSimpleBracedStatements);
1820 verifyFormat("if (true)\n"
1821 "{\n"
1822 " f();\n"
1823 "} else\n"
1824 "{\n"
1825 " f();\n"
1826 "}",
1827 AllowSimpleBracedStatements);
1828 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1829 verifyFormat("MYIF (true)\n"
1830 "{\n"
1831 " f();\n"
1832 "}",
1833 AllowSimpleBracedStatements);
1834 verifyFormat("MYIF (true)\n"
1835 "{\n"
1836 " f();\n"
1837 "} else\n"
1838 "{\n"
1839 " f();\n"
1840 "}",
1841 AllowSimpleBracedStatements);
1842
1843 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1844 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1845 verifyFormat("while (true)\n"
1846 "{\n"
1847 " f();\n"
1848 "}",
1849 AllowSimpleBracedStatements);
1850 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1851 verifyFormat("for (;;)\n"
1852 "{\n"
1853 " f();\n"
1854 "}",
1855 AllowSimpleBracedStatements);
1856 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements);
1857 verifyFormat("BOOST_FOREACH (int v, vec)\n"
1858 "{\n"
1859 " f();\n"
1860 "}",
1861 AllowSimpleBracedStatements);
1862
1863 FormatStyle Style = getLLVMStyle();
1864 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1865 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
1866
1867 verifyFormat("while (i > 0)\n"
1868 "{\n"
1869 " --i;\n"
1870 "}",
1871 Style);
1872
1873 verifyFormat("if (a)\n"
1874 "{\n"
1875 " ++b;\n"
1876 "}",
1877 Style);
1878
1879 verifyFormat("if (a)\n"
1880 "{\n"
1881 " b = 1;\n"
1882 "} else\n"
1883 "{\n"
1884 " b = 0;\n"
1885 "}",
1886 Style);
1887
1888 verifyFormat("if (a)\n"
1889 "{\n"
1890 " b = 1;\n"
1891 "} else if (c)\n"
1892 "{\n"
1893 " b = 2;\n"
1894 "} else\n"
1895 "{\n"
1896 " b = 0;\n"
1897 "}",
1898 Style);
1899
1900 Style.BraceWrapping.BeforeElse = true;
1901
1902 verifyFormat("if (a)\n"
1903 "{\n"
1904 " b = 1;\n"
1905 "}\n"
1906 "else\n"
1907 "{\n"
1908 " b = 0;\n"
1909 "}",
1910 Style);
1911
1912 verifyFormat("if (a)\n"
1913 "{\n"
1914 " b = 1;\n"
1915 "}\n"
1916 "else if (c)\n"
1917 "{\n"
1918 " b = 2;\n"
1919 "}\n"
1920 "else\n"
1921 "{\n"
1922 " b = 0;\n"
1923 "}",
1924 Style);
1925 }
1926
1927 TEST_F(FormatTest, UnderstandsMacros) {
1928 verifyFormat("#define A (parentheses)");
1929 verifyFormat("/* comment */ #define A (parentheses)");
1930 verifyFormat("/* comment */ /* another comment */ #define A (parentheses)");
1931 // Even the partial code should never be merged.
1932 EXPECT_EQ("/* comment */ #define A (parentheses)\n"
1933 "#",
1934 format("/* comment */ #define A (parentheses)\n"
1935 "#"));
1936 verifyFormat("/* comment */ #define A (parentheses)\n"
1937 "#\n");
1938 verifyFormat("/* comment */ #define A (parentheses)\n"
1939 "#define B (parentheses)");
1940 verifyFormat("#define true ((int)1)");
1941 verifyFormat("#define and(x)");
1942 verifyFormat("#define if(x) x");
1943 verifyFormat("#define return(x) (x)");
1944 verifyFormat("#define while(x) for (; x;)");
1945 verifyFormat("#define xor(x) (^(x))");
1946 verifyFormat("#define __except(x)");
1947 verifyFormat("#define __try(x)");
1948
1949 // https://llvm.org/PR54348.
1950 verifyFormat(
1951 "#define A"
1952 " "
1953 "\\\n"
1954 " class & {}");
1955
1956 FormatStyle Style = getLLVMStyle();
1957 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1958 Style.BraceWrapping.AfterFunction = true;
1959 // Test that a macro definition never gets merged with the following
1960 // definition.
1961 // FIXME: The AAA macro definition probably should not be split into 3 lines.
1962 verifyFormat("#define AAA "
1963 " \\\n"
1964 " N "
1965 " \\\n"
1966 " {\n"
1967 "#define BBB }\n",
1968 Style);
1969 // verifyFormat("#define AAA N { //\n", Style);
1970
1971 verifyFormat("MACRO(return)");
1972 verifyFormat("MACRO(co_await)");
1973 verifyFormat("MACRO(co_return)");
1974 verifyFormat("MACRO(co_yield)");
1975 verifyFormat("MACRO(return, something)");
1976 verifyFormat("MACRO(co_return, something)");
1977 verifyFormat("MACRO(something##something)");
1978 verifyFormat("MACRO(return##something)");
1979 verifyFormat("MACRO(co_return##something)");
1980 }
1981
1982 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
1983 FormatStyle Style = getLLVMStyleWithColumns(60);
1984 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
1985 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
1986 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1987 EXPECT_EQ("#define A \\\n"
1988 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n"
1989 " { \\\n"
1990 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1991 " }\n"
1992 "X;",
1993 format("#define A \\\n"
1994 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
1995 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1996 " }\n"
1997 "X;",
1998 Style));
1999 }
2000
2001 TEST_F(FormatTest, ParseIfElse) {
2002 verifyFormat("if (true)\n"
2003 " if (true)\n"
2004 " if (true)\n"
2005 " f();\n"
2006 " else\n"
2007 " g();\n"
2008 " else\n"
2009 " h();\n"
2010 "else\n"
2011 " i();");
2012 verifyFormat("if (true)\n"
2013 " if (true)\n"
2014 " if (true) {\n"
2015 " if (true)\n"
2016 " f();\n"
2017 " } else {\n"
2018 " g();\n"
2019 " }\n"
2020 " else\n"
2021 " h();\n"
2022 "else {\n"
2023 " i();\n"
2024 "}");
2025 verifyFormat("if (true)\n"
2026 " if constexpr (true)\n"
2027 " if (true) {\n"
2028 " if constexpr (true)\n"
2029 " f();\n"
2030 " } else {\n"
2031 " g();\n"
2032 " }\n"
2033 " else\n"
2034 " h();\n"
2035 "else {\n"
2036 " i();\n"
2037 "}");
2038 verifyFormat("if (true)\n"
2039 " if CONSTEXPR (true)\n"
2040 " if (true) {\n"
2041 " if CONSTEXPR (true)\n"
2042 " f();\n"
2043 " } else {\n"
2044 " g();\n"
2045 " }\n"
2046 " else\n"
2047 " h();\n"
2048 "else {\n"
2049 " i();\n"
2050 "}");
2051 verifyFormat("void f() {\n"
2052 " if (a) {\n"
2053 " } else {\n"
2054 " }\n"
2055 "}");
2056 }
2057
2058 TEST_F(FormatTest, ElseIf) {
2059 verifyFormat("if (a) {\n} else if (b) {\n}");
2060 verifyFormat("if (a)\n"
2061 " f();\n"
2062 "else if (b)\n"
2063 " g();\n"
2064 "else\n"
2065 " h();");
2066 verifyFormat("if (a)\n"
2067 " f();\n"
2068 "else // comment\n"
2069 " if (b) {\n"
2070 " g();\n"
2071 " h();\n"
2072 " }");
2073 verifyFormat("if constexpr (a)\n"
2074 " f();\n"
2075 "else if constexpr (b)\n"
2076 " g();\n"
2077 "else\n"
2078 " h();");
2079 verifyFormat("if CONSTEXPR (a)\n"
2080 " f();\n"
2081 "else if CONSTEXPR (b)\n"
2082 " g();\n"
2083 "else\n"
2084 " h();");
2085 verifyFormat("if (a) {\n"
2086 " f();\n"
2087 "}\n"
2088 "// or else ..\n"
2089 "else {\n"
2090 " g()\n"
2091 "}");
2092
2093 verifyFormat("if (a) {\n"
2094 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2095 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
2096 "}");
2097 verifyFormat("if (a) {\n"
2098 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2099 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
2100 "}");
2101 verifyFormat("if (a) {\n"
2102 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2103 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
2104 "}");
2105 verifyFormat("if (a) {\n"
2106 "} else if (\n"
2107 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2108 "}",
2109 getLLVMStyleWithColumns(62));
2110 verifyFormat("if (a) {\n"
2111 "} else if constexpr (\n"
2112 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2113 "}",
2114 getLLVMStyleWithColumns(62));
2115 verifyFormat("if (a) {\n"
2116 "} else if CONSTEXPR (\n"
2117 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2118 "}",
2119 getLLVMStyleWithColumns(62));
2120 }
2121
2122 TEST_F(FormatTest, SeparatePointerReferenceAlignment) {
2123 FormatStyle Style = getLLVMStyle();
2124 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
2125 EXPECT_EQ(Style.ReferenceAlignment, FormatStyle::RAS_Pointer);
2126 verifyFormat("int *f1(int *a, int &b, int &&c);", Style);
2127 verifyFormat("int &f2(int &&c, int *a, int &b);", Style);
2128 verifyFormat("int &&f3(int &b, int &&c, int *a);", Style);
2129 verifyFormat("int *f1(int &a) const &;", Style);
2130 verifyFormat("int *f1(int &a) const & = 0;", Style);
2131 verifyFormat("int *a = f1();", Style);
2132 verifyFormat("int &b = f2();", Style);
2133 verifyFormat("int &&c = f3();", Style);
2134 verifyFormat("int f3() { return sizeof(Foo &); }", Style);
2135 verifyFormat("int f4() { return sizeof(Foo &&); }", Style);
2136 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style);
2137 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style);
2138 verifyFormat("for (auto a = 0, b = 0; const auto &c : {1, 2, 3})", Style);
2139 verifyFormat("for (auto a = 0, b = 0; const int &c : {1, 2, 3})", Style);
2140 verifyFormat("for (auto a = 0, b = 0; const Foo &c : {1, 2, 3})", Style);
2141 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2142 verifyFormat("for (int a = 0, b = 0; const auto &c : {1, 2, 3})", Style);
2143 verifyFormat("for (int a = 0, b = 0; const int &c : {1, 2, 3})", Style);
2144 verifyFormat("for (int a = 0, b = 0; const Foo &c : {1, 2, 3})", Style);
2145 verifyFormat("for (int a = 0, b++; const auto &c : {1, 2, 3})", Style);
2146 verifyFormat("for (int a = 0, b++; const int &c : {1, 2, 3})", Style);
2147 verifyFormat("for (int a = 0, b++; const Foo &c : {1, 2, 3})", Style);
2148 verifyFormat("for (auto x = 0; auto &c : {1, 2, 3})", Style);
2149 verifyFormat("for (auto x = 0; int &c : {1, 2, 3})", Style);
2150 verifyFormat("for (int x = 0; auto &c : {1, 2, 3})", Style);
2151 verifyFormat("for (int x = 0; int &c : {1, 2, 3})", Style);
2152 verifyFormat("for (f(); auto &c : {1, 2, 3})", Style);
2153 verifyFormat("for (f(); int &c : {1, 2, 3})", Style);
2154 verifyFormat(
2155 "function<int(int &)> res1 = [](int &a) { return 0000000000000; },\n"
2156 " res2 = [](int &a) { return 0000000000000; };",
2157 Style);
2158
2159 Style.AlignConsecutiveDeclarations.Enabled = true;
2160 verifyFormat("Const unsigned int *c;\n"
2161 "const unsigned int *d;\n"
2162 "Const unsigned int &e;\n"
2163 "const unsigned int &f;\n"
2164 "const unsigned &&g;\n"
2165 "Const unsigned h;",
2166 Style);
2167
2168 Style.PointerAlignment = FormatStyle::PAS_Left;
2169 Style.ReferenceAlignment = FormatStyle::RAS_Pointer;
2170 verifyFormat("int* f1(int* a, int& b, int&& c);", Style);
2171 verifyFormat("int& f2(int&& c, int* a, int& b);", Style);
2172 verifyFormat("int&& f3(int& b, int&& c, int* a);", Style);
2173 verifyFormat("int* f1(int& a) const& = 0;", Style);
2174 verifyFormat("int* a = f1();", Style);
2175 verifyFormat("int& b = f2();", Style);
2176 verifyFormat("int&& c = f3();", Style);
2177 verifyFormat("int f3() { return sizeof(Foo&); }", Style);
2178 verifyFormat("int f4() { return sizeof(Foo&&); }", Style);
2179 verifyFormat("void f5() { int f6(Foo&, Bar&); }", Style);
2180 verifyFormat("void f5() { int f6(Foo&&, Bar&&); }", Style);
2181 verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
2182 verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style);
2183 verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style);
2184 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2185 verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
2186 verifyFormat("for (int a = 0, b = 0; const int& c : {1, 2, 3})", Style);
2187 verifyFormat("for (int a = 0, b = 0; const Foo& c : {1, 2, 3})", Style);
2188 verifyFormat("for (int a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2189 verifyFormat("for (int a = 0, b++; const auto& c : {1, 2, 3})", Style);
2190 verifyFormat("for (int a = 0, b++; const int& c : {1, 2, 3})", Style);
2191 verifyFormat("for (int a = 0, b++; const Foo& c : {1, 2, 3})", Style);
2192 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
2193 verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style);
2194 verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style);
2195 verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style);
2196 verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style);
2197 verifyFormat("for (f(); auto& c : {1, 2, 3})", Style);
2198 verifyFormat("for (f(); int& c : {1, 2, 3})", Style);
2199 verifyFormat(
2200 "function<int(int&)> res1 = [](int& a) { return 0000000000000; },\n"
2201 " res2 = [](int& a) { return 0000000000000; };",
2202 Style);
2203
2204 Style.AlignConsecutiveDeclarations.Enabled = true;
2205 verifyFormat("Const unsigned int* c;\n"
2206 "const unsigned int* d;\n"
2207 "Const unsigned int& e;\n"
2208 "const unsigned int& f;\n"
2209 "const unsigned&& g;\n"
2210 "Const unsigned h;",
2211 Style);
2212
2213 Style.PointerAlignment = FormatStyle::PAS_Right;
2214 Style.ReferenceAlignment = FormatStyle::RAS_Left;
2215 verifyFormat("int *f1(int *a, int& b, int&& c);", Style);
2216 verifyFormat("int& f2(int&& c, int *a, int& b);", Style);
2217 verifyFormat("int&& f3(int& b, int&& c, int *a);", Style);
2218 verifyFormat("int *a = f1();", Style);
2219 verifyFormat("int& b = f2();", Style);
2220 verifyFormat("int&& c = f3();", Style);
2221 verifyFormat("int f3() { return sizeof(Foo&); }", Style);
2222 verifyFormat("int f4() { return sizeof(Foo&&); }", Style);
2223 verifyFormat("void f5() { int f6(Foo&, Bar&); }", Style);
2224 verifyFormat("void f5() { int f6(Foo&&, Bar&&); }", Style);
2225 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2226 verifyFormat("for (int a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2227 verifyFormat("for (int a = 0, b++; const Foo *c : {1, 2, 3})", Style);
2228
2229 Style.AlignConsecutiveDeclarations.Enabled = true;
2230 verifyFormat("Const unsigned int *c;\n"
2231 "const unsigned int *d;\n"
2232 "Const unsigned int& e;\n"
2233 "const unsigned int& f;\n"
2234 "const unsigned g;\n"
2235 "Const unsigned h;",
2236 Style);
2237
2238 Style.PointerAlignment = FormatStyle::PAS_Left;
2239 Style.ReferenceAlignment = FormatStyle::RAS_Middle;
2240 verifyFormat("int* f1(int* a, int & b, int && c);", Style);
2241 verifyFormat("int & f2(int && c, int* a, int & b);", Style);
2242 verifyFormat("int && f3(int & b, int && c, int* a);", Style);
2243 verifyFormat("int* a = f1();", Style);
2244 verifyFormat("int & b = f2();", Style);
2245 verifyFormat("int && c = f3();", Style);
2246 verifyFormat("int f3() { return sizeof(Foo &); }", Style);
2247 verifyFormat("int f4() { return sizeof(Foo &&); }", Style);
2248 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style);
2249 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style);
2250 verifyFormat("for (auto a = 0, b = 0; const auto & c : {1, 2, 3})", Style);
2251 verifyFormat("for (auto a = 0, b = 0; const int & c : {1, 2, 3})", Style);
2252 verifyFormat("for (auto a = 0, b = 0; const Foo & c : {1, 2, 3})", Style);
2253 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2254 verifyFormat("for (int a = 0, b++; const auto & c : {1, 2, 3})", Style);
2255 verifyFormat("for (int a = 0, b++; const int & c : {1, 2, 3})", Style);
2256 verifyFormat("for (int a = 0, b++; const Foo & c : {1, 2, 3})", Style);
2257 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
2258 verifyFormat("for (auto x = 0; auto & c : {1, 2, 3})", Style);
2259 verifyFormat("for (auto x = 0; int & c : {1, 2, 3})", Style);
2260 verifyFormat("for (int x = 0; auto & c : {1, 2, 3})", Style);
2261 verifyFormat("for (int x = 0; int & c : {1, 2, 3})", Style);
2262 verifyFormat("for (f(); auto & c : {1, 2, 3})", Style);
2263 verifyFormat("for (f(); int & c : {1, 2, 3})", Style);
2264 verifyFormat(
2265 "function<int(int &)> res1 = [](int & a) { return 0000000000000; },\n"
2266 " res2 = [](int & a) { return 0000000000000; };",
2267 Style);
2268
2269 Style.AlignConsecutiveDeclarations.Enabled = true;
2270 verifyFormat("Const unsigned int* c;\n"
2271 "const unsigned int* d;\n"
2272 "Const unsigned int & e;\n"
2273 "const unsigned int & f;\n"
2274 "const unsigned && g;\n"
2275 "Const unsigned h;",
2276 Style);
2277
2278 Style.PointerAlignment = FormatStyle::PAS_Middle;
2279 Style.ReferenceAlignment = FormatStyle::RAS_Right;
2280 verifyFormat("int * f1(int * a, int &b, int &&c);", Style);
2281 verifyFormat("int &f2(int &&c, int * a, int &b);", Style);
2282 verifyFormat("int &&f3(int &b, int &&c, int * a);", Style);
2283 verifyFormat("int * a = f1();", Style);
2284 verifyFormat("int &b = f2();", Style);
2285 verifyFormat("int &&c = f3();", Style);
2286 verifyFormat("int f3() { return sizeof(Foo &); }", Style);
2287 verifyFormat("int f4() { return sizeof(Foo &&); }", Style);
2288 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style);
2289 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style);
2290 verifyFormat("for (auto a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
2291 verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
2292 verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style);
2293
2294 // FIXME: we don't handle this yet, so output may be arbitrary until it's
2295 // specifically handled
2296 // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style);
2297 }
2298
2299 TEST_F(FormatTest, FormatsForLoop) {
2300 verifyFormat(
2301 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
2302 " ++VeryVeryLongLoopVariable)\n"
2303 " ;");
2304 verifyFormat("for (;;)\n"
2305 " f();");
2306 verifyFormat("for (;;) {\n}");
2307 verifyFormat("for (;;) {\n"
2308 " f();\n"
2309 "}");
2310 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
2311
2312 verifyFormat(
2313 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2314 " E = UnwrappedLines.end();\n"
2315 " I != E; ++I) {\n}");
2316
2317 verifyFormat(
2318 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
2319 " ++IIIII) {\n}");
2320 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
2321 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
2322 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
2323 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
2324 " I = FD->getDeclsInPrototypeScope().begin(),\n"
2325 " E = FD->getDeclsInPrototypeScope().end();\n"
2326 " I != E; ++I) {\n}");
2327 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
2328 " I = Container.begin(),\n"
2329 " E = Container.end();\n"
2330 " I != E; ++I) {\n}",
2331 getLLVMStyleWithColumns(76));
2332
2333 verifyFormat(
2334 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2335 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
2336 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2337 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2338 " ++aaaaaaaaaaa) {\n}");
2339 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2340 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
2341 " ++i) {\n}");
2342 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
2343 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2344 "}");
2345 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
2346 " aaaaaaaaaa);\n"
2347 " iter; ++iter) {\n"
2348 "}");
2349 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2350 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2351 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
2352 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
2353
2354 // These should not be formatted as Objective-C for-in loops.
2355 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
2356 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
2357 verifyFormat("Foo *x;\nfor (x in y) {\n}");
2358 verifyFormat(
2359 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
2360
2361 FormatStyle NoBinPacking = getLLVMStyle();
2362 NoBinPacking.BinPackParameters = false;
2363 verifyFormat("for (int aaaaaaaaaaa = 1;\n"
2364 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
2365 " aaaaaaaaaaaaaaaa,\n"
2366 " aaaaaaaaaaaaaaaa,\n"
2367 " aaaaaaaaaaaaaaaa);\n"
2368 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2369 "}",
2370 NoBinPacking);
2371 verifyFormat(
2372 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2373 " E = UnwrappedLines.end();\n"
2374 " I != E;\n"
2375 " ++I) {\n}",
2376 NoBinPacking);
2377
2378 FormatStyle AlignLeft = getLLVMStyle();
2379 AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
2380 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
2381 }
2382
2383 TEST_F(FormatTest, RangeBasedForLoops) {
2384 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
2385 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2386 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
2387 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
2388 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
2389 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2390 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
2391 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
2392 }
2393
2394 TEST_F(FormatTest, ForEachLoops) {
2395 FormatStyle Style = getLLVMStyle();
2396 EXPECT_EQ(Style.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
2397 EXPECT_EQ(Style.AllowShortLoopsOnASingleLine, false);
2398 verifyFormat("void f() {\n"
2399 " for (;;) {\n"
2400 " }\n"
2401 " foreach (Item *item, itemlist) {\n"
2402 " }\n"
2403 " Q_FOREACH (Item *item, itemlist) {\n"
2404 " }\n"
2405 " BOOST_FOREACH (Item *item, itemlist) {\n"
2406 " }\n"
2407 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2408 "}",
2409 Style);
2410 verifyFormat("void f() {\n"
2411 " for (;;)\n"
2412 " int j = 1;\n"
2413 " Q_FOREACH (int v, vec)\n"
2414 " v *= 2;\n"
2415 " for (;;) {\n"
2416 " int j = 1;\n"
2417 " }\n"
2418 " Q_FOREACH (int v, vec) {\n"
2419 " v *= 2;\n"
2420 " }\n"
2421 "}",
2422 Style);
2423
2424 FormatStyle ShortBlocks = getLLVMStyle();
2425 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
2426 EXPECT_EQ(ShortBlocks.AllowShortLoopsOnASingleLine, false);
2427 verifyFormat("void f() {\n"
2428 " for (;;)\n"
2429 " int j = 1;\n"
2430 " Q_FOREACH (int &v, vec)\n"
2431 " v *= 2;\n"
2432 " for (;;) {\n"
2433 " int j = 1;\n"
2434 " }\n"
2435 " Q_FOREACH (int &v, vec) {\n"
2436 " int j = 1;\n"
2437 " }\n"
2438 "}",
2439 ShortBlocks);
2440
2441 FormatStyle ShortLoops = getLLVMStyle();
2442 ShortLoops.AllowShortLoopsOnASingleLine = true;
2443 EXPECT_EQ(ShortLoops.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
2444 verifyFormat("void f() {\n"
2445 " for (;;) int j = 1;\n"
2446 " Q_FOREACH (int &v, vec) int j = 1;\n"
2447 " for (;;) {\n"
2448 " int j = 1;\n"
2449 " }\n"
2450 " Q_FOREACH (int &v, vec) {\n"
2451 " int j = 1;\n"
2452 " }\n"
2453 "}",
2454 ShortLoops);
2455
2456 FormatStyle ShortBlocksAndLoops = getLLVMStyle();
2457 ShortBlocksAndLoops.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
2458 ShortBlocksAndLoops.AllowShortLoopsOnASingleLine = true;
2459 verifyFormat("void f() {\n"
2460 " for (;;) int j = 1;\n"
2461 " Q_FOREACH (int &v, vec) int j = 1;\n"
2462 " for (;;) { int j = 1; }\n"
2463 " Q_FOREACH (int &v, vec) { int j = 1; }\n"
2464 "}",
2465 ShortBlocksAndLoops);
2466
2467 Style.SpaceBeforeParens =
2468 FormatStyle::SBPO_ControlStatementsExceptControlMacros;
2469 verifyFormat("void f() {\n"
2470 " for (;;) {\n"
2471 " }\n"
2472 " foreach(Item *item, itemlist) {\n"
2473 " }\n"
2474 " Q_FOREACH(Item *item, itemlist) {\n"
2475 " }\n"
2476 " BOOST_FOREACH(Item *item, itemlist) {\n"
2477 " }\n"
2478 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2479 "}",
2480 Style);
2481
2482 // As function-like macros.
2483 verifyFormat("#define foreach(x, y)\n"
2484 "#define Q_FOREACH(x, y)\n"
2485 "#define BOOST_FOREACH(x, y)\n"
2486 "#define UNKNOWN_FOREACH(x, y)\n");
2487
2488 // Not as function-like macros.
2489 verifyFormat("#define foreach (x, y)\n"
2490 "#define Q_FOREACH (x, y)\n"
2491 "#define BOOST_FOREACH (x, y)\n"
2492 "#define UNKNOWN_FOREACH (x, y)\n");
2493
2494 // handle microsoft non standard extension
2495 verifyFormat("for each (char c in x->MyStringProperty)");
2496 }
2497
2498 TEST_F(FormatTest, FormatsWhileLoop) {
2499 verifyFormat("while (true) {\n}");
2500 verifyFormat("while (true)\n"
2501 " f();");
2502 verifyFormat("while () {\n}");
2503 verifyFormat("while () {\n"
2504 " f();\n"
2505 "}");
2506 }
2507
2508 TEST_F(FormatTest, FormatsDoWhile) {
2509 verifyFormat("do {\n"
2510 " do_something();\n"
2511 "} while (something());");
2512 verifyFormat("do\n"
2513 " do_something();\n"
2514 "while (something());");
2515 }
2516
2517 TEST_F(FormatTest, FormatsSwitchStatement) {
2518 verifyFormat("switch (x) {\n"
2519 "case 1:\n"
2520 " f();\n"
2521 " break;\n"
2522 "case kFoo:\n"
2523 "case ns::kBar:\n"
2524 "case kBaz:\n"
2525 " break;\n"
2526 "default:\n"
2527 " g();\n"
2528 " break;\n"
2529 "}");
2530 verifyFormat("switch (x) {\n"
2531 "case 1: {\n"
2532 " f();\n"
2533 " break;\n"
2534 "}\n"
2535 "case 2: {\n"
2536 " break;\n"
2537 "}\n"
2538 "}");
2539 verifyFormat("switch (x) {\n"
2540 "case 1: {\n"
2541 " f();\n"
2542 " {\n"
2543 " g();\n"
2544 " h();\n"
2545 " }\n"
2546 " break;\n"
2547 "}\n"
2548 "}");
2549 verifyFormat("switch (x) {\n"
2550 "case 1: {\n"
2551 " f();\n"
2552 " if (foo) {\n"
2553 " g();\n"
2554 " h();\n"
2555 " }\n"
2556 " break;\n"
2557 "}\n"
2558 "}");
2559 verifyFormat("switch (x) {\n"
2560 "case 1: {\n"
2561 " f();\n"
2562 " g();\n"
2563 "} break;\n"
2564 "}");
2565 verifyFormat("switch (test)\n"
2566 " ;");
2567 verifyFormat("switch (x) {\n"
2568 "default: {\n"
2569 " // Do nothing.\n"
2570 "}\n"
2571 "}");
2572 verifyFormat("switch (x) {\n"
2573 "// comment\n"
2574 "// if 1, do f()\n"
2575 "case 1:\n"
2576 " f();\n"
2577 "}");
2578 verifyFormat("switch (x) {\n"
2579 "case 1:\n"
2580 " // Do amazing stuff\n"
2581 " {\n"
2582 " f();\n"
2583 " g();\n"
2584 " }\n"
2585 " break;\n"
2586 "}");
2587 verifyFormat("#define A \\\n"
2588 " switch (x) { \\\n"
2589 " case a: \\\n"
2590 " foo = b; \\\n"
2591 " }",
2592 getLLVMStyleWithColumns(20));
2593 verifyFormat("#define OPERATION_CASE(name) \\\n"
2594 " case OP_name: \\\n"
2595 " return operations::Operation##name\n",
2596 getLLVMStyleWithColumns(40));
2597 verifyFormat("switch (x) {\n"
2598 "case 1:;\n"
2599 "default:;\n"
2600 " int i;\n"
2601 "}");
2602
2603 verifyGoogleFormat("switch (x) {\n"
2604 " case 1:\n"
2605 " f();\n"
2606 " break;\n"
2607 " case kFoo:\n"
2608 " case ns::kBar:\n"
2609 " case kBaz:\n"
2610 " break;\n"
2611 " default:\n"
2612 " g();\n"
2613 " break;\n"
2614 "}");
2615 verifyGoogleFormat("switch (x) {\n"
2616 " case 1: {\n"
2617 " f();\n"
2618 " break;\n"
2619 " }\n"
2620 "}");
2621 verifyGoogleFormat("switch (test)\n"
2622 " ;");
2623
2624 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
2625 " case OP_name: \\\n"
2626 " return operations::Operation##name\n");
2627 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
2628 " // Get the correction operation class.\n"
2629 " switch (OpCode) {\n"
2630 " CASE(Add);\n"
2631 " CASE(Subtract);\n"
2632 " default:\n"
2633 " return operations::Unknown;\n"
2634 " }\n"
2635 "#undef OPERATION_CASE\n"
2636 "}");
2637 verifyFormat("DEBUG({\n"
2638 " switch (x) {\n"
2639 " case A:\n"
2640 " f();\n"
2641 " break;\n"
2642 " // fallthrough\n"
2643 " case B:\n"
2644 " g();\n"
2645 " break;\n"
2646 " }\n"
2647 "});");
2648 EXPECT_EQ("DEBUG({\n"
2649 " switch (x) {\n"
2650 " case A:\n"
2651 " f();\n"
2652 " break;\n"
2653 " // On B:\n"
2654 " case B:\n"
2655 " g();\n"
2656 " break;\n"
2657 " }\n"
2658 "});",
2659 format("DEBUG({\n"
2660 " switch (x) {\n"
2661 " case A:\n"
2662 " f();\n"
2663 " break;\n"
2664 " // On B:\n"
2665 " case B:\n"
2666 " g();\n"
2667 " break;\n"
2668 " }\n"
2669 "});",
2670 getLLVMStyle()));
2671 EXPECT_EQ("switch (n) {\n"
2672 "case 0: {\n"
2673 " return false;\n"
2674 "}\n"
2675 "default: {\n"
2676 " return true;\n"
2677 "}\n"
2678 "}",
2679 format("switch (n)\n"
2680 "{\n"
2681 "case 0: {\n"
2682 " return false;\n"
2683 "}\n"
2684 "default: {\n"
2685 " return true;\n"
2686 "}\n"
2687 "}",
2688 getLLVMStyle()));
2689 verifyFormat("switch (a) {\n"
2690 "case (b):\n"
2691 " return;\n"
2692 "}");
2693
2694 verifyFormat("switch (a) {\n"
2695 "case some_namespace::\n"
2696 " some_constant:\n"
2697 " return;\n"
2698 "}",
2699 getLLVMStyleWithColumns(34));
2700
2701 verifyFormat("switch (a) {\n"
2702 "[[likely]] case 1:\n"
2703 " return;\n"
2704 "}");
2705 verifyFormat("switch (a) {\n"
2706 "[[likely]] [[other::likely]] case 1:\n"
2707 " return;\n"
2708 "}");
2709 verifyFormat("switch (x) {\n"
2710 "case 1:\n"
2711 " return;\n"
2712 "[[likely]] case 2:\n"
2713 " return;\n"
2714 "}");
2715 verifyFormat("switch (a) {\n"
2716 "case 1:\n"
2717 "[[likely]] case 2:\n"
2718 " return;\n"
2719 "}");
2720 FormatStyle Attributes = getLLVMStyle();
2721 Attributes.AttributeMacros.push_back("LIKELY");
2722 Attributes.AttributeMacros.push_back("OTHER_LIKELY");
2723 verifyFormat("switch (a) {\n"
2724 "LIKELY case b:\n"
2725 " return;\n"
2726 "}",
2727 Attributes);
2728 verifyFormat("switch (a) {\n"
2729 "LIKELY OTHER_LIKELY() case b:\n"
2730 " return;\n"
2731 "}",
2732 Attributes);
2733 verifyFormat("switch (a) {\n"
2734 "case 1:\n"
2735 " return;\n"
2736 "LIKELY case 2:\n"
2737 " return;\n"
2738 "}",
2739 Attributes);
2740 verifyFormat("switch (a) {\n"
2741 "case 1:\n"
2742 "LIKELY case 2:\n"
2743 " return;\n"
2744 "}",
2745 Attributes);
2746
2747 FormatStyle Style = getLLVMStyle();
2748 Style.IndentCaseLabels = true;
2749 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
2750 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2751 Style.BraceWrapping.AfterCaseLabel = true;
2752 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2753 EXPECT_EQ("switch (n)\n"
2754 "{\n"
2755 " case 0:\n"
2756 " {\n"
2757 " return false;\n"
2758 " }\n"
2759 " default:\n"
2760 " {\n"
2761 " return true;\n"
2762 " }\n"
2763 "}",
2764 format("switch (n) {\n"
2765 " case 0: {\n"
2766 " return false;\n"
2767 " }\n"
2768 " default: {\n"
2769 " return true;\n"
2770 " }\n"
2771 "}",
2772 Style));
2773 Style.BraceWrapping.AfterCaseLabel = false;
2774 EXPECT_EQ("switch (n)\n"
2775 "{\n"
2776 " case 0: {\n"
2777 " return false;\n"
2778 " }\n"
2779 " default: {\n"
2780 " return true;\n"
2781 " }\n"
2782 "}",
2783 format("switch (n) {\n"
2784 " case 0:\n"
2785 " {\n"
2786 " return false;\n"
2787 " }\n"
2788 " default:\n"
2789 " {\n"
2790 " return true;\n"
2791 " }\n"
2792 "}",
2793 Style));
2794 Style.IndentCaseLabels = false;
2795 Style.IndentCaseBlocks = true;
2796 EXPECT_EQ("switch (n)\n"
2797 "{\n"
2798 "case 0:\n"
2799 " {\n"
2800 " return false;\n"
2801 " }\n"
2802 "case 1:\n"
2803 " break;\n"
2804 "default:\n"
2805 " {\n"
2806 " return true;\n"
2807 " }\n"
2808 "}",
2809 format("switch (n) {\n"
2810 "case 0: {\n"
2811 " return false;\n"
2812 "}\n"
2813 "case 1:\n"
2814 " break;\n"
2815 "default: {\n"
2816 " return true;\n"
2817 "}\n"
2818 "}",
2819 Style));
2820 Style.IndentCaseLabels = true;
2821 Style.IndentCaseBlocks = true;
2822 EXPECT_EQ("switch (n)\n"
2823 "{\n"
2824 " case 0:\n"
2825 " {\n"
2826 " return false;\n"
2827 " }\n"
2828 " case 1:\n"
2829 " break;\n"
2830 " default:\n"
2831 " {\n"
2832 " return true;\n"
2833 " }\n"
2834 "}",
2835 format("switch (n) {\n"
2836 "case 0: {\n"
2837 " return false;\n"
2838 "}\n"
2839 "case 1:\n"
2840 " break;\n"
2841 "default: {\n"
2842 " return true;\n"
2843 "}\n"
2844 "}",
2845 Style));
2846 }
2847
2848 TEST_F(FormatTest, CaseRanges) {
2849 verifyFormat("switch (x) {\n"
2850 "case 'A' ... 'Z':\n"
2851 "case 1 ... 5:\n"
2852 "case a ... b:\n"
2853 " break;\n"
2854 "}");
2855 }
2856
2857 TEST_F(FormatTest, ShortEnums) {
2858 FormatStyle Style = getLLVMStyle();
2859 EXPECT_TRUE(Style.AllowShortEnumsOnASingleLine);
2860 EXPECT_FALSE(Style.BraceWrapping.AfterEnum);
2861 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2862 verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2863 Style.AllowShortEnumsOnASingleLine = false;
2864 verifyFormat("enum {\n"
2865 " A,\n"
2866 " B,\n"
2867 " C\n"
2868 "} ShortEnum1, ShortEnum2;",
2869 Style);
2870 verifyFormat("typedef enum {\n"
2871 " A,\n"
2872 " B,\n"
2873 " C\n"
2874 "} ShortEnum1, ShortEnum2;",
2875 Style);
2876 verifyFormat("enum {\n"
2877 " A,\n"
2878 "} ShortEnum1, ShortEnum2;",
2879 Style);
2880 verifyFormat("typedef enum {\n"
2881 " A,\n"
2882 "} ShortEnum1, ShortEnum2;",
2883 Style);
2884 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2885 Style.BraceWrapping.AfterEnum = true;
2886 verifyFormat("enum\n"
2887 "{\n"
2888 " A,\n"
2889 " B,\n"
2890 " C\n"
2891 "} ShortEnum1, ShortEnum2;",
2892 Style);
2893 verifyFormat("typedef enum\n"
2894 "{\n"
2895 " A,\n"
2896 " B,\n"
2897 " C\n"
2898 "} ShortEnum1, ShortEnum2;",
2899 Style);
2900 }
2901
2902 TEST_F(FormatTest, ShortCaseLabels) {
2903 FormatStyle Style = getLLVMStyle();
2904 Style.AllowShortCaseLabelsOnASingleLine = true;
2905 verifyFormat("switch (a) {\n"
2906 "case 1: x = 1; break;\n"
2907 "case 2: return;\n"
2908 "case 3:\n"
2909 "case 4:\n"
2910 "case 5: return;\n"
2911 "case 6: // comment\n"
2912 " return;\n"
2913 "case 7:\n"
2914 " // comment\n"
2915 " return;\n"
2916 "case 8:\n"
2917 " x = 8; // comment\n"
2918 " break;\n"
2919 "default: y = 1; break;\n"
2920 "}",
2921 Style);
2922 verifyFormat("switch (a) {\n"
2923 "case 0: return; // comment\n"
2924 "case 1: break; // comment\n"
2925 "case 2: return;\n"
2926 "// comment\n"
2927 "case 3: return;\n"
2928 "// comment 1\n"
2929 "// comment 2\n"
2930 "// comment 3\n"
2931 "case 4: break; /* comment */\n"
2932 "case 5:\n"
2933 " // comment\n"
2934 " break;\n"
2935 "case 6: /* comment */ x = 1; break;\n"
2936 "case 7: x = /* comment */ 1; break;\n"
2937 "case 8:\n"
2938 " x = 1; /* comment */\n"
2939 " break;\n"
2940 "case 9:\n"
2941 " break; // comment line 1\n"
2942 " // comment line 2\n"
2943 "}",
2944 Style);
2945 EXPECT_EQ("switch (a) {\n"
2946 "case 1:\n"
2947 " x = 8;\n"
2948 " // fall through\n"
2949 "case 2: x = 8;\n"
2950 "// comment\n"
2951 "case 3:\n"
2952 " return; /* comment line 1\n"
2953 " * comment line 2 */\n"
2954 "case 4: i = 8;\n"
2955 "// something else\n"
2956 "#if FOO\n"
2957 "case 5: break;\n"
2958 "#endif\n"
2959 "}",
2960 format("switch (a) {\n"
2961 "case 1: x = 8;\n"
2962 " // fall through\n"
2963 "case 2:\n"
2964 " x = 8;\n"
2965 "// comment\n"
2966 "case 3:\n"
2967 " return; /* comment line 1\n"
2968 " * comment line 2 */\n"
2969 "case 4:\n"
2970 " i = 8;\n"
2971 "// something else\n"
2972 "#if FOO\n"
2973 "case 5: break;\n"
2974 "#endif\n"
2975 "}",
2976 Style));
2977 EXPECT_EQ("switch (a) {\n"
2978 "case 0:\n"
2979 " return; // long long long long long long long long long long "
2980 "long long comment\n"
2981 " // line\n"
2982 "}",
2983 format("switch (a) {\n"
2984 "case 0: return; // long long long long long long long long "
2985 "long long long long comment line\n"
2986 "}",
2987 Style));
2988 EXPECT_EQ("switch (a) {\n"
2989 "case 0:\n"
2990 " return; /* long long long long long long long long long long "
2991 "long long comment\n"
2992 " line */\n"
2993 "}",
2994 format("switch (a) {\n"
2995 "case 0: return; /* long long long long long long long long "
2996 "long long long long comment line */\n"
2997 "}",
2998 Style));
2999 verifyFormat("switch (a) {\n"
3000 "#if FOO\n"
3001 "case 0: return 0;\n"
3002 "#endif\n"
3003 "}",
3004 Style);
3005 verifyFormat("switch (a) {\n"
3006 "case 1: {\n"
3007 "}\n"
3008 "case 2: {\n"
3009 " return;\n"
3010 "}\n"
3011 "case 3: {\n"
3012 " x = 1;\n"
3013 " return;\n"
3014 "}\n"
3015 "case 4:\n"
3016 " if (x)\n"
3017 " return;\n"
3018 "}",
3019 Style);
3020 Style.ColumnLimit = 21;
3021 verifyFormat("#define X \\\n"
3022 " case 0: break;\n"
3023 "#include \"f\"",
3024 Style);
3025 verifyFormat("switch (a) {\n"
3026 "case 1: x = 1; break;\n"
3027 "case 2: return;\n"
3028 "case 3:\n"
3029 "case 4:\n"
3030 "case 5: return;\n"
3031 "default:\n"
3032 " y = 1;\n"
3033 " break;\n"
3034 "}",
3035 Style);
3036 Style.ColumnLimit = 80;
3037 Style.AllowShortCaseLabelsOnASingleLine = false;
3038 Style.IndentCaseLabels = true;
3039 EXPECT_EQ("switch (n) {\n"
3040 " default /*comments*/:\n"
3041 " return true;\n"
3042 " case 0:\n"
3043 " return false;\n"
3044 "}",
3045 format("switch (n) {\n"
3046 "default/*comments*/:\n"
3047 " return true;\n"
3048 "case 0:\n"
3049 " return false;\n"
3050 "}",
3051 Style));
3052 Style.AllowShortCaseLabelsOnASingleLine = true;
3053 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3054 Style.BraceWrapping.AfterCaseLabel = true;
3055 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
3056 EXPECT_EQ("switch (n)\n"
3057 "{\n"
3058 " case 0:\n"
3059 " {\n"
3060 " return false;\n"
3061 " }\n"
3062 " default:\n"
3063 " {\n"
3064 " return true;\n"
3065 " }\n"
3066 "}",
3067 format("switch (n) {\n"
3068 " case 0: {\n"
3069 " return false;\n"
3070 " }\n"
3071 " default:\n"
3072 " {\n"
3073 " return true;\n"
3074 " }\n"
3075 "}",
3076 Style));
3077 }
3078
3079 TEST_F(FormatTest, FormatsLabels) {
3080 verifyFormat("void f() {\n"
3081 " some_code();\n"
3082 "test_label:\n"
3083 " some_other_code();\n"
3084 " {\n"
3085 " some_more_code();\n"
3086 " another_label:\n"
3087 " some_more_code();\n"
3088 " }\n"
3089 "}");
3090 verifyFormat("{\n"
3091 " some_code();\n"
3092 "test_label:\n"
3093 " some_other_code();\n"
3094 "}");
3095 verifyFormat("{\n"
3096 " some_code();\n"
3097 "test_label:;\n"
3098 " int i = 0;\n"
3099 "}");
3100 FormatStyle Style = getLLVMStyle();
3101 Style.IndentGotoLabels = false;
3102 verifyFormat("void f() {\n"
3103 " some_code();\n"
3104 "test_label:\n"
3105 " some_other_code();\n"
3106 " {\n"
3107 " some_more_code();\n"
3108 "another_label:\n"
3109 " some_more_code();\n"
3110 " }\n"
3111 "}",
3112 Style);
3113 verifyFormat("{\n"
3114 " some_code();\n"
3115 "test_label:\n"
3116 " some_other_code();\n"
3117 "}",
3118 Style);
3119 verifyFormat("{\n"
3120 " some_code();\n"
3121 "test_label:;\n"
3122 " int i = 0;\n"
3123 "}");
3124 }
3125
3126 TEST_F(FormatTest, MultiLineControlStatements) {
3127 FormatStyle Style = getLLVMStyleWithColumns(20);
3128 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
3129 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
3130 // Short lines should keep opening brace on same line.
3131 EXPECT_EQ("if (foo) {\n"
3132 " bar();\n"
3133 "}",
3134 format("if(foo){bar();}", Style));
3135 EXPECT_EQ("if (foo) {\n"
3136 " bar();\n"
3137 "} else {\n"
3138 " baz();\n"
3139 "}",
3140 format("if(foo){bar();}else{baz();}", Style));
3141 EXPECT_EQ("if (foo && bar) {\n"
3142 " baz();\n"
3143 "}",
3144 format("if(foo&&bar){baz();}", Style));
3145 EXPECT_EQ("if (foo) {\n"
3146 " bar();\n"
3147 "} else if (baz) {\n"
3148 " quux();\n"
3149 "}",
3150 format("if(foo){bar();}else if(baz){quux();}", Style));
3151 EXPECT_EQ(
3152 "if (foo) {\n"
3153 " bar();\n"
3154 "} else if (baz) {\n"
3155 " quux();\n"
3156 "} else {\n"
3157 " foobar();\n"
3158 "}",
3159 format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style));
3160 EXPECT_EQ("for (;;) {\n"
3161 " foo();\n"
3162 "}",
3163 format("for(;;){foo();}"));
3164 EXPECT_EQ("while (1) {\n"
3165 " foo();\n"
3166 "}",
3167 format("while(1){foo();}", Style));
3168 EXPECT_EQ("switch (foo) {\n"
3169 "case bar:\n"
3170 " return;\n"
3171 "}",
3172 format("switch(foo){case bar:return;}", Style));
3173 EXPECT_EQ("try {\n"
3174 " foo();\n"
3175 "} catch (...) {\n"
3176 " bar();\n"
3177 "}",
3178 format("try{foo();}catch(...){bar();}", Style));
3179 EXPECT_EQ("do {\n"
3180 " foo();\n"
3181 "} while (bar &&\n"
3182 " baz);",
3183 format("do{foo();}while(bar&&baz);", Style));
3184 // Long lines should put opening brace on new line.
3185 verifyFormat("void f() {\n"
3186 " if (a1 && a2 &&\n"
3187 " a3)\n"
3188 " {\n"
3189 " quux();\n"
3190 " }\n"
3191 "}",
3192 "void f(){if(a1&&a2&&a3){quux();}}", Style);
3193 EXPECT_EQ("if (foo && bar &&\n"
3194 " baz)\n"
3195 "{\n"
3196 " quux();\n"
3197 "}",
3198 format("if(foo&&bar&&baz){quux();}", Style));
3199 EXPECT_EQ("if (foo && bar &&\n"
3200 " baz)\n"
3201 "{\n"
3202 " quux();\n"
3203 "}",
3204 format("if (foo && bar &&\n"
3205 " baz) {\n"
3206 " quux();\n"
3207 "}",
3208 Style));
3209 EXPECT_EQ("if (foo) {\n"
3210 " bar();\n"
3211 "} else if (baz ||\n"
3212 " quux)\n"
3213 "{\n"
3214 " foobar();\n"
3215 "}",
3216 format("if(foo){bar();}else if(baz||quux){foobar();}", Style));
3217 EXPECT_EQ(
3218 "if (foo) {\n"
3219 " bar();\n"
3220 "} else if (baz ||\n"
3221 " quux)\n"
3222 "{\n"
3223 " foobar();\n"
3224 "} else {\n"
3225 " barbaz();\n"
3226 "}",
3227 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
3228 Style));
3229 EXPECT_EQ("for (int i = 0;\n"
3230 " i < 10; ++i)\n"
3231 "{\n"
3232 " foo();\n"
3233 "}",
3234 format("for(int i=0;i<10;++i){foo();}", Style));
3235 EXPECT_EQ("foreach (int i,\n"
3236 " list)\n"
3237 "{\n"
3238 " foo();\n"
3239 "}",
3240 format("foreach(int i, list){foo();}", Style));
3241 Style.ColumnLimit =
3242 40; // to concentrate at brace wrapping, not line wrap due to column limit
3243 EXPECT_EQ("foreach (int i, list) {\n"
3244 " foo();\n"
3245 "}",
3246 format("foreach(int i, list){foo();}", Style));
3247 Style.ColumnLimit =
3248 20; // to concentrate at brace wrapping, not line wrap due to column limit
3249 EXPECT_EQ("while (foo || bar ||\n"
3250 " baz)\n"
3251 "{\n"
3252 " quux();\n"
3253 "}",
3254 format("while(foo||bar||baz){quux();}", Style));
3255 EXPECT_EQ("switch (\n"
3256 " foo = barbaz)\n"
3257 "{\n"
3258 "case quux:\n"
3259 " return;\n"
3260 "}",
3261 format("switch(foo=barbaz){case quux:return;}", Style));
3262 EXPECT_EQ("try {\n"
3263 " foo();\n"
3264 "} catch (\n"
3265 " Exception &bar)\n"
3266 "{\n"
3267 " baz();\n"
3268 "}",
3269 format("try{foo();}catch(Exception&bar){baz();}", Style));
3270 Style.ColumnLimit =
3271 40; // to concentrate at brace wrapping, not line wrap due to column limit
3272 EXPECT_EQ("try {\n"
3273 " foo();\n"
3274 "} catch (Exception &bar) {\n"
3275 " baz();\n"
3276 "}",
3277 format("try{foo();}catch(Exception&bar){baz();}", Style));
3278 Style.ColumnLimit =
3279 20; // to concentrate at brace wrapping, not line wrap due to column limit
3280
3281 Style.BraceWrapping.BeforeElse = true;
3282 EXPECT_EQ(
3283 "if (foo) {\n"
3284 " bar();\n"
3285 "}\n"
3286 "else if (baz ||\n"
3287 " quux)\n"
3288 "{\n"
3289 " foobar();\n"
3290 "}\n"
3291 "else {\n"
3292 " barbaz();\n"
3293 "}",
3294 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
3295 Style));
3296
3297 Style.BraceWrapping.BeforeCatch = true;
3298 EXPECT_EQ("try {\n"
3299 " foo();\n"
3300 "}\n"
3301 "catch (...) {\n"
3302 " baz();\n"
3303 "}",
3304 format("try{foo();}catch(...){baz();}", Style));
3305
3306 Style.BraceWrapping.AfterFunction = true;
3307 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
3308 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3309 Style.ColumnLimit = 80;
3310 verifyFormat("void shortfunction() { bar(); }", Style);
3311
3312 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3313 verifyFormat("void shortfunction()\n"
3314 "{\n"
3315 " bar();\n"
3316 "}",
3317 Style);
3318 }
3319
3320 TEST_F(FormatTest, BeforeWhile) {
3321 FormatStyle Style = getLLVMStyle();
3322 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
3323
3324 verifyFormat("do {\n"
3325 " foo();\n"
3326 "} while (1);",
3327 Style);
3328 Style.BraceWrapping.BeforeWhile = true;
3329 verifyFormat("do {\n"
3330 " foo();\n"
3331 "}\n"
3332 "while (1);",
3333 Style);
3334 }
3335
3336 //===----------------------------------------------------------------------===//
3337 // Tests for classes, namespaces, etc.
3338 //===----------------------------------------------------------------------===//
3339
3340 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
3341 verifyFormat("class A {};");
3342 }
3343
3344 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
3345 verifyFormat("class A {\n"
3346 "public:\n"
3347 "public: // comment\n"
3348 "protected:\n"
3349 "private:\n"
3350 " void f() {}\n"
3351 "};");
3352 verifyFormat("export class A {\n"
3353 "public:\n"
3354 "public: // comment\n"
3355 "protected:\n"
3356 "private:\n"
3357 " void f() {}\n"
3358 "};");
3359 verifyGoogleFormat("class A {\n"
3360 " public:\n"
3361 " protected:\n"
3362 " private:\n"
3363 " void f() {}\n"
3364 "};");
3365 verifyGoogleFormat("export class A {\n"
3366 " public:\n"
3367 " protected:\n"
3368 " private:\n"
3369 " void f() {}\n"
3370 "};");
3371 verifyFormat("class A {\n"
3372 "public slots:\n"
3373 " void f1() {}\n"
3374 "public Q_SLOTS:\n"
3375 " void f2() {}\n"
3376 "protected slots:\n"
3377 " void f3() {}\n"
3378 "protected Q_SLOTS:\n"
3379 " void f4() {}\n"
3380 "private slots:\n"
3381 " void f5() {}\n"
3382 "private Q_SLOTS:\n"
3383 " void f6() {}\n"
3384 "signals:\n"
3385 " void g1();\n"
3386 "Q_SIGNALS:\n"
3387 " void g2();\n"
3388 "};");
3389
3390 // Don't interpret 'signals' the wrong way.
3391 verifyFormat("signals.set();");
3392 verifyFormat("for (Signals signals : f()) {\n}");
3393 verifyFormat("{\n"
3394 " signals.set(); // This needs indentation.\n"
3395 "}");
3396 verifyFormat("void f() {\n"
3397 "label:\n"
3398 " signals.baz();\n"
3399 "}");
3400 verifyFormat("private[1];");
3401 verifyFormat("testArray[public] = 1;");
3402 verifyFormat("public();");
3403 verifyFormat("myFunc(public);");
3404 verifyFormat("std::vector<int> testVec = {private};");
3405 verifyFormat("private.p = 1;");
3406 verifyFormat("void function(private...){};");
3407 verifyFormat("if (private && public)\n");
3408 verifyFormat("private &= true;");
3409 verifyFormat("int x = private * public;");
3410 verifyFormat("public *= private;");
3411 verifyFormat("int x = public + private;");
3412 verifyFormat("private++;");
3413 verifyFormat("++private;");
3414 verifyFormat("public += private;");
3415 verifyFormat("public = public - private;");
3416 verifyFormat("public->foo();");
3417 verifyFormat("private--;");
3418 verifyFormat("--private;");
3419 verifyFormat("public -= 1;");
3420 verifyFormat("if (!private && !public)\n");
3421 verifyFormat("public != private;");
3422 verifyFormat("int x = public / private;");
3423 verifyFormat("public /= 2;");
3424 verifyFormat("public = public % 2;");
3425 verifyFormat("public %= 2;");
3426 verifyFormat("if (public < private)\n");
3427 verifyFormat("public << private;");
3428 verifyFormat("public <<= private;");
3429 verifyFormat("if (public > private)\n");
3430 verifyFormat("public >> private;");
3431 verifyFormat("public >>= private;");
3432 verifyFormat("public ^ private;");
3433 verifyFormat("public ^= private;");
3434 verifyFormat("public | private;");
3435 verifyFormat("public |= private;");
3436 verifyFormat("auto x = private ? 1 : 2;");
3437 verifyFormat("if (public == private)\n");
3438 verifyFormat("void foo(public, private)");
3439 verifyFormat("public::foo();");
3440
3441 verifyFormat("class A {\n"
3442 "public:\n"
3443 " std::unique_ptr<int *[]> b() { return nullptr; }\n"
3444 "\n"
3445 "private:\n"
3446 " int c;\n"
3447 "};\n"
3448 "class B {\n"
3449 "public:\n"
3450 " std::unique_ptr<int *[] /* okay */> b() { return nullptr; }\n"
3451 "\n"
3452 "private:\n"
3453 " int c;\n"
3454 "};");
3455 }
3456
3457 TEST_F(FormatTest, SeparatesLogicalBlocks) {
3458 EXPECT_EQ("class A {\n"
3459 "public:\n"
3460 " void f();\n"
3461 "\n"
3462 "private:\n"
3463 " void g() {}\n"
3464 " // test\n"
3465 "protected:\n"
3466 " int h;\n"
3467 "};",
3468 format("class A {\n"
3469 "public:\n"
3470 "void f();\n"
3471 "private:\n"
3472 "void g() {}\n"
3473 "// test\n"
3474 "protected:\n"
3475 "int h;\n"
3476 "};"));
3477 EXPECT_EQ("class A {\n"
3478 "protected:\n"
3479 "public:\n"
3480 " void f();\n"
3481 "};",
3482 format("class A {\n"
3483 "protected:\n"
3484 "\n"
3485 "public:\n"
3486 "\n"
3487 " void f();\n"
3488 "};"));
3489
3490 // Even ensure proper spacing inside macros.
3491 EXPECT_EQ("#define B \\\n"
3492 " class A { \\\n"
3493 " protected: \\\n"
3494 " public: \\\n"
3495 " void f(); \\\n"
3496 " };",
3497 format("#define B \\\n"
3498 " class A { \\\n"
3499 " protected: \\\n"
3500 " \\\n"
3501 " public: \\\n"
3502 " \\\n"
3503 " void f(); \\\n"
3504 " };",
3505 getGoogleStyle()));
3506 // But don't remove empty lines after macros ending in access specifiers.
3507 EXPECT_EQ("#define A private:\n"
3508 "\n"
3509 "int i;",
3510 format("#define A private:\n"
3511 "\n"
3512 "int i;"));
3513 }
3514
3515 TEST_F(FormatTest, FormatsClasses) {
3516 verifyFormat("class A : public B {};");
3517 verifyFormat("class A : public ::B {};");
3518
3519 verifyFormat(
3520 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3521 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3522 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3523 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3524 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3525 verifyFormat(
3526 "class A : public B, public C, public D, public E, public F {};");
3527 verifyFormat("class AAAAAAAAAAAA : public B,\n"
3528 " public C,\n"
3529 " public D,\n"
3530 " public E,\n"
3531 " public F,\n"
3532 " public G {};");
3533
3534 verifyFormat("class\n"
3535 " ReallyReallyLongClassName {\n"
3536 " int i;\n"
3537 "};",
3538 getLLVMStyleWithColumns(32));
3539 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3540 " aaaaaaaaaaaaaaaa> {};");
3541 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
3542 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
3543 " aaaaaaaaaaaaaaaaaaaaaa> {};");
3544 verifyFormat("template <class R, class C>\n"
3545 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
3546 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
3547 verifyFormat("class ::A::B {};");
3548 }
3549
3550 TEST_F(FormatTest, BreakInheritanceStyle) {
3551 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
3552 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
3553 FormatStyle::BILS_BeforeComma;
3554 verifyFormat("class MyClass : public X {};",
3555 StyleWithInheritanceBreakBeforeComma);
3556 verifyFormat("class MyClass\n"
3557 " : public X\n"
3558 " , public Y {};",
3559 StyleWithInheritanceBreakBeforeComma);
3560 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
3561 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
3562 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3563 StyleWithInheritanceBreakBeforeComma);
3564 verifyFormat("struct aaaaaaaaaaaaa\n"
3565 " : public aaaaaaaaaaaaaaaaaaa< // break\n"
3566 " aaaaaaaaaaaaaaaa> {};",
3567 StyleWithInheritanceBreakBeforeComma);
3568
3569 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
3570 StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
3571 FormatStyle::BILS_AfterColon;
3572 verifyFormat("class MyClass : public X {};",
3573 StyleWithInheritanceBreakAfterColon);
3574 verifyFormat("class MyClass : public X, public Y {};",
3575 StyleWithInheritanceBreakAfterColon);
3576 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
3577 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3578 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3579 StyleWithInheritanceBreakAfterColon);
3580 verifyFormat("struct aaaaaaaaaaaaa :\n"
3581 " public aaaaaaaaaaaaaaaaaaa< // break\n"
3582 " aaaaaaaaaaaaaaaa> {};",
3583 StyleWithInheritanceBreakAfterColon);
3584
3585 FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle();
3586 StyleWithInheritanceBreakAfterComma.BreakInheritanceList =
3587 FormatStyle::BILS_AfterComma;
3588 verifyFormat("class MyClass : public X {};",
3589 StyleWithInheritanceBreakAfterComma);
3590 verifyFormat("class MyClass : public X,\n"
3591 " public Y {};",
3592 StyleWithInheritanceBreakAfterComma);
3593 verifyFormat(
3594 "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3595 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC "
3596 "{};",
3597 StyleWithInheritanceBreakAfterComma);
3598 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3599 " aaaaaaaaaaaaaaaa> {};",
3600 StyleWithInheritanceBreakAfterComma);
3601 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3602 " : public OnceBreak,\n"
3603 " public AlwaysBreak,\n"
3604 " EvenBasesFitInOneLine {};",
3605 StyleWithInheritanceBreakAfterComma);
3606 }
3607
3608 TEST_F(FormatTest, FormatsVariableDeclarationsAfterRecord) {
3609 verifyFormat("class A {\n} a, b;");
3610 verifyFormat("struct A {\n} a, b;");
3611 verifyFormat("union A {\n} a, b;");
3612
3613 verifyFormat("constexpr class A {\n} a, b;");
3614 verifyFormat("constexpr struct A {\n} a, b;");
3615 verifyFormat("constexpr union A {\n} a, b;");
3616
3617 verifyFormat("namespace {\nclass A {\n} a, b;\n} // namespace");
3618 verifyFormat("namespace {\nstruct A {\n} a, b;\n} // namespace");
3619 verifyFormat("namespace {\nunion A {\n} a, b;\n} // namespace");
3620
3621 verifyFormat("namespace {\nconstexpr class A {\n} a, b;\n} // namespace");
3622 verifyFormat("namespace {\nconstexpr struct A {\n} a, b;\n} // namespace");
3623 verifyFormat("namespace {\nconstexpr union A {\n} a, b;\n} // namespace");
3624
3625 verifyFormat("namespace ns {\n"
3626 "class {\n"
3627 "} a, b;\n"
3628 "} // namespace ns");
3629 verifyFormat("namespace ns {\n"
3630 "const class {\n"
3631 "} a, b;\n"
3632 "} // namespace ns");
3633 verifyFormat("namespace ns {\n"
3634 "constexpr class C {\n"
3635 "} a, b;\n"
3636 "} // namespace ns");
3637 verifyFormat("namespace ns {\n"
3638 "class { /* comment */\n"
3639 "} a, b;\n"
3640 "} // namespace ns");
3641 verifyFormat("namespace ns {\n"
3642 "const class { /* comment */\n"
3643 "} a, b;\n"
3644 "} // namespace ns");
3645 }
3646
3647 TEST_F(FormatTest, FormatsEnum) {
3648 verifyFormat("enum {\n"
3649 " Zero,\n"
3650 " One = 1,\n"
3651 " Two = One + 1,\n"
3652 " Three = (One + Two),\n"
3653 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3654 " Five = (One, Two, Three, Four, 5)\n"
3655 "};");
3656 verifyGoogleFormat("enum {\n"
3657 " Zero,\n"
3658 " One = 1,\n"
3659 " Two = One + 1,\n"
3660 " Three = (One + Two),\n"
3661 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3662 " Five = (One, Two, Three, Four, 5)\n"
3663 "};");
3664 verifyFormat("enum Enum {};");
3665 verifyFormat("enum {};");
3666 verifyFormat("enum X E {} d;");
3667 verifyFormat("enum __attribute__((...)) E {} d;");
3668 verifyFormat("enum __declspec__((...)) E {} d;");
3669 verifyFormat("enum [[nodiscard]] E {} d;");
3670 verifyFormat("enum {\n"
3671 " Bar = Foo<int, int>::value\n"
3672 "};",
3673 getLLVMStyleWithColumns(30));
3674
3675 verifyFormat("enum ShortEnum { A, B, C };");
3676 verifyGoogleFormat("enum ShortEnum { A, B, C };");
3677
3678 EXPECT_EQ("enum KeepEmptyLines {\n"
3679 " ONE,\n"
3680 "\n"
3681 " TWO,\n"
3682 "\n"
3683 " THREE\n"
3684 "}",
3685 format("enum KeepEmptyLines {\n"
3686 " ONE,\n"
3687 "\n"
3688 " TWO,\n"
3689 "\n"
3690 "\n"
3691 " THREE\n"
3692 "}"));
3693 verifyFormat("enum E { // comment\n"
3694 " ONE,\n"
3695 " TWO\n"
3696 "};\n"
3697 "int i;");
3698
3699 FormatStyle EightIndent = getLLVMStyle();
3700 EightIndent.IndentWidth = 8;
3701 verifyFormat("enum {\n"
3702 " VOID,\n"
3703 " CHAR,\n"
3704 " SHORT,\n"
3705 " INT,\n"
3706 " LONG,\n"
3707 " SIGNED,\n"
3708 " UNSIGNED,\n"
3709 " BOOL,\n"
3710 " FLOAT,\n"
3711 " DOUBLE,\n"
3712 " COMPLEX\n"
3713 "};",
3714 EightIndent);
3715
3716 verifyFormat("enum [[nodiscard]] E {\n"
3717 " ONE,\n"
3718 " TWO,\n"
3719 "};");
3720 verifyFormat("enum [[nodiscard]] E {\n"
3721 " // Comment 1\n"
3722 " ONE,\n"
3723 " // Comment 2\n"
3724 " TWO,\n"
3725 "};");
3726
3727 // Not enums.
3728 verifyFormat("enum X f() {\n"
3729 " a();\n"
3730 " return 42;\n"
3731 "}");
3732 verifyFormat("enum X Type::f() {\n"
3733 " a();\n"
3734 " return 42;\n"
3735 "}");
3736 verifyFormat("enum ::X f() {\n"
3737 " a();\n"
3738 " return 42;\n"
3739 "}");
3740 verifyFormat("enum ns::X f() {\n"
3741 " a();\n"
3742 " return 42;\n"
3743 "}");
3744 }
3745
3746 TEST_F(FormatTest, FormatsEnumsWithErrors) {
3747 verifyFormat("enum Type {\n"
3748 " One = 0; // These semicolons should be commas.\n"
3749 " Two = 1;\n"
3750 "};");
3751 verifyFormat("namespace n {\n"
3752 "enum Type {\n"
3753 " One,\n"
3754 " Two, // missing };\n"
3755 " int i;\n"
3756 "}\n"
3757 "void g() {}");
3758 }
3759
3760 TEST_F(FormatTest, FormatsEnumStruct) {
3761 verifyFormat("enum struct {\n"
3762 " Zero,\n"
3763 " One = 1,\n"
3764 " Two = One + 1,\n"
3765 " Three = (One + Two),\n"
3766 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3767 " Five = (One, Two, Three, Four, 5)\n"
3768 "};");
3769 verifyFormat("enum struct Enum {};");
3770 verifyFormat("enum struct {};");
3771 verifyFormat("enum struct X E {} d;");
3772 verifyFormat("enum struct __attribute__((...)) E {} d;");
3773 verifyFormat("enum struct __declspec__((...)) E {} d;");
3774 verifyFormat("enum struct [[nodiscard]] E {} d;");
3775 verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
3776
3777 verifyFormat("enum struct [[nodiscard]] E {\n"
3778 " ONE,\n"
3779 " TWO,\n"
3780 "};");
3781 verifyFormat("enum struct [[nodiscard]] E {\n"
3782 " // Comment 1\n"
3783 " ONE,\n"
3784 " // Comment 2\n"
3785 " TWO,\n"
3786 "};");
3787 }
3788
3789 TEST_F(FormatTest, FormatsEnumClass) {
3790 verifyFormat("enum class {\n"
3791 " Zero,\n"
3792 " One = 1,\n"
3793 " Two = One + 1,\n"
3794 " Three = (One + Two),\n"
3795 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3796 " Five = (One, Two, Three, Four, 5)\n"
3797 "};");
3798 verifyFormat("enum class Enum {};");
3799 verifyFormat("enum class {};");
3800 verifyFormat("enum class X E {} d;");
3801 verifyFormat("enum class __attribute__((...)) E {} d;");
3802 verifyFormat("enum class __declspec__((...)) E {} d;");
3803 verifyFormat("enum class [[nodiscard]] E {} d;");
3804 verifyFormat("enum class X f() {\n a();\n return 42;\n}");
3805
3806 verifyFormat("enum class [[nodiscard]] E {\n"
3807 " ONE,\n"
3808 " TWO,\n"
3809 "};");
3810 verifyFormat("enum class [[nodiscard]] E {\n"
3811 " // Comment 1\n"
3812 " ONE,\n"
3813 " // Comment 2\n"
3814 " TWO,\n"
3815 "};");
3816 }
3817
3818 TEST_F(FormatTest, FormatsEnumTypes) {
3819 verifyFormat("enum X : int {\n"
3820 " A, // Force multiple lines.\n"
3821 " B\n"
3822 "};");
3823 verifyFormat("enum X : int { A, B };");
3824 verifyFormat("enum X : std::uint32_t { A, B };");
3825 }
3826
3827 TEST_F(FormatTest, FormatsTypedefEnum) {
3828 FormatStyle Style = getLLVMStyleWithColumns(40);
3829 verifyFormat("typedef enum {} EmptyEnum;");
3830 verifyFormat("typedef enum { A, B, C } ShortEnum;");
3831 verifyFormat("typedef enum {\n"
3832 " ZERO = 0,\n"
3833 " ONE = 1,\n"
3834 " TWO = 2,\n"
3835 " THREE = 3\n"
3836 "} LongEnum;",
3837 Style);
3838 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3839 Style.BraceWrapping.AfterEnum = true;
3840 verifyFormat("typedef enum {} EmptyEnum;");
3841 verifyFormat("typedef enum { A, B, C } ShortEnum;");
3842 verifyFormat("typedef enum\n"
3843 "{\n"
3844 " ZERO = 0,\n"
3845 " ONE = 1,\n"
3846 " TWO = 2,\n"
3847 " THREE = 3\n"
3848 "} LongEnum;",
3849 Style);
3850 }
3851
3852 TEST_F(FormatTest, FormatsNSEnums) {
3853 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
3854 verifyGoogleFormat(
3855 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
3856 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
3857 " // Information about someDecentlyLongValue.\n"
3858 " someDecentlyLongValue,\n"
3859 " // Information about anotherDecentlyLongValue.\n"
3860 " anotherDecentlyLongValue,\n"
3861 " // Information about aThirdDecentlyLongValue.\n"
3862 " aThirdDecentlyLongValue\n"
3863 "};");
3864 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n"
3865 " // Information about someDecentlyLongValue.\n"
3866 " someDecentlyLongValue,\n"
3867 " // Information about anotherDecentlyLongValue.\n"
3868 " anotherDecentlyLongValue,\n"
3869 " // Information about aThirdDecentlyLongValue.\n"
3870 " aThirdDecentlyLongValue\n"
3871 "};");
3872 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
3873 " a = 1,\n"
3874 " b = 2,\n"
3875 " c = 3,\n"
3876 "};");
3877 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
3878 " a = 1,\n"
3879 " b = 2,\n"
3880 " c = 3,\n"
3881 "};");
3882 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
3883 " a = 1,\n"
3884 " b = 2,\n"
3885 " c = 3,\n"
3886 "};");
3887 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
3888 " a = 1,\n"
3889 " b = 2,\n"
3890 " c = 3,\n"
3891 "};");
3892 }
3893
3894 TEST_F(FormatTest, FormatsBitfields) {
3895 verifyFormat("struct Bitfields {\n"
3896 " unsigned sClass : 8;\n"
3897 " unsigned ValueKind : 2;\n"
3898 "};");
3899 verifyFormat("struct A {\n"
3900 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
3901 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
3902 "};");
3903 verifyFormat("struct MyStruct {\n"
3904 " uchar data;\n"
3905 " uchar : 8;\n"
3906 " uchar : 8;\n"
3907 " uchar other;\n"
3908 "};");
3909 FormatStyle Style = getLLVMStyle();
3910 Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
3911 verifyFormat("struct Bitfields {\n"
3912 " unsigned sClass:8;\n"
3913 " unsigned ValueKind:2;\n"
3914 " uchar other;\n"
3915 "};",
3916 Style);
3917 verifyFormat("struct A {\n"
3918 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n"
3919 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n"
3920 "};",
3921 Style);
3922 Style.BitFieldColonSpacing = FormatStyle::BFCS_Before;
3923 verifyFormat("struct Bitfields {\n"
3924 " unsigned sClass :8;\n"
3925 " unsigned ValueKind :2;\n"
3926 " uchar other;\n"
3927 "};",
3928 Style);
3929 Style.BitFieldColonSpacing = FormatStyle::BFCS_After;
3930 verifyFormat("struct Bitfields {\n"
3931 " unsigned sClass: 8;\n"
3932 " unsigned ValueKind: 2;\n"
3933 " uchar other;\n"
3934 "};",
3935 Style);
3936 }
3937
3938 TEST_F(FormatTest, FormatsNamespaces) {
3939 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
3940 LLVMWithNoNamespaceFix.FixNamespaceComments = false;
3941
3942 verifyFormat("namespace some_namespace {\n"
3943 "class A {};\n"
3944 "void f() { f(); }\n"
3945 "}",
3946 LLVMWithNoNamespaceFix);
3947 verifyFormat("#define M(x) x##x\n"
3948 "namespace M(x) {\n"
3949 "class A {};\n"
3950 "void f() { f(); }\n"
3951 "}",
3952 LLVMWithNoNamespaceFix);
3953 verifyFormat("#define M(x) x##x\n"
3954 "namespace N::inline M(x) {\n"
3955 "class A {};\n"
3956 "void f() { f(); }\n"
3957 "}",
3958 LLVMWithNoNamespaceFix);
3959 verifyFormat("#define M(x) x##x\n"
3960 "namespace M(x)::inline N {\n"
3961 "class A {};\n"
3962 "void f() { f(); }\n"
3963 "}",
3964 LLVMWithNoNamespaceFix);
3965 verifyFormat("#define M(x) x##x\n"
3966 "namespace N::M(x) {\n"
3967 "class A {};\n"
3968 "void f() { f(); }\n"
3969 "}",
3970 LLVMWithNoNamespaceFix);
3971 verifyFormat("#define M(x) x##x\n"
3972 "namespace M::N(x) {\n"
3973 "class A {};\n"
3974 "void f() { f(); }\n"
3975 "}",
3976 LLVMWithNoNamespaceFix);
3977 verifyFormat("namespace N::inline D {\n"
3978 "class A {};\n"
3979 "void f() { f(); }\n"
3980 "}",
3981 LLVMWithNoNamespaceFix);
3982 verifyFormat("namespace N::inline D::E {\n"
3983 "class A {};\n"
3984 "void f() { f(); }\n"
3985 "}",
3986 LLVMWithNoNamespaceFix);
3987 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
3988 "class A {};\n"
3989 "void f() { f(); }\n"
3990 "}",
3991 LLVMWithNoNamespaceFix);
3992 verifyFormat("/* something */ namespace some_namespace {\n"
3993 "class A {};\n"
3994 "void f() { f(); }\n"
3995 "}",
3996 LLVMWithNoNamespaceFix);
3997 verifyFormat("namespace {\n"
3998 "class A {};\n"
3999 "void f() { f(); }\n"
4000 "}",
4001 LLVMWithNoNamespaceFix);
4002 verifyFormat("/* something */ namespace {\n"
4003 "class A {};\n"
4004 "void f() { f(); }\n"
4005 "}",
4006 LLVMWithNoNamespaceFix);
4007 verifyFormat("inline namespace X {\n"
4008 "class A {};\n"
4009 "void f() { f(); }\n"
4010 "}",
4011 LLVMWithNoNamespaceFix);
4012 verifyFormat("/* something */ inline namespace X {\n"
4013 "class A {};\n"
4014 "void f() { f(); }\n"
4015 "}",
4016 LLVMWithNoNamespaceFix);
4017 verifyFormat("export namespace X {\n"
4018 "class A {};\n"
4019 "void f() { f(); }\n"
4020 "}",
4021 LLVMWithNoNamespaceFix);
4022 verifyFormat("using namespace some_namespace;\n"
4023 "class A {};\n"
4024 "void f() { f(); }",
4025 LLVMWithNoNamespaceFix);
4026
4027 // This code is more common than we thought; if we
4028 // layout this correctly the semicolon will go into
4029 // its own line, which is undesirable.
4030 verifyFormat("namespace {};", LLVMWithNoNamespaceFix);
4031 verifyFormat("namespace {\n"
4032 "class A {};\n"
4033 "};",
4034 LLVMWithNoNamespaceFix);
4035
4036 verifyFormat("namespace {\n"
4037 "int SomeVariable = 0; // comment\n"
4038 "} // namespace",
4039 LLVMWithNoNamespaceFix);
4040 EXPECT_EQ("#ifndef HEADER_GUARD\n"
4041 "#define HEADER_GUARD\n"
4042 "namespace my_namespace {\n"
4043 "int i;\n"
4044 "} // my_namespace\n"
4045 "#endif // HEADER_GUARD",
4046 format("#ifndef HEADER_GUARD\n"
4047 " #define HEADER_GUARD\n"
4048 " namespace my_namespace {\n"
4049 "int i;\n"
4050 "} // my_namespace\n"
4051 "#endif // HEADER_GUARD",
4052 LLVMWithNoNamespaceFix));
4053
4054 EXPECT_EQ("namespace A::B {\n"
4055 "class C {};\n"
4056 "}",
4057 format("namespace A::B {\n"
4058 "class C {};\n"
4059 "}",
4060 LLVMWithNoNamespaceFix));
4061
4062 FormatStyle Style = getLLVMStyle();
4063 Style.NamespaceIndentation = FormatStyle::NI_All;
4064 EXPECT_EQ("namespace out {\n"
4065 " int i;\n"
4066 " namespace in {\n"
4067 " int i;\n"
4068 " } // namespace in\n"
4069 "} // namespace out",
4070 format("namespace out {\n"
4071 "int i;\n"
4072 "namespace in {\n"
4073 "int i;\n"
4074 "} // namespace in\n"
4075 "} // namespace out",
4076 Style));
4077
4078 FormatStyle ShortInlineFunctions = getLLVMStyle();
4079 ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All;
4080 ShortInlineFunctions.AllowShortFunctionsOnASingleLine =
4081 FormatStyle::SFS_Inline;
4082 verifyFormat("namespace {\n"
4083 " void f() {\n"
4084 " return;\n"
4085 " }\n"
4086 "} // namespace\n",
4087 ShortInlineFunctions);
4088 verifyFormat("namespace { /* comment */\n"
4089 " void f() {\n"
4090 " return;\n"
4091 " }\n"
4092 "} // namespace\n",
4093 ShortInlineFunctions);
4094 verifyFormat("namespace { // comment\n"
4095 " void f() {\n"
4096 " return;\n"
4097 " }\n"
4098 "} // namespace\n",
4099 ShortInlineFunctions);
4100 verifyFormat("namespace {\n"
4101 " int some_int;\n"
4102 " void f() {\n"
4103 " return;\n"
4104 " }\n"
4105 "} // namespace\n",
4106 ShortInlineFunctions);
4107 verifyFormat("namespace interface {\n"
4108 " void f() {\n"
4109 " return;\n"
4110 " }\n"
4111 "} // namespace interface\n",
4112 ShortInlineFunctions);
4113 verifyFormat("namespace {\n"
4114 " class X {\n"
4115 " void f() { return; }\n"
4116 " };\n"
4117 "} // namespace\n",
4118 ShortInlineFunctions);
4119 verifyFormat("namespace {\n"
4120 " class X { /* comment */\n"
4121 " void f() { return; }\n"
4122 " };\n"
4123 "} // namespace\n",
4124 ShortInlineFunctions);
4125 verifyFormat("namespace {\n"
4126 " class X { // comment\n"
4127 " void f() { return; }\n"
4128 " };\n"
4129 "} // namespace\n",
4130 ShortInlineFunctions);
4131 verifyFormat("namespace {\n"
4132 " struct X {\n"
4133 " void f() { return; }\n"
4134 " };\n"
4135 "} // namespace\n",
4136 ShortInlineFunctions);
4137 verifyFormat("namespace {\n"
4138 " union X {\n"
4139 " void f() { return; }\n"
4140 " };\n"
4141 "} // namespace\n",
4142 ShortInlineFunctions);
4143 verifyFormat("extern \"C\" {\n"
4144 "void f() {\n"
4145 " return;\n"
4146 "}\n"
4147 "} // namespace\n",
4148 ShortInlineFunctions);
4149 verifyFormat("namespace {\n"
4150 " class X {\n"
4151 " void f() { return; }\n"
4152 " } x;\n"
4153 "} // namespace\n",
4154 ShortInlineFunctions);
4155 verifyFormat("namespace {\n"
4156 " [[nodiscard]] class X {\n"
4157 " void f() { return; }\n"
4158 " };\n"
4159 "} // namespace\n",
4160 ShortInlineFunctions);
4161 verifyFormat("namespace {\n"
4162 " static class X {\n"
4163 " void f() { return; }\n"
4164 " } x;\n"
4165 "} // namespace\n",
4166 ShortInlineFunctions);
4167 verifyFormat("namespace {\n"
4168 " constexpr class X {\n"
4169 " void f() { return; }\n"
4170 " } x;\n"
4171 "} // namespace\n",
4172 ShortInlineFunctions);
4173
4174 ShortInlineFunctions.IndentExternBlock = FormatStyle::IEBS_Indent;
4175 verifyFormat("extern \"C\" {\n"
4176 " void f() {\n"
4177 " return;\n"
4178 " }\n"
4179 "} // namespace\n",
4180 ShortInlineFunctions);
4181
4182 Style.NamespaceIndentation = FormatStyle::NI_Inner;
4183 EXPECT_EQ("namespace out {\n"
4184 "int i;\n"
4185 "namespace in {\n"
4186 " int i;\n"
4187 "} // namespace in\n"
4188 "} // namespace out",
4189 format("namespace out {\n"
4190 "int i;\n"
4191 "namespace in {\n"
4192 "int i;\n"
4193 "} // namespace in\n"
4194 "} // namespace out",
4195 Style));
4196
4197 Style.NamespaceIndentation = FormatStyle::NI_None;
4198 verifyFormat("template <class T>\n"
4199 "concept a_concept = X<>;\n"
4200 "namespace B {\n"
4201 "struct b_struct {};\n"
4202 "} // namespace B\n",
4203 Style);
4204 verifyFormat("template <int I>\n"
4205 "constexpr void foo()\n"
4206 " requires(I == 42)\n"
4207 "{}\n"
4208 "namespace ns {\n"
4209 "void foo() {}\n"
4210 "} // namespace ns\n",
4211 Style);
4212 }
4213
4214 TEST_F(FormatTest, NamespaceMacros) {
4215 FormatStyle Style = getLLVMStyle();
4216 Style.NamespaceMacros.push_back("TESTSUITE");
4217
4218 verifyFormat("TESTSUITE(A) {\n"
4219 "int foo();\n"
4220 "} // TESTSUITE(A)",
4221 Style);
4222
4223 verifyFormat("TESTSUITE(A, B) {\n"
4224 "int foo();\n"
4225 "} // TESTSUITE(A)",
4226 Style);
4227
4228 // Properly indent according to NamespaceIndentation style
4229 Style.NamespaceIndentation = FormatStyle::NI_All;
4230 verifyFormat("TESTSUITE(A) {\n"
4231 " int foo();\n"
4232 "} // TESTSUITE(A)",
4233 Style);
4234 verifyFormat("TESTSUITE(A) {\n"
4235 " namespace B {\n"
4236 " int foo();\n"
4237 " } // namespace B\n"
4238 "} // TESTSUITE(A)",
4239 Style);
4240 verifyFormat("namespace A {\n"
4241 " TESTSUITE(B) {\n"
4242 " int foo();\n"
4243 " } // TESTSUITE(B)\n"
4244 "} // namespace A",
4245 Style);
4246
4247 Style.NamespaceIndentation = FormatStyle::NI_Inner;
4248 verifyFormat("TESTSUITE(A) {\n"
4249 "TESTSUITE(B) {\n"
4250 " int foo();\n"
4251 "} // TESTSUITE(B)\n"
4252 "} // TESTSUITE(A)",
4253 Style);
4254 verifyFormat("TESTSUITE(A) {\n"
4255 "namespace B {\n"
4256 " int foo();\n"
4257 "} // namespace B\n"
4258 "} // TESTSUITE(A)",
4259 Style);
4260 verifyFormat("namespace A {\n"
4261 "TESTSUITE(B) {\n"
4262 " int foo();\n"
4263 "} // TESTSUITE(B)\n"
4264 "} // namespace A",
4265 Style);
4266
4267 // Properly merge namespace-macros blocks in CompactNamespaces mode
4268 Style.NamespaceIndentation = FormatStyle::NI_None;
4269 Style.CompactNamespaces = true;
4270 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
4271 "}} // TESTSUITE(A::B)",
4272 Style);
4273
4274 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
4275 "}} // TESTSUITE(out::in)",
4276 format("TESTSUITE(out) {\n"
4277 "TESTSUITE(in) {\n"
4278 "} // TESTSUITE(in)\n"
4279 "} // TESTSUITE(out)",
4280 Style));
4281
4282 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
4283 "}} // TESTSUITE(out::in)",
4284 format("TESTSUITE(out) {\n"
4285 "TESTSUITE(in) {\n"
4286 "} // TESTSUITE(in)\n"
4287 "} // TESTSUITE(out)",
4288 Style));
4289
4290 // Do not merge different namespaces/macros
4291 EXPECT_EQ("namespace out {\n"
4292 "TESTSUITE(in) {\n"
4293 "} // TESTSUITE(in)\n"
4294 "} // namespace out",
4295 format("namespace out {\n"
4296 "TESTSUITE(in) {\n"
4297 "} // TESTSUITE(in)\n"
4298 "} // namespace out",
4299 Style));
4300 EXPECT_EQ("TESTSUITE(out) {\n"
4301 "namespace in {\n"
4302 "} // namespace in\n"
4303 "} // TESTSUITE(out)",
4304 format("TESTSUITE(out) {\n"
4305 "namespace in {\n"
4306 "} // namespace in\n"
4307 "} // TESTSUITE(out)",
4308 Style));
4309 Style.NamespaceMacros.push_back("FOOBAR");
4310 EXPECT_EQ("TESTSUITE(out) {\n"
4311 "FOOBAR(in) {\n"
4312 "} // FOOBAR(in)\n"
4313 "} // TESTSUITE(out)",
4314 format("TESTSUITE(out) {\n"
4315 "FOOBAR(in) {\n"
4316 "} // FOOBAR(in)\n"
4317 "} // TESTSUITE(out)",
4318 Style));
4319 }
4320
4321 TEST_F(FormatTest, FormatsCompactNamespaces) {
4322 FormatStyle Style = getLLVMStyle();
4323 Style.CompactNamespaces = true;
4324 Style.NamespaceMacros.push_back("TESTSUITE");
4325
4326 verifyFormat("namespace A { namespace B {\n"
4327 "}} // namespace A::B",
4328 Style);
4329
4330 EXPECT_EQ("namespace out { namespace in {\n"
4331 "}} // namespace out::in",
4332 format("namespace out {\n"
4333 "namespace in {\n"
4334 "} // namespace in\n"
4335 "} // namespace out",
4336 Style));
4337
4338 // Only namespaces which have both consecutive opening and end get compacted
4339 EXPECT_EQ("namespace out {\n"
4340 "namespace in1 {\n"
4341 "} // namespace in1\n"
4342 "namespace in2 {\n"
4343 "} // namespace in2\n"
4344 "} // namespace out",
4345 format("namespace out {\n"
4346 "namespace in1 {\n"
4347 "} // namespace in1\n"
4348 "namespace in2 {\n"
4349 "} // namespace in2\n"
4350 "} // namespace out",
4351 Style));
4352
4353 EXPECT_EQ("namespace out {\n"
4354 "int i;\n"
4355 "namespace in {\n"
4356 "int j;\n"
4357 "} // namespace in\n"
4358 "int k;\n"
4359 "} // namespace out",
4360 format("namespace out { int i;\n"
4361 "namespace in { int j; } // namespace in\n"
4362 "int k; } // namespace out",
4363 Style));
4364
4365 EXPECT_EQ("namespace A { namespace B { namespace C {\n"
4366 "}}} // namespace A::B::C\n",
4367 format("namespace A { namespace B {\n"
4368 "namespace C {\n"
4369 "}} // namespace B::C\n"
4370 "} // namespace A\n",
4371 Style));
4372
4373 Style.ColumnLimit = 40;
4374 EXPECT_EQ("namespace aaaaaaaaaa {\n"
4375 "namespace bbbbbbbbbb {\n"
4376 "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
4377 format("namespace aaaaaaaaaa {\n"
4378 "namespace bbbbbbbbbb {\n"
4379 "} // namespace bbbbbbbbbb\n"
4380 "} // namespace aaaaaaaaaa",
4381 Style));
4382
4383 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
4384 "namespace cccccc {\n"
4385 "}}} // namespace aaaaaa::bbbbbb::cccccc",
4386 format("namespace aaaaaa {\n"
4387 "namespace bbbbbb {\n"
4388 "namespace cccccc {\n"
4389 "} // namespace cccccc\n"
4390 "} // namespace bbbbbb\n"
4391 "} // namespace aaaaaa",
4392 Style));
4393 Style.ColumnLimit = 80;
4394
4395 // Extra semicolon after 'inner' closing brace prevents merging
4396 EXPECT_EQ("namespace out { namespace in {\n"
4397 "}; } // namespace out::in",
4398 format("namespace out {\n"
4399 "namespace in {\n"
4400 "}; // namespace in\n"
4401 "} // namespace out",
4402 Style));
4403
4404 // Extra semicolon after 'outer' closing brace is conserved
4405 EXPECT_EQ("namespace out { namespace in {\n"
4406 "}}; // namespace out::in",
4407 format("namespace out {\n"
4408 "namespace in {\n"
4409 "} // namespace in\n"
4410 "}; // namespace out",
4411 Style));
4412
4413 Style.NamespaceIndentation = FormatStyle::NI_All;
4414 EXPECT_EQ("namespace out { namespace in {\n"
4415 " int i;\n"
4416 "}} // namespace out::in",
4417 format("namespace out {\n"
4418 "namespace in {\n"
4419 "int i;\n"
4420 "} // namespace in\n"
4421 "} // namespace out",
4422 Style));
4423 EXPECT_EQ("namespace out { namespace mid {\n"
4424 " namespace in {\n"
4425 " int j;\n"
4426 " } // namespace in\n"
4427 " int k;\n"
4428 "}} // namespace out::mid",
4429 format("namespace out { namespace mid {\n"
4430 "namespace in { int j; } // namespace in\n"
4431 "int k; }} // namespace out::mid",
4432 Style));
4433
4434 Style.NamespaceIndentation = FormatStyle::NI_Inner;
4435 EXPECT_EQ("namespace out { namespace in {\n"
4436 " int i;\n"
4437 "}} // namespace out::in",
4438 format("namespace out {\n"
4439 "namespace in {\n"
4440 "int i;\n"
4441 "} // namespace in\n"
4442 "} // namespace out",
4443 Style));
4444 EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
4445 " int i;\n"
4446 "}}} // namespace out::mid::in",
4447 format("namespace out {\n"
4448 "namespace mid {\n"
4449 "namespace in {\n"
4450 "int i;\n"
4451 "} // namespace in\n"
4452 "} // namespace mid\n"
4453 "} // namespace out",
4454 Style));
4455
4456 Style.CompactNamespaces = true;
4457 Style.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
4458 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4459 Style.BraceWrapping.BeforeLambdaBody = true;
4460 verifyFormat("namespace out { namespace in {\n"
4461 "}} // namespace out::in",
4462 Style);
4463 EXPECT_EQ("namespace out { namespace in {\n"
4464 "}} // namespace out::in",
4465 format("namespace out {\n"
4466 "namespace in {\n"
4467 "} // namespace in\n"
4468 "} // namespace out",
4469 Style));
4470 }
4471
4472 TEST_F(FormatTest, FormatsExternC) {
4473 verifyFormat("extern \"C\" {\nint a;");
4474 verifyFormat("extern \"C\" {}");
4475 verifyFormat("extern \"C\" {\n"
4476 "int foo();\n"
4477 "}");
4478 verifyFormat("extern \"C\" int foo() {}");
4479 verifyFormat("extern \"C\" int foo();");
4480 verifyFormat("extern \"C\" int foo() {\n"
4481 " int i = 42;\n"
4482 " return i;\n"
4483 "}");
4484
4485 FormatStyle Style = getLLVMStyle();
4486 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4487 Style.BraceWrapping.AfterFunction = true;
4488 verifyFormat("extern \"C\" int foo() {}", Style);
4489 verifyFormat("extern \"C\" int foo();", Style);
4490 verifyFormat("extern \"C\" int foo()\n"
4491 "{\n"
4492 " int i = 42;\n"
4493 " return i;\n"
4494 "}",
4495 Style);
4496
4497 Style.BraceWrapping.AfterExternBlock = true;
4498 Style.BraceWrapping.SplitEmptyRecord = false;
4499 verifyFormat("extern \"C\"\n"
4500 "{}",
4501 Style);
4502 verifyFormat("extern \"C\"\n"
4503 "{\n"
4504 " int foo();\n"
4505 "}",
4506 Style);
4507 }
4508
4509 TEST_F(FormatTest, IndentExternBlockStyle) {
4510 FormatStyle Style = getLLVMStyle();
4511 Style.IndentWidth = 2;
4512
4513 Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4514 verifyFormat("extern \"C\" { /*9*/\n"
4515 "}",
4516 Style);
4517 verifyFormat("extern \"C\" {\n"
4518 " int foo10();\n"
4519 "}",
4520 Style);
4521
4522 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4523 verifyFormat("extern \"C\" { /*11*/\n"
4524 "}",
4525 Style);
4526 verifyFormat("extern \"C\" {\n"
4527 "int foo12();\n"
4528 "}",
4529 Style);
4530
4531 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4532 Style.BraceWrapping.AfterExternBlock = true;
4533 Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4534 verifyFormat("extern \"C\"\n"
4535 "{ /*13*/\n"
4536 "}",
4537 Style);
4538 verifyFormat("extern \"C\"\n{\n"
4539 " int foo14();\n"
4540 "}",
4541 Style);
4542
4543 Style.BraceWrapping.AfterExternBlock = false;
4544 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4545 verifyFormat("extern \"C\" { /*15*/\n"
4546 "}",
4547 Style);
4548 verifyFormat("extern \"C\" {\n"
4549 "int foo16();\n"
4550 "}",
4551 Style);
4552
4553 Style.BraceWrapping.AfterExternBlock = true;
4554 verifyFormat("extern \"C\"\n"
4555 "{ /*13*/\n"
4556 "}",
4557 Style);
4558 verifyFormat("extern \"C\"\n"
4559 "{\n"
4560 "int foo14();\n"
4561 "}",
4562 Style);
4563
4564 Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4565 verifyFormat("extern \"C\"\n"
4566 "{ /*13*/\n"
4567 "}",
4568 Style);
4569 verifyFormat("extern \"C\"\n"
4570 "{\n"
4571 " int foo14();\n"
4572 "}",
4573 Style);
4574 }
4575
4576 TEST_F(FormatTest, FormatsInlineASM) {
4577 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
4578 verifyFormat("asm(\"nop\" ::: \"memory\");");
4579 verifyFormat(
4580 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
4581 " \"cpuid\\n\\t\"\n"
4582 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
4583 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
4584 " : \"a\"(value));");
4585 EXPECT_EQ(
4586 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
4587 " __asm {\n"
4588 " mov edx,[that] // vtable in edx\n"
4589 " mov eax,methodIndex\n"
4590 " call [edx][eax*4] // stdcall\n"
4591 " }\n"
4592 "}",
4593 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
4594 " __asm {\n"
4595 " mov edx,[that] // vtable in edx\n"
4596 " mov eax,methodIndex\n"
4597 " call [edx][eax*4] // stdcall\n"
4598 " }\n"
4599 "}"));
4600 EXPECT_EQ("_asm {\n"
4601 " xor eax, eax;\n"
4602 " cpuid;\n"
4603 "}",
4604 format("_asm {\n"
4605 " xor eax, eax;\n"
4606 " cpuid;\n"
4607 "}"));
4608 verifyFormat("void function() {\n"
4609 " // comment\n"
4610 " asm(\"\");\n"
4611 "}");
4612 EXPECT_EQ("__asm {\n"
4613 "}\n"
4614 "int i;",
4615 format("__asm {\n"
4616 "}\n"
4617 "int i;"));
4618 }
4619
4620 TEST_F(FormatTest, FormatTryCatch) {
4621 verifyFormat("try {\n"
4622 " throw a * b;\n"
4623 "} catch (int a) {\n"
4624 " // Do nothing.\n"
4625 "} catch (...) {\n"
4626 " exit(42);\n"
4627 "}");
4628
4629 // Function-level try statements.
4630 verifyFormat("int f() try { return 4; } catch (...) {\n"
4631 " return 5;\n"
4632 "}");
4633 verifyFormat("class A {\n"
4634 " int a;\n"
4635 " A() try : a(0) {\n"
4636 " } catch (...) {\n"
4637 " throw;\n"
4638 " }\n"
4639 "};\n");
4640 verifyFormat("class A {\n"
4641 " int a;\n"
4642 " A() try : a(0), b{1} {\n"
4643 " } catch (...) {\n"
4644 " throw;\n"
4645 " }\n"
4646 "};\n");
4647 verifyFormat("class A {\n"
4648 " int a;\n"
4649 " A() try : a(0), b{1}, c{2} {\n"
4650 " } catch (...) {\n"
4651 " throw;\n"
4652 " }\n"
4653 "};\n");
4654 verifyFormat("class A {\n"
4655 " int a;\n"
4656 " A() try : a(0), b{1}, c{2} {\n"
4657 " { // New scope.\n"
4658 " }\n"
4659 " } catch (...) {\n"
4660 " throw;\n"
4661 " }\n"
4662 "};\n");
4663
4664 // Incomplete try-catch blocks.
4665 verifyIncompleteFormat("try {} catch (");
4666 }
4667
4668 TEST_F(FormatTest, FormatTryAsAVariable) {
4669 verifyFormat("int try;");
4670 verifyFormat("int try, size;");
4671 verifyFormat("try = foo();");
4672 verifyFormat("if (try < size) {\n return true;\n}");
4673
4674 verifyFormat("int catch;");
4675 verifyFormat("int catch, size;");
4676 verifyFormat("catch = foo();");
4677 verifyFormat("if (catch < size) {\n return true;\n}");
4678
4679 FormatStyle Style = getLLVMStyle();
4680 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4681 Style.BraceWrapping.AfterFunction = true;
4682 Style.BraceWrapping.BeforeCatch = true;
4683 verifyFormat("try {\n"
4684 " int bar = 1;\n"
4685 "}\n"
4686 "catch (...) {\n"
4687 " int bar = 1;\n"
4688 "}",
4689 Style);
4690 verifyFormat("#if NO_EX\n"
4691 "try\n"
4692 "#endif\n"
4693 "{\n"
4694 "}\n"
4695 "#if NO_EX\n"
4696 "catch (...) {\n"
4697 "}",
4698 Style);
4699 verifyFormat("try /* abc */ {\n"
4700 " int bar = 1;\n"
4701 "}\n"
4702 "catch (...) {\n"
4703 " int bar = 1;\n"
4704 "}",
4705 Style);
4706 verifyFormat("try\n"
4707 "// abc\n"
4708 "{\n"
4709 " int bar = 1;\n"
4710 "}\n"
4711 "catch (...) {\n"
4712 " int bar = 1;\n"
4713 "}",
4714 Style);
4715 }
4716
4717 TEST_F(FormatTest, FormatSEHTryCatch) {
4718 verifyFormat("__try {\n"
4719 " int a = b * c;\n"
4720 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
4721 " // Do nothing.\n"
4722 "}");
4723
4724 verifyFormat("__try {\n"
4725 " int a = b * c;\n"
4726 "} __finally {\n"
4727 " // Do nothing.\n"
4728 "}");
4729
4730 verifyFormat("DEBUG({\n"
4731 " __try {\n"
4732 " } __finally {\n"
4733 " }\n"
4734 "});\n");
4735 }
4736
4737 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
4738 verifyFormat("try {\n"
4739 " f();\n"
4740 "} catch {\n"
4741 " g();\n"
4742 "}");
4743 verifyFormat("try {\n"
4744 " f();\n"
4745 "} catch (A a) MACRO(x) {\n"
4746 " g();\n"
4747 "} catch (B b) MACRO(x) {\n"
4748 " g();\n"
4749 "}");
4750 }
4751
4752 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
4753 FormatStyle Style = getLLVMStyle();
4754 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
4755 FormatStyle::BS_WebKit}) {
4756 Style.BreakBeforeBraces = BraceStyle;
4757 verifyFormat("try {\n"
4758 " // something\n"
4759 "} catch (...) {\n"
4760 " // something\n"
4761 "}",
4762 Style);
4763 }
4764 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4765 verifyFormat("try {\n"
4766 " // something\n"
4767 "}\n"
4768 "catch (...) {\n"
4769 " // something\n"
4770 "}",
4771 Style);
4772 verifyFormat("__try {\n"
4773 " // something\n"
4774 "}\n"
4775 "__finally {\n"
4776 " // something\n"
4777 "}",
4778 Style);
4779 verifyFormat("@try {\n"
4780 " // something\n"
4781 "}\n"
4782 "@finally {\n"
4783 " // something\n"
4784 "}",
4785 Style);
4786 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4787 verifyFormat("try\n"
4788 "{\n"
4789 " // something\n"
4790 "}\n"
4791 "catch (...)\n"
4792 "{\n"
4793 " // something\n"
4794 "}",
4795 Style);
4796 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
4797 verifyFormat("try\n"
4798 " {\n"
4799 " // something white\n"
4800 " }\n"
4801 "catch (...)\n"
4802 " {\n"
4803 " // something white\n"
4804 " }",
4805 Style);
4806 Style.BreakBeforeBraces = FormatStyle::BS_GNU;
4807 verifyFormat("try\n"
4808 " {\n"
4809 " // something\n"
4810 " }\n"
4811 "catch (...)\n"
4812 " {\n"
4813 " // something\n"
4814 " }",
4815 Style);
4816 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4817 Style.BraceWrapping.BeforeCatch = true;
4818 verifyFormat("try {\n"
4819 " // something\n"
4820 "}\n"
4821 "catch (...) {\n"
4822 " // something\n"
4823 "}",
4824 Style);
4825 }
4826
4827 TEST_F(FormatTest, StaticInitializers) {
4828 verifyFormat("static SomeClass SC = {1, 'a'};");
4829
4830 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
4831 " 100000000, "
4832 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
4833
4834 // Here, everything other than the "}" would fit on a line.
4835 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
4836 " 10000000000000000000000000};");
4837 EXPECT_EQ("S s = {a,\n"
4838 "\n"
4839 " b};",
4840 format("S s = {\n"
4841 " a,\n"
4842 "\n"
4843 " b\n"
4844 "};"));
4845
4846 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
4847 // line. However, the formatting looks a bit off and this probably doesn't
4848 // happen often in practice.
4849 verifyFormat("static int Variable[1] = {\n"
4850 " {1000000000000000000000000000000000000}};",
4851 getLLVMStyleWithColumns(40));
4852 }
4853
4854 TEST_F(FormatTest, DesignatedInitializers) {
4855 verifyFormat("const struct A a = {.a = 1, .b = 2};");
4856 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
4857 " .bbbbbbbbbb = 2,\n"
4858 " .cccccccccc = 3,\n"
4859 " .dddddddddd = 4,\n"
4860 " .eeeeeeeeee = 5};");
4861 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4862 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
4863 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
4864 " .ccccccccccccccccccccccccccc = 3,\n"
4865 " .ddddddddddddddddddddddddddd = 4,\n"
4866 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
4867
4868 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
4869
4870 verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
4871 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
4872 " [2] = bbbbbbbbbb,\n"
4873 " [3] = cccccccccc,\n"
4874 " [4] = dddddddddd,\n"
4875 " [5] = eeeeeeeeee};");
4876 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4877 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4878 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
4879 " [3] = cccccccccccccccccccccccccccccccccccccc,\n"
4880 " [4] = dddddddddddddddddddddddddddddddddddddd,\n"
4881 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
4882 }
4883
4884 TEST_F(FormatTest, NestedStaticInitializers) {
4885 verifyFormat("static A x = {{{}}};\n");
4886 verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
4887 " {init1, init2, init3, init4}}};",
4888 getLLVMStyleWithColumns(50));
4889
4890 verifyFormat("somes Status::global_reps[3] = {\n"
4891 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4892 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4893 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
4894 getLLVMStyleWithColumns(60));
4895 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
4896 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4897 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4898 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
4899 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
4900 " {rect.fRight - rect.fLeft, rect.fBottom - "
4901 "rect.fTop}};");
4902
4903 verifyFormat(
4904 "SomeArrayOfSomeType a = {\n"
4905 " {{1, 2, 3},\n"
4906 " {1, 2, 3},\n"
4907 " {111111111111111111111111111111, 222222222222222222222222222222,\n"
4908 " 333333333333333333333333333333},\n"
4909 " {1, 2, 3},\n"
4910 " {1, 2, 3}}};");
4911 verifyFormat(
4912 "SomeArrayOfSomeType a = {\n"
4913 " {{1, 2, 3}},\n"
4914 " {{1, 2, 3}},\n"
4915 " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
4916 " 333333333333333333333333333333}},\n"
4917 " {{1, 2, 3}},\n"
4918 " {{1, 2, 3}}};");
4919
4920 verifyFormat("struct {\n"
4921 " unsigned bit;\n"
4922 " const char *const name;\n"
4923 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
4924 " {kOsWin, \"Windows\"},\n"
4925 " {kOsLinux, \"Linux\"},\n"
4926 " {kOsCrOS, \"Chrome OS\"}};");
4927 verifyFormat("struct {\n"
4928 " unsigned bit;\n"
4929 " const char *const name;\n"
4930 "} kBitsToOs[] = {\n"
4931 " {kOsMac, \"Mac\"},\n"
4932 " {kOsWin, \"Windows\"},\n"
4933 " {kOsLinux, \"Linux\"},\n"
4934 " {kOsCrOS, \"Chrome OS\"},\n"
4935 "};");
4936 }
4937
4938 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
4939 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
4940 " \\\n"
4941 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
4942 }
4943
4944 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
4945 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
4946 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
4947
4948 // Do break defaulted and deleted functions.
4949 verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4950 " default;",
4951 getLLVMStyleWithColumns(40));
4952 verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4953 " delete;",
4954 getLLVMStyleWithColumns(40));
4955 }
4956
4957 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
4958 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
4959 getLLVMStyleWithColumns(40));
4960 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4961 getLLVMStyleWithColumns(40));
4962 EXPECT_EQ("#define Q \\\n"
4963 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
4964 " \"aaaaaaaa.cpp\"",
4965 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4966 getLLVMStyleWithColumns(40)));
4967 }
4968
4969 TEST_F(FormatTest, UnderstandsLinePPDirective) {
4970 EXPECT_EQ("# 123 \"A string literal\"",
4971 format(" # 123 \"A string literal\""));
4972 }
4973
4974 TEST_F(FormatTest, LayoutUnknownPPDirective) {
4975 EXPECT_EQ("#;", format("#;"));
4976 verifyFormat("#\n;\n;\n;");
4977 }
4978
4979 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
4980 EXPECT_EQ("#line 42 \"test\"\n",
4981 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
4982 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n",
4983 getLLVMStyleWithColumns(12)));
4984 }
4985
4986 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
4987 EXPECT_EQ("#line 42 \"test\"",
4988 format("# \\\n line \\\n 42 \\\n \"test\""));
4989 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B"));
4990 }
4991
4992 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
4993 verifyFormat("#define A \\x20");
4994 verifyFormat("#define A \\ x20");
4995 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20"));
4996 verifyFormat("#define A ''");
4997 verifyFormat("#define A ''qqq");
4998 verifyFormat("#define A `qqq");
4999 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
5000 EXPECT_EQ("const char *c = STRINGIFY(\n"
5001 "\\na : b);",
5002 format("const char * c = STRINGIFY(\n"
5003 "\\na : b);"));
5004
5005 verifyFormat("a\r\\");
5006 verifyFormat("a\v\\");
5007 verifyFormat("a\f\\");
5008 }
5009
5010 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
5011 FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp);
5012 style.IndentWidth = 4;
5013 style.PPIndentWidth = 1;
5014
5015 style.IndentPPDirectives = FormatStyle::PPDIS_None;
5016 verifyFormat("#ifdef __linux__\n"
5017 "void foo() {\n"
5018 " int x = 0;\n"
5019 "}\n"
5020 "#define FOO\n"
5021 "#endif\n"
5022 "void bar() {\n"
5023 " int y = 0;\n"
5024 "}\n",
5025 style);
5026
5027 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
5028 verifyFormat("#ifdef __linux__\n"
5029 "void foo() {\n"
5030 " int x = 0;\n"
5031 "}\n"
5032 "# define FOO foo\n"
5033 "#endif\n"
5034 "void bar() {\n"
5035 " int y = 0;\n"
5036 "}\n",
5037 style);
5038
5039 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
5040 verifyFormat("#ifdef __linux__\n"
5041 "void foo() {\n"
5042 " int x = 0;\n"
5043 "}\n"
5044 " #define FOO foo\n"
5045 "#endif\n"
5046 "void bar() {\n"
5047 " int y = 0;\n"
5048 "}\n",
5049 style);
5050 verifyFormat("#if 1\n"
5051 " // some comments\n"
5052 " // another\n"
5053 " #define foo 1\n"
5054 "// not a define comment\n"
5055 "void bar() {\n"
5056 " // comment\n"
5057 " int y = 0;\n"
5058 "}",
5059 "#if 1\n"
5060 "// some comments\n"
5061 "// another\n"
5062 "#define foo 1\n"
5063 "// not a define comment\n"
5064 "void bar() {\n"
5065 " // comment\n"
5066 " int y = 0;\n"
5067 "}",
5068 style);
5069
5070 style.IndentPPDirectives = FormatStyle::PPDIS_None;
5071 verifyFormat("#ifdef foo\n"
5072 "#define bar() \\\n"
5073 " if (A) { \\\n"
5074 " B(); \\\n"
5075 " } \\\n"
5076 " C();\n"
5077 "#endif",
5078 style);
5079 verifyFormat("if (emacs) {\n"
5080 "#ifdef is\n"
5081 "#define lit \\\n"
5082 " if (af) { \\\n"
5083 " return duh(); \\\n"
5084 " }\n"
5085 "#endif\n"
5086 "}",
5087 style);
5088 verifyFormat("#if abc\n"
5089 "#ifdef foo\n"
5090 "#define bar() \\\n"
5091 " if (A) { \\\n"
5092 " if (B) { \\\n"
5093 " C(); \\\n"
5094 " } \\\n"
5095 " } \\\n"
5096 " D();\n"
5097 "#endif\n"
5098 "#endif",
5099 style);
5100 verifyFormat("#ifndef foo\n"
5101 "#define foo\n"
5102 "if (emacs) {\n"
5103 "#ifdef is\n"
5104 "#define lit \\\n"
5105 " if (af) { \\\n"
5106 " return duh(); \\\n"
5107 " }\n"
5108 "#endif\n"
5109 "}\n"
5110 "#endif",
5111 style);
5112 verifyFormat("#if 1\n"
5113 "#define X \\\n"
5114 " { \\\n"
5115 " x; \\\n"
5116 " x; \\\n"
5117 " }\n"
5118 "#endif",
5119 style);
5120 verifyFormat("#define X \\\n"
5121 " { \\\n"
5122 " x; \\\n"
5123 " x; \\\n"
5124 " }",
5125 style);
5126
5127 style.PPIndentWidth = 2;
5128 verifyFormat("#ifdef foo\n"
5129 "#define bar() \\\n"
5130 " if (A) { \\\n"
5131 " B(); \\\n"
5132 " } \\\n"
5133 " C();\n"
5134 "#endif",
5135 style);
5136 style.IndentWidth = 8;
5137 verifyFormat("#ifdef foo\n"
5138 "#define bar() \\\n"
5139 " if (A) { \\\n"
5140 " B(); \\\n"
5141 " } \\\n"
5142 " C();\n"
5143 "#endif",
5144 style);
5145
5146 style.IndentWidth = 1;
5147 style.PPIndentWidth = 4;
5148 verifyFormat("#if 1\n"
5149 "#define X \\\n"
5150 " { \\\n"
5151 " x; \\\n"
5152 " x; \\\n"
5153 " }\n"
5154 "#endif",
5155 style);
5156 verifyFormat("#define X \\\n"
5157 " { \\\n"
5158 " x; \\\n"
5159 " x; \\\n"
5160 " }",
5161 style);
5162
5163 style.IndentWidth = 4;
5164 style.PPIndentWidth = 1;
5165 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
5166 verifyFormat("#ifdef foo\n"
5167 "# define bar() \\\n"
5168 " if (A) { \\\n"
5169 " B(); \\\n"
5170 " } \\\n"
5171 " C();\n"
5172 "#endif",
5173 style);
5174 verifyFormat("#if abc\n"
5175 "# ifdef foo\n"
5176 "# define bar() \\\n"
5177 " if (A) { \\\n"
5178 " if (B) { \\\n"
5179 " C(); \\\n"
5180 " } \\\n"
5181 " } \\\n"
5182 " D();\n"
5183 "# endif\n"
5184 "#endif",
5185 style);
5186 verifyFormat("#ifndef foo\n"
5187 "#define foo\n"
5188 "if (emacs) {\n"
5189 "#ifdef is\n"
5190 "# define lit \\\n"
5191 " if (af) { \\\n"
5192 " return duh(); \\\n"
5193 " }\n"
5194 "#endif\n"
5195 "}\n"
5196 "#endif",
5197 style);
5198 verifyFormat("#define X \\\n"
5199 " { \\\n"
5200 " x; \\\n"
5201 " x; \\\n"
5202 " }",
5203 style);
5204
5205 style.PPIndentWidth = 2;
5206 style.IndentWidth = 8;
5207 verifyFormat("#ifdef foo\n"
5208 "# define bar() \\\n"
5209 " if (A) { \\\n"
5210 " B(); \\\n"
5211 " } \\\n"
5212 " C();\n"
5213 "#endif",
5214 style);
5215
5216 style.PPIndentWidth = 4;
5217 style.IndentWidth = 1;
5218 verifyFormat("#define X \\\n"
5219 " { \\\n"
5220 " x; \\\n"
5221 " x; \\\n"
5222 " }",
5223 style);
5224
5225 style.IndentWidth = 4;
5226 style.PPIndentWidth = 1;
5227 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
5228 verifyFormat("if (emacs) {\n"
5229 "#ifdef is\n"
5230 " #define lit \\\n"
5231 " if (af) { \\\n"
5232 " return duh(); \\\n"
5233 " }\n"
5234 "#endif\n"
5235 "}",
5236 style);
5237 verifyFormat("#if abc\n"
5238 " #ifdef foo\n"
5239 " #define bar() \\\n"
5240 " if (A) { \\\n"
5241 " B(); \\\n"
5242 " } \\\n"
5243 " C();\n"
5244 " #endif\n"
5245 "#endif",
5246 style);
5247 verifyFormat("#if 1\n"
5248 " #define X \\\n"
5249 " { \\\n"
5250 " x; \\\n"
5251 " x; \\\n"
5252 " }\n"
5253 "#endif",
5254 style);
5255
5256 style.PPIndentWidth = 2;
5257 verifyFormat("#ifdef foo\n"
5258 " #define bar() \\\n"
5259 " if (A) { \\\n"
5260 " B(); \\\n"
5261 " } \\\n"
5262 " C();\n"
5263 "#endif",
5264 style);
5265
5266 style.PPIndentWidth = 4;
5267 style.IndentWidth = 1;
5268 verifyFormat("#if 1\n"
5269 " #define X \\\n"
5270 " { \\\n"
5271 " x; \\\n"
5272 " x; \\\n"
5273 " }\n"
5274 "#endif",
5275 style);
5276 }
5277
5278 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
5279 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
5280 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
5281 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
5282 // FIXME: We never break before the macro name.
5283 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
5284
5285 verifyFormat("#define A A\n#define A A");
5286 verifyFormat("#define A(X) A\n#define A A");
5287
5288 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
5289 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
5290 }
5291
5292 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
5293 EXPECT_EQ("// somecomment\n"
5294 "#include \"a.h\"\n"
5295 "#define A( \\\n"
5296 " A, B)\n"
5297 "#include \"b.h\"\n"
5298 "// somecomment\n",
5299 format(" // somecomment\n"
5300 " #include \"a.h\"\n"
5301 "#define A(A,\\\n"
5302 " B)\n"
5303 " #include \"b.h\"\n"
5304 " // somecomment\n",
5305 getLLVMStyleWithColumns(13)));
5306 }
5307
5308 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
5309
5310 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
5311 EXPECT_EQ("#define A \\\n"
5312 " c; \\\n"
5313 " e;\n"
5314 "f;",
5315 format("#define A c; e;\n"
5316 "f;",
5317 getLLVMStyleWithColumns(14)));
5318 }
5319
5320 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
5321
5322 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
5323 EXPECT_EQ("int x,\n"
5324 "#define A\n"
5325 " y;",
5326 format("int x,\n#define A\ny;"));
5327 }
5328
5329 TEST_F(FormatTest, HashInMacroDefinition) {
5330 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
5331 EXPECT_EQ("#define A(c) u#c", format("#define A(c) u#c", getLLVMStyle()));
5332 EXPECT_EQ("#define A(c) U#c", format("#define A(c) U#c", getLLVMStyle()));
5333 EXPECT_EQ("#define A(c) u8#c", format("#define A(c) u8#c", getLLVMStyle()));
5334 EXPECT_EQ("#define A(c) LR#c", format("#define A(c) LR#c", getLLVMStyle()));
5335 EXPECT_EQ("#define A(c) uR#c", format("#define A(c) uR#c", getLLVMStyle()));
5336 EXPECT_EQ("#define A(c) UR#c", format("#define A(c) UR#c", getLLVMStyle()));
5337 EXPECT_EQ("#define A(c) u8R#c", format("#define A(c) u8R#c", getLLVMStyle()));
5338 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
5339 verifyFormat("#define A \\\n"
5340 " { \\\n"
5341 " f(#c); \\\n"
5342 " }",
5343 getLLVMStyleWithColumns(11));
5344
5345 verifyFormat("#define A(X) \\\n"
5346 " void function##X()",
5347 getLLVMStyleWithColumns(22));
5348
5349 verifyFormat("#define A(a, b, c) \\\n"
5350 " void a##b##c()",
5351 getLLVMStyleWithColumns(22));
5352
5353 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
5354 }
5355
5356 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
5357 EXPECT_EQ("#define A (x)", format("#define A (x)"));
5358 EXPECT_EQ("#define A(x)", format("#define A(x)"));
5359
5360 FormatStyle Style = getLLVMStyle();
5361 Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
5362 verifyFormat("#define true ((foo)1)", Style);
5363 Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
5364 verifyFormat("#define false((foo)0)", Style);
5365 }
5366
5367 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
5368 EXPECT_EQ("#define A b;", format("#define A \\\n"
5369 " \\\n"
5370 " b;",
5371 getLLVMStyleWithColumns(25)));
5372 EXPECT_EQ("#define A \\\n"
5373 " \\\n"
5374 " a; \\\n"
5375 " b;",
5376 format("#define A \\\n"
5377 " \\\n"
5378 " a; \\\n"
5379 " b;",
5380 getLLVMStyleWithColumns(11)));
5381 EXPECT_EQ("#define A \\\n"
5382 " a; \\\n"
5383 " \\\n"
5384 " b;",
5385 format("#define A \\\n"
5386 " a; \\\n"
5387 " \\\n"
5388 " b;",
5389 getLLVMStyleWithColumns(11)));
5390 }
5391
5392 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
5393 verifyIncompleteFormat("#define A :");
5394 verifyFormat("#define SOMECASES \\\n"
5395 " case 1: \\\n"
5396 " case 2\n",
5397 getLLVMStyleWithColumns(20));
5398 verifyFormat("#define MACRO(a) \\\n"
5399 " if (a) \\\n"
5400 " f(); \\\n"
5401 " else \\\n"
5402 " g()",
5403 getLLVMStyleWithColumns(18));
5404 verifyFormat("#define A template <typename T>");
5405 verifyIncompleteFormat("#define STR(x) #x\n"
5406 "f(STR(this_is_a_string_literal{));");
5407 verifyFormat("#pragma omp threadprivate( \\\n"
5408 " y)), // expected-warning",
5409 getLLVMStyleWithColumns(28));
5410 verifyFormat("#d, = };");
5411 verifyFormat("#if \"a");
5412 verifyIncompleteFormat("({\n"
5413 "#define b \\\n"
5414 " } \\\n"
5415 " a\n"
5416 "a",
5417 getLLVMStyleWithColumns(15));
5418 verifyFormat("#define A \\\n"
5419 " { \\\n"
5420 " {\n"
5421 "#define B \\\n"
5422 " } \\\n"
5423 " }",
5424 getLLVMStyleWithColumns(15));
5425 verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
5426 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
5427 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
5428 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}");
5429 verifyNoCrash("#else\n"
5430 "#else\n"
5431 "#endif\n"
5432 "#endif");
5433 verifyNoCrash("#else\n"
5434 "#if X\n"
5435 "#endif\n"
5436 "#endif");
5437 verifyNoCrash("#else\n"
5438 "#endif\n"
5439 "#if X\n"
5440 "#endif");
5441 verifyNoCrash("#if X\n"
5442 "#else\n"
5443 "#else\n"
5444 "#endif\n"
5445 "#endif");
5446 verifyNoCrash("#if X\n"
5447 "#elif Y\n"
5448 "#elif Y\n"
5449 "#endif\n"
5450 "#endif");
5451 verifyNoCrash("#endif\n"
5452 "#endif");
5453 verifyNoCrash("#endif\n"
5454 "#else");
5455 verifyNoCrash("#endif\n"
5456 "#elif Y");
5457 }
5458
5459 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
5460 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
5461 EXPECT_EQ("class A : public QObject {\n"
5462 " Q_OBJECT\n"
5463 "\n"
5464 " A() {}\n"
5465 "};",
5466 format("class A : public QObject {\n"
5467 " Q_OBJECT\n"
5468 "\n"
5469 " A() {\n}\n"
5470 "} ;"));
5471 EXPECT_EQ("MACRO\n"
5472 "/*static*/ int i;",
5473 format("MACRO\n"
5474 " /*static*/ int i;"));
5475 EXPECT_EQ("SOME_MACRO\n"
5476 "namespace {\n"
5477 "void f();\n"
5478 "} // namespace",
5479 format("SOME_MACRO\n"
5480 " namespace {\n"
5481 "void f( );\n"
5482 "} // namespace"));
5483 // Only if the identifier contains at least 5 characters.
5484 EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
5485 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
5486 // Only if everything is upper case.
5487 EXPECT_EQ("class A : public QObject {\n"
5488 " Q_Object A() {}\n"
5489 "};",
5490 format("class A : public QObject {\n"
5491 " Q_Object\n"
5492 " A() {\n}\n"
5493 "} ;"));
5494
5495 // Only if the next line can actually start an unwrapped line.
5496 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
5497 format("SOME_WEIRD_LOG_MACRO\n"
5498 "<< SomeThing;"));
5499
5500 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
5501 "(n, buffers))\n",
5502 getChromiumStyle(FormatStyle::LK_Cpp));
5503
5504 // See PR41483
5505 EXPECT_EQ("/**/ FOO(a)\n"
5506 "FOO(b)",
5507 format("/**/ FOO(a)\n"
5508 "FOO(b)"));
5509 }
5510
5511 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
5512 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
5513 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
5514 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
5515 "class X {};\n"
5516 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
5517 "int *createScopDetectionPass() { return 0; }",
5518 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
5519 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
5520 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
5521 " class X {};\n"
5522 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
5523 " int *createScopDetectionPass() { return 0; }"));
5524 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
5525 // braces, so that inner block is indented one level more.
5526 EXPECT_EQ("int q() {\n"
5527 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
5528 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
5529 " IPC_END_MESSAGE_MAP()\n"
5530 "}",
5531 format("int q() {\n"
5532 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
5533 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
5534 " IPC_END_MESSAGE_MAP()\n"
5535 "}"));
5536
5537 // Same inside macros.
5538 EXPECT_EQ("#define LIST(L) \\\n"
5539 " L(A) \\\n"
5540 " L(B) \\\n"
5541 " L(C)",
5542 format("#define LIST(L) \\\n"
5543 " L(A) \\\n"
5544 " L(B) \\\n"
5545 " L(C)",
5546 getGoogleStyle()));
5547
5548 // These must not be recognized as macros.
5549 EXPECT_EQ("int q() {\n"
5550 " f(x);\n"
5551 " f(x) {}\n"
5552 " f(x)->g();\n"
5553 " f(x)->*g();\n"
5554 " f(x).g();\n"
5555 " f(x) = x;\n"
5556 " f(x) += x;\n"
5557 " f(x) -= x;\n"
5558 " f(x) *= x;\n"
5559 " f(x) /= x;\n"
5560 " f(x) %= x;\n"
5561 " f(x) &= x;\n"
5562 " f(x) |= x;\n"
5563 " f(x) ^= x;\n"
5564 " f(x) >>= x;\n"
5565 " f(x) <<= x;\n"
5566 " f(x)[y].z();\n"
5567 " LOG(INFO) << x;\n"
5568 " ifstream(x) >> x;\n"
5569 "}\n",
5570 format("int q() {\n"
5571 " f(x)\n;\n"
5572 " f(x)\n {}\n"
5573 " f(x)\n->g();\n"
5574 " f(x)\n->*g();\n"
5575 " f(x)\n.g();\n"
5576 " f(x)\n = x;\n"
5577 " f(x)\n += x;\n"
5578 " f(x)\n -= x;\n"
5579 " f(x)\n *= x;\n"
5580 " f(x)\n /= x;\n"
5581 " f(x)\n %= x;\n"
5582 " f(x)\n &= x;\n"
5583 " f(x)\n |= x;\n"
5584 " f(x)\n ^= x;\n"
5585 " f(x)\n >>= x;\n"
5586 " f(x)\n <<= x;\n"
5587 " f(x)\n[y].z();\n"
5588 " LOG(INFO)\n << x;\n"
5589 " ifstream(x)\n >> x;\n"
5590 "}\n"));
5591 EXPECT_EQ("int q() {\n"
5592 " F(x)\n"
5593 " if (1) {\n"
5594 " }\n"
5595 " F(x)\n"
5596 " while (1) {\n"
5597 " }\n"
5598 " F(x)\n"
5599 " G(x);\n"
5600 " F(x)\n"
5601 " try {\n"
5602 " Q();\n"
5603 " } catch (...) {\n"
5604 " }\n"
5605 "}\n",
5606 format("int q() {\n"
5607 "F(x)\n"
5608 "if (1) {}\n"
5609 "F(x)\n"
5610 "while (1) {}\n"
5611 "F(x)\n"
5612 "G(x);\n"
5613 "F(x)\n"
5614 "try { Q(); } catch (...) {}\n"
5615 "}\n"));
5616 EXPECT_EQ("class A {\n"
5617 " A() : t(0) {}\n"
5618 " A(int i) noexcept() : {}\n"
5619 " A(X x)\n" // FIXME: function-level try blocks are broken.
5620 " try : t(0) {\n"
5621 " } catch (...) {\n"
5622 " }\n"
5623 "};",
5624 format("class A {\n"
5625 " A()\n : t(0) {}\n"
5626 " A(int i)\n noexcept() : {}\n"
5627 " A(X x)\n"
5628 " try : t(0) {} catch (...) {}\n"
5629 "};"));
5630 FormatStyle Style = getLLVMStyle();
5631 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
5632 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
5633 Style.BraceWrapping.AfterFunction = true;
5634 EXPECT_EQ("void f()\n"
5635 "try\n"
5636 "{\n"
5637 "}",
5638 format("void f() try {\n"
5639 "}",
5640 Style));
5641 EXPECT_EQ("class SomeClass {\n"
5642 "public:\n"
5643 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5644 "};",
5645 format("class SomeClass {\n"
5646 "public:\n"
5647 " SomeClass()\n"
5648 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5649 "};"));
5650 EXPECT_EQ("class SomeClass {\n"
5651 "public:\n"
5652 " SomeClass()\n"
5653 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5654 "};",
5655 format("class SomeClass {\n"
5656 "public:\n"
5657 " SomeClass()\n"
5658 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5659 "};",
5660 getLLVMStyleWithColumns(40)));
5661
5662 verifyFormat("MACRO(>)");
5663
5664 // Some macros contain an implicit semicolon.
5665 Style = getLLVMStyle();
5666 Style.StatementMacros.push_back("FOO");
5667 verifyFormat("FOO(a) int b = 0;");
5668 verifyFormat("FOO(a)\n"
5669 "int b = 0;",
5670 Style);
5671 verifyFormat("FOO(a);\n"
5672 "int b = 0;",
5673 Style);
5674 verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
5675 "int b = 0;",
5676 Style);
5677 verifyFormat("FOO()\n"
5678 "int b = 0;",
5679 Style);
5680 verifyFormat("FOO\n"
5681 "int b = 0;",
5682 Style);
5683 verifyFormat("void f() {\n"
5684 " FOO(a)\n"
5685 " return a;\n"
5686 "}",
5687 Style);
5688 verifyFormat("FOO(a)\n"
5689 "FOO(b)",
5690 Style);
5691 verifyFormat("int a = 0;\n"
5692 "FOO(b)\n"
5693 "int c = 0;",
5694 Style);
5695 verifyFormat("int a = 0;\n"
5696 "int x = FOO(a)\n"
5697 "int b = 0;",
5698 Style);
5699 verifyFormat("void foo(int a) { FOO(a) }\n"
5700 "uint32_t bar() {}",
5701 Style);
5702 }
5703
5704 TEST_F(FormatTest, FormatsMacrosWithZeroColumnWidth) {
5705 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
5706
5707 verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()",
5708 ZeroColumn);
5709 }
5710
5711 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
5712 verifyFormat("#define A \\\n"
5713 " f({ \\\n"
5714 " g(); \\\n"
5715 " });",
5716 getLLVMStyleWithColumns(11));
5717 }
5718
5719 TEST_F(FormatTest, IndentPreprocessorDirectives) {
5720 FormatStyle Style = getLLVMStyleWithColumns(40);
5721 Style.IndentPPDirectives = FormatStyle::PPDIS_None;
5722 verifyFormat("#ifdef _WIN32\n"
5723 "#define A 0\n"
5724 "#ifdef VAR2\n"
5725 "#define B 1\n"
5726 "#include <someheader.h>\n"
5727 "#define MACRO \\\n"
5728 " some_very_long_func_aaaaaaaaaa();\n"
5729 "#endif\n"
5730 "#else\n"
5731 "#define A 1\n"
5732 "#endif",
5733 Style);
5734 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
5735 verifyFormat("#if 1\n"
5736 "# define __STR(x) #x\n"
5737 "#endif",
5738 Style);
5739 verifyFormat("#ifdef _WIN32\n"
5740 "# define A 0\n"
5741 "# ifdef VAR2\n"
5742 "# define B 1\n"
5743 "# include <someheader.h>\n"
5744 "# define MACRO \\\n"
5745 " some_very_long_func_aaaaaaaaaa();\n"
5746 "# endif\n"
5747 "#else\n"
5748 "# define A 1\n"
5749 "#endif",
5750 Style);
5751 verifyFormat("#if A\n"
5752 "# define MACRO \\\n"
5753 " void a(int x) { \\\n"
5754 " b(); \\\n"
5755 " c(); \\\n"
5756 " d(); \\\n"
5757 " e(); \\\n"
5758 " f(); \\\n"
5759 " }\n"
5760 "#endif",
5761 Style);
5762 // Comments before include guard.
5763 verifyFormat("// file comment\n"
5764 "// file comment\n"
5765 "#ifndef HEADER_H\n"
5766 "#define HEADER_H\n"
5767 "code();\n"
5768 "#endif",
5769 Style);
5770 // Test with include guards.
5771 verifyFormat("#ifndef HEADER_H\n"
5772 "#define HEADER_H\n"
5773 "code();\n"
5774 "#endif",
5775 Style);
5776 // Include guards must have a #define with the same variable immediately
5777 // after #ifndef.
5778 verifyFormat("#ifndef NOT_GUARD\n"
5779 "# define FOO\n"
5780 "code();\n"
5781 "#endif",
5782 Style);
5783
5784 // Include guards must cover the entire file.
5785 verifyFormat("code();\n"
5786 "code();\n"
5787 "#ifndef NOT_GUARD\n"
5788 "# define NOT_GUARD\n"
5789 "code();\n"
5790 "#endif",
5791 Style);
5792 verifyFormat("#ifndef NOT_GUARD\n"
5793 "# define NOT_GUARD\n"
5794 "code();\n"
5795 "#endif\n"
5796 "code();",
5797 Style);
5798 // Test with trailing blank lines.
5799 verifyFormat("#ifndef HEADER_H\n"
5800 "#define HEADER_H\n"
5801 "code();\n"
5802 "#endif\n",
5803 Style);
5804 // Include guards don't have #else.
5805 verifyFormat("#ifndef NOT_GUARD\n"
5806 "# define NOT_GUARD\n"
5807 "code();\n"
5808 "#else\n"
5809 "#endif",
5810 Style);
5811 verifyFormat("#ifndef NOT_GUARD\n"
5812 "# define NOT_GUARD\n"
5813 "code();\n"
5814 "#elif FOO\n"
5815 "#endif",
5816 Style);
5817 // Non-identifier #define after potential include guard.
5818 verifyFormat("#ifndef FOO\n"
5819 "# define 1\n"
5820 "#endif\n",
5821 Style);
5822 // #if closes past last non-preprocessor line.
5823 verifyFormat("#ifndef FOO\n"
5824 "#define FOO\n"
5825 "#if 1\n"
5826 "int i;\n"
5827 "# define A 0\n"
5828 "#endif\n"
5829 "#endif\n",
5830 Style);
5831 // Don't crash if there is an #elif directive without a condition.
5832 verifyFormat("#if 1\n"
5833 "int x;\n"
5834 "#elif\n"
5835 "int y;\n"
5836 "#else\n"
5837 "int z;\n"
5838 "#endif",
5839 Style);
5840 // FIXME: This doesn't handle the case where there's code between the
5841 // #ifndef and #define but all other conditions hold. This is because when
5842 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
5843 // previous code line yet, so we can't detect it.
5844 EXPECT_EQ("#ifndef NOT_GUARD\n"
5845 "code();\n"
5846 "#define NOT_GUARD\n"
5847 "code();\n"
5848 "#endif",
5849 format("#ifndef NOT_GUARD\n"
5850 "code();\n"
5851 "# define NOT_GUARD\n"
5852 "code();\n"
5853 "#endif",
5854 Style));
5855 // FIXME: This doesn't handle cases where legitimate preprocessor lines may
5856 // be outside an include guard. Examples are #pragma once and
5857 // #pragma GCC diagnostic, or anything else that does not change the meaning
5858 // of the file if it's included multiple times.
5859 EXPECT_EQ("#ifdef WIN32\n"
5860 "# pragma once\n"
5861 "#endif\n"
5862 "#ifndef HEADER_H\n"
5863 "# define HEADER_H\n"
5864 "code();\n"
5865 "#endif",
5866 format("#ifdef WIN32\n"
5867 "# pragma once\n"
5868 "#endif\n"
5869 "#ifndef HEADER_H\n"
5870 "#define HEADER_H\n"
5871 "code();\n"
5872 "#endif",
5873 Style));
5874 // FIXME: This does not detect when there is a single non-preprocessor line
5875 // in front of an include-guard-like structure where other conditions hold
5876 // because ScopedLineState hides the line.
5877 EXPECT_EQ("code();\n"
5878 "#ifndef HEADER_H\n"
5879 "#define HEADER_H\n"
5880 "code();\n"
5881 "#endif",
5882 format("code();\n"
5883 "#ifndef HEADER_H\n"
5884 "# define HEADER_H\n"
5885 "code();\n"
5886 "#endif",
5887 Style));
5888 // Keep comments aligned with #, otherwise indent comments normally. These
5889 // tests cannot use verifyFormat because messUp manipulates leading
5890 // whitespace.
5891 {
5892 const char *Expected = ""
5893 "void f() {\n"
5894 "#if 1\n"
5895 "// Preprocessor aligned.\n"
5896 "# define A 0\n"
5897 " // Code. Separated by blank line.\n"
5898 "\n"
5899 "# define B 0\n"
5900 " // Code. Not aligned with #\n"
5901 "# define C 0\n"
5902 "#endif";
5903 const char *ToFormat = ""
5904 "void f() {\n"
5905 "#if 1\n"
5906 "// Preprocessor aligned.\n"
5907 "# define A 0\n"
5908 "// Code. Separated by blank line.\n"
5909 "\n"
5910 "# define B 0\n"
5911 " // Code. Not aligned with #\n"
5912 "# define C 0\n"
5913 "#endif";
5914 EXPECT_EQ(Expected, format(ToFormat, Style));
5915 EXPECT_EQ(Expected, format(Expected, Style));
5916 }
5917 // Keep block quotes aligned.
5918 {
5919 const char *Expected = ""
5920 "void f() {\n"
5921 "#if 1\n"
5922 "/* Preprocessor aligned. */\n"
5923 "# define A 0\n"
5924 " /* Code. Separated by blank line. */\n"
5925 "\n"
5926 "# define B 0\n"
5927 " /* Code. Not aligned with # */\n"
5928 "# define C 0\n"
5929 "#endif";
5930 const char *ToFormat = ""
5931 "void f() {\n"
5932 "#if 1\n"
5933 "/* Preprocessor aligned. */\n"
5934 "# define A 0\n"
5935 "/* Code. Separated by blank line. */\n"
5936 "\n"
5937 "# define B 0\n"
5938 " /* Code. Not aligned with # */\n"
5939 "# define C 0\n"
5940 "#endif";
5941 EXPECT_EQ(Expected, format(ToFormat, Style));
5942 EXPECT_EQ(Expected, format(Expected, Style));
5943 }
5944 // Keep comments aligned with un-indented directives.
5945 {
5946 const char *Expected = ""
5947 "void f() {\n"
5948 "// Preprocessor aligned.\n"
5949 "#define A 0\n"
5950 " // Code. Separated by blank line.\n"
5951 "\n"
5952 "#define B 0\n"
5953 " // Code. Not aligned with #\n"
5954 "#define C 0\n";
5955 const char *ToFormat = ""
5956 "void f() {\n"
5957 "// Preprocessor aligned.\n"
5958 "#define A 0\n"
5959 "// Code. Separated by blank line.\n"
5960 "\n"
5961 "#define B 0\n"
5962 " // Code. Not aligned with #\n"
5963 "#define C 0\n";
5964 EXPECT_EQ(Expected, format(ToFormat, Style));
5965 EXPECT_EQ(Expected, format(Expected, Style));
5966 }
5967 // Test AfterHash with tabs.
5968 {
5969 FormatStyle Tabbed = Style;
5970 Tabbed.UseTab = FormatStyle::UT_Always;
5971 Tabbed.IndentWidth = 8;
5972 Tabbed.TabWidth = 8;
5973 verifyFormat("#ifdef _WIN32\n"
5974 "#\tdefine A 0\n"
5975 "#\tifdef VAR2\n"
5976 "#\t\tdefine B 1\n"
5977 "#\t\tinclude <someheader.h>\n"
5978 "#\t\tdefine MACRO \\\n"
5979 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
5980 "#\tendif\n"
5981 "#else\n"
5982 "#\tdefine A 1\n"
5983 "#endif",
5984 Tabbed);
5985 }
5986
5987 // Regression test: Multiline-macro inside include guards.
5988 verifyFormat("#ifndef HEADER_H\n"
5989 "#define HEADER_H\n"
5990 "#define A() \\\n"
5991 " int i; \\\n"
5992 " int j;\n"
5993 "#endif // HEADER_H",
5994 getLLVMStyleWithColumns(20));
5995
5996 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
5997 // Basic before hash indent tests
5998 verifyFormat("#ifdef _WIN32\n"
5999 " #define A 0\n"
6000 " #ifdef VAR2\n"
6001 " #define B 1\n"
6002 " #include <someheader.h>\n"
6003 " #define MACRO \\\n"
6004 " some_very_long_func_aaaaaaaaaa();\n"
6005 " #endif\n"
6006 "#else\n"
6007 " #define A 1\n"
6008 "#endif",
6009 Style);
6010 verifyFormat("#if A\n"
6011 " #define MACRO \\\n"
6012 " void a(int x) { \\\n"
6013 " b(); \\\n"
6014 " c(); \\\n"
6015 " d(); \\\n"
6016 " e(); \\\n"
6017 " f(); \\\n"
6018 " }\n"
6019 "#endif",
6020 Style);
6021 // Keep comments aligned with indented directives. These
6022 // tests cannot use verifyFormat because messUp manipulates leading
6023 // whitespace.
6024 {
6025 const char *Expected = "void f() {\n"
6026 "// Aligned to preprocessor.\n"
6027 "#if 1\n"
6028 " // Aligned to code.\n"
6029 " int a;\n"
6030 " #if 1\n"
6031 " // Aligned to preprocessor.\n"
6032 " #define A 0\n"
6033 " // Aligned to code.\n"
6034 " int b;\n"
6035 " #endif\n"
6036 "#endif\n"
6037 "}";
6038 const char *ToFormat = "void f() {\n"
6039 "// Aligned to preprocessor.\n"
6040 "#if 1\n"
6041 "// Aligned to code.\n"
6042 "int a;\n"
6043 "#if 1\n"
6044 "// Aligned to preprocessor.\n"
6045 "#define A 0\n"
6046 "// Aligned to code.\n"
6047 "int b;\n"
6048 "#endif\n"
6049 "#endif\n"
6050 "}";
6051 EXPECT_EQ(Expected, format(ToFormat, Style));
6052 EXPECT_EQ(Expected, format(Expected, Style));
6053 }
6054 {
6055 const char *Expected = "void f() {\n"
6056 "/* Aligned to preprocessor. */\n"
6057 "#if 1\n"
6058 " /* Aligned to code. */\n"
6059 " int a;\n"
6060 " #if 1\n"
6061 " /* Aligned to preprocessor. */\n"
6062 " #define A 0\n"
6063 " /* Aligned to code. */\n"
6064 " int b;\n"
6065 " #endif\n"
6066 "#endif\n"
6067 "}";
6068 const char *ToFormat = "void f() {\n"
6069 "/* Aligned to preprocessor. */\n"
6070 "#if 1\n"
6071 "/* Aligned to code. */\n"
6072 "int a;\n"
6073 "#if 1\n"
6074 "/* Aligned to preprocessor. */\n"
6075 "#define A 0\n"
6076 "/* Aligned to code. */\n"
6077 "int b;\n"
6078 "#endif\n"
6079 "#endif\n"
6080 "}";
6081 EXPECT_EQ(Expected, format(ToFormat, Style));
6082 EXPECT_EQ(Expected, format(Expected, Style));
6083 }
6084
6085 // Test single comment before preprocessor
6086 verifyFormat("// Comment\n"
6087 "\n"
6088 "#if 1\n"
6089 "#endif",
6090 Style);
6091 }
6092
6093 TEST_F(FormatTest, FormatAlignInsidePreprocessorElseBlock) {
6094 FormatStyle Style = getLLVMStyle();
6095 Style.AlignConsecutiveAssignments.Enabled = true;
6096 Style.AlignConsecutiveDeclarations.Enabled = true;
6097
6098 // Test with just #if blocks.
6099 verifyFormat("void f1() {\n"
6100 "#if 1\n"
6101 " int foo = 1;\n"
6102 " int foobar = 2;\n"
6103 "#endif\n"
6104 "}\n"
6105 "#if 1\n"
6106 "int baz = 3;\n"
6107 "#endif\n"
6108 "void f2() {\n"
6109 "#if 1\n"
6110 " char *foobarbaz = \"foobarbaz\";\n"
6111 " int quux = 4;\n"
6112 "}",
6113 Style);
6114
6115 // Test with just #else blocks.
6116 verifyFormat("void f1() {\n"
6117 "#if 1\n"
6118 "#else\n"
6119 " int foo = 1;\n"
6120 " int foobar = 2;\n"
6121 "#endif\n"
6122 "}\n"
6123 "#if 1\n"
6124 "#else\n"
6125 "int baz = 3;\n"
6126 "#endif\n"
6127 "void f2() {\n"
6128 "#if 1\n"
6129 "#else\n"
6130 " char *foobarbaz = \"foobarbaz\";\n"
6131 " int quux = 4;\n"
6132 "}",
6133 Style);
6134
6135 // Test with a mix of #if and #else blocks.
6136 verifyFormat("void f1() {\n"
6137 "#if 1\n"
6138 "#else\n"
6139 " int foo = 1;\n"
6140 " int foobar = 2;\n"
6141 "#endif\n"
6142 "}\n"
6143 "#if 1\n"
6144 "int baz = 3;\n"
6145 "#endif\n"
6146 "void f2() {\n"
6147 "#if 1\n"
6148 "#else\n"
6149 " // prevent alignment with #else in f1\n"
6150 " char *foobarbaz = \"foobarbaz\";\n"
6151 " int quux = 4;\n"
6152 "}",
6153 Style);
6154
6155 // Test with nested #if and #else blocks.
6156 verifyFormat("void f1() {\n"
6157 "#if 1\n"
6158 "#else\n"
6159 "#if 2\n"
6160 "#else\n"
6161 " int foo = 1;\n"
6162 " int foobar = 2;\n"
6163 "#endif\n"
6164 "#endif\n"
6165 "}\n"
6166 "#if 1\n"
6167 "#else\n"
6168 "#if 2\n"
6169 "int baz = 3;\n"
6170 "#endif\n"
6171 "#endif\n"
6172 "void f2() {\n"
6173 "#if 1\n"
6174 "#if 2\n"
6175 "#else\n"
6176 " // prevent alignment with #else in f1\n"
6177 " char *foobarbaz = \"foobarbaz\";\n"
6178 " int quux = 4;\n"
6179 "#endif\n"
6180 "#endif\n"
6181 "}",
6182 Style);
6183 }
6184
6185 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
6186 verifyFormat("{\n { a #c; }\n}");
6187 }
6188
6189 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
6190 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
6191 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
6192 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
6193 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
6194 }
6195
6196 TEST_F(FormatTest, EscapedNewlines) {
6197 FormatStyle Narrow = getLLVMStyleWithColumns(11);
6198 EXPECT_EQ("#define A \\\n int i; \\\n int j;",
6199 format("#define A \\\nint i;\\\n int j;", Narrow));
6200 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
6201 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
6202 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */"));
6203 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
6204
6205 FormatStyle AlignLeft = getLLVMStyle();
6206 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
6207 EXPECT_EQ("#define MACRO(x) \\\n"
6208 "private: \\\n"
6209 " int x(int a);\n",
6210 format("#define MACRO(x) \\\n"
6211 "private: \\\n"
6212 " int x(int a);\n",
6213 AlignLeft));
6214
6215 // CRLF line endings
6216 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;",
6217 format("#define A \\\r\nint i;\\\r\n int j;", Narrow));
6218 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
6219 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
6220 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */"));
6221 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
6222 EXPECT_EQ("#define MACRO(x) \\\r\n"
6223 "private: \\\r\n"
6224 " int x(int a);\r\n",
6225 format("#define MACRO(x) \\\r\n"
6226 "private: \\\r\n"
6227 " int x(int a);\r\n",
6228 AlignLeft));
6229
6230 FormatStyle DontAlign = getLLVMStyle();
6231 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
6232 DontAlign.MaxEmptyLinesToKeep = 3;
6233 // FIXME: can't use verifyFormat here because the newline before
6234 // "public:" is not inserted the first time it's reformatted
6235 EXPECT_EQ("#define A \\\n"
6236 " class Foo { \\\n"
6237 " void bar(); \\\n"
6238 "\\\n"
6239 "\\\n"
6240 "\\\n"
6241 " public: \\\n"
6242 " void baz(); \\\n"
6243 " };",
6244 format("#define A \\\n"
6245 " class Foo { \\\n"
6246 " void bar(); \\\n"
6247 "\\\n"
6248 "\\\n"
6249 "\\\n"
6250 " public: \\\n"
6251 " void baz(); \\\n"
6252 " };",
6253 DontAlign));
6254 }
6255
6256 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
6257 verifyFormat("#define A \\\n"
6258 " int v( \\\n"
6259 " a); \\\n"
6260 " int i;",
6261 getLLVMStyleWithColumns(11));
6262 }
6263
6264 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
6265 EXPECT_EQ(
6266 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
6267 " \\\n"
6268 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
6269 "\n"
6270 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
6271 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
6272 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
6273 "\\\n"
6274 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
6275 " \n"
6276 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
6277 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
6278 }
6279
6280 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
6281 EXPECT_EQ("int\n"
6282 "#define A\n"
6283 " a;",
6284 format("int\n#define A\na;"));
6285 verifyFormat("functionCallTo(\n"
6286 " someOtherFunction(\n"
6287 " withSomeParameters, whichInSequence,\n"
6288 " areLongerThanALine(andAnotherCall,\n"
6289 "#define A B\n"
6290 " withMoreParamters,\n"
6291 " whichStronglyInfluenceTheLayout),\n"
6292 " andMoreParameters),\n"
6293 " trailing);",
6294 getLLVMStyleWithColumns(69));
6295 verifyFormat("Foo::Foo()\n"
6296 "#ifdef BAR\n"
6297 " : baz(0)\n"
6298 "#endif\n"
6299 "{\n"
6300 "}");
6301 verifyFormat("void f() {\n"
6302 " if (true)\n"
6303 "#ifdef A\n"
6304 " f(42);\n"
6305 " x();\n"
6306 "#else\n"
6307 " g();\n"
6308 " x();\n"
6309 "#endif\n"
6310 "}");
6311 verifyFormat("void f(param1, param2,\n"
6312 " param3,\n"
6313 "#ifdef A\n"
6314 " param4(param5,\n"
6315 "#ifdef A1\n"
6316 " param6,\n"
6317 "#ifdef A2\n"
6318 " param7),\n"
6319 "#else\n"
6320 " param8),\n"
6321 " param9,\n"
6322 "#endif\n"
6323 " param10,\n"
6324 "#endif\n"
6325 " param11)\n"
6326 "#else\n"
6327 " param12)\n"
6328 "#endif\n"
6329 "{\n"
6330 " x();\n"
6331 "}",
6332 getLLVMStyleWithColumns(28));
6333 verifyFormat("#if 1\n"
6334 "int i;");
6335 verifyFormat("#if 1\n"
6336 "#endif\n"
6337 "#if 1\n"
6338 "#else\n"
6339 "#endif\n");
6340 verifyFormat("DEBUG({\n"
6341 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6342 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
6343 "});\n"
6344 "#if a\n"
6345 "#else\n"
6346 "#endif");
6347
6348 verifyIncompleteFormat("void f(\n"
6349 "#if A\n"
6350 ");\n"
6351 "#else\n"
6352 "#endif");
6353
6354 // Verify that indentation is correct when there is an `#if 0` with an
6355 // `#else`.
6356 verifyFormat("#if 0\n"
6357 "{\n"
6358 "#else\n"
6359 "{\n"
6360 "#endif\n"
6361 " x;\n"
6362 "}");
6363
6364 verifyFormat("#if 0\n"
6365 "#endif\n"
6366 "#if X\n"
6367 "int something_fairly_long; // Align here please\n"
6368 "#endif // Should be aligned");
6369
6370 verifyFormat("#if 0\n"
6371 "#endif\n"
6372 "#if X\n"
6373 "#else // Align\n"
6374 ";\n"
6375 "#endif // Align");
6376
6377 verifyFormat("void SomeFunction(int param1,\n"
6378 " template <\n"
6379 "#ifdef A\n"
6380 "#if 0\n"
6381 "#endif\n"
6382 " MyType<Some>>\n"
6383 "#else\n"
6384 " Type1, Type2>\n"
6385 "#endif\n"
6386 " param2,\n"
6387 " param3) {\n"
6388 " f();\n"
6389 "}");
6390 }
6391
6392 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
6393 verifyFormat("#endif\n"
6394 "#if B");
6395 }
6396
6397 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
6398 FormatStyle SingleLine = getLLVMStyle();
6399 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
6400 verifyFormat("#if 0\n"
6401 "#elif 1\n"
6402 "#endif\n"
6403 "void foo() {\n"
6404 " if (test) foo2();\n"
6405 "}",
6406 SingleLine);
6407 }
6408
6409 TEST_F(FormatTest, LayoutBlockInsideParens) {
6410 verifyFormat("functionCall({ int i; });");
6411 verifyFormat("functionCall({\n"
6412 " int i;\n"
6413 " int j;\n"
6414 "});");
6415 verifyFormat("functionCall(\n"
6416 " {\n"
6417 " int i;\n"
6418 " int j;\n"
6419 " },\n"
6420 " aaaa, bbbb, cccc);");
6421 verifyFormat("functionA(functionB({\n"
6422 " int i;\n"
6423 " int j;\n"
6424 " }),\n"
6425 " aaaa, bbbb, cccc);");
6426 verifyFormat("functionCall(\n"
6427 " {\n"
6428 " int i;\n"
6429 " int j;\n"
6430 " },\n"
6431 " aaaa, bbbb, // comment\n"
6432 " cccc);");
6433 verifyFormat("functionA(functionB({\n"
6434 " int i;\n"
6435 " int j;\n"
6436 " }),\n"
6437 " aaaa, bbbb, // comment\n"
6438 " cccc);");
6439 verifyFormat("functionCall(aaaa, bbbb, { int i; });");
6440 verifyFormat("functionCall(aaaa, bbbb, {\n"
6441 " int i;\n"
6442 " int j;\n"
6443 "});");
6444 verifyFormat(
6445 "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
6446 " {\n"
6447 " int i; // break\n"
6448 " },\n"
6449 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6450 " ccccccccccccccccc));");
6451 verifyFormat("DEBUG({\n"
6452 " if (a)\n"
6453 " f();\n"
6454 "});");
6455 }
6456
6457 TEST_F(FormatTest, LayoutBlockInsideStatement) {
6458 EXPECT_EQ("SOME_MACRO { int i; }\n"
6459 "int i;",
6460 format(" SOME_MACRO {int i;} int i;"));
6461 }
6462
6463 TEST_F(FormatTest, LayoutNestedBlocks) {
6464 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
6465 " struct s {\n"
6466 " int i;\n"
6467 " };\n"
6468 " s kBitsToOs[] = {{10}};\n"
6469 " for (int i = 0; i < 10; ++i)\n"
6470 " return;\n"
6471 "}");
6472 verifyFormat("call(parameter, {\n"
6473 " something();\n"
6474 " // Comment using all columns.\n"
6475 " somethingelse();\n"
6476 "});",
6477 getLLVMStyleWithColumns(40));
6478 verifyFormat("DEBUG( //\n"
6479 " { f(); }, a);");
6480 verifyFormat("DEBUG( //\n"
6481 " {\n"
6482 " f(); //\n"
6483 " },\n"
6484 " a);");
6485
6486 EXPECT_EQ("call(parameter, {\n"
6487 " something();\n"
6488 " // Comment too\n"
6489 " // looooooooooong.\n"
6490 " somethingElse();\n"
6491 "});",
6492 format("call(parameter, {\n"
6493 " something();\n"
6494 " // Comment too looooooooooong.\n"
6495 " somethingElse();\n"
6496 "});",
6497 getLLVMStyleWithColumns(29)));
6498 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });"));
6499 EXPECT_EQ("DEBUG({ // comment\n"
6500 " int i;\n"
6501 "});",
6502 format("DEBUG({ // comment\n"
6503 "int i;\n"
6504 "});"));
6505 EXPECT_EQ("DEBUG({\n"
6506 " int i;\n"
6507 "\n"
6508 " // comment\n"
6509 " int j;\n"
6510 "});",
6511 format("DEBUG({\n"
6512 " int i;\n"
6513 "\n"
6514 " // comment\n"
6515 " int j;\n"
6516 "});"));
6517
6518 verifyFormat("DEBUG({\n"
6519 " if (a)\n"
6520 " return;\n"
6521 "});");
6522 verifyGoogleFormat("DEBUG({\n"
6523 " if (a) return;\n"
6524 "});");
6525 FormatStyle Style = getGoogleStyle();
6526 Style.ColumnLimit = 45;
6527 verifyFormat("Debug(\n"
6528 " aaaaa,\n"
6529 " {\n"
6530 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
6531 " },\n"
6532 " a);",
6533 Style);
6534
6535 verifyFormat("SomeFunction({MACRO({ return output; }), b});");
6536
6537 verifyNoCrash("^{v^{a}}");
6538 }
6539
6540 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
6541 EXPECT_EQ("#define MACRO() \\\n"
6542 " Debug(aaa, /* force line break */ \\\n"
6543 " { \\\n"
6544 " int i; \\\n"
6545 " int j; \\\n"
6546 " })",
6547 format("#define MACRO() Debug(aaa, /* force line break */ \\\n"
6548 " { int i; int j; })",
6549 getGoogleStyle()));
6550
6551 EXPECT_EQ("#define A \\\n"
6552 " [] { \\\n"
6553 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
6554 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
6555 " }",
6556 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
6557 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
6558 getGoogleStyle()));
6559 }
6560
6561 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
6562 EXPECT_EQ("{}", format("{}"));
6563 verifyFormat("enum E {};");
6564 verifyFormat("enum E {}");
6565 FormatStyle Style = getLLVMStyle();
6566 Style.SpaceInEmptyBlock = true;
6567 EXPECT_EQ("void f() { }", format("void f() {}", Style));
6568 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
6569 EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
6570 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
6571 Style.BraceWrapping.BeforeElse = false;
6572 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
6573 verifyFormat("if (a)\n"
6574 "{\n"
6575 "} else if (b)\n"
6576 "{\n"
6577 "} else\n"
6578 "{ }",
6579 Style);
6580 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
6581 verifyFormat("if (a) {\n"
6582 "} else if (b) {\n"
6583 "} else {\n"
6584 "}",
6585 Style);
6586 Style.BraceWrapping.BeforeElse = true;
6587 verifyFormat("if (a) { }\n"
6588 "else if (b) { }\n"
6589 "else { }",
6590 Style);
6591 }
6592
6593 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
6594 FormatStyle Style = getLLVMStyle();
6595 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
6596 Style.MacroBlockEnd = "^[A-Z_]+_END$";
6597 verifyFormat("FOO_BEGIN\n"
6598 " FOO_ENTRY\n"
6599 "FOO_END",
6600 Style);
6601 verifyFormat("FOO_BEGIN\n"
6602 " NESTED_FOO_BEGIN\n"
6603 " NESTED_FOO_ENTRY\n"
6604 " NESTED_FOO_END\n"
6605 "FOO_END",
6606 Style);
6607 verifyFormat("FOO_BEGIN(Foo, Bar)\n"
6608 " int x;\n"
6609 " x = 1;\n"
6610 "FOO_END(Baz)",
6611 Style);
6612
6613 Style.RemoveBracesLLVM = true;
6614 verifyNoCrash("for (;;)\n"
6615 " FOO_BEGIN\n"
6616 " foo();\n"
6617 " FOO_END",
6618 Style);
6619 }
6620
6621 //===----------------------------------------------------------------------===//
6622 // Line break tests.
6623 //===----------------------------------------------------------------------===//
6624
6625 TEST_F(FormatTest, PreventConfusingIndents) {
6626 verifyFormat(
6627 "void f() {\n"
6628 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
6629 " parameter, parameter, parameter)),\n"
6630 " SecondLongCall(parameter));\n"
6631 "}");
6632 verifyFormat(
6633 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6634 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6635 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6636 " aaaaaaaaaaaaaaaaaaaaaaaa);");
6637 verifyFormat(
6638 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6639 " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
6640 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6641 " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
6642 verifyFormat(
6643 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
6644 " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
6645 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
6646 " aaaaaaaaaaaaaaaaaaaaaaaa>;");
6647 verifyFormat("int a = bbbb && ccc &&\n"
6648 " fffff(\n"
6649 "#define A Just forcing a new line\n"
6650 " ddd);");
6651 }
6652
6653 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
6654 verifyFormat(
6655 "bool aaaaaaa =\n"
6656 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
6657 " bbbbbbbb();");
6658 verifyFormat(
6659 "bool aaaaaaa =\n"
6660 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
6661 " bbbbbbbb();");
6662
6663 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
6664 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
6665 " ccccccccc == ddddddddddd;");
6666 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
6667 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
6668 " ccccccccc == ddddddddddd;");
6669 verifyFormat(
6670 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
6671 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
6672 " ccccccccc == ddddddddddd;");
6673
6674 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
6675 " aaaaaa) &&\n"
6676 " bbbbbb && cccccc;");
6677 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
6678 " aaaaaa) >>\n"
6679 " bbbbbb;");
6680 verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
6681 " SourceMgr.getSpellingColumnNumber(\n"
6682 " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
6683 " 1);");
6684
6685 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6686 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
6687 " cccccc) {\n}");
6688 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6689 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
6690 " cccccc) {\n}");
6691 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6692 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
6693 " cccccc) {\n}");
6694 verifyFormat("b = a &&\n"
6695 " // Comment\n"
6696 " b.c && d;");
6697
6698 // If the LHS of a comparison is not a binary expression itself, the
6699 // additional linebreak confuses many people.
6700 verifyFormat(
6701 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6702 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
6703 "}");
6704 verifyFormat(
6705 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6706 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
6707 "}");
6708 verifyFormat(
6709 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
6710 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
6711 "}");
6712 verifyFormat(
6713 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6714 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
6715 "}");
6716 // Even explicit parentheses stress the precedence enough to make the
6717 // additional break unnecessary.
6718 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6719 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
6720 "}");
6721 // This cases is borderline, but with the indentation it is still readable.
6722 verifyFormat(
6723 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6724 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6725 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
6726 "}",
6727 getLLVMStyleWithColumns(75));
6728
6729 // If the LHS is a binary expression, we should still use the additional break
6730 // as otherwise the formatting hides the operator precedence.
6731 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6732 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
6733 " 5) {\n"
6734 "}");
6735 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6736 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
6737 " 5) {\n"
6738 "}");
6739
6740 FormatStyle OnePerLine = getLLVMStyle();
6741 OnePerLine.BinPackParameters = false;
6742 verifyFormat(
6743 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6744 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6745 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
6746 OnePerLine);
6747
6748 verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
6749 " .aaa(aaaaaaaaaaaaa) *\n"
6750 " aaaaaaa +\n"
6751 " aaaaaaa;",
6752 getLLVMStyleWithColumns(40));
6753 }
6754
6755 TEST_F(FormatTest, ExpressionIndentation) {
6756 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6757 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6758 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
6759 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
6760 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
6761 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
6762 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
6763 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
6764 " ccccccccccccccccccccccccccccccccccccccccc;");
6765 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
6766 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6767 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
6768 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
6769 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6770 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
6771 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
6772 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
6773 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
6774 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
6775 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6776 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
6777 verifyFormat("if () {\n"
6778 "} else if (aaaaa && bbbbb > // break\n"
6779 " ccccc) {\n"
6780 "}");
6781 verifyFormat("if () {\n"
6782 "} else if constexpr (aaaaa && bbbbb > // break\n"
6783 " ccccc) {\n"
6784 "}");
6785 verifyFormat("if () {\n"
6786 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
6787 " ccccc) {\n"
6788 "}");
6789 verifyFormat("if () {\n"
6790 "} else if (aaaaa &&\n"
6791 " bbbbb > // break\n"
6792 " ccccc &&\n"
6793 " ddddd) {\n"
6794 "}");
6795
6796 // Presence of a trailing comment used to change indentation of b.
6797 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
6798 " b;\n"
6799 "return aaaaaaaaaaaaaaaaaaa +\n"
6800 " b; //",
6801 getLLVMStyleWithColumns(30));
6802 }
6803
6804 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
6805 // Not sure what the best system is here. Like this, the LHS can be found
6806 // immediately above an operator (everything with the same or a higher
6807 // indent). The RHS is aligned right of the operator and so compasses
6808 // everything until something with the same indent as the operator is found.
6809 // FIXME: Is this a good system?
6810 FormatStyle Style = getLLVMStyle();
6811 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
6812 verifyFormat(
6813 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6814 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6815 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6816 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6817 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6818 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6819 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6820 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6821 " > ccccccccccccccccccccccccccccccccccccccccc;",
6822 Style);
6823 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6824 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6825 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6826 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6827 Style);
6828 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6829 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6830 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6831 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6832 Style);
6833 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6834 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6835 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6836 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6837 Style);
6838 verifyFormat("if () {\n"
6839 "} else if (aaaaa\n"
6840 " && bbbbb // break\n"
6841 " > ccccc) {\n"
6842 "}",
6843 Style);
6844 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6845 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
6846 Style);
6847 verifyFormat("return (a)\n"
6848 " // comment\n"
6849 " + b;",
6850 Style);
6851 verifyFormat(
6852 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6853 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6854 " + cc;",
6855 Style);
6856
6857 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6858 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
6859 Style);
6860
6861 // Forced by comments.
6862 verifyFormat(
6863 "unsigned ContentSize =\n"
6864 " sizeof(int16_t) // DWARF ARange version number\n"
6865 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6866 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
6867 " + sizeof(int8_t); // Segment Size (in bytes)");
6868
6869 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
6870 " == boost::fusion::at_c<1>(iiii).second;",
6871 Style);
6872
6873 Style.ColumnLimit = 60;
6874 verifyFormat("zzzzzzzzzz\n"
6875 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6876 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
6877 Style);
6878
6879 Style.ColumnLimit = 80;
6880 Style.IndentWidth = 4;
6881 Style.TabWidth = 4;
6882 Style.UseTab = FormatStyle::UT_Always;
6883 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6884 Style.AlignOperands = FormatStyle::OAS_DontAlign;
6885 EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
6886 "\t&& (someOtherLongishConditionPart1\n"
6887 "\t\t|| someOtherEvenLongerNestedConditionPart2);",
6888 format("return someVeryVeryLongConditionThatBarelyFitsOnALine && "
6889 "(someOtherLongishConditionPart1 || "
6890 "someOtherEvenLongerNestedConditionPart2);",
6891 Style));
6892 }
6893
6894 TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
6895 FormatStyle Style = getLLVMStyle();
6896 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
6897 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
6898
6899 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6900 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6901 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6902 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6903 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6904 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6905 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6906 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6907 " > ccccccccccccccccccccccccccccccccccccccccc;",
6908 Style);
6909 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6910 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6911 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6912 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6913 Style);
6914 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6915 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6916 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6917 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6918 Style);
6919 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6920 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6921 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6922 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6923 Style);
6924 verifyFormat("if () {\n"
6925 "} else if (aaaaa\n"
6926 " && bbbbb // break\n"
6927 " > ccccc) {\n"
6928 "}",
6929 Style);
6930 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6931 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
6932 Style);
6933 verifyFormat("return (a)\n"
6934 " // comment\n"
6935 " + b;",
6936 Style);
6937 verifyFormat(
6938 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6939 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6940 " + cc;",
6941 Style);
6942 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
6943 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
6944 " : 3333333333333333;",
6945 Style);
6946 verifyFormat(
6947 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6948 " : ccccccccccccccc ? dddddddddddddddddd\n"
6949 " : eeeeeeeeeeeeeeeeee)\n"
6950 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
6951 " : 3333333333333333;",
6952 Style);
6953 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6954 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
6955 Style);
6956
6957 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
6958 " == boost::fusion::at_c<1>(iiii).second;",
6959 Style);
6960
6961 Style.ColumnLimit = 60;
6962 verifyFormat("zzzzzzzzzzzzz\n"
6963 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6964 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
6965 Style);
6966
6967 // Forced by comments.
6968 Style.ColumnLimit = 80;
6969 verifyFormat(
6970 "unsigned ContentSize\n"
6971 " = sizeof(int16_t) // DWARF ARange version number\n"
6972 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6973 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
6974 " + sizeof(int8_t); // Segment Size (in bytes)",
6975 Style);
6976
6977 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6978 verifyFormat(
6979 "unsigned ContentSize =\n"
6980 " sizeof(int16_t) // DWARF ARange version number\n"
6981 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6982 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
6983 " + sizeof(int8_t); // Segment Size (in bytes)",
6984 Style);
6985
6986 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
6987 verifyFormat(
6988 "unsigned ContentSize =\n"
6989 " sizeof(int16_t) // DWARF ARange version number\n"
6990 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6991 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
6992 " + sizeof(int8_t); // Segment Size (in bytes)",
6993 Style);
6994 }
6995
6996 TEST_F(FormatTest, EnforcedOperatorWraps) {
6997 // Here we'd like to wrap after the || operators, but a comment is forcing an
6998 // earlier wrap.
6999 verifyFormat("bool x = aaaaa //\n"
7000 " || bbbbb\n"
7001 " //\n"
7002 " || cccc;");
7003 }
7004
7005 TEST_F(FormatTest, NoOperandAlignment) {
7006 FormatStyle Style = getLLVMStyle();
7007 Style.AlignOperands = FormatStyle::OAS_DontAlign;
7008 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
7009 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7010 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7011 Style);
7012 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7013 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7014 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7015 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7016 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7017 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7018 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7019 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7020 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7021 " > ccccccccccccccccccccccccccccccccccccccccc;",
7022 Style);
7023
7024 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7025 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7026 " + cc;",
7027 Style);
7028 verifyFormat("int a = aa\n"
7029 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7030 " * cccccccccccccccccccccccccccccccccccc;\n",
7031 Style);
7032
7033 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7034 verifyFormat("return (a > b\n"
7035 " // comment1\n"
7036 " // comment2\n"
7037 " || c);",
7038 Style);
7039 }
7040
7041 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
7042 FormatStyle Style = getLLVMStyle();
7043 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7044 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7045 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7046 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7047 Style);
7048 }
7049
7050 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
7051 FormatStyle Style = getLLVMStyleWithColumns(40);
7052 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7053 Style.BinPackArguments = false;
7054 verifyFormat("void test() {\n"
7055 " someFunction(\n"
7056 " this + argument + is + quite\n"
7057 " + long + so + it + gets + wrapped\n"
7058 " + but + remains + bin - packed);\n"
7059 "}",
7060 Style);
7061 verifyFormat("void test() {\n"
7062 " someFunction(arg1,\n"
7063 " this + argument + is\n"
7064 " + quite + long + so\n"
7065 " + it + gets + wrapped\n"
7066 " + but + remains + bin\n"
7067 " - packed,\n"
7068 " arg3);\n"
7069 "}",
7070 Style);
7071 verifyFormat("void test() {\n"
7072 " someFunction(\n"
7073 " arg1,\n"
7074 " this + argument + has\n"
7075 " + anotherFunc(nested,\n"
7076 " calls + whose\n"
7077 " + arguments\n"
7078 " + are + also\n"
7079 " + wrapped,\n"
7080 " in + addition)\n"
7081 " + to + being + bin - packed,\n"
7082 " arg3);\n"
7083 "}",
7084 Style);
7085
7086 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7087 verifyFormat("void test() {\n"
7088 " someFunction(\n"
7089 " arg1,\n"
7090 " this + argument + has +\n"
7091 " anotherFunc(nested,\n"
7092 " calls + whose +\n"
7093 " arguments +\n"
7094 " are + also +\n"
7095 " wrapped,\n"
7096 " in + addition) +\n"
7097 " to + being + bin - packed,\n"
7098 " arg3);\n"
7099 "}",
7100 Style);
7101 }
7102
7103 TEST_F(FormatTest, BreakBinaryOperatorsInPresenceOfTemplates) {
7104 auto Style = getLLVMStyleWithColumns(45);
7105 EXPECT_EQ(Style.BreakBeforeBinaryOperators, FormatStyle::BOS_None);
7106 verifyFormat("bool b =\n"
7107 " is_default_constructible_v<hash<T>> and\n"
7108 " is_copy_constructible_v<hash<T>> and\n"
7109 " is_move_constructible_v<hash<T>> and\n"
7110 " is_copy_assignable_v<hash<T>> and\n"
7111 " is_move_assignable_v<hash<T>> and\n"
7112 " is_destructible_v<hash<T>> and\n"
7113 " is_swappable_v<hash<T>> and\n"
7114 " is_callable_v<hash<T>(T)>;",
7115 Style);
7116
7117 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7118 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n"
7119 " and is_copy_constructible_v<hash<T>>\n"
7120 " and is_move_constructible_v<hash<T>>\n"
7121 " and is_copy_assignable_v<hash<T>>\n"
7122 " and is_move_assignable_v<hash<T>>\n"
7123 " and is_destructible_v<hash<T>>\n"
7124 " and is_swappable_v<hash<T>>\n"
7125 " and is_callable_v<hash<T>(T)>;",
7126 Style);
7127
7128 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7129 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n"
7130 " and is_copy_constructible_v<hash<T>>\n"
7131 " and is_move_constructible_v<hash<T>>\n"
7132 " and is_copy_assignable_v<hash<T>>\n"
7133 " and is_move_assignable_v<hash<T>>\n"
7134 " and is_destructible_v<hash<T>>\n"
7135 " and is_swappable_v<hash<T>>\n"
7136 " and is_callable_v<hash<T>(T)>;",
7137 Style);
7138 }
7139
7140 TEST_F(FormatTest, ConstructorInitializers) {
7141 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
7142 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
7143 getLLVMStyleWithColumns(45));
7144 verifyFormat("Constructor()\n"
7145 " : Inttializer(FitsOnTheLine) {}",
7146 getLLVMStyleWithColumns(44));
7147 verifyFormat("Constructor()\n"
7148 " : Inttializer(FitsOnTheLine) {}",
7149 getLLVMStyleWithColumns(43));
7150
7151 verifyFormat("template <typename T>\n"
7152 "Constructor() : Initializer(FitsOnTheLine) {}",
7153 getLLVMStyleWithColumns(45));
7154
7155 verifyFormat(
7156 "SomeClass::Constructor()\n"
7157 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
7158
7159 verifyFormat(
7160 "SomeClass::Constructor()\n"
7161 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7162 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
7163 verifyFormat(
7164 "SomeClass::Constructor()\n"
7165 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7166 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
7167 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7168 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7169 " : aaaaaaaaaa(aaaaaa) {}");
7170
7171 verifyFormat("Constructor()\n"
7172 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7173 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7174 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7175 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
7176
7177 verifyFormat("Constructor()\n"
7178 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7179 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7180
7181 verifyFormat("Constructor(int Parameter = 0)\n"
7182 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
7183 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
7184 verifyFormat("Constructor()\n"
7185 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
7186 "}",
7187 getLLVMStyleWithColumns(60));
7188 verifyFormat("Constructor()\n"
7189 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7190 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
7191
7192 // Here a line could be saved by splitting the second initializer onto two
7193 // lines, but that is not desirable.
7194 verifyFormat("Constructor()\n"
7195 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
7196 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
7197 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7198
7199 FormatStyle OnePerLine = getLLVMStyle();
7200 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never;
7201 verifyFormat("MyClass::MyClass()\n"
7202 " : a(a),\n"
7203 " b(b),\n"
7204 " c(c) {}",
7205 OnePerLine);
7206 verifyFormat("MyClass::MyClass()\n"
7207 " : a(a), // comment\n"
7208 " b(b),\n"
7209 " c(c) {}",
7210 OnePerLine);
7211 verifyFormat("MyClass::MyClass(int a)\n"
7212 " : b(a), // comment\n"
7213 " c(a + 1) { // lined up\n"
7214 "}",
7215 OnePerLine);
7216 verifyFormat("Constructor()\n"
7217 " : a(b, b, b) {}",
7218 OnePerLine);
7219 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7220 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
7221 verifyFormat("SomeClass::Constructor()\n"
7222 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7223 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7224 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7225 OnePerLine);
7226 verifyFormat("SomeClass::Constructor()\n"
7227 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
7228 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7229 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7230 OnePerLine);
7231 verifyFormat("MyClass::MyClass(int var)\n"
7232 " : some_var_(var), // 4 space indent\n"
7233 " some_other_var_(var + 1) { // lined up\n"
7234 "}",
7235 OnePerLine);
7236 verifyFormat("Constructor()\n"
7237 " : aaaaa(aaaaaa),\n"
7238 " aaaaa(aaaaaa),\n"
7239 " aaaaa(aaaaaa),\n"
7240 " aaaaa(aaaaaa),\n"
7241 " aaaaa(aaaaaa) {}",
7242 OnePerLine);
7243 verifyFormat("Constructor()\n"
7244 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
7245 " aaaaaaaaaaaaaaaaaaaaaa) {}",
7246 OnePerLine);
7247 OnePerLine.BinPackParameters = false;
7248 verifyFormat(
7249 "Constructor()\n"
7250 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7251 " aaaaaaaaaaa().aaa(),\n"
7252 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7253 OnePerLine);
7254 OnePerLine.ColumnLimit = 60;
7255 verifyFormat("Constructor()\n"
7256 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7257 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
7258 OnePerLine);
7259
7260 EXPECT_EQ("Constructor()\n"
7261 " : // Comment forcing unwanted break.\n"
7262 " aaaa(aaaa) {}",
7263 format("Constructor() :\n"
7264 " // Comment forcing unwanted break.\n"
7265 " aaaa(aaaa) {}"));
7266 }
7267
7268 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
7269 FormatStyle Style = getLLVMStyleWithColumns(60);
7270 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7271 Style.BinPackParameters = false;
7272
7273 for (int i = 0; i < 4; ++i) {
7274 // Test all combinations of parameters that should not have an effect.
7275 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
7276 Style.AllowAllArgumentsOnNextLine = i & 2;
7277
7278 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7279 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7280 verifyFormat("Constructor()\n"
7281 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7282 Style);
7283 verifyFormat("Constructor() : a(a), b(b) {}", Style);
7284
7285 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7286 verifyFormat("Constructor()\n"
7287 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7288 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7289 Style);
7290 verifyFormat("Constructor() : a(a), b(b) {}", Style);
7291
7292 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
7293 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7294 verifyFormat("Constructor()\n"
7295 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7296 Style);
7297
7298 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7299 verifyFormat("Constructor()\n"
7300 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7301 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7302 Style);
7303
7304 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
7305 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7306 verifyFormat("Constructor() :\n"
7307 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7308 Style);
7309
7310 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7311 verifyFormat("Constructor() :\n"
7312 " aaaaaaaaaaaaaaaaaa(a),\n"
7313 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7314 Style);
7315 }
7316
7317 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
7318 // AllowAllConstructorInitializersOnNextLine in all
7319 // BreakConstructorInitializers modes
7320 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7321 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7322 verifyFormat("SomeClassWithALongName::Constructor(\n"
7323 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
7324 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7325 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7326 Style);
7327
7328 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7329 verifyFormat("SomeClassWithALongName::Constructor(\n"
7330 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7331 " int bbbbbbbbbbbbb,\n"
7332 " int cccccccccccccccc)\n"
7333 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7334 Style);
7335
7336 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7337 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7338 verifyFormat("SomeClassWithALongName::Constructor(\n"
7339 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7340 " int bbbbbbbbbbbbb)\n"
7341 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7342 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7343 Style);
7344
7345 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
7346
7347 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7348 verifyFormat("SomeClassWithALongName::Constructor(\n"
7349 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
7350 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7351 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7352 Style);
7353
7354 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7355 verifyFormat("SomeClassWithALongName::Constructor(\n"
7356 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7357 " int bbbbbbbbbbbbb,\n"
7358 " int cccccccccccccccc)\n"
7359 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7360 Style);
7361
7362 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7363 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7364 verifyFormat("SomeClassWithALongName::Constructor(\n"
7365 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7366 " int bbbbbbbbbbbbb)\n"
7367 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7368 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7369 Style);
7370
7371 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
7372 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7373 verifyFormat("SomeClassWithALongName::Constructor(\n"
7374 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
7375 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7376 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7377 Style);
7378
7379 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7380 verifyFormat("SomeClassWithALongName::Constructor(\n"
7381 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7382 " int bbbbbbbbbbbbb,\n"
7383 " int cccccccccccccccc) :\n"
7384 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7385 Style);
7386
7387 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7388 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7389 verifyFormat("SomeClassWithALongName::Constructor(\n"
7390 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7391 " int bbbbbbbbbbbbb) :\n"
7392 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7393 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7394 Style);
7395 }
7396
7397 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
7398 FormatStyle Style = getLLVMStyleWithColumns(60);
7399 Style.BinPackArguments = false;
7400 for (int i = 0; i < 4; ++i) {
7401 // Test all combinations of parameters that should not have an effect.
7402 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
7403 Style.PackConstructorInitializers =
7404 i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never;
7405
7406 Style.AllowAllArgumentsOnNextLine = true;
7407 verifyFormat("void foo() {\n"
7408 " FunctionCallWithReallyLongName(\n"
7409 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
7410 "}",
7411 Style);
7412 Style.AllowAllArgumentsOnNextLine = false;
7413 verifyFormat("void foo() {\n"
7414 " FunctionCallWithReallyLongName(\n"
7415 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7416 " bbbbbbbbbbbb);\n"
7417 "}",
7418 Style);
7419
7420 Style.AllowAllArgumentsOnNextLine = true;
7421 verifyFormat("void foo() {\n"
7422 " auto VariableWithReallyLongName = {\n"
7423 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
7424 "}",
7425 Style);
7426 Style.AllowAllArgumentsOnNextLine = false;
7427 verifyFormat("void foo() {\n"
7428 " auto VariableWithReallyLongName = {\n"
7429 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7430 " bbbbbbbbbbbb};\n"
7431 "}",
7432 Style);
7433 }
7434
7435 // This parameter should not affect declarations.
7436 Style.BinPackParameters = false;
7437 Style.AllowAllArgumentsOnNextLine = false;
7438 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7439 verifyFormat("void FunctionCallWithReallyLongName(\n"
7440 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
7441 Style);
7442 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7443 verifyFormat("void FunctionCallWithReallyLongName(\n"
7444 " int aaaaaaaaaaaaaaaaaaaaaaa,\n"
7445 " int bbbbbbbbbbbb);",
7446 Style);
7447 }
7448
7449 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
7450 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
7451 // and BAS_Align.
7452 FormatStyle Style = getLLVMStyleWithColumns(35);
7453 StringRef Input = "functionCall(paramA, paramB, paramC);\n"
7454 "void functionDecl(int A, int B, int C);";
7455 Style.AllowAllArgumentsOnNextLine = false;
7456 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7457 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
7458 " paramC);\n"
7459 "void functionDecl(int A, int B,\n"
7460 " int C);"),
7461 format(Input, Style));
7462 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
7463 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
7464 " paramC);\n"
7465 "void functionDecl(int A, int B,\n"
7466 " int C);"),
7467 format(Input, Style));
7468 // However, BAS_AlwaysBreak and BAS_BlockIndent should take precedence over
7469 // AllowAllArgumentsOnNextLine.
7470 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7471 EXPECT_EQ(StringRef("functionCall(\n"
7472 " paramA, paramB, paramC);\n"
7473 "void functionDecl(\n"
7474 " int A, int B, int C);"),
7475 format(Input, Style));
7476 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
7477 verifyFormat("functionCall(\n"
7478 " paramA, paramB, paramC\n"
7479 ");\n"
7480 "void functionDecl(\n"
7481 " int A, int B, int C\n"
7482 ");",
7483 Input, Style);
7484
7485 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
7486 // first argument.
7487 Style.AllowAllArgumentsOnNextLine = true;
7488 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7489 EXPECT_EQ(StringRef("functionCall(\n"
7490 " paramA, paramB, paramC);\n"
7491 "void functionDecl(\n"
7492 " int A, int B, int C);"),
7493 format(Input, Style));
7494 // It wouldn't fit on one line with aligned parameters so this setting
7495 // doesn't change anything for BAS_Align.
7496 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
7497 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
7498 " paramC);\n"
7499 "void functionDecl(int A, int B,\n"
7500 " int C);"),
7501 format(Input, Style));
7502 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7503 EXPECT_EQ(StringRef("functionCall(\n"
7504 " paramA, paramB, paramC);\n"
7505 "void functionDecl(\n"
7506 " int A, int B, int C);"),
7507 format(Input, Style));
7508 }
7509
7510 TEST_F(FormatTest, BreakBeforeInlineASMColon) {
7511 FormatStyle Style = getLLVMStyle();
7512 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Never;
7513 /* Test the behaviour with long lines */
7514 Style.ColumnLimit = 40;
7515 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
7516 " : : val);",
7517 Style);
7518 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
7519 " : val1 : val2);",
7520 Style);
7521 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
7522 " \"cpuid\\n\\t\"\n"
7523 " \"xchgq\\t%%rbx %%rsi\\n\\t\",\n"
7524 " : \"=a\" : \"a\");",
7525 Style);
7526 Style.ColumnLimit = 80;
7527 verifyFormat("asm volatile(\"string\", : : val);", Style);
7528 verifyFormat("asm volatile(\"string\", : val1 : val2);", Style);
7529
7530 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Always;
7531 verifyFormat("asm volatile(\"string\",\n"
7532 " :\n"
7533 " : val);",
7534 Style);
7535 verifyFormat("asm volatile(\"string\",\n"
7536 " : val1\n"
7537 " : val2);",
7538 Style);
7539 /* Test the behaviour with long lines */
7540 Style.ColumnLimit = 40;
7541 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
7542 " \"cpuid\\n\\t\"\n"
7543 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
7544 " : \"=a\"(*rEAX)\n"
7545 " : \"a\"(value));",
7546 Style);
7547 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
7548 " \"cpuid\\n\\t\"\n"
7549 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
7550 " :\n"
7551 " : \"a\"(value));",
7552 Style);
7553 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
7554 " :\n"
7555 " : val);",
7556 Style);
7557 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
7558 " : val1\n"
7559 " : val2);",
7560 Style);
7561 }
7562
7563 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
7564 FormatStyle Style = getLLVMStyle();
7565 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
7566
7567 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
7568 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
7569 getStyleWithColumns(Style, 45));
7570 verifyFormat("Constructor() :\n"
7571 " Initializer(FitsOnTheLine) {}",
7572 getStyleWithColumns(Style, 44));
7573 verifyFormat("Constructor() :\n"
7574 " Initializer(FitsOnTheLine) {}",
7575 getStyleWithColumns(Style, 43));
7576
7577 verifyFormat("template <typename T>\n"
7578 "Constructor() : Initializer(FitsOnTheLine) {}",
7579 getStyleWithColumns(Style, 50));
7580 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7581 verifyFormat(
7582 "SomeClass::Constructor() :\n"
7583 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
7584 Style);
7585 verifyFormat(
7586 "SomeClass::Constructor() : // NOLINT\n"
7587 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
7588 Style);
7589
7590 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
7591 verifyFormat(
7592 "SomeClass::Constructor() :\n"
7593 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
7594 Style);
7595
7596 verifyFormat(
7597 "SomeClass::Constructor() :\n"
7598 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7599 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7600 Style);
7601 verifyFormat(
7602 "SomeClass::Constructor() :\n"
7603 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7604 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
7605 Style);
7606 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7607 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
7608 " aaaaaaaaaa(aaaaaa) {}",
7609 Style);
7610
7611 verifyFormat("Constructor() :\n"
7612 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7613 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7614 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7615 " aaaaaaaaaaaaaaaaaaaaaaa() {}",
7616 Style);
7617
7618 verifyFormat("Constructor() :\n"
7619 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7620 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7621 Style);
7622
7623 verifyFormat("Constructor(int Parameter = 0) :\n"
7624 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
7625 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
7626 Style);
7627 verifyFormat("Constructor() :\n"
7628 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
7629 "}",
7630 getStyleWithColumns(Style, 60));
7631 verifyFormat("Constructor() :\n"
7632 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7633 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
7634 Style);
7635
7636 // Here a line could be saved by splitting the second initializer onto two
7637 // lines, but that is not desirable.
7638 verifyFormat("Constructor() :\n"
7639 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
7640 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
7641 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7642 Style);
7643
7644 FormatStyle OnePerLine = Style;
7645 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7646 verifyFormat("SomeClass::Constructor() :\n"
7647 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7648 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7649 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7650 OnePerLine);
7651 verifyFormat("SomeClass::Constructor() :\n"
7652 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
7653 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7654 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7655 OnePerLine);
7656 verifyFormat("Foo::Foo(int i, int j) : // NOLINT\n"
7657 " i(i), // comment\n"
7658 " j(j) {}",
7659 OnePerLine);
7660 verifyFormat("MyClass::MyClass(int var) :\n"
7661 " some_var_(var), // 4 space indent\n"
7662 " some_other_var_(var + 1) { // lined up\n"
7663 "}",
7664 OnePerLine);
7665 verifyFormat("Constructor() :\n"
7666 " aaaaa(aaaaaa),\n"
7667 " aaaaa(aaaaaa),\n"
7668 " aaaaa(aaaaaa),\n"
7669 " aaaaa(aaaaaa),\n"
7670 " aaaaa(aaaaaa) {}",
7671 OnePerLine);
7672 verifyFormat("Constructor() :\n"
7673 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
7674 " aaaaaaaaaaaaaaaaaaaaaa) {}",
7675 OnePerLine);
7676 OnePerLine.BinPackParameters = false;
7677 verifyFormat("Constructor() :\n"
7678 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7679 " aaaaaaaaaaa().aaa(),\n"
7680 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7681 OnePerLine);
7682 OnePerLine.ColumnLimit = 60;
7683 verifyFormat("Constructor() :\n"
7684 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7685 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
7686 OnePerLine);
7687
7688 verifyFormat("Constructor() :\n"
7689 " // Comment forcing unwanted break.\n"
7690 " aaaa(aaaa) {}",
7691 Style);
7692 verifyFormat("Constructor() : // NOLINT\n"
7693 " aaaa(aaaa) {}",
7694 Style);
7695 verifyFormat("Constructor() : // A very long trailing comment that cannot fit"
7696 " on a single\n"
7697 " // line.\n"
7698 " aaaa(aaaa) {}",
7699 "Constructor() : // A very long trailing comment that cannot fit"
7700 " on a single line.\n"
7701 " aaaa(aaaa) {}",
7702 Style);
7703
7704 Style.ColumnLimit = 0;
7705 verifyFormat("SomeClass::Constructor() :\n"
7706 " a(a) {}",
7707 Style);
7708 verifyFormat("SomeClass::Constructor() noexcept :\n"
7709 " a(a) {}",
7710 Style);
7711 verifyFormat("SomeClass::Constructor() :\n"
7712 " a(a), b(b), c(c) {}",
7713 Style);
7714 verifyFormat("SomeClass::Constructor() :\n"
7715 " a(a) {\n"
7716 " foo();\n"
7717 " bar();\n"
7718 "}",
7719 Style);
7720
7721 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
7722 verifyFormat("SomeClass::Constructor() :\n"
7723 " a(a), b(b), c(c) {\n"
7724 "}",
7725 Style);
7726 verifyFormat("SomeClass::Constructor() :\n"
7727 " a(a) {\n"
7728 "}",
7729 Style);
7730
7731 Style.ColumnLimit = 80;
7732 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
7733 Style.ConstructorInitializerIndentWidth = 2;
7734 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style);
7735 verifyFormat("SomeClass::Constructor() :\n"
7736 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7737 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
7738 Style);
7739
7740 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
7741 // well
7742 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
7743 verifyFormat(
7744 "class SomeClass\n"
7745 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7746 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
7747 Style);
7748 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
7749 verifyFormat(
7750 "class SomeClass\n"
7751 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7752 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
7753 Style);
7754 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
7755 verifyFormat(
7756 "class SomeClass :\n"
7757 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7758 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
7759 Style);
7760 Style.BreakInheritanceList = FormatStyle::BILS_AfterComma;
7761 verifyFormat(
7762 "class SomeClass\n"
7763 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7764 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
7765 Style);
7766 }
7767
7768 #ifndef EXPENSIVE_CHECKS
7769 // Expensive checks enables libstdc++ checking which includes validating the
7770 // state of ranges used in std::priority_queue - this blows out the
7771 // runtime/scalability of the function and makes this test unacceptably slow.
7772 TEST_F(FormatTest, MemoizationTests) {
7773 // This breaks if the memoization lookup does not take \c Indent and
7774 // \c LastSpace into account.
7775 verifyFormat(
7776 "extern CFRunLoopTimerRef\n"
7777 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
7778 " CFTimeInterval interval, CFOptionFlags flags,\n"
7779 " CFIndex order, CFRunLoopTimerCallBack callout,\n"
7780 " CFRunLoopTimerContext *context) {}");
7781
7782 // Deep nesting somewhat works around our memoization.
7783 verifyFormat(
7784 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
7785 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
7786 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
7787 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
7788 " aaaaa())))))))))))))))))))))))))))))))))))))));",
7789 getLLVMStyleWithColumns(65));
7790 verifyFormat(
7791 "aaaaa(\n"
7792 " aaaaa,\n"
7793 " aaaaa(\n"
7794 " aaaaa,\n"
7795 " aaaaa(\n"
7796 " aaaaa,\n"
7797 " aaaaa(\n"
7798 " aaaaa,\n"
7799 " aaaaa(\n"
7800 " aaaaa,\n"
7801 " aaaaa(\n"
7802 " aaaaa,\n"
7803 " aaaaa(\n"
7804 " aaaaa,\n"
7805 " aaaaa(\n"
7806 " aaaaa,\n"
7807 " aaaaa(\n"
7808 " aaaaa,\n"
7809 " aaaaa(\n"
7810 " aaaaa,\n"
7811 " aaaaa(\n"
7812 " aaaaa,\n"
7813 " aaaaa(\n"
7814 " aaaaa,\n"
7815 " aaaaa))))))))))));",
7816 getLLVMStyleWithColumns(65));
7817 verifyFormat(
7818 "a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(), a), a), a), a),\n"
7819 " a),\n"
7820 " a),\n"
7821 " a),\n"
7822 " a),\n"
7823 " a),\n"
7824 " a),\n"
7825 " a),\n"
7826 " a),\n"
7827 " a),\n"
7828 " a),\n"
7829 " a),\n"
7830 " a),\n"
7831 " a),\n"
7832 " a),\n"
7833 " a),\n"
7834 " a),\n"
7835 " a)",
7836 getLLVMStyleWithColumns(65));
7837
7838 // This test takes VERY long when memoization is broken.
7839 FormatStyle OnePerLine = getLLVMStyle();
7840 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7841 OnePerLine.BinPackParameters = false;
7842 std::string input = "Constructor()\n"
7843 " : aaaa(a,\n";
7844 for (unsigned i = 0, e = 80; i != e; ++i)
7845 input += " a,\n";
7846 input += " a) {}";
7847 verifyFormat(input, OnePerLine);
7848 }
7849 #endif
7850
7851 TEST_F(FormatTest, BreaksAsHighAsPossible) {
7852 verifyFormat(
7853 "void f() {\n"
7854 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
7855 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
7856 " f();\n"
7857 "}");
7858 verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
7859 " Intervals[i - 1].getRange().getLast()) {\n}");
7860 }
7861
7862 TEST_F(FormatTest, BreaksFunctionDeclarations) {
7863 // Principially, we break function declarations in a certain order:
7864 // 1) break amongst arguments.
7865 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
7866 " Cccccccccccccc cccccccccccccc);");
7867 verifyFormat("template <class TemplateIt>\n"
7868 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
7869 " TemplateIt *stop) {}");
7870
7871 // 2) break after return type.
7872 verifyFormat(
7873 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7874 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
7875 getGoogleStyle());
7876
7877 // 3) break after (.
7878 verifyFormat(
7879 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
7880 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
7881 getGoogleStyle());
7882
7883 // 4) break before after nested name specifiers.
7884 verifyFormat(
7885 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7886 "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
7887 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
7888 getGoogleStyle());
7889
7890 // However, there are exceptions, if a sufficient amount of lines can be
7891 // saved.
7892 // FIXME: The precise cut-offs wrt. the number of saved lines might need some
7893 // more adjusting.
7894 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
7895 " Cccccccccccccc cccccccccc,\n"
7896 " Cccccccccccccc cccccccccc,\n"
7897 " Cccccccccccccc cccccccccc,\n"
7898 " Cccccccccccccc cccccccccc);");
7899 verifyFormat(
7900 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7901 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7902 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7903 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
7904 getGoogleStyle());
7905 verifyFormat(
7906 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
7907 " Cccccccccccccc cccccccccc,\n"
7908 " Cccccccccccccc cccccccccc,\n"
7909 " Cccccccccccccc cccccccccc,\n"
7910 " Cccccccccccccc cccccccccc,\n"
7911 " Cccccccccccccc cccccccccc,\n"
7912 " Cccccccccccccc cccccccccc);");
7913 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
7914 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7915 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7916 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
7917 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
7918
7919 // Break after multi-line parameters.
7920 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7921 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7922 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7923 " bbbb bbbb);");
7924 verifyFormat("void SomeLoooooooooooongFunction(\n"
7925 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
7926 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7927 " int bbbbbbbbbbbbb);");
7928
7929 // Treat overloaded operators like other functions.
7930 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
7931 "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
7932 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
7933 "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
7934 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
7935 "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
7936 verifyGoogleFormat(
7937 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
7938 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
7939 verifyGoogleFormat(
7940 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
7941 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
7942 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7943 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
7944 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
7945 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
7946 verifyGoogleFormat(
7947 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
7948 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7949 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
7950 verifyGoogleFormat("template <typename T>\n"
7951 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7952 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
7953 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
7954
7955 FormatStyle Style = getLLVMStyle();
7956 Style.PointerAlignment = FormatStyle::PAS_Left;
7957 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7958 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
7959 Style);
7960 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
7961 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7962 Style);
7963 }
7964
7965 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
7966 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
7967 // Prefer keeping `::` followed by `operator` together.
7968 EXPECT_EQ("const aaaa::bbbbbbb &\n"
7969 "ccccccccc::operator++() {\n"
7970 " stuff();\n"
7971 "}",
7972 format("const aaaa::bbbbbbb\n"
7973 "&ccccccccc::operator++() { stuff(); }",
7974 getLLVMStyleWithColumns(40)));
7975 }
7976
7977 TEST_F(FormatTest, TrailingReturnType) {
7978 verifyFormat("auto foo() -> int;\n");
7979 // correct trailing return type spacing
7980 verifyFormat("auto operator->() -> int;\n");
7981 verifyFormat("auto operator++(int) -> int;\n");
7982
7983 verifyFormat("struct S {\n"
7984 " auto bar() const -> int;\n"
7985 "};");
7986 verifyFormat("template <size_t Order, typename T>\n"
7987 "auto load_img(const std::string &filename)\n"
7988 " -> alias::tensor<Order, T, mem::tag::cpu> {}");
7989 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
7990 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
7991 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
7992 verifyFormat("template <typename T>\n"
7993 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
7994 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
7995
7996 // Not trailing return types.
7997 verifyFormat("void f() { auto a = b->c(); }");
7998 verifyFormat("auto a = p->foo();");
7999 verifyFormat("int a = p->foo();");
8000 verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };");
8001 }
8002
8003 TEST_F(FormatTest, DeductionGuides) {
8004 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
8005 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
8006 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
8007 verifyFormat(
8008 "template <class... T>\n"
8009 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
8010 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
8011 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
8012 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
8013 verifyFormat("template <class T> A() -> A<(3 < 2)>;");
8014 verifyFormat("template <class T> A() -> A<((3) < (2))>;");
8015 verifyFormat("template <class T> x() -> x<1>;");
8016 verifyFormat("template <class T> explicit x(T &) -> x<1>;");
8017
8018 verifyFormat("A(const char *) -> A<string &>;");
8019 verifyFormat("A() -> A<int>;");
8020
8021 // Ensure not deduction guides.
8022 verifyFormat("c()->f<int>();");
8023 verifyFormat("x()->foo<1>;");
8024 verifyFormat("x = p->foo<3>();");
8025 verifyFormat("x()->x<1>();");
8026 }
8027
8028 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
8029 // Avoid breaking before trailing 'const' or other trailing annotations, if
8030 // they are not function-like.
8031 FormatStyle Style = getGoogleStyleWithColumns(47);
8032 verifyFormat("void someLongFunction(\n"
8033 " int someLoooooooooooooongParameter) const {\n}",
8034 getLLVMStyleWithColumns(47));
8035 verifyFormat("LoooooongReturnType\n"
8036 "someLoooooooongFunction() const {}",
8037 getLLVMStyleWithColumns(47));
8038 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
8039 " const {}",
8040 Style);
8041 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8042 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
8043 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8044 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
8045 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8046 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
8047 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
8048 " aaaaaaaaaaa aaaaa) const override;");
8049 verifyGoogleFormat(
8050 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8051 " const override;");
8052
8053 // Even if the first parameter has to be wrapped.
8054 verifyFormat("void someLongFunction(\n"
8055 " int someLongParameter) const {}",
8056 getLLVMStyleWithColumns(46));
8057 verifyFormat("void someLongFunction(\n"
8058 " int someLongParameter) const {}",
8059 Style);
8060 verifyFormat("void someLongFunction(\n"
8061 " int someLongParameter) override {}",
8062 Style);
8063 verifyFormat("void someLongFunction(\n"
8064 " int someLongParameter) OVERRIDE {}",
8065 Style);
8066 verifyFormat("void someLongFunction(\n"
8067 " int someLongParameter) final {}",
8068 Style);
8069 verifyFormat("void someLongFunction(\n"
8070 " int someLongParameter) FINAL {}",
8071 Style);
8072 verifyFormat("void someLongFunction(\n"
8073 " int parameter) const override {}",
8074 Style);
8075
8076 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
8077 verifyFormat("void someLongFunction(\n"
8078 " int someLongParameter) const\n"
8079 "{\n"
8080 "}",
8081 Style);
8082
8083 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
8084 verifyFormat("void someLongFunction(\n"
8085 " int someLongParameter) const\n"
8086 " {\n"
8087 " }",
8088 Style);
8089
8090 // Unless these are unknown annotations.
8091 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
8092 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8093 " LONG_AND_UGLY_ANNOTATION;");
8094
8095 // Breaking before function-like trailing annotations is fine to keep them
8096 // close to their arguments.
8097 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8098 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
8099 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
8100 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
8101 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
8102 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
8103 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
8104 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
8105 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
8106
8107 verifyFormat(
8108 "void aaaaaaaaaaaaaaaaaa()\n"
8109 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
8110 " aaaaaaaaaaaaaaaaaaaaaaaaa));");
8111 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8112 " __attribute__((unused));");
8113 verifyGoogleFormat(
8114 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8115 " GUARDED_BY(aaaaaaaaaaaa);");
8116 verifyGoogleFormat(
8117 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8118 " GUARDED_BY(aaaaaaaaaaaa);");
8119 verifyGoogleFormat(
8120 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
8121 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8122 verifyGoogleFormat(
8123 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
8124 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
8125 }
8126
8127 TEST_F(FormatTest, FunctionAnnotations) {
8128 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8129 "int OldFunction(const string &parameter) {}");
8130 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8131 "string OldFunction(const string &parameter) {}");
8132 verifyFormat("template <typename T>\n"
8133 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8134 "string OldFunction(const string &parameter) {}");
8135
8136 // Not function annotations.
8137 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8138 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
8139 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
8140 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
8141 verifyFormat("MACRO(abc).function() // wrap\n"
8142 " << abc;");
8143 verifyFormat("MACRO(abc)->function() // wrap\n"
8144 " << abc;");
8145 verifyFormat("MACRO(abc)::function() // wrap\n"
8146 " << abc;");
8147 }
8148
8149 TEST_F(FormatTest, BreaksDesireably) {
8150 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
8151 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
8152 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
8153 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8154 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
8155 "}");
8156
8157 verifyFormat(
8158 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8159 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
8160
8161 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8162 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8163 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8164
8165 verifyFormat(
8166 "aaaaaaaa(aaaaaaaaaaaaa,\n"
8167 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8168 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
8169 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8170 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
8171
8172 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
8173 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8174
8175 verifyFormat(
8176 "void f() {\n"
8177 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
8178 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8179 "}");
8180 verifyFormat(
8181 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8182 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8183 verifyFormat(
8184 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8185 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8186 verifyFormat(
8187 "aaaaaa(aaa,\n"
8188 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8189 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8190 " aaaa);");
8191 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8192 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8193 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8194
8195 // Indent consistently independent of call expression and unary operator.
8196 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8197 " dddddddddddddddddddddddddddddd));");
8198 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8199 " dddddddddddddddddddddddddddddd));");
8200 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
8201 " dddddddddddddddddddddddddddddd));");
8202
8203 // This test case breaks on an incorrect memoization, i.e. an optimization not
8204 // taking into account the StopAt value.
8205 verifyFormat(
8206 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8207 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8208 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8209 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8210
8211 verifyFormat("{\n {\n {\n"
8212 " Annotation.SpaceRequiredBefore =\n"
8213 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
8214 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
8215 " }\n }\n}");
8216
8217 // Break on an outer level if there was a break on an inner level.
8218 EXPECT_EQ("f(g(h(a, // comment\n"
8219 " b, c),\n"
8220 " d, e),\n"
8221 " x, y);",
8222 format("f(g(h(a, // comment\n"
8223 " b, c), d, e), x, y);"));
8224
8225 // Prefer breaking similar line breaks.
8226 verifyFormat(
8227 "const int kTrackingOptions = NSTrackingMouseMoved |\n"
8228 " NSTrackingMouseEnteredAndExited |\n"
8229 " NSTrackingActiveAlways;");
8230 }
8231
8232 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
8233 FormatStyle NoBinPacking = getGoogleStyle();
8234 NoBinPacking.BinPackParameters = false;
8235 NoBinPacking.BinPackArguments = true;
8236 verifyFormat("void f() {\n"
8237 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
8238 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8239 "}",
8240 NoBinPacking);
8241 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
8242 " int aaaaaaaaaaaaaaaaaaaa,\n"
8243 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8244 NoBinPacking);
8245
8246 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
8247 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8248 " vector<int> bbbbbbbbbbbbbbb);",
8249 NoBinPacking);
8250 // FIXME: This behavior difference is probably not wanted. However, currently
8251 // we cannot distinguish BreakBeforeParameter being set because of the wrapped
8252 // template arguments from BreakBeforeParameter being set because of the
8253 // one-per-line formatting.
8254 verifyFormat(
8255 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
8256 " aaaaaaaaaa> aaaaaaaaaa);",
8257 NoBinPacking);
8258 verifyFormat(
8259 "void fffffffffff(\n"
8260 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
8261 " aaaaaaaaaa);");
8262 }
8263
8264 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
8265 FormatStyle NoBinPacking = getGoogleStyle();
8266 NoBinPacking.BinPackParameters = false;
8267 NoBinPacking.BinPackArguments = false;
8268 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
8269 " aaaaaaaaaaaaaaaaaaaa,\n"
8270 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
8271 NoBinPacking);
8272 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
8273 " aaaaaaaaaaaaa,\n"
8274 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
8275 NoBinPacking);
8276 verifyFormat(
8277 "aaaaaaaa(aaaaaaaaaaaaa,\n"
8278 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8279 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
8280 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8281 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
8282 NoBinPacking);
8283 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
8284 " .aaaaaaaaaaaaaaaaaa();",
8285 NoBinPacking);
8286 verifyFormat("void f() {\n"
8287 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8288 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
8289 "}",
8290 NoBinPacking);
8291
8292 verifyFormat(
8293 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8294 " aaaaaaaaaaaa,\n"
8295 " aaaaaaaaaaaa);",
8296 NoBinPacking);
8297 verifyFormat(
8298 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
8299 " ddddddddddddddddddddddddddddd),\n"
8300 " test);",
8301 NoBinPacking);
8302
8303 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
8304 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
8305 " aaaaaaaaaaaaaaaaaaaaaaa>\n"
8306 " aaaaaaaaaaaaaaaaaa;",
8307 NoBinPacking);
8308 verifyFormat("a(\"a\"\n"
8309 " \"a\",\n"
8310 " a);");
8311
8312 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
8313 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
8314 " aaaaaaaaa,\n"
8315 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8316 NoBinPacking);
8317 verifyFormat(
8318 "void f() {\n"
8319 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
8320 " .aaaaaaa();\n"
8321 "}",
8322 NoBinPacking);
8323 verifyFormat(
8324 "template <class SomeType, class SomeOtherType>\n"
8325 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
8326 NoBinPacking);
8327 }
8328
8329 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
8330 FormatStyle Style = getLLVMStyleWithColumns(15);
8331 Style.ExperimentalAutoDetectBinPacking = true;
8332 EXPECT_EQ("aaa(aaaa,\n"
8333 " aaaa,\n"
8334 " aaaa);\n"
8335 "aaa(aaaa,\n"
8336 " aaaa,\n"
8337 " aaaa);",
8338 format("aaa(aaaa,\n" // one-per-line
8339 " aaaa,\n"
8340 " aaaa );\n"
8341 "aaa(aaaa, aaaa, aaaa);", // inconclusive
8342 Style));
8343 EXPECT_EQ("aaa(aaaa, aaaa,\n"
8344 " aaaa);\n"
8345 "aaa(aaaa, aaaa,\n"
8346 " aaaa);",
8347 format("aaa(aaaa, aaaa,\n" // bin-packed
8348 " aaaa );\n"
8349 "aaa(aaaa, aaaa, aaaa);", // inconclusive
8350 Style));
8351 }
8352
8353 TEST_F(FormatTest, FormatsBuilderPattern) {
8354 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
8355 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
8356 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
8357 " .StartsWith(\".init\", ORDER_INIT)\n"
8358 " .StartsWith(\".fini\", ORDER_FINI)\n"
8359 " .StartsWith(\".hash\", ORDER_HASH)\n"
8360 " .Default(ORDER_TEXT);\n");
8361
8362 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
8363 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
8364 verifyFormat("aaaaaaa->aaaaaaa\n"
8365 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8366 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8367 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
8368 verifyFormat(
8369 "aaaaaaa->aaaaaaa\n"
8370 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8371 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
8372 verifyFormat(
8373 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
8374 " aaaaaaaaaaaaaa);");
8375 verifyFormat(
8376 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
8377 " aaaaaa->aaaaaaaaaaaa()\n"
8378 " ->aaaaaaaaaaaaaaaa(\n"
8379 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8380 " ->aaaaaaaaaaaaaaaaa();");
8381 verifyGoogleFormat(
8382 "void f() {\n"
8383 " someo->Add((new util::filetools::Handler(dir))\n"
8384 " ->OnEvent1(NewPermanentCallback(\n"
8385 " this, &HandlerHolderClass::EventHandlerCBA))\n"
8386 " ->OnEvent2(NewPermanentCallback(\n"
8387 " this, &HandlerHolderClass::EventHandlerCBB))\n"
8388 " ->OnEvent3(NewPermanentCallback(\n"
8389 " this, &HandlerHolderClass::EventHandlerCBC))\n"
8390 " ->OnEvent5(NewPermanentCallback(\n"
8391 " this, &HandlerHolderClass::EventHandlerCBD))\n"
8392 " ->OnEvent6(NewPermanentCallback(\n"
8393 " this, &HandlerHolderClass::EventHandlerCBE)));\n"
8394 "}");
8395
8396 verifyFormat(
8397 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
8398 verifyFormat("aaaaaaaaaaaaaaa()\n"
8399 " .aaaaaaaaaaaaaaa()\n"
8400 " .aaaaaaaaaaaaaaa()\n"
8401 " .aaaaaaaaaaaaaaa()\n"
8402 " .aaaaaaaaaaaaaaa();");
8403 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
8404 " .aaaaaaaaaaaaaaa()\n"
8405 " .aaaaaaaaaaaaaaa()\n"
8406 " .aaaaaaaaaaaaaaa();");
8407 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
8408 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
8409 " .aaaaaaaaaaaaaaa();");
8410 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
8411 " ->aaaaaaaaaaaaaae(0)\n"
8412 " ->aaaaaaaaaaaaaaa();");
8413
8414 // Don't linewrap after very short segments.
8415 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8416 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8417 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8418 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8419 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8420 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8421 verifyFormat("aaa()\n"
8422 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8423 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8424 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8425
8426 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
8427 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8428 " .has<bbbbbbbbbbbbbbbbbbbbb>();");
8429 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
8430 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
8431 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
8432
8433 // Prefer not to break after empty parentheses.
8434 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
8435 " First->LastNewlineOffset);");
8436
8437 // Prefer not to create "hanging" indents.
8438 verifyFormat(
8439 "return !soooooooooooooome_map\n"
8440 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8441 " .second;");
8442 verifyFormat(
8443 "return aaaaaaaaaaaaaaaa\n"
8444 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
8445 " .aaaa(aaaaaaaaaaaaaa);");
8446 // No hanging indent here.
8447 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
8448 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8449 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
8450 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8451 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
8452 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8453 getLLVMStyleWithColumns(60));
8454 verifyFormat("aaaaaaaaaaaaaaaaaa\n"
8455 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
8456 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8457 getLLVMStyleWithColumns(59));
8458 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8459 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8460 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8461
8462 // Dont break if only closing statements before member call
8463 verifyFormat("test() {\n"
8464 " ([]() -> {\n"
8465 " int b = 32;\n"
8466 " return 3;\n"
8467 " }).foo();\n"
8468 "}");
8469 verifyFormat("test() {\n"
8470 " (\n"
8471 " []() -> {\n"
8472 " int b = 32;\n"
8473 " return 3;\n"
8474 " },\n"
8475 " foo, bar)\n"
8476 " .foo();\n"
8477 "}");
8478 verifyFormat("test() {\n"
8479 " ([]() -> {\n"
8480 " int b = 32;\n"
8481 " return 3;\n"
8482 " })\n"
8483 " .foo()\n"
8484 " .bar();\n"
8485 "}");
8486 verifyFormat("test() {\n"
8487 " ([]() -> {\n"
8488 " int b = 32;\n"
8489 " return 3;\n"
8490 " })\n"
8491 " .foo(\"aaaaaaaaaaaaaaaaa\"\n"
8492 " \"bbbb\");\n"
8493 "}",
8494 getLLVMStyleWithColumns(30));
8495 }
8496
8497 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
8498 verifyFormat(
8499 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
8500 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
8501 verifyFormat(
8502 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
8503 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
8504
8505 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
8506 " ccccccccccccccccccccccccc) {\n}");
8507 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
8508 " ccccccccccccccccccccccccc) {\n}");
8509
8510 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
8511 " ccccccccccccccccccccccccc) {\n}");
8512 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
8513 " ccccccccccccccccccccccccc) {\n}");
8514
8515 verifyFormat(
8516 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
8517 " ccccccccccccccccccccccccc) {\n}");
8518 verifyFormat(
8519 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
8520 " ccccccccccccccccccccccccc) {\n}");
8521
8522 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
8523 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
8524 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
8525 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
8526 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
8527 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
8528 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
8529 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
8530
8531 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
8532 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
8533 " aaaaaaaaaaaaaaa != aa) {\n}");
8534 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
8535 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
8536 " aaaaaaaaaaaaaaa != aa) {\n}");
8537 }
8538
8539 TEST_F(FormatTest, BreaksAfterAssignments) {
8540 verifyFormat(
8541 "unsigned Cost =\n"
8542 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
8543 " SI->getPointerAddressSpaceee());\n");
8544 verifyFormat(
8545 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
8546 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
8547
8548 verifyFormat(
8549 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
8550 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
8551 verifyFormat("unsigned OriginalStartColumn =\n"
8552 " SourceMgr.getSpellingColumnNumber(\n"
8553 " Current.FormatTok.getStartOfNonWhitespace()) -\n"
8554 " 1;");
8555 }
8556
8557 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
8558 FormatStyle Style = getLLVMStyle();
8559 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8560 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
8561 Style);
8562
8563 Style.PenaltyBreakAssignment = 20;
8564 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
8565 " cccccccccccccccccccccccccc;",
8566 Style);
8567 }
8568
8569 TEST_F(FormatTest, AlignsAfterAssignments) {
8570 verifyFormat(
8571 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8572 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
8573 verifyFormat(
8574 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8575 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
8576 verifyFormat(
8577 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8578 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
8579 verifyFormat(
8580 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8581 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
8582 verifyFormat(
8583 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
8584 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
8585 " aaaaaaaaaaaaaaaaaaaaaaaa;");
8586 }
8587
8588 TEST_F(FormatTest, AlignsAfterReturn) {
8589 verifyFormat(
8590 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8591 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
8592 verifyFormat(
8593 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8594 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
8595 verifyFormat(
8596 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
8597 " aaaaaaaaaaaaaaaaaaaaaa();");
8598 verifyFormat(
8599 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
8600 " aaaaaaaaaaaaaaaaaaaaaa());");
8601 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8602 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8603 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8604 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
8605 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8606 verifyFormat("return\n"
8607 " // true if code is one of a or b.\n"
8608 " code == a || code == b;");
8609 }
8610
8611 TEST_F(FormatTest, AlignsAfterOpenBracket) {
8612 verifyFormat(
8613 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
8614 " aaaaaaaaa aaaaaaa) {}");
8615 verifyFormat(
8616 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
8617 " aaaaaaaaaaa aaaaaaaaa);");
8618 verifyFormat(
8619 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
8620 " aaaaaaaaaaaaaaaaaaaaa));");
8621 FormatStyle Style = getLLVMStyle();
8622 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
8623 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8624 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
8625 Style);
8626 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
8627 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
8628 Style);
8629 verifyFormat("SomeLongVariableName->someFunction(\n"
8630 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
8631 Style);
8632 verifyFormat(
8633 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
8634 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8635 Style);
8636 verifyFormat(
8637 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
8638 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8639 Style);
8640 verifyFormat(
8641 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
8642 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
8643 Style);
8644
8645 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
8646 " ccccccc(aaaaaaaaaaaaaaaaa, //\n"
8647 " b));",
8648 Style);
8649
8650 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8651 Style.BinPackArguments = false;
8652 Style.BinPackParameters = false;
8653 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8654 " aaaaaaaaaaa aaaaaaaa,\n"
8655 " aaaaaaaaa aaaaaaa,\n"
8656 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8657 Style);
8658 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
8659 " aaaaaaaaaaa aaaaaaaaa,\n"
8660 " aaaaaaaaaaa aaaaaaaaa,\n"
8661 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8662 Style);
8663 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
8664 " aaaaaaaaaaaaaaa,\n"
8665 " aaaaaaaaaaaaaaaaaaaaa,\n"
8666 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
8667 Style);
8668 verifyFormat(
8669 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
8670 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
8671 Style);
8672 verifyFormat(
8673 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
8674 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
8675 Style);
8676 verifyFormat(
8677 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
8678 " aaaaaaaaaaaaaaaaaaaaa(\n"
8679 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
8680 " aaaaaaaaaaaaaaaa);",
8681 Style);
8682 verifyFormat(
8683 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
8684 " aaaaaaaaaaaaaaaaaaaaa(\n"
8685 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
8686 " aaaaaaaaaaaaaaaa);",
8687 Style);
8688
8689 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
8690 Style.BinPackArguments = false;
8691 Style.BinPackParameters = false;
8692 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8693 " aaaaaaaaaaa aaaaaaaa,\n"
8694 " aaaaaaaaa aaaaaaa,\n"
8695 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8696 ") {}",
8697 Style);
8698 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
8699 " aaaaaaaaaaa aaaaaaaaa,\n"
8700 " aaaaaaaaaaa aaaaaaaaa,\n"
8701 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8702 ");",
8703 Style);
8704 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
8705 " aaaaaaaaaaaaaaa,\n"
8706 " aaaaaaaaaaaaaaaaaaaaa,\n"
8707 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8708 "));",
8709 Style);
8710 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
8711 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
8712 "));",
8713 Style);
8714 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
8715 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
8716 "));",
8717 Style);
8718 verifyFormat(
8719 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
8720 " aaaaaaaaaaaaaaaaaaaaa(\n"
8721 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
8722 " ),\n"
8723 " aaaaaaaaaaaaaaaa\n"
8724 ");",
8725 Style);
8726 verifyFormat(
8727 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
8728 " aaaaaaaaaaaaaaaaaaaaa(\n"
8729 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
8730 " ) &&\n"
8731 " aaaaaaaaaaaaaaaa\n"
8732 ");",
8733 Style);
8734 }
8735
8736 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
8737 FormatStyle Style = getLLVMStyleWithColumns(40);
8738 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
8739 " bbbbbbbbbbbbbbbbbbbbbb);",
8740 Style);
8741 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
8742 Style.AlignOperands = FormatStyle::OAS_DontAlign;
8743 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
8744 " bbbbbbbbbbbbbbbbbbbbbb);",
8745 Style);
8746 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
8747 Style.AlignOperands = FormatStyle::OAS_Align;
8748 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
8749 " bbbbbbbbbbbbbbbbbbbbbb);",
8750 Style);
8751 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
8752 Style.AlignOperands = FormatStyle::OAS_DontAlign;
8753 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
8754 " bbbbbbbbbbbbbbbbbbbbbb);",
8755 Style);
8756 }
8757
8758 TEST_F(FormatTest, BreaksConditionalExpressions) {
8759 verifyFormat(
8760 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8761 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8762 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8763 verifyFormat(
8764 "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
8765 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8766 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8767 verifyFormat(
8768 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8769 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8770 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
8771 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8772 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8773 verifyFormat(
8774 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
8775 " : aaaaaaaaaaaaa);");
8776 verifyFormat(
8777 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8778 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8779 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8780 " aaaaaaaaaaaaa);");
8781 verifyFormat(
8782 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8783 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8784 " aaaaaaaaaaaaa);");
8785 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8786 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8787 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8788 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8789 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8790 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8791 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8792 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8793 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8794 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8795 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8796 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8797 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8798 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8799 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8800 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8801 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8802 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8803 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8804 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8805 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
8806 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8807 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8808 " : aaaaaaaaaaaaaaaa;");
8809 verifyFormat(
8810 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8811 " ? aaaaaaaaaaaaaaa\n"
8812 " : aaaaaaaaaaaaaaa;");
8813 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
8814 " aaaaaaaaa\n"
8815 " ? b\n"
8816 " : c);");
8817 verifyFormat("return aaaa == bbbb\n"
8818 " // comment\n"
8819 " ? aaaa\n"
8820 " : bbbb;");
8821 verifyFormat("unsigned Indent =\n"
8822 " format(TheLine.First,\n"
8823 " IndentForLevel[TheLine.Level] >= 0\n"
8824 " ? IndentForLevel[TheLine.Level]\n"
8825 " : TheLine * 2,\n"
8826 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
8827 getLLVMStyleWithColumns(60));
8828 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
8829 " ? aaaaaaaaaaaaaaa\n"
8830 " : bbbbbbbbbbbbbbb //\n"
8831 " ? ccccccccccccccc\n"
8832 " : ddddddddddddddd;");
8833 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
8834 " ? aaaaaaaaaaaaaaa\n"
8835 " : (bbbbbbbbbbbbbbb //\n"
8836 " ? ccccccccccccccc\n"
8837 " : ddddddddddddddd);");
8838 verifyFormat(
8839 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8840 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8841 " aaaaaaaaaaaaaaaaaaaaa +\n"
8842 " aaaaaaaaaaaaaaaaaaaaa\n"
8843 " : aaaaaaaaaa;");
8844 verifyFormat(
8845 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8846 " : aaaaaaaaaaaaaaaaaaaaaa\n"
8847 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8848
8849 FormatStyle NoBinPacking = getLLVMStyle();
8850 NoBinPacking.BinPackArguments = false;
8851 verifyFormat(
8852 "void f() {\n"
8853 " g(aaa,\n"
8854 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
8855 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8856 " ? aaaaaaaaaaaaaaa\n"
8857 " : aaaaaaaaaaaaaaa);\n"
8858 "}",
8859 NoBinPacking);
8860 verifyFormat(
8861 "void f() {\n"
8862 " g(aaa,\n"
8863 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
8864 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8865 " ?: aaaaaaaaaaaaaaa);\n"
8866 "}",
8867 NoBinPacking);
8868
8869 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
8870 " // comment.\n"
8871 " ccccccccccccccccccccccccccccccccccccccc\n"
8872 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8873 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
8874
8875 // Assignments in conditional expressions. Apparently not uncommon :-(.
8876 verifyFormat("return a != b\n"
8877 " // comment\n"
8878 " ? a = b\n"
8879 " : a = b;");
8880 verifyFormat("return a != b\n"
8881 " // comment\n"
8882 " ? a = a != b\n"
8883 " // comment\n"
8884 " ? a = b\n"
8885 " : a\n"
8886 " : a;\n");
8887 verifyFormat("return a != b\n"
8888 " // comment\n"
8889 " ? a\n"
8890 " : a = a != b\n"
8891 " // comment\n"
8892 " ? a = b\n"
8893 " : a;");
8894
8895 // Chained conditionals
8896 FormatStyle Style = getLLVMStyleWithColumns(70);
8897 Style.AlignOperands = FormatStyle::OAS_Align;
8898 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
8899 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
8900 " : 3333333333333333;",
8901 Style);
8902 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
8903 " : bbbbbbbbbb ? 2222222222222222\n"
8904 " : 3333333333333333;",
8905 Style);
8906 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n"
8907 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
8908 " : 3333333333333333;",
8909 Style);
8910 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
8911 " : bbbbbbbbbbbbbb ? 222222\n"
8912 " : 333333;",
8913 Style);
8914 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
8915 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
8916 " : cccccccccccccc ? 3333333333333333\n"
8917 " : 4444444444444444;",
8918 Style);
8919 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
8920 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
8921 " : 3333333333333333;",
8922 Style);
8923 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
8924 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
8925 " : (aaa ? bbb : ccc);",
8926 Style);
8927 verifyFormat(
8928 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8929 " : cccccccccccccccccc)\n"
8930 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
8931 " : 3333333333333333;",
8932 Style);
8933 verifyFormat(
8934 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8935 " : cccccccccccccccccc)\n"
8936 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
8937 " : 3333333333333333;",
8938 Style);
8939 verifyFormat(
8940 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8941 " : dddddddddddddddddd)\n"
8942 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
8943 " : 3333333333333333;",
8944 Style);
8945 verifyFormat(
8946 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8947 " : dddddddddddddddddd)\n"
8948 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
8949 " : 3333333333333333;",
8950 Style);
8951 verifyFormat(
8952 "return aaaaaaaaa ? 1111111111111111\n"
8953 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
8954 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8955 " : dddddddddddddddddd)\n",
8956 Style);
8957 verifyFormat(
8958 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
8959 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
8960 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8961 " : cccccccccccccccccc);",
8962 Style);
8963 verifyFormat(
8964 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8965 " : ccccccccccccccc ? dddddddddddddddddd\n"
8966 " : eeeeeeeeeeeeeeeeee)\n"
8967 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
8968 " : 3333333333333333;",
8969 Style);
8970 verifyFormat(
8971 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8972 " : ccccccccccccccc ? dddddddddddddddddd\n"
8973 " : eeeeeeeeeeeeeeeeee)\n"
8974 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
8975 " : 3333333333333333;",
8976 Style);
8977 verifyFormat(
8978 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8979 " : cccccccccccc ? dddddddddddddddddd\n"
8980 " : eeeeeeeeeeeeeeeeee)\n"
8981 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
8982 " : 3333333333333333;",
8983 Style);
8984 verifyFormat(
8985 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8986 " : cccccccccccccccccc\n"
8987 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
8988 " : 3333333333333333;",
8989 Style);
8990 verifyFormat(
8991 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8992 " : cccccccccccccccc ? dddddddddddddddddd\n"
8993 " : eeeeeeeeeeeeeeeeee\n"
8994 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
8995 " : 3333333333333333;",
8996 Style);
8997 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
8998 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8999 " : cccccccccccccccccc ? dddddddddddddddddd\n"
9000 " : eeeeeeeeeeeeeeeeee)\n"
9001 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
9002 " : 3333333333333333;",
9003 Style);
9004 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
9005 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9006 " : cccccccccccccccc ? dddddddddddddddddd\n"
9007 " : eeeeeeeeeeeeeeeeee\n"
9008 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
9009 " : 3333333333333333;",
9010 Style);
9011
9012 Style.AlignOperands = FormatStyle::OAS_DontAlign;
9013 Style.BreakBeforeTernaryOperators = false;
9014 // FIXME: Aligning the question marks is weird given DontAlign.
9015 // Consider disabling this alignment in this case. Also check whether this
9016 // will render the adjustment from https://reviews.llvm.org/D82199
9017 // unnecessary.
9018 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
9019 " bbbb ? cccccccccccccccccc :\n"
9020 " ddddd;\n",
9021 Style);
9022
9023 EXPECT_EQ(
9024 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
9025 " /*\n"
9026 " */\n"
9027 " function() {\n"
9028 " try {\n"
9029 " return JJJJJJJJJJJJJJ(\n"
9030 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
9031 " }\n"
9032 " } :\n"
9033 " function() {};",
9034 format(
9035 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
9036 " /*\n"
9037 " */\n"
9038 " function() {\n"
9039 " try {\n"
9040 " return JJJJJJJJJJJJJJ(\n"
9041 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
9042 " }\n"
9043 " } :\n"
9044 " function() {};",
9045 getGoogleStyle(FormatStyle::LK_JavaScript)));
9046 }
9047
9048 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
9049 FormatStyle Style = getLLVMStyleWithColumns(70);
9050 Style.BreakBeforeTernaryOperators = false;
9051 verifyFormat(
9052 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9053 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9054 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9055 Style);
9056 verifyFormat(
9057 "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
9058 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9059 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9060 Style);
9061 verifyFormat(
9062 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9063 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9064 Style);
9065 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
9066 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9067 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9068 Style);
9069 verifyFormat(
9070 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
9071 " aaaaaaaaaaaaa);",
9072 Style);
9073 verifyFormat(
9074 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9075 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9076 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9077 " aaaaaaaaaaaaa);",
9078 Style);
9079 verifyFormat(
9080 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9081 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9082 " aaaaaaaaaaaaa);",
9083 Style);
9084 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9085 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9086 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
9087 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9088 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9089 Style);
9090 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9091 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9092 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9093 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
9094 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9095 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9096 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9097 Style);
9098 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9099 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
9100 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9101 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9102 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9103 Style);
9104 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9105 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9106 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9107 Style);
9108 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
9109 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9110 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9111 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9112 Style);
9113 verifyFormat(
9114 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9115 " aaaaaaaaaaaaaaa :\n"
9116 " aaaaaaaaaaaaaaa;",
9117 Style);
9118 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
9119 " aaaaaaaaa ?\n"
9120 " b :\n"
9121 " c);",
9122 Style);
9123 verifyFormat("unsigned Indent =\n"
9124 " format(TheLine.First,\n"
9125 " IndentForLevel[TheLine.Level] >= 0 ?\n"
9126 " IndentForLevel[TheLine.Level] :\n"
9127 " TheLine * 2,\n"
9128 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
9129 Style);
9130 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
9131 " aaaaaaaaaaaaaaa :\n"
9132 " bbbbbbbbbbbbbbb ? //\n"
9133 " ccccccccccccccc :\n"
9134 " ddddddddddddddd;",
9135 Style);
9136 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
9137 " aaaaaaaaaaaaaaa :\n"
9138 " (bbbbbbbbbbbbbbb ? //\n"
9139 " ccccccccccccccc :\n"
9140 " ddddddddddddddd);",
9141 Style);
9142 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9143 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
9144 " ccccccccccccccccccccccccccc;",
9145 Style);
9146 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9147 " aaaaa :\n"
9148 " bbbbbbbbbbbbbbb + cccccccccccccccc;",
9149 Style);
9150
9151 // Chained conditionals
9152 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9153 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9154 " 3333333333333333;",
9155 Style);
9156 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9157 " bbbbbbbbbb ? 2222222222222222 :\n"
9158 " 3333333333333333;",
9159 Style);
9160 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n"
9161 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9162 " 3333333333333333;",
9163 Style);
9164 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9165 " bbbbbbbbbbbbbbbb ? 222222 :\n"
9166 " 333333;",
9167 Style);
9168 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9169 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9170 " cccccccccccccccc ? 3333333333333333 :\n"
9171 " 4444444444444444;",
9172 Style);
9173 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
9174 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9175 " 3333333333333333;",
9176 Style);
9177 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9178 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9179 " (aaa ? bbb : ccc);",
9180 Style);
9181 verifyFormat(
9182 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9183 " cccccccccccccccccc) :\n"
9184 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9185 " 3333333333333333;",
9186 Style);
9187 verifyFormat(
9188 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9189 " cccccccccccccccccc) :\n"
9190 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9191 " 3333333333333333;",
9192 Style);
9193 verifyFormat(
9194 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9195 " dddddddddddddddddd) :\n"
9196 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9197 " 3333333333333333;",
9198 Style);
9199 verifyFormat(
9200 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9201 " dddddddddddddddddd) :\n"
9202 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9203 " 3333333333333333;",
9204 Style);
9205 verifyFormat(
9206 "return aaaaaaaaa ? 1111111111111111 :\n"
9207 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9208 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9209 " dddddddddddddddddd)\n",
9210 Style);
9211 verifyFormat(
9212 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9213 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9214 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9215 " cccccccccccccccccc);",
9216 Style);
9217 verifyFormat(
9218 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9219 " ccccccccccccccccc ? dddddddddddddddddd :\n"
9220 " eeeeeeeeeeeeeeeeee) :\n"
9221 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9222 " 3333333333333333;",
9223 Style);
9224 verifyFormat(
9225 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9226 " ccccccccccccc ? dddddddddddddddddd :\n"
9227 " eeeeeeeeeeeeeeeeee) :\n"
9228 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9229 " 3333333333333333;",
9230 Style);
9231 verifyFormat(
9232 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9233 " ccccccccccccccccc ? dddddddddddddddddd :\n"
9234 " eeeeeeeeeeeeeeeeee) :\n"
9235 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9236 " 3333333333333333;",
9237 Style);
9238 verifyFormat(
9239 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9240 " cccccccccccccccccc :\n"
9241 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9242 " 3333333333333333;",
9243 Style);
9244 verifyFormat(
9245 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9246 " cccccccccccccccccc ? dddddddddddddddddd :\n"
9247 " eeeeeeeeeeeeeeeeee :\n"
9248 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9249 " 3333333333333333;",
9250 Style);
9251 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
9252 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9253 " cccccccccccccccccc ? dddddddddddddddddd :\n"
9254 " eeeeeeeeeeeeeeeeee) :\n"
9255 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9256 " 3333333333333333;",
9257 Style);
9258 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
9259 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9260 " cccccccccccccccccccc ? dddddddddddddddddd :\n"
9261 " eeeeeeeeeeeeeeeeee :\n"
9262 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9263 " 3333333333333333;",
9264 Style);
9265 }
9266
9267 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
9268 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
9269 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
9270 verifyFormat("bool a = true, b = false;");
9271
9272 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9273 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
9274 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
9275 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
9276 verifyFormat(
9277 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
9278 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
9279 " d = e && f;");
9280 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
9281 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
9282 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
9283 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
9284 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
9285 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
9286
9287 FormatStyle Style = getGoogleStyle();
9288 Style.PointerAlignment = FormatStyle::PAS_Left;
9289 Style.DerivePointerAlignment = false;
9290 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9291 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
9292 " *b = bbbbbbbbbbbbbbbbbbb;",
9293 Style);
9294 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
9295 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
9296 Style);
9297 verifyFormat("vector<int*> a, b;", Style);
9298 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
9299 verifyFormat("/*comment*/ for (int *p, *q; p != q; p = p->next) {\n}", Style);
9300 verifyFormat("if (int *p, *q; p != q) {\n p = p->next;\n}", Style);
9301 verifyFormat("/*comment*/ if (int *p, *q; p != q) {\n p = p->next;\n}",
9302 Style);
9303 verifyFormat("switch (int *p, *q; p != q) {\n default:\n break;\n}",
9304 Style);
9305 verifyFormat(
9306 "/*comment*/ switch (int *p, *q; p != q) {\n default:\n break;\n}",
9307 Style);
9308
9309 verifyFormat("if ([](int* p, int* q) {}()) {\n}", Style);
9310 verifyFormat("for ([](int* p, int* q) {}();;) {\n}", Style);
9311 verifyFormat("for (; [](int* p, int* q) {}();) {\n}", Style);
9312 verifyFormat("for (;; [](int* p, int* q) {}()) {\n}", Style);
9313 verifyFormat("switch ([](int* p, int* q) {}()) {\n default:\n break;\n}",
9314 Style);
9315 }
9316
9317 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
9318 verifyFormat("arr[foo ? bar : baz];");
9319 verifyFormat("f()[foo ? bar : baz];");
9320 verifyFormat("(a + b)[foo ? bar : baz];");
9321 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
9322 }
9323
9324 TEST_F(FormatTest, AlignsStringLiterals) {
9325 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
9326 " \"short literal\");");
9327 verifyFormat(
9328 "looooooooooooooooooooooooongFunction(\n"
9329 " \"short literal\"\n"
9330 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
9331 verifyFormat("someFunction(\"Always break between multi-line\"\n"
9332 " \" string literals\",\n"
9333 " also, other, parameters);");
9334 EXPECT_EQ("fun + \"1243\" /* comment */\n"
9335 " \"5678\";",
9336 format("fun + \"1243\" /* comment */\n"
9337 " \"5678\";",
9338 getLLVMStyleWithColumns(28)));
9339 EXPECT_EQ(
9340 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
9341 " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
9342 " \"aaaaaaaaaaaaaaaa\";",
9343 format("aaaaaa ="
9344 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
9345 "aaaaaaaaaaaaaaaaaaaaa\" "
9346 "\"aaaaaaaaaaaaaaaa\";"));
9347 verifyFormat("a = a + \"a\"\n"
9348 " \"a\"\n"
9349 " \"a\";");
9350 verifyFormat("f(\"a\", \"b\"\n"
9351 " \"c\");");
9352
9353 verifyFormat(
9354 "#define LL_FORMAT \"ll\"\n"
9355 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
9356 " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
9357
9358 verifyFormat("#define A(X) \\\n"
9359 " \"aaaaa\" #X \"bbbbbb\" \\\n"
9360 " \"ccccc\"",
9361 getLLVMStyleWithColumns(23));
9362 verifyFormat("#define A \"def\"\n"
9363 "f(\"abc\" A \"ghi\"\n"
9364 " \"jkl\");");
9365
9366 verifyFormat("f(L\"a\"\n"
9367 " L\"b\");");
9368 verifyFormat("#define A(X) \\\n"
9369 " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
9370 " L\"ccccc\"",
9371 getLLVMStyleWithColumns(25));
9372
9373 verifyFormat("f(@\"a\"\n"
9374 " @\"b\");");
9375 verifyFormat("NSString s = @\"a\"\n"
9376 " @\"b\"\n"
9377 " @\"c\";");
9378 verifyFormat("NSString s = @\"a\"\n"
9379 " \"b\"\n"
9380 " \"c\";");
9381 }
9382
9383 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
9384 FormatStyle Style = getLLVMStyle();
9385 // No declarations or definitions should be moved to own line.
9386 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
9387 verifyFormat("class A {\n"
9388 " int f() { return 1; }\n"
9389 " int g();\n"
9390 "};\n"
9391 "int f() { return 1; }\n"
9392 "int g();\n",
9393 Style);
9394
9395 // All declarations and definitions should have the return type moved to its
9396 // own line.
9397 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
9398 Style.TypenameMacros = {"LIST"};
9399 verifyFormat("SomeType\n"
9400 "funcdecl(LIST(uint64_t));",
9401 Style);
9402 verifyFormat("class E {\n"
9403 " int\n"
9404 " f() {\n"
9405 " return 1;\n"
9406 " }\n"
9407 " int\n"
9408 " g();\n"
9409 "};\n"
9410 "int\n"
9411 "f() {\n"
9412 " return 1;\n"
9413 "}\n"
9414 "int\n"
9415 "g();\n",
9416 Style);
9417
9418 // Top-level definitions, and no kinds of declarations should have the
9419 // return type moved to its own line.
9420 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
9421 verifyFormat("class B {\n"
9422 " int f() { return 1; }\n"
9423 " int g();\n"
9424 "};\n"
9425 "int\n"
9426 "f() {\n"
9427 " return 1;\n"
9428 "}\n"
9429 "int g();\n",
9430 Style);
9431
9432 // Top-level definitions and declarations should have the return type moved
9433 // to its own line.
9434 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
9435 verifyFormat("class C {\n"
9436 " int f() { return 1; }\n"
9437 " int g();\n"
9438 "};\n"
9439 "int\n"
9440 "f() {\n"
9441 " return 1;\n"
9442 "}\n"
9443 "int\n"
9444 "g();\n",
9445 Style);
9446
9447 // All definitions should have the return type moved to its own line, but no
9448 // kinds of declarations.
9449 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
9450 verifyFormat("class D {\n"
9451 " int\n"
9452 " f() {\n"
9453 " return 1;\n"
9454 " }\n"
9455 " int g();\n"
9456 "};\n"
9457 "int\n"
9458 "f() {\n"
9459 " return 1;\n"
9460 "}\n"
9461 "int g();\n",
9462 Style);
9463 verifyFormat("const char *\n"
9464 "f(void) {\n" // Break here.
9465 " return \"\";\n"
9466 "}\n"
9467 "const char *bar(void);\n", // No break here.
9468 Style);
9469 verifyFormat("template <class T>\n"
9470 "T *\n"
9471 "f(T &c) {\n" // Break here.
9472 " return NULL;\n"
9473 "}\n"
9474 "template <class T> T *f(T &c);\n", // No break here.
9475 Style);
9476 verifyFormat("class C {\n"
9477 " int\n"
9478 " operator+() {\n"
9479 " return 1;\n"
9480 " }\n"
9481 " int\n"
9482 " operator()() {\n"
9483 " return 1;\n"
9484 " }\n"
9485 "};\n",
9486 Style);
9487 verifyFormat("void\n"
9488 "A::operator()() {}\n"
9489 "void\n"
9490 "A::operator>>() {}\n"
9491 "void\n"
9492 "A::operator+() {}\n"
9493 "void\n"
9494 "A::operator*() {}\n"
9495 "void\n"
9496 "A::operator->() {}\n"
9497 "void\n"
9498 "A::operator void *() {}\n"
9499 "void\n"
9500 "A::operator void &() {}\n"
9501 "void\n"
9502 "A::operator void &&() {}\n"
9503 "void\n"
9504 "A::operator char *() {}\n"
9505 "void\n"
9506 "A::operator[]() {}\n"
9507 "void\n"
9508 "A::operator!() {}\n"
9509 "void\n"
9510 "A::operator**() {}\n"
9511 "void\n"
9512 "A::operator<Foo> *() {}\n"
9513 "void\n"
9514 "A::operator<Foo> **() {}\n"
9515 "void\n"
9516 "A::operator<Foo> &() {}\n"
9517 "void\n"
9518 "A::operator void **() {}\n",
9519 Style);
9520 verifyFormat("constexpr auto\n"
9521 "operator()() const -> reference {}\n"
9522 "constexpr auto\n"
9523 "operator>>() const -> reference {}\n"
9524 "constexpr auto\n"
9525 "operator+() const -> reference {}\n"
9526 "constexpr auto\n"
9527 "operator*() const -> reference {}\n"
9528 "constexpr auto\n"
9529 "operator->() const -> reference {}\n"
9530 "constexpr auto\n"
9531 "operator++() const -> reference {}\n"
9532 "constexpr auto\n"
9533 "operator void *() const -> reference {}\n"
9534 "constexpr auto\n"
9535 "operator void **() const -> reference {}\n"
9536 "constexpr auto\n"
9537 "operator void *() const -> reference {}\n"
9538 "constexpr auto\n"
9539 "operator void &() const -> reference {}\n"
9540 "constexpr auto\n"
9541 "operator void &&() const -> reference {}\n"
9542 "constexpr auto\n"
9543 "operator char *() const -> reference {}\n"
9544 "constexpr auto\n"
9545 "operator!() const -> reference {}\n"
9546 "constexpr auto\n"
9547 "operator[]() const -> reference {}\n",
9548 Style);
9549 verifyFormat("void *operator new(std::size_t s);", // No break here.
9550 Style);
9551 verifyFormat("void *\n"
9552 "operator new(std::size_t s) {}",
9553 Style);
9554 verifyFormat("void *\n"
9555 "operator delete[](void *ptr) {}",
9556 Style);
9557 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9558 verifyFormat("const char *\n"
9559 "f(void)\n" // Break here.
9560 "{\n"
9561 " return \"\";\n"
9562 "}\n"
9563 "const char *bar(void);\n", // No break here.
9564 Style);
9565 verifyFormat("template <class T>\n"
9566 "T *\n" // Problem here: no line break
9567 "f(T &c)\n" // Break here.
9568 "{\n"
9569 " return NULL;\n"
9570 "}\n"
9571 "template <class T> T *f(T &c);\n", // No break here.
9572 Style);
9573 verifyFormat("int\n"
9574 "foo(A<bool> a)\n"
9575 "{\n"
9576 " return a;\n"
9577 "}\n",
9578 Style);
9579 verifyFormat("int\n"
9580 "foo(A<8> a)\n"
9581 "{\n"
9582 " return a;\n"
9583 "}\n",
9584 Style);
9585 verifyFormat("int\n"
9586 "foo(A<B<bool>, 8> a)\n"
9587 "{\n"
9588 " return a;\n"
9589 "}\n",
9590 Style);
9591 verifyFormat("int\n"
9592 "foo(A<B<8>, bool> a)\n"
9593 "{\n"
9594 " return a;\n"
9595 "}\n",
9596 Style);
9597 verifyFormat("int\n"
9598 "foo(A<B<bool>, bool> a)\n"
9599 "{\n"
9600 " return a;\n"
9601 "}\n",
9602 Style);
9603 verifyFormat("int\n"
9604 "foo(A<B<8>, 8> a)\n"
9605 "{\n"
9606 " return a;\n"
9607 "}\n",
9608 Style);
9609
9610 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
9611 Style.BraceWrapping.AfterFunction = true;
9612 verifyFormat("int f(i);\n" // No break here.
9613 "int\n" // Break here.
9614 "f(i)\n"
9615 "{\n"
9616 " return i + 1;\n"
9617 "}\n"
9618 "int\n" // Break here.
9619 "f(i)\n"
9620 "{\n"
9621 " return i + 1;\n"
9622 "};",
9623 Style);
9624 verifyFormat("int f(a, b, c);\n" // No break here.
9625 "int\n" // Break here.
9626 "f(a, b, c)\n" // Break here.
9627 "short a, b;\n"
9628 "float c;\n"
9629 "{\n"
9630 " return a + b < c;\n"
9631 "}\n"
9632 "int\n" // Break here.
9633 "f(a, b, c)\n" // Break here.
9634 "short a, b;\n"
9635 "float c;\n"
9636 "{\n"
9637 " return a + b < c;\n"
9638 "};",
9639 Style);
9640 verifyFormat("byte *\n" // Break here.
9641 "f(a)\n" // Break here.
9642 "byte a[];\n"
9643 "{\n"
9644 " return a;\n"
9645 "}",
9646 Style);
9647 verifyFormat("byte *\n"
9648 "f(a)\n"
9649 "byte /* K&R C */ a[];\n"
9650 "{\n"
9651 " return a;\n"
9652 "}\n"
9653 "byte *\n"
9654 "g(p)\n"
9655 "byte /* K&R C */ *p;\n"
9656 "{\n"
9657 " return p;\n"
9658 "}",
9659 Style);
9660 verifyFormat("bool f(int a, int) override;\n"
9661 "Bar g(int a, Bar) final;\n"
9662 "Bar h(a, Bar) final;",
9663 Style);
9664 verifyFormat("int\n"
9665 "f(a)",
9666 Style);
9667 verifyFormat("bool\n"
9668 "f(size_t = 0, bool b = false)\n"
9669 "{\n"
9670 " return !b;\n"
9671 "}",
9672 Style);
9673
9674 // The return breaking style doesn't affect:
9675 // * function and object definitions with attribute-like macros
9676 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
9677 " ABSL_GUARDED_BY(mutex) = {};",
9678 getGoogleStyleWithColumns(40));
9679 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
9680 " ABSL_GUARDED_BY(mutex); // comment",
9681 getGoogleStyleWithColumns(40));
9682 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
9683 " ABSL_GUARDED_BY(mutex1)\n"
9684 " ABSL_GUARDED_BY(mutex2);",
9685 getGoogleStyleWithColumns(40));
9686 verifyFormat("Tttttt f(int a, int b)\n"
9687 " ABSL_GUARDED_BY(mutex1)\n"
9688 " ABSL_GUARDED_BY(mutex2);",
9689 getGoogleStyleWithColumns(40));
9690 // * typedefs
9691 verifyFormat("typedef ATTR(X) char x;", getGoogleStyle());
9692
9693 Style = getGNUStyle();
9694
9695 // Test for comments at the end of function declarations.
9696 verifyFormat("void\n"
9697 "foo (int a, /*abc*/ int b) // def\n"
9698 "{\n"
9699 "}\n",
9700 Style);
9701
9702 verifyFormat("void\n"
9703 "foo (int a, /* abc */ int b) /* def */\n"
9704 "{\n"
9705 "}\n",
9706 Style);
9707
9708 // Definitions that should not break after return type
9709 verifyFormat("void foo (int a, int b); // def\n", Style);
9710 verifyFormat("void foo (int a, int b); /* def */\n", Style);
9711 verifyFormat("void foo (int a, int b);\n", Style);
9712 }
9713
9714 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
9715 FormatStyle NoBreak = getLLVMStyle();
9716 NoBreak.AlwaysBreakBeforeMultilineStrings = false;
9717 FormatStyle Break = getLLVMStyle();
9718 Break.AlwaysBreakBeforeMultilineStrings = true;
9719 verifyFormat("aaaa = \"bbbb\"\n"
9720 " \"cccc\";",
9721 NoBreak);
9722 verifyFormat("aaaa =\n"
9723 " \"bbbb\"\n"
9724 " \"cccc\";",
9725 Break);
9726 verifyFormat("aaaa(\"bbbb\"\n"
9727 " \"cccc\");",
9728 NoBreak);
9729 verifyFormat("aaaa(\n"
9730 " \"bbbb\"\n"
9731 " \"cccc\");",
9732 Break);
9733 verifyFormat("aaaa(qqq, \"bbbb\"\n"
9734 " \"cccc\");",
9735 NoBreak);
9736 verifyFormat("aaaa(qqq,\n"
9737 " \"bbbb\"\n"
9738 " \"cccc\");",
9739 Break);
9740 verifyFormat("aaaa(qqq,\n"
9741 " L\"bbbb\"\n"
9742 " L\"cccc\");",
9743 Break);
9744 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
9745 " \"bbbb\"));",
9746 Break);
9747 verifyFormat("string s = someFunction(\n"
9748 " \"abc\"\n"
9749 " \"abc\");",
9750 Break);
9751
9752 // As we break before unary operators, breaking right after them is bad.
9753 verifyFormat("string foo = abc ? \"x\"\n"
9754 " \"blah blah blah blah blah blah\"\n"
9755 " : \"y\";",
9756 Break);
9757
9758 // Don't break if there is no column gain.
9759 verifyFormat("f(\"aaaa\"\n"
9760 " \"bbbb\");",
9761 Break);
9762
9763 // Treat literals with escaped newlines like multi-line string literals.
9764 EXPECT_EQ("x = \"a\\\n"
9765 "b\\\n"
9766 "c\";",
9767 format("x = \"a\\\n"
9768 "b\\\n"
9769 "c\";",
9770 NoBreak));
9771 EXPECT_EQ("xxxx =\n"
9772 " \"a\\\n"
9773 "b\\\n"
9774 "c\";",
9775 format("xxxx = \"a\\\n"
9776 "b\\\n"
9777 "c\";",
9778 Break));
9779
9780 EXPECT_EQ("NSString *const kString =\n"
9781 " @\"aaaa\"\n"
9782 " @\"bbbb\";",
9783 format("NSString *const kString = @\"aaaa\"\n"
9784 "@\"bbbb\";",
9785 Break));
9786
9787 Break.ColumnLimit = 0;
9788 verifyFormat("const char *hello = \"hello llvm\";", Break);
9789 }
9790
9791 TEST_F(FormatTest, AlignsPipes) {
9792 verifyFormat(
9793 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9794 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9795 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9796 verifyFormat(
9797 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
9798 " << aaaaaaaaaaaaaaaaaaaa;");
9799 verifyFormat(
9800 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9801 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9802 verifyFormat(
9803 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9804 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9805 verifyFormat(
9806 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
9807 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
9808 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
9809 verifyFormat(
9810 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9811 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9812 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9813 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9814 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9815 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9816 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
9817 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
9818 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
9819 verifyFormat(
9820 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9821 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9822 verifyFormat(
9823 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
9824 " aaaaaaaaaaaaaaaaaaaaaaaaaa);");
9825
9826 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
9827 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
9828 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9829 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9830 " aaaaaaaaaaaaaaaaaaaaa)\n"
9831 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
9832 verifyFormat("LOG_IF(aaa == //\n"
9833 " bbb)\n"
9834 " << a << b;");
9835
9836 // But sometimes, breaking before the first "<<" is desirable.
9837 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
9838 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
9839 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
9840 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9841 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9842 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
9843 " << BEF << IsTemplate << Description << E->getType();");
9844 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
9845 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9846 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9847 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
9848 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9849 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9850 " << aaa;");
9851
9852 verifyFormat(
9853 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9854 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9855
9856 // Incomplete string literal.
9857 EXPECT_EQ("llvm::errs() << \"\n"
9858 " << a;",
9859 format("llvm::errs() << \"\n<<a;"));
9860
9861 verifyFormat("void f() {\n"
9862 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
9863 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
9864 "}");
9865
9866 // Handle 'endl'.
9867 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
9868 " << bbbbbbbbbbbbbbbbbbbbbb << endl;");
9869 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
9870
9871 // Handle '\n'.
9872 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
9873 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
9874 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
9875 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
9876 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
9877 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
9878 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
9879 }
9880
9881 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
9882 verifyFormat("return out << \"somepacket = {\\n\"\n"
9883 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
9884 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
9885 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
9886 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
9887 " << \"}\";");
9888
9889 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
9890 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
9891 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
9892 verifyFormat(
9893 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
9894 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
9895 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
9896 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
9897 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
9898 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
9899 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
9900 verifyFormat(
9901 "void f() {\n"
9902 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
9903 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
9904 "}");
9905
9906 // Breaking before the first "<<" is generally not desirable.
9907 verifyFormat(
9908 "llvm::errs()\n"
9909 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9910 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9911 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9912 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9913 getLLVMStyleWithColumns(70));
9914 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
9915 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9916 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
9917 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9918 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
9919 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9920 getLLVMStyleWithColumns(70));
9921
9922 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
9923 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
9924 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
9925 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
9926 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
9927 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
9928 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
9929 " (aaaa + aaaa);",
9930 getLLVMStyleWithColumns(40));
9931 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
9932 " (aaaaaaa + aaaaa));",
9933 getLLVMStyleWithColumns(40));
9934 verifyFormat(
9935 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
9936 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
9937 " bbbbbbbbbbbbbbbbbbbbbbb);");
9938 }
9939
9940 TEST_F(FormatTest, UnderstandsEquals) {
9941 verifyFormat(
9942 "aaaaaaaaaaaaaaaaa =\n"
9943 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9944 verifyFormat(
9945 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9946 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
9947 verifyFormat(
9948 "if (a) {\n"
9949 " f();\n"
9950 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9951 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
9952 "}");
9953
9954 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9955 " 100000000 + 10000000) {\n}");
9956 }
9957
9958 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
9959 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
9960 " .looooooooooooooooooooooooooooooooooooooongFunction();");
9961
9962 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
9963 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
9964
9965 verifyFormat(
9966 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
9967 " Parameter2);");
9968
9969 verifyFormat(
9970 "ShortObject->shortFunction(\n"
9971 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
9972 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
9973
9974 verifyFormat("loooooooooooooongFunction(\n"
9975 " LoooooooooooooongObject->looooooooooooooooongFunction());");
9976
9977 verifyFormat(
9978 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
9979 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
9980
9981 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
9982 " .WillRepeatedly(Return(SomeValue));");
9983 verifyFormat("void f() {\n"
9984 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
9985 " .Times(2)\n"
9986 " .WillRepeatedly(Return(SomeValue));\n"
9987 "}");
9988 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
9989 " ccccccccccccccccccccccc);");
9990 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9991 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9992 " .aaaaa(aaaaa),\n"
9993 " aaaaaaaaaaaaaaaaaaaaa);");
9994 verifyFormat("void f() {\n"
9995 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9996 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
9997 "}");
9998 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9999 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10000 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10001 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10002 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
10003 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10004 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10005 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10006 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
10007 "}");
10008
10009 // Here, it is not necessary to wrap at "." or "->".
10010 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
10011 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
10012 verifyFormat(
10013 "aaaaaaaaaaa->aaaaaaaaa(\n"
10014 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10015 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
10016
10017 verifyFormat(
10018 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10019 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
10020 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
10021 " aaaaaaaaa()->aaaaaa()->aaaaa());");
10022 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
10023 " aaaaaaaaa()->aaaaaa()->aaaaa());");
10024
10025 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10026 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10027 " .a();");
10028
10029 FormatStyle NoBinPacking = getLLVMStyle();
10030 NoBinPacking.BinPackParameters = false;
10031 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
10032 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
10033 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
10034 " aaaaaaaaaaaaaaaaaaa,\n"
10035 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10036 NoBinPacking);
10037
10038 // If there is a subsequent call, change to hanging indentation.
10039 verifyFormat(
10040 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10041 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
10042 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10043 verifyFormat(
10044 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10045 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
10046 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10047 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10048 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10049 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10050 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10051 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
10052 }
10053
10054 TEST_F(FormatTest, WrapsTemplateDeclarations) {
10055 verifyFormat("template <typename T>\n"
10056 "virtual void loooooooooooongFunction(int Param1, int Param2);");
10057 verifyFormat("template <typename T>\n"
10058 "// T should be one of {A, B}.\n"
10059 "virtual void loooooooooooongFunction(int Param1, int Param2);");
10060 verifyFormat(
10061 "template <typename T>\n"
10062 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
10063 verifyFormat("template <typename T>\n"
10064 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
10065 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
10066 verifyFormat(
10067 "template <typename T>\n"
10068 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
10069 " int Paaaaaaaaaaaaaaaaaaaaram2);");
10070 verifyFormat(
10071 "template <typename T>\n"
10072 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
10073 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
10074 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10075 verifyFormat("template <typename T>\n"
10076 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10077 " int aaaaaaaaaaaaaaaaaaaaaa);");
10078 verifyFormat(
10079 "template <typename T1, typename T2 = char, typename T3 = char,\n"
10080 " typename T4 = char>\n"
10081 "void f();");
10082 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
10083 " template <typename> class cccccccccccccccccccccc,\n"
10084 " typename ddddddddddddd>\n"
10085 "class C {};");
10086 verifyFormat(
10087 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
10088 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10089
10090 verifyFormat("void f() {\n"
10091 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
10092 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
10093 "}");
10094
10095 verifyFormat("template <typename T> class C {};");
10096 verifyFormat("template <typename T> void f();");
10097 verifyFormat("template <typename T> void f() {}");
10098 verifyFormat(
10099 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
10100 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10101 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
10102 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
10103 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10104 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
10105 " bbbbbbbbbbbbbbbbbbbbbbbb);",
10106 getLLVMStyleWithColumns(72));
10107 EXPECT_EQ("static_cast<A< //\n"
10108 " B> *>(\n"
10109 "\n"
10110 ");",
10111 format("static_cast<A<//\n"
10112 " B>*>(\n"
10113 "\n"
10114 " );"));
10115 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10116 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
10117
10118 FormatStyle AlwaysBreak = getLLVMStyle();
10119 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
10120 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
10121 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
10122 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
10123 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10124 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
10125 " ccccccccccccccccccccccccccccccccccccccccccccccc);");
10126 verifyFormat("template <template <typename> class Fooooooo,\n"
10127 " template <typename> class Baaaaaaar>\n"
10128 "struct C {};",
10129 AlwaysBreak);
10130 verifyFormat("template <typename T> // T can be A, B or C.\n"
10131 "struct C {};",
10132 AlwaysBreak);
10133 verifyFormat("template <typename T>\n"
10134 "C(T) noexcept;",
10135 AlwaysBreak);
10136 verifyFormat("template <typename T>\n"
10137 "ClassName(T) noexcept;",
10138 AlwaysBreak);
10139 verifyFormat("template <typename T>\n"
10140 "POOR_NAME(T) noexcept;",
10141 AlwaysBreak);
10142 verifyFormat("template <enum E> class A {\n"
10143 "public:\n"
10144 " E *f();\n"
10145 "};");
10146
10147 FormatStyle NeverBreak = getLLVMStyle();
10148 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No;
10149 verifyFormat("template <typename T> class C {};", NeverBreak);
10150 verifyFormat("template <typename T> void f();", NeverBreak);
10151 verifyFormat("template <typename T> void f() {}", NeverBreak);
10152 verifyFormat("template <typename T> C(T) noexcept;", NeverBreak);
10153 verifyFormat("template <typename T> ClassName(T) noexcept;", NeverBreak);
10154 verifyFormat("template <typename T> POOR_NAME(T) noexcept;", NeverBreak);
10155 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
10156 "bbbbbbbbbbbbbbbbbbbb) {}",
10157 NeverBreak);
10158 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10159 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
10160 " ccccccccccccccccccccccccccccccccccccccccccccccc);",
10161 NeverBreak);
10162 verifyFormat("template <template <typename> class Fooooooo,\n"
10163 " template <typename> class Baaaaaaar>\n"
10164 "struct C {};",
10165 NeverBreak);
10166 verifyFormat("template <typename T> // T can be A, B or C.\n"
10167 "struct C {};",
10168 NeverBreak);
10169 verifyFormat("template <enum E> class A {\n"
10170 "public:\n"
10171 " E *f();\n"
10172 "};",
10173 NeverBreak);
10174 NeverBreak.PenaltyBreakTemplateDeclaration = 100;
10175 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
10176 "bbbbbbbbbbbbbbbbbbbb) {}",
10177 NeverBreak);
10178 }
10179
10180 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
10181 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
10182 Style.ColumnLimit = 60;
10183 EXPECT_EQ("// Baseline - no comments.\n"
10184 "template <\n"
10185 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
10186 "void f() {}",
10187 format("// Baseline - no comments.\n"
10188 "template <\n"
10189 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
10190 "void f() {}",
10191 Style));
10192
10193 EXPECT_EQ("template <\n"
10194 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
10195 "void f() {}",
10196 format("template <\n"
10197 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
10198 "void f() {}",
10199 Style));
10200
10201 EXPECT_EQ(
10202 "template <\n"
10203 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
10204 "void f() {}",
10205 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
10206 "void f() {}",
10207 Style));
10208
10209 EXPECT_EQ(
10210 "template <\n"
10211 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
10212 " // multiline\n"
10213 "void f() {}",
10214 format("template <\n"
10215 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
10216 " // multiline\n"
10217 "void f() {}",
10218 Style));
10219
10220 EXPECT_EQ(
10221 "template <typename aaaaaaaaaa<\n"
10222 " bbbbbbbbbbbb>::value> // trailing loooong\n"
10223 "void f() {}",
10224 format(
10225 "template <\n"
10226 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
10227 "void f() {}",
10228 Style));
10229 }
10230
10231 TEST_F(FormatTest, WrapsTemplateParameters) {
10232 FormatStyle Style = getLLVMStyle();
10233 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
10234 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
10235 verifyFormat(
10236 "template <typename... a> struct q {};\n"
10237 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
10238 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
10239 " y;",
10240 Style);
10241 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
10242 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
10243 verifyFormat(
10244 "template <typename... a> struct r {};\n"
10245 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
10246 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
10247 " y;",
10248 Style);
10249 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10250 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
10251 verifyFormat("template <typename... a> struct s {};\n"
10252 "extern s<\n"
10253 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
10254 "aaaaaaaaaaaaaaaaaaaaaa,\n"
10255 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
10256 "aaaaaaaaaaaaaaaaaaaaaa>\n"
10257 " y;",
10258 Style);
10259 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10260 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
10261 verifyFormat("template <typename... a> struct t {};\n"
10262 "extern t<\n"
10263 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
10264 "aaaaaaaaaaaaaaaaaaaaaa,\n"
10265 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
10266 "aaaaaaaaaaaaaaaaaaaaaa>\n"
10267 " y;",
10268 Style);
10269 }
10270
10271 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
10272 verifyFormat(
10273 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
10274 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10275 verifyFormat(
10276 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
10277 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10278 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
10279
10280 // FIXME: Should we have the extra indent after the second break?
10281 verifyFormat(
10282 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
10283 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
10284 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10285
10286 verifyFormat(
10287 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
10288 " cccccccccccccccccccccccccccccccccccccccccccccc());");
10289
10290 // Breaking at nested name specifiers is generally not desirable.
10291 verifyFormat(
10292 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10293 " aaaaaaaaaaaaaaaaaaaaaaa);");
10294
10295 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
10296 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
10297 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10298 " aaaaaaaaaaaaaaaaaaaaa);",
10299 getLLVMStyleWithColumns(74));
10300
10301 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
10302 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10303 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10304 }
10305
10306 TEST_F(FormatTest, UnderstandsTemplateParameters) {
10307 verifyFormat("A<int> a;");
10308 verifyFormat("A<A<A<int>>> a;");
10309 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
10310 verifyFormat("bool x = a < 1 || 2 > a;");
10311 verifyFormat("bool x = 5 < f<int>();");
10312 verifyFormat("bool x = f<int>() > 5;");
10313 verifyFormat("bool x = 5 < a<int>::x;");
10314 verifyFormat("bool x = a < 4 ? a > 2 : false;");
10315 verifyFormat("bool x = f() ? a < 2 : a > 2;");
10316
10317 verifyGoogleFormat("A<A<int>> a;");
10318 verifyGoogleFormat("A<A<A<int>>> a;");
10319 verifyGoogleFormat("A<A<A<A<int>>>> a;");
10320 verifyGoogleFormat("A<A<int> > a;");
10321 verifyGoogleFormat("A<A<A<int> > > a;");
10322 verifyGoogleFormat("A<A<A<A<int> > > > a;");
10323 verifyGoogleFormat("A<::A<int>> a;");
10324 verifyGoogleFormat("A<::A> a;");
10325 verifyGoogleFormat("A< ::A> a;");
10326 verifyGoogleFormat("A< ::A<int> > a;");
10327 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
10328 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
10329 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
10330 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
10331 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
10332 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
10333
10334 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
10335
10336 // template closer followed by a token that starts with > or =
10337 verifyFormat("bool b = a<1> > 1;");
10338 verifyFormat("bool b = a<1> >= 1;");
10339 verifyFormat("int i = a<1> >> 1;");
10340 FormatStyle Style = getLLVMStyle();
10341 Style.SpaceBeforeAssignmentOperators = false;
10342 verifyFormat("bool b= a<1> == 1;", Style);
10343 verifyFormat("a<int> = 1;", Style);
10344 verifyFormat("a<int> >>= 1;", Style);
10345
10346 verifyFormat("test < a | b >> c;");
10347 verifyFormat("test<test<a | b>> c;");
10348 verifyFormat("test >> a >> b;");
10349 verifyFormat("test << a >> b;");
10350
10351 verifyFormat("f<int>();");
10352 verifyFormat("template <typename T> void f() {}");
10353 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
10354 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
10355 "sizeof(char)>::type>;");
10356 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
10357 verifyFormat("f(a.operator()<A>());");
10358 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10359 " .template operator()<A>());",
10360 getLLVMStyleWithColumns(35));
10361 verifyFormat("bool_constant<a && noexcept(f())>");
10362 verifyFormat("bool_constant<a || noexcept(f())>");
10363
10364 verifyFormat("if (std::tuple_size_v<T> > 0)");
10365
10366 // Not template parameters.
10367 verifyFormat("return a < b && c > d;");
10368 verifyFormat("void f() {\n"
10369 " while (a < b && c > d) {\n"
10370 " }\n"
10371 "}");
10372 verifyFormat("template <typename... Types>\n"
10373 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
10374
10375 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10376 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
10377 getLLVMStyleWithColumns(60));
10378 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
10379 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
10380 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
10381 verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
10382
10383 verifyFormat("#define FOO(typeName, realClass) \\\n"
10384 " { #typeName, foo<FooType>(new foo<realClass>(#typeName)) }",
10385 getLLVMStyleWithColumns(60));
10386 }
10387
10388 TEST_F(FormatTest, UnderstandsShiftOperators) {
10389 verifyFormat("if (i < x >> 1)");
10390 verifyFormat("while (i < x >> 1)");
10391 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
10392 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
10393 verifyFormat(
10394 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
10395 verifyFormat("Foo.call<Bar<Function>>()");
10396 verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
10397 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
10398 "++i, v = v >> 1)");
10399 verifyFormat("if (w<u<v<x>>, 1>::t)");
10400 }
10401
10402 TEST_F(FormatTest, BitshiftOperatorWidth) {
10403 EXPECT_EQ("int a = 1 << 2; /* foo\n"
10404 " bar */",
10405 format("int a=1<<2; /* foo\n"
10406 " bar */"));
10407
10408 EXPECT_EQ("int b = 256 >> 1; /* foo\n"
10409 " bar */",
10410 format("int b =256>>1 ; /* foo\n"
10411 " bar */"));
10412 }
10413
10414 TEST_F(FormatTest, UnderstandsBinaryOperators) {
10415 verifyFormat("COMPARE(a, ==, b);");
10416 verifyFormat("auto s = sizeof...(Ts) - 1;");
10417 }
10418
10419 TEST_F(FormatTest, UnderstandsPointersToMembers) {
10420 verifyFormat("int A::*x;");
10421 verifyFormat("int (S::*func)(void *);");
10422 verifyFormat("void f() { int (S::*func)(void *); }");
10423 verifyFormat("typedef bool *(Class::*Member)() const;");
10424 verifyFormat("void f() {\n"
10425 " (a->*f)();\n"
10426 " a->*x;\n"
10427 " (a.*f)();\n"
10428 " ((*a).*f)();\n"
10429 " a.*x;\n"
10430 "}");
10431 verifyFormat("void f() {\n"
10432 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
10433 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
10434 "}");
10435 verifyFormat(
10436 "(aaaaaaaaaa->*bbbbbbb)(\n"
10437 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
10438 FormatStyle Style = getLLVMStyle();
10439 Style.PointerAlignment = FormatStyle::PAS_Left;
10440 verifyFormat("typedef bool* (Class::*Member)() const;", Style);
10441 }
10442
10443 TEST_F(FormatTest, UnderstandsUnaryOperators) {
10444 verifyFormat("int a = -2;");
10445 verifyFormat("f(-1, -2, -3);");
10446 verifyFormat("a[-1] = 5;");
10447 verifyFormat("int a = 5 + -2;");
10448 verifyFormat("if (i == -1) {\n}");
10449 verifyFormat("if (i != -1) {\n}");
10450 verifyFormat("if (i > -1) {\n}");
10451 verifyFormat("if (i < -1) {\n}");
10452 verifyFormat("++(a->f());");
10453 verifyFormat("--(a->f());");
10454 verifyFormat("(a->f())++;");
10455 verifyFormat("a[42]++;");
10456 verifyFormat("if (!(a->f())) {\n}");
10457 verifyFormat("if (!+i) {\n}");
10458 verifyFormat("~&a;");
10459 verifyFormat("for (x = 0; -10 < x; --x) {\n}");
10460 verifyFormat("sizeof -x");
10461 verifyFormat("sizeof +x");
10462 verifyFormat("sizeof *x");
10463 verifyFormat("sizeof &x");
10464 verifyFormat("delete +x;");
10465 verifyFormat("co_await +x;");
10466 verifyFormat("case *x:");
10467 verifyFormat("case &x:");
10468
10469 verifyFormat("a-- > b;");
10470 verifyFormat("b ? -a : c;");
10471 verifyFormat("n * sizeof char16;");
10472 verifyFormat("n * alignof char16;", getGoogleStyle());
10473 verifyFormat("sizeof(char);");
10474 verifyFormat("alignof(char);", getGoogleStyle());
10475
10476 verifyFormat("return -1;");
10477 verifyFormat("throw -1;");
10478 verifyFormat("switch (a) {\n"
10479 "case -1:\n"
10480 " break;\n"
10481 "}");
10482 verifyFormat("#define X -1");
10483 verifyFormat("#define X -kConstant");
10484
10485 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
10486 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
10487
10488 verifyFormat("int a = /* confusing comment */ -1;");
10489 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
10490 verifyFormat("int a = i /* confusing comment */++;");
10491
10492 verifyFormat("co_yield -1;");
10493 verifyFormat("co_return -1;");
10494
10495 // Check that * is not treated as a binary operator when we set
10496 // PointerAlignment as PAS_Left after a keyword and not a declaration.
10497 FormatStyle PASLeftStyle = getLLVMStyle();
10498 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
10499 verifyFormat("co_return *a;", PASLeftStyle);
10500 verifyFormat("co_await *a;", PASLeftStyle);
10501 verifyFormat("co_yield *a", PASLeftStyle);
10502 verifyFormat("return *a;", PASLeftStyle);
10503 }
10504
10505 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
10506 verifyFormat("if (!aaaaaaaaaa( // break\n"
10507 " aaaaa)) {\n"
10508 "}");
10509 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
10510 " aaaaa));");
10511 verifyFormat("*aaa = aaaaaaa( // break\n"
10512 " bbbbbb);");
10513 }
10514
10515 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
10516 verifyFormat("bool operator<();");
10517 verifyFormat("bool operator>();");
10518 verifyFormat("bool operator=();");
10519 verifyFormat("bool operator==();");
10520 verifyFormat("bool operator!=();");
10521 verifyFormat("int operator+();");
10522 verifyFormat("int operator++();");
10523 verifyFormat("int operator++(int) volatile noexcept;");
10524 verifyFormat("bool operator,();");
10525 verifyFormat("bool operator();");
10526 verifyFormat("bool operator()();");
10527 verifyFormat("bool operator[]();");
10528 verifyFormat("operator bool();");
10529 verifyFormat("operator int();");
10530 verifyFormat("operator void *();");
10531 verifyFormat("operator SomeType<int>();");
10532 verifyFormat("operator SomeType<int, int>();");
10533 verifyFormat("operator SomeType<SomeType<int>>();");
10534 verifyFormat("operator< <>();");
10535 verifyFormat("operator<< <>();");
10536 verifyFormat("< <>");
10537
10538 verifyFormat("void *operator new(std::size_t size);");
10539 verifyFormat("void *operator new[](std::size_t size);");
10540 verifyFormat("void operator delete(void *ptr);");
10541 verifyFormat("void operator delete[](void *ptr);");
10542 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
10543 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
10544 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
10545 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
10546
10547 verifyFormat(
10548 "ostream &operator<<(ostream &OutputStream,\n"
10549 " SomeReallyLongType WithSomeReallyLongValue);");
10550 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
10551 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
10552 " return left.group < right.group;\n"
10553 "}");
10554 verifyFormat("SomeType &operator=(const SomeType &S);");
10555 verifyFormat("f.template operator()<int>();");
10556
10557 verifyGoogleFormat("operator void*();");
10558 verifyGoogleFormat("operator SomeType<SomeType<int>>();");
10559 verifyGoogleFormat("operator ::A();");
10560
10561 verifyFormat("using A::operator+;");
10562 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
10563 "int i;");
10564
10565 // Calling an operator as a member function.
10566 verifyFormat("void f() { a.operator*(); }");
10567 verifyFormat("void f() { a.operator*(b & b); }");
10568 verifyFormat("void f() { a->operator&(a * b); }");
10569 verifyFormat("void f() { NS::a.operator+(*b * *b); }");
10570 // TODO: Calling an operator as a non-member function is hard to distinguish.
10571 // https://llvm.org/PR50629
10572 // verifyFormat("void f() { operator*(a & a); }");
10573 // verifyFormat("void f() { operator&(a, b * b); }");
10574
10575 verifyFormat("void f() { return operator()(x) * b; }");
10576 verifyFormat("void f() { return operator[](x) * b; }");
10577 verifyFormat("void f() { return operator\"\"_a(x) * b; }");
10578 verifyFormat("void f() { return operator\"\" _a(x) * b; }");
10579 verifyFormat("void f() { return operator\"\"s(x) * b; }");
10580 verifyFormat("void f() { return operator\"\" s(x) * b; }");
10581 verifyFormat("void f() { return operator\"\"if(x) * b; }");
10582
10583 verifyFormat("::operator delete(foo);");
10584 verifyFormat("::operator new(n * sizeof(foo));");
10585 verifyFormat("foo() { ::operator delete(foo); }");
10586 verifyFormat("foo() { ::operator new(n * sizeof(foo)); }");
10587 }
10588
10589 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
10590 verifyFormat("void A::b() && {}");
10591 verifyFormat("void A::b() && noexcept {}");
10592 verifyFormat("Deleted &operator=(const Deleted &) & = default;");
10593 verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
10594 verifyFormat("Deleted &operator=(const Deleted &) & noexcept = default;");
10595 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
10596 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
10597 verifyFormat("Deleted &operator=(const Deleted &) &;");
10598 verifyFormat("Deleted &operator=(const Deleted &) &&;");
10599 verifyFormat("SomeType MemberFunction(const Deleted &) &;");
10600 verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
10601 verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
10602 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
10603 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
10604 verifyFormat("SomeType MemberFunction(const Deleted &) && noexcept {}");
10605 verifyFormat("void Fn(T const &) const &;");
10606 verifyFormat("void Fn(T const volatile &&) const volatile &&;");
10607 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;");
10608 verifyFormat("template <typename T>\n"
10609 "void F(T) && = delete;",
10610 getGoogleStyle());
10611 verifyFormat("template <typename T> void operator=(T) &;");
10612 verifyFormat("template <typename T> void operator=(T) const &;");
10613 verifyFormat("template <typename T> void operator=(T) & noexcept;");
10614 verifyFormat("template <typename T> void operator=(T) & = default;");
10615 verifyFormat("template <typename T> void operator=(T) &&;");
10616 verifyFormat("template <typename T> void operator=(T) && = delete;");
10617 verifyFormat("template <typename T> void operator=(T) & {}");
10618 verifyFormat("template <typename T> void operator=(T) && {}");
10619
10620 FormatStyle AlignLeft = getLLVMStyle();
10621 AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
10622 verifyFormat("void A::b() && {}", AlignLeft);
10623 verifyFormat("void A::b() && noexcept {}", AlignLeft);
10624 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
10625 verifyFormat("Deleted& operator=(const Deleted&) & noexcept = default;",
10626 AlignLeft);
10627 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
10628 AlignLeft);
10629 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
10630 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
10631 verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
10632 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
10633 verifyFormat("auto Function(T) & -> void {}", AlignLeft);
10634 verifyFormat("auto Function(T) & -> void;", AlignLeft);
10635 verifyFormat("void Fn(T const&) const&;", AlignLeft);
10636 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
10637 verifyFormat("void Fn(T const volatile&&) const volatile&& noexcept;",
10638 AlignLeft);
10639 verifyFormat("template <typename T> void operator=(T) &;", AlignLeft);
10640 verifyFormat("template <typename T> void operator=(T) const&;", AlignLeft);
10641 verifyFormat("template <typename T> void operator=(T) & noexcept;",
10642 AlignLeft);
10643 verifyFormat("template <typename T> void operator=(T) & = default;",
10644 AlignLeft);
10645 verifyFormat("template <typename T> void operator=(T) &&;", AlignLeft);
10646 verifyFormat("template <typename T> void operator=(T) && = delete;",
10647 AlignLeft);
10648 verifyFormat("template <typename T> void operator=(T) & {}", AlignLeft);
10649 verifyFormat("template <typename T> void operator=(T) && {}", AlignLeft);
10650
10651 FormatStyle AlignMiddle = getLLVMStyle();
10652 AlignMiddle.PointerAlignment = FormatStyle::PAS_Middle;
10653 verifyFormat("void A::b() && {}", AlignMiddle);
10654 verifyFormat("void A::b() && noexcept {}", AlignMiddle);
10655 verifyFormat("Deleted & operator=(const Deleted &) & = default;",
10656 AlignMiddle);
10657 verifyFormat("Deleted & operator=(const Deleted &) & noexcept = default;",
10658 AlignMiddle);
10659 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;",
10660 AlignMiddle);
10661 verifyFormat("Deleted & operator=(const Deleted &) &;", AlignMiddle);
10662 verifyFormat("SomeType MemberFunction(const Deleted &) &;", AlignMiddle);
10663 verifyFormat("auto Function(T t) & -> void {}", AlignMiddle);
10664 verifyFormat("auto Function(T... t) & -> void {}", AlignMiddle);
10665 verifyFormat("auto Function(T) & -> void {}", AlignMiddle);
10666 verifyFormat("auto Function(T) & -> void;", AlignMiddle);
10667 verifyFormat("void Fn(T const &) const &;", AlignMiddle);
10668 verifyFormat("void Fn(T const volatile &&) const volatile &&;", AlignMiddle);
10669 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;",
10670 AlignMiddle);
10671 verifyFormat("template <typename T> void operator=(T) &;", AlignMiddle);
10672 verifyFormat("template <typename T> void operator=(T) const &;", AlignMiddle);
10673 verifyFormat("template <typename T> void operator=(T) & noexcept;",
10674 AlignMiddle);
10675 verifyFormat("template <typename T> void operator=(T) & = default;",
10676 AlignMiddle);
10677 verifyFormat("template <typename T> void operator=(T) &&;", AlignMiddle);
10678 verifyFormat("template <typename T> void operator=(T) && = delete;",
10679 AlignMiddle);
10680 verifyFormat("template <typename T> void operator=(T) & {}", AlignMiddle);
10681 verifyFormat("template <typename T> void operator=(T) && {}", AlignMiddle);
10682
10683 FormatStyle Spaces = getLLVMStyle();
10684 Spaces.SpacesInCStyleCastParentheses = true;
10685 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
10686 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
10687 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
10688 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
10689
10690 Spaces.SpacesInCStyleCastParentheses = false;
10691 Spaces.SpacesInParentheses = true;
10692 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
10693 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
10694 Spaces);
10695 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
10696 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
10697
10698 FormatStyle BreakTemplate = getLLVMStyle();
10699 BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
10700
10701 verifyFormat("struct f {\n"
10702 " template <class T>\n"
10703 " int &foo(const std::string &str) & noexcept {}\n"
10704 "};",
10705 BreakTemplate);
10706
10707 verifyFormat("struct f {\n"
10708 " template <class T>\n"
10709 " int &foo(const std::string &str) && noexcept {}\n"
10710 "};",
10711 BreakTemplate);
10712
10713 verifyFormat("struct f {\n"
10714 " template <class T>\n"
10715 " int &foo(const std::string &str) const & noexcept {}\n"
10716 "};",
10717 BreakTemplate);
10718
10719 verifyFormat("struct f {\n"
10720 " template <class T>\n"
10721 " int &foo(const std::string &str) const & noexcept {}\n"
10722 "};",
10723 BreakTemplate);
10724
10725 verifyFormat("struct f {\n"
10726 " template <class T>\n"
10727 " auto foo(const std::string &str) && noexcept -> int & {}\n"
10728 "};",
10729 BreakTemplate);
10730
10731 FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
10732 AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations =
10733 FormatStyle::BTDS_Yes;
10734 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
10735
10736 verifyFormat("struct f {\n"
10737 " template <class T>\n"
10738 " int& foo(const std::string& str) & noexcept {}\n"
10739 "};",
10740 AlignLeftBreakTemplate);
10741
10742 verifyFormat("struct f {\n"
10743 " template <class T>\n"
10744 " int& foo(const std::string& str) && noexcept {}\n"
10745 "};",
10746 AlignLeftBreakTemplate);
10747
10748 verifyFormat("struct f {\n"
10749 " template <class T>\n"
10750 " int& foo(const std::string& str) const& noexcept {}\n"
10751 "};",
10752 AlignLeftBreakTemplate);
10753
10754 verifyFormat("struct f {\n"
10755 " template <class T>\n"
10756 " int& foo(const std::string& str) const&& noexcept {}\n"
10757 "};",
10758 AlignLeftBreakTemplate);
10759
10760 verifyFormat("struct f {\n"
10761 " template <class T>\n"
10762 " auto foo(const std::string& str) && noexcept -> int& {}\n"
10763 "};",
10764 AlignLeftBreakTemplate);
10765
10766 // The `&` in `Type&` should not be confused with a trailing `&` of
10767 // DEPRECATED(reason) member function.
10768 verifyFormat("struct f {\n"
10769 " template <class T>\n"
10770 " DEPRECATED(reason)\n"
10771 " Type &foo(arguments) {}\n"
10772 "};",
10773 BreakTemplate);
10774
10775 verifyFormat("struct f {\n"
10776 " template <class T>\n"
10777 " DEPRECATED(reason)\n"
10778 " Type& foo(arguments) {}\n"
10779 "};",
10780 AlignLeftBreakTemplate);
10781
10782 verifyFormat("void (*foopt)(int) = &func;");
10783
10784 FormatStyle DerivePointerAlignment = getLLVMStyle();
10785 DerivePointerAlignment.DerivePointerAlignment = true;
10786 // There's always a space between the function and its trailing qualifiers.
10787 // This isn't evidence for PAS_Right (or for PAS_Left).
10788 std::string Prefix = "void a() &;\n"
10789 "void b() &;\n";
10790 verifyFormat(Prefix + "int* x;", DerivePointerAlignment);
10791 verifyFormat(Prefix + "int *x;", DerivePointerAlignment);
10792 // Same if the function is an overloaded operator, and with &&.
10793 Prefix = "void operator()() &&;\n"
10794 "void operator()() &&;\n";
10795 verifyFormat(Prefix + "int* x;", DerivePointerAlignment);
10796 verifyFormat(Prefix + "int *x;", DerivePointerAlignment);
10797 // However a space between cv-qualifiers and ref-qualifiers *is* evidence.
10798 Prefix = "void a() const &;\n"
10799 "void b() const &;\n";
10800 EXPECT_EQ(Prefix + "int *x;",
10801 format(Prefix + "int* x;", DerivePointerAlignment));
10802 }
10803
10804 TEST_F(FormatTest, PointerAlignmentFallback) {
10805 FormatStyle Style = getLLVMStyle();
10806 Style.DerivePointerAlignment = true;
10807
10808 const StringRef Code("int* p;\n"
10809 "int *q;\n"
10810 "int * r;");
10811
10812 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
10813 verifyFormat("int *p;\n"
10814 "int *q;\n"
10815 "int *r;",
10816 Code, Style);
10817
10818 Style.PointerAlignment = FormatStyle::PAS_Left;
10819 verifyFormat("int* p;\n"
10820 "int* q;\n"
10821 "int* r;",
10822 Code, Style);
10823
10824 Style.PointerAlignment = FormatStyle::PAS_Middle;
10825 verifyFormat("int * p;\n"
10826 "int * q;\n"
10827 "int * r;",
10828 Code, Style);
10829 }
10830
10831 TEST_F(FormatTest, UnderstandsNewAndDelete) {
10832 verifyFormat("void f() {\n"
10833 " A *a = new A;\n"
10834 " A *a = new (placement) A;\n"
10835 " delete a;\n"
10836 " delete (A *)a;\n"
10837 "}");
10838 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
10839 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
10840 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10841 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
10842 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
10843 verifyFormat("delete[] h->p;");
10844 verifyFormat("delete[] (void *)p;");
10845
10846 verifyFormat("void operator delete(void *foo) ATTRIB;");
10847 verifyFormat("void operator new(void *foo) ATTRIB;");
10848 verifyFormat("void operator delete[](void *foo) ATTRIB;");
10849 verifyFormat("void operator delete(void *ptr) noexcept;");
10850
10851 EXPECT_EQ("void new(link p);\n"
10852 "void delete(link p);\n",
10853 format("void new (link p);\n"
10854 "void delete (link p);\n"));
10855 }
10856
10857 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
10858 verifyFormat("int *f(int *a) {}");
10859 verifyFormat("int main(int argc, char **argv) {}");
10860 verifyFormat("Test::Test(int b) : a(b * b) {}");
10861 verifyIndependentOfContext("f(a, *a);");
10862 verifyFormat("void g() { f(*a); }");
10863 verifyIndependentOfContext("int a = b * 10;");
10864 verifyIndependentOfContext("int a = 10 * b;");
10865 verifyIndependentOfContext("int a = b * c;");
10866 verifyIndependentOfContext("int a += b * c;");
10867 verifyIndependentOfContext("int a -= b * c;");
10868 verifyIndependentOfContext("int a *= b * c;");
10869 verifyIndependentOfContext("int a /= b * c;");
10870 verifyIndependentOfContext("int a = *b;");
10871 verifyIndependentOfContext("int a = *b * c;");
10872 verifyIndependentOfContext("int a = b * *c;");
10873 verifyIndependentOfContext("int a = b * (10);");
10874 verifyIndependentOfContext("S << b * (10);");
10875 verifyIndependentOfContext("return 10 * b;");
10876 verifyIndependentOfContext("return *b * *c;");
10877 verifyIndependentOfContext("return a & ~b;");
10878 verifyIndependentOfContext("f(b ? *c : *d);");
10879 verifyIndependentOfContext("int a = b ? *c : *d;");
10880 verifyIndependentOfContext("*b = a;");
10881 verifyIndependentOfContext("a * ~b;");
10882 verifyIndependentOfContext("a * !b;");
10883 verifyIndependentOfContext("a * +b;");
10884 verifyIndependentOfContext("a * -b;");
10885 verifyIndependentOfContext("a * ++b;");
10886 verifyIndependentOfContext("a * --b;");
10887 verifyIndependentOfContext("a[4] * b;");
10888 verifyIndependentOfContext("a[a * a] = 1;");
10889 verifyIndependentOfContext("f() * b;");
10890 verifyIndependentOfContext("a * [self dostuff];");
10891 verifyIndependentOfContext("int x = a * (a + b);");
10892 verifyIndependentOfContext("(a *)(a + b);");
10893 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
10894 verifyIndependentOfContext("int *pa = (int *)&a;");
10895 verifyIndependentOfContext("return sizeof(int **);");
10896 verifyIndependentOfContext("return sizeof(int ******);");
10897 verifyIndependentOfContext("return (int **&)a;");
10898 verifyIndependentOfContext("f((*PointerToArray)[10]);");
10899 verifyFormat("void f(Type (*parameter)[10]) {}");
10900 verifyFormat("void f(Type (&parameter)[10]) {}");
10901 verifyGoogleFormat("return sizeof(int**);");
10902 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
10903 verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
10904 verifyFormat("auto a = [](int **&, int ***) {};");
10905 verifyFormat("auto PointerBinding = [](const char *S) {};");
10906 verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
10907 verifyFormat("[](const decltype(*a) &value) {}");
10908 verifyFormat("[](const typeof(*a) &value) {}");
10909 verifyFormat("[](const _Atomic(a *) &value) {}");
10910 verifyFormat("[](const __underlying_type(a) &value) {}");
10911 verifyFormat("decltype(a * b) F();");
10912 verifyFormat("typeof(a * b) F();");
10913 verifyFormat("#define MACRO() [](A *a) { return 1; }");
10914 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
10915 verifyIndependentOfContext("typedef void (*f)(int *a);");
10916 verifyIndependentOfContext("typedef void (*f)(Type *a);");
10917 verifyIndependentOfContext("int i{a * b};");
10918 verifyIndependentOfContext("aaa && aaa->f();");
10919 verifyIndependentOfContext("int x = ~*p;");
10920 verifyFormat("Constructor() : a(a), area(width * height) {}");
10921 verifyFormat("Constructor() : a(a), area(a, width * height) {}");
10922 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
10923 verifyFormat("void f() { f(a, c * d); }");
10924 verifyFormat("void f() { f(new a(), c * d); }");
10925 verifyFormat("void f(const MyOverride &override);");
10926 verifyFormat("void f(const MyFinal &final);");
10927 verifyIndependentOfContext("bool a = f() && override.f();");
10928 verifyIndependentOfContext("bool a = f() && final.f();");
10929
10930 verifyIndependentOfContext("InvalidRegions[*R] = 0;");
10931
10932 verifyIndependentOfContext("A<int *> a;");
10933 verifyIndependentOfContext("A<int **> a;");
10934 verifyIndependentOfContext("A<int *, int *> a;");
10935 verifyIndependentOfContext("A<int *[]> a;");
10936 verifyIndependentOfContext(
10937 "const char *const p = reinterpret_cast<const char *const>(q);");
10938 verifyIndependentOfContext("A<int **, int **> a;");
10939 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
10940 verifyFormat("for (char **a = b; *a; ++a) {\n}");
10941 verifyFormat("for (; a && b;) {\n}");
10942 verifyFormat("bool foo = true && [] { return false; }();");
10943
10944 verifyFormat(
10945 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10946 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10947
10948 verifyGoogleFormat("int const* a = &b;");
10949 verifyGoogleFormat("**outparam = 1;");
10950 verifyGoogleFormat("*outparam = a * b;");
10951 verifyGoogleFormat("int main(int argc, char** argv) {}");
10952 verifyGoogleFormat("A<int*> a;");
10953 verifyGoogleFormat("A<int**> a;");
10954 verifyGoogleFormat("A<int*, int*> a;");
10955 verifyGoogleFormat("A<int**, int**> a;");
10956 verifyGoogleFormat("f(b ? *c : *d);");
10957 verifyGoogleFormat("int a = b ? *c : *d;");
10958 verifyGoogleFormat("Type* t = **x;");
10959 verifyGoogleFormat("Type* t = *++*x;");
10960 verifyGoogleFormat("*++*x;");
10961 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
10962 verifyGoogleFormat("Type* t = x++ * y;");
10963 verifyGoogleFormat(
10964 "const char* const p = reinterpret_cast<const char* const>(q);");
10965 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
10966 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
10967 verifyGoogleFormat("template <typename T>\n"
10968 "void f(int i = 0, SomeType** temps = NULL);");
10969
10970 FormatStyle Left = getLLVMStyle();
10971 Left.PointerAlignment = FormatStyle::PAS_Left;
10972 verifyFormat("x = *a(x) = *a(y);", Left);
10973 verifyFormat("for (;; *a = b) {\n}", Left);
10974 verifyFormat("return *this += 1;", Left);
10975 verifyFormat("throw *x;", Left);
10976 verifyFormat("delete *x;", Left);
10977 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
10978 verifyFormat("[](const decltype(*a)* ptr) {}", Left);
10979 verifyFormat("[](const typeof(*a)* ptr) {}", Left);
10980 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left);
10981 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left);
10982 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
10983 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left);
10984 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left);
10985 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left);
10986
10987 verifyIndependentOfContext("a = *(x + y);");
10988 verifyIndependentOfContext("a = &(x + y);");
10989 verifyIndependentOfContext("*(x + y).call();");
10990 verifyIndependentOfContext("&(x + y)->call();");
10991 verifyFormat("void f() { &(*I).first; }");
10992
10993 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
10994 verifyFormat("f(* /* confusing comment */ foo);");
10995 verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)");
10996 verifyFormat("void foo(int * // this is the first paramters\n"
10997 " ,\n"
10998 " int second);");
10999 verifyFormat("double term = a * // first\n"
11000 " b;");
11001 verifyFormat(
11002 "int *MyValues = {\n"
11003 " *A, // Operator detection might be confused by the '{'\n"
11004 " *BB // Operator detection might be confused by previous comment\n"
11005 "};");
11006
11007 verifyIndependentOfContext("if (int *a = &b)");
11008 verifyIndependentOfContext("if (int &a = *b)");
11009 verifyIndependentOfContext("if (a & b[i])");
11010 verifyIndependentOfContext("if constexpr (a & b[i])");
11011 verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
11012 verifyIndependentOfContext("if (a * (b * c))");
11013 verifyIndependentOfContext("if constexpr (a * (b * c))");
11014 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
11015 verifyIndependentOfContext("if (a::b::c::d & b[i])");
11016 verifyIndependentOfContext("if (*b[i])");
11017 verifyIndependentOfContext("if (int *a = (&b))");
11018 verifyIndependentOfContext("while (int *a = &b)");
11019 verifyIndependentOfContext("while (a * (b * c))");
11020 verifyIndependentOfContext("size = sizeof *a;");
11021 verifyIndependentOfContext("if (a && (b = c))");
11022 verifyFormat("void f() {\n"
11023 " for (const int &v : Values) {\n"
11024 " }\n"
11025 "}");
11026 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
11027 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
11028 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
11029
11030 verifyFormat("#define A (!a * b)");
11031 verifyFormat("#define MACRO \\\n"
11032 " int *i = a * b; \\\n"
11033 " void f(a *b);",
11034 getLLVMStyleWithColumns(19));
11035
11036 verifyIndependentOfContext("A = new SomeType *[Length];");
11037 verifyIndependentOfContext("A = new SomeType *[Length]();");
11038 verifyIndependentOfContext("T **t = new T *;");
11039 verifyIndependentOfContext("T **t = new T *();");
11040 verifyGoogleFormat("A = new SomeType*[Length]();");
11041 verifyGoogleFormat("A = new SomeType*[Length];");
11042 verifyGoogleFormat("T** t = new T*;");
11043 verifyGoogleFormat("T** t = new T*();");
11044
11045 verifyFormat("STATIC_ASSERT((a & b) == 0);");
11046 verifyFormat("STATIC_ASSERT(0 == (a & b));");
11047 verifyFormat("template <bool a, bool b> "
11048 "typename t::if<x && y>::type f() {}");
11049 verifyFormat("template <int *y> f() {}");
11050 verifyFormat("vector<int *> v;");
11051 verifyFormat("vector<int *const> v;");
11052 verifyFormat("vector<int *const **const *> v;");
11053 verifyFormat("vector<int *volatile> v;");
11054 verifyFormat("vector<a *_Nonnull> v;");
11055 verifyFormat("vector<a *_Nullable> v;");
11056 verifyFormat("vector<a *_Null_unspecified> v;");
11057 verifyFormat("vector<a *__ptr32> v;");
11058 verifyFormat("vector<a *__ptr64> v;");
11059 verifyFormat("vector<a *__capability> v;");
11060 FormatStyle TypeMacros = getLLVMStyle();
11061 TypeMacros.TypenameMacros = {"LIST"};
11062 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros);
11063 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros);
11064 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros);
11065 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros);
11066 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication
11067
11068 FormatStyle CustomQualifier = getLLVMStyle();
11069 // Add identifiers that should not be parsed as a qualifier by default.
11070 CustomQualifier.AttributeMacros.push_back("__my_qualifier");
11071 CustomQualifier.AttributeMacros.push_back("_My_qualifier");
11072 CustomQualifier.AttributeMacros.push_back("my_other_qualifier");
11073 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
11074 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier);
11075 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
11076 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier);
11077 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
11078 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier);
11079 verifyFormat("vector<a * _NotAQualifier> v;");
11080 verifyFormat("vector<a * __not_a_qualifier> v;");
11081 verifyFormat("vector<a * b> v;");
11082 verifyFormat("foo<b && false>();");
11083 verifyFormat("foo<b & 1>();");
11084 verifyFormat("foo<b & (1)>();");
11085 verifyFormat("foo<b & (~0)>();");
11086 verifyFormat("foo<b & (true)>();");
11087 verifyFormat("foo<b & ((1))>();");
11088 verifyFormat("foo<b & (/*comment*/ 1)>();");
11089 verifyFormat("decltype(*::std::declval<const T &>()) void F();");
11090 verifyFormat("typeof(*::std::declval<const T &>()) void F();");
11091 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
11092 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
11093 verifyFormat(
11094 "template <class T, class = typename std::enable_if<\n"
11095 " std::is_integral<T>::value &&\n"
11096 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
11097 "void F();",
11098 getLLVMStyleWithColumns(70));
11099 verifyFormat("template <class T,\n"
11100 " class = typename std::enable_if<\n"
11101 " std::is_integral<T>::value &&\n"
11102 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
11103 " class U>\n"
11104 "void F();",
11105 getLLVMStyleWithColumns(70));
11106 verifyFormat(
11107 "template <class T,\n"
11108 " class = typename ::std::enable_if<\n"
11109 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
11110 "void F();",
11111 getGoogleStyleWithColumns(68));
11112
11113 FormatStyle Style = getLLVMStyle();
11114 Style.PointerAlignment = FormatStyle::PAS_Left;
11115 verifyFormat("struct {\n"
11116 "}* ptr;",
11117 Style);
11118 verifyFormat("union {\n"
11119 "}* ptr;",
11120 Style);
11121 verifyFormat("class {\n"
11122 "}* ptr;",
11123 Style);
11124 // Don't confuse a multiplication after a brace-initialized expression with
11125 // a class pointer.
11126 verifyFormat("int i = int{42} * 34;", Style);
11127 verifyFormat("struct {\n"
11128 "}&& ptr = {};",
11129 Style);
11130 verifyFormat("union {\n"
11131 "}&& ptr = {};",
11132 Style);
11133 verifyFormat("class {\n"
11134 "}&& ptr = {};",
11135 Style);
11136 verifyFormat("bool b = 3 == int{3} && true;");
11137
11138 Style.PointerAlignment = FormatStyle::PAS_Middle;
11139 verifyFormat("struct {\n"
11140 "} * ptr;",
11141 Style);
11142 verifyFormat("union {\n"
11143 "} * ptr;",
11144 Style);
11145 verifyFormat("class {\n"
11146 "} * ptr;",
11147 Style);
11148 verifyFormat("struct {\n"
11149 "} && ptr = {};",
11150 Style);
11151 verifyFormat("union {\n"
11152 "} && ptr = {};",
11153 Style);
11154 verifyFormat("class {\n"
11155 "} && ptr = {};",
11156 Style);
11157
11158 Style.PointerAlignment = FormatStyle::PAS_Right;
11159 verifyFormat("struct {\n"
11160 "} *ptr;",
11161 Style);
11162 verifyFormat("union {\n"
11163 "} *ptr;",
11164 Style);
11165 verifyFormat("class {\n"
11166 "} *ptr;",
11167 Style);
11168 verifyFormat("struct {\n"
11169 "} &&ptr = {};",
11170 Style);
11171 verifyFormat("union {\n"
11172 "} &&ptr = {};",
11173 Style);
11174 verifyFormat("class {\n"
11175 "} &&ptr = {};",
11176 Style);
11177
11178 Style.PointerAlignment = FormatStyle::PAS_Left;
11179 verifyFormat("delete[] *ptr;", Style);
11180 verifyFormat("delete[] **ptr;", Style);
11181 verifyFormat("delete[] *(ptr);", Style);
11182
11183 verifyIndependentOfContext("MACRO(int *i);");
11184 verifyIndependentOfContext("MACRO(auto *a);");
11185 verifyIndependentOfContext("MACRO(const A *a);");
11186 verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
11187 verifyIndependentOfContext("MACRO(decltype(A) *a);");
11188 verifyIndependentOfContext("MACRO(typeof(A) *a);");
11189 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
11190 verifyIndependentOfContext("MACRO(A *const a);");
11191 verifyIndependentOfContext("MACRO(A *restrict a);");
11192 verifyIndependentOfContext("MACRO(A *__restrict__ a);");
11193 verifyIndependentOfContext("MACRO(A *__restrict a);");
11194 verifyIndependentOfContext("MACRO(A *volatile a);");
11195 verifyIndependentOfContext("MACRO(A *__volatile a);");
11196 verifyIndependentOfContext("MACRO(A *__volatile__ a);");
11197 verifyIndependentOfContext("MACRO(A *_Nonnull a);");
11198 verifyIndependentOfContext("MACRO(A *_Nullable a);");
11199 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
11200 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
11201 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
11202 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
11203 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
11204 verifyIndependentOfContext("MACRO(A *__ptr32 a);");
11205 verifyIndependentOfContext("MACRO(A *__ptr64 a);");
11206 verifyIndependentOfContext("MACRO(A *__capability);");
11207 verifyIndependentOfContext("MACRO(A &__capability);");
11208 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration
11209 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
11210 // If we add __my_qualifier to AttributeMacros it should always be parsed as
11211 // a type declaration:
11212 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier);
11213 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier);
11214 // Also check that TypenameMacros prevents parsing it as multiplication:
11215 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
11216 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type
11217
11218 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
11219 verifyFormat("void f() { f(float{1}, a * a); }");
11220 verifyFormat("void f() { f(float(1), a * a); }");
11221
11222 verifyFormat("f((void (*)(int))g);");
11223 verifyFormat("f((void (&)(int))g);");
11224 verifyFormat("f((void (^)(int))g);");
11225
11226 // FIXME: Is there a way to make this work?
11227 // verifyIndependentOfContext("MACRO(A *a);");
11228 verifyFormat("MACRO(A &B);");
11229 verifyFormat("MACRO(A *B);");
11230 verifyFormat("void f() { MACRO(A * B); }");
11231 verifyFormat("void f() { MACRO(A & B); }");
11232
11233 // This lambda was mis-formatted after D88956 (treating it as a binop):
11234 verifyFormat("auto x = [](const decltype(x) &ptr) {};");
11235 verifyFormat("auto x = [](const decltype(x) *ptr) {};");
11236 verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
11237 verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
11238
11239 verifyFormat("DatumHandle const *operator->() const { return input_; }");
11240 verifyFormat("return options != nullptr && operator==(*options);");
11241
11242 EXPECT_EQ("#define OP(x) \\\n"
11243 " ostream &operator<<(ostream &s, const A &a) { \\\n"
11244 " return s << a.DebugString(); \\\n"
11245 " }",
11246 format("#define OP(x) \\\n"
11247 " ostream &operator<<(ostream &s, const A &a) { \\\n"
11248 " return s << a.DebugString(); \\\n"
11249 " }",
11250 getLLVMStyleWithColumns(50)));
11251
11252 // FIXME: We cannot handle this case yet; we might be able to figure out that
11253 // foo<x> d > v; doesn't make sense.
11254 verifyFormat("foo<a<b && c> d> v;");
11255
11256 FormatStyle PointerMiddle = getLLVMStyle();
11257 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
11258 verifyFormat("delete *x;", PointerMiddle);
11259 verifyFormat("int * x;", PointerMiddle);
11260 verifyFormat("int *[] x;", PointerMiddle);
11261 verifyFormat("template <int * y> f() {}", PointerMiddle);
11262 verifyFormat("int * f(int * a) {}", PointerMiddle);
11263 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
11264 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
11265 verifyFormat("A<int *> a;", PointerMiddle);
11266 verifyFormat("A<int **> a;", PointerMiddle);
11267 verifyFormat("A<int *, int *> a;", PointerMiddle);
11268 verifyFormat("A<int *[]> a;", PointerMiddle);
11269 verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
11270 verifyFormat("A = new SomeType *[Length];", PointerMiddle);
11271 verifyFormat("T ** t = new T *;", PointerMiddle);
11272
11273 // Member function reference qualifiers aren't binary operators.
11274 verifyFormat("string // break\n"
11275 "operator()() & {}");
11276 verifyFormat("string // break\n"
11277 "operator()() && {}");
11278 verifyGoogleFormat("template <typename T>\n"
11279 "auto x() & -> int {}");
11280
11281 // Should be binary operators when used as an argument expression (overloaded
11282 // operator invoked as a member function).
11283 verifyFormat("void f() { a.operator()(a * a); }");
11284 verifyFormat("void f() { a->operator()(a & a); }");
11285 verifyFormat("void f() { a.operator()(*a & *a); }");
11286 verifyFormat("void f() { a->operator()(*a * *a); }");
11287
11288 verifyFormat("int operator()(T (&&)[N]) { return 1; }");
11289 verifyFormat("int operator()(T (&)[N]) { return 0; }");
11290 }
11291
11292 TEST_F(FormatTest, UnderstandsAttributes) {
11293 verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
11294 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
11295 "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
11296 verifyFormat("__attribute__((nodebug)) ::qualified_type f();");
11297 FormatStyle AfterType = getLLVMStyle();
11298 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
11299 verifyFormat("__attribute__((nodebug)) void\n"
11300 "foo() {}\n",
11301 AfterType);
11302 verifyFormat("__unused void\n"
11303 "foo() {}",
11304 AfterType);
11305
11306 FormatStyle CustomAttrs = getLLVMStyle();
11307 CustomAttrs.AttributeMacros.push_back("__unused");
11308 CustomAttrs.AttributeMacros.push_back("__attr1");
11309 CustomAttrs.AttributeMacros.push_back("__attr2");
11310 CustomAttrs.AttributeMacros.push_back("no_underscore_attr");
11311 verifyFormat("vector<SomeType *__attribute((foo))> v;");
11312 verifyFormat("vector<SomeType *__attribute__((foo))> v;");
11313 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
11314 // Check that it is parsed as a multiplication without AttributeMacros and
11315 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
11316 verifyFormat("vector<SomeType * __attr1> v;");
11317 verifyFormat("vector<SomeType __attr1 *> v;");
11318 verifyFormat("vector<SomeType __attr1 *const> v;");
11319 verifyFormat("vector<SomeType __attr1 * __attr2> v;");
11320 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs);
11321 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs);
11322 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs);
11323 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs);
11324 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs);
11325 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs);
11326 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs);
11327
11328 // Check that these are not parsed as function declarations:
11329 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11330 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;
11331 verifyFormat("SomeType s(InitValue);", CustomAttrs);
11332 verifyFormat("SomeType s{InitValue};", CustomAttrs);
11333 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs);
11334 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs);
11335 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs);
11336 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
11337 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
11338 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
11339 }
11340
11341 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
11342 // Check that qualifiers on pointers don't break parsing of casts.
11343 verifyFormat("x = (foo *const)*v;");
11344 verifyFormat("x = (foo *volatile)*v;");
11345 verifyFormat("x = (foo *restrict)*v;");
11346 verifyFormat("x = (foo *__attribute__((foo)))*v;");
11347 verifyFormat("x = (foo *_Nonnull)*v;");
11348 verifyFormat("x = (foo *_Nullable)*v;");
11349 verifyFormat("x = (foo *_Null_unspecified)*v;");
11350 verifyFormat("x = (foo *_Nonnull)*v;");
11351 verifyFormat("x = (foo *[[clang::attr]])*v;");
11352 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
11353 verifyFormat("x = (foo *__ptr32)*v;");
11354 verifyFormat("x = (foo *__ptr64)*v;");
11355 verifyFormat("x = (foo *__capability)*v;");
11356
11357 // Check that we handle multiple trailing qualifiers and skip them all to
11358 // determine that the expression is a cast to a pointer type.
11359 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999);
11360 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999);
11361 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left;
11362 StringRef AllQualifiers =
11363 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
11364 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability";
11365 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight);
11366 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft);
11367
11368 // Also check that address-of is not parsed as a binary bitwise-and:
11369 verifyFormat("x = (foo *const)&v;");
11370 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight);
11371 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft);
11372
11373 // Check custom qualifiers:
11374 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999);
11375 CustomQualifier.AttributeMacros.push_back("__my_qualifier");
11376 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
11377 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier);
11378 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(),
11379 CustomQualifier);
11380 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(),
11381 CustomQualifier);
11382
11383 // Check that unknown identifiers result in binary operator parsing:
11384 verifyFormat("x = (foo * __unknown_qualifier) * v;");
11385 verifyFormat("x = (foo * __unknown_qualifier) & v;");
11386 }
11387
11388 TEST_F(FormatTest, UnderstandsSquareAttributes) {
11389 verifyFormat("SomeType s [[unused]] (InitValue);");
11390 verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
11391 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
11392 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
11393 verifyFormat("void f() [[deprecated(\"so sorry\")]];");
11394 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11395 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
11396 verifyFormat("[[nodiscard]] bool f() { return false; }");
11397 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}");
11398 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}");
11399 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}");
11400 verifyFormat("[[nodiscard]] ::qualified_type f();");
11401
11402 // Make sure we do not mistake attributes for array subscripts.
11403 verifyFormat("int a() {}\n"
11404 "[[unused]] int b() {}\n");
11405 verifyFormat("NSArray *arr;\n"
11406 "arr[[Foo() bar]];");
11407
11408 // On the other hand, we still need to correctly find array subscripts.
11409 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
11410
11411 // Make sure that we do not mistake Objective-C method inside array literals
11412 // as attributes, even if those method names are also keywords.
11413 verifyFormat("@[ [foo bar] ];");
11414 verifyFormat("@[ [NSArray class] ];");
11415 verifyFormat("@[ [foo enum] ];");
11416
11417 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
11418
11419 // Make sure we do not parse attributes as lambda introducers.
11420 FormatStyle MultiLineFunctions = getLLVMStyle();
11421 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11422 verifyFormat("[[unused]] int b() {\n"
11423 " return 42;\n"
11424 "}\n",
11425 MultiLineFunctions);
11426 }
11427
11428 TEST_F(FormatTest, AttributeClass) {
11429 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
11430 verifyFormat("class S {\n"
11431 " S(S&&) = default;\n"
11432 "};",
11433 Style);
11434 verifyFormat("class [[nodiscard]] S {\n"
11435 " S(S&&) = default;\n"
11436 "};",
11437 Style);
11438 verifyFormat("class __attribute((maybeunused)) S {\n"
11439 " S(S&&) = default;\n"
11440 "};",
11441 Style);
11442 verifyFormat("struct S {\n"
11443 " S(S&&) = default;\n"
11444 "};",
11445 Style);
11446 verifyFormat("struct [[nodiscard]] S {\n"
11447 " S(S&&) = default;\n"
11448 "};",
11449 Style);
11450 }
11451
11452 TEST_F(FormatTest, AttributesAfterMacro) {
11453 FormatStyle Style = getLLVMStyle();
11454 verifyFormat("MACRO;\n"
11455 "__attribute__((maybe_unused)) int foo() {\n"
11456 " //...\n"
11457 "}");
11458
11459 verifyFormat("MACRO;\n"
11460 "[[nodiscard]] int foo() {\n"
11461 " //...\n"
11462 "}");
11463
11464 EXPECT_EQ("MACRO\n\n"
11465 "__attribute__((maybe_unused)) int foo() {\n"
11466 " //...\n"
11467 "}",
11468 format("MACRO\n\n"
11469 "__attribute__((maybe_unused)) int foo() {\n"
11470 " //...\n"
11471 "}"));
11472
11473 EXPECT_EQ("MACRO\n\n"
11474 "[[nodiscard]] int foo() {\n"
11475 " //...\n"
11476 "}",
11477 format("MACRO\n\n"
11478 "[[nodiscard]] int foo() {\n"
11479 " //...\n"
11480 "}"));
11481 }
11482
11483 TEST_F(FormatTest, AttributePenaltyBreaking) {
11484 FormatStyle Style = getLLVMStyle();
11485 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
11486 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
11487 Style);
11488 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
11489 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
11490 Style);
11491 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
11492 "shared_ptr<ALongTypeName> &C d) {\n}",
11493 Style);
11494 }
11495
11496 TEST_F(FormatTest, UnderstandsEllipsis) {
11497 FormatStyle Style = getLLVMStyle();
11498 verifyFormat("int printf(const char *fmt, ...);");
11499 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
11500 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
11501
11502 verifyFormat("template <int *...PP> a;", Style);
11503
11504 Style.PointerAlignment = FormatStyle::PAS_Left;
11505 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style);
11506
11507 verifyFormat("template <int*... PP> a;", Style);
11508
11509 Style.PointerAlignment = FormatStyle::PAS_Middle;
11510 verifyFormat("template <int *... PP> a;", Style);
11511 }
11512
11513 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
11514 EXPECT_EQ("int *a;\n"
11515 "int *a;\n"
11516 "int *a;",
11517 format("int *a;\n"
11518 "int* a;\n"
11519 "int *a;",
11520 getGoogleStyle()));
11521 EXPECT_EQ("int* a;\n"
11522 "int* a;\n"
11523 "int* a;",
11524 format("int* a;\n"
11525 "int* a;\n"
11526 "int *a;",
11527 getGoogleStyle()));
11528 EXPECT_EQ("int *a;\n"
11529 "int *a;\n"
11530 "int *a;",
11531 format("int *a;\n"
11532 "int * a;\n"
11533 "int * a;",
11534 getGoogleStyle()));
11535 EXPECT_EQ("auto x = [] {\n"
11536 " int *a;\n"
11537 " int *a;\n"
11538 " int *a;\n"
11539 "};",
11540 format("auto x=[]{int *a;\n"
11541 "int * a;\n"
11542 "int * a;};",
11543 getGoogleStyle()));
11544 }
11545
11546 TEST_F(FormatTest, UnderstandsRvalueReferences) {
11547 verifyFormat("int f(int &&a) {}");
11548 verifyFormat("int f(int a, char &&b) {}");
11549 verifyFormat("void f() { int &&a = b; }");
11550 verifyGoogleFormat("int f(int a, char&& b) {}");
11551 verifyGoogleFormat("void f() { int&& a = b; }");
11552
11553 verifyIndependentOfContext("A<int &&> a;");
11554 verifyIndependentOfContext("A<int &&, int &&> a;");
11555 verifyGoogleFormat("A<int&&> a;");
11556 verifyGoogleFormat("A<int&&, int&&> a;");
11557
11558 // Not rvalue references:
11559 verifyFormat("template <bool B, bool C> class A {\n"
11560 " static_assert(B && C, \"Something is wrong\");\n"
11561 "};");
11562 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
11563 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
11564 verifyFormat("#define A(a, b) (a && b)");
11565 }
11566
11567 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
11568 verifyFormat("void f() {\n"
11569 " x[aaaaaaaaa -\n"
11570 " b] = 23;\n"
11571 "}",
11572 getLLVMStyleWithColumns(15));
11573 }
11574
11575 TEST_F(FormatTest, FormatsCasts) {
11576 verifyFormat("Type *A = static_cast<Type *>(P);");
11577 verifyFormat("static_cast<Type *>(P);");
11578 verifyFormat("static_cast<Type &>(Fun)(Args);");
11579 verifyFormat("static_cast<Type &>(*Fun)(Args);");
11580 verifyFormat("if (static_cast<int>(A) + B >= 0)\n ;");
11581 // Check that static_cast<...>(...) does not require the next token to be on
11582 // the same line.
11583 verifyFormat("some_loooong_output << something_something__ << "
11584 "static_cast<const void *>(R)\n"
11585 " << something;");
11586 verifyFormat("a = static_cast<Type &>(*Fun)(Args);");
11587 verifyFormat("const_cast<Type &>(*Fun)(Args);");
11588 verifyFormat("dynamic_cast<Type &>(*Fun)(Args);");
11589 verifyFormat("reinterpret_cast<Type &>(*Fun)(Args);");
11590 verifyFormat("Type *A = (Type *)P;");
11591 verifyFormat("Type *A = (vector<Type *, int *>)P;");
11592 verifyFormat("int a = (int)(2.0f);");
11593 verifyFormat("int a = (int)2.0f;");
11594 verifyFormat("x[(int32)y];");
11595 verifyFormat("x = (int32)y;");
11596 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
11597 verifyFormat("int a = (int)*b;");
11598 verifyFormat("int a = (int)2.0f;");
11599 verifyFormat("int a = (int)~0;");
11600 verifyFormat("int a = (int)++a;");
11601 verifyFormat("int a = (int)sizeof(int);");
11602 verifyFormat("int a = (int)+2;");
11603 verifyFormat("my_int a = (my_int)2.0f;");
11604 verifyFormat("my_int a = (my_int)sizeof(int);");
11605 verifyFormat("return (my_int)aaa;");
11606 verifyFormat("throw (my_int)aaa;");
11607 verifyFormat("#define x ((int)-1)");
11608 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
11609 verifyFormat("#define p(q) ((int *)&q)");
11610 verifyFormat("fn(a)(b) + 1;");
11611
11612 verifyFormat("void f() { my_int a = (my_int)*b; }");
11613 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
11614 verifyFormat("my_int a = (my_int)~0;");
11615 verifyFormat("my_int a = (my_int)++a;");
11616 verifyFormat("my_int a = (my_int)-2;");
11617 verifyFormat("my_int a = (my_int)1;");
11618 verifyFormat("my_int a = (my_int *)1;");
11619 verifyFormat("my_int a = (const my_int)-1;");
11620 verifyFormat("my_int a = (const my_int *)-1;");
11621 verifyFormat("my_int a = (my_int)(my_int)-1;");
11622 verifyFormat("my_int a = (ns::my_int)-2;");
11623 verifyFormat("case (my_int)ONE:");
11624 verifyFormat("auto x = (X)this;");
11625 // Casts in Obj-C style calls used to not be recognized as such.
11626 verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle());
11627
11628 // FIXME: single value wrapped with paren will be treated as cast.
11629 verifyFormat("void f(int i = (kValue)*kMask) {}");
11630
11631 verifyFormat("{ (void)F; }");
11632
11633 // Don't break after a cast's
11634 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
11635 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
11636 " bbbbbbbbbbbbbbbbbbbbbb);");
11637
11638 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)");
11639 verifyFormat("#define CONF_BOOL(x) (bool *)(x)");
11640 verifyFormat("#define CONF_BOOL(x) (bool)(x)");
11641 verifyFormat("bool *y = (bool *)(void *)(x);");
11642 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)");
11643 verifyFormat("bool *y = (bool *)(void *)(int)(x);");
11644 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)");
11645 verifyFormat("bool *y = (bool *)(void *)(int)foo(x);");
11646
11647 // These are not casts.
11648 verifyFormat("void f(int *) {}");
11649 verifyFormat("f(foo)->b;");
11650 verifyFormat("f(foo).b;");
11651 verifyFormat("f(foo)(b);");
11652 verifyFormat("f(foo)[b];");
11653 verifyFormat("[](foo) { return 4; }(bar);");
11654 verifyFormat("(*funptr)(foo)[4];");
11655 verifyFormat("funptrs[4](foo)[4];");
11656 verifyFormat("void f(int *);");
11657 verifyFormat("void f(int *) = 0;");
11658 verifyFormat("void f(SmallVector<int>) {}");
11659 verifyFormat("void f(SmallVector<int>);");
11660 verifyFormat("void f(SmallVector<int>) = 0;");
11661 verifyFormat("void f(int i = (kA * kB) & kMask) {}");
11662 verifyFormat("int a = sizeof(int) * b;");
11663 verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
11664 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
11665 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
11666 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
11667
11668 // These are not casts, but at some point were confused with casts.
11669 verifyFormat("virtual void foo(int *) override;");
11670 verifyFormat("virtual void foo(char &) const;");
11671 verifyFormat("virtual void foo(int *a, char *) const;");
11672 verifyFormat("int a = sizeof(int *) + b;");
11673 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
11674 verifyFormat("bool b = f(g<int>) && c;");
11675 verifyFormat("typedef void (*f)(int i) func;");
11676 verifyFormat("void operator++(int) noexcept;");
11677 verifyFormat("void operator++(int &) noexcept;");
11678 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
11679 "&) noexcept;");
11680 verifyFormat(
11681 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
11682 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
11683 verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
11684 verifyFormat("void operator delete(nothrow_t &) noexcept;");
11685 verifyFormat("void operator delete(foo &) noexcept;");
11686 verifyFormat("void operator delete(foo) noexcept;");
11687 verifyFormat("void operator delete(int) noexcept;");
11688 verifyFormat("void operator delete(int &) noexcept;");
11689 verifyFormat("void operator delete(int &) volatile noexcept;");
11690 verifyFormat("void operator delete(int &) const");
11691 verifyFormat("void operator delete(int &) = default");
11692 verifyFormat("void operator delete(int &) = delete");
11693 verifyFormat("void operator delete(int &) [[noreturn]]");
11694 verifyFormat("void operator delete(int &) throw();");
11695 verifyFormat("void operator delete(int &) throw(int);");
11696 verifyFormat("auto operator delete(int &) -> int;");
11697 verifyFormat("auto operator delete(int &) override");
11698 verifyFormat("auto operator delete(int &) final");
11699
11700 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
11701 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
11702 // FIXME: The indentation here is not ideal.
11703 verifyFormat(
11704 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11705 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
11706 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
11707 }
11708
11709 TEST_F(FormatTest, FormatsFunctionTypes) {
11710 verifyFormat("A<bool()> a;");
11711 verifyFormat("A<SomeType()> a;");
11712 verifyFormat("A<void (*)(int, std::string)> a;");
11713 verifyFormat("A<void *(int)>;");
11714 verifyFormat("void *(*a)(int *, SomeType *);");
11715 verifyFormat("int (*func)(void *);");
11716 verifyFormat("void f() { int (*func)(void *); }");
11717 verifyFormat("template <class CallbackClass>\n"
11718 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
11719
11720 verifyGoogleFormat("A<void*(int*, SomeType*)>;");
11721 verifyGoogleFormat("void* (*a)(int);");
11722 verifyGoogleFormat(
11723 "template <class CallbackClass>\n"
11724 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
11725
11726 // Other constructs can look somewhat like function types:
11727 verifyFormat("A<sizeof(*x)> a;");
11728 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
11729 verifyFormat("some_var = function(*some_pointer_var)[0];");
11730 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
11731 verifyFormat("int x = f(&h)();");
11732 verifyFormat("returnsFunction(&param1, &param2)(param);");
11733 verifyFormat("std::function<\n"
11734 " LooooooooooongTemplatedType<\n"
11735 " SomeType>*(\n"
11736 " LooooooooooooooooongType type)>\n"
11737 " function;",
11738 getGoogleStyleWithColumns(40));
11739 }
11740
11741 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
11742 verifyFormat("A (*foo_)[6];");
11743 verifyFormat("vector<int> (*foo_)[6];");
11744 }
11745
11746 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
11747 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
11748 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
11749 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
11750 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
11751 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
11752 " *LoooooooooooooooooooooooooooooooooooooooongVariable;");
11753
11754 // Different ways of ()-initializiation.
11755 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
11756 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
11757 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
11758 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
11759 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
11760 " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
11761 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
11762 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
11763
11764 // Lambdas should not confuse the variable declaration heuristic.
11765 verifyFormat("LooooooooooooooooongType\n"
11766 " variable(nullptr, [](A *a) {});",
11767 getLLVMStyleWithColumns(40));
11768 }
11769
11770 TEST_F(FormatTest, BreaksLongDeclarations) {
11771 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
11772 " AnotherNameForTheLongType;");
11773 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
11774 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
11775 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
11776 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
11777 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
11778 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
11779 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
11780 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
11781 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
11782 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
11783 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
11784 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
11785 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
11786 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
11787 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
11788 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
11789 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
11790 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
11791 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
11792 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
11793 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
11794 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
11795 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
11796 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
11797 FormatStyle Indented = getLLVMStyle();
11798 Indented.IndentWrappedFunctionNames = true;
11799 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
11800 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
11801 Indented);
11802 verifyFormat(
11803 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
11804 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
11805 Indented);
11806 verifyFormat(
11807 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
11808 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
11809 Indented);
11810 verifyFormat(
11811 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
11812 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
11813 Indented);
11814
11815 // FIXME: Without the comment, this breaks after "(".
11816 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
11817 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
11818 getGoogleStyle());
11819
11820 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
11821 " int LoooooooooooooooooooongParam2) {}");
11822 verifyFormat(
11823 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
11824 " SourceLocation L, IdentifierIn *II,\n"
11825 " Type *T) {}");
11826 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
11827 "ReallyReaaallyLongFunctionName(\n"
11828 " const std::string &SomeParameter,\n"
11829 " const SomeType<string, SomeOtherTemplateParameter>\n"
11830 " &ReallyReallyLongParameterName,\n"
11831 " const SomeType<string, SomeOtherTemplateParameter>\n"
11832 " &AnotherLongParameterName) {}");
11833 verifyFormat("template <typename A>\n"
11834 "SomeLoooooooooooooooooooooongType<\n"
11835 " typename some_namespace::SomeOtherType<A>::Type>\n"
11836 "Function() {}");
11837
11838 verifyGoogleFormat(
11839 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
11840 " aaaaaaaaaaaaaaaaaaaaaaa;");
11841 verifyGoogleFormat(
11842 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
11843 " SourceLocation L) {}");
11844 verifyGoogleFormat(
11845 "some_namespace::LongReturnType\n"
11846 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
11847 " int first_long_parameter, int second_parameter) {}");
11848
11849 verifyGoogleFormat("template <typename T>\n"
11850 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
11851 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
11852 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11853 " int aaaaaaaaaaaaaaaaaaaaaaa);");
11854
11855 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
11856 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11857 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
11858 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11859 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
11860 " aaaaaaaaaaaaaaaaaaaaaaaa);");
11861 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11862 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
11863 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
11864 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
11865
11866 verifyFormat("template <typename T> // Templates on own line.\n"
11867 "static int // Some comment.\n"
11868 "MyFunction(int a);",
11869 getLLVMStyle());
11870 }
11871
11872 TEST_F(FormatTest, FormatsAccessModifiers) {
11873 FormatStyle Style = getLLVMStyle();
11874 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier,
11875 FormatStyle::ELBAMS_LogicalBlock);
11876 verifyFormat("struct foo {\n"
11877 "private:\n"
11878 " void f() {}\n"
11879 "\n"
11880 "private:\n"
11881 " int i;\n"
11882 "\n"
11883 "protected:\n"
11884 " int j;\n"
11885 "};\n",
11886 Style);
11887 verifyFormat("struct foo {\n"
11888 "private:\n"
11889 " void f() {}\n"
11890 "\n"
11891 "private:\n"
11892 " int i;\n"
11893 "\n"
11894 "protected:\n"
11895 " int j;\n"
11896 "};\n",
11897 "struct foo {\n"
11898 "private:\n"
11899 " void f() {}\n"
11900 "private:\n"
11901 " int i;\n"
11902 "protected:\n"
11903 " int j;\n"
11904 "};\n",
11905 Style);
11906 verifyFormat("struct foo { /* comment */\n"
11907 "private:\n"
11908 " int i;\n"
11909 " // comment\n"
11910 "private:\n"
11911 " int j;\n"
11912 "};\n",
11913 Style);
11914 verifyFormat("struct foo {\n"
11915 "#ifdef FOO\n"
11916 "#endif\n"
11917 "private:\n"
11918 " int i;\n"
11919 "#ifdef FOO\n"
11920 "private:\n"
11921 "#endif\n"
11922 " int j;\n"
11923 "};\n",
11924 Style);
11925 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11926 verifyFormat("struct foo {\n"
11927 "private:\n"
11928 " void f() {}\n"
11929 "private:\n"
11930 " int i;\n"
11931 "protected:\n"
11932 " int j;\n"
11933 "};\n",
11934 Style);
11935 verifyFormat("struct foo {\n"
11936 "private:\n"
11937 " void f() {}\n"
11938 "private:\n"
11939 " int i;\n"
11940 "protected:\n"
11941 " int j;\n"
11942 "};\n",
11943 "struct foo {\n"
11944 "\n"
11945 "private:\n"
11946 " void f() {}\n"
11947 "\n"
11948 "private:\n"
11949 " int i;\n"
11950 "\n"
11951 "protected:\n"
11952 " int j;\n"
11953 "};\n",
11954 Style);
11955 verifyFormat("struct foo { /* comment */\n"
11956 "private:\n"
11957 " int i;\n"
11958 " // comment\n"
11959 "private:\n"
11960 " int j;\n"
11961 "};\n",
11962 "struct foo { /* comment */\n"
11963 "\n"
11964 "private:\n"
11965 " int i;\n"
11966 " // comment\n"
11967 "\n"
11968 "private:\n"
11969 " int j;\n"
11970 "};\n",
11971 Style);
11972 verifyFormat("struct foo {\n"
11973 "#ifdef FOO\n"
11974 "#endif\n"
11975 "private:\n"
11976 " int i;\n"
11977 "#ifdef FOO\n"
11978 "private:\n"
11979 "#endif\n"
11980 " int j;\n"
11981 "};\n",
11982 "struct foo {\n"
11983 "#ifdef FOO\n"
11984 "#endif\n"
11985 "\n"
11986 "private:\n"
11987 " int i;\n"
11988 "#ifdef FOO\n"
11989 "\n"
11990 "private:\n"
11991 "#endif\n"
11992 " int j;\n"
11993 "};\n",
11994 Style);
11995 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11996 verifyFormat("struct foo {\n"
11997 "private:\n"
11998 " void f() {}\n"
11999 "\n"
12000 "private:\n"
12001 " int i;\n"
12002 "\n"
12003 "protected:\n"
12004 " int j;\n"
12005 "};\n",
12006 Style);
12007 verifyFormat("struct foo {\n"
12008 "private:\n"
12009 " void f() {}\n"
12010 "\n"
12011 "private:\n"
12012 " int i;\n"
12013 "\n"
12014 "protected:\n"
12015 " int j;\n"
12016 "};\n",
12017 "struct foo {\n"
12018 "private:\n"
12019 " void f() {}\n"
12020 "private:\n"
12021 " int i;\n"
12022 "protected:\n"
12023 " int j;\n"
12024 "};\n",
12025 Style);
12026 verifyFormat("struct foo { /* comment */\n"
12027 "private:\n"
12028 " int i;\n"
12029 " // comment\n"
12030 "\n"
12031 "private:\n"
12032 " int j;\n"
12033 "};\n",
12034 "struct foo { /* comment */\n"
12035 "private:\n"
12036 " int i;\n"
12037 " // comment\n"
12038 "\n"
12039 "private:\n"
12040 " int j;\n"
12041 "};\n",
12042 Style);
12043 verifyFormat("struct foo {\n"
12044 "#ifdef FOO\n"
12045 "#endif\n"
12046 "\n"
12047 "private:\n"
12048 " int i;\n"
12049 "#ifdef FOO\n"
12050 "\n"
12051 "private:\n"
12052 "#endif\n"
12053 " int j;\n"
12054 "};\n",
12055 "struct foo {\n"
12056 "#ifdef FOO\n"
12057 "#endif\n"
12058 "private:\n"
12059 " int i;\n"
12060 "#ifdef FOO\n"
12061 "private:\n"
12062 "#endif\n"
12063 " int j;\n"
12064 "};\n",
12065 Style);
12066 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
12067 EXPECT_EQ("struct foo {\n"
12068 "\n"
12069 "private:\n"
12070 " void f() {}\n"
12071 "\n"
12072 "private:\n"
12073 " int i;\n"
12074 "\n"
12075 "protected:\n"
12076 " int j;\n"
12077 "};\n",
12078 format("struct foo {\n"
12079 "\n"
12080 "private:\n"
12081 " void f() {}\n"
12082 "\n"
12083 "private:\n"
12084 " int i;\n"
12085 "\n"
12086 "protected:\n"
12087 " int j;\n"
12088 "};\n",
12089 Style));
12090 verifyFormat("struct foo {\n"
12091 "private:\n"
12092 " void f() {}\n"
12093 "private:\n"
12094 " int i;\n"
12095 "protected:\n"
12096 " int j;\n"
12097 "};\n",
12098 Style);
12099 EXPECT_EQ("struct foo { /* comment */\n"
12100 "\n"
12101 "private:\n"
12102 " int i;\n"
12103 " // comment\n"
12104 "\n"
12105 "private:\n"
12106 " int j;\n"
12107 "};\n",
12108 format("struct foo { /* comment */\n"
12109 "\n"
12110 "private:\n"
12111 " int i;\n"
12112 " // comment\n"
12113 "\n"
12114 "private:\n"
12115 " int j;\n"
12116 "};\n",
12117 Style));
12118 verifyFormat("struct foo { /* comment */\n"
12119 "private:\n"
12120 " int i;\n"
12121 " // comment\n"
12122 "private:\n"
12123 " int j;\n"
12124 "};\n",
12125 Style);
12126 EXPECT_EQ("struct foo {\n"
12127 "#ifdef FOO\n"
12128 "#endif\n"
12129 "\n"
12130 "private:\n"
12131 " int i;\n"
12132 "#ifdef FOO\n"
12133 "\n"
12134 "private:\n"
12135 "#endif\n"
12136 " int j;\n"
12137 "};\n",
12138 format("struct foo {\n"
12139 "#ifdef FOO\n"
12140 "#endif\n"
12141 "\n"
12142 "private:\n"
12143 " int i;\n"
12144 "#ifdef FOO\n"
12145 "\n"
12146 "private:\n"
12147 "#endif\n"
12148 " int j;\n"
12149 "};\n",
12150 Style));
12151 verifyFormat("struct foo {\n"
12152 "#ifdef FOO\n"
12153 "#endif\n"
12154 "private:\n"
12155 " int i;\n"
12156 "#ifdef FOO\n"
12157 "private:\n"
12158 "#endif\n"
12159 " int j;\n"
12160 "};\n",
12161 Style);
12162
12163 FormatStyle NoEmptyLines = getLLVMStyle();
12164 NoEmptyLines.MaxEmptyLinesToKeep = 0;
12165 verifyFormat("struct foo {\n"
12166 "private:\n"
12167 " void f() {}\n"
12168 "\n"
12169 "private:\n"
12170 " int i;\n"
12171 "\n"
12172 "public:\n"
12173 "protected:\n"
12174 " int j;\n"
12175 "};\n",
12176 NoEmptyLines);
12177
12178 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
12179 verifyFormat("struct foo {\n"
12180 "private:\n"
12181 " void f() {}\n"
12182 "private:\n"
12183 " int i;\n"
12184 "public:\n"
12185 "protected:\n"
12186 " int j;\n"
12187 "};\n",
12188 NoEmptyLines);
12189
12190 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
12191 verifyFormat("struct foo {\n"
12192 "private:\n"
12193 " void f() {}\n"
12194 "\n"
12195 "private:\n"
12196 " int i;\n"
12197 "\n"
12198 "public:\n"
12199 "\n"
12200 "protected:\n"
12201 " int j;\n"
12202 "};\n",
12203 NoEmptyLines);
12204 }
12205
12206 TEST_F(FormatTest, FormatsAfterAccessModifiers) {
12207
12208 FormatStyle Style = getLLVMStyle();
12209 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never);
12210 verifyFormat("struct foo {\n"
12211 "private:\n"
12212 " void f() {}\n"
12213 "\n"
12214 "private:\n"
12215 " int i;\n"
12216 "\n"
12217 "protected:\n"
12218 " int j;\n"
12219 "};\n",
12220 Style);
12221
12222 // Check if lines are removed.
12223 verifyFormat("struct foo {\n"
12224 "private:\n"
12225 " void f() {}\n"
12226 "\n"
12227 "private:\n"
12228 " int i;\n"
12229 "\n"
12230 "protected:\n"
12231 " int j;\n"
12232 "};\n",
12233 "struct foo {\n"
12234 "private:\n"
12235 "\n"
12236 " void f() {}\n"
12237 "\n"
12238 "private:\n"
12239 "\n"
12240 " int i;\n"
12241 "\n"
12242 "protected:\n"
12243 "\n"
12244 " int j;\n"
12245 "};\n",
12246 Style);
12247
12248 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
12249 verifyFormat("struct foo {\n"
12250 "private:\n"
12251 "\n"
12252 " void f() {}\n"
12253 "\n"
12254 "private:\n"
12255 "\n"
12256 " int i;\n"
12257 "\n"
12258 "protected:\n"
12259 "\n"
12260 " int j;\n"
12261 "};\n",
12262 Style);
12263
12264 // Check if lines are added.
12265 verifyFormat("struct foo {\n"
12266 "private:\n"
12267 "\n"
12268 " void f() {}\n"
12269 "\n"
12270 "private:\n"
12271 "\n"
12272 " int i;\n"
12273 "\n"
12274 "protected:\n"
12275 "\n"
12276 " int j;\n"
12277 "};\n",
12278 "struct foo {\n"
12279 "private:\n"
12280 " void f() {}\n"
12281 "\n"
12282 "private:\n"
12283 " int i;\n"
12284 "\n"
12285 "protected:\n"
12286 " int j;\n"
12287 "};\n",
12288 Style);
12289
12290 // Leave tests rely on the code layout, test::messUp can not be used.
12291 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
12292 Style.MaxEmptyLinesToKeep = 0u;
12293 verifyFormat("struct foo {\n"
12294 "private:\n"
12295 " void f() {}\n"
12296 "\n"
12297 "private:\n"
12298 " int i;\n"
12299 "\n"
12300 "protected:\n"
12301 " int j;\n"
12302 "};\n",
12303 Style);
12304
12305 // Check if MaxEmptyLinesToKeep is respected.
12306 EXPECT_EQ("struct foo {\n"
12307 "private:\n"
12308 " void f() {}\n"
12309 "\n"
12310 "private:\n"
12311 " int i;\n"
12312 "\n"
12313 "protected:\n"
12314 " int j;\n"
12315 "};\n",
12316 format("struct foo {\n"
12317 "private:\n"
12318 "\n\n\n"
12319 " void f() {}\n"
12320 "\n"
12321 "private:\n"
12322 "\n\n\n"
12323 " int i;\n"
12324 "\n"
12325 "protected:\n"
12326 "\n\n\n"
12327 " int j;\n"
12328 "};\n",
12329 Style));
12330
12331 Style.MaxEmptyLinesToKeep = 1u;
12332 EXPECT_EQ("struct foo {\n"
12333 "private:\n"
12334 "\n"
12335 " void f() {}\n"
12336 "\n"
12337 "private:\n"
12338 "\n"
12339 " int i;\n"
12340 "\n"
12341 "protected:\n"
12342 "\n"
12343 " int j;\n"
12344 "};\n",
12345 format("struct foo {\n"
12346 "private:\n"
12347 "\n"
12348 " void f() {}\n"
12349 "\n"
12350 "private:\n"
12351 "\n"
12352 " int i;\n"
12353 "\n"
12354 "protected:\n"
12355 "\n"
12356 " int j;\n"
12357 "};\n",
12358 Style));
12359 // Check if no lines are kept.
12360 EXPECT_EQ("struct foo {\n"
12361 "private:\n"
12362 " void f() {}\n"
12363 "\n"
12364 "private:\n"
12365 " int i;\n"
12366 "\n"
12367 "protected:\n"
12368 " int j;\n"
12369 "};\n",
12370 format("struct foo {\n"
12371 "private:\n"
12372 " void f() {}\n"
12373 "\n"
12374 "private:\n"
12375 " int i;\n"
12376 "\n"
12377 "protected:\n"
12378 " int j;\n"
12379 "};\n",
12380 Style));
12381 // Check if MaxEmptyLinesToKeep is respected.
12382 EXPECT_EQ("struct foo {\n"
12383 "private:\n"
12384 "\n"
12385 " void f() {}\n"
12386 "\n"
12387 "private:\n"
12388 "\n"
12389 " int i;\n"
12390 "\n"
12391 "protected:\n"
12392 "\n"
12393 " int j;\n"
12394 "};\n",
12395 format("struct foo {\n"
12396 "private:\n"
12397 "\n\n\n"
12398 " void f() {}\n"
12399 "\n"
12400 "private:\n"
12401 "\n\n\n"
12402 " int i;\n"
12403 "\n"
12404 "protected:\n"
12405 "\n\n\n"
12406 " int j;\n"
12407 "};\n",
12408 Style));
12409
12410 Style.MaxEmptyLinesToKeep = 10u;
12411 EXPECT_EQ("struct foo {\n"
12412 "private:\n"
12413 "\n\n\n"
12414 " void f() {}\n"
12415 "\n"
12416 "private:\n"
12417 "\n\n\n"
12418 " int i;\n"
12419 "\n"
12420 "protected:\n"
12421 "\n\n\n"
12422 " int j;\n"
12423 "};\n",
12424 format("struct foo {\n"
12425 "private:\n"
12426 "\n\n\n"
12427 " void f() {}\n"
12428 "\n"
12429 "private:\n"
12430 "\n\n\n"
12431 " int i;\n"
12432 "\n"
12433 "protected:\n"
12434 "\n\n\n"
12435 " int j;\n"
12436 "};\n",
12437 Style));
12438
12439 // Test with comments.
12440 Style = getLLVMStyle();
12441 verifyFormat("struct foo {\n"
12442 "private:\n"
12443 " // comment\n"
12444 " void f() {}\n"
12445 "\n"
12446 "private: /* comment */\n"
12447 " int i;\n"
12448 "};\n",
12449 Style);
12450 verifyFormat("struct foo {\n"
12451 "private:\n"
12452 " // comment\n"
12453 " void f() {}\n"
12454 "\n"
12455 "private: /* comment */\n"
12456 " int i;\n"
12457 "};\n",
12458 "struct foo {\n"
12459 "private:\n"
12460 "\n"
12461 " // comment\n"
12462 " void f() {}\n"
12463 "\n"
12464 "private: /* comment */\n"
12465 "\n"
12466 " int i;\n"
12467 "};\n",
12468 Style);
12469
12470 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
12471 verifyFormat("struct foo {\n"
12472 "private:\n"
12473 "\n"
12474 " // comment\n"
12475 " void f() {}\n"
12476 "\n"
12477 "private: /* comment */\n"
12478 "\n"
12479 " int i;\n"
12480 "};\n",
12481 "struct foo {\n"
12482 "private:\n"
12483 " // comment\n"
12484 " void f() {}\n"
12485 "\n"
12486 "private: /* comment */\n"
12487 " int i;\n"
12488 "};\n",
12489 Style);
12490 verifyFormat("struct foo {\n"
12491 "private:\n"
12492 "\n"
12493 " // comment\n"
12494 " void f() {}\n"
12495 "\n"
12496 "private: /* comment */\n"
12497 "\n"
12498 " int i;\n"
12499 "};\n",
12500 Style);
12501
12502 // Test with preprocessor defines.
12503 Style = getLLVMStyle();
12504 verifyFormat("struct foo {\n"
12505 "private:\n"
12506 "#ifdef FOO\n"
12507 "#endif\n"
12508 " void f() {}\n"
12509 "};\n",
12510 Style);
12511 verifyFormat("struct foo {\n"
12512 "private:\n"
12513 "#ifdef FOO\n"
12514 "#endif\n"
12515 " void f() {}\n"
12516 "};\n",
12517 "struct foo {\n"
12518 "private:\n"
12519 "\n"
12520 "#ifdef FOO\n"
12521 "#endif\n"
12522 " void f() {}\n"
12523 "};\n",
12524 Style);
12525
12526 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
12527 verifyFormat("struct foo {\n"
12528 "private:\n"
12529 "\n"
12530 "#ifdef FOO\n"
12531 "#endif\n"
12532 " void f() {}\n"
12533 "};\n",
12534 "struct foo {\n"
12535 "private:\n"
12536 "#ifdef FOO\n"
12537 "#endif\n"
12538 " void f() {}\n"
12539 "};\n",
12540 Style);
12541 verifyFormat("struct foo {\n"
12542 "private:\n"
12543 "\n"
12544 "#ifdef FOO\n"
12545 "#endif\n"
12546 " void f() {}\n"
12547 "};\n",
12548 Style);
12549 }
12550
12551 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) {
12552 // Combined tests of EmptyLineAfterAccessModifier and
12553 // EmptyLineBeforeAccessModifier.
12554 FormatStyle Style = getLLVMStyle();
12555 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
12556 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
12557 verifyFormat("struct foo {\n"
12558 "private:\n"
12559 "\n"
12560 "protected:\n"
12561 "};\n",
12562 Style);
12563
12564 Style.MaxEmptyLinesToKeep = 10u;
12565 // Both remove all new lines.
12566 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
12567 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
12568 verifyFormat("struct foo {\n"
12569 "private:\n"
12570 "protected:\n"
12571 "};\n",
12572 "struct foo {\n"
12573 "private:\n"
12574 "\n\n\n"
12575 "protected:\n"
12576 "};\n",
12577 Style);
12578
12579 // Leave tests rely on the code layout, test::messUp can not be used.
12580 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
12581 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
12582 Style.MaxEmptyLinesToKeep = 10u;
12583 EXPECT_EQ("struct foo {\n"
12584 "private:\n"
12585 "\n\n\n"
12586 "protected:\n"
12587 "};\n",
12588 format("struct foo {\n"
12589 "private:\n"
12590 "\n\n\n"
12591 "protected:\n"
12592 "};\n",
12593 Style));
12594 Style.MaxEmptyLinesToKeep = 3u;
12595 EXPECT_EQ("struct foo {\n"
12596 "private:\n"
12597 "\n\n\n"
12598 "protected:\n"
12599 "};\n",
12600 format("struct foo {\n"
12601 "private:\n"
12602 "\n\n\n"
12603 "protected:\n"
12604 "};\n",
12605 Style));
12606 Style.MaxEmptyLinesToKeep = 1u;
12607 EXPECT_EQ("struct foo {\n"
12608 "private:\n"
12609 "\n\n\n"
12610 "protected:\n"
12611 "};\n",
12612 format("struct foo {\n"
12613 "private:\n"
12614 "\n\n\n"
12615 "protected:\n"
12616 "};\n",
12617 Style)); // Based on new lines in original document and not
12618 // on the setting.
12619
12620 Style.MaxEmptyLinesToKeep = 10u;
12621 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
12622 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
12623 // Newlines are kept if they are greater than zero,
12624 // test::messUp removes all new lines which changes the logic
12625 EXPECT_EQ("struct foo {\n"
12626 "private:\n"
12627 "\n\n\n"
12628 "protected:\n"
12629 "};\n",
12630 format("struct foo {\n"
12631 "private:\n"
12632 "\n\n\n"
12633 "protected:\n"
12634 "};\n",
12635 Style));
12636
12637 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
12638 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
12639 // test::messUp removes all new lines which changes the logic
12640 EXPECT_EQ("struct foo {\n"
12641 "private:\n"
12642 "\n\n\n"
12643 "protected:\n"
12644 "};\n",
12645 format("struct foo {\n"
12646 "private:\n"
12647 "\n\n\n"
12648 "protected:\n"
12649 "};\n",
12650 Style));
12651
12652 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
12653 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
12654 EXPECT_EQ("struct foo {\n"
12655 "private:\n"
12656 "\n\n\n"
12657 "protected:\n"
12658 "};\n",
12659 format("struct foo {\n"
12660 "private:\n"
12661 "\n\n\n"
12662 "protected:\n"
12663 "};\n",
12664 Style)); // test::messUp removes all new lines which changes
12665 // the logic.
12666
12667 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
12668 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
12669 verifyFormat("struct foo {\n"
12670 "private:\n"
12671 "protected:\n"
12672 "};\n",
12673 "struct foo {\n"
12674 "private:\n"
12675 "\n\n\n"
12676 "protected:\n"
12677 "};\n",
12678 Style);
12679
12680 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
12681 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
12682 EXPECT_EQ("struct foo {\n"
12683 "private:\n"
12684 "\n\n\n"
12685 "protected:\n"
12686 "};\n",
12687 format("struct foo {\n"
12688 "private:\n"
12689 "\n\n\n"
12690 "protected:\n"
12691 "};\n",
12692 Style)); // test::messUp removes all new lines which changes
12693 // the logic.
12694
12695 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
12696 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
12697 verifyFormat("struct foo {\n"
12698 "private:\n"
12699 "protected:\n"
12700 "};\n",
12701 "struct foo {\n"
12702 "private:\n"
12703 "\n\n\n"
12704 "protected:\n"
12705 "};\n",
12706 Style);
12707
12708 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
12709 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
12710 verifyFormat("struct foo {\n"
12711 "private:\n"
12712 "protected:\n"
12713 "};\n",
12714 "struct foo {\n"
12715 "private:\n"
12716 "\n\n\n"
12717 "protected:\n"
12718 "};\n",
12719 Style);
12720
12721 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
12722 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
12723 verifyFormat("struct foo {\n"
12724 "private:\n"
12725 "protected:\n"
12726 "};\n",
12727 "struct foo {\n"
12728 "private:\n"
12729 "\n\n\n"
12730 "protected:\n"
12731 "};\n",
12732 Style);
12733
12734 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
12735 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
12736 verifyFormat("struct foo {\n"
12737 "private:\n"
12738 "protected:\n"
12739 "};\n",
12740 "struct foo {\n"
12741 "private:\n"
12742 "\n\n\n"
12743 "protected:\n"
12744 "};\n",
12745 Style);
12746 }
12747
12748 TEST_F(FormatTest, FormatsArrays) {
12749 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
12750 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
12751 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
12752 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
12753 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
12754 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
12755 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12756 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
12757 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12758 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
12759 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12760 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
12761 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
12762 verifyFormat(
12763 "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
12764 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
12765 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
12766 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
12767 " .aaaaaaaaaaaaaaaaaaaaaa();");
12768
12769 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
12770 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
12771 verifyFormat(
12772 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
12773 " .aaaaaaa[0]\n"
12774 " .aaaaaaaaaaaaaaaaaaaaaa();");
12775 verifyFormat("a[::b::c];");
12776
12777 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
12778
12779 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
12780 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
12781 }
12782
12783 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
12784 verifyFormat("(a)->b();");
12785 verifyFormat("--a;");
12786 }
12787
12788 TEST_F(FormatTest, HandlesIncludeDirectives) {
12789 verifyFormat("#include <string>\n"
12790 "#include <a/b/c.h>\n"
12791 "#include \"a/b/string\"\n"
12792 "#include \"string.h\"\n"
12793 "#include \"string.h\"\n"
12794 "#include <a-a>\n"
12795 "#include < path with space >\n"
12796 "#include_next <test.h>"
12797 "#include \"abc.h\" // this is included for ABC\n"
12798 "#include \"some long include\" // with a comment\n"
12799 "#include \"some very long include path\"\n"
12800 "#include <some/very/long/include/path>\n",
12801 getLLVMStyleWithColumns(35));
12802 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\""));
12803 EXPECT_EQ("#include <a>", format("#include<a>"));
12804
12805 verifyFormat("#import <string>");
12806 verifyFormat("#import <a/b/c.h>");
12807 verifyFormat("#import \"a/b/string\"");
12808 verifyFormat("#import \"string.h\"");
12809 verifyFormat("#import \"string.h\"");
12810 verifyFormat("#if __has_include(<strstream>)\n"
12811 "#include <strstream>\n"
12812 "#endif");
12813
12814 verifyFormat("#define MY_IMPORT <a/b>");
12815
12816 verifyFormat("#if __has_include(<a/b>)");
12817 verifyFormat("#if __has_include_next(<a/b>)");
12818 verifyFormat("#define F __has_include(<a/b>)");
12819 verifyFormat("#define F __has_include_next(<a/b>)");
12820
12821 // Protocol buffer definition or missing "#".
12822 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
12823 getLLVMStyleWithColumns(30));
12824
12825 FormatStyle Style = getLLVMStyle();
12826 Style.AlwaysBreakBeforeMultilineStrings = true;
12827 Style.ColumnLimit = 0;
12828 verifyFormat("#import \"abc.h\"", Style);
12829
12830 // But 'import' might also be a regular C++ namespace.
12831 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12832 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
12833 verifyFormat("import::Bar foo(val ? 2 : 1);");
12834 }
12835
12836 //===----------------------------------------------------------------------===//
12837 // Error recovery tests.
12838 //===----------------------------------------------------------------------===//
12839
12840 TEST_F(FormatTest, IncompleteParameterLists) {
12841 FormatStyle NoBinPacking = getLLVMStyle();
12842 NoBinPacking.BinPackParameters = false;
12843 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
12844 " double *min_x,\n"
12845 " double *max_x,\n"
12846 " double *min_y,\n"
12847 " double *max_y,\n"
12848 " double *min_z,\n"
12849 " double *max_z, ) {}",
12850 NoBinPacking);
12851 }
12852
12853 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
12854 verifyFormat("void f() { return; }\n42");
12855 verifyFormat("void f() {\n"
12856 " if (0)\n"
12857 " return;\n"
12858 "}\n"
12859 "42");
12860 verifyFormat("void f() { return }\n42");
12861 verifyFormat("void f() {\n"
12862 " if (0)\n"
12863 " return\n"
12864 "}\n"
12865 "42");
12866 }
12867
12868 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
12869 EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
12870 EXPECT_EQ("void f() {\n"
12871 " if (a)\n"
12872 " return\n"
12873 "}",
12874 format("void f ( ) { if ( a ) return }"));
12875 EXPECT_EQ("namespace N {\n"
12876 "void f()\n"
12877 "}",
12878 format("namespace N { void f() }"));
12879 EXPECT_EQ("namespace N {\n"
12880 "void f() {}\n"
12881 "void g()\n"
12882 "} // namespace N",
12883 format("namespace N { void f( ) { } void g( ) }"));
12884 }
12885
12886 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
12887 verifyFormat("int aaaaaaaa =\n"
12888 " // Overlylongcomment\n"
12889 " b;",
12890 getLLVMStyleWithColumns(20));
12891 verifyFormat("function(\n"
12892 " ShortArgument,\n"
12893 " LoooooooooooongArgument);\n",
12894 getLLVMStyleWithColumns(20));
12895 }
12896
12897 TEST_F(FormatTest, IncorrectAccessSpecifier) {
12898 verifyFormat("public:");
12899 verifyFormat("class A {\n"
12900 "public\n"
12901 " void f() {}\n"
12902 "};");
12903 verifyFormat("public\n"
12904 "int qwerty;");
12905 verifyFormat("public\n"
12906 "B {}");
12907 verifyFormat("public\n"
12908 "{}");
12909 verifyFormat("public\n"
12910 "B { int x; }");
12911 }
12912
12913 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
12914 verifyFormat("{");
12915 verifyFormat("#})");
12916 verifyNoCrash("(/**/[:!] ?[).");
12917 }
12918
12919 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
12920 // Found by oss-fuzz:
12921 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
12922 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
12923 Style.ColumnLimit = 60;
12924 verifyNoCrash(
12925 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
12926 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
12927 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
12928 Style);
12929 }
12930
12931 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
12932 verifyFormat("do {\n}");
12933 verifyFormat("do {\n}\n"
12934 "f();");
12935 verifyFormat("do {\n}\n"
12936 "wheeee(fun);");
12937 verifyFormat("do {\n"
12938 " f();\n"
12939 "}");
12940 }
12941
12942 TEST_F(FormatTest, IncorrectCodeMissingParens) {
12943 verifyFormat("if {\n foo;\n foo();\n}");
12944 verifyFormat("switch {\n foo;\n foo();\n}");
12945 verifyIncompleteFormat("for {\n foo;\n foo();\n}");
12946 verifyIncompleteFormat("ERROR: for target;");
12947 verifyFormat("while {\n foo;\n foo();\n}");
12948 verifyFormat("do {\n foo;\n foo();\n} while;");
12949 }
12950
12951 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
12952 verifyIncompleteFormat("namespace {\n"
12953 "class Foo { Foo (\n"
12954 "};\n"
12955 "} // namespace");
12956 }
12957
12958 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
12959 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n"));
12960 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
12961 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
12962 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n"));
12963
12964 EXPECT_EQ("{\n"
12965 " {\n"
12966 " breakme(\n"
12967 " qwe);\n"
12968 " }\n",
12969 format("{\n"
12970 " {\n"
12971 " breakme(qwe);\n"
12972 "}\n",
12973 getLLVMStyleWithColumns(10)));
12974 }
12975
12976 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
12977 verifyFormat("int x = {\n"
12978 " avariable,\n"
12979 " b(alongervariable)};",
12980 getLLVMStyleWithColumns(25));
12981 }
12982
12983 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
12984 verifyFormat("return (a)(b){1, 2, 3};");
12985 }
12986
12987 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
12988 verifyFormat("vector<int> x{1, 2, 3, 4};");
12989 verifyFormat("vector<int> x{\n"
12990 " 1,\n"
12991 " 2,\n"
12992 " 3,\n"
12993 " 4,\n"
12994 "};");
12995 verifyFormat("vector<T> x{{}, {}, {}, {}};");
12996 verifyFormat("f({1, 2});");
12997 verifyFormat("auto v = Foo{-1};");
12998 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
12999 verifyFormat("Class::Class : member{1, 2, 3} {}");
13000 verifyFormat("new vector<int>{1, 2, 3};");
13001 verifyFormat("new int[3]{1, 2, 3};");
13002 verifyFormat("new int{1};");
13003 verifyFormat("return {arg1, arg2};");
13004 verifyFormat("return {arg1, SomeType{parameter}};");
13005 verifyFormat("int count = set<int>{f(), g(), h()}.size();");
13006 verifyFormat("new T{arg1, arg2};");
13007 verifyFormat("f(MyMap[{composite, key}]);");
13008 verifyFormat("class Class {\n"
13009 " T member = {arg1, arg2};\n"
13010 "};");
13011 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
13012 verifyFormat("const struct A a = {.a = 1, .b = 2};");
13013 verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
13014 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
13015 verifyFormat("int a = std::is_integral<int>{} + 0;");
13016
13017 verifyFormat("int foo(int i) { return fo1{}(i); }");
13018 verifyFormat("int foo(int i) { return fo1{}(i); }");
13019 verifyFormat("auto i = decltype(x){};");
13020 verifyFormat("auto i = typeof(x){};");
13021 verifyFormat("auto i = _Atomic(x){};");
13022 verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
13023 verifyFormat("Node n{1, Node{1000}, //\n"
13024 " 2};");
13025 verifyFormat("Aaaa aaaaaaa{\n"
13026 " {\n"
13027 " aaaa,\n"
13028 " },\n"
13029 "};");
13030 verifyFormat("class C : public D {\n"
13031 " SomeClass SC{2};\n"
13032 "};");
13033 verifyFormat("class C : public A {\n"
13034 " class D : public B {\n"
13035 " void f() { int i{2}; }\n"
13036 " };\n"
13037 "};");
13038 verifyFormat("#define A {a, a},");
13039 // Don't confuse braced list initializers with compound statements.
13040 verifyFormat(
13041 "class A {\n"
13042 " A() : a{} {}\n"
13043 " A(int b) : b(b) {}\n"
13044 " A(int a, int b) : a(a), bs{{bs...}} { f(); }\n"
13045 " int a, b;\n"
13046 " explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n"
13047 " explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} "
13048 "{}\n"
13049 "};");
13050
13051 // Avoid breaking between equal sign and opening brace
13052 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
13053 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
13054 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
13055 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
13056 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
13057 " {\"ccccccccccccccccccccc\", 2}};",
13058 AvoidBreakingFirstArgument);
13059
13060 // Binpacking only if there is no trailing comma
13061 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
13062 " cccccccccc, dddddddddd};",
13063 getLLVMStyleWithColumns(50));
13064 verifyFormat("const Aaaaaa aaaaa = {\n"
13065 " aaaaaaaaaaa,\n"
13066 " bbbbbbbbbbb,\n"
13067 " ccccccccccc,\n"
13068 " ddddddddddd,\n"
13069 "};",
13070 getLLVMStyleWithColumns(50));
13071
13072 // Cases where distinguising braced lists and blocks is hard.
13073 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
13074 verifyFormat("void f() {\n"
13075 " return; // comment\n"
13076 "}\n"
13077 "SomeType t;");
13078 verifyFormat("void f() {\n"
13079 " if (a) {\n"
13080 " f();\n"
13081 " }\n"
13082 "}\n"
13083 "SomeType t;");
13084
13085 // In combination with BinPackArguments = false.
13086 FormatStyle NoBinPacking = getLLVMStyle();
13087 NoBinPacking.BinPackArguments = false;
13088 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
13089 " bbbbb,\n"
13090 " ccccc,\n"
13091 " ddddd,\n"
13092 " eeeee,\n"
13093 " ffffff,\n"
13094 " ggggg,\n"
13095 " hhhhhh,\n"
13096 " iiiiii,\n"
13097 " jjjjjj,\n"
13098 " kkkkkk};",
13099 NoBinPacking);
13100 verifyFormat("const Aaaaaa aaaaa = {\n"
13101 " aaaaa,\n"
13102 " bbbbb,\n"
13103 " ccccc,\n"
13104 " ddddd,\n"
13105 " eeeee,\n"
13106 " ffffff,\n"
13107 " ggggg,\n"
13108 " hhhhhh,\n"
13109 " iiiiii,\n"
13110 " jjjjjj,\n"
13111 " kkkkkk,\n"
13112 "};",
13113 NoBinPacking);
13114 verifyFormat(
13115 "const Aaaaaa aaaaa = {\n"
13116 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
13117 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
13118 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
13119 "};",
13120 NoBinPacking);
13121
13122 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
13123 EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n"
13124 " CDDDP83848_BMCR_REGISTER,\n"
13125 " CDDDP83848_BMSR_REGISTER,\n"
13126 " CDDDP83848_RBR_REGISTER};",
13127 format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
13128 " CDDDP83848_BMSR_REGISTER,\n"
13129 " CDDDP83848_RBR_REGISTER};",
13130 NoBinPacking));
13131
13132 // FIXME: The alignment of these trailing comments might be bad. Then again,
13133 // this might be utterly useless in real code.
13134 verifyFormat("Constructor::Constructor()\n"
13135 " : some_value{ //\n"
13136 " aaaaaaa, //\n"
13137 " bbbbbbb} {}");
13138
13139 // In braced lists, the first comment is always assumed to belong to the
13140 // first element. Thus, it can be moved to the next or previous line as
13141 // appropriate.
13142 EXPECT_EQ("function({// First element:\n"
13143 " 1,\n"
13144 " // Second element:\n"
13145 " 2});",
13146 format("function({\n"
13147 " // First element:\n"
13148 " 1,\n"
13149 " // Second element:\n"
13150 " 2});"));
13151 EXPECT_EQ("std::vector<int> MyNumbers{\n"
13152 " // First element:\n"
13153 " 1,\n"
13154 " // Second element:\n"
13155 " 2};",
13156 format("std::vector<int> MyNumbers{// First element:\n"
13157 " 1,\n"
13158 " // Second element:\n"
13159 " 2};",
13160 getLLVMStyleWithColumns(30)));
13161 // A trailing comma should still lead to an enforced line break and no
13162 // binpacking.
13163 EXPECT_EQ("vector<int> SomeVector = {\n"
13164 " // aaa\n"
13165 " 1,\n"
13166 " 2,\n"
13167 "};",
13168 format("vector<int> SomeVector = { // aaa\n"
13169 " 1, 2, };"));
13170
13171 // C++11 brace initializer list l-braces should not be treated any differently
13172 // when breaking before lambda bodies is enabled
13173 FormatStyle BreakBeforeLambdaBody = getLLVMStyle();
13174 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
13175 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
13176 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true;
13177 verifyFormat(
13178 "std::runtime_error{\n"
13179 " \"Long string which will force a break onto the next line...\"};",
13180 BreakBeforeLambdaBody);
13181
13182 FormatStyle ExtraSpaces = getLLVMStyle();
13183 ExtraSpaces.Cpp11BracedListStyle = false;
13184 ExtraSpaces.ColumnLimit = 75;
13185 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
13186 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
13187 verifyFormat("f({ 1, 2 });", ExtraSpaces);
13188 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
13189 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
13190 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
13191 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
13192 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
13193 verifyFormat("return { arg1, arg2 };", ExtraSpaces);
13194 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
13195 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
13196 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
13197 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
13198 verifyFormat("class Class {\n"
13199 " T member = { arg1, arg2 };\n"
13200 "};",
13201 ExtraSpaces);
13202 verifyFormat(
13203 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
13204 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
13205 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
13206 " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
13207 ExtraSpaces);
13208 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
13209 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
13210 ExtraSpaces);
13211 verifyFormat(
13212 "someFunction(OtherParam,\n"
13213 " BracedList{ // comment 1 (Forcing interesting break)\n"
13214 " param1, param2,\n"
13215 " // comment 2\n"
13216 " param3, param4 });",
13217 ExtraSpaces);
13218 verifyFormat(
13219 "std::this_thread::sleep_for(\n"
13220 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
13221 ExtraSpaces);
13222 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
13223 " aaaaaaa,\n"
13224 " aaaaaaaaaa,\n"
13225 " aaaaa,\n"
13226 " aaaaaaaaaaaaaaa,\n"
13227 " aaa,\n"
13228 " aaaaaaaaaa,\n"
13229 " a,\n"
13230 " aaaaaaaaaaaaaaaaaaaaa,\n"
13231 " aaaaaaaaaaaa,\n"
13232 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
13233 " aaaaaaa,\n"
13234 " a};");
13235 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
13236 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
13237 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
13238
13239 // Avoid breaking between initializer/equal sign and opening brace
13240 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
13241 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
13242 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
13243 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
13244 " { \"ccccccccccccccccccccc\", 2 }\n"
13245 "};",
13246 ExtraSpaces);
13247 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
13248 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
13249 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
13250 " { \"ccccccccccccccccccccc\", 2 }\n"
13251 "};",
13252 ExtraSpaces);
13253
13254 FormatStyle SpaceBeforeBrace = getLLVMStyle();
13255 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
13256 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
13257 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
13258
13259 FormatStyle SpaceBetweenBraces = getLLVMStyle();
13260 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
13261 SpaceBetweenBraces.SpacesInParentheses = true;
13262 SpaceBetweenBraces.SpacesInSquareBrackets = true;
13263 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
13264 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
13265 verifyFormat("vector< int > x{ // comment 1\n"
13266 " 1, 2, 3, 4 };",
13267 SpaceBetweenBraces);
13268 SpaceBetweenBraces.ColumnLimit = 20;
13269 EXPECT_EQ("vector< int > x{\n"
13270 " 1, 2, 3, 4 };",
13271 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
13272 SpaceBetweenBraces.ColumnLimit = 24;
13273 EXPECT_EQ("vector< int > x{ 1, 2,\n"
13274 " 3, 4 };",
13275 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
13276 EXPECT_EQ("vector< int > x{\n"
13277 " 1,\n"
13278 " 2,\n"
13279 " 3,\n"
13280 " 4,\n"
13281 "};",
13282 format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces));
13283 verifyFormat("vector< int > x{};", SpaceBetweenBraces);
13284 SpaceBetweenBraces.SpaceInEmptyParentheses = true;
13285 verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
13286 }
13287
13288 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
13289 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13290 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13291 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13292 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13293 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13294 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
13295 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
13296 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13297 " 1, 22, 333, 4444, 55555, //\n"
13298 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13299 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
13300 verifyFormat(
13301 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13302 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13303 " 1, 22, 333, 4444, 55555, 666666, // comment\n"
13304 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
13305 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
13306 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
13307 " 7777777};");
13308 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
13309 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
13310 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
13311 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
13312 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
13313 " // Separating comment.\n"
13314 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
13315 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
13316 " // Leading comment\n"
13317 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
13318 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
13319 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
13320 " 1, 1, 1, 1};",
13321 getLLVMStyleWithColumns(39));
13322 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
13323 " 1, 1, 1, 1};",
13324 getLLVMStyleWithColumns(38));
13325 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
13326 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
13327 getLLVMStyleWithColumns(43));
13328 verifyFormat(
13329 "static unsigned SomeValues[10][3] = {\n"
13330 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n"
13331 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
13332 verifyFormat("static auto fields = new vector<string>{\n"
13333 " \"aaaaaaaaaaaaa\",\n"
13334 " \"aaaaaaaaaaaaa\",\n"
13335 " \"aaaaaaaaaaaa\",\n"
13336 " \"aaaaaaaaaaaaaa\",\n"
13337 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
13338 " \"aaaaaaaaaaaa\",\n"
13339 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
13340 "};");
13341 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
13342 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
13343 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
13344 " 3, cccccccccccccccccccccc};",
13345 getLLVMStyleWithColumns(60));
13346
13347 // Trailing commas.
13348 verifyFormat("vector<int> x = {\n"
13349 " 1, 1, 1, 1, 1, 1, 1, 1,\n"
13350 "};",
13351 getLLVMStyleWithColumns(39));
13352 verifyFormat("vector<int> x = {\n"
13353 " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
13354 "};",
13355 getLLVMStyleWithColumns(39));
13356 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
13357 " 1, 1, 1, 1,\n"
13358 " /**/ /**/};",
13359 getLLVMStyleWithColumns(39));
13360
13361 // Trailing comment in the first line.
13362 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n"
13363 " 1111111111, 2222222222, 33333333333, 4444444444, //\n"
13364 " 111111111, 222222222, 3333333333, 444444444, //\n"
13365 " 11111111, 22222222, 333333333, 44444444};");
13366 // Trailing comment in the last line.
13367 verifyFormat("int aaaaa[] = {\n"
13368 " 1, 2, 3, // comment\n"
13369 " 4, 5, 6 // comment\n"
13370 "};");
13371
13372 // With nested lists, we should either format one item per line or all nested
13373 // lists one on line.
13374 // FIXME: For some nested lists, we can do better.
13375 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
13376 " {aaaaaaaaaaaaaaaaaaa},\n"
13377 " {aaaaaaaaaaaaaaaaaaaaa},\n"
13378 " {aaaaaaaaaaaaaaaaa}};",
13379 getLLVMStyleWithColumns(60));
13380 verifyFormat(
13381 "SomeStruct my_struct_array = {\n"
13382 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
13383 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
13384 " {aaa, aaa},\n"
13385 " {aaa, aaa},\n"
13386 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
13387 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
13388 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
13389
13390 // No column layout should be used here.
13391 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
13392 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
13393
13394 verifyNoCrash("a<,");
13395
13396 // No braced initializer here.
13397 verifyFormat("void f() {\n"
13398 " struct Dummy {};\n"
13399 " f(v);\n"
13400 "}");
13401
13402 // Long lists should be formatted in columns even if they are nested.
13403 verifyFormat(
13404 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13405 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13406 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13407 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13408 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13409 " 1, 22, 333, 4444, 55555, 666666, 7777777});");
13410
13411 // Allow "single-column" layout even if that violates the column limit. There
13412 // isn't going to be a better way.
13413 verifyFormat("std::vector<int> a = {\n"
13414 " aaaaaaaa,\n"
13415 " aaaaaaaa,\n"
13416 " aaaaaaaa,\n"
13417 " aaaaaaaa,\n"
13418 " aaaaaaaaaa,\n"
13419 " aaaaaaaa,\n"
13420 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
13421 getLLVMStyleWithColumns(30));
13422 verifyFormat("vector<int> aaaa = {\n"
13423 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
13424 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
13425 " aaaaaa.aaaaaaa,\n"
13426 " aaaaaa.aaaaaaa,\n"
13427 " aaaaaa.aaaaaaa,\n"
13428 " aaaaaa.aaaaaaa,\n"
13429 "};");
13430
13431 // Don't create hanging lists.
13432 verifyFormat("someFunction(Param, {List1, List2,\n"
13433 " List3});",
13434 getLLVMStyleWithColumns(35));
13435 verifyFormat("someFunction(Param, Param,\n"
13436 " {List1, List2,\n"
13437 " List3});",
13438 getLLVMStyleWithColumns(35));
13439 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
13440 " aaaaaaaaaaaaaaaaaaaaaaa);");
13441 }
13442
13443 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
13444 FormatStyle DoNotMerge = getLLVMStyle();
13445 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
13446
13447 verifyFormat("void f() { return 42; }");
13448 verifyFormat("void f() {\n"
13449 " return 42;\n"
13450 "}",
13451 DoNotMerge);
13452 verifyFormat("void f() {\n"
13453 " // Comment\n"
13454 "}");
13455 verifyFormat("{\n"
13456 "#error {\n"
13457 " int a;\n"
13458 "}");
13459 verifyFormat("{\n"
13460 " int a;\n"
13461 "#error {\n"
13462 "}");
13463 verifyFormat("void f() {} // comment");
13464 verifyFormat("void f() { int a; } // comment");
13465 verifyFormat("void f() {\n"
13466 "} // comment",
13467 DoNotMerge);
13468 verifyFormat("void f() {\n"
13469 " int a;\n"
13470 "} // comment",
13471 DoNotMerge);
13472 verifyFormat("void f() {\n"
13473 "} // comment",
13474 getLLVMStyleWithColumns(15));
13475
13476 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
13477 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
13478
13479 verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
13480 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
13481 verifyFormat("class C {\n"
13482 " C()\n"
13483 " : iiiiiiii(nullptr),\n"
13484 " kkkkkkk(nullptr),\n"
13485 " mmmmmmm(nullptr),\n"
13486 " nnnnnnn(nullptr) {}\n"
13487 "};",
13488 getGoogleStyle());
13489
13490 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
13491 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
13492 EXPECT_EQ("class C {\n"
13493 " A() : b(0) {}\n"
13494 "};",
13495 format("class C{A():b(0){}};", NoColumnLimit));
13496 EXPECT_EQ("A()\n"
13497 " : b(0) {\n"
13498 "}",
13499 format("A()\n:b(0)\n{\n}", NoColumnLimit));
13500
13501 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
13502 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
13503 FormatStyle::SFS_None;
13504 EXPECT_EQ("A()\n"
13505 " : b(0) {\n"
13506 "}",
13507 format("A():b(0){}", DoNotMergeNoColumnLimit));
13508 EXPECT_EQ("A()\n"
13509 " : b(0) {\n"
13510 "}",
13511 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
13512
13513 verifyFormat("#define A \\\n"
13514 " void f() { \\\n"
13515 " int i; \\\n"
13516 " }",
13517 getLLVMStyleWithColumns(20));
13518 verifyFormat("#define A \\\n"
13519 " void f() { int i; }",
13520 getLLVMStyleWithColumns(21));
13521 verifyFormat("#define A \\\n"
13522 " void f() { \\\n"
13523 " int i; \\\n"
13524 " } \\\n"
13525 " int j;",
13526 getLLVMStyleWithColumns(22));
13527 verifyFormat("#define A \\\n"
13528 " void f() { int i; } \\\n"
13529 " int j;",
13530 getLLVMStyleWithColumns(23));
13531 }
13532
13533 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
13534 FormatStyle MergeEmptyOnly = getLLVMStyle();
13535 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
13536 verifyFormat("class C {\n"
13537 " int f() {}\n"
13538 "};",
13539 MergeEmptyOnly);
13540 verifyFormat("class C {\n"
13541 " int f() {\n"
13542 " return 42;\n"
13543 " }\n"
13544 "};",
13545 MergeEmptyOnly);
13546 verifyFormat("int f() {}", MergeEmptyOnly);
13547 verifyFormat("int f() {\n"
13548 " return 42;\n"
13549 "}",
13550 MergeEmptyOnly);
13551
13552 // Also verify behavior when BraceWrapping.AfterFunction = true
13553 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
13554 MergeEmptyOnly.BraceWrapping.AfterFunction = true;
13555 verifyFormat("int f() {}", MergeEmptyOnly);
13556 verifyFormat("class C {\n"
13557 " int f() {}\n"
13558 "};",
13559 MergeEmptyOnly);
13560 }
13561
13562 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
13563 FormatStyle MergeInlineOnly = getLLVMStyle();
13564 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
13565 verifyFormat("class C {\n"
13566 " int f() { return 42; }\n"
13567 "};",
13568 MergeInlineOnly);
13569 verifyFormat("int f() {\n"
13570 " return 42;\n"
13571 "}",
13572 MergeInlineOnly);
13573
13574 // SFS_Inline implies SFS_Empty
13575 verifyFormat("class C {\n"
13576 " int f() {}\n"
13577 "};",
13578 MergeInlineOnly);
13579 verifyFormat("int f() {}", MergeInlineOnly);
13580 // https://llvm.org/PR54147
13581 verifyFormat("auto lambda = []() {\n"
13582 " // comment\n"
13583 " f();\n"
13584 " g();\n"
13585 "};",
13586 MergeInlineOnly);
13587
13588 verifyFormat("class C {\n"
13589 "#ifdef A\n"
13590 " int f() { return 42; }\n"
13591 "#endif\n"
13592 "};",
13593 MergeInlineOnly);
13594
13595 verifyFormat("struct S {\n"
13596 "// comment\n"
13597 "#ifdef FOO\n"
13598 " int foo() { bar(); }\n"
13599 "#endif\n"
13600 "};",
13601 MergeInlineOnly);
13602
13603 // Also verify behavior when BraceWrapping.AfterFunction = true
13604 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
13605 MergeInlineOnly.BraceWrapping.AfterFunction = true;
13606 verifyFormat("class C {\n"
13607 " int f() { return 42; }\n"
13608 "};",
13609 MergeInlineOnly);
13610 verifyFormat("int f()\n"
13611 "{\n"
13612 " return 42;\n"
13613 "}",
13614 MergeInlineOnly);
13615
13616 // SFS_Inline implies SFS_Empty
13617 verifyFormat("int f() {}", MergeInlineOnly);
13618 verifyFormat("class C {\n"
13619 " int f() {}\n"
13620 "};",
13621 MergeInlineOnly);
13622
13623 MergeInlineOnly.BraceWrapping.AfterClass = true;
13624 MergeInlineOnly.BraceWrapping.AfterStruct = true;
13625 verifyFormat("class C\n"
13626 "{\n"
13627 " int f() { return 42; }\n"
13628 "};",
13629 MergeInlineOnly);
13630 verifyFormat("struct C\n"
13631 "{\n"
13632 " int f() { return 42; }\n"
13633 "};",
13634 MergeInlineOnly);
13635 verifyFormat("int f()\n"
13636 "{\n"
13637 " return 42;\n"
13638 "}",
13639 MergeInlineOnly);
13640 verifyFormat("int f() {}", MergeInlineOnly);
13641 verifyFormat("class C\n"
13642 "{\n"
13643 " int f() { return 42; }\n"
13644 "};",
13645 MergeInlineOnly);
13646 verifyFormat("struct C\n"
13647 "{\n"
13648 " int f() { return 42; }\n"
13649 "};",
13650 MergeInlineOnly);
13651 verifyFormat("struct C\n"
13652 "// comment\n"
13653 "/* comment */\n"
13654 "// comment\n"
13655 "{\n"
13656 " int f() { return 42; }\n"
13657 "};",
13658 MergeInlineOnly);
13659 verifyFormat("/* comment */ struct C\n"
13660 "{\n"
13661 " int f() { return 42; }\n"
13662 "};",
13663 MergeInlineOnly);
13664 }
13665
13666 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
13667 FormatStyle MergeInlineOnly = getLLVMStyle();
13668 MergeInlineOnly.AllowShortFunctionsOnASingleLine =
13669 FormatStyle::SFS_InlineOnly;
13670 verifyFormat("class C {\n"
13671 " int f() { return 42; }\n"
13672 "};",
13673 MergeInlineOnly);
13674 verifyFormat("int f() {\n"
13675 " return 42;\n"
13676 "}",
13677 MergeInlineOnly);
13678
13679 // SFS_InlineOnly does not imply SFS_Empty
13680 verifyFormat("class C {\n"
13681 " int f() {}\n"
13682 "};",
13683 MergeInlineOnly);
13684 verifyFormat("int f() {\n"
13685 "}",
13686 MergeInlineOnly);
13687
13688 // Also verify behavior when BraceWrapping.AfterFunction = true
13689 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
13690 MergeInlineOnly.BraceWrapping.AfterFunction = true;
13691 verifyFormat("class C {\n"
13692 " int f() { return 42; }\n"
13693 "};",
13694 MergeInlineOnly);
13695 verifyFormat("int f()\n"
13696 "{\n"
13697 " return 42;\n"
13698 "}",
13699 MergeInlineOnly);
13700
13701 // SFS_InlineOnly does not imply SFS_Empty
13702 verifyFormat("int f()\n"
13703 "{\n"
13704 "}",
13705 MergeInlineOnly);
13706 verifyFormat("class C {\n"
13707 " int f() {}\n"
13708 "};",
13709 MergeInlineOnly);
13710 }
13711
13712 TEST_F(FormatTest, SplitEmptyFunction) {
13713 FormatStyle Style = getLLVMStyleWithColumns(40);
13714 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
13715 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
13716 Style.BraceWrapping.AfterFunction = true;
13717 Style.BraceWrapping.SplitEmptyFunction = false;
13718
13719 verifyFormat("int f()\n"
13720 "{}",
13721 Style);
13722 verifyFormat("int f()\n"
13723 "{\n"
13724 " return 42;\n"
13725 "}",
13726 Style);
13727 verifyFormat("int f()\n"
13728 "{\n"
13729 " // some comment\n"
13730 "}",
13731 Style);
13732
13733 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
13734 verifyFormat("int f() {}", Style);
13735 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
13736 "{}",
13737 Style);
13738 verifyFormat("int f()\n"
13739 "{\n"
13740 " return 0;\n"
13741 "}",
13742 Style);
13743
13744 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
13745 verifyFormat("class Foo {\n"
13746 " int f() {}\n"
13747 "};\n",
13748 Style);
13749 verifyFormat("class Foo {\n"
13750 " int f() { return 0; }\n"
13751 "};\n",
13752 Style);
13753 verifyFormat("class Foo {\n"
13754 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
13755 " {}\n"
13756 "};\n",
13757 Style);
13758 verifyFormat("class Foo {\n"
13759 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
13760 " {\n"
13761 " return 0;\n"
13762 " }\n"
13763 "};\n",
13764 Style);
13765
13766 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
13767 verifyFormat("int f() {}", Style);
13768 verifyFormat("int f() { return 0; }", Style);
13769 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
13770 "{}",
13771 Style);
13772 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
13773 "{\n"
13774 " return 0;\n"
13775 "}",
13776 Style);
13777 }
13778
13779 TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) {
13780 FormatStyle Style = getLLVMStyleWithColumns(40);
13781 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
13782 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
13783 Style.BraceWrapping.AfterFunction = true;
13784 Style.BraceWrapping.SplitEmptyFunction = true;
13785 Style.BraceWrapping.SplitEmptyRecord = false;
13786
13787 verifyFormat("class C {};", Style);
13788 verifyFormat("struct C {};", Style);
13789 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
13790 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
13791 "{\n"
13792 "}",
13793 Style);
13794 verifyFormat("class C {\n"
13795 " C()\n"
13796 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n"
13797 " bbbbbbbbbbbbbbbbbbb()\n"
13798 " {\n"
13799 " }\n"
13800 " void\n"
13801 " m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
13802 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
13803 " {\n"
13804 " }\n"
13805 "};",
13806 Style);
13807 }
13808
13809 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
13810 FormatStyle Style = getLLVMStyle();
13811 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
13812 verifyFormat("#ifdef A\n"
13813 "int f() {}\n"
13814 "#else\n"
13815 "int g() {}\n"
13816 "#endif",
13817 Style);
13818 }
13819
13820 TEST_F(FormatTest, SplitEmptyClass) {
13821 FormatStyle Style = getLLVMStyle();
13822 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
13823 Style.BraceWrapping.AfterClass = true;
13824 Style.BraceWrapping.SplitEmptyRecord = false;
13825
13826 verifyFormat("class Foo\n"
13827 "{};",
13828 Style);
13829 verifyFormat("/* something */ class Foo\n"
13830 "{};",
13831 Style);
13832 verifyFormat("template <typename X> class Foo\n"
13833 "{};",
13834 Style);
13835 verifyFormat("class Foo\n"
13836 "{\n"
13837 " Foo();\n"
13838 "};",
13839 Style);
13840 verifyFormat("typedef class Foo\n"
13841 "{\n"
13842 "} Foo_t;",
13843 Style);
13844
13845 Style.BraceWrapping.SplitEmptyRecord = true;
13846 Style.BraceWrapping.AfterStruct = true;
13847 verifyFormat("class rep\n"
13848 "{\n"
13849 "};",
13850 Style);
13851 verifyFormat("struct rep\n"
13852 "{\n"
13853 "};",
13854 Style);
13855 verifyFormat("template <typename T> class rep\n"
13856 "{\n"
13857 "};",
13858 Style);
13859 verifyFormat("template <typename T> struct rep\n"
13860 "{\n"
13861 "};",
13862 Style);
13863 verifyFormat("class rep\n"
13864 "{\n"
13865 " int x;\n"
13866 "};",
13867 Style);
13868 verifyFormat("struct rep\n"
13869 "{\n"
13870 " int x;\n"
13871 "};",
13872 Style);
13873 verifyFormat("template <typename T> class rep\n"
13874 "{\n"
13875 " int x;\n"
13876 "};",
13877 Style);
13878 verifyFormat("template <typename T> struct rep\n"
13879 "{\n"
13880 " int x;\n"
13881 "};",
13882 Style);
13883 verifyFormat("template <typename T> class rep // Foo\n"
13884 "{\n"
13885 " int x;\n"
13886 "};",
13887 Style);
13888 verifyFormat("template <typename T> struct rep // Bar\n"
13889 "{\n"
13890 " int x;\n"
13891 "};",
13892 Style);
13893
13894 verifyFormat("template <typename T> class rep<T>\n"
13895 "{\n"
13896 " int x;\n"
13897 "};",
13898 Style);
13899
13900 verifyFormat("template <typename T> class rep<std::complex<T>>\n"
13901 "{\n"
13902 " int x;\n"
13903 "};",
13904 Style);
13905 verifyFormat("template <typename T> class rep<std::complex<T>>\n"
13906 "{\n"
13907 "};",
13908 Style);
13909
13910 verifyFormat("#include \"stdint.h\"\n"
13911 "namespace rep {}",
13912 Style);
13913 verifyFormat("#include <stdint.h>\n"
13914 "namespace rep {}",
13915 Style);
13916 verifyFormat("#include <stdint.h>\n"
13917 "namespace rep {}",
13918 "#include <stdint.h>\n"
13919 "namespace rep {\n"
13920 "\n"
13921 "\n"
13922 "}",
13923 Style);
13924 }
13925
13926 TEST_F(FormatTest, SplitEmptyStruct) {
13927 FormatStyle Style = getLLVMStyle();
13928 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
13929 Style.BraceWrapping.AfterStruct = true;
13930 Style.BraceWrapping.SplitEmptyRecord = false;
13931
13932 verifyFormat("struct Foo\n"
13933 "{};",
13934 Style);
13935 verifyFormat("/* something */ struct Foo\n"
13936 "{};",
13937 Style);
13938 verifyFormat("template <typename X> struct Foo\n"
13939 "{};",
13940 Style);
13941 verifyFormat("struct Foo\n"
13942 "{\n"
13943 " Foo();\n"
13944 "};",
13945 Style);
13946 verifyFormat("typedef struct Foo\n"
13947 "{\n"
13948 "} Foo_t;",
13949 Style);
13950 // typedef struct Bar {} Bar_t;
13951 }
13952
13953 TEST_F(FormatTest, SplitEmptyUnion) {
13954 FormatStyle Style = getLLVMStyle();
13955 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
13956 Style.BraceWrapping.AfterUnion = true;
13957 Style.BraceWrapping.SplitEmptyRecord = false;
13958
13959 verifyFormat("union Foo\n"
13960 "{};",
13961 Style);
13962 verifyFormat("/* something */ union Foo\n"
13963 "{};",
13964 Style);
13965 verifyFormat("union Foo\n"
13966 "{\n"
13967 " A,\n"
13968 "};",
13969 Style);
13970 verifyFormat("typedef union Foo\n"
13971 "{\n"
13972 "} Foo_t;",
13973 Style);
13974 }
13975
13976 TEST_F(FormatTest, SplitEmptyNamespace) {
13977 FormatStyle Style = getLLVMStyle();
13978 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
13979 Style.BraceWrapping.AfterNamespace = true;
13980 Style.BraceWrapping.SplitEmptyNamespace = false;
13981
13982 verifyFormat("namespace Foo\n"
13983 "{};",
13984 Style);
13985 verifyFormat("/* something */ namespace Foo\n"
13986 "{};",
13987 Style);
13988 verifyFormat("inline namespace Foo\n"
13989 "{};",
13990 Style);
13991 verifyFormat("/* something */ inline namespace Foo\n"
13992 "{};",
13993 Style);
13994 verifyFormat("export namespace Foo\n"
13995 "{};",
13996 Style);
13997 verifyFormat("namespace Foo\n"
13998 "{\n"
13999 "void Bar();\n"
14000 "};",
14001 Style);
14002 }
14003
14004 TEST_F(FormatTest, NeverMergeShortRecords) {
14005 FormatStyle Style = getLLVMStyle();
14006
14007 verifyFormat("class Foo {\n"
14008 " Foo();\n"
14009 "};",
14010 Style);
14011 verifyFormat("typedef class Foo {\n"
14012 " Foo();\n"
14013 "} Foo_t;",
14014 Style);
14015 verifyFormat("struct Foo {\n"
14016 " Foo();\n"
14017 "};",
14018 Style);
14019 verifyFormat("typedef struct Foo {\n"
14020 " Foo();\n"
14021 "} Foo_t;",
14022 Style);
14023 verifyFormat("union Foo {\n"
14024 " A,\n"
14025 "};",
14026 Style);
14027 verifyFormat("typedef union Foo {\n"
14028 " A,\n"
14029 "} Foo_t;",
14030 Style);
14031 verifyFormat("namespace Foo {\n"
14032 "void Bar();\n"
14033 "};",
14034 Style);
14035
14036 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14037 Style.BraceWrapping.AfterClass = true;
14038 Style.BraceWrapping.AfterStruct = true;
14039 Style.BraceWrapping.AfterUnion = true;
14040 Style.BraceWrapping.AfterNamespace = true;
14041 verifyFormat("class Foo\n"
14042 "{\n"
14043 " Foo();\n"
14044 "};",
14045 Style);
14046 verifyFormat("typedef class Foo\n"
14047 "{\n"
14048 " Foo();\n"
14049 "} Foo_t;",
14050 Style);
14051 verifyFormat("struct Foo\n"
14052 "{\n"
14053 " Foo();\n"
14054 "};",
14055 Style);
14056 verifyFormat("typedef struct Foo\n"
14057 "{\n"
14058 " Foo();\n"
14059 "} Foo_t;",
14060 Style);
14061 verifyFormat("union Foo\n"
14062 "{\n"
14063 " A,\n"
14064 "};",
14065 Style);
14066 verifyFormat("typedef union Foo\n"
14067 "{\n"
14068 " A,\n"
14069 "} Foo_t;",
14070 Style);
14071 verifyFormat("namespace Foo\n"
14072 "{\n"
14073 "void Bar();\n"
14074 "};",
14075 Style);
14076 }
14077
14078 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
14079 // Elaborate type variable declarations.
14080 verifyFormat("struct foo a = {bar};\nint n;");
14081 verifyFormat("class foo a = {bar};\nint n;");
14082 verifyFormat("union foo a = {bar};\nint n;");
14083
14084 // Elaborate types inside function definitions.
14085 verifyFormat("struct foo f() {}\nint n;");
14086 verifyFormat("class foo f() {}\nint n;");
14087 verifyFormat("union foo f() {}\nint n;");
14088
14089 // Templates.
14090 verifyFormat("template <class X> void f() {}\nint n;");
14091 verifyFormat("template <struct X> void f() {}\nint n;");
14092 verifyFormat("template <union X> void f() {}\nint n;");
14093
14094 // Actual definitions...
14095 verifyFormat("struct {\n} n;");
14096 verifyFormat(
14097 "template <template <class T, class Y>, class Z> class X {\n} n;");
14098 verifyFormat("union Z {\n int n;\n} x;");
14099 verifyFormat("class MACRO Z {\n} n;");
14100 verifyFormat("class MACRO(X) Z {\n} n;");
14101 verifyFormat("class __attribute__(X) Z {\n} n;");
14102 verifyFormat("class __declspec(X) Z {\n} n;");
14103 verifyFormat("class A##B##C {\n} n;");
14104 verifyFormat("class alignas(16) Z {\n} n;");
14105 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
14106 verifyFormat("class MACROA MACRO(X) Z {\n} n;");
14107
14108 // Redefinition from nested context:
14109 verifyFormat("class A::B::C {\n} n;");
14110
14111 // Template definitions.
14112 verifyFormat(
14113 "template <typename F>\n"
14114 "Matcher(const Matcher<F> &Other,\n"
14115 " typename enable_if_c<is_base_of<F, T>::value &&\n"
14116 " !is_same<F, T>::value>::type * = 0)\n"
14117 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
14118
14119 // FIXME: This is still incorrectly handled at the formatter side.
14120 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
14121 verifyFormat("int i = SomeFunction(a<b, a> b);");
14122
14123 // FIXME:
14124 // This now gets parsed incorrectly as class definition.
14125 // verifyFormat("class A<int> f() {\n}\nint n;");
14126
14127 // Elaborate types where incorrectly parsing the structural element would
14128 // break the indent.
14129 verifyFormat("if (true)\n"
14130 " class X x;\n"
14131 "else\n"
14132 " f();\n");
14133
14134 // This is simply incomplete. Formatting is not important, but must not crash.
14135 verifyFormat("class A:");
14136 }
14137
14138 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
14139 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n",
14140 format("#error Leave all white!!!!! space* alone!\n"));
14141 EXPECT_EQ(
14142 "#warning Leave all white!!!!! space* alone!\n",
14143 format("#warning Leave all white!!!!! space* alone!\n"));
14144 EXPECT_EQ("#error 1", format(" # error 1"));
14145 EXPECT_EQ("#warning 1", format(" # warning 1"));
14146 }
14147
14148 TEST_F(FormatTest, FormatHashIfExpressions) {
14149 verifyFormat("#if AAAA && BBBB");
14150 verifyFormat("#if (AAAA && BBBB)");
14151 verifyFormat("#elif (AAAA && BBBB)");
14152 // FIXME: Come up with a better indentation for #elif.
14153 verifyFormat(
14154 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
14155 " defined(BBBBBBBB)\n"
14156 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
14157 " defined(BBBBBBBB)\n"
14158 "#endif",
14159 getLLVMStyleWithColumns(65));
14160 }
14161
14162 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
14163 FormatStyle AllowsMergedIf = getGoogleStyle();
14164 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
14165 FormatStyle::SIS_WithoutElse;
14166 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
14167 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
14168 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
14169 EXPECT_EQ("if (true) return 42;",
14170 format("if (true)\nreturn 42;", AllowsMergedIf));
14171 FormatStyle ShortMergedIf = AllowsMergedIf;
14172 ShortMergedIf.ColumnLimit = 25;
14173 verifyFormat("#define A \\\n"
14174 " if (true) return 42;",
14175 ShortMergedIf);
14176 verifyFormat("#define A \\\n"
14177 " f(); \\\n"
14178 " if (true)\n"
14179 "#define B",
14180 ShortMergedIf);
14181 verifyFormat("#define A \\\n"
14182 " f(); \\\n"
14183 " if (true)\n"
14184 "g();",
14185 ShortMergedIf);
14186 verifyFormat("{\n"
14187 "#ifdef A\n"
14188 " // Comment\n"
14189 " if (true) continue;\n"
14190 "#endif\n"
14191 " // Comment\n"
14192 " if (true) continue;\n"
14193 "}",
14194 ShortMergedIf);
14195 ShortMergedIf.ColumnLimit = 33;
14196 verifyFormat("#define A \\\n"
14197 " if constexpr (true) return 42;",
14198 ShortMergedIf);
14199 verifyFormat("#define A \\\n"
14200 " if CONSTEXPR (true) return 42;",
14201 ShortMergedIf);
14202 ShortMergedIf.ColumnLimit = 29;
14203 verifyFormat("#define A \\\n"
14204 " if (aaaaaaaaaa) return 1; \\\n"
14205 " return 2;",
14206 ShortMergedIf);
14207 ShortMergedIf.ColumnLimit = 28;
14208 verifyFormat("#define A \\\n"
14209 " if (aaaaaaaaaa) \\\n"
14210 " return 1; \\\n"
14211 " return 2;",
14212 ShortMergedIf);
14213 verifyFormat("#define A \\\n"
14214 " if constexpr (aaaaaaa) \\\n"
14215 " return 1; \\\n"
14216 " return 2;",
14217 ShortMergedIf);
14218 verifyFormat("#define A \\\n"
14219 " if CONSTEXPR (aaaaaaa) \\\n"
14220 " return 1; \\\n"
14221 " return 2;",
14222 ShortMergedIf);
14223
14224 verifyFormat("//\n"
14225 "#define a \\\n"
14226 " if \\\n"
14227 " 0",
14228 getChromiumStyle(FormatStyle::LK_Cpp));
14229 }
14230
14231 TEST_F(FormatTest, FormatStarDependingOnContext) {
14232 verifyFormat("void f(int *a);");
14233 verifyFormat("void f() { f(fint * b); }");
14234 verifyFormat("class A {\n void f(int *a);\n};");
14235 verifyFormat("class A {\n int *a;\n};");
14236 verifyFormat("namespace a {\n"
14237 "namespace b {\n"
14238 "class A {\n"
14239 " void f() {}\n"
14240 " int *a;\n"
14241 "};\n"
14242 "} // namespace b\n"
14243 "} // namespace a");
14244 }
14245
14246 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
14247 verifyFormat("while");
14248 verifyFormat("operator");
14249 }
14250
14251 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
14252 // This code would be painfully slow to format if we didn't skip it.
14253 std::string Code("A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" // 20x
14254 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
14255 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
14256 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
14257 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
14258 "A(1, 1)\n"
14259 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
14260 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
14261 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
14262 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
14263 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
14264 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
14265 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
14266 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
14267 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
14268 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
14269 // Deeply nested part is untouched, rest is formatted.
14270 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
14271 format(std::string("int i;\n") + Code + "int j;\n",
14272 getLLVMStyle(), SC_ExpectIncomplete));
14273 }
14274
14275 //===----------------------------------------------------------------------===//
14276 // Objective-C tests.
14277 //===----------------------------------------------------------------------===//
14278
14279 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
14280 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
14281 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
14282 format("-(NSUInteger)indexOfObject:(id)anObject;"));
14283 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
14284 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
14285 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
14286 format("-(NSInteger)Method3:(id)anObject;"));
14287 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
14288 format("-(NSInteger)Method4:(id)anObject;"));
14289 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
14290 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
14291 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
14292 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
14293 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
14294 "forAllCells:(BOOL)flag;",
14295 format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
14296 "forAllCells:(BOOL)flag;"));
14297
14298 // Very long objectiveC method declaration.
14299 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
14300 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
14301 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
14302 " inRange:(NSRange)range\n"
14303 " outRange:(NSRange)out_range\n"
14304 " outRange1:(NSRange)out_range1\n"
14305 " outRange2:(NSRange)out_range2\n"
14306 " outRange3:(NSRange)out_range3\n"
14307 " outRange4:(NSRange)out_range4\n"
14308 " outRange5:(NSRange)out_range5\n"
14309 " outRange6:(NSRange)out_range6\n"
14310 " outRange7:(NSRange)out_range7\n"
14311 " outRange8:(NSRange)out_range8\n"
14312 " outRange9:(NSRange)out_range9;");
14313
14314 // When the function name has to be wrapped.
14315 FormatStyle Style = getLLVMStyle();
14316 // ObjC ignores IndentWrappedFunctionNames when wrapping methods
14317 // and always indents instead.
14318 Style.IndentWrappedFunctionNames = false;
14319 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
14320 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
14321 " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
14322 "}",
14323 Style);
14324 Style.IndentWrappedFunctionNames = true;
14325 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
14326 " veryLooooooooooongName:(NSString)cccccccccccccc\n"
14327 " anotherName:(NSString)dddddddddddddd {\n"
14328 "}",
14329 Style);
14330
14331 verifyFormat("- (int)sum:(vector<int>)numbers;");
14332 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
14333 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
14334 // protocol lists (but not for template classes):
14335 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
14336
14337 verifyFormat("- (int (*)())foo:(int (*)())f;");
14338 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
14339
14340 // If there's no return type (very rare in practice!), LLVM and Google style
14341 // agree.
14342 verifyFormat("- foo;");
14343 verifyFormat("- foo:(int)f;");
14344 verifyGoogleFormat("- foo:(int)foo;");
14345 }
14346
14347 TEST_F(FormatTest, BreaksStringLiterals) {
14348 EXPECT_EQ("\"some text \"\n"
14349 "\"other\";",
14350 format("\"some text other\";", getLLVMStyleWithColumns(12)));
14351 EXPECT_EQ("\"some text \"\n"
14352 "\"other\";",
14353 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
14354 EXPECT_EQ(
14355 "#define A \\\n"
14356 " \"some \" \\\n"
14357 " \"text \" \\\n"
14358 " \"other\";",
14359 format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
14360 EXPECT_EQ(
14361 "#define A \\\n"
14362 " \"so \" \\\n"
14363 " \"text \" \\\n"
14364 " \"other\";",
14365 format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
14366
14367 EXPECT_EQ("\"some text\"",
14368 format("\"some text\"", getLLVMStyleWithColumns(1)));
14369 EXPECT_EQ("\"some text\"",
14370 format("\"some text\"", getLLVMStyleWithColumns(11)));
14371 EXPECT_EQ("\"some \"\n"
14372 "\"text\"",
14373 format("\"some text\"", getLLVMStyleWithColumns(10)));
14374 EXPECT_EQ("\"some \"\n"
14375 "\"text\"",
14376 format("\"some text\"", getLLVMStyleWithColumns(7)));
14377 EXPECT_EQ("\"some\"\n"
14378 "\" tex\"\n"
14379 "\"t\"",
14380 format("\"some text\"", getLLVMStyleWithColumns(6)));
14381 EXPECT_EQ("\"some\"\n"
14382 "\" tex\"\n"
14383 "\" and\"",
14384 format("\"some tex and\"", getLLVMStyleWithColumns(6)));
14385 EXPECT_EQ("\"some\"\n"
14386 "\"/tex\"\n"
14387 "\"/and\"",
14388 format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
14389
14390 EXPECT_EQ("variable =\n"
14391 " \"long string \"\n"
14392 " \"literal\";",
14393 format("variable = \"long string literal\";",
14394 getLLVMStyleWithColumns(20)));
14395
14396 EXPECT_EQ("variable = f(\n"
14397 " \"long string \"\n"
14398 " \"literal\",\n"
14399 " short,\n"
14400 " loooooooooooooooooooong);",
14401 format("variable = f(\"long string literal\", short, "
14402 "loooooooooooooooooooong);",
14403 getLLVMStyleWithColumns(20)));
14404
14405 EXPECT_EQ(
14406 "f(g(\"long string \"\n"
14407 " \"literal\"),\n"
14408 " b);",
14409 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
14410 EXPECT_EQ("f(g(\"long string \"\n"
14411 " \"literal\",\n"
14412 " a),\n"
14413 " b);",
14414 format("f(g(\"long string literal\", a), b);",
14415 getLLVMStyleWithColumns(20)));
14416 EXPECT_EQ(
14417 "f(\"one two\".split(\n"
14418 " variable));",
14419 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
14420 EXPECT_EQ("f(\"one two three four five six \"\n"
14421 " \"seven\".split(\n"
14422 " really_looooong_variable));",
14423 format("f(\"one two three four five six seven\"."
14424 "split(really_looooong_variable));",
14425 getLLVMStyleWithColumns(33)));
14426
14427 EXPECT_EQ("f(\"some \"\n"
14428 " \"text\",\n"
14429 " other);",
14430 format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
14431
14432 // Only break as a last resort.
14433 verifyFormat(
14434 "aaaaaaaaaaaaaaaaaaaa(\n"
14435 " aaaaaaaaaaaaaaaaaaaa,\n"
14436 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
14437
14438 EXPECT_EQ("\"splitmea\"\n"
14439 "\"trandomp\"\n"
14440 "\"oint\"",
14441 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
14442
14443 EXPECT_EQ("\"split/\"\n"
14444 "\"pathat/\"\n"
14445 "\"slashes\"",
14446 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
14447
14448 EXPECT_EQ("\"split/\"\n"
14449 "\"pathat/\"\n"
14450 "\"slashes\"",
14451 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
14452 EXPECT_EQ("\"split at \"\n"
14453 "\"spaces/at/\"\n"
14454 "\"slashes.at.any$\"\n"
14455 "\"non-alphanumeric%\"\n"
14456 "\"1111111111characte\"\n"
14457 "\"rs\"",
14458 format("\"split at "
14459 "spaces/at/"
14460 "slashes.at."
14461 "any$non-"
14462 "alphanumeric%"
14463 "1111111111characte"
14464 "rs\"",
14465 getLLVMStyleWithColumns(20)));
14466
14467 // Verify that splitting the strings understands
14468 // Style::AlwaysBreakBeforeMultilineStrings.
14469 EXPECT_EQ("aaaaaaaaaaaa(\n"
14470 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
14471 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
14472 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
14473 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
14474 "aaaaaaaaaaaaaaaaaaaaaa\");",
14475 getGoogleStyle()));
14476 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
14477 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
14478 format("return \"aaaaaaaaaaaaaaaaaaaaaa "
14479 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
14480 "aaaaaaaaaaaaaaaaaaaaaa\";",
14481 getGoogleStyle()));
14482 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
14483 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
14484 format("llvm::outs() << "
14485 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
14486 "aaaaaaaaaaaaaaaaaaa\";"));
14487 EXPECT_EQ("ffff(\n"
14488 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
14489 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
14490 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
14491 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
14492 getGoogleStyle()));
14493
14494 FormatStyle Style = getLLVMStyleWithColumns(12);
14495 Style.BreakStringLiterals = false;
14496 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
14497
14498 FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
14499 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
14500 EXPECT_EQ("#define A \\\n"
14501 " \"some \" \\\n"
14502 " \"text \" \\\n"
14503 " \"other\";",
14504 format("#define A \"some text other\";", AlignLeft));
14505 }
14506
14507 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
14508 EXPECT_EQ("C a = \"some more \"\n"
14509 " \"text\";",
14510 format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
14511 }
14512
14513 TEST_F(FormatTest, FullyRemoveEmptyLines) {
14514 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
14515 NoEmptyLines.MaxEmptyLinesToKeep = 0;
14516 EXPECT_EQ("int i = a(b());",
14517 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
14518 }
14519
14520 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
14521 EXPECT_EQ(
14522 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
14523 "(\n"
14524 " \"x\t\");",
14525 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
14526 "aaaaaaa("
14527 "\"x\t\");"));
14528 }
14529
14530 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
14531 EXPECT_EQ(
14532 "u8\"utf8 string \"\n"
14533 "u8\"literal\";",
14534 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
14535 EXPECT_EQ(
14536 "u\"utf16 string \"\n"
14537 "u\"literal\";",
14538 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
14539 EXPECT_EQ(
14540 "U\"utf32 string \"\n"
14541 "U\"literal\";",
14542 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
14543 EXPECT_EQ("L\"wide string \"\n"
14544 "L\"literal\";",
14545 format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
14546 EXPECT_EQ("@\"NSString \"\n"
14547 "@\"literal\";",
14548 format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
14549 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
14550
14551 // This input makes clang-format try to split the incomplete unicode escape
14552 // sequence, which used to lead to a crasher.
14553 verifyNoCrash(
14554 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
14555 getLLVMStyleWithColumns(60));
14556 }
14557
14558 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
14559 FormatStyle Style = getGoogleStyleWithColumns(15);
14560 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
14561 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
14562 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
14563 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
14564 EXPECT_EQ("u8R\"x(raw literal)x\";",
14565 format("u8R\"x(raw literal)x\";", Style));
14566 }
14567
14568 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
14569 FormatStyle Style = getLLVMStyleWithColumns(20);
14570 EXPECT_EQ(
14571 "_T(\"aaaaaaaaaaaaaa\")\n"
14572 "_T(\"aaaaaaaaaaaaaa\")\n"
14573 "_T(\"aaaaaaaaaaaa\")",
14574 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
14575 EXPECT_EQ("f(x,\n"
14576 " _T(\"aaaaaaaaaaaa\")\n"
14577 " _T(\"aaa\"),\n"
14578 " z);",
14579 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
14580
14581 // FIXME: Handle embedded spaces in one iteration.
14582 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
14583 // "_T(\"aaaaaaaaaaaaa\")\n"
14584 // "_T(\"aaaaaaaaaaaaa\")\n"
14585 // "_T(\"a\")",
14586 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
14587 // getLLVMStyleWithColumns(20)));
14588 EXPECT_EQ(
14589 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
14590 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
14591 EXPECT_EQ("f(\n"
14592 "#if !TEST\n"
14593 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
14594 "#endif\n"
14595 ");",
14596 format("f(\n"
14597 "#if !TEST\n"
14598 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
14599 "#endif\n"
14600 ");"));
14601 EXPECT_EQ("f(\n"
14602 "\n"
14603 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
14604 format("f(\n"
14605 "\n"
14606 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
14607 // Regression test for accessing tokens past the end of a vector in the
14608 // TokenLexer.
14609 verifyNoCrash(R"(_T(
14610 "
14611 )
14612 )");
14613 }
14614
14615 TEST_F(FormatTest, BreaksStringLiteralOperands) {
14616 // In a function call with two operands, the second can be broken with no line
14617 // break before it.
14618 EXPECT_EQ(
14619 "func(a, \"long long \"\n"
14620 " \"long long\");",
14621 format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24)));
14622 // In a function call with three operands, the second must be broken with a
14623 // line break before it.
14624 EXPECT_EQ("func(a,\n"
14625 " \"long long long \"\n"
14626 " \"long\",\n"
14627 " c);",
14628 format("func(a, \"long long long long\", c);",
14629 getLLVMStyleWithColumns(24)));
14630 // In a function call with three operands, the third must be broken with a
14631 // line break before it.
14632 EXPECT_EQ("func(a, b,\n"
14633 " \"long long long \"\n"
14634 " \"long\");",
14635 format("func(a, b, \"long long long long\");",
14636 getLLVMStyleWithColumns(24)));
14637 // In a function call with three operands, both the second and the third must
14638 // be broken with a line break before them.
14639 EXPECT_EQ("func(a,\n"
14640 " \"long long long \"\n"
14641 " \"long\",\n"
14642 " \"long long long \"\n"
14643 " \"long\");",
14644 format("func(a, \"long long long long\", \"long long long long\");",
14645 getLLVMStyleWithColumns(24)));
14646 // In a chain of << with two operands, the second can be broken with no line
14647 // break before it.
14648 EXPECT_EQ("a << \"line line \"\n"
14649 " \"line\";",
14650 format("a << \"line line line\";", getLLVMStyleWithColumns(20)));
14651 // In a chain of << with three operands, the second can be broken with no line
14652 // break before it.
14653 EXPECT_EQ(
14654 "abcde << \"line \"\n"
14655 " \"line line\"\n"
14656 " << c;",
14657 format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20)));
14658 // In a chain of << with three operands, the third must be broken with a line
14659 // break before it.
14660 EXPECT_EQ(
14661 "a << b\n"
14662 " << \"line line \"\n"
14663 " \"line\";",
14664 format("a << b << \"line line line\";", getLLVMStyleWithColumns(20)));
14665 // In a chain of << with three operands, the second can be broken with no line
14666 // break before it and the third must be broken with a line break before it.
14667 EXPECT_EQ("abcd << \"line line \"\n"
14668 " \"line\"\n"
14669 " << \"line line \"\n"
14670 " \"line\";",
14671 format("abcd << \"line line line\" << \"line line line\";",
14672 getLLVMStyleWithColumns(20)));
14673 // In a chain of binary operators with two operands, the second can be broken
14674 // with no line break before it.
14675 EXPECT_EQ(
14676 "abcd + \"line line \"\n"
14677 " \"line line\";",
14678 format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20)));
14679 // In a chain of binary operators with three operands, the second must be
14680 // broken with a line break before it.
14681 EXPECT_EQ("abcd +\n"
14682 " \"line line \"\n"
14683 " \"line line\" +\n"
14684 " e;",
14685 format("abcd + \"line line line line\" + e;",
14686 getLLVMStyleWithColumns(20)));
14687 // In a function call with two operands, with AlignAfterOpenBracket enabled,
14688 // the first must be broken with a line break before it.
14689 FormatStyle Style = getLLVMStyleWithColumns(25);
14690 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
14691 EXPECT_EQ("someFunction(\n"
14692 " \"long long long \"\n"
14693 " \"long\",\n"
14694 " a);",
14695 format("someFunction(\"long long long long\", a);", Style));
14696 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
14697 verifyFormat("someFunction(\n"
14698 " \"long long long \"\n"
14699 " \"long\",\n"
14700 " a\n"
14701 ");",
14702 Style);
14703 }
14704
14705 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
14706 EXPECT_EQ(
14707 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
14708 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
14709 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
14710 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
14711 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
14712 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
14713 }
14714
14715 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
14716 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
14717 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()));
14718 EXPECT_EQ("fffffffffff(g(R\"x(\n"
14719 "multiline raw string literal xxxxxxxxxxxxxx\n"
14720 ")x\",\n"
14721 " a),\n"
14722 " b);",
14723 format("fffffffffff(g(R\"x(\n"
14724 "multiline raw string literal xxxxxxxxxxxxxx\n"
14725 ")x\", a), b);",
14726 getGoogleStyleWithColumns(20)));
14727 EXPECT_EQ("fffffffffff(\n"
14728 " g(R\"x(qqq\n"
14729 "multiline raw string literal xxxxxxxxxxxxxx\n"
14730 ")x\",\n"
14731 " a),\n"
14732 " b);",
14733 format("fffffffffff(g(R\"x(qqq\n"
14734 "multiline raw string literal xxxxxxxxxxxxxx\n"
14735 ")x\", a), b);",
14736 getGoogleStyleWithColumns(20)));
14737
14738 EXPECT_EQ("fffffffffff(R\"x(\n"
14739 "multiline raw string literal xxxxxxxxxxxxxx\n"
14740 ")x\");",
14741 format("fffffffffff(R\"x(\n"
14742 "multiline raw string literal xxxxxxxxxxxxxx\n"
14743 ")x\");",
14744 getGoogleStyleWithColumns(20)));
14745 EXPECT_EQ("fffffffffff(R\"x(\n"
14746 "multiline raw string literal xxxxxxxxxxxxxx\n"
14747 ")x\" + bbbbbb);",
14748 format("fffffffffff(R\"x(\n"
14749 "multiline raw string literal xxxxxxxxxxxxxx\n"
14750 ")x\" + bbbbbb);",
14751 getGoogleStyleWithColumns(20)));
14752 EXPECT_EQ("fffffffffff(\n"
14753 " R\"x(\n"
14754 "multiline raw string literal xxxxxxxxxxxxxx\n"
14755 ")x\" +\n"
14756 " bbbbbb);",
14757 format("fffffffffff(\n"
14758 " R\"x(\n"
14759 "multiline raw string literal xxxxxxxxxxxxxx\n"
14760 ")x\" + bbbbbb);",
14761 getGoogleStyleWithColumns(20)));
14762 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
14763 format("fffffffffff(\n"
14764 " R\"(single line raw string)\" + bbbbbb);"));
14765 }
14766
14767 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
14768 verifyFormat("string a = \"unterminated;");
14769 EXPECT_EQ("function(\"unterminated,\n"
14770 " OtherParameter);",
14771 format("function( \"unterminated,\n"
14772 " OtherParameter);"));
14773 }
14774
14775 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
14776 FormatStyle Style = getLLVMStyle();
14777 Style.Standard = FormatStyle::LS_Cpp03;
14778 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
14779 format("#define x(_a) printf(\"foo\"_a);", Style));
14780 }
14781
14782 TEST_F(FormatTest, CppLexVersion) {
14783 FormatStyle Style = getLLVMStyle();
14784 // Formatting of x * y differs if x is a type.
14785 verifyFormat("void foo() { MACRO(a * b); }", Style);
14786 verifyFormat("void foo() { MACRO(int *b); }", Style);
14787
14788 // LLVM style uses latest lexer.
14789 verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
14790 Style.Standard = FormatStyle::LS_Cpp17;
14791 // But in c++17, char8_t isn't a keyword.
14792 verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
14793 }
14794
14795 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
14796
14797 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
14798 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
14799 " \"ddeeefff\");",
14800 format("someFunction(\"aaabbbcccdddeeefff\");",
14801 getLLVMStyleWithColumns(25)));
14802 EXPECT_EQ("someFunction1234567890(\n"
14803 " \"aaabbbcccdddeeefff\");",
14804 format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
14805 getLLVMStyleWithColumns(26)));
14806 EXPECT_EQ("someFunction1234567890(\n"
14807 " \"aaabbbcccdddeeeff\"\n"
14808 " \"f\");",
14809 format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
14810 getLLVMStyleWithColumns(25)));
14811 EXPECT_EQ("someFunction1234567890(\n"
14812 " \"aaabbbcccdddeeeff\"\n"
14813 " \"f\");",
14814 format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
14815 getLLVMStyleWithColumns(24)));
14816 EXPECT_EQ("someFunction(\n"
14817 " \"aaabbbcc ddde \"\n"
14818 " \"efff\");",
14819 format("someFunction(\"aaabbbcc ddde efff\");",
14820 getLLVMStyleWithColumns(25)));
14821 EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
14822 " \"ddeeefff\");",
14823 format("someFunction(\"aaabbbccc ddeeefff\");",
14824 getLLVMStyleWithColumns(25)));
14825 EXPECT_EQ("someFunction1234567890(\n"
14826 " \"aaabb \"\n"
14827 " \"cccdddeeefff\");",
14828 format("someFunction1234567890(\"aaabb cccdddeeefff\");",
14829 getLLVMStyleWithColumns(25)));
14830 EXPECT_EQ("#define A \\\n"
14831 " string s = \\\n"
14832 " \"123456789\" \\\n"
14833 " \"0\"; \\\n"
14834 " int i;",
14835 format("#define A string s = \"1234567890\"; int i;",
14836 getLLVMStyleWithColumns(20)));
14837 EXPECT_EQ("someFunction(\n"
14838 " \"aaabbbcc \"\n"
14839 " \"dddeeefff\");",
14840 format("someFunction(\"aaabbbcc dddeeefff\");",
14841 getLLVMStyleWithColumns(25)));
14842 }
14843
14844 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
14845 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
14846 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
14847 EXPECT_EQ("\"test\"\n"
14848 "\"\\n\"",
14849 format("\"test\\n\"", getLLVMStyleWithColumns(7)));
14850 EXPECT_EQ("\"tes\\\\\"\n"
14851 "\"n\"",
14852 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
14853 EXPECT_EQ("\"\\\\\\\\\"\n"
14854 "\"\\n\"",
14855 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
14856 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
14857 EXPECT_EQ("\"\\uff01\"\n"
14858 "\"test\"",
14859 format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
14860 EXPECT_EQ("\"\\Uff01ff02\"",
14861 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
14862 EXPECT_EQ("\"\\x000000000001\"\n"
14863 "\"next\"",
14864 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
14865 EXPECT_EQ("\"\\x000000000001next\"",
14866 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
14867 EXPECT_EQ("\"\\x000000000001\"",
14868 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
14869 EXPECT_EQ("\"test\"\n"
14870 "\"\\000000\"\n"
14871 "\"000001\"",
14872 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
14873 EXPECT_EQ("\"test\\000\"\n"
14874 "\"00000000\"\n"
14875 "\"1\"",
14876 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
14877 }
14878
14879 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
14880 verifyFormat("void f() {\n"
14881 " return g() {}\n"
14882 " void h() {}");
14883 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
14884 "g();\n"
14885 "}");
14886 }
14887
14888 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
14889 verifyFormat(
14890 "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
14891 }
14892
14893 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
14894 verifyFormat("class X {\n"
14895 " void f() {\n"
14896 " }\n"
14897 "};",
14898 getLLVMStyleWithColumns(12));
14899 }
14900
14901 TEST_F(FormatTest, ConfigurableIndentWidth) {
14902 FormatStyle EightIndent = getLLVMStyleWithColumns(18);
14903 EightIndent.IndentWidth = 8;
14904 EightIndent.ContinuationIndentWidth = 8;
14905 verifyFormat("void f() {\n"
14906 " someFunction();\n"
14907 " if (true) {\n"
14908 " f();\n"
14909 " }\n"
14910 "}",
14911 EightIndent);
14912 verifyFormat("class X {\n"
14913 " void f() {\n"
14914 " }\n"
14915 "};",
14916 EightIndent);
14917 verifyFormat("int x[] = {\n"
14918 " call(),\n"
14919 " call()};",
14920 EightIndent);
14921 }
14922
14923 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
14924 verifyFormat("double\n"
14925 "f();",
14926 getLLVMStyleWithColumns(8));
14927 }
14928
14929 TEST_F(FormatTest, ConfigurableUseOfTab) {
14930 FormatStyle Tab = getLLVMStyleWithColumns(42);
14931 Tab.IndentWidth = 8;
14932 Tab.UseTab = FormatStyle::UT_Always;
14933 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
14934
14935 EXPECT_EQ("if (aaaaaaaa && // q\n"
14936 " bb)\t\t// w\n"
14937 "\t;",
14938 format("if (aaaaaaaa &&// q\n"
14939 "bb)// w\n"
14940 ";",
14941 Tab));
14942 EXPECT_EQ("if (aaa && bbb) // w\n"
14943 "\t;",
14944 format("if(aaa&&bbb)// w\n"
14945 ";",
14946 Tab));
14947
14948 verifyFormat("class X {\n"
14949 "\tvoid f() {\n"
14950 "\t\tsomeFunction(parameter1,\n"
14951 "\t\t\t parameter2);\n"
14952 "\t}\n"
14953 "};",
14954 Tab);
14955 verifyFormat("#define A \\\n"
14956 "\tvoid f() { \\\n"
14957 "\t\tsomeFunction( \\\n"
14958 "\t\t parameter1, \\\n"
14959 "\t\t parameter2); \\\n"
14960 "\t}",
14961 Tab);
14962 verifyFormat("int a;\t // x\n"
14963 "int bbbbbbbb; // x\n",
14964 Tab);
14965
14966 FormatStyle TabAlignment = Tab;
14967 TabAlignment.AlignConsecutiveDeclarations.Enabled = true;
14968 TabAlignment.PointerAlignment = FormatStyle::PAS_Left;
14969 verifyFormat("unsigned long long big;\n"
14970 "char*\t\t ptr;",
14971 TabAlignment);
14972 TabAlignment.PointerAlignment = FormatStyle::PAS_Middle;
14973 verifyFormat("unsigned long long big;\n"
14974 "char *\t\t ptr;",
14975 TabAlignment);
14976 TabAlignment.PointerAlignment = FormatStyle::PAS_Right;
14977 verifyFormat("unsigned long long big;\n"
14978 "char\t\t *ptr;",
14979 TabAlignment);
14980
14981 Tab.TabWidth = 4;
14982 Tab.IndentWidth = 8;
14983 verifyFormat("class TabWidth4Indent8 {\n"
14984 "\t\tvoid f() {\n"
14985 "\t\t\t\tsomeFunction(parameter1,\n"
14986 "\t\t\t\t\t\t\t parameter2);\n"
14987 "\t\t}\n"
14988 "};",
14989 Tab);
14990
14991 Tab.TabWidth = 4;
14992 Tab.IndentWidth = 4;
14993 verifyFormat("class TabWidth4Indent4 {\n"
14994 "\tvoid f() {\n"
14995 "\t\tsomeFunction(parameter1,\n"
14996 "\t\t\t\t\t parameter2);\n"
14997 "\t}\n"
14998 "};",
14999 Tab);
15000
15001 Tab.TabWidth = 8;
15002 Tab.IndentWidth = 4;
15003 verifyFormat("class TabWidth8Indent4 {\n"
15004 " void f() {\n"
15005 "\tsomeFunction(parameter1,\n"
15006 "\t\t parameter2);\n"
15007 " }\n"
15008 "};",
15009 Tab);
15010
15011 Tab.TabWidth = 8;
15012 Tab.IndentWidth = 8;
15013 EXPECT_EQ("/*\n"
15014 "\t a\t\tcomment\n"
15015 "\t in multiple lines\n"
15016 " */",
15017 format(" /*\t \t \n"
15018 " \t \t a\t\tcomment\t \t\n"
15019 " \t \t in multiple lines\t\n"
15020 " \t */",
15021 Tab));
15022
15023 TabAlignment.UseTab = FormatStyle::UT_ForIndentation;
15024 TabAlignment.PointerAlignment = FormatStyle::PAS_Left;
15025 verifyFormat("void f() {\n"
15026 "\tunsigned long long big;\n"
15027 "\tchar* ptr;\n"
15028 "}",
15029 TabAlignment);
15030 TabAlignment.PointerAlignment = FormatStyle::PAS_Middle;
15031 verifyFormat("void f() {\n"
15032 "\tunsigned long long big;\n"
15033 "\tchar * ptr;\n"
15034 "}",
15035 TabAlignment);
15036 TabAlignment.PointerAlignment = FormatStyle::PAS_Right;
15037 verifyFormat("void f() {\n"
15038 "\tunsigned long long big;\n"
15039 "\tchar *ptr;\n"
15040 "}",
15041 TabAlignment);
15042
15043 Tab.UseTab = FormatStyle::UT_ForIndentation;
15044 verifyFormat("{\n"
15045 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15046 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15047 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15048 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15049 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15050 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15051 "};",
15052 Tab);
15053 verifyFormat("enum AA {\n"
15054 "\ta1, // Force multiple lines\n"
15055 "\ta2,\n"
15056 "\ta3\n"
15057 "};",
15058 Tab);
15059 EXPECT_EQ("if (aaaaaaaa && // q\n"
15060 " bb) // w\n"
15061 "\t;",
15062 format("if (aaaaaaaa &&// q\n"
15063 "bb)// w\n"
15064 ";",
15065 Tab));
15066 verifyFormat("class X {\n"
15067 "\tvoid f() {\n"
15068 "\t\tsomeFunction(parameter1,\n"
15069 "\t\t parameter2);\n"
15070 "\t}\n"
15071 "};",
15072 Tab);
15073 verifyFormat("{\n"
15074 "\tQ(\n"
15075 "\t {\n"
15076 "\t\t int a;\n"
15077 "\t\t someFunction(aaaaaaaa,\n"
15078 "\t\t bbbbbbb);\n"
15079 "\t },\n"
15080 "\t p);\n"
15081 "}",
15082 Tab);
15083 EXPECT_EQ("{\n"
15084 "\t/* aaaa\n"
15085 "\t bbbb */\n"
15086 "}",
15087 format("{\n"
15088 "/* aaaa\n"
15089 " bbbb */\n"
15090 "}",
15091 Tab));
15092 EXPECT_EQ("{\n"
15093 "\t/*\n"
15094 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15095 "\t bbbbbbbbbbbbb\n"
15096 "\t*/\n"
15097 "}",
15098 format("{\n"
15099 "/*\n"
15100 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15101 "*/\n"
15102 "}",
15103 Tab));
15104 EXPECT_EQ("{\n"
15105 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15106 "\t// bbbbbbbbbbbbb\n"
15107 "}",
15108 format("{\n"
15109 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15110 "}",
15111 Tab));
15112 EXPECT_EQ("{\n"
15113 "\t/*\n"
15114 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15115 "\t bbbbbbbbbbbbb\n"
15116 "\t*/\n"
15117 "}",
15118 format("{\n"
15119 "\t/*\n"
15120 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15121 "\t*/\n"
15122 "}",
15123 Tab));
15124 EXPECT_EQ("{\n"
15125 "\t/*\n"
15126 "\n"
15127 "\t*/\n"
15128 "}",
15129 format("{\n"
15130 "\t/*\n"
15131 "\n"
15132 "\t*/\n"
15133 "}",
15134 Tab));
15135 EXPECT_EQ("{\n"
15136 "\t/*\n"
15137 " asdf\n"
15138 "\t*/\n"
15139 "}",
15140 format("{\n"
15141 "\t/*\n"
15142 " asdf\n"
15143 "\t*/\n"
15144 "}",
15145 Tab));
15146
15147 verifyFormat("void f() {\n"
15148 "\treturn true ? aaaaaaaaaaaaaaaaaa\n"
15149 "\t : bbbbbbbbbbbbbbbbbb\n"
15150 "}",
15151 Tab);
15152 FormatStyle TabNoBreak = Tab;
15153 TabNoBreak.BreakBeforeTernaryOperators = false;
15154 verifyFormat("void f() {\n"
15155 "\treturn true ? aaaaaaaaaaaaaaaaaa :\n"
15156 "\t bbbbbbbbbbbbbbbbbb\n"
15157 "}",
15158 TabNoBreak);
15159 verifyFormat("void f() {\n"
15160 "\treturn true ?\n"
15161 "\t aaaaaaaaaaaaaaaaaaaa :\n"
15162 "\t bbbbbbbbbbbbbbbbbbbb\n"
15163 "}",
15164 TabNoBreak);
15165
15166 Tab.UseTab = FormatStyle::UT_Never;
15167 EXPECT_EQ("/*\n"
15168 " a\t\tcomment\n"
15169 " in multiple lines\n"
15170 " */",
15171 format(" /*\t \t \n"
15172 " \t \t a\t\tcomment\t \t\n"
15173 " \t \t in multiple lines\t\n"
15174 " \t */",
15175 Tab));
15176 EXPECT_EQ("/* some\n"
15177 " comment */",
15178 format(" \t \t /* some\n"
15179 " \t \t comment */",
15180 Tab));
15181 EXPECT_EQ("int a; /* some\n"
15182 " comment */",
15183 format(" \t \t int a; /* some\n"
15184 " \t \t comment */",
15185 Tab));
15186
15187 EXPECT_EQ("int a; /* some\n"
15188 "comment */",
15189 format(" \t \t int\ta; /* some\n"
15190 " \t \t comment */",
15191 Tab));
15192 EXPECT_EQ("f(\"\t\t\"); /* some\n"
15193 " comment */",
15194 format(" \t \t f(\"\t\t\"); /* some\n"
15195 " \t \t comment */",
15196 Tab));
15197 EXPECT_EQ("{\n"
15198 " /*\n"
15199 " * Comment\n"
15200 " */\n"
15201 " int i;\n"
15202 "}",
15203 format("{\n"
15204 "\t/*\n"
15205 "\t * Comment\n"
15206 "\t */\n"
15207 "\t int i;\n"
15208 "}",
15209 Tab));
15210
15211 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
15212 Tab.TabWidth = 8;
15213 Tab.IndentWidth = 8;
15214 EXPECT_EQ("if (aaaaaaaa && // q\n"
15215 " bb) // w\n"
15216 "\t;",
15217 format("if (aaaaaaaa &&// q\n"
15218 "bb)// w\n"
15219 ";",
15220 Tab));
15221 EXPECT_EQ("if (aaa && bbb) // w\n"
15222 "\t;",
15223 format("if(aaa&&bbb)// w\n"
15224 ";",
15225 Tab));
15226 verifyFormat("class X {\n"
15227 "\tvoid f() {\n"
15228 "\t\tsomeFunction(parameter1,\n"
15229 "\t\t\t parameter2);\n"
15230 "\t}\n"
15231 "};",
15232 Tab);
15233 verifyFormat("#define A \\\n"
15234 "\tvoid f() { \\\n"
15235 "\t\tsomeFunction( \\\n"
15236 "\t\t parameter1, \\\n"
15237 "\t\t parameter2); \\\n"
15238 "\t}",
15239 Tab);
15240 Tab.TabWidth = 4;
15241 Tab.IndentWidth = 8;
15242 verifyFormat("class TabWidth4Indent8 {\n"
15243 "\t\tvoid f() {\n"
15244 "\t\t\t\tsomeFunction(parameter1,\n"
15245 "\t\t\t\t\t\t\t parameter2);\n"
15246 "\t\t}\n"
15247 "};",
15248 Tab);
15249 Tab.TabWidth = 4;
15250 Tab.IndentWidth = 4;
15251 verifyFormat("class TabWidth4Indent4 {\n"
15252 "\tvoid f() {\n"
15253 "\t\tsomeFunction(parameter1,\n"
15254 "\t\t\t\t\t parameter2);\n"
15255 "\t}\n"
15256 "};",
15257 Tab);
15258 Tab.TabWidth = 8;
15259 Tab.IndentWidth = 4;
15260 verifyFormat("class TabWidth8Indent4 {\n"
15261 " void f() {\n"
15262 "\tsomeFunction(parameter1,\n"
15263 "\t\t parameter2);\n"
15264 " }\n"
15265 "};",
15266 Tab);
15267 Tab.TabWidth = 8;
15268 Tab.IndentWidth = 8;
15269 EXPECT_EQ("/*\n"
15270 "\t a\t\tcomment\n"
15271 "\t in multiple lines\n"
15272 " */",
15273 format(" /*\t \t \n"
15274 " \t \t a\t\tcomment\t \t\n"
15275 " \t \t in multiple lines\t\n"
15276 " \t */",
15277 Tab));
15278 verifyFormat("{\n"
15279 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15280 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15281 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15282 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15283 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15284 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15285 "};",
15286 Tab);
15287 verifyFormat("enum AA {\n"
15288 "\ta1, // Force multiple lines\n"
15289 "\ta2,\n"
15290 "\ta3\n"
15291 "};",
15292 Tab);
15293 EXPECT_EQ("if (aaaaaaaa && // q\n"
15294 " bb) // w\n"
15295 "\t;",
15296 format("if (aaaaaaaa &&// q\n"
15297 "bb)// w\n"
15298 ";",
15299 Tab));
15300 verifyFormat("class X {\n"
15301 "\tvoid f() {\n"
15302 "\t\tsomeFunction(parameter1,\n"
15303 "\t\t\t parameter2);\n"
15304 "\t}\n"
15305 "};",
15306 Tab);
15307 verifyFormat("{\n"
15308 "\tQ(\n"
15309 "\t {\n"
15310 "\t\t int a;\n"
15311 "\t\t someFunction(aaaaaaaa,\n"
15312 "\t\t\t\t bbbbbbb);\n"
15313 "\t },\n"
15314 "\t p);\n"
15315 "}",
15316 Tab);
15317 EXPECT_EQ("{\n"
15318 "\t/* aaaa\n"
15319 "\t bbbb */\n"
15320 "}",
15321 format("{\n"
15322 "/* aaaa\n"
15323 " bbbb */\n"
15324 "}",
15325 Tab));
15326 EXPECT_EQ("{\n"
15327 "\t/*\n"
15328 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15329 "\t bbbbbbbbbbbbb\n"
15330 "\t*/\n"
15331 "}",
15332 format("{\n"
15333 "/*\n"
15334 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15335 "*/\n"
15336 "}",
15337 Tab));
15338 EXPECT_EQ("{\n"
15339 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15340 "\t// bbbbbbbbbbbbb\n"
15341 "}",
15342 format("{\n"
15343 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15344 "}",
15345 Tab));
15346 EXPECT_EQ("{\n"
15347 "\t/*\n"
15348 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15349 "\t bbbbbbbbbbbbb\n"
15350 "\t*/\n"
15351 "}",
15352 format("{\n"
15353 "\t/*\n"
15354 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15355 "\t*/\n"
15356 "}",
15357 Tab));
15358 EXPECT_EQ("{\n"
15359 "\t/*\n"
15360 "\n"
15361 "\t*/\n"
15362 "}",
15363 format("{\n"
15364 "\t/*\n"
15365 "\n"
15366 "\t*/\n"
15367 "}",
15368 Tab));
15369 EXPECT_EQ("{\n"
15370 "\t/*\n"
15371 " asdf\n"
15372 "\t*/\n"
15373 "}",
15374 format("{\n"
15375 "\t/*\n"
15376 " asdf\n"
15377 "\t*/\n"
15378 "}",
15379 Tab));
15380 EXPECT_EQ("/* some\n"
15381 " comment */",
15382 format(" \t \t /* some\n"
15383 " \t \t comment */",
15384 Tab));
15385 EXPECT_EQ("int a; /* some\n"
15386 " comment */",
15387 format(" \t \t int a; /* some\n"
15388 " \t \t comment */",
15389 Tab));
15390 EXPECT_EQ("int a; /* some\n"
15391 "comment */",
15392 format(" \t \t int\ta; /* some\n"
15393 " \t \t comment */",
15394 Tab));
15395 EXPECT_EQ("f(\"\t\t\"); /* some\n"
15396 " comment */",
15397 format(" \t \t f(\"\t\t\"); /* some\n"
15398 " \t \t comment */",
15399 Tab));
15400 EXPECT_EQ("{\n"
15401 "\t/*\n"
15402 "\t * Comment\n"
15403 "\t */\n"
15404 "\tint i;\n"
15405 "}",
15406 format("{\n"
15407 "\t/*\n"
15408 "\t * Comment\n"
15409 "\t */\n"
15410 "\t int i;\n"
15411 "}",
15412 Tab));
15413 Tab.TabWidth = 2;
15414 Tab.IndentWidth = 2;
15415 EXPECT_EQ("{\n"
15416 "\t/* aaaa\n"
15417 "\t\t bbbb */\n"
15418 "}",
15419 format("{\n"
15420 "/* aaaa\n"
15421 "\t bbbb */\n"
15422 "}",
15423 Tab));
15424 EXPECT_EQ("{\n"
15425 "\t/*\n"
15426 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15427 "\t\tbbbbbbbbbbbbb\n"
15428 "\t*/\n"
15429 "}",
15430 format("{\n"
15431 "/*\n"
15432 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15433 "*/\n"
15434 "}",
15435 Tab));
15436 Tab.AlignConsecutiveAssignments.Enabled = true;
15437 Tab.AlignConsecutiveDeclarations.Enabled = true;
15438 Tab.TabWidth = 4;
15439 Tab.IndentWidth = 4;
15440 verifyFormat("class Assign {\n"
15441 "\tvoid f() {\n"
15442 "\t\tint x = 123;\n"
15443 "\t\tint random = 4;\n"
15444 "\t\tstd::string alphabet =\n"
15445 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
15446 "\t}\n"
15447 "};",
15448 Tab);
15449
15450 Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
15451 Tab.TabWidth = 8;
15452 Tab.IndentWidth = 8;
15453 EXPECT_EQ("if (aaaaaaaa && // q\n"
15454 " bb) // w\n"
15455 "\t;",
15456 format("if (aaaaaaaa &&// q\n"
15457 "bb)// w\n"
15458 ";",
15459 Tab));
15460 EXPECT_EQ("if (aaa && bbb) // w\n"
15461 "\t;",
15462 format("if(aaa&&bbb)// w\n"
15463 ";",
15464 Tab));
15465 verifyFormat("class X {\n"
15466 "\tvoid f() {\n"
15467 "\t\tsomeFunction(parameter1,\n"
15468 "\t\t parameter2);\n"
15469 "\t}\n"
15470 "};",
15471 Tab);
15472 verifyFormat("#define A \\\n"
15473 "\tvoid f() { \\\n"
15474 "\t\tsomeFunction( \\\n"
15475 "\t\t parameter1, \\\n"
15476 "\t\t parameter2); \\\n"
15477 "\t}",
15478 Tab);
15479 Tab.TabWidth = 4;
15480 Tab.IndentWidth = 8;
15481 verifyFormat("class TabWidth4Indent8 {\n"
15482 "\t\tvoid f() {\n"
15483 "\t\t\t\tsomeFunction(parameter1,\n"
15484 "\t\t\t\t parameter2);\n"
15485 "\t\t}\n"
15486 "};",
15487 Tab);
15488 Tab.TabWidth = 4;
15489 Tab.IndentWidth = 4;
15490 verifyFormat("class TabWidth4Indent4 {\n"
15491 "\tvoid f() {\n"
15492 "\t\tsomeFunction(parameter1,\n"
15493 "\t\t parameter2);\n"
15494 "\t}\n"
15495 "};",
15496 Tab);
15497 Tab.TabWidth = 8;
15498 Tab.IndentWidth = 4;
15499 verifyFormat("class TabWidth8Indent4 {\n"
15500 " void f() {\n"
15501 "\tsomeFunction(parameter1,\n"
15502 "\t parameter2);\n"
15503 " }\n"
15504 "};",
15505 Tab);
15506 Tab.TabWidth = 8;
15507 Tab.IndentWidth = 8;
15508 EXPECT_EQ("/*\n"
15509 " a\t\tcomment\n"
15510 " in multiple lines\n"
15511 " */",
15512 format(" /*\t \t \n"
15513 " \t \t a\t\tcomment\t \t\n"
15514 " \t \t in multiple lines\t\n"
15515 " \t */",
15516 Tab));
15517 verifyFormat("{\n"
15518 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15519 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15520 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15521 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15522 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15523 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15524 "};",
15525 Tab);
15526 verifyFormat("enum AA {\n"
15527 "\ta1, // Force multiple lines\n"
15528 "\ta2,\n"
15529 "\ta3\n"
15530 "};",
15531 Tab);
15532 EXPECT_EQ("if (aaaaaaaa && // q\n"
15533 " bb) // w\n"
15534 "\t;",
15535 format("if (aaaaaaaa &&// q\n"
15536 "bb)// w\n"
15537 ";",
15538 Tab));
15539 verifyFormat("class X {\n"
15540 "\tvoid f() {\n"
15541 "\t\tsomeFunction(parameter1,\n"
15542 "\t\t parameter2);\n"
15543 "\t}\n"
15544 "};",
15545 Tab);
15546 verifyFormat("{\n"
15547 "\tQ(\n"
15548 "\t {\n"
15549 "\t\t int a;\n"
15550 "\t\t someFunction(aaaaaaaa,\n"
15551 "\t\t bbbbbbb);\n"
15552 "\t },\n"
15553 "\t p);\n"
15554 "}",
15555 Tab);
15556 EXPECT_EQ("{\n"
15557 "\t/* aaaa\n"
15558 "\t bbbb */\n"
15559 "}",
15560 format("{\n"
15561 "/* aaaa\n"
15562 " bbbb */\n"
15563 "}",
15564 Tab));
15565 EXPECT_EQ("{\n"
15566 "\t/*\n"
15567 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15568 "\t bbbbbbbbbbbbb\n"
15569 "\t*/\n"
15570 "}",
15571 format("{\n"
15572 "/*\n"
15573 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15574 "*/\n"
15575 "}",
15576 Tab));
15577 EXPECT_EQ("{\n"
15578 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15579 "\t// bbbbbbbbbbbbb\n"
15580 "}",
15581 format("{\n"
15582 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15583 "}",
15584 Tab));
15585 EXPECT_EQ("{\n"
15586 "\t/*\n"
15587 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15588 "\t bbbbbbbbbbbbb\n"
15589 "\t*/\n"
15590 "}",
15591 format("{\n"
15592 "\t/*\n"
15593 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15594 "\t*/\n"
15595 "}",
15596 Tab));
15597 EXPECT_EQ("{\n"
15598 "\t/*\n"
15599 "\n"
15600 "\t*/\n"
15601 "}",
15602 format("{\n"
15603 "\t/*\n"
15604 "\n"
15605 "\t*/\n"
15606 "}",
15607 Tab));
15608 EXPECT_EQ("{\n"
15609 "\t/*\n"
15610 " asdf\n"
15611 "\t*/\n"
15612 "}",
15613 format("{\n"
15614 "\t/*\n"
15615 " asdf\n"
15616 "\t*/\n"
15617 "}",
15618 Tab));
15619 EXPECT_EQ("/* some\n"
15620 " comment */",
15621 format(" \t \t /* some\n"
15622 " \t \t comment */",
15623 Tab));
15624 EXPECT_EQ("int a; /* some\n"
15625 " comment */",
15626 format(" \t \t int a; /* some\n"
15627 " \t \t comment */",
15628 Tab));
15629 EXPECT_EQ("int a; /* some\n"
15630 "comment */",
15631 format(" \t \t int\ta; /* some\n"
15632 " \t \t comment */",
15633 Tab));
15634 EXPECT_EQ("f(\"\t\t\"); /* some\n"
15635 " comment */",
15636 format(" \t \t f(\"\t\t\"); /* some\n"
15637 " \t \t comment */",
15638 Tab));
15639 EXPECT_EQ("{\n"
15640 "\t/*\n"
15641 "\t * Comment\n"
15642 "\t */\n"
15643 "\tint i;\n"
15644 "}",
15645 format("{\n"
15646 "\t/*\n"
15647 "\t * Comment\n"
15648 "\t */\n"
15649 "\t int i;\n"
15650 "}",
15651 Tab));
15652 Tab.TabWidth = 2;
15653 Tab.IndentWidth = 2;
15654 EXPECT_EQ("{\n"
15655 "\t/* aaaa\n"
15656 "\t bbbb */\n"
15657 "}",
15658 format("{\n"
15659 "/* aaaa\n"
15660 " bbbb */\n"
15661 "}",
15662 Tab));
15663 EXPECT_EQ("{\n"
15664 "\t/*\n"
15665 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15666 "\t bbbbbbbbbbbbb\n"
15667 "\t*/\n"
15668 "}",
15669 format("{\n"
15670 "/*\n"
15671 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15672 "*/\n"
15673 "}",
15674 Tab));
15675 Tab.AlignConsecutiveAssignments.Enabled = true;
15676 Tab.AlignConsecutiveDeclarations.Enabled = true;
15677 Tab.TabWidth = 4;
15678 Tab.IndentWidth = 4;
15679 verifyFormat("class Assign {\n"
15680 "\tvoid f() {\n"
15681 "\t\tint x = 123;\n"
15682 "\t\tint random = 4;\n"
15683 "\t\tstd::string alphabet =\n"
15684 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
15685 "\t}\n"
15686 "};",
15687 Tab);
15688 Tab.AlignOperands = FormatStyle::OAS_Align;
15689 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
15690 " cccccccccccccccccccc;",
15691 Tab);
15692 // no alignment
15693 verifyFormat("int aaaaaaaaaa =\n"
15694 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
15695 Tab);
15696 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
15697 " : bbbbbbbbbbbbbb ? 222222222222222\n"
15698 " : 333333333333333;",
15699 Tab);
15700 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
15701 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
15702 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
15703 " + cccccccccccccccccccc;",
15704 Tab);
15705 }
15706
15707 TEST_F(FormatTest, ZeroTabWidth) {
15708 FormatStyle Tab = getLLVMStyleWithColumns(42);
15709 Tab.IndentWidth = 8;
15710 Tab.UseTab = FormatStyle::UT_Never;
15711 Tab.TabWidth = 0;
15712 EXPECT_EQ("void a(){\n"
15713 " // line starts with '\t'\n"
15714 "};",
15715 format("void a(){\n"
15716 "\t// line starts with '\t'\n"
15717 "};",
15718 Tab));
15719
15720 EXPECT_EQ("void a(){\n"
15721 " // line starts with '\t'\n"
15722 "};",
15723 format("void a(){\n"
15724 "\t\t// line starts with '\t'\n"
15725 "};",
15726 Tab));
15727
15728 Tab.UseTab = FormatStyle::UT_ForIndentation;
15729 EXPECT_EQ("void a(){\n"
15730 " // line starts with '\t'\n"
15731 "};",
15732 format("void a(){\n"
15733 "\t// line starts with '\t'\n"
15734 "};",
15735 Tab));
15736
15737 EXPECT_EQ("void a(){\n"
15738 " // line starts with '\t'\n"
15739 "};",
15740 format("void a(){\n"
15741 "\t\t// line starts with '\t'\n"
15742 "};",
15743 Tab));
15744
15745 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
15746 EXPECT_EQ("void a(){\n"
15747 " // line starts with '\t'\n"
15748 "};",
15749 format("void a(){\n"
15750 "\t// line starts with '\t'\n"
15751 "};",
15752 Tab));
15753
15754 EXPECT_EQ("void a(){\n"
15755 " // line starts with '\t'\n"
15756 "};",
15757 format("void a(){\n"
15758 "\t\t// line starts with '\t'\n"
15759 "};",
15760 Tab));
15761
15762 Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
15763 EXPECT_EQ("void a(){\n"
15764 " // line starts with '\t'\n"
15765 "};",
15766 format("void a(){\n"
15767 "\t// line starts with '\t'\n"
15768 "};",
15769 Tab));
15770
15771 EXPECT_EQ("void a(){\n"
15772 " // line starts with '\t'\n"
15773 "};",
15774 format("void a(){\n"
15775 "\t\t// line starts with '\t'\n"
15776 "};",
15777 Tab));
15778
15779 Tab.UseTab = FormatStyle::UT_Always;
15780 EXPECT_EQ("void a(){\n"
15781 "// line starts with '\t'\n"
15782 "};",
15783 format("void a(){\n"
15784 "\t// line starts with '\t'\n"
15785 "};",
15786 Tab));
15787
15788 EXPECT_EQ("void a(){\n"
15789 "// line starts with '\t'\n"
15790 "};",
15791 format("void a(){\n"
15792 "\t\t// line starts with '\t'\n"
15793 "};",
15794 Tab));
15795 }
15796
15797 TEST_F(FormatTest, CalculatesOriginalColumn) {
15798 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
15799 "q\"; /* some\n"
15800 " comment */",
15801 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
15802 "q\"; /* some\n"
15803 " comment */",
15804 getLLVMStyle()));
15805 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
15806 "/* some\n"
15807 " comment */",
15808 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
15809 " /* some\n"
15810 " comment */",
15811 getLLVMStyle()));
15812 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
15813 "qqq\n"
15814 "/* some\n"
15815 " comment */",
15816 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
15817 "qqq\n"
15818 " /* some\n"
15819 " comment */",
15820 getLLVMStyle()));
15821 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
15822 "wwww; /* some\n"
15823 " comment */",
15824 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
15825 "wwww; /* some\n"
15826 " comment */",
15827 getLLVMStyle()));
15828 }
15829
15830 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
15831 FormatStyle NoSpace = getLLVMStyle();
15832 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
15833
15834 verifyFormat("while(true)\n"
15835 " continue;",
15836 NoSpace);
15837 verifyFormat("for(;;)\n"
15838 " continue;",
15839 NoSpace);
15840 verifyFormat("if(true)\n"
15841 " f();\n"
15842 "else if(true)\n"
15843 " f();",
15844 NoSpace);
15845 verifyFormat("do {\n"
15846 " do_something();\n"
15847 "} while(something());",
15848 NoSpace);
15849 verifyFormat("switch(x) {\n"
15850 "default:\n"
15851 " break;\n"
15852 "}",
15853 NoSpace);
15854 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
15855 verifyFormat("size_t x = sizeof(x);", NoSpace);
15856 verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
15857 verifyFormat("auto f(int x) -> typeof(x);", NoSpace);
15858 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace);
15859 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace);
15860 verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
15861 verifyFormat("alignas(128) char a[128];", NoSpace);
15862 verifyFormat("size_t x = alignof(MyType);", NoSpace);
15863 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
15864 verifyFormat("int f() throw(Deprecated);", NoSpace);
15865 verifyFormat("typedef void (*cb)(int);", NoSpace);
15866 verifyFormat("T A::operator()();", NoSpace);
15867 verifyFormat("X A::operator++(T);", NoSpace);
15868 verifyFormat("auto lambda = []() { return 0; };", NoSpace);
15869
15870 FormatStyle Space = getLLVMStyle();
15871 Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
15872
15873 verifyFormat("int f ();", Space);
15874 verifyFormat("bool operator< ();", Space);
15875 verifyFormat("bool operator> ();", Space);
15876 verifyFormat("void f (int a, T b) {\n"
15877 " while (true)\n"
15878 " continue;\n"
15879 "}",
15880 Space);
15881 verifyFormat("if (true)\n"
15882 " f ();\n"
15883 "else if (true)\n"
15884 " f ();",
15885 Space);
15886 verifyFormat("do {\n"
15887 " do_something ();\n"
15888 "} while (something ());",
15889 Space);
15890 verifyFormat("switch (x) {\n"
15891 "default:\n"
15892 " break;\n"
15893 "}",
15894 Space);
15895 verifyFormat("A::A () : a (1) {}", Space);
15896 verifyFormat("void f () __attribute__ ((asdf));", Space);
15897 verifyFormat("*(&a + 1);\n"
15898 "&((&a)[1]);\n"
15899 "a[(b + c) * d];\n"
15900 "(((a + 1) * 2) + 3) * 4;",
15901 Space);
15902 verifyFormat("#define A(x) x", Space);
15903 verifyFormat("#define A (x) x", Space);
15904 verifyFormat("#if defined(x)\n"
15905 "#endif",
15906 Space);
15907 verifyFormat("auto i = std::make_unique<int> (5);", Space);
15908 verifyFormat("size_t x = sizeof (x);", Space);
15909 verifyFormat("auto f (int x) -> decltype (x);", Space);
15910 verifyFormat("auto f (int x) -> typeof (x);", Space);
15911 verifyFormat("auto f (int x) -> _Atomic (x);", Space);
15912 verifyFormat("auto f (int x) -> __underlying_type (x);", Space);
15913 verifyFormat("int f (T x) noexcept (x.create ());", Space);
15914 verifyFormat("alignas (128) char a[128];", Space);
15915 verifyFormat("size_t x = alignof (MyType);", Space);
15916 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
15917 verifyFormat("int f () throw (Deprecated);", Space);
15918 verifyFormat("typedef void (*cb) (int);", Space);
15919 // FIXME these tests regressed behaviour.
15920 // verifyFormat("T A::operator() ();", Space);
15921 // verifyFormat("X A::operator++ (T);", Space);
15922 verifyFormat("auto lambda = [] () { return 0; };", Space);
15923 verifyFormat("int x = int (y);", Space);
15924 verifyFormat("#define F(...) __VA_OPT__ (__VA_ARGS__)", Space);
15925 verifyFormat("__builtin_LINE ()", Space);
15926 verifyFormat("__builtin_UNKNOWN ()", Space);
15927
15928 FormatStyle SomeSpace = getLLVMStyle();
15929 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
15930
15931 verifyFormat("[]() -> float {}", SomeSpace);
15932 verifyFormat("[] (auto foo) {}", SomeSpace);
15933 verifyFormat("[foo]() -> int {}", SomeSpace);
15934 verifyFormat("int f();", SomeSpace);
15935 verifyFormat("void f (int a, T b) {\n"
15936 " while (true)\n"
15937 " continue;\n"
15938 "}",
15939 SomeSpace);
15940 verifyFormat("if (true)\n"
15941 " f();\n"
15942 "else if (true)\n"
15943 " f();",
15944 SomeSpace);
15945 verifyFormat("do {\n"
15946 " do_something();\n"
15947 "} while (something());",
15948 SomeSpace);
15949 verifyFormat("switch (x) {\n"
15950 "default:\n"
15951 " break;\n"
15952 "}",
15953 SomeSpace);
15954 verifyFormat("A::A() : a (1) {}", SomeSpace);
15955 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
15956 verifyFormat("*(&a + 1);\n"
15957 "&((&a)[1]);\n"
15958 "a[(b + c) * d];\n"
15959 "(((a + 1) * 2) + 3) * 4;",
15960 SomeSpace);
15961 verifyFormat("#define A(x) x", SomeSpace);
15962 verifyFormat("#define A (x) x", SomeSpace);
15963 verifyFormat("#if defined(x)\n"
15964 "#endif",
15965 SomeSpace);
15966 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
15967 verifyFormat("size_t x = sizeof (x);", SomeSpace);
15968 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
15969 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace);
15970 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace);
15971 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace);
15972 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
15973 verifyFormat("alignas (128) char a[128];", SomeSpace);
15974 verifyFormat("size_t x = alignof (MyType);", SomeSpace);
15975 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
15976 SomeSpace);
15977 verifyFormat("int f() throw (Deprecated);", SomeSpace);
15978 verifyFormat("typedef void (*cb) (int);", SomeSpace);
15979 verifyFormat("T A::operator()();", SomeSpace);
15980 // FIXME these tests regressed behaviour.
15981 // verifyFormat("X A::operator++ (T);", SomeSpace);
15982 verifyFormat("int x = int (y);", SomeSpace);
15983 verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
15984
15985 FormatStyle SpaceControlStatements = getLLVMStyle();
15986 SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom;
15987 SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true;
15988
15989 verifyFormat("while (true)\n"
15990 " continue;",
15991 SpaceControlStatements);
15992 verifyFormat("if (true)\n"
15993 " f();\n"
15994 "else if (true)\n"
15995 " f();",
15996 SpaceControlStatements);
15997 verifyFormat("for (;;) {\n"
15998 " do_something();\n"
15999 "}",
16000 SpaceControlStatements);
16001 verifyFormat("do {\n"
16002 " do_something();\n"
16003 "} while (something());",
16004 SpaceControlStatements);
16005 verifyFormat("switch (x) {\n"
16006 "default:\n"
16007 " break;\n"
16008 "}",
16009 SpaceControlStatements);
16010
16011 FormatStyle SpaceFuncDecl = getLLVMStyle();
16012 SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16013 SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true;
16014
16015 verifyFormat("int f ();", SpaceFuncDecl);
16016 verifyFormat("void f(int a, T b) {}", SpaceFuncDecl);
16017 verifyFormat("A::A() : a(1) {}", SpaceFuncDecl);
16018 verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl);
16019 verifyFormat("#define A(x) x", SpaceFuncDecl);
16020 verifyFormat("#define A (x) x", SpaceFuncDecl);
16021 verifyFormat("#if defined(x)\n"
16022 "#endif",
16023 SpaceFuncDecl);
16024 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl);
16025 verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl);
16026 verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl);
16027 verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl);
16028 verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl);
16029 verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl);
16030 verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl);
16031 verifyFormat("alignas(128) char a[128];", SpaceFuncDecl);
16032 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl);
16033 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
16034 SpaceFuncDecl);
16035 verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl);
16036 verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl);
16037 // FIXME these tests regressed behaviour.
16038 // verifyFormat("T A::operator() ();", SpaceFuncDecl);
16039 // verifyFormat("X A::operator++ (T);", SpaceFuncDecl);
16040 verifyFormat("T A::operator()() {}", SpaceFuncDecl);
16041 verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl);
16042 verifyFormat("int x = int(y);", SpaceFuncDecl);
16043 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
16044 SpaceFuncDecl);
16045
16046 FormatStyle SpaceFuncDef = getLLVMStyle();
16047 SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16048 SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true;
16049
16050 verifyFormat("int f();", SpaceFuncDef);
16051 verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
16052 verifyFormat("A::A() : a(1) {}", SpaceFuncDef);
16053 verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
16054 verifyFormat("#define A(x) x", SpaceFuncDef);
16055 verifyFormat("#define A (x) x", SpaceFuncDef);
16056 verifyFormat("#if defined(x)\n"
16057 "#endif",
16058 SpaceFuncDef);
16059 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef);
16060 verifyFormat("size_t x = sizeof(x);", SpaceFuncDef);
16061 verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef);
16062 verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef);
16063 verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef);
16064 verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef);
16065 verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef);
16066 verifyFormat("alignas(128) char a[128];", SpaceFuncDef);
16067 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef);
16068 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
16069 SpaceFuncDef);
16070 verifyFormat("int f() throw(Deprecated);", SpaceFuncDef);
16071 verifyFormat("typedef void (*cb)(int);", SpaceFuncDef);
16072 verifyFormat("T A::operator()();", SpaceFuncDef);
16073 verifyFormat("X A::operator++(T);", SpaceFuncDef);
16074 // verifyFormat("T A::operator() () {}", SpaceFuncDef);
16075 verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef);
16076 verifyFormat("int x = int(y);", SpaceFuncDef);
16077 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
16078 SpaceFuncDef);
16079
16080 FormatStyle SpaceIfMacros = getLLVMStyle();
16081 SpaceIfMacros.IfMacros.clear();
16082 SpaceIfMacros.IfMacros.push_back("MYIF");
16083 SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16084 SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true;
16085 verifyFormat("MYIF (a)\n return;", SpaceIfMacros);
16086 verifyFormat("MYIF (a)\n return;\nelse MYIF (b)\n return;", SpaceIfMacros);
16087 verifyFormat("MYIF (a)\n return;\nelse\n return;", SpaceIfMacros);
16088
16089 FormatStyle SpaceForeachMacros = getLLVMStyle();
16090 EXPECT_EQ(SpaceForeachMacros.AllowShortBlocksOnASingleLine,
16091 FormatStyle::SBS_Never);
16092 EXPECT_EQ(SpaceForeachMacros.AllowShortLoopsOnASingleLine, false);
16093 SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16094 SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true;
16095 verifyFormat("for (;;) {\n"
16096 "}",
16097 SpaceForeachMacros);
16098 verifyFormat("foreach (Item *item, itemlist) {\n"
16099 "}",
16100 SpaceForeachMacros);
16101 verifyFormat("Q_FOREACH (Item *item, itemlist) {\n"
16102 "}",
16103 SpaceForeachMacros);
16104 verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n"
16105 "}",
16106 SpaceForeachMacros);
16107 verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros);
16108
16109 FormatStyle SomeSpace2 = getLLVMStyle();
16110 SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16111 SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true;
16112 verifyFormat("[]() -> float {}", SomeSpace2);
16113 verifyFormat("[] (auto foo) {}", SomeSpace2);
16114 verifyFormat("[foo]() -> int {}", SomeSpace2);
16115 verifyFormat("int f();", SomeSpace2);
16116 verifyFormat("void f (int a, T b) {\n"
16117 " while (true)\n"
16118 " continue;\n"
16119 "}",
16120 SomeSpace2);
16121 verifyFormat("if (true)\n"
16122 " f();\n"
16123 "else if (true)\n"
16124 " f();",
16125 SomeSpace2);
16126 verifyFormat("do {\n"
16127 " do_something();\n"
16128 "} while (something());",
16129 SomeSpace2);
16130 verifyFormat("switch (x) {\n"
16131 "default:\n"
16132 " break;\n"
16133 "}",
16134 SomeSpace2);
16135 verifyFormat("A::A() : a (1) {}", SomeSpace2);
16136 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2);
16137 verifyFormat("*(&a + 1);\n"
16138 "&((&a)[1]);\n"
16139 "a[(b + c) * d];\n"
16140 "(((a + 1) * 2) + 3) * 4;",
16141 SomeSpace2);
16142 verifyFormat("#define A(x) x", SomeSpace2);
16143 verifyFormat("#define A (x) x", SomeSpace2);
16144 verifyFormat("#if defined(x)\n"
16145 "#endif",
16146 SomeSpace2);
16147 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2);
16148 verifyFormat("size_t x = sizeof (x);", SomeSpace2);
16149 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2);
16150 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2);
16151 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2);
16152 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2);
16153 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2);
16154 verifyFormat("alignas (128) char a[128];", SomeSpace2);
16155 verifyFormat("size_t x = alignof (MyType);", SomeSpace2);
16156 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
16157 SomeSpace2);
16158 verifyFormat("int f() throw (Deprecated);", SomeSpace2);
16159 verifyFormat("typedef void (*cb) (int);", SomeSpace2);
16160 verifyFormat("T A::operator()();", SomeSpace2);
16161 // verifyFormat("X A::operator++ (T);", SomeSpace2);
16162 verifyFormat("int x = int (y);", SomeSpace2);
16163 verifyFormat("auto lambda = []() { return 0; };", SomeSpace2);
16164
16165 FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle();
16166 SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16167 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
16168 .AfterOverloadedOperator = true;
16169
16170 verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator);
16171 verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator);
16172 verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator);
16173 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
16174
16175 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
16176 .AfterOverloadedOperator = false;
16177
16178 verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator);
16179 verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator);
16180 verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator);
16181 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
16182
16183 auto SpaceAfterRequires = getLLVMStyle();
16184 SpaceAfterRequires.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16185 EXPECT_FALSE(
16186 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause);
16187 EXPECT_FALSE(
16188 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression);
16189 verifyFormat("void f(auto x)\n"
16190 " requires requires(int i) { x + i; }\n"
16191 "{}",
16192 SpaceAfterRequires);
16193 verifyFormat("void f(auto x)\n"
16194 " requires(requires(int i) { x + i; })\n"
16195 "{}",
16196 SpaceAfterRequires);
16197 verifyFormat("if (requires(int i) { x + i; })\n"
16198 " return;",
16199 SpaceAfterRequires);
16200 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires);
16201 verifyFormat("template <typename T>\n"
16202 " requires(Foo<T>)\n"
16203 "class Bar;",
16204 SpaceAfterRequires);
16205
16206 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true;
16207 verifyFormat("void f(auto x)\n"
16208 " requires requires(int i) { x + i; }\n"
16209 "{}",
16210 SpaceAfterRequires);
16211 verifyFormat("void f(auto x)\n"
16212 " requires (requires(int i) { x + i; })\n"
16213 "{}",
16214 SpaceAfterRequires);
16215 verifyFormat("if (requires(int i) { x + i; })\n"
16216 " return;",
16217 SpaceAfterRequires);
16218 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires);
16219 verifyFormat("template <typename T>\n"
16220 " requires (Foo<T>)\n"
16221 "class Bar;",
16222 SpaceAfterRequires);
16223
16224 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = false;
16225 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression = true;
16226 verifyFormat("void f(auto x)\n"
16227 " requires requires (int i) { x + i; }\n"
16228 "{}",
16229 SpaceAfterRequires);
16230 verifyFormat("void f(auto x)\n"
16231 " requires(requires (int i) { x + i; })\n"
16232 "{}",
16233 SpaceAfterRequires);
16234 verifyFormat("if (requires (int i) { x + i; })\n"
16235 " return;",
16236 SpaceAfterRequires);
16237 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires);
16238 verifyFormat("template <typename T>\n"
16239 " requires(Foo<T>)\n"
16240 "class Bar;",
16241 SpaceAfterRequires);
16242
16243 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true;
16244 verifyFormat("void f(auto x)\n"
16245 " requires requires (int i) { x + i; }\n"
16246 "{}",
16247 SpaceAfterRequires);
16248 verifyFormat("void f(auto x)\n"
16249 " requires (requires (int i) { x + i; })\n"
16250 "{}",
16251 SpaceAfterRequires);
16252 verifyFormat("if (requires (int i) { x + i; })\n"
16253 " return;",
16254 SpaceAfterRequires);
16255 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires);
16256 verifyFormat("template <typename T>\n"
16257 " requires (Foo<T>)\n"
16258 "class Bar;",
16259 SpaceAfterRequires);
16260 }
16261
16262 TEST_F(FormatTest, SpaceAfterLogicalNot) {
16263 FormatStyle Spaces = getLLVMStyle();
16264 Spaces.SpaceAfterLogicalNot = true;
16265
16266 verifyFormat("bool x = ! y", Spaces);
16267 verifyFormat("if (! isFailure())", Spaces);
16268 verifyFormat("if (! (a && b))", Spaces);
16269 verifyFormat("\"Error!\"", Spaces);
16270 verifyFormat("! ! x", Spaces);
16271 }
16272
16273 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
16274 FormatStyle Spaces = getLLVMStyle();
16275
16276 Spaces.SpacesInParentheses = true;
16277 verifyFormat("do_something( ::globalVar );", Spaces);
16278 verifyFormat("call( x, y, z );", Spaces);
16279 verifyFormat("call();", Spaces);
16280 verifyFormat("std::function<void( int, int )> callback;", Spaces);
16281 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
16282 Spaces);
16283 verifyFormat("while ( (bool)1 )\n"
16284 " continue;",
16285 Spaces);
16286 verifyFormat("for ( ;; )\n"
16287 " continue;",
16288 Spaces);
16289 verifyFormat("if ( true )\n"
16290 " f();\n"
16291 "else if ( true )\n"
16292 " f();",
16293 Spaces);
16294 verifyFormat("do {\n"
16295 " do_something( (int)i );\n"
16296 "} while ( something() );",
16297 Spaces);
16298 verifyFormat("switch ( x ) {\n"
16299 "default:\n"
16300 " break;\n"
16301 "}",
16302 Spaces);
16303
16304 Spaces.SpacesInParentheses = false;
16305 Spaces.SpacesInCStyleCastParentheses = true;
16306 verifyFormat("Type *A = ( Type * )P;", Spaces);
16307 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
16308 verifyFormat("x = ( int32 )y;", Spaces);
16309 verifyFormat("throw ( int32 )x;", Spaces);
16310 verifyFormat("int a = ( int )(2.0f);", Spaces);
16311 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
16312 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
16313 verifyFormat("#define x (( int )-1)", Spaces);
16314
16315 // Run the first set of tests again with:
16316 Spaces.SpacesInParentheses = false;
16317 Spaces.SpaceInEmptyParentheses = true;
16318 Spaces.SpacesInCStyleCastParentheses = true;
16319 verifyFormat("call(x, y, z);", Spaces);
16320 verifyFormat("call( );", Spaces);
16321 verifyFormat("std::function<void(int, int)> callback;", Spaces);
16322 verifyFormat("while (( bool )1)\n"
16323 " continue;",
16324 Spaces);
16325 verifyFormat("for (;;)\n"
16326 " continue;",
16327 Spaces);
16328 verifyFormat("if (true)\n"
16329 " f( );\n"
16330 "else if (true)\n"
16331 " f( );",
16332 Spaces);
16333 verifyFormat("do {\n"
16334 " do_something(( int )i);\n"
16335 "} while (something( ));",
16336 Spaces);
16337 verifyFormat("switch (x) {\n"
16338 "default:\n"
16339 " break;\n"
16340 "}",
16341 Spaces);
16342
16343 // Run the first set of tests again with:
16344 Spaces.SpaceAfterCStyleCast = true;
16345 verifyFormat("call(x, y, z);", Spaces);
16346 verifyFormat("call( );", Spaces);
16347 verifyFormat("std::function<void(int, int)> callback;", Spaces);
16348 verifyFormat("while (( bool ) 1)\n"
16349 " continue;",
16350 Spaces);
16351 verifyFormat("for (;;)\n"
16352 " continue;",
16353 Spaces);
16354 verifyFormat("if (true)\n"
16355 " f( );\n"
16356 "else if (true)\n"
16357 " f( );",
16358 Spaces);
16359 verifyFormat("do {\n"
16360 " do_something(( int ) i);\n"
16361 "} while (something( ));",
16362 Spaces);
16363 verifyFormat("switch (x) {\n"
16364 "default:\n"
16365 " break;\n"
16366 "}",
16367 Spaces);
16368 verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces);
16369 verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces);
16370 verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces);
16371 verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces);
16372 verifyFormat("bool *y = ( bool * ) (x);", Spaces);
16373 verifyFormat("throw ( int32 ) x;", Spaces);
16374
16375 // Run subset of tests again with:
16376 Spaces.SpacesInCStyleCastParentheses = false;
16377 Spaces.SpaceAfterCStyleCast = true;
16378 verifyFormat("while ((bool) 1)\n"
16379 " continue;",
16380 Spaces);
16381 verifyFormat("do {\n"
16382 " do_something((int) i);\n"
16383 "} while (something( ));",
16384 Spaces);
16385
16386 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
16387 verifyFormat("size_t idx = (size_t) a;", Spaces);
16388 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
16389 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
16390 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
16391 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
16392 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
16393 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces);
16394 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces);
16395 verifyFormat("bool *y = (bool *) (void *) (x);", Spaces);
16396 verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces);
16397 verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces);
16398 verifyFormat("throw (int32) x;", Spaces);
16399
16400 Spaces.ColumnLimit = 80;
16401 Spaces.IndentWidth = 4;
16402 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
16403 verifyFormat("void foo( ) {\n"
16404 " size_t foo = (*(function))(\n"
16405 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
16406 "BarrrrrrrrrrrrLong,\n"
16407 " FoooooooooLooooong);\n"
16408 "}",
16409 Spaces);
16410 Spaces.SpaceAfterCStyleCast = false;
16411 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
16412 verifyFormat("size_t idx = (size_t)a;", Spaces);
16413 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
16414 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
16415 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
16416 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
16417 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
16418
16419 verifyFormat("void foo( ) {\n"
16420 " size_t foo = (*(function))(\n"
16421 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
16422 "BarrrrrrrrrrrrLong,\n"
16423 " FoooooooooLooooong);\n"
16424 "}",
16425 Spaces);
16426
16427 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
16428 verifyFormat("void foo( ) {\n"
16429 " size_t foo = (*(function))(\n"
16430 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
16431 "BarrrrrrrrrrrrLong,\n"
16432 " FoooooooooLooooong\n"
16433 " );\n"
16434 "}",
16435 Spaces);
16436 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
16437 verifyFormat("size_t idx = (size_t)a;", Spaces);
16438 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
16439 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
16440 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
16441 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
16442 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
16443 }
16444
16445 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
16446 verifyFormat("int a[5];");
16447 verifyFormat("a[3] += 42;");
16448
16449 FormatStyle Spaces = getLLVMStyle();
16450 Spaces.SpacesInSquareBrackets = true;
16451 // Not lambdas.
16452 verifyFormat("int a[ 5 ];", Spaces);
16453 verifyFormat("a[ 3 ] += 42;", Spaces);
16454 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
16455 verifyFormat("double &operator[](int i) { return 0; }\n"
16456 "int i;",
16457 Spaces);
16458 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
16459 verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
16460 verifyFormat("int i = (*b)[ a ]->f();", Spaces);
16461 // Lambdas.
16462 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
16463 verifyFormat("return [ i, args... ] {};", Spaces);
16464 verifyFormat("int foo = [ &bar ]() {};", Spaces);
16465 verifyFormat("int foo = [ = ]() {};", Spaces);
16466 verifyFormat("int foo = [ & ]() {};", Spaces);
16467 verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
16468 verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
16469 }
16470
16471 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
16472 FormatStyle NoSpaceStyle = getLLVMStyle();
16473 verifyFormat("int a[5];", NoSpaceStyle);
16474 verifyFormat("a[3] += 42;", NoSpaceStyle);
16475
16476 verifyFormat("int a[1];", NoSpaceStyle);
16477 verifyFormat("int 1 [a];", NoSpaceStyle);
16478 verifyFormat("int a[1][2];", NoSpaceStyle);
16479 verifyFormat("a[7] = 5;", NoSpaceStyle);
16480 verifyFormat("int a = (f())[23];", NoSpaceStyle);
16481 verifyFormat("f([] {})", NoSpaceStyle);
16482
16483 FormatStyle Space = getLLVMStyle();
16484 Space.SpaceBeforeSquareBrackets = true;
16485 verifyFormat("int c = []() -> int { return 2; }();\n", Space);
16486 verifyFormat("return [i, args...] {};", Space);
16487
16488 verifyFormat("int a [5];", Space);
16489 verifyFormat("a [3] += 42;", Space);
16490 verifyFormat("constexpr char hello []{\"hello\"};", Space);
16491 verifyFormat("double &operator[](int i) { return 0; }\n"
16492 "int i;",
16493 Space);
16494 verifyFormat("std::unique_ptr<int []> foo() {}", Space);
16495 verifyFormat("int i = a [a][a]->f();", Space);
16496 verifyFormat("int i = (*b) [a]->f();", Space);
16497
16498 verifyFormat("int a [1];", Space);
16499 verifyFormat("int 1 [a];", Space);
16500 verifyFormat("int a [1][2];", Space);
16501 verifyFormat("a [7] = 5;", Space);
16502 verifyFormat("int a = (f()) [23];", Space);
16503 verifyFormat("f([] {})", Space);
16504 }
16505
16506 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
16507 verifyFormat("int a = 5;");
16508 verifyFormat("a += 42;");
16509 verifyFormat("a or_eq 8;");
16510
16511 FormatStyle Spaces = getLLVMStyle();
16512 Spaces.SpaceBeforeAssignmentOperators = false;
16513 verifyFormat("int a= 5;", Spaces);
16514 verifyFormat("a+= 42;", Spaces);
16515 verifyFormat("a or_eq 8;", Spaces);
16516 }
16517
16518 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
16519 verifyFormat("class Foo : public Bar {};");
16520 verifyFormat("Foo::Foo() : foo(1) {}");
16521 verifyFormat("for (auto a : b) {\n}");
16522 verifyFormat("int x = a ? b : c;");
16523 verifyFormat("{\n"
16524 "label0:\n"
16525 " int x = 0;\n"
16526 "}");
16527 verifyFormat("switch (x) {\n"
16528 "case 1:\n"
16529 "default:\n"
16530 "}");
16531 verifyFormat("switch (allBraces) {\n"
16532 "case 1: {\n"
16533 " break;\n"
16534 "}\n"
16535 "case 2: {\n"
16536 " [[fallthrough]];\n"
16537 "}\n"
16538 "default: {\n"
16539 " break;\n"
16540 "}\n"
16541 "}");
16542
16543 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
16544 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
16545 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
16546 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
16547 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
16548 verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
16549 verifyFormat("{\n"
16550 "label1:\n"
16551 " int x = 0;\n"
16552 "}",
16553 CtorInitializerStyle);
16554 verifyFormat("switch (x) {\n"
16555 "case 1:\n"
16556 "default:\n"
16557 "}",
16558 CtorInitializerStyle);
16559 verifyFormat("switch (allBraces) {\n"
16560 "case 1: {\n"
16561 " break;\n"
16562 "}\n"
16563 "case 2: {\n"
16564 " [[fallthrough]];\n"
16565 "}\n"
16566 "default: {\n"
16567 " break;\n"
16568 "}\n"
16569 "}",
16570 CtorInitializerStyle);
16571 CtorInitializerStyle.BreakConstructorInitializers =
16572 FormatStyle::BCIS_AfterColon;
16573 verifyFormat("Fooooooooooo::Fooooooooooo():\n"
16574 " aaaaaaaaaaaaaaaa(1),\n"
16575 " bbbbbbbbbbbbbbbb(2) {}",
16576 CtorInitializerStyle);
16577 CtorInitializerStyle.BreakConstructorInitializers =
16578 FormatStyle::BCIS_BeforeComma;
16579 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
16580 " : aaaaaaaaaaaaaaaa(1)\n"
16581 " , bbbbbbbbbbbbbbbb(2) {}",
16582 CtorInitializerStyle);
16583 CtorInitializerStyle.BreakConstructorInitializers =
16584 FormatStyle::BCIS_BeforeColon;
16585 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
16586 " : aaaaaaaaaaaaaaaa(1),\n"
16587 " bbbbbbbbbbbbbbbb(2) {}",
16588 CtorInitializerStyle);
16589 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
16590 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
16591 ": aaaaaaaaaaaaaaaa(1),\n"
16592 " bbbbbbbbbbbbbbbb(2) {}",
16593 CtorInitializerStyle);
16594
16595 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
16596 InheritanceStyle.SpaceBeforeInheritanceColon = false;
16597 verifyFormat("class Foo: public Bar {};", InheritanceStyle);
16598 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
16599 verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
16600 verifyFormat("int x = a ? b : c;", InheritanceStyle);
16601 verifyFormat("{\n"
16602 "label2:\n"
16603 " int x = 0;\n"
16604 "}",
16605 InheritanceStyle);
16606 verifyFormat("switch (x) {\n"
16607 "case 1:\n"
16608 "default:\n"
16609 "}",
16610 InheritanceStyle);
16611 verifyFormat("switch (allBraces) {\n"
16612 "case 1: {\n"
16613 " break;\n"
16614 "}\n"
16615 "case 2: {\n"
16616 " [[fallthrough]];\n"
16617 "}\n"
16618 "default: {\n"
16619 " break;\n"
16620 "}\n"
16621 "}",
16622 InheritanceStyle);
16623 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma;
16624 verifyFormat("class Foooooooooooooooooooooo\n"
16625 " : public aaaaaaaaaaaaaaaaaa,\n"
16626 " public bbbbbbbbbbbbbbbbbb {\n"
16627 "}",
16628 InheritanceStyle);
16629 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
16630 verifyFormat("class Foooooooooooooooooooooo:\n"
16631 " public aaaaaaaaaaaaaaaaaa,\n"
16632 " public bbbbbbbbbbbbbbbbbb {\n"
16633 "}",
16634 InheritanceStyle);
16635 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
16636 verifyFormat("class Foooooooooooooooooooooo\n"
16637 " : public aaaaaaaaaaaaaaaaaa\n"
16638 " , public bbbbbbbbbbbbbbbbbb {\n"
16639 "}",
16640 InheritanceStyle);
16641 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
16642 verifyFormat("class Foooooooooooooooooooooo\n"
16643 " : public aaaaaaaaaaaaaaaaaa,\n"
16644 " public bbbbbbbbbbbbbbbbbb {\n"
16645 "}",
16646 InheritanceStyle);
16647 InheritanceStyle.ConstructorInitializerIndentWidth = 0;
16648 verifyFormat("class Foooooooooooooooooooooo\n"
16649 ": public aaaaaaaaaaaaaaaaaa,\n"
16650 " public bbbbbbbbbbbbbbbbbb {}",
16651 InheritanceStyle);
16652
16653 FormatStyle ForLoopStyle = getLLVMStyle();
16654 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
16655 verifyFormat("class Foo : public Bar {};", ForLoopStyle);
16656 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
16657 verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
16658 verifyFormat("int x = a ? b : c;", ForLoopStyle);
16659 verifyFormat("{\n"
16660 "label2:\n"
16661 " int x = 0;\n"
16662 "}",
16663 ForLoopStyle);
16664 verifyFormat("switch (x) {\n"
16665 "case 1:\n"
16666 "default:\n"
16667 "}",
16668 ForLoopStyle);
16669 verifyFormat("switch (allBraces) {\n"
16670 "case 1: {\n"
16671 " break;\n"
16672 "}\n"
16673 "case 2: {\n"
16674 " [[fallthrough]];\n"
16675 "}\n"
16676 "default: {\n"
16677 " break;\n"
16678 "}\n"
16679 "}",
16680 ForLoopStyle);
16681
16682 FormatStyle CaseStyle = getLLVMStyle();
16683 CaseStyle.SpaceBeforeCaseColon = true;
16684 verifyFormat("class Foo : public Bar {};", CaseStyle);
16685 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle);
16686 verifyFormat("for (auto a : b) {\n}", CaseStyle);
16687 verifyFormat("int x = a ? b : c;", CaseStyle);
16688 verifyFormat("switch (x) {\n"
16689 "case 1 :\n"
16690 "default :\n"
16691 "}",
16692 CaseStyle);
16693 verifyFormat("switch (allBraces) {\n"
16694 "case 1 : {\n"
16695 " break;\n"
16696 "}\n"
16697 "case 2 : {\n"
16698 " [[fallthrough]];\n"
16699 "}\n"
16700 "default : {\n"
16701 " break;\n"
16702 "}\n"
16703 "}",
16704 CaseStyle);
16705
16706 FormatStyle NoSpaceStyle = getLLVMStyle();
16707 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false);
16708 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
16709 NoSpaceStyle.SpaceBeforeInheritanceColon = false;
16710 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
16711 verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
16712 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
16713 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
16714 verifyFormat("int x = a ? b : c;", NoSpaceStyle);
16715 verifyFormat("{\n"
16716 "label3:\n"
16717 " int x = 0;\n"
16718 "}",
16719 NoSpaceStyle);
16720 verifyFormat("switch (x) {\n"
16721 "case 1:\n"
16722 "default:\n"
16723 "}",
16724 NoSpaceStyle);
16725 verifyFormat("switch (allBraces) {\n"
16726 "case 1: {\n"
16727 " break;\n"
16728 "}\n"
16729 "case 2: {\n"
16730 " [[fallthrough]];\n"
16731 "}\n"
16732 "default: {\n"
16733 " break;\n"
16734 "}\n"
16735 "}",
16736 NoSpaceStyle);
16737
16738 FormatStyle InvertedSpaceStyle = getLLVMStyle();
16739 InvertedSpaceStyle.SpaceBeforeCaseColon = true;
16740 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false;
16741 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false;
16742 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
16743 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle);
16744 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle);
16745 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle);
16746 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle);
16747 verifyFormat("{\n"
16748 "label3:\n"
16749 " int x = 0;\n"
16750 "}",
16751 InvertedSpaceStyle);
16752 verifyFormat("switch (x) {\n"
16753 "case 1 :\n"
16754 "case 2 : {\n"
16755 " break;\n"
16756 "}\n"
16757 "default :\n"
16758 " break;\n"
16759 "}",
16760 InvertedSpaceStyle);
16761 verifyFormat("switch (allBraces) {\n"
16762 "case 1 : {\n"
16763 " break;\n"
16764 "}\n"
16765 "case 2 : {\n"
16766 " [[fallthrough]];\n"
16767 "}\n"
16768 "default : {\n"
16769 " break;\n"
16770 "}\n"
16771 "}",
16772 InvertedSpaceStyle);
16773 }
16774
16775 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
16776 FormatStyle Style = getLLVMStyle();
16777
16778 Style.PointerAlignment = FormatStyle::PAS_Left;
16779 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
16780 verifyFormat("void* const* x = NULL;", Style);
16781
16782 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \
16783 do { \
16784 Style.PointerAlignment = FormatStyle::Pointers; \
16785 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \
16786 verifyFormat(Code, Style); \
16787 } while (false)
16788
16789 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default);
16790 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default);
16791 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default);
16792
16793 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before);
16794 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before);
16795 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before);
16796
16797 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After);
16798 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After);
16799 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After);
16800
16801 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both);
16802 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
16803 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
16804
16805 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
16806 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
16807 SAPQ_Default);
16808 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
16809 SAPQ_Default);
16810
16811 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
16812 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
16813 SAPQ_Before);
16814 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
16815 SAPQ_Before);
16816
16817 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
16818 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
16819 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
16820 SAPQ_After);
16821
16822 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
16823 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
16824 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
16825
16826 #undef verifyQualifierSpaces
16827
16828 FormatStyle Spaces = getLLVMStyle();
16829 Spaces.AttributeMacros.push_back("qualified");
16830 Spaces.PointerAlignment = FormatStyle::PAS_Right;
16831 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
16832 verifyFormat("SomeType *volatile *a = NULL;", Spaces);
16833 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
16834 verifyFormat("std::vector<SomeType *const *> x;", Spaces);
16835 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces);
16836 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
16837 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
16838 verifyFormat("SomeType * volatile *a = NULL;", Spaces);
16839 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces);
16840 verifyFormat("std::vector<SomeType * const *> x;", Spaces);
16841 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
16842 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
16843
16844 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
16845 Spaces.PointerAlignment = FormatStyle::PAS_Left;
16846 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
16847 verifyFormat("SomeType* volatile* a = NULL;", Spaces);
16848 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces);
16849 verifyFormat("std::vector<SomeType* const*> x;", Spaces);
16850 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces);
16851 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
16852 // However, setting it to SAPQ_After should add spaces after __attribute, etc.
16853 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
16854 verifyFormat("SomeType* volatile * a = NULL;", Spaces);
16855 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces);
16856 verifyFormat("std::vector<SomeType* const *> x;", Spaces);
16857 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces);
16858 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
16859
16860 // PAS_Middle should not have any noticeable changes even for SAPQ_Both
16861 Spaces.PointerAlignment = FormatStyle::PAS_Middle;
16862 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
16863 verifyFormat("SomeType * volatile * a = NULL;", Spaces);
16864 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces);
16865 verifyFormat("std::vector<SomeType * const *> x;", Spaces);
16866 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
16867 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
16868 }
16869
16870 TEST_F(FormatTest, AlignConsecutiveMacros) {
16871 FormatStyle Style = getLLVMStyle();
16872 Style.AlignConsecutiveAssignments.Enabled = true;
16873 Style.AlignConsecutiveDeclarations.Enabled = true;
16874
16875 verifyFormat("#define a 3\n"
16876 "#define bbbb 4\n"
16877 "#define ccc (5)",
16878 Style);
16879
16880 verifyFormat("#define f(x) (x * x)\n"
16881 "#define fff(x, y, z) (x * y + z)\n"
16882 "#define ffff(x, y) (x - y)",
16883 Style);
16884
16885 verifyFormat("#define foo(x, y) (x + y)\n"
16886 "#define bar (5, 6)(2 + 2)",
16887 Style);
16888
16889 verifyFormat("#define a 3\n"
16890 "#define bbbb 4\n"
16891 "#define ccc (5)\n"
16892 "#define f(x) (x * x)\n"
16893 "#define fff(x, y, z) (x * y + z)\n"
16894 "#define ffff(x, y) (x - y)",
16895 Style);
16896
16897 Style.AlignConsecutiveMacros.Enabled = true;
16898 verifyFormat("#define a 3\n"
16899 "#define bbbb 4\n"
16900 "#define ccc (5)",
16901 Style);
16902
16903 verifyFormat("#define true 1\n"
16904 "#define false 0",
16905 Style);
16906
16907 verifyFormat("#define f(x) (x * x)\n"
16908 "#define fff(x, y, z) (x * y + z)\n"
16909 "#define ffff(x, y) (x - y)",
16910 Style);
16911
16912 verifyFormat("#define foo(x, y) (x + y)\n"
16913 "#define bar (5, 6)(2 + 2)",
16914 Style);
16915
16916 verifyFormat("#define a 3\n"
16917 "#define bbbb 4\n"
16918 "#define ccc (5)\n"
16919 "#define f(x) (x * x)\n"
16920 "#define fff(x, y, z) (x * y + z)\n"
16921 "#define ffff(x, y) (x - y)",
16922 Style);
16923
16924 verifyFormat("#define a 5\n"
16925 "#define foo(x, y) (x + y)\n"
16926 "#define CCC (6)\n"
16927 "auto lambda = []() {\n"
16928 " auto ii = 0;\n"
16929 " float j = 0;\n"
16930 " return 0;\n"
16931 "};\n"
16932 "int i = 0;\n"
16933 "float i2 = 0;\n"
16934 "auto v = type{\n"
16935 " i = 1, //\n"
16936 " (i = 2), //\n"
16937 " i = 3 //\n"
16938 "};",
16939 Style);
16940
16941 Style.AlignConsecutiveMacros.Enabled = false;
16942 Style.ColumnLimit = 20;
16943
16944 verifyFormat("#define a \\\n"
16945 " \"aabbbbbbbbbbbb\"\n"
16946 "#define D \\\n"
16947 " \"aabbbbbbbbbbbb\" \\\n"
16948 " \"ccddeeeeeeeee\"\n"
16949 "#define B \\\n"
16950 " \"QQQQQQQQQQQQQ\" \\\n"
16951 " \"FFFFFFFFFFFFF\" \\\n"
16952 " \"LLLLLLLL\"\n",
16953 Style);
16954
16955 Style.AlignConsecutiveMacros.Enabled = true;
16956 verifyFormat("#define a \\\n"
16957 " \"aabbbbbbbbbbbb\"\n"
16958 "#define D \\\n"
16959 " \"aabbbbbbbbbbbb\" \\\n"
16960 " \"ccddeeeeeeeee\"\n"
16961 "#define B \\\n"
16962 " \"QQQQQQQQQQQQQ\" \\\n"
16963 " \"FFFFFFFFFFFFF\" \\\n"
16964 " \"LLLLLLLL\"\n",
16965 Style);
16966
16967 // Test across comments
16968 Style.MaxEmptyLinesToKeep = 10;
16969 Style.ReflowComments = false;
16970 Style.AlignConsecutiveMacros.AcrossComments = true;
16971 EXPECT_EQ("#define a 3\n"
16972 "// line comment\n"
16973 "#define bbbb 4\n"
16974 "#define ccc (5)",
16975 format("#define a 3\n"
16976 "// line comment\n"
16977 "#define bbbb 4\n"
16978 "#define ccc (5)",
16979 Style));
16980
16981 EXPECT_EQ("#define a 3\n"
16982 "/* block comment */\n"
16983 "#define bbbb 4\n"
16984 "#define ccc (5)",
16985 format("#define a 3\n"
16986 "/* block comment */\n"
16987 "#define bbbb 4\n"
16988 "#define ccc (5)",
16989 Style));
16990
16991 EXPECT_EQ("#define a 3\n"
16992 "/* multi-line *\n"
16993 " * block comment */\n"
16994 "#define bbbb 4\n"
16995 "#define ccc (5)",
16996 format("#define a 3\n"
16997 "/* multi-line *\n"
16998 " * block comment */\n"
16999 "#define bbbb 4\n"
17000 "#define ccc (5)",
17001 Style));
17002
17003 EXPECT_EQ("#define a 3\n"
17004 "// multi-line line comment\n"
17005 "//\n"
17006 "#define bbbb 4\n"
17007 "#define ccc (5)",
17008 format("#define a 3\n"
17009 "// multi-line line comment\n"
17010 "//\n"
17011 "#define bbbb 4\n"
17012 "#define ccc (5)",
17013 Style));
17014
17015 EXPECT_EQ("#define a 3\n"
17016 "// empty lines still break.\n"
17017 "\n"
17018 "#define bbbb 4\n"
17019 "#define ccc (5)",
17020 format("#define a 3\n"
17021 "// empty lines still break.\n"
17022 "\n"
17023 "#define bbbb 4\n"
17024 "#define ccc (5)",
17025 Style));
17026
17027 // Test across empty lines
17028 Style.AlignConsecutiveMacros.AcrossComments = false;
17029 Style.AlignConsecutiveMacros.AcrossEmptyLines = true;
17030 EXPECT_EQ("#define a 3\n"
17031 "\n"
17032 "#define bbbb 4\n"
17033 "#define ccc (5)",
17034 format("#define a 3\n"
17035 "\n"
17036 "#define bbbb 4\n"
17037 "#define ccc (5)",
17038 Style));
17039
17040 EXPECT_EQ("#define a 3\n"
17041 "\n"
17042 "\n"
17043 "\n"
17044 "#define bbbb 4\n"
17045 "#define ccc (5)",
17046 format("#define a 3\n"
17047 "\n"
17048 "\n"
17049 "\n"
17050 "#define bbbb 4\n"
17051 "#define ccc (5)",
17052 Style));
17053
17054 EXPECT_EQ("#define a 3\n"
17055 "// comments should break alignment\n"
17056 "//\n"
17057 "#define bbbb 4\n"
17058 "#define ccc (5)",
17059 format("#define a 3\n"
17060 "// comments should break alignment\n"
17061 "//\n"
17062 "#define bbbb 4\n"
17063 "#define ccc (5)",
17064 Style));
17065
17066 // Test across empty lines and comments
17067 Style.AlignConsecutiveMacros.AcrossComments = true;
17068 verifyFormat("#define a 3\n"
17069 "\n"
17070 "// line comment\n"
17071 "#define bbbb 4\n"
17072 "#define ccc (5)",
17073 Style);
17074
17075 EXPECT_EQ("#define a 3\n"
17076 "\n"
17077 "\n"
17078 "/* multi-line *\n"
17079 " * block comment */\n"
17080 "\n"
17081 "\n"
17082 "#define bbbb 4\n"
17083 "#define ccc (5)",
17084 format("#define a 3\n"
17085 "\n"
17086 "\n"
17087 "/* multi-line *\n"
17088 " * block comment */\n"
17089 "\n"
17090 "\n"
17091 "#define bbbb 4\n"
17092 "#define ccc (5)",
17093 Style));
17094
17095 EXPECT_EQ("#define a 3\n"
17096 "\n"
17097 "\n"
17098 "/* multi-line *\n"
17099 " * block comment */\n"
17100 "\n"
17101 "\n"
17102 "#define bbbb 4\n"
17103 "#define ccc (5)",
17104 format("#define a 3\n"
17105 "\n"
17106 "\n"
17107 "/* multi-line *\n"
17108 " * block comment */\n"
17109 "\n"
17110 "\n"
17111 "#define bbbb 4\n"
17112 "#define ccc (5)",
17113 Style));
17114 }
17115
17116 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
17117 FormatStyle Alignment = getLLVMStyle();
17118 Alignment.AlignConsecutiveMacros.Enabled = true;
17119 Alignment.AlignConsecutiveAssignments.Enabled = true;
17120 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
17121
17122 Alignment.MaxEmptyLinesToKeep = 10;
17123 /* Test alignment across empty lines */
17124 EXPECT_EQ("int a = 5;\n"
17125 "\n"
17126 "int oneTwoThree = 123;",
17127 format("int a = 5;\n"
17128 "\n"
17129 "int oneTwoThree= 123;",
17130 Alignment));
17131 EXPECT_EQ("int a = 5;\n"
17132 "int one = 1;\n"
17133 "\n"
17134 "int oneTwoThree = 123;",
17135 format("int a = 5;\n"
17136 "int one = 1;\n"
17137 "\n"
17138 "int oneTwoThree = 123;",
17139 Alignment));
17140 EXPECT_EQ("int a = 5;\n"
17141 "int one = 1;\n"
17142 "\n"
17143 "int oneTwoThree = 123;\n"
17144 "int oneTwo = 12;",
17145 format("int a = 5;\n"
17146 "int one = 1;\n"
17147 "\n"
17148 "int oneTwoThree = 123;\n"
17149 "int oneTwo = 12;",
17150 Alignment));
17151
17152 /* Test across comments */
17153 EXPECT_EQ("int a = 5;\n"
17154 "/* block comment */\n"
17155 "int oneTwoThree = 123;",
17156 format("int a = 5;\n"
17157 "/* block comment */\n"
17158 "int oneTwoThree=123;",
17159 Alignment));
17160
17161 EXPECT_EQ("int a = 5;\n"
17162 "// line comment\n"
17163 "int oneTwoThree = 123;",
17164 format("int a = 5;\n"
17165 "// line comment\n"
17166 "int oneTwoThree=123;",
17167 Alignment));
17168
17169 /* Test across comments and newlines */
17170 EXPECT_EQ("int a = 5;\n"
17171 "\n"
17172 "/* block comment */\n"
17173 "int oneTwoThree = 123;",
17174 format("int a = 5;\n"
17175 "\n"
17176 "/* block comment */\n"
17177 "int oneTwoThree=123;",
17178 Alignment));
17179
17180 EXPECT_EQ("int a = 5;\n"
17181 "\n"
17182 "// line comment\n"
17183 "int oneTwoThree = 123;",
17184 format("int a = 5;\n"
17185 "\n"
17186 "// line comment\n"
17187 "int oneTwoThree=123;",
17188 Alignment));
17189 }
17190
17191 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
17192 FormatStyle Alignment = getLLVMStyle();
17193 Alignment.AlignConsecutiveDeclarations.Enabled = true;
17194 Alignment.AlignConsecutiveDeclarations.AcrossEmptyLines = true;
17195 Alignment.AlignConsecutiveDeclarations.AcrossComments = true;
17196
17197 Alignment.MaxEmptyLinesToKeep = 10;
17198 /* Test alignment across empty lines */
17199 EXPECT_EQ("int a = 5;\n"
17200 "\n"
17201 "float const oneTwoThree = 123;",
17202 format("int a = 5;\n"
17203 "\n"
17204 "float const oneTwoThree = 123;",
17205 Alignment));
17206 EXPECT_EQ("int a = 5;\n"
17207 "float const one = 1;\n"
17208 "\n"
17209 "int oneTwoThree = 123;",
17210 format("int a = 5;\n"
17211 "float const one = 1;\n"
17212 "\n"
17213 "int oneTwoThree = 123;",
17214 Alignment));
17215
17216 /* Test across comments */
17217 EXPECT_EQ("float const a = 5;\n"
17218 "/* block comment */\n"
17219 "int oneTwoThree = 123;",
17220 format("float const a = 5;\n"
17221 "/* block comment */\n"
17222 "int oneTwoThree=123;",
17223 Alignment));
17224
17225 EXPECT_EQ("float const a = 5;\n"
17226 "// line comment\n"
17227 "int oneTwoThree = 123;",
17228 format("float const a = 5;\n"
17229 "// line comment\n"
17230 "int oneTwoThree=123;",
17231 Alignment));
17232
17233 /* Test across comments and newlines */
17234 EXPECT_EQ("float const a = 5;\n"
17235 "\n"
17236 "/* block comment */\n"
17237 "int oneTwoThree = 123;",
17238 format("float const a = 5;\n"
17239 "\n"
17240 "/* block comment */\n"
17241 "int oneTwoThree=123;",
17242 Alignment));
17243
17244 EXPECT_EQ("float const a = 5;\n"
17245 "\n"
17246 "// line comment\n"
17247 "int oneTwoThree = 123;",
17248 format("float const a = 5;\n"
17249 "\n"
17250 "// line comment\n"
17251 "int oneTwoThree=123;",
17252 Alignment));
17253 }
17254
17255 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) {
17256 FormatStyle Alignment = getLLVMStyle();
17257 Alignment.AlignConsecutiveBitFields.Enabled = true;
17258 Alignment.AlignConsecutiveBitFields.AcrossEmptyLines = true;
17259 Alignment.AlignConsecutiveBitFields.AcrossComments = true;
17260
17261 Alignment.MaxEmptyLinesToKeep = 10;
17262 /* Test alignment across empty lines */
17263 EXPECT_EQ("int a : 5;\n"
17264 "\n"
17265 "int longbitfield : 6;",
17266 format("int a : 5;\n"
17267 "\n"
17268 "int longbitfield : 6;",
17269 Alignment));
17270 EXPECT_EQ("int a : 5;\n"
17271 "int one : 1;\n"
17272 "\n"
17273 "int longbitfield : 6;",
17274 format("int a : 5;\n"
17275 "int one : 1;\n"
17276 "\n"
17277 "int longbitfield : 6;",
17278 Alignment));
17279
17280 /* Test across comments */
17281 EXPECT_EQ("int a : 5;\n"
17282 "/* block comment */\n"
17283 "int longbitfield : 6;",
17284 format("int a : 5;\n"
17285 "/* block comment */\n"
17286 "int longbitfield : 6;",
17287 Alignment));
17288 EXPECT_EQ("int a : 5;\n"
17289 "int one : 1;\n"
17290 "// line comment\n"
17291 "int longbitfield : 6;",
17292 format("int a : 5;\n"
17293 "int one : 1;\n"
17294 "// line comment\n"
17295 "int longbitfield : 6;",
17296 Alignment));
17297
17298 /* Test across comments and newlines */
17299 EXPECT_EQ("int a : 5;\n"
17300 "/* block comment */\n"
17301 "\n"
17302 "int longbitfield : 6;",
17303 format("int a : 5;\n"
17304 "/* block comment */\n"
17305 "\n"
17306 "int longbitfield : 6;",
17307 Alignment));
17308 EXPECT_EQ("int a : 5;\n"
17309 "int one : 1;\n"
17310 "\n"
17311 "// line comment\n"
17312 "\n"
17313 "int longbitfield : 6;",
17314 format("int a : 5;\n"
17315 "int one : 1;\n"
17316 "\n"
17317 "// line comment \n"
17318 "\n"
17319 "int longbitfield : 6;",
17320 Alignment));
17321 }
17322
17323 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) {
17324 FormatStyle Alignment = getLLVMStyle();
17325 Alignment.AlignConsecutiveMacros.Enabled = true;
17326 Alignment.AlignConsecutiveAssignments.Enabled = true;
17327 Alignment.AlignConsecutiveAssignments.AcrossComments = true;
17328
17329 Alignment.MaxEmptyLinesToKeep = 10;
17330 /* Test alignment across empty lines */
17331 EXPECT_EQ("int a = 5;\n"
17332 "\n"
17333 "int oneTwoThree = 123;",
17334 format("int a = 5;\n"
17335 "\n"
17336 "int oneTwoThree= 123;",
17337 Alignment));
17338 EXPECT_EQ("int a = 5;\n"
17339 "int one = 1;\n"
17340 "\n"
17341 "int oneTwoThree = 123;",
17342 format("int a = 5;\n"
17343 "int one = 1;\n"
17344 "\n"
17345 "int oneTwoThree = 123;",
17346 Alignment));
17347
17348 /* Test across comments */
17349 EXPECT_EQ("int a = 5;\n"
17350 "/* block comment */\n"
17351 "int oneTwoThree = 123;",
17352 format("int a = 5;\n"
17353 "/* block comment */\n"
17354 "int oneTwoThree=123;",
17355 Alignment));
17356
17357 EXPECT_EQ("int a = 5;\n"
17358 "// line comment\n"
17359 "int oneTwoThree = 123;",
17360 format("int a = 5;\n"
17361 "// line comment\n"
17362 "int oneTwoThree=123;",
17363 Alignment));
17364
17365 EXPECT_EQ("int a = 5;\n"
17366 "/*\n"
17367 " * multi-line block comment\n"
17368 " */\n"
17369 "int oneTwoThree = 123;",
17370 format("int a = 5;\n"
17371 "/*\n"
17372 " * multi-line block comment\n"
17373 " */\n"
17374 "int oneTwoThree=123;",
17375 Alignment));
17376
17377 EXPECT_EQ("int a = 5;\n"
17378 "//\n"
17379 "// multi-line line comment\n"
17380 "//\n"
17381 "int oneTwoThree = 123;",
17382 format("int a = 5;\n"
17383 "//\n"
17384 "// multi-line line comment\n"
17385 "//\n"
17386 "int oneTwoThree=123;",
17387 Alignment));
17388
17389 /* Test across comments and newlines */
17390 EXPECT_EQ("int a = 5;\n"
17391 "\n"
17392 "/* block comment */\n"
17393 "int oneTwoThree = 123;",
17394 format("int a = 5;\n"
17395 "\n"
17396 "/* block comment */\n"
17397 "int oneTwoThree=123;",
17398 Alignment));
17399
17400 EXPECT_EQ("int a = 5;\n"
17401 "\n"
17402 "// line comment\n"
17403 "int oneTwoThree = 123;",
17404 format("int a = 5;\n"
17405 "\n"
17406 "// line comment\n"
17407 "int oneTwoThree=123;",
17408 Alignment));
17409 }
17410
17411 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) {
17412 FormatStyle Alignment = getLLVMStyle();
17413 Alignment.AlignConsecutiveMacros.Enabled = true;
17414 Alignment.AlignConsecutiveAssignments.Enabled = true;
17415 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
17416 Alignment.AlignConsecutiveAssignments.AcrossComments = true;
17417 verifyFormat("int a = 5;\n"
17418 "int oneTwoThree = 123;",
17419 Alignment);
17420 verifyFormat("int a = method();\n"
17421 "int oneTwoThree = 133;",
17422 Alignment);
17423 verifyFormat("a &= 5;\n"
17424 "bcd *= 5;\n"
17425 "ghtyf += 5;\n"
17426 "dvfvdb -= 5;\n"
17427 "a /= 5;\n"
17428 "vdsvsv %= 5;\n"
17429 "sfdbddfbdfbb ^= 5;\n"
17430 "dvsdsv |= 5;\n"
17431 "int dsvvdvsdvvv = 123;",
17432 Alignment);
17433 verifyFormat("int i = 1, j = 10;\n"
17434 "something = 2000;",
17435 Alignment);
17436 verifyFormat("something = 2000;\n"
17437 "int i = 1, j = 10;\n",
17438 Alignment);
17439 verifyFormat("something = 2000;\n"
17440 "another = 911;\n"
17441 "int i = 1, j = 10;\n"
17442 "oneMore = 1;\n"
17443 "i = 2;",
17444 Alignment);
17445 verifyFormat("int a = 5;\n"
17446 "int one = 1;\n"
17447 "method();\n"
17448 "int oneTwoThree = 123;\n"
17449 "int oneTwo = 12;",
17450 Alignment);
17451 verifyFormat("int oneTwoThree = 123;\n"
17452 "int oneTwo = 12;\n"
17453 "method();\n",
17454 Alignment);
17455 verifyFormat("int oneTwoThree = 123; // comment\n"
17456 "int oneTwo = 12; // comment",
17457 Alignment);
17458
17459 // Bug 25167
17460 /* Uncomment when fixed
17461 verifyFormat("#if A\n"
17462 "#else\n"
17463 "int aaaaaaaa = 12;\n"
17464 "#endif\n"
17465 "#if B\n"
17466 "#else\n"
17467 "int a = 12;\n"
17468 "#endif\n",
17469 Alignment);
17470 verifyFormat("enum foo {\n"
17471 "#if A\n"
17472 "#else\n"
17473 " aaaaaaaa = 12;\n"
17474 "#endif\n"
17475 "#if B\n"
17476 "#else\n"
17477 " a = 12;\n"
17478 "#endif\n"
17479 "};\n",
17480 Alignment);
17481 */
17482
17483 Alignment.MaxEmptyLinesToKeep = 10;
17484 /* Test alignment across empty lines */
17485 EXPECT_EQ("int a = 5;\n"
17486 "\n"
17487 "int oneTwoThree = 123;",
17488 format("int a = 5;\n"
17489 "\n"
17490 "int oneTwoThree= 123;",
17491 Alignment));
17492 EXPECT_EQ("int a = 5;\n"
17493 "int one = 1;\n"
17494 "\n"
17495 "int oneTwoThree = 123;",
17496 format("int a = 5;\n"
17497 "int one = 1;\n"
17498 "\n"
17499 "int oneTwoThree = 123;",
17500 Alignment));
17501 EXPECT_EQ("int a = 5;\n"
17502 "int one = 1;\n"
17503 "\n"
17504 "int oneTwoThree = 123;\n"
17505 "int oneTwo = 12;",
17506 format("int a = 5;\n"
17507 "int one = 1;\n"
17508 "\n"
17509 "int oneTwoThree = 123;\n"
17510 "int oneTwo = 12;",
17511 Alignment));
17512
17513 /* Test across comments */
17514 EXPECT_EQ("int a = 5;\n"
17515 "/* block comment */\n"
17516 "int oneTwoThree = 123;",
17517 format("int a = 5;\n"
17518 "/* block comment */\n"
17519 "int oneTwoThree=123;",
17520 Alignment));
17521
17522 EXPECT_EQ("int a = 5;\n"
17523 "// line comment\n"
17524 "int oneTwoThree = 123;",
17525 format("int a = 5;\n"
17526 "// line comment\n"
17527 "int oneTwoThree=123;",
17528 Alignment));
17529
17530 /* Test across comments and newlines */
17531 EXPECT_EQ("int a = 5;\n"
17532 "\n"
17533 "/* block comment */\n"
17534 "int oneTwoThree = 123;",
17535 format("int a = 5;\n"
17536 "\n"
17537 "/* block comment */\n"
17538 "int oneTwoThree=123;",
17539 Alignment));
17540
17541 EXPECT_EQ("int a = 5;\n"
17542 "\n"
17543 "// line comment\n"
17544 "int oneTwoThree = 123;",
17545 format("int a = 5;\n"
17546 "\n"
17547 "// line comment\n"
17548 "int oneTwoThree=123;",
17549 Alignment));
17550
17551 EXPECT_EQ("int a = 5;\n"
17552 "//\n"
17553 "// multi-line line comment\n"
17554 "//\n"
17555 "int oneTwoThree = 123;",
17556 format("int a = 5;\n"
17557 "//\n"
17558 "// multi-line line comment\n"
17559 "//\n"
17560 "int oneTwoThree=123;",
17561 Alignment));
17562
17563 EXPECT_EQ("int a = 5;\n"
17564 "/*\n"
17565 " * multi-line block comment\n"
17566 " */\n"
17567 "int oneTwoThree = 123;",
17568 format("int a = 5;\n"
17569 "/*\n"
17570 " * multi-line block comment\n"
17571 " */\n"
17572 "int oneTwoThree=123;",
17573 Alignment));
17574
17575 EXPECT_EQ("int a = 5;\n"
17576 "\n"
17577 "/* block comment */\n"
17578 "\n"
17579 "\n"
17580 "\n"
17581 "int oneTwoThree = 123;",
17582 format("int a = 5;\n"
17583 "\n"
17584 "/* block comment */\n"
17585 "\n"
17586 "\n"
17587 "\n"
17588 "int oneTwoThree=123;",
17589 Alignment));
17590
17591 EXPECT_EQ("int a = 5;\n"
17592 "\n"
17593 "// line comment\n"
17594 "\n"
17595 "\n"
17596 "\n"
17597 "int oneTwoThree = 123;",
17598 format("int a = 5;\n"
17599 "\n"
17600 "// line comment\n"
17601 "\n"
17602 "\n"
17603 "\n"
17604 "int oneTwoThree=123;",
17605 Alignment));
17606
17607 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
17608 verifyFormat("#define A \\\n"
17609 " int aaaa = 12; \\\n"
17610 " int b = 23; \\\n"
17611 " int ccc = 234; \\\n"
17612 " int dddddddddd = 2345;",
17613 Alignment);
17614 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
17615 verifyFormat("#define A \\\n"
17616 " int aaaa = 12; \\\n"
17617 " int b = 23; \\\n"
17618 " int ccc = 234; \\\n"
17619 " int dddddddddd = 2345;",
17620 Alignment);
17621 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
17622 verifyFormat("#define A "
17623 " \\\n"
17624 " int aaaa = 12; "
17625 " \\\n"
17626 " int b = 23; "
17627 " \\\n"
17628 " int ccc = 234; "
17629 " \\\n"
17630 " int dddddddddd = 2345;",
17631 Alignment);
17632 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
17633 "k = 4, int l = 5,\n"
17634 " int m = 6) {\n"
17635 " int j = 10;\n"
17636 " otherThing = 1;\n"
17637 "}",
17638 Alignment);
17639 verifyFormat("void SomeFunction(int parameter = 0) {\n"
17640 " int i = 1;\n"
17641 " int j = 2;\n"
17642 " int big = 10000;\n"
17643 "}",
17644 Alignment);
17645 verifyFormat("class C {\n"
17646 "public:\n"
17647 " int i = 1;\n"
17648 " virtual void f() = 0;\n"
17649 "};",
17650 Alignment);
17651 verifyFormat("int i = 1;\n"
17652 "if (SomeType t = getSomething()) {\n"
17653 "}\n"
17654 "int j = 2;\n"
17655 "int big = 10000;",
17656 Alignment);
17657 verifyFormat("int j = 7;\n"
17658 "for (int k = 0; k < N; ++k) {\n"
17659 "}\n"
17660 "int j = 2;\n"
17661 "int big = 10000;\n"
17662 "}",
17663 Alignment);
17664 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
17665 verifyFormat("int i = 1;\n"
17666 "LooooooooooongType loooooooooooooooooooooongVariable\n"
17667 " = someLooooooooooooooooongFunction();\n"
17668 "int j = 2;",
17669 Alignment);
17670 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
17671 verifyFormat("int i = 1;\n"
17672 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
17673 " someLooooooooooooooooongFunction();\n"
17674 "int j = 2;",
17675 Alignment);
17676
17677 verifyFormat("auto lambda = []() {\n"
17678 " auto i = 0;\n"
17679 " return 0;\n"
17680 "};\n"
17681 "int i = 0;\n"
17682 "auto v = type{\n"
17683 " i = 1, //\n"
17684 " (i = 2), //\n"
17685 " i = 3 //\n"
17686 "};",
17687 Alignment);
17688
17689 verifyFormat(
17690 "int i = 1;\n"
17691 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
17692 " loooooooooooooooooooooongParameterB);\n"
17693 "int j = 2;",
17694 Alignment);
17695
17696 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
17697 " typename B = very_long_type_name_1,\n"
17698 " typename T_2 = very_long_type_name_2>\n"
17699 "auto foo() {}\n",
17700 Alignment);
17701 verifyFormat("int a, b = 1;\n"
17702 "int c = 2;\n"
17703 "int dd = 3;\n",
17704 Alignment);
17705 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
17706 "float b[1][] = {{3.f}};\n",
17707 Alignment);
17708 verifyFormat("for (int i = 0; i < 1; i++)\n"
17709 " int x = 1;\n",
17710 Alignment);
17711 verifyFormat("for (i = 0; i < 1; i++)\n"
17712 " x = 1;\n"
17713 "y = 1;\n",
17714 Alignment);
17715
17716 Alignment.ReflowComments = true;
17717 Alignment.ColumnLimit = 50;
17718 EXPECT_EQ("int x = 0;\n"
17719 "int yy = 1; /// specificlennospace\n"
17720 "int zzz = 2;\n",
17721 format("int x = 0;\n"
17722 "int yy = 1; ///specificlennospace\n"
17723 "int zzz = 2;\n",
17724 Alignment));
17725 }
17726
17727 TEST_F(FormatTest, AlignCompoundAssignments) {
17728 FormatStyle Alignment = getLLVMStyle();
17729 Alignment.AlignConsecutiveAssignments.Enabled = true;
17730 Alignment.AlignConsecutiveAssignments.AlignCompound = true;
17731 Alignment.AlignConsecutiveAssignments.PadOperators = false;
17732 verifyFormat("sfdbddfbdfbb = 5;\n"
17733 "dvsdsv = 5;\n"
17734 "int dsvvdvsdvvv = 123;",
17735 Alignment);
17736 verifyFormat("sfdbddfbdfbb ^= 5;\n"
17737 "dvsdsv |= 5;\n"
17738 "int dsvvdvsdvvv = 123;",
17739 Alignment);
17740 verifyFormat("sfdbddfbdfbb ^= 5;\n"
17741 "dvsdsv <<= 5;\n"
17742 "int dsvvdvsdvvv = 123;",
17743 Alignment);
17744 verifyFormat("int xxx = 5;\n"
17745 "xxx = 5;\n"
17746 "{\n"
17747 " int yyy = 6;\n"
17748 " yyy = 6;\n"
17749 "}",
17750 Alignment);
17751 verifyFormat("int xxx = 5;\n"
17752 "xxx += 5;\n"
17753 "{\n"
17754 " int yyy = 6;\n"
17755 " yyy += 6;\n"
17756 "}",
17757 Alignment);
17758 // Test that `<=` is not treated as a compound assignment.
17759 verifyFormat("aa &= 5;\n"
17760 "b <= 10;\n"
17761 "c = 15;",
17762 Alignment);
17763 Alignment.AlignConsecutiveAssignments.PadOperators = true;
17764 verifyFormat("sfdbddfbdfbb = 5;\n"
17765 "dvsdsv = 5;\n"
17766 "int dsvvdvsdvvv = 123;",
17767 Alignment);
17768 verifyFormat("sfdbddfbdfbb ^= 5;\n"
17769 "dvsdsv |= 5;\n"
17770 "int dsvvdvsdvvv = 123;",
17771 Alignment);
17772 verifyFormat("sfdbddfbdfbb ^= 5;\n"
17773 "dvsdsv <<= 5;\n"
17774 "int dsvvdvsdvvv = 123;",
17775 Alignment);
17776 EXPECT_EQ("a += 5;\n"
17777 "one = 1;\n"
17778 "\n"
17779 "oneTwoThree = 123;\n",
17780 format("a += 5;\n"
17781 "one = 1;\n"
17782 "\n"
17783 "oneTwoThree = 123;\n",
17784 Alignment));
17785 EXPECT_EQ("a += 5;\n"
17786 "one = 1;\n"
17787 "//\n"
17788 "oneTwoThree = 123;\n",
17789 format("a += 5;\n"
17790 "one = 1;\n"
17791 "//\n"
17792 "oneTwoThree = 123;\n",
17793 Alignment));
17794 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
17795 EXPECT_EQ("a += 5;\n"
17796 "one = 1;\n"
17797 "\n"
17798 "oneTwoThree = 123;\n",
17799 format("a += 5;\n"
17800 "one = 1;\n"
17801 "\n"
17802 "oneTwoThree = 123;\n",
17803 Alignment));
17804 EXPECT_EQ("a += 5;\n"
17805 "one = 1;\n"
17806 "//\n"
17807 "oneTwoThree = 123;\n",
17808 format("a += 5;\n"
17809 "one = 1;\n"
17810 "//\n"
17811 "oneTwoThree = 123;\n",
17812 Alignment));
17813 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = false;
17814 Alignment.AlignConsecutiveAssignments.AcrossComments = true;
17815 EXPECT_EQ("a += 5;\n"
17816 "one = 1;\n"
17817 "\n"
17818 "oneTwoThree = 123;\n",
17819 format("a += 5;\n"
17820 "one = 1;\n"
17821 "\n"
17822 "oneTwoThree = 123;\n",
17823 Alignment));
17824 EXPECT_EQ("a += 5;\n"
17825 "one = 1;\n"
17826 "//\n"
17827 "oneTwoThree = 123;\n",
17828 format("a += 5;\n"
17829 "one = 1;\n"
17830 "//\n"
17831 "oneTwoThree = 123;\n",
17832 Alignment));
17833 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
17834 EXPECT_EQ("a += 5;\n"
17835 "one >>= 1;\n"
17836 "\n"
17837 "oneTwoThree = 123;\n",
17838 format("a += 5;\n"
17839 "one >>= 1;\n"
17840 "\n"
17841 "oneTwoThree = 123;\n",
17842 Alignment));
17843 EXPECT_EQ("a += 5;\n"
17844 "one = 1;\n"
17845 "//\n"
17846 "oneTwoThree <<= 123;\n",
17847 format("a += 5;\n"
17848 "one = 1;\n"
17849 "//\n"
17850 "oneTwoThree <<= 123;\n",
17851 Alignment));
17852 }
17853
17854 TEST_F(FormatTest, AlignConsecutiveAssignments) {
17855 FormatStyle Alignment = getLLVMStyle();
17856 Alignment.AlignConsecutiveMacros.Enabled = true;
17857 verifyFormat("int a = 5;\n"
17858 "int oneTwoThree = 123;",
17859 Alignment);
17860 verifyFormat("int a = 5;\n"
17861 "int oneTwoThree = 123;",
17862 Alignment);
17863
17864 Alignment.AlignConsecutiveAssignments.Enabled = true;
17865 verifyFormat("int a = 5;\n"
17866 "int oneTwoThree = 123;",
17867 Alignment);
17868 verifyFormat("int a = method();\n"
17869 "int oneTwoThree = 133;",
17870 Alignment);
17871 verifyFormat("aa <= 5;\n"
17872 "a &= 5;\n"
17873 "bcd *= 5;\n"
17874 "ghtyf += 5;\n"
17875 "dvfvdb -= 5;\n"
17876 "a /= 5;\n"
17877 "vdsvsv %= 5;\n"
17878 "sfdbddfbdfbb ^= 5;\n"
17879 "dvsdsv |= 5;\n"
17880 "int dsvvdvsdvvv = 123;",
17881 Alignment);
17882 verifyFormat("int i = 1, j = 10;\n"
17883 "something = 2000;",
17884 Alignment);
17885 verifyFormat("something = 2000;\n"
17886 "int i = 1, j = 10;\n",
17887 Alignment);
17888 verifyFormat("something = 2000;\n"
17889 "another = 911;\n"
17890 "int i = 1, j = 10;\n"
17891 "oneMore = 1;\n"
17892 "i = 2;",
17893 Alignment);
17894 verifyFormat("int a = 5;\n"
17895 "int one = 1;\n"
17896 "method();\n"
17897 "int oneTwoThree = 123;\n"
17898 "int oneTwo = 12;",
17899 Alignment);
17900 verifyFormat("int oneTwoThree = 123;\n"
17901 "int oneTwo = 12;\n"
17902 "method();\n",
17903 Alignment);
17904 verifyFormat("int oneTwoThree = 123; // comment\n"
17905 "int oneTwo = 12; // comment",
17906 Alignment);
17907 verifyFormat("int f() = default;\n"
17908 "int &operator() = default;\n"
17909 "int &operator=() {",
17910 Alignment);
17911 verifyFormat("int f() = delete;\n"
17912 "int &operator() = delete;\n"
17913 "int &operator=() {",
17914 Alignment);
17915 verifyFormat("int f() = default; // comment\n"
17916 "int &operator() = default; // comment\n"
17917 "int &operator=() {",
17918 Alignment);
17919 verifyFormat("int f() = default;\n"
17920 "int &operator() = default;\n"
17921 "int &operator==() {",
17922 Alignment);
17923 verifyFormat("int f() = default;\n"
17924 "int &operator() = default;\n"
17925 "int &operator<=() {",
17926 Alignment);
17927 verifyFormat("int f() = default;\n"
17928 "int &operator() = default;\n"
17929 "int &operator!=() {",
17930 Alignment);
17931 verifyFormat("int f() = default;\n"
17932 "int &operator() = default;\n"
17933 "int &operator=();",
17934 Alignment);
17935 verifyFormat("int f() = delete;\n"
17936 "int &operator() = delete;\n"
17937 "int &operator=();",
17938 Alignment);
17939 verifyFormat("/* long long padding */ int f() = default;\n"
17940 "int &operator() = default;\n"
17941 "int &operator/**/ =();",
17942 Alignment);
17943 // https://llvm.org/PR33697
17944 FormatStyle AlignmentWithPenalty = getLLVMStyle();
17945 AlignmentWithPenalty.AlignConsecutiveAssignments.Enabled = true;
17946 AlignmentWithPenalty.PenaltyReturnTypeOnItsOwnLine = 5000;
17947 verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n"
17948 " void f() = delete;\n"
17949 " SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n"
17950 " const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n"
17951 "};\n",
17952 AlignmentWithPenalty);
17953
17954 // Bug 25167
17955 /* Uncomment when fixed
17956 verifyFormat("#if A\n"
17957 "#else\n"
17958 "int aaaaaaaa = 12;\n"
17959 "#endif\n"
17960 "#if B\n"
17961 "#else\n"
17962 "int a = 12;\n"
17963 "#endif\n",
17964 Alignment);
17965 verifyFormat("enum foo {\n"
17966 "#if A\n"
17967 "#else\n"
17968 " aaaaaaaa = 12;\n"
17969 "#endif\n"
17970 "#if B\n"
17971 "#else\n"
17972 " a = 12;\n"
17973 "#endif\n"
17974 "};\n",
17975 Alignment);
17976 */
17977
17978 EXPECT_EQ("int a = 5;\n"
17979 "\n"
17980 "int oneTwoThree = 123;",
17981 format("int a = 5;\n"
17982 "\n"
17983 "int oneTwoThree= 123;",
17984 Alignment));
17985 EXPECT_EQ("int a = 5;\n"
17986 "int one = 1;\n"
17987 "\n"
17988 "int oneTwoThree = 123;",
17989 format("int a = 5;\n"
17990 "int one = 1;\n"
17991 "\n"
17992 "int oneTwoThree = 123;",
17993 Alignment));
17994 EXPECT_EQ("int a = 5;\n"
17995 "int one = 1;\n"
17996 "\n"
17997 "int oneTwoThree = 123;\n"
17998 "int oneTwo = 12;",
17999 format("int a = 5;\n"
18000 "int one = 1;\n"
18001 "\n"
18002 "int oneTwoThree = 123;\n"
18003 "int oneTwo = 12;",
18004 Alignment));
18005 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
18006 verifyFormat("#define A \\\n"
18007 " int aaaa = 12; \\\n"
18008 " int b = 23; \\\n"
18009 " int ccc = 234; \\\n"
18010 " int dddddddddd = 2345;",
18011 Alignment);
18012 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
18013 verifyFormat("#define A \\\n"
18014 " int aaaa = 12; \\\n"
18015 " int b = 23; \\\n"
18016 " int ccc = 234; \\\n"
18017 " int dddddddddd = 2345;",
18018 Alignment);
18019 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
18020 verifyFormat("#define A "
18021 " \\\n"
18022 " int aaaa = 12; "
18023 " \\\n"
18024 " int b = 23; "
18025 " \\\n"
18026 " int ccc = 234; "
18027 " \\\n"
18028 " int dddddddddd = 2345;",
18029 Alignment);
18030 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
18031 "k = 4, int l = 5,\n"
18032 " int m = 6) {\n"
18033 " int j = 10;\n"
18034 " otherThing = 1;\n"
18035 "}",
18036 Alignment);
18037 verifyFormat("void SomeFunction(int parameter = 0) {\n"
18038 " int i = 1;\n"
18039 " int j = 2;\n"
18040 " int big = 10000;\n"
18041 "}",
18042 Alignment);
18043 verifyFormat("class C {\n"
18044 "public:\n"
18045 " int i = 1;\n"
18046 " virtual void f() = 0;\n"
18047 "};",
18048 Alignment);
18049 verifyFormat("int i = 1;\n"
18050 "if (SomeType t = getSomething()) {\n"
18051 "}\n"
18052 "int j = 2;\n"
18053 "int big = 10000;",
18054 Alignment);
18055 verifyFormat("int j = 7;\n"
18056 "for (int k = 0; k < N; ++k) {\n"
18057 "}\n"
18058 "int j = 2;\n"
18059 "int big = 10000;\n"
18060 "}",
18061 Alignment);
18062 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
18063 verifyFormat("int i = 1;\n"
18064 "LooooooooooongType loooooooooooooooooooooongVariable\n"
18065 " = someLooooooooooooooooongFunction();\n"
18066 "int j = 2;",
18067 Alignment);
18068 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
18069 verifyFormat("int i = 1;\n"
18070 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
18071 " someLooooooooooooooooongFunction();\n"
18072 "int j = 2;",
18073 Alignment);
18074
18075 verifyFormat("auto lambda = []() {\n"
18076 " auto i = 0;\n"
18077 " return 0;\n"
18078 "};\n"
18079 "int i = 0;\n"
18080 "auto v = type{\n"
18081 " i = 1, //\n"
18082 " (i = 2), //\n"
18083 " i = 3 //\n"
18084 "};",
18085 Alignment);
18086
18087 verifyFormat(
18088 "int i = 1;\n"
18089 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
18090 " loooooooooooooooooooooongParameterB);\n"
18091 "int j = 2;",
18092 Alignment);
18093
18094 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
18095 " typename B = very_long_type_name_1,\n"
18096 " typename T_2 = very_long_type_name_2>\n"
18097 "auto foo() {}\n",
18098 Alignment);
18099 verifyFormat("int a, b = 1;\n"
18100 "int c = 2;\n"
18101 "int dd = 3;\n",
18102 Alignment);
18103 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
18104 "float b[1][] = {{3.f}};\n",
18105 Alignment);
18106 verifyFormat("for (int i = 0; i < 1; i++)\n"
18107 " int x = 1;\n",
18108 Alignment);
18109 verifyFormat("for (i = 0; i < 1; i++)\n"
18110 " x = 1;\n"
18111 "y = 1;\n",
18112 Alignment);
18113
18114 EXPECT_EQ(Alignment.ReflowComments, true);
18115 Alignment.ColumnLimit = 50;
18116 EXPECT_EQ("int x = 0;\n"
18117 "int yy = 1; /// specificlennospace\n"
18118 "int zzz = 2;\n",
18119 format("int x = 0;\n"
18120 "int yy = 1; ///specificlennospace\n"
18121 "int zzz = 2;\n",
18122 Alignment));
18123
18124 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
18125 "auto b = [] {\n"
18126 " f();\n"
18127 " return;\n"
18128 "};",
18129 Alignment);
18130 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
18131 "auto b = g([] {\n"
18132 " f();\n"
18133 " return;\n"
18134 "});",
18135 Alignment);
18136 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
18137 "auto b = g(param, [] {\n"
18138 " f();\n"
18139 " return;\n"
18140 "});",
18141 Alignment);
18142 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
18143 "auto b = [] {\n"
18144 " if (condition) {\n"
18145 " return;\n"
18146 " }\n"
18147 "};",
18148 Alignment);
18149
18150 verifyFormat("auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
18151 " ccc ? aaaaa : bbbbb,\n"
18152 " dddddddddddddddddddddddddd);",
18153 Alignment);
18154 // FIXME: https://llvm.org/PR53497
18155 // verifyFormat("auto aaaaaaaaaaaa = f();\n"
18156 // "auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
18157 // " ccc ? aaaaa : bbbbb,\n"
18158 // " dddddddddddddddddddddddddd);",
18159 // Alignment);
18160
18161 // Confirm proper handling of AlignConsecutiveAssignments with
18162 // BinPackArguments.
18163 // See https://llvm.org/PR55360
18164 Alignment = getLLVMStyleWithColumns(50);
18165 Alignment.AlignConsecutiveAssignments.Enabled = true;
18166 Alignment.BinPackArguments = false;
18167 verifyFormat("int a_long_name = 1;\n"
18168 "auto b = B({a_long_name, a_long_name},\n"
18169 " {a_longer_name_for_wrap,\n"
18170 " a_longer_name_for_wrap});",
18171 Alignment);
18172 verifyFormat("int a_long_name = 1;\n"
18173 "auto b = B{{a_long_name, a_long_name},\n"
18174 " {a_longer_name_for_wrap,\n"
18175 " a_longer_name_for_wrap}};",
18176 Alignment);
18177 }
18178
18179 TEST_F(FormatTest, AlignConsecutiveBitFields) {
18180 FormatStyle Alignment = getLLVMStyle();
18181 Alignment.AlignConsecutiveBitFields.Enabled = true;
18182 verifyFormat("int const a : 5;\n"
18183 "int oneTwoThree : 23;",
18184 Alignment);
18185
18186 // Initializers are allowed starting with c++2a
18187 verifyFormat("int const a : 5 = 1;\n"
18188 "int oneTwoThree : 23 = 0;",
18189 Alignment);
18190
18191 Alignment.AlignConsecutiveDeclarations.Enabled = true;
18192 verifyFormat("int const a : 5;\n"
18193 "int oneTwoThree : 23;",
18194 Alignment);
18195
18196 verifyFormat("int const a : 5; // comment\n"
18197 "int oneTwoThree : 23; // comment",
18198 Alignment);
18199
18200 verifyFormat("int const a : 5 = 1;\n"
18201 "int oneTwoThree : 23 = 0;",
18202 Alignment);
18203
18204 Alignment.AlignConsecutiveAssignments.Enabled = true;
18205 verifyFormat("int const a : 5 = 1;\n"
18206 "int oneTwoThree : 23 = 0;",
18207 Alignment);
18208 verifyFormat("int const a : 5 = {1};\n"
18209 "int oneTwoThree : 23 = 0;",
18210 Alignment);
18211
18212 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None;
18213 verifyFormat("int const a :5;\n"
18214 "int oneTwoThree:23;",
18215 Alignment);
18216
18217 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before;
18218 verifyFormat("int const a :5;\n"
18219 "int oneTwoThree :23;",
18220 Alignment);
18221
18222 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After;
18223 verifyFormat("int const a : 5;\n"
18224 "int oneTwoThree: 23;",
18225 Alignment);
18226
18227 // Known limitations: ':' is only recognized as a bitfield colon when
18228 // followed by a number.
18229 /*
18230 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
18231 "int a : 5;",
18232 Alignment);
18233 */
18234 }
18235
18236 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
18237 FormatStyle Alignment = getLLVMStyle();
18238 Alignment.AlignConsecutiveMacros.Enabled = true;
18239 Alignment.PointerAlignment = FormatStyle::PAS_Right;
18240 verifyFormat("float const a = 5;\n"
18241 "int oneTwoThree = 123;",
18242 Alignment);
18243 verifyFormat("int a = 5;\n"
18244 "float const oneTwoThree = 123;",
18245 Alignment);
18246
18247 Alignment.AlignConsecutiveDeclarations.Enabled = true;
18248 verifyFormat("float const a = 5;\n"
18249 "int oneTwoThree = 123;",
18250 Alignment);
18251 verifyFormat("int a = method();\n"
18252 "float const oneTwoThree = 133;",
18253 Alignment);
18254 verifyFormat("int i = 1, j = 10;\n"
18255 "something = 2000;",
18256 Alignment);
18257 verifyFormat("something = 2000;\n"
18258 "int i = 1, j = 10;\n",
18259 Alignment);
18260 verifyFormat("float something = 2000;\n"
18261 "double another = 911;\n"
18262 "int i = 1, j = 10;\n"
18263 "const int *oneMore = 1;\n"
18264 "unsigned i = 2;",
18265 Alignment);
18266 verifyFormat("float a = 5;\n"
18267 "int one = 1;\n"
18268 "method();\n"
18269 "const double oneTwoThree = 123;\n"
18270 "const unsigned int oneTwo = 12;",
18271 Alignment);
18272 verifyFormat("int oneTwoThree{0}; // comment\n"
18273 "unsigned oneTwo; // comment",
18274 Alignment);
18275 verifyFormat("unsigned int *a;\n"
18276 "int *b;\n"
18277 "unsigned int Const *c;\n"
18278 "unsigned int const *d;\n"
18279 "unsigned int Const &e;\n"
18280 "unsigned int const &f;",
18281 Alignment);
18282 verifyFormat("Const unsigned int *c;\n"
18283 "const unsigned int *d;\n"
18284 "Const unsigned int &e;\n"
18285 "const unsigned int &f;\n"
18286 "const unsigned g;\n"
18287 "Const unsigned h;",
18288 Alignment);
18289 EXPECT_EQ("float const a = 5;\n"
18290 "\n"
18291 "int oneTwoThree = 123;",
18292 format("float const a = 5;\n"
18293 "\n"
18294 "int oneTwoThree= 123;",
18295 Alignment));
18296 EXPECT_EQ("float a = 5;\n"
18297 "int one = 1;\n"
18298 "\n"
18299 "unsigned oneTwoThree = 123;",
18300 format("float a = 5;\n"
18301 "int one = 1;\n"
18302 "\n"
18303 "unsigned oneTwoThree = 123;",
18304 Alignment));
18305 EXPECT_EQ("float a = 5;\n"
18306 "int one = 1;\n"
18307 "\n"
18308 "unsigned oneTwoThree = 123;\n"
18309 "int oneTwo = 12;",
18310 format("float a = 5;\n"
18311 "int one = 1;\n"
18312 "\n"
18313 "unsigned oneTwoThree = 123;\n"
18314 "int oneTwo = 12;",
18315 Alignment));
18316 // Function prototype alignment
18317 verifyFormat("int a();\n"
18318 "double b();",
18319 Alignment);
18320 verifyFormat("int a(int x);\n"
18321 "double b();",
18322 Alignment);
18323 unsigned OldColumnLimit = Alignment.ColumnLimit;
18324 // We need to set ColumnLimit to zero, in order to stress nested alignments,
18325 // otherwise the function parameters will be re-flowed onto a single line.
18326 Alignment.ColumnLimit = 0;
18327 EXPECT_EQ("int a(int x,\n"
18328 " float y);\n"
18329 "double b(int x,\n"
18330 " double y);",
18331 format("int a(int x,\n"
18332 " float y);\n"
18333 "double b(int x,\n"
18334 " double y);",
18335 Alignment));
18336 // This ensures that function parameters of function declarations are
18337 // correctly indented when their owning functions are indented.
18338 // The failure case here is for 'double y' to not be indented enough.
18339 EXPECT_EQ("double a(int x);\n"
18340 "int b(int y,\n"
18341 " double z);",
18342 format("double a(int x);\n"
18343 "int b(int y,\n"
18344 " double z);",
18345 Alignment));
18346 // Set ColumnLimit low so that we induce wrapping immediately after
18347 // the function name and opening paren.
18348 Alignment.ColumnLimit = 13;
18349 verifyFormat("int function(\n"
18350 " int x,\n"
18351 " bool y);",
18352 Alignment);
18353 Alignment.ColumnLimit = OldColumnLimit;
18354 // Ensure function pointers don't screw up recursive alignment
18355 verifyFormat("int a(int x, void (*fp)(int y));\n"
18356 "double b();",
18357 Alignment);
18358 Alignment.AlignConsecutiveAssignments.Enabled = true;
18359 // Ensure recursive alignment is broken by function braces, so that the
18360 // "a = 1" does not align with subsequent assignments inside the function
18361 // body.
18362 verifyFormat("int func(int a = 1) {\n"
18363 " int b = 2;\n"
18364 " int cc = 3;\n"
18365 "}",
18366 Alignment);
18367 verifyFormat("float something = 2000;\n"
18368 "double another = 911;\n"
18369 "int i = 1, j = 10;\n"
18370 "const int *oneMore = 1;\n"
18371 "unsigned i = 2;",
18372 Alignment);
18373 verifyFormat("int oneTwoThree = {0}; // comment\n"
18374 "unsigned oneTwo = 0; // comment",
18375 Alignment);
18376 // Make sure that scope is correctly tracked, in the absence of braces
18377 verifyFormat("for (int i = 0; i < n; i++)\n"
18378 " j = i;\n"
18379 "double x = 1;\n",
18380 Alignment);
18381 verifyFormat("if (int i = 0)\n"
18382 " j = i;\n"
18383 "double x = 1;\n",
18384 Alignment);
18385 // Ensure operator[] and operator() are comprehended
18386 verifyFormat("struct test {\n"
18387 " long long int foo();\n"
18388 " int operator[](int a);\n"
18389 " double bar();\n"
18390 "};\n",
18391 Alignment);
18392 verifyFormat("struct test {\n"
18393 " long long int foo();\n"
18394 " int operator()(int a);\n"
18395 " double bar();\n"
18396 "};\n",
18397 Alignment);
18398 // http://llvm.org/PR52914
18399 verifyFormat("char *a[] = {\"a\", // comment\n"
18400 " \"bb\"};\n"
18401 "int bbbbbbb = 0;",
18402 Alignment);
18403
18404 // PAS_Right
18405 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
18406 " int const i = 1;\n"
18407 " int *j = 2;\n"
18408 " int big = 10000;\n"
18409 "\n"
18410 " unsigned oneTwoThree = 123;\n"
18411 " int oneTwo = 12;\n"
18412 " method();\n"
18413 " float k = 2;\n"
18414 " int ll = 10000;\n"
18415 "}",
18416 format("void SomeFunction(int parameter= 0) {\n"
18417 " int const i= 1;\n"
18418 " int *j=2;\n"
18419 " int big = 10000;\n"
18420 "\n"
18421 "unsigned oneTwoThree =123;\n"
18422 "int oneTwo = 12;\n"
18423 " method();\n"
18424 "float k= 2;\n"
18425 "int ll=10000;\n"
18426 "}",
18427 Alignment));
18428 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
18429 " int const i = 1;\n"
18430 " int **j = 2, ***k;\n"
18431 " int &k = i;\n"
18432 " int &&l = i + j;\n"
18433 " int big = 10000;\n"
18434 "\n"
18435 " unsigned oneTwoThree = 123;\n"
18436 " int oneTwo = 12;\n"
18437 " method();\n"
18438 " float k = 2;\n"
18439 " int ll = 10000;\n"
18440 "}",
18441 format("void SomeFunction(int parameter= 0) {\n"
18442 " int const i= 1;\n"
18443 " int **j=2,***k;\n"
18444 "int &k=i;\n"
18445 "int &&l=i+j;\n"
18446 " int big = 10000;\n"
18447 "\n"
18448 "unsigned oneTwoThree =123;\n"
18449 "int oneTwo = 12;\n"
18450 " method();\n"
18451 "float k= 2;\n"
18452 "int ll=10000;\n"
18453 "}",
18454 Alignment));
18455 // variables are aligned at their name, pointers are at the right most
18456 // position
18457 verifyFormat("int *a;\n"
18458 "int **b;\n"
18459 "int ***c;\n"
18460 "int foobar;\n",
18461 Alignment);
18462
18463 // PAS_Left
18464 FormatStyle AlignmentLeft = Alignment;
18465 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left;
18466 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
18467 " int const i = 1;\n"
18468 " int* j = 2;\n"
18469 " int big = 10000;\n"
18470 "\n"
18471 " unsigned oneTwoThree = 123;\n"
18472 " int oneTwo = 12;\n"
18473 " method();\n"
18474 " float k = 2;\n"
18475 " int ll = 10000;\n"
18476 "}",
18477 format("void SomeFunction(int parameter= 0) {\n"
18478 " int const i= 1;\n"
18479 " int *j=2;\n"
18480 " int big = 10000;\n"
18481 "\n"
18482 "unsigned oneTwoThree =123;\n"
18483 "int oneTwo = 12;\n"
18484 " method();\n"
18485 "float k= 2;\n"
18486 "int ll=10000;\n"
18487 "}",
18488 AlignmentLeft));
18489 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
18490 " int const i = 1;\n"
18491 " int** j = 2;\n"
18492 " int& k = i;\n"
18493 " int&& l = i + j;\n"
18494 " int big = 10000;\n"
18495 "\n"
18496 " unsigned oneTwoThree = 123;\n"
18497 " int oneTwo = 12;\n"
18498 " method();\n"
18499 " float k = 2;\n"
18500 " int ll = 10000;\n"
18501 "}",
18502 format("void SomeFunction(int parameter= 0) {\n"
18503 " int const i= 1;\n"
18504 " int **j=2;\n"
18505 "int &k=i;\n"
18506 "int &&l=i+j;\n"
18507 " int big = 10000;\n"
18508 "\n"
18509 "unsigned oneTwoThree =123;\n"
18510 "int oneTwo = 12;\n"
18511 " method();\n"
18512 "float k= 2;\n"
18513 "int ll=10000;\n"
18514 "}",
18515 AlignmentLeft));
18516 // variables are aligned at their name, pointers are at the left most position
18517 verifyFormat("int* a;\n"
18518 "int** b;\n"
18519 "int*** c;\n"
18520 "int foobar;\n",
18521 AlignmentLeft);
18522
18523 // PAS_Middle
18524 FormatStyle AlignmentMiddle = Alignment;
18525 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
18526 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
18527 " int const i = 1;\n"
18528 " int * j = 2;\n"
18529 " int big = 10000;\n"
18530 "\n"
18531 " unsigned oneTwoThree = 123;\n"
18532 " int oneTwo = 12;\n"
18533 " method();\n"
18534 " float k = 2;\n"
18535 " int ll = 10000;\n"
18536 "}",
18537 format("void SomeFunction(int parameter= 0) {\n"
18538 " int const i= 1;\n"
18539 " int *j=2;\n"
18540 " int big = 10000;\n"
18541 "\n"
18542 "unsigned oneTwoThree =123;\n"
18543 "int oneTwo = 12;\n"
18544 " method();\n"
18545 "float k= 2;\n"
18546 "int ll=10000;\n"
18547 "}",
18548 AlignmentMiddle));
18549 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
18550 " int const i = 1;\n"
18551 " int ** j = 2, ***k;\n"
18552 " int & k = i;\n"
18553 " int && l = i + j;\n"
18554 " int big = 10000;\n"
18555 "\n"
18556 " unsigned oneTwoThree = 123;\n"
18557 " int oneTwo = 12;\n"
18558 " method();\n"
18559 " float k = 2;\n"
18560 " int ll = 10000;\n"
18561 "}",
18562 format("void SomeFunction(int parameter= 0) {\n"
18563 " int const i= 1;\n"
18564 " int **j=2,***k;\n"
18565 "int &k=i;\n"
18566 "int &&l=i+j;\n"
18567 " int big = 10000;\n"
18568 "\n"
18569 "unsigned oneTwoThree =123;\n"
18570 "int oneTwo = 12;\n"
18571 " method();\n"
18572 "float k= 2;\n"
18573 "int ll=10000;\n"
18574 "}",
18575 AlignmentMiddle));
18576 // variables are aligned at their name, pointers are in the middle
18577 verifyFormat("int * a;\n"
18578 "int * b;\n"
18579 "int *** c;\n"
18580 "int foobar;\n",
18581 AlignmentMiddle);
18582
18583 Alignment.AlignConsecutiveAssignments.Enabled = false;
18584 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
18585 verifyFormat("#define A \\\n"
18586 " int aaaa = 12; \\\n"
18587 " float b = 23; \\\n"
18588 " const int ccc = 234; \\\n"
18589 " unsigned dddddddddd = 2345;",
18590 Alignment);
18591 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
18592 verifyFormat("#define A \\\n"
18593 " int aaaa = 12; \\\n"
18594 " float b = 23; \\\n"
18595 " const int ccc = 234; \\\n"
18596 " unsigned dddddddddd = 2345;",
18597 Alignment);
18598 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
18599 Alignment.ColumnLimit = 30;
18600 verifyFormat("#define A \\\n"
18601 " int aaaa = 12; \\\n"
18602 " float b = 23; \\\n"
18603 " const int ccc = 234; \\\n"
18604 " int dddddddddd = 2345;",
18605 Alignment);
18606 Alignment.ColumnLimit = 80;
18607 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
18608 "k = 4, int l = 5,\n"
18609 " int m = 6) {\n"
18610 " const int j = 10;\n"
18611 " otherThing = 1;\n"
18612 "}",
18613 Alignment);
18614 verifyFormat("void SomeFunction(int parameter = 0) {\n"
18615 " int const i = 1;\n"
18616 " int *j = 2;\n"
18617 " int big = 10000;\n"
18618 "}",
18619 Alignment);
18620 verifyFormat("class C {\n"
18621 "public:\n"
18622 " int i = 1;\n"
18623 " virtual void f() = 0;\n"
18624 "};",
18625 Alignment);
18626 verifyFormat("float i = 1;\n"
18627 "if (SomeType t = getSomething()) {\n"
18628 "}\n"
18629 "const unsigned j = 2;\n"
18630 "int big = 10000;",
18631 Alignment);
18632 verifyFormat("float j = 7;\n"
18633 "for (int k = 0; k < N; ++k) {\n"
18634 "}\n"
18635 "unsigned j = 2;\n"
18636 "int big = 10000;\n"
18637 "}",
18638 Alignment);
18639 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
18640 verifyFormat("float i = 1;\n"
18641 "LooooooooooongType loooooooooooooooooooooongVariable\n"
18642 " = someLooooooooooooooooongFunction();\n"
18643 "int j = 2;",
18644 Alignment);
18645 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
18646 verifyFormat("int i = 1;\n"
18647 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
18648 " someLooooooooooooooooongFunction();\n"
18649 "int j = 2;",
18650 Alignment);
18651
18652 Alignment.AlignConsecutiveAssignments.Enabled = true;
18653 verifyFormat("auto lambda = []() {\n"
18654 " auto ii = 0;\n"
18655 " float j = 0;\n"
18656 " return 0;\n"
18657 "};\n"
18658 "int i = 0;\n"
18659 "float i2 = 0;\n"
18660 "auto v = type{\n"
18661 " i = 1, //\n"
18662 " (i = 2), //\n"
18663 " i = 3 //\n"
18664 "};",
18665 Alignment);
18666 Alignment.AlignConsecutiveAssignments.Enabled = false;
18667
18668 verifyFormat(
18669 "int i = 1;\n"
18670 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
18671 " loooooooooooooooooooooongParameterB);\n"
18672 "int j = 2;",
18673 Alignment);
18674
18675 // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
18676 // We expect declarations and assignments to align, as long as it doesn't
18677 // exceed the column limit, starting a new alignment sequence whenever it
18678 // happens.
18679 Alignment.AlignConsecutiveAssignments.Enabled = true;
18680 Alignment.ColumnLimit = 30;
18681 verifyFormat("float ii = 1;\n"
18682 "unsigned j = 2;\n"
18683 "int someVerylongVariable = 1;\n"
18684 "AnotherLongType ll = 123456;\n"
18685 "VeryVeryLongType k = 2;\n"
18686 "int myvar = 1;",
18687 Alignment);
18688 Alignment.ColumnLimit = 80;
18689 Alignment.AlignConsecutiveAssignments.Enabled = false;
18690
18691 verifyFormat(
18692 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
18693 " typename LongType, typename B>\n"
18694 "auto foo() {}\n",
18695 Alignment);
18696 verifyFormat("float a, b = 1;\n"
18697 "int c = 2;\n"
18698 "int dd = 3;\n",
18699 Alignment);
18700 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
18701 "float b[1][] = {{3.f}};\n",
18702 Alignment);
18703 Alignment.AlignConsecutiveAssignments.Enabled = true;
18704 verifyFormat("float a, b = 1;\n"
18705 "int c = 2;\n"
18706 "int dd = 3;\n",
18707 Alignment);
18708 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
18709 "float b[1][] = {{3.f}};\n",
18710 Alignment);
18711 Alignment.AlignConsecutiveAssignments.Enabled = false;
18712
18713 Alignment.ColumnLimit = 30;
18714 Alignment.BinPackParameters = false;
18715 verifyFormat("void foo(float a,\n"
18716 " float b,\n"
18717 " int c,\n"
18718 " uint32_t *d) {\n"
18719 " int *e = 0;\n"
18720 " float f = 0;\n"
18721 " double g = 0;\n"
18722 "}\n"
18723 "void bar(ino_t a,\n"
18724 " int b,\n"
18725 " uint32_t *c,\n"
18726 " bool d) {}\n",
18727 Alignment);
18728 Alignment.BinPackParameters = true;
18729 Alignment.ColumnLimit = 80;
18730
18731 // Bug 33507
18732 Alignment.PointerAlignment = FormatStyle::PAS_Middle;
18733 verifyFormat(
18734 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
18735 " static const Version verVs2017;\n"
18736 " return true;\n"
18737 "});\n",
18738 Alignment);
18739 Alignment.PointerAlignment = FormatStyle::PAS_Right;
18740
18741 // See llvm.org/PR35641
18742 Alignment.AlignConsecutiveDeclarations.Enabled = true;
18743 verifyFormat("int func() { //\n"
18744 " int b;\n"
18745 " unsigned c;\n"
18746 "}",
18747 Alignment);
18748
18749 // See PR37175
18750 FormatStyle Style = getMozillaStyle();
18751 Style.AlignConsecutiveDeclarations.Enabled = true;
18752 EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
18753 "foo(int a);",
18754 format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style));
18755
18756 Alignment.PointerAlignment = FormatStyle::PAS_Left;
18757 verifyFormat("unsigned int* a;\n"
18758 "int* b;\n"
18759 "unsigned int Const* c;\n"
18760 "unsigned int const* d;\n"
18761 "unsigned int Const& e;\n"
18762 "unsigned int const& f;",
18763 Alignment);
18764 verifyFormat("Const unsigned int* c;\n"
18765 "const unsigned int* d;\n"
18766 "Const unsigned int& e;\n"
18767 "const unsigned int& f;\n"
18768 "const unsigned g;\n"
18769 "Const unsigned h;",
18770 Alignment);
18771
18772 Alignment.PointerAlignment = FormatStyle::PAS_Middle;
18773 verifyFormat("unsigned int * a;\n"
18774 "int * b;\n"
18775 "unsigned int Const * c;\n"
18776 "unsigned int const * d;\n"
18777 "unsigned int Const & e;\n"
18778 "unsigned int const & f;",
18779 Alignment);
18780 verifyFormat("Const unsigned int * c;\n"
18781 "const unsigned int * d;\n"
18782 "Const unsigned int & e;\n"
18783 "const unsigned int & f;\n"
18784 "const unsigned g;\n"
18785 "Const unsigned h;",
18786 Alignment);
18787
18788 // See PR46529
18789 FormatStyle BracedAlign = getLLVMStyle();
18790 BracedAlign.AlignConsecutiveDeclarations.Enabled = true;
18791 verifyFormat("const auto result{[]() {\n"
18792 " const auto something = 1;\n"
18793 " return 2;\n"
18794 "}};",
18795 BracedAlign);
18796 verifyFormat("int foo{[]() {\n"
18797 " int bar{0};\n"
18798 " return 0;\n"
18799 "}()};",
18800 BracedAlign);
18801 BracedAlign.Cpp11BracedListStyle = false;
18802 verifyFormat("const auto result{ []() {\n"
18803 " const auto something = 1;\n"
18804 " return 2;\n"
18805 "} };",
18806 BracedAlign);
18807 verifyFormat("int foo{ []() {\n"
18808 " int bar{ 0 };\n"
18809 " return 0;\n"
18810 "}() };",
18811 BracedAlign);
18812 }
18813
18814 TEST_F(FormatTest, AlignWithLineBreaks) {
18815 auto Style = getLLVMStyleWithColumns(120);
18816
18817 EXPECT_EQ(Style.AlignConsecutiveAssignments,
18818 FormatStyle::AlignConsecutiveStyle(
18819 {/*Enabled=*/false, /*AcrossEmptyLines=*/false,
18820 /*AcrossComments=*/false, /*AlignCompound=*/false,
18821 /*PadOperators=*/true}));
18822 EXPECT_EQ(Style.AlignConsecutiveDeclarations,
18823 FormatStyle::AlignConsecutiveStyle({}));
18824 verifyFormat("void foo() {\n"
18825 " int myVar = 5;\n"
18826 " double x = 3.14;\n"
18827 " auto str = \"Hello \"\n"
18828 " \"World\";\n"
18829 " auto s = \"Hello \"\n"
18830 " \"Again\";\n"
18831 "}",
18832 Style);
18833
18834 // clang-format off
18835 verifyFormat("void foo() {\n"
18836 " const int capacityBefore = Entries.capacity();\n"
18837 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
18838 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
18839 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
18840 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
18841 "}",
18842 Style);
18843 // clang-format on
18844
18845 Style.AlignConsecutiveAssignments.Enabled = true;
18846 verifyFormat("void foo() {\n"
18847 " int myVar = 5;\n"
18848 " double x = 3.14;\n"
18849 " auto str = \"Hello \"\n"
18850 " \"World\";\n"
18851 " auto s = \"Hello \"\n"
18852 " \"Again\";\n"
18853 "}",
18854 Style);
18855
18856 // clang-format off
18857 verifyFormat("void foo() {\n"
18858 " const int capacityBefore = Entries.capacity();\n"
18859 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
18860 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
18861 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
18862 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
18863 "}",
18864 Style);
18865 // clang-format on
18866
18867 Style.AlignConsecutiveAssignments.Enabled = false;
18868 Style.AlignConsecutiveDeclarations.Enabled = true;
18869 verifyFormat("void foo() {\n"
18870 " int myVar = 5;\n"
18871 " double x = 3.14;\n"
18872 " auto str = \"Hello \"\n"
18873 " \"World\";\n"
18874 " auto s = \"Hello \"\n"
18875 " \"Again\";\n"
18876 "}",
18877 Style);
18878
18879 // clang-format off
18880 verifyFormat("void foo() {\n"
18881 " const int capacityBefore = Entries.capacity();\n"
18882 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
18883 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
18884 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
18885 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
18886 "}",
18887 Style);
18888 // clang-format on
18889
18890 Style.AlignConsecutiveAssignments.Enabled = true;
18891 Style.AlignConsecutiveDeclarations.Enabled = true;
18892
18893 verifyFormat("void foo() {\n"
18894 " int myVar = 5;\n"
18895 " double x = 3.14;\n"
18896 " auto str = \"Hello \"\n"
18897 " \"World\";\n"
18898 " auto s = \"Hello \"\n"
18899 " \"Again\";\n"
18900 "}",
18901 Style);
18902
18903 // clang-format off
18904 verifyFormat("void foo() {\n"
18905 " const int capacityBefore = Entries.capacity();\n"
18906 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
18907 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
18908 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
18909 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
18910 "}",
18911 Style);
18912 // clang-format on
18913
18914 Style = getLLVMStyleWithColumns(20);
18915 Style.AlignConsecutiveAssignments.Enabled = true;
18916 Style.IndentWidth = 4;
18917
18918 verifyFormat("void foo() {\n"
18919 " int i1 = 1;\n"
18920 " int j = 0;\n"
18921 " int k = bar(\n"
18922 " argument1,\n"
18923 " argument2);\n"
18924 "}",
18925 Style);
18926
18927 verifyFormat("unsigned i = 0;\n"
18928 "int a[] = {\n"
18929 " 1234567890,\n"
18930 " -1234567890};",
18931 Style);
18932
18933 Style.ColumnLimit = 120;
18934
18935 // clang-format off
18936 verifyFormat("void SomeFunc() {\n"
18937 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
18938 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
18939 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
18940 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
18941 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
18942 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
18943 "}",
18944 Style);
18945 // clang-format on
18946
18947 Style.BinPackArguments = false;
18948
18949 // clang-format off
18950 verifyFormat("void SomeFunc() {\n"
18951 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n"
18952 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
18953 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n"
18954 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
18955 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n"
18956 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
18957 "}",
18958 Style);
18959 // clang-format on
18960 }
18961
18962 TEST_F(FormatTest, AlignWithInitializerPeriods) {
18963 auto Style = getLLVMStyleWithColumns(60);
18964
18965 verifyFormat("void foo1(void) {\n"
18966 " BYTE p[1] = 1;\n"
18967 " A B = {.one_foooooooooooooooo = 2,\n"
18968 " .two_fooooooooooooo = 3,\n"
18969 " .three_fooooooooooooo = 4};\n"
18970 " BYTE payload = 2;\n"
18971 "}",
18972 Style);
18973
18974 Style.AlignConsecutiveAssignments.Enabled = true;
18975 Style.AlignConsecutiveDeclarations.Enabled = false;
18976 verifyFormat("void foo2(void) {\n"
18977 " BYTE p[1] = 1;\n"
18978 " A B = {.one_foooooooooooooooo = 2,\n"
18979 " .two_fooooooooooooo = 3,\n"
18980 " .three_fooooooooooooo = 4};\n"
18981 " BYTE payload = 2;\n"
18982 "}",
18983 Style);
18984
18985 Style.AlignConsecutiveAssignments.Enabled = false;
18986 Style.AlignConsecutiveDeclarations.Enabled = true;
18987 verifyFormat("void foo3(void) {\n"
18988 " BYTE p[1] = 1;\n"
18989 " A B = {.one_foooooooooooooooo = 2,\n"
18990 " .two_fooooooooooooo = 3,\n"
18991 " .three_fooooooooooooo = 4};\n"
18992 " BYTE payload = 2;\n"
18993 "}",
18994 Style);
18995
18996 Style.AlignConsecutiveAssignments.Enabled = true;
18997 Style.AlignConsecutiveDeclarations.Enabled = true;
18998 verifyFormat("void foo4(void) {\n"
18999 " BYTE p[1] = 1;\n"
19000 " A B = {.one_foooooooooooooooo = 2,\n"
19001 " .two_fooooooooooooo = 3,\n"
19002 " .three_fooooooooooooo = 4};\n"
19003 " BYTE payload = 2;\n"
19004 "}",
19005 Style);
19006 }
19007
19008 TEST_F(FormatTest, LinuxBraceBreaking) {
19009 FormatStyle LinuxBraceStyle = getLLVMStyle();
19010 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
19011 verifyFormat("namespace a\n"
19012 "{\n"
19013 "class A\n"
19014 "{\n"
19015 " void f()\n"
19016 " {\n"
19017 " if (true) {\n"
19018 " a();\n"
19019 " b();\n"
19020 " } else {\n"
19021 " a();\n"
19022 " }\n"
19023 " }\n"
19024 " void g() { return; }\n"
19025 "};\n"
19026 "struct B {\n"
19027 " int x;\n"
19028 "};\n"
19029 "} // namespace a\n",
19030 LinuxBraceStyle);
19031 verifyFormat("enum X {\n"
19032 " Y = 0,\n"
19033 "}\n",
19034 LinuxBraceStyle);
19035 verifyFormat("struct S {\n"
19036 " int Type;\n"
19037 " union {\n"
19038 " int x;\n"
19039 " double y;\n"
19040 " } Value;\n"
19041 " class C\n"
19042 " {\n"
19043 " MyFavoriteType Value;\n"
19044 " } Class;\n"
19045 "}\n",
19046 LinuxBraceStyle);
19047 }
19048
19049 TEST_F(FormatTest, MozillaBraceBreaking) {
19050 FormatStyle MozillaBraceStyle = getLLVMStyle();
19051 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
19052 MozillaBraceStyle.FixNamespaceComments = false;
19053 verifyFormat("namespace a {\n"
19054 "class A\n"
19055 "{\n"
19056 " void f()\n"
19057 " {\n"
19058 " if (true) {\n"
19059 " a();\n"
19060 " b();\n"
19061 " }\n"
19062 " }\n"
19063 " void g() { return; }\n"
19064 "};\n"
19065 "enum E\n"
19066 "{\n"
19067 " A,\n"
19068 " // foo\n"
19069 " B,\n"
19070 " C\n"
19071 "};\n"
19072 "struct B\n"
19073 "{\n"
19074 " int x;\n"
19075 "};\n"
19076 "}\n",
19077 MozillaBraceStyle);
19078 verifyFormat("struct S\n"
19079 "{\n"
19080 " int Type;\n"
19081 " union\n"
19082 " {\n"
19083 " int x;\n"
19084 " double y;\n"
19085 " } Value;\n"
19086 " class C\n"
19087 " {\n"
19088 " MyFavoriteType Value;\n"
19089 " } Class;\n"
19090 "}\n",
19091 MozillaBraceStyle);
19092 }
19093
19094 TEST_F(FormatTest, StroustrupBraceBreaking) {
19095 FormatStyle StroustrupBraceStyle = getLLVMStyle();
19096 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
19097 verifyFormat("namespace a {\n"
19098 "class A {\n"
19099 " void f()\n"
19100 " {\n"
19101 " if (true) {\n"
19102 " a();\n"
19103 " b();\n"
19104 " }\n"
19105 " }\n"
19106 " void g() { return; }\n"
19107 "};\n"
19108 "struct B {\n"
19109 " int x;\n"
19110 "};\n"
19111 "} // namespace a\n",
19112 StroustrupBraceStyle);
19113
19114 verifyFormat("void foo()\n"
19115 "{\n"
19116 " if (a) {\n"
19117 " a();\n"
19118 " }\n"
19119 " else {\n"
19120 " b();\n"
19121 " }\n"
19122 "}\n",
19123 StroustrupBraceStyle);
19124
19125 verifyFormat("#ifdef _DEBUG\n"
19126 "int foo(int i = 0)\n"
19127 "#else\n"
19128 "int foo(int i = 5)\n"
19129 "#endif\n"
19130 "{\n"
19131 " return i;\n"
19132 "}",
19133 StroustrupBraceStyle);
19134
19135 verifyFormat("void foo() {}\n"
19136 "void bar()\n"
19137 "#ifdef _DEBUG\n"
19138 "{\n"
19139 " foo();\n"
19140 "}\n"
19141 "#else\n"
19142 "{\n"
19143 "}\n"
19144 "#endif",
19145 StroustrupBraceStyle);
19146
19147 verifyFormat("void foobar() { int i = 5; }\n"
19148 "#ifdef _DEBUG\n"
19149 "void bar() {}\n"
19150 "#else\n"
19151 "void bar() { foobar(); }\n"
19152 "#endif",
19153 StroustrupBraceStyle);
19154 }
19155
19156 TEST_F(FormatTest, AllmanBraceBreaking) {
19157 FormatStyle AllmanBraceStyle = getLLVMStyle();
19158 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
19159
19160 EXPECT_EQ("namespace a\n"
19161 "{\n"
19162 "void f();\n"
19163 "void g();\n"
19164 "} // namespace a\n",
19165 format("namespace a\n"
19166 "{\n"
19167 "void f();\n"
19168 "void g();\n"
19169 "}\n",
19170 AllmanBraceStyle));
19171
19172 verifyFormat("namespace a\n"
19173 "{\n"
19174 "class A\n"
19175 "{\n"
19176 " void f()\n"
19177 " {\n"
19178 " if (true)\n"
19179 " {\n"
19180 " a();\n"
19181 " b();\n"
19182 " }\n"
19183 " }\n"
19184 " void g() { return; }\n"
19185 "};\n"
19186 "struct B\n"
19187 "{\n"
19188 " int x;\n"
19189 "};\n"
19190 "union C\n"
19191 "{\n"
19192 "};\n"
19193 "} // namespace a",
19194 AllmanBraceStyle);
19195
19196 verifyFormat("void f()\n"
19197 "{\n"
19198 " if (true)\n"
19199 " {\n"
19200 " a();\n"
19201 " }\n"
19202 " else if (false)\n"
19203 " {\n"
19204 " b();\n"
19205 " }\n"
19206 " else\n"
19207 " {\n"
19208 " c();\n"
19209 " }\n"
19210 "}\n",
19211 AllmanBraceStyle);
19212
19213 verifyFormat("void f()\n"
19214 "{\n"
19215 " for (int i = 0; i < 10; ++i)\n"
19216 " {\n"
19217 " a();\n"
19218 " }\n"
19219 " while (false)\n"
19220 " {\n"
19221 " b();\n"
19222 " }\n"
19223 " do\n"
19224 " {\n"
19225 " c();\n"
19226 " } while (false)\n"
19227 "}\n",
19228 AllmanBraceStyle);
19229
19230 verifyFormat("void f(int a)\n"
19231 "{\n"
19232 " switch (a)\n"
19233 " {\n"
19234 " case 0:\n"
19235 " break;\n"
19236 " case 1:\n"
19237 " {\n"
19238 " break;\n"
19239 " }\n"
19240 " case 2:\n"
19241 " {\n"
19242 " }\n"
19243 " break;\n"
19244 " default:\n"
19245 " break;\n"
19246 " }\n"
19247 "}\n",
19248 AllmanBraceStyle);
19249
19250 verifyFormat("enum X\n"
19251 "{\n"
19252 " Y = 0,\n"
19253 "}\n",
19254 AllmanBraceStyle);
19255 verifyFormat("enum X\n"
19256 "{\n"
19257 " Y = 0\n"
19258 "}\n",
19259 AllmanBraceStyle);
19260
19261 verifyFormat("@interface BSApplicationController ()\n"
19262 "{\n"
19263 "@private\n"
19264 " id _extraIvar;\n"
19265 "}\n"
19266 "@end\n",
19267 AllmanBraceStyle);
19268
19269 verifyFormat("#ifdef _DEBUG\n"
19270 "int foo(int i = 0)\n"
19271 "#else\n"
19272 "int foo(int i = 5)\n"
19273 "#endif\n"
19274 "{\n"
19275 " return i;\n"
19276 "}",
19277 AllmanBraceStyle);
19278
19279 verifyFormat("void foo() {}\n"
19280 "void bar()\n"
19281 "#ifdef _DEBUG\n"
19282 "{\n"
19283 " foo();\n"
19284 "}\n"
19285 "#else\n"
19286 "{\n"
19287 "}\n"
19288 "#endif",
19289 AllmanBraceStyle);
19290
19291 verifyFormat("void foobar() { int i = 5; }\n"
19292 "#ifdef _DEBUG\n"
19293 "void bar() {}\n"
19294 "#else\n"
19295 "void bar() { foobar(); }\n"
19296 "#endif",
19297 AllmanBraceStyle);
19298
19299 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine,
19300 FormatStyle::SLS_All);
19301
19302 verifyFormat("[](int i) { return i + 2; };\n"
19303 "[](int i, int j)\n"
19304 "{\n"
19305 " auto x = i + j;\n"
19306 " auto y = i * j;\n"
19307 " return x ^ y;\n"
19308 "};\n"
19309 "void foo()\n"
19310 "{\n"
19311 " auto shortLambda = [](int i) { return i + 2; };\n"
19312 " auto longLambda = [](int i, int j)\n"
19313 " {\n"
19314 " auto x = i + j;\n"
19315 " auto y = i * j;\n"
19316 " return x ^ y;\n"
19317 " };\n"
19318 "}",
19319 AllmanBraceStyle);
19320
19321 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
19322
19323 verifyFormat("[](int i)\n"
19324 "{\n"
19325 " return i + 2;\n"
19326 "};\n"
19327 "[](int i, int j)\n"
19328 "{\n"
19329 " auto x = i + j;\n"
19330 " auto y = i * j;\n"
19331 " return x ^ y;\n"
19332 "};\n"
19333 "void foo()\n"
19334 "{\n"
19335 " auto shortLambda = [](int i)\n"
19336 " {\n"
19337 " return i + 2;\n"
19338 " };\n"
19339 " auto longLambda = [](int i, int j)\n"
19340 " {\n"
19341 " auto x = i + j;\n"
19342 " auto y = i * j;\n"
19343 " return x ^ y;\n"
19344 " };\n"
19345 "}",
19346 AllmanBraceStyle);
19347
19348 // Reset
19349 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
19350
19351 // This shouldn't affect ObjC blocks..
19352 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
19353 " // ...\n"
19354 " int i;\n"
19355 "}];",
19356 AllmanBraceStyle);
19357 verifyFormat("void (^block)(void) = ^{\n"
19358 " // ...\n"
19359 " int i;\n"
19360 "};",
19361 AllmanBraceStyle);
19362 // .. or dict literals.
19363 verifyFormat("void f()\n"
19364 "{\n"
19365 " // ...\n"
19366 " [object someMethod:@{@\"a\" : @\"b\"}];\n"
19367 "}",
19368 AllmanBraceStyle);
19369 verifyFormat("void f()\n"
19370 "{\n"
19371 " // ...\n"
19372 " [object someMethod:@{a : @\"b\"}];\n"
19373 "}",
19374 AllmanBraceStyle);
19375 verifyFormat("int f()\n"
19376 "{ // comment\n"
19377 " return 42;\n"
19378 "}",
19379 AllmanBraceStyle);
19380
19381 AllmanBraceStyle.ColumnLimit = 19;
19382 verifyFormat("void f() { int i; }", AllmanBraceStyle);
19383 AllmanBraceStyle.ColumnLimit = 18;
19384 verifyFormat("void f()\n"
19385 "{\n"
19386 " int i;\n"
19387 "}",
19388 AllmanBraceStyle);
19389 AllmanBraceStyle.ColumnLimit = 80;
19390
19391 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
19392 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
19393 FormatStyle::SIS_WithoutElse;
19394 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
19395 verifyFormat("void f(bool b)\n"
19396 "{\n"
19397 " if (b)\n"
19398 " {\n"
19399 " return;\n"
19400 " }\n"
19401 "}\n",
19402 BreakBeforeBraceShortIfs);
19403 verifyFormat("void f(bool b)\n"
19404 "{\n"
19405 " if constexpr (b)\n"
19406 " {\n"
19407 " return;\n"
19408 " }\n"
19409 "}\n",
19410 BreakBeforeBraceShortIfs);
19411 verifyFormat("void f(bool b)\n"
19412 "{\n"
19413 " if CONSTEXPR (b)\n"
19414 " {\n"
19415 " return;\n"
19416 " }\n"
19417 "}\n",
19418 BreakBeforeBraceShortIfs);
19419 verifyFormat("void f(bool b)\n"
19420 "{\n"
19421 " if (b) return;\n"
19422 "}\n",
19423 BreakBeforeBraceShortIfs);
19424 verifyFormat("void f(bool b)\n"
19425 "{\n"
19426 " if constexpr (b) return;\n"
19427 "}\n",
19428 BreakBeforeBraceShortIfs);
19429 verifyFormat("void f(bool b)\n"
19430 "{\n"
19431 " if CONSTEXPR (b) return;\n"
19432 "}\n",
19433 BreakBeforeBraceShortIfs);
19434 verifyFormat("void f(bool b)\n"
19435 "{\n"
19436 " while (b)\n"
19437 " {\n"
19438 " return;\n"
19439 " }\n"
19440 "}\n",
19441 BreakBeforeBraceShortIfs);
19442 }
19443
19444 TEST_F(FormatTest, WhitesmithsBraceBreaking) {
19445 FormatStyle WhitesmithsBraceStyle = getLLVMStyleWithColumns(0);
19446 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
19447
19448 // Make a few changes to the style for testing purposes
19449 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
19450 FormatStyle::SFS_Empty;
19451 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
19452
19453 // FIXME: this test case can't decide whether there should be a blank line
19454 // after the ~D() line or not. It adds one if one doesn't exist in the test
19455 // and it removes the line if one exists.
19456 /*
19457 verifyFormat("class A;\n"
19458 "namespace B\n"
19459 " {\n"
19460 "class C;\n"
19461 "// Comment\n"
19462 "class D\n"
19463 " {\n"
19464 "public:\n"
19465 " D();\n"
19466 " ~D() {}\n"
19467 "private:\n"
19468 " enum E\n"
19469 " {\n"
19470 " F\n"
19471 " }\n"
19472 " };\n"
19473 " } // namespace B\n",
19474 WhitesmithsBraceStyle);
19475 */
19476
19477 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None;
19478 verifyFormat("namespace a\n"
19479 " {\n"
19480 "class A\n"
19481 " {\n"
19482 " void f()\n"
19483 " {\n"
19484 " if (true)\n"
19485 " {\n"
19486 " a();\n"
19487 " b();\n"
19488 " }\n"
19489 " }\n"
19490 " void g()\n"
19491 " {\n"
19492 " return;\n"
19493 " }\n"
19494 " };\n"
19495 "struct B\n"
19496 " {\n"
19497 " int x;\n"
19498 " };\n"
19499 " } // namespace a",
19500 WhitesmithsBraceStyle);
19501
19502 verifyFormat("namespace a\n"
19503 " {\n"
19504 "namespace b\n"
19505 " {\n"
19506 "class A\n"
19507 " {\n"
19508 " void f()\n"
19509 " {\n"
19510 " if (true)\n"
19511 " {\n"
19512 " a();\n"
19513 " b();\n"
19514 " }\n"
19515 " }\n"
19516 " void g()\n"
19517 " {\n"
19518 " return;\n"
19519 " }\n"
19520 " };\n"
19521 "struct B\n"
19522 " {\n"
19523 " int x;\n"
19524 " };\n"
19525 " } // namespace b\n"
19526 " } // namespace a",
19527 WhitesmithsBraceStyle);
19528
19529 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner;
19530 verifyFormat("namespace a\n"
19531 " {\n"
19532 "namespace b\n"
19533 " {\n"
19534 " class A\n"
19535 " {\n"
19536 " void f()\n"
19537 " {\n"
19538 " if (true)\n"
19539 " {\n"
19540 " a();\n"
19541 " b();\n"
19542 " }\n"
19543 " }\n"
19544 " void g()\n"
19545 " {\n"
19546 " return;\n"
19547 " }\n"
19548 " };\n"
19549 " struct B\n"
19550 " {\n"
19551 " int x;\n"
19552 " };\n"
19553 " } // namespace b\n"
19554 " } // namespace a",
19555 WhitesmithsBraceStyle);
19556
19557 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All;
19558 verifyFormat("namespace a\n"
19559 " {\n"
19560 " namespace b\n"
19561 " {\n"
19562 " class A\n"
19563 " {\n"
19564 " void f()\n"
19565 " {\n"
19566 " if (true)\n"
19567 " {\n"
19568 " a();\n"
19569 " b();\n"
19570 " }\n"
19571 " }\n"
19572 " void g()\n"
19573 " {\n"
19574 " return;\n"
19575 " }\n"
19576 " };\n"
19577 " struct B\n"
19578 " {\n"
19579 " int x;\n"
19580 " };\n"
19581 " } // namespace b\n"
19582 " } // namespace a",
19583 WhitesmithsBraceStyle);
19584
19585 verifyFormat("void f()\n"
19586 " {\n"
19587 " if (true)\n"
19588 " {\n"
19589 " a();\n"
19590 " }\n"
19591 " else if (false)\n"
19592 " {\n"
19593 " b();\n"
19594 " }\n"
19595 " else\n"
19596 " {\n"
19597 " c();\n"
19598 " }\n"
19599 " }\n",
19600 WhitesmithsBraceStyle);
19601
19602 verifyFormat("void f()\n"
19603 " {\n"
19604 " for (int i = 0; i < 10; ++i)\n"
19605 " {\n"
19606 " a();\n"
19607 " }\n"
19608 " while (false)\n"
19609 " {\n"
19610 " b();\n"
19611 " }\n"
19612 " do\n"
19613 " {\n"
19614 " c();\n"
19615 " } while (false)\n"
19616 " }\n",
19617 WhitesmithsBraceStyle);
19618
19619 WhitesmithsBraceStyle.IndentCaseLabels = true;
19620 verifyFormat("void switchTest1(int a)\n"
19621 " {\n"
19622 " switch (a)\n"
19623 " {\n"
19624 " case 2:\n"
19625 " {\n"
19626 " }\n"
19627 " break;\n"
19628 " }\n"
19629 " }\n",
19630 WhitesmithsBraceStyle);
19631
19632 verifyFormat("void switchTest2(int a)\n"
19633 " {\n"
19634 " switch (a)\n"
19635 " {\n"
19636 " case 0:\n"
19637 " break;\n"
19638 " case 1:\n"
19639 " {\n"
19640 " break;\n"
19641 " }\n"
19642 " case 2:\n"
19643 " {\n"
19644 " }\n"
19645 " break;\n"
19646 " default:\n"
19647 " break;\n"
19648 " }\n"
19649 " }\n",
19650 WhitesmithsBraceStyle);
19651
19652 verifyFormat("void switchTest3(int a)\n"
19653 " {\n"
19654 " switch (a)\n"
19655 " {\n"
19656 " case 0:\n"
19657 " {\n"
19658 " foo(x);\n"
19659 " }\n"
19660 " break;\n"
19661 " default:\n"
19662 " {\n"
19663 " foo(1);\n"
19664 " }\n"
19665 " break;\n"
19666 " }\n"
19667 " }\n",
19668 WhitesmithsBraceStyle);
19669
19670 WhitesmithsBraceStyle.IndentCaseLabels = false;
19671
19672 verifyFormat("void switchTest4(int a)\n"
19673 " {\n"
19674 " switch (a)\n"
19675 " {\n"
19676 " case 2:\n"
19677 " {\n"
19678 " }\n"
19679 " break;\n"
19680 " }\n"
19681 " }\n",
19682 WhitesmithsBraceStyle);
19683
19684 verifyFormat("void switchTest5(int a)\n"
19685 " {\n"
19686 " switch (a)\n"
19687 " {\n"
19688 " case 0:\n"
19689 " break;\n"
19690 " case 1:\n"
19691 " {\n"
19692 " foo();\n"
19693 " break;\n"
19694 " }\n"
19695 " case 2:\n"
19696 " {\n"
19697 " }\n"
19698 " break;\n"
19699 " default:\n"
19700 " break;\n"
19701 " }\n"
19702 " }\n",
19703 WhitesmithsBraceStyle);
19704
19705 verifyFormat("void switchTest6(int a)\n"
19706 " {\n"
19707 " switch (a)\n"
19708 " {\n"
19709 " case 0:\n"
19710 " {\n"
19711 " foo(x);\n"
19712 " }\n"
19713 " break;\n"
19714 " default:\n"
19715 " {\n"
19716 " foo(1);\n"
19717 " }\n"
19718 " break;\n"
19719 " }\n"
19720 " }\n",
19721 WhitesmithsBraceStyle);
19722
19723 verifyFormat("enum X\n"
19724 " {\n"
19725 " Y = 0, // testing\n"
19726 " }\n",
19727 WhitesmithsBraceStyle);
19728
19729 verifyFormat("enum X\n"
19730 " {\n"
19731 " Y = 0\n"
19732 " }\n",
19733 WhitesmithsBraceStyle);
19734 verifyFormat("enum X\n"
19735 " {\n"
19736 " Y = 0,\n"
19737 " Z = 1\n"
19738 " };\n",
19739 WhitesmithsBraceStyle);
19740
19741 verifyFormat("@interface BSApplicationController ()\n"
19742 " {\n"
19743 "@private\n"
19744 " id _extraIvar;\n"
19745 " }\n"
19746 "@end\n",
19747 WhitesmithsBraceStyle);
19748
19749 verifyFormat("#ifdef _DEBUG\n"
19750 "int foo(int i = 0)\n"
19751 "#else\n"
19752 "int foo(int i = 5)\n"
19753 "#endif\n"
19754 " {\n"
19755 " return i;\n"
19756 " }",
19757 WhitesmithsBraceStyle);
19758
19759 verifyFormat("void foo() {}\n"
19760 "void bar()\n"
19761 "#ifdef _DEBUG\n"
19762 " {\n"
19763 " foo();\n"
19764 " }\n"
19765 "#else\n"
19766 " {\n"
19767 " }\n"
19768 "#endif",
19769 WhitesmithsBraceStyle);
19770
19771 verifyFormat("void foobar()\n"
19772 " {\n"
19773 " int i = 5;\n"
19774 " }\n"
19775 "#ifdef _DEBUG\n"
19776 "void bar()\n"
19777 " {\n"
19778 " }\n"
19779 "#else\n"
19780 "void bar()\n"
19781 " {\n"
19782 " foobar();\n"
19783 " }\n"
19784 "#endif",
19785 WhitesmithsBraceStyle);
19786
19787 // This shouldn't affect ObjC blocks..
19788 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
19789 " // ...\n"
19790 " int i;\n"
19791 "}];",
19792 WhitesmithsBraceStyle);
19793 verifyFormat("void (^block)(void) = ^{\n"
19794 " // ...\n"
19795 " int i;\n"
19796 "};",
19797 WhitesmithsBraceStyle);
19798 // .. or dict literals.
19799 verifyFormat("void f()\n"
19800 " {\n"
19801 " [object someMethod:@{@\"a\" : @\"b\"}];\n"
19802 " }",
19803 WhitesmithsBraceStyle);
19804
19805 verifyFormat("int f()\n"
19806 " { // comment\n"
19807 " return 42;\n"
19808 " }",
19809 WhitesmithsBraceStyle);
19810
19811 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
19812 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
19813 FormatStyle::SIS_OnlyFirstIf;
19814 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
19815 verifyFormat("void f(bool b)\n"
19816 " {\n"
19817 " if (b)\n"
19818 " {\n"
19819 " return;\n"
19820 " }\n"
19821 " }\n",
19822 BreakBeforeBraceShortIfs);
19823 verifyFormat("void f(bool b)\n"
19824 " {\n"
19825 " if (b) return;\n"
19826 " }\n",
19827 BreakBeforeBraceShortIfs);
19828 verifyFormat("void f(bool b)\n"
19829 " {\n"
19830 " while (b)\n"
19831 " {\n"
19832 " return;\n"
19833 " }\n"
19834 " }\n",
19835 BreakBeforeBraceShortIfs);
19836 }
19837
19838 TEST_F(FormatTest, GNUBraceBreaking) {
19839 FormatStyle GNUBraceStyle = getLLVMStyle();
19840 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
19841 verifyFormat("namespace a\n"
19842 "{\n"
19843 "class A\n"
19844 "{\n"
19845 " void f()\n"
19846 " {\n"
19847 " int a;\n"
19848 " {\n"
19849 " int b;\n"
19850 " }\n"
19851 " if (true)\n"
19852 " {\n"
19853 " a();\n"
19854 " b();\n"
19855 " }\n"
19856 " }\n"
19857 " void g() { return; }\n"
19858 "}\n"
19859 "} // namespace a",
19860 GNUBraceStyle);
19861
19862 verifyFormat("void f()\n"
19863 "{\n"
19864 " if (true)\n"
19865 " {\n"
19866 " a();\n"
19867 " }\n"
19868 " else if (false)\n"
19869 " {\n"
19870 " b();\n"
19871 " }\n"
19872 " else\n"
19873 " {\n"
19874 " c();\n"
19875 " }\n"
19876 "}\n",
19877 GNUBraceStyle);
19878
19879 verifyFormat("void f()\n"
19880 "{\n"
19881 " for (int i = 0; i < 10; ++i)\n"
19882 " {\n"
19883 " a();\n"
19884 " }\n"
19885 " while (false)\n"
19886 " {\n"
19887 " b();\n"
19888 " }\n"
19889 " do\n"
19890 " {\n"
19891 " c();\n"
19892 " }\n"
19893 " while (false);\n"
19894 "}\n",
19895 GNUBraceStyle);
19896
19897 verifyFormat("void f(int a)\n"
19898 "{\n"
19899 " switch (a)\n"
19900 " {\n"
19901 " case 0:\n"
19902 " break;\n"
19903 " case 1:\n"
19904 " {\n"
19905 " break;\n"
19906 " }\n"
19907 " case 2:\n"
19908 " {\n"
19909 " }\n"
19910 " break;\n"
19911 " default:\n"
19912 " break;\n"
19913 " }\n"
19914 "}\n",
19915 GNUBraceStyle);
19916
19917 verifyFormat("enum X\n"
19918 "{\n"
19919 " Y = 0,\n"
19920 "}\n",
19921 GNUBraceStyle);
19922
19923 verifyFormat("@interface BSApplicationController ()\n"
19924 "{\n"
19925 "@private\n"
19926 " id _extraIvar;\n"
19927 "}\n"
19928 "@end\n",
19929 GNUBraceStyle);
19930
19931 verifyFormat("#ifdef _DEBUG\n"
19932 "int foo(int i = 0)\n"
19933 "#else\n"
19934 "int foo(int i = 5)\n"
19935 "#endif\n"
19936 "{\n"
19937 " return i;\n"
19938 "}",
19939 GNUBraceStyle);
19940
19941 verifyFormat("void foo() {}\n"
19942 "void bar()\n"
19943 "#ifdef _DEBUG\n"
19944 "{\n"
19945 " foo();\n"
19946 "}\n"
19947 "#else\n"
19948 "{\n"
19949 "}\n"
19950 "#endif",
19951 GNUBraceStyle);
19952
19953 verifyFormat("void foobar() { int i = 5; }\n"
19954 "#ifdef _DEBUG\n"
19955 "void bar() {}\n"
19956 "#else\n"
19957 "void bar() { foobar(); }\n"
19958 "#endif",
19959 GNUBraceStyle);
19960 }
19961
19962 TEST_F(FormatTest, WebKitBraceBreaking) {
19963 FormatStyle WebKitBraceStyle = getLLVMStyle();
19964 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
19965 WebKitBraceStyle.FixNamespaceComments = false;
19966 verifyFormat("namespace a {\n"
19967 "class A {\n"
19968 " void f()\n"
19969 " {\n"
19970 " if (true) {\n"
19971 " a();\n"
19972 " b();\n"
19973 " }\n"
19974 " }\n"
19975 " void g() { return; }\n"
19976 "};\n"
19977 "enum E {\n"
19978 " A,\n"
19979 " // foo\n"
19980 " B,\n"
19981 " C\n"
19982 "};\n"
19983 "struct B {\n"
19984 " int x;\n"
19985 "};\n"
19986 "}\n",
19987 WebKitBraceStyle);
19988 verifyFormat("struct S {\n"
19989 " int Type;\n"
19990 " union {\n"
19991 " int x;\n"
19992 " double y;\n"
19993 " } Value;\n"
19994 " class C {\n"
19995 " MyFavoriteType Value;\n"
19996 " } Class;\n"
19997 "};\n",
19998 WebKitBraceStyle);
19999 }
20000
20001 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
20002 verifyFormat("void f() {\n"
20003 " try {\n"
20004 " } catch (const Exception &e) {\n"
20005 " }\n"
20006 "}\n",
20007 getLLVMStyle());
20008 }
20009
20010 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
20011 auto Style = getLLVMStyle();
20012 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
20013 Style.AlignConsecutiveAssignments.Enabled = true;
20014 Style.AlignConsecutiveDeclarations.Enabled = true;
20015 verifyFormat("struct test demo[] = {\n"
20016 " {56, 23, \"hello\"},\n"
20017 " {-1, 93463, \"world\"},\n"
20018 " { 7, 5, \"!!\"}\n"
20019 "};\n",
20020 Style);
20021
20022 verifyFormat("struct test demo[] = {\n"
20023 " {56, 23, \"hello\"}, // first line\n"
20024 " {-1, 93463, \"world\"}, // second line\n"
20025 " { 7, 5, \"!!\"} // third line\n"
20026 "};\n",
20027 Style);
20028
20029 verifyFormat("struct test demo[4] = {\n"
20030 " { 56, 23, 21, \"oh\"}, // first line\n"
20031 " { -1, 93463, 22, \"my\"}, // second line\n"
20032 " { 7, 5, 1, \"goodness\"} // third line\n"
20033 " {234, 5, 1, \"gracious\"} // fourth line\n"
20034 "};\n",
20035 Style);
20036
20037 verifyFormat("struct test demo[3] = {\n"
20038 " {56, 23, \"hello\"},\n"
20039 " {-1, 93463, \"world\"},\n"
20040 " { 7, 5, \"!!\"}\n"
20041 "};\n",
20042 Style);
20043
20044 verifyFormat("struct test demo[3] = {\n"
20045 " {int{56}, 23, \"hello\"},\n"
20046 " {int{-1}, 93463, \"world\"},\n"
20047 " { int{7}, 5, \"!!\"}\n"
20048 "};\n",
20049 Style);
20050
20051 verifyFormat("struct test demo[] = {\n"
20052 " {56, 23, \"hello\"},\n"
20053 " {-1, 93463, \"world\"},\n"
20054 " { 7, 5, \"!!\"},\n"
20055 "};\n",
20056 Style);
20057
20058 verifyFormat("test demo[] = {\n"
20059 " {56, 23, \"hello\"},\n"
20060 " {-1, 93463, \"world\"},\n"
20061 " { 7, 5, \"!!\"},\n"
20062 "};\n",
20063 Style);
20064
20065 verifyFormat("demo = std::array<struct test, 3>{\n"
20066 " test{56, 23, \"hello\"},\n"
20067 " test{-1, 93463, \"world\"},\n"
20068 " test{ 7, 5, \"!!\"},\n"
20069 "};\n",
20070 Style);
20071
20072 verifyFormat("test demo[] = {\n"
20073 " {56, 23, \"hello\"},\n"
20074 "#if X\n"
20075 " {-1, 93463, \"world\"},\n"
20076 "#endif\n"
20077 " { 7, 5, \"!!\"}\n"
20078 "};\n",
20079 Style);
20080
20081 verifyFormat(
20082 "test demo[] = {\n"
20083 " { 7, 23,\n"
20084 " \"hello world i am a very long line that really, in any\"\n"
20085 " \"just world, ought to be split over multiple lines\"},\n"
20086 " {-1, 93463, \"world\"},\n"
20087 " {56, 5, \"!!\"}\n"
20088 "};\n",
20089 Style);
20090
20091 verifyFormat("return GradForUnaryCwise(g, {\n"
20092 " {{\"sign\"}, \"Sign\", "
20093 " {\"x\", \"dy\"}},\n"
20094 " { {\"dx\"}, \"Mul\", {\"dy\""
20095 ", \"sign\"}},\n"
20096 "});\n",
20097 Style);
20098
20099 Style.ColumnLimit = 0;
20100 EXPECT_EQ(
20101 "test demo[] = {\n"
20102 " {56, 23, \"hello world i am a very long line that really, "
20103 "in any just world, ought to be split over multiple lines\"},\n"
20104 " {-1, 93463, "
20105 " \"world\"},\n"
20106 " { 7, 5, "
20107 " \"!!\"},\n"
20108 "};",
20109 format("test demo[] = {{56, 23, \"hello world i am a very long line "
20110 "that really, in any just world, ought to be split over multiple "
20111 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
20112 Style));
20113
20114 Style.ColumnLimit = 80;
20115 verifyFormat("test demo[] = {\n"
20116 " {56, 23, /* a comment */ \"hello\"},\n"
20117 " {-1, 93463, \"world\"},\n"
20118 " { 7, 5, \"!!\"}\n"
20119 "};\n",
20120 Style);
20121
20122 verifyFormat("test demo[] = {\n"
20123 " {56, 23, \"hello\"},\n"
20124 " {-1, 93463, \"world\" /* comment here */},\n"
20125 " { 7, 5, \"!!\"}\n"
20126 "};\n",
20127 Style);
20128
20129 verifyFormat("test demo[] = {\n"
20130 " {56, /* a comment */ 23, \"hello\"},\n"
20131 " {-1, 93463, \"world\"},\n"
20132 " { 7, 5, \"!!\"}\n"
20133 "};\n",
20134 Style);
20135
20136 Style.ColumnLimit = 20;
20137 EXPECT_EQ(
20138 "demo = std::array<\n"
20139 " struct test, 3>{\n"
20140 " test{\n"
20141 " 56, 23,\n"
20142 " \"hello \"\n"
20143 " \"world i \"\n"
20144 " \"am a very \"\n"
20145 " \"long line \"\n"
20146 " \"that \"\n"
20147 " \"really, \"\n"
20148 " \"in any \"\n"
20149 " \"just \"\n"
20150 " \"world, \"\n"
20151 " \"ought to \"\n"
20152 " \"be split \"\n"
20153 " \"over \"\n"
20154 " \"multiple \"\n"
20155 " \"lines\"},\n"
20156 " test{-1, 93463,\n"
20157 " \"world\"},\n"
20158 " test{ 7, 5,\n"
20159 " \"!!\" },\n"
20160 "};",
20161 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
20162 "i am a very long line that really, in any just world, ought "
20163 "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
20164 "test{7, 5, \"!!\"},};",
20165 Style));
20166 // This caused a core dump by enabling Alignment in the LLVMStyle globally
20167 Style = getLLVMStyleWithColumns(50);
20168 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
20169 verifyFormat("static A x = {\n"
20170 " {{init1, init2, init3, init4},\n"
20171 " {init1, init2, init3, init4}}\n"
20172 "};",
20173 Style);
20174 // TODO: Fix the indentations below when this option is fully functional.
20175 verifyFormat("int a[][] = {\n"
20176 " {\n"
20177 " {0, 2}, //\n"
20178 " {1, 2} //\n"
20179 " }\n"
20180 "};",
20181 Style);
20182 Style.ColumnLimit = 100;
20183 EXPECT_EQ(
20184 "test demo[] = {\n"
20185 " {56, 23,\n"
20186 " \"hello world i am a very long line that really, in any just world"
20187 ", ought to be split over \"\n"
20188 " \"multiple lines\" },\n"
20189 " {-1, 93463, \"world\"},\n"
20190 " { 7, 5, \"!!\"},\n"
20191 "};",
20192 format("test demo[] = {{56, 23, \"hello world i am a very long line "
20193 "that really, in any just world, ought to be split over multiple "
20194 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
20195 Style));
20196
20197 Style = getLLVMStyleWithColumns(50);
20198 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
20199 verifyFormat("struct test demo[] = {\n"
20200 " {56, 23, \"hello\"},\n"
20201 " {-1, 93463, \"world\"},\n"
20202 " { 7, 5, \"!!\"}\n"
20203 "};\n"
20204 "static A x = {\n"
20205 " {{init1, init2, init3, init4},\n"
20206 " {init1, init2, init3, init4}}\n"
20207 "};",
20208 Style);
20209 Style.ColumnLimit = 100;
20210 Style.AlignConsecutiveAssignments.AcrossComments = true;
20211 Style.AlignConsecutiveDeclarations.AcrossComments = true;
20212 verifyFormat("struct test demo[] = {\n"
20213 " {56, 23, \"hello\"},\n"
20214 " {-1, 93463, \"world\"},\n"
20215 " { 7, 5, \"!!\"}\n"
20216 "};\n"
20217 "struct test demo[4] = {\n"
20218 " { 56, 23, 21, \"oh\"}, // first line\n"
20219 " { -1, 93463, 22, \"my\"}, // second line\n"
20220 " { 7, 5, 1, \"goodness\"} // third line\n"
20221 " {234, 5, 1, \"gracious\"} // fourth line\n"
20222 "};\n",
20223 Style);
20224 EXPECT_EQ(
20225 "test demo[] = {\n"
20226 " {56,\n"
20227 " \"hello world i am a very long line that really, in any just world"
20228 ", ought to be split over \"\n"
20229 " \"multiple lines\", 23},\n"
20230 " {-1, \"world\", 93463},\n"
20231 " { 7, \"!!\", 5},\n"
20232 "};",
20233 format("test demo[] = {{56, \"hello world i am a very long line "
20234 "that really, in any just world, ought to be split over multiple "
20235 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
20236 Style));
20237 }
20238
20239 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
20240 auto Style = getLLVMStyle();
20241 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
20242 /* FIXME: This case gets misformatted.
20243 verifyFormat("auto foo = Items{\n"
20244 " Section{0, bar(), },\n"
20245 " Section{1, boo() }\n"
20246 "};\n",
20247 Style);
20248 */
20249 verifyFormat("auto foo = Items{\n"
20250 " Section{\n"
20251 " 0, bar(),\n"
20252 " }\n"
20253 "};\n",
20254 Style);
20255 verifyFormat("struct test demo[] = {\n"
20256 " {56, 23, \"hello\"},\n"
20257 " {-1, 93463, \"world\"},\n"
20258 " {7, 5, \"!!\" }\n"
20259 "};\n",
20260 Style);
20261 verifyFormat("struct test demo[] = {\n"
20262 " {56, 23, \"hello\"}, // first line\n"
20263 " {-1, 93463, \"world\"}, // second line\n"
20264 " {7, 5, \"!!\" } // third line\n"
20265 "};\n",
20266 Style);
20267 verifyFormat("struct test demo[4] = {\n"
20268 " {56, 23, 21, \"oh\" }, // first line\n"
20269 " {-1, 93463, 22, \"my\" }, // second line\n"
20270 " {7, 5, 1, \"goodness\"} // third line\n"
20271 " {234, 5, 1, \"gracious\"} // fourth line\n"
20272 "};\n",
20273 Style);
20274 verifyFormat("struct test demo[3] = {\n"
20275 " {56, 23, \"hello\"},\n"
20276 " {-1, 93463, \"world\"},\n"
20277 " {7, 5, \"!!\" }\n"
20278 "};\n",
20279 Style);
20280
20281 verifyFormat("struct test demo[3] = {\n"
20282 " {int{56}, 23, \"hello\"},\n"
20283 " {int{-1}, 93463, \"world\"},\n"
20284 " {int{7}, 5, \"!!\" }\n"
20285 "};\n",
20286 Style);
20287 verifyFormat("struct test demo[] = {\n"
20288 " {56, 23, \"hello\"},\n"
20289 " {-1, 93463, \"world\"},\n"
20290 " {7, 5, \"!!\" },\n"
20291 "};\n",
20292 Style);
20293 verifyFormat("test demo[] = {\n"
20294 " {56, 23, \"hello\"},\n"
20295 " {-1, 93463, \"world\"},\n"
20296 " {7, 5, \"!!\" },\n"
20297 "};\n",
20298 Style);
20299 verifyFormat("demo = std::array<struct test, 3>{\n"
20300 " test{56, 23, \"hello\"},\n"
20301 " test{-1, 93463, \"world\"},\n"
20302 " test{7, 5, \"!!\" },\n"
20303 "};\n",
20304 Style);
20305 verifyFormat("test demo[] = {\n"
20306 " {56, 23, \"hello\"},\n"
20307 "#if X\n"
20308 " {-1, 93463, \"world\"},\n"
20309 "#endif\n"
20310 " {7, 5, \"!!\" }\n"
20311 "};\n",
20312 Style);
20313 verifyFormat(
20314 "test demo[] = {\n"
20315 " {7, 23,\n"
20316 " \"hello world i am a very long line that really, in any\"\n"
20317 " \"just world, ought to be split over multiple lines\"},\n"
20318 " {-1, 93463, \"world\" },\n"
20319 " {56, 5, \"!!\" }\n"
20320 "};\n",
20321 Style);
20322
20323 verifyFormat("return GradForUnaryCwise(g, {\n"
20324 " {{\"sign\"}, \"Sign\", {\"x\", "
20325 "\"dy\"} },\n"
20326 " {{\"dx\"}, \"Mul\", "
20327 "{\"dy\", \"sign\"}},\n"
20328 "});\n",
20329 Style);
20330
20331 Style.ColumnLimit = 0;
20332 EXPECT_EQ(
20333 "test demo[] = {\n"
20334 " {56, 23, \"hello world i am a very long line that really, in any "
20335 "just world, ought to be split over multiple lines\"},\n"
20336 " {-1, 93463, \"world\" "
20337 " },\n"
20338 " {7, 5, \"!!\" "
20339 " },\n"
20340 "};",
20341 format("test demo[] = {{56, 23, \"hello world i am a very long line "
20342 "that really, in any just world, ought to be split over multiple "
20343 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
20344 Style));
20345
20346 Style.ColumnLimit = 80;
20347 verifyFormat("test demo[] = {\n"
20348 " {56, 23, /* a comment */ \"hello\"},\n"
20349 " {-1, 93463, \"world\" },\n"
20350 " {7, 5, \"!!\" }\n"
20351 "};\n",
20352 Style);
20353
20354 verifyFormat("test demo[] = {\n"
20355 " {56, 23, \"hello\" },\n"
20356 " {-1, 93463, \"world\" /* comment here */},\n"
20357 " {7, 5, \"!!\" }\n"
20358 "};\n",
20359 Style);
20360
20361 verifyFormat("test demo[] = {\n"
20362 " {56, /* a comment */ 23, \"hello\"},\n"
20363 " {-1, 93463, \"world\"},\n"
20364 " {7, 5, \"!!\" }\n"
20365 "};\n",
20366 Style);
20367
20368 Style.ColumnLimit = 20;
20369 EXPECT_EQ(
20370 "demo = std::array<\n"
20371 " struct test, 3>{\n"
20372 " test{\n"
20373 " 56, 23,\n"
20374 " \"hello \"\n"
20375 " \"world i \"\n"
20376 " \"am a very \"\n"
20377 " \"long line \"\n"
20378 " \"that \"\n"
20379 " \"really, \"\n"
20380 " \"in any \"\n"
20381 " \"just \"\n"
20382 " \"world, \"\n"
20383 " \"ought to \"\n"
20384 " \"be split \"\n"
20385 " \"over \"\n"
20386 " \"multiple \"\n"
20387 " \"lines\"},\n"
20388 " test{-1, 93463,\n"
20389 " \"world\"},\n"
20390 " test{7, 5,\n"
20391 " \"!!\" },\n"
20392 "};",
20393 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
20394 "i am a very long line that really, in any just world, ought "
20395 "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
20396 "test{7, 5, \"!!\"},};",
20397 Style));
20398
20399 // This caused a core dump by enabling Alignment in the LLVMStyle globally
20400 Style = getLLVMStyleWithColumns(50);
20401 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
20402 verifyFormat("static A x = {\n"
20403 " {{init1, init2, init3, init4},\n"
20404 " {init1, init2, init3, init4}}\n"
20405 "};",
20406 Style);
20407 Style.ColumnLimit = 100;
20408 EXPECT_EQ(
20409 "test demo[] = {\n"
20410 " {56, 23,\n"
20411 " \"hello world i am a very long line that really, in any just world"
20412 ", ought to be split over \"\n"
20413 " \"multiple lines\" },\n"
20414 " {-1, 93463, \"world\"},\n"
20415 " {7, 5, \"!!\" },\n"
20416 "};",
20417 format("test demo[] = {{56, 23, \"hello world i am a very long line "
20418 "that really, in any just world, ought to be split over multiple "
20419 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
20420 Style));
20421 }
20422
20423 TEST_F(FormatTest, UnderstandsPragmas) {
20424 verifyFormat("#pragma omp reduction(| : var)");
20425 verifyFormat("#pragma omp reduction(+ : var)");
20426
20427 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
20428 "(including parentheses).",
20429 format("#pragma mark Any non-hyphenated or hyphenated string "
20430 "(including parentheses)."));
20431 }
20432
20433 TEST_F(FormatTest, UnderstandsPragmaOmpTarget) {
20434 verifyFormat("#pragma omp target map(to : var)");
20435 verifyFormat("#pragma omp target map(to : var[ : N])");
20436 verifyFormat("#pragma omp target map(to : var[0 : N])");
20437 verifyFormat("#pragma omp target map(always, to : var[0 : N])");
20438
20439 EXPECT_EQ(
20440 "#pragma omp target \\\n"
20441 " reduction(+ : var) \\\n"
20442 " map(to : A[0 : N]) \\\n"
20443 " map(to : B[0 : N]) \\\n"
20444 " map(from : C[0 : N]) \\\n"
20445 " firstprivate(i) \\\n"
20446 " firstprivate(j) \\\n"
20447 " firstprivate(k)",
20448 format(
20449 "#pragma omp target reduction(+:var) map(to:A[0:N]) map(to:B[0:N]) "
20450 "map(from:C[0:N]) firstprivate(i) firstprivate(j) firstprivate(k)",
20451 getLLVMStyleWithColumns(26)));
20452 }
20453
20454 TEST_F(FormatTest, UnderstandPragmaOption) {
20455 verifyFormat("#pragma option -C -A");
20456
20457 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A"));
20458 }
20459
20460 TEST_F(FormatTest, UnderstandPragmaRegion) {
20461 auto Style = getLLVMStyleWithColumns(0);
20462 verifyFormat("#pragma region TEST(FOO : BAR)", Style);
20463 verifyFormat("#pragma region TEST(FOO: NOSPACE)", Style);
20464 }
20465
20466 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
20467 FormatStyle Style = getLLVMStyleWithColumns(20);
20468
20469 // See PR41213
20470 EXPECT_EQ("/*\n"
20471 " *\t9012345\n"
20472 " * /8901\n"
20473 " */",
20474 format("/*\n"
20475 " *\t9012345 /8901\n"
20476 " */",
20477 Style));
20478 EXPECT_EQ("/*\n"
20479 " *345678\n"
20480 " *\t/8901\n"
20481 " */",
20482 format("/*\n"
20483 " *345678\t/8901\n"
20484 " */",
20485 Style));
20486
20487 verifyFormat("int a; // the\n"
20488 " // comment",
20489 Style);
20490 EXPECT_EQ("int a; /* first line\n"
20491 " * second\n"
20492 " * line third\n"
20493 " * line\n"
20494 " */",
20495 format("int a; /* first line\n"
20496 " * second\n"
20497 " * line third\n"
20498 " * line\n"
20499 " */",
20500 Style));
20501 EXPECT_EQ("int a; // first line\n"
20502 " // second\n"
20503 " // line third\n"
20504 " // line",
20505 format("int a; // first line\n"
20506 " // second line\n"
20507 " // third line",
20508 Style));
20509
20510 Style.PenaltyExcessCharacter = 90;
20511 verifyFormat("int a; // the comment", Style);
20512 EXPECT_EQ("int a; // the comment\n"
20513 " // aaa",
20514 format("int a; // the comment aaa", Style));
20515 EXPECT_EQ("int a; /* first line\n"
20516 " * second line\n"
20517 " * third line\n"
20518 " */",
20519 format("int a; /* first line\n"
20520 " * second line\n"
20521 " * third line\n"
20522 " */",
20523 Style));
20524 EXPECT_EQ("int a; // first line\n"
20525 " // second line\n"
20526 " // third line",
20527 format("int a; // first line\n"
20528 " // second line\n"
20529 " // third line",
20530 Style));
20531 // FIXME: Investigate why this is not getting the same layout as the test
20532 // above.
20533 EXPECT_EQ("int a; /* first line\n"
20534 " * second line\n"
20535 " * third line\n"
20536 " */",
20537 format("int a; /* first line second line third line"
20538 "\n*/",
20539 Style));
20540
20541 EXPECT_EQ("// foo bar baz bazfoo\n"
20542 "// foo bar foo bar\n",
20543 format("// foo bar baz bazfoo\n"
20544 "// foo bar foo bar\n",
20545 Style));
20546 EXPECT_EQ("// foo bar baz bazfoo\n"
20547 "// foo bar foo bar\n",
20548 format("// foo bar baz bazfoo\n"
20549 "// foo bar foo bar\n",
20550 Style));
20551
20552 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
20553 // next one.
20554 EXPECT_EQ("// foo bar baz bazfoo\n"
20555 "// bar foo bar\n",
20556 format("// foo bar baz bazfoo bar\n"
20557 "// foo bar\n",
20558 Style));
20559
20560 EXPECT_EQ("// foo bar baz bazfoo\n"
20561 "// foo bar baz bazfoo\n"
20562 "// bar foo bar\n",
20563 format("// foo bar baz bazfoo\n"
20564 "// foo bar baz bazfoo bar\n"
20565 "// foo bar\n",
20566 Style));
20567
20568 EXPECT_EQ("// foo bar baz bazfoo\n"
20569 "// foo bar baz bazfoo\n"
20570 "// bar foo bar\n",
20571 format("// foo bar baz bazfoo\n"
20572 "// foo bar baz bazfoo bar\n"
20573 "// foo bar\n",
20574 Style));
20575
20576 // Make sure we do not keep protruding characters if strict mode reflow is
20577 // cheaper than keeping protruding characters.
20578 Style.ColumnLimit = 21;
20579 EXPECT_EQ(
20580 "// foo foo foo foo\n"
20581 "// foo foo foo foo\n"
20582 "// foo foo foo foo\n",
20583 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style));
20584
20585 EXPECT_EQ("int a = /* long block\n"
20586 " comment */\n"
20587 " 42;",
20588 format("int a = /* long block comment */ 42;", Style));
20589 }
20590
20591 TEST_F(FormatTest, BreakPenaltyAfterLParen) {
20592 FormatStyle Style = getLLVMStyle();
20593 Style.ColumnLimit = 8;
20594 Style.PenaltyExcessCharacter = 15;
20595 verifyFormat("int foo(\n"
20596 " int aaaaaaaaaaaaaaaaaaaaaaaa);",
20597 Style);
20598 Style.PenaltyBreakOpenParenthesis = 200;
20599 EXPECT_EQ("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);",
20600 format("int foo(\n"
20601 " int aaaaaaaaaaaaaaaaaaaaaaaa);",
20602 Style));
20603 }
20604
20605 TEST_F(FormatTest, BreakPenaltyAfterCastLParen) {
20606 FormatStyle Style = getLLVMStyle();
20607 Style.ColumnLimit = 5;
20608 Style.PenaltyExcessCharacter = 150;
20609 verifyFormat("foo((\n"
20610 " int)aaaaaaaaaaaaaaaaaaaaaaaa);",
20611
20612 Style);
20613 Style.PenaltyBreakOpenParenthesis = 100000;
20614 EXPECT_EQ("foo((int)\n"
20615 " aaaaaaaaaaaaaaaaaaaaaaaa);",
20616 format("foo((\n"
20617 "int)aaaaaaaaaaaaaaaaaaaaaaaa);",
20618 Style));
20619 }
20620
20621 TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) {
20622 FormatStyle Style = getLLVMStyle();
20623 Style.ColumnLimit = 4;
20624 Style.PenaltyExcessCharacter = 100;
20625 verifyFormat("for (\n"
20626 " int iiiiiiiiiiiiiiiii =\n"
20627 " 0;\n"
20628 " iiiiiiiiiiiiiiiii <\n"
20629 " 2;\n"
20630 " iiiiiiiiiiiiiiiii++) {\n"
20631 "}",
20632
20633 Style);
20634 Style.PenaltyBreakOpenParenthesis = 1250;
20635 EXPECT_EQ("for (int iiiiiiiiiiiiiiiii =\n"
20636 " 0;\n"
20637 " iiiiiiiiiiiiiiiii <\n"
20638 " 2;\n"
20639 " iiiiiiiiiiiiiiiii++) {\n"
20640 "}",
20641 format("for (\n"
20642 " int iiiiiiiiiiiiiiiii =\n"
20643 " 0;\n"
20644 " iiiiiiiiiiiiiiiii <\n"
20645 " 2;\n"
20646 " iiiiiiiiiiiiiiiii++) {\n"
20647 "}",
20648 Style));
20649 }
20650
20651 TEST_F(FormatTest, WorksFor8bitEncodings) {
20652 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
20653 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
20654 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
20655 "\"\xef\xee\xf0\xf3...\"",
20656 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
20657 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
20658 "\xef\xee\xf0\xf3...\"",
20659 getLLVMStyleWithColumns(12)));
20660 }
20661
20662 TEST_F(FormatTest, HandlesUTF8BOM) {
20663 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
20664 EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
20665 format("\xef\xbb\xbf#include <iostream>"));
20666 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
20667 format("\xef\xbb\xbf\n#include <iostream>"));
20668 }
20669
20670 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
20671 #if !defined(_MSC_VER)
20672
20673 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
20674 verifyFormat("\"Однажды в студёную зимнюю пору...\"",
20675 getLLVMStyleWithColumns(35));
20676 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
20677 getLLVMStyleWithColumns(31));
20678 verifyFormat("// Однажды в студёную зимнюю пору...",
20679 getLLVMStyleWithColumns(36));
20680 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
20681 verifyFormat("/* Однажды в студёную зимнюю пору... */",
20682 getLLVMStyleWithColumns(39));
20683 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
20684 getLLVMStyleWithColumns(35));
20685 }
20686
20687 TEST_F(FormatTest, SplitsUTF8Strings) {
20688 // Non-printable characters' width is currently considered to be the length in
20689 // bytes in UTF8. The characters can be displayed in very different manner
20690 // (zero-width, single width with a substitution glyph, expanded to their code
20691 // (e.g. "<8d>"), so there's no single correct way to handle them.
20692 EXPECT_EQ("\"aaaaÄ\"\n"
20693 "\"\xc2\x8d\";",
20694 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
20695 EXPECT_EQ("\"aaaaaaaÄ\"\n"
20696 "\"\xc2\x8d\";",
20697 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
20698 EXPECT_EQ("\"Однажды, в \"\n"
20699 "\"студёную \"\n"
20700 "\"зимнюю \"\n"
20701 "\"пору,\"",
20702 format("\"Однажды, в студёную зимнюю пору,\"",
20703 getLLVMStyleWithColumns(13)));
20704 EXPECT_EQ(
20705 "\"一 二 三 \"\n"
20706 "\"四 五六 \"\n"
20707 "\"七 八 九 \"\n"
20708 "\"十\"",
20709 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
20710 EXPECT_EQ("\"一\t\"\n"
20711 "\"二 \t\"\n"
20712 "\"三 四 \"\n"
20713 "\"五\t\"\n"
20714 "\"六 \t\"\n"
20715 "\"七 \"\n"
20716 "\"八九十\tqq\"",
20717 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
20718 getLLVMStyleWithColumns(11)));
20719
20720 // UTF8 character in an escape sequence.
20721 EXPECT_EQ("\"aaaaaa\"\n"
20722 "\"\\\xC2\x8D\"",
20723 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
20724 }
20725
20726 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
20727 EXPECT_EQ("const char *sssss =\n"
20728 " \"一二三四五六七八\\\n"
20729 " 九 十\";",
20730 format("const char *sssss = \"一二三四五六七八\\\n"
20731 " 九 十\";",
20732 getLLVMStyleWithColumns(30)));
20733 }
20734
20735 TEST_F(FormatTest, SplitsUTF8LineComments) {
20736 EXPECT_EQ("// aaaaÄ\xc2\x8d",
20737 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
20738 EXPECT_EQ("// Я из лесу\n"
20739 "// вышел; был\n"
20740 "// сильный\n"
20741 "// мороз.",
20742 format("// Я из лесу вышел; был сильный мороз.",
20743 getLLVMStyleWithColumns(13)));
20744 EXPECT_EQ("// 一二三\n"
20745 "// 四五六七\n"
20746 "// 八 九\n"
20747 "// 十",
20748 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)));
20749 }
20750
20751 TEST_F(FormatTest, SplitsUTF8BlockComments) {
20752 EXPECT_EQ("/* Гляжу,\n"
20753 " * поднимается\n"
20754 " * медленно в\n"
20755 " * гору\n"
20756 " * Лошадка,\n"
20757 " * везущая\n"
20758 " * хворосту\n"
20759 " * воз. */",
20760 format("/* Гляжу, поднимается медленно в гору\n"
20761 " * Лошадка, везущая хворосту воз. */",
20762 getLLVMStyleWithColumns(13)));
20763 EXPECT_EQ(
20764 "/* 一二三\n"
20765 " * 四五六七\n"
20766 " * 八 九\n"
20767 " * 十 */",
20768 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)));
20769 EXPECT_EQ("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
20770 " * 𝕓𝕪𝕥𝕖\n"
20771 " * 𝖀𝕿𝕱-𝟠 */",
20772 format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12)));
20773 }
20774
20775 #endif // _MSC_VER
20776
20777 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
20778 FormatStyle Style = getLLVMStyle();
20779
20780 Style.ConstructorInitializerIndentWidth = 4;
20781 verifyFormat(
20782 "SomeClass::Constructor()\n"
20783 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
20784 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
20785 Style);
20786
20787 Style.ConstructorInitializerIndentWidth = 2;
20788 verifyFormat(
20789 "SomeClass::Constructor()\n"
20790 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
20791 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
20792 Style);
20793
20794 Style.ConstructorInitializerIndentWidth = 0;
20795 verifyFormat(
20796 "SomeClass::Constructor()\n"
20797 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
20798 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
20799 Style);
20800 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
20801 verifyFormat(
20802 "SomeLongTemplateVariableName<\n"
20803 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
20804 Style);
20805 verifyFormat("bool smaller = 1 < "
20806 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
20807 " "
20808 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
20809 Style);
20810
20811 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
20812 verifyFormat("SomeClass::Constructor() :\n"
20813 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
20814 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
20815 Style);
20816 }
20817
20818 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
20819 FormatStyle Style = getLLVMStyle();
20820 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
20821 Style.ConstructorInitializerIndentWidth = 4;
20822 verifyFormat("SomeClass::Constructor()\n"
20823 " : a(a)\n"
20824 " , b(b)\n"
20825 " , c(c) {}",
20826 Style);
20827 verifyFormat("SomeClass::Constructor()\n"
20828 " : a(a) {}",
20829 Style);
20830
20831 Style.ColumnLimit = 0;
20832 verifyFormat("SomeClass::Constructor()\n"
20833 " : a(a) {}",
20834 Style);
20835 verifyFormat("SomeClass::Constructor() noexcept\n"
20836 " : a(a) {}",
20837 Style);
20838 verifyFormat("SomeClass::Constructor()\n"
20839 " : a(a)\n"
20840 " , b(b)\n"
20841 " , c(c) {}",
20842 Style);
20843 verifyFormat("SomeClass::Constructor()\n"
20844 " : a(a) {\n"
20845 " foo();\n"
20846 " bar();\n"
20847 "}",
20848 Style);
20849
20850 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
20851 verifyFormat("SomeClass::Constructor()\n"
20852 " : a(a)\n"
20853 " , b(b)\n"
20854 " , c(c) {\n}",
20855 Style);
20856 verifyFormat("SomeClass::Constructor()\n"
20857 " : a(a) {\n}",
20858 Style);
20859
20860 Style.ColumnLimit = 80;
20861 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
20862 Style.ConstructorInitializerIndentWidth = 2;
20863 verifyFormat("SomeClass::Constructor()\n"
20864 " : a(a)\n"
20865 " , b(b)\n"
20866 " , c(c) {}",
20867 Style);
20868
20869 Style.ConstructorInitializerIndentWidth = 0;
20870 verifyFormat("SomeClass::Constructor()\n"
20871 ": a(a)\n"
20872 ", b(b)\n"
20873 ", c(c) {}",
20874 Style);
20875
20876 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
20877 Style.ConstructorInitializerIndentWidth = 4;
20878 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
20879 verifyFormat(
20880 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
20881 Style);
20882 verifyFormat(
20883 "SomeClass::Constructor()\n"
20884 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
20885 Style);
20886 Style.ConstructorInitializerIndentWidth = 4;
20887 Style.ColumnLimit = 60;
20888 verifyFormat("SomeClass::Constructor()\n"
20889 " : aaaaaaaa(aaaaaaaa)\n"
20890 " , aaaaaaaa(aaaaaaaa)\n"
20891 " , aaaaaaaa(aaaaaaaa) {}",
20892 Style);
20893 }
20894
20895 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) {
20896 FormatStyle Style = getLLVMStyle();
20897 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
20898 Style.ConstructorInitializerIndentWidth = 4;
20899 verifyFormat("SomeClass::Constructor()\n"
20900 " : a{a}\n"
20901 " , b{b} {}",
20902 Style);
20903 verifyFormat("SomeClass::Constructor()\n"
20904 " : a{a}\n"
20905 "#if CONDITION\n"
20906 " , b{b}\n"
20907 "#endif\n"
20908 "{\n}",
20909 Style);
20910 Style.ConstructorInitializerIndentWidth = 2;
20911 verifyFormat("SomeClass::Constructor()\n"
20912 "#if CONDITION\n"
20913 " : a{a}\n"
20914 "#endif\n"
20915 " , b{b}\n"
20916 " , c{c} {\n}",
20917 Style);
20918 Style.ConstructorInitializerIndentWidth = 0;
20919 verifyFormat("SomeClass::Constructor()\n"
20920 ": a{a}\n"
20921 "#ifdef CONDITION\n"
20922 ", b{b}\n"
20923 "#else\n"
20924 ", c{c}\n"
20925 "#endif\n"
20926 ", d{d} {\n}",
20927 Style);
20928 Style.ConstructorInitializerIndentWidth = 4;
20929 verifyFormat("SomeClass::Constructor()\n"
20930 " : a{a}\n"
20931 "#if WINDOWS\n"
20932 "#if DEBUG\n"
20933 " , b{0}\n"
20934 "#else\n"
20935 " , b{1}\n"
20936 "#endif\n"
20937 "#else\n"
20938 "#if DEBUG\n"
20939 " , b{2}\n"
20940 "#else\n"
20941 " , b{3}\n"
20942 "#endif\n"
20943 "#endif\n"
20944 "{\n}",
20945 Style);
20946 verifyFormat("SomeClass::Constructor()\n"
20947 " : a{a}\n"
20948 "#if WINDOWS\n"
20949 " , b{0}\n"
20950 "#if DEBUG\n"
20951 " , c{0}\n"
20952 "#else\n"
20953 " , c{1}\n"
20954 "#endif\n"
20955 "#else\n"
20956 "#if DEBUG\n"
20957 " , c{2}\n"
20958 "#else\n"
20959 " , c{3}\n"
20960 "#endif\n"
20961 " , b{1}\n"
20962 "#endif\n"
20963 "{\n}",
20964 Style);
20965 }
20966
20967 TEST_F(FormatTest, Destructors) {
20968 verifyFormat("void F(int &i) { i.~int(); }");
20969 verifyFormat("void F(int &i) { i->~int(); }");
20970 }
20971
20972 TEST_F(FormatTest, FormatsWithWebKitStyle) {
20973 FormatStyle Style = getWebKitStyle();
20974
20975 // Don't indent in outer namespaces.
20976 verifyFormat("namespace outer {\n"
20977 "int i;\n"
20978 "namespace inner {\n"
20979 " int i;\n"
20980 "} // namespace inner\n"
20981 "} // namespace outer\n"
20982 "namespace other_outer {\n"
20983 "int i;\n"
20984 "}",
20985 Style);
20986
20987 // Don't indent case labels.
20988 verifyFormat("switch (variable) {\n"
20989 "case 1:\n"
20990 "case 2:\n"
20991 " doSomething();\n"
20992 " break;\n"
20993 "default:\n"
20994 " ++variable;\n"
20995 "}",
20996 Style);
20997
20998 // Wrap before binary operators.
20999 EXPECT_EQ("void f()\n"
21000 "{\n"
21001 " if (aaaaaaaaaaaaaaaa\n"
21002 " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
21003 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
21004 " return;\n"
21005 "}",
21006 format("void f() {\n"
21007 "if (aaaaaaaaaaaaaaaa\n"
21008 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
21009 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
21010 "return;\n"
21011 "}",
21012 Style));
21013
21014 // Allow functions on a single line.
21015 verifyFormat("void f() { return; }", Style);
21016
21017 // Allow empty blocks on a single line and insert a space in empty blocks.
21018 EXPECT_EQ("void f() { }", format("void f() {}", Style));
21019 EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
21020 // However, don't merge non-empty short loops.
21021 EXPECT_EQ("while (true) {\n"
21022 " continue;\n"
21023 "}",
21024 format("while (true) { continue; }", Style));
21025
21026 // Constructor initializers are formatted one per line with the "," on the
21027 // new line.
21028 verifyFormat("Constructor()\n"
21029 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
21030 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
21031 " aaaaaaaaaaaaaa)\n"
21032 " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
21033 "{\n"
21034 "}",
21035 Style);
21036 verifyFormat("SomeClass::Constructor()\n"
21037 " : a(a)\n"
21038 "{\n"
21039 "}",
21040 Style);
21041 EXPECT_EQ("SomeClass::Constructor()\n"
21042 " : a(a)\n"
21043 "{\n"
21044 "}",
21045 format("SomeClass::Constructor():a(a){}", Style));
21046 verifyFormat("SomeClass::Constructor()\n"
21047 " : a(a)\n"
21048 " , b(b)\n"
21049 " , c(c)\n"
21050 "{\n"
21051 "}",
21052 Style);
21053 verifyFormat("SomeClass::Constructor()\n"
21054 " : a(a)\n"
21055 "{\n"
21056 " foo();\n"
21057 " bar();\n"
21058 "}",
21059 Style);
21060
21061 // Access specifiers should be aligned left.
21062 verifyFormat("class C {\n"
21063 "public:\n"
21064 " int i;\n"
21065 "};",
21066 Style);
21067
21068 // Do not align comments.
21069 verifyFormat("int a; // Do not\n"
21070 "double b; // align comments.",
21071 Style);
21072
21073 // Do not align operands.
21074 EXPECT_EQ("ASSERT(aaaa\n"
21075 " || bbbb);",
21076 format("ASSERT ( aaaa\n||bbbb);", Style));
21077
21078 // Accept input's line breaks.
21079 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
21080 " || bbbbbbbbbbbbbbb) {\n"
21081 " i++;\n"
21082 "}",
21083 format("if (aaaaaaaaaaaaaaa\n"
21084 "|| bbbbbbbbbbbbbbb) { i++; }",
21085 Style));
21086 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
21087 " i++;\n"
21088 "}",
21089 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
21090
21091 // Don't automatically break all macro definitions (llvm.org/PR17842).
21092 verifyFormat("#define aNumber 10", Style);
21093 // However, generally keep the line breaks that the user authored.
21094 EXPECT_EQ("#define aNumber \\\n"
21095 " 10",
21096 format("#define aNumber \\\n"
21097 " 10",
21098 Style));
21099
21100 // Keep empty and one-element array literals on a single line.
21101 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
21102 " copyItems:YES];",
21103 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
21104 "copyItems:YES];",
21105 Style));
21106 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
21107 " copyItems:YES];",
21108 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
21109 " copyItems:YES];",
21110 Style));
21111 // FIXME: This does not seem right, there should be more indentation before
21112 // the array literal's entries. Nested blocks have the same problem.
21113 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
21114 " @\"a\",\n"
21115 " @\"a\"\n"
21116 "]\n"
21117 " copyItems:YES];",
21118 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
21119 " @\"a\",\n"
21120 " @\"a\"\n"
21121 " ]\n"
21122 " copyItems:YES];",
21123 Style));
21124 EXPECT_EQ(
21125 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
21126 " copyItems:YES];",
21127 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
21128 " copyItems:YES];",
21129 Style));
21130
21131 verifyFormat("[self.a b:c c:d];", Style);
21132 EXPECT_EQ("[self.a b:c\n"
21133 " c:d];",
21134 format("[self.a b:c\n"
21135 "c:d];",
21136 Style));
21137 }
21138
21139 TEST_F(FormatTest, FormatsLambdas) {
21140 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
21141 verifyFormat(
21142 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n");
21143 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
21144 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
21145 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
21146 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
21147 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
21148 verifyFormat("auto c = [a = [b = 42] {}] {};\n");
21149 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
21150 verifyFormat("int x = f(*+[] {});");
21151 verifyFormat("void f() {\n"
21152 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
21153 "}\n");
21154 verifyFormat("void f() {\n"
21155 " other(x.begin(), //\n"
21156 " x.end(), //\n"
21157 " [&](int, int) { return 1; });\n"
21158 "}\n");
21159 verifyFormat("void f() {\n"
21160 " other.other.other.other.other(\n"
21161 " x.begin(), x.end(),\n"
21162 " [something, rather](int, int, int, int, int, int, int) { "
21163 "return 1; });\n"
21164 "}\n");
21165 verifyFormat(
21166 "void f() {\n"
21167 " other.other.other.other.other(\n"
21168 " x.begin(), x.end(),\n"
21169 " [something, rather](int, int, int, int, int, int, int) {\n"
21170 " //\n"
21171 " });\n"
21172 "}\n");
21173 verifyFormat("SomeFunction([]() { // A cool function...\n"
21174 " return 43;\n"
21175 "});");
21176 EXPECT_EQ("SomeFunction([]() {\n"
21177 "#define A a\n"
21178 " return 43;\n"
21179 "});",
21180 format("SomeFunction([](){\n"
21181 "#define A a\n"
21182 "return 43;\n"
21183 "});"));
21184 verifyFormat("void f() {\n"
21185 " SomeFunction([](decltype(x), A *a) {});\n"
21186 " SomeFunction([](typeof(x), A *a) {});\n"
21187 " SomeFunction([](_Atomic(x), A *a) {});\n"
21188 " SomeFunction([](__underlying_type(x), A *a) {});\n"
21189 "}");
21190 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
21191 " [](const aaaaaaaaaa &a) { return a; });");
21192 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
21193 " SomeOtherFunctioooooooooooooooooooooooooon();\n"
21194 "});");
21195 verifyFormat("Constructor()\n"
21196 " : Field([] { // comment\n"
21197 " int i;\n"
21198 " }) {}");
21199 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
21200 " return some_parameter.size();\n"
21201 "};");
21202 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
21203 " [](const string &s) { return s; };");
21204 verifyFormat("int i = aaaaaa ? 1 //\n"
21205 " : [] {\n"
21206 " return 2; //\n"
21207 " }();");
21208 verifyFormat("llvm::errs() << \"number of twos is \"\n"
21209 " << std::count_if(v.begin(), v.end(), [](int x) {\n"
21210 " return x == 2; // force break\n"
21211 " });");
21212 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
21213 " [=](int iiiiiiiiiiii) {\n"
21214 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
21215 " aaaaaaaaaaaaaaaaaaaaaaa;\n"
21216 " });",
21217 getLLVMStyleWithColumns(60));
21218
21219 verifyFormat("SomeFunction({[&] {\n"
21220 " // comment\n"
21221 " },\n"
21222 " [&] {\n"
21223 " // comment\n"
21224 " }});");
21225 verifyFormat("SomeFunction({[&] {\n"
21226 " // comment\n"
21227 "}});");
21228 verifyFormat(
21229 "virtual aaaaaaaaaaaaaaaa(\n"
21230 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
21231 " aaaaa aaaaaaaaa);");
21232
21233 // Lambdas with return types.
21234 verifyFormat("int c = []() -> int { return 2; }();\n");
21235 verifyFormat("int c = []() -> int * { return 2; }();\n");
21236 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
21237 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
21238 verifyFormat("foo([]() noexcept -> int {});");
21239 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
21240 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
21241 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
21242 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
21243 verifyFormat("[a, a]() -> a<1> {};");
21244 verifyFormat("[]() -> foo<5 + 2> { return {}; };");
21245 verifyFormat("[]() -> foo<5 - 2> { return {}; };");
21246 verifyFormat("[]() -> foo<5 / 2> { return {}; };");
21247 verifyFormat("[]() -> foo<5 * 2> { return {}; };");
21248 verifyFormat("[]() -> foo<5 % 2> { return {}; };");
21249 verifyFormat("[]() -> foo<5 << 2> { return {}; };");
21250 verifyFormat("[]() -> foo<!5> { return {}; };");
21251 verifyFormat("[]() -> foo<~5> { return {}; };");
21252 verifyFormat("[]() -> foo<5 | 2> { return {}; };");
21253 verifyFormat("[]() -> foo<5 || 2> { return {}; };");
21254 verifyFormat("[]() -> foo<5 & 2> { return {}; };");
21255 verifyFormat("[]() -> foo<5 && 2> { return {}; };");
21256 verifyFormat("[]() -> foo<5 == 2> { return {}; };");
21257 verifyFormat("[]() -> foo<5 != 2> { return {}; };");
21258 verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
21259 verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
21260 verifyFormat("[]() -> foo<5 < 2> { return {}; };");
21261 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
21262 verifyFormat("namespace bar {\n"
21263 "// broken:\n"
21264 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
21265 "} // namespace bar");
21266 verifyFormat("namespace bar {\n"
21267 "// broken:\n"
21268 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
21269 "} // namespace bar");
21270 verifyFormat("namespace bar {\n"
21271 "// broken:\n"
21272 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
21273 "} // namespace bar");
21274 verifyFormat("namespace bar {\n"
21275 "// broken:\n"
21276 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
21277 "} // namespace bar");
21278 verifyFormat("namespace bar {\n"
21279 "// broken:\n"
21280 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
21281 "} // namespace bar");
21282 verifyFormat("namespace bar {\n"
21283 "// broken:\n"
21284 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
21285 "} // namespace bar");
21286 verifyFormat("namespace bar {\n"
21287 "// broken:\n"
21288 "auto foo{[]() -> foo<!5> { return {}; }};\n"
21289 "} // namespace bar");
21290 verifyFormat("namespace bar {\n"
21291 "// broken:\n"
21292 "auto foo{[]() -> foo<~5> { return {}; }};\n"
21293 "} // namespace bar");
21294 verifyFormat("namespace bar {\n"
21295 "// broken:\n"
21296 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
21297 "} // namespace bar");
21298 verifyFormat("namespace bar {\n"
21299 "// broken:\n"
21300 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
21301 "} // namespace bar");
21302 verifyFormat("namespace bar {\n"
21303 "// broken:\n"
21304 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
21305 "} // namespace bar");
21306 verifyFormat("namespace bar {\n"
21307 "// broken:\n"
21308 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
21309 "} // namespace bar");
21310 verifyFormat("namespace bar {\n"
21311 "// broken:\n"
21312 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
21313 "} // namespace bar");
21314 verifyFormat("namespace bar {\n"
21315 "// broken:\n"
21316 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
21317 "} // namespace bar");
21318 verifyFormat("namespace bar {\n"
21319 "// broken:\n"
21320 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
21321 "} // namespace bar");
21322 verifyFormat("namespace bar {\n"
21323 "// broken:\n"
21324 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
21325 "} // namespace bar");
21326 verifyFormat("namespace bar {\n"
21327 "// broken:\n"
21328 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
21329 "} // namespace bar");
21330 verifyFormat("namespace bar {\n"
21331 "// broken:\n"
21332 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
21333 "} // namespace bar");
21334 verifyFormat("[]() -> a<1> {};");
21335 verifyFormat("[]() -> a<1> { ; };");
21336 verifyFormat("[]() -> a<1> { ; }();");
21337 verifyFormat("[a, a]() -> a<true> {};");
21338 verifyFormat("[]() -> a<true> {};");
21339 verifyFormat("[]() -> a<true> { ; };");
21340 verifyFormat("[]() -> a<true> { ; }();");
21341 verifyFormat("[a, a]() -> a<false> {};");
21342 verifyFormat("[]() -> a<false> {};");
21343 verifyFormat("[]() -> a<false> { ; };");
21344 verifyFormat("[]() -> a<false> { ; }();");
21345 verifyFormat("auto foo{[]() -> foo<false> { ; }};");
21346 verifyFormat("namespace bar {\n"
21347 "auto foo{[]() -> foo<false> { ; }};\n"
21348 "} // namespace bar");
21349 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
21350 " int j) -> int {\n"
21351 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
21352 "};");
21353 verifyFormat(
21354 "aaaaaaaaaaaaaaaaaaaaaa(\n"
21355 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
21356 " return aaaaaaaaaaaaaaaaa;\n"
21357 " });",
21358 getLLVMStyleWithColumns(70));
21359 verifyFormat("[]() //\n"
21360 " -> int {\n"
21361 " return 1; //\n"
21362 "};");
21363 verifyFormat("[]() -> Void<T...> {};");
21364 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
21365 verifyFormat("SomeFunction({[]() -> int[] { return {}; }});");
21366 verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});");
21367 verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});");
21368 verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});");
21369 verifyFormat("return int{[x = x]() { return x; }()};");
21370
21371 // Lambdas with explicit template argument lists.
21372 verifyFormat(
21373 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n");
21374 verifyFormat("auto L = []<class T>(T) {\n"
21375 " {\n"
21376 " f();\n"
21377 " g();\n"
21378 " }\n"
21379 "};\n");
21380 verifyFormat("auto L = []<class... T>(T...) {\n"
21381 " {\n"
21382 " f();\n"
21383 " g();\n"
21384 " }\n"
21385 "};\n");
21386 verifyFormat("auto L = []<typename... T>(T...) {\n"
21387 " {\n"
21388 " f();\n"
21389 " g();\n"
21390 " }\n"
21391 "};\n");
21392 verifyFormat("auto L = []<template <typename...> class T>(T...) {\n"
21393 " {\n"
21394 " f();\n"
21395 " g();\n"
21396 " }\n"
21397 "};\n");
21398 verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n"
21399 " {\n"
21400 " f();\n"
21401 " g();\n"
21402 " }\n"
21403 "};\n");
21404 verifyFormat("auto L = []<int... T>(T...) {\n"
21405 " {\n"
21406 " f();\n"
21407 " g();\n"
21408 " }\n"
21409 "};");
21410 verifyFormat("auto L = []<Foo... T>(T...) {\n"
21411 " {\n"
21412 " f();\n"
21413 " g();\n"
21414 " }\n"
21415 "};");
21416
21417 // Multiple lambdas in the same parentheses change indentation rules. These
21418 // lambdas are forced to start on new lines.
21419 verifyFormat("SomeFunction(\n"
21420 " []() {\n"
21421 " //\n"
21422 " },\n"
21423 " []() {\n"
21424 " //\n"
21425 " });");
21426
21427 // A lambda passed as arg0 is always pushed to the next line.
21428 verifyFormat("SomeFunction(\n"
21429 " [this] {\n"
21430 " //\n"
21431 " },\n"
21432 " 1);\n");
21433
21434 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
21435 // the arg0 case above.
21436 auto Style = getGoogleStyle();
21437 Style.BinPackArguments = false;
21438 verifyFormat("SomeFunction(\n"
21439 " a,\n"
21440 " [this] {\n"
21441 " //\n"
21442 " },\n"
21443 " b);\n",
21444 Style);
21445 verifyFormat("SomeFunction(\n"
21446 " a,\n"
21447 " [this] {\n"
21448 " //\n"
21449 " },\n"
21450 " b);\n");
21451
21452 // A lambda with a very long line forces arg0 to be pushed out irrespective of
21453 // the BinPackArguments value (as long as the code is wide enough).
21454 verifyFormat(
21455 "something->SomeFunction(\n"
21456 " a,\n"
21457 " [this] {\n"
21458 " "
21459 "D0000000000000000000000000000000000000000000000000000000000001();\n"
21460 " },\n"
21461 " b);\n");
21462
21463 // A multi-line lambda is pulled up as long as the introducer fits on the
21464 // previous line and there are no further args.
21465 verifyFormat("function(1, [this, that] {\n"
21466 " //\n"
21467 "});\n");
21468 verifyFormat("function([this, that] {\n"
21469 " //\n"
21470 "});\n");
21471 // FIXME: this format is not ideal and we should consider forcing the first
21472 // arg onto its own line.
21473 verifyFormat("function(a, b, c, //\n"
21474 " d, [this, that] {\n"
21475 " //\n"
21476 " });\n");
21477
21478 // Multiple lambdas are treated correctly even when there is a short arg0.
21479 verifyFormat("SomeFunction(\n"
21480 " 1,\n"
21481 " [this] {\n"
21482 " //\n"
21483 " },\n"
21484 " [this] {\n"
21485 " //\n"
21486 " },\n"
21487 " 1);\n");
21488
21489 // More complex introducers.
21490 verifyFormat("return [i, args...] {};");
21491
21492 // Not lambdas.
21493 verifyFormat("constexpr char hello[]{\"hello\"};");
21494 verifyFormat("double &operator[](int i) { return 0; }\n"
21495 "int i;");
21496 verifyFormat("std::unique_ptr<int[]> foo() {}");
21497 verifyFormat("int i = a[a][a]->f();");
21498 verifyFormat("int i = (*b)[a]->f();");
21499
21500 // Other corner cases.
21501 verifyFormat("void f() {\n"
21502 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
21503 " );\n"
21504 "}");
21505 verifyFormat("auto k = *[](int *j) { return j; }(&i);");
21506
21507 // Lambdas created through weird macros.
21508 verifyFormat("void f() {\n"
21509 " MACRO((const AA &a) { return 1; });\n"
21510 " MACRO((AA &a) { return 1; });\n"
21511 "}");
21512
21513 verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
21514 " doo_dah();\n"
21515 " doo_dah();\n"
21516 " })) {\n"
21517 "}");
21518 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
21519 " doo_dah();\n"
21520 " doo_dah();\n"
21521 " })) {\n"
21522 "}");
21523 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
21524 " doo_dah();\n"
21525 " doo_dah();\n"
21526 " })) {\n"
21527 "}");
21528 verifyFormat("auto lambda = []() {\n"
21529 " int a = 2\n"
21530 "#if A\n"
21531 " + 2\n"
21532 "#endif\n"
21533 " ;\n"
21534 "};");
21535
21536 // Lambdas with complex multiline introducers.
21537 verifyFormat(
21538 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
21539 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
21540 " -> ::std::unordered_set<\n"
21541 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
21542 " //\n"
21543 " });");
21544
21545 FormatStyle DoNotMerge = getLLVMStyle();
21546 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
21547 verifyFormat("auto c = []() {\n"
21548 " return b;\n"
21549 "};",
21550 "auto c = []() { return b; };", DoNotMerge);
21551 verifyFormat("auto c = []() {\n"
21552 "};",
21553 " auto c = []() {};", DoNotMerge);
21554
21555 FormatStyle MergeEmptyOnly = getLLVMStyle();
21556 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
21557 verifyFormat("auto c = []() {\n"
21558 " return b;\n"
21559 "};",
21560 "auto c = []() {\n"
21561 " return b;\n"
21562 " };",
21563 MergeEmptyOnly);
21564 verifyFormat("auto c = []() {};",
21565 "auto c = []() {\n"
21566 "};",
21567 MergeEmptyOnly);
21568
21569 FormatStyle MergeInline = getLLVMStyle();
21570 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
21571 verifyFormat("auto c = []() {\n"
21572 " return b;\n"
21573 "};",
21574 "auto c = []() { return b; };", MergeInline);
21575 verifyFormat("function([]() { return b; })", "function([]() { return b; })",
21576 MergeInline);
21577 verifyFormat("function([]() { return b; }, a)",
21578 "function([]() { return b; }, a)", MergeInline);
21579 verifyFormat("function(a, []() { return b; })",
21580 "function(a, []() { return b; })", MergeInline);
21581
21582 // Check option "BraceWrapping.BeforeLambdaBody" and different state of
21583 // AllowShortLambdasOnASingleLine
21584 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
21585 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
21586 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
21587 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21588 FormatStyle::ShortLambdaStyle::SLS_None;
21589 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
21590 " []()\n"
21591 " {\n"
21592 " return 17;\n"
21593 " });",
21594 LLVMWithBeforeLambdaBody);
21595 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
21596 " []()\n"
21597 " {\n"
21598 " });",
21599 LLVMWithBeforeLambdaBody);
21600 verifyFormat("auto fct_SLS_None = []()\n"
21601 "{\n"
21602 " return 17;\n"
21603 "};",
21604 LLVMWithBeforeLambdaBody);
21605 verifyFormat("TwoNestedLambdas_SLS_None(\n"
21606 " []()\n"
21607 " {\n"
21608 " return Call(\n"
21609 " []()\n"
21610 " {\n"
21611 " return 17;\n"
21612 " });\n"
21613 " });",
21614 LLVMWithBeforeLambdaBody);
21615 verifyFormat("void Fct() {\n"
21616 " return {[]()\n"
21617 " {\n"
21618 " return 17;\n"
21619 " }};\n"
21620 "}",
21621 LLVMWithBeforeLambdaBody);
21622
21623 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21624 FormatStyle::ShortLambdaStyle::SLS_Empty;
21625 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
21626 " []()\n"
21627 " {\n"
21628 " return 17;\n"
21629 " });",
21630 LLVMWithBeforeLambdaBody);
21631 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
21632 LLVMWithBeforeLambdaBody);
21633 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
21634 "ongFunctionName_SLS_Empty(\n"
21635 " []() {});",
21636 LLVMWithBeforeLambdaBody);
21637 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
21638 " []()\n"
21639 " {\n"
21640 " return 17;\n"
21641 " });",
21642 LLVMWithBeforeLambdaBody);
21643 verifyFormat("auto fct_SLS_Empty = []()\n"
21644 "{\n"
21645 " return 17;\n"
21646 "};",
21647 LLVMWithBeforeLambdaBody);
21648 verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
21649 " []()\n"
21650 " {\n"
21651 " return Call([]() {});\n"
21652 " });",
21653 LLVMWithBeforeLambdaBody);
21654 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
21655 " []()\n"
21656 " {\n"
21657 " return Call([]() {});\n"
21658 " });",
21659 LLVMWithBeforeLambdaBody);
21660 verifyFormat(
21661 "FctWithLongLineInLambda_SLS_Empty(\n"
21662 " []()\n"
21663 " {\n"
21664 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21665 " AndShouldNotBeConsiderAsInline,\n"
21666 " LambdaBodyMustBeBreak);\n"
21667 " });",
21668 LLVMWithBeforeLambdaBody);
21669
21670 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21671 FormatStyle::ShortLambdaStyle::SLS_Inline;
21672 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
21673 LLVMWithBeforeLambdaBody);
21674 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
21675 LLVMWithBeforeLambdaBody);
21676 verifyFormat("auto fct_SLS_Inline = []()\n"
21677 "{\n"
21678 " return 17;\n"
21679 "};",
21680 LLVMWithBeforeLambdaBody);
21681 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
21682 "17; }); });",
21683 LLVMWithBeforeLambdaBody);
21684 verifyFormat(
21685 "FctWithLongLineInLambda_SLS_Inline(\n"
21686 " []()\n"
21687 " {\n"
21688 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21689 " AndShouldNotBeConsiderAsInline,\n"
21690 " LambdaBodyMustBeBreak);\n"
21691 " });",
21692 LLVMWithBeforeLambdaBody);
21693 verifyFormat("FctWithMultipleParams_SLS_Inline("
21694 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
21695 " []() { return 17; });",
21696 LLVMWithBeforeLambdaBody);
21697 verifyFormat(
21698 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
21699 LLVMWithBeforeLambdaBody);
21700
21701 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21702 FormatStyle::ShortLambdaStyle::SLS_All;
21703 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
21704 LLVMWithBeforeLambdaBody);
21705 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
21706 LLVMWithBeforeLambdaBody);
21707 verifyFormat("auto fct_SLS_All = []() { return 17; };",
21708 LLVMWithBeforeLambdaBody);
21709 verifyFormat("FctWithOneParam_SLS_All(\n"
21710 " []()\n"
21711 " {\n"
21712 " // A cool function...\n"
21713 " return 43;\n"
21714 " });",
21715 LLVMWithBeforeLambdaBody);
21716 verifyFormat("FctWithMultipleParams_SLS_All("
21717 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
21718 " []() { return 17; });",
21719 LLVMWithBeforeLambdaBody);
21720 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
21721 LLVMWithBeforeLambdaBody);
21722 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
21723 LLVMWithBeforeLambdaBody);
21724 verifyFormat(
21725 "FctWithLongLineInLambda_SLS_All(\n"
21726 " []()\n"
21727 " {\n"
21728 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21729 " AndShouldNotBeConsiderAsInline,\n"
21730 " LambdaBodyMustBeBreak);\n"
21731 " });",
21732 LLVMWithBeforeLambdaBody);
21733 verifyFormat(
21734 "auto fct_SLS_All = []()\n"
21735 "{\n"
21736 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21737 " AndShouldNotBeConsiderAsInline,\n"
21738 " LambdaBodyMustBeBreak);\n"
21739 "};",
21740 LLVMWithBeforeLambdaBody);
21741 LLVMWithBeforeLambdaBody.BinPackParameters = false;
21742 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
21743 LLVMWithBeforeLambdaBody);
21744 verifyFormat(
21745 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
21746 " FirstParam,\n"
21747 " SecondParam,\n"
21748 " ThirdParam,\n"
21749 " FourthParam);",
21750 LLVMWithBeforeLambdaBody);
21751 verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
21752 " []() { return "
21753 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
21754 " FirstParam,\n"
21755 " SecondParam,\n"
21756 " ThirdParam,\n"
21757 " FourthParam);",
21758 LLVMWithBeforeLambdaBody);
21759 verifyFormat(
21760 "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
21761 " SecondParam,\n"
21762 " ThirdParam,\n"
21763 " FourthParam,\n"
21764 " []() { return SomeValueNotSoLong; });",
21765 LLVMWithBeforeLambdaBody);
21766 verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
21767 " []()\n"
21768 " {\n"
21769 " return "
21770 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
21771 "eConsiderAsInline;\n"
21772 " });",
21773 LLVMWithBeforeLambdaBody);
21774 verifyFormat(
21775 "FctWithLongLineInLambda_SLS_All(\n"
21776 " []()\n"
21777 " {\n"
21778 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21779 " AndShouldNotBeConsiderAsInline,\n"
21780 " LambdaBodyMustBeBreak);\n"
21781 " });",
21782 LLVMWithBeforeLambdaBody);
21783 verifyFormat("FctWithTwoParams_SLS_All(\n"
21784 " []()\n"
21785 " {\n"
21786 " // A cool function...\n"
21787 " return 43;\n"
21788 " },\n"
21789 " 87);",
21790 LLVMWithBeforeLambdaBody);
21791 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
21792 LLVMWithBeforeLambdaBody);
21793 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
21794 LLVMWithBeforeLambdaBody);
21795 verifyFormat(
21796 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
21797 LLVMWithBeforeLambdaBody);
21798 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
21799 "}); }, x);",
21800 LLVMWithBeforeLambdaBody);
21801 verifyFormat("TwoNestedLambdas_SLS_All(\n"
21802 " []()\n"
21803 " {\n"
21804 " // A cool function...\n"
21805 " return Call([]() { return 17; });\n"
21806 " });",
21807 LLVMWithBeforeLambdaBody);
21808 verifyFormat("TwoNestedLambdas_SLS_All(\n"
21809 " []()\n"
21810 " {\n"
21811 " return Call(\n"
21812 " []()\n"
21813 " {\n"
21814 " // A cool function...\n"
21815 " return 17;\n"
21816 " });\n"
21817 " });",
21818 LLVMWithBeforeLambdaBody);
21819
21820 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21821 FormatStyle::ShortLambdaStyle::SLS_None;
21822
21823 verifyFormat("auto select = [this]() -> const Library::Object *\n"
21824 "{\n"
21825 " return MyAssignment::SelectFromList(this);\n"
21826 "};\n",
21827 LLVMWithBeforeLambdaBody);
21828
21829 verifyFormat("auto select = [this]() -> const Library::Object &\n"
21830 "{\n"
21831 " return MyAssignment::SelectFromList(this);\n"
21832 "};\n",
21833 LLVMWithBeforeLambdaBody);
21834
21835 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
21836 "{\n"
21837 " return MyAssignment::SelectFromList(this);\n"
21838 "};\n",
21839 LLVMWithBeforeLambdaBody);
21840
21841 verifyFormat("namespace test {\n"
21842 "class Test {\n"
21843 "public:\n"
21844 " Test() = default;\n"
21845 "};\n"
21846 "} // namespace test",
21847 LLVMWithBeforeLambdaBody);
21848
21849 // Lambdas with different indentation styles.
21850 Style = getLLVMStyleWithColumns(100);
21851 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
21852 " return promise.then(\n"
21853 " [this, &someVariable, someObject = "
21854 "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21855 " return someObject.startAsyncAction().then(\n"
21856 " [this, &someVariable](AsyncActionResult result) "
21857 "mutable { result.processMore(); });\n"
21858 " });\n"
21859 "}\n",
21860 format("SomeResult doSomething(SomeObject promise) {\n"
21861 " return promise.then([this, &someVariable, someObject = "
21862 "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21863 " return someObject.startAsyncAction().then([this, "
21864 "&someVariable](AsyncActionResult result) mutable {\n"
21865 " result.processMore();\n"
21866 " });\n"
21867 " });\n"
21868 "}\n",
21869 Style));
21870 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
21871 verifyFormat("test() {\n"
21872 " ([]() -> {\n"
21873 " int b = 32;\n"
21874 " return 3;\n"
21875 " }).foo();\n"
21876 "}",
21877 Style);
21878 verifyFormat("test() {\n"
21879 " []() -> {\n"
21880 " int b = 32;\n"
21881 " return 3;\n"
21882 " }\n"
21883 "}",
21884 Style);
21885 verifyFormat("std::sort(v.begin(), v.end(),\n"
21886 " [](const auto &someLongArgumentName, const auto "
21887 "&someOtherLongArgumentName) {\n"
21888 " return someLongArgumentName.someMemberVariable < "
21889 "someOtherLongArgumentName.someMemberVariable;\n"
21890 "});",
21891 Style);
21892 verifyFormat("test() {\n"
21893 " (\n"
21894 " []() -> {\n"
21895 " int b = 32;\n"
21896 " return 3;\n"
21897 " },\n"
21898 " foo, bar)\n"
21899 " .foo();\n"
21900 "}",
21901 Style);
21902 verifyFormat("test() {\n"
21903 " ([]() -> {\n"
21904 " int b = 32;\n"
21905 " return 3;\n"
21906 " })\n"
21907 " .foo()\n"
21908 " .bar();\n"
21909 "}",
21910 Style);
21911 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
21912 " return promise.then(\n"
21913 " [this, &someVariable, someObject = "
21914 "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21915 " return someObject.startAsyncAction().then(\n"
21916 " [this, &someVariable](AsyncActionResult result) mutable { "
21917 "result.processMore(); });\n"
21918 " });\n"
21919 "}\n",
21920 format("SomeResult doSomething(SomeObject promise) {\n"
21921 " return promise.then([this, &someVariable, someObject = "
21922 "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21923 " return someObject.startAsyncAction().then([this, "
21924 "&someVariable](AsyncActionResult result) mutable {\n"
21925 " result.processMore();\n"
21926 " });\n"
21927 " });\n"
21928 "}\n",
21929 Style));
21930 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
21931 " return promise.then([this, &someVariable] {\n"
21932 " return someObject.startAsyncAction().then(\n"
21933 " [this, &someVariable](AsyncActionResult result) mutable { "
21934 "result.processMore(); });\n"
21935 " });\n"
21936 "}\n",
21937 format("SomeResult doSomething(SomeObject promise) {\n"
21938 " return promise.then([this, &someVariable] {\n"
21939 " return someObject.startAsyncAction().then([this, "
21940 "&someVariable](AsyncActionResult result) mutable {\n"
21941 " result.processMore();\n"
21942 " });\n"
21943 " });\n"
21944 "}\n",
21945 Style));
21946 Style = getGoogleStyle();
21947 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
21948 EXPECT_EQ("#define A \\\n"
21949 " [] { \\\n"
21950 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
21951 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
21952 " }",
21953 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
21954 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
21955 Style));
21956 // TODO: The current formatting has a minor issue that's not worth fixing
21957 // right now whereby the closing brace is indented relative to the signature
21958 // instead of being aligned. This only happens with macros.
21959 }
21960
21961 TEST_F(FormatTest, LambdaWithLineComments) {
21962 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
21963 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
21964 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
21965 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21966 FormatStyle::ShortLambdaStyle::SLS_All;
21967
21968 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
21969 verifyFormat("auto k = []() // comment\n"
21970 "{ return; }",
21971 LLVMWithBeforeLambdaBody);
21972 verifyFormat("auto k = []() /* comment */ { return; }",
21973 LLVMWithBeforeLambdaBody);
21974 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
21975 LLVMWithBeforeLambdaBody);
21976 verifyFormat("auto k = []() // X\n"
21977 "{ return; }",
21978 LLVMWithBeforeLambdaBody);
21979 verifyFormat(
21980 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
21981 "{ return; }",
21982 LLVMWithBeforeLambdaBody);
21983
21984 LLVMWithBeforeLambdaBody.ColumnLimit = 0;
21985
21986 verifyFormat("foo([]()\n"
21987 " {\n"
21988 " bar(); //\n"
21989 " return 1; // comment\n"
21990 " }());",
21991 "foo([]() {\n"
21992 " bar(); //\n"
21993 " return 1; // comment\n"
21994 "}());",
21995 LLVMWithBeforeLambdaBody);
21996 verifyFormat("foo(\n"
21997 " 1, MACRO {\n"
21998 " baz();\n"
21999 " bar(); // comment\n"
22000 " },\n"
22001 " []() {});",
22002 "foo(\n"
22003 " 1, MACRO { baz(); bar(); // comment\n"
22004 " }, []() {}\n"
22005 ");",
22006 LLVMWithBeforeLambdaBody);
22007 }
22008
22009 TEST_F(FormatTest, EmptyLinesInLambdas) {
22010 verifyFormat("auto lambda = []() {\n"
22011 " x(); //\n"
22012 "};",
22013 "auto lambda = []() {\n"
22014 "\n"
22015 " x(); //\n"
22016 "\n"
22017 "};");
22018 }
22019
22020 TEST_F(FormatTest, FormatsBlocks) {
22021 FormatStyle ShortBlocks = getLLVMStyle();
22022 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
22023 verifyFormat("int (^Block)(int, int);", ShortBlocks);
22024 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
22025 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
22026 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
22027 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
22028 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
22029
22030 verifyFormat("foo(^{ bar(); });", ShortBlocks);
22031 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
22032 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
22033
22034 verifyFormat("[operation setCompletionBlock:^{\n"
22035 " [self onOperationDone];\n"
22036 "}];");
22037 verifyFormat("int i = {[operation setCompletionBlock:^{\n"
22038 " [self onOperationDone];\n"
22039 "}]};");
22040 verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
22041 " f();\n"
22042 "}];");
22043 verifyFormat("int a = [operation block:^int(int *i) {\n"
22044 " return 1;\n"
22045 "}];");
22046 verifyFormat("[myObject doSomethingWith:arg1\n"
22047 " aaa:^int(int *a) {\n"
22048 " return 1;\n"
22049 " }\n"
22050 " bbb:f(a * bbbbbbbb)];");
22051
22052 verifyFormat("[operation setCompletionBlock:^{\n"
22053 " [self.delegate newDataAvailable];\n"
22054 "}];",
22055 getLLVMStyleWithColumns(60));
22056 verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
22057 " NSString *path = [self sessionFilePath];\n"
22058 " if (path) {\n"
22059 " // ...\n"
22060 " }\n"
22061 "});");
22062 verifyFormat("[[SessionService sharedService]\n"
22063 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
22064 " if (window) {\n"
22065 " [self windowDidLoad:window];\n"
22066 " } else {\n"
22067 " [self errorLoadingWindow];\n"
22068 " }\n"
22069 " }];");
22070 verifyFormat("void (^largeBlock)(void) = ^{\n"
22071 " // ...\n"
22072 "};\n",
22073 getLLVMStyleWithColumns(40));
22074 verifyFormat("[[SessionService sharedService]\n"
22075 " loadWindowWithCompletionBlock: //\n"
22076 " ^(SessionWindow *window) {\n"
22077 " if (window) {\n"
22078 " [self windowDidLoad:window];\n"
22079 " } else {\n"
22080 " [self errorLoadingWindow];\n"
22081 " }\n"
22082 " }];",
22083 getLLVMStyleWithColumns(60));
22084 verifyFormat("[myObject doSomethingWith:arg1\n"
22085 " firstBlock:^(Foo *a) {\n"
22086 " // ...\n"
22087 " int i;\n"
22088 " }\n"
22089 " secondBlock:^(Bar *b) {\n"
22090 " // ...\n"
22091 " int i;\n"
22092 " }\n"
22093 " thirdBlock:^Foo(Bar *b) {\n"
22094 " // ...\n"
22095 " int i;\n"
22096 " }];");
22097 verifyFormat("[myObject doSomethingWith:arg1\n"
22098 " firstBlock:-1\n"
22099 " secondBlock:^(Bar *b) {\n"
22100 " // ...\n"
22101 " int i;\n"
22102 " }];");
22103
22104 verifyFormat("f(^{\n"
22105 " @autoreleasepool {\n"
22106 " if (a) {\n"
22107 " g();\n"
22108 " }\n"
22109 " }\n"
22110 "});");
22111 verifyFormat("Block b = ^int *(A *a, B *b) {}");
22112 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
22113 "};");
22114
22115 FormatStyle FourIndent = getLLVMStyle();
22116 FourIndent.ObjCBlockIndentWidth = 4;
22117 verifyFormat("[operation setCompletionBlock:^{\n"
22118 " [self onOperationDone];\n"
22119 "}];",
22120 FourIndent);
22121 }
22122
22123 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
22124 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
22125
22126 verifyFormat("[[SessionService sharedService] "
22127 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
22128 " if (window) {\n"
22129 " [self windowDidLoad:window];\n"
22130 " } else {\n"
22131 " [self errorLoadingWindow];\n"
22132 " }\n"
22133 "}];",
22134 ZeroColumn);
22135 EXPECT_EQ("[[SessionService sharedService]\n"
22136 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
22137 " if (window) {\n"
22138 " [self windowDidLoad:window];\n"
22139 " } else {\n"
22140 " [self errorLoadingWindow];\n"
22141 " }\n"
22142 " }];",
22143 format("[[SessionService sharedService]\n"
22144 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
22145 " if (window) {\n"
22146 " [self windowDidLoad:window];\n"
22147 " } else {\n"
22148 " [self errorLoadingWindow];\n"
22149 " }\n"
22150 "}];",
22151 ZeroColumn));
22152 verifyFormat("[myObject doSomethingWith:arg1\n"
22153 " firstBlock:^(Foo *a) {\n"
22154 " // ...\n"
22155 " int i;\n"
22156 " }\n"
22157 " secondBlock:^(Bar *b) {\n"
22158 " // ...\n"
22159 " int i;\n"
22160 " }\n"
22161 " thirdBlock:^Foo(Bar *b) {\n"
22162 " // ...\n"
22163 " int i;\n"
22164 " }];",
22165 ZeroColumn);
22166 verifyFormat("f(^{\n"
22167 " @autoreleasepool {\n"
22168 " if (a) {\n"
22169 " g();\n"
22170 " }\n"
22171 " }\n"
22172 "});",
22173 ZeroColumn);
22174 verifyFormat("void (^largeBlock)(void) = ^{\n"
22175 " // ...\n"
22176 "};",
22177 ZeroColumn);
22178
22179 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
22180 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
22181 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
22182 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
22183 EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
22184 " int i;\n"
22185 "};",
22186 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
22187 }
22188
22189 TEST_F(FormatTest, SupportsCRLF) {
22190 EXPECT_EQ("int a;\r\n"
22191 "int b;\r\n"
22192 "int c;\r\n",
22193 format("int a;\r\n"
22194 " int b;\r\n"
22195 " int c;\r\n",
22196 getLLVMStyle()));
22197 EXPECT_EQ("int a;\r\n"
22198 "int b;\r\n"
22199 "int c;\r\n",
22200 format("int a;\r\n"
22201 " int b;\n"
22202 " int c;\r\n",
22203 getLLVMStyle()));
22204 EXPECT_EQ("int a;\n"
22205 "int b;\n"
22206 "int c;\n",
22207 format("int a;\r\n"
22208 " int b;\n"
22209 " int c;\n",
22210 getLLVMStyle()));
22211 EXPECT_EQ("\"aaaaaaa \"\r\n"
22212 "\"bbbbbbb\";\r\n",
22213 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
22214 EXPECT_EQ("#define A \\\r\n"
22215 " b; \\\r\n"
22216 " c; \\\r\n"
22217 " d;\r\n",
22218 format("#define A \\\r\n"
22219 " b; \\\r\n"
22220 " c; d; \r\n",
22221 getGoogleStyle()));
22222
22223 EXPECT_EQ("/*\r\n"
22224 "multi line block comments\r\n"
22225 "should not introduce\r\n"
22226 "an extra carriage return\r\n"
22227 "*/\r\n",
22228 format("/*\r\n"
22229 "multi line block comments\r\n"
22230 "should not introduce\r\n"
22231 "an extra carriage return\r\n"
22232 "*/\r\n"));
22233 EXPECT_EQ("/*\r\n"
22234 "\r\n"
22235 "*/",
22236 format("/*\r\n"
22237 " \r\r\r\n"
22238 "*/"));
22239
22240 FormatStyle style = getLLVMStyle();
22241
22242 EXPECT_EQ(style.LineEnding, FormatStyle::LE_DeriveLF);
22243 EXPECT_EQ("union FooBarBazQux {\n"
22244 " int foo;\n"
22245 " int bar;\n"
22246 " int baz;\n"
22247 "};",
22248 format("union FooBarBazQux {\r\n"
22249 " int foo;\n"
22250 " int bar;\r\n"
22251 " int baz;\n"
22252 "};",
22253 style));
22254 style.LineEnding = FormatStyle::LE_DeriveCRLF;
22255 EXPECT_EQ("union FooBarBazQux {\r\n"
22256 " int foo;\r\n"
22257 " int bar;\r\n"
22258 " int baz;\r\n"
22259 "};",
22260 format("union FooBarBazQux {\r\n"
22261 " int foo;\n"
22262 " int bar;\r\n"
22263 " int baz;\n"
22264 "};",
22265 style));
22266
22267 style.LineEnding = FormatStyle::LE_LF;
22268 EXPECT_EQ("union FooBarBazQux {\n"
22269 " int foo;\n"
22270 " int bar;\n"
22271 " int baz;\n"
22272 " int qux;\n"
22273 "};",
22274 format("union FooBarBazQux {\r\n"
22275 " int foo;\n"
22276 " int bar;\r\n"
22277 " int baz;\n"
22278 " int qux;\r\n"
22279 "};",
22280 style));
22281 style.LineEnding = FormatStyle::LE_CRLF;
22282 EXPECT_EQ("union FooBarBazQux {\r\n"
22283 " int foo;\r\n"
22284 " int bar;\r\n"
22285 " int baz;\r\n"
22286 " int qux;\r\n"
22287 "};",
22288 format("union FooBarBazQux {\r\n"
22289 " int foo;\n"
22290 " int bar;\r\n"
22291 " int baz;\n"
22292 " int qux;\n"
22293 "};",
22294 style));
22295
22296 style.LineEnding = FormatStyle::LE_DeriveLF;
22297 EXPECT_EQ("union FooBarBazQux {\r\n"
22298 " int foo;\r\n"
22299 " int bar;\r\n"
22300 " int baz;\r\n"
22301 " int qux;\r\n"
22302 "};",
22303 format("union FooBarBazQux {\r\n"
22304 " int foo;\n"
22305 " int bar;\r\n"
22306 " int baz;\n"
22307 " int qux;\r\n"
22308 "};",
22309 style));
22310 style.LineEnding = FormatStyle::LE_DeriveCRLF;
22311 EXPECT_EQ("union FooBarBazQux {\n"
22312 " int foo;\n"
22313 " int bar;\n"
22314 " int baz;\n"
22315 " int qux;\n"
22316 "};",
22317 format("union FooBarBazQux {\r\n"
22318 " int foo;\n"
22319 " int bar;\r\n"
22320 " int baz;\n"
22321 " int qux;\n"
22322 "};",
22323 style));
22324 }
22325
22326 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
22327 verifyFormat("MY_CLASS(C) {\n"
22328 " int i;\n"
22329 " int j;\n"
22330 "};");
22331 }
22332
22333 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
22334 FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
22335 TwoIndent.ContinuationIndentWidth = 2;
22336
22337 EXPECT_EQ("int i =\n"
22338 " longFunction(\n"
22339 " arg);",
22340 format("int i = longFunction(arg);", TwoIndent));
22341
22342 FormatStyle SixIndent = getLLVMStyleWithColumns(20);
22343 SixIndent.ContinuationIndentWidth = 6;
22344
22345 EXPECT_EQ("int i =\n"
22346 " longFunction(\n"
22347 " arg);",
22348 format("int i = longFunction(arg);", SixIndent));
22349 }
22350
22351 TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
22352 FormatStyle Style = getLLVMStyle();
22353 verifyFormat("int Foo::getter(\n"
22354 " //\n"
22355 ") const {\n"
22356 " return foo;\n"
22357 "}",
22358 Style);
22359 verifyFormat("void Foo::setter(\n"
22360 " //\n"
22361 ") {\n"
22362 " foo = 1;\n"
22363 "}",
22364 Style);
22365 }
22366
22367 TEST_F(FormatTest, SpacesInAngles) {
22368 FormatStyle Spaces = getLLVMStyle();
22369 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
22370
22371 verifyFormat("vector< ::std::string > x1;", Spaces);
22372 verifyFormat("Foo< int, Bar > x2;", Spaces);
22373 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces);
22374
22375 verifyFormat("static_cast< int >(arg);", Spaces);
22376 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
22377 verifyFormat("f< int, float >();", Spaces);
22378 verifyFormat("template <> g() {}", Spaces);
22379 verifyFormat("template < std::vector< int > > f() {}", Spaces);
22380 verifyFormat("std::function< void(int, int) > fct;", Spaces);
22381 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
22382 Spaces);
22383
22384 Spaces.Standard = FormatStyle::LS_Cpp03;
22385 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
22386 verifyFormat("A< A< int > >();", Spaces);
22387
22388 Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
22389 verifyFormat("A<A<int> >();", Spaces);
22390
22391 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
22392 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
22393 Spaces);
22394 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
22395 Spaces);
22396
22397 verifyFormat("A<A<int> >();", Spaces);
22398 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces);
22399 verifyFormat("A< A< int > >();", Spaces);
22400
22401 Spaces.Standard = FormatStyle::LS_Cpp11;
22402 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
22403 verifyFormat("A< A< int > >();", Spaces);
22404
22405 Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
22406 verifyFormat("vector<::std::string> x4;", Spaces);
22407 verifyFormat("vector<int> x5;", Spaces);
22408 verifyFormat("Foo<int, Bar> x6;", Spaces);
22409 verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
22410
22411 verifyFormat("A<A<int>>();", Spaces);
22412
22413 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
22414 verifyFormat("vector<::std::string> x4;", Spaces);
22415 verifyFormat("vector< ::std::string > x4;", Spaces);
22416 verifyFormat("vector<int> x5;", Spaces);
22417 verifyFormat("vector< int > x5;", Spaces);
22418 verifyFormat("Foo<int, Bar> x6;", Spaces);
22419 verifyFormat("Foo< int, Bar > x6;", Spaces);
22420 verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
22421 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces);
22422
22423 verifyFormat("A<A<int>>();", Spaces);
22424 verifyFormat("A< A< int > >();", Spaces);
22425 verifyFormat("A<A<int > >();", Spaces);
22426 verifyFormat("A< A< int>>();", Spaces);
22427
22428 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
22429 verifyFormat("// clang-format off\n"
22430 "foo<<<1, 1>>>();\n"
22431 "// clang-format on\n",
22432 Spaces);
22433 verifyFormat("// clang-format off\n"
22434 "foo< < <1, 1> > >();\n"
22435 "// clang-format on\n",
22436 Spaces);
22437 }
22438
22439 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
22440 FormatStyle Style = getLLVMStyle();
22441 Style.SpaceAfterTemplateKeyword = false;
22442 verifyFormat("template<int> void foo();", Style);
22443 }
22444
22445 TEST_F(FormatTest, TripleAngleBrackets) {
22446 verifyFormat("f<<<1, 1>>>();");
22447 verifyFormat("f<<<1, 1, 1, s>>>();");
22448 verifyFormat("f<<<a, b, c, d>>>();");
22449 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
22450 verifyFormat("f<param><<<1, 1>>>();");
22451 verifyFormat("f<1><<<1, 1>>>();");
22452 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
22453 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
22454 "aaaaaaaaaaa<<<\n 1, 1>>>();");
22455 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
22456 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
22457 }
22458
22459 TEST_F(FormatTest, MergeLessLessAtEnd) {
22460 verifyFormat("<<");
22461 EXPECT_EQ("< < <", format("\\\n<<<"));
22462 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
22463 "aaallvm::outs() <<");
22464 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
22465 "aaaallvm::outs()\n <<");
22466 }
22467
22468 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
22469 std::string code = "#if A\n"
22470 "#if B\n"
22471 "a.\n"
22472 "#endif\n"
22473 " a = 1;\n"
22474 "#else\n"
22475 "#endif\n"
22476 "#if C\n"
22477 "#else\n"
22478 "#endif\n";
22479 EXPECT_EQ(code, format(code));
22480 }
22481
22482 TEST_F(FormatTest, HandleConflictMarkers) {
22483 // Git/SVN conflict markers.
22484 EXPECT_EQ("int a;\n"
22485 "void f() {\n"
22486 " callme(some(parameter1,\n"
22487 "<<<<<<< text by the vcs\n"
22488 " parameter2),\n"
22489 "||||||| text by the vcs\n"
22490 " parameter2),\n"
22491 " parameter3,\n"
22492 "======= text by the vcs\n"
22493 " parameter2, parameter3),\n"
22494 ">>>>>>> text by the vcs\n"
22495 " otherparameter);\n",
22496 format("int a;\n"
22497 "void f() {\n"
22498 " callme(some(parameter1,\n"
22499 "<<<<<<< text by the vcs\n"
22500 " parameter2),\n"
22501 "||||||| text by the vcs\n"
22502 " parameter2),\n"
22503 " parameter3,\n"
22504 "======= text by the vcs\n"
22505 " parameter2,\n"
22506 " parameter3),\n"
22507 ">>>>>>> text by the vcs\n"
22508 " otherparameter);\n"));
22509
22510 // Perforce markers.
22511 EXPECT_EQ("void f() {\n"
22512 " function(\n"
22513 ">>>> text by the vcs\n"
22514 " parameter,\n"
22515 "==== text by the vcs\n"
22516 " parameter,\n"
22517 "==== text by the vcs\n"
22518 " parameter,\n"
22519 "<<<< text by the vcs\n"
22520 " parameter);\n",
22521 format("void f() {\n"
22522 " function(\n"
22523 ">>>> text by the vcs\n"
22524 " parameter,\n"
22525 "==== text by the vcs\n"
22526 " parameter,\n"
22527 "==== text by the vcs\n"
22528 " parameter,\n"
22529 "<<<< text by the vcs\n"
22530 " parameter);\n"));
22531
22532 EXPECT_EQ("<<<<<<<\n"
22533 "|||||||\n"
22534 "=======\n"
22535 ">>>>>>>",
22536 format("<<<<<<<\n"
22537 "|||||||\n"
22538 "=======\n"
22539 ">>>>>>>"));
22540
22541 EXPECT_EQ("<<<<<<<\n"
22542 "|||||||\n"
22543 "int i;\n"
22544 "=======\n"
22545 ">>>>>>>",
22546 format("<<<<<<<\n"
22547 "|||||||\n"
22548 "int i;\n"
22549 "=======\n"
22550 ">>>>>>>"));
22551
22552 // FIXME: Handle parsing of macros around conflict markers correctly:
22553 EXPECT_EQ("#define Macro \\\n"
22554 "<<<<<<<\n"
22555 "Something \\\n"
22556 "|||||||\n"
22557 "Else \\\n"
22558 "=======\n"
22559 "Other \\\n"
22560 ">>>>>>>\n"
22561 " End int i;\n",
22562 format("#define Macro \\\n"
22563 "<<<<<<<\n"
22564 " Something \\\n"
22565 "|||||||\n"
22566 " Else \\\n"
22567 "=======\n"
22568 " Other \\\n"
22569 ">>>>>>>\n"
22570 " End\n"
22571 "int i;\n"));
22572
22573 verifyFormat(R"(====
22574 #ifdef A
22575 a
22576 #else
22577 b
22578 #endif
22579 )");
22580 }
22581
22582 TEST_F(FormatTest, DisableRegions) {
22583 EXPECT_EQ("int i;\n"
22584 "// clang-format off\n"
22585 " int j;\n"
22586 "// clang-format on\n"
22587 "int k;",
22588 format(" int i;\n"
22589 " // clang-format off\n"
22590 " int j;\n"
22591 " // clang-format on\n"
22592 " int k;"));
22593 EXPECT_EQ("int i;\n"
22594 "/* clang-format off */\n"
22595 " int j;\n"
22596 "/* clang-format on */\n"
22597 "int k;",
22598 format(" int i;\n"
22599 " /* clang-format off */\n"
22600 " int j;\n"
22601 " /* clang-format on */\n"
22602 " int k;"));
22603
22604 // Don't reflow comments within disabled regions.
22605 EXPECT_EQ("// clang-format off\n"
22606 "// long long long long long long line\n"
22607 "/* clang-format on */\n"
22608 "/* long long long\n"
22609 " * long long long\n"
22610 " * line */\n"
22611 "int i;\n"
22612 "/* clang-format off */\n"
22613 "/* long long long long long long line */\n",
22614 format("// clang-format off\n"
22615 "// long long long long long long line\n"
22616 "/* clang-format on */\n"
22617 "/* long long long long long long line */\n"
22618 "int i;\n"
22619 "/* clang-format off */\n"
22620 "/* long long long long long long line */\n",
22621 getLLVMStyleWithColumns(20)));
22622 }
22623
22624 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
22625 format("? ) =");
22626 verifyNoCrash("#define a\\\n /**/}");
22627 }
22628
22629 TEST_F(FormatTest, FormatsTableGenCode) {
22630 FormatStyle Style = getLLVMStyle();
22631 Style.Language = FormatStyle::LK_TableGen;
22632 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
22633 }
22634
22635 TEST_F(FormatTest, ArrayOfTemplates) {
22636 EXPECT_EQ("auto a = new unique_ptr<int>[10];",
22637 format("auto a = new unique_ptr<int > [ 10];"));
22638
22639 FormatStyle Spaces = getLLVMStyle();
22640 Spaces.SpacesInSquareBrackets = true;
22641 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
22642 format("auto a = new unique_ptr<int > [10];", Spaces));
22643 }
22644
22645 TEST_F(FormatTest, ArrayAsTemplateType) {
22646 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
22647 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
22648
22649 FormatStyle Spaces = getLLVMStyle();
22650 Spaces.SpacesInSquareBrackets = true;
22651 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
22652 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
22653 }
22654
22655 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
22656
22657 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
22658 // Column limit is 20.
22659 std::string Code = "Type *a =\n"
22660 " new Type();\n"
22661 "g(iiiii, 0, jjjjj,\n"
22662 " 0, kkkkk, 0, mm);\n"
22663 "int bad = format ;";
22664 std::string Expected = "auto a = new Type();\n"
22665 "g(iiiii, nullptr,\n"
22666 " jjjjj, nullptr,\n"
22667 " kkkkk, nullptr,\n"
22668 " mm);\n"
22669 "int bad = format ;";
22670 FileID ID = Context.createInMemoryFile("format.cpp", Code);
22671 tooling::Replacements Replaces = toReplacements(
22672 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
22673 "auto "),
22674 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
22675 "nullptr"),
22676 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
22677 "nullptr"),
22678 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
22679 "nullptr")});
22680
22681 FormatStyle Style = getLLVMStyle();
22682 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
22683 auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
22684 EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
22685 << llvm::toString(FormattedReplaces.takeError()) << "\n";
22686 auto Result = applyAllReplacements(Code, *FormattedReplaces);
22687 EXPECT_TRUE(static_cast<bool>(Result));
22688 EXPECT_EQ(Expected, *Result);
22689 }
22690
22691 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
22692 std::string Code = "#include \"a.h\"\n"
22693 "#include \"c.h\"\n"
22694 "\n"
22695 "int main() {\n"
22696 " return 0;\n"
22697 "}";
22698 std::string Expected = "#include \"a.h\"\n"
22699 "#include \"b.h\"\n"
22700 "#include \"c.h\"\n"
22701 "\n"
22702 "int main() {\n"
22703 " return 0;\n"
22704 "}";
22705 FileID ID = Context.createInMemoryFile("fix.cpp", Code);
22706 tooling::Replacements Replaces = toReplacements(
22707 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
22708 "#include \"b.h\"\n")});
22709
22710 FormatStyle Style = getLLVMStyle();
22711 Style.SortIncludes = FormatStyle::SI_CaseSensitive;
22712 auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
22713 EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
22714 << llvm::toString(FormattedReplaces.takeError()) << "\n";
22715 auto Result = applyAllReplacements(Code, *FormattedReplaces);
22716 EXPECT_TRUE(static_cast<bool>(Result));
22717 EXPECT_EQ(Expected, *Result);
22718 }
22719
22720 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
22721 EXPECT_EQ("using std::cin;\n"
22722 "using std::cout;",
22723 format("using std::cout;\n"
22724 "using std::cin;",
22725 getGoogleStyle()));
22726 }
22727
22728 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
22729 FormatStyle Style = getLLVMStyle();
22730 Style.Standard = FormatStyle::LS_Cpp03;
22731 // cpp03 recognize this string as identifier u8 and literal character 'a'
22732 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
22733 }
22734
22735 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
22736 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
22737 // all modes, including C++11, C++14 and C++17
22738 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
22739 }
22740
22741 TEST_F(FormatTest, DoNotFormatLikelyXml) {
22742 EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle()));
22743 EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle()));
22744 }
22745
22746 TEST_F(FormatTest, StructuredBindings) {
22747 // Structured bindings is a C++17 feature.
22748 // all modes, including C++11, C++14 and C++17
22749 verifyFormat("auto [a, b] = f();");
22750 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
22751 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();"));
22752 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();"));
22753 EXPECT_EQ("auto const volatile [a, b] = f();",
22754 format("auto const volatile[a, b] = f();"));
22755 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();"));
22756 EXPECT_EQ("auto &[a, b, c] = f();",
22757 format("auto &[ a , b,c ] = f();"));
22758 EXPECT_EQ("auto &&[a, b, c] = f();",
22759 format("auto &&[ a , b,c ] = f();"));
22760 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();"));
22761 EXPECT_EQ("auto const volatile &&[a, b] = f();",
22762 format("auto const volatile &&[a, b] = f();"));
22763 EXPECT_EQ("auto const &&[a, b] = f();",
22764 format("auto const && [a, b] = f();"));
22765 EXPECT_EQ("const auto &[a, b] = f();",
22766 format("const auto & [a, b] = f();"));
22767 EXPECT_EQ("const auto volatile &&[a, b] = f();",
22768 format("const auto volatile &&[a, b] = f();"));
22769 EXPECT_EQ("volatile const auto &&[a, b] = f();",
22770 format("volatile const auto &&[a, b] = f();"));
22771 EXPECT_EQ("const auto &&[a, b] = f();",
22772 format("const auto && [a, b] = f();"));
22773
22774 // Make sure we don't mistake structured bindings for lambdas.
22775 FormatStyle PointerMiddle = getLLVMStyle();
22776 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
22777 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
22778 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
22779 verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
22780 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
22781 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
22782 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
22783 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
22784 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
22785 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
22786 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
22787 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
22788 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
22789
22790 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
22791 format("for (const auto && [a, b] : some_range) {\n}"));
22792 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
22793 format("for (const auto & [a, b] : some_range) {\n}"));
22794 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
22795 format("for (const auto[a, b] : some_range) {\n}"));
22796 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);"));
22797 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);"));
22798 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);"));
22799 EXPECT_EQ("auto const &[x, y](expr);",
22800 format("auto const & [x,y] (expr);"));
22801 EXPECT_EQ("auto const &&[x, y](expr);",
22802 format("auto const && [x,y] (expr);"));
22803 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};"));
22804 EXPECT_EQ("auto const &[x, y]{expr};",
22805 format("auto const & [x,y] {expr};"));
22806 EXPECT_EQ("auto const &&[x, y]{expr};",
22807 format("auto const && [x,y] {expr};"));
22808
22809 FormatStyle Spaces = getLLVMStyle();
22810 Spaces.SpacesInSquareBrackets = true;
22811 verifyFormat("auto [ a, b ] = f();", Spaces);
22812 verifyFormat("auto &&[ a, b ] = f();", Spaces);
22813 verifyFormat("auto &[ a, b ] = f();", Spaces);
22814 verifyFormat("auto const &&[ a, b ] = f();", Spaces);
22815 verifyFormat("auto const &[ a, b ] = f();", Spaces);
22816 }
22817
22818 TEST_F(FormatTest, FileAndCode) {
22819 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
22820 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
22821 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
22822 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
22823 EXPECT_EQ(FormatStyle::LK_ObjC,
22824 guessLanguage("foo.h", "@interface Foo\n@end\n"));
22825 EXPECT_EQ(
22826 FormatStyle::LK_ObjC,
22827 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
22828 EXPECT_EQ(FormatStyle::LK_ObjC,
22829 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
22830 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
22831 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
22832 EXPECT_EQ(FormatStyle::LK_ObjC,
22833 guessLanguage("foo", "@interface Foo\n@end\n"));
22834 EXPECT_EQ(FormatStyle::LK_ObjC,
22835 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
22836 EXPECT_EQ(
22837 FormatStyle::LK_ObjC,
22838 guessLanguage("foo.h",
22839 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
22840 EXPECT_EQ(
22841 FormatStyle::LK_Cpp,
22842 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
22843 // Only one of the two preprocessor regions has ObjC-like code.
22844 EXPECT_EQ(FormatStyle::LK_ObjC,
22845 guessLanguage("foo.h", "#if A\n"
22846 "#define B() C\n"
22847 "#else\n"
22848 "#define B() [NSString a:@\"\"]\n"
22849 "#endif\n"));
22850 }
22851
22852 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
22853 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
22854 EXPECT_EQ(FormatStyle::LK_ObjC,
22855 guessLanguage("foo.h", "array[[calculator getIndex]];"));
22856 EXPECT_EQ(FormatStyle::LK_Cpp,
22857 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
22858 EXPECT_EQ(
22859 FormatStyle::LK_Cpp,
22860 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
22861 EXPECT_EQ(FormatStyle::LK_ObjC,
22862 guessLanguage("foo.h", "[[noreturn foo] bar];"));
22863 EXPECT_EQ(FormatStyle::LK_Cpp,
22864 guessLanguage("foo.h", "[[clang::fallthrough]];"));
22865 EXPECT_EQ(FormatStyle::LK_ObjC,
22866 guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
22867 EXPECT_EQ(FormatStyle::LK_Cpp,
22868 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
22869 EXPECT_EQ(FormatStyle::LK_Cpp,
22870 guessLanguage("foo.h", "[[using clang: fallthrough]];"));
22871 EXPECT_EQ(FormatStyle::LK_ObjC,
22872 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
22873 EXPECT_EQ(FormatStyle::LK_Cpp,
22874 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
22875 EXPECT_EQ(
22876 FormatStyle::LK_Cpp,
22877 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
22878 EXPECT_EQ(
22879 FormatStyle::LK_Cpp,
22880 guessLanguage("foo.h",
22881 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
22882 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
22883 }
22884
22885 TEST_F(FormatTest, GuessLanguageWithCaret) {
22886 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
22887 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
22888 EXPECT_EQ(FormatStyle::LK_ObjC,
22889 guessLanguage("foo.h", "int(^)(char, float);"));
22890 EXPECT_EQ(FormatStyle::LK_ObjC,
22891 guessLanguage("foo.h", "int(^foo)(char, float);"));
22892 EXPECT_EQ(FormatStyle::LK_ObjC,
22893 guessLanguage("foo.h", "int(^foo[10])(char, float);"));
22894 EXPECT_EQ(FormatStyle::LK_ObjC,
22895 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
22896 EXPECT_EQ(
22897 FormatStyle::LK_ObjC,
22898 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
22899 }
22900
22901 TEST_F(FormatTest, GuessLanguageWithPragmas) {
22902 EXPECT_EQ(FormatStyle::LK_Cpp,
22903 guessLanguage("foo.h", "__pragma(warning(disable:))"));
22904 EXPECT_EQ(FormatStyle::LK_Cpp,
22905 guessLanguage("foo.h", "#pragma(warning(disable:))"));
22906 EXPECT_EQ(FormatStyle::LK_Cpp,
22907 guessLanguage("foo.h", "_Pragma(warning(disable:))"));
22908 }
22909
22910 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
22911 // ASM symbolic names are identifiers that must be surrounded by [] without
22912 // space in between:
22913 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
22914
22915 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
22916 verifyFormat(R"(//
22917 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
22918 )");
22919
22920 // A list of several ASM symbolic names.
22921 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
22922
22923 // ASM symbolic names in inline ASM with inputs and outputs.
22924 verifyFormat(R"(//
22925 asm("cmoveq %1, %2, %[result]"
22926 : [result] "=r"(result)
22927 : "r"(test), "r"(new), "[result]"(old));
22928 )");
22929
22930 // ASM symbolic names in inline ASM with no outputs.
22931 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
22932 }
22933
22934 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
22935 EXPECT_EQ(FormatStyle::LK_Cpp,
22936 guessLanguage("foo.h", "void f() {\n"
22937 " asm (\"mov %[e], %[d]\"\n"
22938 " : [d] \"=rm\" (d)\n"
22939 " [e] \"rm\" (*e));\n"
22940 "}"));
22941 EXPECT_EQ(FormatStyle::LK_Cpp,
22942 guessLanguage("foo.h", "void f() {\n"
22943 " _asm (\"mov %[e], %[d]\"\n"
22944 " : [d] \"=rm\" (d)\n"
22945 " [e] \"rm\" (*e));\n"
22946 "}"));
22947 EXPECT_EQ(FormatStyle::LK_Cpp,
22948 guessLanguage("foo.h", "void f() {\n"
22949 " __asm (\"mov %[e], %[d]\"\n"
22950 " : [d] \"=rm\" (d)\n"
22951 " [e] \"rm\" (*e));\n"
22952 "}"));
22953 EXPECT_EQ(FormatStyle::LK_Cpp,
22954 guessLanguage("foo.h", "void f() {\n"
22955 " __asm__ (\"mov %[e], %[d]\"\n"
22956 " : [d] \"=rm\" (d)\n"
22957 " [e] \"rm\" (*e));\n"
22958 "}"));
22959 EXPECT_EQ(FormatStyle::LK_Cpp,
22960 guessLanguage("foo.h", "void f() {\n"
22961 " asm (\"mov %[e], %[d]\"\n"
22962 " : [d] \"=rm\" (d),\n"
22963 " [e] \"rm\" (*e));\n"
22964 "}"));
22965 EXPECT_EQ(FormatStyle::LK_Cpp,
22966 guessLanguage("foo.h", "void f() {\n"
22967 " asm volatile (\"mov %[e], %[d]\"\n"
22968 " : [d] \"=rm\" (d)\n"
22969 " [e] \"rm\" (*e));\n"
22970 "}"));
22971 }
22972
22973 TEST_F(FormatTest, GuessLanguageWithChildLines) {
22974 EXPECT_EQ(FormatStyle::LK_Cpp,
22975 guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
22976 EXPECT_EQ(FormatStyle::LK_ObjC,
22977 guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
22978 EXPECT_EQ(
22979 FormatStyle::LK_Cpp,
22980 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
22981 EXPECT_EQ(
22982 FormatStyle::LK_ObjC,
22983 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
22984 }
22985
22986 TEST_F(FormatTest, TypenameMacros) {
22987 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
22988
22989 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
22990 FormatStyle Google = getGoogleStyleWithColumns(0);
22991 Google.TypenameMacros = TypenameMacros;
22992 verifyFormat("struct foo {\n"
22993 " int bar;\n"
22994 " TAILQ_ENTRY(a) bleh;\n"
22995 "};",
22996 Google);
22997
22998 FormatStyle Macros = getLLVMStyle();
22999 Macros.TypenameMacros = TypenameMacros;
23000
23001 verifyFormat("STACK_OF(int) a;", Macros);
23002 verifyFormat("STACK_OF(int) *a;", Macros);
23003 verifyFormat("STACK_OF(int const *) *a;", Macros);
23004 verifyFormat("STACK_OF(int *const) *a;", Macros);
23005 verifyFormat("STACK_OF(int, string) a;", Macros);
23006 verifyFormat("STACK_OF(LIST(int)) a;", Macros);
23007 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
23008 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
23009 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
23010 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros);
23011 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros);
23012
23013 Macros.PointerAlignment = FormatStyle::PAS_Left;
23014 verifyFormat("STACK_OF(int)* a;", Macros);
23015 verifyFormat("STACK_OF(int*)* a;", Macros);
23016 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
23017 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
23018 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros);
23019 }
23020
23021 TEST_F(FormatTest, AtomicQualifier) {
23022 // Check that we treate _Atomic as a type and not a function call
23023 FormatStyle Google = getGoogleStyleWithColumns(0);
23024 verifyFormat("struct foo {\n"
23025 " int a1;\n"
23026 " _Atomic(a) a2;\n"
23027 " _Atomic(_Atomic(int) *const) a3;\n"
23028 "};",
23029 Google);
23030 verifyFormat("_Atomic(uint64_t) a;");
23031 verifyFormat("_Atomic(uint64_t) *a;");
23032 verifyFormat("_Atomic(uint64_t const *) *a;");
23033 verifyFormat("_Atomic(uint64_t *const) *a;");
23034 verifyFormat("_Atomic(const uint64_t *) *a;");
23035 verifyFormat("_Atomic(uint64_t) a;");
23036 verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
23037 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
23038 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
23039 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
23040
23041 verifyFormat("_Atomic(uint64_t) *s(InitValue);");
23042 verifyFormat("_Atomic(uint64_t) *s{InitValue};");
23043 FormatStyle Style = getLLVMStyle();
23044 Style.PointerAlignment = FormatStyle::PAS_Left;
23045 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style);
23046 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style);
23047 verifyFormat("_Atomic(int)* a;", Style);
23048 verifyFormat("_Atomic(int*)* a;", Style);
23049 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
23050
23051 Style.SpacesInCStyleCastParentheses = true;
23052 Style.SpacesInParentheses = false;
23053 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
23054 Style.SpacesInCStyleCastParentheses = false;
23055 Style.SpacesInParentheses = true;
23056 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
23057 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
23058 }
23059
23060 TEST_F(FormatTest, C11Generic) {
23061 verifyFormat("_Generic(x, int: 1, default: 0)");
23062 verifyFormat("#define cbrt(X) _Generic((X), float: cbrtf, default: cbrt)(X)");
23063 verifyFormat("_Generic(x, const char *: 1, char *const: 16, int: 8);");
23064 verifyFormat("_Generic(x, int: f1, const int: f2)();");
23065 verifyFormat("_Generic(x, struct A: 1, void (*)(void): 2);");
23066
23067 verifyFormat("_Generic(x,\n"
23068 " float: f,\n"
23069 " default: d,\n"
23070 " long double: ld,\n"
23071 " float _Complex: fc,\n"
23072 " double _Complex: dc,\n"
23073 " long double _Complex: ldc)");
23074
23075 FormatStyle Style = getLLVMStyle();
23076 Style.ColumnLimit = 40;
23077 verifyFormat("#define LIMIT_MAX(T) \\\n"
23078 " _Generic(((T)0), \\\n"
23079 " unsigned int: UINT_MAX, \\\n"
23080 " unsigned long: ULONG_MAX, \\\n"
23081 " unsigned long long: ULLONG_MAX)",
23082 Style);
23083 verifyFormat("_Generic(x,\n"
23084 " struct A: 1,\n"
23085 " void (*)(void): 2);",
23086 Style);
23087
23088 Style.ContinuationIndentWidth = 2;
23089 verifyFormat("_Generic(x,\n"
23090 " struct A: 1,\n"
23091 " void (*)(void): 2);",
23092 Style);
23093 }
23094
23095 TEST_F(FormatTest, AmbersandInLamda) {
23096 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
23097 FormatStyle AlignStyle = getLLVMStyle();
23098 AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
23099 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
23100 AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
23101 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
23102 }
23103
23104 TEST_F(FormatTest, TrailingReturnTypeAuto) {
23105 FormatStyle Style = getLLVMStyle();
23106 verifyFormat("[]() -> auto { return Val; }", Style);
23107 verifyFormat("[]() -> auto * { return Val; }", Style);
23108 verifyFormat("[]() -> auto & { return Val; }", Style);
23109 verifyFormat("auto foo() -> auto { return Val; }", Style);
23110 verifyFormat("auto foo() -> auto * { return Val; }", Style);
23111 verifyFormat("auto foo() -> auto & { return Val; }", Style);
23112 }
23113
23114 TEST_F(FormatTest, SpacesInConditionalStatement) {
23115 FormatStyle Spaces = getLLVMStyle();
23116 Spaces.IfMacros.clear();
23117 Spaces.IfMacros.push_back("MYIF");
23118 Spaces.SpacesInConditionalStatement = true;
23119 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces);
23120 verifyFormat("if ( !a )\n return;", Spaces);
23121 verifyFormat("if ( a )\n return;", Spaces);
23122 verifyFormat("if constexpr ( a )\n return;", Spaces);
23123 verifyFormat("MYIF ( a )\n return;", Spaces);
23124 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces);
23125 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces);
23126 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces);
23127 verifyFormat("while ( a )\n return;", Spaces);
23128 verifyFormat("while ( (a && b) )\n return;", Spaces);
23129 verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
23130 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
23131 // Check that space on the left of "::" is inserted as expected at beginning
23132 // of condition.
23133 verifyFormat("while ( ::func() )\n return;", Spaces);
23134
23135 // Check impact of ControlStatementsExceptControlMacros is honored.
23136 Spaces.SpaceBeforeParens =
23137 FormatStyle::SBPO_ControlStatementsExceptControlMacros;
23138 verifyFormat("MYIF( a )\n return;", Spaces);
23139 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces);
23140 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces);
23141 }
23142
23143 TEST_F(FormatTest, AlternativeOperators) {
23144 // Test case for ensuring alternate operators are not
23145 // combined with their right most neighbour.
23146 verifyFormat("int a and b;");
23147 verifyFormat("int a and_eq b;");
23148 verifyFormat("int a bitand b;");
23149 verifyFormat("int a bitor b;");
23150 verifyFormat("int a compl b;");
23151 verifyFormat("int a not b;");
23152 verifyFormat("int a not_eq b;");
23153 verifyFormat("int a or b;");
23154 verifyFormat("int a xor b;");
23155 verifyFormat("int a xor_eq b;");
23156 verifyFormat("return this not_eq bitand other;");
23157 verifyFormat("bool operator not_eq(const X bitand other)");
23158
23159 verifyFormat("int a and 5;");
23160 verifyFormat("int a and_eq 5;");
23161 verifyFormat("int a bitand 5;");
23162 verifyFormat("int a bitor 5;");
23163 verifyFormat("int a compl 5;");
23164 verifyFormat("int a not 5;");
23165 verifyFormat("int a not_eq 5;");
23166 verifyFormat("int a or 5;");
23167 verifyFormat("int a xor 5;");
23168 verifyFormat("int a xor_eq 5;");
23169
23170 verifyFormat("int a compl(5);");
23171 verifyFormat("int a not(5);");
23172
23173 /* FIXME handle alternate tokens
23174 * https://en.cppreference.com/w/cpp/language/operator_alternative
23175 // alternative tokens
23176 verifyFormat("compl foo();"); // ~foo();
23177 verifyFormat("foo() <%%>;"); // foo();
23178 verifyFormat("void foo() <%%>;"); // void foo(){}
23179 verifyFormat("int a <:1:>;"); // int a[1];[
23180 verifyFormat("%:define ABC abc"); // #define ABC abc
23181 verifyFormat("%:%:"); // ##
23182 */
23183 }
23184
23185 TEST_F(FormatTest, STLWhileNotDefineChed) {
23186 verifyFormat("#if defined(while)\n"
23187 "#define while EMIT WARNING C4005\n"
23188 "#endif // while");
23189 }
23190
23191 TEST_F(FormatTest, OperatorSpacing) {
23192 FormatStyle Style = getLLVMStyle();
23193 Style.PointerAlignment = FormatStyle::PAS_Right;
23194 verifyFormat("Foo::operator*();", Style);
23195 verifyFormat("Foo::operator void *();", Style);
23196 verifyFormat("Foo::operator void **();", Style);
23197 verifyFormat("Foo::operator void *&();", Style);
23198 verifyFormat("Foo::operator void *&&();", Style);
23199 verifyFormat("Foo::operator void const *();", Style);
23200 verifyFormat("Foo::operator void const **();", Style);
23201 verifyFormat("Foo::operator void const *&();", Style);
23202 verifyFormat("Foo::operator void const *&&();", Style);
23203 verifyFormat("Foo::operator()(void *);", Style);
23204 verifyFormat("Foo::operator*(void *);", Style);
23205 verifyFormat("Foo::operator*();", Style);
23206 verifyFormat("Foo::operator**();", Style);
23207 verifyFormat("Foo::operator&();", Style);
23208 verifyFormat("Foo::operator<int> *();", Style);
23209 verifyFormat("Foo::operator<Foo> *();", Style);
23210 verifyFormat("Foo::operator<int> **();", Style);
23211 verifyFormat("Foo::operator<Foo> **();", Style);
23212 verifyFormat("Foo::operator<int> &();", Style);
23213 verifyFormat("Foo::operator<Foo> &();", Style);
23214 verifyFormat("Foo::operator<int> &&();", Style);
23215 verifyFormat("Foo::operator<Foo> &&();", Style);
23216 verifyFormat("Foo::operator<int> *&();", Style);
23217 verifyFormat("Foo::operator<Foo> *&();", Style);
23218 verifyFormat("Foo::operator<int> *&&();", Style);
23219 verifyFormat("Foo::operator<Foo> *&&();", Style);
23220 verifyFormat("operator*(int (*)(), class Foo);", Style);
23221
23222 verifyFormat("Foo::operator&();", Style);
23223 verifyFormat("Foo::operator void &();", Style);
23224 verifyFormat("Foo::operator void const &();", Style);
23225 verifyFormat("Foo::operator()(void &);", Style);
23226 verifyFormat("Foo::operator&(void &);", Style);
23227 verifyFormat("Foo::operator&();", Style);
23228 verifyFormat("operator&(int (&)(), class Foo);", Style);
23229 verifyFormat("operator&&(int (&)(), class Foo);", Style);
23230
23231 verifyFormat("Foo::operator&&();", Style);
23232 verifyFormat("Foo::operator**();", Style);
23233 verifyFormat("Foo::operator void &&();", Style);
23234 verifyFormat("Foo::operator void const &&();", Style);
23235 verifyFormat("Foo::operator()(void &&);", Style);
23236 verifyFormat("Foo::operator&&(void &&);", Style);
23237 verifyFormat("Foo::operator&&();", Style);
23238 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
23239 verifyFormat("operator const nsTArrayRight<E> &()", Style);
23240 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
23241 Style);
23242 verifyFormat("operator void **()", Style);
23243 verifyFormat("operator const FooRight<Object> &()", Style);
23244 verifyFormat("operator const FooRight<Object> *()", Style);
23245 verifyFormat("operator const FooRight<Object> **()", Style);
23246 verifyFormat("operator const FooRight<Object> *&()", Style);
23247 verifyFormat("operator const FooRight<Object> *&&()", Style);
23248
23249 Style.PointerAlignment = FormatStyle::PAS_Left;
23250 verifyFormat("Foo::operator*();", Style);
23251 verifyFormat("Foo::operator**();", Style);
23252 verifyFormat("Foo::operator void*();", Style);
23253 verifyFormat("Foo::operator void**();", Style);
23254 verifyFormat("Foo::operator void*&();", Style);
23255 verifyFormat("Foo::operator void*&&();", Style);
23256 verifyFormat("Foo::operator void const*();", Style);
23257 verifyFormat("Foo::operator void const**();", Style);
23258 verifyFormat("Foo::operator void const*&();", Style);
23259 verifyFormat("Foo::operator void const*&&();", Style);
23260 verifyFormat("Foo::operator/*comment*/ void*();", Style);
23261 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
23262 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
23263 verifyFormat("Foo::operator()(void*);", Style);
23264 verifyFormat("Foo::operator*(void*);", Style);
23265 verifyFormat("Foo::operator*();", Style);
23266 verifyFormat("Foo::operator<int>*();", Style);
23267 verifyFormat("Foo::operator<Foo>*();", Style);
23268 verifyFormat("Foo::operator<int>**();", Style);
23269 verifyFormat("Foo::operator<Foo>**();", Style);
23270 verifyFormat("Foo::operator<Foo>*&();", Style);
23271 verifyFormat("Foo::operator<int>&();", Style);
23272 verifyFormat("Foo::operator<Foo>&();", Style);
23273 verifyFormat("Foo::operator<int>&&();", Style);
23274 verifyFormat("Foo::operator<Foo>&&();", Style);
23275 verifyFormat("Foo::operator<int>*&();", Style);
23276 verifyFormat("Foo::operator<Foo>*&();", Style);
23277 verifyFormat("operator*(int (*)(), class Foo);", Style);
23278
23279 verifyFormat("Foo::operator&();", Style);
23280 verifyFormat("Foo::operator void&();", Style);
23281 verifyFormat("Foo::operator void const&();", Style);
23282 verifyFormat("Foo::operator/*comment*/ void&();", Style);
23283 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
23284 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
23285 verifyFormat("Foo::operator()(void&);", Style);
23286 verifyFormat("Foo::operator&(void&);", Style);
23287 verifyFormat("Foo::operator&();", Style);
23288 verifyFormat("operator&(int (&)(), class Foo);", Style);
23289 verifyFormat("operator&(int (&&)(), class Foo);", Style);
23290 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
23291
23292 verifyFormat("Foo::operator&&();", Style);
23293 verifyFormat("Foo::operator void&&();", Style);
23294 verifyFormat("Foo::operator void const&&();", Style);
23295 verifyFormat("Foo::operator/*comment*/ void&&();", Style);
23296 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
23297 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
23298 verifyFormat("Foo::operator()(void&&);", Style);
23299 verifyFormat("Foo::operator&&(void&&);", Style);
23300 verifyFormat("Foo::operator&&();", Style);
23301 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
23302 verifyFormat("operator const nsTArrayLeft<E>&()", Style);
23303 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
23304 Style);
23305 verifyFormat("operator void**()", Style);
23306 verifyFormat("operator const FooLeft<Object>&()", Style);
23307 verifyFormat("operator const FooLeft<Object>*()", Style);
23308 verifyFormat("operator const FooLeft<Object>**()", Style);
23309 verifyFormat("operator const FooLeft<Object>*&()", Style);
23310 verifyFormat("operator const FooLeft<Object>*&&()", Style);
23311
23312 // PR45107
23313 verifyFormat("operator Vector<String>&();", Style);
23314 verifyFormat("operator const Vector<String>&();", Style);
23315 verifyFormat("operator foo::Bar*();", Style);
23316 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
23317 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
23318 Style);
23319
23320 Style.PointerAlignment = FormatStyle::PAS_Middle;
23321 verifyFormat("Foo::operator*();", Style);
23322 verifyFormat("Foo::operator void *();", Style);
23323 verifyFormat("Foo::operator()(void *);", Style);
23324 verifyFormat("Foo::operator*(void *);", Style);
23325 verifyFormat("Foo::operator*();", Style);
23326 verifyFormat("operator*(int (*)(), class Foo);", Style);
23327
23328 verifyFormat("Foo::operator&();", Style);
23329 verifyFormat("Foo::operator void &();", Style);
23330 verifyFormat("Foo::operator void const &();", Style);
23331 verifyFormat("Foo::operator()(void &);", Style);
23332 verifyFormat("Foo::operator&(void &);", Style);
23333 verifyFormat("Foo::operator&();", Style);
23334 verifyFormat("operator&(int (&)(), class Foo);", Style);
23335
23336 verifyFormat("Foo::operator&&();", Style);
23337 verifyFormat("Foo::operator void &&();", Style);
23338 verifyFormat("Foo::operator void const &&();", Style);
23339 verifyFormat("Foo::operator()(void &&);", Style);
23340 verifyFormat("Foo::operator&&(void &&);", Style);
23341 verifyFormat("Foo::operator&&();", Style);
23342 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
23343 }
23344
23345 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
23346 FormatStyle Style = getLLVMStyle();
23347 // PR46157
23348 verifyFormat("foo(operator+, -42);", Style);
23349 verifyFormat("foo(operator++, -42);", Style);
23350 verifyFormat("foo(operator--, -42);", Style);
23351 verifyFormat("foo(-42, operator--);", Style);
23352 verifyFormat("foo(-42, operator, );", Style);
23353 verifyFormat("foo(operator, , -42);", Style);
23354 }
23355
23356 TEST_F(FormatTest, WhitespaceSensitiveMacros) {
23357 FormatStyle Style = getLLVMStyle();
23358 Style.WhitespaceSensitiveMacros.push_back("FOO");
23359
23360 // Newlines are important here.
23361 verifyFormat("FOO(1+2 )\n", Style);
23362 verifyFormat("FOO(a:b:c)\n", Style);
23363
23364 // Don't use the helpers here, since 'mess up' will change the whitespace
23365 // and these are all whitespace sensitive by definition
23366 EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);",
23367 format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style));
23368 EXPECT_EQ(
23369 "FOO(String-ized&Messy+But\\(: :Still)=Intentional);",
23370 format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style));
23371 EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);",
23372 format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style));
23373 EXPECT_EQ("FOO(String-ized&Messy+But,: :\n"
23374 " Still=Intentional);",
23375 format("FOO(String-ized&Messy+But,: :\n"
23376 " Still=Intentional);",
23377 Style));
23378 Style.AlignConsecutiveAssignments.Enabled = true;
23379 EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n"
23380 " Still=Intentional);",
23381 format("FOO(String-ized=&Messy+But,: :\n"
23382 " Still=Intentional);",
23383 Style));
23384
23385 Style.ColumnLimit = 21;
23386 EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);",
23387 format("FOO(String-ized&Messy+But: :Still=Intentional);", Style));
23388 }
23389
23390 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
23391 // These tests are not in NamespaceEndCommentsFixerTest because that doesn't
23392 // test its interaction with line wrapping
23393 FormatStyle Style = getLLVMStyleWithColumns(80);
23394 verifyFormat("namespace {\n"
23395 "int i;\n"
23396 "int j;\n"
23397 "} // namespace",
23398 Style);
23399
23400 verifyFormat("namespace AAA {\n"
23401 "int i;\n"
23402 "int j;\n"
23403 "} // namespace AAA",
23404 Style);
23405
23406 EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n"
23407 "int i;\n"
23408 "int j;\n"
23409 "} // namespace Averyveryveryverylongnamespace",
23410 format("namespace Averyveryveryverylongnamespace {\n"
23411 "int i;\n"
23412 "int j;\n"
23413 "}",
23414 Style));
23415
23416 EXPECT_EQ(
23417 "namespace "
23418 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
23419 " went::mad::now {\n"
23420 "int i;\n"
23421 "int j;\n"
23422 "} // namespace\n"
23423 " // "
23424 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
23425 "went::mad::now",
23426 format("namespace "
23427 "would::it::save::you::a::lot::of::time::if_::i::"
23428 "just::gave::up::and_::went::mad::now {\n"
23429 "int i;\n"
23430 "int j;\n"
23431 "}",
23432 Style));
23433
23434 // This used to duplicate the comment again and again on subsequent runs
23435 EXPECT_EQ(
23436 "namespace "
23437 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
23438 " went::mad::now {\n"
23439 "int i;\n"
23440 "int j;\n"
23441 "} // namespace\n"
23442 " // "
23443 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
23444 "went::mad::now",
23445 format("namespace "
23446 "would::it::save::you::a::lot::of::time::if_::i::"
23447 "just::gave::up::and_::went::mad::now {\n"
23448 "int i;\n"
23449 "int j;\n"
23450 "} // namespace\n"
23451 " // "
23452 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
23453 "and_::went::mad::now",
23454 Style));
23455 }
23456
23457 TEST_F(FormatTest, LikelyUnlikely) {
23458 FormatStyle Style = getLLVMStyle();
23459
23460 verifyFormat("if (argc > 5) [[unlikely]] {\n"
23461 " return 29;\n"
23462 "}",
23463 Style);
23464
23465 verifyFormat("if (argc > 5) [[likely]] {\n"
23466 " return 29;\n"
23467 "}",
23468 Style);
23469
23470 verifyFormat("if (argc > 5) [[unlikely]] {\n"
23471 " return 29;\n"
23472 "} else [[likely]] {\n"
23473 " return 42;\n"
23474 "}\n",
23475 Style);
23476
23477 verifyFormat("if (argc > 5) [[unlikely]] {\n"
23478 " return 29;\n"
23479 "} else if (argc > 10) [[likely]] {\n"
23480 " return 99;\n"
23481 "} else {\n"
23482 " return 42;\n"
23483 "}\n",
23484 Style);
23485
23486 verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
23487 " return 29;\n"
23488 "}",
23489 Style);
23490
23491 verifyFormat("if (argc > 5) [[unlikely]]\n"
23492 " return 29;\n",
23493 Style);
23494 verifyFormat("if (argc > 5) [[likely]]\n"
23495 " return 29;\n",
23496 Style);
23497
23498 verifyFormat("while (limit > 0) [[unlikely]] {\n"
23499 " --limit;\n"
23500 "}",
23501 Style);
23502 verifyFormat("for (auto &limit : limits) [[likely]] {\n"
23503 " --limit;\n"
23504 "}",
23505 Style);
23506
23507 verifyFormat("for (auto &limit : limits) [[unlikely]]\n"
23508 " --limit;",
23509 Style);
23510 verifyFormat("while (limit > 0) [[likely]]\n"
23511 " --limit;",
23512 Style);
23513
23514 Style.AttributeMacros.push_back("UNLIKELY");
23515 Style.AttributeMacros.push_back("LIKELY");
23516 verifyFormat("if (argc > 5) UNLIKELY\n"
23517 " return 29;\n",
23518 Style);
23519
23520 verifyFormat("if (argc > 5) UNLIKELY {\n"
23521 " return 29;\n"
23522 "}",
23523 Style);
23524 verifyFormat("if (argc > 5) UNLIKELY {\n"
23525 " return 29;\n"
23526 "} else [[likely]] {\n"
23527 " return 42;\n"
23528 "}\n",
23529 Style);
23530 verifyFormat("if (argc > 5) UNLIKELY {\n"
23531 " return 29;\n"
23532 "} else LIKELY {\n"
23533 " return 42;\n"
23534 "}\n",
23535 Style);
23536 verifyFormat("if (argc > 5) [[unlikely]] {\n"
23537 " return 29;\n"
23538 "} else LIKELY {\n"
23539 " return 42;\n"
23540 "}\n",
23541 Style);
23542
23543 verifyFormat("for (auto &limit : limits) UNLIKELY {\n"
23544 " --limit;\n"
23545 "}",
23546 Style);
23547 verifyFormat("while (limit > 0) LIKELY {\n"
23548 " --limit;\n"
23549 "}",
23550 Style);
23551
23552 verifyFormat("while (limit > 0) UNLIKELY\n"
23553 " --limit;",
23554 Style);
23555 verifyFormat("for (auto &limit : limits) LIKELY\n"
23556 " --limit;",
23557 Style);
23558 }
23559
23560 TEST_F(FormatTest, PenaltyIndentedWhitespace) {
23561 verifyFormat("Constructor()\n"
23562 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
23563 " aaaa(aaaaaaaaaaaaaaaaaa, "
23564 "aaaaaaaaaaaaaaaaaat))");
23565 verifyFormat("Constructor()\n"
23566 " : aaaaaaaaaaaaa(aaaaaa), "
23567 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
23568
23569 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
23570 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
23571 verifyFormat("Constructor()\n"
23572 " : aaaaaa(aaaaaa),\n"
23573 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
23574 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
23575 StyleWithWhitespacePenalty);
23576 verifyFormat("Constructor()\n"
23577 " : aaaaaaaaaaaaa(aaaaaa), "
23578 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
23579 StyleWithWhitespacePenalty);
23580 }
23581
23582 TEST_F(FormatTest, LLVMDefaultStyle) {
23583 FormatStyle Style = getLLVMStyle();
23584 verifyFormat("extern \"C\" {\n"
23585 "int foo();\n"
23586 "}",
23587 Style);
23588 }
23589 TEST_F(FormatTest, GNUDefaultStyle) {
23590 FormatStyle Style = getGNUStyle();
23591 verifyFormat("extern \"C\"\n"
23592 "{\n"
23593 " int foo ();\n"
23594 "}",
23595 Style);
23596 }
23597 TEST_F(FormatTest, MozillaDefaultStyle) {
23598 FormatStyle Style = getMozillaStyle();
23599 verifyFormat("extern \"C\"\n"
23600 "{\n"
23601 " int foo();\n"
23602 "}",
23603 Style);
23604 }
23605 TEST_F(FormatTest, GoogleDefaultStyle) {
23606 FormatStyle Style = getGoogleStyle();
23607 verifyFormat("extern \"C\" {\n"
23608 "int foo();\n"
23609 "}",
23610 Style);
23611 }
23612 TEST_F(FormatTest, ChromiumDefaultStyle) {
23613 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp);
23614 verifyFormat("extern \"C\" {\n"
23615 "int foo();\n"
23616 "}",
23617 Style);
23618 }
23619 TEST_F(FormatTest, MicrosoftDefaultStyle) {
23620 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp);
23621 verifyFormat("extern \"C\"\n"
23622 "{\n"
23623 " int foo();\n"
23624 "}",
23625 Style);
23626 }
23627 TEST_F(FormatTest, WebKitDefaultStyle) {
23628 FormatStyle Style = getWebKitStyle();
23629 verifyFormat("extern \"C\" {\n"
23630 "int foo();\n"
23631 "}",
23632 Style);
23633 }
23634
23635 TEST_F(FormatTest, Concepts) {
23636 EXPECT_EQ(getLLVMStyle().BreakBeforeConceptDeclarations,
23637 FormatStyle::BBCDS_Always);
23638
23639 // The default in LLVM style is REI_OuterScope, but these tests were written
23640 // when the default was REI_Keyword.
23641 FormatStyle Style = getLLVMStyle();
23642 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
23643
23644 verifyFormat("template <typename T>\n"
23645 "concept True = true;");
23646
23647 verifyFormat("template <typename T>\n"
23648 "concept C = ((false || foo()) && C2<T>) ||\n"
23649 " (std::trait<T>::value && Baz) || sizeof(T) >= 6;",
23650 getLLVMStyleWithColumns(60));
23651
23652 verifyFormat("template <typename T>\n"
23653 "concept DelayedCheck = true && requires(T t) { t.bar(); } && "
23654 "sizeof(T) <= 8;");
23655
23656 verifyFormat("template <typename T>\n"
23657 "concept DelayedCheck = true && requires(T t) {\n"
23658 " t.bar();\n"
23659 " t.baz();\n"
23660 " } && sizeof(T) <= 8;",
23661 Style);
23662
23663 verifyFormat("template <typename T>\n"
23664 "concept DelayedCheck = true && requires(T t) { // Comment\n"
23665 " t.bar();\n"
23666 " t.baz();\n"
23667 " } && sizeof(T) <= 8;",
23668 Style);
23669
23670 verifyFormat("template <typename T>\n"
23671 "concept DelayedCheck = false || requires(T t) { t.bar(); } && "
23672 "sizeof(T) <= 8;");
23673
23674 verifyFormat("template <typename T>\n"
23675 "concept DelayedCheck = Unit<T> && !DerivedUnit<T>;");
23676
23677 verifyFormat("template <typename T>\n"
23678 "concept DelayedCheck = Unit<T> && !(DerivedUnit<T>);");
23679
23680 verifyFormat("template <typename T>\n"
23681 "concept DelayedCheck = Unit<T> && !!DerivedUnit<T>;");
23682
23683 verifyFormat("template <typename T>\n"
23684 "concept DelayedCheck = !!false || requires(T t) { t.bar(); } "
23685 "&& sizeof(T) <= 8;");
23686
23687 verifyFormat("template <typename T>\n"
23688 "concept DelayedCheck =\n"
23689 " static_cast<bool>(0) || requires(T t) { t.bar(); } && "
23690 "sizeof(T) <= 8;");
23691
23692 verifyFormat("template <typename T>\n"
23693 "concept DelayedCheck = bool(0) || requires(T t) { t.bar(); } "
23694 "&& sizeof(T) <= 8;");
23695
23696 verifyFormat(
23697 "template <typename T>\n"
23698 "concept DelayedCheck =\n"
23699 " (bool)(0) || requires(T t) { t.bar(); } && sizeof(T) <= 8;");
23700
23701 verifyFormat("template <typename T>\n"
23702 "concept DelayedCheck = (bool)0 || requires(T t) { t.bar(); } "
23703 "&& sizeof(T) <= 8;");
23704
23705 verifyFormat("template <typename T>\n"
23706 "concept Size = sizeof(T) >= 5 && requires(T t) { t.bar(); } && "
23707 "sizeof(T) <= 8;");
23708
23709 verifyFormat("template <typename T>\n"
23710 "concept Size = 2 < 5 && 2 <= 5 && 8 >= 5 && 8 > 5 &&\n"
23711 " requires(T t) {\n"
23712 " t.bar();\n"
23713 " t.baz();\n"
23714 " } && sizeof(T) <= 8 && !(4 < 3);",
23715 getLLVMStyleWithColumns(60));
23716
23717 verifyFormat("template <typename T>\n"
23718 "concept TrueOrNot = IsAlwaysTrue || IsNeverTrue;");
23719
23720 verifyFormat("template <typename T>\n"
23721 "concept C = foo();");
23722
23723 verifyFormat("template <typename T>\n"
23724 "concept C = foo(T());");
23725
23726 verifyFormat("template <typename T>\n"
23727 "concept C = foo(T{});");
23728
23729 verifyFormat("template <typename T>\n"
23730 "concept Size = V<sizeof(T)>::Value > 5;");
23731
23732 verifyFormat("template <typename T>\n"
23733 "concept True = S<T>::Value;");
23734
23735 verifyFormat("template <S T>\n"
23736 "concept True = T.field;");
23737
23738 verifyFormat(
23739 "template <typename T>\n"
23740 "concept C = []() { return true; }() && requires(T t) { t.bar(); } &&\n"
23741 " sizeof(T) <= 8;");
23742
23743 // FIXME: This is misformatted because the fake l paren starts at bool, not at
23744 // the lambda l square.
23745 verifyFormat("template <typename T>\n"
23746 "concept C = [] -> bool { return true; }() && requires(T t) { "
23747 "t.bar(); } &&\n"
23748 " sizeof(T) <= 8;");
23749
23750 verifyFormat(
23751 "template <typename T>\n"
23752 "concept C = decltype([]() { return std::true_type{}; }())::value &&\n"
23753 " requires(T t) { t.bar(); } && sizeof(T) <= 8;");
23754
23755 verifyFormat("template <typename T>\n"
23756 "concept C = decltype([]() { return std::true_type{}; "
23757 "}())::value && requires(T t) { t.bar(); } && sizeof(T) <= 8;",
23758 getLLVMStyleWithColumns(120));
23759
23760 verifyFormat("template <typename T>\n"
23761 "concept C = decltype([]() -> std::true_type { return {}; "
23762 "}())::value &&\n"
23763 " requires(T t) { t.bar(); } && sizeof(T) <= 8;");
23764
23765 verifyFormat("template <typename T>\n"
23766 "concept C = true;\n"
23767 "Foo Bar;");
23768
23769 verifyFormat("template <typename T>\n"
23770 "concept Hashable = requires(T a) {\n"
23771 " { std::hash<T>{}(a) } -> "
23772 "std::convertible_to<std::size_t>;\n"
23773 " };",
23774 Style);
23775
23776 verifyFormat(
23777 "template <typename T>\n"
23778 "concept EqualityComparable = requires(T a, T b) {\n"
23779 " { a == b } -> std::same_as<bool>;\n"
23780 " };",
23781 Style);
23782
23783 verifyFormat(
23784 "template <typename T>\n"
23785 "concept EqualityComparable = requires(T a, T b) {\n"
23786 " { a == b } -> std::same_as<bool>;\n"
23787 " { a != b } -> std::same_as<bool>;\n"
23788 " };",
23789 Style);
23790
23791 verifyFormat("template <typename T>\n"
23792 "concept WeakEqualityComparable = requires(T a, T b) {\n"
23793 " { a == b };\n"
23794 " { a != b };\n"
23795 " };",
23796 Style);
23797
23798 verifyFormat("template <typename T>\n"
23799 "concept HasSizeT = requires { typename T::size_t; };");
23800
23801 verifyFormat("template <typename T>\n"
23802 "concept Semiregular =\n"
23803 " DefaultConstructible<T> && CopyConstructible<T> && "
23804 "CopyAssignable<T> &&\n"
23805 " requires(T a, std::size_t n) {\n"
23806 " requires Same<T *, decltype(&a)>;\n"
23807 " { a.~T() } noexcept;\n"
23808 " requires Same<T *, decltype(new T)>;\n"
23809 " requires Same<T *, decltype(new T[n])>;\n"
23810 " { delete new T; };\n"
23811 " { delete new T[n]; };\n"
23812 " };",
23813 Style);
23814
23815 verifyFormat("template <typename T>\n"
23816 "concept Semiregular =\n"
23817 " requires(T a, std::size_t n) {\n"
23818 " requires Same<T *, decltype(&a)>;\n"
23819 " { a.~T() } noexcept;\n"
23820 " requires Same<T *, decltype(new T)>;\n"
23821 " requires Same<T *, decltype(new T[n])>;\n"
23822 " { delete new T; };\n"
23823 " { delete new T[n]; };\n"
23824 " { new T } -> std::same_as<T *>;\n"
23825 " } && DefaultConstructible<T> && CopyConstructible<T> && "
23826 "CopyAssignable<T>;",
23827 Style);
23828
23829 verifyFormat(
23830 "template <typename T>\n"
23831 "concept Semiregular =\n"
23832 " DefaultConstructible<T> && requires(T a, std::size_t n) {\n"
23833 " requires Same<T *, decltype(&a)>;\n"
23834 " { a.~T() } noexcept;\n"
23835 " requires Same<T *, decltype(new T)>;\n"
23836 " requires Same<T *, decltype(new "
23837 "T[n])>;\n"
23838 " { delete new T; };\n"
23839 " { delete new T[n]; };\n"
23840 " } && CopyConstructible<T> && "
23841 "CopyAssignable<T>;",
23842 Style);
23843
23844 verifyFormat("template <typename T>\n"
23845 "concept Two = requires(T t) {\n"
23846 " { t.foo() } -> std::same_as<Bar>;\n"
23847 " } && requires(T &&t) {\n"
23848 " { t.foo() } -> std::same_as<Bar &&>;\n"
23849 " };",
23850 Style);
23851
23852 verifyFormat(
23853 "template <typename T>\n"
23854 "concept C = requires(T x) {\n"
23855 " { *x } -> std::convertible_to<typename T::inner>;\n"
23856 " { x + 1 } noexcept -> std::same_as<int>;\n"
23857 " { x * 1 } -> std::convertible_to<T>;\n"
23858 " };",
23859 Style);
23860
23861 verifyFormat("template <typename T>\n"
23862 "concept C = requires(T x) {\n"
23863 " {\n"
23864 " long_long_long_function_call(1, 2, 3, 4, 5)\n"
23865 " } -> long_long_concept_name<T>;\n"
23866 " {\n"
23867 " long_long_long_function_call(1, 2, 3, 4, 5)\n"
23868 " } noexcept -> long_long_concept_name<T>;\n"
23869 " };",
23870 Style);
23871
23872 verifyFormat(
23873 "template <typename T, typename U = T>\n"
23874 "concept Swappable = requires(T &&t, U &&u) {\n"
23875 " swap(std::forward<T>(t), std::forward<U>(u));\n"
23876 " swap(std::forward<U>(u), std::forward<T>(t));\n"
23877 " };",
23878 Style);
23879
23880 verifyFormat("template <typename T, typename U>\n"
23881 "concept Common = requires(T &&t, U &&u) {\n"
23882 " typename CommonType<T, U>;\n"
23883 " { CommonType<T, U>(std::forward<T>(t)) };\n"
23884 " };",
23885 Style);
23886
23887 verifyFormat("template <typename T, typename U>\n"
23888 "concept Common = requires(T &&t, U &&u) {\n"
23889 " typename CommonType<T, U>;\n"
23890 " { CommonType<T, U>{std::forward<T>(t)} };\n"
23891 " };",
23892 Style);
23893
23894 verifyFormat(
23895 "template <typename T>\n"
23896 "concept C = requires(T t) {\n"
23897 " requires Bar<T> && Foo<T>;\n"
23898 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
23899 " };",
23900 Style);
23901
23902 verifyFormat("template <typename T>\n"
23903 "concept HasFoo = requires(T t) {\n"
23904 " { t.foo() };\n"
23905 " t.foo();\n"
23906 " };\n"
23907 "template <typename T>\n"
23908 "concept HasBar = requires(T t) {\n"
23909 " { t.bar() };\n"
23910 " t.bar();\n"
23911 " };",
23912 Style);
23913
23914 verifyFormat("template <typename T>\n"
23915 "concept Large = sizeof(T) > 10;");
23916
23917 verifyFormat("template <typename T, typename U>\n"
23918 "concept FooableWith = requires(T t, U u) {\n"
23919 " typename T::foo_type;\n"
23920 " { t.foo(u) } -> typename T::foo_type;\n"
23921 " t++;\n"
23922 " };\n"
23923 "void doFoo(FooableWith<int> auto t) { t.foo(3); }",
23924 Style);
23925
23926 verifyFormat("template <typename T>\n"
23927 "concept Context = is_specialization_of_v<context, T>;");
23928
23929 verifyFormat("template <typename T>\n"
23930 "concept Node = std::is_object_v<T>;");
23931
23932 verifyFormat("template <class T>\n"
23933 "concept integral = __is_integral(T);");
23934
23935 verifyFormat("template <class T>\n"
23936 "concept is2D = __array_extent(T, 1) == 2;");
23937
23938 verifyFormat("template <class T>\n"
23939 "concept isRhs = __is_rvalue_expr(std::declval<T>() + 2)");
23940
23941 verifyFormat("template <class T, class T2>\n"
23942 "concept Same = __is_same_as<T, T2>;");
23943
23944 verifyFormat(
23945 "template <class _InIt, class _OutIt>\n"
23946 "concept _Can_reread_dest =\n"
23947 " std::forward_iterator<_OutIt> &&\n"
23948 " std::same_as<std::iter_value_t<_InIt>, std::iter_value_t<_OutIt>>;");
23949
23950 Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Allowed;
23951
23952 verifyFormat(
23953 "template <typename T>\n"
23954 "concept C = requires(T t) {\n"
23955 " requires Bar<T> && Foo<T>;\n"
23956 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
23957 " };",
23958 Style);
23959
23960 verifyFormat("template <typename T>\n"
23961 "concept HasFoo = requires(T t) {\n"
23962 " { t.foo() };\n"
23963 " t.foo();\n"
23964 " };\n"
23965 "template <typename T>\n"
23966 "concept HasBar = requires(T t) {\n"
23967 " { t.bar() };\n"
23968 " t.bar();\n"
23969 " };",
23970 Style);
23971
23972 verifyFormat("template <typename T> concept True = true;", Style);
23973
23974 verifyFormat("template <typename T>\n"
23975 "concept C = decltype([]() -> std::true_type { return {}; "
23976 "}())::value &&\n"
23977 " requires(T t) { t.bar(); } && sizeof(T) <= 8;",
23978 Style);
23979
23980 verifyFormat("template <typename T>\n"
23981 "concept Semiregular =\n"
23982 " DefaultConstructible<T> && CopyConstructible<T> && "
23983 "CopyAssignable<T> &&\n"
23984 " requires(T a, std::size_t n) {\n"
23985 " requires Same<T *, decltype(&a)>;\n"
23986 " { a.~T() } noexcept;\n"
23987 " requires Same<T *, decltype(new T)>;\n"
23988 " requires Same<T *, decltype(new T[n])>;\n"
23989 " { delete new T; };\n"
23990 " { delete new T[n]; };\n"
23991 " };",
23992 Style);
23993
23994 Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Never;
23995
23996 verifyFormat("template <typename T> concept C =\n"
23997 " requires(T t) {\n"
23998 " requires Bar<T> && Foo<T>;\n"
23999 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
24000 " };",
24001 Style);
24002
24003 verifyFormat("template <typename T> concept HasFoo = requires(T t) {\n"
24004 " { t.foo() };\n"
24005 " t.foo();\n"
24006 " };\n"
24007 "template <typename T> concept HasBar = requires(T t) {\n"
24008 " { t.bar() };\n"
24009 " t.bar();\n"
24010 " };",
24011 Style);
24012
24013 verifyFormat("template <typename T> concept True = true;", Style);
24014
24015 verifyFormat(
24016 "template <typename T> concept C =\n"
24017 " decltype([]() -> std::true_type { return {}; }())::value &&\n"
24018 " requires(T t) { t.bar(); } && sizeof(T) <= 8;",
24019 Style);
24020
24021 verifyFormat("template <typename T> concept Semiregular =\n"
24022 " DefaultConstructible<T> && CopyConstructible<T> && "
24023 "CopyAssignable<T> &&\n"
24024 " requires(T a, std::size_t n) {\n"
24025 " requires Same<T *, decltype(&a)>;\n"
24026 " { a.~T() } noexcept;\n"
24027 " requires Same<T *, decltype(new T)>;\n"
24028 " requires Same<T *, decltype(new T[n])>;\n"
24029 " { delete new T; };\n"
24030 " { delete new T[n]; };\n"
24031 " };",
24032 Style);
24033
24034 // The following tests are invalid C++, we just want to make sure we don't
24035 // assert.
24036 verifyNoCrash("template <typename T>\n"
24037 "concept C = requires C2<T>;");
24038
24039 verifyNoCrash("template <typename T>\n"
24040 "concept C = 5 + 4;");
24041
24042 verifyNoCrash("template <typename T>\n"
24043 "concept C = class X;");
24044
24045 verifyNoCrash("template <typename T>\n"
24046 "concept C = [] && true;");
24047
24048 verifyNoCrash("template <typename T>\n"
24049 "concept C = [] && requires(T t) { typename T::size_type; };");
24050 }
24051
24052 TEST_F(FormatTest, RequiresClausesPositions) {
24053 auto Style = getLLVMStyle();
24054 EXPECT_EQ(Style.RequiresClausePosition, FormatStyle::RCPS_OwnLine);
24055 EXPECT_EQ(Style.IndentRequiresClause, true);
24056
24057 // The default in LLVM style is REI_OuterScope, but these tests were written
24058 // when the default was REI_Keyword.
24059 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
24060
24061 verifyFormat("template <typename T>\n"
24062 " requires(Foo<T> && std::trait<T>)\n"
24063 "struct Bar;",
24064 Style);
24065
24066 verifyFormat("template <typename T>\n"
24067 " requires(Foo<T> && std::trait<T>)\n"
24068 "class Bar {\n"
24069 "public:\n"
24070 " Bar(T t);\n"
24071 " bool baz();\n"
24072 "};",
24073 Style);
24074
24075 verifyFormat(
24076 "template <typename T>\n"
24077 " requires requires(T &&t) {\n"
24078 " typename T::I;\n"
24079 " requires(F<typename T::I> && std::trait<typename T::I>);\n"
24080 " }\n"
24081 "Bar(T) -> Bar<typename T::I>;",
24082 Style);
24083
24084 verifyFormat("template <typename T>\n"
24085 " requires(Foo<T> && std::trait<T>)\n"
24086 "constexpr T MyGlobal;",
24087 Style);
24088
24089 verifyFormat("template <typename T>\n"
24090 " requires Foo<T> && requires(T t) {\n"
24091 " { t.baz() } -> std::same_as<bool>;\n"
24092 " requires std::same_as<T::Factor, int>;\n"
24093 " }\n"
24094 "inline int bar(T t) {\n"
24095 " return t.baz() ? T::Factor : 5;\n"
24096 "}",
24097 Style);
24098
24099 verifyFormat("template <typename T>\n"
24100 "inline int bar(T t)\n"
24101 " requires Foo<T> && requires(T t) {\n"
24102 " { t.baz() } -> std::same_as<bool>;\n"
24103 " requires std::same_as<T::Factor, int>;\n"
24104 " }\n"
24105 "{\n"
24106 " return t.baz() ? T::Factor : 5;\n"
24107 "}",
24108 Style);
24109
24110 verifyFormat("template <typename T>\n"
24111 " requires F<T>\n"
24112 "int bar(T t) {\n"
24113 " return 5;\n"
24114 "}",
24115 Style);
24116
24117 verifyFormat("template <typename T>\n"
24118 "int bar(T t)\n"
24119 " requires F<T>\n"
24120 "{\n"
24121 " return 5;\n"
24122 "}",
24123 Style);
24124
24125 verifyFormat("template <typename T>\n"
24126 "int bar(T t)\n"
24127 " requires F<T>;",
24128 Style);
24129
24130 Style.IndentRequiresClause = false;
24131 verifyFormat("template <typename T>\n"
24132 "requires F<T>\n"
24133 "int bar(T t) {\n"
24134 " return 5;\n"
24135 "}",
24136 Style);
24137
24138 verifyFormat("template <typename T>\n"
24139 "int bar(T t)\n"
24140 "requires F<T>\n"
24141 "{\n"
24142 " return 5;\n"
24143 "}",
24144 Style);
24145
24146 Style.RequiresClausePosition = FormatStyle::RCPS_SingleLine;
24147 verifyFormat("template <typename T> requires Foo<T> struct Bar {};\n"
24148 "template <typename T> requires Foo<T> void bar() {}\n"
24149 "template <typename T> void bar() requires Foo<T> {}\n"
24150 "template <typename T> void bar() requires Foo<T>;\n"
24151 "template <typename T> requires Foo<T> Bar(T) -> Bar<T>;",
24152 Style);
24153
24154 auto ColumnStyle = Style;
24155 ColumnStyle.ColumnLimit = 40;
24156 verifyFormat("template <typename AAAAAAA>\n"
24157 "requires Foo<T> struct Bar {};\n"
24158 "template <typename AAAAAAA>\n"
24159 "requires Foo<T> void bar() {}\n"
24160 "template <typename AAAAAAA>\n"
24161 "void bar() requires Foo<T> {}\n"
24162 "template <typename AAAAAAA>\n"
24163 "requires Foo<T> Baz(T) -> Baz<T>;",
24164 ColumnStyle);
24165
24166 verifyFormat("template <typename T>\n"
24167 "requires Foo<AAAAAAA> struct Bar {};\n"
24168 "template <typename T>\n"
24169 "requires Foo<AAAAAAA> void bar() {}\n"
24170 "template <typename T>\n"
24171 "void bar() requires Foo<AAAAAAA> {}\n"
24172 "template <typename T>\n"
24173 "requires Foo<AAAAAAA> Bar(T) -> Bar<T>;",
24174 ColumnStyle);
24175
24176 verifyFormat("template <typename AAAAAAA>\n"
24177 "requires Foo<AAAAAAAAAAAAAAAA>\n"
24178 "struct Bar {};\n"
24179 "template <typename AAAAAAA>\n"
24180 "requires Foo<AAAAAAAAAAAAAAAA>\n"
24181 "void bar() {}\n"
24182 "template <typename AAAAAAA>\n"
24183 "void bar()\n"
24184 " requires Foo<AAAAAAAAAAAAAAAA> {}\n"
24185 "template <typename AAAAAAA>\n"
24186 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
24187 "template <typename AAAAAAA>\n"
24188 "requires Foo<AAAAAAAAAAAAAAAA>\n"
24189 "Bar(T) -> Bar<T>;",
24190 ColumnStyle);
24191
24192 Style.RequiresClausePosition = FormatStyle::RCPS_WithFollowing;
24193 ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithFollowing;
24194
24195 verifyFormat("template <typename T>\n"
24196 "requires Foo<T> struct Bar {};\n"
24197 "template <typename T>\n"
24198 "requires Foo<T> void bar() {}\n"
24199 "template <typename T>\n"
24200 "void bar()\n"
24201 "requires Foo<T> {}\n"
24202 "template <typename T>\n"
24203 "void bar()\n"
24204 "requires Foo<T>;\n"
24205 "template <typename T>\n"
24206 "requires Foo<T> Bar(T) -> Bar<T>;",
24207 Style);
24208
24209 verifyFormat("template <typename AAAAAAA>\n"
24210 "requires Foo<AAAAAAAAAAAAAAAA>\n"
24211 "struct Bar {};\n"
24212 "template <typename AAAAAAA>\n"
24213 "requires Foo<AAAAAAAAAAAAAAAA>\n"
24214 "void bar() {}\n"
24215 "template <typename AAAAAAA>\n"
24216 "void bar()\n"
24217 "requires Foo<AAAAAAAAAAAAAAAA> {}\n"
24218 "template <typename AAAAAAA>\n"
24219 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
24220 "template <typename AAAAAAA>\n"
24221 "requires Foo<AAAAAAAAAAAAAAAA>\n"
24222 "Bar(T) -> Bar<T>;",
24223 ColumnStyle);
24224
24225 Style.IndentRequiresClause = true;
24226 ColumnStyle.IndentRequiresClause = true;
24227
24228 verifyFormat("template <typename T>\n"
24229 " requires Foo<T> struct Bar {};\n"
24230 "template <typename T>\n"
24231 " requires Foo<T> void bar() {}\n"
24232 "template <typename T>\n"
24233 "void bar()\n"
24234 " requires Foo<T> {}\n"
24235 "template <typename T>\n"
24236 " requires Foo<T> Bar(T) -> Bar<T>;",
24237 Style);
24238
24239 verifyFormat("template <typename AAAAAAA>\n"
24240 " requires Foo<AAAAAAAAAAAAAAAA>\n"
24241 "struct Bar {};\n"
24242 "template <typename AAAAAAA>\n"
24243 " requires Foo<AAAAAAAAAAAAAAAA>\n"
24244 "void bar() {}\n"
24245 "template <typename AAAAAAA>\n"
24246 "void bar()\n"
24247 " requires Foo<AAAAAAAAAAAAAAAA> {}\n"
24248 "template <typename AAAAAAA>\n"
24249 " requires Foo<AAAAAA> Bar(T) -> Bar<T>;\n"
24250 "template <typename AAAAAAA>\n"
24251 " requires Foo<AAAAAAAAAAAAAAAA>\n"
24252 "Bar(T) -> Bar<T>;",
24253 ColumnStyle);
24254
24255 Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
24256 ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
24257
24258 verifyFormat("template <typename T> requires Foo<T>\n"
24259 "struct Bar {};\n"
24260 "template <typename T> requires Foo<T>\n"
24261 "void bar() {}\n"
24262 "template <typename T>\n"
24263 "void bar() requires Foo<T>\n"
24264 "{}\n"
24265 "template <typename T> void bar() requires Foo<T>;\n"
24266 "template <typename T> requires Foo<T>\n"
24267 "Bar(T) -> Bar<T>;",
24268 Style);
24269
24270 verifyFormat("template <typename AAAAAAA>\n"
24271 "requires Foo<AAAAAAAAAAAAAAAA>\n"
24272 "struct Bar {};\n"
24273 "template <typename AAAAAAA>\n"
24274 "requires Foo<AAAAAAAAAAAAAAAA>\n"
24275 "void bar() {}\n"
24276 "template <typename AAAAAAA>\n"
24277 "void bar()\n"
24278 " requires Foo<AAAAAAAAAAAAAAAA>\n"
24279 "{}\n"
24280 "template <typename AAAAAAA>\n"
24281 "requires Foo<AAAAAAAA>\n"
24282 "Bar(T) -> Bar<T>;\n"
24283 "template <typename AAAAAAA>\n"
24284 "requires Foo<AAAAAAAAAAAAAAAA>\n"
24285 "Bar(T) -> Bar<T>;",
24286 ColumnStyle);
24287 }
24288
24289 TEST_F(FormatTest, RequiresClauses) {
24290 verifyFormat("struct [[nodiscard]] zero_t {\n"
24291 " template <class T>\n"
24292 " requires requires { number_zero_v<T>; }\n"
24293 " [[nodiscard]] constexpr operator T() const {\n"
24294 " return number_zero_v<T>;\n"
24295 " }\n"
24296 "};");
24297
24298 verifyFormat("template <class T>\n"
24299 " requires(std::same_as<int, T>)\n"
24300 "decltype(auto) fun() {}");
24301
24302 auto Style = getLLVMStyle();
24303
24304 verifyFormat(
24305 "template <typename T>\n"
24306 " requires is_default_constructible_v<hash<T>> and\n"
24307 " is_copy_constructible_v<hash<T>> and\n"
24308 " is_move_constructible_v<hash<T>> and\n"
24309 " is_copy_assignable_v<hash<T>> and "
24310 "is_move_assignable_v<hash<T>> and\n"
24311 " is_destructible_v<hash<T>> and is_swappable_v<hash<T>> and\n"
24312 " is_callable_v<hash<T>(T)> and\n"
24313 " is_same_v<size_t, decltype(hash<T>(declval<T>()))> and\n"
24314 " is_same_v<size_t, decltype(hash<T>(declval<T &>()))> and\n"
24315 " is_same_v<size_t, decltype(hash<T>(declval<const T &>()))>\n"
24316 "struct S {};",
24317 Style);
24318
24319 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
24320 verifyFormat(
24321 "template <typename T>\n"
24322 " requires is_default_constructible_v<hash<T>>\n"
24323 " and is_copy_constructible_v<hash<T>>\n"
24324 " and is_move_constructible_v<hash<T>>\n"
24325 " and is_copy_assignable_v<hash<T>> and "
24326 "is_move_assignable_v<hash<T>>\n"
24327 " and is_destructible_v<hash<T>> and is_swappable_v<hash<T>>\n"
24328 " and is_callable_v<hash<T>(T)>\n"
24329 " and is_same_v<size_t, decltype(hash<T>(declval<T>()))>\n"
24330 " and is_same_v<size_t, decltype(hash<T>(declval<T &>()))>\n"
24331 " and is_same_v<size_t, decltype(hash<T>(declval<const T "
24332 "&>()))>\n"
24333 "struct S {};",
24334 Style);
24335
24336 Style = getLLVMStyle();
24337 Style.ConstructorInitializerIndentWidth = 4;
24338 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
24339 Style.PackConstructorInitializers = FormatStyle::PCIS_Never;
24340 verifyFormat("constexpr Foo(Foo const &other)\n"
24341 " requires std::is_copy_constructible<T>\n"
24342 " : value{other.value} {\n"
24343 " do_magic();\n"
24344 " do_more_magic();\n"
24345 "}",
24346 Style);
24347
24348 // Not a clause, but we once hit an assert.
24349 verifyFormat("#if 0\n"
24350 "#else\n"
24351 "foo();\n"
24352 "#endif\n"
24353 "bar(requires);");
24354 }
24355
24356 TEST_F(FormatTest, RequiresExpressionIndentation) {
24357 auto Style = getLLVMStyle();
24358 EXPECT_EQ(Style.RequiresExpressionIndentation, FormatStyle::REI_OuterScope);
24359
24360 verifyFormat("template <typename T>\n"
24361 "concept C = requires(T t) {\n"
24362 " typename T::value;\n"
24363 " requires requires(typename T::value v) {\n"
24364 " { t == v } -> std::same_as<bool>;\n"
24365 " };\n"
24366 "};",
24367 Style);
24368
24369 verifyFormat("template <typename T>\n"
24370 "void bar(T)\n"
24371 " requires Foo<T> && requires(T t) {\n"
24372 " { t.foo() } -> std::same_as<int>;\n"
24373 " } && requires(T t) {\n"
24374 " { t.bar() } -> std::same_as<bool>;\n"
24375 " --t;\n"
24376 " };",
24377 Style);
24378
24379 verifyFormat("template <typename T>\n"
24380 " requires Foo<T> &&\n"
24381 " requires(T t) {\n"
24382 " { t.foo() } -> std::same_as<int>;\n"
24383 " } && requires(T t) {\n"
24384 " { t.bar() } -> std::same_as<bool>;\n"
24385 " --t;\n"
24386 " }\n"
24387 "void bar(T);",
24388 Style);
24389
24390 verifyFormat("template <typename T> void f() {\n"
24391 " if constexpr (requires(T t) {\n"
24392 " { t.bar() } -> std::same_as<bool>;\n"
24393 " }) {\n"
24394 " }\n"
24395 "}",
24396 Style);
24397
24398 verifyFormat("template <typename T> void f() {\n"
24399 " if constexpr (condition && requires(T t) {\n"
24400 " { t.bar() } -> std::same_as<bool>;\n"
24401 " }) {\n"
24402 " }\n"
24403 "}",
24404 Style);
24405
24406 verifyFormat("template <typename T> struct C {\n"
24407 " void f()\n"
24408 " requires requires(T t) {\n"
24409 " { t.bar() } -> std::same_as<bool>;\n"
24410 " };\n"
24411 "};",
24412 Style);
24413
24414 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
24415
24416 verifyFormat("template <typename T>\n"
24417 "concept C = requires(T t) {\n"
24418 " typename T::value;\n"
24419 " requires requires(typename T::value v) {\n"
24420 " { t == v } -> std::same_as<bool>;\n"
24421 " };\n"
24422 " };",
24423 Style);
24424
24425 verifyFormat(
24426 "template <typename T>\n"
24427 "void bar(T)\n"
24428 " requires Foo<T> && requires(T t) {\n"
24429 " { t.foo() } -> std::same_as<int>;\n"
24430 " } && requires(T t) {\n"
24431 " { t.bar() } -> std::same_as<bool>;\n"
24432 " --t;\n"
24433 " };",
24434 Style);
24435
24436 verifyFormat("template <typename T>\n"
24437 " requires Foo<T> &&\n"
24438 " requires(T t) {\n"
24439 " { t.foo() } -> std::same_as<int>;\n"
24440 " } && requires(T t) {\n"
24441 " { t.bar() } -> std::same_as<bool>;\n"
24442 " --t;\n"
24443 " }\n"
24444 "void bar(T);",
24445 Style);
24446
24447 verifyFormat("template <typename T> void f() {\n"
24448 " if constexpr (requires(T t) {\n"
24449 " { t.bar() } -> std::same_as<bool>;\n"
24450 " }) {\n"
24451 " }\n"
24452 "}",
24453 Style);
24454
24455 verifyFormat(
24456 "template <typename T> void f() {\n"
24457 " if constexpr (condition && requires(T t) {\n"
24458 " { t.bar() } -> std::same_as<bool>;\n"
24459 " }) {\n"
24460 " }\n"
24461 "}",
24462 Style);
24463
24464 verifyFormat("template <typename T> struct C {\n"
24465 " void f()\n"
24466 " requires requires(T t) {\n"
24467 " { t.bar() } -> std::same_as<bool>;\n"
24468 " };\n"
24469 "};",
24470 Style);
24471 }
24472
24473 TEST_F(FormatTest, StatementAttributeLikeMacros) {
24474 FormatStyle Style = getLLVMStyle();
24475 StringRef Source = "void Foo::slot() {\n"
24476 " unsigned char MyChar = 'x';\n"
24477 " emit signal(MyChar);\n"
24478 " Q_EMIT signal(MyChar);\n"
24479 "}";
24480
24481 EXPECT_EQ(Source, format(Source, Style));
24482
24483 Style.AlignConsecutiveDeclarations.Enabled = true;
24484 EXPECT_EQ("void Foo::slot() {\n"
24485 " unsigned char MyChar = 'x';\n"
24486 " emit signal(MyChar);\n"
24487 " Q_EMIT signal(MyChar);\n"
24488 "}",
24489 format(Source, Style));
24490
24491 Style.StatementAttributeLikeMacros.push_back("emit");
24492 EXPECT_EQ(Source, format(Source, Style));
24493
24494 Style.StatementAttributeLikeMacros = {};
24495 EXPECT_EQ("void Foo::slot() {\n"
24496 " unsigned char MyChar = 'x';\n"
24497 " emit signal(MyChar);\n"
24498 " Q_EMIT signal(MyChar);\n"
24499 "}",
24500 format(Source, Style));
24501 }
24502
24503 TEST_F(FormatTest, IndentAccessModifiers) {
24504 FormatStyle Style = getLLVMStyle();
24505 Style.IndentAccessModifiers = true;
24506 // Members are *two* levels below the record;
24507 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
24508 verifyFormat("class C {\n"
24509 " int i;\n"
24510 "};\n",
24511 Style);
24512 verifyFormat("union C {\n"
24513 " int i;\n"
24514 " unsigned u;\n"
24515 "};\n",
24516 Style);
24517 // Access modifiers should be indented one level below the record.
24518 verifyFormat("class C {\n"
24519 " public:\n"
24520 " int i;\n"
24521 "};\n",
24522 Style);
24523 verifyFormat("class C {\n"
24524 " public /* comment */:\n"
24525 " int i;\n"
24526 "};",
24527 Style);
24528 verifyFormat("struct S {\n"
24529 " private:\n"
24530 " class C {\n"
24531 " int j;\n"
24532 "\n"
24533 " public:\n"
24534 " C();\n"
24535 " };\n"
24536 "\n"
24537 " public:\n"
24538 " int i;\n"
24539 "};\n",
24540 Style);
24541 // Enumerations are not records and should be unaffected.
24542 Style.AllowShortEnumsOnASingleLine = false;
24543 verifyFormat("enum class E {\n"
24544 " A,\n"
24545 " B\n"
24546 "};\n",
24547 Style);
24548 // Test with a different indentation width;
24549 // also proves that the result is Style.AccessModifierOffset agnostic.
24550 Style.IndentWidth = 3;
24551 verifyFormat("class C {\n"
24552 " public:\n"
24553 " int i;\n"
24554 "};\n",
24555 Style);
24556 verifyFormat("class C {\n"
24557 " public /**/:\n"
24558 " int i;\n"
24559 "};",
24560 Style);
24561 }
24562
24563 TEST_F(FormatTest, LimitlessStringsAndComments) {
24564 auto Style = getLLVMStyleWithColumns(0);
24565 constexpr StringRef Code =
24566 "/**\n"
24567 " * This is a multiline comment with quite some long lines, at least for "
24568 "the LLVM Style.\n"
24569 " * We will redo this with strings and line comments. Just to check if "
24570 "everything is working.\n"
24571 " */\n"
24572 "bool foo() {\n"
24573 " /* Single line multi line comment. */\n"
24574 " const std::string String = \"This is a multiline string with quite "
24575 "some long lines, at least for the LLVM Style.\"\n"
24576 " \"We already did it with multi line "
24577 "comments, and we will do it with line comments. Just to check if "
24578 "everything is working.\";\n"
24579 " // This is a line comment (block) with quite some long lines, at "
24580 "least for the LLVM Style.\n"
24581 " // We already did this with multi line comments and strings. Just to "
24582 "check if everything is working.\n"
24583 " const std::string SmallString = \"Hello World\";\n"
24584 " // Small line comment\n"
24585 " return String.size() > SmallString.size();\n"
24586 "}";
24587 EXPECT_EQ(Code, format(Code, Style));
24588 }
24589
24590 TEST_F(FormatTest, FormatDecayCopy) {
24591 // error cases from unit tests
24592 verifyFormat("foo(auto())");
24593 verifyFormat("foo(auto{})");
24594 verifyFormat("foo(auto({}))");
24595 verifyFormat("foo(auto{{}})");
24596
24597 verifyFormat("foo(auto(1))");
24598 verifyFormat("foo(auto{1})");
24599 verifyFormat("foo(new auto(1))");
24600 verifyFormat("foo(new auto{1})");
24601 verifyFormat("decltype(auto(1)) x;");
24602 verifyFormat("decltype(auto{1}) x;");
24603 verifyFormat("auto(x);");
24604 verifyFormat("auto{x};");
24605 verifyFormat("new auto{x};");
24606 verifyFormat("auto{x} = y;");
24607 verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly
24608 // the user's own fault
24609 verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is
24610 // clearly the user's own fault
24611 verifyFormat("auto (*p)() = f;");
24612 }
24613
24614 TEST_F(FormatTest, Cpp20ModulesSupport) {
24615 FormatStyle Style = getLLVMStyle();
24616 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
24617 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
24618
24619 verifyFormat("export import foo;", Style);
24620 verifyFormat("export import foo:bar;", Style);
24621 verifyFormat("export import foo.bar;", Style);
24622 verifyFormat("export import foo.bar:baz;", Style);
24623 verifyFormat("export import :bar;", Style);
24624 verifyFormat("export module foo:bar;", Style);
24625 verifyFormat("export module foo;", Style);
24626 verifyFormat("export module foo.bar;", Style);
24627 verifyFormat("export module foo.bar:baz;", Style);
24628 verifyFormat("export import <string_view>;", Style);
24629 verifyFormat("export import <Foo/Bar>;", Style);
24630
24631 verifyFormat("export type_name var;", Style);
24632 verifyFormat("template <class T> export using A = B<T>;", Style);
24633 verifyFormat("export using A = B;", Style);
24634 verifyFormat("export int func() {\n"
24635 " foo();\n"
24636 "}",
24637 Style);
24638 verifyFormat("export struct {\n"
24639 " int foo;\n"
24640 "};",
24641 Style);
24642 verifyFormat("export {\n"
24643 " int foo;\n"
24644 "};",
24645 Style);
24646 verifyFormat("export export char const *hello() { return \"hello\"; }");
24647
24648 verifyFormat("import bar;", Style);
24649 verifyFormat("import foo.bar;", Style);
24650 verifyFormat("import foo:bar;", Style);
24651 verifyFormat("import :bar;", Style);
24652 verifyFormat("import /* module partition */ :bar;", Style);
24653 verifyFormat("import <ctime>;", Style);
24654 verifyFormat("import \"header\";", Style);
24655
24656 verifyFormat("module foo;", Style);
24657 verifyFormat("module foo:bar;", Style);
24658 verifyFormat("module foo.bar;", Style);
24659 verifyFormat("module;", Style);
24660
24661 verifyFormat("export namespace hi {\n"
24662 "const char *sayhi();\n"
24663 "}",
24664 Style);
24665
24666 verifyFormat("module :private;", Style);
24667 verifyFormat("import <foo/bar.h>;", Style);
24668 verifyFormat("import foo...bar;", Style);
24669 verifyFormat("import ..........;", Style);
24670 verifyFormat("module foo:private;", Style);
24671 verifyFormat("import a", Style);
24672 verifyFormat("module a", Style);
24673 verifyFormat("export import a", Style);
24674 verifyFormat("export module a", Style);
24675
24676 verifyFormat("import", Style);
24677 verifyFormat("module", Style);
24678 verifyFormat("export", Style);
24679
24680 verifyFormat("import /* not keyword */ = val ? 2 : 1;");
24681 }
24682
24683 TEST_F(FormatTest, CoroutineForCoawait) {
24684 FormatStyle Style = getLLVMStyle();
24685 verifyFormat("for co_await (auto x : range())\n ;");
24686 verifyFormat("for (auto i : arr) {\n"
24687 "}",
24688 Style);
24689 verifyFormat("for co_await (auto i : arr) {\n"
24690 "}",
24691 Style);
24692 verifyFormat("for co_await (auto i : foo(T{})) {\n"
24693 "}",
24694 Style);
24695 }
24696
24697 TEST_F(FormatTest, CoroutineCoAwait) {
24698 verifyFormat("int x = co_await foo();");
24699 verifyFormat("int x = (co_await foo());");
24700 verifyFormat("co_await (42);");
24701 verifyFormat("void operator co_await(int);");
24702 verifyFormat("void operator co_await(a);");
24703 verifyFormat("co_await a;");
24704 verifyFormat("co_await missing_await_resume{};");
24705 verifyFormat("co_await a; // comment");
24706 verifyFormat("void test0() { co_await a; }");
24707 verifyFormat("co_await co_await co_await foo();");
24708 verifyFormat("co_await foo().bar();");
24709 verifyFormat("co_await [this]() -> Task { co_return x; }");
24710 verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await "
24711 "foo(); }(x, y);");
24712
24713 FormatStyle Style = getLLVMStyleWithColumns(40);
24714 verifyFormat("co_await [this](int a, int b) -> Task {\n"
24715 " co_return co_await foo();\n"
24716 "}(x, y);",
24717 Style);
24718 verifyFormat("co_await;");
24719 }
24720
24721 TEST_F(FormatTest, CoroutineCoYield) {
24722 verifyFormat("int x = co_yield foo();");
24723 verifyFormat("int x = (co_yield foo());");
24724 verifyFormat("co_yield (42);");
24725 verifyFormat("co_yield {42};");
24726 verifyFormat("co_yield 42;");
24727 verifyFormat("co_yield n++;");
24728 verifyFormat("co_yield ++n;");
24729 verifyFormat("co_yield;");
24730 }
24731
24732 TEST_F(FormatTest, CoroutineCoReturn) {
24733 verifyFormat("co_return (42);");
24734 verifyFormat("co_return;");
24735 verifyFormat("co_return {};");
24736 verifyFormat("co_return x;");
24737 verifyFormat("co_return co_await foo();");
24738 verifyFormat("co_return co_yield foo();");
24739 }
24740
24741 TEST_F(FormatTest, EmptyShortBlock) {
24742 auto Style = getLLVMStyle();
24743 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
24744
24745 verifyFormat("try {\n"
24746 " doA();\n"
24747 "} catch (Exception &e) {\n"
24748 " e.printStackTrace();\n"
24749 "}\n",
24750 Style);
24751
24752 verifyFormat("try {\n"
24753 " doA();\n"
24754 "} catch (Exception &e) {}\n",
24755 Style);
24756 }
24757
24758 TEST_F(FormatTest, ShortTemplatedArgumentLists) {
24759 auto Style = getLLVMStyle();
24760
24761 verifyFormat("template <> struct S : Template<int (*)[]> {};\n", Style);
24762 verifyFormat("template <> struct S : Template<int (*)[10]> {};\n", Style);
24763 verifyFormat("struct Y : X<[] { return 0; }> {};", Style);
24764 verifyFormat("struct Y<[] { return 0; }> {};", Style);
24765
24766 verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style);
24767 verifyFormat("template <int N> struct Foo<char[N]> {};", Style);
24768 }
24769
24770 TEST_F(FormatTest, MultilineLambdaInConditional) {
24771 auto Style = getLLVMStyleWithColumns(70);
24772 verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? []() {\n"
24773 " ;\n"
24774 " return 5;\n"
24775 "}()\n"
24776 " : 2;",
24777 Style);
24778 verifyFormat(
24779 "auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? 2 : []() {\n"
24780 " ;\n"
24781 " return 5;\n"
24782 "}();",
24783 Style);
24784
24785 Style = getLLVMStyleWithColumns(60);
24786 verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak\n"
24787 " ? []() {\n"
24788 " ;\n"
24789 " return 5;\n"
24790 " }()\n"
24791 " : 2;",
24792 Style);
24793 verifyFormat("auto aLengthyIdentifier =\n"
24794 " oneExpressionSoThatWeBreak ? 2 : []() {\n"
24795 " ;\n"
24796 " return 5;\n"
24797 " }();",
24798 Style);
24799
24800 Style = getLLVMStyleWithColumns(40);
24801 verifyFormat("auto aLengthyIdentifier =\n"
24802 " oneExpressionSoThatWeBreak ? []() {\n"
24803 " ;\n"
24804 " return 5;\n"
24805 " }()\n"
24806 " : 2;",
24807 Style);
24808 verifyFormat("auto aLengthyIdentifier =\n"
24809 " oneExpressionSoThatWeBreak\n"
24810 " ? 2\n"
24811 " : []() {\n"
24812 " ;\n"
24813 " return 5;\n"
24814 " };",
24815 Style);
24816 }
24817
24818 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) {
24819 auto Style = getLLVMStyle();
24820
24821 StringRef Short = "functionCall(paramA, paramB, paramC);\n"
24822 "void functionDecl(int a, int b, int c);";
24823
24824 StringRef Medium = "functionCall(paramA, paramB, paramC, paramD, paramE, "
24825 "paramF, paramG, paramH, paramI);\n"
24826 "void functionDecl(int argumentA, int argumentB, int "
24827 "argumentC, int argumentD, int argumentE);";
24828
24829 verifyFormat(Short, Style);
24830
24831 StringRef NoBreak = "functionCall(paramA, paramB, paramC, paramD, paramE, "
24832 "paramF, paramG, paramH,\n"
24833 " paramI);\n"
24834 "void functionDecl(int argumentA, int argumentB, int "
24835 "argumentC, int argumentD,\n"
24836 " int argumentE);";
24837
24838 verifyFormat(NoBreak, Medium, Style);
24839 verifyFormat(NoBreak,
24840 "functionCall(\n"
24841 " paramA,\n"
24842 " paramB,\n"
24843 " paramC,\n"
24844 " paramD,\n"
24845 " paramE,\n"
24846 " paramF,\n"
24847 " paramG,\n"
24848 " paramH,\n"
24849 " paramI\n"
24850 ");\n"
24851 "void functionDecl(\n"
24852 " int argumentA,\n"
24853 " int argumentB,\n"
24854 " int argumentC,\n"
24855 " int argumentD,\n"
24856 " int argumentE\n"
24857 ");",
24858 Style);
24859
24860 verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n"
24861 " nestedLongFunctionCall(argument1, "
24862 "argument2, argument3,\n"
24863 " argument4, "
24864 "argument5));",
24865 Style);
24866
24867 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
24868
24869 verifyFormat(Short, Style);
24870 verifyFormat(
24871 "functionCall(\n"
24872 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
24873 "paramI\n"
24874 ");\n"
24875 "void functionDecl(\n"
24876 " int argumentA, int argumentB, int argumentC, int argumentD, int "
24877 "argumentE\n"
24878 ");",
24879 Medium, Style);
24880
24881 Style.AllowAllArgumentsOnNextLine = false;
24882 Style.AllowAllParametersOfDeclarationOnNextLine = false;
24883
24884 verifyFormat(Short, Style);
24885 verifyFormat(
24886 "functionCall(\n"
24887 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
24888 "paramI\n"
24889 ");\n"
24890 "void functionDecl(\n"
24891 " int argumentA, int argumentB, int argumentC, int argumentD, int "
24892 "argumentE\n"
24893 ");",
24894 Medium, Style);
24895
24896 Style.BinPackArguments = false;
24897 Style.BinPackParameters = false;
24898
24899 verifyFormat(Short, Style);
24900
24901 verifyFormat("functionCall(\n"
24902 " paramA,\n"
24903 " paramB,\n"
24904 " paramC,\n"
24905 " paramD,\n"
24906 " paramE,\n"
24907 " paramF,\n"
24908 " paramG,\n"
24909 " paramH,\n"
24910 " paramI\n"
24911 ");\n"
24912 "void functionDecl(\n"
24913 " int argumentA,\n"
24914 " int argumentB,\n"
24915 " int argumentC,\n"
24916 " int argumentD,\n"
24917 " int argumentE\n"
24918 ");",
24919 Medium, Style);
24920
24921 verifyFormat("outerFunctionCall(\n"
24922 " nestedFunctionCall(argument1),\n"
24923 " nestedLongFunctionCall(\n"
24924 " argument1,\n"
24925 " argument2,\n"
24926 " argument3,\n"
24927 " argument4,\n"
24928 " argument5\n"
24929 " )\n"
24930 ");",
24931 Style);
24932
24933 verifyFormat("int a = (int)b;", Style);
24934 verifyFormat("int a = (int)b;",
24935 "int a = (\n"
24936 " int\n"
24937 ") b;",
24938 Style);
24939
24940 verifyFormat("return (true);", Style);
24941 verifyFormat("return (true);",
24942 "return (\n"
24943 " true\n"
24944 ");",
24945 Style);
24946
24947 verifyFormat("void foo();", Style);
24948 verifyFormat("void foo();",
24949 "void foo(\n"
24950 ");",
24951 Style);
24952
24953 verifyFormat("void foo() {}", Style);
24954 verifyFormat("void foo() {}",
24955 "void foo(\n"
24956 ") {\n"
24957 "}",
24958 Style);
24959
24960 verifyFormat("auto string = std::string();", Style);
24961 verifyFormat("auto string = std::string();",
24962 "auto string = std::string(\n"
24963 ");",
24964 Style);
24965
24966 verifyFormat("void (*functionPointer)() = nullptr;", Style);
24967 verifyFormat("void (*functionPointer)() = nullptr;",
24968 "void (\n"
24969 " *functionPointer\n"
24970 ")\n"
24971 "(\n"
24972 ") = nullptr;",
24973 Style);
24974 }
24975
24976 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) {
24977 auto Style = getLLVMStyle();
24978
24979 verifyFormat("if (foo()) {\n"
24980 " return;\n"
24981 "}",
24982 Style);
24983
24984 verifyFormat("if (quitelongarg !=\n"
24985 " (alsolongarg - 1)) { // ABC is a very longgggggggggggg "
24986 "comment\n"
24987 " return;\n"
24988 "}",
24989 Style);
24990
24991 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
24992
24993 verifyFormat("if (foo()) {\n"
24994 " return;\n"
24995 "}",
24996 Style);
24997
24998 verifyFormat("if (quitelongarg !=\n"
24999 " (alsolongarg - 1)) { // ABC is a very longgggggggggggg "
25000 "comment\n"
25001 " return;\n"
25002 "}",
25003 Style);
25004 }
25005
25006 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
25007 auto Style = getLLVMStyle();
25008
25009 verifyFormat("for (int i = 0; i < 5; ++i) {\n"
25010 " doSomething();\n"
25011 "}",
25012 Style);
25013
25014 verifyFormat("for (int myReallyLongCountVariable = 0; "
25015 "myReallyLongCountVariable < count;\n"
25016 " myReallyLongCountVariable++) {\n"
25017 " doSomething();\n"
25018 "}",
25019 Style);
25020
25021 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
25022
25023 verifyFormat("for (int i = 0; i < 5; ++i) {\n"
25024 " doSomething();\n"
25025 "}",
25026 Style);
25027
25028 verifyFormat("for (int myReallyLongCountVariable = 0; "
25029 "myReallyLongCountVariable < count;\n"
25030 " myReallyLongCountVariable++) {\n"
25031 " doSomething();\n"
25032 "}",
25033 Style);
25034 }
25035
25036 TEST_F(FormatTest, UnderstandsDigraphs) {
25037 verifyFormat("int arr<:5:> = {};");
25038 verifyFormat("int arr[5] = <%%>;");
25039 verifyFormat("int arr<:::qualified_variable:> = {};");
25040 verifyFormat("int arr[::qualified_variable] = <%%>;");
25041 verifyFormat("%:include <header>");
25042 verifyFormat("%:define A x##y");
25043 verifyFormat("#define A x%:%:y");
25044 }
25045
25046 TEST_F(FormatTest, AlignArrayOfStructuresLeftAlignmentNonSquare) {
25047 auto Style = getLLVMStyle();
25048 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
25049 Style.AlignConsecutiveAssignments.Enabled = true;
25050 Style.AlignConsecutiveDeclarations.Enabled = true;
25051
25052 // The AlignArray code is incorrect for non square Arrays and can cause
25053 // crashes, these tests assert that the array is not changed but will
25054 // also act as regression tests for when it is properly fixed
25055 verifyFormat("struct test demo[] = {\n"
25056 " {1, 2},\n"
25057 " {3, 4, 5},\n"
25058 " {6, 7, 8}\n"
25059 "};",
25060 Style);
25061 verifyFormat("struct test demo[] = {\n"
25062 " {1, 2, 3, 4, 5},\n"
25063 " {3, 4, 5},\n"
25064 " {6, 7, 8}\n"
25065 "};",
25066 Style);
25067 verifyFormat("struct test demo[] = {\n"
25068 " {1, 2, 3, 4, 5},\n"
25069 " {3, 4, 5},\n"
25070 " {6, 7, 8, 9, 10, 11, 12}\n"
25071 "};",
25072 Style);
25073 verifyFormat("struct test demo[] = {\n"
25074 " {1, 2, 3},\n"
25075 " {3, 4, 5},\n"
25076 " {6, 7, 8, 9, 10, 11, 12}\n"
25077 "};",
25078 Style);
25079
25080 verifyFormat("S{\n"
25081 " {},\n"
25082 " {},\n"
25083 " {a, b}\n"
25084 "};",
25085 Style);
25086 verifyFormat("S{\n"
25087 " {},\n"
25088 " {},\n"
25089 " {a, b},\n"
25090 "};",
25091 Style);
25092 verifyFormat("void foo() {\n"
25093 " auto thing = test{\n"
25094 " {\n"
25095 " {13}, {something}, // A\n"
25096 " }\n"
25097 " };\n"
25098 "}",
25099 "void foo() {\n"
25100 " auto thing = test{\n"
25101 " {\n"
25102 " {13},\n"
25103 " {something}, // A\n"
25104 " }\n"
25105 " };\n"
25106 "}",
25107 Style);
25108 }
25109
25110 TEST_F(FormatTest, AlignArrayOfStructuresRightAlignmentNonSquare) {
25111 auto Style = getLLVMStyle();
25112 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
25113 Style.AlignConsecutiveAssignments.Enabled = true;
25114 Style.AlignConsecutiveDeclarations.Enabled = true;
25115
25116 // The AlignArray code is incorrect for non square Arrays and can cause
25117 // crashes, these tests assert that the array is not changed but will
25118 // also act as regression tests for when it is properly fixed
25119 verifyFormat("struct test demo[] = {\n"
25120 " {1, 2},\n"
25121 " {3, 4, 5},\n"
25122 " {6, 7, 8}\n"
25123 "};",
25124 Style);
25125 verifyFormat("struct test demo[] = {\n"
25126 " {1, 2, 3, 4, 5},\n"
25127 " {3, 4, 5},\n"
25128 " {6, 7, 8}\n"
25129 "};",
25130 Style);
25131 verifyFormat("struct test demo[] = {\n"
25132 " {1, 2, 3, 4, 5},\n"
25133 " {3, 4, 5},\n"
25134 " {6, 7, 8, 9, 10, 11, 12}\n"
25135 "};",
25136 Style);
25137 verifyFormat("struct test demo[] = {\n"
25138 " {1, 2, 3},\n"
25139 " {3, 4, 5},\n"
25140 " {6, 7, 8, 9, 10, 11, 12}\n"
25141 "};",
25142 Style);
25143
25144 verifyFormat("S{\n"
25145 " {},\n"
25146 " {},\n"
25147 " {a, b}\n"
25148 "};",
25149 Style);
25150 verifyFormat("S{\n"
25151 " {},\n"
25152 " {},\n"
25153 " {a, b},\n"
25154 "};",
25155 Style);
25156 verifyFormat("void foo() {\n"
25157 " auto thing = test{\n"
25158 " {\n"
25159 " {13}, {something}, // A\n"
25160 " }\n"
25161 " };\n"
25162 "}",
25163 "void foo() {\n"
25164 " auto thing = test{\n"
25165 " {\n"
25166 " {13},\n"
25167 " {something}, // A\n"
25168 " }\n"
25169 " };\n"
25170 "}",
25171 Style);
25172 }
25173
25174 TEST_F(FormatTest, FormatsVariableTemplates) {
25175 verifyFormat("inline bool var = is_integral_v<int> && is_signed_v<int>;");
25176 verifyFormat("template <typename T> "
25177 "inline bool var = is_integral_v<T> && is_signed_v<T>;");
25178 }
25179
25180 TEST_F(FormatTest, RemoveSemicolon) {
25181 FormatStyle Style = getLLVMStyle();
25182 Style.RemoveSemicolon = true;
25183
25184 verifyFormat("int max(int a, int b) { return a > b ? a : b; }",
25185 "int max(int a, int b) { return a > b ? a : b; };", Style);
25186
25187 verifyFormat("int max(int a, int b) { return a > b ? a : b; }",
25188 "int max(int a, int b) { return a > b ? a : b; };;", Style);
25189
25190 verifyFormat("class Foo {\n"
25191 " int getSomething() const { return something; }\n"
25192 "};",
25193 "class Foo {\n"
25194 " int getSomething() const { return something; };\n"
25195 "};",
25196 Style);
25197
25198 verifyFormat("class Foo {\n"
25199 " int getSomething() const { return something; }\n"
25200 "};",
25201 "class Foo {\n"
25202 " int getSomething() const { return something; };;\n"
25203 "};",
25204 Style);
25205
25206 verifyFormat("for (;;) {\n"
25207 "}",
25208 Style);
25209
25210 verifyFormat("class [[deprecated(\"\")]] C {\n"
25211 " int i;\n"
25212 "};",
25213 Style);
25214
25215 verifyFormat("struct EXPORT_MACRO [[nodiscard]] C {\n"
25216 " int i;\n"
25217 "};",
25218 Style);
25219
25220 verifyIncompleteFormat("class C final [[deprecated(l]] {});", Style);
25221
25222 // These tests are here to show a problem that may not be easily
25223 // solved, our implementation to remove semicolons is only as good
25224 // as our FunctionLBrace detection and this fails for empty braces
25225 // because we can't distringuish this from a bracelist.
25226 // We will enable when that is resolved.
25227 #if 0
25228 verifyFormat("void main() {}", "void main() {};", Style);
25229 verifyFormat("void foo() {} //\n"
25230 "int bar;",
25231 "void foo() {}; //\n"
25232 "; int bar;",
25233 Style);
25234 #endif
25235 }
25236
25237 TEST_F(FormatTest, BreakAfterAttributes) {
25238 FormatStyle Style = getLLVMStyle();
25239 EXPECT_EQ(Style.BreakAfterAttributes, FormatStyle::ABS_Never);
25240
25241 const StringRef Code("[[nodiscard]] inline int f(int &i);\n"
25242 "[[foo([[]])]] [[nodiscard]]\n"
25243 "int g(int &i);\n"
25244 "[[nodiscard]]\n"
25245 "inline int f(int &i) {\n"
25246 " i = 1;\n"
25247 " return 0;\n"
25248 "}\n"
25249 "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n"
25250 " i = 0;\n"
25251 " return 1;\n"
25252 "}");
25253
25254 verifyFormat("[[nodiscard]] inline int f(int &i);\n"
25255 "[[foo([[]])]] [[nodiscard]] int g(int &i);\n"
25256 "[[nodiscard]] inline int f(int &i) {\n"
25257 " i = 1;\n"
25258 " return 0;\n"
25259 "}\n"
25260 "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n"
25261 " i = 0;\n"
25262 " return 1;\n"
25263 "}",
25264 Code, Style);
25265
25266 Style.BreakAfterAttributes = FormatStyle::ABS_Always;
25267 verifyFormat("[[nodiscard]]\n"
25268 "inline int f(int &i);\n"
25269 "[[foo([[]])]] [[nodiscard]]\n"
25270 "int g(int &i);\n"
25271 "[[nodiscard]]\n"
25272 "inline int f(int &i) {\n"
25273 " i = 1;\n"
25274 " return 0;\n"
25275 "}\n"
25276 "[[foo([[]])]] [[nodiscard]]\n"
25277 "int g(int &i) {\n"
25278 " i = 0;\n"
25279 " return 1;\n"
25280 "}",
25281 Code, Style);
25282
25283 Style.BreakAfterAttributes = FormatStyle::ABS_Leave;
25284 EXPECT_EQ(Code, format(Code, Style));
25285 }
25286
25287 TEST_F(FormatTest, InsertNewlineAtEOF) {
25288 FormatStyle Style = getLLVMStyle();
25289 Style.InsertNewlineAtEOF = true;
25290
25291 verifyFormat("int i;\n", Style);
25292 verifyFormat("int i;\n", "int i;", Style);
25293 }
25294
25295 TEST_F(FormatTest, SpaceAfterUDL) {
25296 verifyFormat("auto c = (4s).count();");
25297 verifyFormat("auto x = 5s .count() == 5;");
25298 }
25299
25300 } // namespace
25301 } // namespace format
25302 } // namespace clang