xref: /llvm-project/clang/unittests/Format/FormatTest.cpp (revision f0bd62d8709a49dd87eb75411e41e2e11e0ab59d)
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 "FormatTestBase.h"
10 
11 #define DEBUG_TYPE "format-test"
12 
13 namespace clang {
14 namespace format {
15 namespace test {
16 namespace {
17 
18 class FormatTest : public test::FormatTestBase {};
19 
20 TEST_F(FormatTest, MessUp) {
21   EXPECT_EQ("1 2 3", messUp("1 2 3"));
22   EXPECT_EQ("1 2 3", messUp("1\n2\n3"));
23   EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc"));
24   EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc"));
25   EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne"));
26 }
27 
28 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) {
29   EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language);
30 }
31 
32 TEST_F(FormatTest, LLVMStyleOverride) {
33   EXPECT_EQ(FormatStyle::LK_Proto,
34             getLLVMStyle(FormatStyle::LK_Proto).Language);
35 }
36 
37 //===----------------------------------------------------------------------===//
38 // Basic function tests.
39 //===----------------------------------------------------------------------===//
40 
41 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { verifyFormat(";"); }
42 
43 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
44   verifyFormat("int i;", "  int i;");
45   verifyFormat("\nint i;", " \n\t \v \f  int i;");
46   verifyFormat("int i;\nint j;", "    int i; int j;");
47   verifyFormat("int i;\nint j;", "    int i;\n  int j;");
48 
49   auto Style = getLLVMStyle();
50   Style.KeepEmptyLines.AtStartOfFile = false;
51   verifyFormat("int i;", " \n\t \v \f  int i;", Style);
52 }
53 
54 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
55   verifyFormat("int i;", "int\ni;");
56 }
57 
58 TEST_F(FormatTest, FormatsNestedBlockStatements) {
59   verifyFormat("{\n"
60                "  {\n"
61                "    {\n"
62                "    }\n"
63                "  }\n"
64                "}",
65                "{{{}}}");
66 }
67 
68 TEST_F(FormatTest, FormatsNestedCall) {
69   verifyFormat("Method(f1, f2(f3));");
70   verifyFormat("Method(f1(f2, f3()));");
71   verifyFormat("Method(f1(f2, (f3())));");
72 }
73 
74 TEST_F(FormatTest, NestedNameSpecifiers) {
75   verifyFormat("vector<::Type> v;");
76   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
77   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
78   verifyFormat("static constexpr bool Bar = typeof(bar())::value;");
79   verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;");
80   verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;");
81   verifyFormat("bool a = 2 < ::SomeFunction();");
82   verifyFormat("ALWAYS_INLINE ::std::string getName();");
83   verifyFormat("some::string getName();");
84 }
85 
86 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
87   verifyFormat("if (a) {\n"
88                "  f();\n"
89                "}",
90                "if(a){f();}");
91   EXPECT_EQ(4, ReplacementCount);
92   verifyNoChange("if (a) {\n"
93                  "  f();\n"
94                  "}");
95   EXPECT_EQ(0, ReplacementCount);
96   verifyNoChange("/*\r\n"
97                  "\r\n"
98                  "*/");
99   EXPECT_EQ(0, ReplacementCount);
100 }
101 
102 TEST_F(FormatTest, RemovesEmptyLines) {
103   verifyFormat("class C {\n"
104                "  int i;\n"
105                "};",
106                "class C {\n"
107                " int i;\n"
108                "\n"
109                "};");
110 
111   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
112   verifyFormat("namespace N {\n"
113                "\n"
114                "int i;\n"
115                "}",
116                "namespace N {\n"
117                "\n"
118                "int    i;\n"
119                "}",
120                getGoogleStyle());
121   verifyFormat("/* something */ namespace N {\n"
122                "\n"
123                "int i;\n"
124                "}",
125                "/* something */ namespace N {\n"
126                "\n"
127                "int    i;\n"
128                "}",
129                getGoogleStyle());
130   verifyFormat("inline namespace N {\n"
131                "\n"
132                "int i;\n"
133                "}",
134                "inline namespace N {\n"
135                "\n"
136                "int    i;\n"
137                "}",
138                getGoogleStyle());
139   verifyFormat("/* something */ inline namespace N {\n"
140                "\n"
141                "int i;\n"
142                "}",
143                "/* something */ inline namespace N {\n"
144                "\n"
145                "int    i;\n"
146                "}",
147                getGoogleStyle());
148   verifyFormat("export namespace N {\n"
149                "\n"
150                "int i;\n"
151                "}",
152                "export namespace N {\n"
153                "\n"
154                "int    i;\n"
155                "}",
156                getGoogleStyle());
157   verifyFormat("extern /**/ \"C\" /**/ {\n"
158                "\n"
159                "int i;\n"
160                "}",
161                "extern /**/ \"C\" /**/ {\n"
162                "\n"
163                "int    i;\n"
164                "}",
165                getGoogleStyle());
166 
167   auto CustomStyle = getLLVMStyle();
168   CustomStyle.BreakBeforeBraces = FormatStyle::BS_Custom;
169   CustomStyle.BraceWrapping.AfterNamespace = true;
170   CustomStyle.KeepEmptyLines.AtStartOfBlock = false;
171   verifyFormat("namespace N\n"
172                "{\n"
173                "\n"
174                "int i;\n"
175                "}",
176                "namespace N\n"
177                "{\n"
178                "\n"
179                "\n"
180                "int    i;\n"
181                "}",
182                CustomStyle);
183   verifyFormat("/* something */ namespace N\n"
184                "{\n"
185                "\n"
186                "int i;\n"
187                "}",
188                "/* something */ namespace N {\n"
189                "\n"
190                "\n"
191                "int    i;\n"
192                "}",
193                CustomStyle);
194   verifyFormat("inline namespace N\n"
195                "{\n"
196                "\n"
197                "int i;\n"
198                "}",
199                "inline namespace N\n"
200                "{\n"
201                "\n"
202                "\n"
203                "int    i;\n"
204                "}",
205                CustomStyle);
206   verifyFormat("/* something */ inline namespace N\n"
207                "{\n"
208                "\n"
209                "int i;\n"
210                "}",
211                "/* something */ inline namespace N\n"
212                "{\n"
213                "\n"
214                "int    i;\n"
215                "}",
216                CustomStyle);
217   verifyFormat("export namespace N\n"
218                "{\n"
219                "\n"
220                "int i;\n"
221                "}",
222                "export namespace N\n"
223                "{\n"
224                "\n"
225                "int    i;\n"
226                "}",
227                CustomStyle);
228   verifyFormat("namespace a\n"
229                "{\n"
230                "namespace b\n"
231                "{\n"
232                "\n"
233                "class AA {};\n"
234                "\n"
235                "} // namespace b\n"
236                "} // namespace a",
237                "namespace a\n"
238                "{\n"
239                "namespace b\n"
240                "{\n"
241                "\n"
242                "\n"
243                "class AA {};\n"
244                "\n"
245                "\n"
246                "}\n"
247                "}",
248                CustomStyle);
249   verifyFormat("namespace A /* comment */\n"
250                "{\n"
251                "class B {}\n"
252                "} // namespace A",
253                "namespace A /* comment */ { class B {} }", CustomStyle);
254   verifyFormat("namespace A\n"
255                "{ /* comment */\n"
256                "class B {}\n"
257                "} // namespace A",
258                "namespace A {/* comment */ class B {} }", CustomStyle);
259   verifyFormat("namespace A\n"
260                "{ /* comment */\n"
261                "\n"
262                "class B {}\n"
263                "\n"
264                ""
265                "} // namespace A",
266                "namespace A { /* comment */\n"
267                "\n"
268                "\n"
269                "class B {}\n"
270                "\n"
271                "\n"
272                "}",
273                CustomStyle);
274   verifyFormat("namespace A /* comment */\n"
275                "{\n"
276                "\n"
277                "class B {}\n"
278                "\n"
279                "} // namespace A",
280                "namespace A/* comment */ {\n"
281                "\n"
282                "\n"
283                "class B {}\n"
284                "\n"
285                "\n"
286                "}",
287                CustomStyle);
288 
289   // ...but do keep inlining and removing empty lines for non-block extern "C"
290   // functions.
291   verifyGoogleFormat("extern \"C\" int f() { return 42; }");
292   verifyFormat("extern \"C\" int f() {\n"
293                "  int i = 42;\n"
294                "  return i;\n"
295                "}",
296                "extern \"C\" int f() {\n"
297                "\n"
298                "  int i = 42;\n"
299                "  return i;\n"
300                "}",
301                getGoogleStyle());
302 
303   // Remove empty lines at the beginning and end of blocks.
304   verifyFormat("void f() {\n"
305                "\n"
306                "  if (a) {\n"
307                "\n"
308                "    f();\n"
309                "  }\n"
310                "}",
311                "void f() {\n"
312                "\n"
313                "  if (a) {\n"
314                "\n"
315                "    f();\n"
316                "\n"
317                "  }\n"
318                "\n"
319                "}");
320   verifyFormat("void f() {\n"
321                "  if (a) {\n"
322                "    f();\n"
323                "  }\n"
324                "}",
325                "void f() {\n"
326                "\n"
327                "  if (a) {\n"
328                "\n"
329                "    f();\n"
330                "\n"
331                "  }\n"
332                "\n"
333                "}",
334                getGoogleStyle());
335 
336   // Don't remove empty lines in more complex control statements.
337   verifyFormat("void f() {\n"
338                "  if (a) {\n"
339                "    f();\n"
340                "\n"
341                "  } else if (b) {\n"
342                "    f();\n"
343                "  }\n"
344                "}",
345                "void f() {\n"
346                "  if (a) {\n"
347                "    f();\n"
348                "\n"
349                "  } else if (b) {\n"
350                "    f();\n"
351                "\n"
352                "  }\n"
353                "\n"
354                "}");
355 
356   // Don't remove empty lines before namespace endings.
357   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
358   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
359   verifyNoChange("namespace {\n"
360                  "int i;\n"
361                  "\n"
362                  "}",
363                  LLVMWithNoNamespaceFix);
364   verifyFormat("namespace {\n"
365                "int i;\n"
366                "}",
367                LLVMWithNoNamespaceFix);
368   verifyNoChange("namespace {\n"
369                  "int i;\n"
370                  "\n"
371                  "};",
372                  LLVMWithNoNamespaceFix);
373   verifyFormat("namespace {\n"
374                "int i;\n"
375                "};",
376                LLVMWithNoNamespaceFix);
377   verifyNoChange("namespace {\n"
378                  "int i;\n"
379                  "\n"
380                  "}");
381   verifyFormat("namespace {\n"
382                "int i;\n"
383                "\n"
384                "} // namespace",
385                "namespace {\n"
386                "int i;\n"
387                "\n"
388                "}  // namespace");
389 
390   FormatStyle Style = getLLVMStyle();
391   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
392   Style.MaxEmptyLinesToKeep = 2;
393   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
394   Style.BraceWrapping.AfterClass = true;
395   Style.BraceWrapping.AfterFunction = true;
396   Style.KeepEmptyLines.AtStartOfBlock = false;
397 
398   verifyFormat("class Foo\n"
399                "{\n"
400                "  Foo() {}\n"
401                "\n"
402                "  void funk() {}\n"
403                "};",
404                "class Foo\n"
405                "{\n"
406                "  Foo()\n"
407                "  {\n"
408                "  }\n"
409                "\n"
410                "  void funk() {}\n"
411                "};",
412                Style);
413 }
414 
415 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
416   verifyFormat("x = (a) and (b);");
417   verifyFormat("x = (a) or (b);");
418   verifyFormat("x = (a) bitand (b);");
419   verifyFormat("x = (a) bitor (b);");
420   verifyFormat("x = (a) not_eq (b);");
421   verifyFormat("x = (a) and_eq (b);");
422   verifyFormat("x = (a) or_eq (b);");
423   verifyFormat("x = (a) xor (b);");
424 }
425 
426 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) {
427   verifyFormat("x = compl(a);");
428   verifyFormat("x = not(a);");
429   verifyFormat("x = bitand(a);");
430   // Unary operator must not be merged with the next identifier
431   verifyFormat("x = compl a;");
432   verifyFormat("x = not a;");
433   verifyFormat("x = bitand a;");
434 }
435 
436 //===----------------------------------------------------------------------===//
437 // Tests for control statements.
438 //===----------------------------------------------------------------------===//
439 
440 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
441   verifyFormat("if (true)\n  f();\ng();");
442   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
443   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
444   verifyFormat("if constexpr (true)\n"
445                "  f();\ng();");
446   verifyFormat("if CONSTEXPR (true)\n"
447                "  f();\ng();");
448   verifyFormat("if constexpr (a)\n"
449                "  if constexpr (b)\n"
450                "    if constexpr (c)\n"
451                "      g();\n"
452                "h();");
453   verifyFormat("if CONSTEXPR (a)\n"
454                "  if CONSTEXPR (b)\n"
455                "    if CONSTEXPR (c)\n"
456                "      g();\n"
457                "h();");
458   verifyFormat("if constexpr (a)\n"
459                "  if constexpr (b) {\n"
460                "    f();\n"
461                "  }\n"
462                "g();");
463   verifyFormat("if CONSTEXPR (a)\n"
464                "  if CONSTEXPR (b) {\n"
465                "    f();\n"
466                "  }\n"
467                "g();");
468 
469   verifyFormat("if consteval {\n}");
470   verifyFormat("if !consteval {\n}");
471   verifyFormat("if not consteval {\n}");
472   verifyFormat("if consteval {\n} else {\n}");
473   verifyFormat("if !consteval {\n} else {\n}");
474   verifyFormat("if consteval {\n"
475                "  f();\n"
476                "}");
477   verifyFormat("if !consteval {\n"
478                "  f();\n"
479                "}");
480   verifyFormat("if consteval {\n"
481                "  f();\n"
482                "} else {\n"
483                "  g();\n"
484                "}");
485   verifyFormat("if CONSTEVAL {\n"
486                "  f();\n"
487                "}");
488   verifyFormat("if !CONSTEVAL {\n"
489                "  f();\n"
490                "}");
491 
492   verifyFormat("if (a)\n"
493                "  g();");
494   verifyFormat("if (a) {\n"
495                "  g()\n"
496                "};");
497   verifyFormat("if (a)\n"
498                "  g();\n"
499                "else\n"
500                "  g();");
501   verifyFormat("if (a) {\n"
502                "  g();\n"
503                "} else\n"
504                "  g();");
505   verifyFormat("if (a)\n"
506                "  g();\n"
507                "else {\n"
508                "  g();\n"
509                "}");
510   verifyFormat("if (a) {\n"
511                "  g();\n"
512                "} else {\n"
513                "  g();\n"
514                "}");
515   verifyFormat("if (a)\n"
516                "  g();\n"
517                "else if (b)\n"
518                "  g();\n"
519                "else\n"
520                "  g();");
521   verifyFormat("if (a) {\n"
522                "  g();\n"
523                "} else if (b)\n"
524                "  g();\n"
525                "else\n"
526                "  g();");
527   verifyFormat("if (a)\n"
528                "  g();\n"
529                "else if (b) {\n"
530                "  g();\n"
531                "} else\n"
532                "  g();");
533   verifyFormat("if (a)\n"
534                "  g();\n"
535                "else if (b)\n"
536                "  g();\n"
537                "else {\n"
538                "  g();\n"
539                "}");
540   verifyFormat("if (a)\n"
541                "  g();\n"
542                "else if (b) {\n"
543                "  g();\n"
544                "} else {\n"
545                "  g();\n"
546                "}");
547   verifyFormat("if (a) {\n"
548                "  g();\n"
549                "} else if (b) {\n"
550                "  g();\n"
551                "} else {\n"
552                "  g();\n"
553                "}");
554 
555   FormatStyle AllowsMergedIf = getLLVMStyle();
556   AllowsMergedIf.IfMacros.push_back("MYIF");
557   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
558   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
559       FormatStyle::SIS_WithoutElse;
560   verifyFormat("if (a)\n"
561                "  // comment\n"
562                "  f();",
563                AllowsMergedIf);
564   verifyFormat("{\n"
565                "  if (a)\n"
566                "  label:\n"
567                "    f();\n"
568                "}",
569                AllowsMergedIf);
570   verifyFormat("#define A \\\n"
571                "  if (a)  \\\n"
572                "  label:  \\\n"
573                "    f()",
574                AllowsMergedIf);
575   verifyFormat("if (a)\n"
576                "  ;",
577                AllowsMergedIf);
578   verifyFormat("if (a)\n"
579                "  if (b) return;",
580                AllowsMergedIf);
581 
582   verifyFormat("if (a) // Can't merge this\n"
583                "  f();",
584                AllowsMergedIf);
585   verifyFormat("if (a) /* still don't merge */\n"
586                "  f();",
587                AllowsMergedIf);
588   verifyFormat("if (a) { // Never merge this\n"
589                "  f();\n"
590                "}",
591                AllowsMergedIf);
592   verifyFormat("if (a) { /* Never merge this */\n"
593                "  f();\n"
594                "}",
595                AllowsMergedIf);
596   verifyFormat("MYIF (a)\n"
597                "  // comment\n"
598                "  f();",
599                AllowsMergedIf);
600   verifyFormat("{\n"
601                "  MYIF (a)\n"
602                "  label:\n"
603                "    f();\n"
604                "}",
605                AllowsMergedIf);
606   verifyFormat("#define A  \\\n"
607                "  MYIF (a) \\\n"
608                "  label:   \\\n"
609                "    f()",
610                AllowsMergedIf);
611   verifyFormat("MYIF (a)\n"
612                "  ;",
613                AllowsMergedIf);
614   verifyFormat("MYIF (a)\n"
615                "  MYIF (b) return;",
616                AllowsMergedIf);
617 
618   verifyFormat("MYIF (a) // Can't merge this\n"
619                "  f();",
620                AllowsMergedIf);
621   verifyFormat("MYIF (a) /* still don't merge */\n"
622                "  f();",
623                AllowsMergedIf);
624   verifyFormat("MYIF (a) { // Never merge this\n"
625                "  f();\n"
626                "}",
627                AllowsMergedIf);
628   verifyFormat("MYIF (a) { /* Never merge this */\n"
629                "  f();\n"
630                "}",
631                AllowsMergedIf);
632 
633   AllowsMergedIf.ColumnLimit = 14;
634   // Where line-lengths matter, a 2-letter synonym that maintains line length.
635   // Not IF to avoid any confusion that IF is somehow special.
636   AllowsMergedIf.IfMacros.push_back("FI");
637   verifyFormat("if (a) return;", AllowsMergedIf);
638   verifyFormat("if (aaaaaaaaa)\n"
639                "  return;",
640                AllowsMergedIf);
641   verifyFormat("FI (a) return;", AllowsMergedIf);
642   verifyFormat("FI (aaaaaaaaa)\n"
643                "  return;",
644                AllowsMergedIf);
645 
646   AllowsMergedIf.ColumnLimit = 13;
647   verifyFormat("if (a)\n  return;", AllowsMergedIf);
648   verifyFormat("FI (a)\n  return;", AllowsMergedIf);
649 
650   FormatStyle AllowsMergedIfElse = getLLVMStyle();
651   AllowsMergedIfElse.IfMacros.push_back("MYIF");
652   AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine =
653       FormatStyle::SIS_AllIfsAndElse;
654   verifyFormat("if (a)\n"
655                "  // comment\n"
656                "  f();\n"
657                "else\n"
658                "  // comment\n"
659                "  f();",
660                AllowsMergedIfElse);
661   verifyFormat("{\n"
662                "  if (a)\n"
663                "  label:\n"
664                "    f();\n"
665                "  else\n"
666                "  label:\n"
667                "    f();\n"
668                "}",
669                AllowsMergedIfElse);
670   verifyFormat("if (a)\n"
671                "  ;\n"
672                "else\n"
673                "  ;",
674                AllowsMergedIfElse);
675   verifyFormat("if (a) {\n"
676                "} else {\n"
677                "}",
678                AllowsMergedIfElse);
679   verifyFormat("if (a) return;\n"
680                "else if (b) return;\n"
681                "else return;",
682                AllowsMergedIfElse);
683   verifyFormat("if (a) {\n"
684                "} else return;",
685                AllowsMergedIfElse);
686   verifyFormat("if (a) {\n"
687                "} else if (b) return;\n"
688                "else return;",
689                AllowsMergedIfElse);
690   verifyFormat("if (a) return;\n"
691                "else if (b) {\n"
692                "} else return;",
693                AllowsMergedIfElse);
694   verifyFormat("if (a)\n"
695                "  if (b) return;\n"
696                "  else return;",
697                AllowsMergedIfElse);
698   verifyFormat("if constexpr (a)\n"
699                "  if constexpr (b) return;\n"
700                "  else if constexpr (c) return;\n"
701                "  else return;",
702                AllowsMergedIfElse);
703   verifyFormat("MYIF (a)\n"
704                "  // comment\n"
705                "  f();\n"
706                "else\n"
707                "  // comment\n"
708                "  f();",
709                AllowsMergedIfElse);
710   verifyFormat("{\n"
711                "  MYIF (a)\n"
712                "  label:\n"
713                "    f();\n"
714                "  else\n"
715                "  label:\n"
716                "    f();\n"
717                "}",
718                AllowsMergedIfElse);
719   verifyFormat("MYIF (a)\n"
720                "  ;\n"
721                "else\n"
722                "  ;",
723                AllowsMergedIfElse);
724   verifyFormat("MYIF (a) {\n"
725                "} else {\n"
726                "}",
727                AllowsMergedIfElse);
728   verifyFormat("MYIF (a) return;\n"
729                "else MYIF (b) return;\n"
730                "else return;",
731                AllowsMergedIfElse);
732   verifyFormat("MYIF (a) {\n"
733                "} else return;",
734                AllowsMergedIfElse);
735   verifyFormat("MYIF (a) {\n"
736                "} else MYIF (b) return;\n"
737                "else return;",
738                AllowsMergedIfElse);
739   verifyFormat("MYIF (a) return;\n"
740                "else MYIF (b) {\n"
741                "} else return;",
742                AllowsMergedIfElse);
743   verifyFormat("MYIF (a)\n"
744                "  MYIF (b) return;\n"
745                "  else return;",
746                AllowsMergedIfElse);
747   verifyFormat("MYIF constexpr (a)\n"
748                "  MYIF constexpr (b) return;\n"
749                "  else MYIF constexpr (c) return;\n"
750                "  else return;",
751                AllowsMergedIfElse);
752 }
753 
754 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) {
755   FormatStyle AllowsMergedIf = getLLVMStyle();
756   AllowsMergedIf.IfMacros.push_back("MYIF");
757   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
758   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
759       FormatStyle::SIS_WithoutElse;
760   verifyFormat("if (a)\n"
761                "  f();\n"
762                "else {\n"
763                "  g();\n"
764                "}",
765                AllowsMergedIf);
766   verifyFormat("if (a)\n"
767                "  f();\n"
768                "else\n"
769                "  g();",
770                AllowsMergedIf);
771 
772   verifyFormat("if (a) g();", AllowsMergedIf);
773   verifyFormat("if (a) {\n"
774                "  g()\n"
775                "};",
776                AllowsMergedIf);
777   verifyFormat("if (a)\n"
778                "  g();\n"
779                "else\n"
780                "  g();",
781                AllowsMergedIf);
782   verifyFormat("if (a) {\n"
783                "  g();\n"
784                "} else\n"
785                "  g();",
786                AllowsMergedIf);
787   verifyFormat("if (a)\n"
788                "  g();\n"
789                "else {\n"
790                "  g();\n"
791                "}",
792                AllowsMergedIf);
793   verifyFormat("if (a) {\n"
794                "  g();\n"
795                "} else {\n"
796                "  g();\n"
797                "}",
798                AllowsMergedIf);
799   verifyFormat("if (a)\n"
800                "  g();\n"
801                "else if (b)\n"
802                "  g();\n"
803                "else\n"
804                "  g();",
805                AllowsMergedIf);
806   verifyFormat("if (a) {\n"
807                "  g();\n"
808                "} else if (b)\n"
809                "  g();\n"
810                "else\n"
811                "  g();",
812                AllowsMergedIf);
813   verifyFormat("if (a)\n"
814                "  g();\n"
815                "else if (b) {\n"
816                "  g();\n"
817                "} else\n"
818                "  g();",
819                AllowsMergedIf);
820   verifyFormat("if (a)\n"
821                "  g();\n"
822                "else if (b)\n"
823                "  g();\n"
824                "else {\n"
825                "  g();\n"
826                "}",
827                AllowsMergedIf);
828   verifyFormat("if (a)\n"
829                "  g();\n"
830                "else if (b) {\n"
831                "  g();\n"
832                "} else {\n"
833                "  g();\n"
834                "}",
835                AllowsMergedIf);
836   verifyFormat("if (a) {\n"
837                "  g();\n"
838                "} else if (b) {\n"
839                "  g();\n"
840                "} else {\n"
841                "  g();\n"
842                "}",
843                AllowsMergedIf);
844   verifyFormat("MYIF (a)\n"
845                "  f();\n"
846                "else {\n"
847                "  g();\n"
848                "}",
849                AllowsMergedIf);
850   verifyFormat("MYIF (a)\n"
851                "  f();\n"
852                "else\n"
853                "  g();",
854                AllowsMergedIf);
855 
856   verifyFormat("MYIF (a) g();", AllowsMergedIf);
857   verifyFormat("MYIF (a) {\n"
858                "  g()\n"
859                "};",
860                AllowsMergedIf);
861   verifyFormat("MYIF (a)\n"
862                "  g();\n"
863                "else\n"
864                "  g();",
865                AllowsMergedIf);
866   verifyFormat("MYIF (a) {\n"
867                "  g();\n"
868                "} else\n"
869                "  g();",
870                AllowsMergedIf);
871   verifyFormat("MYIF (a)\n"
872                "  g();\n"
873                "else {\n"
874                "  g();\n"
875                "}",
876                AllowsMergedIf);
877   verifyFormat("MYIF (a) {\n"
878                "  g();\n"
879                "} else {\n"
880                "  g();\n"
881                "}",
882                AllowsMergedIf);
883   verifyFormat("MYIF (a)\n"
884                "  g();\n"
885                "else MYIF (b)\n"
886                "  g();\n"
887                "else\n"
888                "  g();",
889                AllowsMergedIf);
890   verifyFormat("MYIF (a)\n"
891                "  g();\n"
892                "else if (b)\n"
893                "  g();\n"
894                "else\n"
895                "  g();",
896                AllowsMergedIf);
897   verifyFormat("MYIF (a) {\n"
898                "  g();\n"
899                "} else MYIF (b)\n"
900                "  g();\n"
901                "else\n"
902                "  g();",
903                AllowsMergedIf);
904   verifyFormat("MYIF (a) {\n"
905                "  g();\n"
906                "} else if (b)\n"
907                "  g();\n"
908                "else\n"
909                "  g();",
910                AllowsMergedIf);
911   verifyFormat("MYIF (a)\n"
912                "  g();\n"
913                "else MYIF (b) {\n"
914                "  g();\n"
915                "} else\n"
916                "  g();",
917                AllowsMergedIf);
918   verifyFormat("MYIF (a)\n"
919                "  g();\n"
920                "else if (b) {\n"
921                "  g();\n"
922                "} else\n"
923                "  g();",
924                AllowsMergedIf);
925   verifyFormat("MYIF (a)\n"
926                "  g();\n"
927                "else MYIF (b)\n"
928                "  g();\n"
929                "else {\n"
930                "  g();\n"
931                "}",
932                AllowsMergedIf);
933   verifyFormat("MYIF (a)\n"
934                "  g();\n"
935                "else if (b)\n"
936                "  g();\n"
937                "else {\n"
938                "  g();\n"
939                "}",
940                AllowsMergedIf);
941   verifyFormat("MYIF (a)\n"
942                "  g();\n"
943                "else MYIF (b) {\n"
944                "  g();\n"
945                "} else {\n"
946                "  g();\n"
947                "}",
948                AllowsMergedIf);
949   verifyFormat("MYIF (a)\n"
950                "  g();\n"
951                "else if (b) {\n"
952                "  g();\n"
953                "} else {\n"
954                "  g();\n"
955                "}",
956                AllowsMergedIf);
957   verifyFormat("MYIF (a) {\n"
958                "  g();\n"
959                "} else MYIF (b) {\n"
960                "  g();\n"
961                "} else {\n"
962                "  g();\n"
963                "}",
964                AllowsMergedIf);
965   verifyFormat("MYIF (a) {\n"
966                "  g();\n"
967                "} else if (b) {\n"
968                "  g();\n"
969                "} else {\n"
970                "  g();\n"
971                "}",
972                AllowsMergedIf);
973 
974   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
975       FormatStyle::SIS_OnlyFirstIf;
976 
977   verifyFormat("if (a) f();\n"
978                "else {\n"
979                "  g();\n"
980                "}",
981                AllowsMergedIf);
982   verifyFormat("if (a) f();\n"
983                "else {\n"
984                "  if (a) f();\n"
985                "  else {\n"
986                "    g();\n"
987                "  }\n"
988                "  g();\n"
989                "}",
990                AllowsMergedIf);
991 
992   verifyFormat("if (a) g();", AllowsMergedIf);
993   verifyFormat("if (a) {\n"
994                "  g()\n"
995                "};",
996                AllowsMergedIf);
997   verifyFormat("if (a) g();\n"
998                "else\n"
999                "  g();",
1000                AllowsMergedIf);
1001   verifyFormat("if (a) {\n"
1002                "  g();\n"
1003                "} else\n"
1004                "  g();",
1005                AllowsMergedIf);
1006   verifyFormat("if (a) g();\n"
1007                "else {\n"
1008                "  g();\n"
1009                "}",
1010                AllowsMergedIf);
1011   verifyFormat("if (a) {\n"
1012                "  g();\n"
1013                "} else {\n"
1014                "  g();\n"
1015                "}",
1016                AllowsMergedIf);
1017   verifyFormat("if (a) g();\n"
1018                "else if (b)\n"
1019                "  g();\n"
1020                "else\n"
1021                "  g();",
1022                AllowsMergedIf);
1023   verifyFormat("if (a) {\n"
1024                "  g();\n"
1025                "} else if (b)\n"
1026                "  g();\n"
1027                "else\n"
1028                "  g();",
1029                AllowsMergedIf);
1030   verifyFormat("if (a) g();\n"
1031                "else if (b) {\n"
1032                "  g();\n"
1033                "} else\n"
1034                "  g();",
1035                AllowsMergedIf);
1036   verifyFormat("if (a) g();\n"
1037                "else if (b)\n"
1038                "  g();\n"
1039                "else {\n"
1040                "  g();\n"
1041                "}",
1042                AllowsMergedIf);
1043   verifyFormat("if (a) g();\n"
1044                "else if (b) {\n"
1045                "  g();\n"
1046                "} else {\n"
1047                "  g();\n"
1048                "}",
1049                AllowsMergedIf);
1050   verifyFormat("if (a) {\n"
1051                "  g();\n"
1052                "} else if (b) {\n"
1053                "  g();\n"
1054                "} else {\n"
1055                "  g();\n"
1056                "}",
1057                AllowsMergedIf);
1058   verifyFormat("MYIF (a) f();\n"
1059                "else {\n"
1060                "  g();\n"
1061                "}",
1062                AllowsMergedIf);
1063   verifyFormat("MYIF (a) f();\n"
1064                "else {\n"
1065                "  if (a) f();\n"
1066                "  else {\n"
1067                "    g();\n"
1068                "  }\n"
1069                "  g();\n"
1070                "}",
1071                AllowsMergedIf);
1072 
1073   verifyFormat("MYIF (a) g();", AllowsMergedIf);
1074   verifyFormat("MYIF (a) {\n"
1075                "  g()\n"
1076                "};",
1077                AllowsMergedIf);
1078   verifyFormat("MYIF (a) g();\n"
1079                "else\n"
1080                "  g();",
1081                AllowsMergedIf);
1082   verifyFormat("MYIF (a) {\n"
1083                "  g();\n"
1084                "} else\n"
1085                "  g();",
1086                AllowsMergedIf);
1087   verifyFormat("MYIF (a) g();\n"
1088                "else {\n"
1089                "  g();\n"
1090                "}",
1091                AllowsMergedIf);
1092   verifyFormat("MYIF (a) {\n"
1093                "  g();\n"
1094                "} else {\n"
1095                "  g();\n"
1096                "}",
1097                AllowsMergedIf);
1098   verifyFormat("MYIF (a) g();\n"
1099                "else MYIF (b)\n"
1100                "  g();\n"
1101                "else\n"
1102                "  g();",
1103                AllowsMergedIf);
1104   verifyFormat("MYIF (a) g();\n"
1105                "else if (b)\n"
1106                "  g();\n"
1107                "else\n"
1108                "  g();",
1109                AllowsMergedIf);
1110   verifyFormat("MYIF (a) {\n"
1111                "  g();\n"
1112                "} else MYIF (b)\n"
1113                "  g();\n"
1114                "else\n"
1115                "  g();",
1116                AllowsMergedIf);
1117   verifyFormat("MYIF (a) {\n"
1118                "  g();\n"
1119                "} else if (b)\n"
1120                "  g();\n"
1121                "else\n"
1122                "  g();",
1123                AllowsMergedIf);
1124   verifyFormat("MYIF (a) g();\n"
1125                "else MYIF (b) {\n"
1126                "  g();\n"
1127                "} else\n"
1128                "  g();",
1129                AllowsMergedIf);
1130   verifyFormat("MYIF (a) g();\n"
1131                "else if (b) {\n"
1132                "  g();\n"
1133                "} else\n"
1134                "  g();",
1135                AllowsMergedIf);
1136   verifyFormat("MYIF (a) g();\n"
1137                "else MYIF (b)\n"
1138                "  g();\n"
1139                "else {\n"
1140                "  g();\n"
1141                "}",
1142                AllowsMergedIf);
1143   verifyFormat("MYIF (a) g();\n"
1144                "else if (b)\n"
1145                "  g();\n"
1146                "else {\n"
1147                "  g();\n"
1148                "}",
1149                AllowsMergedIf);
1150   verifyFormat("MYIF (a) g();\n"
1151                "else MYIF (b) {\n"
1152                "  g();\n"
1153                "} else {\n"
1154                "  g();\n"
1155                "}",
1156                AllowsMergedIf);
1157   verifyFormat("MYIF (a) g();\n"
1158                "else if (b) {\n"
1159                "  g();\n"
1160                "} else {\n"
1161                "  g();\n"
1162                "}",
1163                AllowsMergedIf);
1164   verifyFormat("MYIF (a) {\n"
1165                "  g();\n"
1166                "} else MYIF (b) {\n"
1167                "  g();\n"
1168                "} else {\n"
1169                "  g();\n"
1170                "}",
1171                AllowsMergedIf);
1172   verifyFormat("MYIF (a) {\n"
1173                "  g();\n"
1174                "} else if (b) {\n"
1175                "  g();\n"
1176                "} else {\n"
1177                "  g();\n"
1178                "}",
1179                AllowsMergedIf);
1180 
1181   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
1182       FormatStyle::SIS_AllIfsAndElse;
1183 
1184   verifyFormat("if (a) f();\n"
1185                "else {\n"
1186                "  g();\n"
1187                "}",
1188                AllowsMergedIf);
1189   verifyFormat("if (a) f();\n"
1190                "else {\n"
1191                "  if (a) f();\n"
1192                "  else {\n"
1193                "    g();\n"
1194                "  }\n"
1195                "  g();\n"
1196                "}",
1197                AllowsMergedIf);
1198 
1199   verifyFormat("if (a) g();", AllowsMergedIf);
1200   verifyFormat("if (a) {\n"
1201                "  g()\n"
1202                "};",
1203                AllowsMergedIf);
1204   verifyFormat("if (a) g();\n"
1205                "else g();",
1206                AllowsMergedIf);
1207   verifyFormat("if (a) {\n"
1208                "  g();\n"
1209                "} else g();",
1210                AllowsMergedIf);
1211   verifyFormat("if (a) g();\n"
1212                "else {\n"
1213                "  g();\n"
1214                "}",
1215                AllowsMergedIf);
1216   verifyFormat("if (a) {\n"
1217                "  g();\n"
1218                "} else {\n"
1219                "  g();\n"
1220                "}",
1221                AllowsMergedIf);
1222   verifyFormat("if (a) g();\n"
1223                "else if (b) g();\n"
1224                "else g();",
1225                AllowsMergedIf);
1226   verifyFormat("if (a) {\n"
1227                "  g();\n"
1228                "} else if (b) g();\n"
1229                "else g();",
1230                AllowsMergedIf);
1231   verifyFormat("if (a) g();\n"
1232                "else if (b) {\n"
1233                "  g();\n"
1234                "} else g();",
1235                AllowsMergedIf);
1236   verifyFormat("if (a) g();\n"
1237                "else if (b) g();\n"
1238                "else {\n"
1239                "  g();\n"
1240                "}",
1241                AllowsMergedIf);
1242   verifyFormat("if (a) g();\n"
1243                "else if (b) {\n"
1244                "  g();\n"
1245                "} else {\n"
1246                "  g();\n"
1247                "}",
1248                AllowsMergedIf);
1249   verifyFormat("if (a) {\n"
1250                "  g();\n"
1251                "} else if (b) {\n"
1252                "  g();\n"
1253                "} else {\n"
1254                "  g();\n"
1255                "}",
1256                AllowsMergedIf);
1257   verifyFormat("MYIF (a) f();\n"
1258                "else {\n"
1259                "  g();\n"
1260                "}",
1261                AllowsMergedIf);
1262   verifyFormat("MYIF (a) f();\n"
1263                "else {\n"
1264                "  if (a) f();\n"
1265                "  else {\n"
1266                "    g();\n"
1267                "  }\n"
1268                "  g();\n"
1269                "}",
1270                AllowsMergedIf);
1271 
1272   verifyFormat("MYIF (a) g();", AllowsMergedIf);
1273   verifyFormat("MYIF (a) {\n"
1274                "  g()\n"
1275                "};",
1276                AllowsMergedIf);
1277   verifyFormat("MYIF (a) g();\n"
1278                "else g();",
1279                AllowsMergedIf);
1280   verifyFormat("MYIF (a) {\n"
1281                "  g();\n"
1282                "} else g();",
1283                AllowsMergedIf);
1284   verifyFormat("MYIF (a) g();\n"
1285                "else {\n"
1286                "  g();\n"
1287                "}",
1288                AllowsMergedIf);
1289   verifyFormat("MYIF (a) {\n"
1290                "  g();\n"
1291                "} else {\n"
1292                "  g();\n"
1293                "}",
1294                AllowsMergedIf);
1295   verifyFormat("MYIF (a) g();\n"
1296                "else MYIF (b) g();\n"
1297                "else g();",
1298                AllowsMergedIf);
1299   verifyFormat("MYIF (a) g();\n"
1300                "else if (b) g();\n"
1301                "else g();",
1302                AllowsMergedIf);
1303   verifyFormat("MYIF (a) {\n"
1304                "  g();\n"
1305                "} else MYIF (b) g();\n"
1306                "else g();",
1307                AllowsMergedIf);
1308   verifyFormat("MYIF (a) {\n"
1309                "  g();\n"
1310                "} else if (b) g();\n"
1311                "else g();",
1312                AllowsMergedIf);
1313   verifyFormat("MYIF (a) g();\n"
1314                "else MYIF (b) {\n"
1315                "  g();\n"
1316                "} else g();",
1317                AllowsMergedIf);
1318   verifyFormat("MYIF (a) g();\n"
1319                "else if (b) {\n"
1320                "  g();\n"
1321                "} else g();",
1322                AllowsMergedIf);
1323   verifyFormat("MYIF (a) g();\n"
1324                "else MYIF (b) g();\n"
1325                "else {\n"
1326                "  g();\n"
1327                "}",
1328                AllowsMergedIf);
1329   verifyFormat("MYIF (a) g();\n"
1330                "else if (b) g();\n"
1331                "else {\n"
1332                "  g();\n"
1333                "}",
1334                AllowsMergedIf);
1335   verifyFormat("MYIF (a) g();\n"
1336                "else MYIF (b) {\n"
1337                "  g();\n"
1338                "} else {\n"
1339                "  g();\n"
1340                "}",
1341                AllowsMergedIf);
1342   verifyFormat("MYIF (a) g();\n"
1343                "else if (b) {\n"
1344                "  g();\n"
1345                "} else {\n"
1346                "  g();\n"
1347                "}",
1348                AllowsMergedIf);
1349   verifyFormat("MYIF (a) {\n"
1350                "  g();\n"
1351                "} else MYIF (b) {\n"
1352                "  g();\n"
1353                "} else {\n"
1354                "  g();\n"
1355                "}",
1356                AllowsMergedIf);
1357   verifyFormat("MYIF (a) {\n"
1358                "  g();\n"
1359                "} else if (b) {\n"
1360                "  g();\n"
1361                "} else {\n"
1362                "  g();\n"
1363                "}",
1364                AllowsMergedIf);
1365 }
1366 
1367 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
1368   verifyFormat("while (true)\n"
1369                "  ;");
1370   verifyFormat("for (;;)\n"
1371                "  ;");
1372 
1373   FormatStyle AllowsMergedLoops = getLLVMStyle();
1374   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
1375 
1376   verifyFormat("while (true) continue;", AllowsMergedLoops);
1377   verifyFormat("for (;;) continue;", AllowsMergedLoops);
1378   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
1379   verifyFormat("BOOST_FOREACH (int &v, vec) v *= 2;", AllowsMergedLoops);
1380   verifyFormat("while (true);", AllowsMergedLoops);
1381   verifyFormat("for (;;);", AllowsMergedLoops);
1382   verifyFormat("for (;;)\n"
1383                "  for (;;) continue;",
1384                AllowsMergedLoops);
1385   verifyFormat("for (;;)\n"
1386                "  while (true) continue;",
1387                AllowsMergedLoops);
1388   verifyFormat("while (true)\n"
1389                "  for (;;) continue;",
1390                AllowsMergedLoops);
1391   verifyFormat("BOOST_FOREACH (int &v, vec)\n"
1392                "  for (;;) continue;",
1393                AllowsMergedLoops);
1394   verifyFormat("for (;;)\n"
1395                "  BOOST_FOREACH (int &v, vec) continue;",
1396                AllowsMergedLoops);
1397   verifyFormat("for (;;) // Can't merge this\n"
1398                "  continue;",
1399                AllowsMergedLoops);
1400   verifyFormat("for (;;) /* still don't merge */\n"
1401                "  continue;",
1402                AllowsMergedLoops);
1403   verifyFormat("do a++;\n"
1404                "while (true);",
1405                AllowsMergedLoops);
1406   verifyFormat("do /* Don't merge */\n"
1407                "  a++;\n"
1408                "while (true);",
1409                AllowsMergedLoops);
1410   verifyFormat("do // Don't merge\n"
1411                "  a++;\n"
1412                "while (true);",
1413                AllowsMergedLoops);
1414   verifyFormat("do\n"
1415                "  // Don't merge\n"
1416                "  a++;\n"
1417                "while (true);",
1418                AllowsMergedLoops);
1419 
1420   // Without braces labels are interpreted differently.
1421   verifyFormat("{\n"
1422                "  do\n"
1423                "  label:\n"
1424                "    a++;\n"
1425                "  while (true);\n"
1426                "}",
1427                AllowsMergedLoops);
1428 
1429   // Don't merge if there are comments before the null statement.
1430   verifyFormat("while (1) //\n"
1431                "  ;",
1432                AllowsMergedLoops);
1433   verifyFormat("for (;;) /**/\n"
1434                "  ;",
1435                AllowsMergedLoops);
1436   verifyFormat("while (true) /**/\n"
1437                "  ;",
1438                "while (true) /**/;", AllowsMergedLoops);
1439 }
1440 
1441 TEST_F(FormatTest, FormatShortBracedStatements) {
1442   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
1443   EXPECT_EQ(AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine, false);
1444   EXPECT_EQ(AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine,
1445             FormatStyle::SIS_Never);
1446   EXPECT_EQ(AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine, false);
1447   EXPECT_EQ(AllowSimpleBracedStatements.BraceWrapping.AfterFunction, false);
1448   verifyFormat("for (;;) {\n"
1449                "  f();\n"
1450                "}");
1451   verifyFormat("/*comment*/ for (;;) {\n"
1452                "  f();\n"
1453                "}");
1454   verifyFormat("BOOST_FOREACH (int v, vec) {\n"
1455                "  f();\n"
1456                "}");
1457   verifyFormat("/*comment*/ BOOST_FOREACH (int v, vec) {\n"
1458                "  f();\n"
1459                "}");
1460   verifyFormat("while (true) {\n"
1461                "  f();\n"
1462                "}");
1463   verifyFormat("/*comment*/ while (true) {\n"
1464                "  f();\n"
1465                "}");
1466   verifyFormat("if (true) {\n"
1467                "  f();\n"
1468                "}");
1469   verifyFormat("/*comment*/ if (true) {\n"
1470                "  f();\n"
1471                "}");
1472 
1473   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
1474       FormatStyle::SBS_Empty;
1475   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1476       FormatStyle::SIS_WithoutElse;
1477   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1478   verifyFormat("if (i) break;", AllowSimpleBracedStatements);
1479   verifyFormat("if (i > 0) {\n"
1480                "  return i;\n"
1481                "}",
1482                AllowSimpleBracedStatements);
1483 
1484   AllowSimpleBracedStatements.IfMacros.push_back("MYIF");
1485   // Where line-lengths matter, a 2-letter synonym that maintains line length.
1486   // Not IF to avoid any confusion that IF is somehow special.
1487   AllowSimpleBracedStatements.IfMacros.push_back("FI");
1488   AllowSimpleBracedStatements.ColumnLimit = 40;
1489   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
1490       FormatStyle::SBS_Always;
1491   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1492   AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
1493   AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
1494   AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
1495 
1496   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1497   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1498   verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1499   verifyFormat("if consteval {}", AllowSimpleBracedStatements);
1500   verifyFormat("if !consteval {}", AllowSimpleBracedStatements);
1501   verifyFormat("if CONSTEVAL {}", AllowSimpleBracedStatements);
1502   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1503   verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1504   verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1505   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1506   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1507   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1508   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1509   verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1510   verifyFormat("if consteval { f(); }", AllowSimpleBracedStatements);
1511   verifyFormat("if CONSTEVAL { f(); }", AllowSimpleBracedStatements);
1512   verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1513   verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1514   verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1515   verifyFormat("MYIF consteval { f(); }", AllowSimpleBracedStatements);
1516   verifyFormat("MYIF CONSTEVAL { f(); }", AllowSimpleBracedStatements);
1517   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1518   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1519   verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1520                AllowSimpleBracedStatements);
1521   verifyFormat("if (true) {\n"
1522                "  ffffffffffffffffffffffff();\n"
1523                "}",
1524                AllowSimpleBracedStatements);
1525   verifyFormat("if (true) {\n"
1526                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1527                "}",
1528                AllowSimpleBracedStatements);
1529   verifyFormat("if (true) { //\n"
1530                "  f();\n"
1531                "}",
1532                AllowSimpleBracedStatements);
1533   verifyFormat("if (true) {\n"
1534                "  f();\n"
1535                "  f();\n"
1536                "}",
1537                AllowSimpleBracedStatements);
1538   verifyFormat("if (true) {\n"
1539                "  f();\n"
1540                "} else {\n"
1541                "  f();\n"
1542                "}",
1543                AllowSimpleBracedStatements);
1544   verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1545                AllowSimpleBracedStatements);
1546   verifyFormat("MYIF (true) {\n"
1547                "  ffffffffffffffffffffffff();\n"
1548                "}",
1549                AllowSimpleBracedStatements);
1550   verifyFormat("MYIF (true) {\n"
1551                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1552                "}",
1553                AllowSimpleBracedStatements);
1554   verifyFormat("MYIF (true) { //\n"
1555                "  f();\n"
1556                "}",
1557                AllowSimpleBracedStatements);
1558   verifyFormat("MYIF (true) {\n"
1559                "  f();\n"
1560                "  f();\n"
1561                "}",
1562                AllowSimpleBracedStatements);
1563   verifyFormat("MYIF (true) {\n"
1564                "  f();\n"
1565                "} else {\n"
1566                "  f();\n"
1567                "}",
1568                AllowSimpleBracedStatements);
1569 
1570   verifyFormat("struct A2 {\n"
1571                "  int X;\n"
1572                "};",
1573                AllowSimpleBracedStatements);
1574   verifyFormat("typedef struct A2 {\n"
1575                "  int X;\n"
1576                "} A2_t;",
1577                AllowSimpleBracedStatements);
1578   verifyFormat("template <int> struct A2 {\n"
1579                "  struct B {};\n"
1580                "};",
1581                AllowSimpleBracedStatements);
1582 
1583   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1584       FormatStyle::SIS_Never;
1585   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1586   verifyFormat("if (true) {\n"
1587                "  f();\n"
1588                "}",
1589                AllowSimpleBracedStatements);
1590   verifyFormat("if (true) {\n"
1591                "  f();\n"
1592                "} else {\n"
1593                "  f();\n"
1594                "}",
1595                AllowSimpleBracedStatements);
1596   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1597   verifyFormat("MYIF (true) {\n"
1598                "  f();\n"
1599                "}",
1600                AllowSimpleBracedStatements);
1601   verifyFormat("MYIF (true) {\n"
1602                "  f();\n"
1603                "} else {\n"
1604                "  f();\n"
1605                "}",
1606                AllowSimpleBracedStatements);
1607 
1608   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1609   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1610   verifyFormat("while (true) {\n"
1611                "  f();\n"
1612                "}",
1613                AllowSimpleBracedStatements);
1614   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1615   verifyFormat("for (;;) {\n"
1616                "  f();\n"
1617                "}",
1618                AllowSimpleBracedStatements);
1619   verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements);
1620   verifyFormat("BOOST_FOREACH (int v, vec) {\n"
1621                "  f();\n"
1622                "}",
1623                AllowSimpleBracedStatements);
1624 
1625   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1626       FormatStyle::SIS_WithoutElse;
1627   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1628   AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement =
1629       FormatStyle::BWACS_Always;
1630 
1631   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1632   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1633   verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1634   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1635   verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1636   verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1637   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1638   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1639   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1640   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1641   verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1642   verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1643   verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1644   verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1645   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1646   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1647   verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1648                AllowSimpleBracedStatements);
1649   verifyFormat("if (true)\n"
1650                "{\n"
1651                "  ffffffffffffffffffffffff();\n"
1652                "}",
1653                AllowSimpleBracedStatements);
1654   verifyFormat("if (true)\n"
1655                "{\n"
1656                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1657                "}",
1658                AllowSimpleBracedStatements);
1659   verifyFormat("if (true)\n"
1660                "{ //\n"
1661                "  f();\n"
1662                "}",
1663                AllowSimpleBracedStatements);
1664   verifyFormat("if (true)\n"
1665                "{\n"
1666                "  f();\n"
1667                "  f();\n"
1668                "}",
1669                AllowSimpleBracedStatements);
1670   verifyFormat("if (true)\n"
1671                "{\n"
1672                "  f();\n"
1673                "} else\n"
1674                "{\n"
1675                "  f();\n"
1676                "}",
1677                AllowSimpleBracedStatements);
1678   verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1679                AllowSimpleBracedStatements);
1680   verifyFormat("MYIF (true)\n"
1681                "{\n"
1682                "  ffffffffffffffffffffffff();\n"
1683                "}",
1684                AllowSimpleBracedStatements);
1685   verifyFormat("MYIF (true)\n"
1686                "{\n"
1687                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1688                "}",
1689                AllowSimpleBracedStatements);
1690   verifyFormat("MYIF (true)\n"
1691                "{ //\n"
1692                "  f();\n"
1693                "}",
1694                AllowSimpleBracedStatements);
1695   verifyFormat("MYIF (true)\n"
1696                "{\n"
1697                "  f();\n"
1698                "  f();\n"
1699                "}",
1700                AllowSimpleBracedStatements);
1701   verifyFormat("MYIF (true)\n"
1702                "{\n"
1703                "  f();\n"
1704                "} else\n"
1705                "{\n"
1706                "  f();\n"
1707                "}",
1708                AllowSimpleBracedStatements);
1709 
1710   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1711       FormatStyle::SIS_Never;
1712   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1713   verifyFormat("if (true)\n"
1714                "{\n"
1715                "  f();\n"
1716                "}",
1717                AllowSimpleBracedStatements);
1718   verifyFormat("if (true)\n"
1719                "{\n"
1720                "  f();\n"
1721                "} else\n"
1722                "{\n"
1723                "  f();\n"
1724                "}",
1725                AllowSimpleBracedStatements);
1726   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1727   verifyFormat("MYIF (true)\n"
1728                "{\n"
1729                "  f();\n"
1730                "}",
1731                AllowSimpleBracedStatements);
1732   verifyFormat("MYIF (true)\n"
1733                "{\n"
1734                "  f();\n"
1735                "} else\n"
1736                "{\n"
1737                "  f();\n"
1738                "}",
1739                AllowSimpleBracedStatements);
1740 
1741   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1742   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1743   verifyFormat("while (true)\n"
1744                "{\n"
1745                "  f();\n"
1746                "}",
1747                AllowSimpleBracedStatements);
1748   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1749   verifyFormat("for (;;)\n"
1750                "{\n"
1751                "  f();\n"
1752                "}",
1753                AllowSimpleBracedStatements);
1754   verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements);
1755   verifyFormat("BOOST_FOREACH (int v, vec)\n"
1756                "{\n"
1757                "  f();\n"
1758                "}",
1759                AllowSimpleBracedStatements);
1760 
1761   FormatStyle Style = getLLVMStyle();
1762   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1763   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
1764 
1765   verifyFormat("while (i > 0)\n"
1766                "{\n"
1767                "  --i;\n"
1768                "}",
1769                Style);
1770 
1771   verifyFormat("if (a)\n"
1772                "{\n"
1773                "  ++b;\n"
1774                "}",
1775                Style);
1776 
1777   verifyFormat("if (a)\n"
1778                "{\n"
1779                "  b = 1;\n"
1780                "} else\n"
1781                "{\n"
1782                "  b = 0;\n"
1783                "}",
1784                Style);
1785 
1786   verifyFormat("if (a)\n"
1787                "{\n"
1788                "  b = 1;\n"
1789                "} else if (c)\n"
1790                "{\n"
1791                "  b = 2;\n"
1792                "} else\n"
1793                "{\n"
1794                "  b = 0;\n"
1795                "}",
1796                Style);
1797 
1798   Style.BraceWrapping.BeforeElse = true;
1799 
1800   verifyFormat("if (a)\n"
1801                "{\n"
1802                "  b = 1;\n"
1803                "}\n"
1804                "else\n"
1805                "{\n"
1806                "  b = 0;\n"
1807                "}",
1808                Style);
1809 
1810   verifyFormat("if (a)\n"
1811                "{\n"
1812                "  b = 1;\n"
1813                "}\n"
1814                "else if (c)\n"
1815                "{\n"
1816                "  b = 2;\n"
1817                "}\n"
1818                "else\n"
1819                "{\n"
1820                "  b = 0;\n"
1821                "}",
1822                Style);
1823 }
1824 
1825 TEST_F(FormatTest, UnderstandsMacros) {
1826   verifyFormat("#define A (parentheses)");
1827   verifyFormat("/* comment */ #define A (parentheses)");
1828   verifyFormat("/* comment */ /* another comment */ #define A (parentheses)");
1829   // Even the partial code should never be merged.
1830   verifyNoChange("/* comment */ #define A (parentheses)\n"
1831                  "#");
1832   verifyFormat("/* comment */ #define A (parentheses)\n"
1833                "#\n");
1834   verifyFormat("/* comment */ #define A (parentheses)\n"
1835                "#define B (parentheses)");
1836   verifyFormat("#define true ((int)1)");
1837   verifyFormat("#define and(x)");
1838   verifyFormat("#define if(x) x");
1839   verifyFormat("#define return(x) (x)");
1840   verifyFormat("#define while(x) for (; x;)");
1841   verifyFormat("#define xor(x) (^(x))");
1842   verifyFormat("#define __except(x)");
1843   verifyFormat("#define __try(x)");
1844 
1845   // https://llvm.org/PR54348.
1846   verifyFormat(
1847       "#define A"
1848       "                                                                      "
1849       "\\\n"
1850       "  class & {}");
1851 
1852   FormatStyle Style = getLLVMStyle();
1853   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1854   Style.BraceWrapping.AfterFunction = true;
1855   // Test that a macro definition never gets merged with the following
1856   // definition.
1857   // FIXME: The AAA macro definition probably should not be split into 3 lines.
1858   verifyFormat("#define AAA                                                    "
1859                "                \\\n"
1860                "  N                                                            "
1861                "                \\\n"
1862                "  {\n"
1863                "#define BBB }",
1864                Style);
1865   // verifyFormat("#define AAA N { //", Style);
1866 
1867   verifyFormat("MACRO(return)");
1868   verifyFormat("MACRO(co_await)");
1869   verifyFormat("MACRO(co_return)");
1870   verifyFormat("MACRO(co_yield)");
1871   verifyFormat("MACRO(return, something)");
1872   verifyFormat("MACRO(co_return, something)");
1873   verifyFormat("MACRO(something##something)");
1874   verifyFormat("MACRO(return##something)");
1875   verifyFormat("MACRO(co_return##something)");
1876 
1877   verifyFormat("#define A x:");
1878 
1879   verifyFormat("#define Foo(Bar) {#Bar}", "#define Foo(Bar) \\\n"
1880                                           "  { \\\n"
1881                                           "    #Bar \\\n"
1882                                           "  }");
1883   verifyFormat("#define Foo(Bar) {#Bar}", "#define Foo(Bar) \\\n"
1884                                           "  { #Bar }");
1885 }
1886 
1887 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
1888   FormatStyle Style = getLLVMStyleWithColumns(60);
1889   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
1890   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
1891   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1892   verifyFormat("#define A                                                  \\\n"
1893                "  if (HANDLEwernufrnuLwrmviferuvnierv)                     \\\n"
1894                "  {                                                        \\\n"
1895                "    RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier;               \\\n"
1896                "  }\n"
1897                "X;",
1898                "#define A \\\n"
1899                "   if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
1900                "      RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1901                "   }\n"
1902                "X;",
1903                Style);
1904 }
1905 
1906 TEST_F(FormatTest, ParseIfElse) {
1907   verifyFormat("if (true)\n"
1908                "  if (true)\n"
1909                "    if (true)\n"
1910                "      f();\n"
1911                "    else\n"
1912                "      g();\n"
1913                "  else\n"
1914                "    h();\n"
1915                "else\n"
1916                "  i();");
1917   verifyFormat("if (true)\n"
1918                "  if (true)\n"
1919                "    if (true) {\n"
1920                "      if (true)\n"
1921                "        f();\n"
1922                "    } else {\n"
1923                "      g();\n"
1924                "    }\n"
1925                "  else\n"
1926                "    h();\n"
1927                "else {\n"
1928                "  i();\n"
1929                "}");
1930   verifyFormat("if (true)\n"
1931                "  if constexpr (true)\n"
1932                "    if (true) {\n"
1933                "      if constexpr (true)\n"
1934                "        f();\n"
1935                "    } else {\n"
1936                "      g();\n"
1937                "    }\n"
1938                "  else\n"
1939                "    h();\n"
1940                "else {\n"
1941                "  i();\n"
1942                "}");
1943   verifyFormat("if (true)\n"
1944                "  if CONSTEXPR (true)\n"
1945                "    if (true) {\n"
1946                "      if CONSTEXPR (true)\n"
1947                "        f();\n"
1948                "    } else {\n"
1949                "      g();\n"
1950                "    }\n"
1951                "  else\n"
1952                "    h();\n"
1953                "else {\n"
1954                "  i();\n"
1955                "}");
1956   verifyFormat("void f() {\n"
1957                "  if (a) {\n"
1958                "  } else {\n"
1959                "  }\n"
1960                "}");
1961 }
1962 
1963 TEST_F(FormatTest, ElseIf) {
1964   verifyFormat("if (a) {\n} else if (b) {\n}");
1965   verifyFormat("if (a)\n"
1966                "  f();\n"
1967                "else if (b)\n"
1968                "  g();\n"
1969                "else\n"
1970                "  h();");
1971   verifyFormat("if (a)\n"
1972                "  f();\n"
1973                "else // comment\n"
1974                "  if (b) {\n"
1975                "    g();\n"
1976                "    h();\n"
1977                "  }");
1978   verifyFormat("if constexpr (a)\n"
1979                "  f();\n"
1980                "else if constexpr (b)\n"
1981                "  g();\n"
1982                "else\n"
1983                "  h();");
1984   verifyFormat("if CONSTEXPR (a)\n"
1985                "  f();\n"
1986                "else if CONSTEXPR (b)\n"
1987                "  g();\n"
1988                "else\n"
1989                "  h();");
1990   verifyFormat("if (a) {\n"
1991                "  f();\n"
1992                "}\n"
1993                "// or else ..\n"
1994                "else {\n"
1995                "  g()\n"
1996                "}");
1997 
1998   verifyFormat("if (a) {\n"
1999                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2000                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
2001                "}");
2002   verifyFormat("if (a) {\n"
2003                "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2004                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
2005                "}");
2006   verifyFormat("if (a) {\n"
2007                "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2008                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
2009                "}");
2010   verifyFormat("if (a) {\n"
2011                "} else if (\n"
2012                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2013                "}",
2014                getLLVMStyleWithColumns(62));
2015   verifyFormat("if (a) {\n"
2016                "} else if constexpr (\n"
2017                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2018                "}",
2019                getLLVMStyleWithColumns(62));
2020   verifyFormat("if (a) {\n"
2021                "} else if CONSTEXPR (\n"
2022                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2023                "}",
2024                getLLVMStyleWithColumns(62));
2025 }
2026 
2027 TEST_F(FormatTest, SeparatePointerReferenceAlignment) {
2028   FormatStyle Style = getLLVMStyle();
2029   EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
2030   EXPECT_EQ(Style.ReferenceAlignment, FormatStyle::RAS_Pointer);
2031   verifyFormat("int *f1(int *a, int &b, int &&c);", Style);
2032   verifyFormat("int &f2(int &&c, int *a, int &b);", Style);
2033   verifyFormat("int &&f3(int &b, int &&c, int *a);", Style);
2034   verifyFormat("int *f1(int &a) const &;", Style);
2035   verifyFormat("int *f1(int &a) const & = 0;", Style);
2036   verifyFormat("int *a = f1();", Style);
2037   verifyFormat("int &b = f2();", Style);
2038   verifyFormat("int &&c = f3();", Style);
2039   verifyFormat("int f3() { return sizeof(Foo &); }", Style);
2040   verifyFormat("int f4() { return sizeof(Foo &&); }", Style);
2041   verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style);
2042   verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style);
2043   verifyFormat("for (auto a = 0, b = 0; const auto &c : {1, 2, 3})", Style);
2044   verifyFormat("for (auto a = 0, b = 0; const int &c : {1, 2, 3})", Style);
2045   verifyFormat("for (auto a = 0, b = 0; const Foo &c : {1, 2, 3})", Style);
2046   verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2047   verifyFormat("for (int a = 0, b = 0; const auto &c : {1, 2, 3})", Style);
2048   verifyFormat("for (int a = 0, b = 0; const int &c : {1, 2, 3})", Style);
2049   verifyFormat("for (int a = 0, b = 0; const Foo &c : {1, 2, 3})", Style);
2050   verifyFormat("for (int a = 0, b++; const auto &c : {1, 2, 3})", Style);
2051   verifyFormat("for (int a = 0, b++; const int &c : {1, 2, 3})", Style);
2052   verifyFormat("for (int a = 0, b++; const Foo &c : {1, 2, 3})", Style);
2053   verifyFormat("for (auto x = 0; auto &c : {1, 2, 3})", Style);
2054   verifyFormat("for (auto x = 0; int &c : {1, 2, 3})", Style);
2055   verifyFormat("for (int x = 0; auto &c : {1, 2, 3})", Style);
2056   verifyFormat("for (int x = 0; int &c : {1, 2, 3})", Style);
2057   verifyFormat("for (f(); auto &c : {1, 2, 3})", Style);
2058   verifyFormat("for (f(); int &c : {1, 2, 3})", Style);
2059   verifyFormat(
2060       "function<int(int &)> res1 = [](int &a) { return 0000000000000; },\n"
2061       "                     res2 = [](int &a) { return 0000000000000; };",
2062       Style);
2063 
2064   Style.AlignConsecutiveDeclarations.Enabled = true;
2065   Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
2066   verifyFormat("Const unsigned int *c;\n"
2067                "const unsigned int *d;\n"
2068                "Const unsigned int &e;\n"
2069                "const unsigned int &f;\n"
2070                "int                *f1(int *a, int &b, int &&c);\n"
2071                "double             *(*f2)(int *a, double &&b);\n"
2072                "const unsigned    &&g;\n"
2073                "Const unsigned      h;",
2074                Style);
2075   Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false;
2076   verifyFormat("Const unsigned int *c;\n"
2077                "const unsigned int *d;\n"
2078                "Const unsigned int &e;\n"
2079                "const unsigned int &f;\n"
2080                "int                *f1(int *a, int &b, int &&c);\n"
2081                "double *(*f2)(int *a, double &&b);\n"
2082                "const unsigned &&g;\n"
2083                "Const unsigned   h;",
2084                Style);
2085 
2086   Style.PointerAlignment = FormatStyle::PAS_Left;
2087   Style.ReferenceAlignment = FormatStyle::RAS_Pointer;
2088   verifyFormat("int* f1(int* a, int& b, int&& c);", Style);
2089   verifyFormat("int& f2(int&& c, int* a, int& b);", Style);
2090   verifyFormat("int&& f3(int& b, int&& c, int* a);", Style);
2091   verifyFormat("int* f1(int& a) const& = 0;", Style);
2092   verifyFormat("int* a = f1();", Style);
2093   verifyFormat("int& b = f2();", Style);
2094   verifyFormat("int&& c = f3();", Style);
2095   verifyFormat("int f3() { return sizeof(Foo&); }", Style);
2096   verifyFormat("int f4() { return sizeof(Foo&&); }", Style);
2097   verifyFormat("void f5() { int f6(Foo&, Bar&); }", Style);
2098   verifyFormat("void f5() { int f6(Foo&&, Bar&&); }", Style);
2099   verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
2100   verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style);
2101   verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style);
2102   verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2103   verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
2104   verifyFormat("for (int a = 0, b = 0; const int& c : {1, 2, 3})", Style);
2105   verifyFormat("for (int a = 0, b = 0; const Foo& c : {1, 2, 3})", Style);
2106   verifyFormat("for (int a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2107   verifyFormat("for (int a = 0, b++; const auto& c : {1, 2, 3})", Style);
2108   verifyFormat("for (int a = 0, b++; const int& c : {1, 2, 3})", Style);
2109   verifyFormat("for (int a = 0, b++; const Foo& c : {1, 2, 3})", Style);
2110   verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
2111   verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style);
2112   verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style);
2113   verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style);
2114   verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style);
2115   verifyFormat("for (f(); auto& c : {1, 2, 3})", Style);
2116   verifyFormat("for (f(); int& c : {1, 2, 3})", Style);
2117   verifyFormat(
2118       "function<int(int&)> res1 = [](int& a) { return 0000000000000; },\n"
2119       "                    res2 = [](int& a) { return 0000000000000; };",
2120       Style);
2121 
2122   Style.AlignConsecutiveDeclarations.Enabled = true;
2123   Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
2124   verifyFormat("Const unsigned int* c;\n"
2125                "const unsigned int* d;\n"
2126                "Const unsigned int& e;\n"
2127                "const unsigned int& f;\n"
2128                "int*                f1(int* a, int& b, int&& c);\n"
2129                "double*             (*f2)(int* a, double&& b);\n"
2130                "const unsigned&&    g;\n"
2131                "Const unsigned      h;",
2132                Style);
2133   Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false;
2134   verifyFormat("Const unsigned int* c;\n"
2135                "const unsigned int* d;\n"
2136                "Const unsigned int& e;\n"
2137                "const unsigned int& f;\n"
2138                "int*                f1(int* a, int& b, int&& c);\n"
2139                "double* (*f2)(int* a, double&& b);\n"
2140                "const unsigned&& g;\n"
2141                "Const unsigned   h;",
2142                Style);
2143 
2144   Style.PointerAlignment = FormatStyle::PAS_Right;
2145   Style.ReferenceAlignment = FormatStyle::RAS_Left;
2146   verifyFormat("int *f1(int *a, int& b, int&& c);", Style);
2147   verifyFormat("int& f2(int&& c, int *a, int& b);", Style);
2148   verifyFormat("int&& f3(int& b, int&& c, int *a);", Style);
2149   verifyFormat("int *a = f1();", Style);
2150   verifyFormat("int& b = f2();", Style);
2151   verifyFormat("int&& c = f3();", Style);
2152   verifyFormat("int f3() { return sizeof(Foo&); }", Style);
2153   verifyFormat("int f4() { return sizeof(Foo&&); }", Style);
2154   verifyFormat("void f5() { int f6(Foo&, Bar&); }", Style);
2155   verifyFormat("void f5() { int f6(Foo&&, Bar&&); }", Style);
2156   verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2157   verifyFormat("for (int a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2158   verifyFormat("for (int a = 0, b++; const Foo *c : {1, 2, 3})", Style);
2159 
2160   Style.AlignConsecutiveDeclarations.Enabled = true;
2161   Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
2162   verifyFormat("Const unsigned int *c;\n"
2163                "const unsigned int *d;\n"
2164                "Const unsigned int& e;\n"
2165                "const unsigned int& f;\n"
2166                "int                *f1(int *a, int& b, int&& c);\n"
2167                "double             *(*f2)(int *a, double&& b);\n"
2168                "const unsigned&&    g;\n"
2169                "Const unsigned      h;",
2170                Style);
2171   Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false;
2172   verifyFormat("Const unsigned int *c;\n"
2173                "const unsigned int *d;\n"
2174                "Const unsigned int& e;\n"
2175                "const unsigned int& f;\n"
2176                "int                *f1(int *a, int& b, int&& c);\n"
2177                "double *(*f2)(int *a, double&& b);\n"
2178                "const unsigned&& g;\n"
2179                "Const unsigned   h;",
2180                Style);
2181 
2182   Style.PointerAlignment = FormatStyle::PAS_Left;
2183   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
2184   verifyFormat("int* f1(int* a, int & b, int && c);", Style);
2185   verifyFormat("int & f2(int && c, int* a, int & b);", Style);
2186   verifyFormat("int && f3(int & b, int && c, int* a);", Style);
2187   verifyFormat("int* a = f1();", Style);
2188   verifyFormat("int & b = f2();", Style);
2189   verifyFormat("int && c = f3();", Style);
2190   verifyFormat("int f3() { return sizeof(Foo &); }", Style);
2191   verifyFormat("int f4() { return sizeof(Foo &&); }", Style);
2192   verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style);
2193   verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style);
2194   verifyFormat("for (auto a = 0, b = 0; const auto & c : {1, 2, 3})", Style);
2195   verifyFormat("for (auto a = 0, b = 0; const int & c : {1, 2, 3})", Style);
2196   verifyFormat("for (auto a = 0, b = 0; const Foo & c : {1, 2, 3})", Style);
2197   verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2198   verifyFormat("for (int a = 0, b++; const auto & c : {1, 2, 3})", Style);
2199   verifyFormat("for (int a = 0, b++; const int & c : {1, 2, 3})", Style);
2200   verifyFormat("for (int a = 0, b++; const Foo & c : {1, 2, 3})", Style);
2201   verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
2202   verifyFormat("for (auto x = 0; auto & c : {1, 2, 3})", Style);
2203   verifyFormat("for (auto x = 0; int & c : {1, 2, 3})", Style);
2204   verifyFormat("for (int x = 0; auto & c : {1, 2, 3})", Style);
2205   verifyFormat("for (int x = 0; int & c : {1, 2, 3})", Style);
2206   verifyFormat("for (f(); auto & c : {1, 2, 3})", Style);
2207   verifyFormat("for (f(); int & c : {1, 2, 3})", Style);
2208   verifyFormat(
2209       "function<int(int &)> res1 = [](int & a) { return 0000000000000; },\n"
2210       "                     res2 = [](int & a) { return 0000000000000; };",
2211       Style);
2212 
2213   Style.AlignConsecutiveDeclarations.Enabled = true;
2214   Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
2215   verifyFormat("Const unsigned int*  c;\n"
2216                "const unsigned int*  d;\n"
2217                "Const unsigned int & e;\n"
2218                "const unsigned int & f;\n"
2219                "int*                 f1(int* a, int & b, int && c);\n"
2220                "double*              (*f2)(int* a, double && b);\n"
2221                "const unsigned &&    g;\n"
2222                "Const unsigned       h;",
2223                Style);
2224   Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false;
2225   verifyFormat("Const unsigned int*  c;\n"
2226                "const unsigned int*  d;\n"
2227                "Const unsigned int & e;\n"
2228                "const unsigned int & f;\n"
2229                "int*                 f1(int* a, int & b, int && c);\n"
2230                "double* (*f2)(int* a, double && b);\n"
2231                "const unsigned && g;\n"
2232                "Const unsigned    h;",
2233                Style);
2234 
2235   Style.PointerAlignment = FormatStyle::PAS_Middle;
2236   Style.ReferenceAlignment = FormatStyle::RAS_Right;
2237   verifyFormat("int * f1(int * a, int &b, int &&c);", Style);
2238   verifyFormat("int &f2(int &&c, int * a, int &b);", Style);
2239   verifyFormat("int &&f3(int &b, int &&c, int * a);", Style);
2240   verifyFormat("int * a = f1();", Style);
2241   verifyFormat("int &b = f2();", Style);
2242   verifyFormat("int &&c = f3();", Style);
2243   verifyFormat("int f3() { return sizeof(Foo &); }", Style);
2244   verifyFormat("int f4() { return sizeof(Foo &&); }", Style);
2245   verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style);
2246   verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style);
2247   verifyFormat("for (auto a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
2248   verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
2249   verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style);
2250 
2251   Style.AlignConsecutiveDeclarations.Enabled = true;
2252   Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
2253   verifyFormat("Const unsigned int * c;\n"
2254                "const unsigned int * d;\n"
2255                "Const unsigned int  &e;\n"
2256                "const unsigned int  &f;\n"
2257                "int *                f1(int * a, int &b, int &&c);\n"
2258                "double *             (*f2)(int * a, double &&b);\n"
2259                "const unsigned     &&g;\n"
2260                "Const unsigned       h;",
2261                Style);
2262   Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false;
2263   verifyFormat("Const unsigned int * c;\n"
2264                "const unsigned int * d;\n"
2265                "Const unsigned int  &e;\n"
2266                "const unsigned int  &f;\n"
2267                "int *                f1(int * a, int &b, int &&c);\n"
2268                "double * (*f2)(int * a, double &&b);\n"
2269                "const unsigned &&g;\n"
2270                "Const unsigned   h;",
2271                Style);
2272 
2273   // FIXME: we don't handle this yet, so output may be arbitrary until it's
2274   // specifically handled
2275   // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style);
2276 }
2277 
2278 TEST_F(FormatTest, FormatsForLoop) {
2279   verifyFormat(
2280       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
2281       "     ++VeryVeryLongLoopVariable)\n"
2282       "  ;");
2283   verifyFormat("for (;;)\n"
2284                "  f();");
2285   verifyFormat("for (;;) {\n}");
2286   verifyFormat("for (;;) {\n"
2287                "  f();\n"
2288                "}");
2289   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
2290 
2291   verifyFormat(
2292       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2293       "                                          E = UnwrappedLines.end();\n"
2294       "     I != E; ++I) {\n}");
2295 
2296   verifyFormat(
2297       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
2298       "     ++IIIII) {\n}");
2299   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
2300                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
2301                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
2302   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
2303                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
2304                "         E = FD->getDeclsInPrototypeScope().end();\n"
2305                "     I != E; ++I) {\n}");
2306   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
2307                "         I = Container.begin(),\n"
2308                "         E = Container.end();\n"
2309                "     I != E; ++I) {\n}",
2310                getLLVMStyleWithColumns(76));
2311 
2312   verifyFormat(
2313       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2314       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
2315       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2316       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2317       "     ++aaaaaaaaaaa) {\n}");
2318   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2319                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
2320                "     ++i) {\n}");
2321   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
2322                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2323                "}");
2324   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
2325                "         aaaaaaaaaa);\n"
2326                "     iter; ++iter) {\n"
2327                "}");
2328   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2329                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2330                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
2331                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
2332 
2333   // These should not be formatted as Objective-C for-in loops.
2334   verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
2335   verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
2336   verifyFormat("Foo *x;\nfor (x in y) {\n}");
2337   verifyFormat(
2338       "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
2339 
2340   FormatStyle NoBinPacking = getLLVMStyle();
2341   NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
2342   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
2343                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
2344                "                                           aaaaaaaaaaaaaaaa,\n"
2345                "                                           aaaaaaaaaaaaaaaa,\n"
2346                "                                           aaaaaaaaaaaaaaaa);\n"
2347                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2348                "}",
2349                NoBinPacking);
2350   verifyFormat(
2351       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2352       "                                          E = UnwrappedLines.end();\n"
2353       "     I != E;\n"
2354       "     ++I) {\n}",
2355       NoBinPacking);
2356 
2357   FormatStyle AlignLeft = getLLVMStyle();
2358   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
2359   verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
2360 }
2361 
2362 TEST_F(FormatTest, RangeBasedForLoops) {
2363   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
2364                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2365   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
2366                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
2367   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
2368                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2369   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
2370                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
2371 }
2372 
2373 TEST_F(FormatTest, ForEachLoops) {
2374   FormatStyle Style = getLLVMStyle();
2375   EXPECT_EQ(Style.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
2376   EXPECT_EQ(Style.AllowShortLoopsOnASingleLine, false);
2377   verifyFormat("void f() {\n"
2378                "  for (;;) {\n"
2379                "  }\n"
2380                "  foreach (Item *item, itemlist) {\n"
2381                "  }\n"
2382                "  Q_FOREACH (Item *item, itemlist) {\n"
2383                "  }\n"
2384                "  BOOST_FOREACH (Item *item, itemlist) {\n"
2385                "  }\n"
2386                "  UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2387                "}",
2388                Style);
2389   verifyFormat("void f() {\n"
2390                "  for (;;)\n"
2391                "    int j = 1;\n"
2392                "  Q_FOREACH (int v, vec)\n"
2393                "    v *= 2;\n"
2394                "  for (;;) {\n"
2395                "    int j = 1;\n"
2396                "  }\n"
2397                "  Q_FOREACH (int v, vec) {\n"
2398                "    v *= 2;\n"
2399                "  }\n"
2400                "}",
2401                Style);
2402 
2403   FormatStyle ShortBlocks = getLLVMStyle();
2404   ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
2405   EXPECT_EQ(ShortBlocks.AllowShortLoopsOnASingleLine, false);
2406   verifyFormat("void f() {\n"
2407                "  for (;;)\n"
2408                "    int j = 1;\n"
2409                "  Q_FOREACH (int &v, vec)\n"
2410                "    v *= 2;\n"
2411                "  for (;;) {\n"
2412                "    int j = 1;\n"
2413                "  }\n"
2414                "  Q_FOREACH (int &v, vec) {\n"
2415                "    int j = 1;\n"
2416                "  }\n"
2417                "}",
2418                ShortBlocks);
2419 
2420   FormatStyle ShortLoops = getLLVMStyle();
2421   ShortLoops.AllowShortLoopsOnASingleLine = true;
2422   EXPECT_EQ(ShortLoops.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
2423   verifyFormat("void f() {\n"
2424                "  for (;;) int j = 1;\n"
2425                "  Q_FOREACH (int &v, vec) int j = 1;\n"
2426                "  for (;;) {\n"
2427                "    int j = 1;\n"
2428                "  }\n"
2429                "  Q_FOREACH (int &v, vec) {\n"
2430                "    int j = 1;\n"
2431                "  }\n"
2432                "}",
2433                ShortLoops);
2434 
2435   FormatStyle ShortBlocksAndLoops = getLLVMStyle();
2436   ShortBlocksAndLoops.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
2437   ShortBlocksAndLoops.AllowShortLoopsOnASingleLine = true;
2438   verifyFormat("void f() {\n"
2439                "  for (;;) int j = 1;\n"
2440                "  Q_FOREACH (int &v, vec) int j = 1;\n"
2441                "  for (;;) { int j = 1; }\n"
2442                "  Q_FOREACH (int &v, vec) { int j = 1; }\n"
2443                "}",
2444                ShortBlocksAndLoops);
2445 
2446   Style.SpaceBeforeParens =
2447       FormatStyle::SBPO_ControlStatementsExceptControlMacros;
2448   verifyFormat("void f() {\n"
2449                "  for (;;) {\n"
2450                "  }\n"
2451                "  foreach(Item *item, itemlist) {\n"
2452                "  }\n"
2453                "  Q_FOREACH(Item *item, itemlist) {\n"
2454                "  }\n"
2455                "  BOOST_FOREACH(Item *item, itemlist) {\n"
2456                "  }\n"
2457                "  UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2458                "}",
2459                Style);
2460 
2461   // As function-like macros.
2462   verifyFormat("#define foreach(x, y)\n"
2463                "#define Q_FOREACH(x, y)\n"
2464                "#define BOOST_FOREACH(x, y)\n"
2465                "#define UNKNOWN_FOREACH(x, y)");
2466 
2467   // Not as function-like macros.
2468   verifyFormat("#define foreach (x, y)\n"
2469                "#define Q_FOREACH (x, y)\n"
2470                "#define BOOST_FOREACH (x, y)\n"
2471                "#define UNKNOWN_FOREACH (x, y)");
2472 
2473   // handle microsoft non standard extension
2474   verifyFormat("for each (char c in x->MyStringProperty)");
2475 }
2476 
2477 TEST_F(FormatTest, FormatsWhileLoop) {
2478   verifyFormat("while (true) {\n}");
2479   verifyFormat("while (true)\n"
2480                "  f();");
2481   verifyFormat("while () {\n}");
2482   verifyFormat("while () {\n"
2483                "  f();\n"
2484                "}");
2485 }
2486 
2487 TEST_F(FormatTest, FormatsDoWhile) {
2488   verifyFormat("do {\n"
2489                "  do_something();\n"
2490                "} while (something());");
2491   verifyFormat("do\n"
2492                "  do_something();\n"
2493                "while (something());");
2494 }
2495 
2496 TEST_F(FormatTest, FormatsSwitchStatement) {
2497   verifyFormat("switch (x) {\n"
2498                "case 1:\n"
2499                "  f();\n"
2500                "  break;\n"
2501                "case kFoo:\n"
2502                "case ns::kBar:\n"
2503                "case kBaz:\n"
2504                "  break;\n"
2505                "default:\n"
2506                "  g();\n"
2507                "  break;\n"
2508                "}");
2509   verifyFormat("switch (x) {\n"
2510                "case 1: {\n"
2511                "  f();\n"
2512                "  break;\n"
2513                "}\n"
2514                "case 2: {\n"
2515                "  break;\n"
2516                "}\n"
2517                "}");
2518   verifyFormat("switch (x) {\n"
2519                "case 1: {\n"
2520                "  f();\n"
2521                "  {\n"
2522                "    g();\n"
2523                "    h();\n"
2524                "  }\n"
2525                "  break;\n"
2526                "}\n"
2527                "}");
2528   verifyFormat("switch (x) {\n"
2529                "case 1: {\n"
2530                "  f();\n"
2531                "  if (foo) {\n"
2532                "    g();\n"
2533                "    h();\n"
2534                "  }\n"
2535                "  break;\n"
2536                "}\n"
2537                "}");
2538   verifyFormat("switch (x) {\n"
2539                "case 1: {\n"
2540                "  f();\n"
2541                "  g();\n"
2542                "} break;\n"
2543                "}");
2544   verifyFormat("switch (test)\n"
2545                "  ;");
2546   verifyFormat("switch (x) {\n"
2547                "default: {\n"
2548                "  // Do nothing.\n"
2549                "}\n"
2550                "}");
2551   verifyFormat("switch (x) {\n"
2552                "// comment\n"
2553                "// if 1, do f()\n"
2554                "case 1:\n"
2555                "  f();\n"
2556                "}");
2557   verifyFormat("switch (x) {\n"
2558                "case 1:\n"
2559                "  // Do amazing stuff\n"
2560                "  {\n"
2561                "    f();\n"
2562                "    g();\n"
2563                "  }\n"
2564                "  break;\n"
2565                "}");
2566   verifyFormat("#define A          \\\n"
2567                "  switch (x) {     \\\n"
2568                "  case a:          \\\n"
2569                "    foo = b;       \\\n"
2570                "  }",
2571                getLLVMStyleWithColumns(20));
2572   verifyFormat("#define OPERATION_CASE(name)           \\\n"
2573                "  case OP_name:                        \\\n"
2574                "    return operations::Operation##name",
2575                getLLVMStyleWithColumns(40));
2576   verifyFormat("switch (x) {\n"
2577                "case 1:;\n"
2578                "default:;\n"
2579                "  int i;\n"
2580                "}");
2581 
2582   verifyGoogleFormat("switch (x) {\n"
2583                      "  case 1:\n"
2584                      "    f();\n"
2585                      "    break;\n"
2586                      "  case kFoo:\n"
2587                      "  case ns::kBar:\n"
2588                      "  case kBaz:\n"
2589                      "    break;\n"
2590                      "  default:\n"
2591                      "    g();\n"
2592                      "    break;\n"
2593                      "}");
2594   verifyGoogleFormat("switch (x) {\n"
2595                      "  case 1: {\n"
2596                      "    f();\n"
2597                      "    break;\n"
2598                      "  }\n"
2599                      "}");
2600   verifyGoogleFormat("switch (test)\n"
2601                      "  ;");
2602 
2603   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
2604                      "  case OP_name:              \\\n"
2605                      "    return operations::Operation##name");
2606   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
2607                      "  // Get the correction operation class.\n"
2608                      "  switch (OpCode) {\n"
2609                      "    CASE(Add);\n"
2610                      "    CASE(Subtract);\n"
2611                      "    default:\n"
2612                      "      return operations::Unknown;\n"
2613                      "  }\n"
2614                      "#undef OPERATION_CASE\n"
2615                      "}");
2616   verifyFormat("DEBUG({\n"
2617                "  switch (x) {\n"
2618                "  case A:\n"
2619                "    f();\n"
2620                "    break;\n"
2621                "    // fallthrough\n"
2622                "  case B:\n"
2623                "    g();\n"
2624                "    break;\n"
2625                "  }\n"
2626                "});");
2627   verifyNoChange("DEBUG({\n"
2628                  "  switch (x) {\n"
2629                  "  case A:\n"
2630                  "    f();\n"
2631                  "    break;\n"
2632                  "  // On B:\n"
2633                  "  case B:\n"
2634                  "    g();\n"
2635                  "    break;\n"
2636                  "  }\n"
2637                  "});");
2638   verifyFormat("switch (n) {\n"
2639                "case 0: {\n"
2640                "  return false;\n"
2641                "}\n"
2642                "default: {\n"
2643                "  return true;\n"
2644                "}\n"
2645                "}",
2646                "switch (n)\n"
2647                "{\n"
2648                "case 0: {\n"
2649                "  return false;\n"
2650                "}\n"
2651                "default: {\n"
2652                "  return true;\n"
2653                "}\n"
2654                "}");
2655   verifyFormat("switch (a) {\n"
2656                "case (b):\n"
2657                "  return;\n"
2658                "}");
2659 
2660   verifyFormat("switch (a) {\n"
2661                "case some_namespace::\n"
2662                "    some_constant:\n"
2663                "  return;\n"
2664                "}",
2665                getLLVMStyleWithColumns(34));
2666 
2667   verifyFormat("switch (a) {\n"
2668                "[[likely]] case 1:\n"
2669                "  return;\n"
2670                "}");
2671   verifyFormat("switch (a) {\n"
2672                "[[likely]] [[other::likely]] case 1:\n"
2673                "  return;\n"
2674                "}");
2675   verifyFormat("switch (x) {\n"
2676                "case 1:\n"
2677                "  return;\n"
2678                "[[likely]] case 2:\n"
2679                "  return;\n"
2680                "}");
2681   verifyFormat("switch (a) {\n"
2682                "case 1:\n"
2683                "[[likely]] case 2:\n"
2684                "  return;\n"
2685                "}");
2686   FormatStyle Attributes = getLLVMStyle();
2687   Attributes.AttributeMacros.push_back("LIKELY");
2688   Attributes.AttributeMacros.push_back("OTHER_LIKELY");
2689   verifyFormat("switch (a) {\n"
2690                "LIKELY case b:\n"
2691                "  return;\n"
2692                "}",
2693                Attributes);
2694   verifyFormat("switch (a) {\n"
2695                "LIKELY OTHER_LIKELY() case b:\n"
2696                "  return;\n"
2697                "}",
2698                Attributes);
2699   verifyFormat("switch (a) {\n"
2700                "case 1:\n"
2701                "  return;\n"
2702                "LIKELY case 2:\n"
2703                "  return;\n"
2704                "}",
2705                Attributes);
2706   verifyFormat("switch (a) {\n"
2707                "case 1:\n"
2708                "LIKELY case 2:\n"
2709                "  return;\n"
2710                "}",
2711                Attributes);
2712 
2713   FormatStyle Style = getLLVMStyle();
2714   Style.IndentCaseLabels = true;
2715   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
2716   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2717   Style.BraceWrapping.AfterCaseLabel = true;
2718   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2719   verifyFormat("switch (n)\n"
2720                "{\n"
2721                "  case 0:\n"
2722                "  {\n"
2723                "    return false;\n"
2724                "  }\n"
2725                "  default:\n"
2726                "  {\n"
2727                "    return true;\n"
2728                "  }\n"
2729                "}",
2730                "switch (n) {\n"
2731                "  case 0: {\n"
2732                "    return false;\n"
2733                "  }\n"
2734                "  default: {\n"
2735                "    return true;\n"
2736                "  }\n"
2737                "}",
2738                Style);
2739   Style.BraceWrapping.AfterCaseLabel = false;
2740   verifyFormat("switch (n)\n"
2741                "{\n"
2742                "  case 0: {\n"
2743                "    return false;\n"
2744                "  }\n"
2745                "  default: {\n"
2746                "    return true;\n"
2747                "  }\n"
2748                "}",
2749                "switch (n) {\n"
2750                "  case 0:\n"
2751                "  {\n"
2752                "    return false;\n"
2753                "  }\n"
2754                "  default:\n"
2755                "  {\n"
2756                "    return true;\n"
2757                "  }\n"
2758                "}",
2759                Style);
2760   Style.IndentCaseLabels = false;
2761   Style.IndentCaseBlocks = true;
2762   verifyFormat("switch (n)\n"
2763                "{\n"
2764                "case 0:\n"
2765                "  {\n"
2766                "    return false;\n"
2767                "  }\n"
2768                "case 1:\n"
2769                "  break;\n"
2770                "default:\n"
2771                "  {\n"
2772                "    return true;\n"
2773                "  }\n"
2774                "}",
2775                "switch (n) {\n"
2776                "case 0: {\n"
2777                "  return false;\n"
2778                "}\n"
2779                "case 1:\n"
2780                "  break;\n"
2781                "default: {\n"
2782                "  return true;\n"
2783                "}\n"
2784                "}",
2785                Style);
2786   Style.IndentCaseLabels = true;
2787   Style.IndentCaseBlocks = true;
2788   verifyFormat("switch (n)\n"
2789                "{\n"
2790                "  case 0:\n"
2791                "    {\n"
2792                "      return false;\n"
2793                "    }\n"
2794                "  case 1:\n"
2795                "    break;\n"
2796                "  default:\n"
2797                "    {\n"
2798                "      return true;\n"
2799                "    }\n"
2800                "}",
2801                "switch (n) {\n"
2802                "case 0: {\n"
2803                "  return false;\n"
2804                "}\n"
2805                "case 1:\n"
2806                "  break;\n"
2807                "default: {\n"
2808                "  return true;\n"
2809                "}\n"
2810                "}",
2811                Style);
2812 }
2813 
2814 TEST_F(FormatTest, CaseRanges) {
2815   verifyFormat("switch (x) {\n"
2816                "case 'A' ... 'Z':\n"
2817                "case 1 ... 5:\n"
2818                "case a ... b:\n"
2819                "  break;\n"
2820                "}");
2821 }
2822 
2823 TEST_F(FormatTest, ShortEnums) {
2824   FormatStyle Style = getLLVMStyle();
2825   EXPECT_TRUE(Style.AllowShortEnumsOnASingleLine);
2826   EXPECT_FALSE(Style.BraceWrapping.AfterEnum);
2827   verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2828   verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2829   Style.AllowShortEnumsOnASingleLine = false;
2830   verifyFormat("enum {\n"
2831                "  A,\n"
2832                "  B,\n"
2833                "  C\n"
2834                "} ShortEnum1, ShortEnum2;",
2835                Style);
2836   verifyFormat("typedef enum {\n"
2837                "  A,\n"
2838                "  B,\n"
2839                "  C\n"
2840                "} ShortEnum1, ShortEnum2;",
2841                Style);
2842   verifyFormat("enum {\n"
2843                "  A,\n"
2844                "} ShortEnum1, ShortEnum2;",
2845                Style);
2846   verifyFormat("typedef enum {\n"
2847                "  A,\n"
2848                "} ShortEnum1, ShortEnum2;",
2849                Style);
2850   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2851   Style.BraceWrapping.AfterEnum = true;
2852   verifyFormat("enum\n"
2853                "{\n"
2854                "  A,\n"
2855                "  B,\n"
2856                "  C\n"
2857                "} ShortEnum1, ShortEnum2;",
2858                Style);
2859   verifyFormat("typedef enum\n"
2860                "{\n"
2861                "  A,\n"
2862                "  B,\n"
2863                "  C\n"
2864                "} ShortEnum1, ShortEnum2;",
2865                Style);
2866 }
2867 
2868 TEST_F(FormatTest, ShortCompoundRequirement) {
2869   FormatStyle Style = getLLVMStyle();
2870   EXPECT_TRUE(Style.AllowShortCompoundRequirementOnASingleLine);
2871   verifyFormat("template <typename T>\n"
2872                "concept c = requires(T x) {\n"
2873                "  { x + 1 } -> std::same_as<int>;\n"
2874                "};",
2875                Style);
2876   verifyFormat("template <typename T>\n"
2877                "concept c = requires(T x) {\n"
2878                "  { x + 1 } -> std::same_as<int>;\n"
2879                "  { x + 2 } -> std::same_as<int>;\n"
2880                "};",
2881                Style);
2882   Style.AllowShortCompoundRequirementOnASingleLine = false;
2883   verifyFormat("template <typename T>\n"
2884                "concept c = requires(T x) {\n"
2885                "  {\n"
2886                "    x + 1\n"
2887                "  } -> std::same_as<int>;\n"
2888                "};",
2889                Style);
2890   verifyFormat("template <typename T>\n"
2891                "concept c = requires(T x) {\n"
2892                "  {\n"
2893                "    x + 1\n"
2894                "  } -> std::same_as<int>;\n"
2895                "  {\n"
2896                "    x + 2\n"
2897                "  } -> std::same_as<int>;\n"
2898                "};",
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   verifyFormat("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                "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   verifyFormat("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                "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   verifyFormat("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                "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   verifyFormat("switch (n) {\n"
3040                "  default /*comments*/:\n"
3041                "    return true;\n"
3042                "  case 0:\n"
3043                "    return false;\n"
3044                "}",
3045                "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   verifyFormat("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                "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   verifyFormat("{\n"
3101                "  some_code();\n"
3102                "test_label: { some_other_code(); }\n"
3103                "}");
3104   verifyFormat("{\n"
3105                "  some_code();\n"
3106                "test_label: {\n"
3107                "  some_other_code();\n"
3108                "  some_other_code();\n"
3109                "}\n"
3110                "}");
3111   verifyFormat("{\n"
3112                "L0:\n"
3113                "[[foo]] L1:\n"
3114                "[[bar]] [[baz]] L2:\n"
3115                "  g();\n"
3116                "}");
3117   verifyFormat("{\n"
3118                "[[foo]] L1: {\n"
3119                "[[bar]] [[baz]] L2:\n"
3120                "  g();\n"
3121                "}\n"
3122                "}");
3123   verifyFormat("{\n"
3124                "[[foo]] L1:\n"
3125                "  f();\n"
3126                "  {\n"
3127                "  [[bar]] [[baz]] L2:\n"
3128                "    g();\n"
3129                "  }\n"
3130                "}");
3131 
3132   FormatStyle Style = getLLVMStyle();
3133   Style.IndentGotoLabels = false;
3134   verifyFormat("void f() {\n"
3135                "  some_code();\n"
3136                "test_label:\n"
3137                "  some_other_code();\n"
3138                "  {\n"
3139                "    some_more_code();\n"
3140                "another_label:\n"
3141                "    some_more_code();\n"
3142                "  }\n"
3143                "}",
3144                Style);
3145   verifyFormat("{\n"
3146                "  some_code();\n"
3147                "test_label:\n"
3148                "  some_other_code();\n"
3149                "}",
3150                Style);
3151   verifyFormat("{\n"
3152                "  some_code();\n"
3153                "test_label:;\n"
3154                "  int i = 0;\n"
3155                "}",
3156                Style);
3157   verifyFormat("{\n"
3158                "  some_code();\n"
3159                "test_label: { some_other_code(); }\n"
3160                "}",
3161                Style);
3162   verifyFormat("{\n"
3163                "[[foo]] L1:\n"
3164                "  f();\n"
3165                "  {\n"
3166                "[[bar]] [[baz]] L2:\n"
3167                "    g();\n"
3168                "  }\n"
3169                "}",
3170                Style);
3171 
3172   Style.ColumnLimit = 15;
3173   verifyFormat("#define FOO   \\\n"
3174                "label:        \\\n"
3175                "  break;",
3176                Style);
3177 
3178   // The opening brace may either be on the same unwrapped line as the colon or
3179   // on a separate one. The formatter should recognize both.
3180   Style = getLLVMStyle();
3181   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Allman;
3182   verifyFormat("{\n"
3183                "  some_code();\n"
3184                "test_label:\n"
3185                "{\n"
3186                "  some_other_code();\n"
3187                "}\n"
3188                "}",
3189                Style);
3190   verifyFormat("{\n"
3191                "[[foo]] L1:\n"
3192                "{\n"
3193                "[[bar]] [[baz]] L2:\n"
3194                "  g();\n"
3195                "}\n"
3196                "}",
3197                Style);
3198 }
3199 
3200 TEST_F(FormatTest, MultiLineControlStatements) {
3201   FormatStyle Style = getLLVMStyleWithColumns(20);
3202   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
3203   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
3204   // Short lines should keep opening brace on same line.
3205   verifyFormat("if (foo) {\n"
3206                "  bar();\n"
3207                "}",
3208                "if(foo){bar();}", Style);
3209   verifyFormat("if (foo) {\n"
3210                "  bar();\n"
3211                "} else {\n"
3212                "  baz();\n"
3213                "}",
3214                "if(foo){bar();}else{baz();}", Style);
3215   verifyFormat("if (foo && bar) {\n"
3216                "  baz();\n"
3217                "}",
3218                "if(foo&&bar){baz();}", Style);
3219   verifyFormat("if (foo) {\n"
3220                "  bar();\n"
3221                "} else if (baz) {\n"
3222                "  quux();\n"
3223                "}",
3224                "if(foo){bar();}else if(baz){quux();}", Style);
3225   verifyFormat("if (foo) {\n"
3226                "  bar();\n"
3227                "} else if (baz) {\n"
3228                "  quux();\n"
3229                "} else {\n"
3230                "  foobar();\n"
3231                "}",
3232                "if(foo){bar();}else if(baz){quux();}else{foobar();}", Style);
3233   verifyFormat("for (;;) {\n"
3234                "  foo();\n"
3235                "}",
3236                "for(;;){foo();}");
3237   verifyFormat("while (1) {\n"
3238                "  foo();\n"
3239                "}",
3240                "while(1){foo();}", Style);
3241   verifyFormat("switch (foo) {\n"
3242                "case bar:\n"
3243                "  return;\n"
3244                "}",
3245                "switch(foo){case bar:return;}", Style);
3246   verifyFormat("try {\n"
3247                "  foo();\n"
3248                "} catch (...) {\n"
3249                "  bar();\n"
3250                "}",
3251                "try{foo();}catch(...){bar();}", Style);
3252   verifyFormat("do {\n"
3253                "  foo();\n"
3254                "} while (bar &&\n"
3255                "         baz);",
3256                "do{foo();}while(bar&&baz);", Style);
3257   // Long lines should put opening brace on new line.
3258   verifyFormat("void f() {\n"
3259                "  if (a1 && a2 &&\n"
3260                "      a3)\n"
3261                "  {\n"
3262                "    quux();\n"
3263                "  }\n"
3264                "}",
3265                "void f(){if(a1&&a2&&a3){quux();}}", Style);
3266   verifyFormat("if (foo && bar &&\n"
3267                "    baz)\n"
3268                "{\n"
3269                "  quux();\n"
3270                "}",
3271                "if(foo&&bar&&baz){quux();}", Style);
3272   verifyFormat("if (foo && bar &&\n"
3273                "    baz)\n"
3274                "{\n"
3275                "  quux();\n"
3276                "}",
3277                "if (foo && bar &&\n"
3278                "    baz) {\n"
3279                "  quux();\n"
3280                "}",
3281                Style);
3282   verifyFormat("if (foo) {\n"
3283                "  bar();\n"
3284                "} else if (baz ||\n"
3285                "           quux)\n"
3286                "{\n"
3287                "  foobar();\n"
3288                "}",
3289                "if(foo){bar();}else if(baz||quux){foobar();}", Style);
3290   verifyFormat("if (foo) {\n"
3291                "  bar();\n"
3292                "} else if (baz ||\n"
3293                "           quux)\n"
3294                "{\n"
3295                "  foobar();\n"
3296                "} else {\n"
3297                "  barbaz();\n"
3298                "}",
3299                "if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
3300                Style);
3301   verifyFormat("for (int i = 0;\n"
3302                "     i < 10; ++i)\n"
3303                "{\n"
3304                "  foo();\n"
3305                "}",
3306                "for(int i=0;i<10;++i){foo();}", Style);
3307   verifyFormat("foreach (int i,\n"
3308                "         list)\n"
3309                "{\n"
3310                "  foo();\n"
3311                "}",
3312                "foreach(int i, list){foo();}", Style);
3313   Style.ColumnLimit =
3314       40; // to concentrate at brace wrapping, not line wrap due to column limit
3315   verifyFormat("foreach (int i, list) {\n"
3316                "  foo();\n"
3317                "}",
3318                "foreach(int i, list){foo();}", Style);
3319   Style.ColumnLimit =
3320       20; // to concentrate at brace wrapping, not line wrap due to column limit
3321   verifyFormat("while (foo || bar ||\n"
3322                "       baz)\n"
3323                "{\n"
3324                "  quux();\n"
3325                "}",
3326                "while(foo||bar||baz){quux();}", Style);
3327   verifyFormat("switch (\n"
3328                "    foo = barbaz)\n"
3329                "{\n"
3330                "case quux:\n"
3331                "  return;\n"
3332                "}",
3333                "switch(foo=barbaz){case quux:return;}", Style);
3334   verifyFormat("try {\n"
3335                "  foo();\n"
3336                "} catch (\n"
3337                "    Exception &bar)\n"
3338                "{\n"
3339                "  baz();\n"
3340                "}",
3341                "try{foo();}catch(Exception&bar){baz();}", Style);
3342   Style.ColumnLimit =
3343       40; // to concentrate at brace wrapping, not line wrap due to column limit
3344   verifyFormat("try {\n"
3345                "  foo();\n"
3346                "} catch (Exception &bar) {\n"
3347                "  baz();\n"
3348                "}",
3349                "try{foo();}catch(Exception&bar){baz();}", Style);
3350   Style.ColumnLimit =
3351       20; // to concentrate at brace wrapping, not line wrap due to column limit
3352 
3353   Style.BraceWrapping.BeforeElse = true;
3354   verifyFormat("if (foo) {\n"
3355                "  bar();\n"
3356                "}\n"
3357                "else if (baz ||\n"
3358                "         quux)\n"
3359                "{\n"
3360                "  foobar();\n"
3361                "}\n"
3362                "else {\n"
3363                "  barbaz();\n"
3364                "}",
3365                "if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
3366                Style);
3367 
3368   Style.BraceWrapping.BeforeCatch = true;
3369   verifyFormat("try {\n"
3370                "  foo();\n"
3371                "}\n"
3372                "catch (...) {\n"
3373                "  baz();\n"
3374                "}",
3375                "try{foo();}catch(...){baz();}", Style);
3376 
3377   Style.BraceWrapping.AfterFunction = true;
3378   Style.BraceWrapping.AfterStruct = false;
3379   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
3380   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3381   Style.ColumnLimit = 80;
3382   verifyFormat("void shortfunction() { bar(); }", Style);
3383   verifyFormat("struct T shortfunction() { return bar(); }", Style);
3384   verifyFormat("struct T {};", Style);
3385 
3386   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3387   verifyFormat("void shortfunction()\n"
3388                "{\n"
3389                "  bar();\n"
3390                "}",
3391                Style);
3392   verifyFormat("struct T shortfunction()\n"
3393                "{\n"
3394                "  return bar();\n"
3395                "}",
3396                Style);
3397   verifyFormat("struct T {};", Style);
3398 
3399   Style.BraceWrapping.AfterFunction = false;
3400   Style.BraceWrapping.AfterStruct = true;
3401   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3402   verifyFormat("void shortfunction() { bar(); }", Style);
3403   verifyFormat("struct T shortfunction() { return bar(); }", Style);
3404   verifyFormat("struct T\n"
3405                "{\n"
3406                "};",
3407                Style);
3408 
3409   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3410   verifyFormat("void shortfunction() {\n"
3411                "  bar();\n"
3412                "}",
3413                Style);
3414   verifyFormat("struct T shortfunction() {\n"
3415                "  return bar();\n"
3416                "}",
3417                Style);
3418   verifyFormat("struct T\n"
3419                "{\n"
3420                "};",
3421                Style);
3422 }
3423 
3424 TEST_F(FormatTest, BeforeWhile) {
3425   FormatStyle Style = getLLVMStyle();
3426   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
3427 
3428   verifyFormat("do {\n"
3429                "  foo();\n"
3430                "} while (1);",
3431                Style);
3432   Style.BraceWrapping.BeforeWhile = true;
3433   verifyFormat("do {\n"
3434                "  foo();\n"
3435                "}\n"
3436                "while (1);",
3437                Style);
3438 }
3439 
3440 //===----------------------------------------------------------------------===//
3441 // Tests for classes, namespaces, etc.
3442 //===----------------------------------------------------------------------===//
3443 
3444 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
3445   verifyFormat("class A {};");
3446 }
3447 
3448 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
3449   verifyFormat("class A {\n"
3450                "public:\n"
3451                "public: // comment\n"
3452                "protected:\n"
3453                "private:\n"
3454                "  void f() {}\n"
3455                "};");
3456   verifyFormat("export class A {\n"
3457                "public:\n"
3458                "public: // comment\n"
3459                "protected:\n"
3460                "private:\n"
3461                "  void f() {}\n"
3462                "};");
3463   verifyGoogleFormat("class A {\n"
3464                      " public:\n"
3465                      " protected:\n"
3466                      " private:\n"
3467                      "  void f() {}\n"
3468                      "};");
3469   verifyGoogleFormat("export class A {\n"
3470                      " public:\n"
3471                      " protected:\n"
3472                      " private:\n"
3473                      "  void f() {}\n"
3474                      "};");
3475   verifyFormat("class A {\n"
3476                "public slots:\n"
3477                "  void f1() {}\n"
3478                "public Q_SLOTS:\n"
3479                "  void f2() {}\n"
3480                "protected slots:\n"
3481                "  void f3() {}\n"
3482                "protected Q_SLOTS:\n"
3483                "  void f4() {}\n"
3484                "private slots:\n"
3485                "  void f5() {}\n"
3486                "private Q_SLOTS:\n"
3487                "  void f6() {}\n"
3488                "signals:\n"
3489                "  void g1();\n"
3490                "Q_SIGNALS:\n"
3491                "  void g2();\n"
3492                "};");
3493 
3494   // Don't interpret 'signals' the wrong way.
3495   verifyFormat("signals.set();");
3496   verifyFormat("for (Signals signals : f()) {\n}");
3497   verifyFormat("{\n"
3498                "  signals.set(); // This needs indentation.\n"
3499                "}");
3500   verifyFormat("void f() {\n"
3501                "label:\n"
3502                "  signals.baz();\n"
3503                "}");
3504   verifyFormat("private[1];");
3505   verifyFormat("testArray[public] = 1;");
3506   verifyFormat("public();");
3507   verifyFormat("myFunc(public);");
3508   verifyFormat("std::vector<int> testVec = {private};");
3509   verifyFormat("private.p = 1;");
3510   verifyFormat("void function(private...) {};");
3511   verifyFormat("if (private && public)");
3512   verifyFormat("private &= true;");
3513   verifyFormat("int x = private * public;");
3514   verifyFormat("public *= private;");
3515   verifyFormat("int x = public + private;");
3516   verifyFormat("private++;");
3517   verifyFormat("++private;");
3518   verifyFormat("public += private;");
3519   verifyFormat("public = public - private;");
3520   verifyFormat("public->foo();");
3521   verifyFormat("private--;");
3522   verifyFormat("--private;");
3523   verifyFormat("public -= 1;");
3524   verifyFormat("if (!private && !public)");
3525   verifyFormat("public != private;");
3526   verifyFormat("int x = public / private;");
3527   verifyFormat("public /= 2;");
3528   verifyFormat("public = public % 2;");
3529   verifyFormat("public %= 2;");
3530   verifyFormat("if (public < private)");
3531   verifyFormat("public << private;");
3532   verifyFormat("public <<= private;");
3533   verifyFormat("if (public > private)");
3534   verifyFormat("public >> private;");
3535   verifyFormat("public >>= private;");
3536   verifyFormat("public ^ private;");
3537   verifyFormat("public ^= private;");
3538   verifyFormat("public | private;");
3539   verifyFormat("public |= private;");
3540   verifyFormat("auto x = private ? 1 : 2;");
3541   verifyFormat("if (public == private)");
3542   verifyFormat("void foo(public, private)");
3543   verifyFormat("public::foo();");
3544 
3545   verifyFormat("class A {\n"
3546                "public:\n"
3547                "  std::unique_ptr<int *[]> b() { return nullptr; }\n"
3548                "\n"
3549                "private:\n"
3550                "  int c;\n"
3551                "};\n"
3552                "class B {\n"
3553                "public:\n"
3554                "  std::unique_ptr<int *[] /* okay */> b() { return nullptr; }\n"
3555                "\n"
3556                "private:\n"
3557                "  int c;\n"
3558                "};");
3559 }
3560 
3561 TEST_F(FormatTest, SeparatesLogicalBlocks) {
3562   verifyFormat("class A {\n"
3563                "public:\n"
3564                "  void f();\n"
3565                "\n"
3566                "private:\n"
3567                "  void g() {}\n"
3568                "  // test\n"
3569                "protected:\n"
3570                "  int h;\n"
3571                "};",
3572                "class A {\n"
3573                "public:\n"
3574                "void f();\n"
3575                "private:\n"
3576                "void g() {}\n"
3577                "// test\n"
3578                "protected:\n"
3579                "int h;\n"
3580                "};");
3581   verifyFormat("class A {\n"
3582                "protected:\n"
3583                "public:\n"
3584                "  void f();\n"
3585                "};",
3586                "class A {\n"
3587                "protected:\n"
3588                "\n"
3589                "public:\n"
3590                "\n"
3591                "  void f();\n"
3592                "};");
3593 
3594   // Even ensure proper spacing inside macros.
3595   verifyFormat("#define B     \\\n"
3596                "  class A {   \\\n"
3597                "   protected: \\\n"
3598                "   public:    \\\n"
3599                "    void f(); \\\n"
3600                "  };",
3601                "#define B     \\\n"
3602                "  class A {   \\\n"
3603                "   protected: \\\n"
3604                "              \\\n"
3605                "   public:    \\\n"
3606                "              \\\n"
3607                "    void f(); \\\n"
3608                "  };",
3609                getGoogleStyle());
3610   // But don't remove empty lines after macros ending in access specifiers.
3611   verifyFormat("#define A private:\n"
3612                "\n"
3613                "int i;",
3614                "#define A         private:\n"
3615                "\n"
3616                "int              i;");
3617 }
3618 
3619 TEST_F(FormatTest, FormatsClasses) {
3620   verifyFormat("class A : public B {};");
3621   verifyFormat("class A : public ::B {};");
3622 
3623   verifyFormat(
3624       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3625       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3626   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3627                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3628                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3629   verifyFormat(
3630       "class A : public B, public C, public D, public E, public F {};");
3631   verifyFormat("class AAAAAAAAAAAA : public B,\n"
3632                "                     public C,\n"
3633                "                     public D,\n"
3634                "                     public E,\n"
3635                "                     public F,\n"
3636                "                     public G {};");
3637 
3638   verifyFormat("class\n"
3639                "    ReallyReallyLongClassName {\n"
3640                "  int i;\n"
3641                "};",
3642                getLLVMStyleWithColumns(32));
3643   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3644                "                           aaaaaaaaaaaaaaaa> {};");
3645   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
3646                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
3647                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
3648   verifyFormat("template <class R, class C>\n"
3649                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
3650                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
3651   verifyFormat("class ::A::B {};");
3652 }
3653 
3654 TEST_F(FormatTest, BreakInheritanceStyle) {
3655   FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
3656   StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
3657       FormatStyle::BILS_BeforeComma;
3658   verifyFormat("class MyClass : public X {};",
3659                StyleWithInheritanceBreakBeforeComma);
3660   verifyFormat("class MyClass\n"
3661                "    : public X\n"
3662                "    , public Y {};",
3663                StyleWithInheritanceBreakBeforeComma);
3664   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
3665                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
3666                "    , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3667                StyleWithInheritanceBreakBeforeComma);
3668   verifyFormat("struct aaaaaaaaaaaaa\n"
3669                "    : public aaaaaaaaaaaaaaaaaaa< // break\n"
3670                "          aaaaaaaaaaaaaaaa> {};",
3671                StyleWithInheritanceBreakBeforeComma);
3672 
3673   FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
3674   StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
3675       FormatStyle::BILS_AfterColon;
3676   verifyFormat("class MyClass : public X {};",
3677                StyleWithInheritanceBreakAfterColon);
3678   verifyFormat("class MyClass : public X, public Y {};",
3679                StyleWithInheritanceBreakAfterColon);
3680   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
3681                "    public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3682                "    public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3683                StyleWithInheritanceBreakAfterColon);
3684   verifyFormat("struct aaaaaaaaaaaaa :\n"
3685                "    public aaaaaaaaaaaaaaaaaaa< // break\n"
3686                "        aaaaaaaaaaaaaaaa> {};",
3687                StyleWithInheritanceBreakAfterColon);
3688 
3689   FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle();
3690   StyleWithInheritanceBreakAfterComma.BreakInheritanceList =
3691       FormatStyle::BILS_AfterComma;
3692   verifyFormat("class MyClass : public X {};",
3693                StyleWithInheritanceBreakAfterComma);
3694   verifyFormat("class MyClass : public X,\n"
3695                "                public Y {};",
3696                StyleWithInheritanceBreakAfterComma);
3697   verifyFormat(
3698       "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3699       "                               public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC "
3700       "{};",
3701       StyleWithInheritanceBreakAfterComma);
3702   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3703                "                           aaaaaaaaaaaaaaaa> {};",
3704                StyleWithInheritanceBreakAfterComma);
3705   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3706                "    : public OnceBreak,\n"
3707                "      public AlwaysBreak,\n"
3708                "      EvenBasesFitInOneLine {};",
3709                StyleWithInheritanceBreakAfterComma);
3710 }
3711 
3712 TEST_F(FormatTest, FormatsVariableDeclarationsAfterRecord) {
3713   verifyFormat("class A {\n} a, b;");
3714   verifyFormat("struct A {\n} a, b;");
3715   verifyFormat("union A {\n} a, b;");
3716 
3717   verifyFormat("constexpr class A {\n} a, b;");
3718   verifyFormat("constexpr struct A {\n} a, b;");
3719   verifyFormat("constexpr union A {\n} a, b;");
3720 
3721   verifyFormat("namespace {\nclass A {\n} a, b;\n} // namespace");
3722   verifyFormat("namespace {\nstruct A {\n} a, b;\n} // namespace");
3723   verifyFormat("namespace {\nunion A {\n} a, b;\n} // namespace");
3724 
3725   verifyFormat("namespace {\nconstexpr class A {\n} a, b;\n} // namespace");
3726   verifyFormat("namespace {\nconstexpr struct A {\n} a, b;\n} // namespace");
3727   verifyFormat("namespace {\nconstexpr union A {\n} a, b;\n} // namespace");
3728 
3729   verifyFormat("namespace ns {\n"
3730                "class {\n"
3731                "} a, b;\n"
3732                "} // namespace ns");
3733   verifyFormat("namespace ns {\n"
3734                "const class {\n"
3735                "} a, b;\n"
3736                "} // namespace ns");
3737   verifyFormat("namespace ns {\n"
3738                "constexpr class C {\n"
3739                "} a, b;\n"
3740                "} // namespace ns");
3741   verifyFormat("namespace ns {\n"
3742                "class { /* comment */\n"
3743                "} a, b;\n"
3744                "} // namespace ns");
3745   verifyFormat("namespace ns {\n"
3746                "const class { /* comment */\n"
3747                "} a, b;\n"
3748                "} // namespace ns");
3749 }
3750 
3751 TEST_F(FormatTest, FormatsEnum) {
3752   verifyFormat("enum {\n"
3753                "  Zero,\n"
3754                "  One = 1,\n"
3755                "  Two = One + 1,\n"
3756                "  Three = (One + Two),\n"
3757                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3758                "  Five = (One, Two, Three, Four, 5)\n"
3759                "};");
3760   verifyGoogleFormat("enum {\n"
3761                      "  Zero,\n"
3762                      "  One = 1,\n"
3763                      "  Two = One + 1,\n"
3764                      "  Three = (One + Two),\n"
3765                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3766                      "  Five = (One, Two, Three, Four, 5)\n"
3767                      "};");
3768   verifyFormat("enum Enum {};");
3769   verifyFormat("enum {};");
3770   verifyFormat("enum X E {} d;");
3771   verifyFormat("enum __attribute__((...)) E {} d;");
3772   verifyFormat("enum __declspec__((...)) E {} d;");
3773   verifyFormat("enum [[nodiscard]] E {} d;");
3774   verifyFormat("enum {\n"
3775                "  Bar = Foo<int, int>::value\n"
3776                "};",
3777                getLLVMStyleWithColumns(30));
3778 
3779   verifyFormat("enum ShortEnum { A, B, C };");
3780   verifyGoogleFormat("enum ShortEnum { A, B, C };");
3781 
3782   verifyFormat("enum KeepEmptyLines {\n"
3783                "  ONE,\n"
3784                "\n"
3785                "  TWO,\n"
3786                "\n"
3787                "  THREE\n"
3788                "}",
3789                "enum KeepEmptyLines {\n"
3790                "  ONE,\n"
3791                "\n"
3792                "  TWO,\n"
3793                "\n"
3794                "\n"
3795                "  THREE\n"
3796                "}");
3797   verifyFormat("enum E { // comment\n"
3798                "  ONE,\n"
3799                "  TWO\n"
3800                "};\n"
3801                "int i;");
3802 
3803   FormatStyle EightIndent = getLLVMStyle();
3804   EightIndent.IndentWidth = 8;
3805   verifyFormat("enum {\n"
3806                "        VOID,\n"
3807                "        CHAR,\n"
3808                "        SHORT,\n"
3809                "        INT,\n"
3810                "        LONG,\n"
3811                "        SIGNED,\n"
3812                "        UNSIGNED,\n"
3813                "        BOOL,\n"
3814                "        FLOAT,\n"
3815                "        DOUBLE,\n"
3816                "        COMPLEX\n"
3817                "};",
3818                EightIndent);
3819 
3820   verifyFormat("enum [[nodiscard]] E {\n"
3821                "  ONE,\n"
3822                "  TWO,\n"
3823                "};");
3824   verifyFormat("enum [[nodiscard]] E {\n"
3825                "  // Comment 1\n"
3826                "  ONE,\n"
3827                "  // Comment 2\n"
3828                "  TWO,\n"
3829                "};");
3830   verifyFormat("enum [[clang::enum_extensibility(open)]] E {\n"
3831                "  // Comment 1\n"
3832                "  ONE,\n"
3833                "  // Comment 2\n"
3834                "  TWO\n"
3835                "};");
3836   verifyFormat("enum [[nodiscard]] [[clang::enum_extensibility(open)]] E {\n"
3837                "  // Comment 1\n"
3838                "  ONE,\n"
3839                "  // Comment 2\n"
3840                "  TWO\n"
3841                "};");
3842   verifyFormat("enum [[clang::enum_extensibility(open)]] E { // foo\n"
3843                "  A,\n"
3844                "  // bar\n"
3845                "  B\n"
3846                "};",
3847                "enum [[clang::enum_extensibility(open)]] E{// foo\n"
3848                "                                           A,\n"
3849                "                                           // bar\n"
3850                "                                           B};");
3851 
3852   // Not enums.
3853   verifyFormat("enum X f() {\n"
3854                "  a();\n"
3855                "  return 42;\n"
3856                "}");
3857   verifyFormat("enum X Type::f() {\n"
3858                "  a();\n"
3859                "  return 42;\n"
3860                "}");
3861   verifyFormat("enum ::X f() {\n"
3862                "  a();\n"
3863                "  return 42;\n"
3864                "}");
3865   verifyFormat("enum ns::X f() {\n"
3866                "  a();\n"
3867                "  return 42;\n"
3868                "}");
3869 }
3870 
3871 TEST_F(FormatTest, FormatsEnumsWithErrors) {
3872   verifyFormat("enum Type {\n"
3873                "  One = 0; // These semicolons should be commas.\n"
3874                "  Two = 1;\n"
3875                "};");
3876   verifyFormat("namespace n {\n"
3877                "enum Type {\n"
3878                "  One,\n"
3879                "  Two, // missing };\n"
3880                "  int i;\n"
3881                "}\n"
3882                "void g() {}");
3883 }
3884 
3885 TEST_F(FormatTest, FormatsEnumStruct) {
3886   verifyFormat("enum struct {\n"
3887                "  Zero,\n"
3888                "  One = 1,\n"
3889                "  Two = One + 1,\n"
3890                "  Three = (One + Two),\n"
3891                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3892                "  Five = (One, Two, Three, Four, 5)\n"
3893                "};");
3894   verifyFormat("enum struct Enum {};");
3895   verifyFormat("enum struct {};");
3896   verifyFormat("enum struct X E {} d;");
3897   verifyFormat("enum struct __attribute__((...)) E {} d;");
3898   verifyFormat("enum struct __declspec__((...)) E {} d;");
3899   verifyFormat("enum struct [[nodiscard]] E {} d;");
3900   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
3901 
3902   verifyFormat("enum struct [[nodiscard]] E {\n"
3903                "  ONE,\n"
3904                "  TWO,\n"
3905                "};");
3906   verifyFormat("enum struct [[nodiscard]] E {\n"
3907                "  // Comment 1\n"
3908                "  ONE,\n"
3909                "  // Comment 2\n"
3910                "  TWO,\n"
3911                "};");
3912 }
3913 
3914 TEST_F(FormatTest, FormatsEnumClass) {
3915   verifyFormat("enum class {\n"
3916                "  Zero,\n"
3917                "  One = 1,\n"
3918                "  Two = One + 1,\n"
3919                "  Three = (One + Two),\n"
3920                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3921                "  Five = (One, Two, Three, Four, 5)\n"
3922                "};");
3923   verifyFormat("enum class Enum {};");
3924   verifyFormat("enum class {};");
3925   verifyFormat("enum class X E {} d;");
3926   verifyFormat("enum class __attribute__((...)) E {} d;");
3927   verifyFormat("enum class __declspec__((...)) E {} d;");
3928   verifyFormat("enum class [[nodiscard]] E {} d;");
3929   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
3930 
3931   verifyFormat("enum class [[nodiscard]] E {\n"
3932                "  ONE,\n"
3933                "  TWO,\n"
3934                "};");
3935   verifyFormat("enum class [[nodiscard]] E {\n"
3936                "  // Comment 1\n"
3937                "  ONE,\n"
3938                "  // Comment 2\n"
3939                "  TWO,\n"
3940                "};");
3941 }
3942 
3943 TEST_F(FormatTest, FormatsEnumTypes) {
3944   verifyFormat("enum X : int {\n"
3945                "  A, // Force multiple lines.\n"
3946                "  B\n"
3947                "};");
3948   verifyFormat("enum X : int { A, B };");
3949   verifyFormat("enum X : std::uint32_t { A, B };");
3950 }
3951 
3952 TEST_F(FormatTest, FormatsTypedefEnum) {
3953   FormatStyle Style = getLLVMStyleWithColumns(40);
3954   verifyFormat("typedef enum {} EmptyEnum;");
3955   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3956   verifyFormat("typedef enum {\n"
3957                "  ZERO = 0,\n"
3958                "  ONE = 1,\n"
3959                "  TWO = 2,\n"
3960                "  THREE = 3\n"
3961                "} LongEnum;",
3962                Style);
3963   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3964   Style.BraceWrapping.AfterEnum = true;
3965   verifyFormat("typedef enum {} EmptyEnum;");
3966   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3967   verifyFormat("typedef enum\n"
3968                "{\n"
3969                "  ZERO = 0,\n"
3970                "  ONE = 1,\n"
3971                "  TWO = 2,\n"
3972                "  THREE = 3\n"
3973                "} LongEnum;",
3974                Style);
3975 }
3976 
3977 TEST_F(FormatTest, FormatsNSEnums) {
3978   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
3979   verifyGoogleFormat(
3980       "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
3981   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
3982                      "  // Information about someDecentlyLongValue.\n"
3983                      "  someDecentlyLongValue,\n"
3984                      "  // Information about anotherDecentlyLongValue.\n"
3985                      "  anotherDecentlyLongValue,\n"
3986                      "  // Information about aThirdDecentlyLongValue.\n"
3987                      "  aThirdDecentlyLongValue\n"
3988                      "};");
3989   verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n"
3990                      "  // Information about someDecentlyLongValue.\n"
3991                      "  someDecentlyLongValue,\n"
3992                      "  // Information about anotherDecentlyLongValue.\n"
3993                      "  anotherDecentlyLongValue,\n"
3994                      "  // Information about aThirdDecentlyLongValue.\n"
3995                      "  aThirdDecentlyLongValue\n"
3996                      "};");
3997   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
3998                      "  a = 1,\n"
3999                      "  b = 2,\n"
4000                      "  c = 3,\n"
4001                      "};");
4002   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
4003                      "  a = 1,\n"
4004                      "  b = 2,\n"
4005                      "  c = 3,\n"
4006                      "};");
4007   verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
4008                      "  a = 1,\n"
4009                      "  b = 2,\n"
4010                      "  c = 3,\n"
4011                      "};");
4012   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
4013                      "  a = 1,\n"
4014                      "  b = 2,\n"
4015                      "  c = 3,\n"
4016                      "};");
4017 }
4018 
4019 TEST_F(FormatTest, FormatsBitfields) {
4020   verifyFormat("struct Bitfields {\n"
4021                "  unsigned sClass : 8;\n"
4022                "  unsigned ValueKind : 2;\n"
4023                "};");
4024   verifyFormat("struct A {\n"
4025                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
4026                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
4027                "};");
4028   verifyFormat("struct MyStruct {\n"
4029                "  uchar data;\n"
4030                "  uchar : 8;\n"
4031                "  uchar : 8;\n"
4032                "  uchar other;\n"
4033                "};");
4034   FormatStyle Style = getLLVMStyle();
4035   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
4036   verifyFormat("struct Bitfields {\n"
4037                "  unsigned sClass:8;\n"
4038                "  unsigned ValueKind:2;\n"
4039                "  uchar other;\n"
4040                "};",
4041                Style);
4042   verifyFormat("struct A {\n"
4043                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n"
4044                "      bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n"
4045                "};",
4046                Style);
4047   Style.BitFieldColonSpacing = FormatStyle::BFCS_Before;
4048   verifyFormat("struct Bitfields {\n"
4049                "  unsigned sClass :8;\n"
4050                "  unsigned ValueKind :2;\n"
4051                "  uchar other;\n"
4052                "};",
4053                Style);
4054   Style.BitFieldColonSpacing = FormatStyle::BFCS_After;
4055   verifyFormat("struct Bitfields {\n"
4056                "  unsigned sClass: 8;\n"
4057                "  unsigned ValueKind: 2;\n"
4058                "  uchar other;\n"
4059                "};",
4060                Style);
4061 }
4062 
4063 TEST_F(FormatTest, FormatsNamespaces) {
4064   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
4065   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
4066 
4067   verifyFormat("namespace some_namespace {\n"
4068                "class A {};\n"
4069                "void f() { f(); }\n"
4070                "}",
4071                LLVMWithNoNamespaceFix);
4072   verifyFormat("#define M(x) x##x\n"
4073                "namespace M(x) {\n"
4074                "class A {};\n"
4075                "void f() { f(); }\n"
4076                "}",
4077                LLVMWithNoNamespaceFix);
4078   verifyFormat("#define M(x) x##x\n"
4079                "namespace N::inline M(x) {\n"
4080                "class A {};\n"
4081                "void f() { f(); }\n"
4082                "}",
4083                LLVMWithNoNamespaceFix);
4084   verifyFormat("#define M(x) x##x\n"
4085                "namespace M(x)::inline N {\n"
4086                "class A {};\n"
4087                "void f() { f(); }\n"
4088                "}",
4089                LLVMWithNoNamespaceFix);
4090   verifyFormat("#define M(x) x##x\n"
4091                "namespace N::M(x) {\n"
4092                "class A {};\n"
4093                "void f() { f(); }\n"
4094                "}",
4095                LLVMWithNoNamespaceFix);
4096   verifyFormat("#define M(x) x##x\n"
4097                "namespace M::N(x) {\n"
4098                "class A {};\n"
4099                "void f() { f(); }\n"
4100                "}",
4101                LLVMWithNoNamespaceFix);
4102   verifyFormat("namespace N::inline D {\n"
4103                "class A {};\n"
4104                "void f() { f(); }\n"
4105                "}",
4106                LLVMWithNoNamespaceFix);
4107   verifyFormat("namespace N::inline D::E {\n"
4108                "class A {};\n"
4109                "void f() { f(); }\n"
4110                "}",
4111                LLVMWithNoNamespaceFix);
4112   verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
4113                "class A {};\n"
4114                "void f() { f(); }\n"
4115                "}",
4116                LLVMWithNoNamespaceFix);
4117   verifyFormat("/* something */ namespace some_namespace {\n"
4118                "class A {};\n"
4119                "void f() { f(); }\n"
4120                "}",
4121                LLVMWithNoNamespaceFix);
4122   verifyFormat("namespace {\n"
4123                "class A {};\n"
4124                "void f() { f(); }\n"
4125                "}",
4126                LLVMWithNoNamespaceFix);
4127   verifyFormat("/* something */ namespace {\n"
4128                "class A {};\n"
4129                "void f() { f(); }\n"
4130                "}",
4131                LLVMWithNoNamespaceFix);
4132   verifyFormat("inline namespace X {\n"
4133                "class A {};\n"
4134                "void f() { f(); }\n"
4135                "}",
4136                LLVMWithNoNamespaceFix);
4137   verifyFormat("/* something */ inline namespace X {\n"
4138                "class A {};\n"
4139                "void f() { f(); }\n"
4140                "}",
4141                LLVMWithNoNamespaceFix);
4142   verifyFormat("export namespace X {\n"
4143                "class A {};\n"
4144                "void f() { f(); }\n"
4145                "}",
4146                LLVMWithNoNamespaceFix);
4147   verifyFormat("using namespace some_namespace;\n"
4148                "class A {};\n"
4149                "void f() { f(); }",
4150                LLVMWithNoNamespaceFix);
4151 
4152   // This code is more common than we thought; if we
4153   // layout this correctly the semicolon will go into
4154   // its own line, which is undesirable.
4155   verifyFormat("namespace {};", LLVMWithNoNamespaceFix);
4156   verifyFormat("namespace {\n"
4157                "class A {};\n"
4158                "};",
4159                LLVMWithNoNamespaceFix);
4160 
4161   verifyFormat("namespace {\n"
4162                "int SomeVariable = 0; // comment\n"
4163                "} // namespace",
4164                LLVMWithNoNamespaceFix);
4165   verifyFormat("#ifndef HEADER_GUARD\n"
4166                "#define HEADER_GUARD\n"
4167                "namespace my_namespace {\n"
4168                "int i;\n"
4169                "} // my_namespace\n"
4170                "#endif // HEADER_GUARD",
4171                "#ifndef HEADER_GUARD\n"
4172                " #define HEADER_GUARD\n"
4173                "   namespace my_namespace {\n"
4174                "int i;\n"
4175                "}    // my_namespace\n"
4176                "#endif    // HEADER_GUARD",
4177                LLVMWithNoNamespaceFix);
4178 
4179   verifyFormat("namespace A::B {\n"
4180                "class C {};\n"
4181                "}",
4182                LLVMWithNoNamespaceFix);
4183 
4184   FormatStyle Style = getLLVMStyle();
4185   Style.NamespaceIndentation = FormatStyle::NI_All;
4186   verifyFormat("namespace out {\n"
4187                "  int i;\n"
4188                "  namespace in {\n"
4189                "    int i;\n"
4190                "  } // namespace in\n"
4191                "} // namespace out",
4192                "namespace out {\n"
4193                "int i;\n"
4194                "namespace in {\n"
4195                "int i;\n"
4196                "} // namespace in\n"
4197                "} // namespace out",
4198                Style);
4199 
4200   FormatStyle ShortInlineFunctions = getLLVMStyle();
4201   ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All;
4202   ShortInlineFunctions.AllowShortFunctionsOnASingleLine =
4203       FormatStyle::SFS_Inline;
4204   verifyFormat("namespace {\n"
4205                "  void f() {\n"
4206                "    return;\n"
4207                "  }\n"
4208                "} // namespace",
4209                ShortInlineFunctions);
4210   verifyFormat("namespace { /* comment */\n"
4211                "  void f() {\n"
4212                "    return;\n"
4213                "  }\n"
4214                "} // namespace",
4215                ShortInlineFunctions);
4216   verifyFormat("namespace { // comment\n"
4217                "  void f() {\n"
4218                "    return;\n"
4219                "  }\n"
4220                "} // namespace",
4221                ShortInlineFunctions);
4222   verifyFormat("namespace {\n"
4223                "  int some_int;\n"
4224                "  void f() {\n"
4225                "    return;\n"
4226                "  }\n"
4227                "} // namespace",
4228                ShortInlineFunctions);
4229   verifyFormat("namespace interface {\n"
4230                "  void f() {\n"
4231                "    return;\n"
4232                "  }\n"
4233                "} // namespace interface",
4234                ShortInlineFunctions);
4235   verifyFormat("namespace {\n"
4236                "  class X {\n"
4237                "    void f() { return; }\n"
4238                "  };\n"
4239                "} // namespace",
4240                ShortInlineFunctions);
4241   verifyFormat("namespace {\n"
4242                "  class X { /* comment */\n"
4243                "    void f() { return; }\n"
4244                "  };\n"
4245                "} // namespace",
4246                ShortInlineFunctions);
4247   verifyFormat("namespace {\n"
4248                "  class X { // comment\n"
4249                "    void f() { return; }\n"
4250                "  };\n"
4251                "} // namespace",
4252                ShortInlineFunctions);
4253   verifyFormat("namespace {\n"
4254                "  struct X {\n"
4255                "    void f() { return; }\n"
4256                "  };\n"
4257                "} // namespace",
4258                ShortInlineFunctions);
4259   verifyFormat("namespace {\n"
4260                "  union X {\n"
4261                "    void f() { return; }\n"
4262                "  };\n"
4263                "} // namespace",
4264                ShortInlineFunctions);
4265   verifyFormat("extern \"C\" {\n"
4266                "void f() {\n"
4267                "  return;\n"
4268                "}\n"
4269                "} // namespace",
4270                ShortInlineFunctions);
4271   verifyFormat("namespace {\n"
4272                "  class X {\n"
4273                "    void f() { return; }\n"
4274                "  } x;\n"
4275                "} // namespace",
4276                ShortInlineFunctions);
4277   verifyFormat("namespace {\n"
4278                "  [[nodiscard]] class X {\n"
4279                "    void f() { return; }\n"
4280                "  };\n"
4281                "} // namespace",
4282                ShortInlineFunctions);
4283   verifyFormat("namespace {\n"
4284                "  static class X {\n"
4285                "    void f() { return; }\n"
4286                "  } x;\n"
4287                "} // namespace",
4288                ShortInlineFunctions);
4289   verifyFormat("namespace {\n"
4290                "  constexpr class X {\n"
4291                "    void f() { return; }\n"
4292                "  } x;\n"
4293                "} // namespace",
4294                ShortInlineFunctions);
4295 
4296   ShortInlineFunctions.IndentExternBlock = FormatStyle::IEBS_Indent;
4297   verifyFormat("extern \"C\" {\n"
4298                "  void f() {\n"
4299                "    return;\n"
4300                "  }\n"
4301                "} // namespace",
4302                ShortInlineFunctions);
4303 
4304   Style.NamespaceIndentation = FormatStyle::NI_Inner;
4305   verifyFormat("namespace out {\n"
4306                "int i;\n"
4307                "namespace in {\n"
4308                "  int i;\n"
4309                "} // namespace in\n"
4310                "} // namespace out",
4311                "namespace out {\n"
4312                "int i;\n"
4313                "namespace in {\n"
4314                "int i;\n"
4315                "} // namespace in\n"
4316                "} // namespace out",
4317                Style);
4318 
4319   Style.NamespaceIndentation = FormatStyle::NI_None;
4320   verifyFormat("template <class T>\n"
4321                "concept a_concept = X<>;\n"
4322                "namespace B {\n"
4323                "struct b_struct {};\n"
4324                "} // namespace B",
4325                Style);
4326   verifyFormat("template <int I>\n"
4327                "constexpr void foo()\n"
4328                "  requires(I == 42)\n"
4329                "{}\n"
4330                "namespace ns {\n"
4331                "void foo() {}\n"
4332                "} // namespace ns",
4333                Style);
4334 
4335   FormatStyle LLVMWithCompactInnerNamespace = getLLVMStyle();
4336   LLVMWithCompactInnerNamespace.CompactNamespaces = true;
4337   LLVMWithCompactInnerNamespace.NamespaceIndentation = FormatStyle::NI_Inner;
4338   verifyFormat("namespace ns1 { namespace ns2 { namespace ns3 {\n"
4339                "// block for debug mode\n"
4340                "#ifndef NDEBUG\n"
4341                "#endif\n"
4342                "}}} // namespace ns1::ns2::ns3",
4343                LLVMWithCompactInnerNamespace);
4344 }
4345 
4346 TEST_F(FormatTest, NamespaceMacros) {
4347   FormatStyle Style = getLLVMStyle();
4348   Style.NamespaceMacros.push_back("TESTSUITE");
4349 
4350   verifyFormat("TESTSUITE(A) {\n"
4351                "int foo();\n"
4352                "} // TESTSUITE(A)",
4353                Style);
4354 
4355   verifyFormat("TESTSUITE(A, B) {\n"
4356                "int foo();\n"
4357                "} // TESTSUITE(A)",
4358                Style);
4359 
4360   // Properly indent according to NamespaceIndentation style
4361   Style.NamespaceIndentation = FormatStyle::NI_All;
4362   verifyFormat("TESTSUITE(A) {\n"
4363                "  int foo();\n"
4364                "} // TESTSUITE(A)",
4365                Style);
4366   verifyFormat("TESTSUITE(A) {\n"
4367                "  namespace B {\n"
4368                "    int foo();\n"
4369                "  } // namespace B\n"
4370                "} // TESTSUITE(A)",
4371                Style);
4372   verifyFormat("namespace A {\n"
4373                "  TESTSUITE(B) {\n"
4374                "    int foo();\n"
4375                "  } // TESTSUITE(B)\n"
4376                "} // namespace A",
4377                Style);
4378 
4379   Style.NamespaceIndentation = FormatStyle::NI_Inner;
4380   verifyFormat("TESTSUITE(A) {\n"
4381                "TESTSUITE(B) {\n"
4382                "  int foo();\n"
4383                "} // TESTSUITE(B)\n"
4384                "} // TESTSUITE(A)",
4385                Style);
4386   verifyFormat("TESTSUITE(A) {\n"
4387                "namespace B {\n"
4388                "  int foo();\n"
4389                "} // namespace B\n"
4390                "} // TESTSUITE(A)",
4391                Style);
4392   verifyFormat("namespace A {\n"
4393                "TESTSUITE(B) {\n"
4394                "  int foo();\n"
4395                "} // TESTSUITE(B)\n"
4396                "} // namespace A",
4397                Style);
4398 
4399   // Properly merge namespace-macros blocks in CompactNamespaces mode
4400   Style.NamespaceIndentation = FormatStyle::NI_None;
4401   Style.CompactNamespaces = true;
4402   verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
4403                "}} // TESTSUITE(A::B)",
4404                Style);
4405 
4406   verifyFormat("TESTSUITE(out) { TESTSUITE(in) {\n"
4407                "}} // TESTSUITE(out::in)",
4408                "TESTSUITE(out) {\n"
4409                "TESTSUITE(in) {\n"
4410                "} // TESTSUITE(in)\n"
4411                "} // TESTSUITE(out)",
4412                Style);
4413 
4414   verifyFormat("TESTSUITE(out) { TESTSUITE(in) {\n"
4415                "}} // TESTSUITE(out::in)",
4416                "TESTSUITE(out) {\n"
4417                "TESTSUITE(in) {\n"
4418                "} // TESTSUITE(in)\n"
4419                "} // TESTSUITE(out)",
4420                Style);
4421 
4422   // Do not merge different namespaces/macros
4423   verifyFormat("namespace out {\n"
4424                "TESTSUITE(in) {\n"
4425                "} // TESTSUITE(in)\n"
4426                "} // namespace out",
4427                Style);
4428   verifyFormat("TESTSUITE(out) {\n"
4429                "namespace in {\n"
4430                "} // namespace in\n"
4431                "} // TESTSUITE(out)",
4432                Style);
4433   Style.NamespaceMacros.push_back("FOOBAR");
4434   verifyFormat("TESTSUITE(out) {\n"
4435                "FOOBAR(in) {\n"
4436                "} // FOOBAR(in)\n"
4437                "} // TESTSUITE(out)",
4438                Style);
4439 }
4440 
4441 TEST_F(FormatTest, FormatsCompactNamespaces) {
4442   FormatStyle Style = getLLVMStyle();
4443   Style.CompactNamespaces = true;
4444   Style.NamespaceMacros.push_back("TESTSUITE");
4445 
4446   verifyFormat("namespace A { namespace B {\n"
4447                "}} // namespace A::B",
4448                Style);
4449 
4450   verifyFormat("namespace out { namespace in {\n"
4451                "}} // namespace out::in",
4452                "namespace out {\n"
4453                "namespace in {\n"
4454                "} // namespace in\n"
4455                "} // namespace out",
4456                Style);
4457 
4458   // Only namespaces which have both consecutive opening and end get compacted
4459   verifyFormat("namespace out {\n"
4460                "namespace in1 {\n"
4461                "} // namespace in1\n"
4462                "namespace in2 {\n"
4463                "} // namespace in2\n"
4464                "} // namespace out",
4465                Style);
4466 
4467   verifyFormat("namespace out {\n"
4468                "int i;\n"
4469                "namespace in {\n"
4470                "int j;\n"
4471                "} // namespace in\n"
4472                "int k;\n"
4473                "} // namespace out",
4474                "namespace out { int i;\n"
4475                "namespace in { int j; } // namespace in\n"
4476                "int k; } // namespace out",
4477                Style);
4478 
4479   verifyFormat("namespace A { namespace B { namespace C {\n"
4480                "}}} // namespace A::B::C",
4481                "namespace A { namespace B {\n"
4482                "namespace C {\n"
4483                "}} // namespace B::C\n"
4484                "} // namespace A",
4485                Style);
4486 
4487   Style.ColumnLimit = 40;
4488   verifyFormat("namespace aaaaaaaaaa {\n"
4489                "namespace bbbbbbbbbb {\n"
4490                "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
4491                "namespace aaaaaaaaaa {\n"
4492                "namespace bbbbbbbbbb {\n"
4493                "} // namespace bbbbbbbbbb\n"
4494                "} // namespace aaaaaaaaaa",
4495                Style);
4496 
4497   verifyFormat("namespace aaaaaa { namespace bbbbbb {\n"
4498                "namespace cccccc {\n"
4499                "}}} // namespace aaaaaa::bbbbbb::cccccc",
4500                "namespace aaaaaa {\n"
4501                "namespace bbbbbb {\n"
4502                "namespace cccccc {\n"
4503                "} // namespace cccccc\n"
4504                "} // namespace bbbbbb\n"
4505                "} // namespace aaaaaa",
4506                Style);
4507   Style.ColumnLimit = 80;
4508 
4509   // Extra semicolon after 'inner' closing brace prevents merging
4510   verifyFormat("namespace out { namespace in {\n"
4511                "}; } // namespace out::in",
4512                "namespace out {\n"
4513                "namespace in {\n"
4514                "}; // namespace in\n"
4515                "} // namespace out",
4516                Style);
4517 
4518   // Extra semicolon after 'outer' closing brace is conserved
4519   verifyFormat("namespace out { namespace in {\n"
4520                "}}; // namespace out::in",
4521                "namespace out {\n"
4522                "namespace in {\n"
4523                "} // namespace in\n"
4524                "}; // namespace out",
4525                Style);
4526 
4527   Style.NamespaceIndentation = FormatStyle::NI_All;
4528   verifyFormat("namespace out { namespace in {\n"
4529                "  int i;\n"
4530                "}} // namespace out::in",
4531                "namespace out {\n"
4532                "namespace in {\n"
4533                "int i;\n"
4534                "} // namespace in\n"
4535                "} // namespace out",
4536                Style);
4537   verifyFormat("namespace out { namespace mid {\n"
4538                "  namespace in {\n"
4539                "    int j;\n"
4540                "  } // namespace in\n"
4541                "  int k;\n"
4542                "}} // namespace out::mid",
4543                "namespace out { namespace mid {\n"
4544                "namespace in { int j; } // namespace in\n"
4545                "int k; }} // namespace out::mid",
4546                Style);
4547 
4548   verifyFormat("namespace A { namespace B { namespace C {\n"
4549                "  int i;\n"
4550                "}}} // namespace A::B::C\n"
4551                "int main() {\n"
4552                "  if (true)\n"
4553                "    return 0;\n"
4554                "}",
4555                "namespace A { namespace B {\n"
4556                "namespace C {\n"
4557                "  int i;\n"
4558                "}} // namespace B::C\n"
4559                "} // namespace A\n"
4560                "int main() {\n"
4561                "  if (true)\n"
4562                "    return 0;\n"
4563                "}",
4564                Style);
4565 
4566   verifyFormat("namespace A { namespace B { namespace C {\n"
4567                "#ifdef FOO\n"
4568                "  int i;\n"
4569                "#endif\n"
4570                "}}} // namespace A::B::C\n"
4571                "int main() {\n"
4572                "  if (true)\n"
4573                "    return 0;\n"
4574                "}",
4575                "namespace A { namespace B {\n"
4576                "namespace C {\n"
4577                "#ifdef FOO\n"
4578                "  int i;\n"
4579                "#endif\n"
4580                "}} // namespace B::C\n"
4581                "} // namespace A\n"
4582                "int main() {\n"
4583                "  if (true)\n"
4584                "    return 0;\n"
4585                "}",
4586                Style);
4587 
4588   Style.NamespaceIndentation = FormatStyle::NI_Inner;
4589   verifyFormat("namespace out { namespace in {\n"
4590                "  int i;\n"
4591                "}} // namespace out::in",
4592                "namespace out {\n"
4593                "namespace in {\n"
4594                "int i;\n"
4595                "} // namespace in\n"
4596                "} // namespace out",
4597                Style);
4598   verifyFormat("namespace out { namespace mid { namespace in {\n"
4599                "  int i;\n"
4600                "}}} // namespace out::mid::in",
4601                "namespace out {\n"
4602                "namespace mid {\n"
4603                "namespace in {\n"
4604                "int i;\n"
4605                "} // namespace in\n"
4606                "} // namespace mid\n"
4607                "} // namespace out",
4608                Style);
4609 
4610   Style.CompactNamespaces = true;
4611   Style.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
4612   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4613   Style.BraceWrapping.BeforeLambdaBody = true;
4614   verifyFormat("namespace out { namespace in {\n"
4615                "}} // namespace out::in",
4616                Style);
4617   verifyFormat("namespace out { namespace in {\n"
4618                "}} // namespace out::in",
4619                "namespace out {\n"
4620                "namespace in {\n"
4621                "} // namespace in\n"
4622                "} // namespace out",
4623                Style);
4624 }
4625 
4626 TEST_F(FormatTest, FormatsExternC) {
4627   verifyFormat("extern \"C\" {\nint a;");
4628   verifyFormat("extern \"C\" {}");
4629   verifyFormat("extern \"C\" {\n"
4630                "int foo();\n"
4631                "}");
4632   verifyFormat("extern \"C\" int foo() {}");
4633   verifyFormat("extern \"C\" int foo();");
4634   verifyFormat("extern \"C\" int foo() {\n"
4635                "  int i = 42;\n"
4636                "  return i;\n"
4637                "}");
4638 
4639   FormatStyle Style = getLLVMStyle();
4640   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4641   Style.BraceWrapping.AfterFunction = true;
4642   verifyFormat("extern \"C\" int foo() {}", Style);
4643   verifyFormat("extern \"C\" int foo();", Style);
4644   verifyFormat("extern \"C\" int foo()\n"
4645                "{\n"
4646                "  int i = 42;\n"
4647                "  return i;\n"
4648                "}",
4649                Style);
4650 
4651   Style.BraceWrapping.AfterExternBlock = true;
4652   Style.BraceWrapping.SplitEmptyRecord = false;
4653   verifyFormat("extern \"C\"\n"
4654                "{}",
4655                Style);
4656   verifyFormat("extern \"C\"\n"
4657                "{\n"
4658                "  int foo();\n"
4659                "}",
4660                Style);
4661 }
4662 
4663 TEST_F(FormatTest, IndentExternBlockStyle) {
4664   FormatStyle Style = getLLVMStyle();
4665   Style.IndentWidth = 2;
4666 
4667   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4668   verifyFormat("extern \"C\" { /*9*/\n"
4669                "}",
4670                Style);
4671   verifyFormat("extern \"C\" {\n"
4672                "  int foo10();\n"
4673                "}",
4674                Style);
4675 
4676   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4677   verifyFormat("extern \"C\" { /*11*/\n"
4678                "}",
4679                Style);
4680   verifyFormat("extern \"C\" {\n"
4681                "int foo12();\n"
4682                "}",
4683                Style);
4684 
4685   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4686   verifyFormat("extern \"C\"\n"
4687                "{\n"
4688                "int i;\n"
4689                "}",
4690                Style);
4691 
4692   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4693   Style.BraceWrapping.AfterExternBlock = true;
4694   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4695   verifyFormat("extern \"C\"\n"
4696                "{ /*13*/\n"
4697                "}",
4698                Style);
4699   verifyFormat("extern \"C\"\n{\n"
4700                "  int foo14();\n"
4701                "}",
4702                Style);
4703 
4704   Style.BraceWrapping.AfterExternBlock = false;
4705   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4706   verifyFormat("extern \"C\" { /*15*/\n"
4707                "}",
4708                Style);
4709   verifyFormat("extern \"C\" {\n"
4710                "int foo16();\n"
4711                "}",
4712                Style);
4713 
4714   Style.BraceWrapping.AfterExternBlock = true;
4715   verifyFormat("extern \"C\"\n"
4716                "{ /*13*/\n"
4717                "}",
4718                Style);
4719   verifyFormat("extern \"C\"\n"
4720                "{\n"
4721                "int foo14();\n"
4722                "}",
4723                Style);
4724 
4725   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4726   verifyFormat("extern \"C\"\n"
4727                "{ /*13*/\n"
4728                "}",
4729                Style);
4730   verifyFormat("extern \"C\"\n"
4731                "{\n"
4732                "  int foo14();\n"
4733                "}",
4734                Style);
4735 }
4736 
4737 TEST_F(FormatTest, FormatsInlineASM) {
4738   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
4739   verifyFormat("asm(\"nop\" ::: \"memory\");");
4740   verifyFormat(
4741       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
4742       "    \"cpuid\\n\\t\"\n"
4743       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
4744       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
4745       "    : \"a\"(value));");
4746   verifyFormat(
4747       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
4748       "  __asm {\n"
4749       "        mov     edx,[that] // vtable in edx\n"
4750       "        mov     eax,methodIndex\n"
4751       "        call    [edx][eax*4] // stdcall\n"
4752       "  }\n"
4753       "}",
4754       "void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
4755       "    __asm {\n"
4756       "        mov     edx,[that] // vtable in edx\n"
4757       "        mov     eax,methodIndex\n"
4758       "        call    [edx][eax*4] // stdcall\n"
4759       "    }\n"
4760       "}");
4761   verifyNoChange("_asm {\n"
4762                  "  xor eax, eax;\n"
4763                  "  cpuid;\n"
4764                  "}");
4765   verifyFormat("void function() {\n"
4766                "  // comment\n"
4767                "  asm(\"\");\n"
4768                "}");
4769   verifyFormat("__asm {\n"
4770                "}\n"
4771                "int i;",
4772                "__asm   {\n"
4773                "}\n"
4774                "int   i;");
4775 
4776   auto Style = getLLVMStyleWithColumns(0);
4777   const StringRef Code1{"asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"};
4778   const StringRef Code2{"asm(\"xyz\"\n"
4779                         "    : \"=a\"(a), \"=d\"(b)\n"
4780                         "    : \"a\"(data));"};
4781   const StringRef Code3{"asm(\"xyz\" : \"=a\"(a), \"=d\"(b)\n"
4782                         "    : \"a\"(data));"};
4783 
4784   Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_OnlyMultiline;
4785   verifyFormat(Code1, Style);
4786   verifyNoChange(Code2, Style);
4787   verifyNoChange(Code3, Style);
4788 
4789   Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Always;
4790   verifyFormat(Code2, Code1, Style);
4791   verifyNoChange(Code2, Style);
4792   verifyFormat(Code2, Code3, Style);
4793 }
4794 
4795 TEST_F(FormatTest, FormatTryCatch) {
4796   verifyFormat("try {\n"
4797                "  throw a * b;\n"
4798                "} catch (int a) {\n"
4799                "  // Do nothing.\n"
4800                "} catch (...) {\n"
4801                "  exit(42);\n"
4802                "}");
4803 
4804   // Function-level try statements.
4805   verifyFormat("int f() try { return 4; } catch (...) {\n"
4806                "  return 5;\n"
4807                "}");
4808   verifyFormat("class A {\n"
4809                "  int a;\n"
4810                "  A() try : a(0) {\n"
4811                "  } catch (...) {\n"
4812                "    throw;\n"
4813                "  }\n"
4814                "};");
4815   verifyFormat("class A {\n"
4816                "  int a;\n"
4817                "  A() try : a(0), b{1} {\n"
4818                "  } catch (...) {\n"
4819                "    throw;\n"
4820                "  }\n"
4821                "};");
4822   verifyFormat("class A {\n"
4823                "  int a;\n"
4824                "  A() try : a(0), b{1}, c{2} {\n"
4825                "  } catch (...) {\n"
4826                "    throw;\n"
4827                "  }\n"
4828                "};");
4829   verifyFormat("class A {\n"
4830                "  int a;\n"
4831                "  A() try : a(0), b{1}, c{2} {\n"
4832                "    { // New scope.\n"
4833                "    }\n"
4834                "  } catch (...) {\n"
4835                "    throw;\n"
4836                "  }\n"
4837                "};");
4838 
4839   // Incomplete try-catch blocks.
4840   verifyIncompleteFormat("try {} catch (");
4841 }
4842 
4843 TEST_F(FormatTest, FormatTryAsAVariable) {
4844   verifyFormat("int try;");
4845   verifyFormat("int try, size;");
4846   verifyFormat("try = foo();");
4847   verifyFormat("if (try < size) {\n  return true;\n}");
4848 
4849   verifyFormat("int catch;");
4850   verifyFormat("int catch, size;");
4851   verifyFormat("catch = foo();");
4852   verifyFormat("if (catch < size) {\n  return true;\n}");
4853 
4854   FormatStyle Style = getLLVMStyle();
4855   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4856   Style.BraceWrapping.AfterFunction = true;
4857   Style.BraceWrapping.BeforeCatch = true;
4858   verifyFormat("try {\n"
4859                "  int bar = 1;\n"
4860                "}\n"
4861                "catch (...) {\n"
4862                "  int bar = 1;\n"
4863                "}",
4864                Style);
4865   verifyFormat("#if NO_EX\n"
4866                "try\n"
4867                "#endif\n"
4868                "{\n"
4869                "}\n"
4870                "#if NO_EX\n"
4871                "catch (...) {\n"
4872                "}",
4873                Style);
4874   verifyFormat("try /* abc */ {\n"
4875                "  int bar = 1;\n"
4876                "}\n"
4877                "catch (...) {\n"
4878                "  int bar = 1;\n"
4879                "}",
4880                Style);
4881   verifyFormat("try\n"
4882                "// abc\n"
4883                "{\n"
4884                "  int bar = 1;\n"
4885                "}\n"
4886                "catch (...) {\n"
4887                "  int bar = 1;\n"
4888                "}",
4889                Style);
4890 }
4891 
4892 TEST_F(FormatTest, FormatSEHTryCatch) {
4893   verifyFormat("__try {\n"
4894                "  int a = b * c;\n"
4895                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
4896                "  // Do nothing.\n"
4897                "}");
4898 
4899   verifyFormat("__try {\n"
4900                "  int a = b * c;\n"
4901                "} __finally {\n"
4902                "  // Do nothing.\n"
4903                "}");
4904 
4905   verifyFormat("DEBUG({\n"
4906                "  __try {\n"
4907                "  } __finally {\n"
4908                "  }\n"
4909                "});");
4910 }
4911 
4912 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
4913   verifyFormat("try {\n"
4914                "  f();\n"
4915                "} catch {\n"
4916                "  g();\n"
4917                "}");
4918   verifyFormat("try {\n"
4919                "  f();\n"
4920                "} catch (A a) MACRO(x) {\n"
4921                "  g();\n"
4922                "} catch (B b) MACRO(x) {\n"
4923                "  g();\n"
4924                "}");
4925 }
4926 
4927 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
4928   FormatStyle Style = getLLVMStyle();
4929   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
4930                           FormatStyle::BS_WebKit}) {
4931     Style.BreakBeforeBraces = BraceStyle;
4932     verifyFormat("try {\n"
4933                  "  // something\n"
4934                  "} catch (...) {\n"
4935                  "  // something\n"
4936                  "}",
4937                  Style);
4938   }
4939   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4940   verifyFormat("try {\n"
4941                "  // something\n"
4942                "}\n"
4943                "catch (...) {\n"
4944                "  // something\n"
4945                "}",
4946                Style);
4947   verifyFormat("__try {\n"
4948                "  // something\n"
4949                "}\n"
4950                "__finally {\n"
4951                "  // something\n"
4952                "}",
4953                Style);
4954   verifyFormat("@try {\n"
4955                "  // something\n"
4956                "}\n"
4957                "@finally {\n"
4958                "  // something\n"
4959                "}",
4960                Style);
4961   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4962   verifyFormat("try\n"
4963                "{\n"
4964                "  // something\n"
4965                "}\n"
4966                "catch (...)\n"
4967                "{\n"
4968                "  // something\n"
4969                "}",
4970                Style);
4971   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
4972   verifyFormat("try\n"
4973                "  {\n"
4974                "  // something white\n"
4975                "  }\n"
4976                "catch (...)\n"
4977                "  {\n"
4978                "  // something white\n"
4979                "  }",
4980                Style);
4981   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
4982   verifyFormat("try\n"
4983                "  {\n"
4984                "    // something\n"
4985                "  }\n"
4986                "catch (...)\n"
4987                "  {\n"
4988                "    // something\n"
4989                "  }",
4990                Style);
4991   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4992   Style.BraceWrapping.BeforeCatch = true;
4993   verifyFormat("try {\n"
4994                "  // something\n"
4995                "}\n"
4996                "catch (...) {\n"
4997                "  // something\n"
4998                "}",
4999                Style);
5000 }
5001 
5002 TEST_F(FormatTest, StaticInitializers) {
5003   verifyFormat("static SomeClass SC = {1, 'a'};");
5004 
5005   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
5006                "    100000000, "
5007                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
5008 
5009   // Here, everything other than the "}" would fit on a line.
5010   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
5011                "    10000000000000000000000000};");
5012   verifyFormat("S s = {a,\n"
5013                "\n"
5014                "       b};",
5015                "S s = {\n"
5016                "  a,\n"
5017                "\n"
5018                "  b\n"
5019                "};");
5020 
5021   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
5022   // line. However, the formatting looks a bit off and this probably doesn't
5023   // happen often in practice.
5024   verifyFormat("static int Variable[1] = {\n"
5025                "    {1000000000000000000000000000000000000}};",
5026                getLLVMStyleWithColumns(40));
5027 }
5028 
5029 TEST_F(FormatTest, DesignatedInitializers) {
5030   verifyFormat("const struct A a = {.a = 1, .b = 2};");
5031   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
5032                "                    .bbbbbbbbbb = 2,\n"
5033                "                    .cccccccccc = 3,\n"
5034                "                    .dddddddddd = 4,\n"
5035                "                    .eeeeeeeeee = 5};");
5036   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
5037                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
5038                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
5039                "    .ccccccccccccccccccccccccccc = 3,\n"
5040                "    .ddddddddddddddddddddddddddd = 4,\n"
5041                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
5042 
5043   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
5044 
5045   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
5046   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
5047                "                    [2] = bbbbbbbbbb,\n"
5048                "                    [3] = cccccccccc,\n"
5049                "                    [4] = dddddddddd,\n"
5050                "                    [5] = eeeeeeeeee};");
5051   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
5052                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5053                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
5054                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
5055                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
5056                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
5057 
5058   verifyFormat("for (const TestCase &test_case : {\n"
5059                "         TestCase{\n"
5060                "             .a = 1,\n"
5061                "             .b = 1,\n"
5062                "         },\n"
5063                "         TestCase{\n"
5064                "             .a = 2,\n"
5065                "             .b = 2,\n"
5066                "         },\n"
5067                "     }) {\n"
5068                "}");
5069 }
5070 
5071 TEST_F(FormatTest, BracedInitializerIndentWidth) {
5072   auto Style = getLLVMStyleWithColumns(60);
5073   Style.BinPackArguments = true;
5074   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5075   Style.BracedInitializerIndentWidth = 6;
5076 
5077   // Non-initializing braces are unaffected by BracedInitializerIndentWidth.
5078   verifyFormat("enum class {\n"
5079                "  One,\n"
5080                "  Two,\n"
5081                "};",
5082                Style);
5083   verifyFormat("class Foo {\n"
5084                "  Foo() {}\n"
5085                "  void bar();\n"
5086                "};",
5087                Style);
5088   verifyFormat("void foo() {\n"
5089                "  auto bar = baz;\n"
5090                "  return baz;\n"
5091                "};",
5092                Style);
5093   verifyFormat("auto foo = [&] {\n"
5094                "  auto bar = baz;\n"
5095                "  return baz;\n"
5096                "};",
5097                Style);
5098   verifyFormat("{\n"
5099                "  auto bar = baz;\n"
5100                "  return baz;\n"
5101                "};",
5102                Style);
5103   // Non-brace initialization is unaffected by BracedInitializerIndentWidth.
5104   verifyFormat("SomeClass clazz(\n"
5105                "    \"xxxxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyyyy\",\n"
5106                "    \"zzzzzzzzzzzzzzzzzz\");",
5107                Style);
5108 
5109   // The following types of initialization are all affected by
5110   // BracedInitializerIndentWidth. Aggregate initialization.
5111   verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
5112                "      10000000, 20000000};",
5113                Style);
5114   verifyFormat("SomeStruct s{\n"
5115                "      \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n"
5116                "      \"zzzzzzzzzzzzzzzz\"};",
5117                Style);
5118   // Designated initializers.
5119   verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
5120                "      [0] = 10000000, [1] = 20000000};",
5121                Style);
5122   verifyFormat("SomeStruct s{\n"
5123                "      .foo = \"xxxxxxxxxxxxx\",\n"
5124                "      .bar = \"yyyyyyyyyyyyy\",\n"
5125                "      .baz = \"zzzzzzzzzzzzz\"};",
5126                Style);
5127   // List initialization.
5128   verifyFormat("SomeStruct s{\n"
5129                "      \"xxxxxxxxxxxxx\",\n"
5130                "      \"yyyyyyyyyyyyy\",\n"
5131                "      \"zzzzzzzzzzzzz\",\n"
5132                "};",
5133                Style);
5134   verifyFormat("SomeStruct{\n"
5135                "      \"xxxxxxxxxxxxx\",\n"
5136                "      \"yyyyyyyyyyyyy\",\n"
5137                "      \"zzzzzzzzzzzzz\",\n"
5138                "};",
5139                Style);
5140   verifyFormat("new SomeStruct{\n"
5141                "      \"xxxxxxxxxxxxx\",\n"
5142                "      \"yyyyyyyyyyyyy\",\n"
5143                "      \"zzzzzzzzzzzzz\",\n"
5144                "};",
5145                Style);
5146   // Member initializer.
5147   verifyFormat("class SomeClass {\n"
5148                "  SomeStruct s{\n"
5149                "        \"xxxxxxxxxxxxx\",\n"
5150                "        \"yyyyyyyyyyyyy\",\n"
5151                "        \"zzzzzzzzzzzzz\",\n"
5152                "  };\n"
5153                "};",
5154                Style);
5155   // Constructor member initializer.
5156   verifyFormat("SomeClass::SomeClass : strct{\n"
5157                "                             \"xxxxxxxxxxxxx\",\n"
5158                "                             \"yyyyyyyyyyyyy\",\n"
5159                "                             \"zzzzzzzzzzzzz\",\n"
5160                "                       } {}",
5161                Style);
5162   // Copy initialization.
5163   verifyFormat("SomeStruct s = SomeStruct{\n"
5164                "      \"xxxxxxxxxxxxx\",\n"
5165                "      \"yyyyyyyyyyyyy\",\n"
5166                "      \"zzzzzzzzzzzzz\",\n"
5167                "};",
5168                Style);
5169   // Copy list initialization.
5170   verifyFormat("SomeStruct s = {\n"
5171                "      \"xxxxxxxxxxxxx\",\n"
5172                "      \"yyyyyyyyyyyyy\",\n"
5173                "      \"zzzzzzzzzzzzz\",\n"
5174                "};",
5175                Style);
5176   // Assignment operand initialization.
5177   verifyFormat("s = {\n"
5178                "      \"xxxxxxxxxxxxx\",\n"
5179                "      \"yyyyyyyyyyyyy\",\n"
5180                "      \"zzzzzzzzzzzzz\",\n"
5181                "};",
5182                Style);
5183   // Returned object initialization.
5184   verifyFormat("return {\n"
5185                "      \"xxxxxxxxxxxxx\",\n"
5186                "      \"yyyyyyyyyyyyy\",\n"
5187                "      \"zzzzzzzzzzzzz\",\n"
5188                "};",
5189                Style);
5190   // Initializer list.
5191   verifyFormat("auto initializerList = {\n"
5192                "      \"xxxxxxxxxxxxx\",\n"
5193                "      \"yyyyyyyyyyyyy\",\n"
5194                "      \"zzzzzzzzzzzzz\",\n"
5195                "};",
5196                Style);
5197   // Function parameter initialization.
5198   verifyFormat("func({\n"
5199                "      \"xxxxxxxxxxxxx\",\n"
5200                "      \"yyyyyyyyyyyyy\",\n"
5201                "      \"zzzzzzzzzzzzz\",\n"
5202                "});",
5203                Style);
5204   // Nested init lists.
5205   verifyFormat("SomeStruct s = {\n"
5206                "      {{init1, init2, init3, init4, init5},\n"
5207                "       {init1, init2, init3, init4, init5}}};",
5208                Style);
5209   verifyFormat("SomeStruct s = {\n"
5210                "      {{\n"
5211                "             .init1 = 1,\n"
5212                "             .init2 = 2,\n"
5213                "             .init3 = 3,\n"
5214                "             .init4 = 4,\n"
5215                "             .init5 = 5,\n"
5216                "       },\n"
5217                "       {init1, init2, init3, init4, init5}}};",
5218                Style);
5219   verifyFormat("SomeArrayT a[3] = {\n"
5220                "      {\n"
5221                "            foo,\n"
5222                "            bar,\n"
5223                "      },\n"
5224                "      {\n"
5225                "            foo,\n"
5226                "            bar,\n"
5227                "      },\n"
5228                "      SomeArrayT{},\n"
5229                "};",
5230                Style);
5231   verifyFormat("SomeArrayT a[3] = {\n"
5232                "      {foo},\n"
5233                "      {\n"
5234                "            {\n"
5235                "                  init1,\n"
5236                "                  init2,\n"
5237                "                  init3,\n"
5238                "            },\n"
5239                "            {\n"
5240                "                  init1,\n"
5241                "                  init2,\n"
5242                "                  init3,\n"
5243                "            },\n"
5244                "      },\n"
5245                "      {baz},\n"
5246                "};",
5247                Style);
5248 
5249   // Aligning after open braces unaffected by BracedInitializerIndentWidth.
5250   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
5251   verifyFormat("SomeStruct s{\"xxxxxxxxxxxxx\", \"yyyyyyyyyyyyy\",\n"
5252                "             \"zzzzzzzzzzzzz\"};",
5253                Style);
5254 }
5255 
5256 TEST_F(FormatTest, NestedStaticInitializers) {
5257   verifyFormat("static A x = {{{}}};");
5258   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
5259                "               {init1, init2, init3, init4}}};",
5260                getLLVMStyleWithColumns(50));
5261 
5262   verifyFormat("somes Status::global_reps[3] = {\n"
5263                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
5264                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
5265                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
5266                getLLVMStyleWithColumns(60));
5267   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
5268                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
5269                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
5270                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
5271   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
5272                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
5273                "rect.fTop}};");
5274 
5275   verifyFormat(
5276       "SomeArrayOfSomeType a = {\n"
5277       "    {{1, 2, 3},\n"
5278       "     {1, 2, 3},\n"
5279       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
5280       "      333333333333333333333333333333},\n"
5281       "     {1, 2, 3},\n"
5282       "     {1, 2, 3}}};");
5283   verifyFormat(
5284       "SomeArrayOfSomeType a = {\n"
5285       "    {{1, 2, 3}},\n"
5286       "    {{1, 2, 3}},\n"
5287       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
5288       "      333333333333333333333333333333}},\n"
5289       "    {{1, 2, 3}},\n"
5290       "    {{1, 2, 3}}};");
5291 
5292   verifyFormat("struct {\n"
5293                "  unsigned bit;\n"
5294                "  const char *const name;\n"
5295                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
5296                "                 {kOsWin, \"Windows\"},\n"
5297                "                 {kOsLinux, \"Linux\"},\n"
5298                "                 {kOsCrOS, \"Chrome OS\"}};");
5299   verifyFormat("struct {\n"
5300                "  unsigned bit;\n"
5301                "  const char *const name;\n"
5302                "} kBitsToOs[] = {\n"
5303                "    {kOsMac, \"Mac\"},\n"
5304                "    {kOsWin, \"Windows\"},\n"
5305                "    {kOsLinux, \"Linux\"},\n"
5306                "    {kOsCrOS, \"Chrome OS\"},\n"
5307                "};");
5308 }
5309 
5310 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
5311   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
5312                "                      \\\n"
5313                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
5314 }
5315 
5316 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
5317   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
5318                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
5319 
5320   // Do break defaulted and deleted functions.
5321   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
5322                "    default;",
5323                getLLVMStyleWithColumns(40));
5324   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
5325                "    delete;",
5326                getLLVMStyleWithColumns(40));
5327 }
5328 
5329 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
5330   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
5331                getLLVMStyleWithColumns(40));
5332   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
5333                getLLVMStyleWithColumns(40));
5334   verifyFormat("#define Q                              \\\n"
5335                "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
5336                "  \"aaaaaaaa.cpp\"",
5337                "#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
5338                getLLVMStyleWithColumns(40));
5339 }
5340 
5341 TEST_F(FormatTest, UnderstandsLinePPDirective) {
5342   verifyFormat("# 123 \"A string literal\"",
5343                "   #     123    \"A string literal\"");
5344 }
5345 
5346 TEST_F(FormatTest, LayoutUnknownPPDirective) {
5347   verifyFormat("#;");
5348   verifyFormat("#\n;\n;\n;");
5349 }
5350 
5351 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
5352   verifyFormat("#line 42 \"test\"", "#  \\\n  line  \\\n  42  \\\n  \"test\"");
5353   verifyFormat("#define A B", "#  \\\n define  \\\n    A  \\\n       B",
5354                getLLVMStyleWithColumns(12));
5355 }
5356 
5357 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
5358   verifyFormat("#line 42 \"test\"", "#  \\\n  line  \\\n  42  \\\n  \"test\"");
5359   verifyFormat("#define A B", "#  \\\n define  \\\n    A  \\\n       B");
5360 }
5361 
5362 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
5363   verifyFormat("#define A \\x20");
5364   verifyFormat("#define A \\ x20");
5365   verifyFormat("#define A \\ x20", "#define A \\   x20");
5366   verifyFormat("#define A ''");
5367   verifyFormat("#define A ''qqq");
5368   verifyFormat("#define A `qqq");
5369   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
5370   verifyFormat("const char *c = STRINGIFY(\n"
5371                "\\na : b);",
5372                "const char * c = STRINGIFY(\n"
5373                "\\na : b);");
5374 
5375   verifyFormat("a\r\\");
5376   verifyFormat("a\v\\");
5377   verifyFormat("a\f\\");
5378 }
5379 
5380 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
5381   FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp);
5382   style.IndentWidth = 4;
5383   style.PPIndentWidth = 1;
5384 
5385   style.IndentPPDirectives = FormatStyle::PPDIS_None;
5386   verifyFormat("#ifdef __linux__\n"
5387                "void foo() {\n"
5388                "    int x = 0;\n"
5389                "}\n"
5390                "#define FOO\n"
5391                "#endif\n"
5392                "void bar() {\n"
5393                "    int y = 0;\n"
5394                "}",
5395                style);
5396 
5397   style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
5398   verifyFormat("#ifdef __linux__\n"
5399                "void foo() {\n"
5400                "    int x = 0;\n"
5401                "}\n"
5402                "# define FOO foo\n"
5403                "#endif\n"
5404                "void bar() {\n"
5405                "    int y = 0;\n"
5406                "}",
5407                style);
5408 
5409   style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
5410   verifyFormat("#ifdef __linux__\n"
5411                "void foo() {\n"
5412                "    int x = 0;\n"
5413                "}\n"
5414                " #define FOO foo\n"
5415                "#endif\n"
5416                "void bar() {\n"
5417                "    int y = 0;\n"
5418                "}",
5419                style);
5420   verifyFormat("#if 1\n"
5421                " // some comments\n"
5422                " // another\n"
5423                " #define foo 1\n"
5424                "// not a define comment\n"
5425                "void bar() {\n"
5426                "    // comment\n"
5427                "    int y = 0;\n"
5428                "}",
5429                "#if 1\n"
5430                "// some comments\n"
5431                "// another\n"
5432                "#define foo 1\n"
5433                "// not a define comment\n"
5434                "void bar() {\n"
5435                "  // comment\n"
5436                "  int y = 0;\n"
5437                "}",
5438                style);
5439 
5440   style.IndentPPDirectives = FormatStyle::PPDIS_None;
5441   verifyFormat("#ifdef foo\n"
5442                "#define bar() \\\n"
5443                "    if (A) {  \\\n"
5444                "        B();  \\\n"
5445                "    }         \\\n"
5446                "    C();\n"
5447                "#endif",
5448                style);
5449   verifyFormat("if (emacs) {\n"
5450                "#ifdef is\n"
5451                "#define lit           \\\n"
5452                "    if (af) {         \\\n"
5453                "        return duh(); \\\n"
5454                "    }\n"
5455                "#endif\n"
5456                "}",
5457                style);
5458   verifyFormat("#if abc\n"
5459                "#ifdef foo\n"
5460                "#define bar()    \\\n"
5461                "    if (A) {     \\\n"
5462                "        if (B) { \\\n"
5463                "            C(); \\\n"
5464                "        }        \\\n"
5465                "    }            \\\n"
5466                "    D();\n"
5467                "#endif\n"
5468                "#endif",
5469                style);
5470   verifyFormat("#ifndef foo\n"
5471                "#define foo\n"
5472                "if (emacs) {\n"
5473                "#ifdef is\n"
5474                "#define lit           \\\n"
5475                "    if (af) {         \\\n"
5476                "        return duh(); \\\n"
5477                "    }\n"
5478                "#endif\n"
5479                "}\n"
5480                "#endif",
5481                style);
5482   verifyFormat("#if 1\n"
5483                "#define X  \\\n"
5484                "    {      \\\n"
5485                "        x; \\\n"
5486                "        x; \\\n"
5487                "    }\n"
5488                "#endif",
5489                style);
5490   verifyFormat("#define X  \\\n"
5491                "    {      \\\n"
5492                "        x; \\\n"
5493                "        x; \\\n"
5494                "    }",
5495                style);
5496 
5497   style.PPIndentWidth = 2;
5498   verifyFormat("#ifdef foo\n"
5499                "#define bar() \\\n"
5500                "    if (A) {  \\\n"
5501                "        B();  \\\n"
5502                "    }         \\\n"
5503                "    C();\n"
5504                "#endif",
5505                style);
5506   style.IndentWidth = 8;
5507   verifyFormat("#ifdef foo\n"
5508                "#define bar()        \\\n"
5509                "        if (A) {     \\\n"
5510                "                B(); \\\n"
5511                "        }            \\\n"
5512                "        C();\n"
5513                "#endif",
5514                style);
5515 
5516   style.IndentWidth = 1;
5517   style.PPIndentWidth = 4;
5518   verifyFormat("#if 1\n"
5519                "#define X \\\n"
5520                " {        \\\n"
5521                "  x;      \\\n"
5522                "  x;      \\\n"
5523                " }\n"
5524                "#endif",
5525                style);
5526   verifyFormat("#define X \\\n"
5527                " {        \\\n"
5528                "  x;      \\\n"
5529                "  x;      \\\n"
5530                " }",
5531                style);
5532 
5533   style.IndentWidth = 4;
5534   style.PPIndentWidth = 1;
5535   style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
5536   verifyFormat("#ifdef foo\n"
5537                "# define bar() \\\n"
5538                "     if (A) {  \\\n"
5539                "         B();  \\\n"
5540                "     }         \\\n"
5541                "     C();\n"
5542                "#endif",
5543                style);
5544   verifyFormat("#if abc\n"
5545                "# ifdef foo\n"
5546                "#  define bar()    \\\n"
5547                "      if (A) {     \\\n"
5548                "          if (B) { \\\n"
5549                "              C(); \\\n"
5550                "          }        \\\n"
5551                "      }            \\\n"
5552                "      D();\n"
5553                "# endif\n"
5554                "#endif",
5555                style);
5556   verifyFormat("#ifndef foo\n"
5557                "#define foo\n"
5558                "if (emacs) {\n"
5559                "#ifdef is\n"
5560                "# define lit           \\\n"
5561                "     if (af) {         \\\n"
5562                "         return duh(); \\\n"
5563                "     }\n"
5564                "#endif\n"
5565                "}\n"
5566                "#endif",
5567                style);
5568   verifyFormat("#define X  \\\n"
5569                "    {      \\\n"
5570                "        x; \\\n"
5571                "        x; \\\n"
5572                "    }",
5573                style);
5574 
5575   style.PPIndentWidth = 2;
5576   style.IndentWidth = 8;
5577   verifyFormat("#ifdef foo\n"
5578                "#  define bar()        \\\n"
5579                "          if (A) {     \\\n"
5580                "                  B(); \\\n"
5581                "          }            \\\n"
5582                "          C();\n"
5583                "#endif",
5584                style);
5585 
5586   style.PPIndentWidth = 4;
5587   style.IndentWidth = 1;
5588   verifyFormat("#define X \\\n"
5589                " {        \\\n"
5590                "  x;      \\\n"
5591                "  x;      \\\n"
5592                " }",
5593                style);
5594 
5595   style.IndentWidth = 4;
5596   style.PPIndentWidth = 1;
5597   style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
5598   verifyFormat("if (emacs) {\n"
5599                "#ifdef is\n"
5600                " #define lit           \\\n"
5601                "     if (af) {         \\\n"
5602                "         return duh(); \\\n"
5603                "     }\n"
5604                "#endif\n"
5605                "}",
5606                style);
5607   verifyFormat("#if abc\n"
5608                " #ifdef foo\n"
5609                "  #define bar() \\\n"
5610                "      if (A) {  \\\n"
5611                "          B();  \\\n"
5612                "      }         \\\n"
5613                "      C();\n"
5614                " #endif\n"
5615                "#endif",
5616                style);
5617   verifyFormat("#if 1\n"
5618                " #define X  \\\n"
5619                "     {      \\\n"
5620                "         x; \\\n"
5621                "         x; \\\n"
5622                "     }\n"
5623                "#endif",
5624                style);
5625 
5626   style.PPIndentWidth = 2;
5627   verifyFormat("#ifdef foo\n"
5628                "  #define bar() \\\n"
5629                "      if (A) {  \\\n"
5630                "          B();  \\\n"
5631                "      }         \\\n"
5632                "      C();\n"
5633                "#endif",
5634                style);
5635 
5636   style.PPIndentWidth = 4;
5637   style.IndentWidth = 1;
5638   verifyFormat("#if 1\n"
5639                "    #define X \\\n"
5640                "     {        \\\n"
5641                "      x;      \\\n"
5642                "      x;      \\\n"
5643                "     }\n"
5644                "#endif",
5645                style);
5646 }
5647 
5648 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
5649   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
5650   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
5651   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
5652   // FIXME: We never break before the macro name.
5653   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
5654 
5655   verifyFormat("#define A A\n#define A A");
5656   verifyFormat("#define A(X) A\n#define A A");
5657 
5658   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
5659   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
5660 }
5661 
5662 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
5663   verifyFormat("// somecomment\n"
5664                "#include \"a.h\"\n"
5665                "#define A(  \\\n"
5666                "    A, B)\n"
5667                "#include \"b.h\"\n"
5668                "// somecomment",
5669                "  // somecomment\n"
5670                "  #include \"a.h\"\n"
5671                "#define A(A,\\\n"
5672                "    B)\n"
5673                "    #include \"b.h\"\n"
5674                " // somecomment",
5675                getLLVMStyleWithColumns(13));
5676 }
5677 
5678 TEST_F(FormatTest, LayoutSingleHash) { verifyFormat("#\na;"); }
5679 
5680 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
5681   verifyFormat("#define A    \\\n"
5682                "  c;         \\\n"
5683                "  e;\n"
5684                "f;",
5685                "#define A c; e;\n"
5686                "f;",
5687                getLLVMStyleWithColumns(14));
5688 }
5689 
5690 TEST_F(FormatTest, LayoutRemainingTokens) {
5691   verifyFormat("{\n"
5692                "}");
5693 }
5694 
5695 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
5696   verifyFormat("int x,\n"
5697                "#define A\n"
5698                "    y;",
5699                "int x,\n#define A\ny;");
5700 }
5701 
5702 TEST_F(FormatTest, HashInMacroDefinition) {
5703   verifyFormat("#define A(c) L#c");
5704   verifyFormat("#define A(c) u#c");
5705   verifyFormat("#define A(c) U#c");
5706   verifyFormat("#define A(c) u8#c");
5707   verifyFormat("#define A(c) LR#c");
5708   verifyFormat("#define A(c) uR#c");
5709   verifyFormat("#define A(c) UR#c");
5710   verifyFormat("#define A(c) u8R#c");
5711   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
5712   verifyFormat("#define A  \\\n"
5713                "  {        \\\n"
5714                "    f(#c); \\\n"
5715                "  }",
5716                getLLVMStyleWithColumns(11));
5717 
5718   verifyFormat("#define A(X)         \\\n"
5719                "  void function##X()",
5720                getLLVMStyleWithColumns(22));
5721 
5722   verifyFormat("#define A(a, b, c)   \\\n"
5723                "  void a##b##c()",
5724                getLLVMStyleWithColumns(22));
5725 
5726   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
5727 }
5728 
5729 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
5730   verifyFormat("#define A (x)");
5731   verifyFormat("#define A(x)");
5732 
5733   FormatStyle Style = getLLVMStyle();
5734   Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
5735   verifyFormat("#define true ((foo)1)", Style);
5736   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
5737   verifyFormat("#define false((foo)0)", Style);
5738 }
5739 
5740 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
5741   verifyFormat("#define A b;",
5742                "#define A \\\n"
5743                "          \\\n"
5744                "  b;",
5745                getLLVMStyleWithColumns(25));
5746   verifyNoChange("#define A \\\n"
5747                  "          \\\n"
5748                  "  a;      \\\n"
5749                  "  b;",
5750                  getLLVMStyleWithColumns(11));
5751   verifyNoChange("#define A \\\n"
5752                  "  a;      \\\n"
5753                  "          \\\n"
5754                  "  b;",
5755                  getLLVMStyleWithColumns(11));
5756 }
5757 
5758 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
5759   verifyIncompleteFormat("#define A :");
5760   verifyFormat("#define SOMECASES  \\\n"
5761                "  case 1:          \\\n"
5762                "  case 2",
5763                getLLVMStyleWithColumns(20));
5764   verifyFormat("#define MACRO(a) \\\n"
5765                "  if (a)         \\\n"
5766                "    f();         \\\n"
5767                "  else           \\\n"
5768                "    g()",
5769                getLLVMStyleWithColumns(18));
5770   verifyFormat("#define A template <typename T>");
5771   verifyIncompleteFormat("#define STR(x) #x\n"
5772                          "f(STR(this_is_a_string_literal{));");
5773   verifyFormat("#pragma omp threadprivate( \\\n"
5774                "        y)), // expected-warning",
5775                getLLVMStyleWithColumns(28));
5776   verifyFormat("#d, = };");
5777   verifyFormat("#if \"a");
5778   verifyIncompleteFormat("({\n"
5779                          "#define b     \\\n"
5780                          "  }           \\\n"
5781                          "  a\n"
5782                          "a",
5783                          getLLVMStyleWithColumns(15));
5784   verifyFormat("#define A     \\\n"
5785                "  {           \\\n"
5786                "    {\n"
5787                "#define B     \\\n"
5788                "  }           \\\n"
5789                "  }",
5790                getLLVMStyleWithColumns(15));
5791   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
5792   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
5793   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
5794   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
5795   verifyNoCrash("#else\n"
5796                 "#else\n"
5797                 "#endif\n"
5798                 "#endif");
5799   verifyNoCrash("#else\n"
5800                 "#if X\n"
5801                 "#endif\n"
5802                 "#endif");
5803   verifyNoCrash("#else\n"
5804                 "#endif\n"
5805                 "#if X\n"
5806                 "#endif");
5807   verifyNoCrash("#if X\n"
5808                 "#else\n"
5809                 "#else\n"
5810                 "#endif\n"
5811                 "#endif");
5812   verifyNoCrash("#if X\n"
5813                 "#elif Y\n"
5814                 "#elif Y\n"
5815                 "#endif\n"
5816                 "#endif");
5817   verifyNoCrash("#endif\n"
5818                 "#endif");
5819   verifyNoCrash("#endif\n"
5820                 "#else");
5821   verifyNoCrash("#endif\n"
5822                 "#elif Y");
5823 }
5824 
5825 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
5826   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
5827   verifyFormat("class A : public QObject {\n"
5828                "  Q_OBJECT\n"
5829                "\n"
5830                "  A() {}\n"
5831                "};",
5832                "class A  :  public QObject {\n"
5833                "     Q_OBJECT\n"
5834                "\n"
5835                "  A() {\n}\n"
5836                "}  ;");
5837   verifyFormat("MACRO\n"
5838                "/*static*/ int i;",
5839                "MACRO\n"
5840                " /*static*/ int   i;");
5841   verifyFormat("SOME_MACRO\n"
5842                "namespace {\n"
5843                "void f();\n"
5844                "} // namespace",
5845                "SOME_MACRO\n"
5846                "  namespace    {\n"
5847                "void   f(  );\n"
5848                "} // namespace");
5849   // Only if the identifier contains at least 5 characters.
5850   verifyFormat("HTTP f();", "HTTP\nf();");
5851   verifyNoChange("MACRO\nf();");
5852   // Only if everything is upper case.
5853   verifyFormat("class A : public QObject {\n"
5854                "  Q_Object A() {}\n"
5855                "};",
5856                "class A  :  public QObject {\n"
5857                "     Q_Object\n"
5858                "  A() {\n}\n"
5859                "}  ;");
5860 
5861   // Only if the next line can actually start an unwrapped line.
5862   verifyFormat("SOME_WEIRD_LOG_MACRO << SomeThing;", "SOME_WEIRD_LOG_MACRO\n"
5863                                                      "<< SomeThing;");
5864 
5865   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
5866                "(n, buffers))",
5867                getChromiumStyle(FormatStyle::LK_Cpp));
5868 
5869   // See PR41483
5870   verifyNoChange("/**/ FOO(a)\n"
5871                  "FOO(b)");
5872 }
5873 
5874 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
5875   verifyFormat("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
5876                "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
5877                "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
5878                "class X {};\n"
5879                "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
5880                "int *createScopDetectionPass() { return 0; }",
5881                "  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
5882                "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
5883                "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
5884                "  class X {};\n"
5885                "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
5886                "  int *createScopDetectionPass() { return 0; }");
5887   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
5888   // braces, so that inner block is indented one level more.
5889   verifyFormat("int q() {\n"
5890                "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
5891                "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
5892                "  IPC_END_MESSAGE_MAP()\n"
5893                "}",
5894                "int q() {\n"
5895                "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
5896                "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
5897                "  IPC_END_MESSAGE_MAP()\n"
5898                "}");
5899 
5900   // Same inside macros.
5901   verifyFormat("#define LIST(L) \\\n"
5902                "  L(A)          \\\n"
5903                "  L(B)          \\\n"
5904                "  L(C)",
5905                "#define LIST(L) \\\n"
5906                "  L(A) \\\n"
5907                "  L(B) \\\n"
5908                "  L(C)",
5909                getGoogleStyle());
5910 
5911   // These must not be recognized as macros.
5912   verifyFormat("int q() {\n"
5913                "  f(x);\n"
5914                "  f(x) {}\n"
5915                "  f(x)->g();\n"
5916                "  f(x)->*g();\n"
5917                "  f(x).g();\n"
5918                "  f(x) = x;\n"
5919                "  f(x) += x;\n"
5920                "  f(x) -= x;\n"
5921                "  f(x) *= x;\n"
5922                "  f(x) /= x;\n"
5923                "  f(x) %= x;\n"
5924                "  f(x) &= x;\n"
5925                "  f(x) |= x;\n"
5926                "  f(x) ^= x;\n"
5927                "  f(x) >>= x;\n"
5928                "  f(x) <<= x;\n"
5929                "  f(x)[y].z();\n"
5930                "  LOG(INFO) << x;\n"
5931                "  ifstream(x) >> x;\n"
5932                "}",
5933                "int q() {\n"
5934                "  f(x)\n;\n"
5935                "  f(x)\n {}\n"
5936                "  f(x)\n->g();\n"
5937                "  f(x)\n->*g();\n"
5938                "  f(x)\n.g();\n"
5939                "  f(x)\n = x;\n"
5940                "  f(x)\n += x;\n"
5941                "  f(x)\n -= x;\n"
5942                "  f(x)\n *= x;\n"
5943                "  f(x)\n /= x;\n"
5944                "  f(x)\n %= x;\n"
5945                "  f(x)\n &= x;\n"
5946                "  f(x)\n |= x;\n"
5947                "  f(x)\n ^= x;\n"
5948                "  f(x)\n >>= x;\n"
5949                "  f(x)\n <<= x;\n"
5950                "  f(x)\n[y].z();\n"
5951                "  LOG(INFO)\n << x;\n"
5952                "  ifstream(x)\n >> x;\n"
5953                "}");
5954   verifyFormat("int q() {\n"
5955                "  F(x)\n"
5956                "  if (1) {\n"
5957                "  }\n"
5958                "  F(x)\n"
5959                "  while (1) {\n"
5960                "  }\n"
5961                "  F(x)\n"
5962                "  G(x);\n"
5963                "  F(x)\n"
5964                "  try {\n"
5965                "    Q();\n"
5966                "  } catch (...) {\n"
5967                "  }\n"
5968                "}",
5969                "int q() {\n"
5970                "F(x)\n"
5971                "if (1) {}\n"
5972                "F(x)\n"
5973                "while (1) {}\n"
5974                "F(x)\n"
5975                "G(x);\n"
5976                "F(x)\n"
5977                "try { Q(); } catch (...) {}\n"
5978                "}");
5979   verifyFormat("class A {\n"
5980                "  A() : t(0) {}\n"
5981                "  A(int i) noexcept() : {}\n"
5982                "  A(X x)\n" // FIXME: function-level try blocks are broken.
5983                "  try : t(0) {\n"
5984                "  } catch (...) {\n"
5985                "  }\n"
5986                "};",
5987                "class A {\n"
5988                "  A()\n : t(0) {}\n"
5989                "  A(int i)\n noexcept() : {}\n"
5990                "  A(X x)\n"
5991                "  try : t(0) {} catch (...) {}\n"
5992                "};");
5993   FormatStyle Style = getLLVMStyle();
5994   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
5995   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
5996   Style.BraceWrapping.AfterFunction = true;
5997   verifyFormat("void f()\n"
5998                "try\n"
5999                "{\n"
6000                "}",
6001                "void f() try {\n"
6002                "}",
6003                Style);
6004   verifyFormat("class SomeClass {\n"
6005                "public:\n"
6006                "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6007                "};",
6008                "class SomeClass {\n"
6009                "public:\n"
6010                "  SomeClass()\n"
6011                "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6012                "};");
6013   verifyFormat("class SomeClass {\n"
6014                "public:\n"
6015                "  SomeClass()\n"
6016                "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6017                "};",
6018                "class SomeClass {\n"
6019                "public:\n"
6020                "  SomeClass()\n"
6021                "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6022                "};",
6023                getLLVMStyleWithColumns(40));
6024 
6025   verifyFormat("MACRO(>)");
6026 
6027   // Some macros contain an implicit semicolon.
6028   Style = getLLVMStyle();
6029   Style.StatementMacros.push_back("FOO");
6030   verifyFormat("FOO(a) int b = 0;");
6031   verifyFormat("FOO(a)\n"
6032                "int b = 0;",
6033                Style);
6034   verifyFormat("FOO(a);\n"
6035                "int b = 0;",
6036                Style);
6037   verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
6038                "int b = 0;",
6039                Style);
6040   verifyFormat("FOO()\n"
6041                "int b = 0;",
6042                Style);
6043   verifyFormat("FOO\n"
6044                "int b = 0;",
6045                Style);
6046   verifyFormat("void f() {\n"
6047                "  FOO(a)\n"
6048                "  return a;\n"
6049                "}",
6050                Style);
6051   verifyFormat("FOO(a)\n"
6052                "FOO(b)",
6053                Style);
6054   verifyFormat("int a = 0;\n"
6055                "FOO(b)\n"
6056                "int c = 0;",
6057                Style);
6058   verifyFormat("int a = 0;\n"
6059                "int x = FOO(a)\n"
6060                "int b = 0;",
6061                Style);
6062   verifyFormat("void foo(int a) { FOO(a) }\n"
6063                "uint32_t bar() {}",
6064                Style);
6065 }
6066 
6067 TEST_F(FormatTest, FormatsMacrosWithZeroColumnWidth) {
6068   FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
6069 
6070   verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()",
6071                ZeroColumn);
6072 }
6073 
6074 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
6075   verifyFormat("#define A \\\n"
6076                "  f({     \\\n"
6077                "    g();  \\\n"
6078                "  });",
6079                getLLVMStyleWithColumns(11));
6080 }
6081 
6082 TEST_F(FormatTest, IndentPreprocessorDirectives) {
6083   FormatStyle Style = getLLVMStyleWithColumns(40);
6084   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
6085   verifyFormat("#ifdef _WIN32\n"
6086                "#define A 0\n"
6087                "#ifdef VAR2\n"
6088                "#define B 1\n"
6089                "#include <someheader.h>\n"
6090                "#define MACRO                          \\\n"
6091                "  some_very_long_func_aaaaaaaaaa();\n"
6092                "#endif\n"
6093                "#else\n"
6094                "#define A 1\n"
6095                "#endif",
6096                Style);
6097   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
6098   verifyFormat("#if 1\n"
6099                "#  define __STR(x) #x\n"
6100                "#endif",
6101                Style);
6102   verifyFormat("#ifdef _WIN32\n"
6103                "#  define A 0\n"
6104                "#  ifdef VAR2\n"
6105                "#    define B 1\n"
6106                "#    include <someheader.h>\n"
6107                "#    define MACRO                      \\\n"
6108                "      some_very_long_func_aaaaaaaaaa();\n"
6109                "#  endif\n"
6110                "#else\n"
6111                "#  define A 1\n"
6112                "#endif",
6113                Style);
6114   verifyFormat("#if A\n"
6115                "#  define MACRO                        \\\n"
6116                "    void a(int x) {                    \\\n"
6117                "      b();                             \\\n"
6118                "      c();                             \\\n"
6119                "      d();                             \\\n"
6120                "      e();                             \\\n"
6121                "      f();                             \\\n"
6122                "    }\n"
6123                "#endif",
6124                Style);
6125   // Comments before include guard.
6126   verifyFormat("// file comment\n"
6127                "// file comment\n"
6128                "#ifndef HEADER_H\n"
6129                "#define HEADER_H\n"
6130                "code();\n"
6131                "#endif",
6132                Style);
6133   // Test with include guards.
6134   verifyFormat("#ifndef HEADER_H\n"
6135                "#define HEADER_H\n"
6136                "code();\n"
6137                "#endif",
6138                Style);
6139   // Include guards must have a #define with the same variable immediately
6140   // after #ifndef.
6141   verifyFormat("#ifndef NOT_GUARD\n"
6142                "#  define FOO\n"
6143                "code();\n"
6144                "#endif",
6145                Style);
6146 
6147   // Include guards must cover the entire file.
6148   verifyFormat("code();\n"
6149                "code();\n"
6150                "#ifndef NOT_GUARD\n"
6151                "#  define NOT_GUARD\n"
6152                "code();\n"
6153                "#endif",
6154                Style);
6155   verifyFormat("#ifndef NOT_GUARD\n"
6156                "#  define NOT_GUARD\n"
6157                "code();\n"
6158                "#endif\n"
6159                "code();",
6160                Style);
6161   // Test with trailing blank lines.
6162   verifyFormat("#ifndef HEADER_H\n"
6163                "#define HEADER_H\n"
6164                "code();\n"
6165                "#endif",
6166                Style);
6167   // Include guards don't have #else.
6168   verifyFormat("#ifndef NOT_GUARD\n"
6169                "#  define NOT_GUARD\n"
6170                "code();\n"
6171                "#else\n"
6172                "#endif",
6173                Style);
6174   verifyFormat("#ifndef NOT_GUARD\n"
6175                "#  define NOT_GUARD\n"
6176                "code();\n"
6177                "#elif FOO\n"
6178                "#endif",
6179                Style);
6180   // Non-identifier #define after potential include guard.
6181   verifyFormat("#ifndef FOO\n"
6182                "#  define 1\n"
6183                "#endif",
6184                Style);
6185   // #if closes past last non-preprocessor line.
6186   verifyFormat("#ifndef FOO\n"
6187                "#define FOO\n"
6188                "#if 1\n"
6189                "int i;\n"
6190                "#  define A 0\n"
6191                "#endif\n"
6192                "#endif",
6193                Style);
6194   // Don't crash if there is an #elif directive without a condition.
6195   verifyFormat("#if 1\n"
6196                "int x;\n"
6197                "#elif\n"
6198                "int y;\n"
6199                "#else\n"
6200                "int z;\n"
6201                "#endif",
6202                Style);
6203   // FIXME: This doesn't handle the case where there's code between the
6204   // #ifndef and #define but all other conditions hold. This is because when
6205   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
6206   // previous code line yet, so we can't detect it.
6207   verifyFormat("#ifndef NOT_GUARD\n"
6208                "code();\n"
6209                "#define NOT_GUARD\n"
6210                "code();\n"
6211                "#endif",
6212                "#ifndef NOT_GUARD\n"
6213                "code();\n"
6214                "#  define NOT_GUARD\n"
6215                "code();\n"
6216                "#endif",
6217                Style);
6218   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
6219   // be outside an include guard. Examples are #pragma once and
6220   // #pragma GCC diagnostic, or anything else that does not change the meaning
6221   // of the file if it's included multiple times.
6222   verifyFormat("#ifdef WIN32\n"
6223                "#  pragma once\n"
6224                "#endif\n"
6225                "#ifndef HEADER_H\n"
6226                "#  define HEADER_H\n"
6227                "code();\n"
6228                "#endif",
6229                "#ifdef WIN32\n"
6230                "#  pragma once\n"
6231                "#endif\n"
6232                "#ifndef HEADER_H\n"
6233                "#define HEADER_H\n"
6234                "code();\n"
6235                "#endif",
6236                Style);
6237   // FIXME: This does not detect when there is a single non-preprocessor line
6238   // in front of an include-guard-like structure where other conditions hold
6239   // because ScopedLineState hides the line.
6240   verifyFormat("code();\n"
6241                "#ifndef HEADER_H\n"
6242                "#define HEADER_H\n"
6243                "code();\n"
6244                "#endif",
6245                "code();\n"
6246                "#ifndef HEADER_H\n"
6247                "#  define HEADER_H\n"
6248                "code();\n"
6249                "#endif",
6250                Style);
6251   // Keep comments aligned with #, otherwise indent comments normally. These
6252   // tests cannot use verifyFormat because messUp manipulates leading
6253   // whitespace.
6254   {
6255     const char *Expected = ""
6256                            "void f() {\n"
6257                            "#if 1\n"
6258                            "// Preprocessor aligned.\n"
6259                            "#  define A 0\n"
6260                            "  // Code. Separated by blank line.\n"
6261                            "\n"
6262                            "#  define B 0\n"
6263                            "  // Code. Not aligned with #\n"
6264                            "#  define C 0\n"
6265                            "#endif";
6266     const char *ToFormat = ""
6267                            "void f() {\n"
6268                            "#if 1\n"
6269                            "// Preprocessor aligned.\n"
6270                            "#  define A 0\n"
6271                            "// Code. Separated by blank line.\n"
6272                            "\n"
6273                            "#  define B 0\n"
6274                            "   // Code. Not aligned with #\n"
6275                            "#  define C 0\n"
6276                            "#endif";
6277     verifyFormat(Expected, ToFormat, Style);
6278     verifyNoChange(Expected, Style);
6279   }
6280   // Keep block quotes aligned.
6281   {
6282     const char *Expected = ""
6283                            "void f() {\n"
6284                            "#if 1\n"
6285                            "/* Preprocessor aligned. */\n"
6286                            "#  define A 0\n"
6287                            "  /* Code. Separated by blank line. */\n"
6288                            "\n"
6289                            "#  define B 0\n"
6290                            "  /* Code. Not aligned with # */\n"
6291                            "#  define C 0\n"
6292                            "#endif";
6293     const char *ToFormat = ""
6294                            "void f() {\n"
6295                            "#if 1\n"
6296                            "/* Preprocessor aligned. */\n"
6297                            "#  define A 0\n"
6298                            "/* Code. Separated by blank line. */\n"
6299                            "\n"
6300                            "#  define B 0\n"
6301                            "   /* Code. Not aligned with # */\n"
6302                            "#  define C 0\n"
6303                            "#endif";
6304     verifyFormat(Expected, ToFormat, Style);
6305     verifyNoChange(Expected, Style);
6306   }
6307   // Keep comments aligned with un-indented directives.
6308   {
6309     const char *Expected = ""
6310                            "void f() {\n"
6311                            "// Preprocessor aligned.\n"
6312                            "#define A 0\n"
6313                            "  // Code. Separated by blank line.\n"
6314                            "\n"
6315                            "#define B 0\n"
6316                            "  // Code. Not aligned with #\n"
6317                            "#define C 0\n";
6318     const char *ToFormat = ""
6319                            "void f() {\n"
6320                            "// Preprocessor aligned.\n"
6321                            "#define A 0\n"
6322                            "// Code. Separated by blank line.\n"
6323                            "\n"
6324                            "#define B 0\n"
6325                            "   // Code. Not aligned with #\n"
6326                            "#define C 0\n";
6327     verifyFormat(Expected, ToFormat, Style);
6328     verifyNoChange(Expected, Style);
6329   }
6330   // Test AfterHash with tabs.
6331   {
6332     FormatStyle Tabbed = Style;
6333     Tabbed.UseTab = FormatStyle::UT_Always;
6334     Tabbed.IndentWidth = 8;
6335     Tabbed.TabWidth = 8;
6336     verifyFormat("#ifdef _WIN32\n"
6337                  "#\tdefine A 0\n"
6338                  "#\tifdef VAR2\n"
6339                  "#\t\tdefine B 1\n"
6340                  "#\t\tinclude <someheader.h>\n"
6341                  "#\t\tdefine MACRO          \\\n"
6342                  "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
6343                  "#\tendif\n"
6344                  "#else\n"
6345                  "#\tdefine A 1\n"
6346                  "#endif",
6347                  Tabbed);
6348   }
6349 
6350   // Regression test: Multiline-macro inside include guards.
6351   verifyFormat("#ifndef HEADER_H\n"
6352                "#define HEADER_H\n"
6353                "#define A()        \\\n"
6354                "  int i;           \\\n"
6355                "  int j;\n"
6356                "#endif // HEADER_H",
6357                getLLVMStyleWithColumns(20));
6358 
6359   Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
6360   // Basic before hash indent tests
6361   verifyFormat("#ifdef _WIN32\n"
6362                "  #define A 0\n"
6363                "  #ifdef VAR2\n"
6364                "    #define B 1\n"
6365                "    #include <someheader.h>\n"
6366                "    #define MACRO                      \\\n"
6367                "      some_very_long_func_aaaaaaaaaa();\n"
6368                "  #endif\n"
6369                "#else\n"
6370                "  #define A 1\n"
6371                "#endif",
6372                Style);
6373   verifyFormat("#if A\n"
6374                "  #define MACRO                        \\\n"
6375                "    void a(int x) {                    \\\n"
6376                "      b();                             \\\n"
6377                "      c();                             \\\n"
6378                "      d();                             \\\n"
6379                "      e();                             \\\n"
6380                "      f();                             \\\n"
6381                "    }\n"
6382                "#endif",
6383                Style);
6384   // Keep comments aligned with indented directives. These
6385   // tests cannot use verifyFormat because messUp manipulates leading
6386   // whitespace.
6387   {
6388     const char *Expected = "void f() {\n"
6389                            "// Aligned to preprocessor.\n"
6390                            "#if 1\n"
6391                            "  // Aligned to code.\n"
6392                            "  int a;\n"
6393                            "  #if 1\n"
6394                            "    // Aligned to preprocessor.\n"
6395                            "    #define A 0\n"
6396                            "  // Aligned to code.\n"
6397                            "  int b;\n"
6398                            "  #endif\n"
6399                            "#endif\n"
6400                            "}";
6401     const char *ToFormat = "void f() {\n"
6402                            "// Aligned to preprocessor.\n"
6403                            "#if 1\n"
6404                            "// Aligned to code.\n"
6405                            "int a;\n"
6406                            "#if 1\n"
6407                            "// Aligned to preprocessor.\n"
6408                            "#define A 0\n"
6409                            "// Aligned to code.\n"
6410                            "int b;\n"
6411                            "#endif\n"
6412                            "#endif\n"
6413                            "}";
6414     verifyFormat(Expected, ToFormat, Style);
6415     verifyNoChange(Expected, Style);
6416   }
6417   {
6418     const char *Expected = "void f() {\n"
6419                            "/* Aligned to preprocessor. */\n"
6420                            "#if 1\n"
6421                            "  /* Aligned to code. */\n"
6422                            "  int a;\n"
6423                            "  #if 1\n"
6424                            "    /* Aligned to preprocessor. */\n"
6425                            "    #define A 0\n"
6426                            "  /* Aligned to code. */\n"
6427                            "  int b;\n"
6428                            "  #endif\n"
6429                            "#endif\n"
6430                            "}";
6431     const char *ToFormat = "void f() {\n"
6432                            "/* Aligned to preprocessor. */\n"
6433                            "#if 1\n"
6434                            "/* Aligned to code. */\n"
6435                            "int a;\n"
6436                            "#if 1\n"
6437                            "/* Aligned to preprocessor. */\n"
6438                            "#define A 0\n"
6439                            "/* Aligned to code. */\n"
6440                            "int b;\n"
6441                            "#endif\n"
6442                            "#endif\n"
6443                            "}";
6444     verifyFormat(Expected, ToFormat, Style);
6445     verifyNoChange(Expected, Style);
6446   }
6447 
6448   // Test single comment before preprocessor
6449   verifyFormat("// Comment\n"
6450                "\n"
6451                "#if 1\n"
6452                "#endif",
6453                Style);
6454 }
6455 
6456 TEST_F(FormatTest, FormatAlignInsidePreprocessorElseBlock) {
6457   FormatStyle Style = getLLVMStyle();
6458   Style.AlignConsecutiveAssignments.Enabled = true;
6459   Style.AlignConsecutiveDeclarations.Enabled = true;
6460 
6461   // Test with just #if blocks.
6462   verifyFormat("void f1() {\n"
6463                "#if 1\n"
6464                "  int foo    = 1;\n"
6465                "  int foobar = 2;\n"
6466                "#endif\n"
6467                "}\n"
6468                "#if 1\n"
6469                "int baz = 3;\n"
6470                "#endif\n"
6471                "void f2() {\n"
6472                "#if 1\n"
6473                "  char *foobarbaz = \"foobarbaz\";\n"
6474                "  int   quux      = 4;\n"
6475                "}",
6476                Style);
6477 
6478   // Test with just #else blocks.
6479   verifyFormat("void f1() {\n"
6480                "#if 1\n"
6481                "#else\n"
6482                "  int foo    = 1;\n"
6483                "  int foobar = 2;\n"
6484                "#endif\n"
6485                "}\n"
6486                "#if 1\n"
6487                "#else\n"
6488                "int baz = 3;\n"
6489                "#endif\n"
6490                "void f2() {\n"
6491                "#if 1\n"
6492                "#else\n"
6493                "  char *foobarbaz = \"foobarbaz\";\n"
6494                "  int   quux      = 4;\n"
6495                "}",
6496                Style);
6497   verifyFormat("auto foo = [] { return; };\n"
6498                "#if FOO\n"
6499                "#else\n"
6500                "count = bar;\n"
6501                "mbid  = bid;\n"
6502                "#endif",
6503                Style);
6504 
6505   // Test with a mix of #if and #else blocks.
6506   verifyFormat("void f1() {\n"
6507                "#if 1\n"
6508                "#else\n"
6509                "  int foo    = 1;\n"
6510                "  int foobar = 2;\n"
6511                "#endif\n"
6512                "}\n"
6513                "#if 1\n"
6514                "int baz = 3;\n"
6515                "#endif\n"
6516                "void f2() {\n"
6517                "#if 1\n"
6518                "#else\n"
6519                "  // prevent alignment with #else in f1\n"
6520                "  char *foobarbaz = \"foobarbaz\";\n"
6521                "  int   quux      = 4;\n"
6522                "}",
6523                Style);
6524 
6525   // Test with nested #if and #else blocks.
6526   verifyFormat("void f1() {\n"
6527                "#if 1\n"
6528                "#else\n"
6529                "#if 2\n"
6530                "#else\n"
6531                "  int foo    = 1;\n"
6532                "  int foobar = 2;\n"
6533                "#endif\n"
6534                "#endif\n"
6535                "}\n"
6536                "#if 1\n"
6537                "#else\n"
6538                "#if 2\n"
6539                "int baz = 3;\n"
6540                "#endif\n"
6541                "#endif\n"
6542                "void f2() {\n"
6543                "#if 1\n"
6544                "#if 2\n"
6545                "#else\n"
6546                "  // prevent alignment with #else in f1\n"
6547                "  char *foobarbaz = \"foobarbaz\";\n"
6548                "  int   quux      = 4;\n"
6549                "#endif\n"
6550                "#endif\n"
6551                "}",
6552                Style);
6553 
6554   verifyFormat("#if FOO\n"
6555                "int a = 1;\n"
6556                "#else\n"
6557                "int ab = 2;\n"
6558                "#endif\n"
6559                "#ifdef BAR\n"
6560                "int abc = 3;\n"
6561                "#elifdef BAZ\n"
6562                "int abcd = 4;\n"
6563                "#endif",
6564                Style);
6565 
6566   verifyFormat("void f() {\n"
6567                "  if (foo) {\n"
6568                "#if FOO\n"
6569                "    int a = 1;\n"
6570                "#else\n"
6571                "    bool a = true;\n"
6572                "#endif\n"
6573                "    int abc = 3;\n"
6574                "#ifndef BAR\n"
6575                "    int abcd = 4;\n"
6576                "#elif BAZ\n"
6577                "    bool abcd = true;\n"
6578                "#endif\n"
6579                "  }\n"
6580                "}",
6581                Style);
6582 
6583   verifyFormat("void f() {\n"
6584                "#if FOO\n"
6585                "  a = 1;\n"
6586                "#else\n"
6587                "  ab = 2;\n"
6588                "#endif\n"
6589                "}\n"
6590                "void g() {\n"
6591                "#if BAR\n"
6592                "  abc = 3;\n"
6593                "#elifndef BAZ\n"
6594                "  abcd = 4;\n"
6595                "#endif\n"
6596                "}",
6597                Style);
6598 }
6599 
6600 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
6601   verifyFormat("{\n"
6602                "  {\n"
6603                "    a #c;\n"
6604                "  }\n"
6605                "}");
6606 }
6607 
6608 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
6609   verifyFormat("#define A \\\n  {       \\\n    {\nint i;",
6610                "#define A { {\nint i;", getLLVMStyleWithColumns(11));
6611   verifyFormat("#define A \\\n  }       \\\n  }\nint i;",
6612                "#define A } }\nint i;", getLLVMStyleWithColumns(11));
6613 }
6614 
6615 TEST_F(FormatTest, EscapedNewlines) {
6616   FormatStyle Narrow = getLLVMStyleWithColumns(11);
6617   verifyFormat("#define A \\\n  int i;  \\\n  int j;",
6618                "#define A \\\nint i;\\\n  int j;", Narrow);
6619   verifyFormat("#define A\n\nint i;", "#define A \\\n\n int i;");
6620   verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();");
6621   verifyFormat("/* \\  \\  \\\n */", "\\\n/* \\  \\  \\\n */");
6622   verifyNoChange("<a\n\\\\\n>");
6623 
6624   FormatStyle AlignLeft = getLLVMStyle();
6625   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
6626   verifyFormat("#define MACRO(x) \\\n"
6627                "private:         \\\n"
6628                "  int x(int a);",
6629                AlignLeft);
6630 
6631   // CRLF line endings
6632   verifyFormat("#define A \\\r\n  int i;  \\\r\n  int j;",
6633                "#define A \\\r\nint i;\\\r\n  int j;", Narrow);
6634   verifyFormat("#define A\r\n\r\nint i;", "#define A \\\r\n\r\n int i;");
6635   verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();");
6636   verifyFormat("/* \\  \\  \\\r\n */", "\\\r\n/* \\  \\  \\\r\n */");
6637   verifyNoChange("<a\r\n\\\\\r\n>");
6638   verifyFormat("#define MACRO(x) \\\r\n"
6639                "private:         \\\r\n"
6640                "  int x(int a);",
6641                AlignLeft);
6642 
6643   constexpr StringRef Code{"#define A   \\\n"
6644                            "  int a123; \\\n"
6645                            "  int a;    \\\n"
6646                            "  int a1234;"};
6647   verifyFormat(Code, AlignLeft);
6648 
6649   constexpr StringRef Code2{"#define A    \\\n"
6650                             "  int a123;  \\\n"
6651                             "  int a;     \\\n"
6652                             "  int a1234;"};
6653   auto LastLine = getLLVMStyle();
6654   LastLine.AlignEscapedNewlines = FormatStyle::ENAS_LeftWithLastLine;
6655   verifyFormat(Code2, LastLine);
6656 
6657   LastLine.ColumnLimit = 13;
6658   verifyFormat(Code, LastLine);
6659 
6660   LastLine.ColumnLimit = 0;
6661   verifyFormat(Code2, LastLine);
6662 
6663   FormatStyle DontAlign = getLLVMStyle();
6664   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
6665   DontAlign.MaxEmptyLinesToKeep = 3;
6666   // FIXME: can't use verifyFormat here because the newline before
6667   // "public:" is not inserted the first time it's reformatted
6668   verifyNoChange("#define A \\\n"
6669                  "  class Foo { \\\n"
6670                  "    void bar(); \\\n"
6671                  "\\\n"
6672                  "\\\n"
6673                  "\\\n"
6674                  "  public: \\\n"
6675                  "    void baz(); \\\n"
6676                  "  };",
6677                  DontAlign);
6678 }
6679 
6680 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
6681   verifyFormat("#define A \\\n"
6682                "  int v(  \\\n"
6683                "      a); \\\n"
6684                "  int i;",
6685                getLLVMStyleWithColumns(11));
6686 }
6687 
6688 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
6689   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
6690                "                      \\\n"
6691                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
6692                "\n"
6693                "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
6694                "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);",
6695                "  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
6696                "\\\n"
6697                "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
6698                "  \n"
6699                "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
6700                "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);");
6701 }
6702 
6703 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
6704   verifyFormat("int\n"
6705                "#define A\n"
6706                "    a;",
6707                "int\n#define A\na;");
6708   verifyFormat("functionCallTo(\n"
6709                "    someOtherFunction(\n"
6710                "        withSomeParameters, whichInSequence,\n"
6711                "        areLongerThanALine(andAnotherCall,\n"
6712                "#define A B\n"
6713                "                           withMoreParamters,\n"
6714                "                           whichStronglyInfluenceTheLayout),\n"
6715                "        andMoreParameters),\n"
6716                "    trailing);",
6717                getLLVMStyleWithColumns(69));
6718   verifyFormat("Foo::Foo()\n"
6719                "#ifdef BAR\n"
6720                "    : baz(0)\n"
6721                "#endif\n"
6722                "{\n"
6723                "}");
6724   verifyFormat("void f() {\n"
6725                "  if (true)\n"
6726                "#ifdef A\n"
6727                "    f(42);\n"
6728                "  x();\n"
6729                "#else\n"
6730                "    g();\n"
6731                "  x();\n"
6732                "#endif\n"
6733                "}");
6734   verifyFormat("void f(param1, param2,\n"
6735                "       param3,\n"
6736                "#ifdef A\n"
6737                "       param4(param5,\n"
6738                "#ifdef A1\n"
6739                "              param6,\n"
6740                "#ifdef A2\n"
6741                "              param7),\n"
6742                "#else\n"
6743                "              param8),\n"
6744                "       param9,\n"
6745                "#endif\n"
6746                "       param10,\n"
6747                "#endif\n"
6748                "       param11)\n"
6749                "#else\n"
6750                "       param12)\n"
6751                "#endif\n"
6752                "{\n"
6753                "  x();\n"
6754                "}",
6755                getLLVMStyleWithColumns(28));
6756   verifyFormat("#if 1\n"
6757                "int i;");
6758   verifyFormat("#if 1\n"
6759                "#endif\n"
6760                "#if 1\n"
6761                "#else\n"
6762                "#endif");
6763   verifyFormat("DEBUG({\n"
6764                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6765                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
6766                "});\n"
6767                "#if a\n"
6768                "#else\n"
6769                "#endif");
6770 
6771   verifyIncompleteFormat("void f(\n"
6772                          "#if A\n"
6773                          ");\n"
6774                          "#else\n"
6775                          "#endif");
6776 
6777   // Verify that indentation is correct when there is an `#if 0` with an
6778   // `#else`.
6779   verifyFormat("#if 0\n"
6780                "{\n"
6781                "#else\n"
6782                "{\n"
6783                "#endif\n"
6784                "  x;\n"
6785                "}");
6786 
6787   verifyFormat("#if 0\n"
6788                "#endif\n"
6789                "#if X\n"
6790                "int something_fairly_long; // Align here please\n"
6791                "#endif                     // Should be aligned");
6792 
6793   verifyFormat("#if 0\n"
6794                "#endif\n"
6795                "#if X\n"
6796                "#else  // Align\n"
6797                ";\n"
6798                "#endif // Align");
6799 
6800   verifyFormat("void SomeFunction(int param1,\n"
6801                "                  template <\n"
6802                "#ifdef A\n"
6803                "#if 0\n"
6804                "#endif\n"
6805                "                      MyType<Some>>\n"
6806                "#else\n"
6807                "                      Type1, Type2>\n"
6808                "#endif\n"
6809                "                  param2,\n"
6810                "                  param3) {\n"
6811                "  f();\n"
6812                "}");
6813 }
6814 
6815 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
6816   verifyFormat("#endif\n"
6817                "#if B");
6818 }
6819 
6820 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
6821   FormatStyle SingleLine = getLLVMStyle();
6822   SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
6823   verifyFormat("#if 0\n"
6824                "#elif 1\n"
6825                "#endif\n"
6826                "void foo() {\n"
6827                "  if (test) foo2();\n"
6828                "}",
6829                SingleLine);
6830 }
6831 
6832 TEST_F(FormatTest, LayoutBlockInsideParens) {
6833   verifyFormat("functionCall({ int i; });");
6834   verifyFormat("functionCall({\n"
6835                "  int i;\n"
6836                "  int j;\n"
6837                "});");
6838   verifyFormat("functionCall(\n"
6839                "    {\n"
6840                "      int i;\n"
6841                "      int j;\n"
6842                "    },\n"
6843                "    aaaa, bbbb, cccc);");
6844   verifyFormat("functionA(functionB({\n"
6845                "            int i;\n"
6846                "            int j;\n"
6847                "          }),\n"
6848                "          aaaa, bbbb, cccc);");
6849   verifyFormat("functionCall(\n"
6850                "    {\n"
6851                "      int i;\n"
6852                "      int j;\n"
6853                "    },\n"
6854                "    aaaa, bbbb, // comment\n"
6855                "    cccc);");
6856   verifyFormat("functionA(functionB({\n"
6857                "            int i;\n"
6858                "            int j;\n"
6859                "          }),\n"
6860                "          aaaa, bbbb, // comment\n"
6861                "          cccc);");
6862   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
6863   verifyFormat("functionCall(aaaa, bbbb, {\n"
6864                "  int i;\n"
6865                "  int j;\n"
6866                "});");
6867   verifyFormat(
6868       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
6869       "    {\n"
6870       "      int i; // break\n"
6871       "    },\n"
6872       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6873       "                                     ccccccccccccccccc));");
6874   verifyFormat("DEBUG({\n"
6875                "  if (a)\n"
6876                "    f();\n"
6877                "});");
6878 }
6879 
6880 TEST_F(FormatTest, LayoutBlockInsideStatement) {
6881   verifyFormat("SOME_MACRO { int i; }\n"
6882                "int i;",
6883                "  SOME_MACRO  {int i;}  int i;");
6884 }
6885 
6886 TEST_F(FormatTest, LayoutNestedBlocks) {
6887   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
6888                "  struct s {\n"
6889                "    int i;\n"
6890                "  };\n"
6891                "  s kBitsToOs[] = {{10}};\n"
6892                "  for (int i = 0; i < 10; ++i)\n"
6893                "    return;\n"
6894                "}");
6895   verifyFormat("call(parameter, {\n"
6896                "  something();\n"
6897                "  // Comment using all columns.\n"
6898                "  somethingelse();\n"
6899                "});",
6900                getLLVMStyleWithColumns(40));
6901   verifyFormat("DEBUG( //\n"
6902                "    { f(); }, a);");
6903   verifyFormat("DEBUG( //\n"
6904                "    {\n"
6905                "      f(); //\n"
6906                "    },\n"
6907                "    a);");
6908 
6909   verifyFormat("call(parameter, {\n"
6910                "  something();\n"
6911                "  // Comment too\n"
6912                "  // looooooooooong.\n"
6913                "  somethingElse();\n"
6914                "});",
6915                "call(parameter, {\n"
6916                "  something();\n"
6917                "  // Comment too looooooooooong.\n"
6918                "  somethingElse();\n"
6919                "});",
6920                getLLVMStyleWithColumns(29));
6921   verifyFormat("DEBUG({ int i; });", "DEBUG({ int   i; });");
6922   verifyFormat("DEBUG({ // comment\n"
6923                "  int i;\n"
6924                "});",
6925                "DEBUG({ // comment\n"
6926                "int  i;\n"
6927                "});");
6928   verifyFormat("DEBUG({\n"
6929                "  int i;\n"
6930                "\n"
6931                "  // comment\n"
6932                "  int j;\n"
6933                "});",
6934                "DEBUG({\n"
6935                "  int  i;\n"
6936                "\n"
6937                "  // comment\n"
6938                "  int  j;\n"
6939                "});");
6940 
6941   verifyFormat("DEBUG({\n"
6942                "  if (a)\n"
6943                "    return;\n"
6944                "});");
6945   verifyGoogleFormat("DEBUG({\n"
6946                      "  if (a) return;\n"
6947                      "});");
6948   FormatStyle Style = getGoogleStyle();
6949   Style.ColumnLimit = 45;
6950   verifyFormat("Debug(\n"
6951                "    aaaaa,\n"
6952                "    {\n"
6953                "      if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
6954                "    },\n"
6955                "    a);",
6956                Style);
6957 
6958   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
6959 
6960   verifyNoCrash("^{v^{a}}");
6961 }
6962 
6963 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
6964   verifyFormat("#define MACRO()                     \\\n"
6965                "  Debug(aaa, /* force line break */ \\\n"
6966                "        {                           \\\n"
6967                "          int i;                    \\\n"
6968                "          int j;                    \\\n"
6969                "        })",
6970                "#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
6971                "          {  int   i;  int  j;   })",
6972                getGoogleStyle());
6973 
6974   verifyFormat("#define A                                       \\\n"
6975                "  [] {                                          \\\n"
6976                "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
6977                "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
6978                "  }",
6979                "#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
6980                "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
6981                getGoogleStyle());
6982 }
6983 
6984 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
6985   verifyFormat("enum E {};");
6986   verifyFormat("enum E {}");
6987   FormatStyle Style = getLLVMStyle();
6988   Style.SpaceInEmptyBlock = true;
6989   verifyFormat("void f() { }", "void f() {}", Style);
6990   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
6991   verifyFormat("{ }", Style);
6992   verifyFormat("while (true) { }", "while (true) {}", Style);
6993   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
6994   Style.BraceWrapping.BeforeElse = false;
6995   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
6996   verifyFormat("if (a)\n"
6997                "{\n"
6998                "} else if (b)\n"
6999                "{\n"
7000                "} else\n"
7001                "{ }",
7002                Style);
7003   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
7004   verifyFormat("if (a) {\n"
7005                "} else if (b) {\n"
7006                "} else {\n"
7007                "}",
7008                Style);
7009   Style.BraceWrapping.BeforeElse = true;
7010   verifyFormat("if (a) { }\n"
7011                "else if (b) { }\n"
7012                "else { }",
7013                Style);
7014 
7015   Style = getLLVMStyle(FormatStyle::LK_CSharp);
7016   Style.SpaceInEmptyBlock = true;
7017   verifyFormat("Event += () => { };", Style);
7018 }
7019 
7020 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
7021   FormatStyle Style = getLLVMStyle();
7022   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
7023   Style.MacroBlockEnd = "^[A-Z_]+_END$";
7024   verifyFormat("FOO_BEGIN\n"
7025                "  FOO_ENTRY\n"
7026                "FOO_END",
7027                Style);
7028   verifyFormat("FOO_BEGIN\n"
7029                "  NESTED_FOO_BEGIN\n"
7030                "    NESTED_FOO_ENTRY\n"
7031                "  NESTED_FOO_END\n"
7032                "FOO_END",
7033                Style);
7034   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
7035                "  int x;\n"
7036                "  x = 1;\n"
7037                "FOO_END(Baz)",
7038                Style);
7039 
7040   Style.RemoveBracesLLVM = true;
7041   verifyNoCrash("for (;;)\n"
7042                 "  FOO_BEGIN\n"
7043                 "    foo();\n"
7044                 "  FOO_END",
7045                 Style);
7046 }
7047 
7048 //===----------------------------------------------------------------------===//
7049 // Line break tests.
7050 //===----------------------------------------------------------------------===//
7051 
7052 TEST_F(FormatTest, PreventConfusingIndents) {
7053   verifyFormat(
7054       "void f() {\n"
7055       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
7056       "                         parameter, parameter, parameter)),\n"
7057       "                     SecondLongCall(parameter));\n"
7058       "}");
7059   verifyFormat(
7060       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7061       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7062       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7063       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
7064   verifyFormat(
7065       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7066       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
7067       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
7068       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
7069   verifyFormat(
7070       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
7071       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
7072       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
7073       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
7074   verifyFormat("int a = bbbb && ccc &&\n"
7075                "        fffff(\n"
7076                "#define A Just forcing a new line\n"
7077                "            ddd);");
7078 }
7079 
7080 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
7081   verifyFormat(
7082       "bool aaaaaaa =\n"
7083       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
7084       "    bbbbbbbb();");
7085   verifyFormat(
7086       "bool aaaaaaa =\n"
7087       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
7088       "    bbbbbbbb();");
7089 
7090   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
7091                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
7092                "    ccccccccc == ddddddddddd;");
7093   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
7094                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
7095                "    ccccccccc == ddddddddddd;");
7096   verifyFormat(
7097       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
7098       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
7099       "    ccccccccc == ddddddddddd;");
7100 
7101   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
7102                "                 aaaaaa) &&\n"
7103                "         bbbbbb && cccccc;");
7104   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
7105                "                 aaaaaa) >>\n"
7106                "         bbbbbb;");
7107   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
7108                "    SourceMgr.getSpellingColumnNumber(\n"
7109                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
7110                "    1);");
7111 
7112   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7113                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
7114                "    cccccc) {\n}");
7115   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7116                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
7117                "              cccccc) {\n}");
7118   verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7119                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
7120                "              cccccc) {\n}");
7121   verifyFormat("b = a &&\n"
7122                "    // Comment\n"
7123                "    b.c && d;");
7124 
7125   // If the LHS of a comparison is not a binary expression itself, the
7126   // additional linebreak confuses many people.
7127   verifyFormat(
7128       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7129       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
7130       "}");
7131   verifyFormat(
7132       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7133       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7134       "}");
7135   verifyFormat(
7136       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
7137       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7138       "}");
7139   verifyFormat(
7140       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7141       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
7142       "}");
7143   // Even explicit parentheses stress the precedence enough to make the
7144   // additional break unnecessary.
7145   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7146                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7147                "}");
7148   // This cases is borderline, but with the indentation it is still readable.
7149   verifyFormat(
7150       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7151       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7152       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
7153       "}",
7154       getLLVMStyleWithColumns(75));
7155 
7156   // If the LHS is a binary expression, we should still use the additional break
7157   // as otherwise the formatting hides the operator precedence.
7158   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7159                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7160                "    5) {\n"
7161                "}");
7162   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7163                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
7164                "    5) {\n"
7165                "}");
7166 
7167   FormatStyle OnePerLine = getLLVMStyle();
7168   OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine;
7169   verifyFormat(
7170       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7171       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7172       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
7173       OnePerLine);
7174 
7175   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
7176                "                .aaa(aaaaaaaaaaaaa) *\n"
7177                "            aaaaaaa +\n"
7178                "        aaaaaaa;",
7179                getLLVMStyleWithColumns(40));
7180 }
7181 
7182 TEST_F(FormatTest, ExpressionIndentation) {
7183   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7184                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7185                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7186                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7187                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
7188                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
7189                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7190                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
7191                "                 ccccccccccccccccccccccccccccccccccccccccc;");
7192   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7193                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7194                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7195                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7196   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7197                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7198                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7199                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7200   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7201                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7202                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7203                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7204   verifyFormat("if () {\n"
7205                "} else if (aaaaa && bbbbb > // break\n"
7206                "                        ccccc) {\n"
7207                "}");
7208   verifyFormat("if () {\n"
7209                "} else if constexpr (aaaaa && bbbbb > // break\n"
7210                "                                  ccccc) {\n"
7211                "}");
7212   verifyFormat("if () {\n"
7213                "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
7214                "                                  ccccc) {\n"
7215                "}");
7216   verifyFormat("if () {\n"
7217                "} else if (aaaaa &&\n"
7218                "           bbbbb > // break\n"
7219                "               ccccc &&\n"
7220                "           ddddd) {\n"
7221                "}");
7222 
7223   // Presence of a trailing comment used to change indentation of b.
7224   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
7225                "       b;\n"
7226                "return aaaaaaaaaaaaaaaaaaa +\n"
7227                "       b; //",
7228                getLLVMStyleWithColumns(30));
7229 }
7230 
7231 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
7232   // Not sure what the best system is here. Like this, the LHS can be found
7233   // immediately above an operator (everything with the same or a higher
7234   // indent). The RHS is aligned right of the operator and so compasses
7235   // everything until something with the same indent as the operator is found.
7236   // FIXME: Is this a good system?
7237   FormatStyle Style = getLLVMStyle();
7238   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7239   verifyFormat(
7240       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7241       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7242       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7243       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7244       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7245       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7246       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7247       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7248       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
7249       Style);
7250   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7251                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7252                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7253                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7254                Style);
7255   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7256                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7257                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7258                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7259                Style);
7260   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7261                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7262                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7263                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7264                Style);
7265   verifyFormat("if () {\n"
7266                "} else if (aaaaa\n"
7267                "           && bbbbb // break\n"
7268                "                  > ccccc) {\n"
7269                "}",
7270                Style);
7271   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7272                "       && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7273                Style);
7274   verifyFormat("return (a)\n"
7275                "       // comment\n"
7276                "       + b;",
7277                Style);
7278   verifyFormat(
7279       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7280       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7281       "             + cc;",
7282       Style);
7283 
7284   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7285                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7286                Style);
7287 
7288   // Forced by comments.
7289   verifyFormat(
7290       "unsigned ContentSize =\n"
7291       "    sizeof(int16_t)   // DWARF ARange version number\n"
7292       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7293       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
7294       "    + sizeof(int8_t); // Segment Size (in bytes)");
7295 
7296   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
7297                "       == boost::fusion::at_c<1>(iiii).second;",
7298                Style);
7299 
7300   Style.ColumnLimit = 60;
7301   verifyFormat("zzzzzzzzzz\n"
7302                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7303                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
7304                Style);
7305 
7306   Style.ColumnLimit = 80;
7307   Style.IndentWidth = 4;
7308   Style.TabWidth = 4;
7309   Style.UseTab = FormatStyle::UT_Always;
7310   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7311   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7312   verifyFormat("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
7313                "\t&& (someOtherLongishConditionPart1\n"
7314                "\t\t|| someOtherEvenLongerNestedConditionPart2);",
7315                "return someVeryVeryLongConditionThatBarelyFitsOnALine && "
7316                "(someOtherLongishConditionPart1 || "
7317                "someOtherEvenLongerNestedConditionPart2);",
7318                Style);
7319 
7320   Style = getLLVMStyleWithColumns(20);
7321   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7322   Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
7323   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7324   Style.ContinuationIndentWidth = 2;
7325   verifyFormat("struct Foo {\n"
7326                "  Foo(\n"
7327                "    int arg1,\n"
7328                "    int arg2)\n"
7329                "      : Base(\n"
7330                "          arg1,\n"
7331                "          arg2) {}\n"
7332                "};",
7333                Style);
7334   verifyFormat("return abc\n"
7335                "         ? foo(\n"
7336                "             a,\n"
7337                "             b,\n"
7338                "             bar(\n"
7339                "               abc))\n"
7340                "         : g(abc);",
7341                Style);
7342 }
7343 
7344 TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
7345   FormatStyle Style = getLLVMStyle();
7346   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7347   Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
7348 
7349   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7350                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7351                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7352                "              == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7353                "                         * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7354                "                     + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7355                "          && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7356                "                     * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7357                "                 > ccccccccccccccccccccccccccccccccccccccccc;",
7358                Style);
7359   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7360                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7361                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7362                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7363                Style);
7364   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7365                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7366                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7367                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7368                Style);
7369   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7370                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7371                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7372                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7373                Style);
7374   verifyFormat("if () {\n"
7375                "} else if (aaaaa\n"
7376                "           && bbbbb // break\n"
7377                "                  > ccccc) {\n"
7378                "}",
7379                Style);
7380   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7381                "    && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7382                Style);
7383   verifyFormat("return (a)\n"
7384                "     // comment\n"
7385                "     + b;",
7386                Style);
7387   verifyFormat(
7388       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7389       "               * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7390       "           + cc;",
7391       Style);
7392   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7393                "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7394                "                        : 3333333333333333;",
7395                Style);
7396   verifyFormat(
7397       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
7398       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
7399       "                                             : eeeeeeeeeeeeeeeeee)\n"
7400       "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7401       "                        : 3333333333333333;",
7402       Style);
7403   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7404                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7405                Style);
7406 
7407   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
7408                "    == boost::fusion::at_c<1>(iiii).second;",
7409                Style);
7410 
7411   Style.ColumnLimit = 60;
7412   verifyFormat("zzzzzzzzzzzzz\n"
7413                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7414                "   >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
7415                Style);
7416 
7417   // Forced by comments.
7418   Style.ColumnLimit = 80;
7419   verifyFormat(
7420       "unsigned ContentSize\n"
7421       "    = sizeof(int16_t) // DWARF ARange version number\n"
7422       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7423       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
7424       "    + sizeof(int8_t); // Segment Size (in bytes)",
7425       Style);
7426 
7427   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7428   verifyFormat(
7429       "unsigned ContentSize =\n"
7430       "    sizeof(int16_t)   // DWARF ARange version number\n"
7431       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7432       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
7433       "    + sizeof(int8_t); // Segment Size (in bytes)",
7434       Style);
7435 
7436   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7437   verifyFormat(
7438       "unsigned ContentSize =\n"
7439       "    sizeof(int16_t)   // DWARF ARange version number\n"
7440       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7441       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
7442       "    + sizeof(int8_t); // Segment Size (in bytes)",
7443       Style);
7444 }
7445 
7446 TEST_F(FormatTest, EnforcedOperatorWraps) {
7447   // Here we'd like to wrap after the || operators, but a comment is forcing an
7448   // earlier wrap.
7449   verifyFormat("bool x = aaaaa //\n"
7450                "         || bbbbb\n"
7451                "         //\n"
7452                "         || cccc;");
7453 }
7454 
7455 TEST_F(FormatTest, NoOperandAlignment) {
7456   FormatStyle Style = getLLVMStyle();
7457   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7458   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
7459                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7460                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7461                Style);
7462   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7463   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7464                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7465                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7466                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7467                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7468                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7469                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7470                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7471                "        > ccccccccccccccccccccccccccccccccccccccccc;",
7472                Style);
7473 
7474   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7475                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7476                "    + cc;",
7477                Style);
7478   verifyFormat("int a = aa\n"
7479                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7480                "        * cccccccccccccccccccccccccccccccccccc;",
7481                Style);
7482 
7483   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7484   verifyFormat("return (a > b\n"
7485                "    // comment1\n"
7486                "    // comment2\n"
7487                "    || c);",
7488                Style);
7489 }
7490 
7491 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
7492   FormatStyle Style = getLLVMStyle();
7493   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7494   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7495                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7496                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7497                Style);
7498 }
7499 
7500 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
7501   FormatStyle Style = getLLVMStyleWithColumns(40);
7502   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7503   Style.BinPackArguments = false;
7504   verifyFormat("void test() {\n"
7505                "  someFunction(\n"
7506                "      this + argument + is + quite\n"
7507                "      + long + so + it + gets + wrapped\n"
7508                "      + but + remains + bin - packed);\n"
7509                "}",
7510                Style);
7511   verifyFormat("void test() {\n"
7512                "  someFunction(arg1,\n"
7513                "               this + argument + is\n"
7514                "                   + quite + long + so\n"
7515                "                   + it + gets + wrapped\n"
7516                "                   + but + remains + bin\n"
7517                "                   - packed,\n"
7518                "               arg3);\n"
7519                "}",
7520                Style);
7521   verifyFormat("void test() {\n"
7522                "  someFunction(\n"
7523                "      arg1,\n"
7524                "      this + argument + has\n"
7525                "          + anotherFunc(nested,\n"
7526                "                        calls + whose\n"
7527                "                            + arguments\n"
7528                "                            + are + also\n"
7529                "                            + wrapped,\n"
7530                "                        in + addition)\n"
7531                "          + to + being + bin - packed,\n"
7532                "      arg3);\n"
7533                "}",
7534                Style);
7535 
7536   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7537   verifyFormat("void test() {\n"
7538                "  someFunction(\n"
7539                "      arg1,\n"
7540                "      this + argument + has +\n"
7541                "          anotherFunc(nested,\n"
7542                "                      calls + whose +\n"
7543                "                          arguments +\n"
7544                "                          are + also +\n"
7545                "                          wrapped,\n"
7546                "                      in + addition) +\n"
7547                "          to + being + bin - packed,\n"
7548                "      arg3);\n"
7549                "}",
7550                Style);
7551 }
7552 
7553 TEST_F(FormatTest, BreakBinaryOperatorsInPresenceOfTemplates) {
7554   auto Style = getLLVMStyleWithColumns(45);
7555   EXPECT_EQ(Style.BreakBeforeBinaryOperators, FormatStyle::BOS_None);
7556   verifyFormat("bool b =\n"
7557                "    is_default_constructible_v<hash<T>> and\n"
7558                "    is_copy_constructible_v<hash<T>> and\n"
7559                "    is_move_constructible_v<hash<T>> and\n"
7560                "    is_copy_assignable_v<hash<T>> and\n"
7561                "    is_move_assignable_v<hash<T>> and\n"
7562                "    is_destructible_v<hash<T>> and\n"
7563                "    is_swappable_v<hash<T>> and\n"
7564                "    is_callable_v<hash<T>(T)>;",
7565                Style);
7566 
7567   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7568   verifyFormat("bool b = is_default_constructible_v<hash<T>>\n"
7569                "         and is_copy_constructible_v<hash<T>>\n"
7570                "         and is_move_constructible_v<hash<T>>\n"
7571                "         and is_copy_assignable_v<hash<T>>\n"
7572                "         and is_move_assignable_v<hash<T>>\n"
7573                "         and is_destructible_v<hash<T>>\n"
7574                "         and is_swappable_v<hash<T>>\n"
7575                "         and is_callable_v<hash<T>(T)>;",
7576                Style);
7577 
7578   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7579   verifyFormat("bool b = is_default_constructible_v<hash<T>>\n"
7580                "         and is_copy_constructible_v<hash<T>>\n"
7581                "         and is_move_constructible_v<hash<T>>\n"
7582                "         and is_copy_assignable_v<hash<T>>\n"
7583                "         and is_move_assignable_v<hash<T>>\n"
7584                "         and is_destructible_v<hash<T>>\n"
7585                "         and is_swappable_v<hash<T>>\n"
7586                "         and is_callable_v<hash<T>(T)>;",
7587                Style);
7588 }
7589 
7590 TEST_F(FormatTest, ConstructorInitializers) {
7591   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
7592   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
7593                getLLVMStyleWithColumns(45));
7594   verifyFormat("Constructor()\n"
7595                "    : Inttializer(FitsOnTheLine) {}",
7596                getLLVMStyleWithColumns(44));
7597   verifyFormat("Constructor()\n"
7598                "    : Inttializer(FitsOnTheLine) {}",
7599                getLLVMStyleWithColumns(43));
7600 
7601   verifyFormat("template <typename T>\n"
7602                "Constructor() : Initializer(FitsOnTheLine) {}",
7603                getLLVMStyleWithColumns(45));
7604 
7605   verifyFormat(
7606       "SomeClass::Constructor()\n"
7607       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
7608 
7609   verifyFormat(
7610       "SomeClass::Constructor()\n"
7611       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7612       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
7613   verifyFormat(
7614       "SomeClass::Constructor()\n"
7615       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7616       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
7617   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7618                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7619                "    : aaaaaaaaaa(aaaaaa) {}");
7620 
7621   verifyFormat("Constructor()\n"
7622                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7623                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7624                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7625                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
7626 
7627   verifyFormat("Constructor()\n"
7628                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7629                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7630 
7631   verifyFormat("Constructor(int Parameter = 0)\n"
7632                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
7633                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
7634   verifyFormat("Constructor()\n"
7635                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
7636                "}",
7637                getLLVMStyleWithColumns(60));
7638   verifyFormat("Constructor()\n"
7639                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7640                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
7641 
7642   // Here a line could be saved by splitting the second initializer onto two
7643   // lines, but that is not desirable.
7644   verifyFormat("Constructor()\n"
7645                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
7646                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
7647                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7648 
7649   FormatStyle OnePerLine = getLLVMStyle();
7650   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never;
7651   verifyFormat("MyClass::MyClass()\n"
7652                "    : a(a),\n"
7653                "      b(b),\n"
7654                "      c(c) {}",
7655                OnePerLine);
7656   verifyFormat("MyClass::MyClass()\n"
7657                "    : a(a), // comment\n"
7658                "      b(b),\n"
7659                "      c(c) {}",
7660                OnePerLine);
7661   verifyFormat("MyClass::MyClass(int a)\n"
7662                "    : b(a),      // comment\n"
7663                "      c(a + 1) { // lined up\n"
7664                "}",
7665                OnePerLine);
7666   verifyFormat("Constructor()\n"
7667                "    : a(b, b, b) {}",
7668                OnePerLine);
7669   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7670   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
7671   verifyFormat("SomeClass::Constructor()\n"
7672                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7673                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7674                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7675                OnePerLine);
7676   verifyFormat("SomeClass::Constructor()\n"
7677                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
7678                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7679                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7680                OnePerLine);
7681   verifyFormat("MyClass::MyClass(int var)\n"
7682                "    : some_var_(var),            // 4 space indent\n"
7683                "      some_other_var_(var + 1) { // lined up\n"
7684                "}",
7685                OnePerLine);
7686   verifyFormat("Constructor()\n"
7687                "    : aaaaa(aaaaaa),\n"
7688                "      aaaaa(aaaaaa),\n"
7689                "      aaaaa(aaaaaa),\n"
7690                "      aaaaa(aaaaaa),\n"
7691                "      aaaaa(aaaaaa) {}",
7692                OnePerLine);
7693   verifyFormat("Constructor()\n"
7694                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
7695                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
7696                OnePerLine);
7697   OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine;
7698   verifyFormat(
7699       "Constructor()\n"
7700       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7701       "          aaaaaaaaaaa().aaa(),\n"
7702       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7703       OnePerLine);
7704   OnePerLine.ColumnLimit = 60;
7705   verifyFormat("Constructor()\n"
7706                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
7707                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
7708                OnePerLine);
7709 
7710   verifyFormat("Constructor()\n"
7711                "    : // Comment forcing unwanted break.\n"
7712                "      aaaa(aaaa) {}",
7713                "Constructor() :\n"
7714                "    // Comment forcing unwanted break.\n"
7715                "    aaaa(aaaa) {}");
7716 }
7717 
7718 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
7719   FormatStyle Style = getLLVMStyleWithColumns(60);
7720   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7721   Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
7722 
7723   for (int i = 0; i < 4; ++i) {
7724     // Test all combinations of parameters that should not have an effect.
7725     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
7726     Style.AllowAllArgumentsOnNextLine = i & 2;
7727 
7728     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7729     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7730     verifyFormat("Constructor()\n"
7731                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7732                  Style);
7733     verifyFormat("Constructor() : a(a), b(b) {}", Style);
7734 
7735     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7736     verifyFormat("Constructor()\n"
7737                  "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
7738                  "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7739                  Style);
7740     verifyFormat("Constructor() : a(a), b(b) {}", Style);
7741 
7742     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7743     verifyFormat("Constructor()\n"
7744                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7745                  Style);
7746     verifyFormat("Constructor()\n"
7747                  "    : a(a), b(b) {}",
7748                  Style);
7749     verifyFormat("Constructor()\n"
7750                  "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
7751                  "    , bbbbbbbbbbbbbbbbbbbbb(b)\n"
7752                  "    , cccccccccccccccccccccc(c) {}",
7753                  Style);
7754 
7755     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
7756     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7757     verifyFormat("Constructor()\n"
7758                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7759                  Style);
7760 
7761     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7762     verifyFormat("Constructor()\n"
7763                  "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
7764                  "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
7765                  Style);
7766 
7767     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7768     verifyFormat("Constructor()\n"
7769                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7770                  Style);
7771     verifyFormat("Constructor()\n"
7772                  "    : a(a), b(b) {}",
7773                  Style);
7774     verifyFormat("Constructor()\n"
7775                  "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
7776                  "      bbbbbbbbbbbbbbbbbbbbb(b),\n"
7777                  "      cccccccccccccccccccccc(c) {}",
7778                  Style);
7779 
7780     Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
7781     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7782     verifyFormat("Constructor() :\n"
7783                  "    aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7784                  Style);
7785 
7786     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7787     verifyFormat("Constructor() :\n"
7788                  "    aaaaaaaaaaaaaaaaaa(a),\n"
7789                  "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
7790                  Style);
7791 
7792     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7793     verifyFormat("Constructor() :\n"
7794                  "    aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7795                  Style);
7796     verifyFormat("Constructor() :\n"
7797                  "    a(a), b(b) {}",
7798                  Style);
7799     verifyFormat("Constructor() :\n"
7800                  "    aaaaaaaaaaaaaaaaaaaa(a),\n"
7801                  "    bbbbbbbbbbbbbbbbbbbbb(b),\n"
7802                  "    cccccccccccccccccccccc(c) {}",
7803                  Style);
7804   }
7805 
7806   // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
7807   // AllowAllConstructorInitializersOnNextLine in all
7808   // BreakConstructorInitializers modes
7809   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7810   Style.AllowAllParametersOfDeclarationOnNextLine = true;
7811   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7812   verifyFormat("SomeClassWithALongName::Constructor(\n"
7813                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
7814                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
7815                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7816                Style);
7817 
7818   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7819   verifyFormat("SomeClassWithALongName::Constructor(\n"
7820                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7821                "    int bbbbbbbbbbbbb,\n"
7822                "    int cccccccccccccccc)\n"
7823                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7824                Style);
7825 
7826   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7827   verifyFormat("SomeClassWithALongName::Constructor(\n"
7828                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7829                "    int bbbbbbbbbbbbb,\n"
7830                "    int cccccccccccccccc)\n"
7831                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7832                Style);
7833 
7834   Style.AllowAllParametersOfDeclarationOnNextLine = false;
7835   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7836   verifyFormat("SomeClassWithALongName::Constructor(\n"
7837                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7838                "    int bbbbbbbbbbbbb)\n"
7839                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
7840                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7841                Style);
7842 
7843   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
7844 
7845   Style.AllowAllParametersOfDeclarationOnNextLine = true;
7846   verifyFormat("SomeClassWithALongName::Constructor(\n"
7847                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
7848                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
7849                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
7850                Style);
7851 
7852   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7853   verifyFormat("SomeClassWithALongName::Constructor(\n"
7854                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7855                "    int bbbbbbbbbbbbb,\n"
7856                "    int cccccccccccccccc)\n"
7857                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7858                Style);
7859 
7860   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7861   verifyFormat("SomeClassWithALongName::Constructor(\n"
7862                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7863                "    int bbbbbbbbbbbbb,\n"
7864                "    int cccccccccccccccc)\n"
7865                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7866                Style);
7867 
7868   Style.AllowAllParametersOfDeclarationOnNextLine = false;
7869   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7870   verifyFormat("SomeClassWithALongName::Constructor(\n"
7871                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7872                "    int bbbbbbbbbbbbb)\n"
7873                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
7874                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
7875                Style);
7876 
7877   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
7878   Style.AllowAllParametersOfDeclarationOnNextLine = true;
7879   verifyFormat("SomeClassWithALongName::Constructor(\n"
7880                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
7881                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
7882                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
7883                Style);
7884 
7885   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7886   verifyFormat("SomeClassWithALongName::Constructor(\n"
7887                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7888                "    int bbbbbbbbbbbbb,\n"
7889                "    int cccccccccccccccc) :\n"
7890                "    aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7891                Style);
7892 
7893   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7894   verifyFormat("SomeClassWithALongName::Constructor(\n"
7895                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7896                "    int bbbbbbbbbbbbb,\n"
7897                "    int cccccccccccccccc) :\n"
7898                "    aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7899                Style);
7900 
7901   Style.AllowAllParametersOfDeclarationOnNextLine = false;
7902   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7903   verifyFormat("SomeClassWithALongName::Constructor(\n"
7904                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7905                "    int bbbbbbbbbbbbb) :\n"
7906                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
7907                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
7908                Style);
7909 
7910   Style = getLLVMStyleWithColumns(0);
7911   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7912   verifyFormat("Foo(Bar bar, Baz baz) : bar(bar), baz(baz) {}", Style);
7913   verifyNoChange("Foo(Bar bar, Baz baz)\n"
7914                  "    : bar(bar), baz(baz) {}",
7915                  Style);
7916 }
7917 
7918 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
7919   FormatStyle Style = getLLVMStyleWithColumns(60);
7920   Style.BinPackArguments = false;
7921   for (int i = 0; i < 4; ++i) {
7922     // Test all combinations of parameters that should not have an effect.
7923     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
7924     Style.PackConstructorInitializers =
7925         i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never;
7926 
7927     Style.AllowAllArgumentsOnNextLine = true;
7928     verifyFormat("void foo() {\n"
7929                  "  FunctionCallWithReallyLongName(\n"
7930                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
7931                  "}",
7932                  Style);
7933     Style.AllowAllArgumentsOnNextLine = false;
7934     verifyFormat("void foo() {\n"
7935                  "  FunctionCallWithReallyLongName(\n"
7936                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7937                  "      bbbbbbbbbbbb);\n"
7938                  "}",
7939                  Style);
7940 
7941     Style.AllowAllArgumentsOnNextLine = true;
7942     verifyFormat("void foo() {\n"
7943                  "  auto VariableWithReallyLongName = {\n"
7944                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
7945                  "}",
7946                  Style);
7947     Style.AllowAllArgumentsOnNextLine = false;
7948     verifyFormat("void foo() {\n"
7949                  "  auto VariableWithReallyLongName = {\n"
7950                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7951                  "      bbbbbbbbbbbb};\n"
7952                  "}",
7953                  Style);
7954   }
7955 
7956   // This parameter should not affect declarations.
7957   Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
7958   Style.AllowAllArgumentsOnNextLine = false;
7959   Style.AllowAllParametersOfDeclarationOnNextLine = true;
7960   verifyFormat("void FunctionCallWithReallyLongName(\n"
7961                "    int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
7962                Style);
7963   Style.AllowAllParametersOfDeclarationOnNextLine = false;
7964   verifyFormat("void FunctionCallWithReallyLongName(\n"
7965                "    int aaaaaaaaaaaaaaaaaaaaaaa,\n"
7966                "    int bbbbbbbbbbbb);",
7967                Style);
7968 }
7969 
7970 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
7971   // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
7972   // and BAS_Align.
7973   FormatStyle Style = getLLVMStyleWithColumns(35);
7974   StringRef Input = "functionCall(paramA, paramB, paramC);\n"
7975                     "void functionDecl(int A, int B, int C);";
7976   Style.AllowAllArgumentsOnNextLine = false;
7977   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7978   verifyFormat(StringRef("functionCall(paramA, paramB,\n"
7979                          "    paramC);\n"
7980                          "void functionDecl(int A, int B,\n"
7981                          "    int C);"),
7982                Input, Style);
7983   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
7984   verifyFormat(StringRef("functionCall(paramA, paramB,\n"
7985                          "             paramC);\n"
7986                          "void functionDecl(int A, int B,\n"
7987                          "                  int C);"),
7988                Input, Style);
7989   // However, BAS_AlwaysBreak and BAS_BlockIndent should take precedence over
7990   // AllowAllArgumentsOnNextLine.
7991   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7992   verifyFormat(StringRef("functionCall(\n"
7993                          "    paramA, paramB, paramC);\n"
7994                          "void functionDecl(\n"
7995                          "    int A, int B, int C);"),
7996                Input, Style);
7997   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
7998   verifyFormat("functionCall(\n"
7999                "    paramA, paramB, paramC\n"
8000                ");\n"
8001                "void functionDecl(\n"
8002                "    int A, int B, int C\n"
8003                ");",
8004                Input, Style);
8005 
8006   // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
8007   // first argument.
8008   Style.AllowAllArgumentsOnNextLine = true;
8009   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8010   verifyFormat(StringRef("functionCall(\n"
8011                          "    paramA, paramB, paramC);\n"
8012                          "void functionDecl(\n"
8013                          "    int A, int B, int C);"),
8014                Input, Style);
8015   // It wouldn't fit on one line with aligned parameters so this setting
8016   // doesn't change anything for BAS_Align.
8017   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
8018   verifyFormat(StringRef("functionCall(paramA, paramB,\n"
8019                          "             paramC);\n"
8020                          "void functionDecl(int A, int B,\n"
8021                          "                  int C);"),
8022                Input, Style);
8023   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
8024   verifyFormat(StringRef("functionCall(\n"
8025                          "    paramA, paramB, paramC);\n"
8026                          "void functionDecl(\n"
8027                          "    int A, int B, int C);"),
8028                Input, Style);
8029 }
8030 
8031 TEST_F(FormatTest, BreakFunctionDefinitionParameters) {
8032   StringRef Input = "void functionDecl(paramA, paramB, paramC);\n"
8033                     "void emptyFunctionDefinition() {}\n"
8034                     "void functionDefinition(int A, int B, int C) {}\n"
8035                     "Class::Class(int A, int B) : m_A(A), m_B(B) {}";
8036   verifyFormat(Input);
8037 
8038   FormatStyle Style = getLLVMStyle();
8039   EXPECT_FALSE(Style.BreakFunctionDefinitionParameters);
8040   Style.BreakFunctionDefinitionParameters = true;
8041   verifyFormat("void functionDecl(paramA, paramB, paramC);\n"
8042                "void emptyFunctionDefinition() {}\n"
8043                "void functionDefinition(\n"
8044                "    int A, int B, int C) {}\n"
8045                "Class::Class(\n"
8046                "    int A, int B)\n"
8047                "    : m_A(A), m_B(B) {}",
8048                Input, Style);
8049 
8050   // Test the style where all parameters are on their own lines.
8051   Style.AllowAllParametersOfDeclarationOnNextLine = false;
8052   Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8053   verifyFormat("void functionDecl(paramA, paramB, paramC);\n"
8054                "void emptyFunctionDefinition() {}\n"
8055                "void functionDefinition(\n"
8056                "    int A,\n"
8057                "    int B,\n"
8058                "    int C) {}\n"
8059                "Class::Class(\n"
8060                "    int A,\n"
8061                "    int B)\n"
8062                "    : m_A(A), m_B(B) {}",
8063                Input, Style);
8064 }
8065 
8066 TEST_F(FormatTest, BreakBeforeInlineASMColon) {
8067   FormatStyle Style = getLLVMStyle();
8068   Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Never;
8069   /* Test the behaviour with long lines */
8070   Style.ColumnLimit = 40;
8071   verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8072                "             : : val);",
8073                Style);
8074   verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8075                "             : val1 : val2);",
8076                Style);
8077   verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8078                "    \"cpuid\\n\\t\"\n"
8079                "    \"xchgq\\t%%rbx %%rsi\\n\\t\",\n"
8080                "    : \"=a\" : \"a\");",
8081                Style);
8082   Style.ColumnLimit = 80;
8083   verifyFormat("asm volatile(\"string\", : : val);", Style);
8084   verifyFormat("asm volatile(\"string\", : val1 : val2);", Style);
8085 
8086   Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Always;
8087   verifyFormat("asm volatile(\"string\",\n"
8088                "             :\n"
8089                "             : val);",
8090                Style);
8091   verifyFormat("asm volatile(\"string\",\n"
8092                "             : val1\n"
8093                "             : val2);",
8094                Style);
8095   /* Test the behaviour with long lines */
8096   Style.ColumnLimit = 40;
8097   verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8098                "    \"cpuid\\n\\t\"\n"
8099                "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
8100                "    : \"=a\"(*rEAX)\n"
8101                "    : \"a\"(value));",
8102                Style);
8103   verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8104                "    \"cpuid\\n\\t\"\n"
8105                "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
8106                "    :\n"
8107                "    : \"a\"(value));",
8108                Style);
8109   verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8110                "             :\n"
8111                "             : val);",
8112                Style);
8113   verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8114                "             : val1\n"
8115                "             : val2);",
8116                Style);
8117 }
8118 
8119 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
8120   FormatStyle Style = getLLVMStyle();
8121   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
8122 
8123   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
8124   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
8125                getStyleWithColumns(Style, 45));
8126   verifyFormat("Constructor() :\n"
8127                "    Initializer(FitsOnTheLine) {}",
8128                getStyleWithColumns(Style, 44));
8129   verifyFormat("Constructor() :\n"
8130                "    Initializer(FitsOnTheLine) {}",
8131                getStyleWithColumns(Style, 43));
8132 
8133   verifyFormat("template <typename T>\n"
8134                "Constructor() : Initializer(FitsOnTheLine) {}",
8135                getStyleWithColumns(Style, 50));
8136   verifyFormat(
8137       "Class::Class(int some, int arguments, int loooooooooooooooooooong,\n"
8138       "             int mooooooooooooore) noexcept :\n"
8139       "    Super{some, arguments}, Member{5}, Member2{2} {}",
8140       Style);
8141   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
8142   verifyFormat(
8143       "SomeClass::Constructor() :\n"
8144       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8145       Style);
8146   verifyFormat(
8147       "SomeClass::Constructor() : // NOLINT\n"
8148       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8149       Style);
8150 
8151   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
8152   verifyFormat(
8153       "SomeClass::Constructor() :\n"
8154       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8155       Style);
8156   verifyFormat(
8157       "SomeClass::Constructor() : // NOLINT\n"
8158       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8159       Style);
8160 
8161   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
8162   verifyFormat(
8163       "SomeClass::Constructor() :\n"
8164       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8165       Style);
8166 
8167   verifyFormat(
8168       "SomeClass::Constructor() :\n"
8169       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8170       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8171       Style);
8172   verifyFormat(
8173       "SomeClass::Constructor() :\n"
8174       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8175       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8176       Style);
8177   verifyFormat(
8178       "Ctor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8179       "     aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) : aaaaaaaaaa(aaaaaa) {}",
8180       Style);
8181 
8182   verifyFormat("Constructor() :\n"
8183                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8184                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8185                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8186                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
8187                Style);
8188 
8189   verifyFormat("Constructor() :\n"
8190                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8191                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8192                Style);
8193 
8194   verifyFormat("Constructor(int Parameter = 0) :\n"
8195                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
8196                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
8197                Style);
8198   verifyFormat("Constructor() :\n"
8199                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
8200                "}",
8201                getStyleWithColumns(Style, 60));
8202   verifyFormat("Constructor() :\n"
8203                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8204                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
8205                Style);
8206 
8207   // Here a line could be saved by splitting the second initializer onto two
8208   // lines, but that is not desirable.
8209   verifyFormat("Constructor() :\n"
8210                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
8211                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
8212                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8213                Style);
8214 
8215   FormatStyle OnePerLine = Style;
8216   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
8217   verifyFormat("SomeClass::Constructor() :\n"
8218                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8219                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8220                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8221                OnePerLine);
8222   verifyFormat("SomeClass::Constructor() :\n"
8223                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
8224                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8225                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8226                OnePerLine);
8227   verifyFormat("Foo::Foo(int i, int j) : // NOLINT\n"
8228                "    i(i),                // comment\n"
8229                "    j(j) {}",
8230                OnePerLine);
8231   verifyFormat("MyClass::MyClass(int var) :\n"
8232                "    some_var_(var),            // 4 space indent\n"
8233                "    some_other_var_(var + 1) { // lined up\n"
8234                "}",
8235                OnePerLine);
8236   verifyFormat("Constructor() :\n"
8237                "    aaaaa(aaaaaa),\n"
8238                "    aaaaa(aaaaaa),\n"
8239                "    aaaaa(aaaaaa),\n"
8240                "    aaaaa(aaaaaa),\n"
8241                "    aaaaa(aaaaaa) {}",
8242                OnePerLine);
8243   verifyFormat("Constructor() :\n"
8244                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
8245                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
8246                OnePerLine);
8247   OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8248   verifyFormat("Constructor() :\n"
8249                "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
8250                "        aaaaaaaaaaa().aaa(),\n"
8251                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8252                OnePerLine);
8253   OnePerLine.ColumnLimit = 60;
8254   verifyFormat("Constructor() :\n"
8255                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
8256                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
8257                OnePerLine);
8258 
8259   verifyFormat("Constructor() :\n"
8260                "    // Comment forcing unwanted break.\n"
8261                "    aaaa(aaaa) {}",
8262                Style);
8263   verifyFormat("Constructor() : // NOLINT\n"
8264                "    aaaa(aaaa) {}",
8265                Style);
8266   verifyFormat("Constructor() : // A very long trailing comment that cannot fit"
8267                " on a single\n"
8268                "                // line.\n"
8269                "    aaaa(aaaa) {}",
8270                "Constructor() : // A very long trailing comment that cannot fit"
8271                " on a single line.\n"
8272                "    aaaa(aaaa) {}",
8273                Style);
8274 
8275   Style.ColumnLimit = 0;
8276   verifyFormat("SomeClass::Constructor() :\n"
8277                "    a(a) {}",
8278                Style);
8279   verifyFormat("SomeClass::Constructor() noexcept :\n"
8280                "    a(a) {}",
8281                Style);
8282   verifyFormat("SomeClass::Constructor() :\n"
8283                "    a(a), b(b), c(c) {}",
8284                Style);
8285   verifyFormat("SomeClass::Constructor() :\n"
8286                "    a(a) {\n"
8287                "  foo();\n"
8288                "  bar();\n"
8289                "}",
8290                Style);
8291 
8292   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
8293   verifyFormat("SomeClass::Constructor() :\n"
8294                "    a(a), b(b), c(c) {\n"
8295                "}",
8296                Style);
8297   verifyFormat("SomeClass::Constructor() :\n"
8298                "    a(a) {\n"
8299                "}",
8300                Style);
8301 
8302   Style.ColumnLimit = 80;
8303   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
8304   Style.ConstructorInitializerIndentWidth = 2;
8305   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style);
8306   verifyFormat("SomeClass::Constructor() :\n"
8307                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8308                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
8309                Style);
8310 
8311   // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
8312   // well
8313   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
8314   verifyFormat(
8315       "class SomeClass\n"
8316       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8317       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8318       Style);
8319   Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
8320   verifyFormat(
8321       "class SomeClass\n"
8322       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8323       "  , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8324       Style);
8325   Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
8326   verifyFormat(
8327       "class SomeClass :\n"
8328       "  public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8329       "  public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8330       Style);
8331   Style.BreakInheritanceList = FormatStyle::BILS_AfterComma;
8332   verifyFormat(
8333       "class SomeClass\n"
8334       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8335       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8336       Style);
8337 }
8338 
8339 #ifndef EXPENSIVE_CHECKS
8340 // Expensive checks enables libstdc++ checking which includes validating the
8341 // state of ranges used in std::priority_queue - this blows out the
8342 // runtime/scalability of the function and makes this test unacceptably slow.
8343 TEST_F(FormatTest, MemoizationTests) {
8344   // This breaks if the memoization lookup does not take \c Indent and
8345   // \c LastSpace into account.
8346   verifyFormat(
8347       "extern CFRunLoopTimerRef\n"
8348       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
8349       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
8350       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
8351       "                     CFRunLoopTimerContext *context) {}");
8352 
8353   // Deep nesting somewhat works around our memoization.
8354   verifyFormat(
8355       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8356       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8357       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8358       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8359       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
8360       getLLVMStyleWithColumns(65));
8361   verifyFormat(
8362       "aaaaa(\n"
8363       "    aaaaa,\n"
8364       "    aaaaa(\n"
8365       "        aaaaa,\n"
8366       "        aaaaa(\n"
8367       "            aaaaa,\n"
8368       "            aaaaa(\n"
8369       "                aaaaa,\n"
8370       "                aaaaa(\n"
8371       "                    aaaaa,\n"
8372       "                    aaaaa(\n"
8373       "                        aaaaa,\n"
8374       "                        aaaaa(\n"
8375       "                            aaaaa,\n"
8376       "                            aaaaa(\n"
8377       "                                aaaaa,\n"
8378       "                                aaaaa(\n"
8379       "                                    aaaaa,\n"
8380       "                                    aaaaa(\n"
8381       "                                        aaaaa,\n"
8382       "                                        aaaaa(\n"
8383       "                                            aaaaa,\n"
8384       "                                            aaaaa(\n"
8385       "                                                aaaaa,\n"
8386       "                                                aaaaa))))))))))));",
8387       getLLVMStyleWithColumns(65));
8388   verifyFormat(
8389       "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"
8390       "                                  a),\n"
8391       "                                a),\n"
8392       "                              a),\n"
8393       "                            a),\n"
8394       "                          a),\n"
8395       "                        a),\n"
8396       "                      a),\n"
8397       "                    a),\n"
8398       "                  a),\n"
8399       "                a),\n"
8400       "              a),\n"
8401       "            a),\n"
8402       "          a),\n"
8403       "        a),\n"
8404       "      a),\n"
8405       "    a),\n"
8406       "  a)",
8407       getLLVMStyleWithColumns(65));
8408 
8409   // This test takes VERY long when memoization is broken.
8410   FormatStyle OnePerLine = getLLVMStyle();
8411   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
8412   OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8413   std::string input = "Constructor()\n"
8414                       "    : aaaa(a,\n";
8415   for (unsigned i = 0, e = 80; i != e; ++i)
8416     input += "           a,\n";
8417   input += "           a) {}";
8418   verifyFormat(input, OnePerLine);
8419   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
8420   verifyFormat(input, OnePerLine);
8421 }
8422 #endif
8423 
8424 TEST_F(FormatTest, BreaksAsHighAsPossible) {
8425   verifyFormat(
8426       "void f() {\n"
8427       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
8428       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
8429       "    f();\n"
8430       "}");
8431   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
8432                "    Intervals[i - 1].getRange().getLast()) {\n}");
8433 }
8434 
8435 TEST_F(FormatTest, BreaksFunctionDeclarations) {
8436   // Principially, we break function declarations in a certain order:
8437   // 1) break amongst arguments.
8438   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
8439                "                              Cccccccccccccc cccccccccccccc);");
8440   verifyFormat("template <class TemplateIt>\n"
8441                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
8442                "                            TemplateIt *stop) {}");
8443 
8444   // 2) break after return type.
8445   verifyGoogleFormat(
8446       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8447       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);");
8448 
8449   // 3) break after (.
8450   verifyGoogleFormat(
8451       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
8452       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);");
8453 
8454   // 4) break before after nested name specifiers.
8455   verifyGoogleFormat(
8456       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8457       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
8458       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);");
8459 
8460   // However, there are exceptions, if a sufficient amount of lines can be
8461   // saved.
8462   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
8463   // more adjusting.
8464   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
8465                "                                  Cccccccccccccc cccccccccc,\n"
8466                "                                  Cccccccccccccc cccccccccc,\n"
8467                "                                  Cccccccccccccc cccccccccc,\n"
8468                "                                  Cccccccccccccc cccccccccc);");
8469   verifyGoogleFormat(
8470       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8471       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8472       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8473       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
8474   verifyFormat(
8475       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
8476       "                                          Cccccccccccccc cccccccccc,\n"
8477       "                                          Cccccccccccccc cccccccccc,\n"
8478       "                                          Cccccccccccccc cccccccccc,\n"
8479       "                                          Cccccccccccccc cccccccccc,\n"
8480       "                                          Cccccccccccccc cccccccccc,\n"
8481       "                                          Cccccccccccccc cccccccccc);");
8482   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8483                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8484                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8485                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8486                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
8487 
8488   // Break after multi-line parameters.
8489   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8490                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8491                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8492                "    bbbb bbbb);");
8493   verifyFormat("void SomeLoooooooooooongFunction(\n"
8494                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
8495                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8496                "    int bbbbbbbbbbbbb);");
8497 
8498   // Treat overloaded operators like other functions.
8499   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8500                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
8501   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8502                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
8503   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8504                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
8505   verifyGoogleFormat(
8506       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
8507       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
8508   verifyGoogleFormat(
8509       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
8510       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
8511   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8512                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
8513   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
8514                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
8515   verifyGoogleFormat(
8516       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
8517       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8518       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
8519   verifyGoogleFormat("template <typename T>\n"
8520                      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8521                      "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
8522                      "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
8523 
8524   FormatStyle Style = getLLVMStyle();
8525   Style.PointerAlignment = FormatStyle::PAS_Left;
8526   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8527                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
8528                Style);
8529   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
8530                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8531                Style);
8532 }
8533 
8534 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
8535   // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
8536   // Prefer keeping `::` followed by `operator` together.
8537   verifyFormat("const aaaa::bbbbbbb &\n"
8538                "ccccccccc::operator++() {\n"
8539                "  stuff();\n"
8540                "}",
8541                "const aaaa::bbbbbbb\n"
8542                "&ccccccccc::operator++() { stuff(); }",
8543                getLLVMStyleWithColumns(40));
8544 }
8545 
8546 TEST_F(FormatTest, TrailingReturnType) {
8547   verifyFormat("auto foo() -> int;");
8548   // correct trailing return type spacing
8549   verifyFormat("auto operator->() -> int;");
8550   verifyFormat("auto operator++(int) -> int;");
8551 
8552   verifyFormat("struct S {\n"
8553                "  auto bar() const -> int;\n"
8554                "};");
8555   verifyFormat("template <size_t Order, typename T>\n"
8556                "auto load_img(const std::string &filename)\n"
8557                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
8558   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
8559                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
8560   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
8561   verifyFormat("template <typename T>\n"
8562                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
8563                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
8564 
8565   FormatStyle Style = getLLVMStyleWithColumns(60);
8566   verifyFormat("#define MAKE_DEF(NAME)                                     \\\n"
8567                "  auto NAME() -> int { return 42; }",
8568                Style);
8569 
8570   // Not trailing return types.
8571   verifyFormat("void f() { auto a = b->c(); }");
8572   verifyFormat("auto a = p->foo();");
8573   verifyFormat("int a = p->foo();");
8574   verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };");
8575 }
8576 
8577 TEST_F(FormatTest, DeductionGuides) {
8578   verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
8579   verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
8580   verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
8581   verifyFormat(
8582       "template <class... T>\n"
8583       "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
8584   verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
8585   verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
8586   verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
8587   verifyFormat("template <class T> A() -> A<(3 < 2)>;");
8588   verifyFormat("template <class T> A() -> A<((3) < (2))>;");
8589   verifyFormat("template <class T> x() -> x<1>;");
8590   verifyFormat("template <class T> explicit x(T &) -> x<1>;");
8591 
8592   verifyFormat("A(const char *) -> A<string &>;");
8593   verifyFormat("A() -> A<int>;");
8594 
8595   // Ensure not deduction guides.
8596   verifyFormat("c()->f<int>();");
8597   verifyFormat("x()->foo<1>;");
8598   verifyFormat("x = p->foo<3>();");
8599   verifyFormat("x()->x<1>();");
8600 }
8601 
8602 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
8603   // Avoid breaking before trailing 'const' or other trailing annotations, if
8604   // they are not function-like.
8605   FormatStyle Style = getGoogleStyleWithColumns(47);
8606   verifyFormat("void someLongFunction(\n"
8607                "    int someLoooooooooooooongParameter) const {\n}",
8608                getLLVMStyleWithColumns(47));
8609   verifyFormat("LoooooongReturnType\n"
8610                "someLoooooooongFunction() const {}",
8611                getLLVMStyleWithColumns(47));
8612   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
8613                "    const {}",
8614                Style);
8615   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8616                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
8617   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8618                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
8619   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8620                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
8621   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
8622                "                   aaaaaaaaaaa aaaaa) const override;");
8623   verifyGoogleFormat(
8624       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8625       "    const override;");
8626 
8627   // Even if the first parameter has to be wrapped.
8628   verifyFormat("void someLongFunction(\n"
8629                "    int someLongParameter) const {}",
8630                getLLVMStyleWithColumns(46));
8631   verifyFormat("void someLongFunction(\n"
8632                "    int someLongParameter) const {}",
8633                Style);
8634   verifyFormat("void someLongFunction(\n"
8635                "    int someLongParameter) override {}",
8636                Style);
8637   verifyFormat("void someLongFunction(\n"
8638                "    int someLongParameter) OVERRIDE {}",
8639                Style);
8640   verifyFormat("void someLongFunction(\n"
8641                "    int someLongParameter) final {}",
8642                Style);
8643   verifyFormat("void someLongFunction(\n"
8644                "    int someLongParameter) FINAL {}",
8645                Style);
8646   verifyFormat("void someLongFunction(\n"
8647                "    int parameter) const override {}",
8648                Style);
8649 
8650   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
8651   verifyFormat("void someLongFunction(\n"
8652                "    int someLongParameter) const\n"
8653                "{\n"
8654                "}",
8655                Style);
8656 
8657   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
8658   verifyFormat("void someLongFunction(\n"
8659                "    int someLongParameter) const\n"
8660                "  {\n"
8661                "  }",
8662                Style);
8663 
8664   // Unless these are unknown annotations.
8665   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
8666                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8667                "    LONG_AND_UGLY_ANNOTATION;");
8668 
8669   // Breaking before function-like trailing annotations is fine to keep them
8670   // close to their arguments.
8671   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8672                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
8673   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
8674                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
8675   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
8676                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
8677   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
8678                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
8679   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
8680 
8681   verifyFormat(
8682       "void aaaaaaaaaaaaaaaaaa()\n"
8683       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
8684       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
8685   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8686                "    __attribute__((unused));");
8687 
8688   Style = getGoogleStyle();
8689 
8690   verifyFormat(
8691       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8692       "    GUARDED_BY(aaaaaaaaaaaa);",
8693       Style);
8694   verifyFormat(
8695       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8696       "    GUARDED_BY(aaaaaaaaaaaa);",
8697       Style);
8698   verifyFormat(
8699       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
8700       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8701       Style);
8702   verifyFormat(
8703       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
8704       "    aaaaaaaaaaaaaaaaaaaaaaaaa;",
8705       Style);
8706 
8707   verifyFormat(
8708       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8709       "    ABSL_GUARDED_BY(aaaaaaaaaaaa);",
8710       Style);
8711   verifyFormat(
8712       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8713       "    ABSL_GUARDED_BY(aaaaaaaaaaaa);",
8714       Style);
8715   verifyFormat(
8716       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ABSL_GUARDED_BY(aaaaaaaaaaaa) =\n"
8717       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8718       Style);
8719   verifyFormat(
8720       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ABSL_GUARDED_BY(aaaaaaaaaaaa) =\n"
8721       "    aaaaaaaaaaaaaaaaaaaaaaaaa;",
8722       Style);
8723 }
8724 
8725 TEST_F(FormatTest, FunctionAnnotations) {
8726   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8727                "int OldFunction(const string &parameter) {}");
8728   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8729                "string OldFunction(const string &parameter) {}");
8730   verifyFormat("template <typename T>\n"
8731                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8732                "string OldFunction(const string &parameter) {}");
8733 
8734   // Not function annotations.
8735   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8736                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
8737   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
8738                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
8739   verifyFormat("MACRO(abc).function() // wrap\n"
8740                "    << abc;");
8741   verifyFormat("MACRO(abc)->function() // wrap\n"
8742                "    << abc;");
8743   verifyFormat("MACRO(abc)::function() // wrap\n"
8744                "    << abc;");
8745   verifyFormat("FOO(bar)();", getLLVMStyleWithColumns(0));
8746 }
8747 
8748 TEST_F(FormatTest, BreaksDesireably) {
8749   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
8750                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
8751                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
8752   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8753                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
8754                "}");
8755 
8756   verifyFormat(
8757       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8758       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
8759 
8760   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8761                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8762                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8763 
8764   verifyFormat(
8765       "aaaaaaaa(aaaaaaaaaaaaa,\n"
8766       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8767       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
8768       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8769       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
8770 
8771   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
8772                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8773 
8774   verifyFormat(
8775       "void f() {\n"
8776       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
8777       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8778       "}");
8779   verifyFormat(
8780       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8781       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8782   verifyFormat(
8783       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8784       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8785   verifyFormat(
8786       "aaaaaa(aaa,\n"
8787       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8788       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8789       "       aaaa);");
8790   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8791                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8792                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8793 
8794   // Indent consistently independent of call expression and unary operator.
8795   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8796                "    dddddddddddddddddddddddddddddd));");
8797   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8798                "    dddddddddddddddddddddddddddddd));");
8799   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
8800                "    dddddddddddddddddddddddddddddd));");
8801 
8802   // This test case breaks on an incorrect memoization, i.e. an optimization not
8803   // taking into account the StopAt value.
8804   verifyFormat(
8805       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8806       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8807       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8808       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8809 
8810   verifyFormat("{\n  {\n    {\n"
8811                "      Annotation.SpaceRequiredBefore =\n"
8812                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
8813                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
8814                "    }\n  }\n}");
8815 
8816   // Break on an outer level if there was a break on an inner level.
8817   verifyFormat("f(g(h(a, // comment\n"
8818                "      b, c),\n"
8819                "    d, e),\n"
8820                "  x, y);",
8821                "f(g(h(a, // comment\n"
8822                "    b, c), d, e), x, y);");
8823 
8824   // Prefer breaking similar line breaks.
8825   verifyFormat(
8826       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
8827       "                             NSTrackingMouseEnteredAndExited |\n"
8828       "                             NSTrackingActiveAlways;");
8829 }
8830 
8831 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
8832   FormatStyle NoBinPacking = getGoogleStyle();
8833   NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8834   NoBinPacking.BinPackArguments = true;
8835   verifyFormat("void f() {\n"
8836                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
8837                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8838                "}",
8839                NoBinPacking);
8840   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
8841                "       int aaaaaaaaaaaaaaaaaaaa,\n"
8842                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8843                NoBinPacking);
8844 
8845   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
8846   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8847                "                        vector<int> bbbbbbbbbbbbbbb);",
8848                NoBinPacking);
8849   // FIXME: This behavior difference is probably not wanted. However, currently
8850   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
8851   // template arguments from BreakBeforeParameter being set because of the
8852   // one-per-line formatting.
8853   verifyFormat(
8854       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
8855       "                                             aaaaaaaaaa> aaaaaaaaaa);",
8856       NoBinPacking);
8857   verifyFormat(
8858       "void fffffffffff(\n"
8859       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
8860       "        aaaaaaaaaa);");
8861 }
8862 
8863 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
8864   FormatStyle NoBinPacking = getGoogleStyle();
8865   NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8866   NoBinPacking.BinPackArguments = false;
8867   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
8868                "  aaaaaaaaaaaaaaaaaaaa,\n"
8869                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
8870                NoBinPacking);
8871   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
8872                "        aaaaaaaaaaaaa,\n"
8873                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
8874                NoBinPacking);
8875   verifyFormat(
8876       "aaaaaaaa(aaaaaaaaaaaaa,\n"
8877       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8878       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
8879       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8880       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
8881       NoBinPacking);
8882   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
8883                "    .aaaaaaaaaaaaaaaaaa();",
8884                NoBinPacking);
8885   verifyFormat("void f() {\n"
8886                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8887                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
8888                "}",
8889                NoBinPacking);
8890 
8891   verifyFormat(
8892       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8893       "             aaaaaaaaaaaa,\n"
8894       "             aaaaaaaaaaaa);",
8895       NoBinPacking);
8896   verifyFormat(
8897       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
8898       "                               ddddddddddddddddddddddddddddd),\n"
8899       "             test);",
8900       NoBinPacking);
8901 
8902   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
8903                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
8904                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
8905                "    aaaaaaaaaaaaaaaaaa;",
8906                NoBinPacking);
8907   verifyFormat("a(\"a\"\n"
8908                "  \"a\",\n"
8909                "  a);");
8910 
8911   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
8912   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
8913                "                aaaaaaaaa,\n"
8914                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8915                NoBinPacking);
8916   verifyFormat(
8917       "void f() {\n"
8918       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
8919       "      .aaaaaaa();\n"
8920       "}",
8921       NoBinPacking);
8922   verifyFormat(
8923       "template <class SomeType, class SomeOtherType>\n"
8924       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
8925       NoBinPacking);
8926 }
8927 
8928 TEST_F(FormatTest, FormatsDeclarationBreakAlways) {
8929   FormatStyle BreakAlways = getGoogleStyle();
8930   BreakAlways.BinPackParameters = FormatStyle::BPPS_AlwaysOnePerLine;
8931   verifyFormat("void f(int a,\n"
8932                "       int b);",
8933                BreakAlways);
8934   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8935                "       int bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
8936                "       int cccccccccccccccccccccccc);",
8937                BreakAlways);
8938 
8939   // Ensure AlignAfterOpenBracket interacts correctly with BinPackParameters set
8940   // to BPPS_AlwaysOnePerLine.
8941   BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8942   verifyFormat(
8943       "void someLongFunctionName(\n"
8944       "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8945       "    int b);",
8946       BreakAlways);
8947   BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
8948   verifyFormat(
8949       "void someLongFunctionName(\n"
8950       "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8951       "    int b\n"
8952       ");",
8953       BreakAlways);
8954 }
8955 
8956 TEST_F(FormatTest, FormatsDefinitionBreakAlways) {
8957   FormatStyle BreakAlways = getGoogleStyle();
8958   BreakAlways.BinPackParameters = FormatStyle::BPPS_AlwaysOnePerLine;
8959   verifyFormat("void f(int a,\n"
8960                "       int b) {\n"
8961                "  f(a, b);\n"
8962                "}",
8963                BreakAlways);
8964 
8965   // Ensure BinPackArguments interact correctly when BinPackParameters is set to
8966   // BPPS_AlwaysOnePerLine.
8967   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8968                "       int bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
8969                "       int cccccccccccccccccccccccc) {\n"
8970                "  f(aaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
8971                "    cccccccccccccccccccccccc);\n"
8972                "}",
8973                BreakAlways);
8974   BreakAlways.BinPackArguments = false;
8975   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8976                "       int bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
8977                "       int cccccccccccccccccccccccc) {\n"
8978                "  f(aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8979                "    bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
8980                "    cccccccccccccccccccccccc);\n"
8981                "}",
8982                BreakAlways);
8983 
8984   // Ensure BreakFunctionDefinitionParameters interacts correctly when
8985   // BinPackParameters is set to BPPS_AlwaysOnePerLine.
8986   BreakAlways.BreakFunctionDefinitionParameters = true;
8987   verifyFormat("void f(\n"
8988                "    int a,\n"
8989                "    int b) {\n"
8990                "  f(a, b);\n"
8991                "}",
8992                BreakAlways);
8993   BreakAlways.BreakFunctionDefinitionParameters = false;
8994 
8995   // Ensure AlignAfterOpenBracket interacts correctly with BinPackParameters set
8996   // to BPPS_AlwaysOnePerLine.
8997   BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8998   verifyFormat(
8999       "void someLongFunctionName(\n"
9000       "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9001       "    int b) {\n"
9002       "  someLongFunctionName(\n"
9003       "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b);\n"
9004       "}",
9005       BreakAlways);
9006   BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
9007   verifyFormat(
9008       "void someLongFunctionName(\n"
9009       "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9010       "    int b\n"
9011       ") {\n"
9012       "  someLongFunctionName(\n"
9013       "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b\n"
9014       "  );\n"
9015       "}",
9016       BreakAlways);
9017 }
9018 
9019 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
9020   FormatStyle Style = getLLVMStyleWithColumns(15);
9021   Style.ExperimentalAutoDetectBinPacking = true;
9022   verifyFormat("aaa(aaaa,\n"
9023                "    aaaa,\n"
9024                "    aaaa);\n"
9025                "aaa(aaaa,\n"
9026                "    aaaa,\n"
9027                "    aaaa);",
9028                "aaa(aaaa,\n" // one-per-line
9029                "  aaaa,\n"
9030                "    aaaa  );\n"
9031                "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
9032                Style);
9033   verifyFormat("aaa(aaaa, aaaa,\n"
9034                "    aaaa);\n"
9035                "aaa(aaaa, aaaa,\n"
9036                "    aaaa);",
9037                "aaa(aaaa,  aaaa,\n" // bin-packed
9038                "    aaaa  );\n"
9039                "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
9040                Style);
9041 }
9042 
9043 TEST_F(FormatTest, FormatsBuilderPattern) {
9044   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
9045                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
9046                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
9047                "    .StartsWith(\".init\", ORDER_INIT)\n"
9048                "    .StartsWith(\".fini\", ORDER_FINI)\n"
9049                "    .StartsWith(\".hash\", ORDER_HASH)\n"
9050                "    .Default(ORDER_TEXT);");
9051 
9052   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
9053                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
9054   verifyFormat("aaaaaaa->aaaaaaa\n"
9055                "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9056                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9057                "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
9058   verifyFormat(
9059       "aaaaaaa->aaaaaaa\n"
9060       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9061       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
9062   verifyFormat(
9063       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
9064       "    aaaaaaaaaaaaaa);");
9065   verifyFormat(
9066       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
9067       "    aaaaaa->aaaaaaaaaaaa()\n"
9068       "        ->aaaaaaaaaaaaaaaa(\n"
9069       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9070       "        ->aaaaaaaaaaaaaaaaa();");
9071   verifyGoogleFormat(
9072       "void f() {\n"
9073       "  someo->Add((new util::filetools::Handler(dir))\n"
9074       "                 ->OnEvent1(NewPermanentCallback(\n"
9075       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
9076       "                 ->OnEvent2(NewPermanentCallback(\n"
9077       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
9078       "                 ->OnEvent3(NewPermanentCallback(\n"
9079       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
9080       "                 ->OnEvent5(NewPermanentCallback(\n"
9081       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
9082       "                 ->OnEvent6(NewPermanentCallback(\n"
9083       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
9084       "}");
9085 
9086   verifyFormat(
9087       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
9088   verifyFormat("aaaaaaaaaaaaaaa()\n"
9089                "    .aaaaaaaaaaaaaaa()\n"
9090                "    .aaaaaaaaaaaaaaa()\n"
9091                "    .aaaaaaaaaaaaaaa()\n"
9092                "    .aaaaaaaaaaaaaaa();");
9093   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
9094                "    .aaaaaaaaaaaaaaa()\n"
9095                "    .aaaaaaaaaaaaaaa()\n"
9096                "    .aaaaaaaaaaaaaaa();");
9097   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
9098                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
9099                "    .aaaaaaaaaaaaaaa();");
9100   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
9101                "    ->aaaaaaaaaaaaaae(0)\n"
9102                "    ->aaaaaaaaaaaaaaa();");
9103 
9104   // Don't linewrap after very short segments.
9105   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9106                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9107                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9108   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9109                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9110                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9111   verifyFormat("aaa()\n"
9112                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9113                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9114                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9115 
9116   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
9117                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9118                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
9119   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
9120                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
9121                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
9122 
9123   // Prefer not to break after empty parentheses.
9124   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
9125                "    First->LastNewlineOffset);");
9126 
9127   // Prefer not to create "hanging" indents.
9128   verifyFormat(
9129       "return !soooooooooooooome_map\n"
9130       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9131       "            .second;");
9132   verifyFormat(
9133       "return aaaaaaaaaaaaaaaa\n"
9134       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
9135       "    .aaaa(aaaaaaaaaaaaaa);");
9136   // No hanging indent here.
9137   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
9138                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9139   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
9140                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9141   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
9142                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9143                getLLVMStyleWithColumns(60));
9144   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
9145                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
9146                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9147                getLLVMStyleWithColumns(59));
9148   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9149                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9150                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9151 
9152   // Dont break if only closing statements before member call
9153   verifyFormat("test() {\n"
9154                "  ([]() -> {\n"
9155                "    int b = 32;\n"
9156                "    return 3;\n"
9157                "  }).foo();\n"
9158                "}");
9159   verifyFormat("test() {\n"
9160                "  (\n"
9161                "      []() -> {\n"
9162                "        int b = 32;\n"
9163                "        return 3;\n"
9164                "      },\n"
9165                "      foo, bar)\n"
9166                "      .foo();\n"
9167                "}");
9168   verifyFormat("test() {\n"
9169                "  ([]() -> {\n"
9170                "    int b = 32;\n"
9171                "    return 3;\n"
9172                "  })\n"
9173                "      .foo()\n"
9174                "      .bar();\n"
9175                "}");
9176   verifyFormat("test() {\n"
9177                "  ([]() -> {\n"
9178                "    int b = 32;\n"
9179                "    return 3;\n"
9180                "  })\n"
9181                "      .foo(\"aaaaaaaaaaaaaaaaa\"\n"
9182                "           \"bbbb\");\n"
9183                "}",
9184                getLLVMStyleWithColumns(30));
9185 }
9186 
9187 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
9188   verifyFormat(
9189       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
9190       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
9191   verifyFormat(
9192       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
9193       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
9194 
9195   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
9196                "    ccccccccccccccccccccccccc) {\n}");
9197   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
9198                "    ccccccccccccccccccccccccc) {\n}");
9199 
9200   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
9201                "    ccccccccccccccccccccccccc) {\n}");
9202   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
9203                "    ccccccccccccccccccccccccc) {\n}");
9204 
9205   verifyFormat(
9206       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
9207       "    ccccccccccccccccccccccccc) {\n}");
9208   verifyFormat(
9209       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
9210       "    ccccccccccccccccccccccccc) {\n}");
9211 
9212   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
9213                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
9214                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
9215                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
9216   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
9217                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
9218                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
9219                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
9220 
9221   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
9222                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
9223                "    aaaaaaaaaaaaaaa != aa) {\n}");
9224   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
9225                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
9226                "    aaaaaaaaaaaaaaa != aa) {\n}");
9227 }
9228 
9229 TEST_F(FormatTest, BreaksAfterAssignments) {
9230   verifyFormat(
9231       "unsigned Cost =\n"
9232       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
9233       "                        SI->getPointerAddressSpaceee());");
9234   verifyFormat(
9235       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
9236       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
9237 
9238   verifyFormat(
9239       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
9240       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
9241   verifyFormat("unsigned OriginalStartColumn =\n"
9242                "    SourceMgr.getSpellingColumnNumber(\n"
9243                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
9244                "    1;");
9245 }
9246 
9247 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
9248   FormatStyle Style = getLLVMStyle();
9249   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9250                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
9251                Style);
9252 
9253   Style.PenaltyBreakAssignment = 20;
9254   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
9255                "                                 cccccccccccccccccccccccccc;",
9256                Style);
9257 }
9258 
9259 TEST_F(FormatTest, AlignsAfterAssignments) {
9260   verifyFormat(
9261       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9262       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
9263   verifyFormat(
9264       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9265       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
9266   verifyFormat(
9267       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9268       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
9269   verifyFormat(
9270       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9271       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
9272   verifyFormat(
9273       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
9274       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
9275       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
9276 }
9277 
9278 TEST_F(FormatTest, AlignsAfterReturn) {
9279   verifyFormat(
9280       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9281       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
9282   verifyFormat(
9283       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9284       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
9285   verifyFormat(
9286       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
9287       "       aaaaaaaaaaaaaaaaaaaaaa();");
9288   verifyFormat(
9289       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
9290       "        aaaaaaaaaaaaaaaaaaaaaa());");
9291   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9292                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9293   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9294                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
9295                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9296   verifyFormat("return\n"
9297                "    // true if code is one of a or b.\n"
9298                "    code == a || code == b;");
9299 }
9300 
9301 TEST_F(FormatTest, AlignsAfterOpenBracket) {
9302   verifyFormat(
9303       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
9304       "                                                aaaaaaaaa aaaaaaa) {}");
9305   verifyFormat(
9306       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
9307       "                                               aaaaaaaaaaa aaaaaaaaa);");
9308   verifyFormat(
9309       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
9310       "                                             aaaaaaaaaaaaaaaaaaaaa));");
9311   FormatStyle Style = getLLVMStyle();
9312   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9313   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9314                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
9315                Style);
9316   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9317                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
9318                Style);
9319   verifyFormat("SomeLongVariableName->someFunction(\n"
9320                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
9321                Style);
9322   verifyFormat(
9323       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
9324       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
9325       Style);
9326   verifyFormat(
9327       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
9328       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9329       Style);
9330   verifyFormat(
9331       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
9332       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
9333       Style);
9334 
9335   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
9336                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
9337                "        b));",
9338                Style);
9339 
9340   Style.ColumnLimit = 30;
9341   verifyFormat("for (int foo = 0; foo < FOO;\n"
9342                "    ++foo) {\n"
9343                "  bar(foo);\n"
9344                "}",
9345                Style);
9346   Style.ColumnLimit = 80;
9347 
9348   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9349   Style.BinPackArguments = false;
9350   Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
9351   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9352                "    aaaaaaaaaaa aaaaaaaa,\n"
9353                "    aaaaaaaaa aaaaaaa,\n"
9354                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
9355                Style);
9356   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9357                "    aaaaaaaaaaa aaaaaaaaa,\n"
9358                "    aaaaaaaaaaa aaaaaaaaa,\n"
9359                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9360                Style);
9361   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
9362                "    aaaaaaaaaaaaaaa,\n"
9363                "    aaaaaaaaaaaaaaaaaaaaa,\n"
9364                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
9365                Style);
9366   verifyFormat(
9367       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
9368       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
9369       Style);
9370   verifyFormat(
9371       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
9372       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
9373       Style);
9374   verifyFormat(
9375       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9376       "    aaaaaaaaaaaaaaaaaaaaa(\n"
9377       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
9378       "    aaaaaaaaaaaaaaaa);",
9379       Style);
9380   verifyFormat(
9381       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9382       "    aaaaaaaaaaaaaaaaaaaaa(\n"
9383       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
9384       "    aaaaaaaaaaaaaaaa);",
9385       Style);
9386   verifyFormat(
9387       "fooooooooooo(new BARRRRRRRRR(\n"
9388       "    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
9389       Style);
9390   verifyFormat(
9391       "fooooooooooo(::new BARRRRRRRRR(\n"
9392       "    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
9393       Style);
9394   verifyFormat(
9395       "fooooooooooo(new FOO::BARRRR(\n"
9396       "    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
9397       Style);
9398 
9399   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
9400   Style.BinPackArguments = false;
9401   Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
9402   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9403                "    aaaaaaaaaaa aaaaaaaa,\n"
9404                "    aaaaaaaaa aaaaaaa,\n"
9405                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9406                ") {}",
9407                Style);
9408   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9409                "    aaaaaaaaaaa aaaaaaaaa,\n"
9410                "    aaaaaaaaaaa aaaaaaaaa,\n"
9411                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9412                ");",
9413                Style);
9414   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
9415                "    aaaaaaaaaaaaaaa,\n"
9416                "    aaaaaaaaaaaaaaaaaaaaa,\n"
9417                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9418                "));",
9419                Style);
9420   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
9421                "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9422                "));",
9423                Style);
9424   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
9425                "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9426                "));",
9427                Style);
9428   verifyFormat(
9429       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9430       "    aaaaaaaaaaaaaaaaaaaaa(\n"
9431       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9432       "    ),\n"
9433       "    aaaaaaaaaaaaaaaa\n"
9434       ");",
9435       Style);
9436   verifyFormat(
9437       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9438       "    aaaaaaaaaaaaaaaaaaaaa(\n"
9439       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9440       "    ) &&\n"
9441       "    aaaaaaaaaaaaaaaa\n"
9442       ");",
9443       Style);
9444   verifyFormat("aaaaaaa<bbbbbbbb> const aaaaaaaaaa{\n"
9445                "    aaaaaaaaaaaaa(aaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9446                "};",
9447                Style);
9448 
9449   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9450                "    const bool &aaaaaaaaa, const void *aaaaaaaaaa\n"
9451                ") const {\n"
9452                "  return true;\n"
9453                "}",
9454                Style);
9455   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9456                "    const bool &aaaaaaaaaa, const void *aaaaaaaaaa\n"
9457                ") const;",
9458                Style);
9459   verifyFormat("void aaaaaaaaa(\n"
9460                "    int aaaaaa, int bbbbbb, int cccccc, int dddddddddd\n"
9461                ") const noexcept -> std::vector<of_very_long_type>;",
9462                Style);
9463   verifyFormat(
9464       "x = aaaaaaaaaaaaaaa(\n"
9465       "    \"a aaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaa\"\n"
9466       ");",
9467       Style);
9468   Style.ColumnLimit = 60;
9469   verifyFormat("auto lambda =\n"
9470                "    [&b](\n"
9471                "        auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9472                "    ) {};",
9473                Style);
9474 }
9475 
9476 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
9477   FormatStyle Style = getLLVMStyleWithColumns(40);
9478   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9479                "          bbbbbbbbbbbbbbbbbbbbbb);",
9480                Style);
9481   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
9482   Style.AlignOperands = FormatStyle::OAS_DontAlign;
9483   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9484                "          bbbbbbbbbbbbbbbbbbbbbb);",
9485                Style);
9486   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9487   Style.AlignOperands = FormatStyle::OAS_Align;
9488   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9489                "          bbbbbbbbbbbbbbbbbbbbbb);",
9490                Style);
9491   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9492   Style.AlignOperands = FormatStyle::OAS_DontAlign;
9493   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9494                "    bbbbbbbbbbbbbbbbbbbbbb);",
9495                Style);
9496 }
9497 
9498 TEST_F(FormatTest, BreaksConditionalExpressions) {
9499   verifyFormat(
9500       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9501       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9502       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9503   verifyFormat(
9504       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
9505       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9506       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9507   verifyFormat(
9508       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9509       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9510   verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
9511                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9512                "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9513   verifyFormat(
9514       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
9515       "                                                    : aaaaaaaaaaaaa);");
9516   verifyFormat(
9517       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9518       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9519       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9520       "                   aaaaaaaaaaaaa);");
9521   verifyFormat(
9522       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9523       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9524       "                   aaaaaaaaaaaaa);");
9525   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9526                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9527                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9528                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9529                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9530   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9531                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9532                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9533                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9534                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9535                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9536                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9537   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9538                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9539                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9540                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9541                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9542   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9543                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9544                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9545   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
9546                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9547                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9548                "        : aaaaaaaaaaaaaaaa;");
9549   verifyFormat(
9550       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9551       "    ? aaaaaaaaaaaaaaa\n"
9552       "    : aaaaaaaaaaaaaaa;");
9553   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
9554                "          aaaaaaaaa\n"
9555                "      ? b\n"
9556                "      : c);");
9557   verifyFormat("return aaaa == bbbb\n"
9558                "           // comment\n"
9559                "           ? aaaa\n"
9560                "           : bbbb;");
9561   verifyFormat("unsigned Indent =\n"
9562                "    format(TheLine.First,\n"
9563                "           IndentForLevel[TheLine.Level] >= 0\n"
9564                "               ? IndentForLevel[TheLine.Level]\n"
9565                "               : TheLine * 2,\n"
9566                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
9567                getLLVMStyleWithColumns(60));
9568   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
9569                "                  ? aaaaaaaaaaaaaaa\n"
9570                "                  : bbbbbbbbbbbbbbb //\n"
9571                "                        ? ccccccccccccccc\n"
9572                "                        : ddddddddddddddd;");
9573   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
9574                "                  ? aaaaaaaaaaaaaaa\n"
9575                "                  : (bbbbbbbbbbbbbbb //\n"
9576                "                         ? ccccccccccccccc\n"
9577                "                         : ddddddddddddddd);");
9578   verifyFormat(
9579       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9580       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9581       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
9582       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
9583       "                                      : aaaaaaaaaa;");
9584   verifyFormat(
9585       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9586       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
9587       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9588 
9589   FormatStyle NoBinPacking = getLLVMStyle();
9590   NoBinPacking.BinPackArguments = false;
9591   verifyFormat(
9592       "void f() {\n"
9593       "  g(aaa,\n"
9594       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
9595       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9596       "        ? aaaaaaaaaaaaaaa\n"
9597       "        : aaaaaaaaaaaaaaa);\n"
9598       "}",
9599       NoBinPacking);
9600   verifyFormat(
9601       "void f() {\n"
9602       "  g(aaa,\n"
9603       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
9604       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9605       "        ?: aaaaaaaaaaaaaaa);\n"
9606       "}",
9607       NoBinPacking);
9608 
9609   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
9610                "             // comment.\n"
9611                "             ccccccccccccccccccccccccccccccccccccccc\n"
9612                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9613                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
9614 
9615   // Assignments in conditional expressions. Apparently not uncommon :-(.
9616   verifyFormat("return a != b\n"
9617                "           // comment\n"
9618                "           ? a = b\n"
9619                "           : a = b;");
9620   verifyFormat("return a != b\n"
9621                "           // comment\n"
9622                "           ? a = a != b\n"
9623                "                     // comment\n"
9624                "                     ? a = b\n"
9625                "                     : a\n"
9626                "           : a;");
9627   verifyFormat("return a != b\n"
9628                "           // comment\n"
9629                "           ? a\n"
9630                "           : a = a != b\n"
9631                "                     // comment\n"
9632                "                     ? a = b\n"
9633                "                     : a;");
9634 
9635   // Chained conditionals
9636   FormatStyle Style = getLLVMStyleWithColumns(70);
9637   Style.AlignOperands = FormatStyle::OAS_Align;
9638   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9639                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
9640                "                        : 3333333333333333;",
9641                Style);
9642   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9643                "       : bbbbbbbbbb     ? 2222222222222222\n"
9644                "                        : 3333333333333333;",
9645                Style);
9646   verifyFormat("return aaaaaaaaaa         ? 1111111111111111\n"
9647                "       : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
9648                "                          : 3333333333333333;",
9649                Style);
9650   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9651                "       : bbbbbbbbbbbbbb ? 222222\n"
9652                "                        : 333333;",
9653                Style);
9654   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9655                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
9656                "       : cccccccccccccc ? 3333333333333333\n"
9657                "                        : 4444444444444444;",
9658                Style);
9659   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
9660                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
9661                "                        : 3333333333333333;",
9662                Style);
9663   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9664                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
9665                "                        : (aaa ? bbb : ccc);",
9666                Style);
9667   verifyFormat(
9668       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9669       "                                             : cccccccccccccccccc)\n"
9670       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
9671       "                        : 3333333333333333;",
9672       Style);
9673   verifyFormat(
9674       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9675       "                                             : cccccccccccccccccc)\n"
9676       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
9677       "                        : 3333333333333333;",
9678       Style);
9679   verifyFormat(
9680       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9681       "                                             : dddddddddddddddddd)\n"
9682       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
9683       "                        : 3333333333333333;",
9684       Style);
9685   verifyFormat(
9686       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9687       "                                             : dddddddddddddddddd)\n"
9688       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
9689       "                        : 3333333333333333;",
9690       Style);
9691   verifyFormat(
9692       "return aaaaaaaaa        ? 1111111111111111\n"
9693       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
9694       "                        : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9695       "                                             : dddddddddddddddddd)",
9696       Style);
9697   verifyFormat(
9698       "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9699       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
9700       "                        : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9701       "                                             : cccccccccccccccccc);",
9702       Style);
9703   verifyFormat(
9704       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9705       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
9706       "                                             : eeeeeeeeeeeeeeeeee)\n"
9707       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
9708       "                        : 3333333333333333;",
9709       Style);
9710   verifyFormat(
9711       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
9712       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
9713       "                                             : eeeeeeeeeeeeeeeeee)\n"
9714       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
9715       "                        : 3333333333333333;",
9716       Style);
9717   verifyFormat(
9718       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9719       "                           : cccccccccccc    ? dddddddddddddddddd\n"
9720       "                                             : eeeeeeeeeeeeeeeeee)\n"
9721       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
9722       "                        : 3333333333333333;",
9723       Style);
9724   verifyFormat(
9725       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9726       "                                             : cccccccccccccccccc\n"
9727       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
9728       "                        : 3333333333333333;",
9729       Style);
9730   verifyFormat(
9731       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9732       "                          : cccccccccccccccc ? dddddddddddddddddd\n"
9733       "                                             : eeeeeeeeeeeeeeeeee\n"
9734       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
9735       "                        : 3333333333333333;",
9736       Style);
9737   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
9738                "           ? (aaaaaaaaaaaaaaaaaa   ? bbbbbbbbbbbbbbbbbb\n"
9739                "              : cccccccccccccccccc ? dddddddddddddddddd\n"
9740                "                                   : eeeeeeeeeeeeeeeeee)\n"
9741                "       : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
9742                "                             : 3333333333333333;",
9743                Style);
9744   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
9745                "           ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9746                "             : cccccccccccccccc ? dddddddddddddddddd\n"
9747                "                                : eeeeeeeeeeeeeeeeee\n"
9748                "       : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
9749                "                                 : 3333333333333333;",
9750                Style);
9751 
9752   Style.AlignOperands = FormatStyle::OAS_DontAlign;
9753   Style.BreakBeforeTernaryOperators = false;
9754   // FIXME: Aligning the question marks is weird given DontAlign.
9755   // Consider disabling this alignment in this case. Also check whether this
9756   // will render the adjustment from https://reviews.llvm.org/D82199
9757   // unnecessary.
9758   verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
9759                "    bbbb                ? cccccccccccccccccc :\n"
9760                "                          ddddd;",
9761                Style);
9762 
9763   verifyFormat(
9764       "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
9765       "    /*\n"
9766       "     */\n"
9767       "    function() {\n"
9768       "      try {\n"
9769       "        return JJJJJJJJJJJJJJ(\n"
9770       "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
9771       "      }\n"
9772       "    } :\n"
9773       "    function() {};",
9774       "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
9775       "     /*\n"
9776       "      */\n"
9777       "     function() {\n"
9778       "      try {\n"
9779       "        return JJJJJJJJJJJJJJ(\n"
9780       "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
9781       "      }\n"
9782       "    } :\n"
9783       "    function() {};",
9784       getGoogleStyle(FormatStyle::LK_JavaScript));
9785 }
9786 
9787 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
9788   FormatStyle Style = getLLVMStyleWithColumns(70);
9789   Style.BreakBeforeTernaryOperators = false;
9790   verifyFormat(
9791       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9792       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9793       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9794       Style);
9795   verifyFormat(
9796       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
9797       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9798       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9799       Style);
9800   verifyFormat(
9801       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9802       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9803       Style);
9804   verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
9805                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9806                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9807                Style);
9808   verifyFormat(
9809       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
9810       "                                                      aaaaaaaaaaaaa);",
9811       Style);
9812   verifyFormat(
9813       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9814       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9815       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9816       "                   aaaaaaaaaaaaa);",
9817       Style);
9818   verifyFormat(
9819       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9820       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9821       "                   aaaaaaaaaaaaa);",
9822       Style);
9823   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9824                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9825                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
9826                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9827                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9828                Style);
9829   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9830                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9831                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9832                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
9833                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9834                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9835                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9836                Style);
9837   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9838                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
9839                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9840                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9841                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9842                Style);
9843   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9844                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9845                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9846                Style);
9847   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
9848                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9849                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9850                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9851                Style);
9852   verifyFormat(
9853       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9854       "    aaaaaaaaaaaaaaa :\n"
9855       "    aaaaaaaaaaaaaaa;",
9856       Style);
9857   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
9858                "          aaaaaaaaa ?\n"
9859                "      b :\n"
9860                "      c);",
9861                Style);
9862   verifyFormat("unsigned Indent =\n"
9863                "    format(TheLine.First,\n"
9864                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
9865                "               IndentForLevel[TheLine.Level] :\n"
9866                "               TheLine * 2,\n"
9867                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
9868                Style);
9869   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
9870                "                  aaaaaaaaaaaaaaa :\n"
9871                "                  bbbbbbbbbbbbbbb ? //\n"
9872                "                      ccccccccccccccc :\n"
9873                "                      ddddddddddddddd;",
9874                Style);
9875   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
9876                "                  aaaaaaaaaaaaaaa :\n"
9877                "                  (bbbbbbbbbbbbbbb ? //\n"
9878                "                       ccccccccccccccc :\n"
9879                "                       ddddddddddddddd);",
9880                Style);
9881   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9882                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
9883                "            ccccccccccccccccccccccccccc;",
9884                Style);
9885   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9886                "           aaaaa :\n"
9887                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
9888                Style);
9889 
9890   // Chained conditionals
9891   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9892                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9893                "                          3333333333333333;",
9894                Style);
9895   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9896                "       bbbbbbbbbb       ? 2222222222222222 :\n"
9897                "                          3333333333333333;",
9898                Style);
9899   verifyFormat("return aaaaaaaaaa       ? 1111111111111111 :\n"
9900                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9901                "                          3333333333333333;",
9902                Style);
9903   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9904                "       bbbbbbbbbbbbbbbb ? 222222 :\n"
9905                "                          333333;",
9906                Style);
9907   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9908                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9909                "       cccccccccccccccc ? 3333333333333333 :\n"
9910                "                          4444444444444444;",
9911                Style);
9912   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
9913                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9914                "                          3333333333333333;",
9915                Style);
9916   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9917                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9918                "                          (aaa ? bbb : ccc);",
9919                Style);
9920   verifyFormat(
9921       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9922       "                                               cccccccccccccccccc) :\n"
9923       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9924       "                          3333333333333333;",
9925       Style);
9926   verifyFormat(
9927       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9928       "                                               cccccccccccccccccc) :\n"
9929       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9930       "                          3333333333333333;",
9931       Style);
9932   verifyFormat(
9933       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9934       "                                               dddddddddddddddddd) :\n"
9935       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9936       "                          3333333333333333;",
9937       Style);
9938   verifyFormat(
9939       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9940       "                                               dddddddddddddddddd) :\n"
9941       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9942       "                          3333333333333333;",
9943       Style);
9944   verifyFormat(
9945       "return aaaaaaaaa        ? 1111111111111111 :\n"
9946       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9947       "                          a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9948       "                                               dddddddddddddddddd)",
9949       Style);
9950   verifyFormat(
9951       "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9952       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9953       "                          (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9954       "                                               cccccccccccccccccc);",
9955       Style);
9956   verifyFormat(
9957       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9958       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
9959       "                                               eeeeeeeeeeeeeeeeee) :\n"
9960       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9961       "                          3333333333333333;",
9962       Style);
9963   verifyFormat(
9964       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9965       "                           ccccccccccccc     ? dddddddddddddddddd :\n"
9966       "                                               eeeeeeeeeeeeeeeeee) :\n"
9967       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9968       "                          3333333333333333;",
9969       Style);
9970   verifyFormat(
9971       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa     ? bbbbbbbbbbbbbbbbbb :\n"
9972       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
9973       "                                               eeeeeeeeeeeeeeeeee) :\n"
9974       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9975       "                          3333333333333333;",
9976       Style);
9977   verifyFormat(
9978       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9979       "                                               cccccccccccccccccc :\n"
9980       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9981       "                          3333333333333333;",
9982       Style);
9983   verifyFormat(
9984       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9985       "                          cccccccccccccccccc ? dddddddddddddddddd :\n"
9986       "                                               eeeeeeeeeeeeeeeeee :\n"
9987       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9988       "                          3333333333333333;",
9989       Style);
9990   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
9991                "           (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9992                "            cccccccccccccccccc ? dddddddddddddddddd :\n"
9993                "                                 eeeeeeeeeeeeeeeeee) :\n"
9994                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9995                "                               3333333333333333;",
9996                Style);
9997   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
9998                "           aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9999                "           cccccccccccccccccccc ? dddddddddddddddddd :\n"
10000                "                                  eeeeeeeeeeeeeeeeee :\n"
10001                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10002                "                               3333333333333333;",
10003                Style);
10004 }
10005 
10006 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
10007   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
10008                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
10009   verifyFormat("bool a = true, b = false;");
10010 
10011   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10012                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
10013                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
10014                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
10015   verifyFormat(
10016       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
10017       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
10018       "     d = e && f;");
10019   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
10020                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
10021   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
10022                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
10023   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
10024                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
10025 
10026   FormatStyle Style = getGoogleStyle();
10027   Style.PointerAlignment = FormatStyle::PAS_Left;
10028   Style.DerivePointerAlignment = false;
10029   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10030                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
10031                "    *b = bbbbbbbbbbbbbbbbbbb;",
10032                Style);
10033   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
10034                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
10035                Style);
10036   verifyFormat("vector<int*> a, b;", Style);
10037   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
10038   verifyFormat("/*comment*/ for (int *p, *q; p != q; p = p->next) {\n}", Style);
10039   verifyFormat("if (int *p, *q; p != q) {\n  p = p->next;\n}", Style);
10040   verifyFormat("/*comment*/ if (int *p, *q; p != q) {\n  p = p->next;\n}",
10041                Style);
10042   verifyFormat("switch (int *p, *q; p != q) {\n  default:\n    break;\n}",
10043                Style);
10044   verifyFormat(
10045       "/*comment*/ switch (int *p, *q; p != q) {\n  default:\n    break;\n}",
10046       Style);
10047 
10048   verifyFormat("if ([](int* p, int* q) {}()) {\n}", Style);
10049   verifyFormat("for ([](int* p, int* q) {}();;) {\n}", Style);
10050   verifyFormat("for (; [](int* p, int* q) {}();) {\n}", Style);
10051   verifyFormat("for (;; [](int* p, int* q) {}()) {\n}", Style);
10052   verifyFormat("switch ([](int* p, int* q) {}()) {\n  default:\n    break;\n}",
10053                Style);
10054 }
10055 
10056 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
10057   verifyFormat("arr[foo ? bar : baz];");
10058   verifyFormat("f()[foo ? bar : baz];");
10059   verifyFormat("(a + b)[foo ? bar : baz];");
10060   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
10061 }
10062 
10063 TEST_F(FormatTest, AlignsStringLiterals) {
10064   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
10065                "                                      \"short literal\");");
10066   verifyFormat(
10067       "looooooooooooooooooooooooongFunction(\n"
10068       "    \"short literal\"\n"
10069       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
10070   verifyFormat("someFunction(\"Always break between multi-line\"\n"
10071                "             \" string literals\",\n"
10072                "             also, other, parameters);");
10073   verifyFormat("fun + \"1243\" /* comment */\n"
10074                "      \"5678\";",
10075                "fun + \"1243\" /* comment */\n"
10076                "    \"5678\";",
10077                getLLVMStyleWithColumns(28));
10078   verifyFormat(
10079       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
10080       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
10081       "         \"aaaaaaaaaaaaaaaa\";",
10082       "aaaaaa ="
10083       "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
10084       "aaaaaaaaaaaaaaaaaaaaa\" "
10085       "\"aaaaaaaaaaaaaaaa\";");
10086   verifyFormat("a = a + \"a\"\n"
10087                "        \"a\"\n"
10088                "        \"a\";");
10089   verifyFormat("f(\"a\", \"b\"\n"
10090                "       \"c\");");
10091 
10092   verifyFormat(
10093       "#define LL_FORMAT \"ll\"\n"
10094       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
10095       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
10096 
10097   verifyFormat("#define A(X)          \\\n"
10098                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
10099                "  \"ccccc\"",
10100                getLLVMStyleWithColumns(23));
10101   verifyFormat("#define A \"def\"\n"
10102                "f(\"abc\" A \"ghi\"\n"
10103                "  \"jkl\");");
10104 
10105   verifyFormat("f(L\"a\"\n"
10106                "  L\"b\");");
10107   verifyFormat("#define A(X)            \\\n"
10108                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
10109                "  L\"ccccc\"",
10110                getLLVMStyleWithColumns(25));
10111 
10112   verifyFormat("f(@\"a\"\n"
10113                "  @\"b\");");
10114   verifyFormat("NSString s = @\"a\"\n"
10115                "             @\"b\"\n"
10116                "             @\"c\";");
10117   verifyFormat("NSString s = @\"a\"\n"
10118                "              \"b\"\n"
10119                "              \"c\";");
10120 }
10121 
10122 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
10123   FormatStyle Style = getLLVMStyle();
10124   Style.ColumnLimit = 60;
10125 
10126   // No declarations or definitions should be moved to own line.
10127   Style.BreakAfterReturnType = FormatStyle::RTBS_None;
10128   verifyFormat("class A {\n"
10129                "  int f() { return 1; }\n"
10130                "  int g();\n"
10131                "  long\n"
10132                "  foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
10133                "};\n"
10134                "int f() { return 1; }\n"
10135                "int g();\n"
10136                "int foooooooooooooooooooooooooooo::\n"
10137                "    baaaaaaaaaaaaaaaaaaaaar();",
10138                Style);
10139 
10140   // It is now allowed to break after a short return type if necessary.
10141   Style.BreakAfterReturnType = FormatStyle::RTBS_Automatic;
10142   verifyFormat("class A {\n"
10143                "  int f() { return 1; }\n"
10144                "  int g();\n"
10145                "  long\n"
10146                "  foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
10147                "};\n"
10148                "int f() { return 1; }\n"
10149                "int g();\n"
10150                "int\n"
10151                "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
10152                Style);
10153 
10154   // It now must never break after a short return type.
10155   Style.BreakAfterReturnType = FormatStyle::RTBS_ExceptShortType;
10156   verifyFormat("class A {\n"
10157                "  int f() { return 1; }\n"
10158                "  int g();\n"
10159                "  long foooooooooooooooooooooooooooo::\n"
10160                "      baaaaaaaaaaaaaaaaaaaar();\n"
10161                "};\n"
10162                "int f() { return 1; }\n"
10163                "int g();\n"
10164                "int foooooooooooooooooooooooooooo::\n"
10165                "    baaaaaaaaaaaaaaaaaaaaar();",
10166                Style);
10167 
10168   // All declarations and definitions should have the return type moved to its
10169   // own line.
10170   Style.BreakAfterReturnType = FormatStyle::RTBS_All;
10171   Style.TypenameMacros = {"LIST"};
10172   verifyFormat("SomeType\n"
10173                "funcdecl(LIST(uint64_t));",
10174                Style);
10175   verifyFormat("class E {\n"
10176                "  int\n"
10177                "  f() {\n"
10178                "    return 1;\n"
10179                "  }\n"
10180                "  int\n"
10181                "  g();\n"
10182                "  long\n"
10183                "  foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
10184                "};\n"
10185                "int\n"
10186                "f() {\n"
10187                "  return 1;\n"
10188                "}\n"
10189                "int\n"
10190                "g();\n"
10191                "int\n"
10192                "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
10193                Style);
10194 
10195   // Top-level definitions, and no kinds of declarations should have the
10196   // return type moved to its own line.
10197   Style.BreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
10198   verifyFormat("class B {\n"
10199                "  int f() { return 1; }\n"
10200                "  int g();\n"
10201                "};\n"
10202                "int\n"
10203                "f() {\n"
10204                "  return 1;\n"
10205                "}\n"
10206                "int g();",
10207                Style);
10208 
10209   // Top-level definitions and declarations should have the return type moved
10210   // to its own line.
10211   Style.BreakAfterReturnType = FormatStyle::RTBS_TopLevel;
10212   verifyFormat("class C {\n"
10213                "  int f() { return 1; }\n"
10214                "  int g();\n"
10215                "};\n"
10216                "int\n"
10217                "f() {\n"
10218                "  return 1;\n"
10219                "}\n"
10220                "int\n"
10221                "g();\n"
10222                "int\n"
10223                "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
10224                Style);
10225 
10226   // All definitions should have the return type moved to its own line, but no
10227   // kinds of declarations.
10228   Style.BreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
10229   verifyFormat("class D {\n"
10230                "  int\n"
10231                "  f() {\n"
10232                "    return 1;\n"
10233                "  }\n"
10234                "  int g();\n"
10235                "};\n"
10236                "int\n"
10237                "f() {\n"
10238                "  return 1;\n"
10239                "}\n"
10240                "int g();",
10241                Style);
10242   verifyFormat("const char *\n"
10243                "f(void) {\n" // Break here.
10244                "  return \"\";\n"
10245                "}\n"
10246                "const char *bar(void);", // No break here.
10247                Style);
10248   verifyFormat("template <class T>\n"
10249                "T *\n"
10250                "f(T &c) {\n" // Break here.
10251                "  return NULL;\n"
10252                "}\n"
10253                "template <class T> T *f(T &c);", // No break here.
10254                Style);
10255   verifyFormat("class C {\n"
10256                "  int\n"
10257                "  operator+() {\n"
10258                "    return 1;\n"
10259                "  }\n"
10260                "  int\n"
10261                "  operator()() {\n"
10262                "    return 1;\n"
10263                "  }\n"
10264                "};",
10265                Style);
10266   verifyFormat("void\n"
10267                "A::operator()() {}\n"
10268                "void\n"
10269                "A::operator>>() {}\n"
10270                "void\n"
10271                "A::operator+() {}\n"
10272                "void\n"
10273                "A::operator*() {}\n"
10274                "void\n"
10275                "A::operator->() {}\n"
10276                "void\n"
10277                "A::operator void *() {}\n"
10278                "void\n"
10279                "A::operator void &() {}\n"
10280                "void\n"
10281                "A::operator void &&() {}\n"
10282                "void\n"
10283                "A::operator char *() {}\n"
10284                "void\n"
10285                "A::operator[]() {}\n"
10286                "void\n"
10287                "A::operator!() {}\n"
10288                "void\n"
10289                "A::operator**() {}\n"
10290                "void\n"
10291                "A::operator<Foo> *() {}\n"
10292                "void\n"
10293                "A::operator<Foo> **() {}\n"
10294                "void\n"
10295                "A::operator<Foo> &() {}\n"
10296                "void\n"
10297                "A::operator void **() {}",
10298                Style);
10299   verifyFormat("constexpr auto\n"
10300                "operator()() const -> reference {}\n"
10301                "constexpr auto\n"
10302                "operator>>() const -> reference {}\n"
10303                "constexpr auto\n"
10304                "operator+() const -> reference {}\n"
10305                "constexpr auto\n"
10306                "operator*() const -> reference {}\n"
10307                "constexpr auto\n"
10308                "operator->() const -> reference {}\n"
10309                "constexpr auto\n"
10310                "operator++() const -> reference {}\n"
10311                "constexpr auto\n"
10312                "operator void *() const -> reference {}\n"
10313                "constexpr auto\n"
10314                "operator void **() const -> reference {}\n"
10315                "constexpr auto\n"
10316                "operator void *() const -> reference {}\n"
10317                "constexpr auto\n"
10318                "operator void &() const -> reference {}\n"
10319                "constexpr auto\n"
10320                "operator void &&() const -> reference {}\n"
10321                "constexpr auto\n"
10322                "operator char *() const -> reference {}\n"
10323                "constexpr auto\n"
10324                "operator!() const -> reference {}\n"
10325                "constexpr auto\n"
10326                "operator[]() const -> reference {}",
10327                Style);
10328   verifyFormat("void *operator new(std::size_t s);", // No break here.
10329                Style);
10330   verifyFormat("void *\n"
10331                "operator new(std::size_t s) {}",
10332                Style);
10333   verifyFormat("void *\n"
10334                "operator delete[](void *ptr) {}",
10335                Style);
10336   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
10337   verifyFormat("const char *\n"
10338                "f(void)\n" // Break here.
10339                "{\n"
10340                "  return \"\";\n"
10341                "}\n"
10342                "const char *bar(void);", // No break here.
10343                Style);
10344   verifyFormat("template <class T>\n"
10345                "T *\n"     // Problem here: no line break
10346                "f(T &c)\n" // Break here.
10347                "{\n"
10348                "  return NULL;\n"
10349                "}\n"
10350                "template <class T> T *f(T &c);", // No break here.
10351                Style);
10352   verifyFormat("int\n"
10353                "foo(A<bool> a)\n"
10354                "{\n"
10355                "  return a;\n"
10356                "}",
10357                Style);
10358   verifyFormat("int\n"
10359                "foo(A<8> a)\n"
10360                "{\n"
10361                "  return a;\n"
10362                "}",
10363                Style);
10364   verifyFormat("int\n"
10365                "foo(A<B<bool>, 8> a)\n"
10366                "{\n"
10367                "  return a;\n"
10368                "}",
10369                Style);
10370   verifyFormat("int\n"
10371                "foo(A<B<8>, bool> a)\n"
10372                "{\n"
10373                "  return a;\n"
10374                "}",
10375                Style);
10376   verifyFormat("int\n"
10377                "foo(A<B<bool>, bool> a)\n"
10378                "{\n"
10379                "  return a;\n"
10380                "}",
10381                Style);
10382   verifyFormat("int\n"
10383                "foo(A<B<8>, 8> a)\n"
10384                "{\n"
10385                "  return a;\n"
10386                "}",
10387                Style);
10388 
10389   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
10390   Style.BraceWrapping.AfterFunction = true;
10391   verifyFormat("int f(i);\n" // No break here.
10392                "int\n"       // Break here.
10393                "f(i)\n"
10394                "{\n"
10395                "  return i + 1;\n"
10396                "}\n"
10397                "int\n" // Break here.
10398                "f(i)\n"
10399                "{\n"
10400                "  return i + 1;\n"
10401                "};",
10402                Style);
10403   verifyFormat("int f(a, b, c);\n" // No break here.
10404                "int\n"             // Break here.
10405                "f(a, b, c)\n"      // Break here.
10406                "short a, b;\n"
10407                "float c;\n"
10408                "{\n"
10409                "  return a + b < c;\n"
10410                "}\n"
10411                "int\n"        // Break here.
10412                "f(a, b, c)\n" // Break here.
10413                "short a, b;\n"
10414                "float c;\n"
10415                "{\n"
10416                "  return a + b < c;\n"
10417                "};",
10418                Style);
10419   verifyFormat("byte *\n" // Break here.
10420                "f(a)\n"   // Break here.
10421                "byte a[];\n"
10422                "{\n"
10423                "  return a;\n"
10424                "}",
10425                Style);
10426   verifyFormat("byte *\n"
10427                "f(a)\n"
10428                "byte /* K&R C */ a[];\n"
10429                "{\n"
10430                "  return a;\n"
10431                "}\n"
10432                "byte *\n"
10433                "g(p)\n"
10434                "byte /* K&R C */ *p;\n"
10435                "{\n"
10436                "  return p;\n"
10437                "}",
10438                Style);
10439   verifyFormat("bool f(int a, int) override;\n"
10440                "Bar g(int a, Bar) final;\n"
10441                "Bar h(a, Bar) final;",
10442                Style);
10443   verifyFormat("int\n"
10444                "f(a)",
10445                Style);
10446   verifyFormat("bool\n"
10447                "f(size_t = 0, bool b = false)\n"
10448                "{\n"
10449                "  return !b;\n"
10450                "}",
10451                Style);
10452 
10453   // The return breaking style doesn't affect:
10454   // * function and object definitions with attribute-like macros
10455   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10456                "    ABSL_GUARDED_BY(mutex) = {};",
10457                getGoogleStyleWithColumns(40));
10458   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10459                "    ABSL_GUARDED_BY(mutex);  // comment",
10460                getGoogleStyleWithColumns(40));
10461   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10462                "    ABSL_GUARDED_BY(mutex1)\n"
10463                "        ABSL_GUARDED_BY(mutex2);",
10464                getGoogleStyleWithColumns(40));
10465   verifyFormat("Tttttt f(int a, int b)\n"
10466                "    ABSL_GUARDED_BY(mutex1)\n"
10467                "        ABSL_GUARDED_BY(mutex2);",
10468                getGoogleStyleWithColumns(40));
10469   // * typedefs
10470   verifyGoogleFormat("typedef ATTR(X) char x;");
10471 
10472   Style = getGNUStyle();
10473 
10474   // Test for comments at the end of function declarations.
10475   verifyFormat("void\n"
10476                "foo (int a, /*abc*/ int b) // def\n"
10477                "{\n"
10478                "}",
10479                Style);
10480 
10481   verifyFormat("void\n"
10482                "foo (int a, /* abc */ int b) /* def */\n"
10483                "{\n"
10484                "}",
10485                Style);
10486 
10487   // Definitions that should not break after return type
10488   verifyFormat("void foo (int a, int b); // def", Style);
10489   verifyFormat("void foo (int a, int b); /* def */", Style);
10490   verifyFormat("void foo (int a, int b);", Style);
10491 }
10492 
10493 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
10494   FormatStyle NoBreak = getLLVMStyle();
10495   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
10496   FormatStyle Break = getLLVMStyle();
10497   Break.AlwaysBreakBeforeMultilineStrings = true;
10498   verifyFormat("aaaa = \"bbbb\"\n"
10499                "       \"cccc\";",
10500                NoBreak);
10501   verifyFormat("aaaa =\n"
10502                "    \"bbbb\"\n"
10503                "    \"cccc\";",
10504                Break);
10505   verifyFormat("aaaa(\"bbbb\"\n"
10506                "     \"cccc\");",
10507                NoBreak);
10508   verifyFormat("aaaa(\n"
10509                "    \"bbbb\"\n"
10510                "    \"cccc\");",
10511                Break);
10512   verifyFormat("aaaa(qqq, \"bbbb\"\n"
10513                "          \"cccc\");",
10514                NoBreak);
10515   verifyFormat("aaaa(qqq,\n"
10516                "     \"bbbb\"\n"
10517                "     \"cccc\");",
10518                Break);
10519   verifyFormat("aaaa(qqq,\n"
10520                "     L\"bbbb\"\n"
10521                "     L\"cccc\");",
10522                Break);
10523   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
10524                "                      \"bbbb\"));",
10525                Break);
10526   verifyFormat("string s = someFunction(\n"
10527                "    \"abc\"\n"
10528                "    \"abc\");",
10529                Break);
10530 
10531   // As we break before unary operators, breaking right after them is bad.
10532   verifyFormat("string foo = abc ? \"x\"\n"
10533                "                   \"blah blah blah blah blah blah\"\n"
10534                "                 : \"y\";",
10535                Break);
10536 
10537   // Don't break if there is no column gain.
10538   verifyFormat("f(\"aaaa\"\n"
10539                "  \"bbbb\");",
10540                Break);
10541 
10542   // Treat literals with escaped newlines like multi-line string literals.
10543   verifyNoChange("x = \"a\\\n"
10544                  "b\\\n"
10545                  "c\";",
10546                  NoBreak);
10547   verifyFormat("xxxx =\n"
10548                "    \"a\\\n"
10549                "b\\\n"
10550                "c\";",
10551                "xxxx = \"a\\\n"
10552                "b\\\n"
10553                "c\";",
10554                Break);
10555 
10556   verifyFormat("NSString *const kString =\n"
10557                "    @\"aaaa\"\n"
10558                "    @\"bbbb\";",
10559                "NSString *const kString = @\"aaaa\"\n"
10560                "@\"bbbb\";",
10561                Break);
10562 
10563   Break.ColumnLimit = 0;
10564   verifyFormat("const char *hello = \"hello llvm\";", Break);
10565 }
10566 
10567 TEST_F(FormatTest, AlignsPipes) {
10568   verifyFormat(
10569       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10570       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10571       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10572   verifyFormat(
10573       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
10574       "                     << aaaaaaaaaaaaaaaaaaaa;");
10575   verifyFormat(
10576       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10577       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10578   verifyFormat(
10579       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
10580       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10581   verifyFormat(
10582       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
10583       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
10584       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
10585   verifyFormat(
10586       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10587       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10588       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10589   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10590                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10591                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10592                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10593   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
10594                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
10595   verifyFormat(
10596       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10597       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10598   verifyFormat(
10599       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
10600       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
10601 
10602   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
10603                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
10604   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10605                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10606                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
10607                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
10608   verifyFormat("LOG_IF(aaa == //\n"
10609                "       bbb)\n"
10610                "    << a << b;");
10611 
10612   // But sometimes, breaking before the first "<<" is desirable.
10613   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10614                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
10615   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
10616                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10617                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10618   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
10619                "    << BEF << IsTemplate << Description << E->getType();");
10620   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10621                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10622                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10623   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10624                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10625                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10626                "    << aaa;");
10627 
10628   verifyFormat(
10629       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10630       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10631 
10632   // Incomplete string literal.
10633   verifyFormat("llvm::errs() << \"\n"
10634                "             << a;",
10635                "llvm::errs() << \"\n<<a;");
10636 
10637   verifyFormat("void f() {\n"
10638                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
10639                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
10640                "}");
10641 
10642   // Handle 'endl'.
10643   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
10644                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
10645   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
10646 
10647   // Handle '\n'.
10648   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
10649                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
10650   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
10651                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
10652   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
10653                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
10654   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
10655 }
10656 
10657 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
10658   verifyFormat("return out << \"somepacket = {\\n\"\n"
10659                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
10660                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
10661                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
10662                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
10663                "           << \"}\";");
10664 
10665   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
10666                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
10667                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
10668   verifyFormat(
10669       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
10670       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
10671       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
10672       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
10673       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
10674   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
10675                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10676   verifyFormat(
10677       "void f() {\n"
10678       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
10679       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
10680       "}");
10681 
10682   // Breaking before the first "<<" is generally not desirable.
10683   verifyFormat(
10684       "llvm::errs()\n"
10685       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10686       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10687       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10688       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
10689       getLLVMStyleWithColumns(70));
10690   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10691                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10692                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10693                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10694                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10695                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
10696                getLLVMStyleWithColumns(70));
10697 
10698   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
10699                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
10700                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
10701   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
10702                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
10703                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
10704   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
10705                "           (aaaa + aaaa);",
10706                getLLVMStyleWithColumns(40));
10707   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
10708                "                  (aaaaaaa + aaaaa));",
10709                getLLVMStyleWithColumns(40));
10710   verifyFormat(
10711       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
10712       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
10713       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
10714 }
10715 
10716 TEST_F(FormatTest, WrapBeforeInsertionOperatorbetweenStringLiterals) {
10717   verifyFormat("QStringList() << \"foo\" << \"bar\";");
10718 
10719   verifyNoChange("QStringList() << \"foo\"\n"
10720                  "              << \"bar\";");
10721 
10722   verifyFormat("log_error(log, \"foo\" << \"bar\");",
10723                "log_error(log, \"foo\"\n"
10724                "                   << \"bar\");");
10725 }
10726 
10727 TEST_F(FormatTest, UnderstandsEquals) {
10728   verifyFormat(
10729       "aaaaaaaaaaaaaaaaa =\n"
10730       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10731   verifyFormat(
10732       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10733       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
10734   verifyFormat(
10735       "if (a) {\n"
10736       "  f();\n"
10737       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10738       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
10739       "}");
10740 
10741   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10742                "        100000000 + 10000000) {\n}");
10743 }
10744 
10745 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
10746   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
10747                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
10748 
10749   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
10750                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
10751 
10752   verifyFormat(
10753       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
10754       "                                                          Parameter2);");
10755 
10756   verifyFormat(
10757       "ShortObject->shortFunction(\n"
10758       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
10759       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
10760 
10761   verifyFormat("loooooooooooooongFunction(\n"
10762                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
10763 
10764   verifyFormat(
10765       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
10766       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
10767 
10768   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
10769                "    .WillRepeatedly(Return(SomeValue));");
10770   verifyFormat("void f() {\n"
10771                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
10772                "      .Times(2)\n"
10773                "      .WillRepeatedly(Return(SomeValue));\n"
10774                "}");
10775   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
10776                "    ccccccccccccccccccccccc);");
10777   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10778                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10779                "          .aaaaa(aaaaa),\n"
10780                "      aaaaaaaaaaaaaaaaaaaaa);");
10781   verifyFormat("void f() {\n"
10782                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10783                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
10784                "}");
10785   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10786                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10787                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10788                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10789                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
10790   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10791                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10792                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10793                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
10794                "}");
10795 
10796   // Here, it is not necessary to wrap at "." or "->".
10797   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
10798                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
10799   verifyFormat(
10800       "aaaaaaaaaaa->aaaaaaaaa(\n"
10801       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10802       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));");
10803 
10804   verifyFormat(
10805       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10806       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
10807   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
10808                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
10809   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
10810                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
10811 
10812   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10813                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10814                "    .a();");
10815 
10816   FormatStyle NoBinPacking = getLLVMStyle();
10817   NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
10818   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
10819                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
10820                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
10821                "                         aaaaaaaaaaaaaaaaaaa,\n"
10822                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10823                NoBinPacking);
10824 
10825   // If there is a subsequent call, change to hanging indentation.
10826   verifyFormat(
10827       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10828       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
10829       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10830   verifyFormat(
10831       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10832       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
10833   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10834                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10835                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10836   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10837                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10838                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
10839 }
10840 
10841 TEST_F(FormatTest, WrapsTemplateDeclarations) {
10842   verifyFormat("template <typename T>\n"
10843                "virtual void loooooooooooongFunction(int Param1, int Param2);");
10844   verifyFormat("template <typename T>\n"
10845                "// T should be one of {A, B}.\n"
10846                "virtual void loooooooooooongFunction(int Param1, int Param2);");
10847   verifyFormat(
10848       "template <typename T>\n"
10849       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
10850   verifyFormat("template <typename T>\n"
10851                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
10852                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
10853   verifyFormat(
10854       "template <typename T>\n"
10855       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
10856       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
10857   verifyFormat(
10858       "template <typename T>\n"
10859       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
10860       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
10861       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10862   verifyFormat("template <typename T>\n"
10863                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10864                "    int aaaaaaaaaaaaaaaaaaaaaa);");
10865   verifyFormat(
10866       "template <typename T1, typename T2 = char, typename T3 = char,\n"
10867       "          typename T4 = char>\n"
10868       "void f();");
10869   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
10870                "          template <typename> class cccccccccccccccccccccc,\n"
10871                "          typename ddddddddddddd>\n"
10872                "class C {};");
10873   verifyFormat(
10874       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
10875       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10876 
10877   verifyFormat("void f() {\n"
10878                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
10879                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
10880                "}");
10881 
10882   verifyFormat("template <typename T> class C {};");
10883   verifyFormat("template <typename T> void f();");
10884   verifyFormat("template <typename T> void f() {}");
10885   verifyFormat(
10886       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
10887       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10888       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
10889       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
10890       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10891       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
10892       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
10893       getLLVMStyleWithColumns(72));
10894   verifyFormat("static_cast<A< //\n"
10895                "    B> *>(\n"
10896                "\n"
10897                ");",
10898                "static_cast<A<//\n"
10899                "    B>*>(\n"
10900                "\n"
10901                "    );");
10902   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10903                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
10904 
10905   FormatStyle AlwaysBreak = getLLVMStyle();
10906   AlwaysBreak.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
10907   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
10908   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
10909   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
10910   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10911                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
10912                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
10913   verifyFormat("template <template <typename> class Fooooooo,\n"
10914                "          template <typename> class Baaaaaaar>\n"
10915                "struct C {};",
10916                AlwaysBreak);
10917   verifyFormat("template <typename T> // T can be A, B or C.\n"
10918                "struct C {};",
10919                AlwaysBreak);
10920   verifyFormat("template <typename T>\n"
10921                "C(T) noexcept;",
10922                AlwaysBreak);
10923   verifyFormat("template <typename T>\n"
10924                "ClassName(T) noexcept;",
10925                AlwaysBreak);
10926   verifyFormat("template <typename T>\n"
10927                "POOR_NAME(T) noexcept;",
10928                AlwaysBreak);
10929   verifyFormat("template <enum E> class A {\n"
10930                "public:\n"
10931                "  E *f();\n"
10932                "};");
10933 
10934   FormatStyle NeverBreak = getLLVMStyle();
10935   NeverBreak.BreakTemplateDeclarations = FormatStyle::BTDS_No;
10936   verifyFormat("template <typename T> class C {};", NeverBreak);
10937   verifyFormat("template <typename T> void f();", NeverBreak);
10938   verifyFormat("template <typename T> void f() {}", NeverBreak);
10939   verifyFormat("template <typename T> C(T) noexcept;", NeverBreak);
10940   verifyFormat("template <typename T> ClassName(T) noexcept;", NeverBreak);
10941   verifyFormat("template <typename T> POOR_NAME(T) noexcept;", NeverBreak);
10942   verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
10943                "bbbbbbbbbbbbbbbbbbbb) {}",
10944                NeverBreak);
10945   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10946                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
10947                "    ccccccccccccccccccccccccccccccccccccccccccccccc);",
10948                NeverBreak);
10949   verifyFormat("template <template <typename> class Fooooooo,\n"
10950                "          template <typename> class Baaaaaaar>\n"
10951                "struct C {};",
10952                NeverBreak);
10953   verifyFormat("template <typename T> // T can be A, B or C.\n"
10954                "struct C {};",
10955                NeverBreak);
10956   verifyFormat("template <enum E> class A {\n"
10957                "public:\n"
10958                "  E *f();\n"
10959                "};",
10960                NeverBreak);
10961   NeverBreak.PenaltyBreakTemplateDeclaration = 100;
10962   verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
10963                "bbbbbbbbbbbbbbbbbbbb) {}",
10964                NeverBreak);
10965 
10966   auto Style = getLLVMStyle();
10967   Style.BreakTemplateDeclarations = FormatStyle::BTDS_Leave;
10968 
10969   verifyNoChange("template <typename T>\n"
10970                  "class C {};",
10971                  Style);
10972   verifyFormat("template <typename T> class C {};", Style);
10973 
10974   verifyNoChange("template <typename T>\n"
10975                  "void f();",
10976                  Style);
10977   verifyFormat("template <typename T> void f();", Style);
10978 
10979   verifyNoChange("template <typename T>\n"
10980                  "void f() {}",
10981                  Style);
10982   verifyFormat("template <typename T> void f() {}", Style);
10983 
10984   verifyNoChange("template <typename T>\n"
10985                  "// T can be A, B or C.\n"
10986                  "struct C {};",
10987                  Style);
10988   verifyFormat("template <typename T> // T can be A, B or C.\n"
10989                "struct C {};",
10990                Style);
10991 
10992   verifyNoChange("template <typename T>\n"
10993                  "C(T) noexcept;",
10994                  Style);
10995   verifyFormat("template <typename T> C(T) noexcept;", Style);
10996 
10997   verifyNoChange("template <enum E>\n"
10998                  "class A {\n"
10999                  "public:\n"
11000                  "  E *f();\n"
11001                  "};",
11002                  Style);
11003   verifyFormat("template <enum E> class A {\n"
11004                "public:\n"
11005                "  E *f();\n"
11006                "};",
11007                Style);
11008 
11009   verifyNoChange("template <auto x>\n"
11010                  "constexpr int simple(int) {\n"
11011                  "  char c;\n"
11012                  "  return 1;\n"
11013                  "}",
11014                  Style);
11015   verifyFormat("template <auto x> constexpr int simple(int) {\n"
11016                "  char c;\n"
11017                "  return 1;\n"
11018                "}",
11019                Style);
11020 
11021   Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
11022   verifyNoChange("template <auto x>\n"
11023                  "requires(x > 1)\n"
11024                  "constexpr int with_req(int) {\n"
11025                  "  return 1;\n"
11026                  "}",
11027                  Style);
11028   verifyFormat("template <auto x> requires(x > 1)\n"
11029                "constexpr int with_req(int) {\n"
11030                "  return 1;\n"
11031                "}",
11032                Style);
11033 }
11034 
11035 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
11036   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
11037   Style.ColumnLimit = 60;
11038   verifyFormat("// Baseline - no comments.\n"
11039                "template <\n"
11040                "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
11041                "void f() {}",
11042                Style);
11043 
11044   verifyFormat("template <\n"
11045                "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
11046                "void f() {}",
11047                "template <\n"
11048                "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
11049                "void f() {}",
11050                Style);
11051 
11052   verifyFormat(
11053       "template <\n"
11054       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
11055       "void f() {}",
11056       "template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  /* line */\n"
11057       "void f() {}",
11058       Style);
11059 
11060   verifyFormat("template <\n"
11061                "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
11062                "                                               // multiline\n"
11063                "void f() {}",
11064                "template <\n"
11065                "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
11066                "                                              // multiline\n"
11067                "void f() {}",
11068                Style);
11069 
11070   verifyFormat(
11071       "template <typename aaaaaaaaaa<\n"
11072       "    bbbbbbbbbbbb>::value>  // trailing loooong\n"
11073       "void f() {}",
11074       "template <\n"
11075       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
11076       "void f() {}",
11077       Style);
11078 }
11079 
11080 TEST_F(FormatTest, WrapsTemplateParameters) {
11081   FormatStyle Style = getLLVMStyle();
11082   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
11083   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
11084   verifyFormat(
11085       "template <typename... a> struct q {};\n"
11086       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
11087       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
11088       "    y;",
11089       Style);
11090   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
11091   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
11092   verifyFormat(
11093       "template <typename... a> struct r {};\n"
11094       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
11095       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
11096       "    y;",
11097       Style);
11098   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11099   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
11100   verifyFormat("template <typename... a> struct s {};\n"
11101                "extern s<\n"
11102                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11103                "aaaaaaaaaaaaaaaaaaaaaa,\n"
11104                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11105                "aaaaaaaaaaaaaaaaaaaaaa>\n"
11106                "    y;",
11107                Style);
11108   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11109   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
11110   verifyFormat("template <typename... a> struct t {};\n"
11111                "extern t<\n"
11112                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11113                "aaaaaaaaaaaaaaaaaaaaaa,\n"
11114                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11115                "aaaaaaaaaaaaaaaaaaaaaa>\n"
11116                "    y;",
11117                Style);
11118 }
11119 
11120 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
11121   verifyFormat(
11122       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11123       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
11124   verifyFormat(
11125       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11126       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11127       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
11128 
11129   // FIXME: Should we have the extra indent after the second break?
11130   verifyFormat(
11131       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11132       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11133       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
11134 
11135   verifyFormat(
11136       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
11137       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
11138 
11139   // Breaking at nested name specifiers is generally not desirable.
11140   verifyFormat(
11141       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11142       "    aaaaaaaaaaaaaaaaaaaaaaa);");
11143 
11144   verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
11145                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11146                "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11147                "                   aaaaaaaaaaaaaaaaaaaaa);",
11148                getLLVMStyleWithColumns(74));
11149 
11150   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11151                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11152                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
11153 
11154   verifyFormat(
11155       "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n"
11156       "    AndAnotherLongClassNameToShowTheIssue() {}\n"
11157       "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n"
11158       "    ~AndAnotherLongClassNameToShowTheIssue() {}");
11159 }
11160 
11161 TEST_F(FormatTest, UnderstandsTemplateParameters) {
11162   verifyFormat("A<int> a;");
11163   verifyFormat("A<A<A<int>>> a;");
11164   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
11165   verifyFormat("bool x = a < 1 || 2 > a;");
11166   verifyFormat("bool x = 5 < f<int>();");
11167   verifyFormat("bool x = f<int>() > 5;");
11168   verifyFormat("bool x = 5 < a<int>::x;");
11169   verifyFormat("bool x = a < 4 ? a > 2 : false;");
11170   verifyFormat("bool x = f() ? a < 2 : a > 2;");
11171 
11172   verifyGoogleFormat("A<A<int>> a;");
11173   verifyGoogleFormat("A<A<A<int>>> a;");
11174   verifyGoogleFormat("A<A<A<A<int>>>> a;");
11175   verifyGoogleFormat("A<A<int> > a;");
11176   verifyGoogleFormat("A<A<A<int> > > a;");
11177   verifyGoogleFormat("A<A<A<A<int> > > > a;");
11178   verifyGoogleFormat("A<::A<int>> a;");
11179   verifyGoogleFormat("A<::A> a;");
11180   verifyGoogleFormat("A< ::A> a;");
11181   verifyGoogleFormat("A< ::A<int> > a;");
11182   verifyFormat("A<A<A<A>>> a;", "A<A<A<A> >> a;", getGoogleStyle());
11183   verifyFormat("A<A<A<A>>> a;", "A<A<A<A>> > a;", getGoogleStyle());
11184   verifyFormat("A<::A<int>> a;", "A< ::A<int>> a;", getGoogleStyle());
11185   verifyFormat("A<::A<int>> a;", "A<::A<int> > a;", getGoogleStyle());
11186   verifyFormat("auto x = [] { A<A<A<A>>> a; };", "auto x=[]{A<A<A<A> >> a;};",
11187                getGoogleStyle());
11188 
11189   verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
11190 
11191   // template closer followed by a token that starts with > or =
11192   verifyFormat("bool b = a<1> > 1;");
11193   verifyFormat("bool b = a<1> >= 1;");
11194   verifyFormat("int i = a<1> >> 1;");
11195   FormatStyle Style = getLLVMStyle();
11196   Style.SpaceBeforeAssignmentOperators = false;
11197   verifyFormat("bool b= a<1> == 1;", Style);
11198   verifyFormat("a<int> = 1;", Style);
11199   verifyFormat("a<int> >>= 1;", Style);
11200 
11201   verifyFormat("test < a | b >> c;");
11202   verifyFormat("test<test<a | b>> c;");
11203   verifyFormat("test >> a >> b;");
11204   verifyFormat("test << a >> b;");
11205 
11206   verifyFormat("f<int>();");
11207   verifyFormat("template <typename T> void f() {}");
11208   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
11209   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
11210                "sizeof(char)>::type>;");
11211   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
11212   verifyFormat("f(a.operator()<A>());");
11213   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11214                "      .template operator()<A>());",
11215                getLLVMStyleWithColumns(35));
11216   verifyFormat("bool_constant<a && noexcept(f())>;");
11217   verifyFormat("bool_constant<a || noexcept(f())>;");
11218 
11219   verifyFormat("if (std::tuple_size_v<T> > 0)");
11220 
11221   // Not template parameters.
11222   verifyFormat("return a < b && c > d;");
11223   verifyFormat("a < 0 ? b : a > 0 ? c : d;");
11224   verifyFormat("ratio{-1, 2} < ratio{-1, 3} == -1 / 3 > -1 / 2;");
11225   verifyFormat("void f() {\n"
11226                "  while (a < b && c > d) {\n"
11227                "  }\n"
11228                "}");
11229   verifyFormat("template <typename... Types>\n"
11230                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
11231 
11232   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11233                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
11234                getLLVMStyleWithColumns(60));
11235   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
11236   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
11237   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
11238   verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
11239 
11240   verifyFormat("#define FOO(typeName, realClass)                           \\\n"
11241                "  {#typeName, foo<FooType>(new foo<realClass>(#typeName))}",
11242                getLLVMStyleWithColumns(60));
11243 }
11244 
11245 TEST_F(FormatTest, UnderstandsShiftOperators) {
11246   verifyFormat("if (i < x >> 1)");
11247   verifyFormat("while (i < x >> 1)");
11248   verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
11249   verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
11250   verifyFormat(
11251       "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
11252   verifyFormat("Foo.call<Bar<Function>>()");
11253   verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
11254   verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
11255                "++i, v = v >> 1)");
11256   verifyFormat("if (w<u<v<x>>, 1>::t)");
11257 }
11258 
11259 TEST_F(FormatTest, BitshiftOperatorWidth) {
11260   verifyFormat("int a = 1 << 2; /* foo\n"
11261                "                   bar */",
11262                "int    a=1<<2;  /* foo\n"
11263                "                   bar */");
11264 
11265   verifyFormat("int b = 256 >> 1; /* foo\n"
11266                "                     bar */",
11267                "int  b  =256>>1 ;  /* foo\n"
11268                "                      bar */");
11269 }
11270 
11271 TEST_F(FormatTest, UnderstandsBinaryOperators) {
11272   verifyFormat("COMPARE(a, ==, b);");
11273   verifyFormat("auto s = sizeof...(Ts) - 1;");
11274 }
11275 
11276 TEST_F(FormatTest, UnderstandsPointersToMembers) {
11277   verifyFormat("int A::*x;");
11278   verifyFormat("int (S::*func)(void *);");
11279   verifyFormat("void f() { int (S::*func)(void *); }");
11280   verifyFormat("typedef bool *(Class::*Member)() const;");
11281   verifyFormat("void f() {\n"
11282                "  (a->*f)();\n"
11283                "  a->*x;\n"
11284                "  (a.*f)();\n"
11285                "  ((*a).*f)();\n"
11286                "  a.*x;\n"
11287                "}");
11288   verifyFormat("void f() {\n"
11289                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
11290                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
11291                "}");
11292   verifyFormat(
11293       "(aaaaaaaaaa->*bbbbbbb)(\n"
11294       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
11295 
11296   FormatStyle Style = getLLVMStyle();
11297   EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
11298   verifyFormat("typedef bool *(Class::*Member)() const;", Style);
11299   verifyFormat("void f(int A::*p) { int A::*v = &A::B; }", Style);
11300 
11301   Style.PointerAlignment = FormatStyle::PAS_Left;
11302   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
11303   verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style);
11304 
11305   Style.PointerAlignment = FormatStyle::PAS_Middle;
11306   verifyFormat("typedef bool * (Class::*Member)() const;", Style);
11307   verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style);
11308 }
11309 
11310 TEST_F(FormatTest, UnderstandsUnaryOperators) {
11311   verifyFormat("int a = -2;");
11312   verifyFormat("f(-1, -2, -3);");
11313   verifyFormat("a[-1] = 5;");
11314   verifyFormat("int a = 5 + -2;");
11315   verifyFormat("if (i == -1) {\n}");
11316   verifyFormat("if (i != -1) {\n}");
11317   verifyFormat("if (i > -1) {\n}");
11318   verifyFormat("if (i < -1) {\n}");
11319   verifyFormat("++(a->f());");
11320   verifyFormat("--(a->f());");
11321   verifyFormat("(a->f())++;");
11322   verifyFormat("a[42]++;");
11323   verifyFormat("if (!(a->f())) {\n}");
11324   verifyFormat("if (!+i) {\n}");
11325   verifyFormat("~&a;");
11326   verifyFormat("for (x = 0; -10 < x; --x) {\n}");
11327   verifyFormat("sizeof -x");
11328   verifyFormat("sizeof +x");
11329   verifyFormat("sizeof *x");
11330   verifyFormat("sizeof &x");
11331   verifyFormat("delete +x;");
11332   verifyFormat("co_await +x;");
11333   verifyFormat("case *x:");
11334   verifyFormat("case &x:");
11335 
11336   verifyFormat("a-- > b;");
11337   verifyFormat("b ? -a : c;");
11338   verifyFormat("n * sizeof char16;");
11339   verifyGoogleFormat("n * alignof char16;");
11340   verifyFormat("sizeof(char);");
11341   verifyGoogleFormat("alignof(char);");
11342 
11343   verifyFormat("return -1;");
11344   verifyFormat("throw -1;");
11345   verifyFormat("switch (a) {\n"
11346                "case -1:\n"
11347                "  break;\n"
11348                "}");
11349   verifyFormat("#define X -1");
11350   verifyFormat("#define X -kConstant");
11351 
11352   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
11353   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
11354 
11355   verifyFormat("int a = /* confusing comment */ -1;");
11356   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
11357   verifyFormat("int a = i /* confusing comment */++;");
11358 
11359   verifyFormat("co_yield -1;");
11360   verifyFormat("co_return -1;");
11361 
11362   // Check that * is not treated as a binary operator when we set
11363   // PointerAlignment as PAS_Left after a keyword and not a declaration.
11364   FormatStyle PASLeftStyle = getLLVMStyle();
11365   PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
11366   verifyFormat("co_return *a;", PASLeftStyle);
11367   verifyFormat("co_await *a;", PASLeftStyle);
11368   verifyFormat("co_yield *a", PASLeftStyle);
11369   verifyFormat("return *a;", PASLeftStyle);
11370 }
11371 
11372 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
11373   verifyFormat("if (!aaaaaaaaaa( // break\n"
11374                "        aaaaa)) {\n"
11375                "}");
11376   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
11377                "    aaaaa));");
11378   verifyFormat("*aaa = aaaaaaa( // break\n"
11379                "    bbbbbb);");
11380 }
11381 
11382 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
11383   verifyFormat("bool operator<();");
11384   verifyFormat("bool operator>();");
11385   verifyFormat("bool operator=();");
11386   verifyFormat("bool operator==();");
11387   verifyFormat("bool operator!=();");
11388   verifyFormat("int operator+();");
11389   verifyFormat("int operator++();");
11390   verifyFormat("int operator++(int) volatile noexcept;");
11391   verifyFormat("bool operator,();");
11392   verifyFormat("bool operator();");
11393   verifyFormat("bool operator()();");
11394   verifyFormat("bool operator[]();");
11395   verifyFormat("operator bool();");
11396   verifyFormat("operator int();");
11397   verifyFormat("operator void *();");
11398   verifyFormat("operator SomeType<int>();");
11399   verifyFormat("operator SomeType<int, int>();");
11400   verifyFormat("operator SomeType<SomeType<int>>();");
11401   verifyFormat("operator< <>();");
11402   verifyFormat("operator<< <>();");
11403   verifyFormat("< <>");
11404 
11405   verifyFormat("void *operator new(std::size_t size);");
11406   verifyFormat("void *operator new[](std::size_t size);");
11407   verifyFormat("void operator delete(void *ptr);");
11408   verifyFormat("void operator delete[](void *ptr);");
11409   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
11410                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
11411   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
11412                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
11413 
11414   verifyFormat(
11415       "ostream &operator<<(ostream &OutputStream,\n"
11416       "                    SomeReallyLongType WithSomeReallyLongValue);");
11417   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
11418                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
11419                "  return left.group < right.group;\n"
11420                "}");
11421   verifyFormat("SomeType &operator=(const SomeType &S);");
11422   verifyFormat("f.template operator()<int>();");
11423 
11424   verifyGoogleFormat("operator void*();");
11425   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
11426   verifyGoogleFormat("operator ::A();");
11427 
11428   verifyFormat("using A::operator+;");
11429   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
11430                "int i;");
11431 
11432   // Calling an operator as a member function.
11433   verifyFormat("void f() { a.operator*(); }");
11434   verifyFormat("void f() { a.operator*(b & b); }");
11435   verifyFormat("void f() { a->operator&(a * b); }");
11436   verifyFormat("void f() { NS::a.operator+(*b * *b); }");
11437   verifyFormat("void f() { operator*(a & a); }");
11438   verifyFormat("void f() { operator&(a, b * b); }");
11439 
11440   verifyFormat("void f() { return operator()(x) * b; }");
11441   verifyFormat("void f() { return operator[](x) * b; }");
11442   verifyFormat("void f() { return operator\"\"_a(x) * b; }");
11443   verifyFormat("void f() { return operator\"\" _a(x) * b; }");
11444   verifyFormat("void f() { return operator\"\"s(x) * b; }");
11445   verifyFormat("void f() { return operator\"\" s(x) * b; }");
11446   verifyFormat("void f() { return operator\"\"if(x) * b; }");
11447 
11448   verifyFormat("::operator delete(foo);");
11449   verifyFormat("::operator new(n * sizeof(foo));");
11450   verifyFormat("foo() { ::operator delete(foo); }");
11451   verifyFormat("foo() { ::operator new(n * sizeof(foo)); }");
11452 }
11453 
11454 TEST_F(FormatTest, SpaceBeforeTemplateCloser) {
11455   verifyFormat("C<&operator- > minus;");
11456   verifyFormat("C<&operator> > gt;");
11457   verifyFormat("C<&operator>= > ge;");
11458   verifyFormat("C<&operator<= > le;");
11459   verifyFormat("C<&operator< <X>> lt;");
11460 }
11461 
11462 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
11463   verifyFormat("void A::b() && {}");
11464   verifyFormat("void A::b() && noexcept {}");
11465   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
11466   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
11467   verifyFormat("Deleted &operator=(const Deleted &) & noexcept = default;");
11468   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
11469   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
11470   verifyFormat("Deleted &operator=(const Deleted &) &;");
11471   verifyFormat("Deleted &operator=(const Deleted &) &&;");
11472   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
11473   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
11474   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
11475   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
11476   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
11477   verifyFormat("SomeType MemberFunction(const Deleted &) && noexcept {}");
11478   verifyFormat("void Fn(T const &) const &;");
11479   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
11480   verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;");
11481   verifyGoogleFormat("template <typename T>\n"
11482                      "void F(T) && = delete;");
11483   verifyFormat("template <typename T> void operator=(T) &;");
11484   verifyFormat("template <typename T> void operator=(T) const &;");
11485   verifyFormat("template <typename T> void operator=(T) & noexcept;");
11486   verifyFormat("template <typename T> void operator=(T) & = default;");
11487   verifyFormat("template <typename T> void operator=(T) &&;");
11488   verifyFormat("template <typename T> void operator=(T) && = delete;");
11489   verifyFormat("template <typename T> void operator=(T) & {}");
11490   verifyFormat("template <typename T> void operator=(T) && {}");
11491 
11492   FormatStyle AlignLeft = getLLVMStyle();
11493   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
11494   verifyFormat("void A::b() && {}", AlignLeft);
11495   verifyFormat("void A::b() && noexcept {}", AlignLeft);
11496   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
11497   verifyFormat("Deleted& operator=(const Deleted&) & noexcept = default;",
11498                AlignLeft);
11499   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
11500                AlignLeft);
11501   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
11502   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
11503   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
11504   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
11505   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
11506   verifyFormat("auto Function(T) & -> void;", AlignLeft);
11507   verifyFormat("void Fn(T const&) const&;", AlignLeft);
11508   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
11509   verifyFormat("void Fn(T const volatile&&) const volatile&& noexcept;",
11510                AlignLeft);
11511   verifyFormat("template <typename T> void operator=(T) &;", AlignLeft);
11512   verifyFormat("template <typename T> void operator=(T) const&;", AlignLeft);
11513   verifyFormat("template <typename T> void operator=(T) & noexcept;",
11514                AlignLeft);
11515   verifyFormat("template <typename T> void operator=(T) & = default;",
11516                AlignLeft);
11517   verifyFormat("template <typename T> void operator=(T) &&;", AlignLeft);
11518   verifyFormat("template <typename T> void operator=(T) && = delete;",
11519                AlignLeft);
11520   verifyFormat("template <typename T> void operator=(T) & {}", AlignLeft);
11521   verifyFormat("template <typename T> void operator=(T) && {}", AlignLeft);
11522 
11523   FormatStyle AlignMiddle = getLLVMStyle();
11524   AlignMiddle.PointerAlignment = FormatStyle::PAS_Middle;
11525   verifyFormat("void A::b() && {}", AlignMiddle);
11526   verifyFormat("void A::b() && noexcept {}", AlignMiddle);
11527   verifyFormat("Deleted & operator=(const Deleted &) & = default;",
11528                AlignMiddle);
11529   verifyFormat("Deleted & operator=(const Deleted &) & noexcept = default;",
11530                AlignMiddle);
11531   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;",
11532                AlignMiddle);
11533   verifyFormat("Deleted & operator=(const Deleted &) &;", AlignMiddle);
11534   verifyFormat("SomeType MemberFunction(const Deleted &) &;", AlignMiddle);
11535   verifyFormat("auto Function(T t) & -> void {}", AlignMiddle);
11536   verifyFormat("auto Function(T... t) & -> void {}", AlignMiddle);
11537   verifyFormat("auto Function(T) & -> void {}", AlignMiddle);
11538   verifyFormat("auto Function(T) & -> void;", AlignMiddle);
11539   verifyFormat("void Fn(T const &) const &;", AlignMiddle);
11540   verifyFormat("void Fn(T const volatile &&) const volatile &&;", AlignMiddle);
11541   verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;",
11542                AlignMiddle);
11543   verifyFormat("template <typename T> void operator=(T) &;", AlignMiddle);
11544   verifyFormat("template <typename T> void operator=(T) const &;", AlignMiddle);
11545   verifyFormat("template <typename T> void operator=(T) & noexcept;",
11546                AlignMiddle);
11547   verifyFormat("template <typename T> void operator=(T) & = default;",
11548                AlignMiddle);
11549   verifyFormat("template <typename T> void operator=(T) &&;", AlignMiddle);
11550   verifyFormat("template <typename T> void operator=(T) && = delete;",
11551                AlignMiddle);
11552   verifyFormat("template <typename T> void operator=(T) & {}", AlignMiddle);
11553   verifyFormat("template <typename T> void operator=(T) && {}", AlignMiddle);
11554 
11555   FormatStyle Spaces = getLLVMStyle();
11556   Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
11557   Spaces.SpacesInParensOptions = {};
11558   Spaces.SpacesInParensOptions.InCStyleCasts = true;
11559   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
11560   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
11561   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
11562   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
11563 
11564   Spaces.SpacesInParensOptions.InCStyleCasts = false;
11565   Spaces.SpacesInParensOptions.Other = true;
11566   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
11567   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
11568                Spaces);
11569   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
11570   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
11571 
11572   FormatStyle BreakTemplate = getLLVMStyle();
11573   BreakTemplate.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
11574 
11575   verifyFormat("struct f {\n"
11576                "  template <class T>\n"
11577                "  int &foo(const std::string &str) & noexcept {}\n"
11578                "};",
11579                BreakTemplate);
11580 
11581   verifyFormat("struct f {\n"
11582                "  template <class T>\n"
11583                "  int &foo(const std::string &str) && noexcept {}\n"
11584                "};",
11585                BreakTemplate);
11586 
11587   verifyFormat("struct f {\n"
11588                "  template <class T>\n"
11589                "  int &foo(const std::string &str) const & noexcept {}\n"
11590                "};",
11591                BreakTemplate);
11592 
11593   verifyFormat("struct f {\n"
11594                "  template <class T>\n"
11595                "  int &foo(const std::string &str) const & noexcept {}\n"
11596                "};",
11597                BreakTemplate);
11598 
11599   verifyFormat("struct f {\n"
11600                "  template <class T>\n"
11601                "  auto foo(const std::string &str) && noexcept -> int & {}\n"
11602                "};",
11603                BreakTemplate);
11604 
11605   FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
11606   AlignLeftBreakTemplate.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
11607   AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
11608 
11609   verifyFormat("struct f {\n"
11610                "  template <class T>\n"
11611                "  int& foo(const std::string& str) & noexcept {}\n"
11612                "};",
11613                AlignLeftBreakTemplate);
11614 
11615   verifyFormat("struct f {\n"
11616                "  template <class T>\n"
11617                "  int& foo(const std::string& str) && noexcept {}\n"
11618                "};",
11619                AlignLeftBreakTemplate);
11620 
11621   verifyFormat("struct f {\n"
11622                "  template <class T>\n"
11623                "  int& foo(const std::string& str) const& noexcept {}\n"
11624                "};",
11625                AlignLeftBreakTemplate);
11626 
11627   verifyFormat("struct f {\n"
11628                "  template <class T>\n"
11629                "  int& foo(const std::string& str) const&& noexcept {}\n"
11630                "};",
11631                AlignLeftBreakTemplate);
11632 
11633   verifyFormat("struct f {\n"
11634                "  template <class T>\n"
11635                "  auto foo(const std::string& str) && noexcept -> int& {}\n"
11636                "};",
11637                AlignLeftBreakTemplate);
11638 
11639   // The `&` in `Type&` should not be confused with a trailing `&` of
11640   // DEPRECATED(reason) member function.
11641   verifyFormat("struct f {\n"
11642                "  template <class T>\n"
11643                "  DEPRECATED(reason)\n"
11644                "  Type &foo(arguments) {}\n"
11645                "};",
11646                BreakTemplate);
11647 
11648   verifyFormat("struct f {\n"
11649                "  template <class T>\n"
11650                "  DEPRECATED(reason)\n"
11651                "  Type& foo(arguments) {}\n"
11652                "};",
11653                AlignLeftBreakTemplate);
11654 
11655   verifyFormat("void (*foopt)(int) = &func;");
11656 
11657   FormatStyle DerivePointerAlignment = getLLVMStyle();
11658   DerivePointerAlignment.DerivePointerAlignment = true;
11659   // There's always a space between the function and its trailing qualifiers.
11660   // This isn't evidence for PAS_Right (or for PAS_Left).
11661   std::string Prefix = "void a() &;\n"
11662                        "void b() &;\n";
11663   verifyFormat(Prefix + "int* x;", DerivePointerAlignment);
11664   verifyFormat(Prefix + "int *x;", DerivePointerAlignment);
11665   // Same if the function is an overloaded operator, and with &&.
11666   Prefix = "void operator()() &&;\n"
11667            "void operator()() &&;\n";
11668   verifyFormat(Prefix + "int* x;", DerivePointerAlignment);
11669   verifyFormat(Prefix + "int *x;", DerivePointerAlignment);
11670   // However a space between cv-qualifiers and ref-qualifiers *is* evidence.
11671   Prefix = "void a() const &;\n"
11672            "void b() const &;\n";
11673   verifyFormat(Prefix + "int *x;", Prefix + "int* x;", DerivePointerAlignment);
11674 }
11675 
11676 TEST_F(FormatTest, PointerAlignmentFallback) {
11677   FormatStyle Style = getLLVMStyle();
11678   Style.DerivePointerAlignment = true;
11679 
11680   const StringRef Code("int* p;\n"
11681                        "int *q;\n"
11682                        "int * r;");
11683 
11684   EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
11685   verifyFormat("int *p;\n"
11686                "int *q;\n"
11687                "int *r;",
11688                Code, Style);
11689 
11690   Style.PointerAlignment = FormatStyle::PAS_Left;
11691   verifyFormat("int* p;\n"
11692                "int* q;\n"
11693                "int* r;",
11694                Code, Style);
11695 
11696   Style.PointerAlignment = FormatStyle::PAS_Middle;
11697   verifyFormat("int * p;\n"
11698                "int * q;\n"
11699                "int * r;",
11700                Code, Style);
11701 }
11702 
11703 TEST_F(FormatTest, UnderstandsNewAndDelete) {
11704   verifyFormat("void f() {\n"
11705                "  A *a = new A;\n"
11706                "  A *a = new (placement) A;\n"
11707                "  delete a;\n"
11708                "  delete (A *)a;\n"
11709                "}");
11710   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
11711                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
11712   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
11713                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
11714                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
11715   verifyFormat("delete[] h->p;");
11716   verifyFormat("delete[] (void *)p;");
11717 
11718   verifyFormat("void operator delete(void *foo) ATTRIB;");
11719   verifyFormat("void operator new(void *foo) ATTRIB;");
11720   verifyFormat("void operator delete[](void *foo) ATTRIB;");
11721   verifyFormat("void operator delete(void *ptr) noexcept;");
11722 
11723   verifyFormat("void new(link p);\n"
11724                "void delete(link p);",
11725                "void new (link p);\n"
11726                "void delete (link p);");
11727 
11728   verifyFormat("{\n"
11729                "  p->new();\n"
11730                "}\n"
11731                "{\n"
11732                "  p->delete();\n"
11733                "}",
11734                "{\n"
11735                "  p->new ();\n"
11736                "}\n"
11737                "{\n"
11738                "  p->delete ();\n"
11739                "}");
11740 
11741   FormatStyle AfterPlacementOperator = getLLVMStyle();
11742   AfterPlacementOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
11743   EXPECT_TRUE(
11744       AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator);
11745   verifyFormat("new (buf) int;", AfterPlacementOperator);
11746   verifyFormat("struct A {\n"
11747                "  int *a;\n"
11748                "  A(int *p) : a(new (p) int) {\n"
11749                "    new (p) int;\n"
11750                "    int *b = new (p) int;\n"
11751                "    int *c = new (p) int(3);\n"
11752                "    delete (b);\n"
11753                "  }\n"
11754                "};",
11755                AfterPlacementOperator);
11756   verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator);
11757   verifyFormat("delete (int *)p;", AfterPlacementOperator);
11758 
11759   AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator =
11760       false;
11761   verifyFormat("new(buf) int;", AfterPlacementOperator);
11762   verifyFormat("struct A {\n"
11763                "  int *a;\n"
11764                "  A(int *p) : a(new(p) int) {\n"
11765                "    new(p) int;\n"
11766                "    int *b = new(p) int;\n"
11767                "    int *c = new(p) int(3);\n"
11768                "    delete(b);\n"
11769                "  }\n"
11770                "};",
11771                AfterPlacementOperator);
11772   verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator);
11773   verifyFormat("delete (int *)p;", AfterPlacementOperator);
11774 }
11775 
11776 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
11777   verifyFormat("int *f(int *a) {}");
11778   verifyFormat("int main(int argc, char **argv) {}");
11779   verifyFormat("Test::Test(int b) : a(b * b) {}");
11780   verifyIndependentOfContext("f(a, *a);");
11781   verifyFormat("void g() { f(*a); }");
11782   verifyIndependentOfContext("int a = b * 10;");
11783   verifyIndependentOfContext("int a = 10 * b;");
11784   verifyIndependentOfContext("int a = b * c;");
11785   verifyIndependentOfContext("int a += b * c;");
11786   verifyIndependentOfContext("int a -= b * c;");
11787   verifyIndependentOfContext("int a *= b * c;");
11788   verifyIndependentOfContext("int a /= b * c;");
11789   verifyIndependentOfContext("int a = *b;");
11790   verifyIndependentOfContext("int a = *b * c;");
11791   verifyIndependentOfContext("int a = b * *c;");
11792   verifyIndependentOfContext("int a = b * (10);");
11793   verifyIndependentOfContext("S << b * (10);");
11794   verifyIndependentOfContext("return 10 * b;");
11795   verifyIndependentOfContext("return *b * *c;");
11796   verifyIndependentOfContext("return a & ~b;");
11797   verifyIndependentOfContext("f(b ? *c : *d);");
11798   verifyIndependentOfContext("int a = b ? *c : *d;");
11799   verifyIndependentOfContext("*b = a;");
11800   verifyIndependentOfContext("a * ~b;");
11801   verifyIndependentOfContext("a * !b;");
11802   verifyIndependentOfContext("a * +b;");
11803   verifyIndependentOfContext("a * -b;");
11804   verifyIndependentOfContext("a * ++b;");
11805   verifyIndependentOfContext("a * --b;");
11806   verifyIndependentOfContext("a[4] * b;");
11807   verifyIndependentOfContext("a[a * a] = 1;");
11808   verifyIndependentOfContext("f() * b;");
11809   verifyIndependentOfContext("a * [self dostuff];");
11810   verifyIndependentOfContext("int x = a * (a + b);");
11811   verifyIndependentOfContext("(a *)(a + b);");
11812   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
11813   verifyIndependentOfContext("int *pa = (int *)&a;");
11814   verifyIndependentOfContext("return sizeof(int **);");
11815   verifyIndependentOfContext("return sizeof(int ******);");
11816   verifyIndependentOfContext("return (int **&)a;");
11817   verifyIndependentOfContext("f((*PointerToArray)[10]);");
11818   verifyFormat("void f(Type (*parameter)[10]) {}");
11819   verifyFormat("void f(Type (&parameter)[10]) {}");
11820   verifyGoogleFormat("return sizeof(int**);");
11821   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
11822   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
11823   verifyFormat("auto a = [](int **&, int ***) {};");
11824   verifyFormat("auto PointerBinding = [](const char *S) {};");
11825   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
11826   verifyFormat("[](const decltype(*a) &value) {}");
11827   verifyFormat("[](const typeof(*a) &value) {}");
11828   verifyFormat("[](const _Atomic(a *) &value) {}");
11829   verifyFormat("[](const __underlying_type(a) &value) {}");
11830   verifyFormat("decltype(a * b) F();");
11831   verifyFormat("typeof(a * b) F();");
11832   verifyFormat("#define MACRO() [](A *a) { return 1; }");
11833   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
11834   verifyIndependentOfContext("typedef void (*f)(int *a);");
11835   verifyIndependentOfContext("typedef void (*f)(Type *a);");
11836   verifyIndependentOfContext("int i{a * b};");
11837   verifyIndependentOfContext("aaa && aaa->f();");
11838   verifyIndependentOfContext("int x = ~*p;");
11839   verifyFormat("Constructor() : a(a), area(width * height) {}");
11840   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
11841   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
11842   verifyFormat("void f() { f(a, c * d); }");
11843   verifyFormat("void f() { f(new a(), c * d); }");
11844   verifyFormat("void f(const MyOverride &override);");
11845   verifyFormat("void f(const MyFinal &final);");
11846   verifyIndependentOfContext("bool a = f() && override.f();");
11847   verifyIndependentOfContext("bool a = f() && final.f();");
11848 
11849   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
11850 
11851   verifyIndependentOfContext("A<int *> a;");
11852   verifyIndependentOfContext("A<int **> a;");
11853   verifyIndependentOfContext("A<int *, int *> a;");
11854   verifyIndependentOfContext("A<int *[]> a;");
11855   verifyIndependentOfContext(
11856       "const char *const p = reinterpret_cast<const char *const>(q);");
11857   verifyIndependentOfContext("A<int **, int **> a;");
11858   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
11859   verifyFormat("for (char **a = b; *a; ++a) {\n}");
11860   verifyFormat("for (; a && b;) {\n}");
11861   verifyFormat("bool foo = true && [] { return false; }();");
11862 
11863   verifyFormat(
11864       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11865       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
11866 
11867   verifyGoogleFormat("int const* a = &b;");
11868   verifyGoogleFormat("**outparam = 1;");
11869   verifyGoogleFormat("*outparam = a * b;");
11870   verifyGoogleFormat("int main(int argc, char** argv) {}");
11871   verifyGoogleFormat("A<int*> a;");
11872   verifyGoogleFormat("A<int**> a;");
11873   verifyGoogleFormat("A<int*, int*> a;");
11874   verifyGoogleFormat("A<int**, int**> a;");
11875   verifyGoogleFormat("f(b ? *c : *d);");
11876   verifyGoogleFormat("int a = b ? *c : *d;");
11877   verifyGoogleFormat("Type* t = **x;");
11878   verifyGoogleFormat("Type* t = *++*x;");
11879   verifyGoogleFormat("*++*x;");
11880   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
11881   verifyGoogleFormat("Type* t = x++ * y;");
11882   verifyGoogleFormat(
11883       "const char* const p = reinterpret_cast<const char* const>(q);");
11884   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
11885   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
11886   verifyGoogleFormat("template <typename T>\n"
11887                      "void f(int i = 0, SomeType** temps = NULL);");
11888 
11889   FormatStyle Left = getLLVMStyle();
11890   Left.PointerAlignment = FormatStyle::PAS_Left;
11891   verifyFormat("x = *a(x) = *a(y);", Left);
11892   verifyFormat("for (;; *a = b) {\n}", Left);
11893   verifyFormat("return *this += 1;", Left);
11894   verifyFormat("throw *x;", Left);
11895   verifyFormat("delete *x;", Left);
11896   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
11897   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
11898   verifyFormat("[](const typeof(*a)* ptr) {}", Left);
11899   verifyFormat("[](const _Atomic(a*)* ptr) {}", Left);
11900   verifyFormat("[](const __underlying_type(a)* ptr) {}", Left);
11901   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
11902   verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left);
11903   verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left);
11904   verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left);
11905 
11906   verifyIndependentOfContext("a = *(x + y);");
11907   verifyIndependentOfContext("a = &(x + y);");
11908   verifyIndependentOfContext("*(x + y).call();");
11909   verifyIndependentOfContext("&(x + y)->call();");
11910   verifyFormat("void f() { &(*I).first; }");
11911 
11912   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
11913   verifyFormat("f(* /* confusing comment */ foo);");
11914   verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)");
11915   verifyFormat("void foo(int * // this is the first paramters\n"
11916                "         ,\n"
11917                "         int second);");
11918   verifyFormat("double term = a * // first\n"
11919                "              b;");
11920   verifyFormat(
11921       "int *MyValues = {\n"
11922       "    *A, // Operator detection might be confused by the '{'\n"
11923       "    *BB // Operator detection might be confused by previous comment\n"
11924       "};");
11925 
11926   verifyIndependentOfContext("if (int *a = &b)");
11927   verifyIndependentOfContext("if (int &a = *b)");
11928   verifyIndependentOfContext("if (a & b[i])");
11929   verifyIndependentOfContext("if constexpr (a & b[i])");
11930   verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
11931   verifyIndependentOfContext("if (a * (b * c))");
11932   verifyIndependentOfContext("if constexpr (a * (b * c))");
11933   verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
11934   verifyIndependentOfContext("if (a::b::c::d & b[i])");
11935   verifyIndependentOfContext("if (*b[i])");
11936   verifyIndependentOfContext("if (int *a = (&b))");
11937   verifyIndependentOfContext("while (int *a = &b)");
11938   verifyIndependentOfContext("while (a * (b * c))");
11939   verifyIndependentOfContext("size = sizeof *a;");
11940   verifyIndependentOfContext("if (a && (b = c))");
11941   verifyFormat("void f() {\n"
11942                "  for (const int &v : Values) {\n"
11943                "  }\n"
11944                "}");
11945   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
11946   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
11947   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
11948 
11949   verifyFormat("#define A (!a * b)");
11950   verifyFormat("#define MACRO     \\\n"
11951                "  int *i = a * b; \\\n"
11952                "  void f(a *b);",
11953                getLLVMStyleWithColumns(19));
11954 
11955   verifyIndependentOfContext("A = new SomeType *[Length];");
11956   verifyIndependentOfContext("A = new SomeType *[Length]();");
11957   verifyIndependentOfContext("T **t = new T *;");
11958   verifyIndependentOfContext("T **t = new T *();");
11959   verifyGoogleFormat("A = new SomeType*[Length]();");
11960   verifyGoogleFormat("A = new SomeType*[Length];");
11961   verifyGoogleFormat("T** t = new T*;");
11962   verifyGoogleFormat("T** t = new T*();");
11963 
11964   verifyFormat("STATIC_ASSERT((a & b) == 0);");
11965   verifyFormat("STATIC_ASSERT(0 == (a & b));");
11966   verifyFormat("template <bool a, bool b> "
11967                "typename t::if<x && y>::type f() {}");
11968   verifyFormat("template <int *y> f() {}");
11969   verifyFormat("vector<int *> v;");
11970   verifyFormat("vector<int *const> v;");
11971   verifyFormat("vector<int *const **const *> v;");
11972   verifyFormat("vector<int *volatile> v;");
11973   verifyFormat("vector<a *_Nonnull> v;");
11974   verifyFormat("vector<a *_Nullable> v;");
11975   verifyFormat("vector<a *_Null_unspecified> v;");
11976   verifyFormat("vector<a *__ptr32> v;");
11977   verifyFormat("vector<a *__ptr64> v;");
11978   verifyFormat("vector<a *__capability> v;");
11979   FormatStyle TypeMacros = getLLVMStyle();
11980   TypeMacros.TypenameMacros = {"LIST"};
11981   verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros);
11982   verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros);
11983   verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros);
11984   verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros);
11985   verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication
11986 
11987   FormatStyle CustomQualifier = getLLVMStyle();
11988   // Add identifiers that should not be parsed as a qualifier by default.
11989   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
11990   CustomQualifier.AttributeMacros.push_back("_My_qualifier");
11991   CustomQualifier.AttributeMacros.push_back("my_other_qualifier");
11992   verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
11993   verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier);
11994   verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
11995   verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier);
11996   verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
11997   verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier);
11998   verifyFormat("vector<a * _NotAQualifier> v;");
11999   verifyFormat("vector<a * __not_a_qualifier> v;");
12000   verifyFormat("vector<a * b> v;");
12001   verifyFormat("foo<b && false>();");
12002   verifyFormat("foo<b & 1>();");
12003   verifyFormat("foo<b & (1)>();");
12004   verifyFormat("foo<b & (~0)>();");
12005   verifyFormat("foo<b & (true)>();");
12006   verifyFormat("foo<b & ((1))>();");
12007   verifyFormat("foo<b & (/*comment*/ 1)>();");
12008   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
12009   verifyFormat("typeof(*::std::declval<const T &>()) void F();");
12010   verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
12011   verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
12012   verifyFormat(
12013       "template <class T, class = typename std::enable_if<\n"
12014       "                       std::is_integral<T>::value &&\n"
12015       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
12016       "void F();",
12017       getLLVMStyleWithColumns(70));
12018   verifyFormat("template <class T,\n"
12019                "          class = typename std::enable_if<\n"
12020                "              std::is_integral<T>::value &&\n"
12021                "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
12022                "          class U>\n"
12023                "void F();",
12024                getLLVMStyleWithColumns(70));
12025   verifyFormat(
12026       "template <class T,\n"
12027       "          class = typename ::std::enable_if<\n"
12028       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
12029       "void F();",
12030       getGoogleStyleWithColumns(68));
12031 
12032   FormatStyle Style = getLLVMStyle();
12033   Style.PointerAlignment = FormatStyle::PAS_Left;
12034   verifyFormat("struct {\n"
12035                "}* ptr;",
12036                Style);
12037   verifyFormat("union {\n"
12038                "}* ptr;",
12039                Style);
12040   verifyFormat("class {\n"
12041                "}* ptr;",
12042                Style);
12043   // Don't confuse a multiplication after a brace-initialized expression with
12044   // a class pointer.
12045   verifyFormat("int i = int{42} * 34;", Style);
12046   verifyFormat("struct {\n"
12047                "}&& ptr = {};",
12048                Style);
12049   verifyFormat("union {\n"
12050                "}&& ptr = {};",
12051                Style);
12052   verifyFormat("class {\n"
12053                "}&& ptr = {};",
12054                Style);
12055   verifyFormat("bool b = 3 == int{3} && true;");
12056 
12057   Style.PointerAlignment = FormatStyle::PAS_Middle;
12058   verifyFormat("struct {\n"
12059                "} * ptr;",
12060                Style);
12061   verifyFormat("union {\n"
12062                "} * ptr;",
12063                Style);
12064   verifyFormat("class {\n"
12065                "} * ptr;",
12066                Style);
12067   verifyFormat("struct {\n"
12068                "} && ptr = {};",
12069                Style);
12070   verifyFormat("union {\n"
12071                "} && ptr = {};",
12072                Style);
12073   verifyFormat("class {\n"
12074                "} && ptr = {};",
12075                Style);
12076 
12077   Style.PointerAlignment = FormatStyle::PAS_Right;
12078   verifyFormat("struct {\n"
12079                "} *ptr;",
12080                Style);
12081   verifyFormat("union {\n"
12082                "} *ptr;",
12083                Style);
12084   verifyFormat("class {\n"
12085                "} *ptr;",
12086                Style);
12087   verifyFormat("struct {\n"
12088                "} &&ptr = {};",
12089                Style);
12090   verifyFormat("union {\n"
12091                "} &&ptr = {};",
12092                Style);
12093   verifyFormat("class {\n"
12094                "} &&ptr = {};",
12095                Style);
12096 
12097   Style.PointerAlignment = FormatStyle::PAS_Left;
12098   verifyFormat("delete[] *ptr;", Style);
12099   verifyFormat("delete[] **ptr;", Style);
12100   verifyFormat("delete[] *(ptr);", Style);
12101 
12102   verifyIndependentOfContext("MACRO(int *i);");
12103   verifyIndependentOfContext("MACRO(auto *a);");
12104   verifyIndependentOfContext("MACRO(const A *a);");
12105   verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
12106   verifyIndependentOfContext("MACRO(decltype(A) *a);");
12107   verifyIndependentOfContext("MACRO(typeof(A) *a);");
12108   verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
12109   verifyIndependentOfContext("MACRO(A *const a);");
12110   verifyIndependentOfContext("MACRO(A *restrict a);");
12111   verifyIndependentOfContext("MACRO(A *__restrict__ a);");
12112   verifyIndependentOfContext("MACRO(A *__restrict a);");
12113   verifyIndependentOfContext("MACRO(A *volatile a);");
12114   verifyIndependentOfContext("MACRO(A *__volatile a);");
12115   verifyIndependentOfContext("MACRO(A *__volatile__ a);");
12116   verifyIndependentOfContext("MACRO(A *_Nonnull a);");
12117   verifyIndependentOfContext("MACRO(A *_Nullable a);");
12118   verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
12119   verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
12120   verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
12121   verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
12122   verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
12123   verifyIndependentOfContext("MACRO(A *__ptr32 a);");
12124   verifyIndependentOfContext("MACRO(A *__ptr64 a);");
12125   verifyIndependentOfContext("MACRO(A *__capability);");
12126   verifyIndependentOfContext("MACRO(A &__capability);");
12127   verifyFormat("MACRO(A *__my_qualifier);");               // type declaration
12128   verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
12129   // If we add __my_qualifier to AttributeMacros it should always be parsed as
12130   // a type declaration:
12131   verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier);
12132   verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier);
12133   // Also check that TypenameMacros prevents parsing it as multiplication:
12134   verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
12135   verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type
12136 
12137   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
12138   verifyFormat("void f() { f(float{1}, a * a); }");
12139   verifyFormat("void f() { f(float(1), a * a); }");
12140 
12141   verifyFormat("f((void (*)(int))g);");
12142   verifyFormat("f((void (&)(int))g);");
12143   verifyFormat("f((void (^)(int))g);");
12144 
12145   // FIXME: Is there a way to make this work?
12146   // verifyIndependentOfContext("MACRO(A *a);");
12147   verifyFormat("MACRO(A &B);");
12148   verifyFormat("MACRO(A *B);");
12149   verifyFormat("void f() { MACRO(A * B); }");
12150   verifyFormat("void f() { MACRO(A & B); }");
12151 
12152   // This lambda was mis-formatted after D88956 (treating it as a binop):
12153   verifyFormat("auto x = [](const decltype(x) &ptr) {};");
12154   verifyFormat("auto x = [](const decltype(x) *ptr) {};");
12155   verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
12156   verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
12157 
12158   verifyFormat("DatumHandle const *operator->() const { return input_; }");
12159   verifyFormat("return options != nullptr && operator==(*options);");
12160 
12161   verifyFormat("#define OP(x)                                    \\\n"
12162                "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
12163                "    return s << a.DebugString();                 \\\n"
12164                "  }",
12165                "#define OP(x) \\\n"
12166                "  ostream &operator<<(ostream &s, const A &a) { \\\n"
12167                "    return s << a.DebugString(); \\\n"
12168                "  }",
12169                getLLVMStyleWithColumns(50));
12170 
12171   verifyFormat("#define FOO             \\\n"
12172                "  void foo() {          \\\n"
12173                "    operator+(a * b);   \\\n"
12174                "  }",
12175                getLLVMStyleWithColumns(25));
12176 
12177   // FIXME: We cannot handle this case yet; we might be able to figure out that
12178   // foo<x> d > v; doesn't make sense.
12179   verifyFormat("foo<a<b && c> d> v;");
12180 
12181   FormatStyle PointerMiddle = getLLVMStyle();
12182   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
12183   verifyFormat("delete *x;", PointerMiddle);
12184   verifyFormat("int * x;", PointerMiddle);
12185   verifyFormat("int *[] x;", PointerMiddle);
12186   verifyFormat("template <int * y> f() {}", PointerMiddle);
12187   verifyFormat("int * f(int * a) {}", PointerMiddle);
12188   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
12189   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
12190   verifyFormat("A<int *> a;", PointerMiddle);
12191   verifyFormat("A<int **> a;", PointerMiddle);
12192   verifyFormat("A<int *, int *> a;", PointerMiddle);
12193   verifyFormat("A<int *[]> a;", PointerMiddle);
12194   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
12195   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
12196   verifyFormat("T ** t = new T *;", PointerMiddle);
12197 
12198   // Member function reference qualifiers aren't binary operators.
12199   verifyFormat("string // break\n"
12200                "operator()() & {}");
12201   verifyFormat("string // break\n"
12202                "operator()() && {}");
12203   verifyGoogleFormat("template <typename T>\n"
12204                      "auto x() & -> int {}");
12205 
12206   // Should be binary operators when used as an argument expression (overloaded
12207   // operator invoked as a member function).
12208   verifyFormat("void f() { a.operator()(a * a); }");
12209   verifyFormat("void f() { a->operator()(a & a); }");
12210   verifyFormat("void f() { a.operator()(*a & *a); }");
12211   verifyFormat("void f() { a->operator()(*a * *a); }");
12212 
12213   verifyFormat("int operator()(T (&&)[N]) { return 1; }");
12214   verifyFormat("int operator()(T (&)[N]) { return 0; }");
12215 
12216   verifyFormat("val1 & val2;");
12217   verifyFormat("val1 & val2 & val3;");
12218   verifyFormat("class c {\n"
12219                "  void func(type &a) { a & member; }\n"
12220                "  anotherType &member;\n"
12221                "}");
12222 }
12223 
12224 TEST_F(FormatTest, UnderstandsAttributes) {
12225   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
12226   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
12227                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
12228   verifyFormat("__attribute__((nodebug)) ::qualified_type f();");
12229   FormatStyle AfterType = getLLVMStyle();
12230   AfterType.BreakAfterReturnType = FormatStyle::RTBS_All;
12231   verifyFormat("__attribute__((nodebug)) void\n"
12232                "foo() {}",
12233                AfterType);
12234   verifyFormat("__unused void\n"
12235                "foo() {}",
12236                AfterType);
12237 
12238   FormatStyle CustomAttrs = getLLVMStyle();
12239   CustomAttrs.AttributeMacros.push_back("__unused");
12240   CustomAttrs.AttributeMacros.push_back("__attr1");
12241   CustomAttrs.AttributeMacros.push_back("__attr2");
12242   CustomAttrs.AttributeMacros.push_back("no_underscore_attr");
12243   verifyFormat("vector<SomeType *__attribute((foo))> v;");
12244   verifyFormat("vector<SomeType *__attribute__((foo))> v;");
12245   verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
12246   // Check that it is parsed as a multiplication without AttributeMacros and
12247   // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
12248   verifyFormat("vector<SomeType * __attr1> v;");
12249   verifyFormat("vector<SomeType __attr1 *> v;");
12250   verifyFormat("vector<SomeType __attr1 *const> v;");
12251   verifyFormat("vector<SomeType __attr1 * __attr2> v;");
12252   verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs);
12253   verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs);
12254   verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs);
12255   verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs);
12256   verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs);
12257   verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs);
12258   verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs);
12259   verifyFormat("__attr1 ::qualified_type f();", CustomAttrs);
12260   verifyFormat("__attr1() ::qualified_type f();", CustomAttrs);
12261   verifyFormat("__attr1(nodebug) ::qualified_type f();", CustomAttrs);
12262 
12263   // Check that these are not parsed as function declarations:
12264   CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12265   CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;
12266   verifyFormat("SomeType s(InitValue);", CustomAttrs);
12267   verifyFormat("SomeType s{InitValue};", CustomAttrs);
12268   verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs);
12269   verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs);
12270   verifyFormat("SomeType s __unused(InitValue);", CustomAttrs);
12271   verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
12272   verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
12273   verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
12274 }
12275 
12276 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
12277   // Check that qualifiers on pointers don't break parsing of casts.
12278   verifyFormat("x = (foo *const)*v;");
12279   verifyFormat("x = (foo *volatile)*v;");
12280   verifyFormat("x = (foo *restrict)*v;");
12281   verifyFormat("x = (foo *__attribute__((foo)))*v;");
12282   verifyFormat("x = (foo *_Nonnull)*v;");
12283   verifyFormat("x = (foo *_Nullable)*v;");
12284   verifyFormat("x = (foo *_Null_unspecified)*v;");
12285   verifyFormat("x = (foo *_Nonnull)*v;");
12286   verifyFormat("x = (foo *[[clang::attr]])*v;");
12287   verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
12288   verifyFormat("x = (foo *__ptr32)*v;");
12289   verifyFormat("x = (foo *__ptr64)*v;");
12290   verifyFormat("x = (foo *__capability)*v;");
12291 
12292   // Check that we handle multiple trailing qualifiers and skip them all to
12293   // determine that the expression is a cast to a pointer type.
12294   FormatStyle LongPointerRight = getLLVMStyleWithColumns(999);
12295   FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999);
12296   LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left;
12297   StringRef AllQualifiers =
12298       "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
12299       "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability";
12300   verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight);
12301   verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft);
12302 
12303   // Also check that address-of is not parsed as a binary bitwise-and:
12304   verifyFormat("x = (foo *const)&v;");
12305   verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight);
12306   verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft);
12307 
12308   // Check custom qualifiers:
12309   FormatStyle CustomQualifier = getLLVMStyleWithColumns(999);
12310   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
12311   verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
12312   verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier);
12313   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(),
12314                CustomQualifier);
12315   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(),
12316                CustomQualifier);
12317 
12318   // Check that unknown identifiers result in binary operator parsing:
12319   verifyFormat("x = (foo * __unknown_qualifier) * v;");
12320   verifyFormat("x = (foo * __unknown_qualifier) & v;");
12321 }
12322 
12323 TEST_F(FormatTest, UnderstandsSquareAttributes) {
12324   verifyFormat("SomeType s [[unused]] (InitValue);");
12325   verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
12326   verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
12327   verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
12328   verifyFormat("[[suppress(type.5)]] int uninitialized_on_purpose;");
12329   verifyFormat("void f() [[deprecated(\"so sorry\")]];");
12330   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12331                "    [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
12332   verifyFormat("[[nodiscard]] bool f() { return false; }");
12333   verifyFormat("class [[nodiscard]] f {\npublic:\n  f() {}\n}");
12334   verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n  f() {}\n}");
12335   verifyFormat("class [[gnu::unused]] f {\npublic:\n  f() {}\n}");
12336   verifyFormat("[[nodiscard]] ::qualified_type f();");
12337 
12338   // Make sure we do not mistake attributes for array subscripts.
12339   verifyFormat("int a() {}\n"
12340                "[[unused]] int b() {}");
12341   verifyFormat("NSArray *arr;\n"
12342                "arr[[Foo() bar]];");
12343 
12344   // On the other hand, we still need to correctly find array subscripts.
12345   verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
12346 
12347   // Make sure that we do not mistake Objective-C method inside array literals
12348   // as attributes, even if those method names are also keywords.
12349   verifyFormat("@[ [foo bar] ];");
12350   verifyFormat("@[ [NSArray class] ];");
12351   verifyFormat("@[ [foo enum] ];");
12352 
12353   verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
12354 
12355   // Make sure we do not parse attributes as lambda introducers.
12356   FormatStyle MultiLineFunctions = getLLVMStyle();
12357   MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12358   verifyFormat("[[unused]] int b() {\n"
12359                "  return 42;\n"
12360                "}",
12361                MultiLineFunctions);
12362 }
12363 
12364 TEST_F(FormatTest, AttributeClass) {
12365   FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
12366   verifyFormat("class S {\n"
12367                "  S(S&&) = default;\n"
12368                "};",
12369                Style);
12370   verifyFormat("class [[nodiscard]] S {\n"
12371                "  S(S&&) = default;\n"
12372                "};",
12373                Style);
12374   verifyFormat("class __attribute((maybeunused)) S {\n"
12375                "  S(S&&) = default;\n"
12376                "};",
12377                Style);
12378   verifyFormat("struct S {\n"
12379                "  S(S&&) = default;\n"
12380                "};",
12381                Style);
12382   verifyFormat("struct [[nodiscard]] S {\n"
12383                "  S(S&&) = default;\n"
12384                "};",
12385                Style);
12386 }
12387 
12388 TEST_F(FormatTest, AttributesAfterMacro) {
12389   FormatStyle Style = getLLVMStyle();
12390   verifyFormat("MACRO;\n"
12391                "__attribute__((maybe_unused)) int foo() {\n"
12392                "  //...\n"
12393                "}");
12394 
12395   verifyFormat("MACRO;\n"
12396                "[[nodiscard]] int foo() {\n"
12397                "  //...\n"
12398                "}");
12399 
12400   verifyNoChange("MACRO\n\n"
12401                  "__attribute__((maybe_unused)) int foo() {\n"
12402                  "  //...\n"
12403                  "}");
12404 
12405   verifyNoChange("MACRO\n\n"
12406                  "[[nodiscard]] int foo() {\n"
12407                  "  //...\n"
12408                  "}");
12409 }
12410 
12411 TEST_F(FormatTest, AttributePenaltyBreaking) {
12412   FormatStyle Style = getLLVMStyle();
12413   verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
12414                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
12415                Style);
12416   verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
12417                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
12418                Style);
12419   verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
12420                "shared_ptr<ALongTypeName> &C d) {\n}",
12421                Style);
12422 }
12423 
12424 TEST_F(FormatTest, UnderstandsEllipsis) {
12425   FormatStyle Style = getLLVMStyle();
12426   verifyFormat("int printf(const char *fmt, ...);");
12427   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
12428   verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
12429 
12430   verifyFormat("template <int *...PP> a;", Style);
12431 
12432   Style.PointerAlignment = FormatStyle::PAS_Left;
12433   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style);
12434 
12435   verifyFormat("template <int*... PP> a;", Style);
12436 
12437   Style.PointerAlignment = FormatStyle::PAS_Middle;
12438   verifyFormat("template <int *... PP> a;", Style);
12439 }
12440 
12441 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
12442   verifyFormat("int *a;\n"
12443                "int *a;\n"
12444                "int *a;",
12445                "int *a;\n"
12446                "int* a;\n"
12447                "int *a;",
12448                getGoogleStyle());
12449   verifyFormat("int* a;\n"
12450                "int* a;\n"
12451                "int* a;",
12452                "int* a;\n"
12453                "int* a;\n"
12454                "int *a;",
12455                getGoogleStyle());
12456   verifyFormat("int *a;\n"
12457                "int *a;\n"
12458                "int *a;",
12459                "int *a;\n"
12460                "int * a;\n"
12461                "int *  a;",
12462                getGoogleStyle());
12463   verifyFormat("auto x = [] {\n"
12464                "  int *a;\n"
12465                "  int *a;\n"
12466                "  int *a;\n"
12467                "};",
12468                "auto x=[]{int *a;\n"
12469                "int * a;\n"
12470                "int *  a;};",
12471                getGoogleStyle());
12472 }
12473 
12474 TEST_F(FormatTest, UnderstandsRvalueReferences) {
12475   verifyFormat("int f(int &&a) {}");
12476   verifyFormat("int f(int a, char &&b) {}");
12477   verifyFormat("void f() { int &&a = b; }");
12478   verifyGoogleFormat("int f(int a, char&& b) {}");
12479   verifyGoogleFormat("void f() { int&& a = b; }");
12480 
12481   verifyIndependentOfContext("A<int &&> a;");
12482   verifyIndependentOfContext("A<int &&, int &&> a;");
12483   verifyGoogleFormat("A<int&&> a;");
12484   verifyGoogleFormat("A<int&&, int&&> a;");
12485 
12486   // Not rvalue references:
12487   verifyFormat("template <bool B, bool C> class A {\n"
12488                "  static_assert(B && C, \"Something is wrong\");\n"
12489                "};");
12490   verifyFormat("template <typename T> void swap() noexcept(Bar<T> && Foo<T>);");
12491   verifyFormat("template <typename T> struct S {\n"
12492                "  explicit(Bar<T> && Foo<T>) S(const S &);\n"
12493                "};");
12494   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
12495   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
12496   verifyFormat("#define A(a, b) (a && b)");
12497 }
12498 
12499 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
12500   verifyFormat("void f() {\n"
12501                "  x[aaaaaaaaa -\n"
12502                "    b] = 23;\n"
12503                "}",
12504                getLLVMStyleWithColumns(15));
12505 }
12506 
12507 TEST_F(FormatTest, FormatsCasts) {
12508   verifyFormat("Type *A = static_cast<Type *>(P);");
12509   verifyFormat("static_cast<Type *>(P);");
12510   verifyFormat("static_cast<Type &>(Fun)(Args);");
12511   verifyFormat("static_cast<Type &>(*Fun)(Args);");
12512   verifyFormat("if (static_cast<int>(A) + B >= 0)\n  ;");
12513   // Check that static_cast<...>(...) does not require the next token to be on
12514   // the same line.
12515   verifyFormat("some_loooong_output << something_something__ << "
12516                "static_cast<const void *>(R)\n"
12517                "                    << something;");
12518   verifyFormat("a = static_cast<Type &>(*Fun)(Args);");
12519   verifyFormat("const_cast<Type &>(*Fun)(Args);");
12520   verifyFormat("dynamic_cast<Type &>(*Fun)(Args);");
12521   verifyFormat("reinterpret_cast<Type &>(*Fun)(Args);");
12522   verifyFormat("Type *A = (Type *)P;");
12523   verifyFormat("Type *A = (vector<Type *, int *>)P;");
12524   verifyFormat("int a = (int)(2.0f);");
12525   verifyFormat("int a = (int)2.0f;");
12526   verifyFormat("x[(int32)y];");
12527   verifyFormat("x = (int32)y;");
12528   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
12529   verifyFormat("int a = (int)*b;");
12530   verifyFormat("int a = (int)2.0f;");
12531   verifyFormat("int a = (int)~0;");
12532   verifyFormat("int a = (int)++a;");
12533   verifyFormat("int a = (int)sizeof(int);");
12534   verifyFormat("int a = (int)+2;");
12535   verifyFormat("my_int a = (my_int)2.0f;");
12536   verifyFormat("my_int a = (my_int)sizeof(int);");
12537   verifyFormat("return (my_int)aaa;");
12538   verifyFormat("throw (my_int)aaa;");
12539   verifyFormat("#define x ((int)-1)");
12540   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
12541   verifyFormat("#define p(q) ((int *)&q)");
12542   verifyFormat("fn(a)(b) + 1;");
12543 
12544   verifyFormat("void f() { my_int a = (my_int)*b; }");
12545   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
12546   verifyFormat("my_int a = (my_int)~0;");
12547   verifyFormat("my_int a = (my_int)++a;");
12548   verifyFormat("my_int a = (my_int)-2;");
12549   verifyFormat("my_int a = (my_int)1;");
12550   verifyFormat("my_int a = (my_int *)1;");
12551   verifyFormat("my_int a = (const my_int)-1;");
12552   verifyFormat("my_int a = (const my_int *)-1;");
12553   verifyFormat("my_int a = (my_int)(my_int)-1;");
12554   verifyFormat("my_int a = (ns::my_int)-2;");
12555   verifyFormat("case (my_int)ONE:");
12556   verifyFormat("auto x = (X)this;");
12557   // Casts in Obj-C style calls used to not be recognized as such.
12558   verifyGoogleFormat("int a = [(type*)[((type*)val) arg] arg];");
12559 
12560   // FIXME: single value wrapped with paren will be treated as cast.
12561   verifyFormat("void f(int i = (kValue)*kMask) {}");
12562 
12563   verifyFormat("{\n"
12564                "  (void)F;\n"
12565                "}");
12566 
12567   // Don't break after a cast's
12568   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
12569                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
12570                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
12571 
12572   verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)");
12573   verifyFormat("#define CONF_BOOL(x) (bool *)(x)");
12574   verifyFormat("#define CONF_BOOL(x) (bool)(x)");
12575   verifyFormat("bool *y = (bool *)(void *)(x);");
12576   verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)");
12577   verifyFormat("bool *y = (bool *)(void *)(int)(x);");
12578   verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)");
12579   verifyFormat("bool *y = (bool *)(void *)(int)foo(x);");
12580 
12581   // These are not casts.
12582   verifyFormat("void f(int *) {}");
12583   verifyFormat("f(foo)->b;");
12584   verifyFormat("f(foo).b;");
12585   verifyFormat("f(foo)(b);");
12586   verifyFormat("f(foo)[b];");
12587   verifyFormat("[](foo) { return 4; }(bar);");
12588   verifyFormat("(*funptr)(foo)[4];");
12589   verifyFormat("funptrs[4](foo)[4];");
12590   verifyFormat("void f(int *);");
12591   verifyFormat("void f(int *) = 0;");
12592   verifyFormat("void f(SmallVector<int>) {}");
12593   verifyFormat("void f(SmallVector<int>);");
12594   verifyFormat("void f(SmallVector<int>) = 0;");
12595   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
12596   verifyFormat("int a = sizeof(int) * b;");
12597   verifyGoogleFormat("int a = alignof(int) * b;");
12598   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
12599   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
12600   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
12601 
12602   // These are not casts, but at some point were confused with casts.
12603   verifyFormat("virtual void foo(int *) override;");
12604   verifyFormat("virtual void foo(char &) const;");
12605   verifyFormat("virtual void foo(int *a, char *) const;");
12606   verifyFormat("int a = sizeof(int *) + b;");
12607   verifyGoogleFormat("int a = alignof(int *) + b;");
12608   verifyFormat("bool b = f(g<int>) && c;");
12609   verifyFormat("typedef void (*f)(int i) func;");
12610   verifyFormat("void operator++(int) noexcept;");
12611   verifyFormat("void operator++(int &) noexcept;");
12612   verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
12613                "&) noexcept;");
12614   verifyFormat(
12615       "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
12616   verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
12617   verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
12618   verifyFormat("void operator delete(nothrow_t &) noexcept;");
12619   verifyFormat("void operator delete(foo &) noexcept;");
12620   verifyFormat("void operator delete(foo) noexcept;");
12621   verifyFormat("void operator delete(int) noexcept;");
12622   verifyFormat("void operator delete(int &) noexcept;");
12623   verifyFormat("void operator delete(int &) volatile noexcept;");
12624   verifyFormat("void operator delete(int &) const");
12625   verifyFormat("void operator delete(int &) = default");
12626   verifyFormat("void operator delete(int &) = delete");
12627   verifyFormat("void operator delete(int &) [[noreturn]]");
12628   verifyFormat("void operator delete(int &) throw();");
12629   verifyFormat("void operator delete(int &) throw(int);");
12630   verifyFormat("auto operator delete(int &) -> int;");
12631   verifyFormat("auto operator delete(int &) override");
12632   verifyFormat("auto operator delete(int &) final");
12633 
12634   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
12635                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
12636   // FIXME: The indentation here is not ideal.
12637   verifyFormat(
12638       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12639       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
12640       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
12641 }
12642 
12643 TEST_F(FormatTest, FormatsFunctionTypes) {
12644   verifyFormat("A<bool()> a;");
12645   verifyFormat("A<SomeType()> a;");
12646   verifyFormat("A<void (*)(int, std::string)> a;");
12647   verifyFormat("A<void *(int)>;");
12648   verifyFormat("void *(*a)(int *, SomeType *);");
12649   verifyFormat("int (*func)(void *);");
12650   verifyFormat("void f() { int (*func)(void *); }");
12651   verifyFormat("template <class CallbackClass>\n"
12652                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
12653 
12654   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
12655   verifyGoogleFormat("void* (*a)(int);");
12656   verifyGoogleFormat(
12657       "template <class CallbackClass>\n"
12658       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
12659 
12660   // Other constructs can look somewhat like function types:
12661   verifyFormat("A<sizeof(*x)> a;");
12662   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
12663   verifyFormat("some_var = function(*some_pointer_var)[0];");
12664   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
12665   verifyFormat("int x = f(&h)();");
12666   verifyFormat("returnsFunction(&param1, &param2)(param);");
12667   verifyFormat("std::function<\n"
12668                "    LooooooooooongTemplatedType<\n"
12669                "        SomeType>*(\n"
12670                "        LooooooooooooooooongType type)>\n"
12671                "    function;",
12672                getGoogleStyleWithColumns(40));
12673 }
12674 
12675 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
12676   verifyFormat("A (*foo_)[6];");
12677   verifyFormat("vector<int> (*foo_)[6];");
12678 }
12679 
12680 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
12681   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12682                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
12683   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
12684                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
12685   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12686                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
12687 
12688   // Different ways of ()-initializiation.
12689   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12690                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
12691   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12692                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
12693   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12694                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
12695   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12696                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
12697 
12698   // Lambdas should not confuse the variable declaration heuristic.
12699   verifyFormat("LooooooooooooooooongType\n"
12700                "    variable(nullptr, [](A *a) {});",
12701                getLLVMStyleWithColumns(40));
12702 }
12703 
12704 TEST_F(FormatTest, BreaksLongDeclarations) {
12705   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
12706                "    AnotherNameForTheLongType;");
12707   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
12708                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
12709   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12710                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
12711   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
12712                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
12713   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12714                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12715   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
12716                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12717   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
12718                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12719   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
12720                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12721   verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
12722                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12723   verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
12724                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12725   verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
12726                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12727   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12728                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
12729   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12730                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
12731   FormatStyle Indented = getLLVMStyle();
12732   Indented.IndentWrappedFunctionNames = true;
12733   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12734                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
12735                Indented);
12736   verifyFormat(
12737       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12738       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12739       Indented);
12740   verifyFormat(
12741       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
12742       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12743       Indented);
12744   verifyFormat(
12745       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
12746       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12747       Indented);
12748 
12749   // FIXME: Without the comment, this breaks after "(".
12750   verifyGoogleFormat(
12751       "LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
12752       "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();");
12753 
12754   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
12755                "                  int LoooooooooooooooooooongParam2) {}");
12756   verifyFormat(
12757       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
12758       "                                   SourceLocation L, IdentifierIn *II,\n"
12759       "                                   Type *T) {}");
12760   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
12761                "ReallyReaaallyLongFunctionName(\n"
12762                "    const std::string &SomeParameter,\n"
12763                "    const SomeType<string, SomeOtherTemplateParameter>\n"
12764                "        &ReallyReallyLongParameterName,\n"
12765                "    const SomeType<string, SomeOtherTemplateParameter>\n"
12766                "        &AnotherLongParameterName) {}");
12767   verifyFormat("template <typename A>\n"
12768                "SomeLoooooooooooooooooooooongType<\n"
12769                "    typename some_namespace::SomeOtherType<A>::Type>\n"
12770                "Function() {}");
12771 
12772   verifyGoogleFormat(
12773       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
12774       "    aaaaaaaaaaaaaaaaaaaaaaa;");
12775   verifyGoogleFormat(
12776       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
12777       "                                   SourceLocation L) {}");
12778   verifyGoogleFormat(
12779       "some_namespace::LongReturnType\n"
12780       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
12781       "    int first_long_parameter, int second_parameter) {}");
12782 
12783   verifyGoogleFormat("template <typename T>\n"
12784                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
12785                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
12786   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12787                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
12788 
12789   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
12790                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12791                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
12792   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12793                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
12794                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
12795   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12796                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
12797                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
12798                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
12799 
12800   verifyFormat("template <typename T> // Templates on own line.\n"
12801                "static int            // Some comment.\n"
12802                "MyFunction(int a);");
12803 }
12804 
12805 TEST_F(FormatTest, FormatsAccessModifiers) {
12806   FormatStyle Style = getLLVMStyle();
12807   EXPECT_EQ(Style.EmptyLineBeforeAccessModifier,
12808             FormatStyle::ELBAMS_LogicalBlock);
12809   verifyFormat("struct foo {\n"
12810                "private:\n"
12811                "  void f() {}\n"
12812                "\n"
12813                "private:\n"
12814                "  int i;\n"
12815                "\n"
12816                "protected:\n"
12817                "  int j;\n"
12818                "};",
12819                Style);
12820   verifyFormat("struct foo {\n"
12821                "private:\n"
12822                "  void f() {}\n"
12823                "\n"
12824                "private:\n"
12825                "  int i;\n"
12826                "\n"
12827                "protected:\n"
12828                "  int j;\n"
12829                "};",
12830                "struct foo {\n"
12831                "private:\n"
12832                "  void f() {}\n"
12833                "private:\n"
12834                "  int i;\n"
12835                "protected:\n"
12836                "  int j;\n"
12837                "};",
12838                Style);
12839   verifyFormat("struct foo { /* comment */\n"
12840                "private:\n"
12841                "  int i;\n"
12842                "  // comment\n"
12843                "private:\n"
12844                "  int j;\n"
12845                "};",
12846                Style);
12847   verifyFormat("struct foo {\n"
12848                "#ifdef FOO\n"
12849                "#endif\n"
12850                "private:\n"
12851                "  int i;\n"
12852                "#ifdef FOO\n"
12853                "private:\n"
12854                "#endif\n"
12855                "  int j;\n"
12856                "};",
12857                Style);
12858   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
12859   verifyFormat("struct foo {\n"
12860                "private:\n"
12861                "  void f() {}\n"
12862                "private:\n"
12863                "  int i;\n"
12864                "protected:\n"
12865                "  int j;\n"
12866                "};",
12867                Style);
12868   verifyFormat("struct foo {\n"
12869                "private:\n"
12870                "  void f() {}\n"
12871                "private:\n"
12872                "  int i;\n"
12873                "protected:\n"
12874                "  int j;\n"
12875                "};",
12876                "struct foo {\n"
12877                "\n"
12878                "private:\n"
12879                "  void f() {}\n"
12880                "\n"
12881                "private:\n"
12882                "  int i;\n"
12883                "\n"
12884                "protected:\n"
12885                "  int j;\n"
12886                "};",
12887                Style);
12888   verifyFormat("struct foo { /* comment */\n"
12889                "private:\n"
12890                "  int i;\n"
12891                "  // comment\n"
12892                "private:\n"
12893                "  int j;\n"
12894                "};",
12895                "struct foo { /* comment */\n"
12896                "\n"
12897                "private:\n"
12898                "  int i;\n"
12899                "  // comment\n"
12900                "\n"
12901                "private:\n"
12902                "  int j;\n"
12903                "};",
12904                Style);
12905   verifyFormat("struct foo {\n"
12906                "#ifdef FOO\n"
12907                "#endif\n"
12908                "private:\n"
12909                "  int i;\n"
12910                "#ifdef FOO\n"
12911                "private:\n"
12912                "#endif\n"
12913                "  int j;\n"
12914                "};",
12915                "struct foo {\n"
12916                "#ifdef FOO\n"
12917                "#endif\n"
12918                "\n"
12919                "private:\n"
12920                "  int i;\n"
12921                "#ifdef FOO\n"
12922                "\n"
12923                "private:\n"
12924                "#endif\n"
12925                "  int j;\n"
12926                "};",
12927                Style);
12928   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
12929   verifyFormat("struct foo {\n"
12930                "private:\n"
12931                "  void f() {}\n"
12932                "\n"
12933                "private:\n"
12934                "  int i;\n"
12935                "\n"
12936                "protected:\n"
12937                "  int j;\n"
12938                "};",
12939                Style);
12940   verifyFormat("struct foo {\n"
12941                "private:\n"
12942                "  void f() {}\n"
12943                "\n"
12944                "private:\n"
12945                "  int i;\n"
12946                "\n"
12947                "protected:\n"
12948                "  int j;\n"
12949                "};",
12950                "struct foo {\n"
12951                "private:\n"
12952                "  void f() {}\n"
12953                "private:\n"
12954                "  int i;\n"
12955                "protected:\n"
12956                "  int j;\n"
12957                "};",
12958                Style);
12959   verifyFormat("struct foo { /* comment */\n"
12960                "private:\n"
12961                "  int i;\n"
12962                "  // comment\n"
12963                "\n"
12964                "private:\n"
12965                "  int j;\n"
12966                "};",
12967                Style);
12968   verifyFormat("struct foo {\n"
12969                "#ifdef FOO\n"
12970                "#endif\n"
12971                "\n"
12972                "private:\n"
12973                "  int i;\n"
12974                "#ifdef FOO\n"
12975                "\n"
12976                "private:\n"
12977                "#endif\n"
12978                "  int j;\n"
12979                "};",
12980                "struct foo {\n"
12981                "#ifdef FOO\n"
12982                "#endif\n"
12983                "private:\n"
12984                "  int i;\n"
12985                "#ifdef FOO\n"
12986                "private:\n"
12987                "#endif\n"
12988                "  int j;\n"
12989                "};",
12990                Style);
12991   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
12992   verifyNoChange("struct foo {\n"
12993                  "\n"
12994                  "private:\n"
12995                  "  void f() {}\n"
12996                  "\n"
12997                  "private:\n"
12998                  "  int i;\n"
12999                  "\n"
13000                  "protected:\n"
13001                  "  int j;\n"
13002                  "};",
13003                  Style);
13004   verifyFormat("struct foo {\n"
13005                "private:\n"
13006                "  void f() {}\n"
13007                "private:\n"
13008                "  int i;\n"
13009                "protected:\n"
13010                "  int j;\n"
13011                "};",
13012                Style);
13013   verifyNoChange("struct foo { /* comment */\n"
13014                  "\n"
13015                  "private:\n"
13016                  "  int i;\n"
13017                  "  // comment\n"
13018                  "\n"
13019                  "private:\n"
13020                  "  int j;\n"
13021                  "};",
13022                  Style);
13023   verifyFormat("struct foo { /* comment */\n"
13024                "private:\n"
13025                "  int i;\n"
13026                "  // comment\n"
13027                "private:\n"
13028                "  int j;\n"
13029                "};",
13030                Style);
13031   verifyNoChange("struct foo {\n"
13032                  "#ifdef FOO\n"
13033                  "#endif\n"
13034                  "\n"
13035                  "private:\n"
13036                  "  int i;\n"
13037                  "#ifdef FOO\n"
13038                  "\n"
13039                  "private:\n"
13040                  "#endif\n"
13041                  "  int j;\n"
13042                  "};",
13043                  Style);
13044   verifyFormat("struct foo {\n"
13045                "#ifdef FOO\n"
13046                "#endif\n"
13047                "private:\n"
13048                "  int i;\n"
13049                "#ifdef FOO\n"
13050                "private:\n"
13051                "#endif\n"
13052                "  int j;\n"
13053                "};",
13054                Style);
13055   Style.AttributeMacros.push_back("FOO");
13056   Style.AttributeMacros.push_back("BAR");
13057   verifyFormat("struct foo {\n"
13058                "FOO private:\n"
13059                "  int i;\n"
13060                "BAR(x) protected:\n"
13061                "  int j;\n"
13062                "};",
13063                Style);
13064 
13065   FormatStyle NoEmptyLines = getLLVMStyle();
13066   NoEmptyLines.MaxEmptyLinesToKeep = 0;
13067   verifyFormat("struct foo {\n"
13068                "private:\n"
13069                "  void f() {}\n"
13070                "\n"
13071                "private:\n"
13072                "  int i;\n"
13073                "\n"
13074                "public:\n"
13075                "protected:\n"
13076                "  int j;\n"
13077                "};",
13078                NoEmptyLines);
13079 
13080   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13081   verifyFormat("struct foo {\n"
13082                "private:\n"
13083                "  void f() {}\n"
13084                "private:\n"
13085                "  int i;\n"
13086                "public:\n"
13087                "protected:\n"
13088                "  int j;\n"
13089                "};",
13090                NoEmptyLines);
13091 
13092   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13093   verifyFormat("struct foo {\n"
13094                "private:\n"
13095                "  void f() {}\n"
13096                "\n"
13097                "private:\n"
13098                "  int i;\n"
13099                "\n"
13100                "public:\n"
13101                "\n"
13102                "protected:\n"
13103                "  int j;\n"
13104                "};",
13105                NoEmptyLines);
13106 }
13107 
13108 TEST_F(FormatTest, FormatsAfterAccessModifiers) {
13109 
13110   FormatStyle Style = getLLVMStyle();
13111   EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never);
13112   verifyFormat("struct foo {\n"
13113                "private:\n"
13114                "  void f() {}\n"
13115                "\n"
13116                "private:\n"
13117                "  int i;\n"
13118                "\n"
13119                "protected:\n"
13120                "  int j;\n"
13121                "};",
13122                Style);
13123 
13124   // Check if lines are removed.
13125   verifyFormat("struct foo {\n"
13126                "private:\n"
13127                "  void f() {}\n"
13128                "\n"
13129                "private:\n"
13130                "  int i;\n"
13131                "\n"
13132                "protected:\n"
13133                "  int j;\n"
13134                "};",
13135                "struct foo {\n"
13136                "private:\n"
13137                "\n"
13138                "  void f() {}\n"
13139                "\n"
13140                "private:\n"
13141                "\n"
13142                "  int i;\n"
13143                "\n"
13144                "protected:\n"
13145                "\n"
13146                "  int j;\n"
13147                "};",
13148                Style);
13149 
13150   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13151   verifyFormat("struct foo {\n"
13152                "private:\n"
13153                "\n"
13154                "  void f() {}\n"
13155                "\n"
13156                "private:\n"
13157                "\n"
13158                "  int i;\n"
13159                "\n"
13160                "protected:\n"
13161                "\n"
13162                "  int j;\n"
13163                "};",
13164                Style);
13165 
13166   // Check if lines are added.
13167   verifyFormat("struct foo {\n"
13168                "private:\n"
13169                "\n"
13170                "  void f() {}\n"
13171                "\n"
13172                "private:\n"
13173                "\n"
13174                "  int i;\n"
13175                "\n"
13176                "protected:\n"
13177                "\n"
13178                "  int j;\n"
13179                "};",
13180                "struct foo {\n"
13181                "private:\n"
13182                "  void f() {}\n"
13183                "\n"
13184                "private:\n"
13185                "  int i;\n"
13186                "\n"
13187                "protected:\n"
13188                "  int j;\n"
13189                "};",
13190                Style);
13191 
13192   // Leave tests rely on the code layout, test::messUp can not be used.
13193   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13194   Style.MaxEmptyLinesToKeep = 0u;
13195   verifyFormat("struct foo {\n"
13196                "private:\n"
13197                "  void f() {}\n"
13198                "\n"
13199                "private:\n"
13200                "  int i;\n"
13201                "\n"
13202                "protected:\n"
13203                "  int j;\n"
13204                "};",
13205                Style);
13206 
13207   // Check if MaxEmptyLinesToKeep is respected.
13208   verifyFormat("struct foo {\n"
13209                "private:\n"
13210                "  void f() {}\n"
13211                "\n"
13212                "private:\n"
13213                "  int i;\n"
13214                "\n"
13215                "protected:\n"
13216                "  int j;\n"
13217                "};",
13218                "struct foo {\n"
13219                "private:\n"
13220                "\n\n\n"
13221                "  void f() {}\n"
13222                "\n"
13223                "private:\n"
13224                "\n\n\n"
13225                "  int i;\n"
13226                "\n"
13227                "protected:\n"
13228                "\n\n\n"
13229                "  int j;\n"
13230                "};",
13231                Style);
13232 
13233   Style.MaxEmptyLinesToKeep = 1u;
13234   verifyNoChange("struct foo {\n"
13235                  "private:\n"
13236                  "\n"
13237                  "  void f() {}\n"
13238                  "\n"
13239                  "private:\n"
13240                  "\n"
13241                  "  int i;\n"
13242                  "\n"
13243                  "protected:\n"
13244                  "\n"
13245                  "  int j;\n"
13246                  "};",
13247                  Style);
13248   // Check if no lines are kept.
13249   verifyFormat("struct foo {\n"
13250                "private:\n"
13251                "  void f() {}\n"
13252                "\n"
13253                "private:\n"
13254                "  int i;\n"
13255                "\n"
13256                "protected:\n"
13257                "  int j;\n"
13258                "};",
13259                Style);
13260   // Check if MaxEmptyLinesToKeep is respected.
13261   verifyFormat("struct foo {\n"
13262                "private:\n"
13263                "\n"
13264                "  void f() {}\n"
13265                "\n"
13266                "private:\n"
13267                "\n"
13268                "  int i;\n"
13269                "\n"
13270                "protected:\n"
13271                "\n"
13272                "  int j;\n"
13273                "};",
13274                "struct foo {\n"
13275                "private:\n"
13276                "\n\n\n"
13277                "  void f() {}\n"
13278                "\n"
13279                "private:\n"
13280                "\n\n\n"
13281                "  int i;\n"
13282                "\n"
13283                "protected:\n"
13284                "\n\n\n"
13285                "  int j;\n"
13286                "};",
13287                Style);
13288 
13289   Style.MaxEmptyLinesToKeep = 10u;
13290   verifyNoChange("struct foo {\n"
13291                  "private:\n"
13292                  "\n\n\n"
13293                  "  void f() {}\n"
13294                  "\n"
13295                  "private:\n"
13296                  "\n\n\n"
13297                  "  int i;\n"
13298                  "\n"
13299                  "protected:\n"
13300                  "\n\n\n"
13301                  "  int j;\n"
13302                  "};",
13303                  Style);
13304 
13305   // Test with comments.
13306   Style = getLLVMStyle();
13307   verifyFormat("struct foo {\n"
13308                "private:\n"
13309                "  // comment\n"
13310                "  void f() {}\n"
13311                "\n"
13312                "private: /* comment */\n"
13313                "  int i;\n"
13314                "};",
13315                Style);
13316   verifyFormat("struct foo {\n"
13317                "private:\n"
13318                "  // comment\n"
13319                "  void f() {}\n"
13320                "\n"
13321                "private: /* comment */\n"
13322                "  int i;\n"
13323                "};",
13324                "struct foo {\n"
13325                "private:\n"
13326                "\n"
13327                "  // comment\n"
13328                "  void f() {}\n"
13329                "\n"
13330                "private: /* comment */\n"
13331                "\n"
13332                "  int i;\n"
13333                "};",
13334                Style);
13335 
13336   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13337   verifyFormat("struct foo {\n"
13338                "private:\n"
13339                "\n"
13340                "  // comment\n"
13341                "  void f() {}\n"
13342                "\n"
13343                "private: /* comment */\n"
13344                "\n"
13345                "  int i;\n"
13346                "};",
13347                "struct foo {\n"
13348                "private:\n"
13349                "  // comment\n"
13350                "  void f() {}\n"
13351                "\n"
13352                "private: /* comment */\n"
13353                "  int i;\n"
13354                "};",
13355                Style);
13356   verifyFormat("struct foo {\n"
13357                "private:\n"
13358                "\n"
13359                "  // comment\n"
13360                "  void f() {}\n"
13361                "\n"
13362                "private: /* comment */\n"
13363                "\n"
13364                "  int i;\n"
13365                "};",
13366                Style);
13367 
13368   // Test with preprocessor defines.
13369   Style = getLLVMStyle();
13370   verifyFormat("struct foo {\n"
13371                "private:\n"
13372                "#ifdef FOO\n"
13373                "#endif\n"
13374                "  void f() {}\n"
13375                "};",
13376                Style);
13377   verifyFormat("struct foo {\n"
13378                "private:\n"
13379                "#ifdef FOO\n"
13380                "#endif\n"
13381                "  void f() {}\n"
13382                "};",
13383                "struct foo {\n"
13384                "private:\n"
13385                "\n"
13386                "#ifdef FOO\n"
13387                "#endif\n"
13388                "  void f() {}\n"
13389                "};",
13390                Style);
13391   verifyNoChange("struct foo {\n"
13392                  "#ifdef FOO\n"
13393                  "#else\n"
13394                  "private:\n"
13395                  "\n"
13396                  "#endif\n"
13397                  "};",
13398                  Style);
13399   verifyFormat("struct foo {\n"
13400                "#ifdef FOO\n"
13401                "#else\n"
13402                "private:\n"
13403                "\n"
13404                "#endif\n"
13405                "};",
13406                "struct foo {\n"
13407                "#ifdef FOO\n"
13408                "#else\n"
13409                "private:\n"
13410                "\n"
13411                "\n"
13412                "#endif\n"
13413                "};",
13414                Style);
13415   verifyFormat("struct foo {\n"
13416                "#ifdef FOO\n"
13417                "private:\n"
13418                "#else\n"
13419                "#endif\n"
13420                "};",
13421                "struct foo {\n"
13422                "#ifdef FOO\n"
13423                "private:\n"
13424                "\n"
13425                "\n"
13426                "#else\n"
13427                "#endif\n"
13428                "};",
13429                Style);
13430   verifyFormat("struct foo {\n"
13431                "#if 0\n"
13432                "#else\n"
13433                "#endif\n"
13434                "#ifdef FOO\n"
13435                "private:\n"
13436                "#endif\n"
13437                "};",
13438                "struct foo {\n"
13439                "#if 0\n"
13440                "#else\n"
13441                "#endif\n"
13442                "#ifdef FOO\n"
13443                "private:\n"
13444                "\n"
13445                "\n"
13446                "#endif\n"
13447                "};",
13448                Style);
13449 
13450   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13451   verifyFormat("struct foo {\n"
13452                "private:\n"
13453                "\n"
13454                "#ifdef FOO\n"
13455                "#endif\n"
13456                "  void f() {}\n"
13457                "};",
13458                "struct foo {\n"
13459                "private:\n"
13460                "#ifdef FOO\n"
13461                "#endif\n"
13462                "  void f() {}\n"
13463                "};",
13464                Style);
13465   verifyFormat("struct foo {\n"
13466                "private:\n"
13467                "\n"
13468                "#ifdef FOO\n"
13469                "#endif\n"
13470                "  void f() {}\n"
13471                "};",
13472                Style);
13473 }
13474 
13475 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) {
13476   // Combined tests of EmptyLineAfterAccessModifier and
13477   // EmptyLineBeforeAccessModifier.
13478   FormatStyle Style = getLLVMStyle();
13479   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13480   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13481   verifyFormat("struct foo {\n"
13482                "private:\n"
13483                "\n"
13484                "protected:\n"
13485                "};",
13486                Style);
13487 
13488   Style.MaxEmptyLinesToKeep = 10u;
13489   // Both remove all new lines.
13490   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13491   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13492   verifyFormat("struct foo {\n"
13493                "private:\n"
13494                "protected:\n"
13495                "};",
13496                "struct foo {\n"
13497                "private:\n"
13498                "\n\n\n"
13499                "protected:\n"
13500                "};",
13501                Style);
13502 
13503   // Leave tests rely on the code layout, test::messUp can not be used.
13504   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
13505   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13506   Style.MaxEmptyLinesToKeep = 10u;
13507   verifyNoChange("struct foo {\n"
13508                  "private:\n"
13509                  "\n\n\n"
13510                  "protected:\n"
13511                  "};",
13512                  Style);
13513   Style.MaxEmptyLinesToKeep = 3u;
13514   verifyNoChange("struct foo {\n"
13515                  "private:\n"
13516                  "\n\n\n"
13517                  "protected:\n"
13518                  "};",
13519                  Style);
13520   Style.MaxEmptyLinesToKeep = 1u;
13521   verifyNoChange("struct foo {\n"
13522                  "private:\n"
13523                  "\n\n\n"
13524                  "protected:\n"
13525                  "};",
13526                  Style); // Based on new lines in original document and not
13527                          // on the setting.
13528 
13529   Style.MaxEmptyLinesToKeep = 10u;
13530   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13531   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13532   // Newlines are kept if they are greater than zero,
13533   // test::messUp removes all new lines which changes the logic
13534   verifyNoChange("struct foo {\n"
13535                  "private:\n"
13536                  "\n\n\n"
13537                  "protected:\n"
13538                  "};",
13539                  Style);
13540 
13541   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
13542   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13543   // test::messUp removes all new lines which changes the logic
13544   verifyNoChange("struct foo {\n"
13545                  "private:\n"
13546                  "\n\n\n"
13547                  "protected:\n"
13548                  "};",
13549                  Style);
13550 
13551   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
13552   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13553   verifyNoChange("struct foo {\n"
13554                  "private:\n"
13555                  "\n\n\n"
13556                  "protected:\n"
13557                  "};",
13558                  Style); // test::messUp removes all new lines which changes
13559                          // the logic.
13560 
13561   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13562   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13563   verifyFormat("struct foo {\n"
13564                "private:\n"
13565                "protected:\n"
13566                "};",
13567                "struct foo {\n"
13568                "private:\n"
13569                "\n\n\n"
13570                "protected:\n"
13571                "};",
13572                Style);
13573 
13574   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13575   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13576   verifyNoChange("struct foo {\n"
13577                  "private:\n"
13578                  "\n\n\n"
13579                  "protected:\n"
13580                  "};",
13581                  Style); // test::messUp removes all new lines which changes
13582                          // the logic.
13583 
13584   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13585   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13586   verifyFormat("struct foo {\n"
13587                "private:\n"
13588                "protected:\n"
13589                "};",
13590                "struct foo {\n"
13591                "private:\n"
13592                "\n\n\n"
13593                "protected:\n"
13594                "};",
13595                Style);
13596 
13597   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
13598   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13599   verifyFormat("struct foo {\n"
13600                "private:\n"
13601                "protected:\n"
13602                "};",
13603                "struct foo {\n"
13604                "private:\n"
13605                "\n\n\n"
13606                "protected:\n"
13607                "};",
13608                Style);
13609 
13610   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
13611   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13612   verifyFormat("struct foo {\n"
13613                "private:\n"
13614                "protected:\n"
13615                "};",
13616                "struct foo {\n"
13617                "private:\n"
13618                "\n\n\n"
13619                "protected:\n"
13620                "};",
13621                Style);
13622 
13623   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
13624   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13625   verifyFormat("struct foo {\n"
13626                "private:\n"
13627                "protected:\n"
13628                "};",
13629                "struct foo {\n"
13630                "private:\n"
13631                "\n\n\n"
13632                "protected:\n"
13633                "};",
13634                Style);
13635 }
13636 
13637 TEST_F(FormatTest, FormatsArrays) {
13638   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13639                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
13640   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
13641                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
13642   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
13643                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
13644   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13645                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
13646   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13647                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
13648   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13649                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13650                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
13651   verifyFormat(
13652       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
13653       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13654       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
13655   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
13656                "    .aaaaaaaaaaaaaaaaaaaaaa();");
13657 
13658   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
13659                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
13660   verifyFormat(
13661       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
13662       "                                  .aaaaaaa[0]\n"
13663       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
13664   verifyFormat("a[::b::c];");
13665 
13666   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
13667 
13668   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
13669   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
13670 }
13671 
13672 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
13673   verifyFormat("(a)->b();");
13674   verifyFormat("--a;");
13675 }
13676 
13677 TEST_F(FormatTest, HandlesIncludeDirectives) {
13678   verifyFormat("#include <string>\n"
13679                "#include <a/b/c.h>\n"
13680                "#include \"a/b/string\"\n"
13681                "#include \"string.h\"\n"
13682                "#include \"string.h\"\n"
13683                "#include <a-a>\n"
13684                "#include < path with space >\n"
13685                "#include_next <test.h>"
13686                "#include \"abc.h\" // this is included for ABC\n"
13687                "#include \"some long include\" // with a comment\n"
13688                "#include \"some very long include path\"\n"
13689                "#include <some/very/long/include/path>",
13690                getLLVMStyleWithColumns(35));
13691   verifyFormat("#include \"a.h\"", "#include  \"a.h\"");
13692   verifyFormat("#include <a>", "#include<a>");
13693 
13694   verifyFormat("#import <string>");
13695   verifyFormat("#import <a/b/c.h>");
13696   verifyFormat("#import \"a/b/string\"");
13697   verifyFormat("#import \"string.h\"");
13698   verifyFormat("#import \"string.h\"");
13699   verifyFormat("#if __has_include(<strstream>)\n"
13700                "#include <strstream>\n"
13701                "#endif");
13702 
13703   verifyFormat("#define MY_IMPORT <a/b>");
13704 
13705   verifyFormat("#if __has_include(<a/b>)");
13706   verifyFormat("#if __has_include_next(<a/b>)");
13707   verifyFormat("#define F __has_include(<a/b>)");
13708   verifyFormat("#define F __has_include_next(<a/b>)");
13709 
13710   // Protocol buffer definition or missing "#".
13711   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
13712                getLLVMStyleWithColumns(30));
13713 
13714   FormatStyle Style = getLLVMStyle();
13715   Style.AlwaysBreakBeforeMultilineStrings = true;
13716   Style.ColumnLimit = 0;
13717   verifyFormat("#import \"abc.h\"", Style);
13718 
13719   // But 'import' might also be a regular C++ namespace.
13720   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
13721                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
13722   verifyFormat("import::Bar foo(val ? 2 : 1);");
13723 }
13724 
13725 //===----------------------------------------------------------------------===//
13726 // Error recovery tests.
13727 //===----------------------------------------------------------------------===//
13728 
13729 TEST_F(FormatTest, IncompleteParameterLists) {
13730   FormatStyle NoBinPacking = getLLVMStyle();
13731   NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
13732   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
13733                "                        double *min_x,\n"
13734                "                        double *max_x,\n"
13735                "                        double *min_y,\n"
13736                "                        double *max_y,\n"
13737                "                        double *min_z,\n"
13738                "                        double *max_z, ) {}",
13739                NoBinPacking);
13740 }
13741 
13742 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
13743   verifyFormat("void f() { return; }\n42");
13744   verifyFormat("void f() {\n"
13745                "  if (0)\n"
13746                "    return;\n"
13747                "}\n"
13748                "42");
13749   verifyFormat("void f() { return }\n42");
13750   verifyFormat("void f() {\n"
13751                "  if (0)\n"
13752                "    return\n"
13753                "}\n"
13754                "42");
13755 }
13756 
13757 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
13758   verifyFormat("void f() { return }", "void  f ( )  {  return  }");
13759   verifyFormat("void f() {\n"
13760                "  if (a)\n"
13761                "    return\n"
13762                "}",
13763                "void  f  (  )  {  if  ( a )  return  }");
13764   verifyFormat("namespace N {\n"
13765                "void f()\n"
13766                "}",
13767                "namespace  N  {  void f()  }");
13768   verifyFormat("namespace N {\n"
13769                "void f() {}\n"
13770                "void g()\n"
13771                "} // namespace N",
13772                "namespace N  { void f( ) { } void g( ) }");
13773 }
13774 
13775 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
13776   verifyFormat("int aaaaaaaa =\n"
13777                "    // Overlylongcomment\n"
13778                "    b;",
13779                getLLVMStyleWithColumns(20));
13780   verifyFormat("function(\n"
13781                "    ShortArgument,\n"
13782                "    LoooooooooooongArgument);",
13783                getLLVMStyleWithColumns(20));
13784 }
13785 
13786 TEST_F(FormatTest, IncorrectAccessSpecifier) {
13787   verifyFormat("public:");
13788   verifyFormat("class A {\n"
13789                "public\n"
13790                "  void f() {}\n"
13791                "};");
13792   verifyFormat("public\n"
13793                "int qwerty;");
13794   verifyFormat("public\n"
13795                "B {}");
13796   verifyFormat("public\n"
13797                "{\n"
13798                "}");
13799   verifyFormat("public\n"
13800                "B { int x; }");
13801 }
13802 
13803 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
13804   verifyFormat("{");
13805   verifyFormat("#})");
13806   verifyNoCrash("(/**/[:!] ?[).");
13807   verifyNoCrash("struct X {\n"
13808                 "  operator iunt(\n"
13809                 "};");
13810   verifyNoCrash("struct Foo {\n"
13811                 "  operator foo(bar\n"
13812                 "};");
13813 }
13814 
13815 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
13816   // Found by oss-fuzz:
13817   // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
13818   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
13819   Style.ColumnLimit = 60;
13820   verifyNoCrash(
13821       "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
13822       "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
13823       "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
13824       Style);
13825 }
13826 
13827 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
13828   verifyFormat("do {\n}");
13829   verifyFormat("do {\n}\n"
13830                "f();");
13831   verifyFormat("do {\n}\n"
13832                "wheeee(fun);");
13833   verifyFormat("do {\n"
13834                "  f();\n"
13835                "}");
13836 }
13837 
13838 TEST_F(FormatTest, IncorrectCodeMissingParens) {
13839   verifyFormat("if {\n  foo;\n  foo();\n}");
13840   verifyFormat("switch {\n  foo;\n  foo();\n}");
13841   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
13842   verifyIncompleteFormat("ERROR: for target;");
13843   verifyFormat("while {\n  foo;\n  foo();\n}");
13844   verifyFormat("do {\n  foo;\n  foo();\n} while;");
13845 }
13846 
13847 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
13848   verifyIncompleteFormat("namespace {\n"
13849                          "class Foo { Foo (\n"
13850                          "};\n"
13851                          "} // namespace");
13852 }
13853 
13854 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
13855   verifyFormat("{\n"
13856                "  {\n"
13857                "  }",
13858                "{\n"
13859                "{\n"
13860                "}");
13861   verifyFormat("{\n"
13862                "  {\n"
13863                "  }",
13864                "{\n"
13865                "  {\n"
13866                "}");
13867   verifyFormat("{\n"
13868                "  {\n"
13869                "  }");
13870   verifyFormat("{\n"
13871                "  {\n"
13872                "  }\n"
13873                "}\n"
13874                "}",
13875                "{\n"
13876                "  {\n"
13877                "    }\n"
13878                "  }\n"
13879                "}");
13880 
13881   verifyFormat("{\n"
13882                "  {\n"
13883                "    breakme(\n"
13884                "        qwe);\n"
13885                "  }",
13886                "{\n"
13887                "    {\n"
13888                " breakme(qwe);\n"
13889                "}",
13890                getLLVMStyleWithColumns(10));
13891 }
13892 
13893 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
13894   verifyFormat("int x = {\n"
13895                "    avariable,\n"
13896                "    b(alongervariable)};",
13897                getLLVMStyleWithColumns(25));
13898 }
13899 
13900 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
13901   verifyFormat("return (a)(b){1, 2, 3};");
13902 }
13903 
13904 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
13905   verifyFormat("vector<int> x{1, 2, 3, 4};");
13906   verifyFormat("vector<int> x{\n"
13907                "    1,\n"
13908                "    2,\n"
13909                "    3,\n"
13910                "    4,\n"
13911                "};");
13912   verifyFormat("vector<T> x{{}, {}, {}, {}};");
13913   verifyFormat("f({1, 2});");
13914   verifyFormat("auto v = Foo{-1};");
13915   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
13916   verifyFormat("Class::Class : member{1, 2, 3} {}");
13917   verifyFormat("new vector<int>{1, 2, 3};");
13918   verifyFormat("new int[3]{1, 2, 3};");
13919   verifyFormat("new int{1};");
13920   verifyFormat("return {arg1, arg2};");
13921   verifyFormat("return {arg1, SomeType{parameter}};");
13922   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
13923   verifyFormat("new T{arg1, arg2};");
13924   verifyFormat("f(MyMap[{composite, key}]);");
13925   verifyFormat("class Class {\n"
13926                "  T member = {arg1, arg2};\n"
13927                "};");
13928   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
13929   verifyFormat("const struct A a = {.a = 1, .b = 2};");
13930   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
13931   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
13932   verifyFormat("int a = std::is_integral<int>{} + 0;");
13933 
13934   verifyFormat("int foo(int i) { return fo1{}(i); }");
13935   verifyFormat("int foo(int i) { return fo1{}(i); }");
13936   verifyFormat("auto i = decltype(x){};");
13937   verifyFormat("auto i = typeof(x){};");
13938   verifyFormat("auto i = _Atomic(x){};");
13939   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
13940   verifyFormat("Node n{1, Node{1000}, //\n"
13941                "       2};");
13942   verifyFormat("Aaaa aaaaaaa{\n"
13943                "    {\n"
13944                "        aaaa,\n"
13945                "    },\n"
13946                "};");
13947   verifyFormat("class C : public D {\n"
13948                "  SomeClass SC{2};\n"
13949                "};");
13950   verifyFormat("class C : public A {\n"
13951                "  class D : public B {\n"
13952                "    void f() { int i{2}; }\n"
13953                "  };\n"
13954                "};");
13955   verifyFormat("#define A {a, a},");
13956   // Don't confuse braced list initializers with compound statements.
13957   verifyFormat(
13958       "class A {\n"
13959       "  A() : a{} {}\n"
13960       "  A() : Base<int>{} {}\n"
13961       "  A() : Base<Foo<int>>{} {}\n"
13962       "  A(int b) : b(b) {}\n"
13963       "  A(int a, int b) : a(a), bs{{bs...}} { f(); }\n"
13964       "  int a, b;\n"
13965       "  explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n"
13966       "  explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} "
13967       "{}\n"
13968       "};");
13969 
13970   // Avoid breaking between equal sign and opening brace
13971   FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
13972   AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
13973   verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
13974                "    {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
13975                "     {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
13976                "     {\"ccccccccccccccccccccc\", 2}};",
13977                AvoidBreakingFirstArgument);
13978 
13979   // Binpacking only if there is no trailing comma
13980   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
13981                "                      cccccccccc, dddddddddd};",
13982                getLLVMStyleWithColumns(50));
13983   verifyFormat("const Aaaaaa aaaaa = {\n"
13984                "    aaaaaaaaaaa,\n"
13985                "    bbbbbbbbbbb,\n"
13986                "    ccccccccccc,\n"
13987                "    ddddddddddd,\n"
13988                "};",
13989                getLLVMStyleWithColumns(50));
13990 
13991   // Cases where distinguising braced lists and blocks is hard.
13992   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
13993   verifyFormat("void f() {\n"
13994                "  return; // comment\n"
13995                "}\n"
13996                "SomeType t;");
13997   verifyFormat("void f() {\n"
13998                "  if (a) {\n"
13999                "    f();\n"
14000                "  }\n"
14001                "}\n"
14002                "SomeType t;");
14003 
14004   // In combination with BinPackArguments = false.
14005   FormatStyle NoBinPacking = getLLVMStyle();
14006   NoBinPacking.BinPackArguments = false;
14007   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
14008                "                      bbbbb,\n"
14009                "                      ccccc,\n"
14010                "                      ddddd,\n"
14011                "                      eeeee,\n"
14012                "                      ffffff,\n"
14013                "                      ggggg,\n"
14014                "                      hhhhhh,\n"
14015                "                      iiiiii,\n"
14016                "                      jjjjjj,\n"
14017                "                      kkkkkk};",
14018                NoBinPacking);
14019   verifyFormat("const Aaaaaa aaaaa = {\n"
14020                "    aaaaa,\n"
14021                "    bbbbb,\n"
14022                "    ccccc,\n"
14023                "    ddddd,\n"
14024                "    eeeee,\n"
14025                "    ffffff,\n"
14026                "    ggggg,\n"
14027                "    hhhhhh,\n"
14028                "    iiiiii,\n"
14029                "    jjjjjj,\n"
14030                "    kkkkkk,\n"
14031                "};",
14032                NoBinPacking);
14033   verifyFormat(
14034       "const Aaaaaa aaaaa = {\n"
14035       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
14036       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
14037       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
14038       "};",
14039       NoBinPacking);
14040 
14041   NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
14042   verifyFormat("static uint8 CddDp83848Reg[] = {\n"
14043                "    CDDDP83848_BMCR_REGISTER,\n"
14044                "    CDDDP83848_BMSR_REGISTER,\n"
14045                "    CDDDP83848_RBR_REGISTER};",
14046                "static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
14047                "                                CDDDP83848_BMSR_REGISTER,\n"
14048                "                                CDDDP83848_RBR_REGISTER};",
14049                NoBinPacking);
14050 
14051   // FIXME: The alignment of these trailing comments might be bad. Then again,
14052   // this might be utterly useless in real code.
14053   verifyFormat("Constructor::Constructor()\n"
14054                "    : some_value{         //\n"
14055                "                 aaaaaaa, //\n"
14056                "                 bbbbbbb} {}");
14057 
14058   // In braced lists, the first comment is always assumed to belong to the
14059   // first element. Thus, it can be moved to the next or previous line as
14060   // appropriate.
14061   verifyFormat("function({// First element:\n"
14062                "          1,\n"
14063                "          // Second element:\n"
14064                "          2});",
14065                "function({\n"
14066                "    // First element:\n"
14067                "    1,\n"
14068                "    // Second element:\n"
14069                "    2});");
14070   verifyFormat("std::vector<int> MyNumbers{\n"
14071                "    // First element:\n"
14072                "    1,\n"
14073                "    // Second element:\n"
14074                "    2};",
14075                "std::vector<int> MyNumbers{// First element:\n"
14076                "                           1,\n"
14077                "                           // Second element:\n"
14078                "                           2};",
14079                getLLVMStyleWithColumns(30));
14080   // A trailing comma should still lead to an enforced line break and no
14081   // binpacking.
14082   verifyFormat("vector<int> SomeVector = {\n"
14083                "    // aaa\n"
14084                "    1,\n"
14085                "    2,\n"
14086                "};",
14087                "vector<int> SomeVector = { // aaa\n"
14088                "    1, 2, };");
14089 
14090   // C++11 brace initializer list l-braces should not be treated any differently
14091   // when breaking before lambda bodies is enabled
14092   FormatStyle BreakBeforeLambdaBody = getLLVMStyle();
14093   BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
14094   BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
14095   BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true;
14096   verifyFormat(
14097       "std::runtime_error{\n"
14098       "    \"Long string which will force a break onto the next line...\"};",
14099       BreakBeforeLambdaBody);
14100 
14101   FormatStyle ExtraSpaces = getLLVMStyle();
14102   ExtraSpaces.Cpp11BracedListStyle = false;
14103   ExtraSpaces.ColumnLimit = 75;
14104   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
14105   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
14106   verifyFormat("f({ 1, 2 });", ExtraSpaces);
14107   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
14108   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
14109   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
14110   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
14111   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
14112   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
14113   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
14114   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
14115   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
14116   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
14117   verifyFormat("class Class {\n"
14118                "  T member = { arg1, arg2 };\n"
14119                "};",
14120                ExtraSpaces);
14121   verifyFormat(
14122       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14123       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
14124       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
14125       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
14126       ExtraSpaces);
14127   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
14128   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
14129                ExtraSpaces);
14130   verifyFormat(
14131       "someFunction(OtherParam,\n"
14132       "             BracedList{ // comment 1 (Forcing interesting break)\n"
14133       "                         param1, param2,\n"
14134       "                         // comment 2\n"
14135       "                         param3, param4 });",
14136       ExtraSpaces);
14137   verifyFormat(
14138       "std::this_thread::sleep_for(\n"
14139       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
14140       ExtraSpaces);
14141   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
14142                "    aaaaaaa,\n"
14143                "    aaaaaaaaaa,\n"
14144                "    aaaaa,\n"
14145                "    aaaaaaaaaaaaaaa,\n"
14146                "    aaa,\n"
14147                "    aaaaaaaaaa,\n"
14148                "    a,\n"
14149                "    aaaaaaaaaaaaaaaaaaaaa,\n"
14150                "    aaaaaaaaaaaa,\n"
14151                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
14152                "    aaaaaaa,\n"
14153                "    a};");
14154   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
14155   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
14156   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
14157 
14158   // Avoid breaking between initializer/equal sign and opening brace
14159   ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
14160   verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
14161                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
14162                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
14163                "  { \"ccccccccccccccccccccc\", 2 }\n"
14164                "};",
14165                ExtraSpaces);
14166   verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
14167                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
14168                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
14169                "  { \"ccccccccccccccccccccc\", 2 }\n"
14170                "};",
14171                ExtraSpaces);
14172 
14173   FormatStyle SpaceBeforeBrace = getLLVMStyle();
14174   SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
14175   verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
14176   verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
14177 
14178   FormatStyle SpaceBetweenBraces = getLLVMStyle();
14179   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
14180   SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
14181   SpaceBetweenBraces.SpacesInParensOptions.Other = true;
14182   SpaceBetweenBraces.SpacesInSquareBrackets = true;
14183   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
14184   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
14185   verifyFormat("vector< int > x{ // comment 1\n"
14186                "                 1, 2, 3, 4 };",
14187                SpaceBetweenBraces);
14188   SpaceBetweenBraces.ColumnLimit = 20;
14189   verifyFormat("vector< int > x{\n"
14190                "    1, 2, 3, 4 };",
14191                "vector<int>x{1,2,3,4};", SpaceBetweenBraces);
14192   SpaceBetweenBraces.ColumnLimit = 24;
14193   verifyFormat("vector< int > x{ 1, 2,\n"
14194                "                 3, 4 };",
14195                "vector<int>x{1,2,3,4};", SpaceBetweenBraces);
14196   verifyFormat("vector< int > x{\n"
14197                "    1,\n"
14198                "    2,\n"
14199                "    3,\n"
14200                "    4,\n"
14201                "};",
14202                "vector<int>x{1,2,3,4,};", SpaceBetweenBraces);
14203   verifyFormat("vector< int > x{};", SpaceBetweenBraces);
14204   SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
14205   SpaceBetweenBraces.SpacesInParensOptions.InEmptyParentheses = true;
14206   verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
14207 }
14208 
14209 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
14210   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14211                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14212                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14213                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14214                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14215                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
14216   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
14217                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14218                "                 1, 22, 333, 4444, 55555, //\n"
14219                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14220                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
14221   verifyFormat(
14222       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
14223       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
14224       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
14225       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
14226       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
14227       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
14228       "                 7777777};");
14229   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14230                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14231                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
14232   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14233                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14234                "    // Separating comment.\n"
14235                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
14236   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14237                "    // Leading comment\n"
14238                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14239                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
14240   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14241                "                 1, 1, 1, 1};",
14242                getLLVMStyleWithColumns(39));
14243   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14244                "                 1, 1, 1, 1};",
14245                getLLVMStyleWithColumns(38));
14246   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
14247                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
14248                getLLVMStyleWithColumns(43));
14249   verifyFormat(
14250       "static unsigned SomeValues[10][3] = {\n"
14251       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
14252       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
14253   verifyFormat("static auto fields = new vector<string>{\n"
14254                "    \"aaaaaaaaaaaaa\",\n"
14255                "    \"aaaaaaaaaaaaa\",\n"
14256                "    \"aaaaaaaaaaaa\",\n"
14257                "    \"aaaaaaaaaaaaaa\",\n"
14258                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
14259                "    \"aaaaaaaaaaaa\",\n"
14260                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
14261                "};");
14262   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
14263   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
14264                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
14265                "                 3, cccccccccccccccccccccc};",
14266                getLLVMStyleWithColumns(60));
14267 
14268   // Trailing commas.
14269   verifyFormat("vector<int> x = {\n"
14270                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
14271                "};",
14272                getLLVMStyleWithColumns(39));
14273   verifyFormat("vector<int> x = {\n"
14274                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
14275                "};",
14276                getLLVMStyleWithColumns(39));
14277   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14278                "                 1, 1, 1, 1,\n"
14279                "                 /**/ /**/};",
14280                getLLVMStyleWithColumns(39));
14281 
14282   // Trailing comment in the first line.
14283   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
14284                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
14285                "    111111111,  222222222,  3333333333,  444444444,  //\n"
14286                "    11111111,   22222222,   333333333,   44444444};");
14287   // Trailing comment in the last line.
14288   verifyFormat("int aaaaa[] = {\n"
14289                "    1, 2, 3, // comment\n"
14290                "    4, 5, 6  // comment\n"
14291                "};");
14292 
14293   // With nested lists, we should either format one item per line or all nested
14294   // lists one on line.
14295   // FIXME: For some nested lists, we can do better.
14296   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
14297                "        {aaaaaaaaaaaaaaaaaaa},\n"
14298                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
14299                "        {aaaaaaaaaaaaaaaaa}};",
14300                getLLVMStyleWithColumns(60));
14301   verifyFormat(
14302       "SomeStruct my_struct_array = {\n"
14303       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
14304       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
14305       "    {aaa, aaa},\n"
14306       "    {aaa, aaa},\n"
14307       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
14308       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
14309       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
14310 
14311   // No column layout should be used here.
14312   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
14313                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
14314 
14315   verifyNoCrash("a<,");
14316 
14317   // No braced initializer here.
14318   verifyFormat("void f() {\n"
14319                "  struct Dummy {};\n"
14320                "  f(v);\n"
14321                "}");
14322   verifyFormat("void foo() {\n"
14323                "  { // asdf\n"
14324                "    {\n"
14325                "      int a;\n"
14326                "    }\n"
14327                "  }\n"
14328                "  {\n"
14329                "    {\n"
14330                "      int b;\n"
14331                "    }\n"
14332                "  }\n"
14333                "}");
14334   verifyFormat("namespace n {\n"
14335                "void foo() {\n"
14336                "  {\n"
14337                "    {\n"
14338                "      statement();\n"
14339                "      if (false) {\n"
14340                "      }\n"
14341                "    }\n"
14342                "  }\n"
14343                "  {\n"
14344                "  }\n"
14345                "}\n"
14346                "} // namespace n");
14347 
14348   // Long lists should be formatted in columns even if they are nested.
14349   verifyFormat(
14350       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14351       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14352       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14353       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14354       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14355       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
14356 
14357   // Allow "single-column" layout even if that violates the column limit. There
14358   // isn't going to be a better way.
14359   verifyFormat("std::vector<int> a = {\n"
14360                "    aaaaaaaa,\n"
14361                "    aaaaaaaa,\n"
14362                "    aaaaaaaa,\n"
14363                "    aaaaaaaa,\n"
14364                "    aaaaaaaaaa,\n"
14365                "    aaaaaaaa,\n"
14366                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
14367                getLLVMStyleWithColumns(30));
14368   verifyFormat("vector<int> aaaa = {\n"
14369                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14370                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14371                "    aaaaaa.aaaaaaa,\n"
14372                "    aaaaaa.aaaaaaa,\n"
14373                "    aaaaaa.aaaaaaa,\n"
14374                "    aaaaaa.aaaaaaa,\n"
14375                "};");
14376 
14377   // Don't create hanging lists.
14378   verifyFormat("someFunction(Param, {List1, List2,\n"
14379                "                     List3});",
14380                getLLVMStyleWithColumns(35));
14381   verifyFormat("someFunction(Param, Param,\n"
14382                "             {List1, List2,\n"
14383                "              List3});",
14384                getLLVMStyleWithColumns(35));
14385   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
14386                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
14387 
14388   // No possible column formats, don't want the optimal paths penalized.
14389   verifyFormat(
14390       "waarudo::unit desk = {\n"
14391       "    .s = \"desk\", .p = p, .b = [] { return w::r{3, 10} * w::m; }};");
14392   verifyFormat("SomeType something1([](const Input &i) -> Output { return "
14393                "Output{1, 2}; },\n"
14394                "                    [](const Input &i) -> Output { return "
14395                "Output{1, 2}; });");
14396   FormatStyle NoBinPacking = getLLVMStyle();
14397   NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
14398   verifyFormat("waarudo::unit desk = {\n"
14399                "    .s = \"desk\", .p = p, .b = [] { return w::r{3, 10, 1, 1, "
14400                "1, 1} * w::m; }};",
14401                NoBinPacking);
14402 }
14403 
14404 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
14405   FormatStyle DoNotMerge = getLLVMStyle();
14406   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
14407 
14408   verifyFormat("void f() { return 42; }");
14409   verifyFormat("void f() {\n"
14410                "  return 42;\n"
14411                "}",
14412                DoNotMerge);
14413   verifyFormat("void f() {\n"
14414                "  // Comment\n"
14415                "}");
14416   verifyFormat("{\n"
14417                "#error {\n"
14418                "  int a;\n"
14419                "}");
14420   verifyFormat("{\n"
14421                "  int a;\n"
14422                "#error {\n"
14423                "}");
14424   verifyFormat("void f() {} // comment");
14425   verifyFormat("void f() { int a; } // comment");
14426   verifyFormat("void f() {\n"
14427                "} // comment",
14428                DoNotMerge);
14429   verifyFormat("void f() {\n"
14430                "  int a;\n"
14431                "} // comment",
14432                DoNotMerge);
14433   verifyFormat("void f() {\n"
14434                "} // comment",
14435                getLLVMStyleWithColumns(15));
14436 
14437   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
14438   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
14439 
14440   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
14441   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
14442   verifyGoogleFormat("class C {\n"
14443                      "  C()\n"
14444                      "      : iiiiiiii(nullptr),\n"
14445                      "        kkkkkkk(nullptr),\n"
14446                      "        mmmmmmm(nullptr),\n"
14447                      "        nnnnnnn(nullptr) {}\n"
14448                      "};");
14449 
14450   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
14451   verifyFormat("A() : b(0) {}", "A():b(0){}", NoColumnLimit);
14452   verifyFormat("class C {\n"
14453                "  A() : b(0) {}\n"
14454                "};",
14455                "class C{A():b(0){}};", NoColumnLimit);
14456   verifyFormat("A()\n"
14457                "    : b(0) {\n"
14458                "}",
14459                "A()\n:b(0)\n{\n}", NoColumnLimit);
14460 
14461   FormatStyle NoColumnLimitWrapAfterFunction = NoColumnLimit;
14462   NoColumnLimitWrapAfterFunction.BreakBeforeBraces = FormatStyle::BS_Custom;
14463   NoColumnLimitWrapAfterFunction.BraceWrapping.AfterFunction = true;
14464   verifyFormat("class C {\n"
14465                "#pragma foo\n"
14466                "  int foo { return 0; }\n"
14467                "};",
14468                NoColumnLimitWrapAfterFunction);
14469   verifyFormat("class C {\n"
14470                "#pragma foo\n"
14471                "  void foo {}\n"
14472                "};",
14473                NoColumnLimitWrapAfterFunction);
14474 
14475   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
14476   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
14477       FormatStyle::SFS_None;
14478   verifyFormat("A()\n"
14479                "    : b(0) {\n"
14480                "}",
14481                "A():b(0){}", DoNotMergeNoColumnLimit);
14482   verifyFormat("A()\n"
14483                "    : b(0) {\n"
14484                "}",
14485                "A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit);
14486 
14487   verifyFormat("#define A          \\\n"
14488                "  void f() {       \\\n"
14489                "    int i;         \\\n"
14490                "  }",
14491                getLLVMStyleWithColumns(20));
14492   verifyFormat("#define A           \\\n"
14493                "  void f() { int i; }",
14494                getLLVMStyleWithColumns(21));
14495   verifyFormat("#define A            \\\n"
14496                "  void f() {         \\\n"
14497                "    int i;           \\\n"
14498                "  }                  \\\n"
14499                "  int j;",
14500                getLLVMStyleWithColumns(22));
14501   verifyFormat("#define A             \\\n"
14502                "  void f() { int i; } \\\n"
14503                "  int j;",
14504                getLLVMStyleWithColumns(23));
14505 
14506   verifyFormat(
14507       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
14508       "    aaaaaaaaaaaaaaaaaa,\n"
14509       "    aaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {}");
14510 
14511   constexpr StringRef Code{"void foo() { /* Empty */ }"};
14512   verifyFormat(Code);
14513   verifyFormat(Code, "void foo() { /* Empty */\n"
14514                      "}");
14515   verifyFormat(Code, "void foo() {\n"
14516                      "/* Empty */\n"
14517                      "}");
14518 }
14519 
14520 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
14521   FormatStyle MergeEmptyOnly = getLLVMStyle();
14522   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
14523   verifyFormat("class C {\n"
14524                "  int f() {}\n"
14525                "};",
14526                MergeEmptyOnly);
14527   verifyFormat("class C {\n"
14528                "  int f() {\n"
14529                "    return 42;\n"
14530                "  }\n"
14531                "};",
14532                MergeEmptyOnly);
14533   verifyFormat("int f() {}", MergeEmptyOnly);
14534   verifyFormat("int f() {\n"
14535                "  return 42;\n"
14536                "}",
14537                MergeEmptyOnly);
14538 
14539   // Also verify behavior when BraceWrapping.AfterFunction = true
14540   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
14541   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
14542   verifyFormat("int f() {}", MergeEmptyOnly);
14543   verifyFormat("class C {\n"
14544                "  int f() {}\n"
14545                "};",
14546                MergeEmptyOnly);
14547 }
14548 
14549 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
14550   FormatStyle MergeInlineOnly = getLLVMStyle();
14551   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
14552   verifyFormat("class C {\n"
14553                "  int f() { return 42; }\n"
14554                "};",
14555                MergeInlineOnly);
14556   verifyFormat("int f() {\n"
14557                "  return 42;\n"
14558                "}",
14559                MergeInlineOnly);
14560 
14561   // SFS_Inline implies SFS_Empty
14562   verifyFormat("class C {\n"
14563                "  int f() {}\n"
14564                "};",
14565                MergeInlineOnly);
14566   verifyFormat("int f() {}", MergeInlineOnly);
14567   // https://llvm.org/PR54147
14568   verifyFormat("auto lambda = []() {\n"
14569                "  // comment\n"
14570                "  f();\n"
14571                "  g();\n"
14572                "};",
14573                MergeInlineOnly);
14574 
14575   verifyFormat("class C {\n"
14576                "#ifdef A\n"
14577                "  int f() { return 42; }\n"
14578                "#endif\n"
14579                "};",
14580                MergeInlineOnly);
14581 
14582   verifyFormat("struct S {\n"
14583                "// comment\n"
14584                "#ifdef FOO\n"
14585                "  int foo() { bar(); }\n"
14586                "#endif\n"
14587                "};",
14588                MergeInlineOnly);
14589 
14590   // Also verify behavior when BraceWrapping.AfterFunction = true
14591   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
14592   MergeInlineOnly.BraceWrapping.AfterFunction = true;
14593   verifyFormat("class C {\n"
14594                "  int f() { return 42; }\n"
14595                "};",
14596                MergeInlineOnly);
14597   verifyFormat("int f()\n"
14598                "{\n"
14599                "  return 42;\n"
14600                "}",
14601                MergeInlineOnly);
14602 
14603   // SFS_Inline implies SFS_Empty
14604   verifyFormat("int f() {}", MergeInlineOnly);
14605   verifyFormat("class C {\n"
14606                "  int f() {}\n"
14607                "};",
14608                MergeInlineOnly);
14609 
14610   MergeInlineOnly.BraceWrapping.AfterClass = true;
14611   MergeInlineOnly.BraceWrapping.AfterStruct = true;
14612   verifyFormat("class C\n"
14613                "{\n"
14614                "  int f() { return 42; }\n"
14615                "};",
14616                MergeInlineOnly);
14617   verifyFormat("struct C\n"
14618                "{\n"
14619                "  int f() { return 42; }\n"
14620                "};",
14621                MergeInlineOnly);
14622   verifyFormat("int f()\n"
14623                "{\n"
14624                "  return 42;\n"
14625                "}",
14626                MergeInlineOnly);
14627   verifyFormat("int f() {}", MergeInlineOnly);
14628   verifyFormat("class C\n"
14629                "{\n"
14630                "  int f() { return 42; }\n"
14631                "};",
14632                MergeInlineOnly);
14633   verifyFormat("struct C\n"
14634                "{\n"
14635                "  int f() { return 42; }\n"
14636                "};",
14637                MergeInlineOnly);
14638   verifyFormat("struct C\n"
14639                "// comment\n"
14640                "/* comment */\n"
14641                "// comment\n"
14642                "{\n"
14643                "  int f() { return 42; }\n"
14644                "};",
14645                MergeInlineOnly);
14646   verifyFormat("/* comment */ struct C\n"
14647                "{\n"
14648                "  int f() { return 42; }\n"
14649                "};",
14650                MergeInlineOnly);
14651 }
14652 
14653 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
14654   FormatStyle MergeInlineOnly = getLLVMStyle();
14655   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
14656       FormatStyle::SFS_InlineOnly;
14657   verifyFormat("class C {\n"
14658                "  int f() { return 42; }\n"
14659                "};",
14660                MergeInlineOnly);
14661   verifyFormat("int f() {\n"
14662                "  return 42;\n"
14663                "}",
14664                MergeInlineOnly);
14665 
14666   // SFS_InlineOnly does not imply SFS_Empty
14667   verifyFormat("class C {\n"
14668                "  int f() {}\n"
14669                "};",
14670                MergeInlineOnly);
14671   verifyFormat("int f() {\n"
14672                "}",
14673                MergeInlineOnly);
14674 
14675   // Also verify behavior when BraceWrapping.AfterFunction = true
14676   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
14677   MergeInlineOnly.BraceWrapping.AfterFunction = true;
14678   verifyFormat("class C {\n"
14679                "  int f() { return 42; }\n"
14680                "};",
14681                MergeInlineOnly);
14682   verifyFormat("int f()\n"
14683                "{\n"
14684                "  return 42;\n"
14685                "}",
14686                MergeInlineOnly);
14687 
14688   // SFS_InlineOnly does not imply SFS_Empty
14689   verifyFormat("int f()\n"
14690                "{\n"
14691                "}",
14692                MergeInlineOnly);
14693   verifyFormat("class C {\n"
14694                "  int f() {}\n"
14695                "};",
14696                MergeInlineOnly);
14697 }
14698 
14699 TEST_F(FormatTest, SplitEmptyFunction) {
14700   FormatStyle Style = getLLVMStyleWithColumns(40);
14701   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
14702   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14703   Style.BraceWrapping.AfterFunction = true;
14704   Style.BraceWrapping.SplitEmptyFunction = false;
14705 
14706   verifyFormat("int f()\n"
14707                "{}",
14708                Style);
14709   verifyFormat("int f()\n"
14710                "{\n"
14711                "  return 42;\n"
14712                "}",
14713                Style);
14714   verifyFormat("int f()\n"
14715                "{\n"
14716                "  // some comment\n"
14717                "}",
14718                Style);
14719 
14720   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
14721   verifyFormat("int f() {}", Style);
14722   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14723                "{}",
14724                Style);
14725   verifyFormat("int f()\n"
14726                "{\n"
14727                "  return 0;\n"
14728                "}",
14729                Style);
14730 
14731   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
14732   verifyFormat("class Foo {\n"
14733                "  int f() {}\n"
14734                "};",
14735                Style);
14736   verifyFormat("class Foo {\n"
14737                "  int f() { return 0; }\n"
14738                "};",
14739                Style);
14740   verifyFormat("class Foo {\n"
14741                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14742                "  {}\n"
14743                "};",
14744                Style);
14745   verifyFormat("class Foo {\n"
14746                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14747                "  {\n"
14748                "    return 0;\n"
14749                "  }\n"
14750                "};",
14751                Style);
14752 
14753   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
14754   verifyFormat("int f() {}", Style);
14755   verifyFormat("int f() { return 0; }", Style);
14756   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14757                "{}",
14758                Style);
14759   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14760                "{\n"
14761                "  return 0;\n"
14762                "}",
14763                Style);
14764 }
14765 
14766 TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) {
14767   FormatStyle Style = getLLVMStyleWithColumns(40);
14768   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
14769   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14770   Style.BraceWrapping.AfterFunction = true;
14771   Style.BraceWrapping.SplitEmptyFunction = true;
14772   Style.BraceWrapping.SplitEmptyRecord = false;
14773 
14774   verifyFormat("class C {};", Style);
14775   verifyFormat("struct C {};", Style);
14776   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14777                "       int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
14778                "{\n"
14779                "}",
14780                Style);
14781   verifyFormat("class C {\n"
14782                "  C()\n"
14783                "      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n"
14784                "        bbbbbbbbbbbbbbbbbbb()\n"
14785                "  {\n"
14786                "  }\n"
14787                "  void\n"
14788                "  m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14789                "    int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
14790                "  {\n"
14791                "  }\n"
14792                "};",
14793                Style);
14794 }
14795 
14796 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
14797   FormatStyle Style = getLLVMStyle();
14798   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
14799   verifyFormat("#ifdef A\n"
14800                "int f() {}\n"
14801                "#else\n"
14802                "int g() {}\n"
14803                "#endif",
14804                Style);
14805 }
14806 
14807 TEST_F(FormatTest, SplitEmptyClass) {
14808   FormatStyle Style = getLLVMStyle();
14809   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14810   Style.BraceWrapping.AfterClass = true;
14811   Style.BraceWrapping.SplitEmptyRecord = false;
14812 
14813   verifyFormat("class Foo\n"
14814                "{};",
14815                Style);
14816   verifyFormat("/* something */ class Foo\n"
14817                "{};",
14818                Style);
14819   verifyFormat("template <typename X> class Foo\n"
14820                "{};",
14821                Style);
14822   verifyFormat("class Foo\n"
14823                "{\n"
14824                "  Foo();\n"
14825                "};",
14826                Style);
14827   verifyFormat("typedef class Foo\n"
14828                "{\n"
14829                "} Foo_t;",
14830                Style);
14831 
14832   Style.BraceWrapping.SplitEmptyRecord = true;
14833   Style.BraceWrapping.AfterStruct = true;
14834   verifyFormat("class rep\n"
14835                "{\n"
14836                "};",
14837                Style);
14838   verifyFormat("struct rep\n"
14839                "{\n"
14840                "};",
14841                Style);
14842   verifyFormat("template <typename T> class rep\n"
14843                "{\n"
14844                "};",
14845                Style);
14846   verifyFormat("template <typename T> struct rep\n"
14847                "{\n"
14848                "};",
14849                Style);
14850   verifyFormat("class rep\n"
14851                "{\n"
14852                "  int x;\n"
14853                "};",
14854                Style);
14855   verifyFormat("struct rep\n"
14856                "{\n"
14857                "  int x;\n"
14858                "};",
14859                Style);
14860   verifyFormat("template <typename T> class rep\n"
14861                "{\n"
14862                "  int x;\n"
14863                "};",
14864                Style);
14865   verifyFormat("template <typename T> struct rep\n"
14866                "{\n"
14867                "  int x;\n"
14868                "};",
14869                Style);
14870   verifyFormat("template <typename T> class rep // Foo\n"
14871                "{\n"
14872                "  int x;\n"
14873                "};",
14874                Style);
14875   verifyFormat("template <typename T> struct rep // Bar\n"
14876                "{\n"
14877                "  int x;\n"
14878                "};",
14879                Style);
14880 
14881   verifyFormat("template <typename T> class rep<T>\n"
14882                "{\n"
14883                "  int x;\n"
14884                "};",
14885                Style);
14886 
14887   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
14888                "{\n"
14889                "  int x;\n"
14890                "};",
14891                Style);
14892   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
14893                "{\n"
14894                "};",
14895                Style);
14896 
14897   verifyFormat("#include \"stdint.h\"\n"
14898                "namespace rep {}",
14899                Style);
14900   verifyFormat("#include <stdint.h>\n"
14901                "namespace rep {}",
14902                Style);
14903   verifyFormat("#include <stdint.h>\n"
14904                "namespace rep {}",
14905                "#include <stdint.h>\n"
14906                "namespace rep {\n"
14907                "\n"
14908                "\n"
14909                "}",
14910                Style);
14911 }
14912 
14913 TEST_F(FormatTest, SplitEmptyStruct) {
14914   FormatStyle Style = getLLVMStyle();
14915   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14916   Style.BraceWrapping.AfterStruct = true;
14917   Style.BraceWrapping.SplitEmptyRecord = false;
14918 
14919   verifyFormat("struct Foo\n"
14920                "{};",
14921                Style);
14922   verifyFormat("/* something */ struct Foo\n"
14923                "{};",
14924                Style);
14925   verifyFormat("template <typename X> struct Foo\n"
14926                "{};",
14927                Style);
14928   verifyFormat("struct Foo\n"
14929                "{\n"
14930                "  Foo();\n"
14931                "};",
14932                Style);
14933   verifyFormat("typedef struct Foo\n"
14934                "{\n"
14935                "} Foo_t;",
14936                Style);
14937   // typedef struct Bar {} Bar_t;
14938 }
14939 
14940 TEST_F(FormatTest, SplitEmptyUnion) {
14941   FormatStyle Style = getLLVMStyle();
14942   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14943   Style.BraceWrapping.AfterUnion = true;
14944   Style.BraceWrapping.SplitEmptyRecord = false;
14945 
14946   verifyFormat("union Foo\n"
14947                "{};",
14948                Style);
14949   verifyFormat("/* something */ union Foo\n"
14950                "{};",
14951                Style);
14952   verifyFormat("union Foo\n"
14953                "{\n"
14954                "  A,\n"
14955                "};",
14956                Style);
14957   verifyFormat("typedef union Foo\n"
14958                "{\n"
14959                "} Foo_t;",
14960                Style);
14961 }
14962 
14963 TEST_F(FormatTest, SplitEmptyNamespace) {
14964   FormatStyle Style = getLLVMStyle();
14965   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14966   Style.BraceWrapping.AfterNamespace = true;
14967   Style.BraceWrapping.SplitEmptyNamespace = false;
14968 
14969   verifyFormat("namespace Foo\n"
14970                "{};",
14971                Style);
14972   verifyFormat("/* something */ namespace Foo\n"
14973                "{};",
14974                Style);
14975   verifyFormat("inline namespace Foo\n"
14976                "{};",
14977                Style);
14978   verifyFormat("/* something */ inline namespace Foo\n"
14979                "{};",
14980                Style);
14981   verifyFormat("export namespace Foo\n"
14982                "{};",
14983                Style);
14984   verifyFormat("namespace Foo\n"
14985                "{\n"
14986                "void Bar();\n"
14987                "};",
14988                Style);
14989 }
14990 
14991 TEST_F(FormatTest, NeverMergeShortRecords) {
14992   FormatStyle Style = getLLVMStyle();
14993 
14994   verifyFormat("class Foo {\n"
14995                "  Foo();\n"
14996                "};",
14997                Style);
14998   verifyFormat("typedef class Foo {\n"
14999                "  Foo();\n"
15000                "} Foo_t;",
15001                Style);
15002   verifyFormat("struct Foo {\n"
15003                "  Foo();\n"
15004                "};",
15005                Style);
15006   verifyFormat("typedef struct Foo {\n"
15007                "  Foo();\n"
15008                "} Foo_t;",
15009                Style);
15010   verifyFormat("union Foo {\n"
15011                "  A,\n"
15012                "};",
15013                Style);
15014   verifyFormat("typedef union Foo {\n"
15015                "  A,\n"
15016                "} Foo_t;",
15017                Style);
15018   verifyFormat("namespace Foo {\n"
15019                "void Bar();\n"
15020                "};",
15021                Style);
15022 
15023   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
15024   Style.BraceWrapping.AfterClass = true;
15025   Style.BraceWrapping.AfterStruct = true;
15026   Style.BraceWrapping.AfterUnion = true;
15027   Style.BraceWrapping.AfterNamespace = true;
15028   verifyFormat("class Foo\n"
15029                "{\n"
15030                "  Foo();\n"
15031                "};",
15032                Style);
15033   verifyFormat("typedef class Foo\n"
15034                "{\n"
15035                "  Foo();\n"
15036                "} Foo_t;",
15037                Style);
15038   verifyFormat("struct Foo\n"
15039                "{\n"
15040                "  Foo();\n"
15041                "};",
15042                Style);
15043   verifyFormat("typedef struct Foo\n"
15044                "{\n"
15045                "  Foo();\n"
15046                "} Foo_t;",
15047                Style);
15048   verifyFormat("union Foo\n"
15049                "{\n"
15050                "  A,\n"
15051                "};",
15052                Style);
15053   verifyFormat("typedef union Foo\n"
15054                "{\n"
15055                "  A,\n"
15056                "} Foo_t;",
15057                Style);
15058   verifyFormat("namespace Foo\n"
15059                "{\n"
15060                "void Bar();\n"
15061                "};",
15062                Style);
15063 }
15064 
15065 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
15066   // Elaborate type variable declarations.
15067   verifyFormat("struct foo a = {bar};\nint n;");
15068   verifyFormat("class foo a = {bar};\nint n;");
15069   verifyFormat("union foo a = {bar};\nint n;");
15070 
15071   // Elaborate types inside function definitions.
15072   verifyFormat("struct foo f() {}\nint n;");
15073   verifyFormat("class foo f() {}\nint n;");
15074   verifyFormat("union foo f() {}\nint n;");
15075 
15076   // Templates.
15077   verifyFormat("template <class X> void f() {}\nint n;");
15078   verifyFormat("template <struct X> void f() {}\nint n;");
15079   verifyFormat("template <union X> void f() {}\nint n;");
15080 
15081   // Actual definitions...
15082   verifyFormat("struct {\n} n;");
15083   verifyFormat(
15084       "template <template <class T, class Y>, class Z> class X {\n} n;");
15085   verifyFormat("union Z {\n  int n;\n} x;");
15086   verifyFormat("class MACRO Z {\n} n;");
15087   verifyFormat("class MACRO(X) Z {\n} n;");
15088   verifyFormat("class __attribute__((X)) Z {\n} n;");
15089   verifyFormat("class __declspec(X) Z {\n} n;");
15090   verifyFormat("class A##B##C {\n} n;");
15091   verifyFormat("class alignas(16) Z {\n} n;");
15092   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
15093   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
15094 
15095   // Redefinition from nested context:
15096   verifyFormat("class A::B::C {\n} n;");
15097 
15098   // Template definitions.
15099   verifyFormat(
15100       "template <typename F>\n"
15101       "Matcher(const Matcher<F> &Other,\n"
15102       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
15103       "                             !is_same<F, T>::value>::type * = 0)\n"
15104       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
15105 
15106   // FIXME: This is still incorrectly handled at the formatter side.
15107   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
15108   verifyFormat("int i = SomeFunction(a<b, a> b);");
15109 
15110   verifyFormat("class A<int> f() {}\n"
15111                "int n;");
15112   verifyFormat("template <typename T> class A<T> f() {}\n"
15113                "int n;");
15114 
15115   verifyFormat("template <> class Foo<int> F() {\n"
15116                "} n;");
15117 
15118   // Elaborate types where incorrectly parsing the structural element would
15119   // break the indent.
15120   verifyFormat("if (true)\n"
15121                "  class X x;\n"
15122                "else\n"
15123                "  f();");
15124 
15125   // This is simply incomplete. Formatting is not important, but must not crash.
15126   verifyFormat("class A:");
15127 }
15128 
15129 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
15130   verifyNoChange("#error Leave     all         white!!!!! space* alone!");
15131   verifyNoChange("#warning Leave     all         white!!!!! space* alone!");
15132   verifyFormat("#error 1", "  #  error   1");
15133   verifyFormat("#warning 1", "  #  warning 1");
15134 }
15135 
15136 TEST_F(FormatTest, FormatHashIfExpressions) {
15137   verifyFormat("#if AAAA && BBBB");
15138   verifyFormat("#if (AAAA && BBBB)");
15139   verifyFormat("#elif (AAAA && BBBB)");
15140   // FIXME: Come up with a better indentation for #elif.
15141   verifyFormat(
15142       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
15143       "    defined(BBBBBBBB)\n"
15144       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
15145       "    defined(BBBBBBBB)\n"
15146       "#endif",
15147       getLLVMStyleWithColumns(65));
15148 }
15149 
15150 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
15151   FormatStyle AllowsMergedIf = getGoogleStyle();
15152   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
15153       FormatStyle::SIS_WithoutElse;
15154   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
15155   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
15156   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
15157   verifyFormat("if (true) return 42;", "if (true)\nreturn 42;", AllowsMergedIf);
15158   FormatStyle ShortMergedIf = AllowsMergedIf;
15159   ShortMergedIf.ColumnLimit = 25;
15160   verifyFormat("#define A \\\n"
15161                "  if (true) return 42;",
15162                ShortMergedIf);
15163   verifyFormat("#define A \\\n"
15164                "  f();    \\\n"
15165                "  if (true)\n"
15166                "#define B",
15167                ShortMergedIf);
15168   verifyFormat("#define A \\\n"
15169                "  f();    \\\n"
15170                "  if (true)\n"
15171                "g();",
15172                ShortMergedIf);
15173   verifyFormat("{\n"
15174                "#ifdef A\n"
15175                "  // Comment\n"
15176                "  if (true) continue;\n"
15177                "#endif\n"
15178                "  // Comment\n"
15179                "  if (true) continue;\n"
15180                "}",
15181                ShortMergedIf);
15182   ShortMergedIf.ColumnLimit = 33;
15183   verifyFormat("#define A \\\n"
15184                "  if constexpr (true) return 42;",
15185                ShortMergedIf);
15186   verifyFormat("#define A \\\n"
15187                "  if CONSTEXPR (true) return 42;",
15188                ShortMergedIf);
15189   ShortMergedIf.ColumnLimit = 29;
15190   verifyFormat("#define A                   \\\n"
15191                "  if (aaaaaaaaaa) return 1; \\\n"
15192                "  return 2;",
15193                ShortMergedIf);
15194   ShortMergedIf.ColumnLimit = 28;
15195   verifyFormat("#define A         \\\n"
15196                "  if (aaaaaaaaaa) \\\n"
15197                "    return 1;     \\\n"
15198                "  return 2;",
15199                ShortMergedIf);
15200   verifyFormat("#define A                \\\n"
15201                "  if constexpr (aaaaaaa) \\\n"
15202                "    return 1;            \\\n"
15203                "  return 2;",
15204                ShortMergedIf);
15205   verifyFormat("#define A                \\\n"
15206                "  if CONSTEXPR (aaaaaaa) \\\n"
15207                "    return 1;            \\\n"
15208                "  return 2;",
15209                ShortMergedIf);
15210 
15211   verifyFormat("//\n"
15212                "#define a \\\n"
15213                "  if      \\\n"
15214                "  0",
15215                getChromiumStyle(FormatStyle::LK_Cpp));
15216 }
15217 
15218 TEST_F(FormatTest, FormatStarDependingOnContext) {
15219   verifyFormat("void f(int *a);");
15220   verifyFormat("void f() { f(fint * b); }");
15221   verifyFormat("class A {\n  void f(int *a);\n};");
15222   verifyFormat("class A {\n  int *a;\n};");
15223   verifyFormat("namespace a {\n"
15224                "namespace b {\n"
15225                "class A {\n"
15226                "  void f() {}\n"
15227                "  int *a;\n"
15228                "};\n"
15229                "} // namespace b\n"
15230                "} // namespace a");
15231 }
15232 
15233 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
15234   verifyFormat("while");
15235   verifyFormat("operator");
15236 }
15237 
15238 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
15239   // This code would be painfully slow to format if we didn't skip it.
15240   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
15241                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15242                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15243                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15244                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15245                    "A(1, 1)\n"
15246                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
15247                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15248                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15249                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15250                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15251                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15252                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15253                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15254                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15255                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
15256   // Deeply nested part is untouched, rest is formatted.
15257   EXPECT_EQ(std::string("int i;") + Code + "int j;",
15258             format(std::string("int    i;") + Code + "int    j;",
15259                    getLLVMStyle(), SC_ExpectIncomplete));
15260 }
15261 
15262 //===----------------------------------------------------------------------===//
15263 // Objective-C tests.
15264 //===----------------------------------------------------------------------===//
15265 
15266 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
15267   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
15268   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject;",
15269                "-(NSUInteger)indexOfObject:(id)anObject;");
15270   verifyFormat("- (NSInteger)Mthod1;", "-(NSInteger)Mthod1;");
15271   verifyFormat("+ (id)Mthod2;", "+(id)Mthod2;");
15272   verifyFormat("- (NSInteger)Method3:(id)anObject;",
15273                "-(NSInteger)Method3:(id)anObject;");
15274   verifyFormat("- (NSInteger)Method4:(id)anObject;",
15275                "-(NSInteger)Method4:(id)anObject;");
15276   verifyFormat("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
15277                "-(NSInteger)Method5:(id)anObject:(id)AnotherObject;");
15278   verifyFormat("- (id)Method6:(id)A:(id)B:(id)C:(id)D;");
15279   verifyFormat("- (void)sendAction:(SEL)aSelector to:(id)anObject "
15280                "forAllCells:(BOOL)flag;");
15281 
15282   // Very long objectiveC method declaration.
15283   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
15284                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
15285   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
15286                "                    inRange:(NSRange)range\n"
15287                "                   outRange:(NSRange)out_range\n"
15288                "                  outRange1:(NSRange)out_range1\n"
15289                "                  outRange2:(NSRange)out_range2\n"
15290                "                  outRange3:(NSRange)out_range3\n"
15291                "                  outRange4:(NSRange)out_range4\n"
15292                "                  outRange5:(NSRange)out_range5\n"
15293                "                  outRange6:(NSRange)out_range6\n"
15294                "                  outRange7:(NSRange)out_range7\n"
15295                "                  outRange8:(NSRange)out_range8\n"
15296                "                  outRange9:(NSRange)out_range9;");
15297 
15298   // When the function name has to be wrapped.
15299   FormatStyle Style = getLLVMStyle();
15300   // ObjC ignores IndentWrappedFunctionNames when wrapping methods
15301   // and always indents instead.
15302   Style.IndentWrappedFunctionNames = false;
15303   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
15304                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
15305                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
15306                "}",
15307                Style);
15308   Style.IndentWrappedFunctionNames = true;
15309   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
15310                "    veryLooooooooooongName:(NSString)cccccccccccccc\n"
15311                "               anotherName:(NSString)dddddddddddddd {\n"
15312                "}",
15313                Style);
15314 
15315   verifyFormat("- (int)sum:(vector<int>)numbers;");
15316   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
15317   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
15318   // protocol lists (but not for template classes):
15319   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
15320 
15321   verifyFormat("- (int (*)())foo:(int (*)())f;");
15322   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
15323 
15324   // If there's no return type (very rare in practice!), LLVM and Google style
15325   // agree.
15326   verifyFormat("- foo;");
15327   verifyFormat("- foo:(int)f;");
15328   verifyGoogleFormat("- foo:(int)foo;");
15329 }
15330 
15331 TEST_F(FormatTest, BreaksStringLiterals) {
15332   // FIXME: unstable test case
15333   EXPECT_EQ("\"some text \"\n"
15334             "\"other\";",
15335             format("\"some text other\";", getLLVMStyleWithColumns(12)));
15336   // FIXME: unstable test case
15337   EXPECT_EQ("\"some text \"\n"
15338             "\"other\";",
15339             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
15340   verifyFormat("#define A  \\\n"
15341                "  \"some \"  \\\n"
15342                "  \"text \"  \\\n"
15343                "  \"other\";",
15344                "#define A \"some text other\";", getLLVMStyleWithColumns(12));
15345   verifyFormat("#define A  \\\n"
15346                "  \"so \"    \\\n"
15347                "  \"text \"  \\\n"
15348                "  \"other\";",
15349                "#define A \"so text other\";", getLLVMStyleWithColumns(12));
15350 
15351   verifyFormat("\"some text\"", getLLVMStyleWithColumns(1));
15352   verifyFormat("\"some text\"", getLLVMStyleWithColumns(11));
15353   // FIXME: unstable test case
15354   EXPECT_EQ("\"some \"\n"
15355             "\"text\"",
15356             format("\"some text\"", getLLVMStyleWithColumns(10)));
15357   // FIXME: unstable test case
15358   EXPECT_EQ("\"some \"\n"
15359             "\"text\"",
15360             format("\"some text\"", getLLVMStyleWithColumns(7)));
15361   // FIXME: unstable test case
15362   EXPECT_EQ("\"some\"\n"
15363             "\" tex\"\n"
15364             "\"t\"",
15365             format("\"some text\"", getLLVMStyleWithColumns(6)));
15366   // FIXME: unstable test case
15367   EXPECT_EQ("\"some\"\n"
15368             "\" tex\"\n"
15369             "\" and\"",
15370             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
15371   // FIXME: unstable test case
15372   EXPECT_EQ("\"some\"\n"
15373             "\"/tex\"\n"
15374             "\"/and\"",
15375             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
15376 
15377   verifyFormat("variable =\n"
15378                "    \"long string \"\n"
15379                "    \"literal\";",
15380                "variable = \"long string literal\";",
15381                getLLVMStyleWithColumns(20));
15382 
15383   verifyFormat("variable = f(\n"
15384                "    \"long string \"\n"
15385                "    \"literal\",\n"
15386                "    short,\n"
15387                "    loooooooooooooooooooong);",
15388                "variable = f(\"long string literal\", short, "
15389                "loooooooooooooooooooong);",
15390                getLLVMStyleWithColumns(20));
15391 
15392   verifyFormat("f(g(\"long string \"\n"
15393                "    \"literal\"),\n"
15394                "  b);",
15395                "f(g(\"long string literal\"), b);",
15396                getLLVMStyleWithColumns(20));
15397   verifyFormat("f(g(\"long string \"\n"
15398                "    \"literal\",\n"
15399                "    a),\n"
15400                "  b);",
15401                "f(g(\"long string literal\", a), b);",
15402                getLLVMStyleWithColumns(20));
15403   verifyFormat("f(\"one two\".split(\n"
15404                "    variable));",
15405                "f(\"one two\".split(variable));", getLLVMStyleWithColumns(20));
15406   verifyFormat("f(\"one two three four five six \"\n"
15407                "  \"seven\".split(\n"
15408                "      really_looooong_variable));",
15409                "f(\"one two three four five six seven\"."
15410                "split(really_looooong_variable));",
15411                getLLVMStyleWithColumns(33));
15412 
15413   verifyFormat("f(\"some \"\n"
15414                "  \"text\",\n"
15415                "  other);",
15416                "f(\"some text\", other);", getLLVMStyleWithColumns(10));
15417 
15418   // Only break as a last resort.
15419   verifyFormat(
15420       "aaaaaaaaaaaaaaaaaaaa(\n"
15421       "    aaaaaaaaaaaaaaaaaaaa,\n"
15422       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
15423 
15424   // FIXME: unstable test case
15425   EXPECT_EQ("\"splitmea\"\n"
15426             "\"trandomp\"\n"
15427             "\"oint\"",
15428             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
15429 
15430   // FIXME: unstable test case
15431   EXPECT_EQ("\"split/\"\n"
15432             "\"pathat/\"\n"
15433             "\"slashes\"",
15434             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
15435 
15436   // FIXME: unstable test case
15437   EXPECT_EQ("\"split/\"\n"
15438             "\"pathat/\"\n"
15439             "\"slashes\"",
15440             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
15441   // FIXME: unstable test case
15442   EXPECT_EQ("\"split at \"\n"
15443             "\"spaces/at/\"\n"
15444             "\"slashes.at.any$\"\n"
15445             "\"non-alphanumeric%\"\n"
15446             "\"1111111111characte\"\n"
15447             "\"rs\"",
15448             format("\"split at "
15449                    "spaces/at/"
15450                    "slashes.at."
15451                    "any$non-"
15452                    "alphanumeric%"
15453                    "1111111111characte"
15454                    "rs\"",
15455                    getLLVMStyleWithColumns(20)));
15456 
15457   // Verify that splitting the strings understands
15458   // Style::AlwaysBreakBeforeMultilineStrings.
15459   verifyFormat("aaaaaaaaaaaa(\n"
15460                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
15461                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
15462                "aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
15463                "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
15464                "aaaaaaaaaaaaaaaaaaaaaa\");",
15465                getGoogleStyle());
15466   verifyFormat("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
15467                "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
15468                "return \"aaaaaaaaaaaaaaaaaaaaaa "
15469                "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
15470                "aaaaaaaaaaaaaaaaaaaaaa\";",
15471                getGoogleStyle());
15472   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
15473                "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
15474                "llvm::outs() << "
15475                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
15476                "aaaaaaaaaaaaaaaaaaa\";");
15477   verifyFormat("ffff(\n"
15478                "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
15479                "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
15480                "ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
15481                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
15482                getGoogleStyle());
15483 
15484   FormatStyle Style = getLLVMStyleWithColumns(12);
15485   Style.BreakStringLiterals = false;
15486   verifyFormat("\"some text other\";", Style);
15487 
15488   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
15489   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15490   verifyFormat("#define A \\\n"
15491                "  \"some \" \\\n"
15492                "  \"text \" \\\n"
15493                "  \"other\";",
15494                "#define A \"some text other\";", AlignLeft);
15495 }
15496 
15497 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
15498   verifyFormat("C a = \"some more \"\n"
15499                "      \"text\";",
15500                "C a = \"some more text\";", getLLVMStyleWithColumns(18));
15501 }
15502 
15503 TEST_F(FormatTest, FullyRemoveEmptyLines) {
15504   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
15505   NoEmptyLines.MaxEmptyLinesToKeep = 0;
15506   verifyFormat("int i = a(b());", "int i=a(\n\n b(\n\n\n )\n\n);",
15507                NoEmptyLines);
15508 }
15509 
15510 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
15511   // FIXME: unstable test case
15512   EXPECT_EQ(
15513       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
15514       "(\n"
15515       "    \"x\t\");",
15516       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
15517              "aaaaaaa("
15518              "\"x\t\");"));
15519 }
15520 
15521 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
15522   // FIXME: unstable test case
15523   EXPECT_EQ(
15524       "u8\"utf8 string \"\n"
15525       "u8\"literal\";",
15526       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
15527   // FIXME: unstable test case
15528   EXPECT_EQ(
15529       "u\"utf16 string \"\n"
15530       "u\"literal\";",
15531       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
15532   // FIXME: unstable test case
15533   EXPECT_EQ(
15534       "U\"utf32 string \"\n"
15535       "U\"literal\";",
15536       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
15537   // FIXME: unstable test case
15538   EXPECT_EQ("L\"wide string \"\n"
15539             "L\"literal\";",
15540             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
15541   verifyFormat("@\"NSString \"\n"
15542                "@\"literal\";",
15543                "@\"NSString literal\";", getGoogleStyleWithColumns(19));
15544   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
15545 
15546   // This input makes clang-format try to split the incomplete unicode escape
15547   // sequence, which used to lead to a crasher.
15548   verifyNoCrash(
15549       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
15550       getLLVMStyleWithColumns(60));
15551 }
15552 
15553 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
15554   FormatStyle Style = getGoogleStyleWithColumns(15);
15555   verifyFormat("R\"x(raw literal)x\";", Style);
15556   verifyFormat("uR\"x(raw literal)x\";", Style);
15557   verifyFormat("LR\"x(raw literal)x\";", Style);
15558   verifyFormat("UR\"x(raw literal)x\";", Style);
15559   verifyFormat("u8R\"x(raw literal)x\";", Style);
15560 }
15561 
15562 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
15563   FormatStyle Style = getLLVMStyleWithColumns(20);
15564   // FIXME: unstable test case
15565   EXPECT_EQ(
15566       "_T(\"aaaaaaaaaaaaaa\")\n"
15567       "_T(\"aaaaaaaaaaaaaa\")\n"
15568       "_T(\"aaaaaaaaaaaa\")",
15569       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
15570   verifyFormat("f(x,\n"
15571                "  _T(\"aaaaaaaaaaaa\")\n"
15572                "  _T(\"aaa\"),\n"
15573                "  z);",
15574                "f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style);
15575 
15576   // FIXME: Handle embedded spaces in one iteration.
15577   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
15578   //            "_T(\"aaaaaaaaaaaaa\")\n"
15579   //            "_T(\"aaaaaaaaaaaaa\")\n"
15580   //            "_T(\"a\")",
15581   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
15582   //                   getLLVMStyleWithColumns(20)));
15583   verifyFormat("_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
15584                "  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style);
15585   verifyFormat("f(\n"
15586                "#if !TEST\n"
15587                "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
15588                "#endif\n"
15589                ");",
15590                "f(\n"
15591                "#if !TEST\n"
15592                "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
15593                "#endif\n"
15594                ");");
15595   verifyFormat("f(\n"
15596                "\n"
15597                "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
15598                "f(\n"
15599                "\n"
15600                "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));");
15601   // Regression test for accessing tokens past the end of a vector in the
15602   // TokenLexer.
15603   verifyNoCrash(R"(_T(
15604 "
15605 )
15606 )");
15607 }
15608 
15609 TEST_F(FormatTest, BreaksStringLiteralOperands) {
15610   // In a function call with two operands, the second can be broken with no line
15611   // break before it.
15612   verifyFormat("func(a, \"long long \"\n"
15613                "        \"long long\");",
15614                "func(a, \"long long long long\");",
15615                getLLVMStyleWithColumns(24));
15616   // In a function call with three operands, the second must be broken with a
15617   // line break before it.
15618   verifyFormat("func(a,\n"
15619                "     \"long long long \"\n"
15620                "     \"long\",\n"
15621                "     c);",
15622                "func(a, \"long long long long\", c);",
15623                getLLVMStyleWithColumns(24));
15624   // In a function call with three operands, the third must be broken with a
15625   // line break before it.
15626   verifyFormat("func(a, b,\n"
15627                "     \"long long long \"\n"
15628                "     \"long\");",
15629                "func(a, b, \"long long long long\");",
15630                getLLVMStyleWithColumns(24));
15631   // In a function call with three operands, both the second and the third must
15632   // be broken with a line break before them.
15633   verifyFormat("func(a,\n"
15634                "     \"long long long \"\n"
15635                "     \"long\",\n"
15636                "     \"long long long \"\n"
15637                "     \"long\");",
15638                "func(a, \"long long long long\", \"long long long long\");",
15639                getLLVMStyleWithColumns(24));
15640   // In a chain of << with two operands, the second can be broken with no line
15641   // break before it.
15642   verifyFormat("a << \"line line \"\n"
15643                "     \"line\";",
15644                "a << \"line line line\";", getLLVMStyleWithColumns(20));
15645   // In a chain of << with three operands, the second can be broken with no line
15646   // break before it.
15647   verifyFormat("abcde << \"line \"\n"
15648                "         \"line line\"\n"
15649                "      << c;",
15650                "abcde << \"line line line\" << c;",
15651                getLLVMStyleWithColumns(20));
15652   // In a chain of << with three operands, the third must be broken with a line
15653   // break before it.
15654   verifyFormat("a << b\n"
15655                "  << \"line line \"\n"
15656                "     \"line\";",
15657                "a << b << \"line line line\";", getLLVMStyleWithColumns(20));
15658   // In a chain of << with three operands, the second can be broken with no line
15659   // break before it and the third must be broken with a line break before it.
15660   verifyFormat("abcd << \"line line \"\n"
15661                "        \"line\"\n"
15662                "     << \"line line \"\n"
15663                "        \"line\";",
15664                "abcd << \"line line line\" << \"line line line\";",
15665                getLLVMStyleWithColumns(20));
15666   // In a chain of binary operators with two operands, the second can be broken
15667   // with no line break before it.
15668   verifyFormat("abcd + \"line line \"\n"
15669                "       \"line line\";",
15670                "abcd + \"line line line line\";", getLLVMStyleWithColumns(20));
15671   // In a chain of binary operators with three operands, the second must be
15672   // broken with a line break before it.
15673   verifyFormat("abcd +\n"
15674                "    \"line line \"\n"
15675                "    \"line line\" +\n"
15676                "    e;",
15677                "abcd + \"line line line line\" + e;",
15678                getLLVMStyleWithColumns(20));
15679   // In a function call with two operands, with AlignAfterOpenBracket enabled,
15680   // the first must be broken with a line break before it.
15681   FormatStyle Style = getLLVMStyleWithColumns(25);
15682   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
15683   verifyFormat("someFunction(\n"
15684                "    \"long long long \"\n"
15685                "    \"long\",\n"
15686                "    a);",
15687                "someFunction(\"long long long long\", a);", Style);
15688   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
15689   verifyFormat("someFunction(\n"
15690                "    \"long long long \"\n"
15691                "    \"long\",\n"
15692                "    a\n"
15693                ");",
15694                Style);
15695 }
15696 
15697 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
15698   verifyFormat("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15699                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15700                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
15701                "aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15702                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15703                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";");
15704 }
15705 
15706 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
15707   verifyFormat("f(g(R\"x(raw literal)x\", a), b);",
15708                "f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle());
15709   verifyFormat("fffffffffff(g(R\"x(\n"
15710                "multiline raw string literal xxxxxxxxxxxxxx\n"
15711                ")x\",\n"
15712                "              a),\n"
15713                "            b);",
15714                "fffffffffff(g(R\"x(\n"
15715                "multiline raw string literal xxxxxxxxxxxxxx\n"
15716                ")x\", a), b);",
15717                getGoogleStyleWithColumns(20));
15718   verifyFormat("fffffffffff(\n"
15719                "    g(R\"x(qqq\n"
15720                "multiline raw string literal xxxxxxxxxxxxxx\n"
15721                ")x\",\n"
15722                "      a),\n"
15723                "    b);",
15724                "fffffffffff(g(R\"x(qqq\n"
15725                "multiline raw string literal xxxxxxxxxxxxxx\n"
15726                ")x\", a), b);",
15727                getGoogleStyleWithColumns(20));
15728 
15729   verifyNoChange("fffffffffff(R\"x(\n"
15730                  "multiline raw string literal xxxxxxxxxxxxxx\n"
15731                  ")x\");",
15732                  getGoogleStyleWithColumns(20));
15733   verifyFormat("fffffffffff(R\"x(\n"
15734                "multiline raw string literal xxxxxxxxxxxxxx\n"
15735                ")x\" + bbbbbb);",
15736                "fffffffffff(R\"x(\n"
15737                "multiline raw string literal xxxxxxxxxxxxxx\n"
15738                ")x\" +   bbbbbb);",
15739                getGoogleStyleWithColumns(20));
15740   verifyFormat("fffffffffff(\n"
15741                "    R\"x(\n"
15742                "multiline raw string literal xxxxxxxxxxxxxx\n"
15743                ")x\" +\n"
15744                "    bbbbbb);",
15745                "fffffffffff(\n"
15746                " R\"x(\n"
15747                "multiline raw string literal xxxxxxxxxxxxxx\n"
15748                ")x\" + bbbbbb);",
15749                getGoogleStyleWithColumns(20));
15750   verifyFormat("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
15751                "fffffffffff(\n"
15752                " R\"(single line raw string)\" + bbbbbb);");
15753 }
15754 
15755 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
15756   verifyFormat("string a = \"unterminated;");
15757   verifyFormat("function(\"unterminated,\n"
15758                "         OtherParameter);",
15759                "function(  \"unterminated,\n"
15760                "    OtherParameter);");
15761 }
15762 
15763 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
15764   FormatStyle Style = getLLVMStyle();
15765   Style.Standard = FormatStyle::LS_Cpp03;
15766   verifyFormat("#define x(_a) printf(\"foo\" _a);",
15767                "#define x(_a) printf(\"foo\"_a);", Style);
15768 }
15769 
15770 TEST_F(FormatTest, CppLexVersion) {
15771   FormatStyle Style = getLLVMStyle();
15772   // Formatting of x * y differs if x is a type.
15773   verifyFormat("void foo() { MACRO(a * b); }", Style);
15774   verifyFormat("void foo() { MACRO(int *b); }", Style);
15775 
15776   // LLVM style uses latest lexer.
15777   verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
15778   Style.Standard = FormatStyle::LS_Cpp17;
15779   // But in c++17, char8_t isn't a keyword.
15780   verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
15781 }
15782 
15783 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
15784 
15785 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
15786   verifyFormat("someFunction(\"aaabbbcccd\"\n"
15787                "             \"ddeeefff\");",
15788                "someFunction(\"aaabbbcccdddeeefff\");",
15789                getLLVMStyleWithColumns(25));
15790   verifyFormat("someFunction1234567890(\n"
15791                "    \"aaabbbcccdddeeefff\");",
15792                "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15793                getLLVMStyleWithColumns(26));
15794   verifyFormat("someFunction1234567890(\n"
15795                "    \"aaabbbcccdddeeeff\"\n"
15796                "    \"f\");",
15797                "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15798                getLLVMStyleWithColumns(25));
15799   verifyFormat("someFunction1234567890(\n"
15800                "    \"aaabbbcccdddeeeff\"\n"
15801                "    \"f\");",
15802                "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15803                getLLVMStyleWithColumns(24));
15804   verifyFormat("someFunction(\n"
15805                "    \"aaabbbcc ddde \"\n"
15806                "    \"efff\");",
15807                "someFunction(\"aaabbbcc ddde efff\");",
15808                getLLVMStyleWithColumns(25));
15809   verifyFormat("someFunction(\"aaabbbccc \"\n"
15810                "             \"ddeeefff\");",
15811                "someFunction(\"aaabbbccc ddeeefff\");",
15812                getLLVMStyleWithColumns(25));
15813   verifyFormat("someFunction1234567890(\n"
15814                "    \"aaabb \"\n"
15815                "    \"cccdddeeefff\");",
15816                "someFunction1234567890(\"aaabb cccdddeeefff\");",
15817                getLLVMStyleWithColumns(25));
15818   verifyFormat("#define A          \\\n"
15819                "  string s =       \\\n"
15820                "      \"123456789\"  \\\n"
15821                "      \"0\";         \\\n"
15822                "  int i;",
15823                "#define A string s = \"1234567890\"; int i;",
15824                getLLVMStyleWithColumns(20));
15825   verifyFormat("someFunction(\n"
15826                "    \"aaabbbcc \"\n"
15827                "    \"dddeeefff\");",
15828                "someFunction(\"aaabbbcc dddeeefff\");",
15829                getLLVMStyleWithColumns(25));
15830 }
15831 
15832 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
15833   verifyFormat("\"\\a\"", getLLVMStyleWithColumns(3));
15834   verifyFormat("\"\\\"", getLLVMStyleWithColumns(2));
15835   // FIXME: unstable test case
15836   EXPECT_EQ("\"test\"\n"
15837             "\"\\n\"",
15838             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
15839   // FIXME: unstable test case
15840   EXPECT_EQ("\"tes\\\\\"\n"
15841             "\"n\"",
15842             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
15843   // FIXME: unstable test case
15844   EXPECT_EQ("\"\\\\\\\\\"\n"
15845             "\"\\n\"",
15846             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
15847   verifyFormat("\"\\uff01\"", getLLVMStyleWithColumns(7));
15848   // FIXME: unstable test case
15849   EXPECT_EQ("\"\\uff01\"\n"
15850             "\"test\"",
15851             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
15852   verifyFormat("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11));
15853   // FIXME: unstable test case
15854   EXPECT_EQ("\"\\x000000000001\"\n"
15855             "\"next\"",
15856             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
15857   verifyFormat("\"\\x000000000001next\"", getLLVMStyleWithColumns(15));
15858   verifyFormat("\"\\x000000000001\"", getLLVMStyleWithColumns(7));
15859   // FIXME: unstable test case
15860   EXPECT_EQ("\"test\"\n"
15861             "\"\\000000\"\n"
15862             "\"000001\"",
15863             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
15864   // FIXME: unstable test case
15865   EXPECT_EQ("\"test\\000\"\n"
15866             "\"00000000\"\n"
15867             "\"1\"",
15868             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
15869 }
15870 
15871 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
15872   verifyFormat("void f() {\n"
15873                "  return g() {}\n"
15874                "  void h() {}");
15875   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
15876                "g();\n"
15877                "}");
15878 }
15879 
15880 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
15881   verifyFormat(
15882       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
15883 }
15884 
15885 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
15886   verifyFormat("class X {\n"
15887                "  void f() {\n"
15888                "  }\n"
15889                "};",
15890                getLLVMStyleWithColumns(12));
15891 }
15892 
15893 TEST_F(FormatTest, ConfigurableIndentWidth) {
15894   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
15895   EightIndent.IndentWidth = 8;
15896   EightIndent.ContinuationIndentWidth = 8;
15897   verifyFormat("void f() {\n"
15898                "        someFunction();\n"
15899                "        if (true) {\n"
15900                "                f();\n"
15901                "        }\n"
15902                "}",
15903                EightIndent);
15904   verifyFormat("class X {\n"
15905                "        void f() {\n"
15906                "        }\n"
15907                "};",
15908                EightIndent);
15909   verifyFormat("int x[] = {\n"
15910                "        call(),\n"
15911                "        call()};",
15912                EightIndent);
15913 }
15914 
15915 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
15916   verifyFormat("double\n"
15917                "f();",
15918                getLLVMStyleWithColumns(8));
15919 }
15920 
15921 TEST_F(FormatTest, ConfigurableUseOfTab) {
15922   FormatStyle Tab = getLLVMStyleWithColumns(42);
15923   Tab.IndentWidth = 8;
15924   Tab.UseTab = FormatStyle::UT_Always;
15925   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15926 
15927   verifyFormat("if (aaaaaaaa && // q\n"
15928                "    bb)\t\t// w\n"
15929                "\t;",
15930                "if (aaaaaaaa &&// q\n"
15931                "bb)// w\n"
15932                ";",
15933                Tab);
15934   verifyFormat("if (aaa && bbb) // w\n"
15935                "\t;",
15936                "if(aaa&&bbb)// w\n"
15937                ";",
15938                Tab);
15939 
15940   verifyFormat("class X {\n"
15941                "\tvoid f() {\n"
15942                "\t\tsomeFunction(parameter1,\n"
15943                "\t\t\t     parameter2);\n"
15944                "\t}\n"
15945                "};",
15946                Tab);
15947   verifyFormat("#define A                        \\\n"
15948                "\tvoid f() {               \\\n"
15949                "\t\tsomeFunction(    \\\n"
15950                "\t\t    parameter1,  \\\n"
15951                "\t\t    parameter2); \\\n"
15952                "\t}",
15953                Tab);
15954   verifyFormat("int a;\t      // x\n"
15955                "int bbbbbbbb; // x",
15956                Tab);
15957 
15958   FormatStyle TabAlignment = Tab;
15959   TabAlignment.AlignConsecutiveDeclarations.Enabled = true;
15960   TabAlignment.PointerAlignment = FormatStyle::PAS_Left;
15961   verifyFormat("unsigned long long big;\n"
15962                "char*\t\t   ptr;",
15963                TabAlignment);
15964   TabAlignment.PointerAlignment = FormatStyle::PAS_Middle;
15965   verifyFormat("unsigned long long big;\n"
15966                "char *\t\t   ptr;",
15967                TabAlignment);
15968   TabAlignment.PointerAlignment = FormatStyle::PAS_Right;
15969   verifyFormat("unsigned long long big;\n"
15970                "char\t\t  *ptr;",
15971                TabAlignment);
15972 
15973   Tab.TabWidth = 4;
15974   Tab.IndentWidth = 8;
15975   verifyFormat("class TabWidth4Indent8 {\n"
15976                "\t\tvoid f() {\n"
15977                "\t\t\t\tsomeFunction(parameter1,\n"
15978                "\t\t\t\t\t\t\t parameter2);\n"
15979                "\t\t}\n"
15980                "};",
15981                Tab);
15982 
15983   Tab.TabWidth = 4;
15984   Tab.IndentWidth = 4;
15985   verifyFormat("class TabWidth4Indent4 {\n"
15986                "\tvoid f() {\n"
15987                "\t\tsomeFunction(parameter1,\n"
15988                "\t\t\t\t\t parameter2);\n"
15989                "\t}\n"
15990                "};",
15991                Tab);
15992 
15993   Tab.TabWidth = 8;
15994   Tab.IndentWidth = 4;
15995   verifyFormat("class TabWidth8Indent4 {\n"
15996                "    void f() {\n"
15997                "\tsomeFunction(parameter1,\n"
15998                "\t\t     parameter2);\n"
15999                "    }\n"
16000                "};",
16001                Tab);
16002 
16003   Tab.TabWidth = 8;
16004   Tab.IndentWidth = 8;
16005   verifyFormat("/*\n"
16006                "\t      a\t\tcomment\n"
16007                "\t      in multiple lines\n"
16008                "       */",
16009                "   /*\t \t \n"
16010                " \t \t a\t\tcomment\t \t\n"
16011                " \t \t in multiple lines\t\n"
16012                " \t  */",
16013                Tab);
16014 
16015   TabAlignment.UseTab = FormatStyle::UT_ForIndentation;
16016   TabAlignment.PointerAlignment = FormatStyle::PAS_Left;
16017   verifyFormat("void f() {\n"
16018                "\tunsigned long long big;\n"
16019                "\tchar*              ptr;\n"
16020                "}",
16021                TabAlignment);
16022   TabAlignment.PointerAlignment = FormatStyle::PAS_Middle;
16023   verifyFormat("void f() {\n"
16024                "\tunsigned long long big;\n"
16025                "\tchar *             ptr;\n"
16026                "}",
16027                TabAlignment);
16028   TabAlignment.PointerAlignment = FormatStyle::PAS_Right;
16029   verifyFormat("void f() {\n"
16030                "\tunsigned long long big;\n"
16031                "\tchar              *ptr;\n"
16032                "}",
16033                TabAlignment);
16034 
16035   Tab.UseTab = FormatStyle::UT_ForIndentation;
16036   verifyFormat("{\n"
16037                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16038                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16039                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16040                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16041                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16042                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16043                "};",
16044                Tab);
16045   verifyFormat("enum AA {\n"
16046                "\ta1, // Force multiple lines\n"
16047                "\ta2,\n"
16048                "\ta3\n"
16049                "};",
16050                Tab);
16051   verifyFormat("if (aaaaaaaa && // q\n"
16052                "    bb)         // w\n"
16053                "\t;",
16054                "if (aaaaaaaa &&// q\n"
16055                "bb)// w\n"
16056                ";",
16057                Tab);
16058   verifyFormat("class X {\n"
16059                "\tvoid f() {\n"
16060                "\t\tsomeFunction(parameter1,\n"
16061                "\t\t             parameter2);\n"
16062                "\t}\n"
16063                "};",
16064                Tab);
16065   verifyFormat("{\n"
16066                "\tQ(\n"
16067                "\t    {\n"
16068                "\t\t    int a;\n"
16069                "\t\t    someFunction(aaaaaaaa,\n"
16070                "\t\t                 bbbbbbb);\n"
16071                "\t    },\n"
16072                "\t    p);\n"
16073                "}",
16074                Tab);
16075   verifyFormat("{\n"
16076                "\t/* aaaa\n"
16077                "\t   bbbb */\n"
16078                "}",
16079                "{\n"
16080                "/* aaaa\n"
16081                "   bbbb */\n"
16082                "}",
16083                Tab);
16084   verifyFormat("{\n"
16085                "\t/*\n"
16086                "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16087                "\t  bbbbbbbbbbbbb\n"
16088                "\t*/\n"
16089                "}",
16090                "{\n"
16091                "/*\n"
16092                "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16093                "*/\n"
16094                "}",
16095                Tab);
16096   verifyFormat("{\n"
16097                "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16098                "\t// bbbbbbbbbbbbb\n"
16099                "}",
16100                "{\n"
16101                "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16102                "}",
16103                Tab);
16104   verifyFormat("{\n"
16105                "\t/*\n"
16106                "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16107                "\t  bbbbbbbbbbbbb\n"
16108                "\t*/\n"
16109                "}",
16110                "{\n"
16111                "\t/*\n"
16112                "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16113                "\t*/\n"
16114                "}",
16115                Tab);
16116   verifyNoChange("{\n"
16117                  "\t/*\n"
16118                  "\n"
16119                  "\t*/\n"
16120                  "}",
16121                  Tab);
16122   verifyNoChange("{\n"
16123                  "\t/*\n"
16124                  " asdf\n"
16125                  "\t*/\n"
16126                  "}",
16127                  Tab);
16128 
16129   verifyFormat("void f() {\n"
16130                "\treturn true ? aaaaaaaaaaaaaaaaaa\n"
16131                "\t            : bbbbbbbbbbbbbbbbbb\n"
16132                "}",
16133                Tab);
16134   FormatStyle TabNoBreak = Tab;
16135   TabNoBreak.BreakBeforeTernaryOperators = false;
16136   verifyFormat("void f() {\n"
16137                "\treturn true ? aaaaaaaaaaaaaaaaaa :\n"
16138                "\t              bbbbbbbbbbbbbbbbbb\n"
16139                "}",
16140                TabNoBreak);
16141   verifyFormat("void f() {\n"
16142                "\treturn true ?\n"
16143                "\t           aaaaaaaaaaaaaaaaaaaa :\n"
16144                "\t           bbbbbbbbbbbbbbbbbbbb\n"
16145                "}",
16146                TabNoBreak);
16147 
16148   Tab.UseTab = FormatStyle::UT_Never;
16149   verifyFormat("/*\n"
16150                "              a\t\tcomment\n"
16151                "              in multiple lines\n"
16152                "       */",
16153                "   /*\t \t \n"
16154                " \t \t a\t\tcomment\t \t\n"
16155                " \t \t in multiple lines\t\n"
16156                " \t  */",
16157                Tab);
16158   verifyFormat("/* some\n"
16159                "   comment */",
16160                " \t \t /* some\n"
16161                " \t \t    comment */",
16162                Tab);
16163   verifyFormat("int a; /* some\n"
16164                "   comment */",
16165                " \t \t int a; /* some\n"
16166                " \t \t    comment */",
16167                Tab);
16168 
16169   verifyFormat("int a; /* some\n"
16170                "comment */",
16171                " \t \t int\ta; /* some\n"
16172                " \t \t    comment */",
16173                Tab);
16174   verifyFormat("f(\"\t\t\"); /* some\n"
16175                "    comment */",
16176                " \t \t f(\"\t\t\"); /* some\n"
16177                " \t \t    comment */",
16178                Tab);
16179   verifyFormat("{\n"
16180                "        /*\n"
16181                "         * Comment\n"
16182                "         */\n"
16183                "        int i;\n"
16184                "}",
16185                "{\n"
16186                "\t/*\n"
16187                "\t * Comment\n"
16188                "\t */\n"
16189                "\t int i;\n"
16190                "}",
16191                Tab);
16192 
16193   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
16194   Tab.TabWidth = 8;
16195   Tab.IndentWidth = 8;
16196   verifyFormat("if (aaaaaaaa && // q\n"
16197                "    bb)         // w\n"
16198                "\t;",
16199                "if (aaaaaaaa &&// q\n"
16200                "bb)// w\n"
16201                ";",
16202                Tab);
16203   verifyFormat("if (aaa && bbb) // w\n"
16204                "\t;",
16205                "if(aaa&&bbb)// w\n"
16206                ";",
16207                Tab);
16208   verifyFormat("class X {\n"
16209                "\tvoid f() {\n"
16210                "\t\tsomeFunction(parameter1,\n"
16211                "\t\t\t     parameter2);\n"
16212                "\t}\n"
16213                "};",
16214                Tab);
16215   verifyFormat("#define A                        \\\n"
16216                "\tvoid f() {               \\\n"
16217                "\t\tsomeFunction(    \\\n"
16218                "\t\t    parameter1,  \\\n"
16219                "\t\t    parameter2); \\\n"
16220                "\t}",
16221                Tab);
16222   Tab.TabWidth = 4;
16223   Tab.IndentWidth = 8;
16224   verifyFormat("class TabWidth4Indent8 {\n"
16225                "\t\tvoid f() {\n"
16226                "\t\t\t\tsomeFunction(parameter1,\n"
16227                "\t\t\t\t\t\t\t parameter2);\n"
16228                "\t\t}\n"
16229                "};",
16230                Tab);
16231   Tab.TabWidth = 4;
16232   Tab.IndentWidth = 4;
16233   verifyFormat("class TabWidth4Indent4 {\n"
16234                "\tvoid f() {\n"
16235                "\t\tsomeFunction(parameter1,\n"
16236                "\t\t\t\t\t parameter2);\n"
16237                "\t}\n"
16238                "};",
16239                Tab);
16240   Tab.TabWidth = 8;
16241   Tab.IndentWidth = 4;
16242   verifyFormat("class TabWidth8Indent4 {\n"
16243                "    void f() {\n"
16244                "\tsomeFunction(parameter1,\n"
16245                "\t\t     parameter2);\n"
16246                "    }\n"
16247                "};",
16248                Tab);
16249   Tab.TabWidth = 8;
16250   Tab.IndentWidth = 8;
16251   verifyFormat("/*\n"
16252                "\t      a\t\tcomment\n"
16253                "\t      in multiple lines\n"
16254                "       */",
16255                "   /*\t \t \n"
16256                " \t \t a\t\tcomment\t \t\n"
16257                " \t \t in multiple lines\t\n"
16258                " \t  */",
16259                Tab);
16260   verifyFormat("{\n"
16261                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16262                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16263                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16264                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16265                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16266                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16267                "};",
16268                Tab);
16269   verifyFormat("enum AA {\n"
16270                "\ta1, // Force multiple lines\n"
16271                "\ta2,\n"
16272                "\ta3\n"
16273                "};",
16274                Tab);
16275   verifyFormat("if (aaaaaaaa && // q\n"
16276                "    bb)         // w\n"
16277                "\t;",
16278                "if (aaaaaaaa &&// q\n"
16279                "bb)// w\n"
16280                ";",
16281                Tab);
16282   verifyFormat("class X {\n"
16283                "\tvoid f() {\n"
16284                "\t\tsomeFunction(parameter1,\n"
16285                "\t\t\t     parameter2);\n"
16286                "\t}\n"
16287                "};",
16288                Tab);
16289   verifyFormat("{\n"
16290                "\tQ(\n"
16291                "\t    {\n"
16292                "\t\t    int a;\n"
16293                "\t\t    someFunction(aaaaaaaa,\n"
16294                "\t\t\t\t bbbbbbb);\n"
16295                "\t    },\n"
16296                "\t    p);\n"
16297                "}",
16298                Tab);
16299   verifyFormat("{\n"
16300                "\t/* aaaa\n"
16301                "\t   bbbb */\n"
16302                "}",
16303                "{\n"
16304                "/* aaaa\n"
16305                "   bbbb */\n"
16306                "}",
16307                Tab);
16308   verifyFormat("{\n"
16309                "\t/*\n"
16310                "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16311                "\t  bbbbbbbbbbbbb\n"
16312                "\t*/\n"
16313                "}",
16314                "{\n"
16315                "/*\n"
16316                "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16317                "*/\n"
16318                "}",
16319                Tab);
16320   verifyFormat("{\n"
16321                "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16322                "\t// bbbbbbbbbbbbb\n"
16323                "}",
16324                "{\n"
16325                "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16326                "}",
16327                Tab);
16328   verifyFormat("{\n"
16329                "\t/*\n"
16330                "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16331                "\t  bbbbbbbbbbbbb\n"
16332                "\t*/\n"
16333                "}",
16334                "{\n"
16335                "\t/*\n"
16336                "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16337                "\t*/\n"
16338                "}",
16339                Tab);
16340   verifyNoChange("{\n"
16341                  "\t/*\n"
16342                  "\n"
16343                  "\t*/\n"
16344                  "}",
16345                  Tab);
16346   verifyNoChange("{\n"
16347                  "\t/*\n"
16348                  " asdf\n"
16349                  "\t*/\n"
16350                  "}",
16351                  Tab);
16352   verifyFormat("/* some\n"
16353                "   comment */",
16354                " \t \t /* some\n"
16355                " \t \t    comment */",
16356                Tab);
16357   verifyFormat("int a; /* some\n"
16358                "   comment */",
16359                " \t \t int a; /* some\n"
16360                " \t \t    comment */",
16361                Tab);
16362   verifyFormat("int a; /* some\n"
16363                "comment */",
16364                " \t \t int\ta; /* some\n"
16365                " \t \t    comment */",
16366                Tab);
16367   verifyFormat("f(\"\t\t\"); /* some\n"
16368                "    comment */",
16369                " \t \t f(\"\t\t\"); /* some\n"
16370                " \t \t    comment */",
16371                Tab);
16372   verifyFormat("{\n"
16373                "\t/*\n"
16374                "\t * Comment\n"
16375                "\t */\n"
16376                "\tint i;\n"
16377                "}",
16378                "{\n"
16379                "\t/*\n"
16380                "\t * Comment\n"
16381                "\t */\n"
16382                "\t int i;\n"
16383                "}",
16384                Tab);
16385   Tab.TabWidth = 2;
16386   Tab.IndentWidth = 2;
16387   verifyFormat("{\n"
16388                "\t/* aaaa\n"
16389                "\t\t bbbb */\n"
16390                "}",
16391                "{\n"
16392                "/* aaaa\n"
16393                "\t bbbb */\n"
16394                "}",
16395                Tab);
16396   verifyFormat("{\n"
16397                "\t/*\n"
16398                "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16399                "\t\tbbbbbbbbbbbbb\n"
16400                "\t*/\n"
16401                "}",
16402                "{\n"
16403                "/*\n"
16404                "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16405                "*/\n"
16406                "}",
16407                Tab);
16408   Tab.AlignConsecutiveAssignments.Enabled = true;
16409   Tab.AlignConsecutiveDeclarations.Enabled = true;
16410   Tab.TabWidth = 4;
16411   Tab.IndentWidth = 4;
16412   verifyFormat("class Assign {\n"
16413                "\tvoid f() {\n"
16414                "\t\tint         x      = 123;\n"
16415                "\t\tint         random = 4;\n"
16416                "\t\tstd::string alphabet =\n"
16417                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
16418                "\t}\n"
16419                "};",
16420                Tab);
16421 
16422   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
16423   Tab.TabWidth = 8;
16424   Tab.IndentWidth = 8;
16425   verifyFormat("if (aaaaaaaa && // q\n"
16426                "    bb)         // w\n"
16427                "\t;",
16428                "if (aaaaaaaa &&// q\n"
16429                "bb)// w\n"
16430                ";",
16431                Tab);
16432   verifyFormat("if (aaa && bbb) // w\n"
16433                "\t;",
16434                "if(aaa&&bbb)// w\n"
16435                ";",
16436                Tab);
16437   verifyFormat("class X {\n"
16438                "\tvoid f() {\n"
16439                "\t\tsomeFunction(parameter1,\n"
16440                "\t\t             parameter2);\n"
16441                "\t}\n"
16442                "};",
16443                Tab);
16444   verifyFormat("#define A                        \\\n"
16445                "\tvoid f() {               \\\n"
16446                "\t\tsomeFunction(    \\\n"
16447                "\t\t    parameter1,  \\\n"
16448                "\t\t    parameter2); \\\n"
16449                "\t}",
16450                Tab);
16451   Tab.TabWidth = 4;
16452   Tab.IndentWidth = 8;
16453   verifyFormat("class TabWidth4Indent8 {\n"
16454                "\t\tvoid f() {\n"
16455                "\t\t\t\tsomeFunction(parameter1,\n"
16456                "\t\t\t\t             parameter2);\n"
16457                "\t\t}\n"
16458                "};",
16459                Tab);
16460   Tab.TabWidth = 4;
16461   Tab.IndentWidth = 4;
16462   verifyFormat("class TabWidth4Indent4 {\n"
16463                "\tvoid f() {\n"
16464                "\t\tsomeFunction(parameter1,\n"
16465                "\t\t             parameter2);\n"
16466                "\t}\n"
16467                "};",
16468                Tab);
16469   Tab.TabWidth = 8;
16470   Tab.IndentWidth = 4;
16471   verifyFormat("class TabWidth8Indent4 {\n"
16472                "    void f() {\n"
16473                "\tsomeFunction(parameter1,\n"
16474                "\t             parameter2);\n"
16475                "    }\n"
16476                "};",
16477                Tab);
16478   Tab.TabWidth = 8;
16479   Tab.IndentWidth = 8;
16480   verifyFormat("/*\n"
16481                "              a\t\tcomment\n"
16482                "              in multiple lines\n"
16483                "       */",
16484                "   /*\t \t \n"
16485                " \t \t a\t\tcomment\t \t\n"
16486                " \t \t in multiple lines\t\n"
16487                " \t  */",
16488                Tab);
16489   verifyFormat("{\n"
16490                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16491                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16492                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16493                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16494                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16495                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16496                "};",
16497                Tab);
16498   verifyFormat("enum AA {\n"
16499                "\ta1, // Force multiple lines\n"
16500                "\ta2,\n"
16501                "\ta3\n"
16502                "};",
16503                Tab);
16504   verifyFormat("if (aaaaaaaa && // q\n"
16505                "    bb)         // w\n"
16506                "\t;",
16507                "if (aaaaaaaa &&// q\n"
16508                "bb)// w\n"
16509                ";",
16510                Tab);
16511   verifyFormat("class X {\n"
16512                "\tvoid f() {\n"
16513                "\t\tsomeFunction(parameter1,\n"
16514                "\t\t             parameter2);\n"
16515                "\t}\n"
16516                "};",
16517                Tab);
16518   verifyFormat("{\n"
16519                "\tQ(\n"
16520                "\t    {\n"
16521                "\t\t    int a;\n"
16522                "\t\t    someFunction(aaaaaaaa,\n"
16523                "\t\t                 bbbbbbb);\n"
16524                "\t    },\n"
16525                "\t    p);\n"
16526                "}",
16527                Tab);
16528   verifyFormat("{\n"
16529                "\t/* aaaa\n"
16530                "\t   bbbb */\n"
16531                "}",
16532                "{\n"
16533                "/* aaaa\n"
16534                "   bbbb */\n"
16535                "}",
16536                Tab);
16537   verifyFormat("{\n"
16538                "\t/*\n"
16539                "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16540                "\t  bbbbbbbbbbbbb\n"
16541                "\t*/\n"
16542                "}",
16543                "{\n"
16544                "/*\n"
16545                "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16546                "*/\n"
16547                "}",
16548                Tab);
16549   verifyFormat("{\n"
16550                "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16551                "\t// bbbbbbbbbbbbb\n"
16552                "}",
16553                "{\n"
16554                "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16555                "}",
16556                Tab);
16557   verifyFormat("{\n"
16558                "\t/*\n"
16559                "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16560                "\t  bbbbbbbbbbbbb\n"
16561                "\t*/\n"
16562                "}",
16563                "{\n"
16564                "\t/*\n"
16565                "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16566                "\t*/\n"
16567                "}",
16568                Tab);
16569   verifyNoChange("{\n"
16570                  "\t/*\n"
16571                  "\n"
16572                  "\t*/\n"
16573                  "}",
16574                  Tab);
16575   verifyNoChange("{\n"
16576                  "\t/*\n"
16577                  " asdf\n"
16578                  "\t*/\n"
16579                  "}",
16580                  Tab);
16581   verifyFormat("/* some\n"
16582                "   comment */",
16583                " \t \t /* some\n"
16584                " \t \t    comment */",
16585                Tab);
16586   verifyFormat("int a; /* some\n"
16587                "   comment */",
16588                " \t \t int a; /* some\n"
16589                " \t \t    comment */",
16590                Tab);
16591   verifyFormat("int a; /* some\n"
16592                "comment */",
16593                " \t \t int\ta; /* some\n"
16594                " \t \t    comment */",
16595                Tab);
16596   verifyFormat("f(\"\t\t\"); /* some\n"
16597                "    comment */",
16598                " \t \t f(\"\t\t\"); /* some\n"
16599                " \t \t    comment */",
16600                Tab);
16601   verifyFormat("{\n"
16602                "\t/*\n"
16603                "\t * Comment\n"
16604                "\t */\n"
16605                "\tint i;\n"
16606                "}",
16607                "{\n"
16608                "\t/*\n"
16609                "\t * Comment\n"
16610                "\t */\n"
16611                "\t int i;\n"
16612                "}",
16613                Tab);
16614   Tab.TabWidth = 2;
16615   Tab.IndentWidth = 2;
16616   verifyFormat("{\n"
16617                "\t/* aaaa\n"
16618                "\t   bbbb */\n"
16619                "}",
16620                "{\n"
16621                "/* aaaa\n"
16622                "   bbbb */\n"
16623                "}",
16624                Tab);
16625   verifyFormat("{\n"
16626                "\t/*\n"
16627                "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16628                "\t  bbbbbbbbbbbbb\n"
16629                "\t*/\n"
16630                "}",
16631                "{\n"
16632                "/*\n"
16633                "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16634                "*/\n"
16635                "}",
16636                Tab);
16637   Tab.AlignConsecutiveAssignments.Enabled = true;
16638   Tab.AlignConsecutiveDeclarations.Enabled = true;
16639   Tab.TabWidth = 4;
16640   Tab.IndentWidth = 4;
16641   verifyFormat("class Assign {\n"
16642                "\tvoid f() {\n"
16643                "\t\tint         x      = 123;\n"
16644                "\t\tint         random = 4;\n"
16645                "\t\tstd::string alphabet =\n"
16646                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
16647                "\t}\n"
16648                "};",
16649                Tab);
16650   Tab.AlignOperands = FormatStyle::OAS_Align;
16651   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
16652                "                 cccccccccccccccccccc;",
16653                Tab);
16654   // no alignment
16655   verifyFormat("int aaaaaaaaaa =\n"
16656                "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
16657                Tab);
16658   verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
16659                "       : bbbbbbbbbbbbbb ? 222222222222222\n"
16660                "                        : 333333333333333;",
16661                Tab);
16662   Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16663   Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
16664   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
16665                "               + cccccccccccccccccccc;",
16666                Tab);
16667 }
16668 
16669 TEST_F(FormatTest, ZeroTabWidth) {
16670   FormatStyle Tab = getLLVMStyleWithColumns(42);
16671   Tab.IndentWidth = 8;
16672   Tab.UseTab = FormatStyle::UT_Never;
16673   Tab.TabWidth = 0;
16674   verifyFormat("void a() {\n"
16675                "        // line starts with '\t'\n"
16676                "};",
16677                "void a(){\n"
16678                "\t// line starts with '\t'\n"
16679                "};",
16680                Tab);
16681 
16682   verifyFormat("void a() {\n"
16683                "        // line starts with '\t'\n"
16684                "};",
16685                "void a(){\n"
16686                "\t\t// line starts with '\t'\n"
16687                "};",
16688                Tab);
16689 
16690   Tab.UseTab = FormatStyle::UT_ForIndentation;
16691   verifyFormat("void a() {\n"
16692                "        // line starts with '\t'\n"
16693                "};",
16694                "void a(){\n"
16695                "\t// line starts with '\t'\n"
16696                "};",
16697                Tab);
16698 
16699   verifyFormat("void a() {\n"
16700                "        // line starts with '\t'\n"
16701                "};",
16702                "void a(){\n"
16703                "\t\t// line starts with '\t'\n"
16704                "};",
16705                Tab);
16706 
16707   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
16708   verifyFormat("void a() {\n"
16709                "        // line starts with '\t'\n"
16710                "};",
16711                "void a(){\n"
16712                "\t// line starts with '\t'\n"
16713                "};",
16714                Tab);
16715 
16716   verifyFormat("void a() {\n"
16717                "        // line starts with '\t'\n"
16718                "};",
16719                "void a(){\n"
16720                "\t\t// line starts with '\t'\n"
16721                "};",
16722                Tab);
16723 
16724   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
16725   verifyFormat("void a() {\n"
16726                "        // line starts with '\t'\n"
16727                "};",
16728                "void a(){\n"
16729                "\t// line starts with '\t'\n"
16730                "};",
16731                Tab);
16732 
16733   verifyFormat("void a() {\n"
16734                "        // line starts with '\t'\n"
16735                "};",
16736                "void a(){\n"
16737                "\t\t// line starts with '\t'\n"
16738                "};",
16739                Tab);
16740 
16741   Tab.UseTab = FormatStyle::UT_Always;
16742   verifyFormat("void a() {\n"
16743                "// line starts with '\t'\n"
16744                "};",
16745                "void a(){\n"
16746                "\t// line starts with '\t'\n"
16747                "};",
16748                Tab);
16749 
16750   verifyFormat("void a() {\n"
16751                "// line starts with '\t'\n"
16752                "};",
16753                "void a(){\n"
16754                "\t\t// line starts with '\t'\n"
16755                "};",
16756                Tab);
16757 }
16758 
16759 TEST_F(FormatTest, CalculatesOriginalColumn) {
16760   verifyFormat("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16761                "q\"; /* some\n"
16762                "       comment */",
16763                "  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16764                "q\"; /* some\n"
16765                "       comment */");
16766   verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
16767                "/* some\n"
16768                "   comment */",
16769                "// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
16770                " /* some\n"
16771                "    comment */");
16772   verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16773                "qqq\n"
16774                "/* some\n"
16775                "   comment */",
16776                "// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16777                "qqq\n"
16778                " /* some\n"
16779                "    comment */");
16780   verifyFormat("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16781                "wwww; /* some\n"
16782                "         comment */",
16783                "  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16784                "wwww; /* some\n"
16785                "         comment */");
16786 }
16787 
16788 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
16789   FormatStyle NoSpace = getLLVMStyle();
16790   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
16791 
16792   verifyFormat("while(true)\n"
16793                "  continue;",
16794                NoSpace);
16795   verifyFormat("for(;;)\n"
16796                "  continue;",
16797                NoSpace);
16798   verifyFormat("if(true)\n"
16799                "  f();\n"
16800                "else if(true)\n"
16801                "  f();",
16802                NoSpace);
16803   verifyFormat("do {\n"
16804                "  do_something();\n"
16805                "} while(something());",
16806                NoSpace);
16807   verifyFormat("switch(x) {\n"
16808                "default:\n"
16809                "  break;\n"
16810                "}",
16811                NoSpace);
16812   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
16813   verifyFormat("size_t x = sizeof(x);", NoSpace);
16814   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
16815   verifyFormat("auto f(int x) -> typeof(x);", NoSpace);
16816   verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace);
16817   verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace);
16818   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
16819   verifyFormat("alignas(128) char a[128];", NoSpace);
16820   verifyFormat("size_t x = alignof(MyType);", NoSpace);
16821   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
16822   verifyFormat("int f() throw(Deprecated);", NoSpace);
16823   verifyFormat("typedef void (*cb)(int);", NoSpace);
16824   verifyFormat("T A::operator()();", NoSpace);
16825   verifyFormat("X A::operator++(T);", NoSpace);
16826   verifyFormat("auto lambda = []() { return 0; };", NoSpace);
16827   verifyFormat("#if (foo || bar) && baz\n"
16828                "#elif ((a || b) && c) || d\n"
16829                "#endif",
16830                NoSpace);
16831 
16832   FormatStyle Space = getLLVMStyle();
16833   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
16834 
16835   verifyFormat("int f ();", Space);
16836   verifyFormat("bool operator< ();", Space);
16837   verifyFormat("bool operator> ();", Space);
16838   verifyFormat("void f (int a, T b) {\n"
16839                "  while (true)\n"
16840                "    continue;\n"
16841                "}",
16842                Space);
16843   verifyFormat("if (true)\n"
16844                "  f ();\n"
16845                "else if (true)\n"
16846                "  f ();",
16847                Space);
16848   verifyFormat("do {\n"
16849                "  do_something ();\n"
16850                "} while (something ());",
16851                Space);
16852   verifyFormat("switch (x) {\n"
16853                "default:\n"
16854                "  break;\n"
16855                "}",
16856                Space);
16857   verifyFormat("A::A () : a (1) {}", Space);
16858   verifyFormat("void f () __attribute__ ((asdf));", Space);
16859   verifyFormat("*(&a + 1);\n"
16860                "&((&a)[1]);\n"
16861                "a[(b + c) * d];\n"
16862                "(((a + 1) * 2) + 3) * 4;",
16863                Space);
16864   verifyFormat("#define A(x) x", Space);
16865   verifyFormat("#define A (x) x", Space);
16866   verifyFormat("#if defined(x)\n"
16867                "#endif",
16868                Space);
16869   verifyFormat("auto i = std::make_unique<int> (5);", Space);
16870   verifyFormat("size_t x = sizeof (x);", Space);
16871   verifyFormat("auto f (int x) -> decltype (x);", Space);
16872   verifyFormat("auto f (int x) -> typeof (x);", Space);
16873   verifyFormat("auto f (int x) -> _Atomic (x);", Space);
16874   verifyFormat("auto f (int x) -> __underlying_type (x);", Space);
16875   verifyFormat("int f (T x) noexcept (x.create ());", Space);
16876   verifyFormat("alignas (128) char a[128];", Space);
16877   verifyFormat("size_t x = alignof (MyType);", Space);
16878   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
16879   verifyFormat("int f () throw (Deprecated);", Space);
16880   verifyFormat("typedef void (*cb) (int);", Space);
16881   verifyFormat("T A::operator() ();", Space);
16882   verifyFormat("X A::operator++ (T);", Space);
16883   verifyFormat("auto lambda = [] () { return 0; };", Space);
16884   verifyFormat("int x = int (y);", Space);
16885   verifyFormat("#define F(...) __VA_OPT__ (__VA_ARGS__)", Space);
16886   verifyFormat("__builtin_LINE ()", Space);
16887   verifyFormat("__builtin_UNKNOWN ()", Space);
16888 
16889   FormatStyle SomeSpace = getLLVMStyle();
16890   SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
16891 
16892   verifyFormat("[]() -> float {}", SomeSpace);
16893   verifyFormat("[] (auto foo) {}", SomeSpace);
16894   verifyFormat("[foo]() -> int {}", SomeSpace);
16895   verifyFormat("int f();", SomeSpace);
16896   verifyFormat("void f (int a, T b) {\n"
16897                "  while (true)\n"
16898                "    continue;\n"
16899                "}",
16900                SomeSpace);
16901   verifyFormat("if (true)\n"
16902                "  f();\n"
16903                "else if (true)\n"
16904                "  f();",
16905                SomeSpace);
16906   verifyFormat("do {\n"
16907                "  do_something();\n"
16908                "} while (something());",
16909                SomeSpace);
16910   verifyFormat("switch (x) {\n"
16911                "default:\n"
16912                "  break;\n"
16913                "}",
16914                SomeSpace);
16915   verifyFormat("A::A() : a (1) {}", SomeSpace);
16916   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
16917   verifyFormat("*(&a + 1);\n"
16918                "&((&a)[1]);\n"
16919                "a[(b + c) * d];\n"
16920                "(((a + 1) * 2) + 3) * 4;",
16921                SomeSpace);
16922   verifyFormat("#define A(x) x", SomeSpace);
16923   verifyFormat("#define A (x) x", SomeSpace);
16924   verifyFormat("#if defined(x)\n"
16925                "#endif",
16926                SomeSpace);
16927   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
16928   verifyFormat("size_t x = sizeof (x);", SomeSpace);
16929   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
16930   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace);
16931   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace);
16932   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace);
16933   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
16934   verifyFormat("alignas (128) char a[128];", SomeSpace);
16935   verifyFormat("size_t x = alignof (MyType);", SomeSpace);
16936   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
16937                SomeSpace);
16938   verifyFormat("int f() throw (Deprecated);", SomeSpace);
16939   verifyFormat("typedef void (*cb) (int);", SomeSpace);
16940   verifyFormat("T A::operator()();", SomeSpace);
16941   verifyFormat("X A::operator++ (T);", SomeSpace);
16942   verifyFormat("int x = int (y);", SomeSpace);
16943   verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
16944 
16945   FormatStyle SpaceControlStatements = getLLVMStyle();
16946   SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16947   SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true;
16948 
16949   verifyFormat("while (true)\n"
16950                "  continue;",
16951                SpaceControlStatements);
16952   verifyFormat("if (true)\n"
16953                "  f();\n"
16954                "else if (true)\n"
16955                "  f();",
16956                SpaceControlStatements);
16957   verifyFormat("for (;;) {\n"
16958                "  do_something();\n"
16959                "}",
16960                SpaceControlStatements);
16961   verifyFormat("do {\n"
16962                "  do_something();\n"
16963                "} while (something());",
16964                SpaceControlStatements);
16965   verifyFormat("switch (x) {\n"
16966                "default:\n"
16967                "  break;\n"
16968                "}",
16969                SpaceControlStatements);
16970 
16971   FormatStyle SpaceFuncDecl = getLLVMStyle();
16972   SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16973   SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true;
16974 
16975   verifyFormat("int f ();", SpaceFuncDecl);
16976   verifyFormat("void f(int a, T b) {}", SpaceFuncDecl);
16977   verifyFormat("void __attribute__((asdf)) f(int a, T b) {}", SpaceFuncDecl);
16978   verifyFormat("A::A() : a(1) {}", SpaceFuncDecl);
16979   verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl);
16980   verifyFormat("void __attribute__((asdf)) f ();", SpaceFuncDecl);
16981   verifyFormat("#define A(x) x", SpaceFuncDecl);
16982   verifyFormat("#define A (x) x", SpaceFuncDecl);
16983   verifyFormat("#if defined(x)\n"
16984                "#endif",
16985                SpaceFuncDecl);
16986   verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl);
16987   verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl);
16988   verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl);
16989   verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl);
16990   verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl);
16991   verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl);
16992   verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl);
16993   verifyFormat("alignas(128) char a[128];", SpaceFuncDecl);
16994   verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl);
16995   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
16996                SpaceFuncDecl);
16997   verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl);
16998   verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl);
16999   verifyFormat("T A::operator()();", SpaceFuncDecl);
17000   verifyFormat("X A::operator++(T);", SpaceFuncDecl);
17001   verifyFormat("T A::operator()() {}", SpaceFuncDecl);
17002   verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl);
17003   verifyFormat("int x = int(y);", SpaceFuncDecl);
17004   verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
17005                SpaceFuncDecl);
17006 
17007   FormatStyle SpaceFuncDef = getLLVMStyle();
17008   SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17009   SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true;
17010 
17011   verifyFormat("int f();", SpaceFuncDef);
17012   verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
17013   verifyFormat("void __attribute__((asdf)) f (int a, T b) {}", SpaceFuncDef);
17014   verifyFormat("A::A () : a(1) {}", SpaceFuncDef);
17015   verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
17016   verifyFormat("void __attribute__((asdf)) f();", SpaceFuncDef);
17017   verifyFormat("#define A(x) x", SpaceFuncDef);
17018   verifyFormat("#define A (x) x", SpaceFuncDef);
17019   verifyFormat("#if defined(x)\n"
17020                "#endif",
17021                SpaceFuncDef);
17022   verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef);
17023   verifyFormat("size_t x = sizeof(x);", SpaceFuncDef);
17024   verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef);
17025   verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef);
17026   verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef);
17027   verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef);
17028   verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef);
17029   verifyFormat("alignas(128) char a[128];", SpaceFuncDef);
17030   verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef);
17031   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
17032                SpaceFuncDef);
17033   verifyFormat("int f() throw(Deprecated);", SpaceFuncDef);
17034   verifyFormat("typedef void (*cb)(int);", SpaceFuncDef);
17035   verifyFormat("T A::operator()();", SpaceFuncDef);
17036   verifyFormat("X A::operator++(T);", SpaceFuncDef);
17037   verifyFormat("T A::operator()() {}", SpaceFuncDef);
17038   verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef);
17039   verifyFormat("int x = int(y);", SpaceFuncDef);
17040   verifyFormat("void foo::bar () {}", SpaceFuncDef);
17041   verifyFormat("M (std::size_t R, std::size_t C) : C(C), data(R) {}",
17042                SpaceFuncDef);
17043 
17044   FormatStyle SpaceIfMacros = getLLVMStyle();
17045   SpaceIfMacros.IfMacros.clear();
17046   SpaceIfMacros.IfMacros.push_back("MYIF");
17047   SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17048   SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true;
17049   verifyFormat("MYIF (a)\n  return;", SpaceIfMacros);
17050   verifyFormat("MYIF (a)\n  return;\nelse MYIF (b)\n  return;", SpaceIfMacros);
17051   verifyFormat("MYIF (a)\n  return;\nelse\n  return;", SpaceIfMacros);
17052 
17053   FormatStyle SpaceForeachMacros = getLLVMStyle();
17054   EXPECT_EQ(SpaceForeachMacros.AllowShortBlocksOnASingleLine,
17055             FormatStyle::SBS_Never);
17056   EXPECT_EQ(SpaceForeachMacros.AllowShortLoopsOnASingleLine, false);
17057   SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17058   SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true;
17059   verifyFormat("for (;;) {\n"
17060                "}",
17061                SpaceForeachMacros);
17062   verifyFormat("foreach (Item *item, itemlist) {\n"
17063                "}",
17064                SpaceForeachMacros);
17065   verifyFormat("Q_FOREACH (Item *item, itemlist) {\n"
17066                "}",
17067                SpaceForeachMacros);
17068   verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n"
17069                "}",
17070                SpaceForeachMacros);
17071   verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros);
17072 
17073   FormatStyle SomeSpace2 = getLLVMStyle();
17074   SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17075   SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true;
17076   verifyFormat("[]() -> float {}", SomeSpace2);
17077   verifyFormat("[] (auto foo) {}", SomeSpace2);
17078   verifyFormat("[foo]() -> int {}", SomeSpace2);
17079   verifyFormat("int f();", SomeSpace2);
17080   verifyFormat("void f (int a, T b) {\n"
17081                "  while (true)\n"
17082                "    continue;\n"
17083                "}",
17084                SomeSpace2);
17085   verifyFormat("if (true)\n"
17086                "  f();\n"
17087                "else if (true)\n"
17088                "  f();",
17089                SomeSpace2);
17090   verifyFormat("do {\n"
17091                "  do_something();\n"
17092                "} while (something());",
17093                SomeSpace2);
17094   verifyFormat("switch (x) {\n"
17095                "default:\n"
17096                "  break;\n"
17097                "}",
17098                SomeSpace2);
17099   verifyFormat("A::A() : a (1) {}", SomeSpace2);
17100   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2);
17101   verifyFormat("*(&a + 1);\n"
17102                "&((&a)[1]);\n"
17103                "a[(b + c) * d];\n"
17104                "(((a + 1) * 2) + 3) * 4;",
17105                SomeSpace2);
17106   verifyFormat("#define A(x) x", SomeSpace2);
17107   verifyFormat("#define A (x) x", SomeSpace2);
17108   verifyFormat("#if defined(x)\n"
17109                "#endif",
17110                SomeSpace2);
17111   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2);
17112   verifyFormat("size_t x = sizeof (x);", SomeSpace2);
17113   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2);
17114   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2);
17115   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2);
17116   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2);
17117   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2);
17118   verifyFormat("alignas (128) char a[128];", SomeSpace2);
17119   verifyFormat("size_t x = alignof (MyType);", SomeSpace2);
17120   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
17121                SomeSpace2);
17122   verifyFormat("int f() throw (Deprecated);", SomeSpace2);
17123   verifyFormat("typedef void (*cb) (int);", SomeSpace2);
17124   verifyFormat("T A::operator()();", SomeSpace2);
17125   verifyFormat("X A::operator++ (T);", SomeSpace2);
17126   verifyFormat("int x = int (y);", SomeSpace2);
17127   verifyFormat("auto lambda = []() { return 0; };", SomeSpace2);
17128 
17129   FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle();
17130   SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17131   SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
17132       .AfterOverloadedOperator = true;
17133 
17134   verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator);
17135   verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator);
17136   verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator);
17137   verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
17138 
17139   SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
17140       .AfterOverloadedOperator = false;
17141 
17142   verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator);
17143   verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator);
17144   verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator);
17145   verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
17146 
17147   auto SpaceAfterRequires = getLLVMStyle();
17148   SpaceAfterRequires.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17149   EXPECT_FALSE(
17150       SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause);
17151   EXPECT_FALSE(
17152       SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression);
17153   verifyFormat("void f(auto x)\n"
17154                "  requires requires(int i) { x + i; }\n"
17155                "{}",
17156                SpaceAfterRequires);
17157   verifyFormat("void f(auto x)\n"
17158                "  requires(requires(int i) { x + i; })\n"
17159                "{}",
17160                SpaceAfterRequires);
17161   verifyFormat("if (requires(int i) { x + i; })\n"
17162                "  return;",
17163                SpaceAfterRequires);
17164   verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires);
17165   verifyFormat("template <typename T>\n"
17166                "  requires(Foo<T>)\n"
17167                "class Bar;",
17168                SpaceAfterRequires);
17169 
17170   SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true;
17171   verifyFormat("void f(auto x)\n"
17172                "  requires requires(int i) { x + i; }\n"
17173                "{}",
17174                SpaceAfterRequires);
17175   verifyFormat("void f(auto x)\n"
17176                "  requires (requires(int i) { x + i; })\n"
17177                "{}",
17178                SpaceAfterRequires);
17179   verifyFormat("if (requires(int i) { x + i; })\n"
17180                "  return;",
17181                SpaceAfterRequires);
17182   verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires);
17183   verifyFormat("template <typename T>\n"
17184                "  requires (Foo<T>)\n"
17185                "class Bar;",
17186                SpaceAfterRequires);
17187 
17188   SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = false;
17189   SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression = true;
17190   verifyFormat("void f(auto x)\n"
17191                "  requires requires (int i) { x + i; }\n"
17192                "{}",
17193                SpaceAfterRequires);
17194   verifyFormat("void f(auto x)\n"
17195                "  requires(requires (int i) { x + i; })\n"
17196                "{}",
17197                SpaceAfterRequires);
17198   verifyFormat("if (requires (int i) { x + i; })\n"
17199                "  return;",
17200                SpaceAfterRequires);
17201   verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires);
17202   verifyFormat("template <typename T>\n"
17203                "  requires(Foo<T>)\n"
17204                "class Bar;",
17205                SpaceAfterRequires);
17206 
17207   SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true;
17208   verifyFormat("void f(auto x)\n"
17209                "  requires requires (int i) { x + i; }\n"
17210                "{}",
17211                SpaceAfterRequires);
17212   verifyFormat("void f(auto x)\n"
17213                "  requires (requires (int i) { x + i; })\n"
17214                "{}",
17215                SpaceAfterRequires);
17216   verifyFormat("if (requires (int i) { x + i; })\n"
17217                "  return;",
17218                SpaceAfterRequires);
17219   verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires);
17220   verifyFormat("template <typename T>\n"
17221                "  requires (Foo<T>)\n"
17222                "class Bar;",
17223                SpaceAfterRequires);
17224 }
17225 
17226 TEST_F(FormatTest, SpaceAfterLogicalNot) {
17227   FormatStyle Spaces = getLLVMStyle();
17228   Spaces.SpaceAfterLogicalNot = true;
17229 
17230   verifyFormat("bool x = ! y", Spaces);
17231   verifyFormat("if (! isFailure())", Spaces);
17232   verifyFormat("if (! (a && b))", Spaces);
17233   verifyFormat("\"Error!\"", Spaces);
17234   verifyFormat("! ! x", Spaces);
17235 }
17236 
17237 TEST_F(FormatTest, ConfigurableSpacesInParens) {
17238   FormatStyle Spaces = getLLVMStyle();
17239 
17240   verifyFormat("do_something(::globalVar);", Spaces);
17241   verifyFormat("call(x, y, z);", Spaces);
17242   verifyFormat("call();", Spaces);
17243   verifyFormat("std::function<void(int, int)> callback;", Spaces);
17244   verifyFormat("void inFunction() { std::function<void(int, int)> fct; }",
17245                Spaces);
17246   verifyFormat("while ((bool)1)\n"
17247                "  continue;",
17248                Spaces);
17249   verifyFormat("for (;;)\n"
17250                "  continue;",
17251                Spaces);
17252   verifyFormat("if (true)\n"
17253                "  f();\n"
17254                "else if (true)\n"
17255                "  f();",
17256                Spaces);
17257   verifyFormat("do {\n"
17258                "  do_something((int)i);\n"
17259                "} while (something());",
17260                Spaces);
17261   verifyFormat("switch (x) {\n"
17262                "default:\n"
17263                "  break;\n"
17264                "}",
17265                Spaces);
17266   verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17267   verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
17268   verifyFormat("void f() __attribute__((asdf));", Spaces);
17269   verifyFormat("x = (int32)y;", Spaces);
17270   verifyFormat("y = ((int (*)(int))foo)(x);", Spaces);
17271   verifyFormat("decltype(x) y = 42;", Spaces);
17272   verifyFormat("decltype((x)) y = z;", Spaces);
17273   verifyFormat("decltype((foo())) a = foo();", Spaces);
17274   verifyFormat("decltype((bar(10))) a = bar(11);", Spaces);
17275   verifyFormat("if ((x - y) && (a ^ b))\n"
17276                "  f();",
17277                Spaces);
17278   verifyFormat("for (int i = 0; i < 10; i = (i + 1))\n"
17279                "  foo(i);",
17280                Spaces);
17281   verifyFormat("switch (x / (y + z)) {\n"
17282                "default:\n"
17283                "  break;\n"
17284                "}",
17285                Spaces);
17286 
17287   Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17288   Spaces.SpacesInParensOptions = {};
17289   Spaces.SpacesInParensOptions.Other = true;
17290 
17291   EXPECT_FALSE(Spaces.SpacesInParensOptions.InConditionalStatements);
17292   verifyFormat("if (a)\n"
17293                "  return;",
17294                Spaces);
17295 
17296   Spaces.SpacesInParensOptions.InConditionalStatements = true;
17297   verifyFormat("do_something( ::globalVar );", Spaces);
17298   verifyFormat("call( x, y, z );", Spaces);
17299   verifyFormat("call();", Spaces);
17300   verifyFormat("std::function<void( int, int )> callback;", Spaces);
17301   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
17302                Spaces);
17303   verifyFormat("while ( (bool)1 )\n"
17304                "  continue;",
17305                Spaces);
17306   verifyFormat("for ( ;; )\n"
17307                "  continue;",
17308                Spaces);
17309   verifyFormat("if ( true )\n"
17310                "  f();\n"
17311                "else if ( true )\n"
17312                "  f();",
17313                Spaces);
17314   verifyFormat("do {\n"
17315                "  do_something( (int)i );\n"
17316                "} while ( something() );",
17317                Spaces);
17318   verifyFormat("switch ( x ) {\n"
17319                "default:\n"
17320                "  break;\n"
17321                "}",
17322                Spaces);
17323   verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
17324   verifyFormat("void __attribute__( ( naked ) ) foo( int bar )", Spaces);
17325   verifyFormat("void f() __attribute__( ( asdf ) );", Spaces);
17326   verifyFormat("x = (int32)y;", Spaces);
17327   verifyFormat("y = ( (int ( * )( int ))foo )( x );", Spaces);
17328   verifyFormat("decltype( x ) y = 42;", Spaces);
17329   verifyFormat("decltype( ( x ) ) y = z;", Spaces);
17330   verifyFormat("decltype( ( foo() ) ) a = foo();", Spaces);
17331   verifyFormat("decltype( ( bar( 10 ) ) ) a = bar( 11 );", Spaces);
17332   verifyFormat("if ( ( x - y ) && ( a ^ b ) )\n"
17333                "  f();",
17334                Spaces);
17335   verifyFormat("for ( int i = 0; i < 10; i = ( i + 1 ) )\n"
17336                "  foo( i );",
17337                Spaces);
17338   verifyFormat("switch ( x / ( y + z ) ) {\n"
17339                "default:\n"
17340                "  break;\n"
17341                "}",
17342                Spaces);
17343 
17344   Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17345   Spaces.SpacesInParensOptions = {};
17346   Spaces.SpacesInParensOptions.InCStyleCasts = true;
17347   verifyFormat("Type *A = ( Type * )P;", Spaces);
17348   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
17349   verifyFormat("x = ( int32 )y;", Spaces);
17350   verifyFormat("throw ( int32 )x;", Spaces);
17351   verifyFormat("int a = ( int )(2.0f);", Spaces);
17352   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
17353   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
17354   verifyFormat("#define x (( int )-1)", Spaces);
17355   verifyFormat("y = (( int (*)(int) )foo)(x);", Spaces);
17356 
17357   // Run the first set of tests again with:
17358   Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17359   Spaces.SpacesInParensOptions = {};
17360   Spaces.SpacesInParensOptions.InEmptyParentheses = true;
17361   Spaces.SpacesInParensOptions.InCStyleCasts = true;
17362   verifyFormat("call(x, y, z);", Spaces);
17363   verifyFormat("call( );", Spaces);
17364   verifyFormat("std::function<void(int, int)> callback;", Spaces);
17365   verifyFormat("while (( bool )1)\n"
17366                "  continue;",
17367                Spaces);
17368   verifyFormat("for (;;)\n"
17369                "  continue;",
17370                Spaces);
17371   verifyFormat("if (true)\n"
17372                "  f( );\n"
17373                "else if (true)\n"
17374                "  f( );",
17375                Spaces);
17376   verifyFormat("do {\n"
17377                "  do_something(( int )i);\n"
17378                "} while (something( ));",
17379                Spaces);
17380   verifyFormat("switch (x) {\n"
17381                "default:\n"
17382                "  break;\n"
17383                "}",
17384                Spaces);
17385   verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17386   verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
17387   verifyFormat("void f( ) __attribute__((asdf));", Spaces);
17388   verifyFormat("x = ( int32 )y;", Spaces);
17389   verifyFormat("y = (( int (*)(int) )foo)(x);", Spaces);
17390   verifyFormat("decltype(x) y = 42;", Spaces);
17391   verifyFormat("decltype((x)) y = z;", Spaces);
17392   verifyFormat("decltype((foo( ))) a = foo( );", Spaces);
17393   verifyFormat("decltype((bar(10))) a = bar(11);", Spaces);
17394   verifyFormat("if ((x - y) && (a ^ b))\n"
17395                "  f( );",
17396                Spaces);
17397   verifyFormat("for (int i = 0; i < 10; i = (i + 1))\n"
17398                "  foo(i);",
17399                Spaces);
17400   verifyFormat("switch (x / (y + z)) {\n"
17401                "default:\n"
17402                "  break;\n"
17403                "}",
17404                Spaces);
17405 
17406   // Run the first set of tests again with:
17407   Spaces.SpaceAfterCStyleCast = true;
17408   verifyFormat("call(x, y, z);", Spaces);
17409   verifyFormat("call( );", Spaces);
17410   verifyFormat("std::function<void(int, int)> callback;", Spaces);
17411   verifyFormat("while (( bool ) 1)\n"
17412                "  continue;",
17413                Spaces);
17414   verifyFormat("for (;;)\n"
17415                "  continue;",
17416                Spaces);
17417   verifyFormat("if (true)\n"
17418                "  f( );\n"
17419                "else if (true)\n"
17420                "  f( );",
17421                Spaces);
17422   verifyFormat("do {\n"
17423                "  do_something(( int ) i);\n"
17424                "} while (something( ));",
17425                Spaces);
17426   verifyFormat("switch (x) {\n"
17427                "default:\n"
17428                "  break;\n"
17429                "}",
17430                Spaces);
17431   verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces);
17432   verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces);
17433   verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces);
17434   verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces);
17435   verifyFormat("bool *y = ( bool * ) (x);", Spaces);
17436   verifyFormat("throw ( int32 ) x;", Spaces);
17437   verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17438   verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
17439   verifyFormat("void f( ) __attribute__((asdf));", Spaces);
17440 
17441   // Run subset of tests again with:
17442   Spaces.SpacesInParensOptions.InCStyleCasts = false;
17443   Spaces.SpaceAfterCStyleCast = true;
17444   verifyFormat("while ((bool) 1)\n"
17445                "  continue;",
17446                Spaces);
17447   verifyFormat("do {\n"
17448                "  do_something((int) i);\n"
17449                "} while (something( ));",
17450                Spaces);
17451 
17452   verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
17453   verifyFormat("size_t idx = (size_t) a;", Spaces);
17454   verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
17455   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
17456   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
17457   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
17458   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
17459   verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces);
17460   verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces);
17461   verifyFormat("bool *y = (bool *) (void *) (x);", Spaces);
17462   verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces);
17463   verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces);
17464   verifyFormat("throw (int32) x;", Spaces);
17465   verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17466   verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
17467   verifyFormat("void f( ) __attribute__((asdf));", Spaces);
17468 
17469   Spaces.ColumnLimit = 80;
17470   Spaces.IndentWidth = 4;
17471   Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
17472   verifyFormat("void foo( ) {\n"
17473                "    size_t foo = (*(function))(\n"
17474                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
17475                "BarrrrrrrrrrrrLong,\n"
17476                "        FoooooooooLooooong);\n"
17477                "}",
17478                Spaces);
17479   Spaces.SpaceAfterCStyleCast = false;
17480   verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
17481   verifyFormat("size_t idx = (size_t)a;", Spaces);
17482   verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
17483   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
17484   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
17485   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
17486   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
17487 
17488   verifyFormat("void foo( ) {\n"
17489                "    size_t foo = (*(function))(\n"
17490                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
17491                "BarrrrrrrrrrrrLong,\n"
17492                "        FoooooooooLooooong);\n"
17493                "}",
17494                Spaces);
17495 
17496   Spaces.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
17497   verifyFormat("void foo( ) {\n"
17498                "    size_t foo = (*(function))(\n"
17499                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
17500                "BarrrrrrrrrrrrLong,\n"
17501                "        FoooooooooLooooong\n"
17502                "    );\n"
17503                "}",
17504                Spaces);
17505   verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
17506   verifyFormat("size_t idx = (size_t)a;", Spaces);
17507   verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
17508   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
17509   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
17510   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
17511   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
17512 
17513   // Check ExceptDoubleParentheses spaces
17514   Spaces.IndentWidth = 2;
17515   Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17516   Spaces.SpacesInParensOptions = {};
17517   Spaces.SpacesInParensOptions.Other = true;
17518   Spaces.SpacesInParensOptions.ExceptDoubleParentheses = true;
17519   verifyFormat("SomeType *__attribute__(( attr )) *a = NULL;", Spaces);
17520   verifyFormat("void __attribute__(( naked )) foo( int bar )", Spaces);
17521   verifyFormat("void f() __attribute__(( asdf ));", Spaces);
17522   verifyFormat("__attribute__(( __aligned__( x ) )) z;", Spaces);
17523   verifyFormat("int x __attribute__(( aligned( 16 ) )) = 0;", Spaces);
17524   verifyFormat("class __declspec( dllimport ) X {};", Spaces);
17525   verifyFormat("class __declspec(( dllimport )) X {};", Spaces);
17526   verifyFormat("int x = ( ( a - 1 ) * 3 );", Spaces);
17527   verifyFormat("int x = ( 3 * ( a - 1 ) );", Spaces);
17528   verifyFormat("decltype( x ) y = 42;", Spaces);
17529   verifyFormat("decltype(( bar( 10 ) )) a = bar( 11 );", Spaces);
17530   verifyFormat("if (( i = j ))\n"
17531                "  do_something( i );",
17532                Spaces);
17533 
17534   Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17535   Spaces.SpacesInParensOptions = {};
17536   Spaces.SpacesInParensOptions.InConditionalStatements = true;
17537   Spaces.SpacesInParensOptions.ExceptDoubleParentheses = true;
17538   verifyFormat("while ( (bool)1 )\n"
17539                "  continue;",
17540                Spaces);
17541   verifyFormat("while ((i = j))\n"
17542                "  continue;",
17543                Spaces);
17544   verifyFormat("do {\n"
17545                "  do_something((int)i);\n"
17546                "} while ( something() );",
17547                Spaces);
17548   verifyFormat("do {\n"
17549                "  do_something((int)i);\n"
17550                "} while ((i = i + 1));",
17551                Spaces);
17552   verifyFormat("if ( (x - y) && (a ^ b) )\n"
17553                "  f();",
17554                Spaces);
17555   verifyFormat("if ((i = j))\n"
17556                "  do_something(i);",
17557                Spaces);
17558   verifyFormat("for ( int i = 0; i < 10; i = (i + 1) )\n"
17559                "  foo(i);",
17560                Spaces);
17561   verifyFormat("switch ( x / (y + z) ) {\n"
17562                "default:\n"
17563                "  break;\n"
17564                "}",
17565                Spaces);
17566   verifyFormat("if constexpr ((a = b))\n"
17567                "  c;",
17568                Spaces);
17569 }
17570 
17571 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
17572   verifyFormat("int a[5];");
17573   verifyFormat("a[3] += 42;");
17574 
17575   FormatStyle Spaces = getLLVMStyle();
17576   Spaces.SpacesInSquareBrackets = true;
17577   // Not lambdas.
17578   verifyFormat("int a[ 5 ];", Spaces);
17579   verifyFormat("a[ 3 ] += 42;", Spaces);
17580   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
17581   verifyFormat("double &operator[](int i) { return 0; }\n"
17582                "int i;",
17583                Spaces);
17584   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
17585   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
17586   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
17587   // Lambdas.
17588   verifyFormat("int c = []() -> int { return 2; }();", Spaces);
17589   verifyFormat("return [ i, args... ] {};", Spaces);
17590   verifyFormat("int foo = [ &bar ]() {};", Spaces);
17591   verifyFormat("int foo = [ = ]() {};", Spaces);
17592   verifyFormat("int foo = [ & ]() {};", Spaces);
17593   verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
17594   verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
17595 }
17596 
17597 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
17598   FormatStyle NoSpaceStyle = getLLVMStyle();
17599   verifyFormat("int a[5];", NoSpaceStyle);
17600   verifyFormat("a[3] += 42;", NoSpaceStyle);
17601 
17602   verifyFormat("int a[1];", NoSpaceStyle);
17603   verifyFormat("int 1 [a];", NoSpaceStyle);
17604   verifyFormat("int a[1][2];", NoSpaceStyle);
17605   verifyFormat("a[7] = 5;", NoSpaceStyle);
17606   verifyFormat("int a = (f())[23];", NoSpaceStyle);
17607   verifyFormat("f([] {})", NoSpaceStyle);
17608 
17609   FormatStyle Space = getLLVMStyle();
17610   Space.SpaceBeforeSquareBrackets = true;
17611   verifyFormat("int c = []() -> int { return 2; }();", Space);
17612   verifyFormat("return [i, args...] {};", Space);
17613 
17614   verifyFormat("int a [5];", Space);
17615   verifyFormat("a [3] += 42;", Space);
17616   verifyFormat("constexpr char hello []{\"hello\"};", Space);
17617   verifyFormat("double &operator[](int i) { return 0; }\n"
17618                "int i;",
17619                Space);
17620   verifyFormat("std::unique_ptr<int []> foo() {}", Space);
17621   verifyFormat("int i = a [a][a]->f();", Space);
17622   verifyFormat("int i = (*b) [a]->f();", Space);
17623 
17624   verifyFormat("int a [1];", Space);
17625   verifyFormat("int 1 [a];", Space);
17626   verifyFormat("int a [1][2];", Space);
17627   verifyFormat("a [7] = 5;", Space);
17628   verifyFormat("int a = (f()) [23];", Space);
17629   verifyFormat("f([] {})", Space);
17630 }
17631 
17632 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
17633   verifyFormat("int a = 5;");
17634   verifyFormat("a += 42;");
17635   verifyFormat("a or_eq 8;");
17636   verifyFormat("xor = foo;");
17637 
17638   FormatStyle Spaces = getLLVMStyle();
17639   Spaces.SpaceBeforeAssignmentOperators = false;
17640   verifyFormat("int a= 5;", Spaces);
17641   verifyFormat("a+= 42;", Spaces);
17642   verifyFormat("a or_eq 8;", Spaces);
17643   verifyFormat("xor= foo;", Spaces);
17644 }
17645 
17646 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
17647   verifyFormat("class Foo : public Bar {};");
17648   verifyFormat("Foo::Foo() : foo(1) {}");
17649   verifyFormat("for (auto a : b) {\n}");
17650   verifyFormat("int x = a ? b : c;");
17651   verifyFormat("{\n"
17652                "label0:\n"
17653                "  int x = 0;\n"
17654                "}");
17655   verifyFormat("switch (x) {\n"
17656                "case 1:\n"
17657                "default:\n"
17658                "}");
17659   verifyFormat("switch (allBraces) {\n"
17660                "case 1: {\n"
17661                "  break;\n"
17662                "}\n"
17663                "case 2: {\n"
17664                "  [[fallthrough]];\n"
17665                "}\n"
17666                "default: {\n"
17667                "  break;\n"
17668                "}\n"
17669                "}");
17670 
17671   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
17672   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
17673   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
17674   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
17675   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
17676   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
17677   verifyFormat("{\n"
17678                "label1:\n"
17679                "  int x = 0;\n"
17680                "}",
17681                CtorInitializerStyle);
17682   verifyFormat("switch (x) {\n"
17683                "case 1:\n"
17684                "default:\n"
17685                "}",
17686                CtorInitializerStyle);
17687   verifyFormat("switch (allBraces) {\n"
17688                "case 1: {\n"
17689                "  break;\n"
17690                "}\n"
17691                "case 2: {\n"
17692                "  [[fallthrough]];\n"
17693                "}\n"
17694                "default: {\n"
17695                "  break;\n"
17696                "}\n"
17697                "}",
17698                CtorInitializerStyle);
17699   CtorInitializerStyle.BreakConstructorInitializers =
17700       FormatStyle::BCIS_AfterColon;
17701   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
17702                "    aaaaaaaaaaaaaaaa(1),\n"
17703                "    bbbbbbbbbbbbbbbb(2) {}",
17704                CtorInitializerStyle);
17705   CtorInitializerStyle.BreakConstructorInitializers =
17706       FormatStyle::BCIS_BeforeComma;
17707   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
17708                "    : aaaaaaaaaaaaaaaa(1)\n"
17709                "    , bbbbbbbbbbbbbbbb(2) {}",
17710                CtorInitializerStyle);
17711   CtorInitializerStyle.BreakConstructorInitializers =
17712       FormatStyle::BCIS_BeforeColon;
17713   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
17714                "    : aaaaaaaaaaaaaaaa(1),\n"
17715                "      bbbbbbbbbbbbbbbb(2) {}",
17716                CtorInitializerStyle);
17717   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
17718   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
17719                ": aaaaaaaaaaaaaaaa(1),\n"
17720                "  bbbbbbbbbbbbbbbb(2) {}",
17721                CtorInitializerStyle);
17722 
17723   FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
17724   InheritanceStyle.SpaceBeforeInheritanceColon = false;
17725   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
17726   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
17727   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
17728   verifyFormat("int x = a ? b : c;", InheritanceStyle);
17729   verifyFormat("{\n"
17730                "label2:\n"
17731                "  int x = 0;\n"
17732                "}",
17733                InheritanceStyle);
17734   verifyFormat("switch (x) {\n"
17735                "case 1:\n"
17736                "default:\n"
17737                "}",
17738                InheritanceStyle);
17739   verifyFormat("switch (allBraces) {\n"
17740                "case 1: {\n"
17741                "  break;\n"
17742                "}\n"
17743                "case 2: {\n"
17744                "  [[fallthrough]];\n"
17745                "}\n"
17746                "default: {\n"
17747                "  break;\n"
17748                "}\n"
17749                "}",
17750                InheritanceStyle);
17751   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma;
17752   verifyFormat("class Foooooooooooooooooooooo\n"
17753                "    : public aaaaaaaaaaaaaaaaaa,\n"
17754                "      public bbbbbbbbbbbbbbbbbb {\n"
17755                "}",
17756                InheritanceStyle);
17757   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
17758   verifyFormat("class Foooooooooooooooooooooo:\n"
17759                "    public aaaaaaaaaaaaaaaaaa,\n"
17760                "    public bbbbbbbbbbbbbbbbbb {\n"
17761                "}",
17762                InheritanceStyle);
17763   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
17764   verifyFormat("class Foooooooooooooooooooooo\n"
17765                "    : public aaaaaaaaaaaaaaaaaa\n"
17766                "    , public bbbbbbbbbbbbbbbbbb {\n"
17767                "}",
17768                InheritanceStyle);
17769   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
17770   verifyFormat("class Foooooooooooooooooooooo\n"
17771                "    : public aaaaaaaaaaaaaaaaaa,\n"
17772                "      public bbbbbbbbbbbbbbbbbb {\n"
17773                "}",
17774                InheritanceStyle);
17775   InheritanceStyle.ConstructorInitializerIndentWidth = 0;
17776   verifyFormat("class Foooooooooooooooooooooo\n"
17777                ": public aaaaaaaaaaaaaaaaaa,\n"
17778                "  public bbbbbbbbbbbbbbbbbb {}",
17779                InheritanceStyle);
17780 
17781   FormatStyle ForLoopStyle = getLLVMStyle();
17782   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
17783   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
17784   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
17785   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
17786   verifyFormat("int x = a ? b : c;", ForLoopStyle);
17787   verifyFormat("{\n"
17788                "label2:\n"
17789                "  int x = 0;\n"
17790                "}",
17791                ForLoopStyle);
17792   verifyFormat("switch (x) {\n"
17793                "case 1:\n"
17794                "default:\n"
17795                "}",
17796                ForLoopStyle);
17797   verifyFormat("switch (allBraces) {\n"
17798                "case 1: {\n"
17799                "  break;\n"
17800                "}\n"
17801                "case 2: {\n"
17802                "  [[fallthrough]];\n"
17803                "}\n"
17804                "default: {\n"
17805                "  break;\n"
17806                "}\n"
17807                "}",
17808                ForLoopStyle);
17809 
17810   FormatStyle CaseStyle = getLLVMStyle();
17811   CaseStyle.SpaceBeforeCaseColon = true;
17812   verifyFormat("class Foo : public Bar {};", CaseStyle);
17813   verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle);
17814   verifyFormat("for (auto a : b) {\n}", CaseStyle);
17815   verifyFormat("int x = a ? b : c;", CaseStyle);
17816   verifyFormat("switch (x) {\n"
17817                "case 1 :\n"
17818                "default :\n"
17819                "}",
17820                CaseStyle);
17821   verifyFormat("switch (allBraces) {\n"
17822                "case 1 : {\n"
17823                "  break;\n"
17824                "}\n"
17825                "case 2 : {\n"
17826                "  [[fallthrough]];\n"
17827                "}\n"
17828                "default : {\n"
17829                "  break;\n"
17830                "}\n"
17831                "}",
17832                CaseStyle);
17833   // Goto labels should not be affected.
17834   verifyFormat("switch (x) {\n"
17835                "goto_label:\n"
17836                "default :\n"
17837                "}",
17838                CaseStyle);
17839   verifyFormat("switch (x) {\n"
17840                "goto_label: { break; }\n"
17841                "default : {\n"
17842                "  break;\n"
17843                "}\n"
17844                "}",
17845                CaseStyle);
17846 
17847   FormatStyle NoSpaceStyle = getLLVMStyle();
17848   EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false);
17849   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
17850   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
17851   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
17852   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
17853   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
17854   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
17855   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
17856   verifyFormat("{\n"
17857                "label3:\n"
17858                "  int x = 0;\n"
17859                "}",
17860                NoSpaceStyle);
17861   verifyFormat("switch (x) {\n"
17862                "case 1:\n"
17863                "default:\n"
17864                "}",
17865                NoSpaceStyle);
17866   verifyFormat("switch (allBraces) {\n"
17867                "case 1: {\n"
17868                "  break;\n"
17869                "}\n"
17870                "case 2: {\n"
17871                "  [[fallthrough]];\n"
17872                "}\n"
17873                "default: {\n"
17874                "  break;\n"
17875                "}\n"
17876                "}",
17877                NoSpaceStyle);
17878 
17879   FormatStyle InvertedSpaceStyle = getLLVMStyle();
17880   InvertedSpaceStyle.SpaceBeforeCaseColon = true;
17881   InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false;
17882   InvertedSpaceStyle.SpaceBeforeInheritanceColon = false;
17883   InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
17884   verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle);
17885   verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle);
17886   verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle);
17887   verifyFormat("int x = a ? b : c;", InvertedSpaceStyle);
17888   verifyFormat("{\n"
17889                "label3:\n"
17890                "  int x = 0;\n"
17891                "}",
17892                InvertedSpaceStyle);
17893   verifyFormat("switch (x) {\n"
17894                "case 1 :\n"
17895                "case 2 : {\n"
17896                "  break;\n"
17897                "}\n"
17898                "default :\n"
17899                "  break;\n"
17900                "}",
17901                InvertedSpaceStyle);
17902   verifyFormat("switch (allBraces) {\n"
17903                "case 1 : {\n"
17904                "  break;\n"
17905                "}\n"
17906                "case 2 : {\n"
17907                "  [[fallthrough]];\n"
17908                "}\n"
17909                "default : {\n"
17910                "  break;\n"
17911                "}\n"
17912                "}",
17913                InvertedSpaceStyle);
17914 }
17915 
17916 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
17917   FormatStyle Style = getLLVMStyle();
17918 
17919   Style.PointerAlignment = FormatStyle::PAS_Left;
17920   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
17921   verifyFormat("void* const* x = NULL;", Style);
17922 
17923 #define verifyQualifierSpaces(Code, Pointers, Qualifiers)                      \
17924   do {                                                                         \
17925     Style.PointerAlignment = FormatStyle::Pointers;                            \
17926     Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers;              \
17927     verifyFormat(Code, Style);                                                 \
17928   } while (false)
17929 
17930   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default);
17931   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default);
17932   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default);
17933 
17934   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before);
17935   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before);
17936   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before);
17937 
17938   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After);
17939   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After);
17940   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After);
17941 
17942   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both);
17943   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
17944   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
17945 
17946   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
17947   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
17948                         SAPQ_Default);
17949   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
17950                         SAPQ_Default);
17951 
17952   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
17953   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
17954                         SAPQ_Before);
17955   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
17956                         SAPQ_Before);
17957 
17958   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
17959   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
17960   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
17961                         SAPQ_After);
17962 
17963   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
17964   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
17965   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
17966 
17967 #undef verifyQualifierSpaces
17968 
17969   FormatStyle Spaces = getLLVMStyle();
17970   Spaces.AttributeMacros.push_back("qualified");
17971   Spaces.PointerAlignment = FormatStyle::PAS_Right;
17972   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
17973   verifyFormat("SomeType *volatile *a = NULL;", Spaces);
17974   verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17975   verifyFormat("std::vector<SomeType *const *> x;", Spaces);
17976   verifyFormat("std::vector<SomeType *qualified *> x;", Spaces);
17977   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
17978   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
17979   verifyFormat("SomeType * volatile *a = NULL;", Spaces);
17980   verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces);
17981   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
17982   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
17983   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
17984 
17985   // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
17986   Spaces.PointerAlignment = FormatStyle::PAS_Left;
17987   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
17988   verifyFormat("SomeType* volatile* a = NULL;", Spaces);
17989   verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces);
17990   verifyFormat("std::vector<SomeType* const*> x;", Spaces);
17991   verifyFormat("std::vector<SomeType* qualified*> x;", Spaces);
17992   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
17993   // However, setting it to SAPQ_After should add spaces after __attribute, etc.
17994   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
17995   verifyFormat("SomeType* volatile * a = NULL;", Spaces);
17996   verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces);
17997   verifyFormat("std::vector<SomeType* const *> x;", Spaces);
17998   verifyFormat("std::vector<SomeType* qualified *> x;", Spaces);
17999   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
18000 
18001   // PAS_Middle should not have any noticeable changes even for SAPQ_Both
18002   Spaces.PointerAlignment = FormatStyle::PAS_Middle;
18003   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
18004   verifyFormat("SomeType * volatile * a = NULL;", Spaces);
18005   verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces);
18006   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
18007   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
18008   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
18009 }
18010 
18011 TEST_F(FormatTest, AlignConsecutiveMacros) {
18012   FormatStyle Style = getLLVMStyle();
18013   Style.AlignConsecutiveAssignments.Enabled = true;
18014   Style.AlignConsecutiveDeclarations.Enabled = true;
18015 
18016   verifyFormat("#define a 3\n"
18017                "#define bbbb 4\n"
18018                "#define ccc (5)",
18019                Style);
18020 
18021   verifyFormat("#define f(x) (x * x)\n"
18022                "#define fff(x, y, z) (x * y + z)\n"
18023                "#define ffff(x, y) (x - y)",
18024                Style);
18025 
18026   verifyFormat("#define foo(x, y) (x + y)\n"
18027                "#define bar (5, 6)(2 + 2)",
18028                Style);
18029 
18030   verifyFormat("#define a 3\n"
18031                "#define bbbb 4\n"
18032                "#define ccc (5)\n"
18033                "#define f(x) (x * x)\n"
18034                "#define fff(x, y, z) (x * y + z)\n"
18035                "#define ffff(x, y) (x - y)",
18036                Style);
18037 
18038   Style.AlignConsecutiveMacros.Enabled = true;
18039   verifyFormat("#define a    3\n"
18040                "#define bbbb 4\n"
18041                "#define ccc  (5)",
18042                Style);
18043 
18044   verifyFormat("#define true  1\n"
18045                "#define false 0",
18046                Style);
18047 
18048   verifyFormat("#define f(x)         (x * x)\n"
18049                "#define fff(x, y, z) (x * y + z)\n"
18050                "#define ffff(x, y)   (x - y)",
18051                Style);
18052 
18053   verifyFormat("#define foo(x, y) (x + y)\n"
18054                "#define bar       (5, 6)(2 + 2)",
18055                Style);
18056 
18057   verifyFormat("#define a            3\n"
18058                "#define bbbb         4\n"
18059                "#define ccc          (5)\n"
18060                "#define f(x)         (x * x)\n"
18061                "#define fff(x, y, z) (x * y + z)\n"
18062                "#define ffff(x, y)   (x - y)",
18063                Style);
18064 
18065   verifyFormat("#define a         5\n"
18066                "#define foo(x, y) (x + y)\n"
18067                "#define CCC       (6)\n"
18068                "auto lambda = []() {\n"
18069                "  auto  ii = 0;\n"
18070                "  float j  = 0;\n"
18071                "  return 0;\n"
18072                "};\n"
18073                "int   i  = 0;\n"
18074                "float i2 = 0;\n"
18075                "auto  v  = type{\n"
18076                "    i = 1,   //\n"
18077                "    (i = 2), //\n"
18078                "    i = 3    //\n"
18079                "};",
18080                Style);
18081 
18082   Style.AlignConsecutiveMacros.Enabled = false;
18083   Style.ColumnLimit = 20;
18084 
18085   verifyFormat("#define a          \\\n"
18086                "  \"aabbbbbbbbbbbb\"\n"
18087                "#define D          \\\n"
18088                "  \"aabbbbbbbbbbbb\" \\\n"
18089                "  \"ccddeeeeeeeee\"\n"
18090                "#define B          \\\n"
18091                "  \"QQQQQQQQQQQQQ\"  \\\n"
18092                "  \"FFFFFFFFFFFFF\"  \\\n"
18093                "  \"LLLLLLLL\"",
18094                Style);
18095 
18096   Style.AlignConsecutiveMacros.Enabled = true;
18097   verifyFormat("#define a          \\\n"
18098                "  \"aabbbbbbbbbbbb\"\n"
18099                "#define D          \\\n"
18100                "  \"aabbbbbbbbbbbb\" \\\n"
18101                "  \"ccddeeeeeeeee\"\n"
18102                "#define B          \\\n"
18103                "  \"QQQQQQQQQQQQQ\"  \\\n"
18104                "  \"FFFFFFFFFFFFF\"  \\\n"
18105                "  \"LLLLLLLL\"",
18106                Style);
18107 
18108   // Test across comments
18109   Style.MaxEmptyLinesToKeep = 10;
18110   Style.ReflowComments = false;
18111   Style.AlignConsecutiveMacros.AcrossComments = true;
18112   verifyFormat("#define a    3\n"
18113                "// line comment\n"
18114                "#define bbbb 4\n"
18115                "#define ccc  (5)",
18116                "#define a 3\n"
18117                "// line comment\n"
18118                "#define bbbb 4\n"
18119                "#define ccc (5)",
18120                Style);
18121 
18122   verifyFormat("#define a    3\n"
18123                "/* block comment */\n"
18124                "#define bbbb 4\n"
18125                "#define ccc  (5)",
18126                "#define a  3\n"
18127                "/* block comment */\n"
18128                "#define bbbb 4\n"
18129                "#define ccc (5)",
18130                Style);
18131 
18132   verifyFormat("#define a    3\n"
18133                "/* multi-line *\n"
18134                " * block comment */\n"
18135                "#define bbbb 4\n"
18136                "#define ccc  (5)",
18137                "#define a 3\n"
18138                "/* multi-line *\n"
18139                " * block comment */\n"
18140                "#define bbbb 4\n"
18141                "#define ccc (5)",
18142                Style);
18143 
18144   verifyFormat("#define a    3\n"
18145                "// multi-line line comment\n"
18146                "//\n"
18147                "#define bbbb 4\n"
18148                "#define ccc  (5)",
18149                "#define a  3\n"
18150                "// multi-line line comment\n"
18151                "//\n"
18152                "#define bbbb 4\n"
18153                "#define ccc (5)",
18154                Style);
18155 
18156   verifyFormat("#define a 3\n"
18157                "// empty lines still break.\n"
18158                "\n"
18159                "#define bbbb 4\n"
18160                "#define ccc  (5)",
18161                "#define a     3\n"
18162                "// empty lines still break.\n"
18163                "\n"
18164                "#define bbbb     4\n"
18165                "#define ccc  (5)",
18166                Style);
18167 
18168   // Test across empty lines
18169   Style.AlignConsecutiveMacros.AcrossComments = false;
18170   Style.AlignConsecutiveMacros.AcrossEmptyLines = true;
18171   verifyFormat("#define a    3\n"
18172                "\n"
18173                "#define bbbb 4\n"
18174                "#define ccc  (5)",
18175                "#define a 3\n"
18176                "\n"
18177                "#define bbbb 4\n"
18178                "#define ccc (5)",
18179                Style);
18180 
18181   verifyFormat("#define a    3\n"
18182                "\n"
18183                "\n"
18184                "\n"
18185                "#define bbbb 4\n"
18186                "#define ccc  (5)",
18187                "#define a        3\n"
18188                "\n"
18189                "\n"
18190                "\n"
18191                "#define bbbb 4\n"
18192                "#define ccc (5)",
18193                Style);
18194 
18195   verifyFormat("#define a 3\n"
18196                "// comments should break alignment\n"
18197                "//\n"
18198                "#define bbbb 4\n"
18199                "#define ccc  (5)",
18200                "#define a        3\n"
18201                "// comments should break alignment\n"
18202                "//\n"
18203                "#define bbbb 4\n"
18204                "#define ccc (5)",
18205                Style);
18206 
18207   // Test across empty lines and comments
18208   Style.AlignConsecutiveMacros.AcrossComments = true;
18209   verifyFormat("#define a    3\n"
18210                "\n"
18211                "// line comment\n"
18212                "#define bbbb 4\n"
18213                "#define ccc  (5)",
18214                Style);
18215 
18216   verifyFormat("#define a    3\n"
18217                "\n"
18218                "\n"
18219                "/* multi-line *\n"
18220                " * block comment */\n"
18221                "\n"
18222                "\n"
18223                "#define bbbb 4\n"
18224                "#define ccc  (5)",
18225                "#define a 3\n"
18226                "\n"
18227                "\n"
18228                "/* multi-line *\n"
18229                " * block comment */\n"
18230                "\n"
18231                "\n"
18232                "#define bbbb 4\n"
18233                "#define ccc (5)",
18234                Style);
18235 
18236   verifyFormat("#define a    3\n"
18237                "\n"
18238                "\n"
18239                "/* multi-line *\n"
18240                " * block comment */\n"
18241                "\n"
18242                "\n"
18243                "#define bbbb 4\n"
18244                "#define ccc  (5)",
18245                "#define a 3\n"
18246                "\n"
18247                "\n"
18248                "/* multi-line *\n"
18249                " * block comment */\n"
18250                "\n"
18251                "\n"
18252                "#define bbbb 4\n"
18253                "#define ccc       (5)",
18254                Style);
18255 }
18256 
18257 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
18258   FormatStyle Alignment = getLLVMStyle();
18259   Alignment.AlignConsecutiveMacros.Enabled = true;
18260   Alignment.AlignConsecutiveAssignments.Enabled = true;
18261   Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
18262 
18263   Alignment.MaxEmptyLinesToKeep = 10;
18264   /* Test alignment across empty lines */
18265   verifyFormat("int a           = 5;\n"
18266                "\n"
18267                "int oneTwoThree = 123;",
18268                "int a       = 5;\n"
18269                "\n"
18270                "int oneTwoThree= 123;",
18271                Alignment);
18272   verifyFormat("int a           = 5;\n"
18273                "int one         = 1;\n"
18274                "\n"
18275                "int oneTwoThree = 123;",
18276                "int a = 5;\n"
18277                "int one = 1;\n"
18278                "\n"
18279                "int oneTwoThree = 123;",
18280                Alignment);
18281   verifyFormat("int a           = 5;\n"
18282                "int one         = 1;\n"
18283                "\n"
18284                "int oneTwoThree = 123;\n"
18285                "int oneTwo      = 12;",
18286                "int a = 5;\n"
18287                "int one = 1;\n"
18288                "\n"
18289                "int oneTwoThree = 123;\n"
18290                "int oneTwo = 12;",
18291                Alignment);
18292 
18293   /* Test across comments */
18294   verifyFormat("int a = 5;\n"
18295                "/* block comment */\n"
18296                "int oneTwoThree = 123;",
18297                "int a = 5;\n"
18298                "/* block comment */\n"
18299                "int oneTwoThree=123;",
18300                Alignment);
18301 
18302   verifyFormat("int a = 5;\n"
18303                "// line comment\n"
18304                "int oneTwoThree = 123;",
18305                "int a = 5;\n"
18306                "// line comment\n"
18307                "int oneTwoThree=123;",
18308                Alignment);
18309 
18310   /* Test across comments and newlines */
18311   verifyFormat("int a = 5;\n"
18312                "\n"
18313                "/* block comment */\n"
18314                "int oneTwoThree = 123;",
18315                "int a = 5;\n"
18316                "\n"
18317                "/* block comment */\n"
18318                "int oneTwoThree=123;",
18319                Alignment);
18320 
18321   verifyFormat("int a = 5;\n"
18322                "\n"
18323                "// line comment\n"
18324                "int oneTwoThree = 123;",
18325                "int a = 5;\n"
18326                "\n"
18327                "// line comment\n"
18328                "int oneTwoThree=123;",
18329                Alignment);
18330 }
18331 
18332 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
18333   FormatStyle Alignment = getLLVMStyle();
18334   Alignment.AlignConsecutiveDeclarations.Enabled = true;
18335   Alignment.AlignConsecutiveDeclarations.AcrossEmptyLines = true;
18336   Alignment.AlignConsecutiveDeclarations.AcrossComments = true;
18337 
18338   Alignment.MaxEmptyLinesToKeep = 10;
18339   /* Test alignment across empty lines */
18340   verifyFormat("int         a = 5;\n"
18341                "\n"
18342                "float const oneTwoThree = 123;",
18343                "int a = 5;\n"
18344                "\n"
18345                "float const oneTwoThree = 123;",
18346                Alignment);
18347   verifyFormat("int         a = 5;\n"
18348                "float const one = 1;\n"
18349                "\n"
18350                "int         oneTwoThree = 123;",
18351                "int a = 5;\n"
18352                "float const one = 1;\n"
18353                "\n"
18354                "int oneTwoThree = 123;",
18355                Alignment);
18356 
18357   /* Test across comments */
18358   verifyFormat("float const a = 5;\n"
18359                "/* block comment */\n"
18360                "int         oneTwoThree = 123;",
18361                "float const a = 5;\n"
18362                "/* block comment */\n"
18363                "int oneTwoThree=123;",
18364                Alignment);
18365 
18366   verifyFormat("float const a = 5;\n"
18367                "// line comment\n"
18368                "int         oneTwoThree = 123;",
18369                "float const a = 5;\n"
18370                "// line comment\n"
18371                "int oneTwoThree=123;",
18372                Alignment);
18373 
18374   /* Test across comments and newlines */
18375   verifyFormat("float const a = 5;\n"
18376                "\n"
18377                "/* block comment */\n"
18378                "int         oneTwoThree = 123;",
18379                "float const a = 5;\n"
18380                "\n"
18381                "/* block comment */\n"
18382                "int         oneTwoThree=123;",
18383                Alignment);
18384 
18385   verifyFormat("float const a = 5;\n"
18386                "\n"
18387                "// line comment\n"
18388                "int         oneTwoThree = 123;",
18389                "float const a = 5;\n"
18390                "\n"
18391                "// line comment\n"
18392                "int oneTwoThree=123;",
18393                Alignment);
18394 }
18395 
18396 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) {
18397   FormatStyle Alignment = getLLVMStyle();
18398   Alignment.AlignConsecutiveBitFields.Enabled = true;
18399   Alignment.AlignConsecutiveBitFields.AcrossEmptyLines = true;
18400   Alignment.AlignConsecutiveBitFields.AcrossComments = true;
18401 
18402   Alignment.MaxEmptyLinesToKeep = 10;
18403   /* Test alignment across empty lines */
18404   verifyFormat("int a            : 5;\n"
18405                "\n"
18406                "int longbitfield : 6;",
18407                "int a : 5;\n"
18408                "\n"
18409                "int longbitfield : 6;",
18410                Alignment);
18411   verifyFormat("int a            : 5;\n"
18412                "int one          : 1;\n"
18413                "\n"
18414                "int longbitfield : 6;",
18415                "int a : 5;\n"
18416                "int one : 1;\n"
18417                "\n"
18418                "int longbitfield : 6;",
18419                Alignment);
18420 
18421   /* Test across comments */
18422   verifyFormat("int a            : 5;\n"
18423                "/* block comment */\n"
18424                "int longbitfield : 6;",
18425                "int a : 5;\n"
18426                "/* block comment */\n"
18427                "int longbitfield : 6;",
18428                Alignment);
18429   verifyFormat("int a            : 5;\n"
18430                "int one          : 1;\n"
18431                "// line comment\n"
18432                "int longbitfield : 6;",
18433                "int a : 5;\n"
18434                "int one : 1;\n"
18435                "// line comment\n"
18436                "int longbitfield : 6;",
18437                Alignment);
18438 
18439   /* Test across comments and newlines */
18440   verifyFormat("int a            : 5;\n"
18441                "/* block comment */\n"
18442                "\n"
18443                "int longbitfield : 6;",
18444                "int a : 5;\n"
18445                "/* block comment */\n"
18446                "\n"
18447                "int longbitfield : 6;",
18448                Alignment);
18449   verifyFormat("int a            : 5;\n"
18450                "int one          : 1;\n"
18451                "\n"
18452                "// line comment\n"
18453                "\n"
18454                "int longbitfield : 6;",
18455                "int a : 5;\n"
18456                "int one : 1;\n"
18457                "\n"
18458                "// line comment \n"
18459                "\n"
18460                "int longbitfield : 6;",
18461                Alignment);
18462 }
18463 
18464 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) {
18465   FormatStyle Alignment = getLLVMStyle();
18466   Alignment.AlignConsecutiveMacros.Enabled = true;
18467   Alignment.AlignConsecutiveAssignments.Enabled = true;
18468   Alignment.AlignConsecutiveAssignments.AcrossComments = true;
18469 
18470   Alignment.MaxEmptyLinesToKeep = 10;
18471   /* Test alignment across empty lines */
18472   verifyFormat("int a = 5;\n"
18473                "\n"
18474                "int oneTwoThree = 123;",
18475                "int a       = 5;\n"
18476                "\n"
18477                "int oneTwoThree= 123;",
18478                Alignment);
18479   verifyFormat("int a   = 5;\n"
18480                "int one = 1;\n"
18481                "\n"
18482                "int oneTwoThree = 123;",
18483                "int a = 5;\n"
18484                "int one = 1;\n"
18485                "\n"
18486                "int oneTwoThree = 123;",
18487                Alignment);
18488 
18489   /* Test across comments */
18490   verifyFormat("int a           = 5;\n"
18491                "/* block comment */\n"
18492                "int oneTwoThree = 123;",
18493                "int a = 5;\n"
18494                "/* block comment */\n"
18495                "int oneTwoThree=123;",
18496                Alignment);
18497 
18498   verifyFormat("int a           = 5;\n"
18499                "// line comment\n"
18500                "int oneTwoThree = 123;",
18501                "int a = 5;\n"
18502                "// line comment\n"
18503                "int oneTwoThree=123;",
18504                Alignment);
18505 
18506   verifyFormat("int a           = 5;\n"
18507                "/*\n"
18508                " * multi-line block comment\n"
18509                " */\n"
18510                "int oneTwoThree = 123;",
18511                "int a = 5;\n"
18512                "/*\n"
18513                " * multi-line block comment\n"
18514                " */\n"
18515                "int oneTwoThree=123;",
18516                Alignment);
18517 
18518   verifyFormat("int a           = 5;\n"
18519                "//\n"
18520                "// multi-line line comment\n"
18521                "//\n"
18522                "int oneTwoThree = 123;",
18523                "int a = 5;\n"
18524                "//\n"
18525                "// multi-line line comment\n"
18526                "//\n"
18527                "int oneTwoThree=123;",
18528                Alignment);
18529 
18530   /* Test across comments and newlines */
18531   verifyFormat("int a = 5;\n"
18532                "\n"
18533                "/* block comment */\n"
18534                "int oneTwoThree = 123;",
18535                "int a = 5;\n"
18536                "\n"
18537                "/* block comment */\n"
18538                "int oneTwoThree=123;",
18539                Alignment);
18540 
18541   verifyFormat("int a = 5;\n"
18542                "\n"
18543                "// line comment\n"
18544                "int oneTwoThree = 123;",
18545                "int a = 5;\n"
18546                "\n"
18547                "// line comment\n"
18548                "int oneTwoThree=123;",
18549                Alignment);
18550 }
18551 
18552 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) {
18553   FormatStyle Alignment = getLLVMStyle();
18554   Alignment.AlignConsecutiveMacros.Enabled = true;
18555   Alignment.AlignConsecutiveAssignments.Enabled = true;
18556   Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
18557   Alignment.AlignConsecutiveAssignments.AcrossComments = true;
18558   verifyFormat("int a           = 5;\n"
18559                "int oneTwoThree = 123;",
18560                Alignment);
18561   verifyFormat("int a           = method();\n"
18562                "int oneTwoThree = 133;",
18563                Alignment);
18564   verifyFormat("a &= 5;\n"
18565                "bcd *= 5;\n"
18566                "ghtyf += 5;\n"
18567                "dvfvdb -= 5;\n"
18568                "a /= 5;\n"
18569                "vdsvsv %= 5;\n"
18570                "sfdbddfbdfbb ^= 5;\n"
18571                "dvsdsv |= 5;\n"
18572                "int dsvvdvsdvvv = 123;",
18573                Alignment);
18574   verifyFormat("int i = 1, j = 10;\n"
18575                "something = 2000;",
18576                Alignment);
18577   verifyFormat("something = 2000;\n"
18578                "int i = 1, j = 10;",
18579                Alignment);
18580   verifyFormat("something = 2000;\n"
18581                "another   = 911;\n"
18582                "int i = 1, j = 10;\n"
18583                "oneMore = 1;\n"
18584                "i       = 2;",
18585                Alignment);
18586   verifyFormat("int a   = 5;\n"
18587                "int one = 1;\n"
18588                "method();\n"
18589                "int oneTwoThree = 123;\n"
18590                "int oneTwo      = 12;",
18591                Alignment);
18592   verifyFormat("int oneTwoThree = 123;\n"
18593                "int oneTwo      = 12;\n"
18594                "method();",
18595                Alignment);
18596   verifyFormat("int oneTwoThree = 123; // comment\n"
18597                "int oneTwo      = 12;  // comment",
18598                Alignment);
18599 
18600   // Bug 25167
18601   /* Uncomment when fixed
18602     verifyFormat("#if A\n"
18603                  "#else\n"
18604                  "int aaaaaaaa = 12;\n"
18605                  "#endif\n"
18606                  "#if B\n"
18607                  "#else\n"
18608                  "int a = 12;\n"
18609                  "#endif",
18610                  Alignment);
18611     verifyFormat("enum foo {\n"
18612                  "#if A\n"
18613                  "#else\n"
18614                  "  aaaaaaaa = 12;\n"
18615                  "#endif\n"
18616                  "#if B\n"
18617                  "#else\n"
18618                  "  a = 12;\n"
18619                  "#endif\n"
18620                  "};",
18621                  Alignment);
18622   */
18623 
18624   Alignment.MaxEmptyLinesToKeep = 10;
18625   /* Test alignment across empty lines */
18626   verifyFormat("int a           = 5;\n"
18627                "\n"
18628                "int oneTwoThree = 123;",
18629                "int a       = 5;\n"
18630                "\n"
18631                "int oneTwoThree= 123;",
18632                Alignment);
18633   verifyFormat("int a           = 5;\n"
18634                "int one         = 1;\n"
18635                "\n"
18636                "int oneTwoThree = 123;",
18637                "int a = 5;\n"
18638                "int one = 1;\n"
18639                "\n"
18640                "int oneTwoThree = 123;",
18641                Alignment);
18642   verifyFormat("int a           = 5;\n"
18643                "int one         = 1;\n"
18644                "\n"
18645                "int oneTwoThree = 123;\n"
18646                "int oneTwo      = 12;",
18647                "int a = 5;\n"
18648                "int one = 1;\n"
18649                "\n"
18650                "int oneTwoThree = 123;\n"
18651                "int oneTwo = 12;",
18652                Alignment);
18653 
18654   /* Test across comments */
18655   verifyFormat("int a           = 5;\n"
18656                "/* block comment */\n"
18657                "int oneTwoThree = 123;",
18658                "int a = 5;\n"
18659                "/* block comment */\n"
18660                "int oneTwoThree=123;",
18661                Alignment);
18662 
18663   verifyFormat("int a           = 5;\n"
18664                "// line comment\n"
18665                "int oneTwoThree = 123;",
18666                "int a = 5;\n"
18667                "// line comment\n"
18668                "int oneTwoThree=123;",
18669                Alignment);
18670 
18671   /* Test across comments and newlines */
18672   verifyFormat("int a           = 5;\n"
18673                "\n"
18674                "/* block comment */\n"
18675                "int oneTwoThree = 123;",
18676                "int a = 5;\n"
18677                "\n"
18678                "/* block comment */\n"
18679                "int oneTwoThree=123;",
18680                Alignment);
18681 
18682   verifyFormat("int a           = 5;\n"
18683                "\n"
18684                "// line comment\n"
18685                "int oneTwoThree = 123;",
18686                "int a = 5;\n"
18687                "\n"
18688                "// line comment\n"
18689                "int oneTwoThree=123;",
18690                Alignment);
18691 
18692   verifyFormat("int a           = 5;\n"
18693                "//\n"
18694                "// multi-line line comment\n"
18695                "//\n"
18696                "int oneTwoThree = 123;",
18697                "int a = 5;\n"
18698                "//\n"
18699                "// multi-line line comment\n"
18700                "//\n"
18701                "int oneTwoThree=123;",
18702                Alignment);
18703 
18704   verifyFormat("int a           = 5;\n"
18705                "/*\n"
18706                " *  multi-line block comment\n"
18707                " */\n"
18708                "int oneTwoThree = 123;",
18709                "int a = 5;\n"
18710                "/*\n"
18711                " *  multi-line block comment\n"
18712                " */\n"
18713                "int oneTwoThree=123;",
18714                Alignment);
18715 
18716   verifyFormat("int a           = 5;\n"
18717                "\n"
18718                "/* block comment */\n"
18719                "\n"
18720                "\n"
18721                "\n"
18722                "int oneTwoThree = 123;",
18723                "int a = 5;\n"
18724                "\n"
18725                "/* block comment */\n"
18726                "\n"
18727                "\n"
18728                "\n"
18729                "int oneTwoThree=123;",
18730                Alignment);
18731 
18732   verifyFormat("int a           = 5;\n"
18733                "\n"
18734                "// line comment\n"
18735                "\n"
18736                "\n"
18737                "\n"
18738                "int oneTwoThree = 123;",
18739                "int a = 5;\n"
18740                "\n"
18741                "// line comment\n"
18742                "\n"
18743                "\n"
18744                "\n"
18745                "int oneTwoThree=123;",
18746                Alignment);
18747 
18748   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
18749   verifyFormat("#define A \\\n"
18750                "  int aaaa       = 12; \\\n"
18751                "  int b          = 23; \\\n"
18752                "  int ccc        = 234; \\\n"
18753                "  int dddddddddd = 2345;",
18754                Alignment);
18755   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
18756   verifyFormat("#define A               \\\n"
18757                "  int aaaa       = 12;  \\\n"
18758                "  int b          = 23;  \\\n"
18759                "  int ccc        = 234; \\\n"
18760                "  int dddddddddd = 2345;",
18761                Alignment);
18762   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
18763   verifyFormat("#define A                                                      "
18764                "                \\\n"
18765                "  int aaaa       = 12;                                         "
18766                "                \\\n"
18767                "  int b          = 23;                                         "
18768                "                \\\n"
18769                "  int ccc        = 234;                                        "
18770                "                \\\n"
18771                "  int dddddddddd = 2345;",
18772                Alignment);
18773   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
18774                "k = 4, int l = 5,\n"
18775                "                  int m = 6) {\n"
18776                "  int j      = 10;\n"
18777                "  otherThing = 1;\n"
18778                "}",
18779                Alignment);
18780   verifyFormat("void SomeFunction(int parameter = 0) {\n"
18781                "  int i   = 1;\n"
18782                "  int j   = 2;\n"
18783                "  int big = 10000;\n"
18784                "}",
18785                Alignment);
18786   verifyFormat("class C {\n"
18787                "public:\n"
18788                "  int i            = 1;\n"
18789                "  virtual void f() = 0;\n"
18790                "};",
18791                Alignment);
18792   verifyFormat("int i = 1;\n"
18793                "if (SomeType t = getSomething()) {\n"
18794                "}\n"
18795                "int j   = 2;\n"
18796                "int big = 10000;",
18797                Alignment);
18798   verifyFormat("int j = 7;\n"
18799                "for (int k = 0; k < N; ++k) {\n"
18800                "}\n"
18801                "int j   = 2;\n"
18802                "int big = 10000;\n"
18803                "}",
18804                Alignment);
18805   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
18806   verifyFormat("int i = 1;\n"
18807                "LooooooooooongType loooooooooooooooooooooongVariable\n"
18808                "    = someLooooooooooooooooongFunction();\n"
18809                "int j = 2;",
18810                Alignment);
18811   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
18812   verifyFormat("int i = 1;\n"
18813                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
18814                "    someLooooooooooooooooongFunction();\n"
18815                "int j = 2;",
18816                Alignment);
18817 
18818   verifyFormat("auto lambda = []() {\n"
18819                "  auto i = 0;\n"
18820                "  return 0;\n"
18821                "};\n"
18822                "int i  = 0;\n"
18823                "auto v = type{\n"
18824                "    i = 1,   //\n"
18825                "    (i = 2), //\n"
18826                "    i = 3    //\n"
18827                "};",
18828                Alignment);
18829 
18830   verifyFormat(
18831       "int i      = 1;\n"
18832       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
18833       "                          loooooooooooooooooooooongParameterB);\n"
18834       "int j      = 2;",
18835       Alignment);
18836 
18837   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
18838                "          typename B   = very_long_type_name_1,\n"
18839                "          typename T_2 = very_long_type_name_2>\n"
18840                "auto foo() {}",
18841                Alignment);
18842   verifyFormat("int a, b = 1;\n"
18843                "int c  = 2;\n"
18844                "int dd = 3;",
18845                Alignment);
18846   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
18847                "float b[1][] = {{3.f}};",
18848                Alignment);
18849   verifyFormat("for (int i = 0; i < 1; i++)\n"
18850                "  int x = 1;",
18851                Alignment);
18852   verifyFormat("for (i = 0; i < 1; i++)\n"
18853                "  x = 1;\n"
18854                "y = 1;",
18855                Alignment);
18856 
18857   Alignment.ReflowComments = true;
18858   Alignment.ColumnLimit = 50;
18859   verifyFormat("int x   = 0;\n"
18860                "int yy  = 1; /// specificlennospace\n"
18861                "int zzz = 2;",
18862                "int x   = 0;\n"
18863                "int yy  = 1; ///specificlennospace\n"
18864                "int zzz = 2;",
18865                Alignment);
18866 }
18867 
18868 TEST_F(FormatTest, AlignCompoundAssignments) {
18869   FormatStyle Alignment = getLLVMStyle();
18870   Alignment.AlignConsecutiveAssignments.Enabled = true;
18871   Alignment.AlignConsecutiveAssignments.AlignCompound = true;
18872   Alignment.AlignConsecutiveAssignments.PadOperators = false;
18873   verifyFormat("sfdbddfbdfbb    = 5;\n"
18874                "dvsdsv          = 5;\n"
18875                "int dsvvdvsdvvv = 123;",
18876                Alignment);
18877   verifyFormat("sfdbddfbdfbb   ^= 5;\n"
18878                "dvsdsv         |= 5;\n"
18879                "int dsvvdvsdvvv = 123;",
18880                Alignment);
18881   verifyFormat("sfdbddfbdfbb   ^= 5;\n"
18882                "dvsdsv        <<= 5;\n"
18883                "int dsvvdvsdvvv = 123;",
18884                Alignment);
18885   verifyFormat("int xxx = 5;\n"
18886                "xxx     = 5;\n"
18887                "{\n"
18888                "  int yyy = 6;\n"
18889                "  yyy     = 6;\n"
18890                "}",
18891                Alignment);
18892   verifyFormat("int xxx = 5;\n"
18893                "xxx    += 5;\n"
18894                "{\n"
18895                "  int yyy = 6;\n"
18896                "  yyy    += 6;\n"
18897                "}",
18898                Alignment);
18899   // Test that `<=` is not treated as a compound assignment.
18900   verifyFormat("aa &= 5;\n"
18901                "b <= 10;\n"
18902                "c = 15;",
18903                Alignment);
18904   Alignment.AlignConsecutiveAssignments.PadOperators = true;
18905   verifyFormat("sfdbddfbdfbb    = 5;\n"
18906                "dvsdsv          = 5;\n"
18907                "int dsvvdvsdvvv = 123;",
18908                Alignment);
18909   verifyFormat("sfdbddfbdfbb    ^= 5;\n"
18910                "dvsdsv          |= 5;\n"
18911                "int dsvvdvsdvvv  = 123;",
18912                Alignment);
18913   verifyFormat("sfdbddfbdfbb     ^= 5;\n"
18914                "dvsdsv          <<= 5;\n"
18915                "int dsvvdvsdvvv   = 123;",
18916                Alignment);
18917   verifyFormat("a   += 5;\n"
18918                "one  = 1;\n"
18919                "\n"
18920                "oneTwoThree = 123;",
18921                "a += 5;\n"
18922                "one = 1;\n"
18923                "\n"
18924                "oneTwoThree = 123;",
18925                Alignment);
18926   verifyFormat("a   += 5;\n"
18927                "one  = 1;\n"
18928                "//\n"
18929                "oneTwoThree = 123;",
18930                "a += 5;\n"
18931                "one = 1;\n"
18932                "//\n"
18933                "oneTwoThree = 123;",
18934                Alignment);
18935   Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
18936   verifyFormat("a           += 5;\n"
18937                "one          = 1;\n"
18938                "\n"
18939                "oneTwoThree  = 123;",
18940                "a += 5;\n"
18941                "one = 1;\n"
18942                "\n"
18943                "oneTwoThree = 123;",
18944                Alignment);
18945   verifyFormat("a   += 5;\n"
18946                "one  = 1;\n"
18947                "//\n"
18948                "oneTwoThree = 123;",
18949                "a += 5;\n"
18950                "one = 1;\n"
18951                "//\n"
18952                "oneTwoThree = 123;",
18953                Alignment);
18954   Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = false;
18955   Alignment.AlignConsecutiveAssignments.AcrossComments = true;
18956   verifyFormat("a   += 5;\n"
18957                "one  = 1;\n"
18958                "\n"
18959                "oneTwoThree = 123;",
18960                "a += 5;\n"
18961                "one = 1;\n"
18962                "\n"
18963                "oneTwoThree = 123;",
18964                Alignment);
18965   verifyFormat("a           += 5;\n"
18966                "one          = 1;\n"
18967                "//\n"
18968                "oneTwoThree  = 123;",
18969                "a += 5;\n"
18970                "one = 1;\n"
18971                "//\n"
18972                "oneTwoThree = 123;",
18973                Alignment);
18974   Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
18975   verifyFormat("a            += 5;\n"
18976                "one         >>= 1;\n"
18977                "\n"
18978                "oneTwoThree   = 123;",
18979                "a += 5;\n"
18980                "one >>= 1;\n"
18981                "\n"
18982                "oneTwoThree = 123;",
18983                Alignment);
18984   verifyFormat("a            += 5;\n"
18985                "one           = 1;\n"
18986                "//\n"
18987                "oneTwoThree <<= 123;",
18988                "a += 5;\n"
18989                "one = 1;\n"
18990                "//\n"
18991                "oneTwoThree <<= 123;",
18992                Alignment);
18993 }
18994 
18995 TEST_F(FormatTest, AlignConsecutiveAssignments) {
18996   FormatStyle Alignment = getLLVMStyle();
18997   Alignment.AlignConsecutiveMacros.Enabled = true;
18998   verifyFormat("int a = 5;\n"
18999                "int oneTwoThree = 123;",
19000                Alignment);
19001   verifyFormat("int a = 5;\n"
19002                "int oneTwoThree = 123;",
19003                Alignment);
19004 
19005   Alignment.AlignConsecutiveAssignments.Enabled = true;
19006   verifyFormat("int a           = 5;\n"
19007                "int oneTwoThree = 123;",
19008                Alignment);
19009   verifyFormat("int a           = method();\n"
19010                "int oneTwoThree = 133;",
19011                Alignment);
19012   verifyFormat("aa <= 5;\n"
19013                "a &= 5;\n"
19014                "bcd *= 5;\n"
19015                "ghtyf += 5;\n"
19016                "dvfvdb -= 5;\n"
19017                "a /= 5;\n"
19018                "vdsvsv %= 5;\n"
19019                "sfdbddfbdfbb ^= 5;\n"
19020                "dvsdsv |= 5;\n"
19021                "int dsvvdvsdvvv = 123;",
19022                Alignment);
19023   verifyFormat("int i = 1, j = 10;\n"
19024                "something = 2000;",
19025                Alignment);
19026   verifyFormat("something = 2000;\n"
19027                "int i = 1, j = 10;",
19028                Alignment);
19029   verifyFormat("something = 2000;\n"
19030                "another   = 911;\n"
19031                "int i = 1, j = 10;\n"
19032                "oneMore = 1;\n"
19033                "i       = 2;",
19034                Alignment);
19035   verifyFormat("int a   = 5;\n"
19036                "int one = 1;\n"
19037                "method();\n"
19038                "int oneTwoThree = 123;\n"
19039                "int oneTwo      = 12;",
19040                Alignment);
19041   verifyFormat("int oneTwoThree = 123;\n"
19042                "int oneTwo      = 12;\n"
19043                "method();",
19044                Alignment);
19045   verifyFormat("int oneTwoThree = 123; // comment\n"
19046                "int oneTwo      = 12;  // comment",
19047                Alignment);
19048   verifyFormat("int f()         = default;\n"
19049                "int &operator() = default;\n"
19050                "int &operator=() {",
19051                Alignment);
19052   verifyFormat("int f()         = delete;\n"
19053                "int &operator() = delete;\n"
19054                "int &operator=() {",
19055                Alignment);
19056   verifyFormat("int f()         = default; // comment\n"
19057                "int &operator() = default; // comment\n"
19058                "int &operator=() {",
19059                Alignment);
19060   verifyFormat("int f()         = default;\n"
19061                "int &operator() = default;\n"
19062                "int &operator==() {",
19063                Alignment);
19064   verifyFormat("int f()         = default;\n"
19065                "int &operator() = default;\n"
19066                "int &operator<=() {",
19067                Alignment);
19068   verifyFormat("int f()         = default;\n"
19069                "int &operator() = default;\n"
19070                "int &operator!=() {",
19071                Alignment);
19072   verifyFormat("int f()         = default;\n"
19073                "int &operator() = default;\n"
19074                "int &operator=();",
19075                Alignment);
19076   verifyFormat("int f()         = delete;\n"
19077                "int &operator() = delete;\n"
19078                "int &operator=();",
19079                Alignment);
19080   verifyFormat("/* long long padding */ int f() = default;\n"
19081                "int &operator()                 = default;\n"
19082                "int &operator/**/ =();",
19083                Alignment);
19084   // https://llvm.org/PR33697
19085   FormatStyle AlignmentWithPenalty = getLLVMStyle();
19086   AlignmentWithPenalty.AlignConsecutiveAssignments.Enabled = true;
19087   AlignmentWithPenalty.PenaltyReturnTypeOnItsOwnLine = 5000;
19088   verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n"
19089                "  void f() = delete;\n"
19090                "  SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n"
19091                "      const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n"
19092                "};",
19093                AlignmentWithPenalty);
19094 
19095   // Bug 25167
19096   /* Uncomment when fixed
19097     verifyFormat("#if A\n"
19098                  "#else\n"
19099                  "int aaaaaaaa = 12;\n"
19100                  "#endif\n"
19101                  "#if B\n"
19102                  "#else\n"
19103                  "int a = 12;\n"
19104                  "#endif",
19105                  Alignment);
19106     verifyFormat("enum foo {\n"
19107                  "#if A\n"
19108                  "#else\n"
19109                  "  aaaaaaaa = 12;\n"
19110                  "#endif\n"
19111                  "#if B\n"
19112                  "#else\n"
19113                  "  a = 12;\n"
19114                  "#endif\n"
19115                  "};",
19116                  Alignment);
19117   */
19118 
19119   verifyFormat("int a = 5;\n"
19120                "\n"
19121                "int oneTwoThree = 123;",
19122                "int a       = 5;\n"
19123                "\n"
19124                "int oneTwoThree= 123;",
19125                Alignment);
19126   verifyFormat("int a   = 5;\n"
19127                "int one = 1;\n"
19128                "\n"
19129                "int oneTwoThree = 123;",
19130                "int a = 5;\n"
19131                "int one = 1;\n"
19132                "\n"
19133                "int oneTwoThree = 123;",
19134                Alignment);
19135   verifyFormat("int a   = 5;\n"
19136                "int one = 1;\n"
19137                "\n"
19138                "int oneTwoThree = 123;\n"
19139                "int oneTwo      = 12;",
19140                "int a = 5;\n"
19141                "int one = 1;\n"
19142                "\n"
19143                "int oneTwoThree = 123;\n"
19144                "int oneTwo = 12;",
19145                Alignment);
19146   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
19147   verifyFormat("#define A \\\n"
19148                "  int aaaa       = 12; \\\n"
19149                "  int b          = 23; \\\n"
19150                "  int ccc        = 234; \\\n"
19151                "  int dddddddddd = 2345;",
19152                Alignment);
19153   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
19154   verifyFormat("#define A               \\\n"
19155                "  int aaaa       = 12;  \\\n"
19156                "  int b          = 23;  \\\n"
19157                "  int ccc        = 234; \\\n"
19158                "  int dddddddddd = 2345;",
19159                Alignment);
19160   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
19161   verifyFormat("#define A                                                      "
19162                "                \\\n"
19163                "  int aaaa       = 12;                                         "
19164                "                \\\n"
19165                "  int b          = 23;                                         "
19166                "                \\\n"
19167                "  int ccc        = 234;                                        "
19168                "                \\\n"
19169                "  int dddddddddd = 2345;",
19170                Alignment);
19171   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
19172                "k = 4, int l = 5,\n"
19173                "                  int m = 6) {\n"
19174                "  int j      = 10;\n"
19175                "  otherThing = 1;\n"
19176                "}",
19177                Alignment);
19178   verifyFormat("void SomeFunction(int parameter = 0) {\n"
19179                "  int i   = 1;\n"
19180                "  int j   = 2;\n"
19181                "  int big = 10000;\n"
19182                "}",
19183                Alignment);
19184   verifyFormat("class C {\n"
19185                "public:\n"
19186                "  int i            = 1;\n"
19187                "  virtual void f() = 0;\n"
19188                "};",
19189                Alignment);
19190   verifyFormat("int i = 1;\n"
19191                "if (SomeType t = getSomething()) {\n"
19192                "}\n"
19193                "int j   = 2;\n"
19194                "int big = 10000;",
19195                Alignment);
19196   verifyFormat("int j = 7;\n"
19197                "for (int k = 0; k < N; ++k) {\n"
19198                "}\n"
19199                "int j   = 2;\n"
19200                "int big = 10000;\n"
19201                "}",
19202                Alignment);
19203   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
19204   verifyFormat("int i = 1;\n"
19205                "LooooooooooongType loooooooooooooooooooooongVariable\n"
19206                "    = someLooooooooooooooooongFunction();\n"
19207                "int j = 2;",
19208                Alignment);
19209   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
19210   verifyFormat("int i = 1;\n"
19211                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
19212                "    someLooooooooooooooooongFunction();\n"
19213                "int j = 2;",
19214                Alignment);
19215 
19216   verifyFormat("auto lambda = []() {\n"
19217                "  auto i = 0;\n"
19218                "  return 0;\n"
19219                "};\n"
19220                "int i  = 0;\n"
19221                "auto v = type{\n"
19222                "    i = 1,   //\n"
19223                "    (i = 2), //\n"
19224                "    i = 3    //\n"
19225                "};",
19226                Alignment);
19227 
19228   verifyFormat(
19229       "int i      = 1;\n"
19230       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
19231       "                          loooooooooooooooooooooongParameterB);\n"
19232       "int j      = 2;",
19233       Alignment);
19234 
19235   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
19236                "          typename B   = very_long_type_name_1,\n"
19237                "          typename T_2 = very_long_type_name_2>\n"
19238                "auto foo() {}",
19239                Alignment);
19240   verifyFormat("int a, b = 1;\n"
19241                "int c  = 2;\n"
19242                "int dd = 3;",
19243                Alignment);
19244   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
19245                "float b[1][] = {{3.f}};",
19246                Alignment);
19247   verifyFormat("for (int i = 0; i < 1; i++)\n"
19248                "  int x = 1;",
19249                Alignment);
19250   verifyFormat("for (i = 0; i < 1; i++)\n"
19251                "  x = 1;\n"
19252                "y = 1;",
19253                Alignment);
19254 
19255   EXPECT_EQ(Alignment.ReflowComments, true);
19256   Alignment.ColumnLimit = 50;
19257   verifyFormat("int x   = 0;\n"
19258                "int yy  = 1; /// specificlennospace\n"
19259                "int zzz = 2;",
19260                "int x   = 0;\n"
19261                "int yy  = 1; ///specificlennospace\n"
19262                "int zzz = 2;",
19263                Alignment);
19264 
19265   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19266                "auto b                     = [] {\n"
19267                "  f();\n"
19268                "  return;\n"
19269                "};",
19270                Alignment);
19271   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19272                "auto b                     = g([] {\n"
19273                "  f();\n"
19274                "  return;\n"
19275                "});",
19276                Alignment);
19277   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19278                "auto b                     = g(param, [] {\n"
19279                "  f();\n"
19280                "  return;\n"
19281                "});",
19282                Alignment);
19283   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19284                "auto b                     = [] {\n"
19285                "  if (condition) {\n"
19286                "    return;\n"
19287                "  }\n"
19288                "};",
19289                Alignment);
19290 
19291   verifyFormat("auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
19292                "           ccc ? aaaaa : bbbbb,\n"
19293                "           dddddddddddddddddddddddddd);",
19294                Alignment);
19295   // FIXME: https://llvm.org/PR53497
19296   // verifyFormat("auto aaaaaaaaaaaa = f();\n"
19297   //              "auto b            = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
19298   //              "    ccc ? aaaaa : bbbbb,\n"
19299   //              "    dddddddddddddddddddddddddd);",
19300   //              Alignment);
19301 
19302   // Confirm proper handling of AlignConsecutiveAssignments with
19303   // BinPackArguments.
19304   // See https://llvm.org/PR55360
19305   Alignment = getLLVMStyleWithColumns(50);
19306   Alignment.AlignConsecutiveAssignments.Enabled = true;
19307   Alignment.BinPackArguments = false;
19308   verifyFormat("int a_long_name = 1;\n"
19309                "auto b          = B({a_long_name, a_long_name},\n"
19310                "                    {a_longer_name_for_wrap,\n"
19311                "                     a_longer_name_for_wrap});",
19312                Alignment);
19313   verifyFormat("int a_long_name = 1;\n"
19314                "auto b          = B{{a_long_name, a_long_name},\n"
19315                "                    {a_longer_name_for_wrap,\n"
19316                "                     a_longer_name_for_wrap}};",
19317                Alignment);
19318 
19319   Alignment = getLLVMStyleWithColumns(60);
19320   Alignment.AlignConsecutiveAssignments.Enabled = true;
19321   verifyFormat("using II = typename TI<T, std::tuple<Types...>>::I;\n"
19322                "using I  = std::conditional_t<II::value >= 0,\n"
19323                "                              std::ic<int, II::value + 1>,\n"
19324                "                              std::ic<int, -1>>;",
19325                Alignment);
19326   verifyFormat("SomeName = Foo;\n"
19327                "X        = func<Type, Type>(looooooooooooooooooooooooong,\n"
19328                "                            arrrrrrrrrrg);",
19329                Alignment);
19330 
19331   Alignment.ColumnLimit = 80;
19332   Alignment.SpacesInAngles = FormatStyle::SIAS_Always;
19333   verifyFormat("void **ptr = reinterpret_cast< void ** >(unkn);\n"
19334                "ptr        = reinterpret_cast< void ** >(ptr[0]);",
19335                Alignment);
19336   verifyFormat("quint32 *dstimg  = reinterpret_cast< quint32 * >(out(i));\n"
19337                "quint32 *dstmask = reinterpret_cast< quint32 * >(outmask(i));",
19338                Alignment);
19339 
19340   Alignment.SpacesInParens = FormatStyle::SIPO_Custom;
19341   Alignment.SpacesInParensOptions.InCStyleCasts = true;
19342   verifyFormat("void **ptr = ( void ** )unkn;\n"
19343                "ptr        = ( void ** )ptr[0];",
19344                Alignment);
19345   verifyFormat("quint32 *dstimg  = ( quint32 * )out.scanLine(i);\n"
19346                "quint32 *dstmask = ( quint32 * )outmask.scanLine(i);",
19347                Alignment);
19348 }
19349 
19350 TEST_F(FormatTest, AlignConsecutiveBitFields) {
19351   FormatStyle Alignment = getLLVMStyle();
19352   Alignment.AlignConsecutiveBitFields.Enabled = true;
19353   verifyFormat("int const a     : 5;\n"
19354                "int oneTwoThree : 23;",
19355                Alignment);
19356 
19357   // Initializers are allowed starting with c++2a
19358   verifyFormat("int const a     : 5 = 1;\n"
19359                "int oneTwoThree : 23 = 0;",
19360                Alignment);
19361 
19362   Alignment.AlignConsecutiveDeclarations.Enabled = true;
19363   verifyFormat("int const a           : 5;\n"
19364                "int       oneTwoThree : 23;",
19365                Alignment);
19366 
19367   verifyFormat("int const a           : 5;  // comment\n"
19368                "int       oneTwoThree : 23; // comment",
19369                Alignment);
19370 
19371   verifyFormat("int const a           : 5 = 1;\n"
19372                "int       oneTwoThree : 23 = 0;",
19373                Alignment);
19374 
19375   Alignment.AlignConsecutiveAssignments.Enabled = true;
19376   verifyFormat("int const a           : 5  = 1;\n"
19377                "int       oneTwoThree : 23 = 0;",
19378                Alignment);
19379   verifyFormat("int const a           : 5  = {1};\n"
19380                "int       oneTwoThree : 23 = 0;",
19381                Alignment);
19382 
19383   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None;
19384   verifyFormat("int const a          :5;\n"
19385                "int       oneTwoThree:23;",
19386                Alignment);
19387 
19388   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before;
19389   verifyFormat("int const a           :5;\n"
19390                "int       oneTwoThree :23;",
19391                Alignment);
19392 
19393   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After;
19394   verifyFormat("int const a          : 5;\n"
19395                "int       oneTwoThree: 23;",
19396                Alignment);
19397 
19398   // Known limitations: ':' is only recognized as a bitfield colon when
19399   // followed by a number.
19400   /*
19401   verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
19402                "int a           : 5;",
19403                Alignment);
19404   */
19405 }
19406 
19407 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
19408   FormatStyle Alignment = getLLVMStyle();
19409   Alignment.AlignConsecutiveMacros.Enabled = true;
19410   Alignment.PointerAlignment = FormatStyle::PAS_Right;
19411   verifyFormat("float const a = 5;\n"
19412                "int oneTwoThree = 123;",
19413                Alignment);
19414   verifyFormat("int a = 5;\n"
19415                "float const oneTwoThree = 123;",
19416                Alignment);
19417 
19418   Alignment.AlignConsecutiveDeclarations.Enabled = true;
19419   verifyFormat("float const a = 5;\n"
19420                "int         oneTwoThree = 123;",
19421                Alignment);
19422   verifyFormat("int         a = method();\n"
19423                "float const oneTwoThree = 133;",
19424                Alignment);
19425   verifyFormat("int i = 1, j = 10;\n"
19426                "something = 2000;",
19427                Alignment);
19428   verifyFormat("something = 2000;\n"
19429                "int i = 1, j = 10;",
19430                Alignment);
19431   verifyFormat("float      something = 2000;\n"
19432                "double     another = 911;\n"
19433                "int        i = 1, j = 10;\n"
19434                "const int *oneMore = 1;\n"
19435                "unsigned   i = 2;",
19436                Alignment);
19437   verifyFormat("float a = 5;\n"
19438                "int   one = 1;\n"
19439                "method();\n"
19440                "const double       oneTwoThree = 123;\n"
19441                "const unsigned int oneTwo = 12;",
19442                Alignment);
19443   verifyFormat("int      oneTwoThree{0}; // comment\n"
19444                "unsigned oneTwo;         // comment",
19445                Alignment);
19446   verifyFormat("unsigned int       *a;\n"
19447                "int                *b;\n"
19448                "unsigned int Const *c;\n"
19449                "unsigned int const *d;\n"
19450                "unsigned int Const &e;\n"
19451                "unsigned int const &f;",
19452                Alignment);
19453   verifyFormat("Const unsigned int *c;\n"
19454                "const unsigned int *d;\n"
19455                "Const unsigned int &e;\n"
19456                "const unsigned int &f;\n"
19457                "const unsigned      g;\n"
19458                "Const unsigned      h;",
19459                Alignment);
19460   verifyFormat("float const a = 5;\n"
19461                "\n"
19462                "int oneTwoThree = 123;",
19463                "float const   a = 5;\n"
19464                "\n"
19465                "int           oneTwoThree= 123;",
19466                Alignment);
19467   verifyFormat("float a = 5;\n"
19468                "int   one = 1;\n"
19469                "\n"
19470                "unsigned oneTwoThree = 123;",
19471                "float    a = 5;\n"
19472                "int      one = 1;\n"
19473                "\n"
19474                "unsigned oneTwoThree = 123;",
19475                Alignment);
19476   verifyFormat("float a = 5;\n"
19477                "int   one = 1;\n"
19478                "\n"
19479                "unsigned oneTwoThree = 123;\n"
19480                "int      oneTwo = 12;",
19481                "float    a = 5;\n"
19482                "int one = 1;\n"
19483                "\n"
19484                "unsigned oneTwoThree = 123;\n"
19485                "int oneTwo = 12;",
19486                Alignment);
19487   // Function prototype alignment
19488   verifyFormat("int    a();\n"
19489                "double b();",
19490                Alignment);
19491   verifyFormat("int    a(int x);\n"
19492                "double b();",
19493                Alignment);
19494   verifyFormat("int    a(const Test & = Test());\n"
19495                "int    a1(int &foo, const Test & = Test());\n"
19496                "int    a2(int &foo, const Test &name = Test());\n"
19497                "double b();",
19498                Alignment);
19499   verifyFormat("struct Test {\n"
19500                "  Test(const Test &) = default;\n"
19501                "  ~Test() = default;\n"
19502                "  Test &operator=(const Test &) = default;\n"
19503                "};",
19504                Alignment);
19505   unsigned OldColumnLimit = Alignment.ColumnLimit;
19506   // We need to set ColumnLimit to zero, in order to stress nested alignments,
19507   // otherwise the function parameters will be re-flowed onto a single line.
19508   Alignment.ColumnLimit = 0;
19509   verifyFormat("int    a(int   x,\n"
19510                "         float y);\n"
19511                "double b(int    x,\n"
19512                "         double y);",
19513                "int a(int x,\n"
19514                " float y);\n"
19515                "double b(int x,\n"
19516                " double y);",
19517                Alignment);
19518   // This ensures that function parameters of function declarations are
19519   // correctly indented when their owning functions are indented.
19520   // The failure case here is for 'double y' to not be indented enough.
19521   verifyFormat("double a(int x);\n"
19522                "int    b(int    y,\n"
19523                "         double z);",
19524                "double a(int x);\n"
19525                "int b(int y,\n"
19526                " double z);",
19527                Alignment);
19528   // Set ColumnLimit low so that we induce wrapping immediately after
19529   // the function name and opening paren.
19530   Alignment.ColumnLimit = 13;
19531   verifyFormat("int function(\n"
19532                "    int  x,\n"
19533                "    bool y);",
19534                Alignment);
19535   // Set ColumnLimit low so that we break the argument list in multiple lines.
19536   Alignment.ColumnLimit = 35;
19537   verifyFormat("int    a3(SomeTypeName1 &x,\n"
19538                "          SomeTypeName2 &y,\n"
19539                "          const Test & = Test());\n"
19540                "double b();",
19541                Alignment);
19542   Alignment.ColumnLimit = OldColumnLimit;
19543   // Ensure function pointers don't screw up recursive alignment
19544   verifyFormat("int    a(int x, void (*fp)(int y));\n"
19545                "double b();",
19546                Alignment);
19547   Alignment.AlignConsecutiveAssignments.Enabled = true;
19548   verifyFormat("struct Test {\n"
19549                "  Test(const Test &)            = default;\n"
19550                "  ~Test()                       = default;\n"
19551                "  Test &operator=(const Test &) = default;\n"
19552                "};",
19553                Alignment);
19554   // Ensure recursive alignment is broken by function braces, so that the
19555   // "a = 1" does not align with subsequent assignments inside the function
19556   // body.
19557   verifyFormat("int func(int a = 1) {\n"
19558                "  int b  = 2;\n"
19559                "  int cc = 3;\n"
19560                "}",
19561                Alignment);
19562   verifyFormat("float      something = 2000;\n"
19563                "double     another   = 911;\n"
19564                "int        i = 1, j = 10;\n"
19565                "const int *oneMore = 1;\n"
19566                "unsigned   i       = 2;",
19567                Alignment);
19568   verifyFormat("int      oneTwoThree = {0}; // comment\n"
19569                "unsigned oneTwo      = 0;   // comment",
19570                Alignment);
19571   // Make sure that scope is correctly tracked, in the absence of braces
19572   verifyFormat("for (int i = 0; i < n; i++)\n"
19573                "  j = i;\n"
19574                "double x = 1;",
19575                Alignment);
19576   verifyFormat("if (int i = 0)\n"
19577                "  j = i;\n"
19578                "double x = 1;",
19579                Alignment);
19580   // Ensure operator[] and operator() are comprehended
19581   verifyFormat("struct test {\n"
19582                "  long long int foo();\n"
19583                "  int           operator[](int a);\n"
19584                "  double        bar();\n"
19585                "};",
19586                Alignment);
19587   verifyFormat("struct test {\n"
19588                "  long long int foo();\n"
19589                "  int           operator()(int a);\n"
19590                "  double        bar();\n"
19591                "};",
19592                Alignment);
19593   // http://llvm.org/PR52914
19594   verifyFormat("char *a[]     = {\"a\", // comment\n"
19595                "                 \"bb\"};\n"
19596                "int   bbbbbbb = 0;",
19597                Alignment);
19598   // http://llvm.org/PR68079
19599   verifyFormat("using Fn   = int (A::*)();\n"
19600                "using RFn  = int (A::*)() &;\n"
19601                "using RRFn = int (A::*)() &&;",
19602                Alignment);
19603   verifyFormat("using Fn   = int (A::*)();\n"
19604                "using RFn  = int *(A::*)() &;\n"
19605                "using RRFn = double (A::*)() &&;",
19606                Alignment);
19607 
19608   // PAS_Right
19609   verifyFormat("void SomeFunction(int parameter = 0) {\n"
19610                "  int const i   = 1;\n"
19611                "  int      *j   = 2;\n"
19612                "  int       big = 10000;\n"
19613                "\n"
19614                "  unsigned oneTwoThree = 123;\n"
19615                "  int      oneTwo      = 12;\n"
19616                "  method();\n"
19617                "  float k  = 2;\n"
19618                "  int   ll = 10000;\n"
19619                "}",
19620                "void SomeFunction(int parameter= 0) {\n"
19621                " int const  i= 1;\n"
19622                "  int *j=2;\n"
19623                " int big  =  10000;\n"
19624                "\n"
19625                "unsigned oneTwoThree  =123;\n"
19626                "int oneTwo = 12;\n"
19627                "  method();\n"
19628                "float k= 2;\n"
19629                "int ll=10000;\n"
19630                "}",
19631                Alignment);
19632   verifyFormat("void SomeFunction(int parameter = 0) {\n"
19633                "  int const i   = 1;\n"
19634                "  int     **j   = 2, ***k;\n"
19635                "  int      &k   = i;\n"
19636                "  int     &&l   = i + j;\n"
19637                "  int       big = 10000;\n"
19638                "\n"
19639                "  unsigned oneTwoThree = 123;\n"
19640                "  int      oneTwo      = 12;\n"
19641                "  method();\n"
19642                "  float k  = 2;\n"
19643                "  int   ll = 10000;\n"
19644                "}",
19645                "void SomeFunction(int parameter= 0) {\n"
19646                " int const  i= 1;\n"
19647                "  int **j=2,***k;\n"
19648                "int &k=i;\n"
19649                "int &&l=i+j;\n"
19650                " int big  =  10000;\n"
19651                "\n"
19652                "unsigned oneTwoThree  =123;\n"
19653                "int oneTwo = 12;\n"
19654                "  method();\n"
19655                "float k= 2;\n"
19656                "int ll=10000;\n"
19657                "}",
19658                Alignment);
19659   // variables are aligned at their name, pointers are at the right most
19660   // position
19661   verifyFormat("int   *a;\n"
19662                "int  **b;\n"
19663                "int ***c;\n"
19664                "int    foobar;",
19665                Alignment);
19666 
19667   // PAS_Left
19668   FormatStyle AlignmentLeft = Alignment;
19669   AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left;
19670   verifyFormat("void SomeFunction(int parameter = 0) {\n"
19671                "  int const i   = 1;\n"
19672                "  int*      j   = 2;\n"
19673                "  int       big = 10000;\n"
19674                "\n"
19675                "  unsigned oneTwoThree = 123;\n"
19676                "  int      oneTwo      = 12;\n"
19677                "  method();\n"
19678                "  float k  = 2;\n"
19679                "  int   ll = 10000;\n"
19680                "}",
19681                "void SomeFunction(int parameter= 0) {\n"
19682                " int const  i= 1;\n"
19683                "  int *j=2;\n"
19684                " int big  =  10000;\n"
19685                "\n"
19686                "unsigned oneTwoThree  =123;\n"
19687                "int oneTwo = 12;\n"
19688                "  method();\n"
19689                "float k= 2;\n"
19690                "int ll=10000;\n"
19691                "}",
19692                AlignmentLeft);
19693   verifyFormat("void SomeFunction(int parameter = 0) {\n"
19694                "  int const i   = 1;\n"
19695                "  int**     j   = 2;\n"
19696                "  int&      k   = i;\n"
19697                "  int&&     l   = i + j;\n"
19698                "  int       big = 10000;\n"
19699                "\n"
19700                "  unsigned oneTwoThree = 123;\n"
19701                "  int      oneTwo      = 12;\n"
19702                "  method();\n"
19703                "  float k  = 2;\n"
19704                "  int   ll = 10000;\n"
19705                "}",
19706                "void SomeFunction(int parameter= 0) {\n"
19707                " int const  i= 1;\n"
19708                "  int **j=2;\n"
19709                "int &k=i;\n"
19710                "int &&l=i+j;\n"
19711                " int big  =  10000;\n"
19712                "\n"
19713                "unsigned oneTwoThree  =123;\n"
19714                "int oneTwo = 12;\n"
19715                "  method();\n"
19716                "float k= 2;\n"
19717                "int ll=10000;\n"
19718                "}",
19719                AlignmentLeft);
19720   // variables are aligned at their name, pointers are at the left most position
19721   verifyFormat("int*   a;\n"
19722                "int**  b;\n"
19723                "int*** c;\n"
19724                "int    foobar;",
19725                AlignmentLeft);
19726 
19727   verifyFormat("int    a(SomeType& foo, const Test& = Test());\n"
19728                "double b();",
19729                AlignmentLeft);
19730 
19731   // PAS_Middle
19732   FormatStyle AlignmentMiddle = Alignment;
19733   AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
19734   verifyFormat("void SomeFunction(int parameter = 0) {\n"
19735                "  int const i   = 1;\n"
19736                "  int *     j   = 2;\n"
19737                "  int       big = 10000;\n"
19738                "\n"
19739                "  unsigned oneTwoThree = 123;\n"
19740                "  int      oneTwo      = 12;\n"
19741                "  method();\n"
19742                "  float k  = 2;\n"
19743                "  int   ll = 10000;\n"
19744                "}",
19745                "void SomeFunction(int parameter= 0) {\n"
19746                " int const  i= 1;\n"
19747                "  int *j=2;\n"
19748                " int big  =  10000;\n"
19749                "\n"
19750                "unsigned oneTwoThree  =123;\n"
19751                "int oneTwo = 12;\n"
19752                "  method();\n"
19753                "float k= 2;\n"
19754                "int ll=10000;\n"
19755                "}",
19756                AlignmentMiddle);
19757   verifyFormat("void SomeFunction(int parameter = 0) {\n"
19758                "  int const i   = 1;\n"
19759                "  int **    j   = 2, ***k;\n"
19760                "  int &     k   = i;\n"
19761                "  int &&    l   = i + j;\n"
19762                "  int       big = 10000;\n"
19763                "\n"
19764                "  unsigned oneTwoThree = 123;\n"
19765                "  int      oneTwo      = 12;\n"
19766                "  method();\n"
19767                "  float k  = 2;\n"
19768                "  int   ll = 10000;\n"
19769                "}",
19770                "void SomeFunction(int parameter= 0) {\n"
19771                " int const  i= 1;\n"
19772                "  int **j=2,***k;\n"
19773                "int &k=i;\n"
19774                "int &&l=i+j;\n"
19775                " int big  =  10000;\n"
19776                "\n"
19777                "unsigned oneTwoThree  =123;\n"
19778                "int oneTwo = 12;\n"
19779                "  method();\n"
19780                "float k= 2;\n"
19781                "int ll=10000;\n"
19782                "}",
19783                AlignmentMiddle);
19784   // variables are aligned at their name, pointers are in the middle
19785   verifyFormat("int *   a;\n"
19786                "int *   b;\n"
19787                "int *** c;\n"
19788                "int     foobar;",
19789                AlignmentMiddle);
19790 
19791   verifyFormat("int    a(SomeType & foo, const Test & = Test());\n"
19792                "double b();",
19793                AlignmentMiddle);
19794 
19795   Alignment.AlignConsecutiveAssignments.Enabled = false;
19796   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
19797   verifyFormat("#define A \\\n"
19798                "  int       aaaa = 12; \\\n"
19799                "  float     b = 23; \\\n"
19800                "  const int ccc = 234; \\\n"
19801                "  unsigned  dddddddddd = 2345;",
19802                Alignment);
19803   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
19804   verifyFormat("#define A              \\\n"
19805                "  int       aaaa = 12; \\\n"
19806                "  float     b = 23;    \\\n"
19807                "  const int ccc = 234; \\\n"
19808                "  unsigned  dddddddddd = 2345;",
19809                Alignment);
19810   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
19811   Alignment.ColumnLimit = 30;
19812   verifyFormat("#define A                    \\\n"
19813                "  int       aaaa = 12;       \\\n"
19814                "  float     b = 23;          \\\n"
19815                "  const int ccc = 234;       \\\n"
19816                "  int       dddddddddd = 2345;",
19817                Alignment);
19818   Alignment.ColumnLimit = 80;
19819   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
19820                "k = 4, int l = 5,\n"
19821                "                  int m = 6) {\n"
19822                "  const int j = 10;\n"
19823                "  otherThing = 1;\n"
19824                "}",
19825                Alignment);
19826   verifyFormat("void SomeFunction(int parameter = 0) {\n"
19827                "  int const i = 1;\n"
19828                "  int      *j = 2;\n"
19829                "  int       big = 10000;\n"
19830                "}",
19831                Alignment);
19832   verifyFormat("class C {\n"
19833                "public:\n"
19834                "  int          i = 1;\n"
19835                "  virtual void f() = 0;\n"
19836                "};",
19837                Alignment);
19838   verifyFormat("float i = 1;\n"
19839                "if (SomeType t = getSomething()) {\n"
19840                "}\n"
19841                "const unsigned j = 2;\n"
19842                "int            big = 10000;",
19843                Alignment);
19844   verifyFormat("float j = 7;\n"
19845                "for (int k = 0; k < N; ++k) {\n"
19846                "}\n"
19847                "unsigned j = 2;\n"
19848                "int      big = 10000;\n"
19849                "}",
19850                Alignment);
19851   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
19852   verifyFormat("float              i = 1;\n"
19853                "LooooooooooongType loooooooooooooooooooooongVariable\n"
19854                "    = someLooooooooooooooooongFunction();\n"
19855                "int j = 2;",
19856                Alignment);
19857   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
19858   verifyFormat("int                i = 1;\n"
19859                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
19860                "    someLooooooooooooooooongFunction();\n"
19861                "int j = 2;",
19862                Alignment);
19863 
19864   Alignment.AlignConsecutiveAssignments.Enabled = true;
19865   verifyFormat("auto lambda = []() {\n"
19866                "  auto  ii = 0;\n"
19867                "  float j  = 0;\n"
19868                "  return 0;\n"
19869                "};\n"
19870                "int   i  = 0;\n"
19871                "float i2 = 0;\n"
19872                "auto  v  = type{\n"
19873                "    i = 1,   //\n"
19874                "    (i = 2), //\n"
19875                "    i = 3    //\n"
19876                "};",
19877                Alignment);
19878   Alignment.AlignConsecutiveAssignments.Enabled = false;
19879 
19880   verifyFormat(
19881       "int      i = 1;\n"
19882       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
19883       "                          loooooooooooooooooooooongParameterB);\n"
19884       "int      j = 2;",
19885       Alignment);
19886 
19887   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
19888   // We expect declarations and assignments to align, as long as it doesn't
19889   // exceed the column limit, starting a new alignment sequence whenever it
19890   // happens.
19891   Alignment.AlignConsecutiveAssignments.Enabled = true;
19892   Alignment.ColumnLimit = 30;
19893   verifyFormat("float    ii              = 1;\n"
19894                "unsigned j               = 2;\n"
19895                "int someVerylongVariable = 1;\n"
19896                "AnotherLongType  ll = 123456;\n"
19897                "VeryVeryLongType k  = 2;\n"
19898                "int              myvar = 1;",
19899                Alignment);
19900   Alignment.ColumnLimit = 80;
19901   Alignment.AlignConsecutiveAssignments.Enabled = false;
19902 
19903   verifyFormat(
19904       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
19905       "          typename LongType, typename B>\n"
19906       "auto foo() {}",
19907       Alignment);
19908   verifyFormat("float a, b = 1;\n"
19909                "int   c = 2;\n"
19910                "int   dd = 3;",
19911                Alignment);
19912   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
19913                "float b[1][] = {{3.f}};",
19914                Alignment);
19915   Alignment.AlignConsecutiveAssignments.Enabled = true;
19916   verifyFormat("float a, b = 1;\n"
19917                "int   c  = 2;\n"
19918                "int   dd = 3;",
19919                Alignment);
19920   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
19921                "float b[1][] = {{3.f}};",
19922                Alignment);
19923   Alignment.AlignConsecutiveAssignments.Enabled = false;
19924 
19925   Alignment.ColumnLimit = 30;
19926   Alignment.BinPackParameters = FormatStyle::BPPS_OnePerLine;
19927   verifyFormat("void foo(float     a,\n"
19928                "         float     b,\n"
19929                "         int       c,\n"
19930                "         uint32_t *d) {\n"
19931                "  int   *e = 0;\n"
19932                "  float  f = 0;\n"
19933                "  double g = 0;\n"
19934                "}\n"
19935                "void bar(ino_t     a,\n"
19936                "         int       b,\n"
19937                "         uint32_t *c,\n"
19938                "         bool      d) {}",
19939                Alignment);
19940   Alignment.BinPackParameters = FormatStyle::BPPS_BinPack;
19941   Alignment.ColumnLimit = 80;
19942 
19943   // Bug 33507
19944   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
19945   verifyFormat(
19946       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
19947       "  static const Version verVs2017;\n"
19948       "  return true;\n"
19949       "});",
19950       Alignment);
19951   Alignment.PointerAlignment = FormatStyle::PAS_Right;
19952 
19953   // See llvm.org/PR35641
19954   Alignment.AlignConsecutiveDeclarations.Enabled = true;
19955   verifyFormat("int func() { //\n"
19956                "  int      b;\n"
19957                "  unsigned c;\n"
19958                "}",
19959                Alignment);
19960 
19961   // See PR37175
19962   FormatStyle Style = getMozillaStyle();
19963   Style.AlignConsecutiveDeclarations.Enabled = true;
19964   verifyFormat("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
19965                "foo(int a);",
19966                "DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style);
19967 
19968   Alignment.PointerAlignment = FormatStyle::PAS_Left;
19969   verifyFormat("unsigned int*       a;\n"
19970                "int*                b;\n"
19971                "unsigned int Const* c;\n"
19972                "unsigned int const* d;\n"
19973                "unsigned int Const& e;\n"
19974                "unsigned int const& f;",
19975                Alignment);
19976   verifyFormat("Const unsigned int* c;\n"
19977                "const unsigned int* d;\n"
19978                "Const unsigned int& e;\n"
19979                "const unsigned int& f;\n"
19980                "const unsigned      g;\n"
19981                "Const unsigned      h;",
19982                Alignment);
19983 
19984   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
19985   verifyFormat("unsigned int *       a;\n"
19986                "int *                b;\n"
19987                "unsigned int Const * c;\n"
19988                "unsigned int const * d;\n"
19989                "unsigned int Const & e;\n"
19990                "unsigned int const & f;",
19991                Alignment);
19992   verifyFormat("Const unsigned int * c;\n"
19993                "const unsigned int * d;\n"
19994                "Const unsigned int & e;\n"
19995                "const unsigned int & f;\n"
19996                "const unsigned       g;\n"
19997                "Const unsigned       h;",
19998                Alignment);
19999 
20000   // See PR46529
20001   FormatStyle BracedAlign = getLLVMStyle();
20002   BracedAlign.AlignConsecutiveDeclarations.Enabled = true;
20003   verifyFormat("const auto result{[]() {\n"
20004                "  const auto something = 1;\n"
20005                "  return 2;\n"
20006                "}};",
20007                BracedAlign);
20008   verifyFormat("int foo{[]() {\n"
20009                "  int bar{0};\n"
20010                "  return 0;\n"
20011                "}()};",
20012                BracedAlign);
20013   BracedAlign.Cpp11BracedListStyle = false;
20014   verifyFormat("const auto result{ []() {\n"
20015                "  const auto something = 1;\n"
20016                "  return 2;\n"
20017                "} };",
20018                BracedAlign);
20019   verifyFormat("int foo{ []() {\n"
20020                "  int bar{ 0 };\n"
20021                "  return 0;\n"
20022                "}() };",
20023                BracedAlign);
20024 
20025   Alignment.AlignConsecutiveDeclarations.AlignFunctionDeclarations = false;
20026   verifyFormat("unsigned int f1(void);\n"
20027                "void f2(void);\n"
20028                "size_t f3(void);",
20029                Alignment);
20030 }
20031 
20032 TEST_F(FormatTest, AlignConsecutiveShortCaseStatements) {
20033   FormatStyle Alignment = getLLVMStyle();
20034   Alignment.AllowShortCaseLabelsOnASingleLine = true;
20035   Alignment.AlignConsecutiveShortCaseStatements.Enabled = true;
20036 
20037   verifyFormat("switch (level) {\n"
20038                "case log::info:    return \"info\";\n"
20039                "case log::warning: return \"warning\";\n"
20040                "default:           return \"default\";\n"
20041                "}",
20042                Alignment);
20043 
20044   verifyFormat("switch (level) {\n"
20045                "case log::info:    return \"info\";\n"
20046                "case log::warning: return \"warning\";\n"
20047                "}",
20048                "switch (level) {\n"
20049                "case log::info: return \"info\";\n"
20050                "case log::warning:\n"
20051                "  return \"warning\";\n"
20052                "}",
20053                Alignment);
20054 
20055   // Empty case statements push out the alignment, but non-short case labels
20056   // don't.
20057   verifyFormat("switch (level) {\n"
20058                "case log::info:     return \"info\";\n"
20059                "case log::critical:\n"
20060                "case log::warning:\n"
20061                "case log::severe:   return \"severe\";\n"
20062                "case log::extra_severe:\n"
20063                "  // comment\n"
20064                "  return \"extra_severe\";\n"
20065                "}",
20066                Alignment);
20067 
20068   // Verify comments and empty lines break the alignment.
20069   verifyNoChange("switch (level) {\n"
20070                  "case log::info:    return \"info\";\n"
20071                  "case log::warning: return \"warning\";\n"
20072                  "// comment\n"
20073                  "case log::critical: return \"critical\";\n"
20074                  "default:            return \"default\";\n"
20075                  "\n"
20076                  "case log::severe: return \"severe\";\n"
20077                  "}",
20078                  Alignment);
20079 
20080   // Empty case statements don't break the alignment, and potentially push it
20081   // out.
20082   verifyFormat("switch (level) {\n"
20083                "case log::info:     return \"info\";\n"
20084                "case log::warning:\n"
20085                "case log::critical:\n"
20086                "default:            return \"default\";\n"
20087                "}",
20088                Alignment);
20089 
20090   // Implicit fallthrough cases can be aligned with either a comment or
20091   // [[fallthrough]]
20092   verifyFormat("switch (level) {\n"
20093                "case log::info:     return \"info\";\n"
20094                "case log::warning:  // fallthrough\n"
20095                "case log::error:    return \"error\";\n"
20096                "case log::critical: /*fallthrough*/\n"
20097                "case log::severe:   return \"severe\";\n"
20098                "case log::diag:     [[fallthrough]];\n"
20099                "default:            return \"default\";\n"
20100                "}",
20101                Alignment);
20102 
20103   // Verify trailing comment that needs a reflow also gets aligned properly.
20104   verifyFormat("switch (level) {\n"
20105                "case log::info:    return \"info\";\n"
20106                "case log::warning: // fallthrough\n"
20107                "case log::error:   return \"error\";\n"
20108                "}",
20109                "switch (level) {\n"
20110                "case log::info:    return \"info\";\n"
20111                "case log::warning: //fallthrough\n"
20112                "case log::error:   return \"error\";\n"
20113                "}",
20114                Alignment);
20115 
20116   // Verify adjacent non-short case statements don't change the alignment, and
20117   // properly break the set of consecutive statements.
20118   verifyFormat("switch (level) {\n"
20119                "case log::critical:\n"
20120                "  // comment\n"
20121                "  return \"critical\";\n"
20122                "case log::info:    return \"info\";\n"
20123                "case log::warning: return \"warning\";\n"
20124                "default:\n"
20125                "  // comment\n"
20126                "  return \"\";\n"
20127                "case log::error:  return \"error\";\n"
20128                "case log::severe: return \"severe\";\n"
20129                "case log::extra_critical:\n"
20130                "  // comment\n"
20131                "  return \"extra critical\";\n"
20132                "}",
20133                Alignment);
20134 
20135   Alignment.SpaceBeforeCaseColon = true;
20136   verifyFormat("switch (level) {\n"
20137                "case log::info :    return \"info\";\n"
20138                "case log::warning : return \"warning\";\n"
20139                "default :           return \"default\";\n"
20140                "}",
20141                Alignment);
20142   Alignment.SpaceBeforeCaseColon = false;
20143 
20144   // Make sure we don't incorrectly align correctly across nested switch cases.
20145   verifyFormat("switch (level) {\n"
20146                "case log::info:    return \"info\";\n"
20147                "case log::warning: return \"warning\";\n"
20148                "case log::other:\n"
20149                "  switch (sublevel) {\n"
20150                "  case log::info:    return \"info\";\n"
20151                "  case log::warning: return \"warning\";\n"
20152                "  }\n"
20153                "  break;\n"
20154                "case log::error: return \"error\";\n"
20155                "default:         return \"default\";\n"
20156                "}",
20157                "switch (level) {\n"
20158                "case log::info:    return \"info\";\n"
20159                "case log::warning: return \"warning\";\n"
20160                "case log::other: switch (sublevel) {\n"
20161                "  case log::info:    return \"info\";\n"
20162                "  case log::warning: return \"warning\";\n"
20163                "}\n"
20164                "break;\n"
20165                "case log::error: return \"error\";\n"
20166                "default:         return \"default\";\n"
20167                "}",
20168                Alignment);
20169 
20170   Alignment.AlignConsecutiveShortCaseStatements.AcrossEmptyLines = true;
20171 
20172   verifyFormat("switch (level) {\n"
20173                "case log::info:    return \"info\";\n"
20174                "\n"
20175                "case log::warning: return \"warning\";\n"
20176                "}",
20177                "switch (level) {\n"
20178                "case log::info: return \"info\";\n"
20179                "\n"
20180                "case log::warning: return \"warning\";\n"
20181                "}",
20182                Alignment);
20183 
20184   Alignment.AlignConsecutiveShortCaseStatements.AcrossComments = true;
20185 
20186   verifyNoChange("switch (level) {\n"
20187                  "case log::info:    return \"info\";\n"
20188                  "\n"
20189                  "/* block comment */\n"
20190                  "\n"
20191                  "// line comment\n"
20192                  "case log::warning: return \"warning\";\n"
20193                  "}",
20194                  Alignment);
20195 
20196   Alignment.AlignConsecutiveShortCaseStatements.AcrossEmptyLines = false;
20197 
20198   verifyFormat("switch (level) {\n"
20199                "case log::info:    return \"info\";\n"
20200                "//\n"
20201                "case log::warning: return \"warning\";\n"
20202                "}",
20203                Alignment);
20204 
20205   Alignment.AlignConsecutiveShortCaseStatements.AlignCaseColons = true;
20206 
20207   verifyFormat("switch (level) {\n"
20208                "case log::info   : return \"info\";\n"
20209                "case log::warning: return \"warning\";\n"
20210                "default          : return \"default\";\n"
20211                "}",
20212                Alignment);
20213 
20214   // With AlignCaseColons, empty case statements don't break alignment of
20215   // consecutive case statements (and are aligned).
20216   verifyFormat("switch (level) {\n"
20217                "case log::info    : return \"info\";\n"
20218                "case log::warning :\n"
20219                "case log::critical:\n"
20220                "default           : return \"default\";\n"
20221                "}",
20222                Alignment);
20223 
20224   // Final non-short case labels shouldn't have their colon aligned
20225   verifyFormat("switch (level) {\n"
20226                "case log::info    : return \"info\";\n"
20227                "case log::warning :\n"
20228                "case log::critical:\n"
20229                "case log::severe  : return \"severe\";\n"
20230                "default:\n"
20231                "  // comment\n"
20232                "  return \"default\";\n"
20233                "}",
20234                Alignment);
20235 
20236   // Verify adjacent non-short case statements break the set of consecutive
20237   // alignments and aren't aligned with adjacent non-short case statements if
20238   // AlignCaseColons is set.
20239   verifyFormat("switch (level) {\n"
20240                "case log::critical:\n"
20241                "  // comment\n"
20242                "  return \"critical\";\n"
20243                "case log::info   : return \"info\";\n"
20244                "case log::warning: return \"warning\";\n"
20245                "default:\n"
20246                "  // comment\n"
20247                "  return \"\";\n"
20248                "case log::error : return \"error\";\n"
20249                "case log::severe: return \"severe\";\n"
20250                "case log::extra_critical:\n"
20251                "  // comment\n"
20252                "  return \"extra critical\";\n"
20253                "}",
20254                Alignment);
20255 
20256   Alignment.SpaceBeforeCaseColon = true;
20257   verifyFormat("switch (level) {\n"
20258                "case log::info    : return \"info\";\n"
20259                "case log::warning : return \"warning\";\n"
20260                "case log::error   :\n"
20261                "default           : return \"default\";\n"
20262                "}",
20263                Alignment);
20264 }
20265 
20266 TEST_F(FormatTest, AlignWithLineBreaks) {
20267   auto Style = getLLVMStyleWithColumns(120);
20268 
20269   EXPECT_EQ(Style.AlignConsecutiveAssignments,
20270             FormatStyle::AlignConsecutiveStyle(
20271                 {/*Enabled=*/false, /*AcrossEmptyLines=*/false,
20272                  /*AcrossComments=*/false, /*AlignCompound=*/false,
20273                  /*AlignFunctionDeclarations=*/false,
20274                  /*AlignFunctionPointers=*/false,
20275                  /*PadOperators=*/true}));
20276   EXPECT_EQ(Style.AlignConsecutiveDeclarations,
20277             FormatStyle::AlignConsecutiveStyle(
20278                 {/*Enabled=*/false, /*AcrossEmptyLines=*/false,
20279                  /*AcrossComments=*/false, /*AlignCompound=*/false,
20280                  /*AlignFunctionDeclarations=*/true,
20281                  /*AlignFunctionPointers=*/false,
20282                  /*PadOperators=*/false}));
20283   verifyFormat("void foo() {\n"
20284                "  int myVar = 5;\n"
20285                "  double x = 3.14;\n"
20286                "  auto str = \"Hello \"\n"
20287                "             \"World\";\n"
20288                "  auto s = \"Hello \"\n"
20289                "           \"Again\";\n"
20290                "}",
20291                Style);
20292 
20293   // clang-format off
20294   verifyFormat("void foo() {\n"
20295                "  const int capacityBefore = Entries.capacity();\n"
20296                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20297                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20298                "  const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20299                "                                          std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20300                "}",
20301                Style);
20302   // clang-format on
20303 
20304   Style.AlignConsecutiveAssignments.Enabled = true;
20305   verifyFormat("void foo() {\n"
20306                "  int myVar = 5;\n"
20307                "  double x  = 3.14;\n"
20308                "  auto str  = \"Hello \"\n"
20309                "              \"World\";\n"
20310                "  auto s    = \"Hello \"\n"
20311                "              \"Again\";\n"
20312                "}",
20313                Style);
20314 
20315   // clang-format off
20316   verifyFormat("void foo() {\n"
20317                "  const int capacityBefore = Entries.capacity();\n"
20318                "  const auto newEntry      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20319                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20320                "  const X newEntry2        = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20321                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20322                "}",
20323                Style);
20324   // clang-format on
20325 
20326   Style.AlignConsecutiveAssignments.Enabled = false;
20327   Style.AlignConsecutiveDeclarations.Enabled = true;
20328   verifyFormat("void foo() {\n"
20329                "  int    myVar = 5;\n"
20330                "  double x = 3.14;\n"
20331                "  auto   str = \"Hello \"\n"
20332                "               \"World\";\n"
20333                "  auto   s = \"Hello \"\n"
20334                "             \"Again\";\n"
20335                "}",
20336                Style);
20337 
20338   // clang-format off
20339   verifyFormat("void foo() {\n"
20340                "  const int  capacityBefore = Entries.capacity();\n"
20341                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20342                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20343                "  const X    newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20344                "                                             std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20345                "}",
20346                Style);
20347   // clang-format on
20348 
20349   Style.AlignConsecutiveAssignments.Enabled = true;
20350   Style.AlignConsecutiveDeclarations.Enabled = true;
20351 
20352   verifyFormat("void foo() {\n"
20353                "  int    myVar = 5;\n"
20354                "  double x     = 3.14;\n"
20355                "  auto   str   = \"Hello \"\n"
20356                "                 \"World\";\n"
20357                "  auto   s     = \"Hello \"\n"
20358                "                 \"Again\";\n"
20359                "}",
20360                Style);
20361 
20362   // clang-format off
20363   verifyFormat("void foo() {\n"
20364                "  const int  capacityBefore = Entries.capacity();\n"
20365                "  const auto newEntry       = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20366                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20367                "  const X    newEntry2      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20368                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20369                "}",
20370                Style);
20371   // clang-format on
20372 
20373   Style = getLLVMStyleWithColumns(20);
20374   Style.AlignConsecutiveAssignments.Enabled = true;
20375   Style.IndentWidth = 4;
20376 
20377   verifyFormat("void foo() {\n"
20378                "    int i1 = 1;\n"
20379                "    int j  = 0;\n"
20380                "    int k  = bar(\n"
20381                "        argument1,\n"
20382                "        argument2);\n"
20383                "}",
20384                Style);
20385 
20386   verifyFormat("unsigned i = 0;\n"
20387                "int a[]    = {\n"
20388                "    1234567890,\n"
20389                "    -1234567890};",
20390                Style);
20391 
20392   Style.ColumnLimit = 120;
20393 
20394   // clang-format off
20395   verifyFormat("void SomeFunc() {\n"
20396                "    newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20397                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20398                "    newWatcher.maxAge     = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20399                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20400                "    newWatcher.max        = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20401                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20402                "}",
20403                Style);
20404   // clang-format on
20405 
20406   Style.BinPackArguments = false;
20407 
20408   // clang-format off
20409   verifyFormat("void SomeFunc() {\n"
20410                "    newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n"
20411                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20412                "    newWatcher.maxAge     = ToLegacyTimestamp(GetMaxAge(\n"
20413                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20414                "    newWatcher.max        = ToLegacyTimestamp(GetMaxAge(\n"
20415                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20416                "}",
20417                Style);
20418   // clang-format on
20419 }
20420 
20421 TEST_F(FormatTest, AlignWithInitializerPeriods) {
20422   auto Style = getLLVMStyleWithColumns(60);
20423 
20424   verifyFormat("void foo1(void) {\n"
20425                "  BYTE p[1] = 1;\n"
20426                "  A B = {.one_foooooooooooooooo = 2,\n"
20427                "         .two_fooooooooooooo = 3,\n"
20428                "         .three_fooooooooooooo = 4};\n"
20429                "  BYTE payload = 2;\n"
20430                "}",
20431                Style);
20432 
20433   Style.AlignConsecutiveAssignments.Enabled = true;
20434   Style.AlignConsecutiveDeclarations.Enabled = false;
20435   verifyFormat("void foo2(void) {\n"
20436                "  BYTE p[1]    = 1;\n"
20437                "  A B          = {.one_foooooooooooooooo = 2,\n"
20438                "                  .two_fooooooooooooo    = 3,\n"
20439                "                  .three_fooooooooooooo  = 4};\n"
20440                "  BYTE payload = 2;\n"
20441                "}",
20442                Style);
20443 
20444   Style.AlignConsecutiveAssignments.Enabled = false;
20445   Style.AlignConsecutiveDeclarations.Enabled = true;
20446   verifyFormat("void foo3(void) {\n"
20447                "  BYTE p[1] = 1;\n"
20448                "  A    B = {.one_foooooooooooooooo = 2,\n"
20449                "            .two_fooooooooooooo = 3,\n"
20450                "            .three_fooooooooooooo = 4};\n"
20451                "  BYTE payload = 2;\n"
20452                "}",
20453                Style);
20454 
20455   Style.AlignConsecutiveAssignments.Enabled = true;
20456   Style.AlignConsecutiveDeclarations.Enabled = true;
20457   verifyFormat("void foo4(void) {\n"
20458                "  BYTE p[1]    = 1;\n"
20459                "  A    B       = {.one_foooooooooooooooo = 2,\n"
20460                "                  .two_fooooooooooooo    = 3,\n"
20461                "                  .three_fooooooooooooo  = 4};\n"
20462                "  BYTE payload = 2;\n"
20463                "}",
20464                Style);
20465 }
20466 
20467 TEST_F(FormatTest, LinuxBraceBreaking) {
20468   FormatStyle LinuxBraceStyle = getLLVMStyle();
20469   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
20470   verifyFormat("namespace a\n"
20471                "{\n"
20472                "class A\n"
20473                "{\n"
20474                "  void f()\n"
20475                "  {\n"
20476                "    if (true) {\n"
20477                "      a();\n"
20478                "      b();\n"
20479                "    } else {\n"
20480                "      a();\n"
20481                "    }\n"
20482                "  }\n"
20483                "  void g() { return; }\n"
20484                "};\n"
20485                "struct B {\n"
20486                "  int x;\n"
20487                "};\n"
20488                "} // namespace a",
20489                LinuxBraceStyle);
20490   verifyFormat("enum X {\n"
20491                "  Y = 0,\n"
20492                "}",
20493                LinuxBraceStyle);
20494   verifyFormat("struct S {\n"
20495                "  int Type;\n"
20496                "  union {\n"
20497                "    int x;\n"
20498                "    double y;\n"
20499                "  } Value;\n"
20500                "  class C\n"
20501                "  {\n"
20502                "    MyFavoriteType Value;\n"
20503                "  } Class;\n"
20504                "}",
20505                LinuxBraceStyle);
20506 }
20507 
20508 TEST_F(FormatTest, MozillaBraceBreaking) {
20509   FormatStyle MozillaBraceStyle = getLLVMStyle();
20510   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
20511   MozillaBraceStyle.FixNamespaceComments = false;
20512   verifyFormat("namespace a {\n"
20513                "class A\n"
20514                "{\n"
20515                "  void f()\n"
20516                "  {\n"
20517                "    if (true) {\n"
20518                "      a();\n"
20519                "      b();\n"
20520                "    }\n"
20521                "  }\n"
20522                "  void g() { return; }\n"
20523                "};\n"
20524                "enum E\n"
20525                "{\n"
20526                "  A,\n"
20527                "  // foo\n"
20528                "  B,\n"
20529                "  C\n"
20530                "};\n"
20531                "struct B\n"
20532                "{\n"
20533                "  int x;\n"
20534                "};\n"
20535                "}",
20536                MozillaBraceStyle);
20537   verifyFormat("struct S\n"
20538                "{\n"
20539                "  int Type;\n"
20540                "  union\n"
20541                "  {\n"
20542                "    int x;\n"
20543                "    double y;\n"
20544                "  } Value;\n"
20545                "  class C\n"
20546                "  {\n"
20547                "    MyFavoriteType Value;\n"
20548                "  } Class;\n"
20549                "}",
20550                MozillaBraceStyle);
20551 }
20552 
20553 TEST_F(FormatTest, StroustrupBraceBreaking) {
20554   FormatStyle StroustrupBraceStyle = getLLVMStyle();
20555   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
20556   verifyFormat("namespace a {\n"
20557                "class A {\n"
20558                "  void f()\n"
20559                "  {\n"
20560                "    if (true) {\n"
20561                "      a();\n"
20562                "      b();\n"
20563                "    }\n"
20564                "  }\n"
20565                "  void g() { return; }\n"
20566                "};\n"
20567                "struct B {\n"
20568                "  int x;\n"
20569                "};\n"
20570                "} // namespace a",
20571                StroustrupBraceStyle);
20572 
20573   verifyFormat("void foo()\n"
20574                "{\n"
20575                "  if (a) {\n"
20576                "    a();\n"
20577                "  }\n"
20578                "  else {\n"
20579                "    b();\n"
20580                "  }\n"
20581                "}",
20582                StroustrupBraceStyle);
20583 
20584   verifyFormat("#ifdef _DEBUG\n"
20585                "int foo(int i = 0)\n"
20586                "#else\n"
20587                "int foo(int i = 5)\n"
20588                "#endif\n"
20589                "{\n"
20590                "  return i;\n"
20591                "}",
20592                StroustrupBraceStyle);
20593 
20594   verifyFormat("void foo() {}\n"
20595                "void bar()\n"
20596                "#ifdef _DEBUG\n"
20597                "{\n"
20598                "  foo();\n"
20599                "}\n"
20600                "#else\n"
20601                "{\n"
20602                "}\n"
20603                "#endif",
20604                StroustrupBraceStyle);
20605 
20606   verifyFormat("void foobar() { int i = 5; }\n"
20607                "#ifdef _DEBUG\n"
20608                "void bar() {}\n"
20609                "#else\n"
20610                "void bar() { foobar(); }\n"
20611                "#endif",
20612                StroustrupBraceStyle);
20613 }
20614 
20615 TEST_F(FormatTest, AllmanBraceBreaking) {
20616   FormatStyle AllmanBraceStyle = getLLVMStyle();
20617   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
20618 
20619   verifyFormat("namespace a\n"
20620                "{\n"
20621                "void f();\n"
20622                "void g();\n"
20623                "} // namespace a",
20624                "namespace a\n"
20625                "{\n"
20626                "void f();\n"
20627                "void g();\n"
20628                "}",
20629                AllmanBraceStyle);
20630 
20631   verifyFormat("namespace a\n"
20632                "{\n"
20633                "class A\n"
20634                "{\n"
20635                "  void f()\n"
20636                "  {\n"
20637                "    if (true)\n"
20638                "    {\n"
20639                "      a();\n"
20640                "      b();\n"
20641                "    }\n"
20642                "  }\n"
20643                "  void g() { return; }\n"
20644                "};\n"
20645                "struct B\n"
20646                "{\n"
20647                "  int x;\n"
20648                "};\n"
20649                "union C\n"
20650                "{\n"
20651                "};\n"
20652                "} // namespace a",
20653                AllmanBraceStyle);
20654 
20655   verifyFormat("void f()\n"
20656                "{\n"
20657                "  if (true)\n"
20658                "  {\n"
20659                "    a();\n"
20660                "  }\n"
20661                "  else if (false)\n"
20662                "  {\n"
20663                "    b();\n"
20664                "  }\n"
20665                "  else\n"
20666                "  {\n"
20667                "    c();\n"
20668                "  }\n"
20669                "}",
20670                AllmanBraceStyle);
20671 
20672   verifyFormat("void f()\n"
20673                "{\n"
20674                "  for (int i = 0; i < 10; ++i)\n"
20675                "  {\n"
20676                "    a();\n"
20677                "  }\n"
20678                "  while (false)\n"
20679                "  {\n"
20680                "    b();\n"
20681                "  }\n"
20682                "  do\n"
20683                "  {\n"
20684                "    c();\n"
20685                "  } while (false)\n"
20686                "}",
20687                AllmanBraceStyle);
20688 
20689   verifyFormat("void f(int a)\n"
20690                "{\n"
20691                "  switch (a)\n"
20692                "  {\n"
20693                "  case 0:\n"
20694                "    break;\n"
20695                "  case 1:\n"
20696                "  {\n"
20697                "    break;\n"
20698                "  }\n"
20699                "  case 2:\n"
20700                "  {\n"
20701                "  }\n"
20702                "  break;\n"
20703                "  default:\n"
20704                "    break;\n"
20705                "  }\n"
20706                "}",
20707                AllmanBraceStyle);
20708 
20709   verifyFormat("enum X\n"
20710                "{\n"
20711                "  Y = 0,\n"
20712                "}",
20713                AllmanBraceStyle);
20714   verifyFormat("enum X\n"
20715                "{\n"
20716                "  Y = 0\n"
20717                "}",
20718                AllmanBraceStyle);
20719 
20720   verifyFormat("@interface BSApplicationController ()\n"
20721                "{\n"
20722                "@private\n"
20723                "  id _extraIvar;\n"
20724                "}\n"
20725                "@end",
20726                AllmanBraceStyle);
20727 
20728   verifyFormat("#ifdef _DEBUG\n"
20729                "int foo(int i = 0)\n"
20730                "#else\n"
20731                "int foo(int i = 5)\n"
20732                "#endif\n"
20733                "{\n"
20734                "  return i;\n"
20735                "}",
20736                AllmanBraceStyle);
20737 
20738   verifyFormat("void foo() {}\n"
20739                "void bar()\n"
20740                "#ifdef _DEBUG\n"
20741                "{\n"
20742                "  foo();\n"
20743                "}\n"
20744                "#else\n"
20745                "{\n"
20746                "}\n"
20747                "#endif",
20748                AllmanBraceStyle);
20749 
20750   verifyFormat("void foobar() { int i = 5; }\n"
20751                "#ifdef _DEBUG\n"
20752                "void bar() {}\n"
20753                "#else\n"
20754                "void bar() { foobar(); }\n"
20755                "#endif",
20756                AllmanBraceStyle);
20757 
20758   EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine,
20759             FormatStyle::SLS_All);
20760 
20761   verifyFormat("[](int i) { return i + 2; };\n"
20762                "[](int i, int j)\n"
20763                "{\n"
20764                "  auto x = i + j;\n"
20765                "  auto y = i * j;\n"
20766                "  return x ^ y;\n"
20767                "};\n"
20768                "void foo()\n"
20769                "{\n"
20770                "  auto shortLambda = [](int i) { return i + 2; };\n"
20771                "  auto longLambda = [](int i, int j)\n"
20772                "  {\n"
20773                "    auto x = i + j;\n"
20774                "    auto y = i * j;\n"
20775                "    return x ^ y;\n"
20776                "  };\n"
20777                "}",
20778                AllmanBraceStyle);
20779 
20780   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
20781 
20782   verifyFormat("[](int i)\n"
20783                "{\n"
20784                "  return i + 2;\n"
20785                "};\n"
20786                "[](int i, int j)\n"
20787                "{\n"
20788                "  auto x = i + j;\n"
20789                "  auto y = i * j;\n"
20790                "  return x ^ y;\n"
20791                "};\n"
20792                "void foo()\n"
20793                "{\n"
20794                "  auto shortLambda = [](int i)\n"
20795                "  {\n"
20796                "    return i + 2;\n"
20797                "  };\n"
20798                "  auto longLambda = [](int i, int j)\n"
20799                "  {\n"
20800                "    auto x = i + j;\n"
20801                "    auto y = i * j;\n"
20802                "    return x ^ y;\n"
20803                "  };\n"
20804                "}",
20805                AllmanBraceStyle);
20806 
20807   // Reset
20808   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
20809 
20810   // This shouldn't affect ObjC blocks..
20811   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
20812                "  // ...\n"
20813                "  int i;\n"
20814                "}];",
20815                AllmanBraceStyle);
20816   verifyFormat("void (^block)(void) = ^{\n"
20817                "  // ...\n"
20818                "  int i;\n"
20819                "};",
20820                AllmanBraceStyle);
20821   // .. or dict literals.
20822   verifyFormat("void f()\n"
20823                "{\n"
20824                "  // ...\n"
20825                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
20826                "}",
20827                AllmanBraceStyle);
20828   verifyFormat("void f()\n"
20829                "{\n"
20830                "  // ...\n"
20831                "  [object someMethod:@{a : @\"b\"}];\n"
20832                "}",
20833                AllmanBraceStyle);
20834   verifyFormat("int f()\n"
20835                "{ // comment\n"
20836                "  return 42;\n"
20837                "}",
20838                AllmanBraceStyle);
20839 
20840   AllmanBraceStyle.ColumnLimit = 19;
20841   verifyFormat("void f() { int i; }", AllmanBraceStyle);
20842   AllmanBraceStyle.ColumnLimit = 18;
20843   verifyFormat("void f()\n"
20844                "{\n"
20845                "  int i;\n"
20846                "}",
20847                AllmanBraceStyle);
20848   AllmanBraceStyle.ColumnLimit = 80;
20849 
20850   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
20851   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
20852       FormatStyle::SIS_WithoutElse;
20853   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
20854   verifyFormat("void f(bool b)\n"
20855                "{\n"
20856                "  if (b)\n"
20857                "  {\n"
20858                "    return;\n"
20859                "  }\n"
20860                "}",
20861                BreakBeforeBraceShortIfs);
20862   verifyFormat("void f(bool b)\n"
20863                "{\n"
20864                "  if constexpr (b)\n"
20865                "  {\n"
20866                "    return;\n"
20867                "  }\n"
20868                "}",
20869                BreakBeforeBraceShortIfs);
20870   verifyFormat("void f(bool b)\n"
20871                "{\n"
20872                "  if CONSTEXPR (b)\n"
20873                "  {\n"
20874                "    return;\n"
20875                "  }\n"
20876                "}",
20877                BreakBeforeBraceShortIfs);
20878   verifyFormat("void f(bool b)\n"
20879                "{\n"
20880                "  if (b) return;\n"
20881                "}",
20882                BreakBeforeBraceShortIfs);
20883   verifyFormat("void f(bool b)\n"
20884                "{\n"
20885                "  if constexpr (b) return;\n"
20886                "}",
20887                BreakBeforeBraceShortIfs);
20888   verifyFormat("void f(bool b)\n"
20889                "{\n"
20890                "  if CONSTEXPR (b) return;\n"
20891                "}",
20892                BreakBeforeBraceShortIfs);
20893   verifyFormat("void f(bool b)\n"
20894                "{\n"
20895                "  while (b)\n"
20896                "  {\n"
20897                "    return;\n"
20898                "  }\n"
20899                "}",
20900                BreakBeforeBraceShortIfs);
20901 }
20902 
20903 TEST_F(FormatTest, WhitesmithsBraceBreaking) {
20904   FormatStyle WhitesmithsBraceStyle = getLLVMStyleWithColumns(0);
20905   WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
20906 
20907   // Make a few changes to the style for testing purposes
20908   WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
20909       FormatStyle::SFS_Empty;
20910   WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
20911 
20912   // FIXME: this test case can't decide whether there should be a blank line
20913   // after the ~D() line or not. It adds one if one doesn't exist in the test
20914   // and it removes the line if one exists.
20915   /*
20916   verifyFormat("class A;\n"
20917                "namespace B\n"
20918                "  {\n"
20919                "class C;\n"
20920                "// Comment\n"
20921                "class D\n"
20922                "  {\n"
20923                "public:\n"
20924                "  D();\n"
20925                "  ~D() {}\n"
20926                "private:\n"
20927                "  enum E\n"
20928                "    {\n"
20929                "    F\n"
20930                "    }\n"
20931                "  };\n"
20932                "  } // namespace B",
20933                WhitesmithsBraceStyle);
20934   */
20935 
20936   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None;
20937   verifyFormat("namespace a\n"
20938                "  {\n"
20939                "class A\n"
20940                "  {\n"
20941                "  void f()\n"
20942                "    {\n"
20943                "    if (true)\n"
20944                "      {\n"
20945                "      a();\n"
20946                "      b();\n"
20947                "      }\n"
20948                "    }\n"
20949                "  void g()\n"
20950                "    {\n"
20951                "    return;\n"
20952                "    }\n"
20953                "  };\n"
20954                "struct B\n"
20955                "  {\n"
20956                "  int x;\n"
20957                "  };\n"
20958                "  } // namespace a",
20959                WhitesmithsBraceStyle);
20960 
20961   verifyFormat("namespace a\n"
20962                "  {\n"
20963                "namespace b\n"
20964                "  {\n"
20965                "class A\n"
20966                "  {\n"
20967                "  void f()\n"
20968                "    {\n"
20969                "    if (true)\n"
20970                "      {\n"
20971                "      a();\n"
20972                "      b();\n"
20973                "      }\n"
20974                "    }\n"
20975                "  void g()\n"
20976                "    {\n"
20977                "    return;\n"
20978                "    }\n"
20979                "  };\n"
20980                "struct B\n"
20981                "  {\n"
20982                "  int x;\n"
20983                "  };\n"
20984                "  } // namespace b\n"
20985                "  } // namespace a",
20986                WhitesmithsBraceStyle);
20987 
20988   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner;
20989   verifyFormat("namespace a\n"
20990                "  {\n"
20991                "namespace b\n"
20992                "  {\n"
20993                "  class A\n"
20994                "    {\n"
20995                "    void f()\n"
20996                "      {\n"
20997                "      if (true)\n"
20998                "        {\n"
20999                "        a();\n"
21000                "        b();\n"
21001                "        }\n"
21002                "      }\n"
21003                "    void g()\n"
21004                "      {\n"
21005                "      return;\n"
21006                "      }\n"
21007                "    };\n"
21008                "  struct B\n"
21009                "    {\n"
21010                "    int x;\n"
21011                "    };\n"
21012                "  } // namespace b\n"
21013                "  } // namespace a",
21014                WhitesmithsBraceStyle);
21015 
21016   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All;
21017   verifyFormat("namespace a\n"
21018                "  {\n"
21019                "  namespace b\n"
21020                "    {\n"
21021                "    class A\n"
21022                "      {\n"
21023                "      void f()\n"
21024                "        {\n"
21025                "        if (true)\n"
21026                "          {\n"
21027                "          a();\n"
21028                "          b();\n"
21029                "          }\n"
21030                "        }\n"
21031                "      void g()\n"
21032                "        {\n"
21033                "        return;\n"
21034                "        }\n"
21035                "      };\n"
21036                "    struct B\n"
21037                "      {\n"
21038                "      int x;\n"
21039                "      };\n"
21040                "    } // namespace b\n"
21041                "  } // namespace a",
21042                WhitesmithsBraceStyle);
21043 
21044   verifyFormat("void f()\n"
21045                "  {\n"
21046                "  if (true)\n"
21047                "    {\n"
21048                "    a();\n"
21049                "    }\n"
21050                "  else if (false)\n"
21051                "    {\n"
21052                "    b();\n"
21053                "    }\n"
21054                "  else\n"
21055                "    {\n"
21056                "    c();\n"
21057                "    }\n"
21058                "  }",
21059                WhitesmithsBraceStyle);
21060 
21061   verifyFormat("void f()\n"
21062                "  {\n"
21063                "  for (int i = 0; i < 10; ++i)\n"
21064                "    {\n"
21065                "    a();\n"
21066                "    }\n"
21067                "  while (false)\n"
21068                "    {\n"
21069                "    b();\n"
21070                "    }\n"
21071                "  do\n"
21072                "    {\n"
21073                "    c();\n"
21074                "    } while (false)\n"
21075                "  }",
21076                WhitesmithsBraceStyle);
21077 
21078   WhitesmithsBraceStyle.IndentCaseLabels = true;
21079   verifyFormat("void switchTest1(int a)\n"
21080                "  {\n"
21081                "  switch (a)\n"
21082                "    {\n"
21083                "    case 2:\n"
21084                "      {\n"
21085                "      }\n"
21086                "      break;\n"
21087                "    }\n"
21088                "  }",
21089                WhitesmithsBraceStyle);
21090 
21091   verifyFormat("void switchTest2(int a)\n"
21092                "  {\n"
21093                "  switch (a)\n"
21094                "    {\n"
21095                "    case 0:\n"
21096                "      break;\n"
21097                "    case 1:\n"
21098                "      {\n"
21099                "      break;\n"
21100                "      }\n"
21101                "    case 2:\n"
21102                "      {\n"
21103                "      }\n"
21104                "      break;\n"
21105                "    default:\n"
21106                "      break;\n"
21107                "    }\n"
21108                "  }",
21109                WhitesmithsBraceStyle);
21110 
21111   verifyFormat("void switchTest3(int a)\n"
21112                "  {\n"
21113                "  switch (a)\n"
21114                "    {\n"
21115                "    case 0:\n"
21116                "      {\n"
21117                "      foo(x);\n"
21118                "      }\n"
21119                "      break;\n"
21120                "    default:\n"
21121                "      {\n"
21122                "      foo(1);\n"
21123                "      }\n"
21124                "      break;\n"
21125                "    }\n"
21126                "  }",
21127                WhitesmithsBraceStyle);
21128 
21129   WhitesmithsBraceStyle.IndentCaseLabels = false;
21130 
21131   verifyFormat("void switchTest4(int a)\n"
21132                "  {\n"
21133                "  switch (a)\n"
21134                "    {\n"
21135                "  case 2:\n"
21136                "    {\n"
21137                "    }\n"
21138                "    break;\n"
21139                "    }\n"
21140                "  }",
21141                WhitesmithsBraceStyle);
21142 
21143   verifyFormat("void switchTest5(int a)\n"
21144                "  {\n"
21145                "  switch (a)\n"
21146                "    {\n"
21147                "  case 0:\n"
21148                "    break;\n"
21149                "  case 1:\n"
21150                "    {\n"
21151                "    foo();\n"
21152                "    break;\n"
21153                "    }\n"
21154                "  case 2:\n"
21155                "    {\n"
21156                "    }\n"
21157                "    break;\n"
21158                "  default:\n"
21159                "    break;\n"
21160                "    }\n"
21161                "  }",
21162                WhitesmithsBraceStyle);
21163 
21164   verifyFormat("void switchTest6(int a)\n"
21165                "  {\n"
21166                "  switch (a)\n"
21167                "    {\n"
21168                "  case 0:\n"
21169                "    {\n"
21170                "    foo(x);\n"
21171                "    }\n"
21172                "    break;\n"
21173                "  default:\n"
21174                "    {\n"
21175                "    foo(1);\n"
21176                "    }\n"
21177                "    break;\n"
21178                "    }\n"
21179                "  }",
21180                WhitesmithsBraceStyle);
21181 
21182   verifyFormat("enum X\n"
21183                "  {\n"
21184                "  Y = 0, // testing\n"
21185                "  }",
21186                WhitesmithsBraceStyle);
21187 
21188   verifyFormat("enum X\n"
21189                "  {\n"
21190                "  Y = 0\n"
21191                "  }",
21192                WhitesmithsBraceStyle);
21193   verifyFormat("enum X\n"
21194                "  {\n"
21195                "  Y = 0,\n"
21196                "  Z = 1\n"
21197                "  };",
21198                WhitesmithsBraceStyle);
21199 
21200   verifyFormat("@interface BSApplicationController ()\n"
21201                "  {\n"
21202                "@private\n"
21203                "  id _extraIvar;\n"
21204                "  }\n"
21205                "@end",
21206                WhitesmithsBraceStyle);
21207 
21208   verifyFormat("#ifdef _DEBUG\n"
21209                "int foo(int i = 0)\n"
21210                "#else\n"
21211                "int foo(int i = 5)\n"
21212                "#endif\n"
21213                "  {\n"
21214                "  return i;\n"
21215                "  }",
21216                WhitesmithsBraceStyle);
21217 
21218   verifyFormat("void foo() {}\n"
21219                "void bar()\n"
21220                "#ifdef _DEBUG\n"
21221                "  {\n"
21222                "  foo();\n"
21223                "  }\n"
21224                "#else\n"
21225                "  {\n"
21226                "  }\n"
21227                "#endif",
21228                WhitesmithsBraceStyle);
21229 
21230   verifyFormat("void foobar()\n"
21231                "  {\n"
21232                "  int i = 5;\n"
21233                "  }\n"
21234                "#ifdef _DEBUG\n"
21235                "void bar() {}\n"
21236                "#else\n"
21237                "void bar()\n"
21238                "  {\n"
21239                "  foobar();\n"
21240                "  }\n"
21241                "#endif",
21242                WhitesmithsBraceStyle);
21243 
21244   // This shouldn't affect ObjC blocks..
21245   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
21246                "  // ...\n"
21247                "  int i;\n"
21248                "}];",
21249                WhitesmithsBraceStyle);
21250   verifyFormat("void (^block)(void) = ^{\n"
21251                "  // ...\n"
21252                "  int i;\n"
21253                "};",
21254                WhitesmithsBraceStyle);
21255   // .. or dict literals.
21256   verifyFormat("void f()\n"
21257                "  {\n"
21258                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
21259                "  }",
21260                WhitesmithsBraceStyle);
21261 
21262   verifyFormat("int f()\n"
21263                "  { // comment\n"
21264                "  return 42;\n"
21265                "  }",
21266                WhitesmithsBraceStyle);
21267 
21268   FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
21269   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
21270       FormatStyle::SIS_OnlyFirstIf;
21271   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
21272   verifyFormat("void f(bool b)\n"
21273                "  {\n"
21274                "  if (b)\n"
21275                "    {\n"
21276                "    return;\n"
21277                "    }\n"
21278                "  }",
21279                BreakBeforeBraceShortIfs);
21280   verifyFormat("void f(bool b)\n"
21281                "  {\n"
21282                "  if (b) return;\n"
21283                "  }",
21284                BreakBeforeBraceShortIfs);
21285   verifyFormat("void f(bool b)\n"
21286                "  {\n"
21287                "  while (b)\n"
21288                "    {\n"
21289                "    return;\n"
21290                "    }\n"
21291                "  }",
21292                BreakBeforeBraceShortIfs);
21293 }
21294 
21295 TEST_F(FormatTest, GNUBraceBreaking) {
21296   FormatStyle GNUBraceStyle = getLLVMStyle();
21297   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
21298   verifyFormat("namespace a\n"
21299                "{\n"
21300                "class A\n"
21301                "{\n"
21302                "  void f()\n"
21303                "  {\n"
21304                "    int a;\n"
21305                "    {\n"
21306                "      int b;\n"
21307                "    }\n"
21308                "    if (true)\n"
21309                "      {\n"
21310                "        a();\n"
21311                "        b();\n"
21312                "      }\n"
21313                "  }\n"
21314                "  void g() { return; }\n"
21315                "}\n"
21316                "} // namespace a",
21317                GNUBraceStyle);
21318 
21319   verifyFormat("void f()\n"
21320                "{\n"
21321                "  if (true)\n"
21322                "    {\n"
21323                "      a();\n"
21324                "    }\n"
21325                "  else if (false)\n"
21326                "    {\n"
21327                "      b();\n"
21328                "    }\n"
21329                "  else\n"
21330                "    {\n"
21331                "      c();\n"
21332                "    }\n"
21333                "}",
21334                GNUBraceStyle);
21335 
21336   verifyFormat("void f()\n"
21337                "{\n"
21338                "  for (int i = 0; i < 10; ++i)\n"
21339                "    {\n"
21340                "      a();\n"
21341                "    }\n"
21342                "  while (false)\n"
21343                "    {\n"
21344                "      b();\n"
21345                "    }\n"
21346                "  do\n"
21347                "    {\n"
21348                "      c();\n"
21349                "    }\n"
21350                "  while (false);\n"
21351                "}",
21352                GNUBraceStyle);
21353 
21354   verifyFormat("void f(int a)\n"
21355                "{\n"
21356                "  switch (a)\n"
21357                "    {\n"
21358                "    case 0:\n"
21359                "      break;\n"
21360                "    case 1:\n"
21361                "      {\n"
21362                "        break;\n"
21363                "      }\n"
21364                "    case 2:\n"
21365                "      {\n"
21366                "      }\n"
21367                "      break;\n"
21368                "    default:\n"
21369                "      break;\n"
21370                "    }\n"
21371                "}",
21372                GNUBraceStyle);
21373 
21374   verifyFormat("enum X\n"
21375                "{\n"
21376                "  Y = 0,\n"
21377                "}",
21378                GNUBraceStyle);
21379 
21380   verifyFormat("@interface BSApplicationController ()\n"
21381                "{\n"
21382                "@private\n"
21383                "  id _extraIvar;\n"
21384                "}\n"
21385                "@end",
21386                GNUBraceStyle);
21387 
21388   verifyFormat("#ifdef _DEBUG\n"
21389                "int foo(int i = 0)\n"
21390                "#else\n"
21391                "int foo(int i = 5)\n"
21392                "#endif\n"
21393                "{\n"
21394                "  return i;\n"
21395                "}",
21396                GNUBraceStyle);
21397 
21398   verifyFormat("void foo() {}\n"
21399                "void bar()\n"
21400                "#ifdef _DEBUG\n"
21401                "{\n"
21402                "  foo();\n"
21403                "}\n"
21404                "#else\n"
21405                "{\n"
21406                "}\n"
21407                "#endif",
21408                GNUBraceStyle);
21409 
21410   verifyFormat("void foobar() { int i = 5; }\n"
21411                "#ifdef _DEBUG\n"
21412                "void bar() {}\n"
21413                "#else\n"
21414                "void bar() { foobar(); }\n"
21415                "#endif",
21416                GNUBraceStyle);
21417 }
21418 
21419 TEST_F(FormatTest, WebKitBraceBreaking) {
21420   FormatStyle WebKitBraceStyle = getLLVMStyle();
21421   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
21422   WebKitBraceStyle.FixNamespaceComments = false;
21423   verifyFormat("namespace a {\n"
21424                "class A {\n"
21425                "  void f()\n"
21426                "  {\n"
21427                "    if (true) {\n"
21428                "      a();\n"
21429                "      b();\n"
21430                "    }\n"
21431                "  }\n"
21432                "  void g() { return; }\n"
21433                "};\n"
21434                "enum E {\n"
21435                "  A,\n"
21436                "  // foo\n"
21437                "  B,\n"
21438                "  C\n"
21439                "};\n"
21440                "struct B {\n"
21441                "  int x;\n"
21442                "};\n"
21443                "}",
21444                WebKitBraceStyle);
21445   verifyFormat("struct S {\n"
21446                "  int Type;\n"
21447                "  union {\n"
21448                "    int x;\n"
21449                "    double y;\n"
21450                "  } Value;\n"
21451                "  class C {\n"
21452                "    MyFavoriteType Value;\n"
21453                "  } Class;\n"
21454                "};",
21455                WebKitBraceStyle);
21456 }
21457 
21458 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
21459   verifyFormat("void f() {\n"
21460                "  try {\n"
21461                "  } catch (const Exception &e) {\n"
21462                "  }\n"
21463                "}");
21464 }
21465 
21466 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
21467   auto Style = getLLVMStyle();
21468   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
21469   verifyNoCrash("f({\n"
21470                 "table({}, table({{\"\", false}}, {}))\n"
21471                 "});",
21472                 Style);
21473 
21474   Style.AlignConsecutiveAssignments.Enabled = true;
21475   Style.AlignConsecutiveDeclarations.Enabled = true;
21476   verifyFormat("struct test demo[] = {\n"
21477                "    {56,    23, \"hello\"},\n"
21478                "    {-1, 93463, \"world\"},\n"
21479                "    { 7,     5,    \"!!\"}\n"
21480                "};",
21481                Style);
21482 
21483   verifyFormat("struct test demo[] = {\n"
21484                "    {56,    23, \"hello\"}, // first line\n"
21485                "    {-1, 93463, \"world\"}, // second line\n"
21486                "    { 7,     5,    \"!!\"}  // third line\n"
21487                "};",
21488                Style);
21489 
21490   verifyFormat("struct test demo[4] = {\n"
21491                "    { 56,    23, 21,       \"oh\"}, // first line\n"
21492                "    { -1, 93463, 22,       \"my\"}, // second line\n"
21493                "    {  7,     5,  1, \"goodness\"}  // third line\n"
21494                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
21495                "};",
21496                Style);
21497 
21498   verifyFormat("struct test demo[3] = {\n"
21499                "    {56,    23, \"hello\"},\n"
21500                "    {-1, 93463, \"world\"},\n"
21501                "    { 7,     5,    \"!!\"}\n"
21502                "};",
21503                Style);
21504 
21505   verifyFormat("struct test demo[3] = {\n"
21506                "    {int{56},    23, \"hello\"},\n"
21507                "    {int{-1}, 93463, \"world\"},\n"
21508                "    { int{7},     5,    \"!!\"}\n"
21509                "};",
21510                Style);
21511 
21512   verifyFormat("struct test demo[] = {\n"
21513                "    {56,    23, \"hello\"},\n"
21514                "    {-1, 93463, \"world\"},\n"
21515                "    { 7,     5,    \"!!\"},\n"
21516                "};",
21517                Style);
21518 
21519   verifyFormat("test demo[] = {\n"
21520                "    {56,    23, \"hello\"},\n"
21521                "    {-1, 93463, \"world\"},\n"
21522                "    { 7,     5,    \"!!\"},\n"
21523                "};",
21524                Style);
21525 
21526   verifyFormat("demo = std::array<struct test, 3>{\n"
21527                "    test{56,    23, \"hello\"},\n"
21528                "    test{-1, 93463, \"world\"},\n"
21529                "    test{ 7,     5,    \"!!\"},\n"
21530                "};",
21531                Style);
21532 
21533   verifyFormat("test demo[] = {\n"
21534                "    {56,    23, \"hello\"},\n"
21535                "#if X\n"
21536                "    {-1, 93463, \"world\"},\n"
21537                "#endif\n"
21538                "    { 7,     5,    \"!!\"}\n"
21539                "};",
21540                Style);
21541 
21542   verifyFormat(
21543       "test demo[] = {\n"
21544       "    { 7,    23,\n"
21545       "     \"hello world i am a very long line that really, in any\"\n"
21546       "     \"just world, ought to be split over multiple lines\"},\n"
21547       "    {-1, 93463,                                  \"world\"},\n"
21548       "    {56,     5,                                     \"!!\"}\n"
21549       "};",
21550       Style);
21551 
21552   verifyNoCrash("Foo f[] = {\n"
21553                 "    [0] = { 1, },\n"
21554                 "    [i] { 1, },\n"
21555                 "};",
21556                 Style);
21557   verifyNoCrash("Foo foo[] = {\n"
21558                 "    [0] = {1, 1},\n"
21559                 "    [1] { 1, 1, },\n"
21560                 "    [2] { 1, 1, },\n"
21561                 "};",
21562                 Style);
21563   verifyNoCrash("test arr[] = {\n"
21564                 "#define FOO(i) {i, i},\n"
21565                 "SOME_GENERATOR(FOO)\n"
21566                 "{2, 2}\n"
21567                 "};",
21568                 Style);
21569 
21570   verifyFormat("return GradForUnaryCwise(g, {\n"
21571                "                                {{\"sign\"}, \"Sign\",  "
21572                "  {\"x\", \"dy\"}},\n"
21573                "                                {  {\"dx\"},  \"Mul\", {\"dy\""
21574                ", \"sign\"}},\n"
21575                "});",
21576                Style);
21577 
21578   Style.Cpp11BracedListStyle = false;
21579   verifyFormat("struct test demo[] = {\n"
21580                "  { 56,    23, \"hello\" },\n"
21581                "  { -1, 93463, \"world\" },\n"
21582                "  {  7,     5,    \"!!\" }\n"
21583                "};",
21584                Style);
21585   Style.Cpp11BracedListStyle = true;
21586 
21587   Style.ColumnLimit = 0;
21588   verifyFormat(
21589       "test demo[] = {\n"
21590       "    {56,    23, \"hello world i am a very long line that really, "
21591       "in any just world, ought to be split over multiple lines\"},\n"
21592       "    {-1, 93463,                                                  "
21593       "                                                 \"world\"},\n"
21594       "    { 7,     5,                                                  "
21595       "                                                    \"!!\"},\n"
21596       "};",
21597       "test demo[] = {{56, 23, \"hello world i am a very long line "
21598       "that really, in any just world, ought to be split over multiple "
21599       "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21600       Style);
21601 
21602   Style.ColumnLimit = 80;
21603   verifyFormat("test demo[] = {\n"
21604                "    {56,    23, /* a comment */ \"hello\"},\n"
21605                "    {-1, 93463,                 \"world\"},\n"
21606                "    { 7,     5,                    \"!!\"}\n"
21607                "};",
21608                Style);
21609 
21610   verifyFormat("test demo[] = {\n"
21611                "    {56,    23,                    \"hello\"},\n"
21612                "    {-1, 93463, \"world\" /* comment here */},\n"
21613                "    { 7,     5,                       \"!!\"}\n"
21614                "};",
21615                Style);
21616 
21617   verifyFormat("test demo[] = {\n"
21618                "    {56, /* a comment */ 23, \"hello\"},\n"
21619                "    {-1,              93463, \"world\"},\n"
21620                "    { 7,                  5,    \"!!\"}\n"
21621                "};",
21622                Style);
21623 
21624   Style.ColumnLimit = 20;
21625   verifyFormat("demo = std::array<\n"
21626                "    struct test, 3>{\n"
21627                "    test{\n"
21628                "         56,    23,\n"
21629                "         \"hello \"\n"
21630                "         \"world i \"\n"
21631                "         \"am a very \"\n"
21632                "         \"long line \"\n"
21633                "         \"that \"\n"
21634                "         \"really, \"\n"
21635                "         \"in any \"\n"
21636                "         \"just \"\n"
21637                "         \"world, \"\n"
21638                "         \"ought to \"\n"
21639                "         \"be split \"\n"
21640                "         \"over \"\n"
21641                "         \"multiple \"\n"
21642                "         \"lines\"},\n"
21643                "    test{-1, 93463,\n"
21644                "         \"world\"},\n"
21645                "    test{ 7,     5,\n"
21646                "         \"!!\"   },\n"
21647                "};",
21648                "demo = std::array<struct test, 3>{test{56, 23, \"hello world "
21649                "i am a very long line that really, in any just world, ought "
21650                "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
21651                "test{7, 5, \"!!\"},};",
21652                Style);
21653   // This caused a core dump by enabling Alignment in the LLVMStyle globally
21654   Style = getLLVMStyleWithColumns(50);
21655   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
21656   verifyFormat("static A x = {\n"
21657                "    {{init1, init2, init3, init4},\n"
21658                "     {init1, init2, init3, init4}}\n"
21659                "};",
21660                Style);
21661   // TODO: Fix the indentations below when this option is fully functional.
21662 #if 0
21663   verifyFormat("int a[][] = {\n"
21664                "    {\n"
21665                "     {0, 2}, //\n"
21666                "     {1, 2}  //\n"
21667                "    }\n"
21668                "};",
21669                Style);
21670 #endif
21671   Style.ColumnLimit = 100;
21672   verifyFormat(
21673       "test demo[] = {\n"
21674       "    {56,    23,\n"
21675       "     \"hello world i am a very long line that really, in any just world"
21676       ", ought to be split over \"\n"
21677       "     \"multiple lines\"  },\n"
21678       "    {-1, 93463, \"world\"},\n"
21679       "    { 7,     5,    \"!!\"},\n"
21680       "};",
21681       "test demo[] = {{56, 23, \"hello world i am a very long line "
21682       "that really, in any just world, ought to be split over multiple "
21683       "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21684       Style);
21685 
21686   Style = getLLVMStyleWithColumns(50);
21687   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
21688   verifyFormat("struct test demo[] = {\n"
21689                "    {56,    23, \"hello\"},\n"
21690                "    {-1, 93463, \"world\"},\n"
21691                "    { 7,     5,    \"!!\"}\n"
21692                "};\n"
21693                "static A x = {\n"
21694                "    {{init1, init2, init3, init4},\n"
21695                "     {init1, init2, init3, init4}}\n"
21696                "};",
21697                Style);
21698   Style.ColumnLimit = 100;
21699   Style.AlignConsecutiveAssignments.AcrossComments = true;
21700   Style.AlignConsecutiveDeclarations.AcrossComments = true;
21701   verifyFormat("struct test demo[] = {\n"
21702                "    {56,    23, \"hello\"},\n"
21703                "    {-1, 93463, \"world\"},\n"
21704                "    { 7,     5,    \"!!\"}\n"
21705                "};\n"
21706                "struct test demo[4] = {\n"
21707                "    { 56,    23, 21,       \"oh\"}, // first line\n"
21708                "    { -1, 93463, 22,       \"my\"}, // second line\n"
21709                "    {  7,     5,  1, \"goodness\"}  // third line\n"
21710                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
21711                "};",
21712                Style);
21713   verifyFormat(
21714       "test demo[] = {\n"
21715       "    {56,\n"
21716       "     \"hello world i am a very long line that really, in any just world"
21717       ", ought to be split over \"\n"
21718       "     \"multiple lines\",    23},\n"
21719       "    {-1,      \"world\", 93463},\n"
21720       "    { 7,         \"!!\",     5},\n"
21721       "};",
21722       "test demo[] = {{56, \"hello world i am a very long line "
21723       "that really, in any just world, ought to be split over multiple "
21724       "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
21725       Style);
21726 }
21727 
21728 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
21729   auto Style = getLLVMStyle();
21730   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
21731   /* FIXME: This case gets misformatted.
21732   verifyFormat("auto foo = Items{\n"
21733                "    Section{0, bar(), },\n"
21734                "    Section{1, boo()  }\n"
21735                "};",
21736                Style);
21737   */
21738   verifyFormat("auto foo = Items{\n"
21739                "    Section{\n"
21740                "            0, bar(),\n"
21741                "            }\n"
21742                "};",
21743                Style);
21744   verifyFormat("struct test demo[] = {\n"
21745                "    {56, 23,    \"hello\"},\n"
21746                "    {-1, 93463, \"world\"},\n"
21747                "    {7,  5,     \"!!\"   }\n"
21748                "};",
21749                Style);
21750   verifyFormat("struct test demo[] = {\n"
21751                "    {56, 23,    \"hello\"}, // first line\n"
21752                "    {-1, 93463, \"world\"}, // second line\n"
21753                "    {7,  5,     \"!!\"   }  // third line\n"
21754                "};",
21755                Style);
21756   verifyFormat("struct test demo[4] = {\n"
21757                "    {56,  23,    21, \"oh\"      }, // first line\n"
21758                "    {-1,  93463, 22, \"my\"      }, // second line\n"
21759                "    {7,   5,     1,  \"goodness\"}  // third line\n"
21760                "    {234, 5,     1,  \"gracious\"}  // fourth line\n"
21761                "};",
21762                Style);
21763   verifyFormat("struct test demo[3] = {\n"
21764                "    {56, 23,    \"hello\"},\n"
21765                "    {-1, 93463, \"world\"},\n"
21766                "    {7,  5,     \"!!\"   }\n"
21767                "};",
21768                Style);
21769 
21770   verifyFormat("struct test demo[3] = {\n"
21771                "    {int{56}, 23,    \"hello\"},\n"
21772                "    {int{-1}, 93463, \"world\"},\n"
21773                "    {int{7},  5,     \"!!\"   }\n"
21774                "};",
21775                Style);
21776   verifyFormat("struct test demo[] = {\n"
21777                "    {56, 23,    \"hello\"},\n"
21778                "    {-1, 93463, \"world\"},\n"
21779                "    {7,  5,     \"!!\"   },\n"
21780                "};",
21781                Style);
21782   verifyFormat("test demo[] = {\n"
21783                "    {56, 23,    \"hello\"},\n"
21784                "    {-1, 93463, \"world\"},\n"
21785                "    {7,  5,     \"!!\"   },\n"
21786                "};",
21787                Style);
21788   verifyFormat("demo = std::array<struct test, 3>{\n"
21789                "    test{56, 23,    \"hello\"},\n"
21790                "    test{-1, 93463, \"world\"},\n"
21791                "    test{7,  5,     \"!!\"   },\n"
21792                "};",
21793                Style);
21794   verifyFormat("test demo[] = {\n"
21795                "    {56, 23,    \"hello\"},\n"
21796                "#if X\n"
21797                "    {-1, 93463, \"world\"},\n"
21798                "#endif\n"
21799                "    {7,  5,     \"!!\"   }\n"
21800                "};",
21801                Style);
21802   verifyFormat(
21803       "test demo[] = {\n"
21804       "    {7,  23,\n"
21805       "     \"hello world i am a very long line that really, in any\"\n"
21806       "     \"just world, ought to be split over multiple lines\"},\n"
21807       "    {-1, 93463, \"world\"                                 },\n"
21808       "    {56, 5,     \"!!\"                                    }\n"
21809       "};",
21810       Style);
21811 
21812   verifyNoCrash("Foo f[] = {\n"
21813                 "    [0] = { 1, },\n"
21814                 "    [i] { 1, },\n"
21815                 "};",
21816                 Style);
21817   verifyNoCrash("Foo foo[] = {\n"
21818                 "    [0] = {1, 1},\n"
21819                 "    [1] { 1, 1, },\n"
21820                 "    [2] { 1, 1, },\n"
21821                 "};",
21822                 Style);
21823   verifyNoCrash("test arr[] = {\n"
21824                 "#define FOO(i) {i, i},\n"
21825                 "SOME_GENERATOR(FOO)\n"
21826                 "{2, 2}\n"
21827                 "};",
21828                 Style);
21829 
21830   verifyFormat("return GradForUnaryCwise(g, {\n"
21831                "                                {{\"sign\"}, \"Sign\", {\"x\", "
21832                "\"dy\"}   },\n"
21833                "                                {{\"dx\"},   \"Mul\",  "
21834                "{\"dy\", \"sign\"}},\n"
21835                "});",
21836                Style);
21837 
21838   Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
21839   verifyFormat("#define FOO \\\n"
21840                "  int foo[][2] = { \\\n"
21841                "      {0, 1} \\\n"
21842                "  };",
21843                Style);
21844 
21845   Style.Cpp11BracedListStyle = false;
21846   verifyFormat("struct test demo[] = {\n"
21847                "  { 56, 23,    \"hello\" },\n"
21848                "  { -1, 93463, \"world\" },\n"
21849                "  { 7,  5,     \"!!\"    }\n"
21850                "};",
21851                Style);
21852   Style.Cpp11BracedListStyle = true;
21853 
21854   Style.ColumnLimit = 0;
21855   verifyFormat(
21856       "test demo[] = {\n"
21857       "    {56, 23,    \"hello world i am a very long line that really, in any "
21858       "just world, ought to be split over multiple lines\"},\n"
21859       "    {-1, 93463, \"world\"                                               "
21860       "                                                   },\n"
21861       "    {7,  5,     \"!!\"                                                  "
21862       "                                                   },\n"
21863       "};",
21864       "test demo[] = {{56, 23, \"hello world i am a very long line "
21865       "that really, in any just world, ought to be split over multiple "
21866       "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21867       Style);
21868 
21869   Style.ColumnLimit = 80;
21870   verifyFormat("test demo[] = {\n"
21871                "    {56, 23,    /* a comment */ \"hello\"},\n"
21872                "    {-1, 93463, \"world\"                },\n"
21873                "    {7,  5,     \"!!\"                   }\n"
21874                "};",
21875                Style);
21876 
21877   verifyFormat("test demo[] = {\n"
21878                "    {56, 23,    \"hello\"                   },\n"
21879                "    {-1, 93463, \"world\" /* comment here */},\n"
21880                "    {7,  5,     \"!!\"                      }\n"
21881                "};",
21882                Style);
21883 
21884   verifyFormat("test demo[] = {\n"
21885                "    {56, /* a comment */ 23, \"hello\"},\n"
21886                "    {-1, 93463,              \"world\"},\n"
21887                "    {7,  5,                  \"!!\"   }\n"
21888                "};",
21889                Style);
21890   verifyFormat("Foo foo = {\n"
21891                "    // comment\n"
21892                "    {1, 2}\n"
21893                "};",
21894                Style);
21895 
21896   Style.ColumnLimit = 20;
21897   // FIXME: unstable test case
21898   EXPECT_EQ(
21899       "demo = std::array<\n"
21900       "    struct test, 3>{\n"
21901       "    test{\n"
21902       "         56, 23,\n"
21903       "         \"hello \"\n"
21904       "         \"world i \"\n"
21905       "         \"am a very \"\n"
21906       "         \"long line \"\n"
21907       "         \"that \"\n"
21908       "         \"really, \"\n"
21909       "         \"in any \"\n"
21910       "         \"just \"\n"
21911       "         \"world, \"\n"
21912       "         \"ought to \"\n"
21913       "         \"be split \"\n"
21914       "         \"over \"\n"
21915       "         \"multiple \"\n"
21916       "         \"lines\"},\n"
21917       "    test{-1, 93463,\n"
21918       "         \"world\"},\n"
21919       "    test{7,  5,\n"
21920       "         \"!!\"   },\n"
21921       "};",
21922       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
21923              "i am a very long line that really, in any just world, ought "
21924              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
21925              "test{7, 5, \"!!\"},};",
21926              Style));
21927 
21928   // This caused a core dump by enabling Alignment in the LLVMStyle globally
21929   Style = getLLVMStyleWithColumns(50);
21930   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
21931   verifyFormat("static A x = {\n"
21932                "    {{init1, init2, init3, init4},\n"
21933                "     {init1, init2, init3, init4}}\n"
21934                "};",
21935                Style);
21936   Style.ColumnLimit = 100;
21937   verifyFormat(
21938       "test demo[] = {\n"
21939       "    {56, 23,\n"
21940       "     \"hello world i am a very long line that really, in any just world"
21941       ", ought to be split over \"\n"
21942       "     \"multiple lines\"  },\n"
21943       "    {-1, 93463, \"world\"},\n"
21944       "    {7,  5,     \"!!\"   },\n"
21945       "};",
21946       "test demo[] = {{56, 23, \"hello world i am a very long line "
21947       "that really, in any just world, ought to be split over multiple "
21948       "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21949       Style);
21950 
21951   Style.ColumnLimit = 25;
21952   verifyNoCrash("Type foo{\n"
21953                 "    {\n"
21954                 "        1,  // A\n"
21955                 "        2,  // B\n"
21956                 "        3,  // C\n"
21957                 "    },\n"
21958                 "    \"hello\",\n"
21959                 "};",
21960                 Style);
21961   verifyNoCrash("Type object[X][Y] = {\n"
21962                 "    {{val}, {val}, {val}},\n"
21963                 "    {{val}, {val}, // some comment\n"
21964                 "                   {val}}\n"
21965                 "};",
21966                 Style);
21967 
21968   Style.ColumnLimit = 120;
21969   verifyNoCrash(
21970       "T v[] {\n"
21971       "    { AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaa, "
21972       "AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaaaaaaa, 1, 0.000000000f, "
21973       "\"00000000000000000000000000000000000000000000000000000000"
21974       "00000000000000000000000000000000000000000000000000000000\" },\n"
21975       "};",
21976       Style);
21977 
21978   Style.SpacesInParens = FormatStyle::SIPO_Custom;
21979   Style.SpacesInParensOptions.Other = true;
21980   verifyFormat("Foo foo[] = {\n"
21981                "    {1, 1},\n"
21982                "    {1, 1},\n"
21983                "};",
21984                Style);
21985 }
21986 
21987 TEST_F(FormatTest, UnderstandsPragmas) {
21988   verifyFormat("#pragma omp reduction(| : var)");
21989   verifyFormat("#pragma omp reduction(+ : var)");
21990 
21991   verifyFormat("#pragma mark Any non-hyphenated or hyphenated string "
21992                "(including parentheses).",
21993                "#pragma    mark   Any non-hyphenated or hyphenated string "
21994                "(including parentheses).");
21995 
21996   verifyFormat("#pragma mark Any non-hyphenated or hyphenated string "
21997                "(including parentheses).",
21998                "#pragma    mark   Any non-hyphenated or hyphenated string "
21999                "(including parentheses).");
22000 
22001   verifyFormat("#pragma comment(linker,    \\\n"
22002                "                \"argument\" \\\n"
22003                "                \"argument\"",
22004                "#pragma comment(linker,      \\\n"
22005                "                 \"argument\" \\\n"
22006                "                 \"argument\"",
22007                getStyleWithColumns(
22008                    getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp), 32));
22009 }
22010 
22011 TEST_F(FormatTest, UnderstandsPragmaOmpTarget) {
22012   verifyFormat("#pragma omp target map(to : var)");
22013   verifyFormat("#pragma omp target map(to : var[ : N])");
22014   verifyFormat("#pragma omp target map(to : var[0 : N])");
22015   verifyFormat("#pragma omp target map(always, to : var[0 : N])");
22016 
22017   verifyFormat(
22018       "#pragma omp target       \\\n"
22019       "    reduction(+ : var)   \\\n"
22020       "    map(to : A[0 : N])   \\\n"
22021       "    map(to : B[0 : N])   \\\n"
22022       "    map(from : C[0 : N]) \\\n"
22023       "    firstprivate(i)      \\\n"
22024       "    firstprivate(j)      \\\n"
22025       "    firstprivate(k)",
22026       "#pragma omp target reduction(+:var) map(to:A[0:N]) map(to:B[0:N]) "
22027       "map(from:C[0:N]) firstprivate(i) firstprivate(j) firstprivate(k)",
22028       getLLVMStyleWithColumns(26));
22029 }
22030 
22031 TEST_F(FormatTest, UnderstandPragmaOption) {
22032   verifyFormat("#pragma option -C -A");
22033 
22034   verifyFormat("#pragma option -C -A", "#pragma    option   -C   -A");
22035 }
22036 
22037 TEST_F(FormatTest, UnderstandPragmaRegion) {
22038   auto Style = getLLVMStyleWithColumns(0);
22039   verifyFormat("#pragma region TEST(FOO : BAR)", Style);
22040   verifyFormat("#pragma region TEST(FOO: NOSPACE)", Style);
22041 }
22042 
22043 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
22044   FormatStyle Style = getLLVMStyleWithColumns(20);
22045 
22046   // See PR41213
22047   verifyFormat("/*\n"
22048                " *\t9012345\n"
22049                " * /8901\n"
22050                " */",
22051                "/*\n"
22052                " *\t9012345 /8901\n"
22053                " */",
22054                Style);
22055   verifyFormat("/*\n"
22056                " *345678\n"
22057                " *\t/8901\n"
22058                " */",
22059                "/*\n"
22060                " *345678\t/8901\n"
22061                " */",
22062                Style);
22063 
22064   verifyFormat("int a; // the\n"
22065                "       // comment",
22066                Style);
22067   verifyNoChange("int a; /* first line\n"
22068                  "        * second\n"
22069                  "        * line third\n"
22070                  "        * line\n"
22071                  "        */",
22072                  Style);
22073   verifyFormat("int a; // first line\n"
22074                "       // second\n"
22075                "       // line third\n"
22076                "       // line",
22077                "int a; // first line\n"
22078                "       // second line\n"
22079                "       // third line",
22080                Style);
22081 
22082   Style.PenaltyExcessCharacter = 90;
22083   verifyFormat("int a; // the comment", Style);
22084   verifyFormat("int a; // the comment\n"
22085                "       // aaa",
22086                "int a; // the comment aaa", Style);
22087   verifyNoChange("int a; /* first line\n"
22088                  "        * second line\n"
22089                  "        * third line\n"
22090                  "        */",
22091                  Style);
22092   verifyFormat("int a; // first line\n"
22093                "       // second line\n"
22094                "       // third line",
22095                Style);
22096   // FIXME: Investigate why this is not getting the same layout as the test
22097   // above.
22098   verifyFormat("int a; /* first line\n"
22099                "        * second line\n"
22100                "        * third line\n"
22101                "        */",
22102                "int a; /* first line second line third line"
22103                "\n*/",
22104                Style);
22105 
22106   verifyFormat("// foo bar baz bazfoo\n"
22107                "// foo bar foo bar",
22108                "// foo bar baz bazfoo\n"
22109                "// foo bar foo           bar",
22110                Style);
22111   verifyFormat("// foo bar baz bazfoo\n"
22112                "// foo bar foo bar",
22113                "// foo bar baz      bazfoo\n"
22114                "// foo            bar foo bar",
22115                Style);
22116 
22117   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
22118   // next one.
22119   verifyFormat("// foo bar baz bazfoo\n"
22120                "// bar foo bar",
22121                "// foo bar baz      bazfoo bar\n"
22122                "// foo            bar",
22123                Style);
22124 
22125   // FIXME: unstable test case
22126   EXPECT_EQ("// foo bar baz bazfoo\n"
22127             "// foo bar baz bazfoo\n"
22128             "// bar foo bar",
22129             format("// foo bar baz      bazfoo\n"
22130                    "// foo bar baz      bazfoo bar\n"
22131                    "// foo bar",
22132                    Style));
22133 
22134   // FIXME: unstable test case
22135   EXPECT_EQ("// foo bar baz bazfoo\n"
22136             "// foo bar baz bazfoo\n"
22137             "// bar foo bar",
22138             format("// foo bar baz      bazfoo\n"
22139                    "// foo bar baz      bazfoo bar\n"
22140                    "// foo           bar",
22141                    Style));
22142 
22143   // Make sure we do not keep protruding characters if strict mode reflow is
22144   // cheaper than keeping protruding characters.
22145   Style.ColumnLimit = 21;
22146   verifyFormat("// foo foo foo foo\n"
22147                "// foo foo foo foo\n"
22148                "// foo foo foo foo",
22149                "// foo foo foo foo foo foo foo foo foo foo foo foo", Style);
22150 
22151   verifyFormat("int a = /* long block\n"
22152                "           comment */\n"
22153                "    42;",
22154                "int a = /* long block comment */ 42;", Style);
22155 }
22156 
22157 TEST_F(FormatTest, BreakPenaltyAfterLParen) {
22158   FormatStyle Style = getLLVMStyle();
22159   Style.ColumnLimit = 8;
22160   Style.PenaltyExcessCharacter = 15;
22161   verifyFormat("int foo(\n"
22162                "    int aaaaaaaaaaaaaaaaaaaaaaaa);",
22163                Style);
22164   Style.PenaltyBreakOpenParenthesis = 200;
22165   verifyFormat("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);",
22166                "int foo(\n"
22167                "    int aaaaaaaaaaaaaaaaaaaaaaaa);",
22168                Style);
22169 }
22170 
22171 TEST_F(FormatTest, BreakPenaltyAfterCastLParen) {
22172   FormatStyle Style = getLLVMStyle();
22173   Style.ColumnLimit = 5;
22174   Style.PenaltyExcessCharacter = 150;
22175   verifyFormat("foo((\n"
22176                "    int)aaaaaaaaaaaaaaaaaaaaaaaa);",
22177 
22178                Style);
22179   Style.PenaltyBreakOpenParenthesis = 100'000;
22180   verifyFormat("foo((int)\n"
22181                "        aaaaaaaaaaaaaaaaaaaaaaaa);",
22182                "foo((\n"
22183                "int)aaaaaaaaaaaaaaaaaaaaaaaa);",
22184                Style);
22185 }
22186 
22187 TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) {
22188   FormatStyle Style = getLLVMStyle();
22189   Style.ColumnLimit = 4;
22190   Style.PenaltyExcessCharacter = 100;
22191   verifyFormat("for (\n"
22192                "    int iiiiiiiiiiiiiiiii =\n"
22193                "        0;\n"
22194                "    iiiiiiiiiiiiiiiii <\n"
22195                "    2;\n"
22196                "    iiiiiiiiiiiiiiiii++) {\n"
22197                "}",
22198 
22199                Style);
22200   Style.PenaltyBreakOpenParenthesis = 1250;
22201   verifyFormat("for (int iiiiiiiiiiiiiiiii =\n"
22202                "         0;\n"
22203                "     iiiiiiiiiiiiiiiii <\n"
22204                "     2;\n"
22205                "     iiiiiiiiiiiiiiiii++) {\n"
22206                "}",
22207                "for (\n"
22208                "    int iiiiiiiiiiiiiiiii =\n"
22209                "        0;\n"
22210                "    iiiiiiiiiiiiiiiii <\n"
22211                "    2;\n"
22212                "    iiiiiiiiiiiiiiiii++) {\n"
22213                "}",
22214                Style);
22215 }
22216 
22217 TEST_F(FormatTest, BreakPenaltyScopeResolution) {
22218   FormatStyle Style = getLLVMStyle();
22219   Style.ColumnLimit = 20;
22220   Style.PenaltyExcessCharacter = 100;
22221   verifyFormat("unsigned long\n"
22222                "foo::bar();",
22223                Style);
22224   Style.PenaltyBreakScopeResolution = 10;
22225   verifyFormat("unsigned long foo::\n"
22226                "    bar();",
22227                Style);
22228 }
22229 
22230 TEST_F(FormatTest, WorksFor8bitEncodings) {
22231   // FIXME: unstable test case
22232   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
22233             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
22234             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
22235             "\"\xef\xee\xf0\xf3...\"",
22236             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
22237                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
22238                    "\xef\xee\xf0\xf3...\"",
22239                    getLLVMStyleWithColumns(12)));
22240 }
22241 
22242 TEST_F(FormatTest, HandlesUTF8BOM) {
22243   verifyFormat("\xef\xbb\xbf");
22244   verifyFormat("\xef\xbb\xbf#include <iostream>");
22245   verifyFormat("\xef\xbb\xbf\n#include <iostream>");
22246 
22247   auto Style = getLLVMStyle();
22248   Style.KeepEmptyLines.AtStartOfFile = false;
22249   verifyFormat("\xef\xbb\xbf#include <iostream>",
22250                "\xef\xbb\xbf\n#include <iostream>", Style);
22251 }
22252 
22253 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
22254 #if !defined(_MSC_VER)
22255 
22256 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
22257   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
22258                getLLVMStyleWithColumns(35));
22259   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
22260                getLLVMStyleWithColumns(31));
22261   verifyFormat("// Однажды в студёную зимнюю пору...",
22262                getLLVMStyleWithColumns(36));
22263   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
22264   verifyFormat("/* Однажды в студёную зимнюю пору... */",
22265                getLLVMStyleWithColumns(39));
22266   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
22267                getLLVMStyleWithColumns(35));
22268 }
22269 
22270 TEST_F(FormatTest, SplitsUTF8Strings) {
22271   // Non-printable characters' width is currently considered to be the length in
22272   // bytes in UTF8. The characters can be displayed in very different manner
22273   // (zero-width, single width with a substitution glyph, expanded to their code
22274   // (e.g. "<8d>"), so there's no single correct way to handle them.
22275   // FIXME: unstable test case
22276   EXPECT_EQ("\"aaaaÄ\"\n"
22277             "\"\xc2\x8d\";",
22278             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
22279   // FIXME: unstable test case
22280   EXPECT_EQ("\"aaaaaaaÄ\"\n"
22281             "\"\xc2\x8d\";",
22282             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
22283   // FIXME: unstable test case
22284   EXPECT_EQ("\"Однажды, в \"\n"
22285             "\"студёную \"\n"
22286             "\"зимнюю \"\n"
22287             "\"пору,\"",
22288             format("\"Однажды, в студёную зимнюю пору,\"",
22289                    getLLVMStyleWithColumns(13)));
22290   // FIXME: unstable test case
22291   EXPECT_EQ(
22292       "\"一 二 三 \"\n"
22293       "\"四 五六 \"\n"
22294       "\"七 八 九 \"\n"
22295       "\"十\"",
22296       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
22297   // FIXME: unstable test case
22298   EXPECT_EQ("\"一\t\"\n"
22299             "\"二 \t\"\n"
22300             "\"三 四 \"\n"
22301             "\"五\t\"\n"
22302             "\"六 \t\"\n"
22303             "\"七 \"\n"
22304             "\"八九十\tqq\"",
22305             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
22306                    getLLVMStyleWithColumns(11)));
22307 
22308   // UTF8 character in an escape sequence.
22309   // FIXME: unstable test case
22310   EXPECT_EQ("\"aaaaaa\"\n"
22311             "\"\\\xC2\x8D\"",
22312             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
22313 }
22314 
22315 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
22316   verifyFormat("const char *sssss =\n"
22317                "    \"一二三四五六七八\\\n"
22318                " 九 十\";",
22319                "const char *sssss = \"一二三四五六七八\\\n"
22320                " 九 十\";",
22321                getLLVMStyleWithColumns(30));
22322 }
22323 
22324 TEST_F(FormatTest, SplitsUTF8LineComments) {
22325   verifyFormat("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10));
22326   verifyFormat("// Я из лесу\n"
22327                "// вышел; был\n"
22328                "// сильный\n"
22329                "// мороз.",
22330                "// Я из лесу вышел; был сильный мороз.",
22331                getLLVMStyleWithColumns(13));
22332   verifyFormat("// 一二三\n"
22333                "// 四五六七\n"
22334                "// 八  九\n"
22335                "// 十",
22336                "// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9));
22337 }
22338 
22339 TEST_F(FormatTest, SplitsUTF8BlockComments) {
22340   verifyFormat("/* Гляжу,\n"
22341                " * поднимается\n"
22342                " * медленно в\n"
22343                " * гору\n"
22344                " * Лошадка,\n"
22345                " * везущая\n"
22346                " * хворосту\n"
22347                " * воз. */",
22348                "/* Гляжу, поднимается медленно в гору\n"
22349                " * Лошадка, везущая хворосту воз. */",
22350                getLLVMStyleWithColumns(13));
22351   verifyFormat("/* 一二三\n"
22352                " * 四五六七\n"
22353                " * 八  九\n"
22354                " * 十  */",
22355                "/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9));
22356   verifyFormat("/* �������� ��������\n"
22357                " * ��������\n"
22358                " * ������-�� */",
22359                "/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12));
22360 }
22361 
22362 #endif // _MSC_VER
22363 
22364 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
22365   FormatStyle Style = getLLVMStyle();
22366 
22367   Style.ConstructorInitializerIndentWidth = 4;
22368   verifyFormat(
22369       "SomeClass::Constructor()\n"
22370       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
22371       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
22372       Style);
22373 
22374   Style.ConstructorInitializerIndentWidth = 2;
22375   verifyFormat(
22376       "SomeClass::Constructor()\n"
22377       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
22378       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
22379       Style);
22380 
22381   Style.ConstructorInitializerIndentWidth = 0;
22382   verifyFormat(
22383       "SomeClass::Constructor()\n"
22384       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
22385       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
22386       Style);
22387   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
22388   verifyFormat(
22389       "SomeLongTemplateVariableName<\n"
22390       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
22391       Style);
22392   verifyFormat("bool smaller = 1 < "
22393                "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
22394                "                       "
22395                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
22396                Style);
22397 
22398   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
22399   verifyFormat("SomeClass::Constructor() :\n"
22400                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
22401                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
22402                Style);
22403 }
22404 
22405 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
22406   FormatStyle Style = getLLVMStyle();
22407   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
22408   Style.ConstructorInitializerIndentWidth = 4;
22409   verifyFormat("SomeClass::Constructor()\n"
22410                "    : a(a)\n"
22411                "    , b(b)\n"
22412                "    , c(c) {}",
22413                Style);
22414   verifyFormat("SomeClass::Constructor()\n"
22415                "    : a(a) {}",
22416                Style);
22417 
22418   Style.ColumnLimit = 0;
22419   verifyFormat("SomeClass::Constructor()\n"
22420                "    : a(a) {}",
22421                Style);
22422   verifyFormat("SomeClass::Constructor() noexcept\n"
22423                "    : a(a) {}",
22424                Style);
22425   verifyFormat("SomeClass::Constructor()\n"
22426                "    : a(a)\n"
22427                "    , b(b)\n"
22428                "    , c(c) {}",
22429                Style);
22430   verifyFormat("SomeClass::Constructor()\n"
22431                "    : a(a) {\n"
22432                "  foo();\n"
22433                "  bar();\n"
22434                "}",
22435                Style);
22436 
22437   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
22438   verifyFormat("SomeClass::Constructor()\n"
22439                "    : a(a)\n"
22440                "    , b(b)\n"
22441                "    , c(c) {\n}",
22442                Style);
22443   verifyFormat("SomeClass::Constructor()\n"
22444                "    : a(a) {\n}",
22445                Style);
22446 
22447   Style.ColumnLimit = 80;
22448   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
22449   Style.ConstructorInitializerIndentWidth = 2;
22450   verifyFormat("SomeClass::Constructor()\n"
22451                "  : a(a)\n"
22452                "  , b(b)\n"
22453                "  , c(c) {}",
22454                Style);
22455 
22456   Style.ConstructorInitializerIndentWidth = 0;
22457   verifyFormat("SomeClass::Constructor()\n"
22458                ": a(a)\n"
22459                ", b(b)\n"
22460                ", c(c) {}",
22461                Style);
22462 
22463   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
22464   Style.ConstructorInitializerIndentWidth = 4;
22465   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
22466   verifyFormat(
22467       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)",
22468       Style);
22469   verifyFormat(
22470       "SomeClass::Constructor()\n"
22471       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
22472       Style);
22473   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
22474   verifyFormat("SomeClass::Constructor()\n"
22475                "    : aaaaaaaa(aaaaaaaa) {}",
22476                Style);
22477   verifyFormat("SomeClass::Constructor()\n"
22478                "    : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)",
22479                Style);
22480   verifyFormat(
22481       "SomeClass::Constructor()\n"
22482       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
22483       Style);
22484 
22485   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
22486   Style.ConstructorInitializerIndentWidth = 4;
22487   Style.ColumnLimit = 60;
22488   verifyFormat("SomeClass::Constructor()\n"
22489                "    : aaaaaaaa(aaaaaaaa)\n"
22490                "    , aaaaaaaa(aaaaaaaa)\n"
22491                "    , aaaaaaaa(aaaaaaaa) {}",
22492                Style);
22493   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
22494   verifyFormat("SomeClass::Constructor()\n"
22495                "    : aaaaaaaa(aaaaaaaa)\n"
22496                "    , aaaaaaaa(aaaaaaaa)\n"
22497                "    , aaaaaaaa(aaaaaaaa) {}",
22498                Style);
22499 }
22500 
22501 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) {
22502   FormatStyle Style = getLLVMStyle();
22503   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
22504   Style.ConstructorInitializerIndentWidth = 4;
22505   verifyFormat("SomeClass::Constructor()\n"
22506                "    : a{a}\n"
22507                "    , b{b} {}",
22508                Style);
22509   verifyFormat("SomeClass::Constructor()\n"
22510                "    : a{a}\n"
22511                "#if CONDITION\n"
22512                "    , b{b}\n"
22513                "#endif\n"
22514                "{\n}",
22515                Style);
22516   Style.ConstructorInitializerIndentWidth = 2;
22517   verifyFormat("SomeClass::Constructor()\n"
22518                "#if CONDITION\n"
22519                "  : a{a}\n"
22520                "#endif\n"
22521                "  , b{b}\n"
22522                "  , c{c} {\n}",
22523                Style);
22524   Style.ConstructorInitializerIndentWidth = 0;
22525   verifyFormat("SomeClass::Constructor()\n"
22526                ": a{a}\n"
22527                "#ifdef CONDITION\n"
22528                ", b{b}\n"
22529                "#else\n"
22530                ", c{c}\n"
22531                "#endif\n"
22532                ", d{d} {\n}",
22533                Style);
22534   Style.ConstructorInitializerIndentWidth = 4;
22535   verifyFormat("SomeClass::Constructor()\n"
22536                "    : a{a}\n"
22537                "#if WINDOWS\n"
22538                "#if DEBUG\n"
22539                "    , b{0}\n"
22540                "#else\n"
22541                "    , b{1}\n"
22542                "#endif\n"
22543                "#else\n"
22544                "#if DEBUG\n"
22545                "    , b{2}\n"
22546                "#else\n"
22547                "    , b{3}\n"
22548                "#endif\n"
22549                "#endif\n"
22550                "{\n}",
22551                Style);
22552   verifyFormat("SomeClass::Constructor()\n"
22553                "    : a{a}\n"
22554                "#if WINDOWS\n"
22555                "    , b{0}\n"
22556                "#if DEBUG\n"
22557                "    , c{0}\n"
22558                "#else\n"
22559                "    , c{1}\n"
22560                "#endif\n"
22561                "#else\n"
22562                "#if DEBUG\n"
22563                "    , c{2}\n"
22564                "#else\n"
22565                "    , c{3}\n"
22566                "#endif\n"
22567                "    , b{1}\n"
22568                "#endif\n"
22569                "{\n}",
22570                Style);
22571 }
22572 
22573 TEST_F(FormatTest, Destructors) {
22574   verifyFormat("void F(int &i) { i.~int(); }");
22575   verifyFormat("void F(int &i) { i->~int(); }");
22576 }
22577 
22578 TEST_F(FormatTest, FormatsWithWebKitStyle) {
22579   FormatStyle Style = getWebKitStyle();
22580 
22581   // Don't indent in outer namespaces.
22582   verifyFormat("namespace outer {\n"
22583                "int i;\n"
22584                "namespace inner {\n"
22585                "    int i;\n"
22586                "} // namespace inner\n"
22587                "} // namespace outer\n"
22588                "namespace other_outer {\n"
22589                "int i;\n"
22590                "}",
22591                Style);
22592 
22593   // Don't indent case labels.
22594   verifyFormat("switch (variable) {\n"
22595                "case 1:\n"
22596                "case 2:\n"
22597                "    doSomething();\n"
22598                "    break;\n"
22599                "default:\n"
22600                "    ++variable;\n"
22601                "}",
22602                Style);
22603 
22604   // Wrap before binary operators.
22605   verifyFormat(
22606       "void f()\n"
22607       "{\n"
22608       "    if (aaaaaaaaaaaaaaaa\n"
22609       "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
22610       "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
22611       "        return;\n"
22612       "}",
22613       "void f() {\n"
22614       "if (aaaaaaaaaaaaaaaa\n"
22615       "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
22616       "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
22617       "return;\n"
22618       "}",
22619       Style);
22620 
22621   // Allow functions on a single line.
22622   verifyFormat("void f() { return; }", Style);
22623 
22624   // Allow empty blocks on a single line and insert a space in empty blocks.
22625   verifyFormat("void f() { }", "void f() {}", Style);
22626   verifyFormat("while (true) { }", "while (true) {}", Style);
22627   // However, don't merge non-empty short loops.
22628   verifyFormat("while (true) {\n"
22629                "    continue;\n"
22630                "}",
22631                "while (true) { continue; }", Style);
22632 
22633   // Constructor initializers are formatted one per line with the "," on the
22634   // new line.
22635   verifyFormat("Constructor()\n"
22636                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
22637                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
22638                "          aaaaaaaaaaaaaa)\n"
22639                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
22640                "{\n"
22641                "}",
22642                Style);
22643   verifyFormat("SomeClass::Constructor()\n"
22644                "    : a(a)\n"
22645                "{\n"
22646                "}",
22647                Style);
22648   verifyFormat("SomeClass::Constructor()\n"
22649                "    : a(a)\n"
22650                "{\n"
22651                "}",
22652                "SomeClass::Constructor():a(a){}", Style);
22653   verifyFormat("SomeClass::Constructor()\n"
22654                "    : a(a)\n"
22655                "    , b(b)\n"
22656                "    , c(c)\n"
22657                "{\n"
22658                "}",
22659                Style);
22660   verifyFormat("SomeClass::Constructor()\n"
22661                "    : a(a)\n"
22662                "{\n"
22663                "    foo();\n"
22664                "    bar();\n"
22665                "}",
22666                Style);
22667 
22668   // Access specifiers should be aligned left.
22669   verifyFormat("class C {\n"
22670                "public:\n"
22671                "    int i;\n"
22672                "};",
22673                Style);
22674 
22675   // Do not align comments.
22676   verifyFormat("int a; // Do not\n"
22677                "double b; // align comments.",
22678                Style);
22679 
22680   // Do not align operands.
22681   verifyFormat("ASSERT(aaaa\n"
22682                "    || bbbb);",
22683                "ASSERT ( aaaa\n||bbbb);", Style);
22684 
22685   // Accept input's line breaks.
22686   verifyFormat("if (aaaaaaaaaaaaaaa\n"
22687                "    || bbbbbbbbbbbbbbb) {\n"
22688                "    i++;\n"
22689                "}",
22690                "if (aaaaaaaaaaaaaaa\n"
22691                "|| bbbbbbbbbbbbbbb) { i++; }",
22692                Style);
22693   verifyFormat("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
22694                "    i++;\n"
22695                "}",
22696                "if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style);
22697 
22698   // Don't automatically break all macro definitions (llvm.org/PR17842).
22699   verifyFormat("#define aNumber 10", Style);
22700   // However, generally keep the line breaks that the user authored.
22701   verifyFormat("#define aNumber \\\n"
22702                "    10",
22703                "#define aNumber \\\n"
22704                " 10",
22705                Style);
22706 
22707   // Keep empty and one-element array literals on a single line.
22708   verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
22709                "                                  copyItems:YES];",
22710                "NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
22711                "copyItems:YES];",
22712                Style);
22713   verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
22714                "                                  copyItems:YES];",
22715                "NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
22716                "             copyItems:YES];",
22717                Style);
22718   // FIXME: This does not seem right, there should be more indentation before
22719   // the array literal's entries. Nested blocks have the same problem.
22720   verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
22721                "    @\"a\",\n"
22722                "    @\"a\"\n"
22723                "]\n"
22724                "                                  copyItems:YES];",
22725                "NSArray* a = [[NSArray alloc] initWithArray:@[\n"
22726                "     @\"a\",\n"
22727                "     @\"a\"\n"
22728                "     ]\n"
22729                "       copyItems:YES];",
22730                Style);
22731   verifyFormat(
22732       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
22733       "                                  copyItems:YES];",
22734       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
22735       "   copyItems:YES];",
22736       Style);
22737 
22738   verifyFormat("[self.a b:c c:d];", Style);
22739   verifyFormat("[self.a b:c\n"
22740                "        c:d];",
22741                "[self.a b:c\n"
22742                "c:d];",
22743                Style);
22744 }
22745 
22746 TEST_F(FormatTest, FormatsLambdas) {
22747   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();");
22748   verifyFormat(
22749       "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();");
22750   verifyFormat("int c = [&] { [=] { return b++; }(); }();");
22751   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();");
22752   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();");
22753   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}");
22754   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}");
22755   verifyFormat("auto c = [a = [b = 42] {}] {};");
22756   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};");
22757   verifyFormat("int x = f(*+[] {});");
22758   verifyFormat("void f() {\n"
22759                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
22760                "}");
22761   verifyFormat("void f() {\n"
22762                "  other(x.begin(), //\n"
22763                "        x.end(),   //\n"
22764                "        [&](int, int) { return 1; });\n"
22765                "}");
22766   verifyFormat("void f() {\n"
22767                "  other.other.other.other.other(\n"
22768                "      x.begin(), x.end(),\n"
22769                "      [something, rather](int, int, int, int, int, int, int) { "
22770                "return 1; });\n"
22771                "}");
22772   verifyFormat(
22773       "void f() {\n"
22774       "  other.other.other.other.other(\n"
22775       "      x.begin(), x.end(),\n"
22776       "      [something, rather](int, int, int, int, int, int, int) {\n"
22777       "        //\n"
22778       "      });\n"
22779       "}");
22780   verifyFormat("SomeFunction([]() { // A cool function...\n"
22781                "  return 43;\n"
22782                "});");
22783   verifyFormat("SomeFunction([]() {\n"
22784                "#define A a\n"
22785                "  return 43;\n"
22786                "});",
22787                "SomeFunction([](){\n"
22788                "#define A a\n"
22789                "return 43;\n"
22790                "});");
22791   verifyFormat("void f() {\n"
22792                "  SomeFunction([](decltype(x), A *a) {});\n"
22793                "  SomeFunction([](typeof(x), A *a) {});\n"
22794                "  SomeFunction([](_Atomic(x), A *a) {});\n"
22795                "  SomeFunction([](__underlying_type(x), A *a) {});\n"
22796                "}");
22797   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22798                "    [](const aaaaaaaaaa &a) { return a; });");
22799   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
22800                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
22801                "});");
22802   verifyFormat("Constructor()\n"
22803                "    : Field([] { // comment\n"
22804                "        int i;\n"
22805                "      }) {}");
22806   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
22807                "  return some_parameter.size();\n"
22808                "};");
22809   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
22810                "    [](const string &s) { return s; };");
22811   verifyFormat("int i = aaaaaa ? 1 //\n"
22812                "               : [] {\n"
22813                "                   return 2; //\n"
22814                "                 }();");
22815   verifyFormat("llvm::errs() << \"number of twos is \"\n"
22816                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
22817                "                  return x == 2; // force break\n"
22818                "                });");
22819   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22820                "    [=](int iiiiiiiiiiii) {\n"
22821                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
22822                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
22823                "    });",
22824                getLLVMStyleWithColumns(60));
22825 
22826   verifyFormat("SomeFunction({[&] {\n"
22827                "                // comment\n"
22828                "              },\n"
22829                "              [&] {\n"
22830                "                // comment\n"
22831                "              }});");
22832   verifyFormat("SomeFunction({[&] {\n"
22833                "  // comment\n"
22834                "}});");
22835   verifyFormat(
22836       "virtual aaaaaaaaaaaaaaaa(\n"
22837       "    std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
22838       "    aaaaa aaaaaaaaa);");
22839 
22840   // Lambdas with return types.
22841   verifyFormat("int c = []() -> int { return 2; }();");
22842   verifyFormat("int c = []() -> int * { return 2; }();");
22843   verifyFormat("int c = []() -> vector<int> { return {2}; }();");
22844   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
22845   verifyFormat("foo([]() noexcept -> int {});");
22846   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
22847   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
22848   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
22849   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
22850   verifyFormat("[a, a]() -> a<1> {};");
22851   verifyFormat("[]() -> foo<5 + 2> { return {}; };");
22852   verifyFormat("[]() -> foo<5 - 2> { return {}; };");
22853   verifyFormat("[]() -> foo<5 / 2> { return {}; };");
22854   verifyFormat("[]() -> foo<5 * 2> { return {}; };");
22855   verifyFormat("[]() -> foo<5 % 2> { return {}; };");
22856   verifyFormat("[]() -> foo<5 << 2> { return {}; };");
22857   verifyFormat("[]() -> foo<!5> { return {}; };");
22858   verifyFormat("[]() -> foo<~5> { return {}; };");
22859   verifyFormat("[]() -> foo<5 | 2> { return {}; };");
22860   verifyFormat("[]() -> foo<5 || 2> { return {}; };");
22861   verifyFormat("[]() -> foo<5 & 2> { return {}; };");
22862   verifyFormat("[]() -> foo<5 && 2> { return {}; };");
22863   verifyFormat("[]() -> foo<5 == 2> { return {}; };");
22864   verifyFormat("[]() -> foo<5 != 2> { return {}; };");
22865   verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
22866   verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
22867   verifyFormat("[]() -> foo<5 < 2> { return {}; };");
22868   verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
22869   verifyFormat("namespace bar {\n"
22870                "// broken:\n"
22871                "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
22872                "} // namespace bar");
22873   verifyFormat("namespace bar {\n"
22874                "// broken:\n"
22875                "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
22876                "} // namespace bar");
22877   verifyFormat("namespace bar {\n"
22878                "// broken:\n"
22879                "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
22880                "} // namespace bar");
22881   verifyFormat("namespace bar {\n"
22882                "// broken:\n"
22883                "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
22884                "} // namespace bar");
22885   verifyFormat("namespace bar {\n"
22886                "// broken:\n"
22887                "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
22888                "} // namespace bar");
22889   verifyFormat("namespace bar {\n"
22890                "// broken:\n"
22891                "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
22892                "} // namespace bar");
22893   verifyFormat("namespace bar {\n"
22894                "// broken:\n"
22895                "auto foo{[]() -> foo<!5> { return {}; }};\n"
22896                "} // namespace bar");
22897   verifyFormat("namespace bar {\n"
22898                "// broken:\n"
22899                "auto foo{[]() -> foo<~5> { return {}; }};\n"
22900                "} // namespace bar");
22901   verifyFormat("namespace bar {\n"
22902                "// broken:\n"
22903                "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
22904                "} // namespace bar");
22905   verifyFormat("namespace bar {\n"
22906                "// broken:\n"
22907                "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
22908                "} // namespace bar");
22909   verifyFormat("namespace bar {\n"
22910                "// broken:\n"
22911                "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
22912                "} // namespace bar");
22913   verifyFormat("namespace bar {\n"
22914                "// broken:\n"
22915                "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
22916                "} // namespace bar");
22917   verifyFormat("namespace bar {\n"
22918                "// broken:\n"
22919                "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
22920                "} // namespace bar");
22921   verifyFormat("namespace bar {\n"
22922                "// broken:\n"
22923                "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
22924                "} // namespace bar");
22925   verifyFormat("namespace bar {\n"
22926                "// broken:\n"
22927                "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
22928                "} // namespace bar");
22929   verifyFormat("namespace bar {\n"
22930                "// broken:\n"
22931                "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
22932                "} // namespace bar");
22933   verifyFormat("namespace bar {\n"
22934                "// broken:\n"
22935                "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
22936                "} // namespace bar");
22937   verifyFormat("namespace bar {\n"
22938                "// broken:\n"
22939                "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
22940                "} // namespace bar");
22941   verifyFormat("[]() -> a<1> {};");
22942   verifyFormat("[]() -> a<1> { ; };");
22943   verifyFormat("[]() -> a<1> { ; }();");
22944   verifyFormat("[a, a]() -> a<true> {};");
22945   verifyFormat("[]() -> a<true> {};");
22946   verifyFormat("[]() -> a<true> { ; };");
22947   verifyFormat("[]() -> a<true> { ; }();");
22948   verifyFormat("[a, a]() -> a<false> {};");
22949   verifyFormat("[]() -> a<false> {};");
22950   verifyFormat("[]() -> a<false> { ; };");
22951   verifyFormat("[]() -> a<false> { ; }();");
22952   verifyFormat("auto foo{[]() -> foo<false> { ; }};");
22953   verifyFormat("namespace bar {\n"
22954                "auto foo{[]() -> foo<false> { ; }};\n"
22955                "} // namespace bar");
22956   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
22957                "                   int j) -> int {\n"
22958                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
22959                "};");
22960   verifyFormat(
22961       "aaaaaaaaaaaaaaaaaaaaaa(\n"
22962       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
22963       "      return aaaaaaaaaaaaaaaaa;\n"
22964       "    });",
22965       getLLVMStyleWithColumns(70));
22966   verifyFormat("[]() //\n"
22967                "    -> int {\n"
22968                "  return 1; //\n"
22969                "};");
22970   verifyFormat("[]() -> Void<T...> {};");
22971   verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
22972   verifyFormat("SomeFunction({[]() -> int[] { return {}; }});");
22973   verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});");
22974   verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});");
22975   verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});");
22976   verifyFormat("foo([&](u32 bar) __attribute__((always_inline)) -> void {});");
22977   verifyFormat("return int{[x = x]() { return x; }()};");
22978 
22979   // Lambdas with explicit template argument lists.
22980   verifyFormat(
22981       "auto L = []<template <typename> class T, class U>(T<U> &&a) {};");
22982   verifyFormat("auto L = []<class T>(T) {\n"
22983                "  {\n"
22984                "    f();\n"
22985                "    g();\n"
22986                "  }\n"
22987                "};");
22988   verifyFormat("auto L = []<class... T>(T...) {\n"
22989                "  {\n"
22990                "    f();\n"
22991                "    g();\n"
22992                "  }\n"
22993                "};");
22994   verifyFormat("auto L = []<typename... T>(T...) {\n"
22995                "  {\n"
22996                "    f();\n"
22997                "    g();\n"
22998                "  }\n"
22999                "};");
23000   verifyFormat("auto L = []<template <typename...> class T>(T...) {\n"
23001                "  {\n"
23002                "    f();\n"
23003                "    g();\n"
23004                "  }\n"
23005                "};");
23006   verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n"
23007                "  {\n"
23008                "    f();\n"
23009                "    g();\n"
23010                "  }\n"
23011                "};");
23012   verifyFormat("auto L = []<int... T>(T...) {\n"
23013                "  {\n"
23014                "    f();\n"
23015                "    g();\n"
23016                "  }\n"
23017                "};");
23018   verifyFormat("auto L = []<Foo... T>(T...) {\n"
23019                "  {\n"
23020                "    f();\n"
23021                "    g();\n"
23022                "  }\n"
23023                "};");
23024 
23025   // Lambdas that fit on a single line within an argument list are not forced
23026   // onto new lines.
23027   verifyFormat("SomeFunction([] {});");
23028   verifyFormat("SomeFunction(0, [] {});");
23029   verifyFormat("SomeFunction([] {}, 0);");
23030   verifyFormat("SomeFunction(0, [] {}, 0);");
23031   verifyFormat("SomeFunction([] { return 0; }, 0);");
23032   verifyFormat("SomeFunction(a, [] { return 0; }, b);");
23033   verifyFormat("SomeFunction([] { return 0; }, [] { return 0; });");
23034   verifyFormat("SomeFunction([] { return 0; }, [] { return 0; }, b);");
23035   verifyFormat("auto loooooooooooooooooooooooooooong =\n"
23036                "    SomeFunction([] { return 0; }, [] { return 0; }, b);");
23037   // Exceeded column limit. We need to break.
23038   verifyFormat("auto loooooooooooooooooooooooooooongName = SomeFunction(\n"
23039                "    [] { return anotherLooooooooooonoooooooongName; }, [] { "
23040                "return 0; }, b);");
23041 
23042   // Multiple multi-line lambdas in the same parentheses change indentation
23043   // rules. These lambdas are always forced to start on new lines.
23044   verifyFormat("SomeFunction(\n"
23045                "    []() {\n"
23046                "      //\n"
23047                "    },\n"
23048                "    []() {\n"
23049                "      //\n"
23050                "    });");
23051 
23052   // A multi-line lambda passed as arg0 is always pushed to the next line.
23053   verifyFormat("SomeFunction(\n"
23054                "    [this] {\n"
23055                "      //\n"
23056                "    },\n"
23057                "    1);");
23058 
23059   // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
23060   // the arg0 case above.
23061   auto Style = getGoogleStyle();
23062   Style.BinPackArguments = false;
23063   verifyFormat("SomeFunction(\n"
23064                "    a,\n"
23065                "    [this] {\n"
23066                "      //\n"
23067                "    },\n"
23068                "    b);",
23069                Style);
23070   verifyFormat("SomeFunction(\n"
23071                "    a,\n"
23072                "    [this] {\n"
23073                "      //\n"
23074                "    },\n"
23075                "    b);");
23076 
23077   // A lambda with a very long line forces arg0 to be pushed out irrespective of
23078   // the BinPackArguments value (as long as the code is wide enough).
23079   verifyFormat(
23080       "something->SomeFunction(\n"
23081       "    a,\n"
23082       "    [this] {\n"
23083       "      "
23084       "D0000000000000000000000000000000000000000000000000000000000001();\n"
23085       "    },\n"
23086       "    b);");
23087 
23088   // A multi-line lambda is pulled up as long as the introducer fits on the
23089   // previous line and there are no further args.
23090   verifyFormat("function(1, [this, that] {\n"
23091                "  //\n"
23092                "});");
23093   verifyFormat("function([this, that] {\n"
23094                "  //\n"
23095                "});");
23096   // FIXME: this format is not ideal and we should consider forcing the first
23097   // arg onto its own line.
23098   verifyFormat("function(a, b, c, //\n"
23099                "         d, [this, that] {\n"
23100                "           //\n"
23101                "         });");
23102 
23103   // Multiple lambdas are treated correctly even when there is a short arg0.
23104   verifyFormat("SomeFunction(\n"
23105                "    1,\n"
23106                "    [this] {\n"
23107                "      //\n"
23108                "    },\n"
23109                "    [this] {\n"
23110                "      //\n"
23111                "    },\n"
23112                "    1);");
23113 
23114   // More complex introducers.
23115   verifyFormat("return [i, args...] {};");
23116 
23117   // Not lambdas.
23118   verifyFormat("constexpr char hello[]{\"hello\"};");
23119   verifyFormat("double &operator[](int i) { return 0; }\n"
23120                "int i;");
23121   verifyFormat("std::unique_ptr<int[]> foo() {}");
23122   verifyFormat("int i = a[a][a]->f();");
23123   verifyFormat("int i = (*b)[a]->f();");
23124 
23125   // Other corner cases.
23126   verifyFormat("void f() {\n"
23127                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
23128                "  );\n"
23129                "}");
23130   verifyFormat("auto k = *[](int *j) { return j; }(&i);");
23131 
23132   // Lambdas created through weird macros.
23133   verifyFormat("void f() {\n"
23134                "  MACRO((const AA &a) { return 1; });\n"
23135                "  MACRO((AA &a) { return 1; });\n"
23136                "}");
23137 
23138   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
23139                "      doo_dah();\n"
23140                "      doo_dah();\n"
23141                "    })) {\n"
23142                "}");
23143   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
23144                "                doo_dah();\n"
23145                "                doo_dah();\n"
23146                "              })) {\n"
23147                "}");
23148   verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
23149                "                doo_dah();\n"
23150                "                doo_dah();\n"
23151                "              })) {\n"
23152                "}");
23153   verifyFormat("auto lambda = []() {\n"
23154                "  int a = 2\n"
23155                "#if A\n"
23156                "          + 2\n"
23157                "#endif\n"
23158                "      ;\n"
23159                "};");
23160 
23161   // Lambdas with complex multiline introducers.
23162   verifyFormat(
23163       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
23164       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
23165       "        -> ::std::unordered_set<\n"
23166       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
23167       "      //\n"
23168       "    });");
23169 
23170   FormatStyle LLVMStyle = getLLVMStyleWithColumns(60);
23171   verifyFormat("very_long_function_name_yes_it_is_really_long(\n"
23172                "    [](auto n) noexcept [[back_attr]]\n"
23173                "        -> std::unordered_map<very_long_type_name_A,\n"
23174                "                              very_long_type_name_B> {\n"
23175                "      really_do_something();\n"
23176                "    });",
23177                LLVMStyle);
23178   verifyFormat("very_long_function_name_yes_it_is_really_long(\n"
23179                "    [](auto n) constexpr\n"
23180                "        -> std::unordered_map<very_long_type_name_A,\n"
23181                "                              very_long_type_name_B> {\n"
23182                "      really_do_something();\n"
23183                "    });",
23184                LLVMStyle);
23185 
23186   FormatStyle DoNotMerge = getLLVMStyle();
23187   DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
23188   verifyFormat("auto c = []() {\n"
23189                "  return b;\n"
23190                "};",
23191                "auto c = []() { return b; };", DoNotMerge);
23192   verifyFormat("auto c = []() {\n"
23193                "};",
23194                " auto c = []() {};", DoNotMerge);
23195 
23196   FormatStyle MergeEmptyOnly = getLLVMStyle();
23197   MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
23198   verifyFormat("auto c = []() {\n"
23199                "  return b;\n"
23200                "};",
23201                "auto c = []() {\n"
23202                "  return b;\n"
23203                " };",
23204                MergeEmptyOnly);
23205   verifyFormat("auto c = []() {};",
23206                "auto c = []() {\n"
23207                "};",
23208                MergeEmptyOnly);
23209 
23210   FormatStyle MergeInline = getLLVMStyle();
23211   MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
23212   verifyFormat("auto c = []() {\n"
23213                "  return b;\n"
23214                "};",
23215                "auto c = []() { return b; };", MergeInline);
23216   verifyFormat("function([]() { return b; })", MergeInline);
23217   verifyFormat("function([]() { return b; }, a)", MergeInline);
23218   verifyFormat("function(a, []() { return b; })", MergeInline);
23219 
23220   // Check option "BraceWrapping.BeforeLambdaBody" and different state of
23221   // AllowShortLambdasOnASingleLine
23222   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
23223   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
23224   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
23225   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23226       FormatStyle::ShortLambdaStyle::SLS_None;
23227   verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
23228                "    []()\n"
23229                "    {\n"
23230                "      return 17;\n"
23231                "    });",
23232                LLVMWithBeforeLambdaBody);
23233   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
23234                "    []()\n"
23235                "    {\n"
23236                "    });",
23237                LLVMWithBeforeLambdaBody);
23238   verifyFormat("auto fct_SLS_None = []()\n"
23239                "{\n"
23240                "  return 17;\n"
23241                "};",
23242                LLVMWithBeforeLambdaBody);
23243   verifyFormat("TwoNestedLambdas_SLS_None(\n"
23244                "    []()\n"
23245                "    {\n"
23246                "      return Call(\n"
23247                "          []()\n"
23248                "          {\n"
23249                "            return 17;\n"
23250                "          });\n"
23251                "    });",
23252                LLVMWithBeforeLambdaBody);
23253   verifyFormat("void Fct() {\n"
23254                "  return {[]()\n"
23255                "          {\n"
23256                "            return 17;\n"
23257                "          }};\n"
23258                "}",
23259                LLVMWithBeforeLambdaBody);
23260 
23261   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23262       FormatStyle::ShortLambdaStyle::SLS_Empty;
23263   verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
23264                "    []()\n"
23265                "    {\n"
23266                "      return 17;\n"
23267                "    });",
23268                LLVMWithBeforeLambdaBody);
23269   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
23270                LLVMWithBeforeLambdaBody);
23271   verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
23272                "ongFunctionName_SLS_Empty(\n"
23273                "    []() {});",
23274                LLVMWithBeforeLambdaBody);
23275   verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
23276                "                                []()\n"
23277                "                                {\n"
23278                "                                  return 17;\n"
23279                "                                });",
23280                LLVMWithBeforeLambdaBody);
23281   verifyFormat("auto fct_SLS_Empty = []()\n"
23282                "{\n"
23283                "  return 17;\n"
23284                "};",
23285                LLVMWithBeforeLambdaBody);
23286   verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
23287                "    []()\n"
23288                "    {\n"
23289                "      return Call([]() {});\n"
23290                "    });",
23291                LLVMWithBeforeLambdaBody);
23292   verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
23293                "                           []()\n"
23294                "                           {\n"
23295                "                             return Call([]() {});\n"
23296                "                           });",
23297                LLVMWithBeforeLambdaBody);
23298   verifyFormat(
23299       "FctWithLongLineInLambda_SLS_Empty(\n"
23300       "    []()\n"
23301       "    {\n"
23302       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23303       "                               AndShouldNotBeConsiderAsInline,\n"
23304       "                               LambdaBodyMustBeBreak);\n"
23305       "    });",
23306       LLVMWithBeforeLambdaBody);
23307 
23308   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23309       FormatStyle::ShortLambdaStyle::SLS_Inline;
23310   verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
23311                LLVMWithBeforeLambdaBody);
23312   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
23313                LLVMWithBeforeLambdaBody);
23314   verifyFormat("auto fct_SLS_Inline = []()\n"
23315                "{\n"
23316                "  return 17;\n"
23317                "};",
23318                LLVMWithBeforeLambdaBody);
23319   verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
23320                "17; }); });",
23321                LLVMWithBeforeLambdaBody);
23322   verifyFormat(
23323       "FctWithLongLineInLambda_SLS_Inline(\n"
23324       "    []()\n"
23325       "    {\n"
23326       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23327       "                               AndShouldNotBeConsiderAsInline,\n"
23328       "                               LambdaBodyMustBeBreak);\n"
23329       "    });",
23330       LLVMWithBeforeLambdaBody);
23331   verifyFormat("FctWithMultipleParams_SLS_Inline("
23332                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
23333                "                                 []() { return 17; });",
23334                LLVMWithBeforeLambdaBody);
23335   verifyFormat(
23336       "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
23337       LLVMWithBeforeLambdaBody);
23338 
23339   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23340       FormatStyle::ShortLambdaStyle::SLS_All;
23341   verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
23342                LLVMWithBeforeLambdaBody);
23343   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
23344                LLVMWithBeforeLambdaBody);
23345   verifyFormat("auto fct_SLS_All = []() { return 17; };",
23346                LLVMWithBeforeLambdaBody);
23347   verifyFormat("FctWithOneParam_SLS_All(\n"
23348                "    []()\n"
23349                "    {\n"
23350                "      // A cool function...\n"
23351                "      return 43;\n"
23352                "    });",
23353                LLVMWithBeforeLambdaBody);
23354   verifyFormat("FctWithMultipleParams_SLS_All("
23355                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
23356                "                              []() { return 17; });",
23357                LLVMWithBeforeLambdaBody);
23358   verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
23359                LLVMWithBeforeLambdaBody);
23360   verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
23361                LLVMWithBeforeLambdaBody);
23362   verifyFormat(
23363       "FctWithLongLineInLambda_SLS_All(\n"
23364       "    []()\n"
23365       "    {\n"
23366       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23367       "                               AndShouldNotBeConsiderAsInline,\n"
23368       "                               LambdaBodyMustBeBreak);\n"
23369       "    });",
23370       LLVMWithBeforeLambdaBody);
23371   verifyFormat(
23372       "auto fct_SLS_All = []()\n"
23373       "{\n"
23374       "  return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23375       "                           AndShouldNotBeConsiderAsInline,\n"
23376       "                           LambdaBodyMustBeBreak);\n"
23377       "};",
23378       LLVMWithBeforeLambdaBody);
23379   LLVMWithBeforeLambdaBody.BinPackParameters = FormatStyle::BPPS_OnePerLine;
23380   verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
23381                LLVMWithBeforeLambdaBody);
23382   verifyFormat(
23383       "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
23384       "                                FirstParam,\n"
23385       "                                SecondParam,\n"
23386       "                                ThirdParam,\n"
23387       "                                FourthParam);",
23388       LLVMWithBeforeLambdaBody);
23389   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
23390                "    []() { return "
23391                "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
23392                "    FirstParam,\n"
23393                "    SecondParam,\n"
23394                "    ThirdParam,\n"
23395                "    FourthParam);",
23396                LLVMWithBeforeLambdaBody);
23397   verifyFormat(
23398       "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
23399       "                                SecondParam,\n"
23400       "                                ThirdParam,\n"
23401       "                                FourthParam,\n"
23402       "                                []() { return SomeValueNotSoLong; });",
23403       LLVMWithBeforeLambdaBody);
23404   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
23405                "    []()\n"
23406                "    {\n"
23407                "      return "
23408                "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
23409                "eConsiderAsInline;\n"
23410                "    });",
23411                LLVMWithBeforeLambdaBody);
23412   verifyFormat(
23413       "FctWithLongLineInLambda_SLS_All(\n"
23414       "    []()\n"
23415       "    {\n"
23416       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23417       "                               AndShouldNotBeConsiderAsInline,\n"
23418       "                               LambdaBodyMustBeBreak);\n"
23419       "    });",
23420       LLVMWithBeforeLambdaBody);
23421   verifyFormat("FctWithTwoParams_SLS_All(\n"
23422                "    []()\n"
23423                "    {\n"
23424                "      // A cool function...\n"
23425                "      return 43;\n"
23426                "    },\n"
23427                "    87);",
23428                LLVMWithBeforeLambdaBody);
23429   verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
23430                LLVMWithBeforeLambdaBody);
23431   verifyFormat(
23432       "FctWithTwoParams_SLS_All(\n"
23433       "    87, []() { return LongLineThatWillForceBothParamsToNewLine(); });",
23434       LLVMWithBeforeLambdaBody);
23435   verifyFormat(
23436       "FctWithTwoParams_SLS_All(\n"
23437       "    87,\n"
23438       "    []()\n"
23439       "    {\n"
23440       "      return "
23441       "LongLineThatWillForceTheLambdaBodyToBeBrokenIntoMultipleLines();\n"
23442       "    });",
23443       LLVMWithBeforeLambdaBody);
23444   verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
23445                LLVMWithBeforeLambdaBody);
23446   verifyFormat(
23447       "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
23448       LLVMWithBeforeLambdaBody);
23449   verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
23450                "}); }, x);",
23451                LLVMWithBeforeLambdaBody);
23452   verifyFormat("TwoNestedLambdas_SLS_All(\n"
23453                "    []()\n"
23454                "    {\n"
23455                "      // A cool function...\n"
23456                "      return Call([]() { return 17; });\n"
23457                "    });",
23458                LLVMWithBeforeLambdaBody);
23459   verifyFormat("TwoNestedLambdas_SLS_All(\n"
23460                "    []()\n"
23461                "    {\n"
23462                "      return Call(\n"
23463                "          []()\n"
23464                "          {\n"
23465                "            // A cool function...\n"
23466                "            return 17;\n"
23467                "          });\n"
23468                "    });",
23469                LLVMWithBeforeLambdaBody);
23470 
23471   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23472       FormatStyle::ShortLambdaStyle::SLS_None;
23473 
23474   verifyFormat("auto select = [this]() -> const Library::Object *\n"
23475                "{\n"
23476                "  return MyAssignment::SelectFromList(this);\n"
23477                "};",
23478                LLVMWithBeforeLambdaBody);
23479 
23480   verifyFormat("auto select = [this]() -> const Library::Object &\n"
23481                "{\n"
23482                "  return MyAssignment::SelectFromList(this);\n"
23483                "};",
23484                LLVMWithBeforeLambdaBody);
23485 
23486   verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
23487                "{\n"
23488                "  return MyAssignment::SelectFromList(this);\n"
23489                "};",
23490                LLVMWithBeforeLambdaBody);
23491 
23492   verifyFormat("namespace test {\n"
23493                "class Test {\n"
23494                "public:\n"
23495                "  Test() = default;\n"
23496                "};\n"
23497                "} // namespace test",
23498                LLVMWithBeforeLambdaBody);
23499 
23500   // Lambdas with different indentation styles.
23501   Style = getLLVMStyleWithColumns(60);
23502   verifyFormat("Result doSomething(Promise promise) {\n"
23503                "  return promise.then(\n"
23504                "      [this, obj = std::move(s)](int bar) mutable {\n"
23505                "        return someObject.startAsyncAction().then(\n"
23506                "            [this, &obj](Result result) mutable {\n"
23507                "              result.processMore();\n"
23508                "            });\n"
23509                "      });\n"
23510                "}",
23511                Style);
23512   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
23513   verifyFormat("Result doSomething(Promise promise) {\n"
23514                "  return promise.then(\n"
23515                "      [this, obj = std::move(s)](int bar) mutable {\n"
23516                "    return obj.startAsyncAction().then(\n"
23517                "        [this, &obj](Result result) mutable {\n"
23518                "      result.processMore();\n"
23519                "    });\n"
23520                "  });\n"
23521                "}",
23522                Style);
23523   verifyFormat("Result doSomething(Promise promise) {\n"
23524                "  return promise.then([this, obj = std::move(s)] {\n"
23525                "    return obj.startAsyncAction().then(\n"
23526                "        [this, &obj](Result result) mutable {\n"
23527                "      result.processMore();\n"
23528                "    });\n"
23529                "  });\n"
23530                "}",
23531                Style);
23532   verifyFormat("void test() {\n"
23533                "  ([]() -> auto {\n"
23534                "    int b = 32;\n"
23535                "    return 3;\n"
23536                "  }).foo();\n"
23537                "}",
23538                Style);
23539   verifyFormat("void test() {\n"
23540                "  []() -> auto {\n"
23541                "    int b = 32;\n"
23542                "    return 3;\n"
23543                "  }\n"
23544                "}",
23545                Style);
23546   verifyFormat("void test() {\n"
23547                "  std::sort(v.begin(), v.end(),\n"
23548                "            [](const auto &foo, const auto &bar) {\n"
23549                "    return foo.baz < bar.baz;\n"
23550                "  });\n"
23551                "};",
23552                Style);
23553   verifyFormat("void test() {\n"
23554                "  (\n"
23555                "      []() -> auto {\n"
23556                "    int b = 32;\n"
23557                "    return 3;\n"
23558                "  }, foo, bar)\n"
23559                "      .foo();\n"
23560                "}",
23561                Style);
23562   verifyFormat("void test() {\n"
23563                "  ([]() -> auto {\n"
23564                "    int b = 32;\n"
23565                "    return 3;\n"
23566                "  })\n"
23567                "      .foo()\n"
23568                "      .bar();\n"
23569                "}",
23570                Style);
23571   verifyFormat("#define A                                                  \\\n"
23572                "  [] {                                                     \\\n"
23573                "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(                   \\\n"
23574                "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx);            \\\n"
23575                "  }",
23576                Style);
23577   verifyFormat("#define SORT(v)                                            \\\n"
23578                "  std::sort(v.begin(), v.end(),                            \\\n"
23579                "            [](const auto &foo, const auto &bar) {         \\\n"
23580                "    return foo.baz < bar.baz;                              \\\n"
23581                "  });",
23582                Style);
23583   verifyFormat("void foo() {\n"
23584                "  aFunction(1, b(c(foo, bar, baz, [](d) {\n"
23585                "    auto f = e(d);\n"
23586                "    return f;\n"
23587                "  })));\n"
23588                "}",
23589                Style);
23590   verifyFormat("void foo() {\n"
23591                "  aFunction(1, b(c(foo, Bar{}, baz, [](d) -> Foo {\n"
23592                "    auto f = e(foo, [&] {\n"
23593                "      auto g = h();\n"
23594                "      return g;\n"
23595                "    }, qux, [&] -> Bar {\n"
23596                "      auto i = j();\n"
23597                "      return i;\n"
23598                "    });\n"
23599                "    return f;\n"
23600                "  })));\n"
23601                "}",
23602                Style);
23603   verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
23604                "                    AnotherLongClassName baz)\n"
23605                "    : baz{baz}, func{[&] {\n"
23606                "        auto qux = bar;\n"
23607                "        return aFunkyFunctionCall(qux);\n"
23608                "      }} {}",
23609                Style);
23610   verifyFormat("void foo() {\n"
23611                "  class Foo {\n"
23612                "  public:\n"
23613                "    Foo()\n"
23614                "        : qux{[](int quux) {\n"
23615                "            auto tmp = quux;\n"
23616                "            return tmp;\n"
23617                "          }} {}\n"
23618                "\n"
23619                "  private:\n"
23620                "    std::function<void(int quux)> qux;\n"
23621                "  };\n"
23622                "}",
23623                Style);
23624   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
23625   verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
23626                "                    AnotherLongClassName baz) :\n"
23627                "    baz{baz}, func{[&] {\n"
23628                "      auto qux = bar;\n"
23629                "      return aFunkyFunctionCall(qux);\n"
23630                "    }} {}",
23631                Style);
23632   Style.PackConstructorInitializers = FormatStyle::PCIS_Never;
23633   verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
23634                "                    AnotherLongClassName baz) :\n"
23635                "    baz{baz},\n"
23636                "    func{[&] {\n"
23637                "      auto qux = bar;\n"
23638                "      return aFunkyFunctionCall(qux);\n"
23639                "    }} {}",
23640                Style);
23641   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
23642   // FIXME: The following test should pass, but fails at the time of writing.
23643 #if 0
23644   // As long as all the non-lambda arguments fit on a single line, AlwaysBreak
23645   // doesn't force an initial line break, even if lambdas span multiple lines.
23646   verifyFormat("void foo() {\n"
23647                "  aFunction(\n"
23648                "      [](d) -> Foo {\n"
23649                "    auto f = e(d);\n"
23650                "    return f;\n"
23651                "  }, foo, Bar{}, [] {\n"
23652                "    auto g = h();\n"
23653                "    return g;\n"
23654                "  }, baz);\n"
23655                "}",
23656                Style);
23657 #endif
23658   // A long non-lambda argument forces arguments to span multiple lines and thus
23659   // forces an initial line break when using AlwaysBreak.
23660   verifyFormat("void foo() {\n"
23661                "  aFunction(\n"
23662                "      1,\n"
23663                "      [](d) -> Foo {\n"
23664                "    auto f = e(d);\n"
23665                "    return f;\n"
23666                "  }, foo, Bar{},\n"
23667                "      [] {\n"
23668                "    auto g = h();\n"
23669                "    return g;\n"
23670                "  }, bazzzzz,\n"
23671                "      quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n"
23672                "}",
23673                Style);
23674   Style.BinPackArguments = false;
23675   verifyFormat("void foo() {\n"
23676                "  aFunction(\n"
23677                "      1,\n"
23678                "      [](d) -> Foo {\n"
23679                "    auto f = e(d);\n"
23680                "    return f;\n"
23681                "  },\n"
23682                "      foo,\n"
23683                "      Bar{},\n"
23684                "      [] {\n"
23685                "    auto g = h();\n"
23686                "    return g;\n"
23687                "  },\n"
23688                "      bazzzzz,\n"
23689                "      quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n"
23690                "}",
23691                Style);
23692   Style.BinPackArguments = true;
23693   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
23694   Style.BraceWrapping.BeforeLambdaBody = true;
23695   verifyFormat("void foo() {\n"
23696                "  aFunction(\n"
23697                "      1, b(c(foo, Bar{}, baz, [](d) -> Foo\n"
23698                "  {\n"
23699                "    auto f = e(\n"
23700                "        [&]\n"
23701                "    {\n"
23702                "      auto g = h();\n"
23703                "      return g;\n"
23704                "    }, qux, [&] -> Bar\n"
23705                "    {\n"
23706                "      auto i = j();\n"
23707                "      return i;\n"
23708                "    });\n"
23709                "    return f;\n"
23710                "  })));\n"
23711                "}",
23712                Style);
23713 }
23714 
23715 TEST_F(FormatTest, LambdaWithLineComments) {
23716   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
23717   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
23718   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
23719   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23720       FormatStyle::ShortLambdaStyle::SLS_All;
23721 
23722   verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
23723   verifyFormat("auto k = []() // comment\n"
23724                "{ return; }",
23725                LLVMWithBeforeLambdaBody);
23726   verifyFormat("auto k = []() /* comment */ { return; }",
23727                LLVMWithBeforeLambdaBody);
23728   verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
23729                LLVMWithBeforeLambdaBody);
23730   verifyFormat("auto k = []() // X\n"
23731                "{ return; }",
23732                LLVMWithBeforeLambdaBody);
23733   verifyFormat(
23734       "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
23735       "{ return; }",
23736       LLVMWithBeforeLambdaBody);
23737 
23738   LLVMWithBeforeLambdaBody.ColumnLimit = 0;
23739 
23740   verifyFormat("foo([]()\n"
23741                "    {\n"
23742                "      bar();    //\n"
23743                "      return 1; // comment\n"
23744                "    }());",
23745                "foo([]() {\n"
23746                "  bar(); //\n"
23747                "  return 1; // comment\n"
23748                "}());",
23749                LLVMWithBeforeLambdaBody);
23750   verifyFormat("foo(\n"
23751                "    1, MACRO {\n"
23752                "      baz();\n"
23753                "      bar(); // comment\n"
23754                "    },\n"
23755                "    []() {});",
23756                "foo(\n"
23757                "  1, MACRO { baz(); bar(); // comment\n"
23758                "  }, []() {}\n"
23759                ");",
23760                LLVMWithBeforeLambdaBody);
23761 }
23762 
23763 TEST_F(FormatTest, EmptyLinesInLambdas) {
23764   verifyFormat("auto lambda = []() {\n"
23765                "  x(); //\n"
23766                "};",
23767                "auto lambda = []() {\n"
23768                "\n"
23769                "  x(); //\n"
23770                "\n"
23771                "};");
23772 }
23773 
23774 TEST_F(FormatTest, FormatsBlocks) {
23775   FormatStyle ShortBlocks = getLLVMStyle();
23776   ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
23777   verifyFormat("int (^Block)(int, int);", ShortBlocks);
23778   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
23779   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
23780   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
23781   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
23782   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
23783 
23784   verifyFormat("foo(^{ bar(); });", ShortBlocks);
23785   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
23786   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
23787 
23788   verifyFormat("[operation setCompletionBlock:^{\n"
23789                "  [self onOperationDone];\n"
23790                "}];");
23791   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
23792                "  [self onOperationDone];\n"
23793                "}]};");
23794   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
23795                "  f();\n"
23796                "}];");
23797   verifyFormat("int a = [operation block:^int(int *i) {\n"
23798                "  return 1;\n"
23799                "}];");
23800   verifyFormat("[myObject doSomethingWith:arg1\n"
23801                "                      aaa:^int(int *a) {\n"
23802                "                        return 1;\n"
23803                "                      }\n"
23804                "                      bbb:f(a * bbbbbbbb)];");
23805 
23806   verifyFormat("[operation setCompletionBlock:^{\n"
23807                "  [self.delegate newDataAvailable];\n"
23808                "}];",
23809                getLLVMStyleWithColumns(60));
23810   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
23811                "  NSString *path = [self sessionFilePath];\n"
23812                "  if (path) {\n"
23813                "    // ...\n"
23814                "  }\n"
23815                "});");
23816   verifyFormat("[[SessionService sharedService]\n"
23817                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23818                "      if (window) {\n"
23819                "        [self windowDidLoad:window];\n"
23820                "      } else {\n"
23821                "        [self errorLoadingWindow];\n"
23822                "      }\n"
23823                "    }];");
23824   verifyFormat("void (^largeBlock)(void) = ^{\n"
23825                "  // ...\n"
23826                "};",
23827                getLLVMStyleWithColumns(40));
23828   verifyFormat("[[SessionService sharedService]\n"
23829                "    loadWindowWithCompletionBlock: //\n"
23830                "        ^(SessionWindow *window) {\n"
23831                "          if (window) {\n"
23832                "            [self windowDidLoad:window];\n"
23833                "          } else {\n"
23834                "            [self errorLoadingWindow];\n"
23835                "          }\n"
23836                "        }];",
23837                getLLVMStyleWithColumns(60));
23838   verifyFormat("[myObject doSomethingWith:arg1\n"
23839                "    firstBlock:^(Foo *a) {\n"
23840                "      // ...\n"
23841                "      int i;\n"
23842                "    }\n"
23843                "    secondBlock:^(Bar *b) {\n"
23844                "      // ...\n"
23845                "      int i;\n"
23846                "    }\n"
23847                "    thirdBlock:^Foo(Bar *b) {\n"
23848                "      // ...\n"
23849                "      int i;\n"
23850                "    }];");
23851   verifyFormat("[myObject doSomethingWith:arg1\n"
23852                "               firstBlock:-1\n"
23853                "              secondBlock:^(Bar *b) {\n"
23854                "                // ...\n"
23855                "                int i;\n"
23856                "              }];");
23857 
23858   verifyFormat("f(^{\n"
23859                "  @autoreleasepool {\n"
23860                "    if (a) {\n"
23861                "      g();\n"
23862                "    }\n"
23863                "  }\n"
23864                "});");
23865   verifyFormat("Block b = ^int *(A *a, B *b) {\n"
23866                "};");
23867   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
23868                "};");
23869 
23870   FormatStyle FourIndent = getLLVMStyle();
23871   FourIndent.ObjCBlockIndentWidth = 4;
23872   verifyFormat("[operation setCompletionBlock:^{\n"
23873                "    [self onOperationDone];\n"
23874                "}];",
23875                FourIndent);
23876 }
23877 
23878 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
23879   FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
23880 
23881   verifyFormat("[[SessionService sharedService] "
23882                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23883                "  if (window) {\n"
23884                "    [self windowDidLoad:window];\n"
23885                "  } else {\n"
23886                "    [self errorLoadingWindow];\n"
23887                "  }\n"
23888                "}];",
23889                ZeroColumn);
23890   verifyFormat("[[SessionService sharedService]\n"
23891                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23892                "      if (window) {\n"
23893                "        [self windowDidLoad:window];\n"
23894                "      } else {\n"
23895                "        [self errorLoadingWindow];\n"
23896                "      }\n"
23897                "    }];",
23898                "[[SessionService sharedService]\n"
23899                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23900                "                if (window) {\n"
23901                "    [self windowDidLoad:window];\n"
23902                "  } else {\n"
23903                "    [self errorLoadingWindow];\n"
23904                "  }\n"
23905                "}];",
23906                ZeroColumn);
23907   verifyFormat("[myObject doSomethingWith:arg1\n"
23908                "    firstBlock:^(Foo *a) {\n"
23909                "      // ...\n"
23910                "      int i;\n"
23911                "    }\n"
23912                "    secondBlock:^(Bar *b) {\n"
23913                "      // ...\n"
23914                "      int i;\n"
23915                "    }\n"
23916                "    thirdBlock:^Foo(Bar *b) {\n"
23917                "      // ...\n"
23918                "      int i;\n"
23919                "    }];",
23920                ZeroColumn);
23921   verifyFormat("f(^{\n"
23922                "  @autoreleasepool {\n"
23923                "    if (a) {\n"
23924                "      g();\n"
23925                "    }\n"
23926                "  }\n"
23927                "});",
23928                ZeroColumn);
23929   verifyFormat("void (^largeBlock)(void) = ^{\n"
23930                "  // ...\n"
23931                "};",
23932                ZeroColumn);
23933 
23934   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
23935   verifyFormat("void (^largeBlock)(void) = ^{ int i; };",
23936                "void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn);
23937   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
23938   verifyFormat("void (^largeBlock)(void) = ^{\n"
23939                "  int i;\n"
23940                "};",
23941                "void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn);
23942 }
23943 
23944 TEST_F(FormatTest, SupportsCRLF) {
23945   verifyFormat("int a;\r\n"
23946                "int b;\r\n"
23947                "int c;",
23948                "int a;\r\n"
23949                "  int b;\r\n"
23950                "    int c;");
23951   verifyFormat("int a;\r\n"
23952                "int b;\r\n"
23953                "int c;\r\n",
23954                "int a;\r\n"
23955                "  int b;\n"
23956                "    int c;\r\n");
23957   verifyFormat("int a;\n"
23958                "int b;\n"
23959                "int c;",
23960                "int a;\r\n"
23961                "  int b;\n"
23962                "    int c;");
23963   // FIXME: unstable test case
23964   EXPECT_EQ("\"aaaaaaa \"\r\n"
23965             "\"bbbbbbb\";\r\n",
23966             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
23967   verifyFormat("#define A \\\r\n"
23968                "  b;      \\\r\n"
23969                "  c;      \\\r\n"
23970                "  d;",
23971                "#define A \\\r\n"
23972                "  b; \\\r\n"
23973                "  c; d; ",
23974                getGoogleStyle());
23975 
23976   verifyNoChange("/*\r\n"
23977                  "multi line block comments\r\n"
23978                  "should not introduce\r\n"
23979                  "an extra carriage return\r\n"
23980                  "*/");
23981   verifyFormat("/*\r\n"
23982                "\r\n"
23983                "*/",
23984                "/*\r\n"
23985                "    \r\r\r\n"
23986                "*/");
23987 
23988   FormatStyle style = getLLVMStyle();
23989 
23990   EXPECT_EQ(style.LineEnding, FormatStyle::LE_DeriveLF);
23991   verifyFormat("union FooBarBazQux {\n"
23992                "  int foo;\n"
23993                "  int bar;\n"
23994                "  int baz;\n"
23995                "};",
23996                "union FooBarBazQux {\r\n"
23997                "  int foo;\n"
23998                "  int bar;\r\n"
23999                "  int baz;\n"
24000                "};",
24001                style);
24002   style.LineEnding = FormatStyle::LE_DeriveCRLF;
24003   verifyFormat("union FooBarBazQux {\r\n"
24004                "  int foo;\r\n"
24005                "  int bar;\r\n"
24006                "  int baz;\r\n"
24007                "};",
24008                "union FooBarBazQux {\r\n"
24009                "  int foo;\n"
24010                "  int bar;\r\n"
24011                "  int baz;\n"
24012                "};",
24013                style);
24014 
24015   style.LineEnding = FormatStyle::LE_LF;
24016   verifyFormat("union FooBarBazQux {\n"
24017                "  int foo;\n"
24018                "  int bar;\n"
24019                "  int baz;\n"
24020                "  int qux;\n"
24021                "};",
24022                "union FooBarBazQux {\r\n"
24023                "  int foo;\n"
24024                "  int bar;\r\n"
24025                "  int baz;\n"
24026                "  int qux;\r\n"
24027                "};",
24028                style);
24029   style.LineEnding = FormatStyle::LE_CRLF;
24030   verifyFormat("union FooBarBazQux {\r\n"
24031                "  int foo;\r\n"
24032                "  int bar;\r\n"
24033                "  int baz;\r\n"
24034                "  int qux;\r\n"
24035                "};",
24036                "union FooBarBazQux {\r\n"
24037                "  int foo;\n"
24038                "  int bar;\r\n"
24039                "  int baz;\n"
24040                "  int qux;\n"
24041                "};",
24042                style);
24043 
24044   style.LineEnding = FormatStyle::LE_DeriveLF;
24045   verifyFormat("union FooBarBazQux {\r\n"
24046                "  int foo;\r\n"
24047                "  int bar;\r\n"
24048                "  int baz;\r\n"
24049                "  int qux;\r\n"
24050                "};",
24051                "union FooBarBazQux {\r\n"
24052                "  int foo;\n"
24053                "  int bar;\r\n"
24054                "  int baz;\n"
24055                "  int qux;\r\n"
24056                "};",
24057                style);
24058   style.LineEnding = FormatStyle::LE_DeriveCRLF;
24059   verifyFormat("union FooBarBazQux {\n"
24060                "  int foo;\n"
24061                "  int bar;\n"
24062                "  int baz;\n"
24063                "  int qux;\n"
24064                "};",
24065                "union FooBarBazQux {\r\n"
24066                "  int foo;\n"
24067                "  int bar;\r\n"
24068                "  int baz;\n"
24069                "  int qux;\n"
24070                "};",
24071                style);
24072 }
24073 
24074 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
24075   verifyFormat("MY_CLASS(C) {\n"
24076                "  int i;\n"
24077                "  int j;\n"
24078                "};");
24079 }
24080 
24081 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
24082   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
24083   TwoIndent.ContinuationIndentWidth = 2;
24084 
24085   verifyFormat("int i =\n"
24086                "  longFunction(\n"
24087                "    arg);",
24088                "int i = longFunction(arg);", TwoIndent);
24089 
24090   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
24091   SixIndent.ContinuationIndentWidth = 6;
24092 
24093   verifyFormat("int i =\n"
24094                "      longFunction(\n"
24095                "            arg);",
24096                "int i = longFunction(arg);", SixIndent);
24097 }
24098 
24099 TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
24100   FormatStyle Style = getLLVMStyle();
24101   verifyFormat("int Foo::getter(\n"
24102                "    //\n"
24103                ") const {\n"
24104                "  return foo;\n"
24105                "}",
24106                Style);
24107   verifyFormat("void Foo::setter(\n"
24108                "    //\n"
24109                ") {\n"
24110                "  foo = 1;\n"
24111                "}",
24112                Style);
24113 }
24114 
24115 TEST_F(FormatTest, SpacesInAngles) {
24116   FormatStyle Spaces = getLLVMStyle();
24117   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
24118 
24119   verifyFormat("vector< ::std::string > x1;", Spaces);
24120   verifyFormat("Foo< int, Bar > x2;", Spaces);
24121   verifyFormat("Foo< ::int, ::Bar > x3;", Spaces);
24122 
24123   verifyFormat("static_cast< int >(arg);", Spaces);
24124   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
24125   verifyFormat("f< int, float >();", Spaces);
24126   verifyFormat("template <> g() {}", Spaces);
24127   verifyFormat("template < std::vector< int > > f() {}", Spaces);
24128   verifyFormat("std::function< void(int, int) > fct;", Spaces);
24129   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
24130                Spaces);
24131 
24132   Spaces.Standard = FormatStyle::LS_Cpp03;
24133   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
24134   verifyFormat("A< A< int > >();", Spaces);
24135 
24136   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
24137   verifyFormat("A<A<int> >();", Spaces);
24138 
24139   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
24140   verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
24141                Spaces);
24142   verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
24143                Spaces);
24144 
24145   verifyFormat("A<A<int> >();", Spaces);
24146   verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces);
24147   verifyFormat("A< A< int > >();", Spaces);
24148 
24149   Spaces.Standard = FormatStyle::LS_Cpp11;
24150   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
24151   verifyFormat("A< A< int > >();", Spaces);
24152 
24153   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
24154   verifyFormat("vector<::std::string> x4;", Spaces);
24155   verifyFormat("vector<int> x5;", Spaces);
24156   verifyFormat("Foo<int, Bar> x6;", Spaces);
24157   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
24158 
24159   verifyFormat("A<A<int>>();", Spaces);
24160 
24161   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
24162   verifyFormat("vector<::std::string> x4;", Spaces);
24163   verifyFormat("vector< ::std::string > x4;", Spaces);
24164   verifyFormat("vector<int> x5;", Spaces);
24165   verifyFormat("vector< int > x5;", Spaces);
24166   verifyFormat("Foo<int, Bar> x6;", Spaces);
24167   verifyFormat("Foo< int, Bar > x6;", Spaces);
24168   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
24169   verifyFormat("Foo< ::int, ::Bar > x7;", Spaces);
24170 
24171   verifyFormat("A<A<int>>();", Spaces);
24172   verifyFormat("A< A< int > >();", Spaces);
24173   verifyFormat("A<A<int > >();", Spaces);
24174   verifyFormat("A< A< int>>();", Spaces);
24175 
24176   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
24177   verifyFormat("// clang-format off\n"
24178                "foo<<<1, 1>>>();\n"
24179                "// clang-format on",
24180                Spaces);
24181   verifyFormat("// clang-format off\n"
24182                "foo< < <1, 1> > >();\n"
24183                "// clang-format on",
24184                Spaces);
24185 }
24186 
24187 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
24188   FormatStyle Style = getLLVMStyle();
24189   Style.SpaceAfterTemplateKeyword = false;
24190   verifyFormat("template<int> void foo();", Style);
24191 }
24192 
24193 TEST_F(FormatTest, TripleAngleBrackets) {
24194   verifyFormat("f<<<1, 1>>>();");
24195   verifyFormat("f<<<1, 1, 1, s>>>();");
24196   verifyFormat("f<<<a, b, c, d>>>();");
24197   verifyFormat("f<<<1, 1>>>();", "f <<< 1, 1 >>> ();");
24198   verifyFormat("f<param><<<1, 1>>>();");
24199   verifyFormat("f<1><<<1, 1>>>();");
24200   verifyFormat("f<param><<<1, 1>>>();", "f< param > <<< 1, 1 >>> ();");
24201   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
24202                "aaaaaaaaaaa<<<\n    1, 1>>>();");
24203   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
24204                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
24205 }
24206 
24207 TEST_F(FormatTest, MergeLessLessAtEnd) {
24208   verifyFormat("<<");
24209   verifyFormat("< < <", "\\\n<<<");
24210   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
24211                "aaallvm::outs() <<");
24212   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
24213                "aaaallvm::outs()\n    <<");
24214 }
24215 
24216 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
24217   std::string code = "#if A\n"
24218                      "#if B\n"
24219                      "a.\n"
24220                      "#endif\n"
24221                      "    a = 1;\n"
24222                      "#else\n"
24223                      "#endif\n"
24224                      "#if C\n"
24225                      "#else\n"
24226                      "#endif\n";
24227   verifyFormat(code);
24228 }
24229 
24230 TEST_F(FormatTest, HandleConflictMarkers) {
24231   // Git/SVN conflict markers.
24232   verifyFormat("int a;\n"
24233                "void f() {\n"
24234                "  callme(some(parameter1,\n"
24235                "<<<<<<< text by the vcs\n"
24236                "              parameter2),\n"
24237                "||||||| text by the vcs\n"
24238                "              parameter2),\n"
24239                "         parameter3,\n"
24240                "======= text by the vcs\n"
24241                "              parameter2, parameter3),\n"
24242                ">>>>>>> text by the vcs\n"
24243                "         otherparameter);",
24244                "int a;\n"
24245                "void f() {\n"
24246                "  callme(some(parameter1,\n"
24247                "<<<<<<< text by the vcs\n"
24248                "  parameter2),\n"
24249                "||||||| text by the vcs\n"
24250                "  parameter2),\n"
24251                "  parameter3,\n"
24252                "======= text by the vcs\n"
24253                "  parameter2,\n"
24254                "  parameter3),\n"
24255                ">>>>>>> text by the vcs\n"
24256                "  otherparameter);");
24257 
24258   // Perforce markers.
24259   verifyFormat("void f() {\n"
24260                "  function(\n"
24261                ">>>> text by the vcs\n"
24262                "      parameter,\n"
24263                "==== text by the vcs\n"
24264                "      parameter,\n"
24265                "==== text by the vcs\n"
24266                "      parameter,\n"
24267                "<<<< text by the vcs\n"
24268                "      parameter);",
24269                "void f() {\n"
24270                "  function(\n"
24271                ">>>> text by the vcs\n"
24272                "  parameter,\n"
24273                "==== text by the vcs\n"
24274                "  parameter,\n"
24275                "==== text by the vcs\n"
24276                "  parameter,\n"
24277                "<<<< text by the vcs\n"
24278                "  parameter);");
24279 
24280   verifyNoChange("<<<<<<<\n"
24281                  "|||||||\n"
24282                  "=======\n"
24283                  ">>>>>>>");
24284 
24285   verifyNoChange("<<<<<<<\n"
24286                  "|||||||\n"
24287                  "int i;\n"
24288                  "=======\n"
24289                  ">>>>>>>");
24290 
24291   // FIXME: Handle parsing of macros around conflict markers correctly:
24292   verifyFormat("#define Macro \\\n"
24293                "<<<<<<<\n"
24294                "Something \\\n"
24295                "|||||||\n"
24296                "Else \\\n"
24297                "=======\n"
24298                "Other \\\n"
24299                ">>>>>>>\n"
24300                "    End int i;",
24301                "#define Macro \\\n"
24302                "<<<<<<<\n"
24303                "  Something \\\n"
24304                "|||||||\n"
24305                "  Else \\\n"
24306                "=======\n"
24307                "  Other \\\n"
24308                ">>>>>>>\n"
24309                "  End\n"
24310                "int i;");
24311 
24312   verifyFormat(R"(====
24313 #ifdef A
24314 a
24315 #else
24316 b
24317 #endif
24318 )");
24319 }
24320 
24321 TEST_F(FormatTest, DisableRegions) {
24322   verifyFormat("int i;\n"
24323                "// clang-format off\n"
24324                "  int j;\n"
24325                "// clang-format on\n"
24326                "int k;",
24327                " int  i;\n"
24328                "   // clang-format off\n"
24329                "  int j;\n"
24330                " // clang-format on\n"
24331                "   int   k;");
24332   verifyFormat("int i;\n"
24333                "/* clang-format off */\n"
24334                "  int j;\n"
24335                "/* clang-format on */\n"
24336                "int k;",
24337                " int  i;\n"
24338                "   /* clang-format off */\n"
24339                "  int j;\n"
24340                " /* clang-format on */\n"
24341                "   int   k;");
24342 
24343   // Don't reflow comments within disabled regions.
24344   verifyFormat("// clang-format off\n"
24345                "// long long long long long long line\n"
24346                "/* clang-format on */\n"
24347                "/* long long long\n"
24348                " * long long long\n"
24349                " * line */\n"
24350                "int i;\n"
24351                "/* clang-format off */\n"
24352                "/* long long long long long long line */",
24353                "// clang-format off\n"
24354                "// long long long long long long line\n"
24355                "/* clang-format on */\n"
24356                "/* long long long long long long line */\n"
24357                "int i;\n"
24358                "/* clang-format off */\n"
24359                "/* long long long long long long line */",
24360                getLLVMStyleWithColumns(20));
24361 
24362   verifyFormat("int *i;\n"
24363                "// clang-format off:\n"
24364                "int* j;\n"
24365                "// clang-format on: 1\n"
24366                "int *k;",
24367                "int* i;\n"
24368                "// clang-format off:\n"
24369                "int* j;\n"
24370                "// clang-format on: 1\n"
24371                "int* k;");
24372 
24373   verifyFormat("int *i;\n"
24374                "// clang-format off:0\n"
24375                "int* j;\n"
24376                "// clang-format only\n"
24377                "int* k;",
24378                "int* i;\n"
24379                "// clang-format off:0\n"
24380                "int* j;\n"
24381                "// clang-format only\n"
24382                "int* k;");
24383 
24384   verifyNoChange("// clang-format off\n"
24385                  "#if 0\n"
24386                  "        #if SHOULD_STAY_INDENTED\n"
24387                  " #endif\n"
24388                  "#endif\n"
24389                  "// clang-format on");
24390 }
24391 
24392 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
24393   format("? ) =");
24394   verifyNoCrash("#define a\\\n /**/}");
24395 }
24396 
24397 TEST_F(FormatTest, FormatsTableGenCode) {
24398   FormatStyle Style = getLLVMStyle();
24399   Style.Language = FormatStyle::LK_TableGen;
24400   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
24401 }
24402 
24403 TEST_F(FormatTest, ArrayOfTemplates) {
24404   verifyFormat("auto a = new unique_ptr<int>[10];",
24405                "auto a = new unique_ptr<int > [ 10];");
24406 
24407   FormatStyle Spaces = getLLVMStyle();
24408   Spaces.SpacesInSquareBrackets = true;
24409   verifyFormat("auto a = new unique_ptr<int>[ 10 ];",
24410                "auto a = new unique_ptr<int > [10];", Spaces);
24411 }
24412 
24413 TEST_F(FormatTest, ArrayAsTemplateType) {
24414   verifyFormat("auto a = unique_ptr<Foo<Bar>[10]>;",
24415                "auto a = unique_ptr < Foo < Bar>[ 10]> ;");
24416 
24417   FormatStyle Spaces = getLLVMStyle();
24418   Spaces.SpacesInSquareBrackets = true;
24419   verifyFormat("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
24420                "auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces);
24421 }
24422 
24423 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
24424 
24425 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
24426   verifyFormat("using std::cin;\n"
24427                "using std::cout;",
24428                "using std::cout;\n"
24429                "using std::cin;",
24430                getGoogleStyle());
24431 }
24432 
24433 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
24434   FormatStyle Style = getLLVMStyle();
24435   Style.Standard = FormatStyle::LS_Cpp03;
24436   // cpp03 recognize this string as identifier u8 and literal character 'a'
24437   verifyFormat("auto c = u8 'a';", "auto c = u8'a';", Style);
24438 }
24439 
24440 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
24441   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
24442   // all modes, including C++11, C++14 and C++17
24443   verifyFormat("auto c = u8'a';");
24444 }
24445 
24446 TEST_F(FormatTest, DoNotFormatLikelyXml) {
24447   verifyGoogleFormat("<!-- ;> -->");
24448   verifyNoChange(" <!-- >; -->", getGoogleStyle());
24449 }
24450 
24451 TEST_F(FormatTest, StructuredBindings) {
24452   // Structured bindings is a C++17 feature.
24453   // all modes, including C++11, C++14 and C++17
24454   verifyFormat("auto [a, b] = f();");
24455   verifyFormat("auto [a, b] = f();", "auto[a, b] = f();");
24456   verifyFormat("const auto [a, b] = f();", "const   auto[a, b] = f();");
24457   verifyFormat("auto const [a, b] = f();", "auto  const[a, b] = f();");
24458   verifyFormat("auto const volatile [a, b] = f();",
24459                "auto  const   volatile[a, b] = f();");
24460   verifyFormat("auto [a, b, c] = f();", "auto   [  a  ,  b,c   ] = f();");
24461   verifyFormat("auto &[a, b, c] = f();", "auto   &[  a  ,  b,c   ] = f();");
24462   verifyFormat("auto &&[a, b, c] = f();", "auto   &&[  a  ,  b,c   ] = f();");
24463   verifyFormat("auto const &[a, b] = f();", "auto  const&[a, b] = f();");
24464   verifyFormat("auto const volatile &&[a, b] = f();",
24465                "auto  const  volatile  &&[a, b] = f();");
24466   verifyFormat("auto const &&[a, b] = f();", "auto  const   &&  [a, b] = f();");
24467   verifyFormat("const auto &[a, b] = f();", "const  auto  &  [a, b] = f();");
24468   verifyFormat("const auto volatile &&[a, b] = f();",
24469                "const  auto   volatile  &&[a, b] = f();");
24470   verifyFormat("volatile const auto &&[a, b] = f();",
24471                "volatile  const  auto   &&[a, b] = f();");
24472   verifyFormat("const auto &&[a, b] = f();", "const  auto  &&  [a, b] = f();");
24473 
24474   // Make sure we don't mistake structured bindings for lambdas.
24475   FormatStyle PointerMiddle = getLLVMStyle();
24476   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
24477   verifyGoogleFormat("auto [a1, b]{A * i};");
24478   verifyFormat("auto [a2, b]{A * i};");
24479   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
24480   verifyGoogleFormat("auto const [a1, b]{A * i};");
24481   verifyFormat("auto const [a2, b]{A * i};");
24482   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
24483   verifyGoogleFormat("auto const& [a1, b]{A * i};");
24484   verifyFormat("auto const &[a2, b]{A * i};");
24485   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
24486   verifyGoogleFormat("auto const&& [a1, b]{A * i};");
24487   verifyFormat("auto const &&[a2, b]{A * i};");
24488   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
24489 
24490   verifyFormat("for (const auto &&[a, b] : some_range) {\n}",
24491                "for (const auto   &&   [a, b] : some_range) {\n}");
24492   verifyFormat("for (const auto &[a, b] : some_range) {\n}",
24493                "for (const auto   &   [a, b] : some_range) {\n}");
24494   verifyFormat("for (const auto [a, b] : some_range) {\n}",
24495                "for (const auto[a, b] : some_range) {\n}");
24496   verifyFormat("auto [x, y](expr);", "auto[x,y]  (expr);");
24497   verifyFormat("auto &[x, y](expr);", "auto  &  [x,y]  (expr);");
24498   verifyFormat("auto &&[x, y](expr);", "auto  &&  [x,y]  (expr);");
24499   verifyFormat("auto const &[x, y](expr);", "auto  const  &  [x,y]  (expr);");
24500   verifyFormat("auto const &&[x, y](expr);", "auto  const  &&  [x,y]  (expr);");
24501   verifyFormat("auto [x, y]{expr};", "auto[x,y]     {expr};");
24502   verifyFormat("auto const &[x, y]{expr};", "auto  const  &  [x,y]  {expr};");
24503   verifyFormat("auto const &&[x, y]{expr};", "auto  const  &&  [x,y]  {expr};");
24504 
24505   FormatStyle Spaces = getLLVMStyle();
24506   Spaces.SpacesInSquareBrackets = true;
24507   verifyFormat("auto [ a, b ] = f();", Spaces);
24508   verifyFormat("auto &&[ a, b ] = f();", Spaces);
24509   verifyFormat("auto &[ a, b ] = f();", Spaces);
24510   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
24511   verifyFormat("auto const &[ a, b ] = f();", Spaces);
24512 }
24513 
24514 TEST_F(FormatTest, FileAndCode) {
24515   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
24516   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
24517   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
24518   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
24519   EXPECT_EQ(FormatStyle::LK_ObjC,
24520             guessLanguage("foo.h", "@interface Foo\n@end"));
24521   EXPECT_EQ(
24522       FormatStyle::LK_ObjC,
24523       guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
24524   EXPECT_EQ(FormatStyle::LK_ObjC,
24525             guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
24526   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
24527   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
24528   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end"));
24529   EXPECT_EQ(FormatStyle::LK_ObjC,
24530             guessLanguage("foo.h", "int DoStuff(CGRect rect);"));
24531   EXPECT_EQ(FormatStyle::LK_ObjC,
24532             guessLanguage(
24533                 "foo.h", "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));"));
24534   EXPECT_EQ(
24535       FormatStyle::LK_Cpp,
24536       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
24537   // Only one of the two preprocessor regions has ObjC-like code.
24538   EXPECT_EQ(FormatStyle::LK_ObjC,
24539             guessLanguage("foo.h", "#if A\n"
24540                                    "#define B() C\n"
24541                                    "#else\n"
24542                                    "#define B() [NSString a:@\"\"]\n"
24543                                    "#endif"));
24544 }
24545 
24546 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
24547   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
24548   EXPECT_EQ(FormatStyle::LK_ObjC,
24549             guessLanguage("foo.h", "array[[calculator getIndex]];"));
24550   EXPECT_EQ(FormatStyle::LK_Cpp,
24551             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
24552   EXPECT_EQ(
24553       FormatStyle::LK_Cpp,
24554       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
24555   EXPECT_EQ(FormatStyle::LK_ObjC,
24556             guessLanguage("foo.h", "[[noreturn foo] bar];"));
24557   EXPECT_EQ(FormatStyle::LK_Cpp,
24558             guessLanguage("foo.h", "[[clang::fallthrough]];"));
24559   EXPECT_EQ(FormatStyle::LK_ObjC,
24560             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
24561   EXPECT_EQ(FormatStyle::LK_Cpp,
24562             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
24563   EXPECT_EQ(FormatStyle::LK_Cpp,
24564             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
24565   EXPECT_EQ(FormatStyle::LK_ObjC,
24566             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
24567   EXPECT_EQ(FormatStyle::LK_Cpp,
24568             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
24569   EXPECT_EQ(
24570       FormatStyle::LK_Cpp,
24571       guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
24572   EXPECT_EQ(
24573       FormatStyle::LK_Cpp,
24574       guessLanguage("foo.h",
24575                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
24576   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
24577 }
24578 
24579 TEST_F(FormatTest, GuessLanguageWithCaret) {
24580   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
24581   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
24582   EXPECT_EQ(FormatStyle::LK_ObjC,
24583             guessLanguage("foo.h", "int(^)(char, float);"));
24584   EXPECT_EQ(FormatStyle::LK_ObjC,
24585             guessLanguage("foo.h", "int(^foo)(char, float);"));
24586   EXPECT_EQ(FormatStyle::LK_ObjC,
24587             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
24588   EXPECT_EQ(FormatStyle::LK_ObjC,
24589             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
24590   EXPECT_EQ(
24591       FormatStyle::LK_ObjC,
24592       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
24593 }
24594 
24595 TEST_F(FormatTest, GuessLanguageWithPragmas) {
24596   EXPECT_EQ(FormatStyle::LK_Cpp,
24597             guessLanguage("foo.h", "__pragma(warning(disable:))"));
24598   EXPECT_EQ(FormatStyle::LK_Cpp,
24599             guessLanguage("foo.h", "#pragma(warning(disable:))"));
24600   EXPECT_EQ(FormatStyle::LK_Cpp,
24601             guessLanguage("foo.h", "_Pragma(warning(disable:))"));
24602 }
24603 
24604 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
24605   // ASM symbolic names are identifiers that must be surrounded by [] without
24606   // space in between:
24607   // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
24608 
24609   // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
24610   verifyFormat(R"(//
24611 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
24612 )");
24613 
24614   // A list of several ASM symbolic names.
24615   verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
24616 
24617   // ASM symbolic names in inline ASM with inputs and outputs.
24618   verifyFormat(R"(//
24619 asm("cmoveq %1, %2, %[result]"
24620     : [result] "=r"(result)
24621     : "r"(test), "r"(new), "[result]"(old));
24622 )");
24623 
24624   // ASM symbolic names in inline ASM with no outputs.
24625   verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
24626 }
24627 
24628 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
24629   EXPECT_EQ(FormatStyle::LK_Cpp,
24630             guessLanguage("foo.h", "void f() {\n"
24631                                    "  asm (\"mov %[e], %[d]\"\n"
24632                                    "     : [d] \"=rm\" (d)\n"
24633                                    "       [e] \"rm\" (*e));\n"
24634                                    "}"));
24635   EXPECT_EQ(FormatStyle::LK_Cpp,
24636             guessLanguage("foo.h", "void f() {\n"
24637                                    "  _asm (\"mov %[e], %[d]\"\n"
24638                                    "     : [d] \"=rm\" (d)\n"
24639                                    "       [e] \"rm\" (*e));\n"
24640                                    "}"));
24641   EXPECT_EQ(FormatStyle::LK_Cpp,
24642             guessLanguage("foo.h", "void f() {\n"
24643                                    "  __asm (\"mov %[e], %[d]\"\n"
24644                                    "     : [d] \"=rm\" (d)\n"
24645                                    "       [e] \"rm\" (*e));\n"
24646                                    "}"));
24647   EXPECT_EQ(FormatStyle::LK_Cpp,
24648             guessLanguage("foo.h", "void f() {\n"
24649                                    "  __asm__ (\"mov %[e], %[d]\"\n"
24650                                    "     : [d] \"=rm\" (d)\n"
24651                                    "       [e] \"rm\" (*e));\n"
24652                                    "}"));
24653   EXPECT_EQ(FormatStyle::LK_Cpp,
24654             guessLanguage("foo.h", "void f() {\n"
24655                                    "  asm (\"mov %[e], %[d]\"\n"
24656                                    "     : [d] \"=rm\" (d),\n"
24657                                    "       [e] \"rm\" (*e));\n"
24658                                    "}"));
24659   EXPECT_EQ(FormatStyle::LK_Cpp,
24660             guessLanguage("foo.h", "void f() {\n"
24661                                    "  asm volatile (\"mov %[e], %[d]\"\n"
24662                                    "     : [d] \"=rm\" (d)\n"
24663                                    "       [e] \"rm\" (*e));\n"
24664                                    "}"));
24665 }
24666 
24667 TEST_F(FormatTest, GuessLanguageWithChildLines) {
24668   EXPECT_EQ(FormatStyle::LK_Cpp,
24669             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
24670   EXPECT_EQ(FormatStyle::LK_ObjC,
24671             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
24672   EXPECT_EQ(
24673       FormatStyle::LK_Cpp,
24674       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
24675   EXPECT_EQ(
24676       FormatStyle::LK_ObjC,
24677       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
24678 }
24679 
24680 TEST_F(FormatTest, TypenameMacros) {
24681   std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
24682 
24683   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
24684   FormatStyle Google = getGoogleStyleWithColumns(0);
24685   Google.TypenameMacros = TypenameMacros;
24686   verifyFormat("struct foo {\n"
24687                "  int bar;\n"
24688                "  TAILQ_ENTRY(a) bleh;\n"
24689                "};",
24690                Google);
24691 
24692   FormatStyle Macros = getLLVMStyle();
24693   Macros.TypenameMacros = TypenameMacros;
24694 
24695   verifyFormat("STACK_OF(int) a;", Macros);
24696   verifyFormat("STACK_OF(int) *a;", Macros);
24697   verifyFormat("STACK_OF(int const *) *a;", Macros);
24698   verifyFormat("STACK_OF(int *const) *a;", Macros);
24699   verifyFormat("STACK_OF(int, string) a;", Macros);
24700   verifyFormat("STACK_OF(LIST(int)) a;", Macros);
24701   verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
24702   verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
24703   verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
24704   verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros);
24705   verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros);
24706 
24707   Macros.PointerAlignment = FormatStyle::PAS_Left;
24708   verifyFormat("STACK_OF(int)* a;", Macros);
24709   verifyFormat("STACK_OF(int*)* a;", Macros);
24710   verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
24711   verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
24712   verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros);
24713 }
24714 
24715 TEST_F(FormatTest, AtomicQualifier) {
24716   // Check that we treate _Atomic as a type and not a function call
24717   FormatStyle Google = getGoogleStyleWithColumns(0);
24718   verifyFormat("struct foo {\n"
24719                "  int a1;\n"
24720                "  _Atomic(a) a2;\n"
24721                "  _Atomic(_Atomic(int) *const) a3;\n"
24722                "};",
24723                Google);
24724   verifyFormat("_Atomic(uint64_t) a;");
24725   verifyFormat("_Atomic(uint64_t) *a;");
24726   verifyFormat("_Atomic(uint64_t const *) *a;");
24727   verifyFormat("_Atomic(uint64_t *const) *a;");
24728   verifyFormat("_Atomic(const uint64_t *) *a;");
24729   verifyFormat("_Atomic(uint64_t) a;");
24730   verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
24731   verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
24732   verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
24733   verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
24734 
24735   verifyFormat("_Atomic(uint64_t) *s(InitValue);");
24736   verifyFormat("_Atomic(uint64_t) *s{InitValue};");
24737   FormatStyle Style = getLLVMStyle();
24738   Style.PointerAlignment = FormatStyle::PAS_Left;
24739   verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style);
24740   verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style);
24741   verifyFormat("_Atomic(int)* a;", Style);
24742   verifyFormat("_Atomic(int*)* a;", Style);
24743   verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
24744 
24745   Style.SpacesInParens = FormatStyle::SIPO_Custom;
24746   Style.SpacesInParensOptions.InCStyleCasts = true;
24747   verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
24748   Style.SpacesInParensOptions.InCStyleCasts = false;
24749   Style.SpacesInParensOptions.Other = true;
24750   verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
24751   verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
24752 }
24753 
24754 TEST_F(FormatTest, C11Generic) {
24755   verifyFormat("_Generic(x, int: 1, default: 0)");
24756   verifyFormat("#define cbrt(X) _Generic((X), float: cbrtf, default: cbrt)(X)");
24757   verifyFormat("_Generic(x, const char *: 1, char *const: 16, int: 8);");
24758   verifyFormat("_Generic(x, int: f1, const int: f2)();");
24759   verifyFormat("_Generic(x, struct A: 1, void (*)(void): 2);");
24760 
24761   verifyFormat("_Generic(x,\n"
24762                "    float: f,\n"
24763                "    default: d,\n"
24764                "    long double: ld,\n"
24765                "    float _Complex: fc,\n"
24766                "    double _Complex: dc,\n"
24767                "    long double _Complex: ldc)");
24768 
24769   verifyFormat("while (_Generic(x, //\n"
24770                "           long: x)(x) > x) {\n"
24771                "}");
24772   verifyFormat("while (_Generic(x, //\n"
24773                "           long: x)(x)) {\n"
24774                "}");
24775   verifyFormat("x(_Generic(x, //\n"
24776                "      long: x)(x));");
24777 
24778   FormatStyle Style = getLLVMStyle();
24779   Style.ColumnLimit = 40;
24780   verifyFormat("#define LIMIT_MAX(T)                   \\\n"
24781                "  _Generic(((T)0),                     \\\n"
24782                "      unsigned int: UINT_MAX,          \\\n"
24783                "      unsigned long: ULONG_MAX,        \\\n"
24784                "      unsigned long long: ULLONG_MAX)",
24785                Style);
24786   verifyFormat("_Generic(x,\n"
24787                "    struct A: 1,\n"
24788                "    void (*)(void): 2);",
24789                Style);
24790 
24791   Style.ContinuationIndentWidth = 2;
24792   verifyFormat("_Generic(x,\n"
24793                "  struct A: 1,\n"
24794                "  void (*)(void): 2);",
24795                Style);
24796 }
24797 
24798 TEST_F(FormatTest, AmbersandInLamda) {
24799   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
24800   FormatStyle AlignStyle = getLLVMStyle();
24801   AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
24802   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
24803   AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
24804   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
24805 }
24806 
24807 TEST_F(FormatTest, TrailingReturnTypeAuto) {
24808   FormatStyle Style = getLLVMStyle();
24809   verifyFormat("[]() -> auto { return Val; }", Style);
24810   verifyFormat("[]() -> auto * { return Val; }", Style);
24811   verifyFormat("[]() -> auto & { return Val; }", Style);
24812   verifyFormat("auto foo() -> auto { return Val; }", Style);
24813   verifyFormat("auto foo() -> auto * { return Val; }", Style);
24814   verifyFormat("auto foo() -> auto & { return Val; }", Style);
24815 }
24816 
24817 TEST_F(FormatTest, SpacesInConditionalStatement) {
24818   FormatStyle Spaces = getLLVMStyle();
24819   Spaces.IfMacros.clear();
24820   Spaces.IfMacros.push_back("MYIF");
24821   Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
24822   Spaces.SpacesInParensOptions.InConditionalStatements = true;
24823   verifyFormat("for ( int i = 0; i; i++ )\n  continue;", Spaces);
24824   verifyFormat("if ( !a )\n  return;", Spaces);
24825   verifyFormat("if ( a )\n  return;", Spaces);
24826   verifyFormat("if constexpr ( a )\n  return;", Spaces);
24827   verifyFormat("MYIF ( a )\n  return;", Spaces);
24828   verifyFormat("MYIF ( a )\n  return;\nelse MYIF ( b )\n  return;", Spaces);
24829   verifyFormat("MYIF ( a )\n  return;\nelse\n  return;", Spaces);
24830   verifyFormat("switch ( a )\ncase 1:\n  return;", Spaces);
24831   verifyFormat("while ( a )\n  return;", Spaces);
24832   verifyFormat("while ( (a && b) )\n  return;", Spaces);
24833   verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
24834   verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
24835   // Check that space on the left of "::" is inserted as expected at beginning
24836   // of condition.
24837   verifyFormat("while ( ::func() )\n  return;", Spaces);
24838 
24839   // Check impact of ControlStatementsExceptControlMacros is honored.
24840   Spaces.SpaceBeforeParens =
24841       FormatStyle::SBPO_ControlStatementsExceptControlMacros;
24842   verifyFormat("MYIF( a )\n  return;", Spaces);
24843   verifyFormat("MYIF( a )\n  return;\nelse MYIF( b )\n  return;", Spaces);
24844   verifyFormat("MYIF( a )\n  return;\nelse\n  return;", Spaces);
24845 }
24846 
24847 TEST_F(FormatTest, AlternativeOperators) {
24848   // Test case for ensuring alternate operators are not
24849   // combined with their right most neighbour.
24850   verifyFormat("int a and b;");
24851   verifyFormat("int a and_eq b;");
24852   verifyFormat("int a bitand b;");
24853   verifyFormat("int a bitor b;");
24854   verifyFormat("int a compl b;");
24855   verifyFormat("int a not b;");
24856   verifyFormat("int a not_eq b;");
24857   verifyFormat("int a or b;");
24858   verifyFormat("int a xor b;");
24859   verifyFormat("int a xor_eq b;");
24860   verifyFormat("return this not_eq bitand other;");
24861   verifyFormat("bool operator not_eq(const X bitand other)");
24862 
24863   verifyFormat("int a and 5;");
24864   verifyFormat("int a and_eq 5;");
24865   verifyFormat("int a bitand 5;");
24866   verifyFormat("int a bitor 5;");
24867   verifyFormat("int a compl 5;");
24868   verifyFormat("int a not 5;");
24869   verifyFormat("int a not_eq 5;");
24870   verifyFormat("int a or 5;");
24871   verifyFormat("int a xor 5;");
24872   verifyFormat("int a xor_eq 5;");
24873 
24874   verifyFormat("int a compl(5);");
24875   verifyFormat("int a not(5);");
24876 
24877   verifyFormat("compl foo();");     // ~foo();
24878   verifyFormat("foo() <%%>");       // foo() {}
24879   verifyFormat("void foo() <%%>");  // void foo() {}
24880   verifyFormat("int a<:1:>;");      // int a[1];
24881   verifyFormat("%:define ABC abc"); // #define ABC abc
24882   verifyFormat("%:%:");             // ##
24883 
24884   verifyFormat("a = v(not;);\n"
24885                "b = v(not+);\n"
24886                "c = v(not x);\n"
24887                "d = v(not 1);\n"
24888                "e = v(not 123.f);");
24889 
24890   verifyNoChange("#define ASSEMBLER_INSTRUCTION_LIST(V)  \\\n"
24891                  "  V(and)                               \\\n"
24892                  "  V(not)                               \\\n"
24893                  "  V(not!)                              \\\n"
24894                  "  V(other)",
24895                  getLLVMStyleWithColumns(40));
24896 }
24897 
24898 TEST_F(FormatTest, STLWhileNotDefineChed) {
24899   verifyFormat("#if defined(while)\n"
24900                "#define while EMIT WARNING C4005\n"
24901                "#endif // while");
24902 }
24903 
24904 TEST_F(FormatTest, OperatorSpacing) {
24905   FormatStyle Style = getLLVMStyle();
24906   Style.PointerAlignment = FormatStyle::PAS_Right;
24907   verifyFormat("Foo::operator*();", Style);
24908   verifyFormat("Foo::operator void *();", Style);
24909   verifyFormat("Foo::operator void **();", Style);
24910   verifyFormat("Foo::operator void *&();", Style);
24911   verifyFormat("Foo::operator void *&&();", Style);
24912   verifyFormat("Foo::operator void const *();", Style);
24913   verifyFormat("Foo::operator void const **();", Style);
24914   verifyFormat("Foo::operator void const *&();", Style);
24915   verifyFormat("Foo::operator void const *&&();", Style);
24916   verifyFormat("Foo::operator()(void *);", Style);
24917   verifyFormat("Foo::operator*(void *);", Style);
24918   verifyFormat("Foo::operator*();", Style);
24919   verifyFormat("Foo::operator**();", Style);
24920   verifyFormat("Foo::operator&();", Style);
24921   verifyFormat("Foo::operator<int> *();", Style);
24922   verifyFormat("Foo::operator<Foo> *();", Style);
24923   verifyFormat("Foo::operator<int> **();", Style);
24924   verifyFormat("Foo::operator<Foo> **();", Style);
24925   verifyFormat("Foo::operator<int> &();", Style);
24926   verifyFormat("Foo::operator<Foo> &();", Style);
24927   verifyFormat("Foo::operator<int> &&();", Style);
24928   verifyFormat("Foo::operator<Foo> &&();", Style);
24929   verifyFormat("Foo::operator<int> *&();", Style);
24930   verifyFormat("Foo::operator<Foo> *&();", Style);
24931   verifyFormat("Foo::operator<int> *&&();", Style);
24932   verifyFormat("Foo::operator<Foo> *&&();", Style);
24933   verifyFormat("operator*(int (*)(), class Foo);", Style);
24934 
24935   verifyFormat("Foo::operator&();", Style);
24936   verifyFormat("Foo::operator void &();", Style);
24937   verifyFormat("Foo::operator void const &();", Style);
24938   verifyFormat("Foo::operator()(void &);", Style);
24939   verifyFormat("Foo::operator&(void &);", Style);
24940   verifyFormat("Foo::operator&();", Style);
24941   verifyFormat("operator&(int (&)(), class Foo);", Style);
24942   verifyFormat("operator&&(int (&)(), class Foo);", Style);
24943 
24944   verifyFormat("Foo::operator&&();", Style);
24945   verifyFormat("Foo::operator**();", Style);
24946   verifyFormat("Foo::operator void &&();", Style);
24947   verifyFormat("Foo::operator void const &&();", Style);
24948   verifyFormat("Foo::operator()(void &&);", Style);
24949   verifyFormat("Foo::operator&&(void &&);", Style);
24950   verifyFormat("Foo::operator&&();", Style);
24951   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
24952   verifyFormat("operator const nsTArrayRight<E> &()", Style);
24953   verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
24954                Style);
24955   verifyFormat("operator void **()", Style);
24956   verifyFormat("operator const FooRight<Object> &()", Style);
24957   verifyFormat("operator const FooRight<Object> *()", Style);
24958   verifyFormat("operator const FooRight<Object> **()", Style);
24959   verifyFormat("operator const FooRight<Object> *&()", Style);
24960   verifyFormat("operator const FooRight<Object> *&&()", Style);
24961 
24962   Style.PointerAlignment = FormatStyle::PAS_Left;
24963   verifyFormat("Foo::operator*();", Style);
24964   verifyFormat("Foo::operator**();", Style);
24965   verifyFormat("Foo::operator void*();", Style);
24966   verifyFormat("Foo::operator void**();", Style);
24967   verifyFormat("Foo::operator void*&();", Style);
24968   verifyFormat("Foo::operator void*&&();", Style);
24969   verifyFormat("Foo::operator void const*();", Style);
24970   verifyFormat("Foo::operator void const**();", Style);
24971   verifyFormat("Foo::operator void const*&();", Style);
24972   verifyFormat("Foo::operator void const*&&();", Style);
24973   verifyFormat("Foo::operator/*comment*/ void*();", Style);
24974   verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
24975   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
24976   verifyFormat("Foo::operator()(void*);", Style);
24977   verifyFormat("Foo::operator*(void*);", Style);
24978   verifyFormat("Foo::operator*();", Style);
24979   verifyFormat("Foo::operator<int>*();", Style);
24980   verifyFormat("Foo::operator<Foo>*();", Style);
24981   verifyFormat("Foo::operator<int>**();", Style);
24982   verifyFormat("Foo::operator<Foo>**();", Style);
24983   verifyFormat("Foo::operator<Foo>*&();", Style);
24984   verifyFormat("Foo::operator<int>&();", Style);
24985   verifyFormat("Foo::operator<Foo>&();", Style);
24986   verifyFormat("Foo::operator<int>&&();", Style);
24987   verifyFormat("Foo::operator<Foo>&&();", Style);
24988   verifyFormat("Foo::operator<int>*&();", Style);
24989   verifyFormat("Foo::operator<Foo>*&();", Style);
24990   verifyFormat("operator*(int (*)(), class Foo);", Style);
24991 
24992   verifyFormat("Foo::operator&();", Style);
24993   verifyFormat("Foo::operator void&();", Style);
24994   verifyFormat("Foo::operator void const&();", Style);
24995   verifyFormat("Foo::operator/*comment*/ void&();", Style);
24996   verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
24997   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
24998   verifyFormat("Foo::operator()(void&);", Style);
24999   verifyFormat("Foo::operator&(void&);", Style);
25000   verifyFormat("Foo::operator&();", Style);
25001   verifyFormat("operator&(int (&)(), class Foo);", Style);
25002   verifyFormat("operator&(int (&&)(), class Foo);", Style);
25003   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
25004 
25005   verifyFormat("Foo::operator&&();", Style);
25006   verifyFormat("Foo::operator void&&();", Style);
25007   verifyFormat("Foo::operator void const&&();", Style);
25008   verifyFormat("Foo::operator/*comment*/ void&&();", Style);
25009   verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
25010   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
25011   verifyFormat("Foo::operator()(void&&);", Style);
25012   verifyFormat("Foo::operator&&(void&&);", Style);
25013   verifyFormat("Foo::operator&&();", Style);
25014   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
25015   verifyFormat("operator const nsTArrayLeft<E>&()", Style);
25016   verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
25017                Style);
25018   verifyFormat("operator void**()", Style);
25019   verifyFormat("operator const FooLeft<Object>&()", Style);
25020   verifyFormat("operator const FooLeft<Object>*()", Style);
25021   verifyFormat("operator const FooLeft<Object>**()", Style);
25022   verifyFormat("operator const FooLeft<Object>*&()", Style);
25023   verifyFormat("operator const FooLeft<Object>*&&()", Style);
25024 
25025   // PR45107
25026   verifyFormat("operator Vector<String>&();", Style);
25027   verifyFormat("operator const Vector<String>&();", Style);
25028   verifyFormat("operator foo::Bar*();", Style);
25029   verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
25030   verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
25031                Style);
25032 
25033   Style.PointerAlignment = FormatStyle::PAS_Middle;
25034   verifyFormat("Foo::operator*();", Style);
25035   verifyFormat("Foo::operator void *();", Style);
25036   verifyFormat("Foo::operator()(void *);", Style);
25037   verifyFormat("Foo::operator*(void *);", Style);
25038   verifyFormat("Foo::operator*();", Style);
25039   verifyFormat("operator*(int (*)(), class Foo);", Style);
25040 
25041   verifyFormat("Foo::operator&();", Style);
25042   verifyFormat("Foo::operator void &();", Style);
25043   verifyFormat("Foo::operator void const &();", Style);
25044   verifyFormat("Foo::operator()(void &);", Style);
25045   verifyFormat("Foo::operator&(void &);", Style);
25046   verifyFormat("Foo::operator&();", Style);
25047   verifyFormat("operator&(int (&)(), class Foo);", Style);
25048 
25049   verifyFormat("Foo::operator&&();", Style);
25050   verifyFormat("Foo::operator void &&();", Style);
25051   verifyFormat("Foo::operator void const &&();", Style);
25052   verifyFormat("Foo::operator()(void &&);", Style);
25053   verifyFormat("Foo::operator&&(void &&);", Style);
25054   verifyFormat("Foo::operator&&();", Style);
25055   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
25056 }
25057 
25058 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
25059   FormatStyle Style = getLLVMStyle();
25060   // PR46157
25061   verifyFormat("foo(operator+, -42);", Style);
25062   verifyFormat("foo(operator++, -42);", Style);
25063   verifyFormat("foo(operator--, -42);", Style);
25064   verifyFormat("foo(-42, operator--);", Style);
25065   verifyFormat("foo(-42, operator, );", Style);
25066   verifyFormat("foo(operator, , -42);", Style);
25067 }
25068 
25069 TEST_F(FormatTest, WhitespaceSensitiveMacros) {
25070   FormatStyle Style = getLLVMStyle();
25071   Style.WhitespaceSensitiveMacros.push_back("FOO");
25072 
25073   // Newlines are important here.
25074   verifyNoChange("FOO(1+2 )\n", Style);
25075   verifyNoChange("FOO(a:b:c)\n", Style);
25076 
25077   // Don't use the helpers here, since 'mess up' will change the whitespace
25078   // and these are all whitespace sensitive by definition
25079   verifyNoChange("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style);
25080   verifyNoChange("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style);
25081   verifyNoChange("FOO(String-ized&Messy+But,: :Still=Intentional);", Style);
25082   verifyNoChange("FOO(String-ized&Messy+But,: :\n"
25083                  "       Still=Intentional);",
25084                  Style);
25085   Style.AlignConsecutiveAssignments.Enabled = true;
25086   verifyNoChange("FOO(String-ized=&Messy+But,: :\n"
25087                  "       Still=Intentional);",
25088                  Style);
25089 
25090   Style.ColumnLimit = 21;
25091   verifyNoChange("FOO(String-ized&Messy+But: :Still=Intentional);", Style);
25092 }
25093 
25094 TEST_F(FormatTest, SkipMacroDefinitionBody) {
25095   auto Style = getLLVMStyle();
25096   Style.SkipMacroDefinitionBody = true;
25097 
25098   verifyFormat("#define A", "#define  A", Style);
25099   verifyFormat("#define A       a   aa", "#define   A       a   aa", Style);
25100   verifyNoChange("#define A   b", Style);
25101   verifyNoChange("#define A  (  args   )", Style);
25102   verifyNoChange("#define A  (  args   )  =  func  (  args  )", Style);
25103   verifyNoChange("#define A  (  args   )  {  int  a  =  1 ;  }", Style);
25104   verifyNoChange("#define A  (  args   ) \\\n"
25105                  "  {\\\n"
25106                  "    int  a  =  1 ;\\\n"
25107                  "}",
25108                  Style);
25109 
25110   verifyNoChange("#define A x:", Style);
25111   verifyNoChange("#define A a. b", Style);
25112 
25113   // Surrounded with formatted code.
25114   verifyFormat("int a;\n"
25115                "#define A  a\n"
25116                "int a;",
25117                "int  a ;\n"
25118                "#define  A  a\n"
25119                "int  a ;",
25120                Style);
25121 
25122   // Columns are not broken when a limit is set.
25123   Style.ColumnLimit = 10;
25124   verifyFormat("#define A  a  a  a  a", " # define  A  a  a  a  a ", Style);
25125   verifyNoChange("#define A a a a a", Style);
25126 
25127   Style.ColumnLimit = 15;
25128   verifyFormat("#define A // a\n"
25129                "          // very\n"
25130                "          // long\n"
25131                "          // comment",
25132                "#define A //a very long comment", Style);
25133   Style.ColumnLimit = 0;
25134 
25135   // Multiline definition.
25136   verifyNoChange("#define A \\\n"
25137                  "Line one with spaces  .  \\\n"
25138                  " Line two.",
25139                  Style);
25140   verifyNoChange("#define A \\\n"
25141                  "a a \\\n"
25142                  "a        \\\n"
25143                  "a",
25144                  Style);
25145   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
25146   verifyNoChange("#define A \\\n"
25147                  "a a \\\n"
25148                  "a        \\\n"
25149                  "a",
25150                  Style);
25151   Style.AlignEscapedNewlines = FormatStyle::ENAS_Right;
25152   verifyNoChange("#define A \\\n"
25153                  "a a \\\n"
25154                  "a        \\\n"
25155                  "a",
25156                  Style);
25157 
25158   // Adjust indendations but don't change the definition.
25159   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
25160   verifyNoChange("#if A\n"
25161                  "#define A  a\n"
25162                  "#endif",
25163                  Style);
25164   verifyFormat("#if A\n"
25165                "#define A  a\n"
25166                "#endif",
25167                "#if A\n"
25168                "  #define A  a\n"
25169                "#endif",
25170                Style);
25171   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
25172   verifyNoChange("#if A\n"
25173                  "#  define A  a\n"
25174                  "#endif",
25175                  Style);
25176   verifyFormat("#if A\n"
25177                "#  define A  a\n"
25178                "#endif",
25179                "#if A\n"
25180                "  #define A  a\n"
25181                "#endif",
25182                Style);
25183   Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
25184   verifyNoChange("#if A\n"
25185                  "  #define A  a\n"
25186                  "#endif",
25187                  Style);
25188   verifyFormat("#if A\n"
25189                "  #define A  a\n"
25190                "#endif",
25191                "#if A\n"
25192                " # define A  a\n"
25193                "#endif",
25194                Style);
25195 
25196   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
25197   // SkipMacroDefinitionBody should not affect other PP directives
25198   verifyFormat("#if !defined(A)\n"
25199                "#define A  a\n"
25200                "#endif",
25201                "#if ! defined ( A )\n"
25202                "  #define  A  a\n"
25203                "#endif",
25204                Style);
25205 
25206   // With comments.
25207   verifyFormat("/* */ #define A  a  //  a  a", "/* */  # define A  a  //  a  a",
25208                Style);
25209   verifyNoChange("/* */ #define A  a //  a  a", Style);
25210 
25211   verifyFormat("int a;    // a\n"
25212                "#define A // a\n"
25213                "int aaa;  // a",
25214                "int a; // a\n"
25215                "#define A  // a\n"
25216                "int aaa; // a",
25217                Style);
25218 
25219   verifyNoChange(
25220       "#define MACRO_WITH_COMMENTS()                                       \\\n"
25221       "  public:                                                           \\\n"
25222       "    /* Documentation parsed by Doxygen for the following method. */ \\\n"
25223       "    static MyType getClassTypeId();                                 \\\n"
25224       "    /** Normal comment for the following method. */                 \\\n"
25225       "    virtual MyType getTypeId() const;",
25226       Style);
25227 
25228   // multiline macro definitions
25229   verifyNoChange("#define A  a\\\n"
25230                  "  A  a \\\n "
25231                  " A  a",
25232                  Style);
25233 }
25234 
25235 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
25236   // These tests are not in NamespaceEndCommentsFixerTest because that doesn't
25237   // test its interaction with line wrapping
25238   FormatStyle Style = getLLVMStyleWithColumns(80);
25239   verifyFormat("namespace {\n"
25240                "int i;\n"
25241                "int j;\n"
25242                "} // namespace",
25243                Style);
25244 
25245   verifyFormat("namespace AAA {\n"
25246                "int i;\n"
25247                "int j;\n"
25248                "} // namespace AAA",
25249                Style);
25250 
25251   verifyFormat("namespace Averyveryveryverylongnamespace {\n"
25252                "int i;\n"
25253                "int j;\n"
25254                "} // namespace Averyveryveryverylongnamespace",
25255                "namespace Averyveryveryverylongnamespace {\n"
25256                "int i;\n"
25257                "int j;\n"
25258                "}",
25259                Style);
25260 
25261   verifyFormat(
25262       "namespace "
25263       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
25264       "    went::mad::now {\n"
25265       "int i;\n"
25266       "int j;\n"
25267       "} // namespace\n"
25268       "  // "
25269       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
25270       "went::mad::now",
25271       "namespace "
25272       "would::it::save::you::a::lot::of::time::if_::i::"
25273       "just::gave::up::and_::went::mad::now {\n"
25274       "int i;\n"
25275       "int j;\n"
25276       "}",
25277       Style);
25278 
25279   // This used to duplicate the comment again and again on subsequent runs
25280   verifyFormat(
25281       "namespace "
25282       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
25283       "    went::mad::now {\n"
25284       "int i;\n"
25285       "int j;\n"
25286       "} // namespace\n"
25287       "  // "
25288       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
25289       "went::mad::now",
25290       "namespace "
25291       "would::it::save::you::a::lot::of::time::if_::i::"
25292       "just::gave::up::and_::went::mad::now {\n"
25293       "int i;\n"
25294       "int j;\n"
25295       "} // namespace\n"
25296       "  // "
25297       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
25298       "and_::went::mad::now",
25299       Style);
25300 }
25301 
25302 TEST_F(FormatTest, LikelyUnlikely) {
25303   FormatStyle Style = getLLVMStyle();
25304 
25305   verifyFormat("if (argc > 5) [[unlikely]] {\n"
25306                "  return 29;\n"
25307                "}",
25308                Style);
25309 
25310   verifyFormat("if (argc > 5) [[likely]] {\n"
25311                "  return 29;\n"
25312                "}",
25313                Style);
25314 
25315   verifyFormat("if (argc > 5) [[unlikely]] {\n"
25316                "  return 29;\n"
25317                "} else [[likely]] {\n"
25318                "  return 42;\n"
25319                "}",
25320                Style);
25321 
25322   verifyFormat("if (argc > 5) [[unlikely]] {\n"
25323                "  return 29;\n"
25324                "} else if (argc > 10) [[likely]] {\n"
25325                "  return 99;\n"
25326                "} else {\n"
25327                "  return 42;\n"
25328                "}",
25329                Style);
25330 
25331   verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
25332                "  return 29;\n"
25333                "}",
25334                Style);
25335 
25336   verifyFormat("if (argc > 5) [[unlikely]]\n"
25337                "  return 29;",
25338                Style);
25339   verifyFormat("if (argc > 5) [[likely]]\n"
25340                "  return 29;",
25341                Style);
25342 
25343   verifyFormat("while (limit > 0) [[unlikely]] {\n"
25344                "  --limit;\n"
25345                "}",
25346                Style);
25347   verifyFormat("for (auto &limit : limits) [[likely]] {\n"
25348                "  --limit;\n"
25349                "}",
25350                Style);
25351 
25352   verifyFormat("for (auto &limit : limits) [[unlikely]]\n"
25353                "  --limit;",
25354                Style);
25355   verifyFormat("while (limit > 0) [[likely]]\n"
25356                "  --limit;",
25357                Style);
25358 
25359   Style.AttributeMacros.push_back("UNLIKELY");
25360   Style.AttributeMacros.push_back("LIKELY");
25361   verifyFormat("if (argc > 5) UNLIKELY\n"
25362                "  return 29;",
25363                Style);
25364 
25365   verifyFormat("if (argc > 5) UNLIKELY {\n"
25366                "  return 29;\n"
25367                "}",
25368                Style);
25369   verifyFormat("if (argc > 5) UNLIKELY {\n"
25370                "  return 29;\n"
25371                "} else [[likely]] {\n"
25372                "  return 42;\n"
25373                "}",
25374                Style);
25375   verifyFormat("if (argc > 5) UNLIKELY {\n"
25376                "  return 29;\n"
25377                "} else LIKELY {\n"
25378                "  return 42;\n"
25379                "}",
25380                Style);
25381   verifyFormat("if (argc > 5) [[unlikely]] {\n"
25382                "  return 29;\n"
25383                "} else LIKELY {\n"
25384                "  return 42;\n"
25385                "}",
25386                Style);
25387 
25388   verifyFormat("for (auto &limit : limits) UNLIKELY {\n"
25389                "  --limit;\n"
25390                "}",
25391                Style);
25392   verifyFormat("while (limit > 0) LIKELY {\n"
25393                "  --limit;\n"
25394                "}",
25395                Style);
25396 
25397   verifyFormat("while (limit > 0) UNLIKELY\n"
25398                "  --limit;",
25399                Style);
25400   verifyFormat("for (auto &limit : limits) LIKELY\n"
25401                "  --limit;",
25402                Style);
25403 }
25404 
25405 TEST_F(FormatTest, PenaltyIndentedWhitespace) {
25406   verifyFormat("Constructor()\n"
25407                "    : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
25408                "                          aaaa(aaaaaaaaaaaaaaaaaa, "
25409                "aaaaaaaaaaaaaaaaaat))");
25410   verifyFormat("Constructor()\n"
25411                "    : aaaaaaaaaaaaa(aaaaaa), "
25412                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
25413 
25414   FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
25415   StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
25416   verifyFormat("Constructor()\n"
25417                "    : aaaaaa(aaaaaa),\n"
25418                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
25419                "          aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
25420                StyleWithWhitespacePenalty);
25421   verifyFormat("Constructor()\n"
25422                "    : aaaaaaaaaaaaa(aaaaaa), "
25423                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
25424                StyleWithWhitespacePenalty);
25425 }
25426 
25427 TEST_F(FormatTest, LLVMDefaultStyle) {
25428   FormatStyle Style = getLLVMStyle();
25429   verifyFormat("extern \"C\" {\n"
25430                "int foo();\n"
25431                "}",
25432                Style);
25433 }
25434 TEST_F(FormatTest, GNUDefaultStyle) {
25435   FormatStyle Style = getGNUStyle();
25436   verifyFormat("extern \"C\"\n"
25437                "{\n"
25438                "  int foo ();\n"
25439                "}",
25440                Style);
25441 }
25442 TEST_F(FormatTest, MozillaDefaultStyle) {
25443   FormatStyle Style = getMozillaStyle();
25444   verifyFormat("extern \"C\"\n"
25445                "{\n"
25446                "  int foo();\n"
25447                "}",
25448                Style);
25449 }
25450 TEST_F(FormatTest, GoogleDefaultStyle) {
25451   FormatStyle Style = getGoogleStyle();
25452   verifyFormat("extern \"C\" {\n"
25453                "int foo();\n"
25454                "}",
25455                Style);
25456 }
25457 TEST_F(FormatTest, ChromiumDefaultStyle) {
25458   FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp);
25459   verifyFormat("extern \"C\" {\n"
25460                "int foo();\n"
25461                "}",
25462                Style);
25463 }
25464 TEST_F(FormatTest, MicrosoftDefaultStyle) {
25465   FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp);
25466   verifyFormat("extern \"C\"\n"
25467                "{\n"
25468                "    int foo();\n"
25469                "}",
25470                Style);
25471 }
25472 TEST_F(FormatTest, WebKitDefaultStyle) {
25473   FormatStyle Style = getWebKitStyle();
25474   verifyFormat("extern \"C\" {\n"
25475                "int foo();\n"
25476                "}",
25477                Style);
25478 }
25479 
25480 TEST_F(FormatTest, Concepts) {
25481   EXPECT_EQ(getLLVMStyle().BreakBeforeConceptDeclarations,
25482             FormatStyle::BBCDS_Always);
25483 
25484   // The default in LLVM style is REI_OuterScope, but these tests were written
25485   // when the default was REI_Keyword.
25486   FormatStyle Style = getLLVMStyle();
25487   Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
25488 
25489   verifyFormat("template <typename T>\n"
25490                "concept True = true;");
25491 
25492   verifyFormat("template <typename T>\n"
25493                "concept C = ((false || foo()) && C2<T>) ||\n"
25494                "            (std::trait<T>::value && Baz) || sizeof(T) >= 6;",
25495                getLLVMStyleWithColumns(60));
25496 
25497   verifyFormat("template <typename T>\n"
25498                "concept DelayedCheck = true && requires(T t) { t.bar(); } && "
25499                "sizeof(T) <= 8;");
25500 
25501   verifyFormat("template <typename T>\n"
25502                "concept DelayedCheck = true && requires(T t) {\n"
25503                "                                 t.bar();\n"
25504                "                                 t.baz();\n"
25505                "                               } && sizeof(T) <= 8;",
25506                Style);
25507 
25508   verifyFormat("template <typename T>\n"
25509                "concept DelayedCheck = true && requires(T t) { // Comment\n"
25510                "                                 t.bar();\n"
25511                "                                 t.baz();\n"
25512                "                               } && sizeof(T) <= 8;",
25513                Style);
25514 
25515   verifyFormat("template <typename T>\n"
25516                "concept DelayedCheck = false || requires(T t) { t.bar(); } && "
25517                "sizeof(T) <= 8;");
25518 
25519   verifyFormat("template <typename T>\n"
25520                "concept DelayedCheck = Unit<T> && !DerivedUnit<T>;");
25521 
25522   verifyFormat("template <typename T>\n"
25523                "concept DelayedCheck = Unit<T> && !(DerivedUnit<T>);");
25524 
25525   verifyFormat("template <typename T>\n"
25526                "concept DelayedCheck = Unit<T> && !!DerivedUnit<T>;");
25527 
25528   verifyFormat("template <typename T>\n"
25529                "concept DelayedCheck = !!false || requires(T t) { t.bar(); } "
25530                "&& sizeof(T) <= 8;");
25531 
25532   verifyFormat("template <typename T>\n"
25533                "concept DelayedCheck =\n"
25534                "    static_cast<bool>(0) || requires(T t) { t.bar(); } && "
25535                "sizeof(T) <= 8;");
25536 
25537   verifyFormat("template <typename T>\n"
25538                "concept DelayedCheck = bool(0) || requires(T t) { t.bar(); } "
25539                "&& sizeof(T) <= 8;");
25540 
25541   verifyFormat(
25542       "template <typename T>\n"
25543       "concept DelayedCheck =\n"
25544       "    (bool)(0) || requires(T t) { t.bar(); } && sizeof(T) <= 8;");
25545 
25546   verifyFormat("template <typename T>\n"
25547                "concept DelayedCheck = (bool)0 || requires(T t) { t.bar(); } "
25548                "&& sizeof(T) <= 8;");
25549 
25550   verifyFormat("template <typename T>\n"
25551                "concept Size = sizeof(T) >= 5 && requires(T t) { t.bar(); } && "
25552                "sizeof(T) <= 8;");
25553 
25554   verifyFormat("template <typename T>\n"
25555                "concept Size = 2 < 5 && 2 <= 5 && 8 >= 5 && 8 > 5 &&\n"
25556                "               requires(T t) {\n"
25557                "                 t.bar();\n"
25558                "                 t.baz();\n"
25559                "               } && sizeof(T) <= 8 && !(4 < 3);",
25560                getLLVMStyleWithColumns(60));
25561 
25562   verifyFormat("template <typename T>\n"
25563                "concept TrueOrNot = IsAlwaysTrue || IsNeverTrue;");
25564 
25565   verifyFormat("template <typename T>\n"
25566                "concept C = foo();");
25567 
25568   verifyFormat("template <typename T>\n"
25569                "concept C = foo(T());");
25570 
25571   verifyFormat("template <typename T>\n"
25572                "concept C = foo(T{});");
25573 
25574   verifyFormat("template <typename T>\n"
25575                "concept Size = V<sizeof(T)>::Value > 5;");
25576 
25577   verifyFormat("template <typename T>\n"
25578                "concept True = S<T>::Value;");
25579 
25580   verifyFormat("template <S T>\n"
25581                "concept True = T.field;");
25582 
25583   verifyFormat(
25584       "template <typename T>\n"
25585       "concept C = []() { return true; }() && requires(T t) { t.bar(); } &&\n"
25586       "            sizeof(T) <= 8;");
25587 
25588   // FIXME: This is misformatted because the fake l paren starts at bool, not at
25589   // the lambda l square.
25590   verifyFormat("template <typename T>\n"
25591                "concept C = [] -> bool { return true; }() && requires(T t) { "
25592                "t.bar(); } &&\n"
25593                "                      sizeof(T) <= 8;");
25594 
25595   verifyFormat(
25596       "template <typename T>\n"
25597       "concept C = decltype([]() { return std::true_type{}; }())::value &&\n"
25598       "            requires(T t) { t.bar(); } && sizeof(T) <= 8;");
25599 
25600   verifyFormat("template <typename T>\n"
25601                "concept C = decltype([]() { return std::true_type{}; "
25602                "}())::value && requires(T t) { t.bar(); } && sizeof(T) <= 8;",
25603                getLLVMStyleWithColumns(120));
25604 
25605   verifyFormat("template <typename T>\n"
25606                "concept C = decltype([]() -> std::true_type { return {}; "
25607                "}())::value &&\n"
25608                "            requires(T t) { t.bar(); } && sizeof(T) <= 8;");
25609 
25610   verifyFormat("template <typename T>\n"
25611                "concept C = true;\n"
25612                "Foo Bar;");
25613 
25614   verifyFormat("template <typename T>\n"
25615                "concept Hashable = requires(T a) {\n"
25616                "                     { std::hash<T>{}(a) } -> "
25617                "std::convertible_to<std::size_t>;\n"
25618                "                   };",
25619                Style);
25620 
25621   verifyFormat(
25622       "template <typename T>\n"
25623       "concept EqualityComparable = requires(T a, T b) {\n"
25624       "                               { a == b } -> std::same_as<bool>;\n"
25625       "                             };",
25626       Style);
25627 
25628   verifyFormat(
25629       "template <typename T>\n"
25630       "concept EqualityComparable = requires(T a, T b) {\n"
25631       "                               { a == b } -> std::same_as<bool>;\n"
25632       "                               { a != b } -> std::same_as<bool>;\n"
25633       "                             };",
25634       Style);
25635 
25636   verifyFormat("template <typename T>\n"
25637                "concept WeakEqualityComparable = requires(T a, T b) {\n"
25638                "                                   { a == b };\n"
25639                "                                   { a != b };\n"
25640                "                                 };",
25641                Style);
25642 
25643   verifyFormat("template <typename T>\n"
25644                "concept HasSizeT = requires { typename T::size_t; };");
25645 
25646   verifyFormat("template <typename T>\n"
25647                "concept Semiregular =\n"
25648                "    DefaultConstructible<T> && CopyConstructible<T> && "
25649                "CopyAssignable<T> &&\n"
25650                "    requires(T a, std::size_t n) {\n"
25651                "      requires Same<T *, decltype(&a)>;\n"
25652                "      { a.~T() } noexcept;\n"
25653                "      requires Same<T *, decltype(new T)>;\n"
25654                "      requires Same<T *, decltype(new T[n])>;\n"
25655                "      { delete new T; };\n"
25656                "      { delete new T[n]; };\n"
25657                "    };",
25658                Style);
25659 
25660   verifyFormat("template <typename T>\n"
25661                "concept Semiregular =\n"
25662                "    requires(T a, std::size_t n) {\n"
25663                "      requires Same<T *, decltype(&a)>;\n"
25664                "      { a.~T() } noexcept;\n"
25665                "      requires Same<T *, decltype(new T)>;\n"
25666                "      requires Same<T *, decltype(new T[n])>;\n"
25667                "      { delete new T; };\n"
25668                "      { delete new T[n]; };\n"
25669                "      { new T } -> std::same_as<T *>;\n"
25670                "    } && DefaultConstructible<T> && CopyConstructible<T> && "
25671                "CopyAssignable<T>;",
25672                Style);
25673 
25674   verifyFormat(
25675       "template <typename T>\n"
25676       "concept Semiregular =\n"
25677       "    DefaultConstructible<T> && requires(T a, std::size_t n) {\n"
25678       "                                 requires Same<T *, decltype(&a)>;\n"
25679       "                                 { a.~T() } noexcept;\n"
25680       "                                 requires Same<T *, decltype(new T)>;\n"
25681       "                                 requires Same<T *, decltype(new "
25682       "T[n])>;\n"
25683       "                                 { delete new T; };\n"
25684       "                                 { delete new T[n]; };\n"
25685       "                               } && CopyConstructible<T> && "
25686       "CopyAssignable<T>;",
25687       Style);
25688 
25689   verifyFormat("template <typename T>\n"
25690                "concept Two = requires(T t) {\n"
25691                "                { t.foo() } -> std::same_as<Bar>;\n"
25692                "              } && requires(T &&t) {\n"
25693                "                     { t.foo() } -> std::same_as<Bar &&>;\n"
25694                "                   };",
25695                Style);
25696 
25697   verifyFormat(
25698       "template <typename T>\n"
25699       "concept C = requires(T x) {\n"
25700       "              { *x } -> std::convertible_to<typename T::inner>;\n"
25701       "              { x + 1 } noexcept -> std::same_as<int>;\n"
25702       "              { x * 1 } -> std::convertible_to<T>;\n"
25703       "            };",
25704       Style);
25705 
25706   verifyFormat("template <typename T>\n"
25707                "concept C = requires(T x) {\n"
25708                "              {\n"
25709                "                long_long_long_function_call(1, 2, 3, 4, 5)\n"
25710                "              } -> long_long_concept_name<T>;\n"
25711                "              {\n"
25712                "                long_long_long_function_call(1, 2, 3, 4, 5)\n"
25713                "              } noexcept -> long_long_concept_name<T>;\n"
25714                "            };",
25715                Style);
25716 
25717   verifyFormat(
25718       "template <typename T, typename U = T>\n"
25719       "concept Swappable = requires(T &&t, U &&u) {\n"
25720       "                      swap(std::forward<T>(t), std::forward<U>(u));\n"
25721       "                      swap(std::forward<U>(u), std::forward<T>(t));\n"
25722       "                    };",
25723       Style);
25724 
25725   verifyFormat("template <typename T, typename U>\n"
25726                "concept Common = requires(T &&t, U &&u) {\n"
25727                "                   typename CommonType<T, U>;\n"
25728                "                   { CommonType<T, U>(std::forward<T>(t)) };\n"
25729                "                 };",
25730                Style);
25731 
25732   verifyFormat("template <typename T, typename U>\n"
25733                "concept Common = requires(T &&t, U &&u) {\n"
25734                "                   typename CommonType<T, U>;\n"
25735                "                   { CommonType<T, U>{std::forward<T>(t)} };\n"
25736                "                 };",
25737                Style);
25738 
25739   verifyFormat(
25740       "template <typename T>\n"
25741       "concept C = requires(T t) {\n"
25742       "              requires Bar<T> && Foo<T>;\n"
25743       "              requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
25744       "            };",
25745       Style);
25746 
25747   verifyFormat("template <typename T>\n"
25748                "concept HasFoo = requires(T t) {\n"
25749                "                   { t.foo() };\n"
25750                "                   t.foo();\n"
25751                "                 };\n"
25752                "template <typename T>\n"
25753                "concept HasBar = requires(T t) {\n"
25754                "                   { t.bar() };\n"
25755                "                   t.bar();\n"
25756                "                 };",
25757                Style);
25758 
25759   verifyFormat("template <typename T>\n"
25760                "concept Large = sizeof(T) > 10;");
25761 
25762   verifyFormat("template <typename T, typename U>\n"
25763                "concept FooableWith = requires(T t, U u) {\n"
25764                "                        typename T::foo_type;\n"
25765                "                        { t.foo(u) } -> typename T::foo_type;\n"
25766                "                        t++;\n"
25767                "                      };\n"
25768                "void doFoo(FooableWith<int> auto t) { t.foo(3); }",
25769                Style);
25770 
25771   verifyFormat("template <typename T>\n"
25772                "concept Context = is_specialization_of_v<context, T>;");
25773 
25774   verifyFormat("template <typename T>\n"
25775                "concept Node = std::is_object_v<T>;");
25776 
25777   verifyFormat("template <class T>\n"
25778                "concept integral = __is_integral(T);");
25779 
25780   verifyFormat("template <class T>\n"
25781                "concept is2D = __array_extent(T, 1) == 2;");
25782 
25783   verifyFormat("template <class T>\n"
25784                "concept isRhs = __is_rvalue_expr(std::declval<T>() + 2)");
25785 
25786   verifyFormat("template <class T, class T2>\n"
25787                "concept Same = __is_same_as<T, T2>;");
25788 
25789   verifyFormat(
25790       "template <class _InIt, class _OutIt>\n"
25791       "concept _Can_reread_dest =\n"
25792       "    std::forward_iterator<_OutIt> &&\n"
25793       "    std::same_as<std::iter_value_t<_InIt>, std::iter_value_t<_OutIt>>;");
25794 
25795   Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Allowed;
25796 
25797   verifyFormat(
25798       "template <typename T>\n"
25799       "concept C = requires(T t) {\n"
25800       "              requires Bar<T> && Foo<T>;\n"
25801       "              requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
25802       "            };",
25803       Style);
25804 
25805   verifyFormat("template <typename T>\n"
25806                "concept HasFoo = requires(T t) {\n"
25807                "                   { t.foo() };\n"
25808                "                   t.foo();\n"
25809                "                 };\n"
25810                "template <typename T>\n"
25811                "concept HasBar = requires(T t) {\n"
25812                "                   { t.bar() };\n"
25813                "                   t.bar();\n"
25814                "                 };",
25815                Style);
25816 
25817   verifyFormat("template <typename T> concept True = true;", Style);
25818 
25819   verifyFormat("template <typename T>\n"
25820                "concept C = decltype([]() -> std::true_type { return {}; "
25821                "}())::value &&\n"
25822                "            requires(T t) { t.bar(); } && sizeof(T) <= 8;",
25823                Style);
25824 
25825   verifyFormat("template <typename T>\n"
25826                "concept Semiregular =\n"
25827                "    DefaultConstructible<T> && CopyConstructible<T> && "
25828                "CopyAssignable<T> &&\n"
25829                "    requires(T a, std::size_t n) {\n"
25830                "      requires Same<T *, decltype(&a)>;\n"
25831                "      { a.~T() } noexcept;\n"
25832                "      requires Same<T *, decltype(new T)>;\n"
25833                "      requires Same<T *, decltype(new T[n])>;\n"
25834                "      { delete new T; };\n"
25835                "      { delete new T[n]; };\n"
25836                "    };",
25837                Style);
25838 
25839   Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Never;
25840 
25841   verifyFormat("template <typename T> concept C =\n"
25842                "    requires(T t) {\n"
25843                "      requires Bar<T> && Foo<T>;\n"
25844                "      requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
25845                "    };",
25846                Style);
25847 
25848   verifyFormat("template <typename T> concept HasFoo = requires(T t) {\n"
25849                "                                         { t.foo() };\n"
25850                "                                         t.foo();\n"
25851                "                                       };\n"
25852                "template <typename T> concept HasBar = requires(T t) {\n"
25853                "                                         { t.bar() };\n"
25854                "                                         t.bar();\n"
25855                "                                       };",
25856                Style);
25857 
25858   verifyFormat("template <typename T> concept True = true;", Style);
25859 
25860   verifyFormat(
25861       "template <typename T> concept C =\n"
25862       "    decltype([]() -> std::true_type { return {}; }())::value &&\n"
25863       "    requires(T t) { t.bar(); } && sizeof(T) <= 8;",
25864       Style);
25865 
25866   verifyFormat("template <typename T> concept Semiregular =\n"
25867                "    DefaultConstructible<T> && CopyConstructible<T> && "
25868                "CopyAssignable<T> &&\n"
25869                "    requires(T a, std::size_t n) {\n"
25870                "      requires Same<T *, decltype(&a)>;\n"
25871                "      { a.~T() } noexcept;\n"
25872                "      requires Same<T *, decltype(new T)>;\n"
25873                "      requires Same<T *, decltype(new T[n])>;\n"
25874                "      { delete new T; };\n"
25875                "      { delete new T[n]; };\n"
25876                "    };",
25877                Style);
25878 
25879   // The following tests are invalid C++, we just want to make sure we don't
25880   // assert.
25881   verifyNoCrash("template <typename T>\n"
25882                 "concept C = requires C2<T>;");
25883 
25884   verifyNoCrash("template <typename T>\n"
25885                 "concept C = 5 + 4;");
25886 
25887   verifyNoCrash("template <typename T>\n"
25888                 "concept C = class X;");
25889 
25890   verifyNoCrash("template <typename T>\n"
25891                 "concept C = [] && true;");
25892 
25893   verifyNoCrash("template <typename T>\n"
25894                 "concept C = [] && requires(T t) { typename T::size_type; };");
25895 }
25896 
25897 TEST_F(FormatTest, RequiresClausesPositions) {
25898   auto Style = getLLVMStyle();
25899   EXPECT_EQ(Style.RequiresClausePosition, FormatStyle::RCPS_OwnLine);
25900   EXPECT_EQ(Style.IndentRequiresClause, true);
25901 
25902   // The default in LLVM style is REI_OuterScope, but these tests were written
25903   // when the default was REI_Keyword.
25904   Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
25905 
25906   verifyFormat("template <typename T>\n"
25907                "  requires(Foo<T> && std::trait<T>)\n"
25908                "struct Bar;",
25909                Style);
25910 
25911   verifyFormat("template <typename T>\n"
25912                "  requires(Foo<T> && std::trait<T>)\n"
25913                "class Bar {\n"
25914                "public:\n"
25915                "  Bar(T t);\n"
25916                "  bool baz();\n"
25917                "};",
25918                Style);
25919 
25920   verifyFormat(
25921       "template <typename T>\n"
25922       "  requires requires(T &&t) {\n"
25923       "             typename T::I;\n"
25924       "             requires(F<typename T::I> && std::trait<typename T::I>);\n"
25925       "           }\n"
25926       "Bar(T) -> Bar<typename T::I>;",
25927       Style);
25928 
25929   verifyFormat("template <typename T>\n"
25930                "  requires(Foo<T> && std::trait<T>)\n"
25931                "constexpr T MyGlobal;",
25932                Style);
25933 
25934   verifyFormat("template <typename T>\n"
25935                "  requires Foo<T> && requires(T t) {\n"
25936                "                       { t.baz() } -> std::same_as<bool>;\n"
25937                "                       requires std::same_as<T::Factor, int>;\n"
25938                "                     }\n"
25939                "inline int bar(T t) {\n"
25940                "  return t.baz() ? T::Factor : 5;\n"
25941                "}",
25942                Style);
25943 
25944   verifyFormat("template <typename T>\n"
25945                "inline int bar(T t)\n"
25946                "  requires Foo<T> && requires(T t) {\n"
25947                "                       { t.baz() } -> std::same_as<bool>;\n"
25948                "                       requires std::same_as<T::Factor, int>;\n"
25949                "                     }\n"
25950                "{\n"
25951                "  return t.baz() ? T::Factor : 5;\n"
25952                "}",
25953                Style);
25954 
25955   verifyFormat("template <typename T>\n"
25956                "  requires F<T>\n"
25957                "int bar(T t) {\n"
25958                "  return 5;\n"
25959                "}",
25960                Style);
25961 
25962   verifyFormat("template <typename T>\n"
25963                "int bar(T t)\n"
25964                "  requires F<T>\n"
25965                "{\n"
25966                "  return 5;\n"
25967                "}",
25968                Style);
25969 
25970   verifyFormat("template <typename T>\n"
25971                "int S::bar(T t) &&\n"
25972                "  requires F<T>\n"
25973                "{\n"
25974                "  return 5;\n"
25975                "}",
25976                Style);
25977 
25978   verifyFormat("template <typename T>\n"
25979                "int bar(T t)\n"
25980                "  requires F<T>;",
25981                Style);
25982 
25983   Style.IndentRequiresClause = false;
25984   verifyFormat("template <typename T>\n"
25985                "requires F<T>\n"
25986                "int bar(T t) {\n"
25987                "  return 5;\n"
25988                "}",
25989                Style);
25990 
25991   verifyFormat("template <typename T>\n"
25992                "int S::bar(T t) &&\n"
25993                "requires F<T>\n"
25994                "{\n"
25995                "  return 5;\n"
25996                "}",
25997                Style);
25998 
25999   verifyFormat("template <typename T>\n"
26000                "int bar(T t)\n"
26001                "requires F<T>\n"
26002                "{\n"
26003                "  return 5;\n"
26004                "}",
26005                Style);
26006 
26007   Style.RequiresClausePosition = FormatStyle::RCPS_OwnLineWithBrace;
26008   Style.IndentRequiresClause = true;
26009 
26010   verifyFormat("template <typename T>\n"
26011                "  requires(Foo<T> && std::trait<T>)\n"
26012                "struct Bar;",
26013                Style);
26014 
26015   verifyFormat("template <typename T>\n"
26016                "  requires(Foo<T> && std::trait<T>)\n"
26017                "class Bar {\n"
26018                "public:\n"
26019                "  Bar(T t);\n"
26020                "  bool baz();\n"
26021                "};",
26022                Style);
26023 
26024   verifyFormat(
26025       "template <typename T>\n"
26026       "  requires requires(T &&t) {\n"
26027       "             typename T::I;\n"
26028       "             requires(F<typename T::I> && std::trait<typename T::I>);\n"
26029       "           }\n"
26030       "Bar(T) -> Bar<typename T::I>;",
26031       Style);
26032 
26033   verifyFormat("template <typename T>\n"
26034                "  requires(Foo<T> && std::trait<T>)\n"
26035                "constexpr T MyGlobal;",
26036                Style);
26037 
26038   verifyFormat("template <typename T>\n"
26039                "  requires Foo<T> && requires(T t) {\n"
26040                "                       { t.baz() } -> std::same_as<bool>;\n"
26041                "                       requires std::same_as<T::Factor, int>;\n"
26042                "                     }\n"
26043                "inline int bar(T t) {\n"
26044                "  return t.baz() ? T::Factor : 5;\n"
26045                "}",
26046                Style);
26047 
26048   verifyFormat("template <typename T>\n"
26049                "inline int bar(T t)\n"
26050                "  requires Foo<T> && requires(T t) {\n"
26051                "                       { t.baz() } -> std::same_as<bool>;\n"
26052                "                       requires std::same_as<T::Factor, int>;\n"
26053                "                     } {\n"
26054                "  return t.baz() ? T::Factor : 5;\n"
26055                "}",
26056                Style);
26057 
26058   verifyFormat("template <typename T>\n"
26059                "  requires F<T>\n"
26060                "int bar(T t) {\n"
26061                "  return 5;\n"
26062                "}",
26063                Style);
26064 
26065   verifyFormat("template <typename T>\n"
26066                "int bar(T t)\n"
26067                "  requires F<T> {\n"
26068                "  return 5;\n"
26069                "}",
26070                Style);
26071 
26072   verifyFormat("template <typename T>\n"
26073                "int S::bar(T t) &&\n"
26074                "  requires F<T> {\n"
26075                "  return 5;\n"
26076                "}",
26077                Style);
26078 
26079   verifyFormat("template <typename T>\n"
26080                "int bar(T t)\n"
26081                "  requires F<T>;",
26082                Style);
26083 
26084   verifyFormat("template <typename T>\n"
26085                "int bar(T t)\n"
26086                "  requires F<T> {}",
26087                Style);
26088 
26089   Style.RequiresClausePosition = FormatStyle::RCPS_SingleLine;
26090   Style.IndentRequiresClause = false;
26091   verifyFormat("template <typename T> requires Foo<T> struct Bar {};\n"
26092                "template <typename T> requires Foo<T> void bar() {}\n"
26093                "template <typename T> void bar() requires Foo<T> {}\n"
26094                "template <typename T> void bar() requires Foo<T>;\n"
26095                "template <typename T> void S::bar() && requires Foo<T> {}\n"
26096                "template <typename T> requires Foo<T> Bar(T) -> Bar<T>;",
26097                Style);
26098 
26099   auto ColumnStyle = Style;
26100   ColumnStyle.ColumnLimit = 40;
26101   verifyFormat("template <typename AAAAAAA>\n"
26102                "requires Foo<T> struct Bar {};\n"
26103                "template <typename AAAAAAA>\n"
26104                "requires Foo<T> void bar() {}\n"
26105                "template <typename AAAAAAA>\n"
26106                "void bar() requires Foo<T> {}\n"
26107                "template <typename T>\n"
26108                "void S::bar() && requires Foo<T> {}\n"
26109                "template <typename AAAAAAA>\n"
26110                "requires Foo<T> Baz(T) -> Baz<T>;",
26111                ColumnStyle);
26112 
26113   verifyFormat("template <typename T>\n"
26114                "requires Foo<AAAAAAA> struct Bar {};\n"
26115                "template <typename T>\n"
26116                "requires Foo<AAAAAAA> void bar() {}\n"
26117                "template <typename T>\n"
26118                "void bar() requires Foo<AAAAAAA> {}\n"
26119                "template <typename T>\n"
26120                "requires Foo<AAAAAAA> Bar(T) -> Bar<T>;",
26121                ColumnStyle);
26122 
26123   verifyFormat("template <typename AAAAAAA>\n"
26124                "requires Foo<AAAAAAAAAAAAAAAA>\n"
26125                "struct Bar {};\n"
26126                "template <typename AAAAAAA>\n"
26127                "requires Foo<AAAAAAAAAAAAAAAA>\n"
26128                "void bar() {}\n"
26129                "template <typename AAAAAAA>\n"
26130                "void bar()\n"
26131                "    requires Foo<AAAAAAAAAAAAAAAA> {}\n"
26132                "template <typename AAAAAAA>\n"
26133                "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
26134                "template <typename AAAAAAA>\n"
26135                "requires Foo<AAAAAAAAAAAAAAAA>\n"
26136                "Bar(T) -> Bar<T>;",
26137                ColumnStyle);
26138 
26139   Style.RequiresClausePosition = FormatStyle::RCPS_WithFollowing;
26140   ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithFollowing;
26141 
26142   verifyFormat("template <typename T>\n"
26143                "requires Foo<T> struct Bar {};\n"
26144                "template <typename T>\n"
26145                "requires Foo<T> void bar() {}\n"
26146                "template <typename T>\n"
26147                "void bar()\n"
26148                "requires Foo<T> {}\n"
26149                "template <typename T>\n"
26150                "void bar()\n"
26151                "requires Foo<T>;\n"
26152                "template <typename T>\n"
26153                "void S::bar() &&\n"
26154                "requires Foo<T> {}\n"
26155                "template <typename T>\n"
26156                "requires Foo<T> Bar(T) -> Bar<T>;",
26157                Style);
26158 
26159   verifyFormat("template <typename AAAAAAA>\n"
26160                "requires Foo<AAAAAAAAAAAAAAAA>\n"
26161                "struct Bar {};\n"
26162                "template <typename AAAAAAA>\n"
26163                "requires Foo<AAAAAAAAAAAAAAAA>\n"
26164                "void bar() {}\n"
26165                "template <typename AAAAAAA>\n"
26166                "void bar()\n"
26167                "requires Foo<AAAAAAAAAAAAAAAA> {}\n"
26168                "template <typename AAAAAAA>\n"
26169                "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
26170                "template <typename AAAAAAA>\n"
26171                "requires Foo<AAAAAAAAAAAAAAAA>\n"
26172                "Bar(T) -> Bar<T>;",
26173                ColumnStyle);
26174 
26175   Style.IndentRequiresClause = true;
26176   ColumnStyle.IndentRequiresClause = true;
26177 
26178   verifyFormat("template <typename T>\n"
26179                "  requires Foo<T> struct Bar {};\n"
26180                "template <typename T>\n"
26181                "  requires Foo<T> void bar() {}\n"
26182                "template <typename T>\n"
26183                "void bar()\n"
26184                "  requires Foo<T> {}\n"
26185                "template <typename T>\n"
26186                "void S::bar() &&\n"
26187                "  requires Foo<T> {}\n"
26188                "template <typename T>\n"
26189                "  requires Foo<T> Bar(T) -> Bar<T>;",
26190                Style);
26191 
26192   verifyFormat("template <typename AAAAAAA>\n"
26193                "  requires Foo<AAAAAAAAAAAAAAAA>\n"
26194                "struct Bar {};\n"
26195                "template <typename AAAAAAA>\n"
26196                "  requires Foo<AAAAAAAAAAAAAAAA>\n"
26197                "void bar() {}\n"
26198                "template <typename AAAAAAA>\n"
26199                "void bar()\n"
26200                "  requires Foo<AAAAAAAAAAAAAAAA> {}\n"
26201                "template <typename AAAAAAA>\n"
26202                "  requires Foo<AAAAAA> Bar(T) -> Bar<T>;\n"
26203                "template <typename AAAAAAA>\n"
26204                "  requires Foo<AAAAAAAAAAAAAAAA>\n"
26205                "Bar(T) -> Bar<T>;",
26206                ColumnStyle);
26207 
26208   Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
26209   ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
26210 
26211   verifyFormat("template <typename T> requires Foo<T>\n"
26212                "struct Bar {};\n"
26213                "template <typename T> requires Foo<T>\n"
26214                "void bar() {}\n"
26215                "template <typename T>\n"
26216                "void bar() requires Foo<T>\n"
26217                "{}\n"
26218                "template <typename T> void bar() requires Foo<T>;\n"
26219                "template <typename T>\n"
26220                "void S::bar() && requires Foo<T>\n"
26221                "{}\n"
26222                "template <typename T> requires Foo<T>\n"
26223                "Bar(T) -> Bar<T>;",
26224                Style);
26225 
26226   verifyFormat("template <typename AAAAAAA>\n"
26227                "requires Foo<AAAAAAAAAAAAAAAA>\n"
26228                "struct Bar {};\n"
26229                "template <typename AAAAAAA>\n"
26230                "requires Foo<AAAAAAAAAAAAAAAA>\n"
26231                "void bar() {}\n"
26232                "template <typename AAAAAAA>\n"
26233                "void bar()\n"
26234                "    requires Foo<AAAAAAAAAAAAAAAA>\n"
26235                "{}\n"
26236                "template <typename AAAAAAA>\n"
26237                "requires Foo<AAAAAAAA>\n"
26238                "Bar(T) -> Bar<T>;\n"
26239                "template <typename AAAAAAA>\n"
26240                "requires Foo<AAAAAAAAAAAAAAAA>\n"
26241                "Bar(T) -> Bar<T>;",
26242                ColumnStyle);
26243 }
26244 
26245 TEST_F(FormatTest, RequiresClauses) {
26246   verifyFormat("struct [[nodiscard]] zero_t {\n"
26247                "  template <class T>\n"
26248                "    requires requires { number_zero_v<T>; }\n"
26249                "  [[nodiscard]] constexpr operator T() const {\n"
26250                "    return number_zero_v<T>;\n"
26251                "  }\n"
26252                "};");
26253 
26254   verifyFormat("template <class T>\n"
26255                "  requires(std::same_as<int, T>)\n"
26256                "decltype(auto) fun() {}");
26257 
26258   auto Style = getLLVMStyle();
26259 
26260   verifyFormat(
26261       "template <typename T>\n"
26262       "  requires is_default_constructible_v<hash<T>> and\n"
26263       "           is_copy_constructible_v<hash<T>> and\n"
26264       "           is_move_constructible_v<hash<T>> and\n"
26265       "           is_copy_assignable_v<hash<T>> and "
26266       "is_move_assignable_v<hash<T>> and\n"
26267       "           is_destructible_v<hash<T>> and is_swappable_v<hash<T>> and\n"
26268       "           is_callable_v<hash<T>(T)> and\n"
26269       "           is_same_v<size_t, decltype(hash<T>(declval<T>()))> and\n"
26270       "           is_same_v<size_t, decltype(hash<T>(declval<T &>()))> and\n"
26271       "           is_same_v<size_t, decltype(hash<T>(declval<const T &>()))>\n"
26272       "struct S {};",
26273       Style);
26274 
26275   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
26276   verifyFormat(
26277       "template <typename T>\n"
26278       "  requires is_default_constructible_v<hash<T>>\n"
26279       "           and is_copy_constructible_v<hash<T>>\n"
26280       "           and is_move_constructible_v<hash<T>>\n"
26281       "           and is_copy_assignable_v<hash<T>> and "
26282       "is_move_assignable_v<hash<T>>\n"
26283       "           and is_destructible_v<hash<T>> and is_swappable_v<hash<T>>\n"
26284       "           and is_callable_v<hash<T>(T)>\n"
26285       "           and is_same_v<size_t, decltype(hash<T>(declval<T>()))>\n"
26286       "           and is_same_v<size_t, decltype(hash<T>(declval<T &>()))>\n"
26287       "           and is_same_v<size_t, decltype(hash<T>(declval<const T "
26288       "&>()))>\n"
26289       "struct S {};",
26290       Style);
26291 
26292   Style = getLLVMStyle();
26293   Style.ConstructorInitializerIndentWidth = 4;
26294   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
26295   Style.PackConstructorInitializers = FormatStyle::PCIS_Never;
26296   verifyFormat("constexpr Foo(Foo const &other)\n"
26297                "  requires std::is_copy_constructible<T>\n"
26298                "    : value{other.value} {\n"
26299                "  do_magic();\n"
26300                "  do_more_magic();\n"
26301                "}",
26302                Style);
26303 
26304   // Not a clause, but we once hit an assert.
26305   verifyFormat("#if 0\n"
26306                "#else\n"
26307                "foo();\n"
26308                "#endif\n"
26309                "bar(requires);");
26310 }
26311 
26312 TEST_F(FormatTest, RequiresExpressionIndentation) {
26313   auto Style = getLLVMStyle();
26314   EXPECT_EQ(Style.RequiresExpressionIndentation, FormatStyle::REI_OuterScope);
26315 
26316   verifyFormat("template <typename T>\n"
26317                "concept C = requires(T t) {\n"
26318                "  typename T::value;\n"
26319                "  requires requires(typename T::value v) {\n"
26320                "    { t == v } -> std::same_as<bool>;\n"
26321                "  };\n"
26322                "};",
26323                Style);
26324 
26325   verifyFormat("template <typename T>\n"
26326                "void bar(T)\n"
26327                "  requires Foo<T> && requires(T t) {\n"
26328                "    { t.foo() } -> std::same_as<int>;\n"
26329                "  } && requires(T t) {\n"
26330                "    { t.bar() } -> std::same_as<bool>;\n"
26331                "    --t;\n"
26332                "  };",
26333                Style);
26334 
26335   verifyFormat("template <typename T>\n"
26336                "  requires Foo<T> &&\n"
26337                "           requires(T t) {\n"
26338                "             { t.foo() } -> std::same_as<int>;\n"
26339                "           } && requires(T t) {\n"
26340                "             { t.bar() } -> std::same_as<bool>;\n"
26341                "             --t;\n"
26342                "           }\n"
26343                "void bar(T);",
26344                Style);
26345 
26346   verifyFormat("template <typename T> void f() {\n"
26347                "  if constexpr (requires(T t) {\n"
26348                "                  { t.bar() } -> std::same_as<bool>;\n"
26349                "                }) {\n"
26350                "  }\n"
26351                "}",
26352                Style);
26353 
26354   verifyFormat("template <typename T> void f() {\n"
26355                "  if constexpr (condition && requires(T t) {\n"
26356                "                  { t.bar() } -> std::same_as<bool>;\n"
26357                "                }) {\n"
26358                "  }\n"
26359                "}",
26360                Style);
26361 
26362   verifyFormat("template <typename T> struct C {\n"
26363                "  void f()\n"
26364                "    requires requires(T t) {\n"
26365                "      { t.bar() } -> std::same_as<bool>;\n"
26366                "    };\n"
26367                "};",
26368                Style);
26369 
26370   Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
26371 
26372   verifyFormat("template <typename T>\n"
26373                "concept C = requires(T t) {\n"
26374                "              typename T::value;\n"
26375                "              requires requires(typename T::value v) {\n"
26376                "                         { t == v } -> std::same_as<bool>;\n"
26377                "                       };\n"
26378                "            };",
26379                Style);
26380 
26381   verifyFormat(
26382       "template <typename T>\n"
26383       "void bar(T)\n"
26384       "  requires Foo<T> && requires(T t) {\n"
26385       "                       { t.foo() } -> std::same_as<int>;\n"
26386       "                     } && requires(T t) {\n"
26387       "                            { t.bar() } -> std::same_as<bool>;\n"
26388       "                            --t;\n"
26389       "                          };",
26390       Style);
26391 
26392   verifyFormat("template <typename T>\n"
26393                "  requires Foo<T> &&\n"
26394                "           requires(T t) {\n"
26395                "             { t.foo() } -> std::same_as<int>;\n"
26396                "           } && requires(T t) {\n"
26397                "                  { t.bar() } -> std::same_as<bool>;\n"
26398                "                  --t;\n"
26399                "                }\n"
26400                "void bar(T);",
26401                Style);
26402 
26403   verifyFormat("template <typename T> void f() {\n"
26404                "  if constexpr (requires(T t) {\n"
26405                "                  { t.bar() } -> std::same_as<bool>;\n"
26406                "                }) {\n"
26407                "  }\n"
26408                "}",
26409                Style);
26410 
26411   verifyFormat(
26412       "template <typename T> void f() {\n"
26413       "  if constexpr (condition && requires(T t) {\n"
26414       "                               { t.bar() } -> std::same_as<bool>;\n"
26415       "                             }) {\n"
26416       "  }\n"
26417       "}",
26418       Style);
26419 
26420   verifyFormat("template <typename T> struct C {\n"
26421                "  void f()\n"
26422                "    requires requires(T t) {\n"
26423                "               { t.bar() } -> std::same_as<bool>;\n"
26424                "             };\n"
26425                "};",
26426                Style);
26427 }
26428 
26429 TEST_F(FormatTest, StatementAttributeLikeMacros) {
26430   FormatStyle Style = getLLVMStyle();
26431   StringRef Source = "void Foo::slot() {\n"
26432                      "  unsigned char MyChar = 'x';\n"
26433                      "  emit signal(MyChar);\n"
26434                      "  Q_EMIT signal(MyChar);\n"
26435                      "}";
26436 
26437   verifyFormat(Source, Style);
26438 
26439   Style.AlignConsecutiveDeclarations.Enabled = true;
26440   verifyFormat("void Foo::slot() {\n"
26441                "  unsigned char MyChar = 'x';\n"
26442                "  emit          signal(MyChar);\n"
26443                "  Q_EMIT signal(MyChar);\n"
26444                "}",
26445                Source, Style);
26446 
26447   Style.StatementAttributeLikeMacros.push_back("emit");
26448   verifyFormat(Source, Style);
26449 
26450   Style.StatementAttributeLikeMacros = {};
26451   verifyFormat("void Foo::slot() {\n"
26452                "  unsigned char MyChar = 'x';\n"
26453                "  emit          signal(MyChar);\n"
26454                "  Q_EMIT        signal(MyChar);\n"
26455                "}",
26456                Source, Style);
26457 }
26458 
26459 TEST_F(FormatTest, IndentAccessModifiers) {
26460   FormatStyle Style = getLLVMStyle();
26461   Style.IndentAccessModifiers = true;
26462   // Members are *two* levels below the record;
26463   // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
26464   verifyFormat("class C {\n"
26465                "    int i;\n"
26466                "};",
26467                Style);
26468   verifyFormat("union C {\n"
26469                "    int i;\n"
26470                "    unsigned u;\n"
26471                "};",
26472                Style);
26473   // Access modifiers should be indented one level below the record.
26474   verifyFormat("class C {\n"
26475                "  public:\n"
26476                "    int i;\n"
26477                "};",
26478                Style);
26479   verifyFormat("class C {\n"
26480                "  public /* comment */:\n"
26481                "    int i;\n"
26482                "};",
26483                Style);
26484   verifyFormat("struct S {\n"
26485                "  private:\n"
26486                "    class C {\n"
26487                "        int j;\n"
26488                "\n"
26489                "      public:\n"
26490                "        C();\n"
26491                "    };\n"
26492                "\n"
26493                "  public:\n"
26494                "    int i;\n"
26495                "};",
26496                Style);
26497   // Enumerations are not records and should be unaffected.
26498   Style.AllowShortEnumsOnASingleLine = false;
26499   verifyFormat("enum class E {\n"
26500                "  A,\n"
26501                "  B\n"
26502                "};",
26503                Style);
26504   // Test with a different indentation width;
26505   // also proves that the result is Style.AccessModifierOffset agnostic.
26506   Style.IndentWidth = 3;
26507   verifyFormat("class C {\n"
26508                "   public:\n"
26509                "      int i;\n"
26510                "};",
26511                Style);
26512   verifyFormat("class C {\n"
26513                "   public /**/:\n"
26514                "      int i;\n"
26515                "};",
26516                Style);
26517   Style.AttributeMacros.push_back("FOO");
26518   verifyFormat("class C {\n"
26519                "   FOO public:\n"
26520                "      int i;\n"
26521                "};",
26522                Style);
26523 }
26524 
26525 TEST_F(FormatTest, LimitlessStringsAndComments) {
26526   auto Style = getLLVMStyleWithColumns(0);
26527   constexpr StringRef Code =
26528       "/**\n"
26529       " * This is a multiline comment with quite some long lines, at least for "
26530       "the LLVM Style.\n"
26531       " * We will redo this with strings and line comments. Just to  check if "
26532       "everything is working.\n"
26533       " */\n"
26534       "bool foo() {\n"
26535       "  /* Single line multi line comment. */\n"
26536       "  const std::string String = \"This is a multiline string with quite "
26537       "some long lines, at least for the LLVM Style.\"\n"
26538       "                             \"We already did it with multi line "
26539       "comments, and we will do it with line comments. Just to check if "
26540       "everything is working.\";\n"
26541       "  // This is a line comment (block) with quite some long lines, at "
26542       "least for the LLVM Style.\n"
26543       "  // We already did this with multi line comments and strings. Just to "
26544       "check if everything is working.\n"
26545       "  const std::string SmallString = \"Hello World\";\n"
26546       "  // Small line comment\n"
26547       "  return String.size() > SmallString.size();\n"
26548       "}";
26549   verifyNoChange(Code, Style);
26550 }
26551 
26552 TEST_F(FormatTest, FormatDecayCopy) {
26553   // error cases from unit tests
26554   verifyFormat("foo(auto())");
26555   verifyFormat("foo(auto{})");
26556   verifyFormat("foo(auto({}))");
26557   verifyFormat("foo(auto{{}})");
26558 
26559   verifyFormat("foo(auto(1))");
26560   verifyFormat("foo(auto{1})");
26561   verifyFormat("foo(new auto(1))");
26562   verifyFormat("foo(new auto{1})");
26563   verifyFormat("decltype(auto(1)) x;");
26564   verifyFormat("decltype(auto{1}) x;");
26565   verifyFormat("auto(x);");
26566   verifyFormat("auto{x};");
26567   verifyFormat("new auto{x};");
26568   verifyFormat("auto{x} = y;");
26569   verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly
26570                                 // the user's own fault
26571   verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is
26572                                          // clearly the user's own fault
26573   verifyFormat("auto (*p)() = f;");
26574 }
26575 
26576 TEST_F(FormatTest, Cpp20ModulesSupport) {
26577   FormatStyle Style = getLLVMStyle();
26578   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
26579   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
26580 
26581   verifyFormat("export import foo;", Style);
26582   verifyFormat("export import foo:bar;", Style);
26583   verifyFormat("export import foo.bar;", Style);
26584   verifyFormat("export import foo.bar:baz;", Style);
26585   verifyFormat("export import :bar;", Style);
26586   verifyFormat("export module foo:bar;", Style);
26587   verifyFormat("export module foo;", Style);
26588   verifyFormat("export module foo.bar;", Style);
26589   verifyFormat("export module foo.bar:baz;", Style);
26590   verifyFormat("export import <string_view>;", Style);
26591   verifyFormat("export import <Foo/Bar>;", Style);
26592 
26593   verifyFormat("export type_name var;", Style);
26594   verifyFormat("template <class T> export using A = B<T>;", Style);
26595   verifyFormat("export using A = B;", Style);
26596   verifyFormat("export int func() {\n"
26597                "  foo();\n"
26598                "}",
26599                Style);
26600   verifyFormat("export struct {\n"
26601                "  int foo;\n"
26602                "};",
26603                Style);
26604   verifyFormat("export {\n"
26605                "  int foo;\n"
26606                "};",
26607                Style);
26608   verifyFormat("export export char const *hello() { return \"hello\"; }");
26609 
26610   verifyFormat("import bar;", Style);
26611   verifyFormat("import foo.bar;", Style);
26612   verifyFormat("import foo:bar;", Style);
26613   verifyFormat("import :bar;", Style);
26614   verifyFormat("import /* module partition */ :bar;", Style);
26615   verifyFormat("import <ctime>;", Style);
26616   verifyFormat("import \"header\";", Style);
26617 
26618   verifyFormat("module foo;", Style);
26619   verifyFormat("module foo:bar;", Style);
26620   verifyFormat("module foo.bar;", Style);
26621   verifyFormat("module;", Style);
26622 
26623   verifyFormat("export namespace hi {\n"
26624                "const char *sayhi();\n"
26625                "}",
26626                Style);
26627 
26628   verifyFormat("module :private;", Style);
26629   verifyFormat("import <foo/bar.h>;", Style);
26630   verifyFormat("import foo...bar;", Style);
26631   verifyFormat("import ..........;", Style);
26632   verifyFormat("module foo:private;", Style);
26633   verifyFormat("import a", Style);
26634   verifyFormat("module a", Style);
26635   verifyFormat("export import a", Style);
26636   verifyFormat("export module a", Style);
26637 
26638   verifyFormat("import", Style);
26639   verifyFormat("module", Style);
26640   verifyFormat("export", Style);
26641 
26642   verifyFormat("import /* not keyword */ = val ? 2 : 1;");
26643 }
26644 
26645 TEST_F(FormatTest, CoroutineForCoawait) {
26646   FormatStyle Style = getLLVMStyle();
26647   verifyFormat("for co_await (auto x : range())\n  ;");
26648   verifyFormat("for (auto i : arr) {\n"
26649                "}",
26650                Style);
26651   verifyFormat("for co_await (auto i : arr) {\n"
26652                "}",
26653                Style);
26654   verifyFormat("for co_await (auto i : foo(T{})) {\n"
26655                "}",
26656                Style);
26657 }
26658 
26659 TEST_F(FormatTest, CoroutineCoAwait) {
26660   verifyFormat("int x = co_await foo();");
26661   verifyFormat("int x = (co_await foo());");
26662   verifyFormat("co_await (42);");
26663   verifyFormat("void operator co_await(int);");
26664   verifyFormat("void operator co_await(a);");
26665   verifyFormat("co_await a;");
26666   verifyFormat("co_await missing_await_resume{};");
26667   verifyFormat("co_await a; // comment");
26668   verifyFormat("void test0() { co_await a; }");
26669   verifyFormat("co_await co_await co_await foo();");
26670   verifyFormat("co_await foo().bar();");
26671   verifyFormat("co_await [this]() -> Task { co_return x; }");
26672   verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await "
26673                "foo(); }(x, y);");
26674 
26675   FormatStyle Style = getLLVMStyleWithColumns(40);
26676   verifyFormat("co_await [this](int a, int b) -> Task {\n"
26677                "  co_return co_await foo();\n"
26678                "}(x, y);",
26679                Style);
26680   verifyFormat("co_await;");
26681 }
26682 
26683 TEST_F(FormatTest, CoroutineCoYield) {
26684   verifyFormat("int x = co_yield foo();");
26685   verifyFormat("int x = (co_yield foo());");
26686   verifyFormat("co_yield (42);");
26687   verifyFormat("co_yield {42};");
26688   verifyFormat("co_yield 42;");
26689   verifyFormat("co_yield n++;");
26690   verifyFormat("co_yield ++n;");
26691   verifyFormat("co_yield;");
26692 }
26693 
26694 TEST_F(FormatTest, CoroutineCoReturn) {
26695   verifyFormat("co_return (42);");
26696   verifyFormat("co_return;");
26697   verifyFormat("co_return {};");
26698   verifyFormat("co_return x;");
26699   verifyFormat("co_return co_await foo();");
26700   verifyFormat("co_return co_yield foo();");
26701 }
26702 
26703 TEST_F(FormatTest, EmptyShortBlock) {
26704   auto Style = getLLVMStyle();
26705   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
26706 
26707   verifyFormat("try {\n"
26708                "  doA();\n"
26709                "} catch (Exception &e) {\n"
26710                "  e.printStackTrace();\n"
26711                "}",
26712                Style);
26713 
26714   verifyFormat("try {\n"
26715                "  doA();\n"
26716                "} catch (Exception &e) {}",
26717                Style);
26718 }
26719 
26720 TEST_F(FormatTest, ShortTemplatedArgumentLists) {
26721   auto Style = getLLVMStyle();
26722 
26723   verifyFormat("template <> struct S : Template<int (*)[]> {};", Style);
26724   verifyFormat("template <> struct S : Template<int (*)[10]> {};", Style);
26725   verifyFormat("struct Y : X<[] { return 0; }> {};", Style);
26726   verifyFormat("struct Y<[] { return 0; }> {};", Style);
26727 
26728   verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style);
26729   verifyFormat("template <int N> struct Foo<char[N]> {};", Style);
26730 }
26731 
26732 TEST_F(FormatTest, MultilineLambdaInConditional) {
26733   auto Style = getLLVMStyleWithColumns(70);
26734   verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? []() {\n"
26735                "  ;\n"
26736                "  return 5;\n"
26737                "}()\n"
26738                "                                                     : 2;",
26739                Style);
26740   verifyFormat(
26741       "auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? 2 : []() {\n"
26742       "  ;\n"
26743       "  return 5;\n"
26744       "}();",
26745       Style);
26746 
26747   Style = getLLVMStyleWithColumns(60);
26748   verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak\n"
26749                "                              ? []() {\n"
26750                "                                  ;\n"
26751                "                                  return 5;\n"
26752                "                                }()\n"
26753                "                              : 2;",
26754                Style);
26755   verifyFormat("auto aLengthyIdentifier =\n"
26756                "    oneExpressionSoThatWeBreak ? 2 : []() {\n"
26757                "      ;\n"
26758                "      return 5;\n"
26759                "    }();",
26760                Style);
26761 
26762   Style = getLLVMStyleWithColumns(40);
26763   verifyFormat("auto aLengthyIdentifier =\n"
26764                "    oneExpressionSoThatWeBreak ? []() {\n"
26765                "      ;\n"
26766                "      return 5;\n"
26767                "    }()\n"
26768                "                               : 2;",
26769                Style);
26770   verifyFormat("auto aLengthyIdentifier =\n"
26771                "    oneExpressionSoThatWeBreak\n"
26772                "        ? 2\n"
26773                "        : []() {\n"
26774                "            ;\n"
26775                "            return 5;\n"
26776                "          };",
26777                Style);
26778 }
26779 
26780 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) {
26781   auto Style = getLLVMStyle();
26782 
26783   StringRef Short = "functionCall(paramA, paramB, paramC);\n"
26784                     "void functionDecl(int a, int b, int c);";
26785 
26786   StringRef Medium = "functionCall(paramA, paramB, paramC, paramD, paramE, "
26787                      "paramF, paramG, paramH, paramI);\n"
26788                      "void functionDecl(int argumentA, int argumentB, int "
26789                      "argumentC, int argumentD, int argumentE);";
26790 
26791   verifyFormat(Short, Style);
26792 
26793   StringRef NoBreak = "functionCall(paramA, paramB, paramC, paramD, paramE, "
26794                       "paramF, paramG, paramH,\n"
26795                       "             paramI);\n"
26796                       "void functionDecl(int argumentA, int argumentB, int "
26797                       "argumentC, int argumentD,\n"
26798                       "                  int argumentE);";
26799 
26800   verifyFormat(NoBreak, Medium, Style);
26801   verifyFormat(NoBreak,
26802                "functionCall(\n"
26803                "    paramA,\n"
26804                "    paramB,\n"
26805                "    paramC,\n"
26806                "    paramD,\n"
26807                "    paramE,\n"
26808                "    paramF,\n"
26809                "    paramG,\n"
26810                "    paramH,\n"
26811                "    paramI\n"
26812                ");\n"
26813                "void functionDecl(\n"
26814                "    int argumentA,\n"
26815                "    int argumentB,\n"
26816                "    int argumentC,\n"
26817                "    int argumentD,\n"
26818                "    int argumentE\n"
26819                ");",
26820                Style);
26821 
26822   verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n"
26823                "                  nestedLongFunctionCall(argument1, "
26824                "argument2, argument3,\n"
26825                "                                         argument4, "
26826                "argument5));",
26827                Style);
26828 
26829   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
26830 
26831   verifyFormat(Short, Style);
26832   verifyFormat(
26833       "functionCall(\n"
26834       "    paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
26835       "paramI\n"
26836       ");\n"
26837       "void functionDecl(\n"
26838       "    int argumentA, int argumentB, int argumentC, int argumentD, int "
26839       "argumentE\n"
26840       ");",
26841       Medium, Style);
26842 
26843   Style.AllowAllArgumentsOnNextLine = false;
26844   Style.AllowAllParametersOfDeclarationOnNextLine = false;
26845 
26846   verifyFormat(Short, Style);
26847   verifyFormat(
26848       "functionCall(\n"
26849       "    paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
26850       "paramI\n"
26851       ");\n"
26852       "void functionDecl(\n"
26853       "    int argumentA, int argumentB, int argumentC, int argumentD, int "
26854       "argumentE\n"
26855       ");",
26856       Medium, Style);
26857 
26858   Style.BinPackArguments = false;
26859   Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
26860 
26861   verifyFormat(Short, Style);
26862 
26863   verifyFormat("functionCall(\n"
26864                "    paramA,\n"
26865                "    paramB,\n"
26866                "    paramC,\n"
26867                "    paramD,\n"
26868                "    paramE,\n"
26869                "    paramF,\n"
26870                "    paramG,\n"
26871                "    paramH,\n"
26872                "    paramI\n"
26873                ");\n"
26874                "void functionDecl(\n"
26875                "    int argumentA,\n"
26876                "    int argumentB,\n"
26877                "    int argumentC,\n"
26878                "    int argumentD,\n"
26879                "    int argumentE\n"
26880                ");",
26881                Medium, Style);
26882 
26883   verifyFormat("outerFunctionCall(\n"
26884                "    nestedFunctionCall(argument1),\n"
26885                "    nestedLongFunctionCall(\n"
26886                "        argument1,\n"
26887                "        argument2,\n"
26888                "        argument3,\n"
26889                "        argument4,\n"
26890                "        argument5\n"
26891                "    )\n"
26892                ");",
26893                Style);
26894 
26895   verifyFormat("int a = (int)b;", Style);
26896   verifyFormat("int a = (int)b;",
26897                "int a = (\n"
26898                "    int\n"
26899                ") b;",
26900                Style);
26901 
26902   verifyFormat("return (true);", Style);
26903   verifyFormat("return (true);",
26904                "return (\n"
26905                "    true\n"
26906                ");",
26907                Style);
26908 
26909   verifyFormat("void foo();", Style);
26910   verifyFormat("void foo();",
26911                "void foo(\n"
26912                ");",
26913                Style);
26914 
26915   verifyFormat("void foo() {}", Style);
26916   verifyFormat("void foo() {}",
26917                "void foo(\n"
26918                ") {\n"
26919                "}",
26920                Style);
26921 
26922   verifyFormat("auto string = std::string();", Style);
26923   verifyFormat("auto string = std::string();",
26924                "auto string = std::string(\n"
26925                ");",
26926                Style);
26927 
26928   verifyFormat("void (*functionPointer)() = nullptr;", Style);
26929   verifyFormat("void (*functionPointer)() = nullptr;",
26930                "void (\n"
26931                "    *functionPointer\n"
26932                ")\n"
26933                "(\n"
26934                ") = nullptr;",
26935                Style);
26936 }
26937 
26938 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) {
26939   auto Style = getLLVMStyle();
26940 
26941   verifyFormat("if (foo()) {\n"
26942                "  return;\n"
26943                "}",
26944                Style);
26945 
26946   verifyFormat("if (quiteLongArg !=\n"
26947                "    (alsoLongArg - 1)) { // ABC is a very longgggggggggggg "
26948                "comment\n"
26949                "  return;\n"
26950                "}",
26951                Style);
26952 
26953   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
26954 
26955   verifyFormat("if (foo()) {\n"
26956                "  return;\n"
26957                "}",
26958                Style);
26959 
26960   verifyFormat("if (quiteLongArg !=\n"
26961                "    (alsoLongArg - 1)) { // ABC is a very longgggggggggggg "
26962                "comment\n"
26963                "  return;\n"
26964                "}",
26965                Style);
26966 
26967   verifyFormat("void foo() {\n"
26968                "  if (camelCaseName < alsoLongName ||\n"
26969                "      anotherEvenLongerName <=\n"
26970                "          thisReallyReallyReallyReallyReallyReallyLongerName ||"
26971                "\n"
26972                "      otherName < thisLastName) {\n"
26973                "    return;\n"
26974                "  } else if (quiteLongName < alsoLongName ||\n"
26975                "             anotherEvenLongerName <=\n"
26976                "                 thisReallyReallyReallyReallyReallyReallyLonger"
26977                "Name ||\n"
26978                "             otherName < thisLastName) {\n"
26979                "    return;\n"
26980                "  }\n"
26981                "}",
26982                Style);
26983 
26984   Style.ContinuationIndentWidth = 2;
26985   verifyFormat("void foo() {\n"
26986                "  if (ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
26987                "      ontoMultipleLines && whenFormattedCorrectly) {\n"
26988                "    if (false) {\n"
26989                "      return;\n"
26990                "    } else if (thisIsRatherALongIfClause && "
26991                "thatIExpectToBeBroken ||\n"
26992                "               ontoMultipleLines && whenFormattedCorrectly) {\n"
26993                "      return;\n"
26994                "    }\n"
26995                "  }\n"
26996                "}",
26997                Style);
26998 }
26999 
27000 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
27001   auto Style = getLLVMStyle();
27002 
27003   verifyFormat("for (int i = 0; i < 5; ++i) {\n"
27004                "  doSomething();\n"
27005                "}",
27006                Style);
27007 
27008   verifyFormat("for (int myReallyLongCountVariable = 0; "
27009                "myReallyLongCountVariable < count;\n"
27010                "     myReallyLongCountVariable++) {\n"
27011                "  doSomething();\n"
27012                "}",
27013                Style);
27014 
27015   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
27016 
27017   verifyFormat("for (int i = 0; i < 5; ++i) {\n"
27018                "  doSomething();\n"
27019                "}",
27020                Style);
27021 
27022   verifyFormat("for (int myReallyLongCountVariable = 0; "
27023                "myReallyLongCountVariable < count;\n"
27024                "     myReallyLongCountVariable++) {\n"
27025                "  doSomething();\n"
27026                "}",
27027                Style);
27028 }
27029 
27030 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentInitializers) {
27031   auto Style = getLLVMStyleWithColumns(60);
27032   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
27033   // Aggregate initialization.
27034   verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
27035                "    10000000, 20000000\n"
27036                "};",
27037                Style);
27038   verifyFormat("SomeStruct s{\n"
27039                "    \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n"
27040                "    \"zzzzzzzzzzzzzzzz\"\n"
27041                "};",
27042                Style);
27043   // Designated initializers.
27044   verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
27045                "    [0] = 10000000, [1] = 20000000\n"
27046                "};",
27047                Style);
27048   verifyFormat("SomeStruct s{\n"
27049                "    .foo = \"xxxxxxxxxxxxx\",\n"
27050                "    .bar = \"yyyyyyyyyyyyy\",\n"
27051                "    .baz = \"zzzzzzzzzzzzz\"\n"
27052                "};",
27053                Style);
27054   // List initialization.
27055   verifyFormat("SomeStruct s{\n"
27056                "    \"xxxxxxxxxxxxx\",\n"
27057                "    \"yyyyyyyyyyyyy\",\n"
27058                "    \"zzzzzzzzzzzzz\",\n"
27059                "};",
27060                Style);
27061   verifyFormat("SomeStruct{\n"
27062                "    \"xxxxxxxxxxxxx\",\n"
27063                "    \"yyyyyyyyyyyyy\",\n"
27064                "    \"zzzzzzzzzzzzz\",\n"
27065                "};",
27066                Style);
27067   verifyFormat("new SomeStruct{\n"
27068                "    \"xxxxxxxxxxxxx\",\n"
27069                "    \"yyyyyyyyyyyyy\",\n"
27070                "    \"zzzzzzzzzzzzz\",\n"
27071                "};",
27072                Style);
27073   // Member initializer.
27074   verifyFormat("class SomeClass {\n"
27075                "  SomeStruct s{\n"
27076                "      \"xxxxxxxxxxxxx\",\n"
27077                "      \"yyyyyyyyyyyyy\",\n"
27078                "      \"zzzzzzzzzzzzz\",\n"
27079                "  };\n"
27080                "};",
27081                Style);
27082   // Constructor member initializer.
27083   verifyFormat("SomeClass::SomeClass : strct{\n"
27084                "                           \"xxxxxxxxxxxxx\",\n"
27085                "                           \"yyyyyyyyyyyyy\",\n"
27086                "                           \"zzzzzzzzzzzzz\",\n"
27087                "                       } {}",
27088                Style);
27089   // Copy initialization.
27090   verifyFormat("SomeStruct s = SomeStruct{\n"
27091                "    \"xxxxxxxxxxxxx\",\n"
27092                "    \"yyyyyyyyyyyyy\",\n"
27093                "    \"zzzzzzzzzzzzz\",\n"
27094                "};",
27095                Style);
27096   // Copy list initialization.
27097   verifyFormat("SomeStruct s = {\n"
27098                "    \"xxxxxxxxxxxxx\",\n"
27099                "    \"yyyyyyyyyyyyy\",\n"
27100                "    \"zzzzzzzzzzzzz\",\n"
27101                "};",
27102                Style);
27103   // Assignment operand initialization.
27104   verifyFormat("s = {\n"
27105                "    \"xxxxxxxxxxxxx\",\n"
27106                "    \"yyyyyyyyyyyyy\",\n"
27107                "    \"zzzzzzzzzzzzz\",\n"
27108                "};",
27109                Style);
27110   // Returned object initialization.
27111   verifyFormat("return {\n"
27112                "    \"xxxxxxxxxxxxx\",\n"
27113                "    \"yyyyyyyyyyyyy\",\n"
27114                "    \"zzzzzzzzzzzzz\",\n"
27115                "};",
27116                Style);
27117   // Initializer list.
27118   verifyFormat("auto initializerList = {\n"
27119                "    \"xxxxxxxxxxxxx\",\n"
27120                "    \"yyyyyyyyyyyyy\",\n"
27121                "    \"zzzzzzzzzzzzz\",\n"
27122                "};",
27123                Style);
27124   // Function parameter initialization.
27125   verifyFormat("func({\n"
27126                "    \"xxxxxxxxxxxxx\",\n"
27127                "    \"yyyyyyyyyyyyy\",\n"
27128                "    \"zzzzzzzzzzzzz\",\n"
27129                "});",
27130                Style);
27131   // Nested init lists.
27132   verifyFormat("SomeStruct s = {\n"
27133                "    {{init1, init2, init3, init4, init5},\n"
27134                "     {init1, init2, init3, init4, init5}}\n"
27135                "};",
27136                Style);
27137   verifyFormat("SomeStruct s = {\n"
27138                "    {{\n"
27139                "         .init1 = 1,\n"
27140                "         .init2 = 2,\n"
27141                "         .init3 = 3,\n"
27142                "         .init4 = 4,\n"
27143                "         .init5 = 5,\n"
27144                "     },\n"
27145                "     {init1, init2, init3, init4, init5}}\n"
27146                "};",
27147                Style);
27148   verifyFormat("SomeArrayT a[3] = {\n"
27149                "    {\n"
27150                "        foo,\n"
27151                "        bar,\n"
27152                "    },\n"
27153                "    {\n"
27154                "        foo,\n"
27155                "        bar,\n"
27156                "    },\n"
27157                "    SomeArrayT{},\n"
27158                "};",
27159                Style);
27160   verifyFormat("SomeArrayT a[3] = {\n"
27161                "    {foo},\n"
27162                "    {\n"
27163                "        {\n"
27164                "            init1,\n"
27165                "            init2,\n"
27166                "            init3,\n"
27167                "        },\n"
27168                "        {\n"
27169                "            init1,\n"
27170                "            init2,\n"
27171                "            init3,\n"
27172                "        },\n"
27173                "    },\n"
27174                "    {baz},\n"
27175                "};",
27176                Style);
27177 }
27178 
27179 TEST_F(FormatTest, UnderstandsDigraphs) {
27180   verifyFormat("int arr<:5:> = {};");
27181   verifyFormat("int arr[5] = <%%>;");
27182   verifyFormat("int arr<:::qualified_variable:> = {};");
27183   verifyFormat("int arr[::qualified_variable] = <%%>;");
27184   verifyFormat("%:include <header>");
27185   verifyFormat("%:define A x##y");
27186   verifyFormat("#define A x%:%:y");
27187 }
27188 
27189 TEST_F(FormatTest, AlignArrayOfStructuresLeftAlignmentNonSquare) {
27190   auto Style = getLLVMStyle();
27191   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
27192   Style.AlignConsecutiveAssignments.Enabled = true;
27193   Style.AlignConsecutiveDeclarations.Enabled = true;
27194 
27195   // The AlignArray code is incorrect for non square Arrays and can cause
27196   // crashes, these tests assert that the array is not changed but will
27197   // also act as regression tests for when it is properly fixed
27198   verifyFormat("struct test demo[] = {\n"
27199                "    {1, 2},\n"
27200                "    {3, 4, 5},\n"
27201                "    {6, 7, 8}\n"
27202                "};",
27203                Style);
27204   verifyFormat("struct test demo[] = {\n"
27205                "    {1, 2, 3, 4, 5},\n"
27206                "    {3, 4, 5},\n"
27207                "    {6, 7, 8}\n"
27208                "};",
27209                Style);
27210   verifyFormat("struct test demo[] = {\n"
27211                "    {1, 2, 3, 4, 5},\n"
27212                "    {3, 4, 5},\n"
27213                "    {6, 7, 8, 9, 10, 11, 12}\n"
27214                "};",
27215                Style);
27216   verifyFormat("struct test demo[] = {\n"
27217                "    {1, 2, 3},\n"
27218                "    {3, 4, 5},\n"
27219                "    {6, 7, 8, 9, 10, 11, 12}\n"
27220                "};",
27221                Style);
27222 
27223   verifyFormat("S{\n"
27224                "    {},\n"
27225                "    {},\n"
27226                "    {a, b}\n"
27227                "};",
27228                Style);
27229   verifyFormat("S{\n"
27230                "    {},\n"
27231                "    {},\n"
27232                "    {a, b},\n"
27233                "};",
27234                Style);
27235   verifyFormat("void foo() {\n"
27236                "  auto thing = test{\n"
27237                "      {\n"
27238                "       {13}, {something}, // A\n"
27239                "      }\n"
27240                "  };\n"
27241                "}",
27242                "void foo() {\n"
27243                "  auto thing = test{\n"
27244                "      {\n"
27245                "       {13},\n"
27246                "       {something}, // A\n"
27247                "      }\n"
27248                "  };\n"
27249                "}",
27250                Style);
27251 }
27252 
27253 TEST_F(FormatTest, AlignArrayOfStructuresRightAlignmentNonSquare) {
27254   auto Style = getLLVMStyle();
27255   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
27256   Style.AlignConsecutiveAssignments.Enabled = true;
27257   Style.AlignConsecutiveDeclarations.Enabled = true;
27258 
27259   // The AlignArray code is incorrect for non square Arrays and can cause
27260   // crashes, these tests assert that the array is not changed but will
27261   // also act as regression tests for when it is properly fixed
27262   verifyFormat("struct test demo[] = {\n"
27263                "    {1, 2},\n"
27264                "    {3, 4, 5},\n"
27265                "    {6, 7, 8}\n"
27266                "};",
27267                Style);
27268   verifyFormat("struct test demo[] = {\n"
27269                "    {1, 2, 3, 4, 5},\n"
27270                "    {3, 4, 5},\n"
27271                "    {6, 7, 8}\n"
27272                "};",
27273                Style);
27274   verifyFormat("struct test demo[] = {\n"
27275                "    {1, 2, 3, 4, 5},\n"
27276                "    {3, 4, 5},\n"
27277                "    {6, 7, 8, 9, 10, 11, 12}\n"
27278                "};",
27279                Style);
27280   verifyFormat("struct test demo[] = {\n"
27281                "    {1, 2, 3},\n"
27282                "    {3, 4, 5},\n"
27283                "    {6, 7, 8, 9, 10, 11, 12}\n"
27284                "};",
27285                Style);
27286 
27287   verifyFormat("S{\n"
27288                "    {},\n"
27289                "    {},\n"
27290                "    {a, b}\n"
27291                "};",
27292                Style);
27293   verifyFormat("S{\n"
27294                "    {},\n"
27295                "    {},\n"
27296                "    {a, b},\n"
27297                "};",
27298                Style);
27299   verifyFormat("void foo() {\n"
27300                "  auto thing = test{\n"
27301                "      {\n"
27302                "       {13}, {something}, // A\n"
27303                "      }\n"
27304                "  };\n"
27305                "}",
27306                "void foo() {\n"
27307                "  auto thing = test{\n"
27308                "      {\n"
27309                "       {13},\n"
27310                "       {something}, // A\n"
27311                "      }\n"
27312                "  };\n"
27313                "}",
27314                Style);
27315 }
27316 
27317 TEST_F(FormatTest, FormatsVariableTemplates) {
27318   verifyFormat("inline bool var = is_integral_v<int> && is_signed_v<int>;");
27319   verifyFormat("template <typename T> "
27320                "inline bool var = is_integral_v<T> && is_signed_v<T>;");
27321 }
27322 
27323 TEST_F(FormatTest, RemoveSemicolon) {
27324   FormatStyle Style = getLLVMStyle();
27325   Style.RemoveSemicolon = true;
27326 
27327   verifyFormat("int max(int a, int b) { return a > b ? a : b; }",
27328                "int max(int a, int b) { return a > b ? a : b; };", Style);
27329 
27330   verifyFormat("int max(int a, int b) { return a > b ? a : b; }",
27331                "int max(int a, int b) { return a > b ? a : b; };;", Style);
27332 
27333   verifyFormat("class Foo {\n"
27334                "  int getSomething() const { return something; }\n"
27335                "};",
27336                "class Foo {\n"
27337                "  int getSomething() const { return something; };\n"
27338                "};",
27339                Style);
27340 
27341   verifyFormat("class Foo {\n"
27342                "  int getSomething() const { return something; }\n"
27343                "};",
27344                "class Foo {\n"
27345                "  int getSomething() const { return something; };;\n"
27346                "};",
27347                Style);
27348 
27349   verifyFormat("for (;;) {\n"
27350                "}",
27351                Style);
27352 
27353   verifyFormat("class [[deprecated(\"\")]] C {\n"
27354                "  int i;\n"
27355                "};",
27356                Style);
27357 
27358   verifyFormat("struct EXPORT_MACRO [[nodiscard]] C {\n"
27359                "  int i;\n"
27360                "};",
27361                Style);
27362 
27363   verifyIncompleteFormat("class C final [[deprecated(l]] {});", Style);
27364 
27365   verifyFormat("void main() {}", "void main() {};", Style);
27366 
27367   verifyFormat("struct Foo {\n"
27368                "  Foo() {}\n"
27369                "  ~Foo() {}\n"
27370                "};",
27371                "struct Foo {\n"
27372                "  Foo() {};\n"
27373                "  ~Foo() {};\n"
27374                "};",
27375                Style);
27376 
27377 // We can't (and probably shouldn't) support the following.
27378 #if 0
27379   verifyFormat("void foo() {} //\n"
27380                "int bar;",
27381                "void foo() {}; //\n"
27382                "; int bar;",
27383                Style);
27384 #endif
27385 
27386   Style.TypenameMacros.push_back("STRUCT");
27387   verifyFormat("STRUCT(T, B) { int i; };", Style);
27388 }
27389 
27390 TEST_F(FormatTest, BreakAfterAttributes) {
27391   constexpr StringRef Code("[[maybe_unused]] const int i;\n"
27392                            "[[foo([[]])]] [[maybe_unused]]\n"
27393                            "int j;\n"
27394                            "[[maybe_unused]]\n"
27395                            "foo<int> k;\n"
27396                            "[[nodiscard]] inline int f(int &i);\n"
27397                            "[[foo([[]])]] [[nodiscard]]\n"
27398                            "int g(int &i);\n"
27399                            "[[nodiscard]]\n"
27400                            "inline int f(int &i) {\n"
27401                            "  i = 1;\n"
27402                            "  return 0;\n"
27403                            "}\n"
27404                            "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n"
27405                            "  i = 0;\n"
27406                            "  return 1;\n"
27407                            "}");
27408 
27409   FormatStyle Style = getLLVMStyle();
27410   EXPECT_EQ(Style.BreakAfterAttributes, FormatStyle::ABS_Leave);
27411   verifyNoChange(Code, Style);
27412 
27413   Style.BreakAfterAttributes = FormatStyle::ABS_Never;
27414   verifyFormat("[[maybe_unused]] const int i;\n"
27415                "[[foo([[]])]] [[maybe_unused]] int j;\n"
27416                "[[maybe_unused]] foo<int> k;\n"
27417                "[[nodiscard]] inline int f(int &i);\n"
27418                "[[foo([[]])]] [[nodiscard]] int g(int &i);\n"
27419                "[[nodiscard]] inline int f(int &i) {\n"
27420                "  i = 1;\n"
27421                "  return 0;\n"
27422                "}\n"
27423                "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n"
27424                "  i = 0;\n"
27425                "  return 1;\n"
27426                "}",
27427                Code, Style);
27428 
27429   Style.BreakAfterAttributes = FormatStyle::ABS_Always;
27430   verifyFormat("[[maybe_unused]]\n"
27431                "const int i;\n"
27432                "[[foo([[]])]] [[maybe_unused]]\n"
27433                "int j;\n"
27434                "[[maybe_unused]]\n"
27435                "foo<int> k;\n"
27436                "[[nodiscard]]\n"
27437                "inline int f(int &i);\n"
27438                "[[foo([[]])]] [[nodiscard]]\n"
27439                "int g(int &i);\n"
27440                "[[nodiscard]]\n"
27441                "inline int f(int &i) {\n"
27442                "  i = 1;\n"
27443                "  return 0;\n"
27444                "}\n"
27445                "[[foo([[]])]] [[nodiscard]]\n"
27446                "int g(int &i) {\n"
27447                "  i = 0;\n"
27448                "  return 1;\n"
27449                "}",
27450                Code, Style);
27451 
27452   constexpr StringRef CtrlStmtCode("[[likely]] if (a)\n"
27453                                    "  f();\n"
27454                                    "else\n"
27455                                    "  g();\n"
27456                                    "[[foo([[]])]]\n"
27457                                    "switch (b) {\n"
27458                                    "[[unlikely]] case 1:\n"
27459                                    "  ++b;\n"
27460                                    "  break;\n"
27461                                    "[[likely]]\n"
27462                                    "default:\n"
27463                                    "  return;\n"
27464                                    "}\n"
27465                                    "[[unlikely]] for (; c > 0; --c)\n"
27466                                    "  h();\n"
27467                                    "[[likely]]\n"
27468                                    "while (d > 0)\n"
27469                                    "  --d;");
27470 
27471   Style.BreakAfterAttributes = FormatStyle::ABS_Leave;
27472   verifyNoChange(CtrlStmtCode, Style);
27473 
27474   Style.BreakAfterAttributes = FormatStyle::ABS_Never;
27475   verifyFormat("[[likely]] if (a)\n"
27476                "  f();\n"
27477                "else\n"
27478                "  g();\n"
27479                "[[foo([[]])]] switch (b) {\n"
27480                "[[unlikely]] case 1:\n"
27481                "  ++b;\n"
27482                "  break;\n"
27483                "[[likely]] default:\n"
27484                "  return;\n"
27485                "}\n"
27486                "[[unlikely]] for (; c > 0; --c)\n"
27487                "  h();\n"
27488                "[[likely]] while (d > 0)\n"
27489                "  --d;",
27490                CtrlStmtCode, Style);
27491 
27492   Style.BreakAfterAttributes = FormatStyle::ABS_Always;
27493   verifyFormat("[[likely]]\n"
27494                "if (a)\n"
27495                "  f();\n"
27496                "else\n"
27497                "  g();\n"
27498                "[[foo([[]])]]\n"
27499                "switch (b) {\n"
27500                "[[unlikely]]\n"
27501                "case 1:\n"
27502                "  ++b;\n"
27503                "  break;\n"
27504                "[[likely]]\n"
27505                "default:\n"
27506                "  return;\n"
27507                "}\n"
27508                "[[unlikely]]\n"
27509                "for (; c > 0; --c)\n"
27510                "  h();\n"
27511                "[[likely]]\n"
27512                "while (d > 0)\n"
27513                "  --d;",
27514                CtrlStmtCode, Style);
27515 
27516   constexpr StringRef CtorDtorCode("struct Foo {\n"
27517                                    "  [[deprecated]] Foo();\n"
27518                                    "  [[deprecated]] Foo() {}\n"
27519                                    "  [[deprecated]] ~Foo();\n"
27520                                    "  [[deprecated]] ~Foo() {}\n"
27521                                    "  [[deprecated]] void f();\n"
27522                                    "  [[deprecated]] void f() {}\n"
27523                                    "};\n"
27524                                    "[[deprecated]] Bar::Bar() {}\n"
27525                                    "[[deprecated]] Bar::~Bar() {}\n"
27526                                    "[[deprecated]] void g() {}");
27527   verifyFormat("struct Foo {\n"
27528                "  [[deprecated]]\n"
27529                "  Foo();\n"
27530                "  [[deprecated]]\n"
27531                "  Foo() {}\n"
27532                "  [[deprecated]]\n"
27533                "  ~Foo();\n"
27534                "  [[deprecated]]\n"
27535                "  ~Foo() {}\n"
27536                "  [[deprecated]]\n"
27537                "  void f();\n"
27538                "  [[deprecated]]\n"
27539                "  void f() {}\n"
27540                "};\n"
27541                "[[deprecated]]\n"
27542                "Bar::Bar() {}\n"
27543                "[[deprecated]]\n"
27544                "Bar::~Bar() {}\n"
27545                "[[deprecated]]\n"
27546                "void g() {}",
27547                CtorDtorCode, Style);
27548 
27549   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
27550   verifyFormat("struct Foo {\n"
27551                "  [[deprecated]]\n"
27552                "  Foo();\n"
27553                "  [[deprecated]]\n"
27554                "  Foo()\n"
27555                "  {\n"
27556                "  }\n"
27557                "  [[deprecated]]\n"
27558                "  ~Foo();\n"
27559                "  [[deprecated]]\n"
27560                "  ~Foo()\n"
27561                "  {\n"
27562                "  }\n"
27563                "  [[deprecated]]\n"
27564                "  void f();\n"
27565                "  [[deprecated]]\n"
27566                "  void f()\n"
27567                "  {\n"
27568                "  }\n"
27569                "};\n"
27570                "[[deprecated]]\n"
27571                "Bar::Bar()\n"
27572                "{\n"
27573                "}\n"
27574                "[[deprecated]]\n"
27575                "Bar::~Bar()\n"
27576                "{\n"
27577                "}\n"
27578                "[[deprecated]]\n"
27579                "void g()\n"
27580                "{\n"
27581                "}",
27582                CtorDtorCode, Style);
27583 
27584   verifyFormat("struct Foo {\n"
27585                "  [[maybe_unused]]\n"
27586                "  void operator+();\n"
27587                "};\n"
27588                "[[nodiscard]]\n"
27589                "Foo &operator-(Foo &);",
27590                Style);
27591 
27592   Style.ReferenceAlignment = FormatStyle::ReferenceAlignmentStyle::RAS_Left;
27593   verifyFormat("[[nodiscard]]\n"
27594                "Foo& operator-(Foo&);",
27595                Style);
27596 }
27597 
27598 TEST_F(FormatTest, InsertNewlineAtEOF) {
27599   FormatStyle Style = getLLVMStyle();
27600   Style.InsertNewlineAtEOF = true;
27601 
27602   verifyNoChange("int i;\n", Style);
27603   verifyFormat("int i;\n", "int i;", Style);
27604 
27605   constexpr StringRef Code{"namespace {\n"
27606                            "int i;\n"
27607                            "} // namespace"};
27608   verifyFormat(Code.str() + '\n', Code, Style,
27609                {tooling::Range(19, 13)}); // line 3
27610 }
27611 
27612 TEST_F(FormatTest, KeepEmptyLinesAtEOF) {
27613   FormatStyle Style = getLLVMStyle();
27614   Style.KeepEmptyLines.AtEndOfFile = true;
27615 
27616   const StringRef Code{"int i;\n\n"};
27617   verifyNoChange(Code, Style);
27618   verifyFormat(Code, "int i;\n\n\n", Style);
27619 }
27620 
27621 TEST_F(FormatTest, SpaceAfterUDL) {
27622   verifyFormat("auto c = (4s).count();");
27623   verifyFormat("auto x = 5s .count() == 5;");
27624 }
27625 
27626 TEST_F(FormatTest, InterfaceAsClassMemberName) {
27627   verifyFormat("class Foo {\n"
27628                "  int interface;\n"
27629                "  Foo::Foo(int iface) : interface{iface} {}\n"
27630                "}");
27631 }
27632 
27633 TEST_F(FormatTest, PreprocessorOverlappingRegions) {
27634   verifyFormat("#ifdef\n\n"
27635                "#else\n"
27636                "#endif",
27637                "#ifdef \n"
27638                "    \n"
27639                "\n"
27640                "#else \n"
27641                "#endif ",
27642                getGoogleStyle());
27643 }
27644 
27645 TEST_F(FormatTest, RemoveParentheses) {
27646   FormatStyle Style = getLLVMStyle();
27647   EXPECT_EQ(Style.RemoveParentheses, FormatStyle::RPS_Leave);
27648 
27649   Style.RemoveParentheses = FormatStyle::RPS_MultipleParentheses;
27650   verifyFormat("#define Foo(...) foo((__VA_ARGS__))", Style);
27651   verifyFormat("int x __attribute__((aligned(16))) = 0;", Style);
27652   verifyFormat("decltype((foo->bar)) baz;", Style);
27653   verifyFormat("class __declspec(dllimport) X {};",
27654                "class __declspec((dllimport)) X {};", Style);
27655   verifyFormat("int x = (({ 0; }));", "int x = ((({ 0; })));", Style);
27656   verifyFormat("while (a)\n"
27657                "  b;",
27658                "while (((a)))\n"
27659                "  b;",
27660                Style);
27661   verifyFormat("while ((a = b))\n"
27662                "  c;",
27663                "while (((a = b)))\n"
27664                "  c;",
27665                Style);
27666   verifyFormat("if (a)\n"
27667                "  b;",
27668                "if (((a)))\n"
27669                "  b;",
27670                Style);
27671   verifyFormat("if constexpr ((a = b))\n"
27672                "  c;",
27673                "if constexpr (((a = b)))\n"
27674                "  c;",
27675                Style);
27676   verifyFormat("if (({ a; }))\n"
27677                "  b;",
27678                "if ((({ a; })))\n"
27679                "  b;",
27680                Style);
27681   verifyFormat("static_assert((std::is_constructible_v<T, Args &&> && ...));",
27682                "static_assert(((std::is_constructible_v<T, Args &&> && ...)));",
27683                Style);
27684   verifyFormat("foo((a, b));", "foo(((a, b)));", Style);
27685   verifyFormat("foo((a, b));", "foo(((a), b));", Style);
27686   verifyFormat("foo((a, b));", "foo((a, (b)));", Style);
27687   verifyFormat("foo((a, b, c));", "foo((a, ((b)), c));", Style);
27688   verifyFormat("return (0);", "return (((0)));", Style);
27689   verifyFormat("return (({ 0; }));", "return ((({ 0; })));", Style);
27690   verifyFormat("return ((... && std::is_convertible_v<TArgsLocal, TArgs>));",
27691                "return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));",
27692                Style);
27693 
27694   Style.RemoveParentheses = FormatStyle::RPS_ReturnStatement;
27695   verifyFormat("#define Return0 return (0);", Style);
27696   verifyFormat("return 0;", "return (0);", Style);
27697   verifyFormat("co_return 0;", "co_return ((0));", Style);
27698   verifyFormat("return 0;", "return (((0)));", Style);
27699   verifyFormat("return ({ 0; });", "return ((({ 0; })));", Style);
27700   verifyFormat("return (... && std::is_convertible_v<TArgsLocal, TArgs>);",
27701                "return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));",
27702                Style);
27703   verifyFormat("inline decltype(auto) f() {\n"
27704                "  if (a) {\n"
27705                "    return (a);\n"
27706                "  }\n"
27707                "  return (b);\n"
27708                "}",
27709                "inline decltype(auto) f() {\n"
27710                "  if (a) {\n"
27711                "    return ((a));\n"
27712                "  }\n"
27713                "  return ((b));\n"
27714                "}",
27715                Style);
27716   verifyFormat("auto g() {\n"
27717                "  decltype(auto) x = [] {\n"
27718                "    auto y = [] {\n"
27719                "      if (a) {\n"
27720                "        return a;\n"
27721                "      }\n"
27722                "      return b;\n"
27723                "    };\n"
27724                "    if (c) {\n"
27725                "      return (c);\n"
27726                "    }\n"
27727                "    return (d);\n"
27728                "  };\n"
27729                "  if (e) {\n"
27730                "    return e;\n"
27731                "  }\n"
27732                "  return f;\n"
27733                "}",
27734                "auto g() {\n"
27735                "  decltype(auto) x = [] {\n"
27736                "    auto y = [] {\n"
27737                "      if (a) {\n"
27738                "        return ((a));\n"
27739                "      }\n"
27740                "      return ((b));\n"
27741                "    };\n"
27742                "    if (c) {\n"
27743                "      return ((c));\n"
27744                "    }\n"
27745                "    return ((d));\n"
27746                "  };\n"
27747                "  if (e) {\n"
27748                "    return ((e));\n"
27749                "  }\n"
27750                "  return ((f));\n"
27751                "}",
27752                Style);
27753 
27754   Style.ColumnLimit = 25;
27755   verifyFormat("return (a + b) - (c + d);",
27756                "return (((a + b)) -\n"
27757                "        ((c + d)));",
27758                Style);
27759 }
27760 
27761 TEST_F(FormatTest, AllowBreakBeforeNoexceptSpecifier) {
27762   auto Style = getLLVMStyleWithColumns(35);
27763 
27764   EXPECT_EQ(Style.AllowBreakBeforeNoexceptSpecifier, FormatStyle::BBNSS_Never);
27765   verifyFormat("void foo(int arg1,\n"
27766                "         double arg2) noexcept;",
27767                Style);
27768 
27769   // The following line does not fit within the 35 column limit, but that's what
27770   // happens with no break allowed.
27771   verifyFormat("void bar(int arg1, double arg2) noexcept(\n"
27772                "    noexcept(baz(arg1)) &&\n"
27773                "    noexcept(baz(arg2)));",
27774                Style);
27775 
27776   verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;",
27777                Style);
27778 
27779   Style.AllowBreakBeforeNoexceptSpecifier = FormatStyle::BBNSS_Always;
27780   verifyFormat("void foo(int arg1,\n"
27781                "         double arg2) noexcept;",
27782                Style);
27783 
27784   verifyFormat("void bar(int arg1, double arg2)\n"
27785                "    noexcept(noexcept(baz(arg1)) &&\n"
27786                "             noexcept(baz(arg2)));",
27787                Style);
27788 
27789   verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments()\n"
27790                "    noexcept;",
27791                Style);
27792 
27793   Style.AllowBreakBeforeNoexceptSpecifier = FormatStyle::BBNSS_OnlyWithParen;
27794   verifyFormat("void foo(int arg1,\n"
27795                "         double arg2) noexcept;",
27796                Style);
27797 
27798   verifyFormat("void bar(int arg1, double arg2)\n"
27799                "    noexcept(noexcept(baz(arg1)) &&\n"
27800                "             noexcept(baz(arg2)));",
27801                Style);
27802 
27803   verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;",
27804                Style);
27805 }
27806 
27807 TEST_F(FormatTest, PPBranchesInBracedInit) {
27808   verifyFormat("A a_{kFlag1,\n"
27809                "#if BUILD_FLAG\n"
27810                "     kFlag2,\n"
27811                "#else\n"
27812                "     kFlag3,\n"
27813                "#endif\n"
27814                "     kFlag4};",
27815                "A a_{\n"
27816                "  kFlag1,\n"
27817                "#if BUILD_FLAG\n"
27818                "      kFlag2,\n"
27819                "#else\n"
27820                "      kFlag3,\n"
27821                "#endif\n"
27822                "      kFlag4\n"
27823                "};");
27824 }
27825 
27826 TEST_F(FormatTest, PPDirectivesAndCommentsInBracedInit) {
27827   verifyFormat("{\n"
27828                "  char *a[] = {\n"
27829                "      /* abc */ \"abc\",\n"
27830                "#if FOO\n"
27831                "      /* xyz */ \"xyz\",\n"
27832                "#endif\n"
27833                "      /* last */ \"last\"};\n"
27834                "}",
27835                getLLVMStyleWithColumns(30));
27836 }
27837 
27838 TEST_F(FormatTest, BreakAdjacentStringLiterals) {
27839   constexpr StringRef Code{
27840       "return \"Code\" \"\\0\\52\\26\\55\\55\\0\" \"x013\" \"\\02\\xBA\";"};
27841 
27842   verifyFormat("return \"Code\"\n"
27843                "       \"\\0\\52\\26\\55\\55\\0\"\n"
27844                "       \"x013\"\n"
27845                "       \"\\02\\xBA\";",
27846                Code);
27847 
27848   auto Style = getLLVMStyle();
27849   Style.BreakAdjacentStringLiterals = false;
27850   verifyFormat(Code, Style);
27851 }
27852 
27853 TEST_F(FormatTest, AlignUTFCommentsAndStringLiterals) {
27854   verifyFormat(
27855       "int rus;      // А теперь комментарии, например, на русском, 2-байта\n"
27856       "int long_rus; // Верхний коммент еще не превысил границу в 80, однако\n"
27857       "              // уже отодвинут. Перенос, при этом, отрабатывает верно");
27858 
27859   auto Style = getLLVMStyle();
27860   Style.ColumnLimit = 15;
27861   verifyNoChange("#define test  \\\n"
27862                  "  /* 测试 */  \\\n"
27863                  "  \"aa\"        \\\n"
27864                  "  \"bb\"",
27865                  Style);
27866 
27867   Style.ColumnLimit = 25;
27868   verifyFormat("struct foo {\n"
27869                "  int iiiiii; ///< iiiiii\n"
27870                "  int b;      ///< ыыы\n"
27871                "  int c;      ///< ыыыы\n"
27872                "};",
27873                Style);
27874 
27875   Style.ColumnLimit = 35;
27876   verifyFormat("#define SENSOR_DESC_1             \\\n"
27877                "  \"{\"                             \\\n"
27878                "  \"unit_of_measurement: \\\"°C\\\",\"  \\\n"
27879                "  \"}\"",
27880                Style);
27881 
27882   Style.ColumnLimit = 80;
27883   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
27884   verifyFormat("Languages languages = {\n"
27885                "    Language{{'e', 'n'}, U\"Test English\" },\n"
27886                "    Language{{'l', 'v'}, U\"Test Latviešu\"},\n"
27887                "    Language{{'r', 'u'}, U\"Test Русский\" },\n"
27888                "};",
27889                Style);
27890 }
27891 
27892 TEST_F(FormatTest, SpaceBetweenKeywordAndLiteral) {
27893   verifyFormat("return .5;");
27894   verifyFormat("return not '5';");
27895   verifyFormat("return sizeof \"5\";");
27896 }
27897 
27898 TEST_F(FormatTest, BreakBinaryOperations) {
27899   auto Style = getLLVMStyleWithColumns(60);
27900   EXPECT_EQ(Style.BreakBinaryOperations, FormatStyle::BBO_Never);
27901 
27902   // Logical operations
27903   verifyFormat("if (condition1 && condition2) {\n"
27904                "}",
27905                Style);
27906 
27907   verifyFormat("if (condition1 && condition2 &&\n"
27908                "    (condition3 || condition4) && condition5 &&\n"
27909                "    condition6) {\n"
27910                "}",
27911                Style);
27912 
27913   verifyFormat("if (loooooooooooooooooooooongcondition1 &&\n"
27914                "    loooooooooooooooooooooongcondition2) {\n"
27915                "}",
27916                Style);
27917 
27918   // Arithmetic
27919   verifyFormat("const int result = lhs + rhs;", Style);
27920 
27921   verifyFormat("const int result = loooooooongop1 + looooooooongop2 +\n"
27922                "                   loooooooooooooooooooooongop3;",
27923                Style);
27924 
27925   verifyFormat("result = longOperand1 + longOperand2 -\n"
27926                "         (longOperand3 + longOperand4) -\n"
27927                "         longOperand5 * longOperand6;",
27928                Style);
27929 
27930   verifyFormat("const int result =\n"
27931                "    operand1 + operand2 - (operand3 + operand4);",
27932                Style);
27933 
27934   Style.BreakBinaryOperations = FormatStyle::BBO_OnePerLine;
27935 
27936   // Logical operations
27937   verifyFormat("if (condition1 && condition2) {\n"
27938                "}",
27939                Style);
27940 
27941   verifyFormat("if (condition1 && // comment\n"
27942                "    condition2 &&\n"
27943                "    (condition3 || condition4) && // comment\n"
27944                "    condition5 &&\n"
27945                "    condition6) {\n"
27946                "}",
27947                Style);
27948 
27949   verifyFormat("if (loooooooooooooooooooooongcondition1 &&\n"
27950                "    loooooooooooooooooooooongcondition2) {\n"
27951                "}",
27952                Style);
27953 
27954   // Arithmetic
27955   verifyFormat("const int result = lhs + rhs;", Style);
27956 
27957   verifyFormat("result = loooooooooooooooooooooongop1 +\n"
27958                "         loooooooooooooooooooooongop2 +\n"
27959                "         loooooooooooooooooooooongop3;",
27960                Style);
27961 
27962   verifyFormat("const int result =\n"
27963                "    operand1 + operand2 - (operand3 + operand4);",
27964                Style);
27965 
27966   verifyFormat("result = longOperand1 +\n"
27967                "         longOperand2 -\n"
27968                "         (longOperand3 + longOperand4) -\n"
27969                "         longOperand5 +\n"
27970                "         longOperand6;",
27971                Style);
27972 
27973   verifyFormat("result = operand1 +\n"
27974                "         operand2 -\n"
27975                "         operand3 +\n"
27976                "         operand4 -\n"
27977                "         operand5 +\n"
27978                "         operand6;",
27979                Style);
27980 
27981   // Ensure mixed precedence operations are handled properly
27982   verifyFormat("result = op1 + op2 * op3 - op4;", Style);
27983 
27984   verifyFormat("result = operand1 +\n"
27985                "         operand2 /\n"
27986                "         operand3 +\n"
27987                "         operand4 /\n"
27988                "         operand5 *\n"
27989                "         operand6;",
27990                Style);
27991 
27992   verifyFormat("result = operand1 *\n"
27993                "         operand2 -\n"
27994                "         operand3 *\n"
27995                "         operand4 -\n"
27996                "         operand5 +\n"
27997                "         operand6;",
27998                Style);
27999 
28000   verifyFormat("result = operand1 *\n"
28001                "         (operand2 - operand3 * operand4) -\n"
28002                "         operand5 +\n"
28003                "         operand6;",
28004                Style);
28005 
28006   verifyFormat("result = operand1.member *\n"
28007                "         (operand2.member() - operand3->mem * operand4) -\n"
28008                "         operand5.member() +\n"
28009                "         operand6->member;",
28010                Style);
28011 
28012   Style.BreakBinaryOperations = FormatStyle::BBO_RespectPrecedence;
28013   verifyFormat("result = op1 + op2 * op3 - op4;", Style);
28014 
28015   verifyFormat("result = operand1 +\n"
28016                "         operand2 / operand3 +\n"
28017                "         operand4 / operand5 * operand6;",
28018                Style);
28019 
28020   verifyFormat("result = operand1 * operand2 -\n"
28021                "         operand3 * operand4 -\n"
28022                "         operand5 +\n"
28023                "         operand6;",
28024                Style);
28025 
28026   verifyFormat("result = operand1 * (operand2 - operand3 * operand4) -\n"
28027                "         operand5 +\n"
28028                "         operand6;",
28029                Style);
28030 
28031   verifyFormat("std::uint32_t a = byte_buffer[0] |\n"
28032                "                  byte_buffer[1] << 8 |\n"
28033                "                  byte_buffer[2] << 16 |\n"
28034                "                  byte_buffer[3] << 24;",
28035                Style);
28036 
28037   Style.BreakBinaryOperations = FormatStyle::BBO_OnePerLine;
28038   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
28039 
28040   // Logical operations
28041   verifyFormat("if (condition1 && condition2) {\n"
28042                "}",
28043                Style);
28044 
28045   verifyFormat("if (loooooooooooooooooooooongcondition1\n"
28046                "    && loooooooooooooooooooooongcondition2) {\n"
28047                "}",
28048                Style);
28049 
28050   // Arithmetic
28051   verifyFormat("const int result = lhs + rhs;", Style);
28052 
28053   verifyFormat("result = loooooooooooooooooooooongop1\n"
28054                "         + loooooooooooooooooooooongop2\n"
28055                "         + loooooooooooooooooooooongop3;",
28056                Style);
28057 
28058   verifyFormat("const int result =\n"
28059                "    operand1 + operand2 - (operand3 + operand4);",
28060                Style);
28061 
28062   verifyFormat("result = longOperand1\n"
28063                "         + longOperand2\n"
28064                "         - (longOperand3 + longOperand4)\n"
28065                "         - longOperand5\n"
28066                "         + longOperand6;",
28067                Style);
28068 
28069   verifyFormat("result = operand1\n"
28070                "         + operand2\n"
28071                "         - operand3\n"
28072                "         + operand4\n"
28073                "         - operand5\n"
28074                "         + operand6;",
28075                Style);
28076 
28077   // Ensure mixed precedence operations are handled properly
28078   verifyFormat("result = op1 + op2 * op3 - op4;", Style);
28079 
28080   verifyFormat("result = operand1\n"
28081                "         + operand2\n"
28082                "         / operand3\n"
28083                "         + operand4\n"
28084                "         / operand5\n"
28085                "         * operand6;",
28086                Style);
28087 
28088   verifyFormat("result = operand1\n"
28089                "         * operand2\n"
28090                "         - operand3\n"
28091                "         * operand4\n"
28092                "         - operand5\n"
28093                "         + operand6;",
28094                Style);
28095 
28096   verifyFormat("result = operand1\n"
28097                "         * (operand2 - operand3 * operand4)\n"
28098                "         - operand5\n"
28099                "         + operand6;",
28100                Style);
28101 
28102   verifyFormat("std::uint32_t a = byte_buffer[0]\n"
28103                "                  | byte_buffer[1]\n"
28104                "                  << 8\n"
28105                "                  | byte_buffer[2]\n"
28106                "                  << 16\n"
28107                "                  | byte_buffer[3]\n"
28108                "                  << 24;",
28109                Style);
28110 
28111   Style.BreakBinaryOperations = FormatStyle::BBO_RespectPrecedence;
28112   verifyFormat("result = op1 + op2 * op3 - op4;", Style);
28113 
28114   verifyFormat("result = operand1\n"
28115                "         + operand2 / operand3\n"
28116                "         + operand4 / operand5 * operand6;",
28117                Style);
28118 
28119   verifyFormat("result = operand1 * operand2\n"
28120                "         - operand3 * operand4\n"
28121                "         - operand5\n"
28122                "         + operand6;",
28123                Style);
28124 
28125   verifyFormat("result = operand1 * (operand2 - operand3 * operand4)\n"
28126                "         - operand5\n"
28127                "         + operand6;",
28128                Style);
28129 
28130   verifyFormat("std::uint32_t a = byte_buffer[0]\n"
28131                "                  | byte_buffer[1] << 8\n"
28132                "                  | byte_buffer[2] << 16\n"
28133                "                  | byte_buffer[3] << 24;",
28134                Style);
28135 }
28136 
28137 } // namespace
28138 } // namespace test
28139 } // namespace format
28140 } // namespace clang
28141