xref: /llvm-project/clang/unittests/Format/FormatTest.cpp (revision d1aed486efc6d35a81ca4acbabb4203c4b91cda9)
1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "clang/Format/Format.h"
10 
11 #include "../Tooling/ReplacementTest.h"
12 #include "FormatTestUtils.h"
13 
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/MemoryBuffer.h"
16 #include "gtest/gtest.h"
17 
18 #define DEBUG_TYPE "format-test"
19 
20 using clang::tooling::ReplacementTest;
21 using clang::tooling::toReplacements;
22 using testing::ScopedTrace;
23 
24 namespace clang {
25 namespace format {
26 namespace {
27 
28 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
29 
30 class FormatTest : public ::testing::Test {
31 protected:
32   enum StatusCheck { SC_ExpectComplete, SC_ExpectIncomplete, SC_DoNotCheck };
33 
34   std::string format(llvm::StringRef Code,
35                      const FormatStyle &Style = getLLVMStyle(),
36                      StatusCheck CheckComplete = SC_ExpectComplete) {
37     LLVM_DEBUG(llvm::errs() << "---\n");
38     LLVM_DEBUG(llvm::errs() << Code << "\n\n");
39     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
40     FormattingAttemptStatus Status;
41     tooling::Replacements Replaces =
42         reformat(Style, Code, Ranges, "<stdin>", &Status);
43     if (CheckComplete != SC_DoNotCheck) {
44       bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
45       EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
46           << Code << "\n\n";
47     }
48     ReplacementCount = Replaces.size();
49     auto Result = applyAllReplacements(Code, Replaces);
50     EXPECT_TRUE(static_cast<bool>(Result));
51     LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
52     return *Result;
53   }
54 
55   FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) {
56     Style.ColumnLimit = ColumnLimit;
57     return Style;
58   }
59 
60   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
61     return getStyleWithColumns(getLLVMStyle(), ColumnLimit);
62   }
63 
64   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
65     return getStyleWithColumns(getGoogleStyle(), ColumnLimit);
66   }
67 
68   void _verifyFormat(const char *File, int Line, llvm::StringRef Expected,
69                      llvm::StringRef Code,
70                      const FormatStyle &Style = getLLVMStyle()) {
71     ScopedTrace t(File, Line, ::testing::Message() << Code.str());
72     EXPECT_EQ(Expected.str(), format(Expected, Style))
73         << "Expected code is not stable";
74     EXPECT_EQ(Expected.str(), format(Code, Style));
75     if (Style.Language == FormatStyle::LK_Cpp) {
76       // Objective-C++ is a superset of C++, so everything checked for C++
77       // needs to be checked for Objective-C++ as well.
78       FormatStyle ObjCStyle = Style;
79       ObjCStyle.Language = FormatStyle::LK_ObjC;
80       EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle));
81     }
82   }
83 
84   void _verifyFormat(const char *File, int Line, llvm::StringRef Code,
85                      const FormatStyle &Style = getLLVMStyle()) {
86     _verifyFormat(File, Line, Code, test::messUp(Code), Style);
87   }
88 
89   void _verifyIncompleteFormat(const char *File, int Line, llvm::StringRef Code,
90                                const FormatStyle &Style = getLLVMStyle()) {
91     ScopedTrace t(File, Line, ::testing::Message() << Code.str());
92     EXPECT_EQ(Code.str(),
93               format(test::messUp(Code), Style, SC_ExpectIncomplete));
94   }
95 
96   void _verifyIndependentOfContext(const char *File, int Line,
97                                    llvm::StringRef Text,
98                                    const FormatStyle &Style = getLLVMStyle()) {
99     _verifyFormat(File, Line, Text, Style);
100     _verifyFormat(File, Line, llvm::Twine("void f() { " + Text + " }").str(),
101                   Style);
102   }
103 
104   /// \brief Verify that clang-format does not crash on the given input.
105   void verifyNoCrash(llvm::StringRef Code,
106                      const FormatStyle &Style = getLLVMStyle()) {
107     format(Code, Style, SC_DoNotCheck);
108   }
109 
110   int ReplacementCount;
111 };
112 
113 #define verifyIndependentOfContext(...)                                        \
114   _verifyIndependentOfContext(__FILE__, __LINE__, __VA_ARGS__)
115 #define verifyIncompleteFormat(...)                                            \
116   _verifyIncompleteFormat(__FILE__, __LINE__, __VA_ARGS__)
117 #define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__)
118 #define verifyGoogleFormat(Code) verifyFormat(Code, getGoogleStyle())
119 
120 TEST_F(FormatTest, MessUp) {
121   EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
122   EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
123   EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
124   EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
125   EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
126 }
127 
128 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) {
129   EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language);
130 }
131 
132 TEST_F(FormatTest, LLVMStyleOverride) {
133   EXPECT_EQ(FormatStyle::LK_Proto,
134             getLLVMStyle(FormatStyle::LK_Proto).Language);
135 }
136 
137 //===----------------------------------------------------------------------===//
138 // Basic function tests.
139 //===----------------------------------------------------------------------===//
140 
141 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
142   EXPECT_EQ(";", format(";"));
143 }
144 
145 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
146   EXPECT_EQ("int i;", format("  int i;"));
147   EXPECT_EQ("\nint i;", format(" \n\t \v \f  int i;"));
148   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
149   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
150 }
151 
152 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
153   EXPECT_EQ("int i;", format("int\ni;"));
154 }
155 
156 TEST_F(FormatTest, FormatsNestedBlockStatements) {
157   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
158 }
159 
160 TEST_F(FormatTest, FormatsNestedCall) {
161   verifyFormat("Method(f1, f2(f3));");
162   verifyFormat("Method(f1(f2, f3()));");
163   verifyFormat("Method(f1(f2, (f3())));");
164 }
165 
166 TEST_F(FormatTest, NestedNameSpecifiers) {
167   verifyFormat("vector<::Type> v;");
168   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
169   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
170   verifyFormat("static constexpr bool Bar = typeof(bar())::value;");
171   verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;");
172   verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;");
173   verifyFormat("bool a = 2 < ::SomeFunction();");
174   verifyFormat("ALWAYS_INLINE ::std::string getName();");
175   verifyFormat("some::string getName();");
176 }
177 
178 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
179   EXPECT_EQ("if (a) {\n"
180             "  f();\n"
181             "}",
182             format("if(a){f();}"));
183   EXPECT_EQ(4, ReplacementCount);
184   EXPECT_EQ("if (a) {\n"
185             "  f();\n"
186             "}",
187             format("if (a) {\n"
188                    "  f();\n"
189                    "}"));
190   EXPECT_EQ(0, ReplacementCount);
191   EXPECT_EQ("/*\r\n"
192             "\r\n"
193             "*/\r\n",
194             format("/*\r\n"
195                    "\r\n"
196                    "*/\r\n"));
197   EXPECT_EQ(0, ReplacementCount);
198 }
199 
200 TEST_F(FormatTest, RemovesEmptyLines) {
201   EXPECT_EQ("class C {\n"
202             "  int i;\n"
203             "};",
204             format("class C {\n"
205                    " int i;\n"
206                    "\n"
207                    "};"));
208 
209   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
210   EXPECT_EQ("namespace N {\n"
211             "\n"
212             "int i;\n"
213             "}",
214             format("namespace N {\n"
215                    "\n"
216                    "int    i;\n"
217                    "}",
218                    getGoogleStyle()));
219   EXPECT_EQ("/* something */ namespace N {\n"
220             "\n"
221             "int i;\n"
222             "}",
223             format("/* something */ namespace N {\n"
224                    "\n"
225                    "int    i;\n"
226                    "}",
227                    getGoogleStyle()));
228   EXPECT_EQ("inline namespace N {\n"
229             "\n"
230             "int i;\n"
231             "}",
232             format("inline namespace N {\n"
233                    "\n"
234                    "int    i;\n"
235                    "}",
236                    getGoogleStyle()));
237   EXPECT_EQ("/* something */ inline namespace N {\n"
238             "\n"
239             "int i;\n"
240             "}",
241             format("/* something */ inline namespace N {\n"
242                    "\n"
243                    "int    i;\n"
244                    "}",
245                    getGoogleStyle()));
246   EXPECT_EQ("export namespace N {\n"
247             "\n"
248             "int i;\n"
249             "}",
250             format("export namespace N {\n"
251                    "\n"
252                    "int    i;\n"
253                    "}",
254                    getGoogleStyle()));
255   EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
256             "\n"
257             "int i;\n"
258             "}",
259             format("extern /**/ \"C\" /**/ {\n"
260                    "\n"
261                    "int    i;\n"
262                    "}",
263                    getGoogleStyle()));
264 
265   auto CustomStyle = getLLVMStyle();
266   CustomStyle.BreakBeforeBraces = FormatStyle::BS_Custom;
267   CustomStyle.BraceWrapping.AfterNamespace = true;
268   CustomStyle.KeepEmptyLinesAtTheStartOfBlocks = false;
269   EXPECT_EQ("namespace N\n"
270             "{\n"
271             "\n"
272             "int i;\n"
273             "}",
274             format("namespace N\n"
275                    "{\n"
276                    "\n"
277                    "\n"
278                    "int    i;\n"
279                    "}",
280                    CustomStyle));
281   EXPECT_EQ("/* something */ namespace N\n"
282             "{\n"
283             "\n"
284             "int i;\n"
285             "}",
286             format("/* something */ namespace N {\n"
287                    "\n"
288                    "\n"
289                    "int    i;\n"
290                    "}",
291                    CustomStyle));
292   EXPECT_EQ("inline namespace N\n"
293             "{\n"
294             "\n"
295             "int i;\n"
296             "}",
297             format("inline namespace N\n"
298                    "{\n"
299                    "\n"
300                    "\n"
301                    "int    i;\n"
302                    "}",
303                    CustomStyle));
304   EXPECT_EQ("/* something */ inline namespace N\n"
305             "{\n"
306             "\n"
307             "int i;\n"
308             "}",
309             format("/* something */ inline namespace N\n"
310                    "{\n"
311                    "\n"
312                    "int    i;\n"
313                    "}",
314                    CustomStyle));
315   EXPECT_EQ("export namespace N\n"
316             "{\n"
317             "\n"
318             "int i;\n"
319             "}",
320             format("export namespace N\n"
321                    "{\n"
322                    "\n"
323                    "int    i;\n"
324                    "}",
325                    CustomStyle));
326   EXPECT_EQ("namespace a\n"
327             "{\n"
328             "namespace b\n"
329             "{\n"
330             "\n"
331             "class AA {};\n"
332             "\n"
333             "} // namespace b\n"
334             "} // namespace a\n",
335             format("namespace a\n"
336                    "{\n"
337                    "namespace b\n"
338                    "{\n"
339                    "\n"
340                    "\n"
341                    "class AA {};\n"
342                    "\n"
343                    "\n"
344                    "}\n"
345                    "}\n",
346                    CustomStyle));
347   EXPECT_EQ("namespace A /* comment */\n"
348             "{\n"
349             "class B {}\n"
350             "} // namespace A",
351             format("namespace A /* comment */ { class B {} }", CustomStyle));
352   EXPECT_EQ("namespace A\n"
353             "{ /* comment */\n"
354             "class B {}\n"
355             "} // namespace A",
356             format("namespace A {/* comment */ class B {} }", CustomStyle));
357   EXPECT_EQ("namespace A\n"
358             "{ /* comment */\n"
359             "\n"
360             "class B {}\n"
361             "\n"
362             ""
363             "} // namespace A",
364             format("namespace A { /* comment */\n"
365                    "\n"
366                    "\n"
367                    "class B {}\n"
368                    "\n"
369                    "\n"
370                    "}",
371                    CustomStyle));
372   EXPECT_EQ("namespace A /* comment */\n"
373             "{\n"
374             "\n"
375             "class B {}\n"
376             "\n"
377             "} // namespace A",
378             format("namespace A/* comment */ {\n"
379                    "\n"
380                    "\n"
381                    "class B {}\n"
382                    "\n"
383                    "\n"
384                    "}",
385                    CustomStyle));
386 
387   // ...but do keep inlining and removing empty lines for non-block extern "C"
388   // functions.
389   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
390   EXPECT_EQ("extern \"C\" int f() {\n"
391             "  int i = 42;\n"
392             "  return i;\n"
393             "}",
394             format("extern \"C\" int f() {\n"
395                    "\n"
396                    "  int i = 42;\n"
397                    "  return i;\n"
398                    "}",
399                    getGoogleStyle()));
400 
401   // Remove empty lines at the beginning and end of blocks.
402   EXPECT_EQ("void f() {\n"
403             "\n"
404             "  if (a) {\n"
405             "\n"
406             "    f();\n"
407             "  }\n"
408             "}",
409             format("void f() {\n"
410                    "\n"
411                    "  if (a) {\n"
412                    "\n"
413                    "    f();\n"
414                    "\n"
415                    "  }\n"
416                    "\n"
417                    "}",
418                    getLLVMStyle()));
419   EXPECT_EQ("void f() {\n"
420             "  if (a) {\n"
421             "    f();\n"
422             "  }\n"
423             "}",
424             format("void f() {\n"
425                    "\n"
426                    "  if (a) {\n"
427                    "\n"
428                    "    f();\n"
429                    "\n"
430                    "  }\n"
431                    "\n"
432                    "}",
433                    getGoogleStyle()));
434 
435   // Don't remove empty lines in more complex control statements.
436   EXPECT_EQ("void f() {\n"
437             "  if (a) {\n"
438             "    f();\n"
439             "\n"
440             "  } else if (b) {\n"
441             "    f();\n"
442             "  }\n"
443             "}",
444             format("void f() {\n"
445                    "  if (a) {\n"
446                    "    f();\n"
447                    "\n"
448                    "  } else if (b) {\n"
449                    "    f();\n"
450                    "\n"
451                    "  }\n"
452                    "\n"
453                    "}"));
454 
455   // Don't remove empty lines before namespace endings.
456   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
457   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
458   EXPECT_EQ("namespace {\n"
459             "int i;\n"
460             "\n"
461             "}",
462             format("namespace {\n"
463                    "int i;\n"
464                    "\n"
465                    "}",
466                    LLVMWithNoNamespaceFix));
467   EXPECT_EQ("namespace {\n"
468             "int i;\n"
469             "}",
470             format("namespace {\n"
471                    "int i;\n"
472                    "}",
473                    LLVMWithNoNamespaceFix));
474   EXPECT_EQ("namespace {\n"
475             "int i;\n"
476             "\n"
477             "};",
478             format("namespace {\n"
479                    "int i;\n"
480                    "\n"
481                    "};",
482                    LLVMWithNoNamespaceFix));
483   EXPECT_EQ("namespace {\n"
484             "int i;\n"
485             "};",
486             format("namespace {\n"
487                    "int i;\n"
488                    "};",
489                    LLVMWithNoNamespaceFix));
490   EXPECT_EQ("namespace {\n"
491             "int i;\n"
492             "\n"
493             "}",
494             format("namespace {\n"
495                    "int i;\n"
496                    "\n"
497                    "}"));
498   EXPECT_EQ("namespace {\n"
499             "int i;\n"
500             "\n"
501             "} // namespace",
502             format("namespace {\n"
503                    "int i;\n"
504                    "\n"
505                    "}  // namespace"));
506 
507   FormatStyle Style = getLLVMStyle();
508   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
509   Style.MaxEmptyLinesToKeep = 2;
510   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
511   Style.BraceWrapping.AfterClass = true;
512   Style.BraceWrapping.AfterFunction = true;
513   Style.KeepEmptyLinesAtTheStartOfBlocks = false;
514 
515   EXPECT_EQ("class Foo\n"
516             "{\n"
517             "  Foo() {}\n"
518             "\n"
519             "  void funk() {}\n"
520             "};",
521             format("class Foo\n"
522                    "{\n"
523                    "  Foo()\n"
524                    "  {\n"
525                    "  }\n"
526                    "\n"
527                    "  void funk() {}\n"
528                    "};",
529                    Style));
530 }
531 
532 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
533   verifyFormat("x = (a) and (b);");
534   verifyFormat("x = (a) or (b);");
535   verifyFormat("x = (a) bitand (b);");
536   verifyFormat("x = (a) bitor (b);");
537   verifyFormat("x = (a) not_eq (b);");
538   verifyFormat("x = (a) and_eq (b);");
539   verifyFormat("x = (a) or_eq (b);");
540   verifyFormat("x = (a) xor (b);");
541 }
542 
543 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) {
544   verifyFormat("x = compl(a);");
545   verifyFormat("x = not(a);");
546   verifyFormat("x = bitand(a);");
547   // Unary operator must not be merged with the next identifier
548   verifyFormat("x = compl a;");
549   verifyFormat("x = not a;");
550   verifyFormat("x = bitand a;");
551 }
552 
553 //===----------------------------------------------------------------------===//
554 // Tests for control statements.
555 //===----------------------------------------------------------------------===//
556 
557 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
558   verifyFormat("if (true)\n  f();\ng();");
559   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
560   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
561   verifyFormat("if constexpr (true)\n"
562                "  f();\ng();");
563   verifyFormat("if CONSTEXPR (true)\n"
564                "  f();\ng();");
565   verifyFormat("if constexpr (a)\n"
566                "  if constexpr (b)\n"
567                "    if constexpr (c)\n"
568                "      g();\n"
569                "h();");
570   verifyFormat("if CONSTEXPR (a)\n"
571                "  if CONSTEXPR (b)\n"
572                "    if CONSTEXPR (c)\n"
573                "      g();\n"
574                "h();");
575   verifyFormat("if constexpr (a)\n"
576                "  if constexpr (b) {\n"
577                "    f();\n"
578                "  }\n"
579                "g();");
580   verifyFormat("if CONSTEXPR (a)\n"
581                "  if CONSTEXPR (b) {\n"
582                "    f();\n"
583                "  }\n"
584                "g();");
585 
586   verifyFormat("if (a)\n"
587                "  g();");
588   verifyFormat("if (a) {\n"
589                "  g()\n"
590                "};");
591   verifyFormat("if (a)\n"
592                "  g();\n"
593                "else\n"
594                "  g();");
595   verifyFormat("if (a) {\n"
596                "  g();\n"
597                "} else\n"
598                "  g();");
599   verifyFormat("if (a)\n"
600                "  g();\n"
601                "else {\n"
602                "  g();\n"
603                "}");
604   verifyFormat("if (a) {\n"
605                "  g();\n"
606                "} else {\n"
607                "  g();\n"
608                "}");
609   verifyFormat("if (a)\n"
610                "  g();\n"
611                "else if (b)\n"
612                "  g();\n"
613                "else\n"
614                "  g();");
615   verifyFormat("if (a) {\n"
616                "  g();\n"
617                "} else if (b)\n"
618                "  g();\n"
619                "else\n"
620                "  g();");
621   verifyFormat("if (a)\n"
622                "  g();\n"
623                "else if (b) {\n"
624                "  g();\n"
625                "} else\n"
626                "  g();");
627   verifyFormat("if (a)\n"
628                "  g();\n"
629                "else if (b)\n"
630                "  g();\n"
631                "else {\n"
632                "  g();\n"
633                "}");
634   verifyFormat("if (a)\n"
635                "  g();\n"
636                "else if (b) {\n"
637                "  g();\n"
638                "} else {\n"
639                "  g();\n"
640                "}");
641   verifyFormat("if (a) {\n"
642                "  g();\n"
643                "} else if (b) {\n"
644                "  g();\n"
645                "} else {\n"
646                "  g();\n"
647                "}");
648 
649   FormatStyle AllowsMergedIf = getLLVMStyle();
650   AllowsMergedIf.IfMacros.push_back("MYIF");
651   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
652   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
653       FormatStyle::SIS_WithoutElse;
654   verifyFormat("if (a)\n"
655                "  // comment\n"
656                "  f();",
657                AllowsMergedIf);
658   verifyFormat("{\n"
659                "  if (a)\n"
660                "  label:\n"
661                "    f();\n"
662                "}",
663                AllowsMergedIf);
664   verifyFormat("#define A \\\n"
665                "  if (a)  \\\n"
666                "  label:  \\\n"
667                "    f()",
668                AllowsMergedIf);
669   verifyFormat("if (a)\n"
670                "  ;",
671                AllowsMergedIf);
672   verifyFormat("if (a)\n"
673                "  if (b) return;",
674                AllowsMergedIf);
675 
676   verifyFormat("if (a) // Can't merge this\n"
677                "  f();\n",
678                AllowsMergedIf);
679   verifyFormat("if (a) /* still don't merge */\n"
680                "  f();",
681                AllowsMergedIf);
682   verifyFormat("if (a) { // Never merge this\n"
683                "  f();\n"
684                "}",
685                AllowsMergedIf);
686   verifyFormat("if (a) { /* Never merge this */\n"
687                "  f();\n"
688                "}",
689                AllowsMergedIf);
690   verifyFormat("MYIF (a)\n"
691                "  // comment\n"
692                "  f();",
693                AllowsMergedIf);
694   verifyFormat("{\n"
695                "  MYIF (a)\n"
696                "  label:\n"
697                "    f();\n"
698                "}",
699                AllowsMergedIf);
700   verifyFormat("#define A  \\\n"
701                "  MYIF (a) \\\n"
702                "  label:   \\\n"
703                "    f()",
704                AllowsMergedIf);
705   verifyFormat("MYIF (a)\n"
706                "  ;",
707                AllowsMergedIf);
708   verifyFormat("MYIF (a)\n"
709                "  MYIF (b) return;",
710                AllowsMergedIf);
711 
712   verifyFormat("MYIF (a) // Can't merge this\n"
713                "  f();\n",
714                AllowsMergedIf);
715   verifyFormat("MYIF (a) /* still don't merge */\n"
716                "  f();",
717                AllowsMergedIf);
718   verifyFormat("MYIF (a) { // Never merge this\n"
719                "  f();\n"
720                "}",
721                AllowsMergedIf);
722   verifyFormat("MYIF (a) { /* Never merge this */\n"
723                "  f();\n"
724                "}",
725                AllowsMergedIf);
726 
727   AllowsMergedIf.ColumnLimit = 14;
728   // Where line-lengths matter, a 2-letter synonym that maintains line length.
729   // Not IF to avoid any confusion that IF is somehow special.
730   AllowsMergedIf.IfMacros.push_back("FI");
731   verifyFormat("if (a) return;", AllowsMergedIf);
732   verifyFormat("if (aaaaaaaaa)\n"
733                "  return;",
734                AllowsMergedIf);
735   verifyFormat("FI (a) return;", AllowsMergedIf);
736   verifyFormat("FI (aaaaaaaaa)\n"
737                "  return;",
738                AllowsMergedIf);
739 
740   AllowsMergedIf.ColumnLimit = 13;
741   verifyFormat("if (a)\n  return;", AllowsMergedIf);
742   verifyFormat("FI (a)\n  return;", AllowsMergedIf);
743 
744   FormatStyle AllowsMergedIfElse = getLLVMStyle();
745   AllowsMergedIfElse.IfMacros.push_back("MYIF");
746   AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine =
747       FormatStyle::SIS_AllIfsAndElse;
748   verifyFormat("if (a)\n"
749                "  // comment\n"
750                "  f();\n"
751                "else\n"
752                "  // comment\n"
753                "  f();",
754                AllowsMergedIfElse);
755   verifyFormat("{\n"
756                "  if (a)\n"
757                "  label:\n"
758                "    f();\n"
759                "  else\n"
760                "  label:\n"
761                "    f();\n"
762                "}",
763                AllowsMergedIfElse);
764   verifyFormat("if (a)\n"
765                "  ;\n"
766                "else\n"
767                "  ;",
768                AllowsMergedIfElse);
769   verifyFormat("if (a) {\n"
770                "} else {\n"
771                "}",
772                AllowsMergedIfElse);
773   verifyFormat("if (a) return;\n"
774                "else if (b) return;\n"
775                "else return;",
776                AllowsMergedIfElse);
777   verifyFormat("if (a) {\n"
778                "} else return;",
779                AllowsMergedIfElse);
780   verifyFormat("if (a) {\n"
781                "} else if (b) return;\n"
782                "else return;",
783                AllowsMergedIfElse);
784   verifyFormat("if (a) return;\n"
785                "else if (b) {\n"
786                "} else return;",
787                AllowsMergedIfElse);
788   verifyFormat("if (a)\n"
789                "  if (b) return;\n"
790                "  else return;",
791                AllowsMergedIfElse);
792   verifyFormat("if constexpr (a)\n"
793                "  if constexpr (b) return;\n"
794                "  else if constexpr (c) return;\n"
795                "  else return;",
796                AllowsMergedIfElse);
797   verifyFormat("MYIF (a)\n"
798                "  // comment\n"
799                "  f();\n"
800                "else\n"
801                "  // comment\n"
802                "  f();",
803                AllowsMergedIfElse);
804   verifyFormat("{\n"
805                "  MYIF (a)\n"
806                "  label:\n"
807                "    f();\n"
808                "  else\n"
809                "  label:\n"
810                "    f();\n"
811                "}",
812                AllowsMergedIfElse);
813   verifyFormat("MYIF (a)\n"
814                "  ;\n"
815                "else\n"
816                "  ;",
817                AllowsMergedIfElse);
818   verifyFormat("MYIF (a) {\n"
819                "} else {\n"
820                "}",
821                AllowsMergedIfElse);
822   verifyFormat("MYIF (a) return;\n"
823                "else MYIF (b) return;\n"
824                "else return;",
825                AllowsMergedIfElse);
826   verifyFormat("MYIF (a) {\n"
827                "} else return;",
828                AllowsMergedIfElse);
829   verifyFormat("MYIF (a) {\n"
830                "} else MYIF (b) return;\n"
831                "else return;",
832                AllowsMergedIfElse);
833   verifyFormat("MYIF (a) return;\n"
834                "else MYIF (b) {\n"
835                "} else return;",
836                AllowsMergedIfElse);
837   verifyFormat("MYIF (a)\n"
838                "  MYIF (b) return;\n"
839                "  else return;",
840                AllowsMergedIfElse);
841   verifyFormat("MYIF constexpr (a)\n"
842                "  MYIF constexpr (b) return;\n"
843                "  else MYIF constexpr (c) return;\n"
844                "  else return;",
845                AllowsMergedIfElse);
846 }
847 
848 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) {
849   FormatStyle AllowsMergedIf = getLLVMStyle();
850   AllowsMergedIf.IfMacros.push_back("MYIF");
851   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
852   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
853       FormatStyle::SIS_WithoutElse;
854   verifyFormat("if (a)\n"
855                "  f();\n"
856                "else {\n"
857                "  g();\n"
858                "}",
859                AllowsMergedIf);
860   verifyFormat("if (a)\n"
861                "  f();\n"
862                "else\n"
863                "  g();\n",
864                AllowsMergedIf);
865 
866   verifyFormat("if (a) g();", AllowsMergedIf);
867   verifyFormat("if (a) {\n"
868                "  g()\n"
869                "};",
870                AllowsMergedIf);
871   verifyFormat("if (a)\n"
872                "  g();\n"
873                "else\n"
874                "  g();",
875                AllowsMergedIf);
876   verifyFormat("if (a) {\n"
877                "  g();\n"
878                "} else\n"
879                "  g();",
880                AllowsMergedIf);
881   verifyFormat("if (a)\n"
882                "  g();\n"
883                "else {\n"
884                "  g();\n"
885                "}",
886                AllowsMergedIf);
887   verifyFormat("if (a) {\n"
888                "  g();\n"
889                "} else {\n"
890                "  g();\n"
891                "}",
892                AllowsMergedIf);
893   verifyFormat("if (a)\n"
894                "  g();\n"
895                "else if (b)\n"
896                "  g();\n"
897                "else\n"
898                "  g();",
899                AllowsMergedIf);
900   verifyFormat("if (a) {\n"
901                "  g();\n"
902                "} else if (b)\n"
903                "  g();\n"
904                "else\n"
905                "  g();",
906                AllowsMergedIf);
907   verifyFormat("if (a)\n"
908                "  g();\n"
909                "else if (b) {\n"
910                "  g();\n"
911                "} else\n"
912                "  g();",
913                AllowsMergedIf);
914   verifyFormat("if (a)\n"
915                "  g();\n"
916                "else if (b)\n"
917                "  g();\n"
918                "else {\n"
919                "  g();\n"
920                "}",
921                AllowsMergedIf);
922   verifyFormat("if (a)\n"
923                "  g();\n"
924                "else if (b) {\n"
925                "  g();\n"
926                "} else {\n"
927                "  g();\n"
928                "}",
929                AllowsMergedIf);
930   verifyFormat("if (a) {\n"
931                "  g();\n"
932                "} else if (b) {\n"
933                "  g();\n"
934                "} else {\n"
935                "  g();\n"
936                "}",
937                AllowsMergedIf);
938   verifyFormat("MYIF (a)\n"
939                "  f();\n"
940                "else {\n"
941                "  g();\n"
942                "}",
943                AllowsMergedIf);
944   verifyFormat("MYIF (a)\n"
945                "  f();\n"
946                "else\n"
947                "  g();\n",
948                AllowsMergedIf);
949 
950   verifyFormat("MYIF (a) g();", AllowsMergedIf);
951   verifyFormat("MYIF (a) {\n"
952                "  g()\n"
953                "};",
954                AllowsMergedIf);
955   verifyFormat("MYIF (a)\n"
956                "  g();\n"
957                "else\n"
958                "  g();",
959                AllowsMergedIf);
960   verifyFormat("MYIF (a) {\n"
961                "  g();\n"
962                "} else\n"
963                "  g();",
964                AllowsMergedIf);
965   verifyFormat("MYIF (a)\n"
966                "  g();\n"
967                "else {\n"
968                "  g();\n"
969                "}",
970                AllowsMergedIf);
971   verifyFormat("MYIF (a) {\n"
972                "  g();\n"
973                "} else {\n"
974                "  g();\n"
975                "}",
976                AllowsMergedIf);
977   verifyFormat("MYIF (a)\n"
978                "  g();\n"
979                "else MYIF (b)\n"
980                "  g();\n"
981                "else\n"
982                "  g();",
983                AllowsMergedIf);
984   verifyFormat("MYIF (a)\n"
985                "  g();\n"
986                "else if (b)\n"
987                "  g();\n"
988                "else\n"
989                "  g();",
990                AllowsMergedIf);
991   verifyFormat("MYIF (a) {\n"
992                "  g();\n"
993                "} else MYIF (b)\n"
994                "  g();\n"
995                "else\n"
996                "  g();",
997                AllowsMergedIf);
998   verifyFormat("MYIF (a) {\n"
999                "  g();\n"
1000                "} else if (b)\n"
1001                "  g();\n"
1002                "else\n"
1003                "  g();",
1004                AllowsMergedIf);
1005   verifyFormat("MYIF (a)\n"
1006                "  g();\n"
1007                "else MYIF (b) {\n"
1008                "  g();\n"
1009                "} else\n"
1010                "  g();",
1011                AllowsMergedIf);
1012   verifyFormat("MYIF (a)\n"
1013                "  g();\n"
1014                "else if (b) {\n"
1015                "  g();\n"
1016                "} else\n"
1017                "  g();",
1018                AllowsMergedIf);
1019   verifyFormat("MYIF (a)\n"
1020                "  g();\n"
1021                "else MYIF (b)\n"
1022                "  g();\n"
1023                "else {\n"
1024                "  g();\n"
1025                "}",
1026                AllowsMergedIf);
1027   verifyFormat("MYIF (a)\n"
1028                "  g();\n"
1029                "else if (b)\n"
1030                "  g();\n"
1031                "else {\n"
1032                "  g();\n"
1033                "}",
1034                AllowsMergedIf);
1035   verifyFormat("MYIF (a)\n"
1036                "  g();\n"
1037                "else MYIF (b) {\n"
1038                "  g();\n"
1039                "} else {\n"
1040                "  g();\n"
1041                "}",
1042                AllowsMergedIf);
1043   verifyFormat("MYIF (a)\n"
1044                "  g();\n"
1045                "else if (b) {\n"
1046                "  g();\n"
1047                "} else {\n"
1048                "  g();\n"
1049                "}",
1050                AllowsMergedIf);
1051   verifyFormat("MYIF (a) {\n"
1052                "  g();\n"
1053                "} else MYIF (b) {\n"
1054                "  g();\n"
1055                "} else {\n"
1056                "  g();\n"
1057                "}",
1058                AllowsMergedIf);
1059   verifyFormat("MYIF (a) {\n"
1060                "  g();\n"
1061                "} else if (b) {\n"
1062                "  g();\n"
1063                "} else {\n"
1064                "  g();\n"
1065                "}",
1066                AllowsMergedIf);
1067 
1068   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
1069       FormatStyle::SIS_OnlyFirstIf;
1070 
1071   verifyFormat("if (a) f();\n"
1072                "else {\n"
1073                "  g();\n"
1074                "}",
1075                AllowsMergedIf);
1076   verifyFormat("if (a) f();\n"
1077                "else {\n"
1078                "  if (a) f();\n"
1079                "  else {\n"
1080                "    g();\n"
1081                "  }\n"
1082                "  g();\n"
1083                "}",
1084                AllowsMergedIf);
1085 
1086   verifyFormat("if (a) g();", AllowsMergedIf);
1087   verifyFormat("if (a) {\n"
1088                "  g()\n"
1089                "};",
1090                AllowsMergedIf);
1091   verifyFormat("if (a) g();\n"
1092                "else\n"
1093                "  g();",
1094                AllowsMergedIf);
1095   verifyFormat("if (a) {\n"
1096                "  g();\n"
1097                "} else\n"
1098                "  g();",
1099                AllowsMergedIf);
1100   verifyFormat("if (a) g();\n"
1101                "else {\n"
1102                "  g();\n"
1103                "}",
1104                AllowsMergedIf);
1105   verifyFormat("if (a) {\n"
1106                "  g();\n"
1107                "} else {\n"
1108                "  g();\n"
1109                "}",
1110                AllowsMergedIf);
1111   verifyFormat("if (a) g();\n"
1112                "else if (b)\n"
1113                "  g();\n"
1114                "else\n"
1115                "  g();",
1116                AllowsMergedIf);
1117   verifyFormat("if (a) {\n"
1118                "  g();\n"
1119                "} else if (b)\n"
1120                "  g();\n"
1121                "else\n"
1122                "  g();",
1123                AllowsMergedIf);
1124   verifyFormat("if (a) g();\n"
1125                "else if (b) {\n"
1126                "  g();\n"
1127                "} else\n"
1128                "  g();",
1129                AllowsMergedIf);
1130   verifyFormat("if (a) g();\n"
1131                "else if (b)\n"
1132                "  g();\n"
1133                "else {\n"
1134                "  g();\n"
1135                "}",
1136                AllowsMergedIf);
1137   verifyFormat("if (a) g();\n"
1138                "else if (b) {\n"
1139                "  g();\n"
1140                "} else {\n"
1141                "  g();\n"
1142                "}",
1143                AllowsMergedIf);
1144   verifyFormat("if (a) {\n"
1145                "  g();\n"
1146                "} else if (b) {\n"
1147                "  g();\n"
1148                "} else {\n"
1149                "  g();\n"
1150                "}",
1151                AllowsMergedIf);
1152   verifyFormat("MYIF (a) f();\n"
1153                "else {\n"
1154                "  g();\n"
1155                "}",
1156                AllowsMergedIf);
1157   verifyFormat("MYIF (a) f();\n"
1158                "else {\n"
1159                "  if (a) f();\n"
1160                "  else {\n"
1161                "    g();\n"
1162                "  }\n"
1163                "  g();\n"
1164                "}",
1165                AllowsMergedIf);
1166 
1167   verifyFormat("MYIF (a) g();", AllowsMergedIf);
1168   verifyFormat("MYIF (a) {\n"
1169                "  g()\n"
1170                "};",
1171                AllowsMergedIf);
1172   verifyFormat("MYIF (a) g();\n"
1173                "else\n"
1174                "  g();",
1175                AllowsMergedIf);
1176   verifyFormat("MYIF (a) {\n"
1177                "  g();\n"
1178                "} else\n"
1179                "  g();",
1180                AllowsMergedIf);
1181   verifyFormat("MYIF (a) g();\n"
1182                "else {\n"
1183                "  g();\n"
1184                "}",
1185                AllowsMergedIf);
1186   verifyFormat("MYIF (a) {\n"
1187                "  g();\n"
1188                "} else {\n"
1189                "  g();\n"
1190                "}",
1191                AllowsMergedIf);
1192   verifyFormat("MYIF (a) g();\n"
1193                "else MYIF (b)\n"
1194                "  g();\n"
1195                "else\n"
1196                "  g();",
1197                AllowsMergedIf);
1198   verifyFormat("MYIF (a) g();\n"
1199                "else if (b)\n"
1200                "  g();\n"
1201                "else\n"
1202                "  g();",
1203                AllowsMergedIf);
1204   verifyFormat("MYIF (a) {\n"
1205                "  g();\n"
1206                "} else MYIF (b)\n"
1207                "  g();\n"
1208                "else\n"
1209                "  g();",
1210                AllowsMergedIf);
1211   verifyFormat("MYIF (a) {\n"
1212                "  g();\n"
1213                "} else if (b)\n"
1214                "  g();\n"
1215                "else\n"
1216                "  g();",
1217                AllowsMergedIf);
1218   verifyFormat("MYIF (a) g();\n"
1219                "else MYIF (b) {\n"
1220                "  g();\n"
1221                "} else\n"
1222                "  g();",
1223                AllowsMergedIf);
1224   verifyFormat("MYIF (a) g();\n"
1225                "else if (b) {\n"
1226                "  g();\n"
1227                "} else\n"
1228                "  g();",
1229                AllowsMergedIf);
1230   verifyFormat("MYIF (a) g();\n"
1231                "else MYIF (b)\n"
1232                "  g();\n"
1233                "else {\n"
1234                "  g();\n"
1235                "}",
1236                AllowsMergedIf);
1237   verifyFormat("MYIF (a) g();\n"
1238                "else if (b)\n"
1239                "  g();\n"
1240                "else {\n"
1241                "  g();\n"
1242                "}",
1243                AllowsMergedIf);
1244   verifyFormat("MYIF (a) g();\n"
1245                "else MYIF (b) {\n"
1246                "  g();\n"
1247                "} else {\n"
1248                "  g();\n"
1249                "}",
1250                AllowsMergedIf);
1251   verifyFormat("MYIF (a) g();\n"
1252                "else if (b) {\n"
1253                "  g();\n"
1254                "} else {\n"
1255                "  g();\n"
1256                "}",
1257                AllowsMergedIf);
1258   verifyFormat("MYIF (a) {\n"
1259                "  g();\n"
1260                "} else MYIF (b) {\n"
1261                "  g();\n"
1262                "} else {\n"
1263                "  g();\n"
1264                "}",
1265                AllowsMergedIf);
1266   verifyFormat("MYIF (a) {\n"
1267                "  g();\n"
1268                "} else if (b) {\n"
1269                "  g();\n"
1270                "} else {\n"
1271                "  g();\n"
1272                "}",
1273                AllowsMergedIf);
1274 
1275   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
1276       FormatStyle::SIS_AllIfsAndElse;
1277 
1278   verifyFormat("if (a) f();\n"
1279                "else {\n"
1280                "  g();\n"
1281                "}",
1282                AllowsMergedIf);
1283   verifyFormat("if (a) f();\n"
1284                "else {\n"
1285                "  if (a) f();\n"
1286                "  else {\n"
1287                "    g();\n"
1288                "  }\n"
1289                "  g();\n"
1290                "}",
1291                AllowsMergedIf);
1292 
1293   verifyFormat("if (a) g();", AllowsMergedIf);
1294   verifyFormat("if (a) {\n"
1295                "  g()\n"
1296                "};",
1297                AllowsMergedIf);
1298   verifyFormat("if (a) g();\n"
1299                "else g();",
1300                AllowsMergedIf);
1301   verifyFormat("if (a) {\n"
1302                "  g();\n"
1303                "} else g();",
1304                AllowsMergedIf);
1305   verifyFormat("if (a) g();\n"
1306                "else {\n"
1307                "  g();\n"
1308                "}",
1309                AllowsMergedIf);
1310   verifyFormat("if (a) {\n"
1311                "  g();\n"
1312                "} else {\n"
1313                "  g();\n"
1314                "}",
1315                AllowsMergedIf);
1316   verifyFormat("if (a) g();\n"
1317                "else if (b) g();\n"
1318                "else g();",
1319                AllowsMergedIf);
1320   verifyFormat("if (a) {\n"
1321                "  g();\n"
1322                "} else if (b) g();\n"
1323                "else g();",
1324                AllowsMergedIf);
1325   verifyFormat("if (a) g();\n"
1326                "else if (b) {\n"
1327                "  g();\n"
1328                "} else g();",
1329                AllowsMergedIf);
1330   verifyFormat("if (a) g();\n"
1331                "else if (b) g();\n"
1332                "else {\n"
1333                "  g();\n"
1334                "}",
1335                AllowsMergedIf);
1336   verifyFormat("if (a) g();\n"
1337                "else if (b) {\n"
1338                "  g();\n"
1339                "} else {\n"
1340                "  g();\n"
1341                "}",
1342                AllowsMergedIf);
1343   verifyFormat("if (a) {\n"
1344                "  g();\n"
1345                "} else if (b) {\n"
1346                "  g();\n"
1347                "} else {\n"
1348                "  g();\n"
1349                "}",
1350                AllowsMergedIf);
1351   verifyFormat("MYIF (a) f();\n"
1352                "else {\n"
1353                "  g();\n"
1354                "}",
1355                AllowsMergedIf);
1356   verifyFormat("MYIF (a) f();\n"
1357                "else {\n"
1358                "  if (a) f();\n"
1359                "  else {\n"
1360                "    g();\n"
1361                "  }\n"
1362                "  g();\n"
1363                "}",
1364                AllowsMergedIf);
1365 
1366   verifyFormat("MYIF (a) g();", AllowsMergedIf);
1367   verifyFormat("MYIF (a) {\n"
1368                "  g()\n"
1369                "};",
1370                AllowsMergedIf);
1371   verifyFormat("MYIF (a) g();\n"
1372                "else g();",
1373                AllowsMergedIf);
1374   verifyFormat("MYIF (a) {\n"
1375                "  g();\n"
1376                "} else g();",
1377                AllowsMergedIf);
1378   verifyFormat("MYIF (a) g();\n"
1379                "else {\n"
1380                "  g();\n"
1381                "}",
1382                AllowsMergedIf);
1383   verifyFormat("MYIF (a) {\n"
1384                "  g();\n"
1385                "} else {\n"
1386                "  g();\n"
1387                "}",
1388                AllowsMergedIf);
1389   verifyFormat("MYIF (a) g();\n"
1390                "else MYIF (b) g();\n"
1391                "else g();",
1392                AllowsMergedIf);
1393   verifyFormat("MYIF (a) g();\n"
1394                "else if (b) g();\n"
1395                "else g();",
1396                AllowsMergedIf);
1397   verifyFormat("MYIF (a) {\n"
1398                "  g();\n"
1399                "} else MYIF (b) g();\n"
1400                "else g();",
1401                AllowsMergedIf);
1402   verifyFormat("MYIF (a) {\n"
1403                "  g();\n"
1404                "} else if (b) g();\n"
1405                "else g();",
1406                AllowsMergedIf);
1407   verifyFormat("MYIF (a) g();\n"
1408                "else MYIF (b) {\n"
1409                "  g();\n"
1410                "} else g();",
1411                AllowsMergedIf);
1412   verifyFormat("MYIF (a) g();\n"
1413                "else if (b) {\n"
1414                "  g();\n"
1415                "} else g();",
1416                AllowsMergedIf);
1417   verifyFormat("MYIF (a) g();\n"
1418                "else MYIF (b) g();\n"
1419                "else {\n"
1420                "  g();\n"
1421                "}",
1422                AllowsMergedIf);
1423   verifyFormat("MYIF (a) g();\n"
1424                "else if (b) g();\n"
1425                "else {\n"
1426                "  g();\n"
1427                "}",
1428                AllowsMergedIf);
1429   verifyFormat("MYIF (a) g();\n"
1430                "else MYIF (b) {\n"
1431                "  g();\n"
1432                "} else {\n"
1433                "  g();\n"
1434                "}",
1435                AllowsMergedIf);
1436   verifyFormat("MYIF (a) g();\n"
1437                "else if (b) {\n"
1438                "  g();\n"
1439                "} else {\n"
1440                "  g();\n"
1441                "}",
1442                AllowsMergedIf);
1443   verifyFormat("MYIF (a) {\n"
1444                "  g();\n"
1445                "} else MYIF (b) {\n"
1446                "  g();\n"
1447                "} else {\n"
1448                "  g();\n"
1449                "}",
1450                AllowsMergedIf);
1451   verifyFormat("MYIF (a) {\n"
1452                "  g();\n"
1453                "} else if (b) {\n"
1454                "  g();\n"
1455                "} else {\n"
1456                "  g();\n"
1457                "}",
1458                AllowsMergedIf);
1459 }
1460 
1461 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
1462   FormatStyle AllowsMergedLoops = getLLVMStyle();
1463   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
1464   verifyFormat("while (true) continue;", AllowsMergedLoops);
1465   verifyFormat("for (;;) continue;", AllowsMergedLoops);
1466   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
1467   verifyFormat("BOOST_FOREACH (int &v, vec) v *= 2;", AllowsMergedLoops);
1468   verifyFormat("while (true)\n"
1469                "  ;",
1470                AllowsMergedLoops);
1471   verifyFormat("for (;;)\n"
1472                "  ;",
1473                AllowsMergedLoops);
1474   verifyFormat("for (;;)\n"
1475                "  for (;;) continue;",
1476                AllowsMergedLoops);
1477   verifyFormat("for (;;)\n"
1478                "  while (true) continue;",
1479                AllowsMergedLoops);
1480   verifyFormat("while (true)\n"
1481                "  for (;;) continue;",
1482                AllowsMergedLoops);
1483   verifyFormat("BOOST_FOREACH (int &v, vec)\n"
1484                "  for (;;) continue;",
1485                AllowsMergedLoops);
1486   verifyFormat("for (;;)\n"
1487                "  BOOST_FOREACH (int &v, vec) continue;",
1488                AllowsMergedLoops);
1489   verifyFormat("for (;;) // Can't merge this\n"
1490                "  continue;",
1491                AllowsMergedLoops);
1492   verifyFormat("for (;;) /* still don't merge */\n"
1493                "  continue;",
1494                AllowsMergedLoops);
1495   verifyFormat("do a++;\n"
1496                "while (true);",
1497                AllowsMergedLoops);
1498   verifyFormat("do /* Don't merge */\n"
1499                "  a++;\n"
1500                "while (true);",
1501                AllowsMergedLoops);
1502   verifyFormat("do // Don't merge\n"
1503                "  a++;\n"
1504                "while (true);",
1505                AllowsMergedLoops);
1506   verifyFormat("do\n"
1507                "  // Don't merge\n"
1508                "  a++;\n"
1509                "while (true);",
1510                AllowsMergedLoops);
1511   // Without braces labels are interpreted differently.
1512   verifyFormat("{\n"
1513                "  do\n"
1514                "  label:\n"
1515                "    a++;\n"
1516                "  while (true);\n"
1517                "}",
1518                AllowsMergedLoops);
1519 }
1520 
1521 TEST_F(FormatTest, FormatShortBracedStatements) {
1522   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
1523   AllowSimpleBracedStatements.IfMacros.push_back("MYIF");
1524   // Where line-lengths matter, a 2-letter synonym that maintains line length.
1525   // Not IF to avoid any confusion that IF is somehow special.
1526   AllowSimpleBracedStatements.IfMacros.push_back("FI");
1527   AllowSimpleBracedStatements.ColumnLimit = 40;
1528   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
1529       FormatStyle::SBS_Always;
1530 
1531   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1532       FormatStyle::SIS_WithoutElse;
1533   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1534 
1535   AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
1536   AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
1537   AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
1538 
1539   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1540   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1541   verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1542   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1543   verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1544   verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1545   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1546   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1547   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1548   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1549   verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1550   verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1551   verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1552   verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1553   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1554   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1555   verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1556                AllowSimpleBracedStatements);
1557   verifyFormat("if (true) {\n"
1558                "  ffffffffffffffffffffffff();\n"
1559                "}",
1560                AllowSimpleBracedStatements);
1561   verifyFormat("if (true) {\n"
1562                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1563                "}",
1564                AllowSimpleBracedStatements);
1565   verifyFormat("if (true) { //\n"
1566                "  f();\n"
1567                "}",
1568                AllowSimpleBracedStatements);
1569   verifyFormat("if (true) {\n"
1570                "  f();\n"
1571                "  f();\n"
1572                "}",
1573                AllowSimpleBracedStatements);
1574   verifyFormat("if (true) {\n"
1575                "  f();\n"
1576                "} else {\n"
1577                "  f();\n"
1578                "}",
1579                AllowSimpleBracedStatements);
1580   verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1581                AllowSimpleBracedStatements);
1582   verifyFormat("MYIF (true) {\n"
1583                "  ffffffffffffffffffffffff();\n"
1584                "}",
1585                AllowSimpleBracedStatements);
1586   verifyFormat("MYIF (true) {\n"
1587                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1588                "}",
1589                AllowSimpleBracedStatements);
1590   verifyFormat("MYIF (true) { //\n"
1591                "  f();\n"
1592                "}",
1593                AllowSimpleBracedStatements);
1594   verifyFormat("MYIF (true) {\n"
1595                "  f();\n"
1596                "  f();\n"
1597                "}",
1598                AllowSimpleBracedStatements);
1599   verifyFormat("MYIF (true) {\n"
1600                "  f();\n"
1601                "} else {\n"
1602                "  f();\n"
1603                "}",
1604                AllowSimpleBracedStatements);
1605 
1606   verifyFormat("struct A2 {\n"
1607                "  int X;\n"
1608                "};",
1609                AllowSimpleBracedStatements);
1610   verifyFormat("typedef struct A2 {\n"
1611                "  int X;\n"
1612                "} A2_t;",
1613                AllowSimpleBracedStatements);
1614   verifyFormat("template <int> struct A2 {\n"
1615                "  struct B {};\n"
1616                "};",
1617                AllowSimpleBracedStatements);
1618 
1619   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1620       FormatStyle::SIS_Never;
1621   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1622   verifyFormat("if (true) {\n"
1623                "  f();\n"
1624                "}",
1625                AllowSimpleBracedStatements);
1626   verifyFormat("if (true) {\n"
1627                "  f();\n"
1628                "} else {\n"
1629                "  f();\n"
1630                "}",
1631                AllowSimpleBracedStatements);
1632   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1633   verifyFormat("MYIF (true) {\n"
1634                "  f();\n"
1635                "}",
1636                AllowSimpleBracedStatements);
1637   verifyFormat("MYIF (true) {\n"
1638                "  f();\n"
1639                "} else {\n"
1640                "  f();\n"
1641                "}",
1642                AllowSimpleBracedStatements);
1643 
1644   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1645   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1646   verifyFormat("while (true) {\n"
1647                "  f();\n"
1648                "}",
1649                AllowSimpleBracedStatements);
1650   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1651   verifyFormat("for (;;) {\n"
1652                "  f();\n"
1653                "}",
1654                AllowSimpleBracedStatements);
1655   verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements);
1656   verifyFormat("BOOST_FOREACH (int v, vec) {\n"
1657                "  f();\n"
1658                "}",
1659                AllowSimpleBracedStatements);
1660 
1661   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1662       FormatStyle::SIS_WithoutElse;
1663   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1664   AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement =
1665       FormatStyle::BWACS_Always;
1666 
1667   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1668   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1669   verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1670   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1671   verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1672   verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1673   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1674   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1675   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1676   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1677   verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1678   verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1679   verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1680   verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1681   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1682   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1683   verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1684                AllowSimpleBracedStatements);
1685   verifyFormat("if (true)\n"
1686                "{\n"
1687                "  ffffffffffffffffffffffff();\n"
1688                "}",
1689                AllowSimpleBracedStatements);
1690   verifyFormat("if (true)\n"
1691                "{\n"
1692                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1693                "}",
1694                AllowSimpleBracedStatements);
1695   verifyFormat("if (true)\n"
1696                "{ //\n"
1697                "  f();\n"
1698                "}",
1699                AllowSimpleBracedStatements);
1700   verifyFormat("if (true)\n"
1701                "{\n"
1702                "  f();\n"
1703                "  f();\n"
1704                "}",
1705                AllowSimpleBracedStatements);
1706   verifyFormat("if (true)\n"
1707                "{\n"
1708                "  f();\n"
1709                "} else\n"
1710                "{\n"
1711                "  f();\n"
1712                "}",
1713                AllowSimpleBracedStatements);
1714   verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1715                AllowSimpleBracedStatements);
1716   verifyFormat("MYIF (true)\n"
1717                "{\n"
1718                "  ffffffffffffffffffffffff();\n"
1719                "}",
1720                AllowSimpleBracedStatements);
1721   verifyFormat("MYIF (true)\n"
1722                "{\n"
1723                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1724                "}",
1725                AllowSimpleBracedStatements);
1726   verifyFormat("MYIF (true)\n"
1727                "{ //\n"
1728                "  f();\n"
1729                "}",
1730                AllowSimpleBracedStatements);
1731   verifyFormat("MYIF (true)\n"
1732                "{\n"
1733                "  f();\n"
1734                "  f();\n"
1735                "}",
1736                AllowSimpleBracedStatements);
1737   verifyFormat("MYIF (true)\n"
1738                "{\n"
1739                "  f();\n"
1740                "} else\n"
1741                "{\n"
1742                "  f();\n"
1743                "}",
1744                AllowSimpleBracedStatements);
1745 
1746   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1747       FormatStyle::SIS_Never;
1748   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1749   verifyFormat("if (true)\n"
1750                "{\n"
1751                "  f();\n"
1752                "}",
1753                AllowSimpleBracedStatements);
1754   verifyFormat("if (true)\n"
1755                "{\n"
1756                "  f();\n"
1757                "} else\n"
1758                "{\n"
1759                "  f();\n"
1760                "}",
1761                AllowSimpleBracedStatements);
1762   verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1763   verifyFormat("MYIF (true)\n"
1764                "{\n"
1765                "  f();\n"
1766                "}",
1767                AllowSimpleBracedStatements);
1768   verifyFormat("MYIF (true)\n"
1769                "{\n"
1770                "  f();\n"
1771                "} else\n"
1772                "{\n"
1773                "  f();\n"
1774                "}",
1775                AllowSimpleBracedStatements);
1776 
1777   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1778   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1779   verifyFormat("while (true)\n"
1780                "{\n"
1781                "  f();\n"
1782                "}",
1783                AllowSimpleBracedStatements);
1784   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1785   verifyFormat("for (;;)\n"
1786                "{\n"
1787                "  f();\n"
1788                "}",
1789                AllowSimpleBracedStatements);
1790   verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements);
1791   verifyFormat("BOOST_FOREACH (int v, vec)\n"
1792                "{\n"
1793                "  f();\n"
1794                "}",
1795                AllowSimpleBracedStatements);
1796 }
1797 
1798 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
1799   FormatStyle Style = getLLVMStyleWithColumns(60);
1800   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
1801   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
1802   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1803   EXPECT_EQ("#define A                                                  \\\n"
1804             "  if (HANDLEwernufrnuLwrmviferuvnierv)                     \\\n"
1805             "  {                                                        \\\n"
1806             "    RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier;               \\\n"
1807             "  }\n"
1808             "X;",
1809             format("#define A \\\n"
1810                    "   if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
1811                    "      RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1812                    "   }\n"
1813                    "X;",
1814                    Style));
1815 }
1816 
1817 TEST_F(FormatTest, ParseIfElse) {
1818   verifyFormat("if (true)\n"
1819                "  if (true)\n"
1820                "    if (true)\n"
1821                "      f();\n"
1822                "    else\n"
1823                "      g();\n"
1824                "  else\n"
1825                "    h();\n"
1826                "else\n"
1827                "  i();");
1828   verifyFormat("if (true)\n"
1829                "  if (true)\n"
1830                "    if (true) {\n"
1831                "      if (true)\n"
1832                "        f();\n"
1833                "    } else {\n"
1834                "      g();\n"
1835                "    }\n"
1836                "  else\n"
1837                "    h();\n"
1838                "else {\n"
1839                "  i();\n"
1840                "}");
1841   verifyFormat("if (true)\n"
1842                "  if constexpr (true)\n"
1843                "    if (true) {\n"
1844                "      if constexpr (true)\n"
1845                "        f();\n"
1846                "    } else {\n"
1847                "      g();\n"
1848                "    }\n"
1849                "  else\n"
1850                "    h();\n"
1851                "else {\n"
1852                "  i();\n"
1853                "}");
1854   verifyFormat("if (true)\n"
1855                "  if CONSTEXPR (true)\n"
1856                "    if (true) {\n"
1857                "      if CONSTEXPR (true)\n"
1858                "        f();\n"
1859                "    } else {\n"
1860                "      g();\n"
1861                "    }\n"
1862                "  else\n"
1863                "    h();\n"
1864                "else {\n"
1865                "  i();\n"
1866                "}");
1867   verifyFormat("void f() {\n"
1868                "  if (a) {\n"
1869                "  } else {\n"
1870                "  }\n"
1871                "}");
1872 }
1873 
1874 TEST_F(FormatTest, ElseIf) {
1875   verifyFormat("if (a) {\n} else if (b) {\n}");
1876   verifyFormat("if (a)\n"
1877                "  f();\n"
1878                "else if (b)\n"
1879                "  g();\n"
1880                "else\n"
1881                "  h();");
1882   verifyFormat("if (a)\n"
1883                "  f();\n"
1884                "else // comment\n"
1885                "  if (b) {\n"
1886                "    g();\n"
1887                "    h();\n"
1888                "  }");
1889   verifyFormat("if constexpr (a)\n"
1890                "  f();\n"
1891                "else if constexpr (b)\n"
1892                "  g();\n"
1893                "else\n"
1894                "  h();");
1895   verifyFormat("if CONSTEXPR (a)\n"
1896                "  f();\n"
1897                "else if CONSTEXPR (b)\n"
1898                "  g();\n"
1899                "else\n"
1900                "  h();");
1901   verifyFormat("if (a) {\n"
1902                "  f();\n"
1903                "}\n"
1904                "// or else ..\n"
1905                "else {\n"
1906                "  g()\n"
1907                "}");
1908 
1909   verifyFormat("if (a) {\n"
1910                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1911                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1912                "}");
1913   verifyFormat("if (a) {\n"
1914                "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1915                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1916                "}");
1917   verifyFormat("if (a) {\n"
1918                "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1919                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1920                "}");
1921   verifyFormat("if (a) {\n"
1922                "} else if (\n"
1923                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1924                "}",
1925                getLLVMStyleWithColumns(62));
1926   verifyFormat("if (a) {\n"
1927                "} else if constexpr (\n"
1928                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1929                "}",
1930                getLLVMStyleWithColumns(62));
1931   verifyFormat("if (a) {\n"
1932                "} else if CONSTEXPR (\n"
1933                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1934                "}",
1935                getLLVMStyleWithColumns(62));
1936 }
1937 
1938 TEST_F(FormatTest, SeparatePointerReferenceAlignment) {
1939   FormatStyle Style = getLLVMStyle();
1940   // Check first the default LLVM style
1941   // Style.PointerAlignment = FormatStyle::PAS_Right;
1942   // Style.ReferenceAlignment = FormatStyle::RAS_Pointer;
1943   verifyFormat("int *f1(int *a, int &b, int &&c);", Style);
1944   verifyFormat("int &f2(int &&c, int *a, int &b);", Style);
1945   verifyFormat("int &&f3(int &b, int &&c, int *a);", Style);
1946   verifyFormat("int *f1(int &a) const &;", Style);
1947   verifyFormat("int *f1(int &a) const & = 0;", Style);
1948   verifyFormat("int *a = f1();", Style);
1949   verifyFormat("int &b = f2();", Style);
1950   verifyFormat("int &&c = f3();", Style);
1951   verifyFormat("for (auto a = 0, b = 0; const auto &c : {1, 2, 3})", Style);
1952   verifyFormat("for (auto a = 0, b = 0; const int &c : {1, 2, 3})", Style);
1953   verifyFormat("for (auto a = 0, b = 0; const Foo &c : {1, 2, 3})", Style);
1954   verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
1955   verifyFormat("for (int a = 0, b = 0; const auto &c : {1, 2, 3})", Style);
1956   verifyFormat("for (int a = 0, b = 0; const int &c : {1, 2, 3})", Style);
1957   verifyFormat("for (int a = 0, b = 0; const Foo &c : {1, 2, 3})", Style);
1958   verifyFormat("for (int a = 0, b++; const auto &c : {1, 2, 3})", Style);
1959   verifyFormat("for (int a = 0, b++; const int &c : {1, 2, 3})", Style);
1960   verifyFormat("for (int a = 0, b++; const Foo &c : {1, 2, 3})", Style);
1961   verifyFormat("for (auto x = 0; auto &c : {1, 2, 3})", Style);
1962   verifyFormat("for (auto x = 0; int &c : {1, 2, 3})", Style);
1963   verifyFormat("for (int x = 0; auto &c : {1, 2, 3})", Style);
1964   verifyFormat("for (int x = 0; int &c : {1, 2, 3})", Style);
1965   verifyFormat("for (f(); auto &c : {1, 2, 3})", Style);
1966   verifyFormat("for (f(); int &c : {1, 2, 3})", Style);
1967 
1968   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
1969   verifyFormat("Const unsigned int *c;\n"
1970                "const unsigned int *d;\n"
1971                "Const unsigned int &e;\n"
1972                "const unsigned int &f;\n"
1973                "const unsigned    &&g;\n"
1974                "Const unsigned      h;",
1975                Style);
1976 
1977   Style.PointerAlignment = FormatStyle::PAS_Left;
1978   Style.ReferenceAlignment = FormatStyle::RAS_Pointer;
1979   verifyFormat("int* f1(int* a, int& b, int&& c);", Style);
1980   verifyFormat("int& f2(int&& c, int* a, int& b);", Style);
1981   verifyFormat("int&& f3(int& b, int&& c, int* a);", Style);
1982   verifyFormat("int* f1(int& a) const& = 0;", Style);
1983   verifyFormat("int* a = f1();", Style);
1984   verifyFormat("int& b = f2();", Style);
1985   verifyFormat("int&& c = f3();", Style);
1986   verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
1987   verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style);
1988   verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style);
1989   verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
1990   verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
1991   verifyFormat("for (int a = 0, b = 0; const int& c : {1, 2, 3})", Style);
1992   verifyFormat("for (int a = 0, b = 0; const Foo& c : {1, 2, 3})", Style);
1993   verifyFormat("for (int a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
1994   verifyFormat("for (int a = 0, b++; const auto& c : {1, 2, 3})", Style);
1995   verifyFormat("for (int a = 0, b++; const int& c : {1, 2, 3})", Style);
1996   verifyFormat("for (int a = 0, b++; const Foo& c : {1, 2, 3})", Style);
1997   verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
1998   verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style);
1999   verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style);
2000   verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style);
2001   verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style);
2002   verifyFormat("for (f(); auto& c : {1, 2, 3})", Style);
2003   verifyFormat("for (f(); int& c : {1, 2, 3})", Style);
2004 
2005   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
2006   verifyFormat("Const unsigned int* c;\n"
2007                "const unsigned int* d;\n"
2008                "Const unsigned int& e;\n"
2009                "const unsigned int& f;\n"
2010                "const unsigned&&    g;\n"
2011                "Const unsigned      h;",
2012                Style);
2013 
2014   Style.PointerAlignment = FormatStyle::PAS_Right;
2015   Style.ReferenceAlignment = FormatStyle::RAS_Left;
2016   verifyFormat("int *f1(int *a, int& b, int&& c);", Style);
2017   verifyFormat("int& f2(int&& c, int *a, int& b);", Style);
2018   verifyFormat("int&& f3(int& b, int&& c, int *a);", Style);
2019   verifyFormat("int *a = f1();", Style);
2020   verifyFormat("int& b = f2();", Style);
2021   verifyFormat("int&& c = f3();", Style);
2022   verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2023   verifyFormat("for (int a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2024   verifyFormat("for (int a = 0, b++; const Foo *c : {1, 2, 3})", Style);
2025 
2026   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
2027   verifyFormat("Const unsigned int *c;\n"
2028                "const unsigned int *d;\n"
2029                "Const unsigned int& e;\n"
2030                "const unsigned int& f;\n"
2031                "const unsigned      g;\n"
2032                "Const unsigned      h;",
2033                Style);
2034 
2035   Style.PointerAlignment = FormatStyle::PAS_Left;
2036   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
2037   verifyFormat("int* f1(int* a, int & b, int && c);", Style);
2038   verifyFormat("int & f2(int && c, int* a, int & b);", Style);
2039   verifyFormat("int && f3(int & b, int && c, int* a);", Style);
2040   verifyFormat("int* a = f1();", Style);
2041   verifyFormat("int & b = f2();", Style);
2042   verifyFormat("int && c = f3();", 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++; const auto & c : {1, 2, 3})", Style);
2048   verifyFormat("for (int a = 0, b++; const int & c : {1, 2, 3})", Style);
2049   verifyFormat("for (int a = 0, b++; const Foo & c : {1, 2, 3})", Style);
2050   verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
2051   verifyFormat("for (auto x = 0; auto & c : {1, 2, 3})", Style);
2052   verifyFormat("for (auto x = 0; int & c : {1, 2, 3})", Style);
2053   verifyFormat("for (int x = 0; auto & c : {1, 2, 3})", Style);
2054   verifyFormat("for (int x = 0; int & c : {1, 2, 3})", Style);
2055   verifyFormat("for (f(); auto & c : {1, 2, 3})", Style);
2056   verifyFormat("for (f(); int & c : {1, 2, 3})", Style);
2057 
2058   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
2059   verifyFormat("Const unsigned int*  c;\n"
2060                "const unsigned int*  d;\n"
2061                "Const unsigned int & e;\n"
2062                "const unsigned int & f;\n"
2063                "const unsigned &&    g;\n"
2064                "Const unsigned       h;",
2065                Style);
2066 
2067   Style.PointerAlignment = FormatStyle::PAS_Middle;
2068   Style.ReferenceAlignment = FormatStyle::RAS_Right;
2069   verifyFormat("int * f1(int * a, int &b, int &&c);", Style);
2070   verifyFormat("int &f2(int &&c, int * a, int &b);", Style);
2071   verifyFormat("int &&f3(int &b, int &&c, int * a);", Style);
2072   verifyFormat("int * a = f1();", Style);
2073   verifyFormat("int &b = f2();", Style);
2074   verifyFormat("int &&c = f3();", Style);
2075   verifyFormat("for (auto a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
2076   verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
2077   verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style);
2078 
2079   // FIXME: we don't handle this yet, so output may be arbitrary until it's
2080   // specifically handled
2081   // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style);
2082 }
2083 
2084 TEST_F(FormatTest, FormatsForLoop) {
2085   verifyFormat(
2086       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
2087       "     ++VeryVeryLongLoopVariable)\n"
2088       "  ;");
2089   verifyFormat("for (;;)\n"
2090                "  f();");
2091   verifyFormat("for (;;) {\n}");
2092   verifyFormat("for (;;) {\n"
2093                "  f();\n"
2094                "}");
2095   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
2096 
2097   verifyFormat(
2098       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2099       "                                          E = UnwrappedLines.end();\n"
2100       "     I != E; ++I) {\n}");
2101 
2102   verifyFormat(
2103       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
2104       "     ++IIIII) {\n}");
2105   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
2106                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
2107                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
2108   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
2109                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
2110                "         E = FD->getDeclsInPrototypeScope().end();\n"
2111                "     I != E; ++I) {\n}");
2112   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
2113                "         I = Container.begin(),\n"
2114                "         E = Container.end();\n"
2115                "     I != E; ++I) {\n}",
2116                getLLVMStyleWithColumns(76));
2117 
2118   verifyFormat(
2119       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2120       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
2121       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2122       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2123       "     ++aaaaaaaaaaa) {\n}");
2124   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2125                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
2126                "     ++i) {\n}");
2127   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
2128                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2129                "}");
2130   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
2131                "         aaaaaaaaaa);\n"
2132                "     iter; ++iter) {\n"
2133                "}");
2134   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2135                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2136                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
2137                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
2138 
2139   // These should not be formatted as Objective-C for-in loops.
2140   verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
2141   verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
2142   verifyFormat("Foo *x;\nfor (x in y) {\n}");
2143   verifyFormat(
2144       "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
2145 
2146   FormatStyle NoBinPacking = getLLVMStyle();
2147   NoBinPacking.BinPackParameters = false;
2148   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
2149                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
2150                "                                           aaaaaaaaaaaaaaaa,\n"
2151                "                                           aaaaaaaaaaaaaaaa,\n"
2152                "                                           aaaaaaaaaaaaaaaa);\n"
2153                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2154                "}",
2155                NoBinPacking);
2156   verifyFormat(
2157       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2158       "                                          E = UnwrappedLines.end();\n"
2159       "     I != E;\n"
2160       "     ++I) {\n}",
2161       NoBinPacking);
2162 
2163   FormatStyle AlignLeft = getLLVMStyle();
2164   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
2165   verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
2166 }
2167 
2168 TEST_F(FormatTest, RangeBasedForLoops) {
2169   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
2170                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2171   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
2172                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
2173   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
2174                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2175   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
2176                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
2177 }
2178 
2179 TEST_F(FormatTest, ForEachLoops) {
2180   FormatStyle Style = getLLVMStyle();
2181   EXPECT_EQ(Style.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
2182   EXPECT_EQ(Style.AllowShortLoopsOnASingleLine, false);
2183   verifyFormat("void f() {\n"
2184                "  for (;;) {\n"
2185                "  }\n"
2186                "  foreach (Item *item, itemlist) {\n"
2187                "  }\n"
2188                "  Q_FOREACH (Item *item, itemlist) {\n"
2189                "  }\n"
2190                "  BOOST_FOREACH (Item *item, itemlist) {\n"
2191                "  }\n"
2192                "  UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2193                "}",
2194                Style);
2195   verifyFormat("void f() {\n"
2196                "  for (;;)\n"
2197                "    int j = 1;\n"
2198                "  Q_FOREACH (int v, vec)\n"
2199                "    v *= 2;\n"
2200                "  for (;;) {\n"
2201                "    int j = 1;\n"
2202                "  }\n"
2203                "  Q_FOREACH (int v, vec) {\n"
2204                "    v *= 2;\n"
2205                "  }\n"
2206                "}",
2207                Style);
2208 
2209   FormatStyle ShortBlocks = getLLVMStyle();
2210   ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
2211   EXPECT_EQ(ShortBlocks.AllowShortLoopsOnASingleLine, false);
2212   verifyFormat("void f() {\n"
2213                "  for (;;)\n"
2214                "    int j = 1;\n"
2215                "  Q_FOREACH (int &v, vec)\n"
2216                "    v *= 2;\n"
2217                "  for (;;) {\n"
2218                "    int j = 1;\n"
2219                "  }\n"
2220                "  Q_FOREACH (int &v, vec) {\n"
2221                "    int j = 1;\n"
2222                "  }\n"
2223                "}",
2224                ShortBlocks);
2225 
2226   FormatStyle ShortLoops = getLLVMStyle();
2227   ShortLoops.AllowShortLoopsOnASingleLine = true;
2228   EXPECT_EQ(ShortLoops.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
2229   verifyFormat("void f() {\n"
2230                "  for (;;) int j = 1;\n"
2231                "  Q_FOREACH (int &v, vec) int j = 1;\n"
2232                "  for (;;) {\n"
2233                "    int j = 1;\n"
2234                "  }\n"
2235                "  Q_FOREACH (int &v, vec) {\n"
2236                "    int j = 1;\n"
2237                "  }\n"
2238                "}",
2239                ShortLoops);
2240 
2241   FormatStyle ShortBlocksAndLoops = getLLVMStyle();
2242   ShortBlocksAndLoops.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
2243   ShortBlocksAndLoops.AllowShortLoopsOnASingleLine = true;
2244   verifyFormat("void f() {\n"
2245                "  for (;;) int j = 1;\n"
2246                "  Q_FOREACH (int &v, vec) int j = 1;\n"
2247                "  for (;;) { int j = 1; }\n"
2248                "  Q_FOREACH (int &v, vec) { int j = 1; }\n"
2249                "}",
2250                ShortBlocksAndLoops);
2251 
2252   Style.SpaceBeforeParens =
2253       FormatStyle::SBPO_ControlStatementsExceptControlMacros;
2254   verifyFormat("void f() {\n"
2255                "  for (;;) {\n"
2256                "  }\n"
2257                "  foreach(Item *item, itemlist) {\n"
2258                "  }\n"
2259                "  Q_FOREACH(Item *item, itemlist) {\n"
2260                "  }\n"
2261                "  BOOST_FOREACH(Item *item, itemlist) {\n"
2262                "  }\n"
2263                "  UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2264                "}",
2265                Style);
2266 
2267   // As function-like macros.
2268   verifyFormat("#define foreach(x, y)\n"
2269                "#define Q_FOREACH(x, y)\n"
2270                "#define BOOST_FOREACH(x, y)\n"
2271                "#define UNKNOWN_FOREACH(x, y)\n");
2272 
2273   // Not as function-like macros.
2274   verifyFormat("#define foreach (x, y)\n"
2275                "#define Q_FOREACH (x, y)\n"
2276                "#define BOOST_FOREACH (x, y)\n"
2277                "#define UNKNOWN_FOREACH (x, y)\n");
2278 
2279   // handle microsoft non standard extension
2280   verifyFormat("for each (char c in x->MyStringProperty)");
2281 }
2282 
2283 TEST_F(FormatTest, FormatsWhileLoop) {
2284   verifyFormat("while (true) {\n}");
2285   verifyFormat("while (true)\n"
2286                "  f();");
2287   verifyFormat("while () {\n}");
2288   verifyFormat("while () {\n"
2289                "  f();\n"
2290                "}");
2291 }
2292 
2293 TEST_F(FormatTest, FormatsDoWhile) {
2294   verifyFormat("do {\n"
2295                "  do_something();\n"
2296                "} while (something());");
2297   verifyFormat("do\n"
2298                "  do_something();\n"
2299                "while (something());");
2300 }
2301 
2302 TEST_F(FormatTest, FormatsSwitchStatement) {
2303   verifyFormat("switch (x) {\n"
2304                "case 1:\n"
2305                "  f();\n"
2306                "  break;\n"
2307                "case kFoo:\n"
2308                "case ns::kBar:\n"
2309                "case kBaz:\n"
2310                "  break;\n"
2311                "default:\n"
2312                "  g();\n"
2313                "  break;\n"
2314                "}");
2315   verifyFormat("switch (x) {\n"
2316                "case 1: {\n"
2317                "  f();\n"
2318                "  break;\n"
2319                "}\n"
2320                "case 2: {\n"
2321                "  break;\n"
2322                "}\n"
2323                "}");
2324   verifyFormat("switch (x) {\n"
2325                "case 1: {\n"
2326                "  f();\n"
2327                "  {\n"
2328                "    g();\n"
2329                "    h();\n"
2330                "  }\n"
2331                "  break;\n"
2332                "}\n"
2333                "}");
2334   verifyFormat("switch (x) {\n"
2335                "case 1: {\n"
2336                "  f();\n"
2337                "  if (foo) {\n"
2338                "    g();\n"
2339                "    h();\n"
2340                "  }\n"
2341                "  break;\n"
2342                "}\n"
2343                "}");
2344   verifyFormat("switch (x) {\n"
2345                "case 1: {\n"
2346                "  f();\n"
2347                "  g();\n"
2348                "} break;\n"
2349                "}");
2350   verifyFormat("switch (test)\n"
2351                "  ;");
2352   verifyFormat("switch (x) {\n"
2353                "default: {\n"
2354                "  // Do nothing.\n"
2355                "}\n"
2356                "}");
2357   verifyFormat("switch (x) {\n"
2358                "// comment\n"
2359                "// if 1, do f()\n"
2360                "case 1:\n"
2361                "  f();\n"
2362                "}");
2363   verifyFormat("switch (x) {\n"
2364                "case 1:\n"
2365                "  // Do amazing stuff\n"
2366                "  {\n"
2367                "    f();\n"
2368                "    g();\n"
2369                "  }\n"
2370                "  break;\n"
2371                "}");
2372   verifyFormat("#define A          \\\n"
2373                "  switch (x) {     \\\n"
2374                "  case a:          \\\n"
2375                "    foo = b;       \\\n"
2376                "  }",
2377                getLLVMStyleWithColumns(20));
2378   verifyFormat("#define OPERATION_CASE(name)           \\\n"
2379                "  case OP_name:                        \\\n"
2380                "    return operations::Operation##name\n",
2381                getLLVMStyleWithColumns(40));
2382   verifyFormat("switch (x) {\n"
2383                "case 1:;\n"
2384                "default:;\n"
2385                "  int i;\n"
2386                "}");
2387 
2388   verifyGoogleFormat("switch (x) {\n"
2389                      "  case 1:\n"
2390                      "    f();\n"
2391                      "    break;\n"
2392                      "  case kFoo:\n"
2393                      "  case ns::kBar:\n"
2394                      "  case kBaz:\n"
2395                      "    break;\n"
2396                      "  default:\n"
2397                      "    g();\n"
2398                      "    break;\n"
2399                      "}");
2400   verifyGoogleFormat("switch (x) {\n"
2401                      "  case 1: {\n"
2402                      "    f();\n"
2403                      "    break;\n"
2404                      "  }\n"
2405                      "}");
2406   verifyGoogleFormat("switch (test)\n"
2407                      "  ;");
2408 
2409   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
2410                      "  case OP_name:              \\\n"
2411                      "    return operations::Operation##name\n");
2412   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
2413                      "  // Get the correction operation class.\n"
2414                      "  switch (OpCode) {\n"
2415                      "    CASE(Add);\n"
2416                      "    CASE(Subtract);\n"
2417                      "    default:\n"
2418                      "      return operations::Unknown;\n"
2419                      "  }\n"
2420                      "#undef OPERATION_CASE\n"
2421                      "}");
2422   verifyFormat("DEBUG({\n"
2423                "  switch (x) {\n"
2424                "  case A:\n"
2425                "    f();\n"
2426                "    break;\n"
2427                "    // fallthrough\n"
2428                "  case B:\n"
2429                "    g();\n"
2430                "    break;\n"
2431                "  }\n"
2432                "});");
2433   EXPECT_EQ("DEBUG({\n"
2434             "  switch (x) {\n"
2435             "  case A:\n"
2436             "    f();\n"
2437             "    break;\n"
2438             "  // On B:\n"
2439             "  case B:\n"
2440             "    g();\n"
2441             "    break;\n"
2442             "  }\n"
2443             "});",
2444             format("DEBUG({\n"
2445                    "  switch (x) {\n"
2446                    "  case A:\n"
2447                    "    f();\n"
2448                    "    break;\n"
2449                    "  // On B:\n"
2450                    "  case B:\n"
2451                    "    g();\n"
2452                    "    break;\n"
2453                    "  }\n"
2454                    "});",
2455                    getLLVMStyle()));
2456   EXPECT_EQ("switch (n) {\n"
2457             "case 0: {\n"
2458             "  return false;\n"
2459             "}\n"
2460             "default: {\n"
2461             "  return true;\n"
2462             "}\n"
2463             "}",
2464             format("switch (n)\n"
2465                    "{\n"
2466                    "case 0: {\n"
2467                    "  return false;\n"
2468                    "}\n"
2469                    "default: {\n"
2470                    "  return true;\n"
2471                    "}\n"
2472                    "}",
2473                    getLLVMStyle()));
2474   verifyFormat("switch (a) {\n"
2475                "case (b):\n"
2476                "  return;\n"
2477                "}");
2478 
2479   verifyFormat("switch (a) {\n"
2480                "case some_namespace::\n"
2481                "    some_constant:\n"
2482                "  return;\n"
2483                "}",
2484                getLLVMStyleWithColumns(34));
2485 
2486   FormatStyle Style = getLLVMStyle();
2487   Style.IndentCaseLabels = true;
2488   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
2489   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2490   Style.BraceWrapping.AfterCaseLabel = true;
2491   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2492   EXPECT_EQ("switch (n)\n"
2493             "{\n"
2494             "  case 0:\n"
2495             "  {\n"
2496             "    return false;\n"
2497             "  }\n"
2498             "  default:\n"
2499             "  {\n"
2500             "    return true;\n"
2501             "  }\n"
2502             "}",
2503             format("switch (n) {\n"
2504                    "  case 0: {\n"
2505                    "    return false;\n"
2506                    "  }\n"
2507                    "  default: {\n"
2508                    "    return true;\n"
2509                    "  }\n"
2510                    "}",
2511                    Style));
2512   Style.BraceWrapping.AfterCaseLabel = false;
2513   EXPECT_EQ("switch (n)\n"
2514             "{\n"
2515             "  case 0: {\n"
2516             "    return false;\n"
2517             "  }\n"
2518             "  default: {\n"
2519             "    return true;\n"
2520             "  }\n"
2521             "}",
2522             format("switch (n) {\n"
2523                    "  case 0:\n"
2524                    "  {\n"
2525                    "    return false;\n"
2526                    "  }\n"
2527                    "  default:\n"
2528                    "  {\n"
2529                    "    return true;\n"
2530                    "  }\n"
2531                    "}",
2532                    Style));
2533   Style.IndentCaseLabels = false;
2534   Style.IndentCaseBlocks = true;
2535   EXPECT_EQ("switch (n)\n"
2536             "{\n"
2537             "case 0:\n"
2538             "  {\n"
2539             "    return false;\n"
2540             "  }\n"
2541             "case 1:\n"
2542             "  break;\n"
2543             "default:\n"
2544             "  {\n"
2545             "    return true;\n"
2546             "  }\n"
2547             "}",
2548             format("switch (n) {\n"
2549                    "case 0: {\n"
2550                    "  return false;\n"
2551                    "}\n"
2552                    "case 1:\n"
2553                    "  break;\n"
2554                    "default: {\n"
2555                    "  return true;\n"
2556                    "}\n"
2557                    "}",
2558                    Style));
2559   Style.IndentCaseLabels = true;
2560   Style.IndentCaseBlocks = true;
2561   EXPECT_EQ("switch (n)\n"
2562             "{\n"
2563             "  case 0:\n"
2564             "    {\n"
2565             "      return false;\n"
2566             "    }\n"
2567             "  case 1:\n"
2568             "    break;\n"
2569             "  default:\n"
2570             "    {\n"
2571             "      return true;\n"
2572             "    }\n"
2573             "}",
2574             format("switch (n) {\n"
2575                    "case 0: {\n"
2576                    "  return false;\n"
2577                    "}\n"
2578                    "case 1:\n"
2579                    "  break;\n"
2580                    "default: {\n"
2581                    "  return true;\n"
2582                    "}\n"
2583                    "}",
2584                    Style));
2585 }
2586 
2587 TEST_F(FormatTest, CaseRanges) {
2588   verifyFormat("switch (x) {\n"
2589                "case 'A' ... 'Z':\n"
2590                "case 1 ... 5:\n"
2591                "case a ... b:\n"
2592                "  break;\n"
2593                "}");
2594 }
2595 
2596 TEST_F(FormatTest, ShortEnums) {
2597   FormatStyle Style = getLLVMStyle();
2598   Style.AllowShortEnumsOnASingleLine = true;
2599   verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2600   verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2601   Style.AllowShortEnumsOnASingleLine = false;
2602   verifyFormat("enum {\n"
2603                "  A,\n"
2604                "  B,\n"
2605                "  C\n"
2606                "} ShortEnum1, ShortEnum2;",
2607                Style);
2608   verifyFormat("typedef enum {\n"
2609                "  A,\n"
2610                "  B,\n"
2611                "  C\n"
2612                "} ShortEnum1, ShortEnum2;",
2613                Style);
2614   verifyFormat("enum {\n"
2615                "  A,\n"
2616                "} ShortEnum1, ShortEnum2;",
2617                Style);
2618   verifyFormat("typedef enum {\n"
2619                "  A,\n"
2620                "} ShortEnum1, ShortEnum2;",
2621                Style);
2622   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2623   Style.BraceWrapping.AfterEnum = true;
2624   verifyFormat("enum\n"
2625                "{\n"
2626                "  A,\n"
2627                "  B,\n"
2628                "  C\n"
2629                "} ShortEnum1, ShortEnum2;",
2630                Style);
2631   verifyFormat("typedef enum\n"
2632                "{\n"
2633                "  A,\n"
2634                "  B,\n"
2635                "  C\n"
2636                "} ShortEnum1, ShortEnum2;",
2637                Style);
2638 }
2639 
2640 TEST_F(FormatTest, ShortCaseLabels) {
2641   FormatStyle Style = getLLVMStyle();
2642   Style.AllowShortCaseLabelsOnASingleLine = true;
2643   verifyFormat("switch (a) {\n"
2644                "case 1: x = 1; break;\n"
2645                "case 2: return;\n"
2646                "case 3:\n"
2647                "case 4:\n"
2648                "case 5: return;\n"
2649                "case 6: // comment\n"
2650                "  return;\n"
2651                "case 7:\n"
2652                "  // comment\n"
2653                "  return;\n"
2654                "case 8:\n"
2655                "  x = 8; // comment\n"
2656                "  break;\n"
2657                "default: y = 1; break;\n"
2658                "}",
2659                Style);
2660   verifyFormat("switch (a) {\n"
2661                "case 0: return; // comment\n"
2662                "case 1: break;  // comment\n"
2663                "case 2: return;\n"
2664                "// comment\n"
2665                "case 3: return;\n"
2666                "// comment 1\n"
2667                "// comment 2\n"
2668                "// comment 3\n"
2669                "case 4: break; /* comment */\n"
2670                "case 5:\n"
2671                "  // comment\n"
2672                "  break;\n"
2673                "case 6: /* comment */ x = 1; break;\n"
2674                "case 7: x = /* comment */ 1; break;\n"
2675                "case 8:\n"
2676                "  x = 1; /* comment */\n"
2677                "  break;\n"
2678                "case 9:\n"
2679                "  break; // comment line 1\n"
2680                "         // comment line 2\n"
2681                "}",
2682                Style);
2683   EXPECT_EQ("switch (a) {\n"
2684             "case 1:\n"
2685             "  x = 8;\n"
2686             "  // fall through\n"
2687             "case 2: x = 8;\n"
2688             "// comment\n"
2689             "case 3:\n"
2690             "  return; /* comment line 1\n"
2691             "           * comment line 2 */\n"
2692             "case 4: i = 8;\n"
2693             "// something else\n"
2694             "#if FOO\n"
2695             "case 5: break;\n"
2696             "#endif\n"
2697             "}",
2698             format("switch (a) {\n"
2699                    "case 1: x = 8;\n"
2700                    "  // fall through\n"
2701                    "case 2:\n"
2702                    "  x = 8;\n"
2703                    "// comment\n"
2704                    "case 3:\n"
2705                    "  return; /* comment line 1\n"
2706                    "           * comment line 2 */\n"
2707                    "case 4:\n"
2708                    "  i = 8;\n"
2709                    "// something else\n"
2710                    "#if FOO\n"
2711                    "case 5: break;\n"
2712                    "#endif\n"
2713                    "}",
2714                    Style));
2715   EXPECT_EQ("switch (a) {\n"
2716             "case 0:\n"
2717             "  return; // long long long long long long long long long long "
2718             "long long comment\n"
2719             "          // line\n"
2720             "}",
2721             format("switch (a) {\n"
2722                    "case 0: return; // long long long long long long long long "
2723                    "long long long long comment line\n"
2724                    "}",
2725                    Style));
2726   EXPECT_EQ("switch (a) {\n"
2727             "case 0:\n"
2728             "  return; /* long long long long long long long long long long "
2729             "long long comment\n"
2730             "             line */\n"
2731             "}",
2732             format("switch (a) {\n"
2733                    "case 0: return; /* long long long long long long long long "
2734                    "long long long long comment line */\n"
2735                    "}",
2736                    Style));
2737   verifyFormat("switch (a) {\n"
2738                "#if FOO\n"
2739                "case 0: return 0;\n"
2740                "#endif\n"
2741                "}",
2742                Style);
2743   verifyFormat("switch (a) {\n"
2744                "case 1: {\n"
2745                "}\n"
2746                "case 2: {\n"
2747                "  return;\n"
2748                "}\n"
2749                "case 3: {\n"
2750                "  x = 1;\n"
2751                "  return;\n"
2752                "}\n"
2753                "case 4:\n"
2754                "  if (x)\n"
2755                "    return;\n"
2756                "}",
2757                Style);
2758   Style.ColumnLimit = 21;
2759   verifyFormat("switch (a) {\n"
2760                "case 1: x = 1; break;\n"
2761                "case 2: return;\n"
2762                "case 3:\n"
2763                "case 4:\n"
2764                "case 5: return;\n"
2765                "default:\n"
2766                "  y = 1;\n"
2767                "  break;\n"
2768                "}",
2769                Style);
2770   Style.ColumnLimit = 80;
2771   Style.AllowShortCaseLabelsOnASingleLine = false;
2772   Style.IndentCaseLabels = true;
2773   EXPECT_EQ("switch (n) {\n"
2774             "  default /*comments*/:\n"
2775             "    return true;\n"
2776             "  case 0:\n"
2777             "    return false;\n"
2778             "}",
2779             format("switch (n) {\n"
2780                    "default/*comments*/:\n"
2781                    "  return true;\n"
2782                    "case 0:\n"
2783                    "  return false;\n"
2784                    "}",
2785                    Style));
2786   Style.AllowShortCaseLabelsOnASingleLine = true;
2787   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2788   Style.BraceWrapping.AfterCaseLabel = true;
2789   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2790   EXPECT_EQ("switch (n)\n"
2791             "{\n"
2792             "  case 0:\n"
2793             "  {\n"
2794             "    return false;\n"
2795             "  }\n"
2796             "  default:\n"
2797             "  {\n"
2798             "    return true;\n"
2799             "  }\n"
2800             "}",
2801             format("switch (n) {\n"
2802                    "  case 0: {\n"
2803                    "    return false;\n"
2804                    "  }\n"
2805                    "  default:\n"
2806                    "  {\n"
2807                    "    return true;\n"
2808                    "  }\n"
2809                    "}",
2810                    Style));
2811 }
2812 
2813 TEST_F(FormatTest, FormatsLabels) {
2814   verifyFormat("void f() {\n"
2815                "  some_code();\n"
2816                "test_label:\n"
2817                "  some_other_code();\n"
2818                "  {\n"
2819                "    some_more_code();\n"
2820                "  another_label:\n"
2821                "    some_more_code();\n"
2822                "  }\n"
2823                "}");
2824   verifyFormat("{\n"
2825                "  some_code();\n"
2826                "test_label:\n"
2827                "  some_other_code();\n"
2828                "}");
2829   verifyFormat("{\n"
2830                "  some_code();\n"
2831                "test_label:;\n"
2832                "  int i = 0;\n"
2833                "}");
2834   FormatStyle Style = getLLVMStyle();
2835   Style.IndentGotoLabels = false;
2836   verifyFormat("void f() {\n"
2837                "  some_code();\n"
2838                "test_label:\n"
2839                "  some_other_code();\n"
2840                "  {\n"
2841                "    some_more_code();\n"
2842                "another_label:\n"
2843                "    some_more_code();\n"
2844                "  }\n"
2845                "}",
2846                Style);
2847   verifyFormat("{\n"
2848                "  some_code();\n"
2849                "test_label:\n"
2850                "  some_other_code();\n"
2851                "}",
2852                Style);
2853   verifyFormat("{\n"
2854                "  some_code();\n"
2855                "test_label:;\n"
2856                "  int i = 0;\n"
2857                "}");
2858 }
2859 
2860 TEST_F(FormatTest, MultiLineControlStatements) {
2861   FormatStyle Style = getLLVMStyleWithColumns(20);
2862   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
2863   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
2864   // Short lines should keep opening brace on same line.
2865   EXPECT_EQ("if (foo) {\n"
2866             "  bar();\n"
2867             "}",
2868             format("if(foo){bar();}", Style));
2869   EXPECT_EQ("if (foo) {\n"
2870             "  bar();\n"
2871             "} else {\n"
2872             "  baz();\n"
2873             "}",
2874             format("if(foo){bar();}else{baz();}", Style));
2875   EXPECT_EQ("if (foo && bar) {\n"
2876             "  baz();\n"
2877             "}",
2878             format("if(foo&&bar){baz();}", Style));
2879   EXPECT_EQ("if (foo) {\n"
2880             "  bar();\n"
2881             "} else if (baz) {\n"
2882             "  quux();\n"
2883             "}",
2884             format("if(foo){bar();}else if(baz){quux();}", Style));
2885   EXPECT_EQ(
2886       "if (foo) {\n"
2887       "  bar();\n"
2888       "} else if (baz) {\n"
2889       "  quux();\n"
2890       "} else {\n"
2891       "  foobar();\n"
2892       "}",
2893       format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style));
2894   EXPECT_EQ("for (;;) {\n"
2895             "  foo();\n"
2896             "}",
2897             format("for(;;){foo();}"));
2898   EXPECT_EQ("while (1) {\n"
2899             "  foo();\n"
2900             "}",
2901             format("while(1){foo();}", Style));
2902   EXPECT_EQ("switch (foo) {\n"
2903             "case bar:\n"
2904             "  return;\n"
2905             "}",
2906             format("switch(foo){case bar:return;}", Style));
2907   EXPECT_EQ("try {\n"
2908             "  foo();\n"
2909             "} catch (...) {\n"
2910             "  bar();\n"
2911             "}",
2912             format("try{foo();}catch(...){bar();}", Style));
2913   EXPECT_EQ("do {\n"
2914             "  foo();\n"
2915             "} while (bar &&\n"
2916             "         baz);",
2917             format("do{foo();}while(bar&&baz);", Style));
2918   // Long lines should put opening brace on new line.
2919   EXPECT_EQ("if (foo && bar &&\n"
2920             "    baz)\n"
2921             "{\n"
2922             "  quux();\n"
2923             "}",
2924             format("if(foo&&bar&&baz){quux();}", Style));
2925   EXPECT_EQ("if (foo && bar &&\n"
2926             "    baz)\n"
2927             "{\n"
2928             "  quux();\n"
2929             "}",
2930             format("if (foo && bar &&\n"
2931                    "    baz) {\n"
2932                    "  quux();\n"
2933                    "}",
2934                    Style));
2935   EXPECT_EQ("if (foo) {\n"
2936             "  bar();\n"
2937             "} else if (baz ||\n"
2938             "           quux)\n"
2939             "{\n"
2940             "  foobar();\n"
2941             "}",
2942             format("if(foo){bar();}else if(baz||quux){foobar();}", Style));
2943   EXPECT_EQ(
2944       "if (foo) {\n"
2945       "  bar();\n"
2946       "} else if (baz ||\n"
2947       "           quux)\n"
2948       "{\n"
2949       "  foobar();\n"
2950       "} else {\n"
2951       "  barbaz();\n"
2952       "}",
2953       format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
2954              Style));
2955   EXPECT_EQ("for (int i = 0;\n"
2956             "     i < 10; ++i)\n"
2957             "{\n"
2958             "  foo();\n"
2959             "}",
2960             format("for(int i=0;i<10;++i){foo();}", Style));
2961   EXPECT_EQ("foreach (int i,\n"
2962             "         list)\n"
2963             "{\n"
2964             "  foo();\n"
2965             "}",
2966             format("foreach(int i, list){foo();}", Style));
2967   Style.ColumnLimit =
2968       40; // to concentrate at brace wrapping, not line wrap due to column limit
2969   EXPECT_EQ("foreach (int i, list) {\n"
2970             "  foo();\n"
2971             "}",
2972             format("foreach(int i, list){foo();}", Style));
2973   Style.ColumnLimit =
2974       20; // to concentrate at brace wrapping, not line wrap due to column limit
2975   EXPECT_EQ("while (foo || bar ||\n"
2976             "       baz)\n"
2977             "{\n"
2978             "  quux();\n"
2979             "}",
2980             format("while(foo||bar||baz){quux();}", Style));
2981   EXPECT_EQ("switch (\n"
2982             "    foo = barbaz)\n"
2983             "{\n"
2984             "case quux:\n"
2985             "  return;\n"
2986             "}",
2987             format("switch(foo=barbaz){case quux:return;}", Style));
2988   EXPECT_EQ("try {\n"
2989             "  foo();\n"
2990             "} catch (\n"
2991             "    Exception &bar)\n"
2992             "{\n"
2993             "  baz();\n"
2994             "}",
2995             format("try{foo();}catch(Exception&bar){baz();}", Style));
2996   Style.ColumnLimit =
2997       40; // to concentrate at brace wrapping, not line wrap due to column limit
2998   EXPECT_EQ("try {\n"
2999             "  foo();\n"
3000             "} catch (Exception &bar) {\n"
3001             "  baz();\n"
3002             "}",
3003             format("try{foo();}catch(Exception&bar){baz();}", Style));
3004   Style.ColumnLimit =
3005       20; // to concentrate at brace wrapping, not line wrap due to column limit
3006 
3007   Style.BraceWrapping.BeforeElse = true;
3008   EXPECT_EQ(
3009       "if (foo) {\n"
3010       "  bar();\n"
3011       "}\n"
3012       "else if (baz ||\n"
3013       "         quux)\n"
3014       "{\n"
3015       "  foobar();\n"
3016       "}\n"
3017       "else {\n"
3018       "  barbaz();\n"
3019       "}",
3020       format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
3021              Style));
3022 
3023   Style.BraceWrapping.BeforeCatch = true;
3024   EXPECT_EQ("try {\n"
3025             "  foo();\n"
3026             "}\n"
3027             "catch (...) {\n"
3028             "  baz();\n"
3029             "}",
3030             format("try{foo();}catch(...){baz();}", Style));
3031 
3032   Style.BraceWrapping.AfterFunction = true;
3033   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
3034   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3035   Style.ColumnLimit = 80;
3036   verifyFormat("void shortfunction() { bar(); }", Style);
3037 
3038   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3039   verifyFormat("void shortfunction()\n"
3040                "{\n"
3041                "  bar();\n"
3042                "}",
3043                Style);
3044 }
3045 
3046 TEST_F(FormatTest, BeforeWhile) {
3047   FormatStyle Style = getLLVMStyle();
3048   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
3049 
3050   verifyFormat("do {\n"
3051                "  foo();\n"
3052                "} while (1);",
3053                Style);
3054   Style.BraceWrapping.BeforeWhile = true;
3055   verifyFormat("do {\n"
3056                "  foo();\n"
3057                "}\n"
3058                "while (1);",
3059                Style);
3060 }
3061 
3062 //===----------------------------------------------------------------------===//
3063 // Tests for classes, namespaces, etc.
3064 //===----------------------------------------------------------------------===//
3065 
3066 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
3067   verifyFormat("class A {};");
3068 }
3069 
3070 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
3071   verifyFormat("class A {\n"
3072                "public:\n"
3073                "public: // comment\n"
3074                "protected:\n"
3075                "private:\n"
3076                "  void f() {}\n"
3077                "};");
3078   verifyFormat("export class A {\n"
3079                "public:\n"
3080                "public: // comment\n"
3081                "protected:\n"
3082                "private:\n"
3083                "  void f() {}\n"
3084                "};");
3085   verifyGoogleFormat("class A {\n"
3086                      " public:\n"
3087                      " protected:\n"
3088                      " private:\n"
3089                      "  void f() {}\n"
3090                      "};");
3091   verifyGoogleFormat("export class A {\n"
3092                      " public:\n"
3093                      " protected:\n"
3094                      " private:\n"
3095                      "  void f() {}\n"
3096                      "};");
3097   verifyFormat("class A {\n"
3098                "public slots:\n"
3099                "  void f1() {}\n"
3100                "public Q_SLOTS:\n"
3101                "  void f2() {}\n"
3102                "protected slots:\n"
3103                "  void f3() {}\n"
3104                "protected Q_SLOTS:\n"
3105                "  void f4() {}\n"
3106                "private slots:\n"
3107                "  void f5() {}\n"
3108                "private Q_SLOTS:\n"
3109                "  void f6() {}\n"
3110                "signals:\n"
3111                "  void g1();\n"
3112                "Q_SIGNALS:\n"
3113                "  void g2();\n"
3114                "};");
3115 
3116   // Don't interpret 'signals' the wrong way.
3117   verifyFormat("signals.set();");
3118   verifyFormat("for (Signals signals : f()) {\n}");
3119   verifyFormat("{\n"
3120                "  signals.set(); // This needs indentation.\n"
3121                "}");
3122   verifyFormat("void f() {\n"
3123                "label:\n"
3124                "  signals.baz();\n"
3125                "}");
3126   verifyFormat("private[1];");
3127   verifyFormat("testArray[public] = 1;");
3128   verifyFormat("public();");
3129   verifyFormat("myFunc(public);");
3130   verifyFormat("std::vector<int> testVec = {private};");
3131   verifyFormat("private.p = 1;");
3132   verifyFormat("void function(private...){};");
3133   verifyFormat("if (private && public)\n");
3134   verifyFormat("private &= true;");
3135   verifyFormat("int x = private * public;");
3136   verifyFormat("public *= private;");
3137   verifyFormat("int x = public + private;");
3138   verifyFormat("private++;");
3139   verifyFormat("++private;");
3140   verifyFormat("public += private;");
3141   verifyFormat("public = public - private;");
3142   verifyFormat("public->foo();");
3143   verifyFormat("private--;");
3144   verifyFormat("--private;");
3145   verifyFormat("public -= 1;");
3146   verifyFormat("if (!private && !public)\n");
3147   verifyFormat("public != private;");
3148   verifyFormat("int x = public / private;");
3149   verifyFormat("public /= 2;");
3150   verifyFormat("public = public % 2;");
3151   verifyFormat("public %= 2;");
3152   verifyFormat("if (public < private)\n");
3153   verifyFormat("public << private;");
3154   verifyFormat("public <<= private;");
3155   verifyFormat("if (public > private)\n");
3156   verifyFormat("public >> private;");
3157   verifyFormat("public >>= private;");
3158   verifyFormat("public ^ private;");
3159   verifyFormat("public ^= private;");
3160   verifyFormat("public | private;");
3161   verifyFormat("public |= private;");
3162   verifyFormat("auto x = private ? 1 : 2;");
3163   verifyFormat("if (public == private)\n");
3164   verifyFormat("void foo(public, private)");
3165   verifyFormat("public::foo();");
3166 }
3167 
3168 TEST_F(FormatTest, SeparatesLogicalBlocks) {
3169   EXPECT_EQ("class A {\n"
3170             "public:\n"
3171             "  void f();\n"
3172             "\n"
3173             "private:\n"
3174             "  void g() {}\n"
3175             "  // test\n"
3176             "protected:\n"
3177             "  int h;\n"
3178             "};",
3179             format("class A {\n"
3180                    "public:\n"
3181                    "void f();\n"
3182                    "private:\n"
3183                    "void g() {}\n"
3184                    "// test\n"
3185                    "protected:\n"
3186                    "int h;\n"
3187                    "};"));
3188   EXPECT_EQ("class A {\n"
3189             "protected:\n"
3190             "public:\n"
3191             "  void f();\n"
3192             "};",
3193             format("class A {\n"
3194                    "protected:\n"
3195                    "\n"
3196                    "public:\n"
3197                    "\n"
3198                    "  void f();\n"
3199                    "};"));
3200 
3201   // Even ensure proper spacing inside macros.
3202   EXPECT_EQ("#define B     \\\n"
3203             "  class A {   \\\n"
3204             "   protected: \\\n"
3205             "   public:    \\\n"
3206             "    void f(); \\\n"
3207             "  };",
3208             format("#define B     \\\n"
3209                    "  class A {   \\\n"
3210                    "   protected: \\\n"
3211                    "              \\\n"
3212                    "   public:    \\\n"
3213                    "              \\\n"
3214                    "    void f(); \\\n"
3215                    "  };",
3216                    getGoogleStyle()));
3217   // But don't remove empty lines after macros ending in access specifiers.
3218   EXPECT_EQ("#define A private:\n"
3219             "\n"
3220             "int i;",
3221             format("#define A         private:\n"
3222                    "\n"
3223                    "int              i;"));
3224 }
3225 
3226 TEST_F(FormatTest, FormatsClasses) {
3227   verifyFormat("class A : public B {};");
3228   verifyFormat("class A : public ::B {};");
3229 
3230   verifyFormat(
3231       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3232       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3233   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3234                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3235                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3236   verifyFormat(
3237       "class A : public B, public C, public D, public E, public F {};");
3238   verifyFormat("class AAAAAAAAAAAA : public B,\n"
3239                "                     public C,\n"
3240                "                     public D,\n"
3241                "                     public E,\n"
3242                "                     public F,\n"
3243                "                     public G {};");
3244 
3245   verifyFormat("class\n"
3246                "    ReallyReallyLongClassName {\n"
3247                "  int i;\n"
3248                "};",
3249                getLLVMStyleWithColumns(32));
3250   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3251                "                           aaaaaaaaaaaaaaaa> {};");
3252   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
3253                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
3254                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
3255   verifyFormat("template <class R, class C>\n"
3256                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
3257                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
3258   verifyFormat("class ::A::B {};");
3259 }
3260 
3261 TEST_F(FormatTest, BreakInheritanceStyle) {
3262   FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
3263   StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
3264       FormatStyle::BILS_BeforeComma;
3265   verifyFormat("class MyClass : public X {};",
3266                StyleWithInheritanceBreakBeforeComma);
3267   verifyFormat("class MyClass\n"
3268                "    : public X\n"
3269                "    , public Y {};",
3270                StyleWithInheritanceBreakBeforeComma);
3271   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
3272                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
3273                "    , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3274                StyleWithInheritanceBreakBeforeComma);
3275   verifyFormat("struct aaaaaaaaaaaaa\n"
3276                "    : public aaaaaaaaaaaaaaaaaaa< // break\n"
3277                "          aaaaaaaaaaaaaaaa> {};",
3278                StyleWithInheritanceBreakBeforeComma);
3279 
3280   FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
3281   StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
3282       FormatStyle::BILS_AfterColon;
3283   verifyFormat("class MyClass : public X {};",
3284                StyleWithInheritanceBreakAfterColon);
3285   verifyFormat("class MyClass : public X, public Y {};",
3286                StyleWithInheritanceBreakAfterColon);
3287   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
3288                "    public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3289                "    public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3290                StyleWithInheritanceBreakAfterColon);
3291   verifyFormat("struct aaaaaaaaaaaaa :\n"
3292                "    public aaaaaaaaaaaaaaaaaaa< // break\n"
3293                "        aaaaaaaaaaaaaaaa> {};",
3294                StyleWithInheritanceBreakAfterColon);
3295 
3296   FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle();
3297   StyleWithInheritanceBreakAfterComma.BreakInheritanceList =
3298       FormatStyle::BILS_AfterComma;
3299   verifyFormat("class MyClass : public X {};",
3300                StyleWithInheritanceBreakAfterComma);
3301   verifyFormat("class MyClass : public X,\n"
3302                "                public Y {};",
3303                StyleWithInheritanceBreakAfterComma);
3304   verifyFormat(
3305       "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3306       "                               public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC "
3307       "{};",
3308       StyleWithInheritanceBreakAfterComma);
3309   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3310                "                           aaaaaaaaaaaaaaaa> {};",
3311                StyleWithInheritanceBreakAfterComma);
3312   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3313                "    : public OnceBreak,\n"
3314                "      public AlwaysBreak,\n"
3315                "      EvenBasesFitInOneLine {};",
3316                StyleWithInheritanceBreakAfterComma);
3317 }
3318 
3319 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
3320   verifyFormat("class A {\n} a, b;");
3321   verifyFormat("struct A {\n} a, b;");
3322   verifyFormat("union A {\n} a;");
3323 }
3324 
3325 TEST_F(FormatTest, FormatsEnum) {
3326   verifyFormat("enum {\n"
3327                "  Zero,\n"
3328                "  One = 1,\n"
3329                "  Two = One + 1,\n"
3330                "  Three = (One + Two),\n"
3331                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3332                "  Five = (One, Two, Three, Four, 5)\n"
3333                "};");
3334   verifyGoogleFormat("enum {\n"
3335                      "  Zero,\n"
3336                      "  One = 1,\n"
3337                      "  Two = One + 1,\n"
3338                      "  Three = (One + Two),\n"
3339                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3340                      "  Five = (One, Two, Three, Four, 5)\n"
3341                      "};");
3342   verifyFormat("enum Enum {};");
3343   verifyFormat("enum {};");
3344   verifyFormat("enum X E {} d;");
3345   verifyFormat("enum __attribute__((...)) E {} d;");
3346   verifyFormat("enum __declspec__((...)) E {} d;");
3347   verifyFormat("enum {\n"
3348                "  Bar = Foo<int, int>::value\n"
3349                "};",
3350                getLLVMStyleWithColumns(30));
3351 
3352   verifyFormat("enum ShortEnum { A, B, C };");
3353   verifyGoogleFormat("enum ShortEnum { A, B, C };");
3354 
3355   EXPECT_EQ("enum KeepEmptyLines {\n"
3356             "  ONE,\n"
3357             "\n"
3358             "  TWO,\n"
3359             "\n"
3360             "  THREE\n"
3361             "}",
3362             format("enum KeepEmptyLines {\n"
3363                    "  ONE,\n"
3364                    "\n"
3365                    "  TWO,\n"
3366                    "\n"
3367                    "\n"
3368                    "  THREE\n"
3369                    "}"));
3370   verifyFormat("enum E { // comment\n"
3371                "  ONE,\n"
3372                "  TWO\n"
3373                "};\n"
3374                "int i;");
3375 
3376   FormatStyle EightIndent = getLLVMStyle();
3377   EightIndent.IndentWidth = 8;
3378   verifyFormat("enum {\n"
3379                "        VOID,\n"
3380                "        CHAR,\n"
3381                "        SHORT,\n"
3382                "        INT,\n"
3383                "        LONG,\n"
3384                "        SIGNED,\n"
3385                "        UNSIGNED,\n"
3386                "        BOOL,\n"
3387                "        FLOAT,\n"
3388                "        DOUBLE,\n"
3389                "        COMPLEX\n"
3390                "};",
3391                EightIndent);
3392 
3393   // Not enums.
3394   verifyFormat("enum X f() {\n"
3395                "  a();\n"
3396                "  return 42;\n"
3397                "}");
3398   verifyFormat("enum X Type::f() {\n"
3399                "  a();\n"
3400                "  return 42;\n"
3401                "}");
3402   verifyFormat("enum ::X f() {\n"
3403                "  a();\n"
3404                "  return 42;\n"
3405                "}");
3406   verifyFormat("enum ns::X f() {\n"
3407                "  a();\n"
3408                "  return 42;\n"
3409                "}");
3410 }
3411 
3412 TEST_F(FormatTest, FormatsEnumsWithErrors) {
3413   verifyFormat("enum Type {\n"
3414                "  One = 0; // These semicolons should be commas.\n"
3415                "  Two = 1;\n"
3416                "};");
3417   verifyFormat("namespace n {\n"
3418                "enum Type {\n"
3419                "  One,\n"
3420                "  Two, // missing };\n"
3421                "  int i;\n"
3422                "}\n"
3423                "void g() {}");
3424 }
3425 
3426 TEST_F(FormatTest, FormatsEnumStruct) {
3427   verifyFormat("enum struct {\n"
3428                "  Zero,\n"
3429                "  One = 1,\n"
3430                "  Two = One + 1,\n"
3431                "  Three = (One + Two),\n"
3432                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3433                "  Five = (One, Two, Three, Four, 5)\n"
3434                "};");
3435   verifyFormat("enum struct Enum {};");
3436   verifyFormat("enum struct {};");
3437   verifyFormat("enum struct X E {} d;");
3438   verifyFormat("enum struct __attribute__((...)) E {} d;");
3439   verifyFormat("enum struct __declspec__((...)) E {} d;");
3440   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
3441 }
3442 
3443 TEST_F(FormatTest, FormatsEnumClass) {
3444   verifyFormat("enum class {\n"
3445                "  Zero,\n"
3446                "  One = 1,\n"
3447                "  Two = One + 1,\n"
3448                "  Three = (One + Two),\n"
3449                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
3450                "  Five = (One, Two, Three, Four, 5)\n"
3451                "};");
3452   verifyFormat("enum class Enum {};");
3453   verifyFormat("enum class {};");
3454   verifyFormat("enum class X E {} d;");
3455   verifyFormat("enum class __attribute__((...)) E {} d;");
3456   verifyFormat("enum class __declspec__((...)) E {} d;");
3457   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
3458 }
3459 
3460 TEST_F(FormatTest, FormatsEnumTypes) {
3461   verifyFormat("enum X : int {\n"
3462                "  A, // Force multiple lines.\n"
3463                "  B\n"
3464                "};");
3465   verifyFormat("enum X : int { A, B };");
3466   verifyFormat("enum X : std::uint32_t { A, B };");
3467 }
3468 
3469 TEST_F(FormatTest, FormatsTypedefEnum) {
3470   FormatStyle Style = getLLVMStyleWithColumns(40);
3471   verifyFormat("typedef enum {} EmptyEnum;");
3472   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3473   verifyFormat("typedef enum {\n"
3474                "  ZERO = 0,\n"
3475                "  ONE = 1,\n"
3476                "  TWO = 2,\n"
3477                "  THREE = 3\n"
3478                "} LongEnum;",
3479                Style);
3480   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3481   Style.BraceWrapping.AfterEnum = true;
3482   verifyFormat("typedef enum {} EmptyEnum;");
3483   verifyFormat("typedef enum { A, B, C } ShortEnum;");
3484   verifyFormat("typedef enum\n"
3485                "{\n"
3486                "  ZERO = 0,\n"
3487                "  ONE = 1,\n"
3488                "  TWO = 2,\n"
3489                "  THREE = 3\n"
3490                "} LongEnum;",
3491                Style);
3492 }
3493 
3494 TEST_F(FormatTest, FormatsNSEnums) {
3495   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
3496   verifyGoogleFormat(
3497       "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
3498   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
3499                      "  // Information about someDecentlyLongValue.\n"
3500                      "  someDecentlyLongValue,\n"
3501                      "  // Information about anotherDecentlyLongValue.\n"
3502                      "  anotherDecentlyLongValue,\n"
3503                      "  // Information about aThirdDecentlyLongValue.\n"
3504                      "  aThirdDecentlyLongValue\n"
3505                      "};");
3506   verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n"
3507                      "  // Information about someDecentlyLongValue.\n"
3508                      "  someDecentlyLongValue,\n"
3509                      "  // Information about anotherDecentlyLongValue.\n"
3510                      "  anotherDecentlyLongValue,\n"
3511                      "  // Information about aThirdDecentlyLongValue.\n"
3512                      "  aThirdDecentlyLongValue\n"
3513                      "};");
3514   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
3515                      "  a = 1,\n"
3516                      "  b = 2,\n"
3517                      "  c = 3,\n"
3518                      "};");
3519   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
3520                      "  a = 1,\n"
3521                      "  b = 2,\n"
3522                      "  c = 3,\n"
3523                      "};");
3524   verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
3525                      "  a = 1,\n"
3526                      "  b = 2,\n"
3527                      "  c = 3,\n"
3528                      "};");
3529   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
3530                      "  a = 1,\n"
3531                      "  b = 2,\n"
3532                      "  c = 3,\n"
3533                      "};");
3534 }
3535 
3536 TEST_F(FormatTest, FormatsBitfields) {
3537   verifyFormat("struct Bitfields {\n"
3538                "  unsigned sClass : 8;\n"
3539                "  unsigned ValueKind : 2;\n"
3540                "};");
3541   verifyFormat("struct A {\n"
3542                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
3543                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
3544                "};");
3545   verifyFormat("struct MyStruct {\n"
3546                "  uchar data;\n"
3547                "  uchar : 8;\n"
3548                "  uchar : 8;\n"
3549                "  uchar other;\n"
3550                "};");
3551   FormatStyle Style = getLLVMStyle();
3552   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
3553   verifyFormat("struct Bitfields {\n"
3554                "  unsigned sClass:8;\n"
3555                "  unsigned ValueKind:2;\n"
3556                "  uchar other;\n"
3557                "};",
3558                Style);
3559   verifyFormat("struct A {\n"
3560                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n"
3561                "      bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n"
3562                "};",
3563                Style);
3564   Style.BitFieldColonSpacing = FormatStyle::BFCS_Before;
3565   verifyFormat("struct Bitfields {\n"
3566                "  unsigned sClass :8;\n"
3567                "  unsigned ValueKind :2;\n"
3568                "  uchar other;\n"
3569                "};",
3570                Style);
3571   Style.BitFieldColonSpacing = FormatStyle::BFCS_After;
3572   verifyFormat("struct Bitfields {\n"
3573                "  unsigned sClass: 8;\n"
3574                "  unsigned ValueKind: 2;\n"
3575                "  uchar other;\n"
3576                "};",
3577                Style);
3578 }
3579 
3580 TEST_F(FormatTest, FormatsNamespaces) {
3581   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
3582   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
3583 
3584   verifyFormat("namespace some_namespace {\n"
3585                "class A {};\n"
3586                "void f() { f(); }\n"
3587                "}",
3588                LLVMWithNoNamespaceFix);
3589   verifyFormat("namespace N::inline D {\n"
3590                "class A {};\n"
3591                "void f() { f(); }\n"
3592                "}",
3593                LLVMWithNoNamespaceFix);
3594   verifyFormat("namespace N::inline D::E {\n"
3595                "class A {};\n"
3596                "void f() { f(); }\n"
3597                "}",
3598                LLVMWithNoNamespaceFix);
3599   verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
3600                "class A {};\n"
3601                "void f() { f(); }\n"
3602                "}",
3603                LLVMWithNoNamespaceFix);
3604   verifyFormat("/* something */ namespace some_namespace {\n"
3605                "class A {};\n"
3606                "void f() { f(); }\n"
3607                "}",
3608                LLVMWithNoNamespaceFix);
3609   verifyFormat("namespace {\n"
3610                "class A {};\n"
3611                "void f() { f(); }\n"
3612                "}",
3613                LLVMWithNoNamespaceFix);
3614   verifyFormat("/* something */ namespace {\n"
3615                "class A {};\n"
3616                "void f() { f(); }\n"
3617                "}",
3618                LLVMWithNoNamespaceFix);
3619   verifyFormat("inline namespace X {\n"
3620                "class A {};\n"
3621                "void f() { f(); }\n"
3622                "}",
3623                LLVMWithNoNamespaceFix);
3624   verifyFormat("/* something */ inline namespace X {\n"
3625                "class A {};\n"
3626                "void f() { f(); }\n"
3627                "}",
3628                LLVMWithNoNamespaceFix);
3629   verifyFormat("export namespace X {\n"
3630                "class A {};\n"
3631                "void f() { f(); }\n"
3632                "}",
3633                LLVMWithNoNamespaceFix);
3634   verifyFormat("using namespace some_namespace;\n"
3635                "class A {};\n"
3636                "void f() { f(); }",
3637                LLVMWithNoNamespaceFix);
3638 
3639   // This code is more common than we thought; if we
3640   // layout this correctly the semicolon will go into
3641   // its own line, which is undesirable.
3642   verifyFormat("namespace {};", LLVMWithNoNamespaceFix);
3643   verifyFormat("namespace {\n"
3644                "class A {};\n"
3645                "};",
3646                LLVMWithNoNamespaceFix);
3647 
3648   verifyFormat("namespace {\n"
3649                "int SomeVariable = 0; // comment\n"
3650                "} // namespace",
3651                LLVMWithNoNamespaceFix);
3652   EXPECT_EQ("#ifndef HEADER_GUARD\n"
3653             "#define HEADER_GUARD\n"
3654             "namespace my_namespace {\n"
3655             "int i;\n"
3656             "} // my_namespace\n"
3657             "#endif // HEADER_GUARD",
3658             format("#ifndef HEADER_GUARD\n"
3659                    " #define HEADER_GUARD\n"
3660                    "   namespace my_namespace {\n"
3661                    "int i;\n"
3662                    "}    // my_namespace\n"
3663                    "#endif    // HEADER_GUARD",
3664                    LLVMWithNoNamespaceFix));
3665 
3666   EXPECT_EQ("namespace A::B {\n"
3667             "class C {};\n"
3668             "}",
3669             format("namespace A::B {\n"
3670                    "class C {};\n"
3671                    "}",
3672                    LLVMWithNoNamespaceFix));
3673 
3674   FormatStyle Style = getLLVMStyle();
3675   Style.NamespaceIndentation = FormatStyle::NI_All;
3676   EXPECT_EQ("namespace out {\n"
3677             "  int i;\n"
3678             "  namespace in {\n"
3679             "    int i;\n"
3680             "  } // namespace in\n"
3681             "} // namespace out",
3682             format("namespace out {\n"
3683                    "int i;\n"
3684                    "namespace in {\n"
3685                    "int i;\n"
3686                    "} // namespace in\n"
3687                    "} // namespace out",
3688                    Style));
3689 
3690   FormatStyle ShortInlineFunctions = getLLVMStyle();
3691   ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All;
3692   ShortInlineFunctions.AllowShortFunctionsOnASingleLine =
3693       FormatStyle::SFS_Inline;
3694   verifyFormat("namespace {\n"
3695                "  void f() {\n"
3696                "    return;\n"
3697                "  }\n"
3698                "} // namespace\n",
3699                ShortInlineFunctions);
3700   verifyFormat("namespace {\n"
3701                "  int some_int;\n"
3702                "  void f() {\n"
3703                "    return;\n"
3704                "  }\n"
3705                "} // namespace\n",
3706                ShortInlineFunctions);
3707   verifyFormat("namespace interface {\n"
3708                "  void f() {\n"
3709                "    return;\n"
3710                "  }\n"
3711                "} // namespace interface\n",
3712                ShortInlineFunctions);
3713   verifyFormat("namespace {\n"
3714                "  class X {\n"
3715                "    void f() { return; }\n"
3716                "  };\n"
3717                "} // namespace\n",
3718                ShortInlineFunctions);
3719   verifyFormat("namespace {\n"
3720                "  struct X {\n"
3721                "    void f() { return; }\n"
3722                "  };\n"
3723                "} // namespace\n",
3724                ShortInlineFunctions);
3725   verifyFormat("namespace {\n"
3726                "  union X {\n"
3727                "    void f() { return; }\n"
3728                "  };\n"
3729                "} // namespace\n",
3730                ShortInlineFunctions);
3731   verifyFormat("extern \"C\" {\n"
3732                "void f() {\n"
3733                "  return;\n"
3734                "}\n"
3735                "} // namespace\n",
3736                ShortInlineFunctions);
3737   verifyFormat("namespace {\n"
3738                "  class X {\n"
3739                "    void f() { return; }\n"
3740                "  } x;\n"
3741                "} // namespace\n",
3742                ShortInlineFunctions);
3743   verifyFormat("namespace {\n"
3744                "  [[nodiscard]] class X {\n"
3745                "    void f() { return; }\n"
3746                "  };\n"
3747                "} // namespace\n",
3748                ShortInlineFunctions);
3749   verifyFormat("namespace {\n"
3750                "  static class X {\n"
3751                "    void f() { return; }\n"
3752                "  } x;\n"
3753                "} // namespace\n",
3754                ShortInlineFunctions);
3755   verifyFormat("namespace {\n"
3756                "  constexpr class X {\n"
3757                "    void f() { return; }\n"
3758                "  } x;\n"
3759                "} // namespace\n",
3760                ShortInlineFunctions);
3761 
3762   ShortInlineFunctions.IndentExternBlock = FormatStyle::IEBS_Indent;
3763   verifyFormat("extern \"C\" {\n"
3764                "  void f() {\n"
3765                "    return;\n"
3766                "  }\n"
3767                "} // namespace\n",
3768                ShortInlineFunctions);
3769 
3770   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3771   EXPECT_EQ("namespace out {\n"
3772             "int i;\n"
3773             "namespace in {\n"
3774             "  int i;\n"
3775             "} // namespace in\n"
3776             "} // namespace out",
3777             format("namespace out {\n"
3778                    "int i;\n"
3779                    "namespace in {\n"
3780                    "int i;\n"
3781                    "} // namespace in\n"
3782                    "} // namespace out",
3783                    Style));
3784 
3785   Style.NamespaceIndentation = FormatStyle::NI_None;
3786   verifyFormat("template <class T>\n"
3787                "concept a_concept = X<>;\n"
3788                "namespace B {\n"
3789                "struct b_struct {};\n"
3790                "} // namespace B\n",
3791                Style);
3792   verifyFormat("template <int I> constexpr void foo requires(I == 42) {}\n"
3793                "namespace ns {\n"
3794                "void foo() {}\n"
3795                "} // namespace ns\n",
3796                Style);
3797 }
3798 
3799 TEST_F(FormatTest, NamespaceMacros) {
3800   FormatStyle Style = getLLVMStyle();
3801   Style.NamespaceMacros.push_back("TESTSUITE");
3802 
3803   verifyFormat("TESTSUITE(A) {\n"
3804                "int foo();\n"
3805                "} // TESTSUITE(A)",
3806                Style);
3807 
3808   verifyFormat("TESTSUITE(A, B) {\n"
3809                "int foo();\n"
3810                "} // TESTSUITE(A)",
3811                Style);
3812 
3813   // Properly indent according to NamespaceIndentation style
3814   Style.NamespaceIndentation = FormatStyle::NI_All;
3815   verifyFormat("TESTSUITE(A) {\n"
3816                "  int foo();\n"
3817                "} // TESTSUITE(A)",
3818                Style);
3819   verifyFormat("TESTSUITE(A) {\n"
3820                "  namespace B {\n"
3821                "    int foo();\n"
3822                "  } // namespace B\n"
3823                "} // TESTSUITE(A)",
3824                Style);
3825   verifyFormat("namespace A {\n"
3826                "  TESTSUITE(B) {\n"
3827                "    int foo();\n"
3828                "  } // TESTSUITE(B)\n"
3829                "} // namespace A",
3830                Style);
3831 
3832   Style.NamespaceIndentation = FormatStyle::NI_Inner;
3833   verifyFormat("TESTSUITE(A) {\n"
3834                "TESTSUITE(B) {\n"
3835                "  int foo();\n"
3836                "} // TESTSUITE(B)\n"
3837                "} // TESTSUITE(A)",
3838                Style);
3839   verifyFormat("TESTSUITE(A) {\n"
3840                "namespace B {\n"
3841                "  int foo();\n"
3842                "} // namespace B\n"
3843                "} // TESTSUITE(A)",
3844                Style);
3845   verifyFormat("namespace A {\n"
3846                "TESTSUITE(B) {\n"
3847                "  int foo();\n"
3848                "} // TESTSUITE(B)\n"
3849                "} // namespace A",
3850                Style);
3851 
3852   // Properly merge namespace-macros blocks in CompactNamespaces mode
3853   Style.NamespaceIndentation = FormatStyle::NI_None;
3854   Style.CompactNamespaces = true;
3855   verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
3856                "}} // TESTSUITE(A::B)",
3857                Style);
3858 
3859   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
3860             "}} // TESTSUITE(out::in)",
3861             format("TESTSUITE(out) {\n"
3862                    "TESTSUITE(in) {\n"
3863                    "} // TESTSUITE(in)\n"
3864                    "} // TESTSUITE(out)",
3865                    Style));
3866 
3867   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
3868             "}} // TESTSUITE(out::in)",
3869             format("TESTSUITE(out) {\n"
3870                    "TESTSUITE(in) {\n"
3871                    "} // TESTSUITE(in)\n"
3872                    "} // TESTSUITE(out)",
3873                    Style));
3874 
3875   // Do not merge different namespaces/macros
3876   EXPECT_EQ("namespace out {\n"
3877             "TESTSUITE(in) {\n"
3878             "} // TESTSUITE(in)\n"
3879             "} // namespace out",
3880             format("namespace out {\n"
3881                    "TESTSUITE(in) {\n"
3882                    "} // TESTSUITE(in)\n"
3883                    "} // namespace out",
3884                    Style));
3885   EXPECT_EQ("TESTSUITE(out) {\n"
3886             "namespace in {\n"
3887             "} // namespace in\n"
3888             "} // TESTSUITE(out)",
3889             format("TESTSUITE(out) {\n"
3890                    "namespace in {\n"
3891                    "} // namespace in\n"
3892                    "} // TESTSUITE(out)",
3893                    Style));
3894   Style.NamespaceMacros.push_back("FOOBAR");
3895   EXPECT_EQ("TESTSUITE(out) {\n"
3896             "FOOBAR(in) {\n"
3897             "} // FOOBAR(in)\n"
3898             "} // TESTSUITE(out)",
3899             format("TESTSUITE(out) {\n"
3900                    "FOOBAR(in) {\n"
3901                    "} // FOOBAR(in)\n"
3902                    "} // TESTSUITE(out)",
3903                    Style));
3904 }
3905 
3906 TEST_F(FormatTest, FormatsCompactNamespaces) {
3907   FormatStyle Style = getLLVMStyle();
3908   Style.CompactNamespaces = true;
3909   Style.NamespaceMacros.push_back("TESTSUITE");
3910 
3911   verifyFormat("namespace A { namespace B {\n"
3912                "}} // namespace A::B",
3913                Style);
3914 
3915   EXPECT_EQ("namespace out { namespace in {\n"
3916             "}} // namespace out::in",
3917             format("namespace out {\n"
3918                    "namespace in {\n"
3919                    "} // namespace in\n"
3920                    "} // namespace out",
3921                    Style));
3922 
3923   // Only namespaces which have both consecutive opening and end get compacted
3924   EXPECT_EQ("namespace out {\n"
3925             "namespace in1 {\n"
3926             "} // namespace in1\n"
3927             "namespace in2 {\n"
3928             "} // namespace in2\n"
3929             "} // namespace out",
3930             format("namespace out {\n"
3931                    "namespace in1 {\n"
3932                    "} // namespace in1\n"
3933                    "namespace in2 {\n"
3934                    "} // namespace in2\n"
3935                    "} // namespace out",
3936                    Style));
3937 
3938   EXPECT_EQ("namespace out {\n"
3939             "int i;\n"
3940             "namespace in {\n"
3941             "int j;\n"
3942             "} // namespace in\n"
3943             "int k;\n"
3944             "} // namespace out",
3945             format("namespace out { int i;\n"
3946                    "namespace in { int j; } // namespace in\n"
3947                    "int k; } // namespace out",
3948                    Style));
3949 
3950   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
3951             "}}} // namespace A::B::C\n",
3952             format("namespace A { namespace B {\n"
3953                    "namespace C {\n"
3954                    "}} // namespace B::C\n"
3955                    "} // namespace A\n",
3956                    Style));
3957 
3958   Style.ColumnLimit = 40;
3959   EXPECT_EQ("namespace aaaaaaaaaa {\n"
3960             "namespace bbbbbbbbbb {\n"
3961             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
3962             format("namespace aaaaaaaaaa {\n"
3963                    "namespace bbbbbbbbbb {\n"
3964                    "} // namespace bbbbbbbbbb\n"
3965                    "} // namespace aaaaaaaaaa",
3966                    Style));
3967 
3968   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
3969             "namespace cccccc {\n"
3970             "}}} // namespace aaaaaa::bbbbbb::cccccc",
3971             format("namespace aaaaaa {\n"
3972                    "namespace bbbbbb {\n"
3973                    "namespace cccccc {\n"
3974                    "} // namespace cccccc\n"
3975                    "} // namespace bbbbbb\n"
3976                    "} // namespace aaaaaa",
3977                    Style));
3978   Style.ColumnLimit = 80;
3979 
3980   // Extra semicolon after 'inner' closing brace prevents merging
3981   EXPECT_EQ("namespace out { namespace in {\n"
3982             "}; } // namespace out::in",
3983             format("namespace out {\n"
3984                    "namespace in {\n"
3985                    "}; // namespace in\n"
3986                    "} // namespace out",
3987                    Style));
3988 
3989   // Extra semicolon after 'outer' closing brace is conserved
3990   EXPECT_EQ("namespace out { namespace in {\n"
3991             "}}; // namespace out::in",
3992             format("namespace out {\n"
3993                    "namespace in {\n"
3994                    "} // namespace in\n"
3995                    "}; // namespace out",
3996                    Style));
3997 
3998   Style.NamespaceIndentation = FormatStyle::NI_All;
3999   EXPECT_EQ("namespace out { namespace in {\n"
4000             "  int i;\n"
4001             "}} // namespace out::in",
4002             format("namespace out {\n"
4003                    "namespace in {\n"
4004                    "int i;\n"
4005                    "} // namespace in\n"
4006                    "} // namespace out",
4007                    Style));
4008   EXPECT_EQ("namespace out { namespace mid {\n"
4009             "  namespace in {\n"
4010             "    int j;\n"
4011             "  } // namespace in\n"
4012             "  int k;\n"
4013             "}} // namespace out::mid",
4014             format("namespace out { namespace mid {\n"
4015                    "namespace in { int j; } // namespace in\n"
4016                    "int k; }} // namespace out::mid",
4017                    Style));
4018 
4019   Style.NamespaceIndentation = FormatStyle::NI_Inner;
4020   EXPECT_EQ("namespace out { namespace in {\n"
4021             "  int i;\n"
4022             "}} // namespace out::in",
4023             format("namespace out {\n"
4024                    "namespace in {\n"
4025                    "int i;\n"
4026                    "} // namespace in\n"
4027                    "} // namespace out",
4028                    Style));
4029   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
4030             "  int i;\n"
4031             "}}} // namespace out::mid::in",
4032             format("namespace out {\n"
4033                    "namespace mid {\n"
4034                    "namespace in {\n"
4035                    "int i;\n"
4036                    "} // namespace in\n"
4037                    "} // namespace mid\n"
4038                    "} // namespace out",
4039                    Style));
4040 
4041   Style.CompactNamespaces = true;
4042   Style.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
4043   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4044   Style.BraceWrapping.BeforeLambdaBody = true;
4045   verifyFormat("namespace out { namespace in {\n"
4046                "}} // namespace out::in",
4047                Style);
4048   EXPECT_EQ("namespace out { namespace in {\n"
4049             "}} // namespace out::in",
4050             format("namespace out {\n"
4051                    "namespace in {\n"
4052                    "} // namespace in\n"
4053                    "} // namespace out",
4054                    Style));
4055 }
4056 
4057 TEST_F(FormatTest, FormatsExternC) {
4058   verifyFormat("extern \"C\" {\nint a;");
4059   verifyFormat("extern \"C\" {}");
4060   verifyFormat("extern \"C\" {\n"
4061                "int foo();\n"
4062                "}");
4063   verifyFormat("extern \"C\" int foo() {}");
4064   verifyFormat("extern \"C\" int foo();");
4065   verifyFormat("extern \"C\" int foo() {\n"
4066                "  int i = 42;\n"
4067                "  return i;\n"
4068                "}");
4069 
4070   FormatStyle Style = getLLVMStyle();
4071   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4072   Style.BraceWrapping.AfterFunction = true;
4073   verifyFormat("extern \"C\" int foo() {}", Style);
4074   verifyFormat("extern \"C\" int foo();", Style);
4075   verifyFormat("extern \"C\" int foo()\n"
4076                "{\n"
4077                "  int i = 42;\n"
4078                "  return i;\n"
4079                "}",
4080                Style);
4081 
4082   Style.BraceWrapping.AfterExternBlock = true;
4083   Style.BraceWrapping.SplitEmptyRecord = false;
4084   verifyFormat("extern \"C\"\n"
4085                "{}",
4086                Style);
4087   verifyFormat("extern \"C\"\n"
4088                "{\n"
4089                "  int foo();\n"
4090                "}",
4091                Style);
4092 }
4093 
4094 TEST_F(FormatTest, IndentExternBlockStyle) {
4095   FormatStyle Style = getLLVMStyle();
4096   Style.IndentWidth = 2;
4097 
4098   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4099   verifyFormat("extern \"C\" { /*9*/\n"
4100                "}",
4101                Style);
4102   verifyFormat("extern \"C\" {\n"
4103                "  int foo10();\n"
4104                "}",
4105                Style);
4106 
4107   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4108   verifyFormat("extern \"C\" { /*11*/\n"
4109                "}",
4110                Style);
4111   verifyFormat("extern \"C\" {\n"
4112                "int foo12();\n"
4113                "}",
4114                Style);
4115 
4116   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4117   Style.BraceWrapping.AfterExternBlock = true;
4118   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4119   verifyFormat("extern \"C\"\n"
4120                "{ /*13*/\n"
4121                "}",
4122                Style);
4123   verifyFormat("extern \"C\"\n{\n"
4124                "  int foo14();\n"
4125                "}",
4126                Style);
4127 
4128   Style.BraceWrapping.AfterExternBlock = false;
4129   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4130   verifyFormat("extern \"C\" { /*15*/\n"
4131                "}",
4132                Style);
4133   verifyFormat("extern \"C\" {\n"
4134                "int foo16();\n"
4135                "}",
4136                Style);
4137 
4138   Style.BraceWrapping.AfterExternBlock = true;
4139   verifyFormat("extern \"C\"\n"
4140                "{ /*13*/\n"
4141                "}",
4142                Style);
4143   verifyFormat("extern \"C\"\n"
4144                "{\n"
4145                "int foo14();\n"
4146                "}",
4147                Style);
4148 
4149   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4150   verifyFormat("extern \"C\"\n"
4151                "{ /*13*/\n"
4152                "}",
4153                Style);
4154   verifyFormat("extern \"C\"\n"
4155                "{\n"
4156                "  int foo14();\n"
4157                "}",
4158                Style);
4159 }
4160 
4161 TEST_F(FormatTest, FormatsInlineASM) {
4162   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
4163   verifyFormat("asm(\"nop\" ::: \"memory\");");
4164   verifyFormat(
4165       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
4166       "    \"cpuid\\n\\t\"\n"
4167       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
4168       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
4169       "    : \"a\"(value));");
4170   EXPECT_EQ(
4171       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
4172       "  __asm {\n"
4173       "        mov     edx,[that] // vtable in edx\n"
4174       "        mov     eax,methodIndex\n"
4175       "        call    [edx][eax*4] // stdcall\n"
4176       "  }\n"
4177       "}",
4178       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
4179              "    __asm {\n"
4180              "        mov     edx,[that] // vtable in edx\n"
4181              "        mov     eax,methodIndex\n"
4182              "        call    [edx][eax*4] // stdcall\n"
4183              "    }\n"
4184              "}"));
4185   EXPECT_EQ("_asm {\n"
4186             "  xor eax, eax;\n"
4187             "  cpuid;\n"
4188             "}",
4189             format("_asm {\n"
4190                    "  xor eax, eax;\n"
4191                    "  cpuid;\n"
4192                    "}"));
4193   verifyFormat("void function() {\n"
4194                "  // comment\n"
4195                "  asm(\"\");\n"
4196                "}");
4197   EXPECT_EQ("__asm {\n"
4198             "}\n"
4199             "int i;",
4200             format("__asm   {\n"
4201                    "}\n"
4202                    "int   i;"));
4203 }
4204 
4205 TEST_F(FormatTest, FormatTryCatch) {
4206   verifyFormat("try {\n"
4207                "  throw a * b;\n"
4208                "} catch (int a) {\n"
4209                "  // Do nothing.\n"
4210                "} catch (...) {\n"
4211                "  exit(42);\n"
4212                "}");
4213 
4214   // Function-level try statements.
4215   verifyFormat("int f() try { return 4; } catch (...) {\n"
4216                "  return 5;\n"
4217                "}");
4218   verifyFormat("class A {\n"
4219                "  int a;\n"
4220                "  A() try : a(0) {\n"
4221                "  } catch (...) {\n"
4222                "    throw;\n"
4223                "  }\n"
4224                "};\n");
4225   verifyFormat("class A {\n"
4226                "  int a;\n"
4227                "  A() try : a(0), b{1} {\n"
4228                "  } catch (...) {\n"
4229                "    throw;\n"
4230                "  }\n"
4231                "};\n");
4232   verifyFormat("class A {\n"
4233                "  int a;\n"
4234                "  A() try : a(0), b{1}, c{2} {\n"
4235                "  } catch (...) {\n"
4236                "    throw;\n"
4237                "  }\n"
4238                "};\n");
4239   verifyFormat("class A {\n"
4240                "  int a;\n"
4241                "  A() try : a(0), b{1}, c{2} {\n"
4242                "    { // New scope.\n"
4243                "    }\n"
4244                "  } catch (...) {\n"
4245                "    throw;\n"
4246                "  }\n"
4247                "};\n");
4248 
4249   // Incomplete try-catch blocks.
4250   verifyIncompleteFormat("try {} catch (");
4251 }
4252 
4253 TEST_F(FormatTest, FormatTryAsAVariable) {
4254   verifyFormat("int try;");
4255   verifyFormat("int try, size;");
4256   verifyFormat("try = foo();");
4257   verifyFormat("if (try < size) {\n  return true;\n}");
4258 
4259   verifyFormat("int catch;");
4260   verifyFormat("int catch, size;");
4261   verifyFormat("catch = foo();");
4262   verifyFormat("if (catch < size) {\n  return true;\n}");
4263 
4264   FormatStyle Style = getLLVMStyle();
4265   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4266   Style.BraceWrapping.AfterFunction = true;
4267   Style.BraceWrapping.BeforeCatch = true;
4268   verifyFormat("try {\n"
4269                "  int bar = 1;\n"
4270                "}\n"
4271                "catch (...) {\n"
4272                "  int bar = 1;\n"
4273                "}",
4274                Style);
4275   verifyFormat("#if NO_EX\n"
4276                "try\n"
4277                "#endif\n"
4278                "{\n"
4279                "}\n"
4280                "#if NO_EX\n"
4281                "catch (...) {\n"
4282                "}",
4283                Style);
4284   verifyFormat("try /* abc */ {\n"
4285                "  int bar = 1;\n"
4286                "}\n"
4287                "catch (...) {\n"
4288                "  int bar = 1;\n"
4289                "}",
4290                Style);
4291   verifyFormat("try\n"
4292                "// abc\n"
4293                "{\n"
4294                "  int bar = 1;\n"
4295                "}\n"
4296                "catch (...) {\n"
4297                "  int bar = 1;\n"
4298                "}",
4299                Style);
4300 }
4301 
4302 TEST_F(FormatTest, FormatSEHTryCatch) {
4303   verifyFormat("__try {\n"
4304                "  int a = b * c;\n"
4305                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
4306                "  // Do nothing.\n"
4307                "}");
4308 
4309   verifyFormat("__try {\n"
4310                "  int a = b * c;\n"
4311                "} __finally {\n"
4312                "  // Do nothing.\n"
4313                "}");
4314 
4315   verifyFormat("DEBUG({\n"
4316                "  __try {\n"
4317                "  } __finally {\n"
4318                "  }\n"
4319                "});\n");
4320 }
4321 
4322 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
4323   verifyFormat("try {\n"
4324                "  f();\n"
4325                "} catch {\n"
4326                "  g();\n"
4327                "}");
4328   verifyFormat("try {\n"
4329                "  f();\n"
4330                "} catch (A a) MACRO(x) {\n"
4331                "  g();\n"
4332                "} catch (B b) MACRO(x) {\n"
4333                "  g();\n"
4334                "}");
4335 }
4336 
4337 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
4338   FormatStyle Style = getLLVMStyle();
4339   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
4340                           FormatStyle::BS_WebKit}) {
4341     Style.BreakBeforeBraces = BraceStyle;
4342     verifyFormat("try {\n"
4343                  "  // something\n"
4344                  "} catch (...) {\n"
4345                  "  // something\n"
4346                  "}",
4347                  Style);
4348   }
4349   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4350   verifyFormat("try {\n"
4351                "  // something\n"
4352                "}\n"
4353                "catch (...) {\n"
4354                "  // something\n"
4355                "}",
4356                Style);
4357   verifyFormat("__try {\n"
4358                "  // something\n"
4359                "}\n"
4360                "__finally {\n"
4361                "  // something\n"
4362                "}",
4363                Style);
4364   verifyFormat("@try {\n"
4365                "  // something\n"
4366                "}\n"
4367                "@finally {\n"
4368                "  // something\n"
4369                "}",
4370                Style);
4371   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4372   verifyFormat("try\n"
4373                "{\n"
4374                "  // something\n"
4375                "}\n"
4376                "catch (...)\n"
4377                "{\n"
4378                "  // something\n"
4379                "}",
4380                Style);
4381   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
4382   verifyFormat("try\n"
4383                "  {\n"
4384                "  // something white\n"
4385                "  }\n"
4386                "catch (...)\n"
4387                "  {\n"
4388                "  // something white\n"
4389                "  }",
4390                Style);
4391   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
4392   verifyFormat("try\n"
4393                "  {\n"
4394                "    // something\n"
4395                "  }\n"
4396                "catch (...)\n"
4397                "  {\n"
4398                "    // something\n"
4399                "  }",
4400                Style);
4401   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4402   Style.BraceWrapping.BeforeCatch = true;
4403   verifyFormat("try {\n"
4404                "  // something\n"
4405                "}\n"
4406                "catch (...) {\n"
4407                "  // something\n"
4408                "}",
4409                Style);
4410 }
4411 
4412 TEST_F(FormatTest, StaticInitializers) {
4413   verifyFormat("static SomeClass SC = {1, 'a'};");
4414 
4415   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
4416                "    100000000, "
4417                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
4418 
4419   // Here, everything other than the "}" would fit on a line.
4420   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
4421                "    10000000000000000000000000};");
4422   EXPECT_EQ("S s = {a,\n"
4423             "\n"
4424             "       b};",
4425             format("S s = {\n"
4426                    "  a,\n"
4427                    "\n"
4428                    "  b\n"
4429                    "};"));
4430 
4431   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
4432   // line. However, the formatting looks a bit off and this probably doesn't
4433   // happen often in practice.
4434   verifyFormat("static int Variable[1] = {\n"
4435                "    {1000000000000000000000000000000000000}};",
4436                getLLVMStyleWithColumns(40));
4437 }
4438 
4439 TEST_F(FormatTest, DesignatedInitializers) {
4440   verifyFormat("const struct A a = {.a = 1, .b = 2};");
4441   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
4442                "                    .bbbbbbbbbb = 2,\n"
4443                "                    .cccccccccc = 3,\n"
4444                "                    .dddddddddd = 4,\n"
4445                "                    .eeeeeeeeee = 5};");
4446   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4447                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
4448                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
4449                "    .ccccccccccccccccccccccccccc = 3,\n"
4450                "    .ddddddddddddddddddddddddddd = 4,\n"
4451                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
4452 
4453   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
4454 
4455   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
4456   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
4457                "                    [2] = bbbbbbbbbb,\n"
4458                "                    [3] = cccccccccc,\n"
4459                "                    [4] = dddddddddd,\n"
4460                "                    [5] = eeeeeeeeee};");
4461   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4462                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4463                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
4464                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
4465                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
4466                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
4467 }
4468 
4469 TEST_F(FormatTest, NestedStaticInitializers) {
4470   verifyFormat("static A x = {{{}}};\n");
4471   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
4472                "               {init1, init2, init3, init4}}};",
4473                getLLVMStyleWithColumns(50));
4474 
4475   verifyFormat("somes Status::global_reps[3] = {\n"
4476                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4477                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4478                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
4479                getLLVMStyleWithColumns(60));
4480   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
4481                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
4482                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
4483                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
4484   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
4485                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
4486                "rect.fTop}};");
4487 
4488   verifyFormat(
4489       "SomeArrayOfSomeType a = {\n"
4490       "    {{1, 2, 3},\n"
4491       "     {1, 2, 3},\n"
4492       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
4493       "      333333333333333333333333333333},\n"
4494       "     {1, 2, 3},\n"
4495       "     {1, 2, 3}}};");
4496   verifyFormat(
4497       "SomeArrayOfSomeType a = {\n"
4498       "    {{1, 2, 3}},\n"
4499       "    {{1, 2, 3}},\n"
4500       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
4501       "      333333333333333333333333333333}},\n"
4502       "    {{1, 2, 3}},\n"
4503       "    {{1, 2, 3}}};");
4504 
4505   verifyFormat("struct {\n"
4506                "  unsigned bit;\n"
4507                "  const char *const name;\n"
4508                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
4509                "                 {kOsWin, \"Windows\"},\n"
4510                "                 {kOsLinux, \"Linux\"},\n"
4511                "                 {kOsCrOS, \"Chrome OS\"}};");
4512   verifyFormat("struct {\n"
4513                "  unsigned bit;\n"
4514                "  const char *const name;\n"
4515                "} kBitsToOs[] = {\n"
4516                "    {kOsMac, \"Mac\"},\n"
4517                "    {kOsWin, \"Windows\"},\n"
4518                "    {kOsLinux, \"Linux\"},\n"
4519                "    {kOsCrOS, \"Chrome OS\"},\n"
4520                "};");
4521 }
4522 
4523 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
4524   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
4525                "                      \\\n"
4526                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
4527 }
4528 
4529 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
4530   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
4531                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
4532 
4533   // Do break defaulted and deleted functions.
4534   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4535                "    default;",
4536                getLLVMStyleWithColumns(40));
4537   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
4538                "    delete;",
4539                getLLVMStyleWithColumns(40));
4540 }
4541 
4542 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
4543   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
4544                getLLVMStyleWithColumns(40));
4545   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4546                getLLVMStyleWithColumns(40));
4547   EXPECT_EQ("#define Q                              \\\n"
4548             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
4549             "  \"aaaaaaaa.cpp\"",
4550             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
4551                    getLLVMStyleWithColumns(40)));
4552 }
4553 
4554 TEST_F(FormatTest, UnderstandsLinePPDirective) {
4555   EXPECT_EQ("# 123 \"A string literal\"",
4556             format("   #     123    \"A string literal\""));
4557 }
4558 
4559 TEST_F(FormatTest, LayoutUnknownPPDirective) {
4560   EXPECT_EQ("#;", format("#;"));
4561   verifyFormat("#\n;\n;\n;");
4562 }
4563 
4564 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
4565   EXPECT_EQ("#line 42 \"test\"\n",
4566             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
4567   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
4568                                     getLLVMStyleWithColumns(12)));
4569 }
4570 
4571 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
4572   EXPECT_EQ("#line 42 \"test\"",
4573             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
4574   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
4575 }
4576 
4577 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
4578   verifyFormat("#define A \\x20");
4579   verifyFormat("#define A \\ x20");
4580   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
4581   verifyFormat("#define A ''");
4582   verifyFormat("#define A ''qqq");
4583   verifyFormat("#define A `qqq");
4584   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
4585   EXPECT_EQ("const char *c = STRINGIFY(\n"
4586             "\\na : b);",
4587             format("const char * c = STRINGIFY(\n"
4588                    "\\na : b);"));
4589 
4590   verifyFormat("a\r\\");
4591   verifyFormat("a\v\\");
4592   verifyFormat("a\f\\");
4593 }
4594 
4595 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
4596   FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp);
4597   style.IndentWidth = 4;
4598   style.PPIndentWidth = 1;
4599 
4600   style.IndentPPDirectives = FormatStyle::PPDIS_None;
4601   verifyFormat("#ifdef __linux__\n"
4602                "void foo() {\n"
4603                "    int x = 0;\n"
4604                "}\n"
4605                "#define FOO\n"
4606                "#endif\n"
4607                "void bar() {\n"
4608                "    int y = 0;\n"
4609                "}\n",
4610                style);
4611 
4612   style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
4613   verifyFormat("#ifdef __linux__\n"
4614                "void foo() {\n"
4615                "    int x = 0;\n"
4616                "}\n"
4617                "# define FOO foo\n"
4618                "#endif\n"
4619                "void bar() {\n"
4620                "    int y = 0;\n"
4621                "}\n",
4622                style);
4623 
4624   style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
4625   verifyFormat("#ifdef __linux__\n"
4626                "void foo() {\n"
4627                "    int x = 0;\n"
4628                "}\n"
4629                " #define FOO foo\n"
4630                "#endif\n"
4631                "void bar() {\n"
4632                "    int y = 0;\n"
4633                "}\n",
4634                style);
4635 }
4636 
4637 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
4638   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
4639   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
4640   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
4641   // FIXME: We never break before the macro name.
4642   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
4643 
4644   verifyFormat("#define A A\n#define A A");
4645   verifyFormat("#define A(X) A\n#define A A");
4646 
4647   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
4648   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
4649 }
4650 
4651 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
4652   EXPECT_EQ("// somecomment\n"
4653             "#include \"a.h\"\n"
4654             "#define A(  \\\n"
4655             "    A, B)\n"
4656             "#include \"b.h\"\n"
4657             "// somecomment\n",
4658             format("  // somecomment\n"
4659                    "  #include \"a.h\"\n"
4660                    "#define A(A,\\\n"
4661                    "    B)\n"
4662                    "    #include \"b.h\"\n"
4663                    " // somecomment\n",
4664                    getLLVMStyleWithColumns(13)));
4665 }
4666 
4667 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
4668 
4669 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
4670   EXPECT_EQ("#define A    \\\n"
4671             "  c;         \\\n"
4672             "  e;\n"
4673             "f;",
4674             format("#define A c; e;\n"
4675                    "f;",
4676                    getLLVMStyleWithColumns(14)));
4677 }
4678 
4679 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
4680 
4681 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
4682   EXPECT_EQ("int x,\n"
4683             "#define A\n"
4684             "    y;",
4685             format("int x,\n#define A\ny;"));
4686 }
4687 
4688 TEST_F(FormatTest, HashInMacroDefinition) {
4689   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
4690   EXPECT_EQ("#define A(c) u#c", format("#define A(c) u#c", getLLVMStyle()));
4691   EXPECT_EQ("#define A(c) U#c", format("#define A(c) U#c", getLLVMStyle()));
4692   EXPECT_EQ("#define A(c) u8#c", format("#define A(c) u8#c", getLLVMStyle()));
4693   EXPECT_EQ("#define A(c) LR#c", format("#define A(c) LR#c", getLLVMStyle()));
4694   EXPECT_EQ("#define A(c) uR#c", format("#define A(c) uR#c", getLLVMStyle()));
4695   EXPECT_EQ("#define A(c) UR#c", format("#define A(c) UR#c", getLLVMStyle()));
4696   EXPECT_EQ("#define A(c) u8R#c", format("#define A(c) u8R#c", getLLVMStyle()));
4697   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
4698   verifyFormat("#define A  \\\n"
4699                "  {        \\\n"
4700                "    f(#c); \\\n"
4701                "  }",
4702                getLLVMStyleWithColumns(11));
4703 
4704   verifyFormat("#define A(X)         \\\n"
4705                "  void function##X()",
4706                getLLVMStyleWithColumns(22));
4707 
4708   verifyFormat("#define A(a, b, c)   \\\n"
4709                "  void a##b##c()",
4710                getLLVMStyleWithColumns(22));
4711 
4712   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
4713 }
4714 
4715 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
4716   EXPECT_EQ("#define A (x)", format("#define A (x)"));
4717   EXPECT_EQ("#define A(x)", format("#define A(x)"));
4718 
4719   FormatStyle Style = getLLVMStyle();
4720   Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
4721   verifyFormat("#define true ((foo)1)", Style);
4722   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
4723   verifyFormat("#define false((foo)0)", Style);
4724 }
4725 
4726 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
4727   EXPECT_EQ("#define A b;", format("#define A \\\n"
4728                                    "          \\\n"
4729                                    "  b;",
4730                                    getLLVMStyleWithColumns(25)));
4731   EXPECT_EQ("#define A \\\n"
4732             "          \\\n"
4733             "  a;      \\\n"
4734             "  b;",
4735             format("#define A \\\n"
4736                    "          \\\n"
4737                    "  a;      \\\n"
4738                    "  b;",
4739                    getLLVMStyleWithColumns(11)));
4740   EXPECT_EQ("#define A \\\n"
4741             "  a;      \\\n"
4742             "          \\\n"
4743             "  b;",
4744             format("#define A \\\n"
4745                    "  a;      \\\n"
4746                    "          \\\n"
4747                    "  b;",
4748                    getLLVMStyleWithColumns(11)));
4749 }
4750 
4751 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
4752   verifyIncompleteFormat("#define A :");
4753   verifyFormat("#define SOMECASES  \\\n"
4754                "  case 1:          \\\n"
4755                "  case 2\n",
4756                getLLVMStyleWithColumns(20));
4757   verifyFormat("#define MACRO(a) \\\n"
4758                "  if (a)         \\\n"
4759                "    f();         \\\n"
4760                "  else           \\\n"
4761                "    g()",
4762                getLLVMStyleWithColumns(18));
4763   verifyFormat("#define A template <typename T>");
4764   verifyIncompleteFormat("#define STR(x) #x\n"
4765                          "f(STR(this_is_a_string_literal{));");
4766   verifyFormat("#pragma omp threadprivate( \\\n"
4767                "    y)), // expected-warning",
4768                getLLVMStyleWithColumns(28));
4769   verifyFormat("#d, = };");
4770   verifyFormat("#if \"a");
4771   verifyIncompleteFormat("({\n"
4772                          "#define b     \\\n"
4773                          "  }           \\\n"
4774                          "  a\n"
4775                          "a",
4776                          getLLVMStyleWithColumns(15));
4777   verifyFormat("#define A     \\\n"
4778                "  {           \\\n"
4779                "    {\n"
4780                "#define B     \\\n"
4781                "  }           \\\n"
4782                "  }",
4783                getLLVMStyleWithColumns(15));
4784   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
4785   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
4786   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
4787   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
4788 }
4789 
4790 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
4791   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
4792   EXPECT_EQ("class A : public QObject {\n"
4793             "  Q_OBJECT\n"
4794             "\n"
4795             "  A() {}\n"
4796             "};",
4797             format("class A  :  public QObject {\n"
4798                    "     Q_OBJECT\n"
4799                    "\n"
4800                    "  A() {\n}\n"
4801                    "}  ;"));
4802   EXPECT_EQ("MACRO\n"
4803             "/*static*/ int i;",
4804             format("MACRO\n"
4805                    " /*static*/ int   i;"));
4806   EXPECT_EQ("SOME_MACRO\n"
4807             "namespace {\n"
4808             "void f();\n"
4809             "} // namespace",
4810             format("SOME_MACRO\n"
4811                    "  namespace    {\n"
4812                    "void   f(  );\n"
4813                    "} // namespace"));
4814   // Only if the identifier contains at least 5 characters.
4815   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
4816   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
4817   // Only if everything is upper case.
4818   EXPECT_EQ("class A : public QObject {\n"
4819             "  Q_Object A() {}\n"
4820             "};",
4821             format("class A  :  public QObject {\n"
4822                    "     Q_Object\n"
4823                    "  A() {\n}\n"
4824                    "}  ;"));
4825 
4826   // Only if the next line can actually start an unwrapped line.
4827   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
4828             format("SOME_WEIRD_LOG_MACRO\n"
4829                    "<< SomeThing;"));
4830 
4831   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
4832                "(n, buffers))\n",
4833                getChromiumStyle(FormatStyle::LK_Cpp));
4834 
4835   // See PR41483
4836   EXPECT_EQ("/**/ FOO(a)\n"
4837             "FOO(b)",
4838             format("/**/ FOO(a)\n"
4839                    "FOO(b)"));
4840 }
4841 
4842 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
4843   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4844             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4845             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4846             "class X {};\n"
4847             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4848             "int *createScopDetectionPass() { return 0; }",
4849             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
4850                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
4851                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
4852                    "  class X {};\n"
4853                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
4854                    "  int *createScopDetectionPass() { return 0; }"));
4855   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
4856   // braces, so that inner block is indented one level more.
4857   EXPECT_EQ("int q() {\n"
4858             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4859             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4860             "  IPC_END_MESSAGE_MAP()\n"
4861             "}",
4862             format("int q() {\n"
4863                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
4864                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
4865                    "  IPC_END_MESSAGE_MAP()\n"
4866                    "}"));
4867 
4868   // Same inside macros.
4869   EXPECT_EQ("#define LIST(L) \\\n"
4870             "  L(A)          \\\n"
4871             "  L(B)          \\\n"
4872             "  L(C)",
4873             format("#define LIST(L) \\\n"
4874                    "  L(A) \\\n"
4875                    "  L(B) \\\n"
4876                    "  L(C)",
4877                    getGoogleStyle()));
4878 
4879   // These must not be recognized as macros.
4880   EXPECT_EQ("int q() {\n"
4881             "  f(x);\n"
4882             "  f(x) {}\n"
4883             "  f(x)->g();\n"
4884             "  f(x)->*g();\n"
4885             "  f(x).g();\n"
4886             "  f(x) = x;\n"
4887             "  f(x) += x;\n"
4888             "  f(x) -= x;\n"
4889             "  f(x) *= x;\n"
4890             "  f(x) /= x;\n"
4891             "  f(x) %= x;\n"
4892             "  f(x) &= x;\n"
4893             "  f(x) |= x;\n"
4894             "  f(x) ^= x;\n"
4895             "  f(x) >>= x;\n"
4896             "  f(x) <<= x;\n"
4897             "  f(x)[y].z();\n"
4898             "  LOG(INFO) << x;\n"
4899             "  ifstream(x) >> x;\n"
4900             "}\n",
4901             format("int q() {\n"
4902                    "  f(x)\n;\n"
4903                    "  f(x)\n {}\n"
4904                    "  f(x)\n->g();\n"
4905                    "  f(x)\n->*g();\n"
4906                    "  f(x)\n.g();\n"
4907                    "  f(x)\n = x;\n"
4908                    "  f(x)\n += x;\n"
4909                    "  f(x)\n -= x;\n"
4910                    "  f(x)\n *= x;\n"
4911                    "  f(x)\n /= x;\n"
4912                    "  f(x)\n %= x;\n"
4913                    "  f(x)\n &= x;\n"
4914                    "  f(x)\n |= x;\n"
4915                    "  f(x)\n ^= x;\n"
4916                    "  f(x)\n >>= x;\n"
4917                    "  f(x)\n <<= x;\n"
4918                    "  f(x)\n[y].z();\n"
4919                    "  LOG(INFO)\n << x;\n"
4920                    "  ifstream(x)\n >> x;\n"
4921                    "}\n"));
4922   EXPECT_EQ("int q() {\n"
4923             "  F(x)\n"
4924             "  if (1) {\n"
4925             "  }\n"
4926             "  F(x)\n"
4927             "  while (1) {\n"
4928             "  }\n"
4929             "  F(x)\n"
4930             "  G(x);\n"
4931             "  F(x)\n"
4932             "  try {\n"
4933             "    Q();\n"
4934             "  } catch (...) {\n"
4935             "  }\n"
4936             "}\n",
4937             format("int q() {\n"
4938                    "F(x)\n"
4939                    "if (1) {}\n"
4940                    "F(x)\n"
4941                    "while (1) {}\n"
4942                    "F(x)\n"
4943                    "G(x);\n"
4944                    "F(x)\n"
4945                    "try { Q(); } catch (...) {}\n"
4946                    "}\n"));
4947   EXPECT_EQ("class A {\n"
4948             "  A() : t(0) {}\n"
4949             "  A(int i) noexcept() : {}\n"
4950             "  A(X x)\n" // FIXME: function-level try blocks are broken.
4951             "  try : t(0) {\n"
4952             "  } catch (...) {\n"
4953             "  }\n"
4954             "};",
4955             format("class A {\n"
4956                    "  A()\n : t(0) {}\n"
4957                    "  A(int i)\n noexcept() : {}\n"
4958                    "  A(X x)\n"
4959                    "  try : t(0) {} catch (...) {}\n"
4960                    "};"));
4961   FormatStyle Style = getLLVMStyle();
4962   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4963   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
4964   Style.BraceWrapping.AfterFunction = true;
4965   EXPECT_EQ("void f()\n"
4966             "try\n"
4967             "{\n"
4968             "}",
4969             format("void f() try {\n"
4970                    "}",
4971                    Style));
4972   EXPECT_EQ("class SomeClass {\n"
4973             "public:\n"
4974             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4975             "};",
4976             format("class SomeClass {\n"
4977                    "public:\n"
4978                    "  SomeClass()\n"
4979                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4980                    "};"));
4981   EXPECT_EQ("class SomeClass {\n"
4982             "public:\n"
4983             "  SomeClass()\n"
4984             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4985             "};",
4986             format("class SomeClass {\n"
4987                    "public:\n"
4988                    "  SomeClass()\n"
4989                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
4990                    "};",
4991                    getLLVMStyleWithColumns(40)));
4992 
4993   verifyFormat("MACRO(>)");
4994 
4995   // Some macros contain an implicit semicolon.
4996   Style = getLLVMStyle();
4997   Style.StatementMacros.push_back("FOO");
4998   verifyFormat("FOO(a) int b = 0;");
4999   verifyFormat("FOO(a)\n"
5000                "int b = 0;",
5001                Style);
5002   verifyFormat("FOO(a);\n"
5003                "int b = 0;",
5004                Style);
5005   verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
5006                "int b = 0;",
5007                Style);
5008   verifyFormat("FOO()\n"
5009                "int b = 0;",
5010                Style);
5011   verifyFormat("FOO\n"
5012                "int b = 0;",
5013                Style);
5014   verifyFormat("void f() {\n"
5015                "  FOO(a)\n"
5016                "  return a;\n"
5017                "}",
5018                Style);
5019   verifyFormat("FOO(a)\n"
5020                "FOO(b)",
5021                Style);
5022   verifyFormat("int a = 0;\n"
5023                "FOO(b)\n"
5024                "int c = 0;",
5025                Style);
5026   verifyFormat("int a = 0;\n"
5027                "int x = FOO(a)\n"
5028                "int b = 0;",
5029                Style);
5030   verifyFormat("void foo(int a) { FOO(a) }\n"
5031                "uint32_t bar() {}",
5032                Style);
5033 }
5034 
5035 TEST_F(FormatTest, FormatsMacrosWithZeroColumnWidth) {
5036   FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
5037 
5038   verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()",
5039                ZeroColumn);
5040 }
5041 
5042 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
5043   verifyFormat("#define A \\\n"
5044                "  f({     \\\n"
5045                "    g();  \\\n"
5046                "  });",
5047                getLLVMStyleWithColumns(11));
5048 }
5049 
5050 TEST_F(FormatTest, IndentPreprocessorDirectives) {
5051   FormatStyle Style = getLLVMStyleWithColumns(40);
5052   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
5053   verifyFormat("#ifdef _WIN32\n"
5054                "#define A 0\n"
5055                "#ifdef VAR2\n"
5056                "#define B 1\n"
5057                "#include <someheader.h>\n"
5058                "#define MACRO                          \\\n"
5059                "  some_very_long_func_aaaaaaaaaa();\n"
5060                "#endif\n"
5061                "#else\n"
5062                "#define A 1\n"
5063                "#endif",
5064                Style);
5065   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
5066   verifyFormat("#ifdef _WIN32\n"
5067                "#  define A 0\n"
5068                "#  ifdef VAR2\n"
5069                "#    define B 1\n"
5070                "#    include <someheader.h>\n"
5071                "#    define MACRO                      \\\n"
5072                "      some_very_long_func_aaaaaaaaaa();\n"
5073                "#  endif\n"
5074                "#else\n"
5075                "#  define A 1\n"
5076                "#endif",
5077                Style);
5078   verifyFormat("#if A\n"
5079                "#  define MACRO                        \\\n"
5080                "    void a(int x) {                    \\\n"
5081                "      b();                             \\\n"
5082                "      c();                             \\\n"
5083                "      d();                             \\\n"
5084                "      e();                             \\\n"
5085                "      f();                             \\\n"
5086                "    }\n"
5087                "#endif",
5088                Style);
5089   // Comments before include guard.
5090   verifyFormat("// file comment\n"
5091                "// file comment\n"
5092                "#ifndef HEADER_H\n"
5093                "#define HEADER_H\n"
5094                "code();\n"
5095                "#endif",
5096                Style);
5097   // Test with include guards.
5098   verifyFormat("#ifndef HEADER_H\n"
5099                "#define HEADER_H\n"
5100                "code();\n"
5101                "#endif",
5102                Style);
5103   // Include guards must have a #define with the same variable immediately
5104   // after #ifndef.
5105   verifyFormat("#ifndef NOT_GUARD\n"
5106                "#  define FOO\n"
5107                "code();\n"
5108                "#endif",
5109                Style);
5110 
5111   // Include guards must cover the entire file.
5112   verifyFormat("code();\n"
5113                "code();\n"
5114                "#ifndef NOT_GUARD\n"
5115                "#  define NOT_GUARD\n"
5116                "code();\n"
5117                "#endif",
5118                Style);
5119   verifyFormat("#ifndef NOT_GUARD\n"
5120                "#  define NOT_GUARD\n"
5121                "code();\n"
5122                "#endif\n"
5123                "code();",
5124                Style);
5125   // Test with trailing blank lines.
5126   verifyFormat("#ifndef HEADER_H\n"
5127                "#define HEADER_H\n"
5128                "code();\n"
5129                "#endif\n",
5130                Style);
5131   // Include guards don't have #else.
5132   verifyFormat("#ifndef NOT_GUARD\n"
5133                "#  define NOT_GUARD\n"
5134                "code();\n"
5135                "#else\n"
5136                "#endif",
5137                Style);
5138   verifyFormat("#ifndef NOT_GUARD\n"
5139                "#  define NOT_GUARD\n"
5140                "code();\n"
5141                "#elif FOO\n"
5142                "#endif",
5143                Style);
5144   // Non-identifier #define after potential include guard.
5145   verifyFormat("#ifndef FOO\n"
5146                "#  define 1\n"
5147                "#endif\n",
5148                Style);
5149   // #if closes past last non-preprocessor line.
5150   verifyFormat("#ifndef FOO\n"
5151                "#define FOO\n"
5152                "#if 1\n"
5153                "int i;\n"
5154                "#  define A 0\n"
5155                "#endif\n"
5156                "#endif\n",
5157                Style);
5158   // Don't crash if there is an #elif directive without a condition.
5159   verifyFormat("#if 1\n"
5160                "int x;\n"
5161                "#elif\n"
5162                "int y;\n"
5163                "#else\n"
5164                "int z;\n"
5165                "#endif",
5166                Style);
5167   // FIXME: This doesn't handle the case where there's code between the
5168   // #ifndef and #define but all other conditions hold. This is because when
5169   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
5170   // previous code line yet, so we can't detect it.
5171   EXPECT_EQ("#ifndef NOT_GUARD\n"
5172             "code();\n"
5173             "#define NOT_GUARD\n"
5174             "code();\n"
5175             "#endif",
5176             format("#ifndef NOT_GUARD\n"
5177                    "code();\n"
5178                    "#  define NOT_GUARD\n"
5179                    "code();\n"
5180                    "#endif",
5181                    Style));
5182   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
5183   // be outside an include guard. Examples are #pragma once and
5184   // #pragma GCC diagnostic, or anything else that does not change the meaning
5185   // of the file if it's included multiple times.
5186   EXPECT_EQ("#ifdef WIN32\n"
5187             "#  pragma once\n"
5188             "#endif\n"
5189             "#ifndef HEADER_H\n"
5190             "#  define HEADER_H\n"
5191             "code();\n"
5192             "#endif",
5193             format("#ifdef WIN32\n"
5194                    "#  pragma once\n"
5195                    "#endif\n"
5196                    "#ifndef HEADER_H\n"
5197                    "#define HEADER_H\n"
5198                    "code();\n"
5199                    "#endif",
5200                    Style));
5201   // FIXME: This does not detect when there is a single non-preprocessor line
5202   // in front of an include-guard-like structure where other conditions hold
5203   // because ScopedLineState hides the line.
5204   EXPECT_EQ("code();\n"
5205             "#ifndef HEADER_H\n"
5206             "#define HEADER_H\n"
5207             "code();\n"
5208             "#endif",
5209             format("code();\n"
5210                    "#ifndef HEADER_H\n"
5211                    "#  define HEADER_H\n"
5212                    "code();\n"
5213                    "#endif",
5214                    Style));
5215   // Keep comments aligned with #, otherwise indent comments normally. These
5216   // tests cannot use verifyFormat because messUp manipulates leading
5217   // whitespace.
5218   {
5219     const char *Expected = ""
5220                            "void f() {\n"
5221                            "#if 1\n"
5222                            "// Preprocessor aligned.\n"
5223                            "#  define A 0\n"
5224                            "  // Code. Separated by blank line.\n"
5225                            "\n"
5226                            "#  define B 0\n"
5227                            "  // Code. Not aligned with #\n"
5228                            "#  define C 0\n"
5229                            "#endif";
5230     const char *ToFormat = ""
5231                            "void f() {\n"
5232                            "#if 1\n"
5233                            "// Preprocessor aligned.\n"
5234                            "#  define A 0\n"
5235                            "// Code. Separated by blank line.\n"
5236                            "\n"
5237                            "#  define B 0\n"
5238                            "   // Code. Not aligned with #\n"
5239                            "#  define C 0\n"
5240                            "#endif";
5241     EXPECT_EQ(Expected, format(ToFormat, Style));
5242     EXPECT_EQ(Expected, format(Expected, Style));
5243   }
5244   // Keep block quotes aligned.
5245   {
5246     const char *Expected = ""
5247                            "void f() {\n"
5248                            "#if 1\n"
5249                            "/* Preprocessor aligned. */\n"
5250                            "#  define A 0\n"
5251                            "  /* Code. Separated by blank line. */\n"
5252                            "\n"
5253                            "#  define B 0\n"
5254                            "  /* Code. Not aligned with # */\n"
5255                            "#  define C 0\n"
5256                            "#endif";
5257     const char *ToFormat = ""
5258                            "void f() {\n"
5259                            "#if 1\n"
5260                            "/* Preprocessor aligned. */\n"
5261                            "#  define A 0\n"
5262                            "/* Code. Separated by blank line. */\n"
5263                            "\n"
5264                            "#  define B 0\n"
5265                            "   /* Code. Not aligned with # */\n"
5266                            "#  define C 0\n"
5267                            "#endif";
5268     EXPECT_EQ(Expected, format(ToFormat, Style));
5269     EXPECT_EQ(Expected, format(Expected, Style));
5270   }
5271   // Keep comments aligned with un-indented directives.
5272   {
5273     const char *Expected = ""
5274                            "void f() {\n"
5275                            "// Preprocessor aligned.\n"
5276                            "#define A 0\n"
5277                            "  // Code. Separated by blank line.\n"
5278                            "\n"
5279                            "#define B 0\n"
5280                            "  // Code. Not aligned with #\n"
5281                            "#define C 0\n";
5282     const char *ToFormat = ""
5283                            "void f() {\n"
5284                            "// Preprocessor aligned.\n"
5285                            "#define A 0\n"
5286                            "// Code. Separated by blank line.\n"
5287                            "\n"
5288                            "#define B 0\n"
5289                            "   // Code. Not aligned with #\n"
5290                            "#define C 0\n";
5291     EXPECT_EQ(Expected, format(ToFormat, Style));
5292     EXPECT_EQ(Expected, format(Expected, Style));
5293   }
5294   // Test AfterHash with tabs.
5295   {
5296     FormatStyle Tabbed = Style;
5297     Tabbed.UseTab = FormatStyle::UT_Always;
5298     Tabbed.IndentWidth = 8;
5299     Tabbed.TabWidth = 8;
5300     verifyFormat("#ifdef _WIN32\n"
5301                  "#\tdefine A 0\n"
5302                  "#\tifdef VAR2\n"
5303                  "#\t\tdefine B 1\n"
5304                  "#\t\tinclude <someheader.h>\n"
5305                  "#\t\tdefine MACRO          \\\n"
5306                  "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
5307                  "#\tendif\n"
5308                  "#else\n"
5309                  "#\tdefine A 1\n"
5310                  "#endif",
5311                  Tabbed);
5312   }
5313 
5314   // Regression test: Multiline-macro inside include guards.
5315   verifyFormat("#ifndef HEADER_H\n"
5316                "#define HEADER_H\n"
5317                "#define A()        \\\n"
5318                "  int i;           \\\n"
5319                "  int j;\n"
5320                "#endif // HEADER_H",
5321                getLLVMStyleWithColumns(20));
5322 
5323   Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
5324   // Basic before hash indent tests
5325   verifyFormat("#ifdef _WIN32\n"
5326                "  #define A 0\n"
5327                "  #ifdef VAR2\n"
5328                "    #define B 1\n"
5329                "    #include <someheader.h>\n"
5330                "    #define MACRO                      \\\n"
5331                "      some_very_long_func_aaaaaaaaaa();\n"
5332                "  #endif\n"
5333                "#else\n"
5334                "  #define A 1\n"
5335                "#endif",
5336                Style);
5337   verifyFormat("#if A\n"
5338                "  #define MACRO                        \\\n"
5339                "    void a(int x) {                    \\\n"
5340                "      b();                             \\\n"
5341                "      c();                             \\\n"
5342                "      d();                             \\\n"
5343                "      e();                             \\\n"
5344                "      f();                             \\\n"
5345                "    }\n"
5346                "#endif",
5347                Style);
5348   // Keep comments aligned with indented directives. These
5349   // tests cannot use verifyFormat because messUp manipulates leading
5350   // whitespace.
5351   {
5352     const char *Expected = "void f() {\n"
5353                            "// Aligned to preprocessor.\n"
5354                            "#if 1\n"
5355                            "  // Aligned to code.\n"
5356                            "  int a;\n"
5357                            "  #if 1\n"
5358                            "    // Aligned to preprocessor.\n"
5359                            "    #define A 0\n"
5360                            "  // Aligned to code.\n"
5361                            "  int b;\n"
5362                            "  #endif\n"
5363                            "#endif\n"
5364                            "}";
5365     const char *ToFormat = "void f() {\n"
5366                            "// Aligned to preprocessor.\n"
5367                            "#if 1\n"
5368                            "// Aligned to code.\n"
5369                            "int a;\n"
5370                            "#if 1\n"
5371                            "// Aligned to preprocessor.\n"
5372                            "#define A 0\n"
5373                            "// Aligned to code.\n"
5374                            "int b;\n"
5375                            "#endif\n"
5376                            "#endif\n"
5377                            "}";
5378     EXPECT_EQ(Expected, format(ToFormat, Style));
5379     EXPECT_EQ(Expected, format(Expected, Style));
5380   }
5381   {
5382     const char *Expected = "void f() {\n"
5383                            "/* Aligned to preprocessor. */\n"
5384                            "#if 1\n"
5385                            "  /* Aligned to code. */\n"
5386                            "  int a;\n"
5387                            "  #if 1\n"
5388                            "    /* Aligned to preprocessor. */\n"
5389                            "    #define A 0\n"
5390                            "  /* Aligned to code. */\n"
5391                            "  int b;\n"
5392                            "  #endif\n"
5393                            "#endif\n"
5394                            "}";
5395     const char *ToFormat = "void f() {\n"
5396                            "/* Aligned to preprocessor. */\n"
5397                            "#if 1\n"
5398                            "/* Aligned to code. */\n"
5399                            "int a;\n"
5400                            "#if 1\n"
5401                            "/* Aligned to preprocessor. */\n"
5402                            "#define A 0\n"
5403                            "/* Aligned to code. */\n"
5404                            "int b;\n"
5405                            "#endif\n"
5406                            "#endif\n"
5407                            "}";
5408     EXPECT_EQ(Expected, format(ToFormat, Style));
5409     EXPECT_EQ(Expected, format(Expected, Style));
5410   }
5411 
5412   // Test single comment before preprocessor
5413   verifyFormat("// Comment\n"
5414                "\n"
5415                "#if 1\n"
5416                "#endif",
5417                Style);
5418 }
5419 
5420 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
5421   verifyFormat("{\n  { a #c; }\n}");
5422 }
5423 
5424 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
5425   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
5426             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
5427   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
5428             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
5429 }
5430 
5431 TEST_F(FormatTest, EscapedNewlines) {
5432   FormatStyle Narrow = getLLVMStyleWithColumns(11);
5433   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
5434             format("#define A \\\nint i;\\\n  int j;", Narrow));
5435   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
5436   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
5437   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
5438   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
5439 
5440   FormatStyle AlignLeft = getLLVMStyle();
5441   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
5442   EXPECT_EQ("#define MACRO(x) \\\n"
5443             "private:         \\\n"
5444             "  int x(int a);\n",
5445             format("#define MACRO(x) \\\n"
5446                    "private:         \\\n"
5447                    "  int x(int a);\n",
5448                    AlignLeft));
5449 
5450   // CRLF line endings
5451   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
5452             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
5453   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
5454   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
5455   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
5456   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
5457   EXPECT_EQ("#define MACRO(x) \\\r\n"
5458             "private:         \\\r\n"
5459             "  int x(int a);\r\n",
5460             format("#define MACRO(x) \\\r\n"
5461                    "private:         \\\r\n"
5462                    "  int x(int a);\r\n",
5463                    AlignLeft));
5464 
5465   FormatStyle DontAlign = getLLVMStyle();
5466   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
5467   DontAlign.MaxEmptyLinesToKeep = 3;
5468   // FIXME: can't use verifyFormat here because the newline before
5469   // "public:" is not inserted the first time it's reformatted
5470   EXPECT_EQ("#define A \\\n"
5471             "  class Foo { \\\n"
5472             "    void bar(); \\\n"
5473             "\\\n"
5474             "\\\n"
5475             "\\\n"
5476             "  public: \\\n"
5477             "    void baz(); \\\n"
5478             "  };",
5479             format("#define A \\\n"
5480                    "  class Foo { \\\n"
5481                    "    void bar(); \\\n"
5482                    "\\\n"
5483                    "\\\n"
5484                    "\\\n"
5485                    "  public: \\\n"
5486                    "    void baz(); \\\n"
5487                    "  };",
5488                    DontAlign));
5489 }
5490 
5491 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
5492   verifyFormat("#define A \\\n"
5493                "  int v(  \\\n"
5494                "      a); \\\n"
5495                "  int i;",
5496                getLLVMStyleWithColumns(11));
5497 }
5498 
5499 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
5500   EXPECT_EQ(
5501       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
5502       "                      \\\n"
5503       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5504       "\n"
5505       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5506       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
5507       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
5508              "\\\n"
5509              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
5510              "  \n"
5511              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
5512              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
5513 }
5514 
5515 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
5516   EXPECT_EQ("int\n"
5517             "#define A\n"
5518             "    a;",
5519             format("int\n#define A\na;"));
5520   verifyFormat("functionCallTo(\n"
5521                "    someOtherFunction(\n"
5522                "        withSomeParameters, whichInSequence,\n"
5523                "        areLongerThanALine(andAnotherCall,\n"
5524                "#define A B\n"
5525                "                           withMoreParamters,\n"
5526                "                           whichStronglyInfluenceTheLayout),\n"
5527                "        andMoreParameters),\n"
5528                "    trailing);",
5529                getLLVMStyleWithColumns(69));
5530   verifyFormat("Foo::Foo()\n"
5531                "#ifdef BAR\n"
5532                "    : baz(0)\n"
5533                "#endif\n"
5534                "{\n"
5535                "}");
5536   verifyFormat("void f() {\n"
5537                "  if (true)\n"
5538                "#ifdef A\n"
5539                "    f(42);\n"
5540                "  x();\n"
5541                "#else\n"
5542                "    g();\n"
5543                "  x();\n"
5544                "#endif\n"
5545                "}");
5546   verifyFormat("void f(param1, param2,\n"
5547                "       param3,\n"
5548                "#ifdef A\n"
5549                "       param4(param5,\n"
5550                "#ifdef A1\n"
5551                "              param6,\n"
5552                "#ifdef A2\n"
5553                "              param7),\n"
5554                "#else\n"
5555                "              param8),\n"
5556                "       param9,\n"
5557                "#endif\n"
5558                "       param10,\n"
5559                "#endif\n"
5560                "       param11)\n"
5561                "#else\n"
5562                "       param12)\n"
5563                "#endif\n"
5564                "{\n"
5565                "  x();\n"
5566                "}",
5567                getLLVMStyleWithColumns(28));
5568   verifyFormat("#if 1\n"
5569                "int i;");
5570   verifyFormat("#if 1\n"
5571                "#endif\n"
5572                "#if 1\n"
5573                "#else\n"
5574                "#endif\n");
5575   verifyFormat("DEBUG({\n"
5576                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5577                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
5578                "});\n"
5579                "#if a\n"
5580                "#else\n"
5581                "#endif");
5582 
5583   verifyIncompleteFormat("void f(\n"
5584                          "#if A\n"
5585                          ");\n"
5586                          "#else\n"
5587                          "#endif");
5588 }
5589 
5590 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
5591   verifyFormat("#endif\n"
5592                "#if B");
5593 }
5594 
5595 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
5596   FormatStyle SingleLine = getLLVMStyle();
5597   SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
5598   verifyFormat("#if 0\n"
5599                "#elif 1\n"
5600                "#endif\n"
5601                "void foo() {\n"
5602                "  if (test) foo2();\n"
5603                "}",
5604                SingleLine);
5605 }
5606 
5607 TEST_F(FormatTest, LayoutBlockInsideParens) {
5608   verifyFormat("functionCall({ int i; });");
5609   verifyFormat("functionCall({\n"
5610                "  int i;\n"
5611                "  int j;\n"
5612                "});");
5613   verifyFormat("functionCall(\n"
5614                "    {\n"
5615                "      int i;\n"
5616                "      int j;\n"
5617                "    },\n"
5618                "    aaaa, bbbb, cccc);");
5619   verifyFormat("functionA(functionB({\n"
5620                "            int i;\n"
5621                "            int j;\n"
5622                "          }),\n"
5623                "          aaaa, bbbb, cccc);");
5624   verifyFormat("functionCall(\n"
5625                "    {\n"
5626                "      int i;\n"
5627                "      int j;\n"
5628                "    },\n"
5629                "    aaaa, bbbb, // comment\n"
5630                "    cccc);");
5631   verifyFormat("functionA(functionB({\n"
5632                "            int i;\n"
5633                "            int j;\n"
5634                "          }),\n"
5635                "          aaaa, bbbb, // comment\n"
5636                "          cccc);");
5637   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
5638   verifyFormat("functionCall(aaaa, bbbb, {\n"
5639                "  int i;\n"
5640                "  int j;\n"
5641                "});");
5642   verifyFormat(
5643       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
5644       "    {\n"
5645       "      int i; // break\n"
5646       "    },\n"
5647       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
5648       "                                     ccccccccccccccccc));");
5649   verifyFormat("DEBUG({\n"
5650                "  if (a)\n"
5651                "    f();\n"
5652                "});");
5653 }
5654 
5655 TEST_F(FormatTest, LayoutBlockInsideStatement) {
5656   EXPECT_EQ("SOME_MACRO { int i; }\n"
5657             "int i;",
5658             format("  SOME_MACRO  {int i;}  int i;"));
5659 }
5660 
5661 TEST_F(FormatTest, LayoutNestedBlocks) {
5662   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
5663                "  struct s {\n"
5664                "    int i;\n"
5665                "  };\n"
5666                "  s kBitsToOs[] = {{10}};\n"
5667                "  for (int i = 0; i < 10; ++i)\n"
5668                "    return;\n"
5669                "}");
5670   verifyFormat("call(parameter, {\n"
5671                "  something();\n"
5672                "  // Comment using all columns.\n"
5673                "  somethingelse();\n"
5674                "});",
5675                getLLVMStyleWithColumns(40));
5676   verifyFormat("DEBUG( //\n"
5677                "    { f(); }, a);");
5678   verifyFormat("DEBUG( //\n"
5679                "    {\n"
5680                "      f(); //\n"
5681                "    },\n"
5682                "    a);");
5683 
5684   EXPECT_EQ("call(parameter, {\n"
5685             "  something();\n"
5686             "  // Comment too\n"
5687             "  // looooooooooong.\n"
5688             "  somethingElse();\n"
5689             "});",
5690             format("call(parameter, {\n"
5691                    "  something();\n"
5692                    "  // Comment too looooooooooong.\n"
5693                    "  somethingElse();\n"
5694                    "});",
5695                    getLLVMStyleWithColumns(29)));
5696   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
5697   EXPECT_EQ("DEBUG({ // comment\n"
5698             "  int i;\n"
5699             "});",
5700             format("DEBUG({ // comment\n"
5701                    "int  i;\n"
5702                    "});"));
5703   EXPECT_EQ("DEBUG({\n"
5704             "  int i;\n"
5705             "\n"
5706             "  // comment\n"
5707             "  int j;\n"
5708             "});",
5709             format("DEBUG({\n"
5710                    "  int  i;\n"
5711                    "\n"
5712                    "  // comment\n"
5713                    "  int  j;\n"
5714                    "});"));
5715 
5716   verifyFormat("DEBUG({\n"
5717                "  if (a)\n"
5718                "    return;\n"
5719                "});");
5720   verifyGoogleFormat("DEBUG({\n"
5721                      "  if (a) return;\n"
5722                      "});");
5723   FormatStyle Style = getGoogleStyle();
5724   Style.ColumnLimit = 45;
5725   verifyFormat("Debug(\n"
5726                "    aaaaa,\n"
5727                "    {\n"
5728                "      if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
5729                "    },\n"
5730                "    a);",
5731                Style);
5732 
5733   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
5734 
5735   verifyNoCrash("^{v^{a}}");
5736 }
5737 
5738 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
5739   EXPECT_EQ("#define MACRO()                     \\\n"
5740             "  Debug(aaa, /* force line break */ \\\n"
5741             "        {                           \\\n"
5742             "          int i;                    \\\n"
5743             "          int j;                    \\\n"
5744             "        })",
5745             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
5746                    "          {  int   i;  int  j;   })",
5747                    getGoogleStyle()));
5748 
5749   EXPECT_EQ("#define A                                       \\\n"
5750             "  [] {                                          \\\n"
5751             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
5752             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
5753             "  }",
5754             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
5755                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
5756                    getGoogleStyle()));
5757 }
5758 
5759 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
5760   EXPECT_EQ("{}", format("{}"));
5761   verifyFormat("enum E {};");
5762   verifyFormat("enum E {}");
5763   FormatStyle Style = getLLVMStyle();
5764   Style.SpaceInEmptyBlock = true;
5765   EXPECT_EQ("void f() { }", format("void f() {}", Style));
5766   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
5767   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
5768   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
5769   Style.BraceWrapping.BeforeElse = false;
5770   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
5771   verifyFormat("if (a)\n"
5772                "{\n"
5773                "} else if (b)\n"
5774                "{\n"
5775                "} else\n"
5776                "{ }",
5777                Style);
5778   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
5779   verifyFormat("if (a) {\n"
5780                "} else if (b) {\n"
5781                "} else {\n"
5782                "}",
5783                Style);
5784   Style.BraceWrapping.BeforeElse = true;
5785   verifyFormat("if (a) { }\n"
5786                "else if (b) { }\n"
5787                "else { }",
5788                Style);
5789 }
5790 
5791 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
5792   FormatStyle Style = getLLVMStyle();
5793   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
5794   Style.MacroBlockEnd = "^[A-Z_]+_END$";
5795   verifyFormat("FOO_BEGIN\n"
5796                "  FOO_ENTRY\n"
5797                "FOO_END",
5798                Style);
5799   verifyFormat("FOO_BEGIN\n"
5800                "  NESTED_FOO_BEGIN\n"
5801                "    NESTED_FOO_ENTRY\n"
5802                "  NESTED_FOO_END\n"
5803                "FOO_END",
5804                Style);
5805   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
5806                "  int x;\n"
5807                "  x = 1;\n"
5808                "FOO_END(Baz)",
5809                Style);
5810 }
5811 
5812 //===----------------------------------------------------------------------===//
5813 // Line break tests.
5814 //===----------------------------------------------------------------------===//
5815 
5816 TEST_F(FormatTest, PreventConfusingIndents) {
5817   verifyFormat(
5818       "void f() {\n"
5819       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
5820       "                         parameter, parameter, parameter)),\n"
5821       "                     SecondLongCall(parameter));\n"
5822       "}");
5823   verifyFormat(
5824       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5825       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5826       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5827       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
5828   verifyFormat(
5829       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5830       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
5831       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5832       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
5833   verifyFormat(
5834       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5835       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
5836       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
5837       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
5838   verifyFormat("int a = bbbb && ccc &&\n"
5839                "        fffff(\n"
5840                "#define A Just forcing a new line\n"
5841                "            ddd);");
5842 }
5843 
5844 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
5845   verifyFormat(
5846       "bool aaaaaaa =\n"
5847       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
5848       "    bbbbbbbb();");
5849   verifyFormat(
5850       "bool aaaaaaa =\n"
5851       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
5852       "    bbbbbbbb();");
5853 
5854   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5855                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
5856                "    ccccccccc == ddddddddddd;");
5857   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
5858                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
5859                "    ccccccccc == ddddddddddd;");
5860   verifyFormat(
5861       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
5862       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
5863       "    ccccccccc == ddddddddddd;");
5864 
5865   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
5866                "                 aaaaaa) &&\n"
5867                "         bbbbbb && cccccc;");
5868   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
5869                "                 aaaaaa) >>\n"
5870                "         bbbbbb;");
5871   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
5872                "    SourceMgr.getSpellingColumnNumber(\n"
5873                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
5874                "    1);");
5875 
5876   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5877                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
5878                "    cccccc) {\n}");
5879   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5880                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
5881                "              cccccc) {\n}");
5882   verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5883                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
5884                "              cccccc) {\n}");
5885   verifyFormat("b = a &&\n"
5886                "    // Comment\n"
5887                "    b.c && d;");
5888 
5889   // If the LHS of a comparison is not a binary expression itself, the
5890   // additional linebreak confuses many people.
5891   verifyFormat(
5892       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5893       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
5894       "}");
5895   verifyFormat(
5896       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5897       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5898       "}");
5899   verifyFormat(
5900       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
5901       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5902       "}");
5903   verifyFormat(
5904       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5905       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
5906       "}");
5907   // Even explicit parentheses stress the precedence enough to make the
5908   // additional break unnecessary.
5909   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5910                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
5911                "}");
5912   // This cases is borderline, but with the indentation it is still readable.
5913   verifyFormat(
5914       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5915       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5916       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5917       "}",
5918       getLLVMStyleWithColumns(75));
5919 
5920   // If the LHS is a binary expression, we should still use the additional break
5921   // as otherwise the formatting hides the operator precedence.
5922   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5923                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5924                "    5) {\n"
5925                "}");
5926   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5927                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
5928                "    5) {\n"
5929                "}");
5930 
5931   FormatStyle OnePerLine = getLLVMStyle();
5932   OnePerLine.BinPackParameters = false;
5933   verifyFormat(
5934       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5935       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5936       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
5937       OnePerLine);
5938 
5939   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
5940                "                .aaa(aaaaaaaaaaaaa) *\n"
5941                "            aaaaaaa +\n"
5942                "        aaaaaaa;",
5943                getLLVMStyleWithColumns(40));
5944 }
5945 
5946 TEST_F(FormatTest, ExpressionIndentation) {
5947   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5948                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5949                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5950                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5951                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
5952                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
5953                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5954                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
5955                "                 ccccccccccccccccccccccccccccccccccccccccc;");
5956   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5957                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5958                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5959                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5960   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5961                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5962                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5963                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5964   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
5965                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5966                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5967                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
5968   verifyFormat("if () {\n"
5969                "} else if (aaaaa && bbbbb > // break\n"
5970                "                        ccccc) {\n"
5971                "}");
5972   verifyFormat("if () {\n"
5973                "} else if constexpr (aaaaa && bbbbb > // break\n"
5974                "                                  ccccc) {\n"
5975                "}");
5976   verifyFormat("if () {\n"
5977                "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
5978                "                                  ccccc) {\n"
5979                "}");
5980   verifyFormat("if () {\n"
5981                "} else if (aaaaa &&\n"
5982                "           bbbbb > // break\n"
5983                "               ccccc &&\n"
5984                "           ddddd) {\n"
5985                "}");
5986 
5987   // Presence of a trailing comment used to change indentation of b.
5988   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
5989                "       b;\n"
5990                "return aaaaaaaaaaaaaaaaaaa +\n"
5991                "       b; //",
5992                getLLVMStyleWithColumns(30));
5993 }
5994 
5995 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
5996   // Not sure what the best system is here. Like this, the LHS can be found
5997   // immediately above an operator (everything with the same or a higher
5998   // indent). The RHS is aligned right of the operator and so compasses
5999   // everything until something with the same indent as the operator is found.
6000   // FIXME: Is this a good system?
6001   FormatStyle Style = getLLVMStyle();
6002   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
6003   verifyFormat(
6004       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6005       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6006       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6007       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6008       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6009       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6010       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6011       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6012       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
6013       Style);
6014   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6015                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6016                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6017                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6018                Style);
6019   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6020                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6021                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6022                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6023                Style);
6024   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6025                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6026                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6027                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6028                Style);
6029   verifyFormat("if () {\n"
6030                "} else if (aaaaa\n"
6031                "           && bbbbb // break\n"
6032                "                  > ccccc) {\n"
6033                "}",
6034                Style);
6035   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6036                "       && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
6037                Style);
6038   verifyFormat("return (a)\n"
6039                "       // comment\n"
6040                "       + b;",
6041                Style);
6042   verifyFormat(
6043       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6044       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6045       "             + cc;",
6046       Style);
6047 
6048   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6049                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
6050                Style);
6051 
6052   // Forced by comments.
6053   verifyFormat(
6054       "unsigned ContentSize =\n"
6055       "    sizeof(int16_t)   // DWARF ARange version number\n"
6056       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6057       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
6058       "    + sizeof(int8_t); // Segment Size (in bytes)");
6059 
6060   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
6061                "       == boost::fusion::at_c<1>(iiii).second;",
6062                Style);
6063 
6064   Style.ColumnLimit = 60;
6065   verifyFormat("zzzzzzzzzz\n"
6066                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6067                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
6068                Style);
6069 
6070   Style.ColumnLimit = 80;
6071   Style.IndentWidth = 4;
6072   Style.TabWidth = 4;
6073   Style.UseTab = FormatStyle::UT_Always;
6074   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6075   Style.AlignOperands = FormatStyle::OAS_DontAlign;
6076   EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
6077             "\t&& (someOtherLongishConditionPart1\n"
6078             "\t\t|| someOtherEvenLongerNestedConditionPart2);",
6079             format("return someVeryVeryLongConditionThatBarelyFitsOnALine && "
6080                    "(someOtherLongishConditionPart1 || "
6081                    "someOtherEvenLongerNestedConditionPart2);",
6082                    Style));
6083 }
6084 
6085 TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
6086   FormatStyle Style = getLLVMStyle();
6087   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
6088   Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
6089 
6090   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6091                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6092                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6093                "              == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6094                "                         * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6095                "                     + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6096                "          && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6097                "                     * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6098                "                 > ccccccccccccccccccccccccccccccccccccccccc;",
6099                Style);
6100   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6101                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6102                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6103                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6104                Style);
6105   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6106                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6107                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6108                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6109                Style);
6110   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6111                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6112                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6113                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
6114                Style);
6115   verifyFormat("if () {\n"
6116                "} else if (aaaaa\n"
6117                "           && bbbbb // break\n"
6118                "                  > ccccc) {\n"
6119                "}",
6120                Style);
6121   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6122                "    && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
6123                Style);
6124   verifyFormat("return (a)\n"
6125                "     // comment\n"
6126                "     + b;",
6127                Style);
6128   verifyFormat(
6129       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6130       "               * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6131       "           + cc;",
6132       Style);
6133   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
6134                "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
6135                "                        : 3333333333333333;",
6136                Style);
6137   verifyFormat(
6138       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
6139       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
6140       "                                             : eeeeeeeeeeeeeeeeee)\n"
6141       "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
6142       "                        : 3333333333333333;",
6143       Style);
6144   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6145                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
6146                Style);
6147 
6148   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
6149                "    == boost::fusion::at_c<1>(iiii).second;",
6150                Style);
6151 
6152   Style.ColumnLimit = 60;
6153   verifyFormat("zzzzzzzzzzzzz\n"
6154                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6155                "   >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
6156                Style);
6157 
6158   // Forced by comments.
6159   Style.ColumnLimit = 80;
6160   verifyFormat(
6161       "unsigned ContentSize\n"
6162       "    = sizeof(int16_t) // DWARF ARange version number\n"
6163       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6164       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
6165       "    + sizeof(int8_t); // Segment Size (in bytes)",
6166       Style);
6167 
6168   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6169   verifyFormat(
6170       "unsigned ContentSize =\n"
6171       "    sizeof(int16_t)   // DWARF ARange version number\n"
6172       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6173       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
6174       "    + sizeof(int8_t); // Segment Size (in bytes)",
6175       Style);
6176 
6177   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
6178   verifyFormat(
6179       "unsigned ContentSize =\n"
6180       "    sizeof(int16_t)   // DWARF ARange version number\n"
6181       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
6182       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
6183       "    + sizeof(int8_t); // Segment Size (in bytes)",
6184       Style);
6185 }
6186 
6187 TEST_F(FormatTest, EnforcedOperatorWraps) {
6188   // Here we'd like to wrap after the || operators, but a comment is forcing an
6189   // earlier wrap.
6190   verifyFormat("bool x = aaaaa //\n"
6191                "         || bbbbb\n"
6192                "         //\n"
6193                "         || cccc;");
6194 }
6195 
6196 TEST_F(FormatTest, NoOperandAlignment) {
6197   FormatStyle Style = getLLVMStyle();
6198   Style.AlignOperands = FormatStyle::OAS_DontAlign;
6199   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
6200                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6201                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6202                Style);
6203   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6204   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6205                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6206                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6207                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6208                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6209                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6210                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6211                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6212                "        > ccccccccccccccccccccccccccccccccccccccccc;",
6213                Style);
6214 
6215   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6216                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6217                "    + cc;",
6218                Style);
6219   verifyFormat("int a = aa\n"
6220                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
6221                "        * cccccccccccccccccccccccccccccccccccc;\n",
6222                Style);
6223 
6224   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6225   verifyFormat("return (a > b\n"
6226                "    // comment1\n"
6227                "    // comment2\n"
6228                "    || c);",
6229                Style);
6230 }
6231 
6232 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
6233   FormatStyle Style = getLLVMStyle();
6234   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6235   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6236                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6237                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
6238                Style);
6239 }
6240 
6241 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
6242   FormatStyle Style = getLLVMStyleWithColumns(40);
6243   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
6244   Style.BinPackArguments = false;
6245   verifyFormat("void test() {\n"
6246                "  someFunction(\n"
6247                "      this + argument + is + quite\n"
6248                "      + long + so + it + gets + wrapped\n"
6249                "      + but + remains + bin - packed);\n"
6250                "}",
6251                Style);
6252   verifyFormat("void test() {\n"
6253                "  someFunction(arg1,\n"
6254                "               this + argument + is\n"
6255                "                   + quite + long + so\n"
6256                "                   + it + gets + wrapped\n"
6257                "                   + but + remains + bin\n"
6258                "                   - packed,\n"
6259                "               arg3);\n"
6260                "}",
6261                Style);
6262   verifyFormat("void test() {\n"
6263                "  someFunction(\n"
6264                "      arg1,\n"
6265                "      this + argument + has\n"
6266                "          + anotherFunc(nested,\n"
6267                "                        calls + whose\n"
6268                "                            + arguments\n"
6269                "                            + are + also\n"
6270                "                            + wrapped,\n"
6271                "                        in + addition)\n"
6272                "          + to + being + bin - packed,\n"
6273                "      arg3);\n"
6274                "}",
6275                Style);
6276 
6277   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
6278   verifyFormat("void test() {\n"
6279                "  someFunction(\n"
6280                "      arg1,\n"
6281                "      this + argument + has +\n"
6282                "          anotherFunc(nested,\n"
6283                "                      calls + whose +\n"
6284                "                          arguments +\n"
6285                "                          are + also +\n"
6286                "                          wrapped,\n"
6287                "                      in + addition) +\n"
6288                "          to + being + bin - packed,\n"
6289                "      arg3);\n"
6290                "}",
6291                Style);
6292 }
6293 
6294 TEST_F(FormatTest, ConstructorInitializers) {
6295   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
6296   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
6297                getLLVMStyleWithColumns(45));
6298   verifyFormat("Constructor()\n"
6299                "    : Inttializer(FitsOnTheLine) {}",
6300                getLLVMStyleWithColumns(44));
6301   verifyFormat("Constructor()\n"
6302                "    : Inttializer(FitsOnTheLine) {}",
6303                getLLVMStyleWithColumns(43));
6304 
6305   verifyFormat("template <typename T>\n"
6306                "Constructor() : Initializer(FitsOnTheLine) {}",
6307                getLLVMStyleWithColumns(45));
6308 
6309   verifyFormat(
6310       "SomeClass::Constructor()\n"
6311       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
6312 
6313   verifyFormat(
6314       "SomeClass::Constructor()\n"
6315       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6316       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
6317   verifyFormat(
6318       "SomeClass::Constructor()\n"
6319       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6320       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
6321   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6322                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6323                "    : aaaaaaaaaa(aaaaaa) {}");
6324 
6325   verifyFormat("Constructor()\n"
6326                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6327                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6328                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6329                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
6330 
6331   verifyFormat("Constructor()\n"
6332                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6333                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
6334 
6335   verifyFormat("Constructor(int Parameter = 0)\n"
6336                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
6337                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
6338   verifyFormat("Constructor()\n"
6339                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
6340                "}",
6341                getLLVMStyleWithColumns(60));
6342   verifyFormat("Constructor()\n"
6343                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6344                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
6345 
6346   // Here a line could be saved by splitting the second initializer onto two
6347   // lines, but that is not desirable.
6348   verifyFormat("Constructor()\n"
6349                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
6350                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
6351                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
6352 
6353   FormatStyle OnePerLine = getLLVMStyle();
6354   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never;
6355   verifyFormat("MyClass::MyClass()\n"
6356                "    : a(a),\n"
6357                "      b(b),\n"
6358                "      c(c) {}",
6359                OnePerLine);
6360   verifyFormat("MyClass::MyClass()\n"
6361                "    : a(a), // comment\n"
6362                "      b(b),\n"
6363                "      c(c) {}",
6364                OnePerLine);
6365   verifyFormat("MyClass::MyClass(int a)\n"
6366                "    : b(a),      // comment\n"
6367                "      c(a + 1) { // lined up\n"
6368                "}",
6369                OnePerLine);
6370   verifyFormat("Constructor()\n"
6371                "    : a(b, b, b) {}",
6372                OnePerLine);
6373   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6374   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
6375   verifyFormat("SomeClass::Constructor()\n"
6376                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6377                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6378                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6379                OnePerLine);
6380   verifyFormat("SomeClass::Constructor()\n"
6381                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
6382                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6383                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6384                OnePerLine);
6385   verifyFormat("MyClass::MyClass(int var)\n"
6386                "    : some_var_(var),            // 4 space indent\n"
6387                "      some_other_var_(var + 1) { // lined up\n"
6388                "}",
6389                OnePerLine);
6390   verifyFormat("Constructor()\n"
6391                "    : aaaaa(aaaaaa),\n"
6392                "      aaaaa(aaaaaa),\n"
6393                "      aaaaa(aaaaaa),\n"
6394                "      aaaaa(aaaaaa),\n"
6395                "      aaaaa(aaaaaa) {}",
6396                OnePerLine);
6397   verifyFormat("Constructor()\n"
6398                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6399                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
6400                OnePerLine);
6401   OnePerLine.BinPackParameters = false;
6402   verifyFormat(
6403       "Constructor()\n"
6404       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6405       "          aaaaaaaaaaa().aaa(),\n"
6406       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6407       OnePerLine);
6408   OnePerLine.ColumnLimit = 60;
6409   verifyFormat("Constructor()\n"
6410                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6411                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
6412                OnePerLine);
6413 
6414   EXPECT_EQ("Constructor()\n"
6415             "    : // Comment forcing unwanted break.\n"
6416             "      aaaa(aaaa) {}",
6417             format("Constructor() :\n"
6418                    "    // Comment forcing unwanted break.\n"
6419                    "    aaaa(aaaa) {}"));
6420 }
6421 
6422 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
6423   FormatStyle Style = getLLVMStyleWithColumns(60);
6424   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6425   Style.BinPackParameters = false;
6426 
6427   for (int i = 0; i < 4; ++i) {
6428     // Test all combinations of parameters that should not have an effect.
6429     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6430     Style.AllowAllArgumentsOnNextLine = i & 2;
6431 
6432     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6433     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6434     verifyFormat("Constructor()\n"
6435                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6436                  Style);
6437     verifyFormat("Constructor() : a(a), b(b) {}", Style);
6438 
6439     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6440     verifyFormat("Constructor()\n"
6441                  "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6442                  "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6443                  Style);
6444     verifyFormat("Constructor() : a(a), b(b) {}", Style);
6445 
6446     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
6447     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6448     verifyFormat("Constructor()\n"
6449                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6450                  Style);
6451 
6452     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6453     verifyFormat("Constructor()\n"
6454                  "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6455                  "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6456                  Style);
6457 
6458     Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6459     Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6460     verifyFormat("Constructor() :\n"
6461                  "    aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6462                  Style);
6463 
6464     Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6465     verifyFormat("Constructor() :\n"
6466                  "    aaaaaaaaaaaaaaaaaa(a),\n"
6467                  "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6468                  Style);
6469   }
6470 
6471   // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
6472   // AllowAllConstructorInitializersOnNextLine in all
6473   // BreakConstructorInitializers modes
6474   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
6475   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6476   verifyFormat("SomeClassWithALongName::Constructor(\n"
6477                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
6478                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6479                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6480                Style);
6481 
6482   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6483   verifyFormat("SomeClassWithALongName::Constructor(\n"
6484                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6485                "    int bbbbbbbbbbbbb,\n"
6486                "    int cccccccccccccccc)\n"
6487                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6488                Style);
6489 
6490   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6491   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6492   verifyFormat("SomeClassWithALongName::Constructor(\n"
6493                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6494                "    int bbbbbbbbbbbbb)\n"
6495                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
6496                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
6497                Style);
6498 
6499   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
6500 
6501   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6502   verifyFormat("SomeClassWithALongName::Constructor(\n"
6503                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
6504                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6505                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6506                Style);
6507 
6508   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6509   verifyFormat("SomeClassWithALongName::Constructor(\n"
6510                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6511                "    int bbbbbbbbbbbbb,\n"
6512                "    int cccccccccccccccc)\n"
6513                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6514                Style);
6515 
6516   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6517   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6518   verifyFormat("SomeClassWithALongName::Constructor(\n"
6519                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6520                "    int bbbbbbbbbbbbb)\n"
6521                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
6522                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
6523                Style);
6524 
6525   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6526   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6527   verifyFormat("SomeClassWithALongName::Constructor(\n"
6528                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
6529                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6530                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6531                Style);
6532 
6533   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6534   verifyFormat("SomeClassWithALongName::Constructor(\n"
6535                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6536                "    int bbbbbbbbbbbbb,\n"
6537                "    int cccccccccccccccc) :\n"
6538                "    aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
6539                Style);
6540 
6541   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6542   Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6543   verifyFormat("SomeClassWithALongName::Constructor(\n"
6544                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6545                "    int bbbbbbbbbbbbb) :\n"
6546                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6547                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
6548                Style);
6549 }
6550 
6551 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
6552   FormatStyle Style = getLLVMStyleWithColumns(60);
6553   Style.BinPackArguments = false;
6554   for (int i = 0; i < 4; ++i) {
6555     // Test all combinations of parameters that should not have an effect.
6556     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
6557     Style.PackConstructorInitializers =
6558         i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never;
6559 
6560     Style.AllowAllArgumentsOnNextLine = true;
6561     verifyFormat("void foo() {\n"
6562                  "  FunctionCallWithReallyLongName(\n"
6563                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
6564                  "}",
6565                  Style);
6566     Style.AllowAllArgumentsOnNextLine = false;
6567     verifyFormat("void foo() {\n"
6568                  "  FunctionCallWithReallyLongName(\n"
6569                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6570                  "      bbbbbbbbbbbb);\n"
6571                  "}",
6572                  Style);
6573 
6574     Style.AllowAllArgumentsOnNextLine = true;
6575     verifyFormat("void foo() {\n"
6576                  "  auto VariableWithReallyLongName = {\n"
6577                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
6578                  "}",
6579                  Style);
6580     Style.AllowAllArgumentsOnNextLine = false;
6581     verifyFormat("void foo() {\n"
6582                  "  auto VariableWithReallyLongName = {\n"
6583                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6584                  "      bbbbbbbbbbbb};\n"
6585                  "}",
6586                  Style);
6587   }
6588 
6589   // This parameter should not affect declarations.
6590   Style.BinPackParameters = false;
6591   Style.AllowAllArgumentsOnNextLine = false;
6592   Style.AllowAllParametersOfDeclarationOnNextLine = true;
6593   verifyFormat("void FunctionCallWithReallyLongName(\n"
6594                "    int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
6595                Style);
6596   Style.AllowAllParametersOfDeclarationOnNextLine = false;
6597   verifyFormat("void FunctionCallWithReallyLongName(\n"
6598                "    int aaaaaaaaaaaaaaaaaaaaaaa,\n"
6599                "    int bbbbbbbbbbbb);",
6600                Style);
6601 }
6602 
6603 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
6604   // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
6605   // and BAS_Align.
6606   FormatStyle Style = getLLVMStyleWithColumns(35);
6607   StringRef Input = "functionCall(paramA, paramB, paramC);\n"
6608                     "void functionDecl(int A, int B, int C);";
6609   Style.AllowAllArgumentsOnNextLine = false;
6610   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6611   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6612                       "    paramC);\n"
6613                       "void functionDecl(int A, int B,\n"
6614                       "    int C);"),
6615             format(Input, Style));
6616   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6617   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6618                       "             paramC);\n"
6619                       "void functionDecl(int A, int B,\n"
6620                       "                  int C);"),
6621             format(Input, Style));
6622   // However, BAS_AlwaysBreak should take precedence over
6623   // AllowAllArgumentsOnNextLine.
6624   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6625   EXPECT_EQ(StringRef("functionCall(\n"
6626                       "    paramA, paramB, paramC);\n"
6627                       "void functionDecl(\n"
6628                       "    int A, int B, int C);"),
6629             format(Input, Style));
6630 
6631   // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
6632   // first argument.
6633   Style.AllowAllArgumentsOnNextLine = true;
6634   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6635   EXPECT_EQ(StringRef("functionCall(\n"
6636                       "    paramA, paramB, paramC);\n"
6637                       "void functionDecl(\n"
6638                       "    int A, int B, int C);"),
6639             format(Input, Style));
6640   // It wouldn't fit on one line with aligned parameters so this setting
6641   // doesn't change anything for BAS_Align.
6642   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6643   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
6644                       "             paramC);\n"
6645                       "void functionDecl(int A, int B,\n"
6646                       "                  int C);"),
6647             format(Input, Style));
6648   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6649   EXPECT_EQ(StringRef("functionCall(\n"
6650                       "    paramA, paramB, paramC);\n"
6651                       "void functionDecl(\n"
6652                       "    int A, int B, int C);"),
6653             format(Input, Style));
6654 }
6655 
6656 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
6657   FormatStyle Style = getLLVMStyle();
6658   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
6659 
6660   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
6661   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
6662                getStyleWithColumns(Style, 45));
6663   verifyFormat("Constructor() :\n"
6664                "    Initializer(FitsOnTheLine) {}",
6665                getStyleWithColumns(Style, 44));
6666   verifyFormat("Constructor() :\n"
6667                "    Initializer(FitsOnTheLine) {}",
6668                getStyleWithColumns(Style, 43));
6669 
6670   verifyFormat("template <typename T>\n"
6671                "Constructor() : Initializer(FitsOnTheLine) {}",
6672                getStyleWithColumns(Style, 50));
6673   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6674   verifyFormat(
6675       "SomeClass::Constructor() :\n"
6676       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6677       Style);
6678 
6679   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
6680   verifyFormat(
6681       "SomeClass::Constructor() :\n"
6682       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6683       Style);
6684 
6685   verifyFormat(
6686       "SomeClass::Constructor() :\n"
6687       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6688       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6689       Style);
6690   verifyFormat(
6691       "SomeClass::Constructor() :\n"
6692       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6693       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
6694       Style);
6695   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6696                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
6697                "    aaaaaaaaaa(aaaaaa) {}",
6698                Style);
6699 
6700   verifyFormat("Constructor() :\n"
6701                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6702                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6703                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6704                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
6705                Style);
6706 
6707   verifyFormat("Constructor() :\n"
6708                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6709                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6710                Style);
6711 
6712   verifyFormat("Constructor(int Parameter = 0) :\n"
6713                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
6714                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
6715                Style);
6716   verifyFormat("Constructor() :\n"
6717                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
6718                "}",
6719                getStyleWithColumns(Style, 60));
6720   verifyFormat("Constructor() :\n"
6721                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6722                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
6723                Style);
6724 
6725   // Here a line could be saved by splitting the second initializer onto two
6726   // lines, but that is not desirable.
6727   verifyFormat("Constructor() :\n"
6728                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
6729                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
6730                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6731                Style);
6732 
6733   FormatStyle OnePerLine = Style;
6734   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
6735   verifyFormat("SomeClass::Constructor() :\n"
6736                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6737                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6738                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6739                OnePerLine);
6740   verifyFormat("SomeClass::Constructor() :\n"
6741                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
6742                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
6743                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
6744                OnePerLine);
6745   verifyFormat("MyClass::MyClass(int var) :\n"
6746                "    some_var_(var),            // 4 space indent\n"
6747                "    some_other_var_(var + 1) { // lined up\n"
6748                "}",
6749                OnePerLine);
6750   verifyFormat("Constructor() :\n"
6751                "    aaaaa(aaaaaa),\n"
6752                "    aaaaa(aaaaaa),\n"
6753                "    aaaaa(aaaaaa),\n"
6754                "    aaaaa(aaaaaa),\n"
6755                "    aaaaa(aaaaaa) {}",
6756                OnePerLine);
6757   verifyFormat("Constructor() :\n"
6758                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6759                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
6760                OnePerLine);
6761   OnePerLine.BinPackParameters = false;
6762   verifyFormat("Constructor() :\n"
6763                "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6764                "        aaaaaaaaaaa().aaa(),\n"
6765                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6766                OnePerLine);
6767   OnePerLine.ColumnLimit = 60;
6768   verifyFormat("Constructor() :\n"
6769                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
6770                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
6771                OnePerLine);
6772 
6773   EXPECT_EQ("Constructor() :\n"
6774             "    // Comment forcing unwanted break.\n"
6775             "    aaaa(aaaa) {}",
6776             format("Constructor() :\n"
6777                    "    // Comment forcing unwanted break.\n"
6778                    "    aaaa(aaaa) {}",
6779                    Style));
6780 
6781   Style.ColumnLimit = 0;
6782   verifyFormat("SomeClass::Constructor() :\n"
6783                "    a(a) {}",
6784                Style);
6785   verifyFormat("SomeClass::Constructor() noexcept :\n"
6786                "    a(a) {}",
6787                Style);
6788   verifyFormat("SomeClass::Constructor() :\n"
6789                "    a(a), b(b), c(c) {}",
6790                Style);
6791   verifyFormat("SomeClass::Constructor() :\n"
6792                "    a(a) {\n"
6793                "  foo();\n"
6794                "  bar();\n"
6795                "}",
6796                Style);
6797 
6798   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6799   verifyFormat("SomeClass::Constructor() :\n"
6800                "    a(a), b(b), c(c) {\n"
6801                "}",
6802                Style);
6803   verifyFormat("SomeClass::Constructor() :\n"
6804                "    a(a) {\n"
6805                "}",
6806                Style);
6807 
6808   Style.ColumnLimit = 80;
6809   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
6810   Style.ConstructorInitializerIndentWidth = 2;
6811   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style);
6812   verifyFormat("SomeClass::Constructor() :\n"
6813                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6814                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
6815                Style);
6816 
6817   // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
6818   // well
6819   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
6820   verifyFormat(
6821       "class SomeClass\n"
6822       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6823       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6824       Style);
6825   Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
6826   verifyFormat(
6827       "class SomeClass\n"
6828       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6829       "  , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6830       Style);
6831   Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
6832   verifyFormat(
6833       "class SomeClass :\n"
6834       "  public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6835       "  public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6836       Style);
6837   Style.BreakInheritanceList = FormatStyle::BILS_AfterComma;
6838   verifyFormat(
6839       "class SomeClass\n"
6840       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6841       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
6842       Style);
6843 }
6844 
6845 #ifndef EXPENSIVE_CHECKS
6846 // Expensive checks enables libstdc++ checking which includes validating the
6847 // state of ranges used in std::priority_queue - this blows out the
6848 // runtime/scalability of the function and makes this test unacceptably slow.
6849 TEST_F(FormatTest, MemoizationTests) {
6850   // This breaks if the memoization lookup does not take \c Indent and
6851   // \c LastSpace into account.
6852   verifyFormat(
6853       "extern CFRunLoopTimerRef\n"
6854       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
6855       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
6856       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
6857       "                     CFRunLoopTimerContext *context) {}");
6858 
6859   // Deep nesting somewhat works around our memoization.
6860   verifyFormat(
6861       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6862       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6863       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6864       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
6865       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
6866       getLLVMStyleWithColumns(65));
6867   verifyFormat(
6868       "aaaaa(\n"
6869       "    aaaaa,\n"
6870       "    aaaaa(\n"
6871       "        aaaaa,\n"
6872       "        aaaaa(\n"
6873       "            aaaaa,\n"
6874       "            aaaaa(\n"
6875       "                aaaaa,\n"
6876       "                aaaaa(\n"
6877       "                    aaaaa,\n"
6878       "                    aaaaa(\n"
6879       "                        aaaaa,\n"
6880       "                        aaaaa(\n"
6881       "                            aaaaa,\n"
6882       "                            aaaaa(\n"
6883       "                                aaaaa,\n"
6884       "                                aaaaa(\n"
6885       "                                    aaaaa,\n"
6886       "                                    aaaaa(\n"
6887       "                                        aaaaa,\n"
6888       "                                        aaaaa(\n"
6889       "                                            aaaaa,\n"
6890       "                                            aaaaa(\n"
6891       "                                                aaaaa,\n"
6892       "                                                aaaaa))))))))))));",
6893       getLLVMStyleWithColumns(65));
6894   verifyFormat(
6895       "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"
6896       "                                  a),\n"
6897       "                                a),\n"
6898       "                              a),\n"
6899       "                            a),\n"
6900       "                          a),\n"
6901       "                        a),\n"
6902       "                      a),\n"
6903       "                    a),\n"
6904       "                  a),\n"
6905       "                a),\n"
6906       "              a),\n"
6907       "            a),\n"
6908       "          a),\n"
6909       "        a),\n"
6910       "      a),\n"
6911       "    a),\n"
6912       "  a)",
6913       getLLVMStyleWithColumns(65));
6914 
6915   // This test takes VERY long when memoization is broken.
6916   FormatStyle OnePerLine = getLLVMStyle();
6917   OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
6918   OnePerLine.BinPackParameters = false;
6919   std::string input = "Constructor()\n"
6920                       "    : aaaa(a,\n";
6921   for (unsigned i = 0, e = 80; i != e; ++i) {
6922     input += "           a,\n";
6923   }
6924   input += "           a) {}";
6925   verifyFormat(input, OnePerLine);
6926 }
6927 #endif
6928 
6929 TEST_F(FormatTest, BreaksAsHighAsPossible) {
6930   verifyFormat(
6931       "void f() {\n"
6932       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
6933       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
6934       "    f();\n"
6935       "}");
6936   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
6937                "    Intervals[i - 1].getRange().getLast()) {\n}");
6938 }
6939 
6940 TEST_F(FormatTest, BreaksFunctionDeclarations) {
6941   // Principially, we break function declarations in a certain order:
6942   // 1) break amongst arguments.
6943   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
6944                "                              Cccccccccccccc cccccccccccccc);");
6945   verifyFormat("template <class TemplateIt>\n"
6946                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
6947                "                            TemplateIt *stop) {}");
6948 
6949   // 2) break after return type.
6950   verifyFormat(
6951       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6952       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
6953       getGoogleStyle());
6954 
6955   // 3) break after (.
6956   verifyFormat(
6957       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
6958       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
6959       getGoogleStyle());
6960 
6961   // 4) break before after nested name specifiers.
6962   verifyFormat(
6963       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6964       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
6965       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
6966       getGoogleStyle());
6967 
6968   // However, there are exceptions, if a sufficient amount of lines can be
6969   // saved.
6970   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
6971   // more adjusting.
6972   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
6973                "                                  Cccccccccccccc cccccccccc,\n"
6974                "                                  Cccccccccccccc cccccccccc,\n"
6975                "                                  Cccccccccccccc cccccccccc,\n"
6976                "                                  Cccccccccccccc cccccccccc);");
6977   verifyFormat(
6978       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6979       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6980       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6981       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
6982       getGoogleStyle());
6983   verifyFormat(
6984       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
6985       "                                          Cccccccccccccc cccccccccc,\n"
6986       "                                          Cccccccccccccc cccccccccc,\n"
6987       "                                          Cccccccccccccc cccccccccc,\n"
6988       "                                          Cccccccccccccc cccccccccc,\n"
6989       "                                          Cccccccccccccc cccccccccc,\n"
6990       "                                          Cccccccccccccc cccccccccc);");
6991   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6992                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6993                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6994                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
6995                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
6996 
6997   // Break after multi-line parameters.
6998   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6999                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7000                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7001                "    bbbb bbbb);");
7002   verifyFormat("void SomeLoooooooooooongFunction(\n"
7003                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
7004                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7005                "    int bbbbbbbbbbbbb);");
7006 
7007   // Treat overloaded operators like other functions.
7008   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
7009                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
7010   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
7011                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
7012   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
7013                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
7014   verifyGoogleFormat(
7015       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
7016       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
7017   verifyGoogleFormat(
7018       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
7019       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
7020   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7021                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
7022   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
7023                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
7024   verifyGoogleFormat(
7025       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
7026       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7027       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
7028   verifyGoogleFormat("template <typename T>\n"
7029                      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7030                      "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
7031                      "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
7032 
7033   FormatStyle Style = getLLVMStyle();
7034   Style.PointerAlignment = FormatStyle::PAS_Left;
7035   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7036                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
7037                Style);
7038   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
7039                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7040                Style);
7041 }
7042 
7043 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
7044   // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
7045   // Prefer keeping `::` followed by `operator` together.
7046   EXPECT_EQ("const aaaa::bbbbbbb &\n"
7047             "ccccccccc::operator++() {\n"
7048             "  stuff();\n"
7049             "}",
7050             format("const aaaa::bbbbbbb\n"
7051                    "&ccccccccc::operator++() { stuff(); }",
7052                    getLLVMStyleWithColumns(40)));
7053 }
7054 
7055 TEST_F(FormatTest, TrailingReturnType) {
7056   verifyFormat("auto foo() -> int;\n");
7057   // correct trailing return type spacing
7058   verifyFormat("auto operator->() -> int;\n");
7059   verifyFormat("auto operator++(int) -> int;\n");
7060 
7061   verifyFormat("struct S {\n"
7062                "  auto bar() const -> int;\n"
7063                "};");
7064   verifyFormat("template <size_t Order, typename T>\n"
7065                "auto load_img(const std::string &filename)\n"
7066                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
7067   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
7068                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
7069   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
7070   verifyFormat("template <typename T>\n"
7071                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
7072                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
7073 
7074   // Not trailing return types.
7075   verifyFormat("void f() { auto a = b->c(); }");
7076   verifyFormat("auto a = p->foo();");
7077   verifyFormat("int a = p->foo();");
7078   verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };");
7079 }
7080 
7081 TEST_F(FormatTest, DeductionGuides) {
7082   verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
7083   verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
7084   verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
7085   verifyFormat(
7086       "template <class... T>\n"
7087       "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
7088   verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
7089   verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
7090   verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
7091   verifyFormat("template <class T> A() -> A<(3 < 2)>;");
7092   verifyFormat("template <class T> A() -> A<((3) < (2))>;");
7093   verifyFormat("template <class T> x() -> x<1>;");
7094   verifyFormat("template <class T> explicit x(T &) -> x<1>;");
7095 
7096   // Ensure not deduction guides.
7097   verifyFormat("c()->f<int>();");
7098   verifyFormat("x()->foo<1>;");
7099   verifyFormat("x = p->foo<3>();");
7100   verifyFormat("x()->x<1>();");
7101   verifyFormat("x()->x<1>;");
7102 }
7103 
7104 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
7105   // Avoid breaking before trailing 'const' or other trailing annotations, if
7106   // they are not function-like.
7107   FormatStyle Style = getGoogleStyleWithColumns(47);
7108   verifyFormat("void someLongFunction(\n"
7109                "    int someLoooooooooooooongParameter) const {\n}",
7110                getLLVMStyleWithColumns(47));
7111   verifyFormat("LoooooongReturnType\n"
7112                "someLoooooooongFunction() const {}",
7113                getLLVMStyleWithColumns(47));
7114   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
7115                "    const {}",
7116                Style);
7117   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
7118                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
7119   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
7120                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
7121   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
7122                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
7123   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
7124                "                   aaaaaaaaaaa aaaaa) const override;");
7125   verifyGoogleFormat(
7126       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7127       "    const override;");
7128 
7129   // Even if the first parameter has to be wrapped.
7130   verifyFormat("void someLongFunction(\n"
7131                "    int someLongParameter) const {}",
7132                getLLVMStyleWithColumns(46));
7133   verifyFormat("void someLongFunction(\n"
7134                "    int someLongParameter) const {}",
7135                Style);
7136   verifyFormat("void someLongFunction(\n"
7137                "    int someLongParameter) override {}",
7138                Style);
7139   verifyFormat("void someLongFunction(\n"
7140                "    int someLongParameter) OVERRIDE {}",
7141                Style);
7142   verifyFormat("void someLongFunction(\n"
7143                "    int someLongParameter) final {}",
7144                Style);
7145   verifyFormat("void someLongFunction(\n"
7146                "    int someLongParameter) FINAL {}",
7147                Style);
7148   verifyFormat("void someLongFunction(\n"
7149                "    int parameter) const override {}",
7150                Style);
7151 
7152   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
7153   verifyFormat("void someLongFunction(\n"
7154                "    int someLongParameter) const\n"
7155                "{\n"
7156                "}",
7157                Style);
7158 
7159   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
7160   verifyFormat("void someLongFunction(\n"
7161                "    int someLongParameter) const\n"
7162                "  {\n"
7163                "  }",
7164                Style);
7165 
7166   // Unless these are unknown annotations.
7167   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
7168                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7169                "    LONG_AND_UGLY_ANNOTATION;");
7170 
7171   // Breaking before function-like trailing annotations is fine to keep them
7172   // close to their arguments.
7173   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7174                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
7175   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
7176                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
7177   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
7178                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
7179   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
7180                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
7181   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
7182 
7183   verifyFormat(
7184       "void aaaaaaaaaaaaaaaaaa()\n"
7185       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
7186       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
7187   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7188                "    __attribute__((unused));");
7189   verifyGoogleFormat(
7190       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7191       "    GUARDED_BY(aaaaaaaaaaaa);");
7192   verifyGoogleFormat(
7193       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7194       "    GUARDED_BY(aaaaaaaaaaaa);");
7195   verifyGoogleFormat(
7196       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
7197       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7198   verifyGoogleFormat(
7199       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
7200       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
7201 }
7202 
7203 TEST_F(FormatTest, FunctionAnnotations) {
7204   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
7205                "int OldFunction(const string &parameter) {}");
7206   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
7207                "string OldFunction(const string &parameter) {}");
7208   verifyFormat("template <typename T>\n"
7209                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
7210                "string OldFunction(const string &parameter) {}");
7211 
7212   // Not function annotations.
7213   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7214                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
7215   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
7216                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
7217   verifyFormat("MACRO(abc).function() // wrap\n"
7218                "    << abc;");
7219   verifyFormat("MACRO(abc)->function() // wrap\n"
7220                "    << abc;");
7221   verifyFormat("MACRO(abc)::function() // wrap\n"
7222                "    << abc;");
7223 }
7224 
7225 TEST_F(FormatTest, BreaksDesireably) {
7226   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
7227                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
7228                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
7229   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7230                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
7231                "}");
7232 
7233   verifyFormat(
7234       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7235       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7236 
7237   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7238                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7239                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
7240 
7241   verifyFormat(
7242       "aaaaaaaa(aaaaaaaaaaaaa,\n"
7243       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7244       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
7245       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7246       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
7247 
7248   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7249                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7250 
7251   verifyFormat(
7252       "void f() {\n"
7253       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
7254       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
7255       "}");
7256   verifyFormat(
7257       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7258       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
7259   verifyFormat(
7260       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7261       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
7262   verifyFormat(
7263       "aaaaaa(aaa,\n"
7264       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7265       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7266       "       aaaa);");
7267   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7268                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7269                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7270 
7271   // Indent consistently independent of call expression and unary operator.
7272   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
7273                "    dddddddddddddddddddddddddddddd));");
7274   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
7275                "    dddddddddddddddddddddddddddddd));");
7276   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
7277                "    dddddddddddddddddddddddddddddd));");
7278 
7279   // This test case breaks on an incorrect memoization, i.e. an optimization not
7280   // taking into account the StopAt value.
7281   verifyFormat(
7282       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
7283       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
7284       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
7285       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7286 
7287   verifyFormat("{\n  {\n    {\n"
7288                "      Annotation.SpaceRequiredBefore =\n"
7289                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
7290                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
7291                "    }\n  }\n}");
7292 
7293   // Break on an outer level if there was a break on an inner level.
7294   EXPECT_EQ("f(g(h(a, // comment\n"
7295             "      b, c),\n"
7296             "    d, e),\n"
7297             "  x, y);",
7298             format("f(g(h(a, // comment\n"
7299                    "    b, c), d, e), x, y);"));
7300 
7301   // Prefer breaking similar line breaks.
7302   verifyFormat(
7303       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
7304       "                             NSTrackingMouseEnteredAndExited |\n"
7305       "                             NSTrackingActiveAlways;");
7306 }
7307 
7308 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
7309   FormatStyle NoBinPacking = getGoogleStyle();
7310   NoBinPacking.BinPackParameters = false;
7311   NoBinPacking.BinPackArguments = true;
7312   verifyFormat("void f() {\n"
7313                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
7314                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
7315                "}",
7316                NoBinPacking);
7317   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
7318                "       int aaaaaaaaaaaaaaaaaaaa,\n"
7319                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7320                NoBinPacking);
7321 
7322   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
7323   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7324                "                        vector<int> bbbbbbbbbbbbbbb);",
7325                NoBinPacking);
7326   // FIXME: This behavior difference is probably not wanted. However, currently
7327   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
7328   // template arguments from BreakBeforeParameter being set because of the
7329   // one-per-line formatting.
7330   verifyFormat(
7331       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
7332       "                                             aaaaaaaaaa> aaaaaaaaaa);",
7333       NoBinPacking);
7334   verifyFormat(
7335       "void fffffffffff(\n"
7336       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
7337       "        aaaaaaaaaa);");
7338 }
7339 
7340 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
7341   FormatStyle NoBinPacking = getGoogleStyle();
7342   NoBinPacking.BinPackParameters = false;
7343   NoBinPacking.BinPackArguments = false;
7344   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
7345                "  aaaaaaaaaaaaaaaaaaaa,\n"
7346                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
7347                NoBinPacking);
7348   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
7349                "        aaaaaaaaaaaaa,\n"
7350                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
7351                NoBinPacking);
7352   verifyFormat(
7353       "aaaaaaaa(aaaaaaaaaaaaa,\n"
7354       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7355       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
7356       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7357       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
7358       NoBinPacking);
7359   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
7360                "    .aaaaaaaaaaaaaaaaaa();",
7361                NoBinPacking);
7362   verifyFormat("void f() {\n"
7363                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7364                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
7365                "}",
7366                NoBinPacking);
7367 
7368   verifyFormat(
7369       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7370       "             aaaaaaaaaaaa,\n"
7371       "             aaaaaaaaaaaa);",
7372       NoBinPacking);
7373   verifyFormat(
7374       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
7375       "                               ddddddddddddddddddddddddddddd),\n"
7376       "             test);",
7377       NoBinPacking);
7378 
7379   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
7380                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
7381                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
7382                "    aaaaaaaaaaaaaaaaaa;",
7383                NoBinPacking);
7384   verifyFormat("a(\"a\"\n"
7385                "  \"a\",\n"
7386                "  a);");
7387 
7388   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
7389   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
7390                "                aaaaaaaaa,\n"
7391                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7392                NoBinPacking);
7393   verifyFormat(
7394       "void f() {\n"
7395       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
7396       "      .aaaaaaa();\n"
7397       "}",
7398       NoBinPacking);
7399   verifyFormat(
7400       "template <class SomeType, class SomeOtherType>\n"
7401       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
7402       NoBinPacking);
7403 }
7404 
7405 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
7406   FormatStyle Style = getLLVMStyleWithColumns(15);
7407   Style.ExperimentalAutoDetectBinPacking = true;
7408   EXPECT_EQ("aaa(aaaa,\n"
7409             "    aaaa,\n"
7410             "    aaaa);\n"
7411             "aaa(aaaa,\n"
7412             "    aaaa,\n"
7413             "    aaaa);",
7414             format("aaa(aaaa,\n" // one-per-line
7415                    "  aaaa,\n"
7416                    "    aaaa  );\n"
7417                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
7418                    Style));
7419   EXPECT_EQ("aaa(aaaa, aaaa,\n"
7420             "    aaaa);\n"
7421             "aaa(aaaa, aaaa,\n"
7422             "    aaaa);",
7423             format("aaa(aaaa,  aaaa,\n" // bin-packed
7424                    "    aaaa  );\n"
7425                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
7426                    Style));
7427 }
7428 
7429 TEST_F(FormatTest, FormatsBuilderPattern) {
7430   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
7431                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
7432                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
7433                "    .StartsWith(\".init\", ORDER_INIT)\n"
7434                "    .StartsWith(\".fini\", ORDER_FINI)\n"
7435                "    .StartsWith(\".hash\", ORDER_HASH)\n"
7436                "    .Default(ORDER_TEXT);\n");
7437 
7438   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
7439                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
7440   verifyFormat("aaaaaaa->aaaaaaa\n"
7441                "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7442                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7443                "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
7444   verifyFormat(
7445       "aaaaaaa->aaaaaaa\n"
7446       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7447       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
7448   verifyFormat(
7449       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
7450       "    aaaaaaaaaaaaaa);");
7451   verifyFormat(
7452       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
7453       "    aaaaaa->aaaaaaaaaaaa()\n"
7454       "        ->aaaaaaaaaaaaaaaa(\n"
7455       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7456       "        ->aaaaaaaaaaaaaaaaa();");
7457   verifyGoogleFormat(
7458       "void f() {\n"
7459       "  someo->Add((new util::filetools::Handler(dir))\n"
7460       "                 ->OnEvent1(NewPermanentCallback(\n"
7461       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
7462       "                 ->OnEvent2(NewPermanentCallback(\n"
7463       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
7464       "                 ->OnEvent3(NewPermanentCallback(\n"
7465       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
7466       "                 ->OnEvent5(NewPermanentCallback(\n"
7467       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
7468       "                 ->OnEvent6(NewPermanentCallback(\n"
7469       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
7470       "}");
7471 
7472   verifyFormat(
7473       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
7474   verifyFormat("aaaaaaaaaaaaaaa()\n"
7475                "    .aaaaaaaaaaaaaaa()\n"
7476                "    .aaaaaaaaaaaaaaa()\n"
7477                "    .aaaaaaaaaaaaaaa()\n"
7478                "    .aaaaaaaaaaaaaaa();");
7479   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7480                "    .aaaaaaaaaaaaaaa()\n"
7481                "    .aaaaaaaaaaaaaaa()\n"
7482                "    .aaaaaaaaaaaaaaa();");
7483   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7484                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
7485                "    .aaaaaaaaaaaaaaa();");
7486   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
7487                "    ->aaaaaaaaaaaaaae(0)\n"
7488                "    ->aaaaaaaaaaaaaaa();");
7489 
7490   // Don't linewrap after very short segments.
7491   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7492                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7493                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7494   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7495                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7496                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7497   verifyFormat("aaa()\n"
7498                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7499                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7500                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7501 
7502   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
7503                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7504                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
7505   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
7506                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
7507                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
7508 
7509   // Prefer not to break after empty parentheses.
7510   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
7511                "    First->LastNewlineOffset);");
7512 
7513   // Prefer not to create "hanging" indents.
7514   verifyFormat(
7515       "return !soooooooooooooome_map\n"
7516       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7517       "            .second;");
7518   verifyFormat(
7519       "return aaaaaaaaaaaaaaaa\n"
7520       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
7521       "    .aaaa(aaaaaaaaaaaaaa);");
7522   // No hanging indent here.
7523   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
7524                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7525   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
7526                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7527   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
7528                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7529                getLLVMStyleWithColumns(60));
7530   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
7531                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
7532                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7533                getLLVMStyleWithColumns(59));
7534   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7535                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7536                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7537 
7538   // Dont break if only closing statements before member call
7539   verifyFormat("test() {\n"
7540                "  ([]() -> {\n"
7541                "    int b = 32;\n"
7542                "    return 3;\n"
7543                "  }).foo();\n"
7544                "}");
7545   verifyFormat("test() {\n"
7546                "  (\n"
7547                "      []() -> {\n"
7548                "        int b = 32;\n"
7549                "        return 3;\n"
7550                "      },\n"
7551                "      foo, bar)\n"
7552                "      .foo();\n"
7553                "}");
7554   verifyFormat("test() {\n"
7555                "  ([]() -> {\n"
7556                "    int b = 32;\n"
7557                "    return 3;\n"
7558                "  })\n"
7559                "      .foo()\n"
7560                "      .bar();\n"
7561                "}");
7562   verifyFormat("test() {\n"
7563                "  ([]() -> {\n"
7564                "    int b = 32;\n"
7565                "    return 3;\n"
7566                "  })\n"
7567                "      .foo(\"aaaaaaaaaaaaaaaaa\"\n"
7568                "           \"bbbb\");\n"
7569                "}",
7570                getLLVMStyleWithColumns(30));
7571 }
7572 
7573 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
7574   verifyFormat(
7575       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7576       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
7577   verifyFormat(
7578       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
7579       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
7580 
7581   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7582                "    ccccccccccccccccccccccccc) {\n}");
7583   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
7584                "    ccccccccccccccccccccccccc) {\n}");
7585 
7586   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
7587                "    ccccccccccccccccccccccccc) {\n}");
7588   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
7589                "    ccccccccccccccccccccccccc) {\n}");
7590 
7591   verifyFormat(
7592       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
7593       "    ccccccccccccccccccccccccc) {\n}");
7594   verifyFormat(
7595       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
7596       "    ccccccccccccccccccccccccc) {\n}");
7597 
7598   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
7599                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
7600                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
7601                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7602   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
7603                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
7604                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
7605                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
7606 
7607   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
7608                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
7609                "    aaaaaaaaaaaaaaa != aa) {\n}");
7610   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
7611                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
7612                "    aaaaaaaaaaaaaaa != aa) {\n}");
7613 }
7614 
7615 TEST_F(FormatTest, BreaksAfterAssignments) {
7616   verifyFormat(
7617       "unsigned Cost =\n"
7618       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
7619       "                        SI->getPointerAddressSpaceee());\n");
7620   verifyFormat(
7621       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
7622       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
7623 
7624   verifyFormat(
7625       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
7626       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
7627   verifyFormat("unsigned OriginalStartColumn =\n"
7628                "    SourceMgr.getSpellingColumnNumber(\n"
7629                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
7630                "    1;");
7631 }
7632 
7633 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
7634   FormatStyle Style = getLLVMStyle();
7635   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7636                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
7637                Style);
7638 
7639   Style.PenaltyBreakAssignment = 20;
7640   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
7641                "                                 cccccccccccccccccccccccccc;",
7642                Style);
7643 }
7644 
7645 TEST_F(FormatTest, AlignsAfterAssignments) {
7646   verifyFormat(
7647       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7648       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
7649   verifyFormat(
7650       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7651       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
7652   verifyFormat(
7653       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7654       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
7655   verifyFormat(
7656       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7657       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
7658   verifyFormat(
7659       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7660       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
7661       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
7662 }
7663 
7664 TEST_F(FormatTest, AlignsAfterReturn) {
7665   verifyFormat(
7666       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7667       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
7668   verifyFormat(
7669       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7670       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
7671   verifyFormat(
7672       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7673       "       aaaaaaaaaaaaaaaaaaaaaa();");
7674   verifyFormat(
7675       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
7676       "        aaaaaaaaaaaaaaaaaaaaaa());");
7677   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7678                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7679   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7680                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
7681                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7682   verifyFormat("return\n"
7683                "    // true if code is one of a or b.\n"
7684                "    code == a || code == b;");
7685 }
7686 
7687 TEST_F(FormatTest, AlignsAfterOpenBracket) {
7688   verifyFormat(
7689       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7690       "                                                aaaaaaaaa aaaaaaa) {}");
7691   verifyFormat(
7692       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7693       "                                               aaaaaaaaaaa aaaaaaaaa);");
7694   verifyFormat(
7695       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7696       "                                             aaaaaaaaaaaaaaaaaaaaa));");
7697   FormatStyle Style = getLLVMStyle();
7698   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7699   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7700                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
7701                Style);
7702   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7703                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
7704                Style);
7705   verifyFormat("SomeLongVariableName->someFunction(\n"
7706                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
7707                Style);
7708   verifyFormat(
7709       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
7710       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7711       Style);
7712   verifyFormat(
7713       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
7714       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7715       Style);
7716   verifyFormat(
7717       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
7718       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7719       Style);
7720 
7721   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
7722                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
7723                "        b));",
7724                Style);
7725 
7726   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7727   Style.BinPackArguments = false;
7728   Style.BinPackParameters = false;
7729   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7730                "    aaaaaaaaaaa aaaaaaaa,\n"
7731                "    aaaaaaaaa aaaaaaa,\n"
7732                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7733                Style);
7734   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
7735                "    aaaaaaaaaaa aaaaaaaaa,\n"
7736                "    aaaaaaaaaaa aaaaaaaaa,\n"
7737                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7738                Style);
7739   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
7740                "    aaaaaaaaaaaaaaa,\n"
7741                "    aaaaaaaaaaaaaaaaaaaaa,\n"
7742                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
7743                Style);
7744   verifyFormat(
7745       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
7746       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7747       Style);
7748   verifyFormat(
7749       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
7750       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
7751       Style);
7752   verifyFormat(
7753       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7754       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7755       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
7756       "    aaaaaaaaaaaaaaaa);",
7757       Style);
7758   verifyFormat(
7759       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7760       "    aaaaaaaaaaaaaaaaaaaaa(\n"
7761       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
7762       "    aaaaaaaaaaaaaaaa);",
7763       Style);
7764 }
7765 
7766 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
7767   FormatStyle Style = getLLVMStyleWithColumns(40);
7768   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7769                "          bbbbbbbbbbbbbbbbbbbbbb);",
7770                Style);
7771   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
7772   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7773   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7774                "          bbbbbbbbbbbbbbbbbbbbbb);",
7775                Style);
7776   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7777   Style.AlignOperands = FormatStyle::OAS_Align;
7778   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7779                "          bbbbbbbbbbbbbbbbbbbbbb);",
7780                Style);
7781   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7782   Style.AlignOperands = FormatStyle::OAS_DontAlign;
7783   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
7784                "    bbbbbbbbbbbbbbbbbbbbbb);",
7785                Style);
7786 }
7787 
7788 TEST_F(FormatTest, BreaksConditionalExpressions) {
7789   verifyFormat(
7790       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7791       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7792       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7793   verifyFormat(
7794       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
7795       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7796       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7797   verifyFormat(
7798       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7799       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7800   verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
7801                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7802                "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7803   verifyFormat(
7804       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
7805       "                                                    : aaaaaaaaaaaaa);");
7806   verifyFormat(
7807       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7808       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7809       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7810       "                   aaaaaaaaaaaaa);");
7811   verifyFormat(
7812       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7813       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7814       "                   aaaaaaaaaaaaa);");
7815   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7816                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7817                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7818                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7819                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7820   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7821                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7822                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7823                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7824                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7825                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7826                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7827   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7828                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7829                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7830                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7831                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7832   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7833                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7834                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7835   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
7836                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7837                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7838                "        : aaaaaaaaaaaaaaaa;");
7839   verifyFormat(
7840       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7841       "    ? aaaaaaaaaaaaaaa\n"
7842       "    : aaaaaaaaaaaaaaa;");
7843   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
7844                "          aaaaaaaaa\n"
7845                "      ? b\n"
7846                "      : c);");
7847   verifyFormat("return aaaa == bbbb\n"
7848                "           // comment\n"
7849                "           ? aaaa\n"
7850                "           : bbbb;");
7851   verifyFormat("unsigned Indent =\n"
7852                "    format(TheLine.First,\n"
7853                "           IndentForLevel[TheLine.Level] >= 0\n"
7854                "               ? IndentForLevel[TheLine.Level]\n"
7855                "               : TheLine * 2,\n"
7856                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
7857                getLLVMStyleWithColumns(60));
7858   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7859                "                  ? aaaaaaaaaaaaaaa\n"
7860                "                  : bbbbbbbbbbbbbbb //\n"
7861                "                        ? ccccccccccccccc\n"
7862                "                        : ddddddddddddddd;");
7863   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
7864                "                  ? aaaaaaaaaaaaaaa\n"
7865                "                  : (bbbbbbbbbbbbbbb //\n"
7866                "                         ? ccccccccccccccc\n"
7867                "                         : ddddddddddddddd);");
7868   verifyFormat(
7869       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7870       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7871       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
7872       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
7873       "                                      : aaaaaaaaaa;");
7874   verifyFormat(
7875       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7876       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
7877       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7878 
7879   FormatStyle NoBinPacking = getLLVMStyle();
7880   NoBinPacking.BinPackArguments = false;
7881   verifyFormat(
7882       "void f() {\n"
7883       "  g(aaa,\n"
7884       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
7885       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7886       "        ? aaaaaaaaaaaaaaa\n"
7887       "        : aaaaaaaaaaaaaaa);\n"
7888       "}",
7889       NoBinPacking);
7890   verifyFormat(
7891       "void f() {\n"
7892       "  g(aaa,\n"
7893       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
7894       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7895       "        ?: aaaaaaaaaaaaaaa);\n"
7896       "}",
7897       NoBinPacking);
7898 
7899   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
7900                "             // comment.\n"
7901                "             ccccccccccccccccccccccccccccccccccccccc\n"
7902                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7903                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
7904 
7905   // Assignments in conditional expressions. Apparently not uncommon :-(.
7906   verifyFormat("return a != b\n"
7907                "           // comment\n"
7908                "           ? a = b\n"
7909                "           : a = b;");
7910   verifyFormat("return a != b\n"
7911                "           // comment\n"
7912                "           ? a = a != b\n"
7913                "                     // comment\n"
7914                "                     ? a = b\n"
7915                "                     : a\n"
7916                "           : a;\n");
7917   verifyFormat("return a != b\n"
7918                "           // comment\n"
7919                "           ? a\n"
7920                "           : a = a != b\n"
7921                "                     // comment\n"
7922                "                     ? a = b\n"
7923                "                     : a;");
7924 
7925   // Chained conditionals
7926   FormatStyle Style = getLLVMStyleWithColumns(70);
7927   Style.AlignOperands = FormatStyle::OAS_Align;
7928   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7929                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7930                "                        : 3333333333333333;",
7931                Style);
7932   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7933                "       : bbbbbbbbbb     ? 2222222222222222\n"
7934                "                        : 3333333333333333;",
7935                Style);
7936   verifyFormat("return aaaaaaaaaa         ? 1111111111111111\n"
7937                "       : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7938                "                          : 3333333333333333;",
7939                Style);
7940   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7941                "       : bbbbbbbbbbbbbb ? 222222\n"
7942                "                        : 333333;",
7943                Style);
7944   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7945                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7946                "       : cccccccccccccc ? 3333333333333333\n"
7947                "                        : 4444444444444444;",
7948                Style);
7949   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
7950                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7951                "                        : 3333333333333333;",
7952                Style);
7953   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7954                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7955                "                        : (aaa ? bbb : ccc);",
7956                Style);
7957   verifyFormat(
7958       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7959       "                                             : cccccccccccccccccc)\n"
7960       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7961       "                        : 3333333333333333;",
7962       Style);
7963   verifyFormat(
7964       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7965       "                                             : cccccccccccccccccc)\n"
7966       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7967       "                        : 3333333333333333;",
7968       Style);
7969   verifyFormat(
7970       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7971       "                                             : dddddddddddddddddd)\n"
7972       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7973       "                        : 3333333333333333;",
7974       Style);
7975   verifyFormat(
7976       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7977       "                                             : dddddddddddddddddd)\n"
7978       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7979       "                        : 3333333333333333;",
7980       Style);
7981   verifyFormat(
7982       "return aaaaaaaaa        ? 1111111111111111\n"
7983       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7984       "                        : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7985       "                                             : dddddddddddddddddd)\n",
7986       Style);
7987   verifyFormat(
7988       "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7989       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7990       "                        : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7991       "                                             : cccccccccccccccccc);",
7992       Style);
7993   verifyFormat(
7994       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7995       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
7996       "                                             : eeeeeeeeeeeeeeeeee)\n"
7997       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
7998       "                        : 3333333333333333;",
7999       Style);
8000   verifyFormat(
8001       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
8002       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
8003       "                                             : eeeeeeeeeeeeeeeeee)\n"
8004       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8005       "                        : 3333333333333333;",
8006       Style);
8007   verifyFormat(
8008       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8009       "                           : cccccccccccc    ? dddddddddddddddddd\n"
8010       "                                             : eeeeeeeeeeeeeeeeee)\n"
8011       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8012       "                        : 3333333333333333;",
8013       Style);
8014   verifyFormat(
8015       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8016       "                                             : cccccccccccccccccc\n"
8017       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8018       "                        : 3333333333333333;",
8019       Style);
8020   verifyFormat(
8021       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8022       "                          : cccccccccccccccc ? dddddddddddddddddd\n"
8023       "                                             : eeeeeeeeeeeeeeeeee\n"
8024       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
8025       "                        : 3333333333333333;",
8026       Style);
8027   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
8028                "           ? (aaaaaaaaaaaaaaaaaa   ? bbbbbbbbbbbbbbbbbb\n"
8029                "              : cccccccccccccccccc ? dddddddddddddddddd\n"
8030                "                                   : eeeeeeeeeeeeeeeeee)\n"
8031                "       : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
8032                "                             : 3333333333333333;",
8033                Style);
8034   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
8035                "           ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
8036                "             : cccccccccccccccc ? dddddddddddddddddd\n"
8037                "                                : eeeeeeeeeeeeeeeeee\n"
8038                "       : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
8039                "                                 : 3333333333333333;",
8040                Style);
8041 
8042   Style.AlignOperands = FormatStyle::OAS_DontAlign;
8043   Style.BreakBeforeTernaryOperators = false;
8044   // FIXME: Aligning the question marks is weird given DontAlign.
8045   // Consider disabling this alignment in this case. Also check whether this
8046   // will render the adjustment from https://reviews.llvm.org/D82199
8047   // unnecessary.
8048   verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
8049                "    bbbb                ? cccccccccccccccccc :\n"
8050                "                          ddddd;\n",
8051                Style);
8052 
8053   EXPECT_EQ(
8054       "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
8055       "    /*\n"
8056       "     */\n"
8057       "    function() {\n"
8058       "      try {\n"
8059       "        return JJJJJJJJJJJJJJ(\n"
8060       "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
8061       "      }\n"
8062       "    } :\n"
8063       "    function() {};",
8064       format(
8065           "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
8066           "     /*\n"
8067           "      */\n"
8068           "     function() {\n"
8069           "      try {\n"
8070           "        return JJJJJJJJJJJJJJ(\n"
8071           "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
8072           "      }\n"
8073           "    } :\n"
8074           "    function() {};",
8075           getGoogleStyle(FormatStyle::LK_JavaScript)));
8076 }
8077 
8078 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
8079   FormatStyle Style = getLLVMStyleWithColumns(70);
8080   Style.BreakBeforeTernaryOperators = false;
8081   verifyFormat(
8082       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8083       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8084       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8085       Style);
8086   verifyFormat(
8087       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
8088       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8089       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8090       Style);
8091   verifyFormat(
8092       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8093       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8094       Style);
8095   verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
8096                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8097                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8098                Style);
8099   verifyFormat(
8100       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
8101       "                                                      aaaaaaaaaaaaa);",
8102       Style);
8103   verifyFormat(
8104       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8105       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8106       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8107       "                   aaaaaaaaaaaaa);",
8108       Style);
8109   verifyFormat(
8110       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8111       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8112       "                   aaaaaaaaaaaaa);",
8113       Style);
8114   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8115                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8116                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
8117                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8118                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8119                Style);
8120   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8121                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8122                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8123                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
8124                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8125                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8126                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8127                Style);
8128   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8129                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
8130                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8131                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8132                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8133                Style);
8134   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8135                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8136                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8137                Style);
8138   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
8139                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8140                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
8141                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8142                Style);
8143   verifyFormat(
8144       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8145       "    aaaaaaaaaaaaaaa :\n"
8146       "    aaaaaaaaaaaaaaa;",
8147       Style);
8148   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
8149                "          aaaaaaaaa ?\n"
8150                "      b :\n"
8151                "      c);",
8152                Style);
8153   verifyFormat("unsigned Indent =\n"
8154                "    format(TheLine.First,\n"
8155                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
8156                "               IndentForLevel[TheLine.Level] :\n"
8157                "               TheLine * 2,\n"
8158                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
8159                Style);
8160   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
8161                "                  aaaaaaaaaaaaaaa :\n"
8162                "                  bbbbbbbbbbbbbbb ? //\n"
8163                "                      ccccccccccccccc :\n"
8164                "                      ddddddddddddddd;",
8165                Style);
8166   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
8167                "                  aaaaaaaaaaaaaaa :\n"
8168                "                  (bbbbbbbbbbbbbbb ? //\n"
8169                "                       ccccccccccccccc :\n"
8170                "                       ddddddddddddddd);",
8171                Style);
8172   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8173                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
8174                "            ccccccccccccccccccccccccccc;",
8175                Style);
8176   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
8177                "           aaaaa :\n"
8178                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
8179                Style);
8180 
8181   // Chained conditionals
8182   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8183                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8184                "                          3333333333333333;",
8185                Style);
8186   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8187                "       bbbbbbbbbb       ? 2222222222222222 :\n"
8188                "                          3333333333333333;",
8189                Style);
8190   verifyFormat("return aaaaaaaaaa       ? 1111111111111111 :\n"
8191                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8192                "                          3333333333333333;",
8193                Style);
8194   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8195                "       bbbbbbbbbbbbbbbb ? 222222 :\n"
8196                "                          333333;",
8197                Style);
8198   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8199                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8200                "       cccccccccccccccc ? 3333333333333333 :\n"
8201                "                          4444444444444444;",
8202                Style);
8203   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
8204                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8205                "                          3333333333333333;",
8206                Style);
8207   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8208                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8209                "                          (aaa ? bbb : ccc);",
8210                Style);
8211   verifyFormat(
8212       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8213       "                                               cccccccccccccccccc) :\n"
8214       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8215       "                          3333333333333333;",
8216       Style);
8217   verifyFormat(
8218       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8219       "                                               cccccccccccccccccc) :\n"
8220       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8221       "                          3333333333333333;",
8222       Style);
8223   verifyFormat(
8224       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8225       "                                               dddddddddddddddddd) :\n"
8226       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8227       "                          3333333333333333;",
8228       Style);
8229   verifyFormat(
8230       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8231       "                                               dddddddddddddddddd) :\n"
8232       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8233       "                          3333333333333333;",
8234       Style);
8235   verifyFormat(
8236       "return aaaaaaaaa        ? 1111111111111111 :\n"
8237       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8238       "                          a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8239       "                                               dddddddddddddddddd)\n",
8240       Style);
8241   verifyFormat(
8242       "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
8243       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8244       "                          (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8245       "                                               cccccccccccccccccc);",
8246       Style);
8247   verifyFormat(
8248       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8249       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
8250       "                                               eeeeeeeeeeeeeeeeee) :\n"
8251       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8252       "                          3333333333333333;",
8253       Style);
8254   verifyFormat(
8255       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8256       "                           ccccccccccccc     ? dddddddddddddddddd :\n"
8257       "                                               eeeeeeeeeeeeeeeeee) :\n"
8258       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8259       "                          3333333333333333;",
8260       Style);
8261   verifyFormat(
8262       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa     ? bbbbbbbbbbbbbbbbbb :\n"
8263       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
8264       "                                               eeeeeeeeeeeeeeeeee) :\n"
8265       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8266       "                          3333333333333333;",
8267       Style);
8268   verifyFormat(
8269       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8270       "                                               cccccccccccccccccc :\n"
8271       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8272       "                          3333333333333333;",
8273       Style);
8274   verifyFormat(
8275       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8276       "                          cccccccccccccccccc ? dddddddddddddddddd :\n"
8277       "                                               eeeeeeeeeeeeeeeeee :\n"
8278       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8279       "                          3333333333333333;",
8280       Style);
8281   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
8282                "           (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8283                "            cccccccccccccccccc ? dddddddddddddddddd :\n"
8284                "                                 eeeeeeeeeeeeeeeeee) :\n"
8285                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8286                "                               3333333333333333;",
8287                Style);
8288   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
8289                "           aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
8290                "           cccccccccccccccccccc ? dddddddddddddddddd :\n"
8291                "                                  eeeeeeeeeeeeeeeeee :\n"
8292                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
8293                "                               3333333333333333;",
8294                Style);
8295 }
8296 
8297 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
8298   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
8299                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
8300   verifyFormat("bool a = true, b = false;");
8301 
8302   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8303                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
8304                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
8305                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
8306   verifyFormat(
8307       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
8308       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
8309       "     d = e && f;");
8310   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
8311                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
8312   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
8313                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
8314   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
8315                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
8316 
8317   FormatStyle Style = getGoogleStyle();
8318   Style.PointerAlignment = FormatStyle::PAS_Left;
8319   Style.DerivePointerAlignment = false;
8320   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8321                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
8322                "    *b = bbbbbbbbbbbbbbbbbbb;",
8323                Style);
8324   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
8325                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
8326                Style);
8327   verifyFormat("vector<int*> a, b;", Style);
8328   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
8329 }
8330 
8331 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
8332   verifyFormat("arr[foo ? bar : baz];");
8333   verifyFormat("f()[foo ? bar : baz];");
8334   verifyFormat("(a + b)[foo ? bar : baz];");
8335   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
8336 }
8337 
8338 TEST_F(FormatTest, AlignsStringLiterals) {
8339   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
8340                "                                      \"short literal\");");
8341   verifyFormat(
8342       "looooooooooooooooooooooooongFunction(\n"
8343       "    \"short literal\"\n"
8344       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
8345   verifyFormat("someFunction(\"Always break between multi-line\"\n"
8346                "             \" string literals\",\n"
8347                "             and, other, parameters);");
8348   EXPECT_EQ("fun + \"1243\" /* comment */\n"
8349             "      \"5678\";",
8350             format("fun + \"1243\" /* comment */\n"
8351                    "    \"5678\";",
8352                    getLLVMStyleWithColumns(28)));
8353   EXPECT_EQ(
8354       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8355       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
8356       "         \"aaaaaaaaaaaaaaaa\";",
8357       format("aaaaaa ="
8358              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
8359              "aaaaaaaaaaaaaaaaaaaaa\" "
8360              "\"aaaaaaaaaaaaaaaa\";"));
8361   verifyFormat("a = a + \"a\"\n"
8362                "        \"a\"\n"
8363                "        \"a\";");
8364   verifyFormat("f(\"a\", \"b\"\n"
8365                "       \"c\");");
8366 
8367   verifyFormat(
8368       "#define LL_FORMAT \"ll\"\n"
8369       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
8370       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
8371 
8372   verifyFormat("#define A(X)          \\\n"
8373                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
8374                "  \"ccccc\"",
8375                getLLVMStyleWithColumns(23));
8376   verifyFormat("#define A \"def\"\n"
8377                "f(\"abc\" A \"ghi\"\n"
8378                "  \"jkl\");");
8379 
8380   verifyFormat("f(L\"a\"\n"
8381                "  L\"b\");");
8382   verifyFormat("#define A(X)            \\\n"
8383                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
8384                "  L\"ccccc\"",
8385                getLLVMStyleWithColumns(25));
8386 
8387   verifyFormat("f(@\"a\"\n"
8388                "  @\"b\");");
8389   verifyFormat("NSString s = @\"a\"\n"
8390                "             @\"b\"\n"
8391                "             @\"c\";");
8392   verifyFormat("NSString s = @\"a\"\n"
8393                "              \"b\"\n"
8394                "              \"c\";");
8395 }
8396 
8397 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
8398   FormatStyle Style = getLLVMStyle();
8399   // No declarations or definitions should be moved to own line.
8400   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
8401   verifyFormat("class A {\n"
8402                "  int f() { return 1; }\n"
8403                "  int g();\n"
8404                "};\n"
8405                "int f() { return 1; }\n"
8406                "int g();\n",
8407                Style);
8408 
8409   // All declarations and definitions should have the return type moved to its
8410   // own line.
8411   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
8412   Style.TypenameMacros = {"LIST"};
8413   verifyFormat("SomeType\n"
8414                "funcdecl(LIST(uint64_t));",
8415                Style);
8416   verifyFormat("class E {\n"
8417                "  int\n"
8418                "  f() {\n"
8419                "    return 1;\n"
8420                "  }\n"
8421                "  int\n"
8422                "  g();\n"
8423                "};\n"
8424                "int\n"
8425                "f() {\n"
8426                "  return 1;\n"
8427                "}\n"
8428                "int\n"
8429                "g();\n",
8430                Style);
8431 
8432   // Top-level definitions, and no kinds of declarations should have the
8433   // return type moved to its own line.
8434   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
8435   verifyFormat("class B {\n"
8436                "  int f() { return 1; }\n"
8437                "  int g();\n"
8438                "};\n"
8439                "int\n"
8440                "f() {\n"
8441                "  return 1;\n"
8442                "}\n"
8443                "int g();\n",
8444                Style);
8445 
8446   // Top-level definitions and declarations should have the return type moved
8447   // to its own line.
8448   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
8449   verifyFormat("class C {\n"
8450                "  int f() { return 1; }\n"
8451                "  int g();\n"
8452                "};\n"
8453                "int\n"
8454                "f() {\n"
8455                "  return 1;\n"
8456                "}\n"
8457                "int\n"
8458                "g();\n",
8459                Style);
8460 
8461   // All definitions should have the return type moved to its own line, but no
8462   // kinds of declarations.
8463   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
8464   verifyFormat("class D {\n"
8465                "  int\n"
8466                "  f() {\n"
8467                "    return 1;\n"
8468                "  }\n"
8469                "  int g();\n"
8470                "};\n"
8471                "int\n"
8472                "f() {\n"
8473                "  return 1;\n"
8474                "}\n"
8475                "int g();\n",
8476                Style);
8477   verifyFormat("const char *\n"
8478                "f(void) {\n" // Break here.
8479                "  return \"\";\n"
8480                "}\n"
8481                "const char *bar(void);\n", // No break here.
8482                Style);
8483   verifyFormat("template <class T>\n"
8484                "T *\n"
8485                "f(T &c) {\n" // Break here.
8486                "  return NULL;\n"
8487                "}\n"
8488                "template <class T> T *f(T &c);\n", // No break here.
8489                Style);
8490   verifyFormat("class C {\n"
8491                "  int\n"
8492                "  operator+() {\n"
8493                "    return 1;\n"
8494                "  }\n"
8495                "  int\n"
8496                "  operator()() {\n"
8497                "    return 1;\n"
8498                "  }\n"
8499                "};\n",
8500                Style);
8501   verifyFormat("void\n"
8502                "A::operator()() {}\n"
8503                "void\n"
8504                "A::operator>>() {}\n"
8505                "void\n"
8506                "A::operator+() {}\n"
8507                "void\n"
8508                "A::operator*() {}\n"
8509                "void\n"
8510                "A::operator->() {}\n"
8511                "void\n"
8512                "A::operator void *() {}\n"
8513                "void\n"
8514                "A::operator void &() {}\n"
8515                "void\n"
8516                "A::operator void &&() {}\n"
8517                "void\n"
8518                "A::operator char *() {}\n"
8519                "void\n"
8520                "A::operator[]() {}\n"
8521                "void\n"
8522                "A::operator!() {}\n"
8523                "void\n"
8524                "A::operator**() {}\n"
8525                "void\n"
8526                "A::operator<Foo> *() {}\n"
8527                "void\n"
8528                "A::operator<Foo> **() {}\n"
8529                "void\n"
8530                "A::operator<Foo> &() {}\n"
8531                "void\n"
8532                "A::operator void **() {}\n",
8533                Style);
8534   verifyFormat("constexpr auto\n"
8535                "operator()() const -> reference {}\n"
8536                "constexpr auto\n"
8537                "operator>>() const -> reference {}\n"
8538                "constexpr auto\n"
8539                "operator+() const -> reference {}\n"
8540                "constexpr auto\n"
8541                "operator*() const -> reference {}\n"
8542                "constexpr auto\n"
8543                "operator->() const -> reference {}\n"
8544                "constexpr auto\n"
8545                "operator++() const -> reference {}\n"
8546                "constexpr auto\n"
8547                "operator void *() const -> reference {}\n"
8548                "constexpr auto\n"
8549                "operator void **() const -> reference {}\n"
8550                "constexpr auto\n"
8551                "operator void *() const -> reference {}\n"
8552                "constexpr auto\n"
8553                "operator void &() const -> reference {}\n"
8554                "constexpr auto\n"
8555                "operator void &&() const -> reference {}\n"
8556                "constexpr auto\n"
8557                "operator char *() const -> reference {}\n"
8558                "constexpr auto\n"
8559                "operator!() const -> reference {}\n"
8560                "constexpr auto\n"
8561                "operator[]() const -> reference {}\n",
8562                Style);
8563   verifyFormat("void *operator new(std::size_t s);", // No break here.
8564                Style);
8565   verifyFormat("void *\n"
8566                "operator new(std::size_t s) {}",
8567                Style);
8568   verifyFormat("void *\n"
8569                "operator delete[](void *ptr) {}",
8570                Style);
8571   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8572   verifyFormat("const char *\n"
8573                "f(void)\n" // Break here.
8574                "{\n"
8575                "  return \"\";\n"
8576                "}\n"
8577                "const char *bar(void);\n", // No break here.
8578                Style);
8579   verifyFormat("template <class T>\n"
8580                "T *\n"     // Problem here: no line break
8581                "f(T &c)\n" // Break here.
8582                "{\n"
8583                "  return NULL;\n"
8584                "}\n"
8585                "template <class T> T *f(T &c);\n", // No break here.
8586                Style);
8587   verifyFormat("int\n"
8588                "foo(A<bool> a)\n"
8589                "{\n"
8590                "  return a;\n"
8591                "}\n",
8592                Style);
8593   verifyFormat("int\n"
8594                "foo(A<8> a)\n"
8595                "{\n"
8596                "  return a;\n"
8597                "}\n",
8598                Style);
8599   verifyFormat("int\n"
8600                "foo(A<B<bool>, 8> a)\n"
8601                "{\n"
8602                "  return a;\n"
8603                "}\n",
8604                Style);
8605   verifyFormat("int\n"
8606                "foo(A<B<8>, bool> a)\n"
8607                "{\n"
8608                "  return a;\n"
8609                "}\n",
8610                Style);
8611   verifyFormat("int\n"
8612                "foo(A<B<bool>, bool> a)\n"
8613                "{\n"
8614                "  return a;\n"
8615                "}\n",
8616                Style);
8617   verifyFormat("int\n"
8618                "foo(A<B<8>, 8> a)\n"
8619                "{\n"
8620                "  return a;\n"
8621                "}\n",
8622                Style);
8623 
8624   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
8625   Style.BraceWrapping.AfterFunction = true;
8626   verifyFormat("int f(i);\n" // No break here.
8627                "int\n"       // Break here.
8628                "f(i)\n"
8629                "{\n"
8630                "  return i + 1;\n"
8631                "}\n"
8632                "int\n" // Break here.
8633                "f(i)\n"
8634                "{\n"
8635                "  return i + 1;\n"
8636                "};",
8637                Style);
8638   verifyFormat("int f(a, b, c);\n" // No break here.
8639                "int\n"             // Break here.
8640                "f(a, b, c)\n"      // Break here.
8641                "short a, b;\n"
8642                "float c;\n"
8643                "{\n"
8644                "  return a + b < c;\n"
8645                "}\n"
8646                "int\n"        // Break here.
8647                "f(a, b, c)\n" // Break here.
8648                "short a, b;\n"
8649                "float c;\n"
8650                "{\n"
8651                "  return a + b < c;\n"
8652                "};",
8653                Style);
8654   verifyFormat("byte *\n" // Break here.
8655                "f(a)\n"   // Break here.
8656                "byte a[];\n"
8657                "{\n"
8658                "  return a;\n"
8659                "}",
8660                Style);
8661   verifyFormat("bool f(int a, int) override;\n"
8662                "Bar g(int a, Bar) final;\n"
8663                "Bar h(a, Bar) final;",
8664                Style);
8665   verifyFormat("int\n"
8666                "f(a)",
8667                Style);
8668   verifyFormat("bool\n"
8669                "f(size_t = 0, bool b = false)\n"
8670                "{\n"
8671                "  return !b;\n"
8672                "}",
8673                Style);
8674 
8675   // The return breaking style doesn't affect:
8676   // * function and object definitions with attribute-like macros
8677   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8678                "    ABSL_GUARDED_BY(mutex) = {};",
8679                getGoogleStyleWithColumns(40));
8680   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8681                "    ABSL_GUARDED_BY(mutex);  // comment",
8682                getGoogleStyleWithColumns(40));
8683   verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
8684                "    ABSL_GUARDED_BY(mutex1)\n"
8685                "        ABSL_GUARDED_BY(mutex2);",
8686                getGoogleStyleWithColumns(40));
8687   verifyFormat("Tttttt f(int a, int b)\n"
8688                "    ABSL_GUARDED_BY(mutex1)\n"
8689                "        ABSL_GUARDED_BY(mutex2);",
8690                getGoogleStyleWithColumns(40));
8691   // * typedefs
8692   verifyFormat("typedef ATTR(X) char x;", getGoogleStyle());
8693 
8694   Style = getGNUStyle();
8695 
8696   // Test for comments at the end of function declarations.
8697   verifyFormat("void\n"
8698                "foo (int a, /*abc*/ int b) // def\n"
8699                "{\n"
8700                "}\n",
8701                Style);
8702 
8703   verifyFormat("void\n"
8704                "foo (int a, /* abc */ int b) /* def */\n"
8705                "{\n"
8706                "}\n",
8707                Style);
8708 
8709   // Definitions that should not break after return type
8710   verifyFormat("void foo (int a, int b); // def\n", Style);
8711   verifyFormat("void foo (int a, int b); /* def */\n", Style);
8712   verifyFormat("void foo (int a, int b);\n", Style);
8713 }
8714 
8715 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
8716   FormatStyle NoBreak = getLLVMStyle();
8717   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
8718   FormatStyle Break = getLLVMStyle();
8719   Break.AlwaysBreakBeforeMultilineStrings = true;
8720   verifyFormat("aaaa = \"bbbb\"\n"
8721                "       \"cccc\";",
8722                NoBreak);
8723   verifyFormat("aaaa =\n"
8724                "    \"bbbb\"\n"
8725                "    \"cccc\";",
8726                Break);
8727   verifyFormat("aaaa(\"bbbb\"\n"
8728                "     \"cccc\");",
8729                NoBreak);
8730   verifyFormat("aaaa(\n"
8731                "    \"bbbb\"\n"
8732                "    \"cccc\");",
8733                Break);
8734   verifyFormat("aaaa(qqq, \"bbbb\"\n"
8735                "          \"cccc\");",
8736                NoBreak);
8737   verifyFormat("aaaa(qqq,\n"
8738                "     \"bbbb\"\n"
8739                "     \"cccc\");",
8740                Break);
8741   verifyFormat("aaaa(qqq,\n"
8742                "     L\"bbbb\"\n"
8743                "     L\"cccc\");",
8744                Break);
8745   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
8746                "                      \"bbbb\"));",
8747                Break);
8748   verifyFormat("string s = someFunction(\n"
8749                "    \"abc\"\n"
8750                "    \"abc\");",
8751                Break);
8752 
8753   // As we break before unary operators, breaking right after them is bad.
8754   verifyFormat("string foo = abc ? \"x\"\n"
8755                "                   \"blah blah blah blah blah blah\"\n"
8756                "                 : \"y\";",
8757                Break);
8758 
8759   // Don't break if there is no column gain.
8760   verifyFormat("f(\"aaaa\"\n"
8761                "  \"bbbb\");",
8762                Break);
8763 
8764   // Treat literals with escaped newlines like multi-line string literals.
8765   EXPECT_EQ("x = \"a\\\n"
8766             "b\\\n"
8767             "c\";",
8768             format("x = \"a\\\n"
8769                    "b\\\n"
8770                    "c\";",
8771                    NoBreak));
8772   EXPECT_EQ("xxxx =\n"
8773             "    \"a\\\n"
8774             "b\\\n"
8775             "c\";",
8776             format("xxxx = \"a\\\n"
8777                    "b\\\n"
8778                    "c\";",
8779                    Break));
8780 
8781   EXPECT_EQ("NSString *const kString =\n"
8782             "    @\"aaaa\"\n"
8783             "    @\"bbbb\";",
8784             format("NSString *const kString = @\"aaaa\"\n"
8785                    "@\"bbbb\";",
8786                    Break));
8787 
8788   Break.ColumnLimit = 0;
8789   verifyFormat("const char *hello = \"hello llvm\";", Break);
8790 }
8791 
8792 TEST_F(FormatTest, AlignsPipes) {
8793   verifyFormat(
8794       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8795       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8796       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8797   verifyFormat(
8798       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
8799       "                     << aaaaaaaaaaaaaaaaaaaa;");
8800   verifyFormat(
8801       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8802       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8803   verifyFormat(
8804       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8805       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8806   verifyFormat(
8807       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
8808       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
8809       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
8810   verifyFormat(
8811       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8812       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8813       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8814   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8815                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8816                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8817                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8818   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
8819                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
8820   verifyFormat(
8821       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8822       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8823   verifyFormat(
8824       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
8825       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
8826 
8827   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
8828                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
8829   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8830                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8831                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
8832                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
8833   verifyFormat("LOG_IF(aaa == //\n"
8834                "       bbb)\n"
8835                "    << a << b;");
8836 
8837   // But sometimes, breaking before the first "<<" is desirable.
8838   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8839                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
8840   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
8841                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8842                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8843   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
8844                "    << BEF << IsTemplate << Description << E->getType();");
8845   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8846                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8847                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8848   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
8849                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8850                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8851                "    << aaa;");
8852 
8853   verifyFormat(
8854       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8855       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8856 
8857   // Incomplete string literal.
8858   EXPECT_EQ("llvm::errs() << \"\n"
8859             "             << a;",
8860             format("llvm::errs() << \"\n<<a;"));
8861 
8862   verifyFormat("void f() {\n"
8863                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
8864                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
8865                "}");
8866 
8867   // Handle 'endl'.
8868   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
8869                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
8870   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
8871 
8872   // Handle '\n'.
8873   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
8874                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
8875   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
8876                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
8877   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
8878                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
8879   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
8880 }
8881 
8882 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
8883   verifyFormat("return out << \"somepacket = {\\n\"\n"
8884                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
8885                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
8886                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
8887                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
8888                "           << \"}\";");
8889 
8890   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
8891                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
8892                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
8893   verifyFormat(
8894       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
8895       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
8896       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
8897       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
8898       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
8899   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
8900                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8901   verifyFormat(
8902       "void f() {\n"
8903       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
8904       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8905       "}");
8906 
8907   // Breaking before the first "<<" is generally not desirable.
8908   verifyFormat(
8909       "llvm::errs()\n"
8910       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8911       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8912       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8913       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8914       getLLVMStyleWithColumns(70));
8915   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8916                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8917                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8918                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8919                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
8920                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8921                getLLVMStyleWithColumns(70));
8922 
8923   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
8924                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
8925                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
8926   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
8927                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
8928                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
8929   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
8930                "           (aaaa + aaaa);",
8931                getLLVMStyleWithColumns(40));
8932   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
8933                "                  (aaaaaaa + aaaaa));",
8934                getLLVMStyleWithColumns(40));
8935   verifyFormat(
8936       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
8937       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
8938       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
8939 }
8940 
8941 TEST_F(FormatTest, UnderstandsEquals) {
8942   verifyFormat(
8943       "aaaaaaaaaaaaaaaaa =\n"
8944       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8945   verifyFormat(
8946       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8947       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
8948   verifyFormat(
8949       "if (a) {\n"
8950       "  f();\n"
8951       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8952       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
8953       "}");
8954 
8955   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8956                "        100000000 + 10000000) {\n}");
8957 }
8958 
8959 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
8960   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
8961                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
8962 
8963   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
8964                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
8965 
8966   verifyFormat(
8967       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
8968       "                                                          Parameter2);");
8969 
8970   verifyFormat(
8971       "ShortObject->shortFunction(\n"
8972       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
8973       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
8974 
8975   verifyFormat("loooooooooooooongFunction(\n"
8976                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
8977 
8978   verifyFormat(
8979       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
8980       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
8981 
8982   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
8983                "    .WillRepeatedly(Return(SomeValue));");
8984   verifyFormat("void f() {\n"
8985                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
8986                "      .Times(2)\n"
8987                "      .WillRepeatedly(Return(SomeValue));\n"
8988                "}");
8989   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
8990                "    ccccccccccccccccccccccc);");
8991   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8992                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8993                "          .aaaaa(aaaaa),\n"
8994                "      aaaaaaaaaaaaaaaaaaaaa);");
8995   verifyFormat("void f() {\n"
8996                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8997                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
8998                "}");
8999   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9000                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9001                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9002                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9003                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
9004   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9005                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9006                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9007                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
9008                "}");
9009 
9010   // Here, it is not necessary to wrap at "." or "->".
9011   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
9012                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
9013   verifyFormat(
9014       "aaaaaaaaaaa->aaaaaaaaa(\n"
9015       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9016       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
9017 
9018   verifyFormat(
9019       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9020       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
9021   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
9022                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
9023   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
9024                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
9025 
9026   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9027                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9028                "    .a();");
9029 
9030   FormatStyle NoBinPacking = getLLVMStyle();
9031   NoBinPacking.BinPackParameters = false;
9032   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
9033                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
9034                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
9035                "                         aaaaaaaaaaaaaaaaaaa,\n"
9036                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9037                NoBinPacking);
9038 
9039   // If there is a subsequent call, change to hanging indentation.
9040   verifyFormat(
9041       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9042       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
9043       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9044   verifyFormat(
9045       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9046       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
9047   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9048                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9049                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9050   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9051                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9052                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
9053 }
9054 
9055 TEST_F(FormatTest, WrapsTemplateDeclarations) {
9056   verifyFormat("template <typename T>\n"
9057                "virtual void loooooooooooongFunction(int Param1, int Param2);");
9058   verifyFormat("template <typename T>\n"
9059                "// T should be one of {A, B}.\n"
9060                "virtual void loooooooooooongFunction(int Param1, int Param2);");
9061   verifyFormat(
9062       "template <typename T>\n"
9063       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
9064   verifyFormat("template <typename T>\n"
9065                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
9066                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
9067   verifyFormat(
9068       "template <typename T>\n"
9069       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
9070       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
9071   verifyFormat(
9072       "template <typename T>\n"
9073       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
9074       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
9075       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9076   verifyFormat("template <typename T>\n"
9077                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9078                "    int aaaaaaaaaaaaaaaaaaaaaa);");
9079   verifyFormat(
9080       "template <typename T1, typename T2 = char, typename T3 = char,\n"
9081       "          typename T4 = char>\n"
9082       "void f();");
9083   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
9084                "          template <typename> class cccccccccccccccccccccc,\n"
9085                "          typename ddddddddddddd>\n"
9086                "class C {};");
9087   verifyFormat(
9088       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
9089       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9090 
9091   verifyFormat("void f() {\n"
9092                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
9093                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
9094                "}");
9095 
9096   verifyFormat("template <typename T> class C {};");
9097   verifyFormat("template <typename T> void f();");
9098   verifyFormat("template <typename T> void f() {}");
9099   verifyFormat(
9100       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
9101       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9102       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
9103       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
9104       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9105       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
9106       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
9107       getLLVMStyleWithColumns(72));
9108   EXPECT_EQ("static_cast<A< //\n"
9109             "    B> *>(\n"
9110             "\n"
9111             ");",
9112             format("static_cast<A<//\n"
9113                    "    B>*>(\n"
9114                    "\n"
9115                    "    );"));
9116   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9117                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
9118 
9119   FormatStyle AlwaysBreak = getLLVMStyle();
9120   AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
9121   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
9122   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
9123   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
9124   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9125                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
9126                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
9127   verifyFormat("template <template <typename> class Fooooooo,\n"
9128                "          template <typename> class Baaaaaaar>\n"
9129                "struct C {};",
9130                AlwaysBreak);
9131   verifyFormat("template <typename T> // T can be A, B or C.\n"
9132                "struct C {};",
9133                AlwaysBreak);
9134   verifyFormat("template <enum E> class A {\n"
9135                "public:\n"
9136                "  E *f();\n"
9137                "};");
9138 
9139   FormatStyle NeverBreak = getLLVMStyle();
9140   NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No;
9141   verifyFormat("template <typename T> class C {};", NeverBreak);
9142   verifyFormat("template <typename T> void f();", NeverBreak);
9143   verifyFormat("template <typename T> void f() {}", NeverBreak);
9144   verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
9145                "bbbbbbbbbbbbbbbbbbbb) {}",
9146                NeverBreak);
9147   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9148                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
9149                "    ccccccccccccccccccccccccccccccccccccccccccccccc);",
9150                NeverBreak);
9151   verifyFormat("template <template <typename> class Fooooooo,\n"
9152                "          template <typename> class Baaaaaaar>\n"
9153                "struct C {};",
9154                NeverBreak);
9155   verifyFormat("template <typename T> // T can be A, B or C.\n"
9156                "struct C {};",
9157                NeverBreak);
9158   verifyFormat("template <enum E> class A {\n"
9159                "public:\n"
9160                "  E *f();\n"
9161                "};",
9162                NeverBreak);
9163   NeverBreak.PenaltyBreakTemplateDeclaration = 100;
9164   verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
9165                "bbbbbbbbbbbbbbbbbbbb) {}",
9166                NeverBreak);
9167 }
9168 
9169 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
9170   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
9171   Style.ColumnLimit = 60;
9172   EXPECT_EQ("// Baseline - no comments.\n"
9173             "template <\n"
9174             "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
9175             "void f() {}",
9176             format("// Baseline - no comments.\n"
9177                    "template <\n"
9178                    "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
9179                    "void f() {}",
9180                    Style));
9181 
9182   EXPECT_EQ("template <\n"
9183             "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
9184             "void f() {}",
9185             format("template <\n"
9186                    "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
9187                    "void f() {}",
9188                    Style));
9189 
9190   EXPECT_EQ(
9191       "template <\n"
9192       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
9193       "void f() {}",
9194       format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  /* line */\n"
9195              "void f() {}",
9196              Style));
9197 
9198   EXPECT_EQ(
9199       "template <\n"
9200       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
9201       "                                               // multiline\n"
9202       "void f() {}",
9203       format("template <\n"
9204              "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
9205              "                                              // multiline\n"
9206              "void f() {}",
9207              Style));
9208 
9209   EXPECT_EQ(
9210       "template <typename aaaaaaaaaa<\n"
9211       "    bbbbbbbbbbbb>::value>  // trailing loooong\n"
9212       "void f() {}",
9213       format(
9214           "template <\n"
9215           "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
9216           "void f() {}",
9217           Style));
9218 }
9219 
9220 TEST_F(FormatTest, WrapsTemplateParameters) {
9221   FormatStyle Style = getLLVMStyle();
9222   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9223   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9224   verifyFormat(
9225       "template <typename... a> struct q {};\n"
9226       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
9227       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
9228       "    y;",
9229       Style);
9230   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9231   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9232   verifyFormat(
9233       "template <typename... a> struct r {};\n"
9234       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
9235       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
9236       "    y;",
9237       Style);
9238   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9239   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9240   verifyFormat("template <typename... a> struct s {};\n"
9241                "extern s<\n"
9242                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9243                "aaaaaaaaaaaaaaaaaaaaaa,\n"
9244                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9245                "aaaaaaaaaaaaaaaaaaaaaa>\n"
9246                "    y;",
9247                Style);
9248   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9249   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9250   verifyFormat("template <typename... a> struct t {};\n"
9251                "extern t<\n"
9252                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9253                "aaaaaaaaaaaaaaaaaaaaaa,\n"
9254                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
9255                "aaaaaaaaaaaaaaaaaaaaaa>\n"
9256                "    y;",
9257                Style);
9258 }
9259 
9260 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
9261   verifyFormat(
9262       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9263       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9264   verifyFormat(
9265       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9266       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9267       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
9268 
9269   // FIXME: Should we have the extra indent after the second break?
9270   verifyFormat(
9271       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9272       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9273       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9274 
9275   verifyFormat(
9276       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
9277       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
9278 
9279   // Breaking at nested name specifiers is generally not desirable.
9280   verifyFormat(
9281       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9282       "    aaaaaaaaaaaaaaaaaaaaaaa);");
9283 
9284   verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
9285                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9286                "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9287                "                   aaaaaaaaaaaaaaaaaaaaa);",
9288                getLLVMStyleWithColumns(74));
9289 
9290   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
9291                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9292                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9293 }
9294 
9295 TEST_F(FormatTest, UnderstandsTemplateParameters) {
9296   verifyFormat("A<int> a;");
9297   verifyFormat("A<A<A<int>>> a;");
9298   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
9299   verifyFormat("bool x = a < 1 || 2 > a;");
9300   verifyFormat("bool x = 5 < f<int>();");
9301   verifyFormat("bool x = f<int>() > 5;");
9302   verifyFormat("bool x = 5 < a<int>::x;");
9303   verifyFormat("bool x = a < 4 ? a > 2 : false;");
9304   verifyFormat("bool x = f() ? a < 2 : a > 2;");
9305 
9306   verifyGoogleFormat("A<A<int>> a;");
9307   verifyGoogleFormat("A<A<A<int>>> a;");
9308   verifyGoogleFormat("A<A<A<A<int>>>> a;");
9309   verifyGoogleFormat("A<A<int> > a;");
9310   verifyGoogleFormat("A<A<A<int> > > a;");
9311   verifyGoogleFormat("A<A<A<A<int> > > > a;");
9312   verifyGoogleFormat("A<::A<int>> a;");
9313   verifyGoogleFormat("A<::A> a;");
9314   verifyGoogleFormat("A< ::A> a;");
9315   verifyGoogleFormat("A< ::A<int> > a;");
9316   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
9317   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
9318   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
9319   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
9320   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
9321             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
9322 
9323   verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
9324 
9325   // template closer followed by a token that starts with > or =
9326   verifyFormat("bool b = a<1> > 1;");
9327   verifyFormat("bool b = a<1> >= 1;");
9328   verifyFormat("int i = a<1> >> 1;");
9329   FormatStyle Style = getLLVMStyle();
9330   Style.SpaceBeforeAssignmentOperators = false;
9331   verifyFormat("bool b= a<1> == 1;", Style);
9332   verifyFormat("a<int> = 1;", Style);
9333   verifyFormat("a<int> >>= 1;", Style);
9334 
9335   verifyFormat("test < a | b >> c;");
9336   verifyFormat("test<test<a | b>> c;");
9337   verifyFormat("test >> a >> b;");
9338   verifyFormat("test << a >> b;");
9339 
9340   verifyFormat("f<int>();");
9341   verifyFormat("template <typename T> void f() {}");
9342   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
9343   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
9344                "sizeof(char)>::type>;");
9345   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
9346   verifyFormat("f(a.operator()<A>());");
9347   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9348                "      .template operator()<A>());",
9349                getLLVMStyleWithColumns(35));
9350 
9351   // Not template parameters.
9352   verifyFormat("return a < b && c > d;");
9353   verifyFormat("void f() {\n"
9354                "  while (a < b && c > d) {\n"
9355                "  }\n"
9356                "}");
9357   verifyFormat("template <typename... Types>\n"
9358                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
9359 
9360   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9361                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
9362                getLLVMStyleWithColumns(60));
9363   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
9364   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
9365   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
9366   verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
9367 }
9368 
9369 TEST_F(FormatTest, UnderstandsShiftOperators) {
9370   verifyFormat("if (i < x >> 1)");
9371   verifyFormat("while (i < x >> 1)");
9372   verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
9373   verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
9374   verifyFormat(
9375       "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
9376   verifyFormat("Foo.call<Bar<Function>>()");
9377   verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
9378   verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
9379                "++i, v = v >> 1)");
9380   verifyFormat("if (w<u<v<x>>, 1>::t)");
9381 }
9382 
9383 TEST_F(FormatTest, BitshiftOperatorWidth) {
9384   EXPECT_EQ("int a = 1 << 2; /* foo\n"
9385             "                   bar */",
9386             format("int    a=1<<2;  /* foo\n"
9387                    "                   bar */"));
9388 
9389   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
9390             "                     bar */",
9391             format("int  b  =256>>1 ;  /* foo\n"
9392                    "                      bar */"));
9393 }
9394 
9395 TEST_F(FormatTest, UnderstandsBinaryOperators) {
9396   verifyFormat("COMPARE(a, ==, b);");
9397   verifyFormat("auto s = sizeof...(Ts) - 1;");
9398 }
9399 
9400 TEST_F(FormatTest, UnderstandsPointersToMembers) {
9401   verifyFormat("int A::*x;");
9402   verifyFormat("int (S::*func)(void *);");
9403   verifyFormat("void f() { int (S::*func)(void *); }");
9404   verifyFormat("typedef bool *(Class::*Member)() const;");
9405   verifyFormat("void f() {\n"
9406                "  (a->*f)();\n"
9407                "  a->*x;\n"
9408                "  (a.*f)();\n"
9409                "  ((*a).*f)();\n"
9410                "  a.*x;\n"
9411                "}");
9412   verifyFormat("void f() {\n"
9413                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
9414                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
9415                "}");
9416   verifyFormat(
9417       "(aaaaaaaaaa->*bbbbbbb)(\n"
9418       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
9419   FormatStyle Style = getLLVMStyle();
9420   Style.PointerAlignment = FormatStyle::PAS_Left;
9421   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
9422 }
9423 
9424 TEST_F(FormatTest, UnderstandsUnaryOperators) {
9425   verifyFormat("int a = -2;");
9426   verifyFormat("f(-1, -2, -3);");
9427   verifyFormat("a[-1] = 5;");
9428   verifyFormat("int a = 5 + -2;");
9429   verifyFormat("if (i == -1) {\n}");
9430   verifyFormat("if (i != -1) {\n}");
9431   verifyFormat("if (i > -1) {\n}");
9432   verifyFormat("if (i < -1) {\n}");
9433   verifyFormat("++(a->f());");
9434   verifyFormat("--(a->f());");
9435   verifyFormat("(a->f())++;");
9436   verifyFormat("a[42]++;");
9437   verifyFormat("if (!(a->f())) {\n}");
9438   verifyFormat("if (!+i) {\n}");
9439   verifyFormat("~&a;");
9440 
9441   verifyFormat("a-- > b;");
9442   verifyFormat("b ? -a : c;");
9443   verifyFormat("n * sizeof char16;");
9444   verifyFormat("n * alignof char16;", getGoogleStyle());
9445   verifyFormat("sizeof(char);");
9446   verifyFormat("alignof(char);", getGoogleStyle());
9447 
9448   verifyFormat("return -1;");
9449   verifyFormat("throw -1;");
9450   verifyFormat("switch (a) {\n"
9451                "case -1:\n"
9452                "  break;\n"
9453                "}");
9454   verifyFormat("#define X -1");
9455   verifyFormat("#define X -kConstant");
9456 
9457   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
9458   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
9459 
9460   verifyFormat("int a = /* confusing comment */ -1;");
9461   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
9462   verifyFormat("int a = i /* confusing comment */++;");
9463 
9464   verifyFormat("co_yield -1;");
9465   verifyFormat("co_return -1;");
9466 
9467   // Check that * is not treated as a binary operator when we set
9468   // PointerAlignment as PAS_Left after a keyword and not a declaration.
9469   FormatStyle PASLeftStyle = getLLVMStyle();
9470   PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
9471   verifyFormat("co_return *a;", PASLeftStyle);
9472   verifyFormat("co_await *a;", PASLeftStyle);
9473   verifyFormat("co_yield *a", PASLeftStyle);
9474   verifyFormat("return *a;", PASLeftStyle);
9475 }
9476 
9477 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
9478   verifyFormat("if (!aaaaaaaaaa( // break\n"
9479                "        aaaaa)) {\n"
9480                "}");
9481   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
9482                "    aaaaa));");
9483   verifyFormat("*aaa = aaaaaaa( // break\n"
9484                "    bbbbbb);");
9485 }
9486 
9487 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
9488   verifyFormat("bool operator<();");
9489   verifyFormat("bool operator>();");
9490   verifyFormat("bool operator=();");
9491   verifyFormat("bool operator==();");
9492   verifyFormat("bool operator!=();");
9493   verifyFormat("int operator+();");
9494   verifyFormat("int operator++();");
9495   verifyFormat("int operator++(int) volatile noexcept;");
9496   verifyFormat("bool operator,();");
9497   verifyFormat("bool operator();");
9498   verifyFormat("bool operator()();");
9499   verifyFormat("bool operator[]();");
9500   verifyFormat("operator bool();");
9501   verifyFormat("operator int();");
9502   verifyFormat("operator void *();");
9503   verifyFormat("operator SomeType<int>();");
9504   verifyFormat("operator SomeType<int, int>();");
9505   verifyFormat("operator SomeType<SomeType<int>>();");
9506   verifyFormat("operator< <>();");
9507   verifyFormat("operator<< <>();");
9508   verifyFormat("< <>");
9509 
9510   verifyFormat("void *operator new(std::size_t size);");
9511   verifyFormat("void *operator new[](std::size_t size);");
9512   verifyFormat("void operator delete(void *ptr);");
9513   verifyFormat("void operator delete[](void *ptr);");
9514   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
9515                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
9516   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
9517                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
9518 
9519   verifyFormat(
9520       "ostream &operator<<(ostream &OutputStream,\n"
9521       "                    SomeReallyLongType WithSomeReallyLongValue);");
9522   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
9523                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
9524                "  return left.group < right.group;\n"
9525                "}");
9526   verifyFormat("SomeType &operator=(const SomeType &S);");
9527   verifyFormat("f.template operator()<int>();");
9528 
9529   verifyGoogleFormat("operator void*();");
9530   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
9531   verifyGoogleFormat("operator ::A();");
9532 
9533   verifyFormat("using A::operator+;");
9534   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
9535                "int i;");
9536 
9537   // Calling an operator as a member function.
9538   verifyFormat("void f() { a.operator*(); }");
9539   verifyFormat("void f() { a.operator*(b & b); }");
9540   verifyFormat("void f() { a->operator&(a * b); }");
9541   verifyFormat("void f() { NS::a.operator+(*b * *b); }");
9542   // TODO: Calling an operator as a non-member function is hard to distinguish.
9543   // https://llvm.org/PR50629
9544   // verifyFormat("void f() { operator*(a & a); }");
9545   // verifyFormat("void f() { operator&(a, b * b); }");
9546 
9547   verifyFormat("::operator delete(foo);");
9548   verifyFormat("::operator new(n * sizeof(foo));");
9549   verifyFormat("foo() { ::operator delete(foo); }");
9550   verifyFormat("foo() { ::operator new(n * sizeof(foo)); }");
9551 }
9552 
9553 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
9554   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
9555   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
9556   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
9557   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
9558   verifyFormat("Deleted &operator=(const Deleted &) &;");
9559   verifyFormat("Deleted &operator=(const Deleted &) &&;");
9560   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
9561   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
9562   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
9563   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
9564   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
9565   verifyFormat("void Fn(T const &) const &;");
9566   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
9567   verifyFormat("template <typename T>\n"
9568                "void F(T) && = delete;",
9569                getGoogleStyle());
9570 
9571   FormatStyle AlignLeft = getLLVMStyle();
9572   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
9573   verifyFormat("void A::b() && {}", AlignLeft);
9574   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
9575   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
9576                AlignLeft);
9577   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
9578   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
9579   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
9580   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
9581   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
9582   verifyFormat("auto Function(T) & -> void;", AlignLeft);
9583   verifyFormat("void Fn(T const&) const&;", AlignLeft);
9584   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
9585 
9586   FormatStyle Spaces = getLLVMStyle();
9587   Spaces.SpacesInCStyleCastParentheses = true;
9588   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
9589   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
9590   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
9591   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
9592 
9593   Spaces.SpacesInCStyleCastParentheses = false;
9594   Spaces.SpacesInParentheses = true;
9595   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
9596   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
9597                Spaces);
9598   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
9599   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
9600 
9601   FormatStyle BreakTemplate = getLLVMStyle();
9602   BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
9603 
9604   verifyFormat("struct f {\n"
9605                "  template <class T>\n"
9606                "  int &foo(const std::string &str) &noexcept {}\n"
9607                "};",
9608                BreakTemplate);
9609 
9610   verifyFormat("struct f {\n"
9611                "  template <class T>\n"
9612                "  int &foo(const std::string &str) &&noexcept {}\n"
9613                "};",
9614                BreakTemplate);
9615 
9616   verifyFormat("struct f {\n"
9617                "  template <class T>\n"
9618                "  int &foo(const std::string &str) const &noexcept {}\n"
9619                "};",
9620                BreakTemplate);
9621 
9622   verifyFormat("struct f {\n"
9623                "  template <class T>\n"
9624                "  int &foo(const std::string &str) const &noexcept {}\n"
9625                "};",
9626                BreakTemplate);
9627 
9628   verifyFormat("struct f {\n"
9629                "  template <class T>\n"
9630                "  auto foo(const std::string &str) &&noexcept -> int & {}\n"
9631                "};",
9632                BreakTemplate);
9633 
9634   FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
9635   AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations =
9636       FormatStyle::BTDS_Yes;
9637   AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
9638 
9639   verifyFormat("struct f {\n"
9640                "  template <class T>\n"
9641                "  int& foo(const std::string& str) & noexcept {}\n"
9642                "};",
9643                AlignLeftBreakTemplate);
9644 
9645   verifyFormat("struct f {\n"
9646                "  template <class T>\n"
9647                "  int& foo(const std::string& str) && noexcept {}\n"
9648                "};",
9649                AlignLeftBreakTemplate);
9650 
9651   verifyFormat("struct f {\n"
9652                "  template <class T>\n"
9653                "  int& foo(const std::string& str) const& noexcept {}\n"
9654                "};",
9655                AlignLeftBreakTemplate);
9656 
9657   verifyFormat("struct f {\n"
9658                "  template <class T>\n"
9659                "  int& foo(const std::string& str) const&& noexcept {}\n"
9660                "};",
9661                AlignLeftBreakTemplate);
9662 
9663   verifyFormat("struct f {\n"
9664                "  template <class T>\n"
9665                "  auto foo(const std::string& str) && noexcept -> int& {}\n"
9666                "};",
9667                AlignLeftBreakTemplate);
9668 
9669   // The `&` in `Type&` should not be confused with a trailing `&` of
9670   // DEPRECATED(reason) member function.
9671   verifyFormat("struct f {\n"
9672                "  template <class T>\n"
9673                "  DEPRECATED(reason)\n"
9674                "  Type &foo(arguments) {}\n"
9675                "};",
9676                BreakTemplate);
9677 
9678   verifyFormat("struct f {\n"
9679                "  template <class T>\n"
9680                "  DEPRECATED(reason)\n"
9681                "  Type& foo(arguments) {}\n"
9682                "};",
9683                AlignLeftBreakTemplate);
9684 
9685   verifyFormat("void (*foopt)(int) = &func;");
9686 }
9687 
9688 TEST_F(FormatTest, UnderstandsNewAndDelete) {
9689   verifyFormat("void f() {\n"
9690                "  A *a = new A;\n"
9691                "  A *a = new (placement) A;\n"
9692                "  delete a;\n"
9693                "  delete (A *)a;\n"
9694                "}");
9695   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9696                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9697   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9698                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
9699                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
9700   verifyFormat("delete[] h->p;");
9701 
9702   verifyFormat("void operator delete(void *foo) ATTRIB;");
9703   verifyFormat("void operator new(void *foo) ATTRIB;");
9704   verifyFormat("void operator delete[](void *foo) ATTRIB;");
9705   verifyFormat("void operator delete(void *ptr) noexcept;");
9706 }
9707 
9708 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
9709   verifyFormat("int *f(int *a) {}");
9710   verifyFormat("int main(int argc, char **argv) {}");
9711   verifyFormat("Test::Test(int b) : a(b * b) {}");
9712   verifyIndependentOfContext("f(a, *a);");
9713   verifyFormat("void g() { f(*a); }");
9714   verifyIndependentOfContext("int a = b * 10;");
9715   verifyIndependentOfContext("int a = 10 * b;");
9716   verifyIndependentOfContext("int a = b * c;");
9717   verifyIndependentOfContext("int a += b * c;");
9718   verifyIndependentOfContext("int a -= b * c;");
9719   verifyIndependentOfContext("int a *= b * c;");
9720   verifyIndependentOfContext("int a /= b * c;");
9721   verifyIndependentOfContext("int a = *b;");
9722   verifyIndependentOfContext("int a = *b * c;");
9723   verifyIndependentOfContext("int a = b * *c;");
9724   verifyIndependentOfContext("int a = b * (10);");
9725   verifyIndependentOfContext("S << b * (10);");
9726   verifyIndependentOfContext("return 10 * b;");
9727   verifyIndependentOfContext("return *b * *c;");
9728   verifyIndependentOfContext("return a & ~b;");
9729   verifyIndependentOfContext("f(b ? *c : *d);");
9730   verifyIndependentOfContext("int a = b ? *c : *d;");
9731   verifyIndependentOfContext("*b = a;");
9732   verifyIndependentOfContext("a * ~b;");
9733   verifyIndependentOfContext("a * !b;");
9734   verifyIndependentOfContext("a * +b;");
9735   verifyIndependentOfContext("a * -b;");
9736   verifyIndependentOfContext("a * ++b;");
9737   verifyIndependentOfContext("a * --b;");
9738   verifyIndependentOfContext("a[4] * b;");
9739   verifyIndependentOfContext("a[a * a] = 1;");
9740   verifyIndependentOfContext("f() * b;");
9741   verifyIndependentOfContext("a * [self dostuff];");
9742   verifyIndependentOfContext("int x = a * (a + b);");
9743   verifyIndependentOfContext("(a *)(a + b);");
9744   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
9745   verifyIndependentOfContext("int *pa = (int *)&a;");
9746   verifyIndependentOfContext("return sizeof(int **);");
9747   verifyIndependentOfContext("return sizeof(int ******);");
9748   verifyIndependentOfContext("return (int **&)a;");
9749   verifyIndependentOfContext("f((*PointerToArray)[10]);");
9750   verifyFormat("void f(Type (*parameter)[10]) {}");
9751   verifyFormat("void f(Type (&parameter)[10]) {}");
9752   verifyGoogleFormat("return sizeof(int**);");
9753   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
9754   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
9755   verifyFormat("auto a = [](int **&, int ***) {};");
9756   verifyFormat("auto PointerBinding = [](const char *S) {};");
9757   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
9758   verifyFormat("[](const decltype(*a) &value) {}");
9759   verifyFormat("[](const typeof(*a) &value) {}");
9760   verifyFormat("[](const _Atomic(a *) &value) {}");
9761   verifyFormat("[](const __underlying_type(a) &value) {}");
9762   verifyFormat("decltype(a * b) F();");
9763   verifyFormat("typeof(a * b) F();");
9764   verifyFormat("#define MACRO() [](A *a) { return 1; }");
9765   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
9766   verifyIndependentOfContext("typedef void (*f)(int *a);");
9767   verifyIndependentOfContext("int i{a * b};");
9768   verifyIndependentOfContext("aaa && aaa->f();");
9769   verifyIndependentOfContext("int x = ~*p;");
9770   verifyFormat("Constructor() : a(a), area(width * height) {}");
9771   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
9772   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
9773   verifyFormat("void f() { f(a, c * d); }");
9774   verifyFormat("void f() { f(new a(), c * d); }");
9775   verifyFormat("void f(const MyOverride &override);");
9776   verifyFormat("void f(const MyFinal &final);");
9777   verifyIndependentOfContext("bool a = f() && override.f();");
9778   verifyIndependentOfContext("bool a = f() && final.f();");
9779 
9780   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
9781 
9782   verifyIndependentOfContext("A<int *> a;");
9783   verifyIndependentOfContext("A<int **> a;");
9784   verifyIndependentOfContext("A<int *, int *> a;");
9785   verifyIndependentOfContext("A<int *[]> a;");
9786   verifyIndependentOfContext(
9787       "const char *const p = reinterpret_cast<const char *const>(q);");
9788   verifyIndependentOfContext("A<int **, int **> a;");
9789   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
9790   verifyFormat("for (char **a = b; *a; ++a) {\n}");
9791   verifyFormat("for (; a && b;) {\n}");
9792   verifyFormat("bool foo = true && [] { return false; }();");
9793 
9794   verifyFormat(
9795       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9796       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9797 
9798   verifyGoogleFormat("int const* a = &b;");
9799   verifyGoogleFormat("**outparam = 1;");
9800   verifyGoogleFormat("*outparam = a * b;");
9801   verifyGoogleFormat("int main(int argc, char** argv) {}");
9802   verifyGoogleFormat("A<int*> a;");
9803   verifyGoogleFormat("A<int**> a;");
9804   verifyGoogleFormat("A<int*, int*> a;");
9805   verifyGoogleFormat("A<int**, int**> a;");
9806   verifyGoogleFormat("f(b ? *c : *d);");
9807   verifyGoogleFormat("int a = b ? *c : *d;");
9808   verifyGoogleFormat("Type* t = **x;");
9809   verifyGoogleFormat("Type* t = *++*x;");
9810   verifyGoogleFormat("*++*x;");
9811   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
9812   verifyGoogleFormat("Type* t = x++ * y;");
9813   verifyGoogleFormat(
9814       "const char* const p = reinterpret_cast<const char* const>(q);");
9815   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
9816   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
9817   verifyGoogleFormat("template <typename T>\n"
9818                      "void f(int i = 0, SomeType** temps = NULL);");
9819 
9820   FormatStyle Left = getLLVMStyle();
9821   Left.PointerAlignment = FormatStyle::PAS_Left;
9822   verifyFormat("x = *a(x) = *a(y);", Left);
9823   verifyFormat("for (;; *a = b) {\n}", Left);
9824   verifyFormat("return *this += 1;", Left);
9825   verifyFormat("throw *x;", Left);
9826   verifyFormat("delete *x;", Left);
9827   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
9828   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
9829   verifyFormat("[](const typeof(*a)* ptr) {}", Left);
9830   verifyFormat("[](const _Atomic(a*)* ptr) {}", Left);
9831   verifyFormat("[](const __underlying_type(a)* ptr) {}", Left);
9832   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
9833   verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left);
9834   verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left);
9835   verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left);
9836 
9837   verifyIndependentOfContext("a = *(x + y);");
9838   verifyIndependentOfContext("a = &(x + y);");
9839   verifyIndependentOfContext("*(x + y).call();");
9840   verifyIndependentOfContext("&(x + y)->call();");
9841   verifyFormat("void f() { &(*I).first; }");
9842 
9843   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
9844   verifyFormat("f(* /* confusing comment */ foo);");
9845   verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)");
9846   verifyFormat("void foo(int * // this is the first paramters\n"
9847                "         ,\n"
9848                "         int second);");
9849   verifyFormat("double term = a * // first\n"
9850                "              b;");
9851   verifyFormat(
9852       "int *MyValues = {\n"
9853       "    *A, // Operator detection might be confused by the '{'\n"
9854       "    *BB // Operator detection might be confused by previous comment\n"
9855       "};");
9856 
9857   verifyIndependentOfContext("if (int *a = &b)");
9858   verifyIndependentOfContext("if (int &a = *b)");
9859   verifyIndependentOfContext("if (a & b[i])");
9860   verifyIndependentOfContext("if constexpr (a & b[i])");
9861   verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
9862   verifyIndependentOfContext("if (a * (b * c))");
9863   verifyIndependentOfContext("if constexpr (a * (b * c))");
9864   verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
9865   verifyIndependentOfContext("if (a::b::c::d & b[i])");
9866   verifyIndependentOfContext("if (*b[i])");
9867   verifyIndependentOfContext("if (int *a = (&b))");
9868   verifyIndependentOfContext("while (int *a = &b)");
9869   verifyIndependentOfContext("while (a * (b * c))");
9870   verifyIndependentOfContext("size = sizeof *a;");
9871   verifyIndependentOfContext("if (a && (b = c))");
9872   verifyFormat("void f() {\n"
9873                "  for (const int &v : Values) {\n"
9874                "  }\n"
9875                "}");
9876   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
9877   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
9878   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
9879 
9880   verifyFormat("#define A (!a * b)");
9881   verifyFormat("#define MACRO     \\\n"
9882                "  int *i = a * b; \\\n"
9883                "  void f(a *b);",
9884                getLLVMStyleWithColumns(19));
9885 
9886   verifyIndependentOfContext("A = new SomeType *[Length];");
9887   verifyIndependentOfContext("A = new SomeType *[Length]();");
9888   verifyIndependentOfContext("T **t = new T *;");
9889   verifyIndependentOfContext("T **t = new T *();");
9890   verifyGoogleFormat("A = new SomeType*[Length]();");
9891   verifyGoogleFormat("A = new SomeType*[Length];");
9892   verifyGoogleFormat("T** t = new T*;");
9893   verifyGoogleFormat("T** t = new T*();");
9894 
9895   verifyFormat("STATIC_ASSERT((a & b) == 0);");
9896   verifyFormat("STATIC_ASSERT(0 == (a & b));");
9897   verifyFormat("template <bool a, bool b> "
9898                "typename t::if<x && y>::type f() {}");
9899   verifyFormat("template <int *y> f() {}");
9900   verifyFormat("vector<int *> v;");
9901   verifyFormat("vector<int *const> v;");
9902   verifyFormat("vector<int *const **const *> v;");
9903   verifyFormat("vector<int *volatile> v;");
9904   verifyFormat("vector<a *_Nonnull> v;");
9905   verifyFormat("vector<a *_Nullable> v;");
9906   verifyFormat("vector<a *_Null_unspecified> v;");
9907   verifyFormat("vector<a *__ptr32> v;");
9908   verifyFormat("vector<a *__ptr64> v;");
9909   verifyFormat("vector<a *__capability> v;");
9910   FormatStyle TypeMacros = getLLVMStyle();
9911   TypeMacros.TypenameMacros = {"LIST"};
9912   verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros);
9913   verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros);
9914   verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros);
9915   verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros);
9916   verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication
9917 
9918   FormatStyle CustomQualifier = getLLVMStyle();
9919   // Add identifiers that should not be parsed as a qualifier by default.
9920   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
9921   CustomQualifier.AttributeMacros.push_back("_My_qualifier");
9922   CustomQualifier.AttributeMacros.push_back("my_other_qualifier");
9923   verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
9924   verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier);
9925   verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
9926   verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier);
9927   verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
9928   verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier);
9929   verifyFormat("vector<a * _NotAQualifier> v;");
9930   verifyFormat("vector<a * __not_a_qualifier> v;");
9931   verifyFormat("vector<a * b> v;");
9932   verifyFormat("foo<b && false>();");
9933   verifyFormat("foo<b & 1>();");
9934   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
9935   verifyFormat("typeof(*::std::declval<const T &>()) void F();");
9936   verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
9937   verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
9938   verifyFormat(
9939       "template <class T, class = typename std::enable_if<\n"
9940       "                       std::is_integral<T>::value &&\n"
9941       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
9942       "void F();",
9943       getLLVMStyleWithColumns(70));
9944   verifyFormat("template <class T,\n"
9945                "          class = typename std::enable_if<\n"
9946                "              std::is_integral<T>::value &&\n"
9947                "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
9948                "          class U>\n"
9949                "void F();",
9950                getLLVMStyleWithColumns(70));
9951   verifyFormat(
9952       "template <class T,\n"
9953       "          class = typename ::std::enable_if<\n"
9954       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
9955       "void F();",
9956       getGoogleStyleWithColumns(68));
9957 
9958   verifyIndependentOfContext("MACRO(int *i);");
9959   verifyIndependentOfContext("MACRO(auto *a);");
9960   verifyIndependentOfContext("MACRO(const A *a);");
9961   verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
9962   verifyIndependentOfContext("MACRO(decltype(A) *a);");
9963   verifyIndependentOfContext("MACRO(typeof(A) *a);");
9964   verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
9965   verifyIndependentOfContext("MACRO(A *const a);");
9966   verifyIndependentOfContext("MACRO(A *restrict a);");
9967   verifyIndependentOfContext("MACRO(A *__restrict__ a);");
9968   verifyIndependentOfContext("MACRO(A *__restrict a);");
9969   verifyIndependentOfContext("MACRO(A *volatile a);");
9970   verifyIndependentOfContext("MACRO(A *__volatile a);");
9971   verifyIndependentOfContext("MACRO(A *__volatile__ a);");
9972   verifyIndependentOfContext("MACRO(A *_Nonnull a);");
9973   verifyIndependentOfContext("MACRO(A *_Nullable a);");
9974   verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
9975   verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
9976   verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
9977   verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
9978   verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
9979   verifyIndependentOfContext("MACRO(A *__ptr32 a);");
9980   verifyIndependentOfContext("MACRO(A *__ptr64 a);");
9981   verifyIndependentOfContext("MACRO(A *__capability);");
9982   verifyIndependentOfContext("MACRO(A &__capability);");
9983   verifyFormat("MACRO(A *__my_qualifier);");               // type declaration
9984   verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
9985   // If we add __my_qualifier to AttributeMacros it should always be parsed as
9986   // a type declaration:
9987   verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier);
9988   verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier);
9989   // Also check that TypenameMacros prevents parsing it as multiplication:
9990   verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
9991   verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type
9992 
9993   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
9994   verifyFormat("void f() { f(float{1}, a * a); }");
9995   verifyFormat("void f() { f(float(1), a * a); }");
9996 
9997   verifyFormat("f((void (*)(int))g);");
9998   verifyFormat("f((void (&)(int))g);");
9999   verifyFormat("f((void (^)(int))g);");
10000 
10001   // FIXME: Is there a way to make this work?
10002   // verifyIndependentOfContext("MACRO(A *a);");
10003   verifyFormat("MACRO(A &B);");
10004   verifyFormat("MACRO(A *B);");
10005   verifyFormat("void f() { MACRO(A * B); }");
10006   verifyFormat("void f() { MACRO(A & B); }");
10007 
10008   // This lambda was mis-formatted after D88956 (treating it as a binop):
10009   verifyFormat("auto x = [](const decltype(x) &ptr) {};");
10010   verifyFormat("auto x = [](const decltype(x) *ptr) {};");
10011   verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
10012   verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
10013 
10014   verifyFormat("DatumHandle const *operator->() const { return input_; }");
10015   verifyFormat("return options != nullptr && operator==(*options);");
10016 
10017   EXPECT_EQ("#define OP(x)                                    \\\n"
10018             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
10019             "    return s << a.DebugString();                 \\\n"
10020             "  }",
10021             format("#define OP(x) \\\n"
10022                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
10023                    "    return s << a.DebugString(); \\\n"
10024                    "  }",
10025                    getLLVMStyleWithColumns(50)));
10026 
10027   // FIXME: We cannot handle this case yet; we might be able to figure out that
10028   // foo<x> d > v; doesn't make sense.
10029   verifyFormat("foo<a<b && c> d> v;");
10030 
10031   FormatStyle PointerMiddle = getLLVMStyle();
10032   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
10033   verifyFormat("delete *x;", PointerMiddle);
10034   verifyFormat("int * x;", PointerMiddle);
10035   verifyFormat("int *[] x;", PointerMiddle);
10036   verifyFormat("template <int * y> f() {}", PointerMiddle);
10037   verifyFormat("int * f(int * a) {}", PointerMiddle);
10038   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
10039   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
10040   verifyFormat("A<int *> a;", PointerMiddle);
10041   verifyFormat("A<int **> a;", PointerMiddle);
10042   verifyFormat("A<int *, int *> a;", PointerMiddle);
10043   verifyFormat("A<int *[]> a;", PointerMiddle);
10044   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
10045   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
10046   verifyFormat("T ** t = new T *;", PointerMiddle);
10047 
10048   // Member function reference qualifiers aren't binary operators.
10049   verifyFormat("string // break\n"
10050                "operator()() & {}");
10051   verifyFormat("string // break\n"
10052                "operator()() && {}");
10053   verifyGoogleFormat("template <typename T>\n"
10054                      "auto x() & -> int {}");
10055 
10056   // Should be binary operators when used as an argument expression (overloaded
10057   // operator invoked as a member function).
10058   verifyFormat("void f() { a.operator()(a * a); }");
10059   verifyFormat("void f() { a->operator()(a & a); }");
10060   verifyFormat("void f() { a.operator()(*a & *a); }");
10061   verifyFormat("void f() { a->operator()(*a * *a); }");
10062 
10063   verifyFormat("int operator()(T (&&)[N]) { return 1; }");
10064   verifyFormat("int operator()(T (&)[N]) { return 0; }");
10065 }
10066 
10067 TEST_F(FormatTest, UnderstandsAttributes) {
10068   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
10069   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
10070                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
10071   verifyFormat("__attribute__((nodebug)) ::qualified_type f();");
10072   FormatStyle AfterType = getLLVMStyle();
10073   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
10074   verifyFormat("__attribute__((nodebug)) void\n"
10075                "foo() {}\n",
10076                AfterType);
10077   verifyFormat("__unused void\n"
10078                "foo() {}",
10079                AfterType);
10080 
10081   FormatStyle CustomAttrs = getLLVMStyle();
10082   CustomAttrs.AttributeMacros.push_back("__unused");
10083   CustomAttrs.AttributeMacros.push_back("__attr1");
10084   CustomAttrs.AttributeMacros.push_back("__attr2");
10085   CustomAttrs.AttributeMacros.push_back("no_underscore_attr");
10086   verifyFormat("vector<SomeType *__attribute((foo))> v;");
10087   verifyFormat("vector<SomeType *__attribute__((foo))> v;");
10088   verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
10089   // Check that it is parsed as a multiplication without AttributeMacros and
10090   // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
10091   verifyFormat("vector<SomeType * __attr1> v;");
10092   verifyFormat("vector<SomeType __attr1 *> v;");
10093   verifyFormat("vector<SomeType __attr1 *const> v;");
10094   verifyFormat("vector<SomeType __attr1 * __attr2> v;");
10095   verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs);
10096   verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs);
10097   verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs);
10098   verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs);
10099   verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs);
10100   verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs);
10101   verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs);
10102 
10103   // Check that these are not parsed as function declarations:
10104   CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10105   CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;
10106   verifyFormat("SomeType s(InitValue);", CustomAttrs);
10107   verifyFormat("SomeType s{InitValue};", CustomAttrs);
10108   verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs);
10109   verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs);
10110   verifyFormat("SomeType s __unused(InitValue);", CustomAttrs);
10111   verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
10112   verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
10113   verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
10114 }
10115 
10116 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
10117   // Check that qualifiers on pointers don't break parsing of casts.
10118   verifyFormat("x = (foo *const)*v;");
10119   verifyFormat("x = (foo *volatile)*v;");
10120   verifyFormat("x = (foo *restrict)*v;");
10121   verifyFormat("x = (foo *__attribute__((foo)))*v;");
10122   verifyFormat("x = (foo *_Nonnull)*v;");
10123   verifyFormat("x = (foo *_Nullable)*v;");
10124   verifyFormat("x = (foo *_Null_unspecified)*v;");
10125   verifyFormat("x = (foo *_Nonnull)*v;");
10126   verifyFormat("x = (foo *[[clang::attr]])*v;");
10127   verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
10128   verifyFormat("x = (foo *__ptr32)*v;");
10129   verifyFormat("x = (foo *__ptr64)*v;");
10130   verifyFormat("x = (foo *__capability)*v;");
10131 
10132   // Check that we handle multiple trailing qualifiers and skip them all to
10133   // determine that the expression is a cast to a pointer type.
10134   FormatStyle LongPointerRight = getLLVMStyleWithColumns(999);
10135   FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999);
10136   LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left;
10137   StringRef AllQualifiers =
10138       "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
10139       "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability";
10140   verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight);
10141   verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft);
10142 
10143   // Also check that address-of is not parsed as a binary bitwise-and:
10144   verifyFormat("x = (foo *const)&v;");
10145   verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight);
10146   verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft);
10147 
10148   // Check custom qualifiers:
10149   FormatStyle CustomQualifier = getLLVMStyleWithColumns(999);
10150   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
10151   verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
10152   verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier);
10153   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(),
10154                CustomQualifier);
10155   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(),
10156                CustomQualifier);
10157 
10158   // Check that unknown identifiers result in binary operator parsing:
10159   verifyFormat("x = (foo * __unknown_qualifier) * v;");
10160   verifyFormat("x = (foo * __unknown_qualifier) & v;");
10161 }
10162 
10163 TEST_F(FormatTest, UnderstandsSquareAttributes) {
10164   verifyFormat("SomeType s [[unused]] (InitValue);");
10165   verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
10166   verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
10167   verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
10168   verifyFormat("void f() [[deprecated(\"so sorry\")]];");
10169   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10170                "    [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
10171   verifyFormat("[[nodiscard]] bool f() { return false; }");
10172   verifyFormat("class [[nodiscard]] f {\npublic:\n  f() {}\n}");
10173   verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n  f() {}\n}");
10174   verifyFormat("class [[gnu::unused]] f {\npublic:\n  f() {}\n}");
10175   verifyFormat("[[nodiscard]] ::qualified_type f();");
10176 
10177   // Make sure we do not mistake attributes for array subscripts.
10178   verifyFormat("int a() {}\n"
10179                "[[unused]] int b() {}\n");
10180   verifyFormat("NSArray *arr;\n"
10181                "arr[[Foo() bar]];");
10182 
10183   // On the other hand, we still need to correctly find array subscripts.
10184   verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
10185 
10186   // Make sure that we do not mistake Objective-C method inside array literals
10187   // as attributes, even if those method names are also keywords.
10188   verifyFormat("@[ [foo bar] ];");
10189   verifyFormat("@[ [NSArray class] ];");
10190   verifyFormat("@[ [foo enum] ];");
10191 
10192   verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
10193 
10194   // Make sure we do not parse attributes as lambda introducers.
10195   FormatStyle MultiLineFunctions = getLLVMStyle();
10196   MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10197   verifyFormat("[[unused]] int b() {\n"
10198                "  return 42;\n"
10199                "}\n",
10200                MultiLineFunctions);
10201 }
10202 
10203 TEST_F(FormatTest, AttributeClass) {
10204   FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
10205   verifyFormat("class S {\n"
10206                "  S(S&&) = default;\n"
10207                "};",
10208                Style);
10209   verifyFormat("class [[nodiscard]] S {\n"
10210                "  S(S&&) = default;\n"
10211                "};",
10212                Style);
10213   verifyFormat("class __attribute((maybeunused)) S {\n"
10214                "  S(S&&) = default;\n"
10215                "};",
10216                Style);
10217   verifyFormat("struct S {\n"
10218                "  S(S&&) = default;\n"
10219                "};",
10220                Style);
10221   verifyFormat("struct [[nodiscard]] S {\n"
10222                "  S(S&&) = default;\n"
10223                "};",
10224                Style);
10225 }
10226 
10227 TEST_F(FormatTest, AttributesAfterMacro) {
10228   FormatStyle Style = getLLVMStyle();
10229   verifyFormat("MACRO;\n"
10230                "__attribute__((maybe_unused)) int foo() {\n"
10231                "  //...\n"
10232                "}");
10233 
10234   verifyFormat("MACRO;\n"
10235                "[[nodiscard]] int foo() {\n"
10236                "  //...\n"
10237                "}");
10238 
10239   EXPECT_EQ("MACRO\n\n"
10240             "__attribute__((maybe_unused)) int foo() {\n"
10241             "  //...\n"
10242             "}",
10243             format("MACRO\n\n"
10244                    "__attribute__((maybe_unused)) int foo() {\n"
10245                    "  //...\n"
10246                    "}"));
10247 
10248   EXPECT_EQ("MACRO\n\n"
10249             "[[nodiscard]] int foo() {\n"
10250             "  //...\n"
10251             "}",
10252             format("MACRO\n\n"
10253                    "[[nodiscard]] int foo() {\n"
10254                    "  //...\n"
10255                    "}"));
10256 }
10257 
10258 TEST_F(FormatTest, AttributePenaltyBreaking) {
10259   FormatStyle Style = getLLVMStyle();
10260   verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
10261                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
10262                Style);
10263   verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
10264                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
10265                Style);
10266   verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
10267                "shared_ptr<ALongTypeName> &C d) {\n}",
10268                Style);
10269 }
10270 
10271 TEST_F(FormatTest, UnderstandsEllipsis) {
10272   FormatStyle Style = getLLVMStyle();
10273   verifyFormat("int printf(const char *fmt, ...);");
10274   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
10275   verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
10276 
10277   verifyFormat("template <int *...PP> a;", Style);
10278 
10279   Style.PointerAlignment = FormatStyle::PAS_Left;
10280   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style);
10281 
10282   verifyFormat("template <int*... PP> a;", Style);
10283 
10284   Style.PointerAlignment = FormatStyle::PAS_Middle;
10285   verifyFormat("template <int *... PP> a;", Style);
10286 }
10287 
10288 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
10289   EXPECT_EQ("int *a;\n"
10290             "int *a;\n"
10291             "int *a;",
10292             format("int *a;\n"
10293                    "int* a;\n"
10294                    "int *a;",
10295                    getGoogleStyle()));
10296   EXPECT_EQ("int* a;\n"
10297             "int* a;\n"
10298             "int* a;",
10299             format("int* a;\n"
10300                    "int* a;\n"
10301                    "int *a;",
10302                    getGoogleStyle()));
10303   EXPECT_EQ("int *a;\n"
10304             "int *a;\n"
10305             "int *a;",
10306             format("int *a;\n"
10307                    "int * a;\n"
10308                    "int *  a;",
10309                    getGoogleStyle()));
10310   EXPECT_EQ("auto x = [] {\n"
10311             "  int *a;\n"
10312             "  int *a;\n"
10313             "  int *a;\n"
10314             "};",
10315             format("auto x=[]{int *a;\n"
10316                    "int * a;\n"
10317                    "int *  a;};",
10318                    getGoogleStyle()));
10319 }
10320 
10321 TEST_F(FormatTest, UnderstandsRvalueReferences) {
10322   verifyFormat("int f(int &&a) {}");
10323   verifyFormat("int f(int a, char &&b) {}");
10324   verifyFormat("void f() { int &&a = b; }");
10325   verifyGoogleFormat("int f(int a, char&& b) {}");
10326   verifyGoogleFormat("void f() { int&& a = b; }");
10327 
10328   verifyIndependentOfContext("A<int &&> a;");
10329   verifyIndependentOfContext("A<int &&, int &&> a;");
10330   verifyGoogleFormat("A<int&&> a;");
10331   verifyGoogleFormat("A<int&&, int&&> a;");
10332 
10333   // Not rvalue references:
10334   verifyFormat("template <bool B, bool C> class A {\n"
10335                "  static_assert(B && C, \"Something is wrong\");\n"
10336                "};");
10337   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
10338   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
10339   verifyFormat("#define A(a, b) (a && b)");
10340 }
10341 
10342 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
10343   verifyFormat("void f() {\n"
10344                "  x[aaaaaaaaa -\n"
10345                "    b] = 23;\n"
10346                "}",
10347                getLLVMStyleWithColumns(15));
10348 }
10349 
10350 TEST_F(FormatTest, FormatsCasts) {
10351   verifyFormat("Type *A = static_cast<Type *>(P);");
10352   verifyFormat("Type *A = (Type *)P;");
10353   verifyFormat("Type *A = (vector<Type *, int *>)P;");
10354   verifyFormat("int a = (int)(2.0f);");
10355   verifyFormat("int a = (int)2.0f;");
10356   verifyFormat("x[(int32)y];");
10357   verifyFormat("x = (int32)y;");
10358   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
10359   verifyFormat("int a = (int)*b;");
10360   verifyFormat("int a = (int)2.0f;");
10361   verifyFormat("int a = (int)~0;");
10362   verifyFormat("int a = (int)++a;");
10363   verifyFormat("int a = (int)sizeof(int);");
10364   verifyFormat("int a = (int)+2;");
10365   verifyFormat("my_int a = (my_int)2.0f;");
10366   verifyFormat("my_int a = (my_int)sizeof(int);");
10367   verifyFormat("return (my_int)aaa;");
10368   verifyFormat("#define x ((int)-1)");
10369   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
10370   verifyFormat("#define p(q) ((int *)&q)");
10371   verifyFormat("fn(a)(b) + 1;");
10372 
10373   verifyFormat("void f() { my_int a = (my_int)*b; }");
10374   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
10375   verifyFormat("my_int a = (my_int)~0;");
10376   verifyFormat("my_int a = (my_int)++a;");
10377   verifyFormat("my_int a = (my_int)-2;");
10378   verifyFormat("my_int a = (my_int)1;");
10379   verifyFormat("my_int a = (my_int *)1;");
10380   verifyFormat("my_int a = (const my_int)-1;");
10381   verifyFormat("my_int a = (const my_int *)-1;");
10382   verifyFormat("my_int a = (my_int)(my_int)-1;");
10383   verifyFormat("my_int a = (ns::my_int)-2;");
10384   verifyFormat("case (my_int)ONE:");
10385   verifyFormat("auto x = (X)this;");
10386   // Casts in Obj-C style calls used to not be recognized as such.
10387   verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle());
10388 
10389   // FIXME: single value wrapped with paren will be treated as cast.
10390   verifyFormat("void f(int i = (kValue)*kMask) {}");
10391 
10392   verifyFormat("{ (void)F; }");
10393 
10394   // Don't break after a cast's
10395   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10396                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
10397                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
10398 
10399   verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)");
10400   verifyFormat("#define CONF_BOOL(x) (bool *)(x)");
10401   verifyFormat("#define CONF_BOOL(x) (bool)(x)");
10402   verifyFormat("bool *y = (bool *)(void *)(x);");
10403   verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)");
10404   verifyFormat("bool *y = (bool *)(void *)(int)(x);");
10405   verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)");
10406   verifyFormat("bool *y = (bool *)(void *)(int)foo(x);");
10407 
10408   // These are not casts.
10409   verifyFormat("void f(int *) {}");
10410   verifyFormat("f(foo)->b;");
10411   verifyFormat("f(foo).b;");
10412   verifyFormat("f(foo)(b);");
10413   verifyFormat("f(foo)[b];");
10414   verifyFormat("[](foo) { return 4; }(bar);");
10415   verifyFormat("(*funptr)(foo)[4];");
10416   verifyFormat("funptrs[4](foo)[4];");
10417   verifyFormat("void f(int *);");
10418   verifyFormat("void f(int *) = 0;");
10419   verifyFormat("void f(SmallVector<int>) {}");
10420   verifyFormat("void f(SmallVector<int>);");
10421   verifyFormat("void f(SmallVector<int>) = 0;");
10422   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
10423   verifyFormat("int a = sizeof(int) * b;");
10424   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
10425   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
10426   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
10427   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
10428 
10429   // These are not casts, but at some point were confused with casts.
10430   verifyFormat("virtual void foo(int *) override;");
10431   verifyFormat("virtual void foo(char &) const;");
10432   verifyFormat("virtual void foo(int *a, char *) const;");
10433   verifyFormat("int a = sizeof(int *) + b;");
10434   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
10435   verifyFormat("bool b = f(g<int>) && c;");
10436   verifyFormat("typedef void (*f)(int i) func;");
10437   verifyFormat("void operator++(int) noexcept;");
10438   verifyFormat("void operator++(int &) noexcept;");
10439   verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
10440                "&) noexcept;");
10441   verifyFormat(
10442       "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
10443   verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
10444   verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
10445   verifyFormat("void operator delete(nothrow_t &) noexcept;");
10446   verifyFormat("void operator delete(foo &) noexcept;");
10447   verifyFormat("void operator delete(foo) noexcept;");
10448   verifyFormat("void operator delete(int) noexcept;");
10449   verifyFormat("void operator delete(int &) noexcept;");
10450   verifyFormat("void operator delete(int &) volatile noexcept;");
10451   verifyFormat("void operator delete(int &) const");
10452   verifyFormat("void operator delete(int &) = default");
10453   verifyFormat("void operator delete(int &) = delete");
10454   verifyFormat("void operator delete(int &) [[noreturn]]");
10455   verifyFormat("void operator delete(int &) throw();");
10456   verifyFormat("void operator delete(int &) throw(int);");
10457   verifyFormat("auto operator delete(int &) -> int;");
10458   verifyFormat("auto operator delete(int &) override");
10459   verifyFormat("auto operator delete(int &) final");
10460 
10461   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
10462                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10463   // FIXME: The indentation here is not ideal.
10464   verifyFormat(
10465       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10466       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
10467       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
10468 }
10469 
10470 TEST_F(FormatTest, FormatsFunctionTypes) {
10471   verifyFormat("A<bool()> a;");
10472   verifyFormat("A<SomeType()> a;");
10473   verifyFormat("A<void (*)(int, std::string)> a;");
10474   verifyFormat("A<void *(int)>;");
10475   verifyFormat("void *(*a)(int *, SomeType *);");
10476   verifyFormat("int (*func)(void *);");
10477   verifyFormat("void f() { int (*func)(void *); }");
10478   verifyFormat("template <class CallbackClass>\n"
10479                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
10480 
10481   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
10482   verifyGoogleFormat("void* (*a)(int);");
10483   verifyGoogleFormat(
10484       "template <class CallbackClass>\n"
10485       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
10486 
10487   // Other constructs can look somewhat like function types:
10488   verifyFormat("A<sizeof(*x)> a;");
10489   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
10490   verifyFormat("some_var = function(*some_pointer_var)[0];");
10491   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
10492   verifyFormat("int x = f(&h)();");
10493   verifyFormat("returnsFunction(&param1, &param2)(param);");
10494   verifyFormat("std::function<\n"
10495                "    LooooooooooongTemplatedType<\n"
10496                "        SomeType>*(\n"
10497                "        LooooooooooooooooongType type)>\n"
10498                "    function;",
10499                getGoogleStyleWithColumns(40));
10500 }
10501 
10502 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
10503   verifyFormat("A (*foo_)[6];");
10504   verifyFormat("vector<int> (*foo_)[6];");
10505 }
10506 
10507 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
10508   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10509                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
10510   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
10511                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
10512   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10513                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
10514 
10515   // Different ways of ()-initializiation.
10516   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10517                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
10518   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10519                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
10520   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10521                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
10522   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
10523                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
10524 
10525   // Lambdas should not confuse the variable declaration heuristic.
10526   verifyFormat("LooooooooooooooooongType\n"
10527                "    variable(nullptr, [](A *a) {});",
10528                getLLVMStyleWithColumns(40));
10529 }
10530 
10531 TEST_F(FormatTest, BreaksLongDeclarations) {
10532   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
10533                "    AnotherNameForTheLongType;");
10534   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
10535                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10536   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10537                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
10538   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
10539                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
10540   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10541                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10542   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
10543                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10544   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
10545                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10546   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
10547                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10548   verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
10549                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10550   verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
10551                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10552   verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
10553                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
10554   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10555                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
10556   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10557                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
10558   FormatStyle Indented = getLLVMStyle();
10559   Indented.IndentWrappedFunctionNames = true;
10560   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10561                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
10562                Indented);
10563   verifyFormat(
10564       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
10565       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10566       Indented);
10567   verifyFormat(
10568       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
10569       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10570       Indented);
10571   verifyFormat(
10572       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
10573       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
10574       Indented);
10575 
10576   // FIXME: Without the comment, this breaks after "(".
10577   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
10578                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
10579                getGoogleStyle());
10580 
10581   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
10582                "                  int LoooooooooooooooooooongParam2) {}");
10583   verifyFormat(
10584       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
10585       "                                   SourceLocation L, IdentifierIn *II,\n"
10586       "                                   Type *T) {}");
10587   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
10588                "ReallyReaaallyLongFunctionName(\n"
10589                "    const std::string &SomeParameter,\n"
10590                "    const SomeType<string, SomeOtherTemplateParameter>\n"
10591                "        &ReallyReallyLongParameterName,\n"
10592                "    const SomeType<string, SomeOtherTemplateParameter>\n"
10593                "        &AnotherLongParameterName) {}");
10594   verifyFormat("template <typename A>\n"
10595                "SomeLoooooooooooooooooooooongType<\n"
10596                "    typename some_namespace::SomeOtherType<A>::Type>\n"
10597                "Function() {}");
10598 
10599   verifyGoogleFormat(
10600       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
10601       "    aaaaaaaaaaaaaaaaaaaaaaa;");
10602   verifyGoogleFormat(
10603       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
10604       "                                   SourceLocation L) {}");
10605   verifyGoogleFormat(
10606       "some_namespace::LongReturnType\n"
10607       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
10608       "    int first_long_parameter, int second_parameter) {}");
10609 
10610   verifyGoogleFormat("template <typename T>\n"
10611                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
10612                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
10613   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10614                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
10615 
10616   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
10617                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10618                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10619   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10620                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
10621                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
10622   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10623                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
10624                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
10625                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10626 
10627   verifyFormat("template <typename T> // Templates on own line.\n"
10628                "static int            // Some comment.\n"
10629                "MyFunction(int a);",
10630                getLLVMStyle());
10631 }
10632 
10633 TEST_F(FormatTest, FormatsAccessModifiers) {
10634   FormatStyle Style = getLLVMStyle();
10635   EXPECT_EQ(Style.EmptyLineBeforeAccessModifier,
10636             FormatStyle::ELBAMS_LogicalBlock);
10637   verifyFormat("struct foo {\n"
10638                "private:\n"
10639                "  void f() {}\n"
10640                "\n"
10641                "private:\n"
10642                "  int i;\n"
10643                "\n"
10644                "protected:\n"
10645                "  int j;\n"
10646                "};\n",
10647                Style);
10648   verifyFormat("struct foo {\n"
10649                "private:\n"
10650                "  void f() {}\n"
10651                "\n"
10652                "private:\n"
10653                "  int i;\n"
10654                "\n"
10655                "protected:\n"
10656                "  int j;\n"
10657                "};\n",
10658                "struct foo {\n"
10659                "private:\n"
10660                "  void f() {}\n"
10661                "private:\n"
10662                "  int i;\n"
10663                "protected:\n"
10664                "  int j;\n"
10665                "};\n",
10666                Style);
10667   verifyFormat("struct foo { /* comment */\n"
10668                "private:\n"
10669                "  int i;\n"
10670                "  // comment\n"
10671                "private:\n"
10672                "  int j;\n"
10673                "};\n",
10674                Style);
10675   verifyFormat("struct foo {\n"
10676                "#ifdef FOO\n"
10677                "#endif\n"
10678                "private:\n"
10679                "  int i;\n"
10680                "#ifdef FOO\n"
10681                "private:\n"
10682                "#endif\n"
10683                "  int j;\n"
10684                "};\n",
10685                Style);
10686   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10687   verifyFormat("struct foo {\n"
10688                "private:\n"
10689                "  void f() {}\n"
10690                "private:\n"
10691                "  int i;\n"
10692                "protected:\n"
10693                "  int j;\n"
10694                "};\n",
10695                Style);
10696   verifyFormat("struct foo {\n"
10697                "private:\n"
10698                "  void f() {}\n"
10699                "private:\n"
10700                "  int i;\n"
10701                "protected:\n"
10702                "  int j;\n"
10703                "};\n",
10704                "struct foo {\n"
10705                "\n"
10706                "private:\n"
10707                "  void f() {}\n"
10708                "\n"
10709                "private:\n"
10710                "  int i;\n"
10711                "\n"
10712                "protected:\n"
10713                "  int j;\n"
10714                "};\n",
10715                Style);
10716   verifyFormat("struct foo { /* comment */\n"
10717                "private:\n"
10718                "  int i;\n"
10719                "  // comment\n"
10720                "private:\n"
10721                "  int j;\n"
10722                "};\n",
10723                "struct foo { /* comment */\n"
10724                "\n"
10725                "private:\n"
10726                "  int i;\n"
10727                "  // comment\n"
10728                "\n"
10729                "private:\n"
10730                "  int j;\n"
10731                "};\n",
10732                Style);
10733   verifyFormat("struct foo {\n"
10734                "#ifdef FOO\n"
10735                "#endif\n"
10736                "private:\n"
10737                "  int i;\n"
10738                "#ifdef FOO\n"
10739                "private:\n"
10740                "#endif\n"
10741                "  int j;\n"
10742                "};\n",
10743                "struct foo {\n"
10744                "#ifdef FOO\n"
10745                "#endif\n"
10746                "\n"
10747                "private:\n"
10748                "  int i;\n"
10749                "#ifdef FOO\n"
10750                "\n"
10751                "private:\n"
10752                "#endif\n"
10753                "  int j;\n"
10754                "};\n",
10755                Style);
10756   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10757   verifyFormat("struct foo {\n"
10758                "private:\n"
10759                "  void f() {}\n"
10760                "\n"
10761                "private:\n"
10762                "  int i;\n"
10763                "\n"
10764                "protected:\n"
10765                "  int j;\n"
10766                "};\n",
10767                Style);
10768   verifyFormat("struct foo {\n"
10769                "private:\n"
10770                "  void f() {}\n"
10771                "\n"
10772                "private:\n"
10773                "  int i;\n"
10774                "\n"
10775                "protected:\n"
10776                "  int j;\n"
10777                "};\n",
10778                "struct foo {\n"
10779                "private:\n"
10780                "  void f() {}\n"
10781                "private:\n"
10782                "  int i;\n"
10783                "protected:\n"
10784                "  int j;\n"
10785                "};\n",
10786                Style);
10787   verifyFormat("struct foo { /* comment */\n"
10788                "private:\n"
10789                "  int i;\n"
10790                "  // comment\n"
10791                "\n"
10792                "private:\n"
10793                "  int j;\n"
10794                "};\n",
10795                "struct foo { /* comment */\n"
10796                "private:\n"
10797                "  int i;\n"
10798                "  // comment\n"
10799                "\n"
10800                "private:\n"
10801                "  int j;\n"
10802                "};\n",
10803                Style);
10804   verifyFormat("struct foo {\n"
10805                "#ifdef FOO\n"
10806                "#endif\n"
10807                "\n"
10808                "private:\n"
10809                "  int i;\n"
10810                "#ifdef FOO\n"
10811                "\n"
10812                "private:\n"
10813                "#endif\n"
10814                "  int j;\n"
10815                "};\n",
10816                "struct foo {\n"
10817                "#ifdef FOO\n"
10818                "#endif\n"
10819                "private:\n"
10820                "  int i;\n"
10821                "#ifdef FOO\n"
10822                "private:\n"
10823                "#endif\n"
10824                "  int j;\n"
10825                "};\n",
10826                Style);
10827   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10828   EXPECT_EQ("struct foo {\n"
10829             "\n"
10830             "private:\n"
10831             "  void f() {}\n"
10832             "\n"
10833             "private:\n"
10834             "  int i;\n"
10835             "\n"
10836             "protected:\n"
10837             "  int j;\n"
10838             "};\n",
10839             format("struct foo {\n"
10840                    "\n"
10841                    "private:\n"
10842                    "  void f() {}\n"
10843                    "\n"
10844                    "private:\n"
10845                    "  int i;\n"
10846                    "\n"
10847                    "protected:\n"
10848                    "  int j;\n"
10849                    "};\n",
10850                    Style));
10851   verifyFormat("struct foo {\n"
10852                "private:\n"
10853                "  void f() {}\n"
10854                "private:\n"
10855                "  int i;\n"
10856                "protected:\n"
10857                "  int j;\n"
10858                "};\n",
10859                Style);
10860   EXPECT_EQ("struct foo { /* comment */\n"
10861             "\n"
10862             "private:\n"
10863             "  int i;\n"
10864             "  // comment\n"
10865             "\n"
10866             "private:\n"
10867             "  int j;\n"
10868             "};\n",
10869             format("struct foo { /* comment */\n"
10870                    "\n"
10871                    "private:\n"
10872                    "  int i;\n"
10873                    "  // comment\n"
10874                    "\n"
10875                    "private:\n"
10876                    "  int j;\n"
10877                    "};\n",
10878                    Style));
10879   verifyFormat("struct foo { /* comment */\n"
10880                "private:\n"
10881                "  int i;\n"
10882                "  // comment\n"
10883                "private:\n"
10884                "  int j;\n"
10885                "};\n",
10886                Style);
10887   EXPECT_EQ("struct foo {\n"
10888             "#ifdef FOO\n"
10889             "#endif\n"
10890             "\n"
10891             "private:\n"
10892             "  int i;\n"
10893             "#ifdef FOO\n"
10894             "\n"
10895             "private:\n"
10896             "#endif\n"
10897             "  int j;\n"
10898             "};\n",
10899             format("struct foo {\n"
10900                    "#ifdef FOO\n"
10901                    "#endif\n"
10902                    "\n"
10903                    "private:\n"
10904                    "  int i;\n"
10905                    "#ifdef FOO\n"
10906                    "\n"
10907                    "private:\n"
10908                    "#endif\n"
10909                    "  int j;\n"
10910                    "};\n",
10911                    Style));
10912   verifyFormat("struct foo {\n"
10913                "#ifdef FOO\n"
10914                "#endif\n"
10915                "private:\n"
10916                "  int i;\n"
10917                "#ifdef FOO\n"
10918                "private:\n"
10919                "#endif\n"
10920                "  int j;\n"
10921                "};\n",
10922                Style);
10923 
10924   FormatStyle NoEmptyLines = getLLVMStyle();
10925   NoEmptyLines.MaxEmptyLinesToKeep = 0;
10926   verifyFormat("struct foo {\n"
10927                "private:\n"
10928                "  void f() {}\n"
10929                "\n"
10930                "private:\n"
10931                "  int i;\n"
10932                "\n"
10933                "public:\n"
10934                "protected:\n"
10935                "  int j;\n"
10936                "};\n",
10937                NoEmptyLines);
10938 
10939   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10940   verifyFormat("struct foo {\n"
10941                "private:\n"
10942                "  void f() {}\n"
10943                "private:\n"
10944                "  int i;\n"
10945                "public:\n"
10946                "protected:\n"
10947                "  int j;\n"
10948                "};\n",
10949                NoEmptyLines);
10950 
10951   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10952   verifyFormat("struct foo {\n"
10953                "private:\n"
10954                "  void f() {}\n"
10955                "\n"
10956                "private:\n"
10957                "  int i;\n"
10958                "\n"
10959                "public:\n"
10960                "\n"
10961                "protected:\n"
10962                "  int j;\n"
10963                "};\n",
10964                NoEmptyLines);
10965 }
10966 
10967 TEST_F(FormatTest, FormatsAfterAccessModifiers) {
10968 
10969   FormatStyle Style = getLLVMStyle();
10970   EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never);
10971   verifyFormat("struct foo {\n"
10972                "private:\n"
10973                "  void f() {}\n"
10974                "\n"
10975                "private:\n"
10976                "  int i;\n"
10977                "\n"
10978                "protected:\n"
10979                "  int j;\n"
10980                "};\n",
10981                Style);
10982 
10983   // Check if lines are removed.
10984   verifyFormat("struct foo {\n"
10985                "private:\n"
10986                "  void f() {}\n"
10987                "\n"
10988                "private:\n"
10989                "  int i;\n"
10990                "\n"
10991                "protected:\n"
10992                "  int j;\n"
10993                "};\n",
10994                "struct foo {\n"
10995                "private:\n"
10996                "\n"
10997                "  void f() {}\n"
10998                "\n"
10999                "private:\n"
11000                "\n"
11001                "  int i;\n"
11002                "\n"
11003                "protected:\n"
11004                "\n"
11005                "  int j;\n"
11006                "};\n",
11007                Style);
11008 
11009   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11010   verifyFormat("struct foo {\n"
11011                "private:\n"
11012                "\n"
11013                "  void f() {}\n"
11014                "\n"
11015                "private:\n"
11016                "\n"
11017                "  int i;\n"
11018                "\n"
11019                "protected:\n"
11020                "\n"
11021                "  int j;\n"
11022                "};\n",
11023                Style);
11024 
11025   // Check if lines are added.
11026   verifyFormat("struct foo {\n"
11027                "private:\n"
11028                "\n"
11029                "  void f() {}\n"
11030                "\n"
11031                "private:\n"
11032                "\n"
11033                "  int i;\n"
11034                "\n"
11035                "protected:\n"
11036                "\n"
11037                "  int j;\n"
11038                "};\n",
11039                "struct foo {\n"
11040                "private:\n"
11041                "  void f() {}\n"
11042                "\n"
11043                "private:\n"
11044                "  int i;\n"
11045                "\n"
11046                "protected:\n"
11047                "  int j;\n"
11048                "};\n",
11049                Style);
11050 
11051   // Leave tests rely on the code layout, test::messUp can not be used.
11052   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11053   Style.MaxEmptyLinesToKeep = 0u;
11054   verifyFormat("struct foo {\n"
11055                "private:\n"
11056                "  void f() {}\n"
11057                "\n"
11058                "private:\n"
11059                "  int i;\n"
11060                "\n"
11061                "protected:\n"
11062                "  int j;\n"
11063                "};\n",
11064                Style);
11065 
11066   // Check if MaxEmptyLinesToKeep is respected.
11067   EXPECT_EQ("struct foo {\n"
11068             "private:\n"
11069             "  void f() {}\n"
11070             "\n"
11071             "private:\n"
11072             "  int i;\n"
11073             "\n"
11074             "protected:\n"
11075             "  int j;\n"
11076             "};\n",
11077             format("struct foo {\n"
11078                    "private:\n"
11079                    "\n\n\n"
11080                    "  void f() {}\n"
11081                    "\n"
11082                    "private:\n"
11083                    "\n\n\n"
11084                    "  int i;\n"
11085                    "\n"
11086                    "protected:\n"
11087                    "\n\n\n"
11088                    "  int j;\n"
11089                    "};\n",
11090                    Style));
11091 
11092   Style.MaxEmptyLinesToKeep = 1u;
11093   EXPECT_EQ("struct foo {\n"
11094             "private:\n"
11095             "\n"
11096             "  void f() {}\n"
11097             "\n"
11098             "private:\n"
11099             "\n"
11100             "  int i;\n"
11101             "\n"
11102             "protected:\n"
11103             "\n"
11104             "  int j;\n"
11105             "};\n",
11106             format("struct foo {\n"
11107                    "private:\n"
11108                    "\n"
11109                    "  void f() {}\n"
11110                    "\n"
11111                    "private:\n"
11112                    "\n"
11113                    "  int i;\n"
11114                    "\n"
11115                    "protected:\n"
11116                    "\n"
11117                    "  int j;\n"
11118                    "};\n",
11119                    Style));
11120   // Check if no lines are kept.
11121   EXPECT_EQ("struct foo {\n"
11122             "private:\n"
11123             "  void f() {}\n"
11124             "\n"
11125             "private:\n"
11126             "  int i;\n"
11127             "\n"
11128             "protected:\n"
11129             "  int j;\n"
11130             "};\n",
11131             format("struct foo {\n"
11132                    "private:\n"
11133                    "  void f() {}\n"
11134                    "\n"
11135                    "private:\n"
11136                    "  int i;\n"
11137                    "\n"
11138                    "protected:\n"
11139                    "  int j;\n"
11140                    "};\n",
11141                    Style));
11142   // Check if MaxEmptyLinesToKeep is respected.
11143   EXPECT_EQ("struct foo {\n"
11144             "private:\n"
11145             "\n"
11146             "  void f() {}\n"
11147             "\n"
11148             "private:\n"
11149             "\n"
11150             "  int i;\n"
11151             "\n"
11152             "protected:\n"
11153             "\n"
11154             "  int j;\n"
11155             "};\n",
11156             format("struct foo {\n"
11157                    "private:\n"
11158                    "\n\n\n"
11159                    "  void f() {}\n"
11160                    "\n"
11161                    "private:\n"
11162                    "\n\n\n"
11163                    "  int i;\n"
11164                    "\n"
11165                    "protected:\n"
11166                    "\n\n\n"
11167                    "  int j;\n"
11168                    "};\n",
11169                    Style));
11170 
11171   Style.MaxEmptyLinesToKeep = 10u;
11172   EXPECT_EQ("struct foo {\n"
11173             "private:\n"
11174             "\n\n\n"
11175             "  void f() {}\n"
11176             "\n"
11177             "private:\n"
11178             "\n\n\n"
11179             "  int i;\n"
11180             "\n"
11181             "protected:\n"
11182             "\n\n\n"
11183             "  int j;\n"
11184             "};\n",
11185             format("struct foo {\n"
11186                    "private:\n"
11187                    "\n\n\n"
11188                    "  void f() {}\n"
11189                    "\n"
11190                    "private:\n"
11191                    "\n\n\n"
11192                    "  int i;\n"
11193                    "\n"
11194                    "protected:\n"
11195                    "\n\n\n"
11196                    "  int j;\n"
11197                    "};\n",
11198                    Style));
11199 
11200   // Test with comments.
11201   Style = getLLVMStyle();
11202   verifyFormat("struct foo {\n"
11203                "private:\n"
11204                "  // comment\n"
11205                "  void f() {}\n"
11206                "\n"
11207                "private: /* comment */\n"
11208                "  int i;\n"
11209                "};\n",
11210                Style);
11211   verifyFormat("struct foo {\n"
11212                "private:\n"
11213                "  // comment\n"
11214                "  void f() {}\n"
11215                "\n"
11216                "private: /* comment */\n"
11217                "  int i;\n"
11218                "};\n",
11219                "struct foo {\n"
11220                "private:\n"
11221                "\n"
11222                "  // comment\n"
11223                "  void f() {}\n"
11224                "\n"
11225                "private: /* comment */\n"
11226                "\n"
11227                "  int i;\n"
11228                "};\n",
11229                Style);
11230 
11231   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11232   verifyFormat("struct foo {\n"
11233                "private:\n"
11234                "\n"
11235                "  // comment\n"
11236                "  void f() {}\n"
11237                "\n"
11238                "private: /* comment */\n"
11239                "\n"
11240                "  int i;\n"
11241                "};\n",
11242                "struct foo {\n"
11243                "private:\n"
11244                "  // comment\n"
11245                "  void f() {}\n"
11246                "\n"
11247                "private: /* comment */\n"
11248                "  int i;\n"
11249                "};\n",
11250                Style);
11251   verifyFormat("struct foo {\n"
11252                "private:\n"
11253                "\n"
11254                "  // comment\n"
11255                "  void f() {}\n"
11256                "\n"
11257                "private: /* comment */\n"
11258                "\n"
11259                "  int i;\n"
11260                "};\n",
11261                Style);
11262 
11263   // Test with preprocessor defines.
11264   Style = getLLVMStyle();
11265   verifyFormat("struct foo {\n"
11266                "private:\n"
11267                "#ifdef FOO\n"
11268                "#endif\n"
11269                "  void f() {}\n"
11270                "};\n",
11271                Style);
11272   verifyFormat("struct foo {\n"
11273                "private:\n"
11274                "#ifdef FOO\n"
11275                "#endif\n"
11276                "  void f() {}\n"
11277                "};\n",
11278                "struct foo {\n"
11279                "private:\n"
11280                "\n"
11281                "#ifdef FOO\n"
11282                "#endif\n"
11283                "  void f() {}\n"
11284                "};\n",
11285                Style);
11286 
11287   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11288   verifyFormat("struct foo {\n"
11289                "private:\n"
11290                "\n"
11291                "#ifdef FOO\n"
11292                "#endif\n"
11293                "  void f() {}\n"
11294                "};\n",
11295                "struct foo {\n"
11296                "private:\n"
11297                "#ifdef FOO\n"
11298                "#endif\n"
11299                "  void f() {}\n"
11300                "};\n",
11301                Style);
11302   verifyFormat("struct foo {\n"
11303                "private:\n"
11304                "\n"
11305                "#ifdef FOO\n"
11306                "#endif\n"
11307                "  void f() {}\n"
11308                "};\n",
11309                Style);
11310 }
11311 
11312 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) {
11313   // Combined tests of EmptyLineAfterAccessModifier and
11314   // EmptyLineBeforeAccessModifier.
11315   FormatStyle Style = getLLVMStyle();
11316   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11317   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11318   verifyFormat("struct foo {\n"
11319                "private:\n"
11320                "\n"
11321                "protected:\n"
11322                "};\n",
11323                Style);
11324 
11325   Style.MaxEmptyLinesToKeep = 10u;
11326   // Both remove all new lines.
11327   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11328   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11329   verifyFormat("struct foo {\n"
11330                "private:\n"
11331                "protected:\n"
11332                "};\n",
11333                "struct foo {\n"
11334                "private:\n"
11335                "\n\n\n"
11336                "protected:\n"
11337                "};\n",
11338                Style);
11339 
11340   // Leave tests rely on the code layout, test::messUp can not be used.
11341   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11342   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11343   Style.MaxEmptyLinesToKeep = 10u;
11344   EXPECT_EQ("struct foo {\n"
11345             "private:\n"
11346             "\n\n\n"
11347             "protected:\n"
11348             "};\n",
11349             format("struct foo {\n"
11350                    "private:\n"
11351                    "\n\n\n"
11352                    "protected:\n"
11353                    "};\n",
11354                    Style));
11355   Style.MaxEmptyLinesToKeep = 3u;
11356   EXPECT_EQ("struct foo {\n"
11357             "private:\n"
11358             "\n\n\n"
11359             "protected:\n"
11360             "};\n",
11361             format("struct foo {\n"
11362                    "private:\n"
11363                    "\n\n\n"
11364                    "protected:\n"
11365                    "};\n",
11366                    Style));
11367   Style.MaxEmptyLinesToKeep = 1u;
11368   EXPECT_EQ("struct foo {\n"
11369             "private:\n"
11370             "\n\n\n"
11371             "protected:\n"
11372             "};\n",
11373             format("struct foo {\n"
11374                    "private:\n"
11375                    "\n\n\n"
11376                    "protected:\n"
11377                    "};\n",
11378                    Style)); // Based on new lines in original document and not
11379                             // on the setting.
11380 
11381   Style.MaxEmptyLinesToKeep = 10u;
11382   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11383   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11384   // Newlines are kept if they are greater than zero,
11385   // test::messUp removes all new lines which changes the logic
11386   EXPECT_EQ("struct foo {\n"
11387             "private:\n"
11388             "\n\n\n"
11389             "protected:\n"
11390             "};\n",
11391             format("struct foo {\n"
11392                    "private:\n"
11393                    "\n\n\n"
11394                    "protected:\n"
11395                    "};\n",
11396                    Style));
11397 
11398   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11399   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11400   // test::messUp removes all new lines which changes the logic
11401   EXPECT_EQ("struct foo {\n"
11402             "private:\n"
11403             "\n\n\n"
11404             "protected:\n"
11405             "};\n",
11406             format("struct foo {\n"
11407                    "private:\n"
11408                    "\n\n\n"
11409                    "protected:\n"
11410                    "};\n",
11411                    Style));
11412 
11413   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
11414   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11415   EXPECT_EQ("struct foo {\n"
11416             "private:\n"
11417             "\n\n\n"
11418             "protected:\n"
11419             "};\n",
11420             format("struct foo {\n"
11421                    "private:\n"
11422                    "\n\n\n"
11423                    "protected:\n"
11424                    "};\n",
11425                    Style)); // test::messUp removes all new lines which changes
11426                             // the logic.
11427 
11428   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11429   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11430   verifyFormat("struct foo {\n"
11431                "private:\n"
11432                "protected:\n"
11433                "};\n",
11434                "struct foo {\n"
11435                "private:\n"
11436                "\n\n\n"
11437                "protected:\n"
11438                "};\n",
11439                Style);
11440 
11441   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
11442   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11443   EXPECT_EQ("struct foo {\n"
11444             "private:\n"
11445             "\n\n\n"
11446             "protected:\n"
11447             "};\n",
11448             format("struct foo {\n"
11449                    "private:\n"
11450                    "\n\n\n"
11451                    "protected:\n"
11452                    "};\n",
11453                    Style)); // test::messUp removes all new lines which changes
11454                             // the logic.
11455 
11456   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
11457   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11458   verifyFormat("struct foo {\n"
11459                "private:\n"
11460                "protected:\n"
11461                "};\n",
11462                "struct foo {\n"
11463                "private:\n"
11464                "\n\n\n"
11465                "protected:\n"
11466                "};\n",
11467                Style);
11468 
11469   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11470   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
11471   verifyFormat("struct foo {\n"
11472                "private:\n"
11473                "protected:\n"
11474                "};\n",
11475                "struct foo {\n"
11476                "private:\n"
11477                "\n\n\n"
11478                "protected:\n"
11479                "};\n",
11480                Style);
11481 
11482   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11483   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
11484   verifyFormat("struct foo {\n"
11485                "private:\n"
11486                "protected:\n"
11487                "};\n",
11488                "struct foo {\n"
11489                "private:\n"
11490                "\n\n\n"
11491                "protected:\n"
11492                "};\n",
11493                Style);
11494 
11495   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
11496   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
11497   verifyFormat("struct foo {\n"
11498                "private:\n"
11499                "protected:\n"
11500                "};\n",
11501                "struct foo {\n"
11502                "private:\n"
11503                "\n\n\n"
11504                "protected:\n"
11505                "};\n",
11506                Style);
11507 }
11508 
11509 TEST_F(FormatTest, FormatsArrays) {
11510   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11511                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
11512   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
11513                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
11514   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
11515                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
11516   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11517                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
11518   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11519                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
11520   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11521                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11522                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
11523   verifyFormat(
11524       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
11525       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
11526       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
11527   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
11528                "    .aaaaaaaaaaaaaaaaaaaaaa();");
11529 
11530   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
11531                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
11532   verifyFormat(
11533       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
11534       "                                  .aaaaaaa[0]\n"
11535       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
11536   verifyFormat("a[::b::c];");
11537 
11538   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
11539 
11540   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
11541   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
11542 }
11543 
11544 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
11545   verifyFormat("(a)->b();");
11546   verifyFormat("--a;");
11547 }
11548 
11549 TEST_F(FormatTest, HandlesIncludeDirectives) {
11550   verifyFormat("#include <string>\n"
11551                "#include <a/b/c.h>\n"
11552                "#include \"a/b/string\"\n"
11553                "#include \"string.h\"\n"
11554                "#include \"string.h\"\n"
11555                "#include <a-a>\n"
11556                "#include < path with space >\n"
11557                "#include_next <test.h>"
11558                "#include \"abc.h\" // this is included for ABC\n"
11559                "#include \"some long include\" // with a comment\n"
11560                "#include \"some very long include path\"\n"
11561                "#include <some/very/long/include/path>\n",
11562                getLLVMStyleWithColumns(35));
11563   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
11564   EXPECT_EQ("#include <a>", format("#include<a>"));
11565 
11566   verifyFormat("#import <string>");
11567   verifyFormat("#import <a/b/c.h>");
11568   verifyFormat("#import \"a/b/string\"");
11569   verifyFormat("#import \"string.h\"");
11570   verifyFormat("#import \"string.h\"");
11571   verifyFormat("#if __has_include(<strstream>)\n"
11572                "#include <strstream>\n"
11573                "#endif");
11574 
11575   verifyFormat("#define MY_IMPORT <a/b>");
11576 
11577   verifyFormat("#if __has_include(<a/b>)");
11578   verifyFormat("#if __has_include_next(<a/b>)");
11579   verifyFormat("#define F __has_include(<a/b>)");
11580   verifyFormat("#define F __has_include_next(<a/b>)");
11581 
11582   // Protocol buffer definition or missing "#".
11583   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
11584                getLLVMStyleWithColumns(30));
11585 
11586   FormatStyle Style = getLLVMStyle();
11587   Style.AlwaysBreakBeforeMultilineStrings = true;
11588   Style.ColumnLimit = 0;
11589   verifyFormat("#import \"abc.h\"", Style);
11590 
11591   // But 'import' might also be a regular C++ namespace.
11592   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11593                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
11594 }
11595 
11596 //===----------------------------------------------------------------------===//
11597 // Error recovery tests.
11598 //===----------------------------------------------------------------------===//
11599 
11600 TEST_F(FormatTest, IncompleteParameterLists) {
11601   FormatStyle NoBinPacking = getLLVMStyle();
11602   NoBinPacking.BinPackParameters = false;
11603   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
11604                "                        double *min_x,\n"
11605                "                        double *max_x,\n"
11606                "                        double *min_y,\n"
11607                "                        double *max_y,\n"
11608                "                        double *min_z,\n"
11609                "                        double *max_z, ) {}",
11610                NoBinPacking);
11611 }
11612 
11613 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
11614   verifyFormat("void f() { return; }\n42");
11615   verifyFormat("void f() {\n"
11616                "  if (0)\n"
11617                "    return;\n"
11618                "}\n"
11619                "42");
11620   verifyFormat("void f() { return }\n42");
11621   verifyFormat("void f() {\n"
11622                "  if (0)\n"
11623                "    return\n"
11624                "}\n"
11625                "42");
11626 }
11627 
11628 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
11629   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
11630   EXPECT_EQ("void f() {\n"
11631             "  if (a)\n"
11632             "    return\n"
11633             "}",
11634             format("void  f  (  )  {  if  ( a )  return  }"));
11635   EXPECT_EQ("namespace N {\n"
11636             "void f()\n"
11637             "}",
11638             format("namespace  N  {  void f()  }"));
11639   EXPECT_EQ("namespace N {\n"
11640             "void f() {}\n"
11641             "void g()\n"
11642             "} // namespace N",
11643             format("namespace N  { void f( ) { } void g( ) }"));
11644 }
11645 
11646 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
11647   verifyFormat("int aaaaaaaa =\n"
11648                "    // Overlylongcomment\n"
11649                "    b;",
11650                getLLVMStyleWithColumns(20));
11651   verifyFormat("function(\n"
11652                "    ShortArgument,\n"
11653                "    LoooooooooooongArgument);\n",
11654                getLLVMStyleWithColumns(20));
11655 }
11656 
11657 TEST_F(FormatTest, IncorrectAccessSpecifier) {
11658   verifyFormat("public:");
11659   verifyFormat("class A {\n"
11660                "public\n"
11661                "  void f() {}\n"
11662                "};");
11663   verifyFormat("public\n"
11664                "int qwerty;");
11665   verifyFormat("public\n"
11666                "B {}");
11667   verifyFormat("public\n"
11668                "{}");
11669   verifyFormat("public\n"
11670                "B { int x; }");
11671 }
11672 
11673 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
11674   verifyFormat("{");
11675   verifyFormat("#})");
11676   verifyNoCrash("(/**/[:!] ?[).");
11677 }
11678 
11679 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
11680   // Found by oss-fuzz:
11681   // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
11682   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
11683   Style.ColumnLimit = 60;
11684   verifyNoCrash(
11685       "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
11686       "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
11687       "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
11688       Style);
11689 }
11690 
11691 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
11692   verifyFormat("do {\n}");
11693   verifyFormat("do {\n}\n"
11694                "f();");
11695   verifyFormat("do {\n}\n"
11696                "wheeee(fun);");
11697   verifyFormat("do {\n"
11698                "  f();\n"
11699                "}");
11700 }
11701 
11702 TEST_F(FormatTest, IncorrectCodeMissingParens) {
11703   verifyFormat("if {\n  foo;\n  foo();\n}");
11704   verifyFormat("switch {\n  foo;\n  foo();\n}");
11705   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
11706   verifyFormat("while {\n  foo;\n  foo();\n}");
11707   verifyFormat("do {\n  foo;\n  foo();\n} while;");
11708 }
11709 
11710 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
11711   verifyIncompleteFormat("namespace {\n"
11712                          "class Foo { Foo (\n"
11713                          "};\n"
11714                          "} // namespace");
11715 }
11716 
11717 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
11718   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
11719   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
11720   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
11721   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
11722 
11723   EXPECT_EQ("{\n"
11724             "  {\n"
11725             "    breakme(\n"
11726             "        qwe);\n"
11727             "  }\n",
11728             format("{\n"
11729                    "    {\n"
11730                    " breakme(qwe);\n"
11731                    "}\n",
11732                    getLLVMStyleWithColumns(10)));
11733 }
11734 
11735 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
11736   verifyFormat("int x = {\n"
11737                "    avariable,\n"
11738                "    b(alongervariable)};",
11739                getLLVMStyleWithColumns(25));
11740 }
11741 
11742 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
11743   verifyFormat("return (a)(b){1, 2, 3};");
11744 }
11745 
11746 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
11747   verifyFormat("vector<int> x{1, 2, 3, 4};");
11748   verifyFormat("vector<int> x{\n"
11749                "    1,\n"
11750                "    2,\n"
11751                "    3,\n"
11752                "    4,\n"
11753                "};");
11754   verifyFormat("vector<T> x{{}, {}, {}, {}};");
11755   verifyFormat("f({1, 2});");
11756   verifyFormat("auto v = Foo{-1};");
11757   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
11758   verifyFormat("Class::Class : member{1, 2, 3} {}");
11759   verifyFormat("new vector<int>{1, 2, 3};");
11760   verifyFormat("new int[3]{1, 2, 3};");
11761   verifyFormat("new int{1};");
11762   verifyFormat("return {arg1, arg2};");
11763   verifyFormat("return {arg1, SomeType{parameter}};");
11764   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
11765   verifyFormat("new T{arg1, arg2};");
11766   verifyFormat("f(MyMap[{composite, key}]);");
11767   verifyFormat("class Class {\n"
11768                "  T member = {arg1, arg2};\n"
11769                "};");
11770   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
11771   verifyFormat("const struct A a = {.a = 1, .b = 2};");
11772   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
11773   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
11774   verifyFormat("int a = std::is_integral<int>{} + 0;");
11775 
11776   verifyFormat("int foo(int i) { return fo1{}(i); }");
11777   verifyFormat("int foo(int i) { return fo1{}(i); }");
11778   verifyFormat("auto i = decltype(x){};");
11779   verifyFormat("auto i = typeof(x){};");
11780   verifyFormat("auto i = _Atomic(x){};");
11781   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
11782   verifyFormat("Node n{1, Node{1000}, //\n"
11783                "       2};");
11784   verifyFormat("Aaaa aaaaaaa{\n"
11785                "    {\n"
11786                "        aaaa,\n"
11787                "    },\n"
11788                "};");
11789   verifyFormat("class C : public D {\n"
11790                "  SomeClass SC{2};\n"
11791                "};");
11792   verifyFormat("class C : public A {\n"
11793                "  class D : public B {\n"
11794                "    void f() { int i{2}; }\n"
11795                "  };\n"
11796                "};");
11797   verifyFormat("#define A {a, a},");
11798   // Don't confuse braced list initializers with compound statements.
11799   verifyFormat(
11800       "class A {\n"
11801       "  A() : a{} {}\n"
11802       "  A(int b) : b(b) {}\n"
11803       "  A(int a, int b) : a(a), bs{{bs...}} { f(); }\n"
11804       "  int a, b;\n"
11805       "  explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n"
11806       "  explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} "
11807       "{}\n"
11808       "};");
11809 
11810   // Avoid breaking between equal sign and opening brace
11811   FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
11812   AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
11813   verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
11814                "    {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
11815                "     {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
11816                "     {\"ccccccccccccccccccccc\", 2}};",
11817                AvoidBreakingFirstArgument);
11818 
11819   // Binpacking only if there is no trailing comma
11820   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
11821                "                      cccccccccc, dddddddddd};",
11822                getLLVMStyleWithColumns(50));
11823   verifyFormat("const Aaaaaa aaaaa = {\n"
11824                "    aaaaaaaaaaa,\n"
11825                "    bbbbbbbbbbb,\n"
11826                "    ccccccccccc,\n"
11827                "    ddddddddddd,\n"
11828                "};",
11829                getLLVMStyleWithColumns(50));
11830 
11831   // Cases where distinguising braced lists and blocks is hard.
11832   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
11833   verifyFormat("void f() {\n"
11834                "  return; // comment\n"
11835                "}\n"
11836                "SomeType t;");
11837   verifyFormat("void f() {\n"
11838                "  if (a) {\n"
11839                "    f();\n"
11840                "  }\n"
11841                "}\n"
11842                "SomeType t;");
11843 
11844   // In combination with BinPackArguments = false.
11845   FormatStyle NoBinPacking = getLLVMStyle();
11846   NoBinPacking.BinPackArguments = false;
11847   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
11848                "                      bbbbb,\n"
11849                "                      ccccc,\n"
11850                "                      ddddd,\n"
11851                "                      eeeee,\n"
11852                "                      ffffff,\n"
11853                "                      ggggg,\n"
11854                "                      hhhhhh,\n"
11855                "                      iiiiii,\n"
11856                "                      jjjjjj,\n"
11857                "                      kkkkkk};",
11858                NoBinPacking);
11859   verifyFormat("const Aaaaaa aaaaa = {\n"
11860                "    aaaaa,\n"
11861                "    bbbbb,\n"
11862                "    ccccc,\n"
11863                "    ddddd,\n"
11864                "    eeeee,\n"
11865                "    ffffff,\n"
11866                "    ggggg,\n"
11867                "    hhhhhh,\n"
11868                "    iiiiii,\n"
11869                "    jjjjjj,\n"
11870                "    kkkkkk,\n"
11871                "};",
11872                NoBinPacking);
11873   verifyFormat(
11874       "const Aaaaaa aaaaa = {\n"
11875       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
11876       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
11877       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
11878       "};",
11879       NoBinPacking);
11880 
11881   NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11882   EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n"
11883             "    CDDDP83848_BMCR_REGISTER,\n"
11884             "    CDDDP83848_BMSR_REGISTER,\n"
11885             "    CDDDP83848_RBR_REGISTER};",
11886             format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
11887                    "                                CDDDP83848_BMSR_REGISTER,\n"
11888                    "                                CDDDP83848_RBR_REGISTER};",
11889                    NoBinPacking));
11890 
11891   // FIXME: The alignment of these trailing comments might be bad. Then again,
11892   // this might be utterly useless in real code.
11893   verifyFormat("Constructor::Constructor()\n"
11894                "    : some_value{         //\n"
11895                "                 aaaaaaa, //\n"
11896                "                 bbbbbbb} {}");
11897 
11898   // In braced lists, the first comment is always assumed to belong to the
11899   // first element. Thus, it can be moved to the next or previous line as
11900   // appropriate.
11901   EXPECT_EQ("function({// First element:\n"
11902             "          1,\n"
11903             "          // Second element:\n"
11904             "          2});",
11905             format("function({\n"
11906                    "    // First element:\n"
11907                    "    1,\n"
11908                    "    // Second element:\n"
11909                    "    2});"));
11910   EXPECT_EQ("std::vector<int> MyNumbers{\n"
11911             "    // First element:\n"
11912             "    1,\n"
11913             "    // Second element:\n"
11914             "    2};",
11915             format("std::vector<int> MyNumbers{// First element:\n"
11916                    "                           1,\n"
11917                    "                           // Second element:\n"
11918                    "                           2};",
11919                    getLLVMStyleWithColumns(30)));
11920   // A trailing comma should still lead to an enforced line break and no
11921   // binpacking.
11922   EXPECT_EQ("vector<int> SomeVector = {\n"
11923             "    // aaa\n"
11924             "    1,\n"
11925             "    2,\n"
11926             "};",
11927             format("vector<int> SomeVector = { // aaa\n"
11928                    "    1, 2, };"));
11929 
11930   // C++11 brace initializer list l-braces should not be treated any differently
11931   // when breaking before lambda bodies is enabled
11932   FormatStyle BreakBeforeLambdaBody = getLLVMStyle();
11933   BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
11934   BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
11935   BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true;
11936   verifyFormat(
11937       "std::runtime_error{\n"
11938       "    \"Long string which will force a break onto the next line...\"};",
11939       BreakBeforeLambdaBody);
11940 
11941   FormatStyle ExtraSpaces = getLLVMStyle();
11942   ExtraSpaces.Cpp11BracedListStyle = false;
11943   ExtraSpaces.ColumnLimit = 75;
11944   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
11945   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
11946   verifyFormat("f({ 1, 2 });", ExtraSpaces);
11947   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
11948   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
11949   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
11950   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
11951   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
11952   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
11953   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
11954   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
11955   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
11956   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
11957   verifyFormat("class Class {\n"
11958                "  T member = { arg1, arg2 };\n"
11959                "};",
11960                ExtraSpaces);
11961   verifyFormat(
11962       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11963       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
11964       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
11965       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
11966       ExtraSpaces);
11967   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
11968   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
11969                ExtraSpaces);
11970   verifyFormat(
11971       "someFunction(OtherParam,\n"
11972       "             BracedList{ // comment 1 (Forcing interesting break)\n"
11973       "                         param1, param2,\n"
11974       "                         // comment 2\n"
11975       "                         param3, param4 });",
11976       ExtraSpaces);
11977   verifyFormat(
11978       "std::this_thread::sleep_for(\n"
11979       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
11980       ExtraSpaces);
11981   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
11982                "    aaaaaaa,\n"
11983                "    aaaaaaaaaa,\n"
11984                "    aaaaa,\n"
11985                "    aaaaaaaaaaaaaaa,\n"
11986                "    aaa,\n"
11987                "    aaaaaaaaaa,\n"
11988                "    a,\n"
11989                "    aaaaaaaaaaaaaaaaaaaaa,\n"
11990                "    aaaaaaaaaaaa,\n"
11991                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
11992                "    aaaaaaa,\n"
11993                "    a};");
11994   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
11995   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
11996   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
11997 
11998   // Avoid breaking between initializer/equal sign and opening brace
11999   ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
12000   verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
12001                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
12002                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
12003                "  { \"ccccccccccccccccccccc\", 2 }\n"
12004                "};",
12005                ExtraSpaces);
12006   verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
12007                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
12008                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
12009                "  { \"ccccccccccccccccccccc\", 2 }\n"
12010                "};",
12011                ExtraSpaces);
12012 
12013   FormatStyle SpaceBeforeBrace = getLLVMStyle();
12014   SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
12015   verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
12016   verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
12017 
12018   FormatStyle SpaceBetweenBraces = getLLVMStyle();
12019   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
12020   SpaceBetweenBraces.SpacesInParentheses = true;
12021   SpaceBetweenBraces.SpacesInSquareBrackets = true;
12022   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
12023   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
12024   verifyFormat("vector< int > x{ // comment 1\n"
12025                "                 1, 2, 3, 4 };",
12026                SpaceBetweenBraces);
12027   SpaceBetweenBraces.ColumnLimit = 20;
12028   EXPECT_EQ("vector< int > x{\n"
12029             "    1, 2, 3, 4 };",
12030             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
12031   SpaceBetweenBraces.ColumnLimit = 24;
12032   EXPECT_EQ("vector< int > x{ 1, 2,\n"
12033             "                 3, 4 };",
12034             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
12035   EXPECT_EQ("vector< int > x{\n"
12036             "    1,\n"
12037             "    2,\n"
12038             "    3,\n"
12039             "    4,\n"
12040             "};",
12041             format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces));
12042   verifyFormat("vector< int > x{};", SpaceBetweenBraces);
12043   SpaceBetweenBraces.SpaceInEmptyParentheses = true;
12044   verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
12045 }
12046 
12047 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
12048   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12049                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12050                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12051                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12052                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12053                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
12054   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
12055                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12056                "                 1, 22, 333, 4444, 55555, //\n"
12057                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12058                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
12059   verifyFormat(
12060       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
12061       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
12062       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
12063       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
12064       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
12065       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
12066       "                 7777777};");
12067   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
12068                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
12069                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
12070   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
12071                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
12072                "    // Separating comment.\n"
12073                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
12074   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
12075                "    // Leading comment\n"
12076                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
12077                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
12078   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
12079                "                 1, 1, 1, 1};",
12080                getLLVMStyleWithColumns(39));
12081   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
12082                "                 1, 1, 1, 1};",
12083                getLLVMStyleWithColumns(38));
12084   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
12085                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
12086                getLLVMStyleWithColumns(43));
12087   verifyFormat(
12088       "static unsigned SomeValues[10][3] = {\n"
12089       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
12090       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
12091   verifyFormat("static auto fields = new vector<string>{\n"
12092                "    \"aaaaaaaaaaaaa\",\n"
12093                "    \"aaaaaaaaaaaaa\",\n"
12094                "    \"aaaaaaaaaaaa\",\n"
12095                "    \"aaaaaaaaaaaaaa\",\n"
12096                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
12097                "    \"aaaaaaaaaaaa\",\n"
12098                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
12099                "};");
12100   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
12101   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
12102                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
12103                "                 3, cccccccccccccccccccccc};",
12104                getLLVMStyleWithColumns(60));
12105 
12106   // Trailing commas.
12107   verifyFormat("vector<int> x = {\n"
12108                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
12109                "};",
12110                getLLVMStyleWithColumns(39));
12111   verifyFormat("vector<int> x = {\n"
12112                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
12113                "};",
12114                getLLVMStyleWithColumns(39));
12115   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
12116                "                 1, 1, 1, 1,\n"
12117                "                 /**/ /**/};",
12118                getLLVMStyleWithColumns(39));
12119 
12120   // Trailing comment in the first line.
12121   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
12122                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
12123                "    111111111,  222222222,  3333333333,  444444444,  //\n"
12124                "    11111111,   22222222,   333333333,   44444444};");
12125   // Trailing comment in the last line.
12126   verifyFormat("int aaaaa[] = {\n"
12127                "    1, 2, 3, // comment\n"
12128                "    4, 5, 6  // comment\n"
12129                "};");
12130 
12131   // With nested lists, we should either format one item per line or all nested
12132   // lists one on line.
12133   // FIXME: For some nested lists, we can do better.
12134   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
12135                "        {aaaaaaaaaaaaaaaaaaa},\n"
12136                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
12137                "        {aaaaaaaaaaaaaaaaa}};",
12138                getLLVMStyleWithColumns(60));
12139   verifyFormat(
12140       "SomeStruct my_struct_array = {\n"
12141       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
12142       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
12143       "    {aaa, aaa},\n"
12144       "    {aaa, aaa},\n"
12145       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
12146       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
12147       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
12148 
12149   // No column layout should be used here.
12150   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
12151                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
12152 
12153   verifyNoCrash("a<,");
12154 
12155   // No braced initializer here.
12156   verifyFormat("void f() {\n"
12157                "  struct Dummy {};\n"
12158                "  f(v);\n"
12159                "}");
12160 
12161   // Long lists should be formatted in columns even if they are nested.
12162   verifyFormat(
12163       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12164       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12165       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12166       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12167       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
12168       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
12169 
12170   // Allow "single-column" layout even if that violates the column limit. There
12171   // isn't going to be a better way.
12172   verifyFormat("std::vector<int> a = {\n"
12173                "    aaaaaaaa,\n"
12174                "    aaaaaaaa,\n"
12175                "    aaaaaaaa,\n"
12176                "    aaaaaaaa,\n"
12177                "    aaaaaaaaaa,\n"
12178                "    aaaaaaaa,\n"
12179                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
12180                getLLVMStyleWithColumns(30));
12181   verifyFormat("vector<int> aaaa = {\n"
12182                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12183                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12184                "    aaaaaa.aaaaaaa,\n"
12185                "    aaaaaa.aaaaaaa,\n"
12186                "    aaaaaa.aaaaaaa,\n"
12187                "    aaaaaa.aaaaaaa,\n"
12188                "};");
12189 
12190   // Don't create hanging lists.
12191   verifyFormat("someFunction(Param, {List1, List2,\n"
12192                "                     List3});",
12193                getLLVMStyleWithColumns(35));
12194   verifyFormat("someFunction(Param, Param,\n"
12195                "             {List1, List2,\n"
12196                "              List3});",
12197                getLLVMStyleWithColumns(35));
12198   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
12199                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
12200 }
12201 
12202 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
12203   FormatStyle DoNotMerge = getLLVMStyle();
12204   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12205 
12206   verifyFormat("void f() { return 42; }");
12207   verifyFormat("void f() {\n"
12208                "  return 42;\n"
12209                "}",
12210                DoNotMerge);
12211   verifyFormat("void f() {\n"
12212                "  // Comment\n"
12213                "}");
12214   verifyFormat("{\n"
12215                "#error {\n"
12216                "  int a;\n"
12217                "}");
12218   verifyFormat("{\n"
12219                "  int a;\n"
12220                "#error {\n"
12221                "}");
12222   verifyFormat("void f() {} // comment");
12223   verifyFormat("void f() { int a; } // comment");
12224   verifyFormat("void f() {\n"
12225                "} // comment",
12226                DoNotMerge);
12227   verifyFormat("void f() {\n"
12228                "  int a;\n"
12229                "} // comment",
12230                DoNotMerge);
12231   verifyFormat("void f() {\n"
12232                "} // comment",
12233                getLLVMStyleWithColumns(15));
12234 
12235   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
12236   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
12237 
12238   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
12239   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
12240   verifyFormat("class C {\n"
12241                "  C()\n"
12242                "      : iiiiiiii(nullptr),\n"
12243                "        kkkkkkk(nullptr),\n"
12244                "        mmmmmmm(nullptr),\n"
12245                "        nnnnnnn(nullptr) {}\n"
12246                "};",
12247                getGoogleStyle());
12248 
12249   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
12250   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
12251   EXPECT_EQ("class C {\n"
12252             "  A() : b(0) {}\n"
12253             "};",
12254             format("class C{A():b(0){}};", NoColumnLimit));
12255   EXPECT_EQ("A()\n"
12256             "    : b(0) {\n"
12257             "}",
12258             format("A()\n:b(0)\n{\n}", NoColumnLimit));
12259 
12260   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
12261   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
12262       FormatStyle::SFS_None;
12263   EXPECT_EQ("A()\n"
12264             "    : b(0) {\n"
12265             "}",
12266             format("A():b(0){}", DoNotMergeNoColumnLimit));
12267   EXPECT_EQ("A()\n"
12268             "    : b(0) {\n"
12269             "}",
12270             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
12271 
12272   verifyFormat("#define A          \\\n"
12273                "  void f() {       \\\n"
12274                "    int i;         \\\n"
12275                "  }",
12276                getLLVMStyleWithColumns(20));
12277   verifyFormat("#define A           \\\n"
12278                "  void f() { int i; }",
12279                getLLVMStyleWithColumns(21));
12280   verifyFormat("#define A            \\\n"
12281                "  void f() {         \\\n"
12282                "    int i;           \\\n"
12283                "  }                  \\\n"
12284                "  int j;",
12285                getLLVMStyleWithColumns(22));
12286   verifyFormat("#define A             \\\n"
12287                "  void f() { int i; } \\\n"
12288                "  int j;",
12289                getLLVMStyleWithColumns(23));
12290 }
12291 
12292 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
12293   FormatStyle MergeEmptyOnly = getLLVMStyle();
12294   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
12295   verifyFormat("class C {\n"
12296                "  int f() {}\n"
12297                "};",
12298                MergeEmptyOnly);
12299   verifyFormat("class C {\n"
12300                "  int f() {\n"
12301                "    return 42;\n"
12302                "  }\n"
12303                "};",
12304                MergeEmptyOnly);
12305   verifyFormat("int f() {}", MergeEmptyOnly);
12306   verifyFormat("int f() {\n"
12307                "  return 42;\n"
12308                "}",
12309                MergeEmptyOnly);
12310 
12311   // Also verify behavior when BraceWrapping.AfterFunction = true
12312   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
12313   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
12314   verifyFormat("int f() {}", MergeEmptyOnly);
12315   verifyFormat("class C {\n"
12316                "  int f() {}\n"
12317                "};",
12318                MergeEmptyOnly);
12319 }
12320 
12321 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
12322   FormatStyle MergeInlineOnly = getLLVMStyle();
12323   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
12324   verifyFormat("class C {\n"
12325                "  int f() { return 42; }\n"
12326                "};",
12327                MergeInlineOnly);
12328   verifyFormat("int f() {\n"
12329                "  return 42;\n"
12330                "}",
12331                MergeInlineOnly);
12332 
12333   // SFS_Inline implies SFS_Empty
12334   verifyFormat("class C {\n"
12335                "  int f() {}\n"
12336                "};",
12337                MergeInlineOnly);
12338   verifyFormat("int f() {}", MergeInlineOnly);
12339 
12340   // Also verify behavior when BraceWrapping.AfterFunction = true
12341   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
12342   MergeInlineOnly.BraceWrapping.AfterFunction = true;
12343   verifyFormat("class C {\n"
12344                "  int f() { return 42; }\n"
12345                "};",
12346                MergeInlineOnly);
12347   verifyFormat("int f()\n"
12348                "{\n"
12349                "  return 42;\n"
12350                "}",
12351                MergeInlineOnly);
12352 
12353   // SFS_Inline implies SFS_Empty
12354   verifyFormat("int f() {}", MergeInlineOnly);
12355   verifyFormat("class C {\n"
12356                "  int f() {}\n"
12357                "};",
12358                MergeInlineOnly);
12359 
12360   MergeInlineOnly.BraceWrapping.AfterClass = true;
12361   MergeInlineOnly.BraceWrapping.AfterStruct = true;
12362   verifyFormat("class C\n"
12363                "{\n"
12364                "  int f() { return 42; }\n"
12365                "};",
12366                MergeInlineOnly);
12367   verifyFormat("struct C\n"
12368                "{\n"
12369                "  int f() { return 42; }\n"
12370                "};",
12371                MergeInlineOnly);
12372   verifyFormat("int f()\n"
12373                "{\n"
12374                "  return 42;\n"
12375                "}",
12376                MergeInlineOnly);
12377   verifyFormat("int f() {}", MergeInlineOnly);
12378   verifyFormat("class C\n"
12379                "{\n"
12380                "  int f() { return 42; }\n"
12381                "};",
12382                MergeInlineOnly);
12383   verifyFormat("struct C\n"
12384                "{\n"
12385                "  int f() { return 42; }\n"
12386                "};",
12387                MergeInlineOnly);
12388   verifyFormat("struct C\n"
12389                "// comment\n"
12390                "/* comment */\n"
12391                "// comment\n"
12392                "{\n"
12393                "  int f() { return 42; }\n"
12394                "};",
12395                MergeInlineOnly);
12396   verifyFormat("/* comment */ struct C\n"
12397                "{\n"
12398                "  int f() { return 42; }\n"
12399                "};",
12400                MergeInlineOnly);
12401 }
12402 
12403 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
12404   FormatStyle MergeInlineOnly = getLLVMStyle();
12405   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
12406       FormatStyle::SFS_InlineOnly;
12407   verifyFormat("class C {\n"
12408                "  int f() { return 42; }\n"
12409                "};",
12410                MergeInlineOnly);
12411   verifyFormat("int f() {\n"
12412                "  return 42;\n"
12413                "}",
12414                MergeInlineOnly);
12415 
12416   // SFS_InlineOnly does not imply SFS_Empty
12417   verifyFormat("class C {\n"
12418                "  int f() {}\n"
12419                "};",
12420                MergeInlineOnly);
12421   verifyFormat("int f() {\n"
12422                "}",
12423                MergeInlineOnly);
12424 
12425   // Also verify behavior when BraceWrapping.AfterFunction = true
12426   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
12427   MergeInlineOnly.BraceWrapping.AfterFunction = true;
12428   verifyFormat("class C {\n"
12429                "  int f() { return 42; }\n"
12430                "};",
12431                MergeInlineOnly);
12432   verifyFormat("int f()\n"
12433                "{\n"
12434                "  return 42;\n"
12435                "}",
12436                MergeInlineOnly);
12437 
12438   // SFS_InlineOnly does not imply SFS_Empty
12439   verifyFormat("int f()\n"
12440                "{\n"
12441                "}",
12442                MergeInlineOnly);
12443   verifyFormat("class C {\n"
12444                "  int f() {}\n"
12445                "};",
12446                MergeInlineOnly);
12447 }
12448 
12449 TEST_F(FormatTest, SplitEmptyFunction) {
12450   FormatStyle Style = getLLVMStyleWithColumns(40);
12451   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12452   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12453   Style.BraceWrapping.AfterFunction = true;
12454   Style.BraceWrapping.SplitEmptyFunction = false;
12455 
12456   verifyFormat("int f()\n"
12457                "{}",
12458                Style);
12459   verifyFormat("int f()\n"
12460                "{\n"
12461                "  return 42;\n"
12462                "}",
12463                Style);
12464   verifyFormat("int f()\n"
12465                "{\n"
12466                "  // some comment\n"
12467                "}",
12468                Style);
12469 
12470   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
12471   verifyFormat("int f() {}", Style);
12472   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12473                "{}",
12474                Style);
12475   verifyFormat("int f()\n"
12476                "{\n"
12477                "  return 0;\n"
12478                "}",
12479                Style);
12480 
12481   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
12482   verifyFormat("class Foo {\n"
12483                "  int f() {}\n"
12484                "};\n",
12485                Style);
12486   verifyFormat("class Foo {\n"
12487                "  int f() { return 0; }\n"
12488                "};\n",
12489                Style);
12490   verifyFormat("class Foo {\n"
12491                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12492                "  {}\n"
12493                "};\n",
12494                Style);
12495   verifyFormat("class Foo {\n"
12496                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12497                "  {\n"
12498                "    return 0;\n"
12499                "  }\n"
12500                "};\n",
12501                Style);
12502 
12503   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
12504   verifyFormat("int f() {}", Style);
12505   verifyFormat("int f() { return 0; }", Style);
12506   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12507                "{}",
12508                Style);
12509   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
12510                "{\n"
12511                "  return 0;\n"
12512                "}",
12513                Style);
12514 }
12515 
12516 TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) {
12517   FormatStyle Style = getLLVMStyleWithColumns(40);
12518   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12519   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12520   Style.BraceWrapping.AfterFunction = true;
12521   Style.BraceWrapping.SplitEmptyFunction = true;
12522   Style.BraceWrapping.SplitEmptyRecord = false;
12523 
12524   verifyFormat("class C {};", Style);
12525   verifyFormat("struct C {};", Style);
12526   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12527                "       int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
12528                "{\n"
12529                "}",
12530                Style);
12531   verifyFormat("class C {\n"
12532                "  C()\n"
12533                "      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n"
12534                "        bbbbbbbbbbbbbbbbbbb()\n"
12535                "  {\n"
12536                "  }\n"
12537                "  void\n"
12538                "  m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12539                "    int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
12540                "  {\n"
12541                "  }\n"
12542                "};",
12543                Style);
12544 }
12545 
12546 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
12547   FormatStyle Style = getLLVMStyle();
12548   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
12549   verifyFormat("#ifdef A\n"
12550                "int f() {}\n"
12551                "#else\n"
12552                "int g() {}\n"
12553                "#endif",
12554                Style);
12555 }
12556 
12557 TEST_F(FormatTest, SplitEmptyClass) {
12558   FormatStyle Style = getLLVMStyle();
12559   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12560   Style.BraceWrapping.AfterClass = true;
12561   Style.BraceWrapping.SplitEmptyRecord = false;
12562 
12563   verifyFormat("class Foo\n"
12564                "{};",
12565                Style);
12566   verifyFormat("/* something */ class Foo\n"
12567                "{};",
12568                Style);
12569   verifyFormat("template <typename X> class Foo\n"
12570                "{};",
12571                Style);
12572   verifyFormat("class Foo\n"
12573                "{\n"
12574                "  Foo();\n"
12575                "};",
12576                Style);
12577   verifyFormat("typedef class Foo\n"
12578                "{\n"
12579                "} Foo_t;",
12580                Style);
12581 
12582   Style.BraceWrapping.SplitEmptyRecord = true;
12583   Style.BraceWrapping.AfterStruct = true;
12584   verifyFormat("class rep\n"
12585                "{\n"
12586                "};",
12587                Style);
12588   verifyFormat("struct rep\n"
12589                "{\n"
12590                "};",
12591                Style);
12592   verifyFormat("template <typename T> class rep\n"
12593                "{\n"
12594                "};",
12595                Style);
12596   verifyFormat("template <typename T> struct rep\n"
12597                "{\n"
12598                "};",
12599                Style);
12600   verifyFormat("class rep\n"
12601                "{\n"
12602                "  int x;\n"
12603                "};",
12604                Style);
12605   verifyFormat("struct rep\n"
12606                "{\n"
12607                "  int x;\n"
12608                "};",
12609                Style);
12610   verifyFormat("template <typename T> class rep\n"
12611                "{\n"
12612                "  int x;\n"
12613                "};",
12614                Style);
12615   verifyFormat("template <typename T> struct rep\n"
12616                "{\n"
12617                "  int x;\n"
12618                "};",
12619                Style);
12620   verifyFormat("template <typename T> class rep // Foo\n"
12621                "{\n"
12622                "  int x;\n"
12623                "};",
12624                Style);
12625   verifyFormat("template <typename T> struct rep // Bar\n"
12626                "{\n"
12627                "  int x;\n"
12628                "};",
12629                Style);
12630 
12631   verifyFormat("template <typename T> class rep<T>\n"
12632                "{\n"
12633                "  int x;\n"
12634                "};",
12635                Style);
12636 
12637   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12638                "{\n"
12639                "  int x;\n"
12640                "};",
12641                Style);
12642   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
12643                "{\n"
12644                "};",
12645                Style);
12646 
12647   verifyFormat("#include \"stdint.h\"\n"
12648                "namespace rep {}",
12649                Style);
12650   verifyFormat("#include <stdint.h>\n"
12651                "namespace rep {}",
12652                Style);
12653   verifyFormat("#include <stdint.h>\n"
12654                "namespace rep {}",
12655                "#include <stdint.h>\n"
12656                "namespace rep {\n"
12657                "\n"
12658                "\n"
12659                "}",
12660                Style);
12661 }
12662 
12663 TEST_F(FormatTest, SplitEmptyStruct) {
12664   FormatStyle Style = getLLVMStyle();
12665   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12666   Style.BraceWrapping.AfterStruct = true;
12667   Style.BraceWrapping.SplitEmptyRecord = false;
12668 
12669   verifyFormat("struct Foo\n"
12670                "{};",
12671                Style);
12672   verifyFormat("/* something */ struct Foo\n"
12673                "{};",
12674                Style);
12675   verifyFormat("template <typename X> struct Foo\n"
12676                "{};",
12677                Style);
12678   verifyFormat("struct Foo\n"
12679                "{\n"
12680                "  Foo();\n"
12681                "};",
12682                Style);
12683   verifyFormat("typedef struct Foo\n"
12684                "{\n"
12685                "} Foo_t;",
12686                Style);
12687   // typedef struct Bar {} Bar_t;
12688 }
12689 
12690 TEST_F(FormatTest, SplitEmptyUnion) {
12691   FormatStyle Style = getLLVMStyle();
12692   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12693   Style.BraceWrapping.AfterUnion = true;
12694   Style.BraceWrapping.SplitEmptyRecord = false;
12695 
12696   verifyFormat("union Foo\n"
12697                "{};",
12698                Style);
12699   verifyFormat("/* something */ union Foo\n"
12700                "{};",
12701                Style);
12702   verifyFormat("union Foo\n"
12703                "{\n"
12704                "  A,\n"
12705                "};",
12706                Style);
12707   verifyFormat("typedef union Foo\n"
12708                "{\n"
12709                "} Foo_t;",
12710                Style);
12711 }
12712 
12713 TEST_F(FormatTest, SplitEmptyNamespace) {
12714   FormatStyle Style = getLLVMStyle();
12715   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12716   Style.BraceWrapping.AfterNamespace = true;
12717   Style.BraceWrapping.SplitEmptyNamespace = false;
12718 
12719   verifyFormat("namespace Foo\n"
12720                "{};",
12721                Style);
12722   verifyFormat("/* something */ namespace Foo\n"
12723                "{};",
12724                Style);
12725   verifyFormat("inline namespace Foo\n"
12726                "{};",
12727                Style);
12728   verifyFormat("/* something */ inline namespace Foo\n"
12729                "{};",
12730                Style);
12731   verifyFormat("export namespace Foo\n"
12732                "{};",
12733                Style);
12734   verifyFormat("namespace Foo\n"
12735                "{\n"
12736                "void Bar();\n"
12737                "};",
12738                Style);
12739 }
12740 
12741 TEST_F(FormatTest, NeverMergeShortRecords) {
12742   FormatStyle Style = getLLVMStyle();
12743 
12744   verifyFormat("class Foo {\n"
12745                "  Foo();\n"
12746                "};",
12747                Style);
12748   verifyFormat("typedef class Foo {\n"
12749                "  Foo();\n"
12750                "} Foo_t;",
12751                Style);
12752   verifyFormat("struct Foo {\n"
12753                "  Foo();\n"
12754                "};",
12755                Style);
12756   verifyFormat("typedef struct Foo {\n"
12757                "  Foo();\n"
12758                "} Foo_t;",
12759                Style);
12760   verifyFormat("union Foo {\n"
12761                "  A,\n"
12762                "};",
12763                Style);
12764   verifyFormat("typedef union Foo {\n"
12765                "  A,\n"
12766                "} Foo_t;",
12767                Style);
12768   verifyFormat("namespace Foo {\n"
12769                "void Bar();\n"
12770                "};",
12771                Style);
12772 
12773   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
12774   Style.BraceWrapping.AfterClass = true;
12775   Style.BraceWrapping.AfterStruct = true;
12776   Style.BraceWrapping.AfterUnion = true;
12777   Style.BraceWrapping.AfterNamespace = true;
12778   verifyFormat("class Foo\n"
12779                "{\n"
12780                "  Foo();\n"
12781                "};",
12782                Style);
12783   verifyFormat("typedef class Foo\n"
12784                "{\n"
12785                "  Foo();\n"
12786                "} Foo_t;",
12787                Style);
12788   verifyFormat("struct Foo\n"
12789                "{\n"
12790                "  Foo();\n"
12791                "};",
12792                Style);
12793   verifyFormat("typedef struct Foo\n"
12794                "{\n"
12795                "  Foo();\n"
12796                "} Foo_t;",
12797                Style);
12798   verifyFormat("union Foo\n"
12799                "{\n"
12800                "  A,\n"
12801                "};",
12802                Style);
12803   verifyFormat("typedef union Foo\n"
12804                "{\n"
12805                "  A,\n"
12806                "} Foo_t;",
12807                Style);
12808   verifyFormat("namespace Foo\n"
12809                "{\n"
12810                "void Bar();\n"
12811                "};",
12812                Style);
12813 }
12814 
12815 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
12816   // Elaborate type variable declarations.
12817   verifyFormat("struct foo a = {bar};\nint n;");
12818   verifyFormat("class foo a = {bar};\nint n;");
12819   verifyFormat("union foo a = {bar};\nint n;");
12820 
12821   // Elaborate types inside function definitions.
12822   verifyFormat("struct foo f() {}\nint n;");
12823   verifyFormat("class foo f() {}\nint n;");
12824   verifyFormat("union foo f() {}\nint n;");
12825 
12826   // Templates.
12827   verifyFormat("template <class X> void f() {}\nint n;");
12828   verifyFormat("template <struct X> void f() {}\nint n;");
12829   verifyFormat("template <union X> void f() {}\nint n;");
12830 
12831   // Actual definitions...
12832   verifyFormat("struct {\n} n;");
12833   verifyFormat(
12834       "template <template <class T, class Y>, class Z> class X {\n} n;");
12835   verifyFormat("union Z {\n  int n;\n} x;");
12836   verifyFormat("class MACRO Z {\n} n;");
12837   verifyFormat("class MACRO(X) Z {\n} n;");
12838   verifyFormat("class __attribute__(X) Z {\n} n;");
12839   verifyFormat("class __declspec(X) Z {\n} n;");
12840   verifyFormat("class A##B##C {\n} n;");
12841   verifyFormat("class alignas(16) Z {\n} n;");
12842   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
12843   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
12844 
12845   // Redefinition from nested context:
12846   verifyFormat("class A::B::C {\n} n;");
12847 
12848   // Template definitions.
12849   verifyFormat(
12850       "template <typename F>\n"
12851       "Matcher(const Matcher<F> &Other,\n"
12852       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
12853       "                             !is_same<F, T>::value>::type * = 0)\n"
12854       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
12855 
12856   // FIXME: This is still incorrectly handled at the formatter side.
12857   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
12858   verifyFormat("int i = SomeFunction(a<b, a> b);");
12859 
12860   // FIXME:
12861   // This now gets parsed incorrectly as class definition.
12862   // verifyFormat("class A<int> f() {\n}\nint n;");
12863 
12864   // Elaborate types where incorrectly parsing the structural element would
12865   // break the indent.
12866   verifyFormat("if (true)\n"
12867                "  class X x;\n"
12868                "else\n"
12869                "  f();\n");
12870 
12871   // This is simply incomplete. Formatting is not important, but must not crash.
12872   verifyFormat("class A:");
12873 }
12874 
12875 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
12876   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
12877             format("#error Leave     all         white!!!!! space* alone!\n"));
12878   EXPECT_EQ(
12879       "#warning Leave     all         white!!!!! space* alone!\n",
12880       format("#warning Leave     all         white!!!!! space* alone!\n"));
12881   EXPECT_EQ("#error 1", format("  #  error   1"));
12882   EXPECT_EQ("#warning 1", format("  #  warning 1"));
12883 }
12884 
12885 TEST_F(FormatTest, FormatHashIfExpressions) {
12886   verifyFormat("#if AAAA && BBBB");
12887   verifyFormat("#if (AAAA && BBBB)");
12888   verifyFormat("#elif (AAAA && BBBB)");
12889   // FIXME: Come up with a better indentation for #elif.
12890   verifyFormat(
12891       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
12892       "    defined(BBBBBBBB)\n"
12893       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
12894       "    defined(BBBBBBBB)\n"
12895       "#endif",
12896       getLLVMStyleWithColumns(65));
12897 }
12898 
12899 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
12900   FormatStyle AllowsMergedIf = getGoogleStyle();
12901   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
12902       FormatStyle::SIS_WithoutElse;
12903   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
12904   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
12905   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
12906   EXPECT_EQ("if (true) return 42;",
12907             format("if (true)\nreturn 42;", AllowsMergedIf));
12908   FormatStyle ShortMergedIf = AllowsMergedIf;
12909   ShortMergedIf.ColumnLimit = 25;
12910   verifyFormat("#define A \\\n"
12911                "  if (true) return 42;",
12912                ShortMergedIf);
12913   verifyFormat("#define A \\\n"
12914                "  f();    \\\n"
12915                "  if (true)\n"
12916                "#define B",
12917                ShortMergedIf);
12918   verifyFormat("#define A \\\n"
12919                "  f();    \\\n"
12920                "  if (true)\n"
12921                "g();",
12922                ShortMergedIf);
12923   verifyFormat("{\n"
12924                "#ifdef A\n"
12925                "  // Comment\n"
12926                "  if (true) continue;\n"
12927                "#endif\n"
12928                "  // Comment\n"
12929                "  if (true) continue;\n"
12930                "}",
12931                ShortMergedIf);
12932   ShortMergedIf.ColumnLimit = 33;
12933   verifyFormat("#define A \\\n"
12934                "  if constexpr (true) return 42;",
12935                ShortMergedIf);
12936   verifyFormat("#define A \\\n"
12937                "  if CONSTEXPR (true) return 42;",
12938                ShortMergedIf);
12939   ShortMergedIf.ColumnLimit = 29;
12940   verifyFormat("#define A                   \\\n"
12941                "  if (aaaaaaaaaa) return 1; \\\n"
12942                "  return 2;",
12943                ShortMergedIf);
12944   ShortMergedIf.ColumnLimit = 28;
12945   verifyFormat("#define A         \\\n"
12946                "  if (aaaaaaaaaa) \\\n"
12947                "    return 1;     \\\n"
12948                "  return 2;",
12949                ShortMergedIf);
12950   verifyFormat("#define A                \\\n"
12951                "  if constexpr (aaaaaaa) \\\n"
12952                "    return 1;            \\\n"
12953                "  return 2;",
12954                ShortMergedIf);
12955   verifyFormat("#define A                \\\n"
12956                "  if CONSTEXPR (aaaaaaa) \\\n"
12957                "    return 1;            \\\n"
12958                "  return 2;",
12959                ShortMergedIf);
12960 }
12961 
12962 TEST_F(FormatTest, FormatStarDependingOnContext) {
12963   verifyFormat("void f(int *a);");
12964   verifyFormat("void f() { f(fint * b); }");
12965   verifyFormat("class A {\n  void f(int *a);\n};");
12966   verifyFormat("class A {\n  int *a;\n};");
12967   verifyFormat("namespace a {\n"
12968                "namespace b {\n"
12969                "class A {\n"
12970                "  void f() {}\n"
12971                "  int *a;\n"
12972                "};\n"
12973                "} // namespace b\n"
12974                "} // namespace a");
12975 }
12976 
12977 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
12978   verifyFormat("while");
12979   verifyFormat("operator");
12980 }
12981 
12982 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
12983   // This code would be painfully slow to format if we didn't skip it.
12984   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
12985                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12986                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12987                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12988                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
12989                    "A(1, 1)\n"
12990                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
12991                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12992                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12993                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12994                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12995                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12996                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12997                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12998                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
12999                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
13000   // Deeply nested part is untouched, rest is formatted.
13001   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
13002             format(std::string("int    i;\n") + Code + "int    j;\n",
13003                    getLLVMStyle(), SC_ExpectIncomplete));
13004 }
13005 
13006 //===----------------------------------------------------------------------===//
13007 // Objective-C tests.
13008 //===----------------------------------------------------------------------===//
13009 
13010 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
13011   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
13012   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
13013             format("-(NSUInteger)indexOfObject:(id)anObject;"));
13014   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
13015   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
13016   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
13017             format("-(NSInteger)Method3:(id)anObject;"));
13018   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
13019             format("-(NSInteger)Method4:(id)anObject;"));
13020   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
13021             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
13022   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
13023             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
13024   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
13025             "forAllCells:(BOOL)flag;",
13026             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
13027                    "forAllCells:(BOOL)flag;"));
13028 
13029   // Very long objectiveC method declaration.
13030   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
13031                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
13032   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
13033                "                    inRange:(NSRange)range\n"
13034                "                   outRange:(NSRange)out_range\n"
13035                "                  outRange1:(NSRange)out_range1\n"
13036                "                  outRange2:(NSRange)out_range2\n"
13037                "                  outRange3:(NSRange)out_range3\n"
13038                "                  outRange4:(NSRange)out_range4\n"
13039                "                  outRange5:(NSRange)out_range5\n"
13040                "                  outRange6:(NSRange)out_range6\n"
13041                "                  outRange7:(NSRange)out_range7\n"
13042                "                  outRange8:(NSRange)out_range8\n"
13043                "                  outRange9:(NSRange)out_range9;");
13044 
13045   // When the function name has to be wrapped.
13046   FormatStyle Style = getLLVMStyle();
13047   // ObjC ignores IndentWrappedFunctionNames when wrapping methods
13048   // and always indents instead.
13049   Style.IndentWrappedFunctionNames = false;
13050   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
13051                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
13052                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
13053                "}",
13054                Style);
13055   Style.IndentWrappedFunctionNames = true;
13056   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
13057                "    veryLooooooooooongName:(NSString)cccccccccccccc\n"
13058                "               anotherName:(NSString)dddddddddddddd {\n"
13059                "}",
13060                Style);
13061 
13062   verifyFormat("- (int)sum:(vector<int>)numbers;");
13063   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
13064   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
13065   // protocol lists (but not for template classes):
13066   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
13067 
13068   verifyFormat("- (int (*)())foo:(int (*)())f;");
13069   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
13070 
13071   // If there's no return type (very rare in practice!), LLVM and Google style
13072   // agree.
13073   verifyFormat("- foo;");
13074   verifyFormat("- foo:(int)f;");
13075   verifyGoogleFormat("- foo:(int)foo;");
13076 }
13077 
13078 TEST_F(FormatTest, BreaksStringLiterals) {
13079   EXPECT_EQ("\"some text \"\n"
13080             "\"other\";",
13081             format("\"some text other\";", getLLVMStyleWithColumns(12)));
13082   EXPECT_EQ("\"some text \"\n"
13083             "\"other\";",
13084             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
13085   EXPECT_EQ(
13086       "#define A  \\\n"
13087       "  \"some \"  \\\n"
13088       "  \"text \"  \\\n"
13089       "  \"other\";",
13090       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
13091   EXPECT_EQ(
13092       "#define A  \\\n"
13093       "  \"so \"    \\\n"
13094       "  \"text \"  \\\n"
13095       "  \"other\";",
13096       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
13097 
13098   EXPECT_EQ("\"some text\"",
13099             format("\"some text\"", getLLVMStyleWithColumns(1)));
13100   EXPECT_EQ("\"some text\"",
13101             format("\"some text\"", getLLVMStyleWithColumns(11)));
13102   EXPECT_EQ("\"some \"\n"
13103             "\"text\"",
13104             format("\"some text\"", getLLVMStyleWithColumns(10)));
13105   EXPECT_EQ("\"some \"\n"
13106             "\"text\"",
13107             format("\"some text\"", getLLVMStyleWithColumns(7)));
13108   EXPECT_EQ("\"some\"\n"
13109             "\" tex\"\n"
13110             "\"t\"",
13111             format("\"some text\"", getLLVMStyleWithColumns(6)));
13112   EXPECT_EQ("\"some\"\n"
13113             "\" tex\"\n"
13114             "\" and\"",
13115             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
13116   EXPECT_EQ("\"some\"\n"
13117             "\"/tex\"\n"
13118             "\"/and\"",
13119             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
13120 
13121   EXPECT_EQ("variable =\n"
13122             "    \"long string \"\n"
13123             "    \"literal\";",
13124             format("variable = \"long string literal\";",
13125                    getLLVMStyleWithColumns(20)));
13126 
13127   EXPECT_EQ("variable = f(\n"
13128             "    \"long string \"\n"
13129             "    \"literal\",\n"
13130             "    short,\n"
13131             "    loooooooooooooooooooong);",
13132             format("variable = f(\"long string literal\", short, "
13133                    "loooooooooooooooooooong);",
13134                    getLLVMStyleWithColumns(20)));
13135 
13136   EXPECT_EQ(
13137       "f(g(\"long string \"\n"
13138       "    \"literal\"),\n"
13139       "  b);",
13140       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
13141   EXPECT_EQ("f(g(\"long string \"\n"
13142             "    \"literal\",\n"
13143             "    a),\n"
13144             "  b);",
13145             format("f(g(\"long string literal\", a), b);",
13146                    getLLVMStyleWithColumns(20)));
13147   EXPECT_EQ(
13148       "f(\"one two\".split(\n"
13149       "    variable));",
13150       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
13151   EXPECT_EQ("f(\"one two three four five six \"\n"
13152             "  \"seven\".split(\n"
13153             "      really_looooong_variable));",
13154             format("f(\"one two three four five six seven\"."
13155                    "split(really_looooong_variable));",
13156                    getLLVMStyleWithColumns(33)));
13157 
13158   EXPECT_EQ("f(\"some \"\n"
13159             "  \"text\",\n"
13160             "  other);",
13161             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
13162 
13163   // Only break as a last resort.
13164   verifyFormat(
13165       "aaaaaaaaaaaaaaaaaaaa(\n"
13166       "    aaaaaaaaaaaaaaaaaaaa,\n"
13167       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
13168 
13169   EXPECT_EQ("\"splitmea\"\n"
13170             "\"trandomp\"\n"
13171             "\"oint\"",
13172             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
13173 
13174   EXPECT_EQ("\"split/\"\n"
13175             "\"pathat/\"\n"
13176             "\"slashes\"",
13177             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
13178 
13179   EXPECT_EQ("\"split/\"\n"
13180             "\"pathat/\"\n"
13181             "\"slashes\"",
13182             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
13183   EXPECT_EQ("\"split at \"\n"
13184             "\"spaces/at/\"\n"
13185             "\"slashes.at.any$\"\n"
13186             "\"non-alphanumeric%\"\n"
13187             "\"1111111111characte\"\n"
13188             "\"rs\"",
13189             format("\"split at "
13190                    "spaces/at/"
13191                    "slashes.at."
13192                    "any$non-"
13193                    "alphanumeric%"
13194                    "1111111111characte"
13195                    "rs\"",
13196                    getLLVMStyleWithColumns(20)));
13197 
13198   // Verify that splitting the strings understands
13199   // Style::AlwaysBreakBeforeMultilineStrings.
13200   EXPECT_EQ("aaaaaaaaaaaa(\n"
13201             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
13202             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
13203             format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
13204                    "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
13205                    "aaaaaaaaaaaaaaaaaaaaaa\");",
13206                    getGoogleStyle()));
13207   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
13208             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
13209             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
13210                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
13211                    "aaaaaaaaaaaaaaaaaaaaaa\";",
13212                    getGoogleStyle()));
13213   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
13214             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
13215             format("llvm::outs() << "
13216                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
13217                    "aaaaaaaaaaaaaaaaaaa\";"));
13218   EXPECT_EQ("ffff(\n"
13219             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
13220             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
13221             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
13222                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
13223                    getGoogleStyle()));
13224 
13225   FormatStyle Style = getLLVMStyleWithColumns(12);
13226   Style.BreakStringLiterals = false;
13227   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
13228 
13229   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
13230   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
13231   EXPECT_EQ("#define A \\\n"
13232             "  \"some \" \\\n"
13233             "  \"text \" \\\n"
13234             "  \"other\";",
13235             format("#define A \"some text other\";", AlignLeft));
13236 }
13237 
13238 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
13239   EXPECT_EQ("C a = \"some more \"\n"
13240             "      \"text\";",
13241             format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
13242 }
13243 
13244 TEST_F(FormatTest, FullyRemoveEmptyLines) {
13245   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
13246   NoEmptyLines.MaxEmptyLinesToKeep = 0;
13247   EXPECT_EQ("int i = a(b());",
13248             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
13249 }
13250 
13251 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
13252   EXPECT_EQ(
13253       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
13254       "(\n"
13255       "    \"x\t\");",
13256       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
13257              "aaaaaaa("
13258              "\"x\t\");"));
13259 }
13260 
13261 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
13262   EXPECT_EQ(
13263       "u8\"utf8 string \"\n"
13264       "u8\"literal\";",
13265       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
13266   EXPECT_EQ(
13267       "u\"utf16 string \"\n"
13268       "u\"literal\";",
13269       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
13270   EXPECT_EQ(
13271       "U\"utf32 string \"\n"
13272       "U\"literal\";",
13273       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
13274   EXPECT_EQ("L\"wide string \"\n"
13275             "L\"literal\";",
13276             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
13277   EXPECT_EQ("@\"NSString \"\n"
13278             "@\"literal\";",
13279             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
13280   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
13281 
13282   // This input makes clang-format try to split the incomplete unicode escape
13283   // sequence, which used to lead to a crasher.
13284   verifyNoCrash(
13285       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
13286       getLLVMStyleWithColumns(60));
13287 }
13288 
13289 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
13290   FormatStyle Style = getGoogleStyleWithColumns(15);
13291   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
13292   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
13293   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
13294   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
13295   EXPECT_EQ("u8R\"x(raw literal)x\";",
13296             format("u8R\"x(raw literal)x\";", Style));
13297 }
13298 
13299 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
13300   FormatStyle Style = getLLVMStyleWithColumns(20);
13301   EXPECT_EQ(
13302       "_T(\"aaaaaaaaaaaaaa\")\n"
13303       "_T(\"aaaaaaaaaaaaaa\")\n"
13304       "_T(\"aaaaaaaaaaaa\")",
13305       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
13306   EXPECT_EQ("f(x,\n"
13307             "  _T(\"aaaaaaaaaaaa\")\n"
13308             "  _T(\"aaa\"),\n"
13309             "  z);",
13310             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
13311 
13312   // FIXME: Handle embedded spaces in one iteration.
13313   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
13314   //            "_T(\"aaaaaaaaaaaaa\")\n"
13315   //            "_T(\"aaaaaaaaaaaaa\")\n"
13316   //            "_T(\"a\")",
13317   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
13318   //                   getLLVMStyleWithColumns(20)));
13319   EXPECT_EQ(
13320       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
13321       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
13322   EXPECT_EQ("f(\n"
13323             "#if !TEST\n"
13324             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
13325             "#endif\n"
13326             ");",
13327             format("f(\n"
13328                    "#if !TEST\n"
13329                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
13330                    "#endif\n"
13331                    ");"));
13332   EXPECT_EQ("f(\n"
13333             "\n"
13334             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
13335             format("f(\n"
13336                    "\n"
13337                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
13338   // Regression test for accessing tokens past the end of a vector in the
13339   // TokenLexer.
13340   verifyNoCrash(R"(_T(
13341 "
13342 )
13343 )");
13344 }
13345 
13346 TEST_F(FormatTest, BreaksStringLiteralOperands) {
13347   // In a function call with two operands, the second can be broken with no line
13348   // break before it.
13349   EXPECT_EQ(
13350       "func(a, \"long long \"\n"
13351       "        \"long long\");",
13352       format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24)));
13353   // In a function call with three operands, the second must be broken with a
13354   // line break before it.
13355   EXPECT_EQ("func(a,\n"
13356             "     \"long long long \"\n"
13357             "     \"long\",\n"
13358             "     c);",
13359             format("func(a, \"long long long long\", c);",
13360                    getLLVMStyleWithColumns(24)));
13361   // In a function call with three operands, the third must be broken with a
13362   // line break before it.
13363   EXPECT_EQ("func(a, b,\n"
13364             "     \"long long long \"\n"
13365             "     \"long\");",
13366             format("func(a, b, \"long long long long\");",
13367                    getLLVMStyleWithColumns(24)));
13368   // In a function call with three operands, both the second and the third must
13369   // be broken with a line break before them.
13370   EXPECT_EQ("func(a,\n"
13371             "     \"long long long \"\n"
13372             "     \"long\",\n"
13373             "     \"long long long \"\n"
13374             "     \"long\");",
13375             format("func(a, \"long long long long\", \"long long long long\");",
13376                    getLLVMStyleWithColumns(24)));
13377   // In a chain of << with two operands, the second can be broken with no line
13378   // break before it.
13379   EXPECT_EQ("a << \"line line \"\n"
13380             "     \"line\";",
13381             format("a << \"line line line\";", getLLVMStyleWithColumns(20)));
13382   // In a chain of << with three operands, the second can be broken with no line
13383   // break before it.
13384   EXPECT_EQ(
13385       "abcde << \"line \"\n"
13386       "         \"line line\"\n"
13387       "      << c;",
13388       format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20)));
13389   // In a chain of << with three operands, the third must be broken with a line
13390   // break before it.
13391   EXPECT_EQ(
13392       "a << b\n"
13393       "  << \"line line \"\n"
13394       "     \"line\";",
13395       format("a << b << \"line line line\";", getLLVMStyleWithColumns(20)));
13396   // In a chain of << with three operands, the second can be broken with no line
13397   // break before it and the third must be broken with a line break before it.
13398   EXPECT_EQ("abcd << \"line line \"\n"
13399             "        \"line\"\n"
13400             "     << \"line line \"\n"
13401             "        \"line\";",
13402             format("abcd << \"line line line\" << \"line line line\";",
13403                    getLLVMStyleWithColumns(20)));
13404   // In a chain of binary operators with two operands, the second can be broken
13405   // with no line break before it.
13406   EXPECT_EQ(
13407       "abcd + \"line line \"\n"
13408       "       \"line line\";",
13409       format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20)));
13410   // In a chain of binary operators with three operands, the second must be
13411   // broken with a line break before it.
13412   EXPECT_EQ("abcd +\n"
13413             "    \"line line \"\n"
13414             "    \"line line\" +\n"
13415             "    e;",
13416             format("abcd + \"line line line line\" + e;",
13417                    getLLVMStyleWithColumns(20)));
13418   // In a function call with two operands, with AlignAfterOpenBracket enabled,
13419   // the first must be broken with a line break before it.
13420   FormatStyle Style = getLLVMStyleWithColumns(25);
13421   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
13422   EXPECT_EQ("someFunction(\n"
13423             "    \"long long long \"\n"
13424             "    \"long\",\n"
13425             "    a);",
13426             format("someFunction(\"long long long long\", a);", Style));
13427 }
13428 
13429 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
13430   EXPECT_EQ(
13431       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13432       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13433       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
13434       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13435              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
13436              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
13437 }
13438 
13439 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
13440   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
13441             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
13442   EXPECT_EQ("fffffffffff(g(R\"x(\n"
13443             "multiline raw string literal xxxxxxxxxxxxxx\n"
13444             ")x\",\n"
13445             "              a),\n"
13446             "            b);",
13447             format("fffffffffff(g(R\"x(\n"
13448                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13449                    ")x\", a), b);",
13450                    getGoogleStyleWithColumns(20)));
13451   EXPECT_EQ("fffffffffff(\n"
13452             "    g(R\"x(qqq\n"
13453             "multiline raw string literal xxxxxxxxxxxxxx\n"
13454             ")x\",\n"
13455             "      a),\n"
13456             "    b);",
13457             format("fffffffffff(g(R\"x(qqq\n"
13458                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13459                    ")x\", a), b);",
13460                    getGoogleStyleWithColumns(20)));
13461 
13462   EXPECT_EQ("fffffffffff(R\"x(\n"
13463             "multiline raw string literal xxxxxxxxxxxxxx\n"
13464             ")x\");",
13465             format("fffffffffff(R\"x(\n"
13466                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13467                    ")x\");",
13468                    getGoogleStyleWithColumns(20)));
13469   EXPECT_EQ("fffffffffff(R\"x(\n"
13470             "multiline raw string literal xxxxxxxxxxxxxx\n"
13471             ")x\" + bbbbbb);",
13472             format("fffffffffff(R\"x(\n"
13473                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13474                    ")x\" +   bbbbbb);",
13475                    getGoogleStyleWithColumns(20)));
13476   EXPECT_EQ("fffffffffff(\n"
13477             "    R\"x(\n"
13478             "multiline raw string literal xxxxxxxxxxxxxx\n"
13479             ")x\" +\n"
13480             "    bbbbbb);",
13481             format("fffffffffff(\n"
13482                    " R\"x(\n"
13483                    "multiline raw string literal xxxxxxxxxxxxxx\n"
13484                    ")x\" + bbbbbb);",
13485                    getGoogleStyleWithColumns(20)));
13486   EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
13487             format("fffffffffff(\n"
13488                    " R\"(single line raw string)\" + bbbbbb);"));
13489 }
13490 
13491 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
13492   verifyFormat("string a = \"unterminated;");
13493   EXPECT_EQ("function(\"unterminated,\n"
13494             "         OtherParameter);",
13495             format("function(  \"unterminated,\n"
13496                    "    OtherParameter);"));
13497 }
13498 
13499 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
13500   FormatStyle Style = getLLVMStyle();
13501   Style.Standard = FormatStyle::LS_Cpp03;
13502   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
13503             format("#define x(_a) printf(\"foo\"_a);", Style));
13504 }
13505 
13506 TEST_F(FormatTest, CppLexVersion) {
13507   FormatStyle Style = getLLVMStyle();
13508   // Formatting of x * y differs if x is a type.
13509   verifyFormat("void foo() { MACRO(a * b); }", Style);
13510   verifyFormat("void foo() { MACRO(int *b); }", Style);
13511 
13512   // LLVM style uses latest lexer.
13513   verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
13514   Style.Standard = FormatStyle::LS_Cpp17;
13515   // But in c++17, char8_t isn't a keyword.
13516   verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
13517 }
13518 
13519 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
13520 
13521 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
13522   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
13523             "             \"ddeeefff\");",
13524             format("someFunction(\"aaabbbcccdddeeefff\");",
13525                    getLLVMStyleWithColumns(25)));
13526   EXPECT_EQ("someFunction1234567890(\n"
13527             "    \"aaabbbcccdddeeefff\");",
13528             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13529                    getLLVMStyleWithColumns(26)));
13530   EXPECT_EQ("someFunction1234567890(\n"
13531             "    \"aaabbbcccdddeeeff\"\n"
13532             "    \"f\");",
13533             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13534                    getLLVMStyleWithColumns(25)));
13535   EXPECT_EQ("someFunction1234567890(\n"
13536             "    \"aaabbbcccdddeeeff\"\n"
13537             "    \"f\");",
13538             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
13539                    getLLVMStyleWithColumns(24)));
13540   EXPECT_EQ("someFunction(\n"
13541             "    \"aaabbbcc ddde \"\n"
13542             "    \"efff\");",
13543             format("someFunction(\"aaabbbcc ddde efff\");",
13544                    getLLVMStyleWithColumns(25)));
13545   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
13546             "             \"ddeeefff\");",
13547             format("someFunction(\"aaabbbccc ddeeefff\");",
13548                    getLLVMStyleWithColumns(25)));
13549   EXPECT_EQ("someFunction1234567890(\n"
13550             "    \"aaabb \"\n"
13551             "    \"cccdddeeefff\");",
13552             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
13553                    getLLVMStyleWithColumns(25)));
13554   EXPECT_EQ("#define A          \\\n"
13555             "  string s =       \\\n"
13556             "      \"123456789\"  \\\n"
13557             "      \"0\";         \\\n"
13558             "  int i;",
13559             format("#define A string s = \"1234567890\"; int i;",
13560                    getLLVMStyleWithColumns(20)));
13561   EXPECT_EQ("someFunction(\n"
13562             "    \"aaabbbcc \"\n"
13563             "    \"dddeeefff\");",
13564             format("someFunction(\"aaabbbcc dddeeefff\");",
13565                    getLLVMStyleWithColumns(25)));
13566 }
13567 
13568 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
13569   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
13570   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
13571   EXPECT_EQ("\"test\"\n"
13572             "\"\\n\"",
13573             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
13574   EXPECT_EQ("\"tes\\\\\"\n"
13575             "\"n\"",
13576             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
13577   EXPECT_EQ("\"\\\\\\\\\"\n"
13578             "\"\\n\"",
13579             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
13580   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
13581   EXPECT_EQ("\"\\uff01\"\n"
13582             "\"test\"",
13583             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
13584   EXPECT_EQ("\"\\Uff01ff02\"",
13585             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
13586   EXPECT_EQ("\"\\x000000000001\"\n"
13587             "\"next\"",
13588             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
13589   EXPECT_EQ("\"\\x000000000001next\"",
13590             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
13591   EXPECT_EQ("\"\\x000000000001\"",
13592             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
13593   EXPECT_EQ("\"test\"\n"
13594             "\"\\000000\"\n"
13595             "\"000001\"",
13596             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
13597   EXPECT_EQ("\"test\\000\"\n"
13598             "\"00000000\"\n"
13599             "\"1\"",
13600             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
13601 }
13602 
13603 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
13604   verifyFormat("void f() {\n"
13605                "  return g() {}\n"
13606                "  void h() {}");
13607   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
13608                "g();\n"
13609                "}");
13610 }
13611 
13612 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
13613   verifyFormat(
13614       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
13615 }
13616 
13617 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
13618   verifyFormat("class X {\n"
13619                "  void f() {\n"
13620                "  }\n"
13621                "};",
13622                getLLVMStyleWithColumns(12));
13623 }
13624 
13625 TEST_F(FormatTest, ConfigurableIndentWidth) {
13626   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
13627   EightIndent.IndentWidth = 8;
13628   EightIndent.ContinuationIndentWidth = 8;
13629   verifyFormat("void f() {\n"
13630                "        someFunction();\n"
13631                "        if (true) {\n"
13632                "                f();\n"
13633                "        }\n"
13634                "}",
13635                EightIndent);
13636   verifyFormat("class X {\n"
13637                "        void f() {\n"
13638                "        }\n"
13639                "};",
13640                EightIndent);
13641   verifyFormat("int x[] = {\n"
13642                "        call(),\n"
13643                "        call()};",
13644                EightIndent);
13645 }
13646 
13647 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
13648   verifyFormat("double\n"
13649                "f();",
13650                getLLVMStyleWithColumns(8));
13651 }
13652 
13653 TEST_F(FormatTest, ConfigurableUseOfTab) {
13654   FormatStyle Tab = getLLVMStyleWithColumns(42);
13655   Tab.IndentWidth = 8;
13656   Tab.UseTab = FormatStyle::UT_Always;
13657   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
13658 
13659   EXPECT_EQ("if (aaaaaaaa && // q\n"
13660             "    bb)\t\t// w\n"
13661             "\t;",
13662             format("if (aaaaaaaa &&// q\n"
13663                    "bb)// w\n"
13664                    ";",
13665                    Tab));
13666   EXPECT_EQ("if (aaa && bbb) // w\n"
13667             "\t;",
13668             format("if(aaa&&bbb)// w\n"
13669                    ";",
13670                    Tab));
13671 
13672   verifyFormat("class X {\n"
13673                "\tvoid f() {\n"
13674                "\t\tsomeFunction(parameter1,\n"
13675                "\t\t\t     parameter2);\n"
13676                "\t}\n"
13677                "};",
13678                Tab);
13679   verifyFormat("#define A                        \\\n"
13680                "\tvoid f() {               \\\n"
13681                "\t\tsomeFunction(    \\\n"
13682                "\t\t    parameter1,  \\\n"
13683                "\t\t    parameter2); \\\n"
13684                "\t}",
13685                Tab);
13686   verifyFormat("int a;\t      // x\n"
13687                "int bbbbbbbb; // x\n",
13688                Tab);
13689 
13690   Tab.TabWidth = 4;
13691   Tab.IndentWidth = 8;
13692   verifyFormat("class TabWidth4Indent8 {\n"
13693                "\t\tvoid f() {\n"
13694                "\t\t\t\tsomeFunction(parameter1,\n"
13695                "\t\t\t\t\t\t\t parameter2);\n"
13696                "\t\t}\n"
13697                "};",
13698                Tab);
13699 
13700   Tab.TabWidth = 4;
13701   Tab.IndentWidth = 4;
13702   verifyFormat("class TabWidth4Indent4 {\n"
13703                "\tvoid f() {\n"
13704                "\t\tsomeFunction(parameter1,\n"
13705                "\t\t\t\t\t parameter2);\n"
13706                "\t}\n"
13707                "};",
13708                Tab);
13709 
13710   Tab.TabWidth = 8;
13711   Tab.IndentWidth = 4;
13712   verifyFormat("class TabWidth8Indent4 {\n"
13713                "    void f() {\n"
13714                "\tsomeFunction(parameter1,\n"
13715                "\t\t     parameter2);\n"
13716                "    }\n"
13717                "};",
13718                Tab);
13719 
13720   Tab.TabWidth = 8;
13721   Tab.IndentWidth = 8;
13722   EXPECT_EQ("/*\n"
13723             "\t      a\t\tcomment\n"
13724             "\t      in multiple lines\n"
13725             "       */",
13726             format("   /*\t \t \n"
13727                    " \t \t a\t\tcomment\t \t\n"
13728                    " \t \t in multiple lines\t\n"
13729                    " \t  */",
13730                    Tab));
13731 
13732   Tab.UseTab = FormatStyle::UT_ForIndentation;
13733   verifyFormat("{\n"
13734                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13735                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13736                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13737                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13738                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13739                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13740                "};",
13741                Tab);
13742   verifyFormat("enum AA {\n"
13743                "\ta1, // Force multiple lines\n"
13744                "\ta2,\n"
13745                "\ta3\n"
13746                "};",
13747                Tab);
13748   EXPECT_EQ("if (aaaaaaaa && // q\n"
13749             "    bb)         // w\n"
13750             "\t;",
13751             format("if (aaaaaaaa &&// q\n"
13752                    "bb)// w\n"
13753                    ";",
13754                    Tab));
13755   verifyFormat("class X {\n"
13756                "\tvoid f() {\n"
13757                "\t\tsomeFunction(parameter1,\n"
13758                "\t\t             parameter2);\n"
13759                "\t}\n"
13760                "};",
13761                Tab);
13762   verifyFormat("{\n"
13763                "\tQ(\n"
13764                "\t    {\n"
13765                "\t\t    int a;\n"
13766                "\t\t    someFunction(aaaaaaaa,\n"
13767                "\t\t                 bbbbbbb);\n"
13768                "\t    },\n"
13769                "\t    p);\n"
13770                "}",
13771                Tab);
13772   EXPECT_EQ("{\n"
13773             "\t/* aaaa\n"
13774             "\t   bbbb */\n"
13775             "}",
13776             format("{\n"
13777                    "/* aaaa\n"
13778                    "   bbbb */\n"
13779                    "}",
13780                    Tab));
13781   EXPECT_EQ("{\n"
13782             "\t/*\n"
13783             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13784             "\t  bbbbbbbbbbbbb\n"
13785             "\t*/\n"
13786             "}",
13787             format("{\n"
13788                    "/*\n"
13789                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13790                    "*/\n"
13791                    "}",
13792                    Tab));
13793   EXPECT_EQ("{\n"
13794             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13795             "\t// bbbbbbbbbbbbb\n"
13796             "}",
13797             format("{\n"
13798                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13799                    "}",
13800                    Tab));
13801   EXPECT_EQ("{\n"
13802             "\t/*\n"
13803             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13804             "\t  bbbbbbbbbbbbb\n"
13805             "\t*/\n"
13806             "}",
13807             format("{\n"
13808                    "\t/*\n"
13809                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
13810                    "\t*/\n"
13811                    "}",
13812                    Tab));
13813   EXPECT_EQ("{\n"
13814             "\t/*\n"
13815             "\n"
13816             "\t*/\n"
13817             "}",
13818             format("{\n"
13819                    "\t/*\n"
13820                    "\n"
13821                    "\t*/\n"
13822                    "}",
13823                    Tab));
13824   EXPECT_EQ("{\n"
13825             "\t/*\n"
13826             " asdf\n"
13827             "\t*/\n"
13828             "}",
13829             format("{\n"
13830                    "\t/*\n"
13831                    " asdf\n"
13832                    "\t*/\n"
13833                    "}",
13834                    Tab));
13835 
13836   verifyFormat("void f() {\n"
13837                "\treturn true ? aaaaaaaaaaaaaaaaaa\n"
13838                "\t            : bbbbbbbbbbbbbbbbbb\n"
13839                "}",
13840                Tab);
13841   FormatStyle TabNoBreak = Tab;
13842   TabNoBreak.BreakBeforeTernaryOperators = false;
13843   verifyFormat("void f() {\n"
13844                "\treturn true ? aaaaaaaaaaaaaaaaaa :\n"
13845                "\t              bbbbbbbbbbbbbbbbbb\n"
13846                "}",
13847                TabNoBreak);
13848   verifyFormat("void f() {\n"
13849                "\treturn true ?\n"
13850                "\t           aaaaaaaaaaaaaaaaaaaa :\n"
13851                "\t           bbbbbbbbbbbbbbbbbbbb\n"
13852                "}",
13853                TabNoBreak);
13854 
13855   Tab.UseTab = FormatStyle::UT_Never;
13856   EXPECT_EQ("/*\n"
13857             "              a\t\tcomment\n"
13858             "              in multiple lines\n"
13859             "       */",
13860             format("   /*\t \t \n"
13861                    " \t \t a\t\tcomment\t \t\n"
13862                    " \t \t in multiple lines\t\n"
13863                    " \t  */",
13864                    Tab));
13865   EXPECT_EQ("/* some\n"
13866             "   comment */",
13867             format(" \t \t /* some\n"
13868                    " \t \t    comment */",
13869                    Tab));
13870   EXPECT_EQ("int a; /* some\n"
13871             "   comment */",
13872             format(" \t \t int a; /* some\n"
13873                    " \t \t    comment */",
13874                    Tab));
13875 
13876   EXPECT_EQ("int a; /* some\n"
13877             "comment */",
13878             format(" \t \t int\ta; /* some\n"
13879                    " \t \t    comment */",
13880                    Tab));
13881   EXPECT_EQ("f(\"\t\t\"); /* some\n"
13882             "    comment */",
13883             format(" \t \t f(\"\t\t\"); /* some\n"
13884                    " \t \t    comment */",
13885                    Tab));
13886   EXPECT_EQ("{\n"
13887             "        /*\n"
13888             "         * Comment\n"
13889             "         */\n"
13890             "        int i;\n"
13891             "}",
13892             format("{\n"
13893                    "\t/*\n"
13894                    "\t * Comment\n"
13895                    "\t */\n"
13896                    "\t int i;\n"
13897                    "}",
13898                    Tab));
13899 
13900   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
13901   Tab.TabWidth = 8;
13902   Tab.IndentWidth = 8;
13903   EXPECT_EQ("if (aaaaaaaa && // q\n"
13904             "    bb)         // w\n"
13905             "\t;",
13906             format("if (aaaaaaaa &&// q\n"
13907                    "bb)// w\n"
13908                    ";",
13909                    Tab));
13910   EXPECT_EQ("if (aaa && bbb) // w\n"
13911             "\t;",
13912             format("if(aaa&&bbb)// w\n"
13913                    ";",
13914                    Tab));
13915   verifyFormat("class X {\n"
13916                "\tvoid f() {\n"
13917                "\t\tsomeFunction(parameter1,\n"
13918                "\t\t\t     parameter2);\n"
13919                "\t}\n"
13920                "};",
13921                Tab);
13922   verifyFormat("#define A                        \\\n"
13923                "\tvoid f() {               \\\n"
13924                "\t\tsomeFunction(    \\\n"
13925                "\t\t    parameter1,  \\\n"
13926                "\t\t    parameter2); \\\n"
13927                "\t}",
13928                Tab);
13929   Tab.TabWidth = 4;
13930   Tab.IndentWidth = 8;
13931   verifyFormat("class TabWidth4Indent8 {\n"
13932                "\t\tvoid f() {\n"
13933                "\t\t\t\tsomeFunction(parameter1,\n"
13934                "\t\t\t\t\t\t\t parameter2);\n"
13935                "\t\t}\n"
13936                "};",
13937                Tab);
13938   Tab.TabWidth = 4;
13939   Tab.IndentWidth = 4;
13940   verifyFormat("class TabWidth4Indent4 {\n"
13941                "\tvoid f() {\n"
13942                "\t\tsomeFunction(parameter1,\n"
13943                "\t\t\t\t\t parameter2);\n"
13944                "\t}\n"
13945                "};",
13946                Tab);
13947   Tab.TabWidth = 8;
13948   Tab.IndentWidth = 4;
13949   verifyFormat("class TabWidth8Indent4 {\n"
13950                "    void f() {\n"
13951                "\tsomeFunction(parameter1,\n"
13952                "\t\t     parameter2);\n"
13953                "    }\n"
13954                "};",
13955                Tab);
13956   Tab.TabWidth = 8;
13957   Tab.IndentWidth = 8;
13958   EXPECT_EQ("/*\n"
13959             "\t      a\t\tcomment\n"
13960             "\t      in multiple lines\n"
13961             "       */",
13962             format("   /*\t \t \n"
13963                    " \t \t a\t\tcomment\t \t\n"
13964                    " \t \t in multiple lines\t\n"
13965                    " \t  */",
13966                    Tab));
13967   verifyFormat("{\n"
13968                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13969                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13970                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13971                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13972                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13973                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
13974                "};",
13975                Tab);
13976   verifyFormat("enum AA {\n"
13977                "\ta1, // Force multiple lines\n"
13978                "\ta2,\n"
13979                "\ta3\n"
13980                "};",
13981                Tab);
13982   EXPECT_EQ("if (aaaaaaaa && // q\n"
13983             "    bb)         // w\n"
13984             "\t;",
13985             format("if (aaaaaaaa &&// q\n"
13986                    "bb)// w\n"
13987                    ";",
13988                    Tab));
13989   verifyFormat("class X {\n"
13990                "\tvoid f() {\n"
13991                "\t\tsomeFunction(parameter1,\n"
13992                "\t\t\t     parameter2);\n"
13993                "\t}\n"
13994                "};",
13995                Tab);
13996   verifyFormat("{\n"
13997                "\tQ(\n"
13998                "\t    {\n"
13999                "\t\t    int a;\n"
14000                "\t\t    someFunction(aaaaaaaa,\n"
14001                "\t\t\t\t bbbbbbb);\n"
14002                "\t    },\n"
14003                "\t    p);\n"
14004                "}",
14005                Tab);
14006   EXPECT_EQ("{\n"
14007             "\t/* aaaa\n"
14008             "\t   bbbb */\n"
14009             "}",
14010             format("{\n"
14011                    "/* aaaa\n"
14012                    "   bbbb */\n"
14013                    "}",
14014                    Tab));
14015   EXPECT_EQ("{\n"
14016             "\t/*\n"
14017             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14018             "\t  bbbbbbbbbbbbb\n"
14019             "\t*/\n"
14020             "}",
14021             format("{\n"
14022                    "/*\n"
14023                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14024                    "*/\n"
14025                    "}",
14026                    Tab));
14027   EXPECT_EQ("{\n"
14028             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14029             "\t// bbbbbbbbbbbbb\n"
14030             "}",
14031             format("{\n"
14032                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14033                    "}",
14034                    Tab));
14035   EXPECT_EQ("{\n"
14036             "\t/*\n"
14037             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14038             "\t  bbbbbbbbbbbbb\n"
14039             "\t*/\n"
14040             "}",
14041             format("{\n"
14042                    "\t/*\n"
14043                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14044                    "\t*/\n"
14045                    "}",
14046                    Tab));
14047   EXPECT_EQ("{\n"
14048             "\t/*\n"
14049             "\n"
14050             "\t*/\n"
14051             "}",
14052             format("{\n"
14053                    "\t/*\n"
14054                    "\n"
14055                    "\t*/\n"
14056                    "}",
14057                    Tab));
14058   EXPECT_EQ("{\n"
14059             "\t/*\n"
14060             " asdf\n"
14061             "\t*/\n"
14062             "}",
14063             format("{\n"
14064                    "\t/*\n"
14065                    " asdf\n"
14066                    "\t*/\n"
14067                    "}",
14068                    Tab));
14069   EXPECT_EQ("/* some\n"
14070             "   comment */",
14071             format(" \t \t /* some\n"
14072                    " \t \t    comment */",
14073                    Tab));
14074   EXPECT_EQ("int a; /* some\n"
14075             "   comment */",
14076             format(" \t \t int a; /* some\n"
14077                    " \t \t    comment */",
14078                    Tab));
14079   EXPECT_EQ("int a; /* some\n"
14080             "comment */",
14081             format(" \t \t int\ta; /* some\n"
14082                    " \t \t    comment */",
14083                    Tab));
14084   EXPECT_EQ("f(\"\t\t\"); /* some\n"
14085             "    comment */",
14086             format(" \t \t f(\"\t\t\"); /* some\n"
14087                    " \t \t    comment */",
14088                    Tab));
14089   EXPECT_EQ("{\n"
14090             "\t/*\n"
14091             "\t * Comment\n"
14092             "\t */\n"
14093             "\tint i;\n"
14094             "}",
14095             format("{\n"
14096                    "\t/*\n"
14097                    "\t * Comment\n"
14098                    "\t */\n"
14099                    "\t int i;\n"
14100                    "}",
14101                    Tab));
14102   Tab.TabWidth = 2;
14103   Tab.IndentWidth = 2;
14104   EXPECT_EQ("{\n"
14105             "\t/* aaaa\n"
14106             "\t\t bbbb */\n"
14107             "}",
14108             format("{\n"
14109                    "/* aaaa\n"
14110                    "\t bbbb */\n"
14111                    "}",
14112                    Tab));
14113   EXPECT_EQ("{\n"
14114             "\t/*\n"
14115             "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14116             "\t\tbbbbbbbbbbbbb\n"
14117             "\t*/\n"
14118             "}",
14119             format("{\n"
14120                    "/*\n"
14121                    "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14122                    "*/\n"
14123                    "}",
14124                    Tab));
14125   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
14126   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
14127   Tab.TabWidth = 4;
14128   Tab.IndentWidth = 4;
14129   verifyFormat("class Assign {\n"
14130                "\tvoid f() {\n"
14131                "\t\tint         x      = 123;\n"
14132                "\t\tint         random = 4;\n"
14133                "\t\tstd::string alphabet =\n"
14134                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
14135                "\t}\n"
14136                "};",
14137                Tab);
14138 
14139   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
14140   Tab.TabWidth = 8;
14141   Tab.IndentWidth = 8;
14142   EXPECT_EQ("if (aaaaaaaa && // q\n"
14143             "    bb)         // w\n"
14144             "\t;",
14145             format("if (aaaaaaaa &&// q\n"
14146                    "bb)// w\n"
14147                    ";",
14148                    Tab));
14149   EXPECT_EQ("if (aaa && bbb) // w\n"
14150             "\t;",
14151             format("if(aaa&&bbb)// w\n"
14152                    ";",
14153                    Tab));
14154   verifyFormat("class X {\n"
14155                "\tvoid f() {\n"
14156                "\t\tsomeFunction(parameter1,\n"
14157                "\t\t             parameter2);\n"
14158                "\t}\n"
14159                "};",
14160                Tab);
14161   verifyFormat("#define A                        \\\n"
14162                "\tvoid f() {               \\\n"
14163                "\t\tsomeFunction(    \\\n"
14164                "\t\t    parameter1,  \\\n"
14165                "\t\t    parameter2); \\\n"
14166                "\t}",
14167                Tab);
14168   Tab.TabWidth = 4;
14169   Tab.IndentWidth = 8;
14170   verifyFormat("class TabWidth4Indent8 {\n"
14171                "\t\tvoid f() {\n"
14172                "\t\t\t\tsomeFunction(parameter1,\n"
14173                "\t\t\t\t             parameter2);\n"
14174                "\t\t}\n"
14175                "};",
14176                Tab);
14177   Tab.TabWidth = 4;
14178   Tab.IndentWidth = 4;
14179   verifyFormat("class TabWidth4Indent4 {\n"
14180                "\tvoid f() {\n"
14181                "\t\tsomeFunction(parameter1,\n"
14182                "\t\t             parameter2);\n"
14183                "\t}\n"
14184                "};",
14185                Tab);
14186   Tab.TabWidth = 8;
14187   Tab.IndentWidth = 4;
14188   verifyFormat("class TabWidth8Indent4 {\n"
14189                "    void f() {\n"
14190                "\tsomeFunction(parameter1,\n"
14191                "\t             parameter2);\n"
14192                "    }\n"
14193                "};",
14194                Tab);
14195   Tab.TabWidth = 8;
14196   Tab.IndentWidth = 8;
14197   EXPECT_EQ("/*\n"
14198             "              a\t\tcomment\n"
14199             "              in multiple lines\n"
14200             "       */",
14201             format("   /*\t \t \n"
14202                    " \t \t a\t\tcomment\t \t\n"
14203                    " \t \t in multiple lines\t\n"
14204                    " \t  */",
14205                    Tab));
14206   verifyFormat("{\n"
14207                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14208                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14209                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14210                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14211                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14212                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
14213                "};",
14214                Tab);
14215   verifyFormat("enum AA {\n"
14216                "\ta1, // Force multiple lines\n"
14217                "\ta2,\n"
14218                "\ta3\n"
14219                "};",
14220                Tab);
14221   EXPECT_EQ("if (aaaaaaaa && // q\n"
14222             "    bb)         // w\n"
14223             "\t;",
14224             format("if (aaaaaaaa &&// q\n"
14225                    "bb)// w\n"
14226                    ";",
14227                    Tab));
14228   verifyFormat("class X {\n"
14229                "\tvoid f() {\n"
14230                "\t\tsomeFunction(parameter1,\n"
14231                "\t\t             parameter2);\n"
14232                "\t}\n"
14233                "};",
14234                Tab);
14235   verifyFormat("{\n"
14236                "\tQ(\n"
14237                "\t    {\n"
14238                "\t\t    int a;\n"
14239                "\t\t    someFunction(aaaaaaaa,\n"
14240                "\t\t                 bbbbbbb);\n"
14241                "\t    },\n"
14242                "\t    p);\n"
14243                "}",
14244                Tab);
14245   EXPECT_EQ("{\n"
14246             "\t/* aaaa\n"
14247             "\t   bbbb */\n"
14248             "}",
14249             format("{\n"
14250                    "/* aaaa\n"
14251                    "   bbbb */\n"
14252                    "}",
14253                    Tab));
14254   EXPECT_EQ("{\n"
14255             "\t/*\n"
14256             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14257             "\t  bbbbbbbbbbbbb\n"
14258             "\t*/\n"
14259             "}",
14260             format("{\n"
14261                    "/*\n"
14262                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14263                    "*/\n"
14264                    "}",
14265                    Tab));
14266   EXPECT_EQ("{\n"
14267             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14268             "\t// bbbbbbbbbbbbb\n"
14269             "}",
14270             format("{\n"
14271                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14272                    "}",
14273                    Tab));
14274   EXPECT_EQ("{\n"
14275             "\t/*\n"
14276             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14277             "\t  bbbbbbbbbbbbb\n"
14278             "\t*/\n"
14279             "}",
14280             format("{\n"
14281                    "\t/*\n"
14282                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14283                    "\t*/\n"
14284                    "}",
14285                    Tab));
14286   EXPECT_EQ("{\n"
14287             "\t/*\n"
14288             "\n"
14289             "\t*/\n"
14290             "}",
14291             format("{\n"
14292                    "\t/*\n"
14293                    "\n"
14294                    "\t*/\n"
14295                    "}",
14296                    Tab));
14297   EXPECT_EQ("{\n"
14298             "\t/*\n"
14299             " asdf\n"
14300             "\t*/\n"
14301             "}",
14302             format("{\n"
14303                    "\t/*\n"
14304                    " asdf\n"
14305                    "\t*/\n"
14306                    "}",
14307                    Tab));
14308   EXPECT_EQ("/* some\n"
14309             "   comment */",
14310             format(" \t \t /* some\n"
14311                    " \t \t    comment */",
14312                    Tab));
14313   EXPECT_EQ("int a; /* some\n"
14314             "   comment */",
14315             format(" \t \t int a; /* some\n"
14316                    " \t \t    comment */",
14317                    Tab));
14318   EXPECT_EQ("int a; /* some\n"
14319             "comment */",
14320             format(" \t \t int\ta; /* some\n"
14321                    " \t \t    comment */",
14322                    Tab));
14323   EXPECT_EQ("f(\"\t\t\"); /* some\n"
14324             "    comment */",
14325             format(" \t \t f(\"\t\t\"); /* some\n"
14326                    " \t \t    comment */",
14327                    Tab));
14328   EXPECT_EQ("{\n"
14329             "\t/*\n"
14330             "\t * Comment\n"
14331             "\t */\n"
14332             "\tint i;\n"
14333             "}",
14334             format("{\n"
14335                    "\t/*\n"
14336                    "\t * Comment\n"
14337                    "\t */\n"
14338                    "\t int i;\n"
14339                    "}",
14340                    Tab));
14341   Tab.TabWidth = 2;
14342   Tab.IndentWidth = 2;
14343   EXPECT_EQ("{\n"
14344             "\t/* aaaa\n"
14345             "\t   bbbb */\n"
14346             "}",
14347             format("{\n"
14348                    "/* aaaa\n"
14349                    "   bbbb */\n"
14350                    "}",
14351                    Tab));
14352   EXPECT_EQ("{\n"
14353             "\t/*\n"
14354             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14355             "\t  bbbbbbbbbbbbb\n"
14356             "\t*/\n"
14357             "}",
14358             format("{\n"
14359                    "/*\n"
14360                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
14361                    "*/\n"
14362                    "}",
14363                    Tab));
14364   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
14365   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
14366   Tab.TabWidth = 4;
14367   Tab.IndentWidth = 4;
14368   verifyFormat("class Assign {\n"
14369                "\tvoid f() {\n"
14370                "\t\tint         x      = 123;\n"
14371                "\t\tint         random = 4;\n"
14372                "\t\tstd::string alphabet =\n"
14373                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
14374                "\t}\n"
14375                "};",
14376                Tab);
14377   Tab.AlignOperands = FormatStyle::OAS_Align;
14378   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
14379                "                 cccccccccccccccccccc;",
14380                Tab);
14381   // no alignment
14382   verifyFormat("int aaaaaaaaaa =\n"
14383                "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
14384                Tab);
14385   verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
14386                "       : bbbbbbbbbbbbbb ? 222222222222222\n"
14387                "                        : 333333333333333;",
14388                Tab);
14389   Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
14390   Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
14391   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
14392                "               + cccccccccccccccccccc;",
14393                Tab);
14394 }
14395 
14396 TEST_F(FormatTest, ZeroTabWidth) {
14397   FormatStyle Tab = getLLVMStyleWithColumns(42);
14398   Tab.IndentWidth = 8;
14399   Tab.UseTab = FormatStyle::UT_Never;
14400   Tab.TabWidth = 0;
14401   EXPECT_EQ("void a(){\n"
14402             "    // line starts with '\t'\n"
14403             "};",
14404             format("void a(){\n"
14405                    "\t// line starts with '\t'\n"
14406                    "};",
14407                    Tab));
14408 
14409   EXPECT_EQ("void a(){\n"
14410             "    // line starts with '\t'\n"
14411             "};",
14412             format("void a(){\n"
14413                    "\t\t// line starts with '\t'\n"
14414                    "};",
14415                    Tab));
14416 
14417   Tab.UseTab = FormatStyle::UT_ForIndentation;
14418   EXPECT_EQ("void a(){\n"
14419             "    // line starts with '\t'\n"
14420             "};",
14421             format("void a(){\n"
14422                    "\t// line starts with '\t'\n"
14423                    "};",
14424                    Tab));
14425 
14426   EXPECT_EQ("void a(){\n"
14427             "    // line starts with '\t'\n"
14428             "};",
14429             format("void a(){\n"
14430                    "\t\t// line starts with '\t'\n"
14431                    "};",
14432                    Tab));
14433 
14434   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
14435   EXPECT_EQ("void a(){\n"
14436             "    // line starts with '\t'\n"
14437             "};",
14438             format("void a(){\n"
14439                    "\t// line starts with '\t'\n"
14440                    "};",
14441                    Tab));
14442 
14443   EXPECT_EQ("void a(){\n"
14444             "    // line starts with '\t'\n"
14445             "};",
14446             format("void a(){\n"
14447                    "\t\t// line starts with '\t'\n"
14448                    "};",
14449                    Tab));
14450 
14451   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
14452   EXPECT_EQ("void a(){\n"
14453             "    // line starts with '\t'\n"
14454             "};",
14455             format("void a(){\n"
14456                    "\t// line starts with '\t'\n"
14457                    "};",
14458                    Tab));
14459 
14460   EXPECT_EQ("void a(){\n"
14461             "    // line starts with '\t'\n"
14462             "};",
14463             format("void a(){\n"
14464                    "\t\t// line starts with '\t'\n"
14465                    "};",
14466                    Tab));
14467 
14468   Tab.UseTab = FormatStyle::UT_Always;
14469   EXPECT_EQ("void a(){\n"
14470             "// line starts with '\t'\n"
14471             "};",
14472             format("void a(){\n"
14473                    "\t// line starts with '\t'\n"
14474                    "};",
14475                    Tab));
14476 
14477   EXPECT_EQ("void a(){\n"
14478             "// line starts with '\t'\n"
14479             "};",
14480             format("void a(){\n"
14481                    "\t\t// line starts with '\t'\n"
14482                    "};",
14483                    Tab));
14484 }
14485 
14486 TEST_F(FormatTest, CalculatesOriginalColumn) {
14487   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14488             "q\"; /* some\n"
14489             "       comment */",
14490             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14491                    "q\"; /* some\n"
14492                    "       comment */",
14493                    getLLVMStyle()));
14494   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
14495             "/* some\n"
14496             "   comment */",
14497             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
14498                    " /* some\n"
14499                    "    comment */",
14500                    getLLVMStyle()));
14501   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14502             "qqq\n"
14503             "/* some\n"
14504             "   comment */",
14505             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14506                    "qqq\n"
14507                    " /* some\n"
14508                    "    comment */",
14509                    getLLVMStyle()));
14510   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14511             "wwww; /* some\n"
14512             "         comment */",
14513             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
14514                    "wwww; /* some\n"
14515                    "         comment */",
14516                    getLLVMStyle()));
14517 }
14518 
14519 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
14520   FormatStyle NoSpace = getLLVMStyle();
14521   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
14522 
14523   verifyFormat("while(true)\n"
14524                "  continue;",
14525                NoSpace);
14526   verifyFormat("for(;;)\n"
14527                "  continue;",
14528                NoSpace);
14529   verifyFormat("if(true)\n"
14530                "  f();\n"
14531                "else if(true)\n"
14532                "  f();",
14533                NoSpace);
14534   verifyFormat("do {\n"
14535                "  do_something();\n"
14536                "} while(something());",
14537                NoSpace);
14538   verifyFormat("switch(x) {\n"
14539                "default:\n"
14540                "  break;\n"
14541                "}",
14542                NoSpace);
14543   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
14544   verifyFormat("size_t x = sizeof(x);", NoSpace);
14545   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
14546   verifyFormat("auto f(int x) -> typeof(x);", NoSpace);
14547   verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace);
14548   verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace);
14549   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
14550   verifyFormat("alignas(128) char a[128];", NoSpace);
14551   verifyFormat("size_t x = alignof(MyType);", NoSpace);
14552   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
14553   verifyFormat("int f() throw(Deprecated);", NoSpace);
14554   verifyFormat("typedef void (*cb)(int);", NoSpace);
14555   verifyFormat("T A::operator()();", NoSpace);
14556   verifyFormat("X A::operator++(T);", NoSpace);
14557   verifyFormat("auto lambda = []() { return 0; };", NoSpace);
14558 
14559   FormatStyle Space = getLLVMStyle();
14560   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
14561 
14562   verifyFormat("int f ();", Space);
14563   verifyFormat("void f (int a, T b) {\n"
14564                "  while (true)\n"
14565                "    continue;\n"
14566                "}",
14567                Space);
14568   verifyFormat("if (true)\n"
14569                "  f ();\n"
14570                "else if (true)\n"
14571                "  f ();",
14572                Space);
14573   verifyFormat("do {\n"
14574                "  do_something ();\n"
14575                "} while (something ());",
14576                Space);
14577   verifyFormat("switch (x) {\n"
14578                "default:\n"
14579                "  break;\n"
14580                "}",
14581                Space);
14582   verifyFormat("A::A () : a (1) {}", Space);
14583   verifyFormat("void f () __attribute__ ((asdf));", Space);
14584   verifyFormat("*(&a + 1);\n"
14585                "&((&a)[1]);\n"
14586                "a[(b + c) * d];\n"
14587                "(((a + 1) * 2) + 3) * 4;",
14588                Space);
14589   verifyFormat("#define A(x) x", Space);
14590   verifyFormat("#define A (x) x", Space);
14591   verifyFormat("#if defined(x)\n"
14592                "#endif",
14593                Space);
14594   verifyFormat("auto i = std::make_unique<int> (5);", Space);
14595   verifyFormat("size_t x = sizeof (x);", Space);
14596   verifyFormat("auto f (int x) -> decltype (x);", Space);
14597   verifyFormat("auto f (int x) -> typeof (x);", Space);
14598   verifyFormat("auto f (int x) -> _Atomic (x);", Space);
14599   verifyFormat("auto f (int x) -> __underlying_type (x);", Space);
14600   verifyFormat("int f (T x) noexcept (x.create ());", Space);
14601   verifyFormat("alignas (128) char a[128];", Space);
14602   verifyFormat("size_t x = alignof (MyType);", Space);
14603   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
14604   verifyFormat("int f () throw (Deprecated);", Space);
14605   verifyFormat("typedef void (*cb) (int);", Space);
14606   // FIXME these tests regressed behaviour.
14607   // verifyFormat("T A::operator() ();", Space);
14608   // verifyFormat("X A::operator++ (T);", Space);
14609   verifyFormat("auto lambda = [] () { return 0; };", Space);
14610   verifyFormat("int x = int (y);", Space);
14611 
14612   FormatStyle SomeSpace = getLLVMStyle();
14613   SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
14614 
14615   verifyFormat("[]() -> float {}", SomeSpace);
14616   verifyFormat("[] (auto foo) {}", SomeSpace);
14617   verifyFormat("[foo]() -> int {}", SomeSpace);
14618   verifyFormat("int f();", SomeSpace);
14619   verifyFormat("void f (int a, T b) {\n"
14620                "  while (true)\n"
14621                "    continue;\n"
14622                "}",
14623                SomeSpace);
14624   verifyFormat("if (true)\n"
14625                "  f();\n"
14626                "else if (true)\n"
14627                "  f();",
14628                SomeSpace);
14629   verifyFormat("do {\n"
14630                "  do_something();\n"
14631                "} while (something());",
14632                SomeSpace);
14633   verifyFormat("switch (x) {\n"
14634                "default:\n"
14635                "  break;\n"
14636                "}",
14637                SomeSpace);
14638   verifyFormat("A::A() : a (1) {}", SomeSpace);
14639   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
14640   verifyFormat("*(&a + 1);\n"
14641                "&((&a)[1]);\n"
14642                "a[(b + c) * d];\n"
14643                "(((a + 1) * 2) + 3) * 4;",
14644                SomeSpace);
14645   verifyFormat("#define A(x) x", SomeSpace);
14646   verifyFormat("#define A (x) x", SomeSpace);
14647   verifyFormat("#if defined(x)\n"
14648                "#endif",
14649                SomeSpace);
14650   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
14651   verifyFormat("size_t x = sizeof (x);", SomeSpace);
14652   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
14653   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace);
14654   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace);
14655   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace);
14656   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
14657   verifyFormat("alignas (128) char a[128];", SomeSpace);
14658   verifyFormat("size_t x = alignof (MyType);", SomeSpace);
14659   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
14660                SomeSpace);
14661   verifyFormat("int f() throw (Deprecated);", SomeSpace);
14662   verifyFormat("typedef void (*cb) (int);", SomeSpace);
14663   verifyFormat("T A::operator()();", SomeSpace);
14664   // FIXME these tests regressed behaviour.
14665   // verifyFormat("X A::operator++ (T);", SomeSpace);
14666   verifyFormat("int x = int (y);", SomeSpace);
14667   verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
14668 
14669   FormatStyle SpaceControlStatements = getLLVMStyle();
14670   SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14671   SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true;
14672 
14673   verifyFormat("while (true)\n"
14674                "  continue;",
14675                SpaceControlStatements);
14676   verifyFormat("if (true)\n"
14677                "  f();\n"
14678                "else if (true)\n"
14679                "  f();",
14680                SpaceControlStatements);
14681   verifyFormat("for (;;) {\n"
14682                "  do_something();\n"
14683                "}",
14684                SpaceControlStatements);
14685   verifyFormat("do {\n"
14686                "  do_something();\n"
14687                "} while (something());",
14688                SpaceControlStatements);
14689   verifyFormat("switch (x) {\n"
14690                "default:\n"
14691                "  break;\n"
14692                "}",
14693                SpaceControlStatements);
14694 
14695   FormatStyle SpaceFuncDecl = getLLVMStyle();
14696   SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14697   SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true;
14698 
14699   verifyFormat("int f ();", SpaceFuncDecl);
14700   verifyFormat("void f(int a, T b) {}", SpaceFuncDecl);
14701   verifyFormat("A::A() : a(1) {}", SpaceFuncDecl);
14702   verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl);
14703   verifyFormat("#define A(x) x", SpaceFuncDecl);
14704   verifyFormat("#define A (x) x", SpaceFuncDecl);
14705   verifyFormat("#if defined(x)\n"
14706                "#endif",
14707                SpaceFuncDecl);
14708   verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl);
14709   verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl);
14710   verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl);
14711   verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl);
14712   verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl);
14713   verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl);
14714   verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl);
14715   verifyFormat("alignas(128) char a[128];", SpaceFuncDecl);
14716   verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl);
14717   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
14718                SpaceFuncDecl);
14719   verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl);
14720   verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl);
14721   // FIXME these tests regressed behaviour.
14722   // verifyFormat("T A::operator() ();", SpaceFuncDecl);
14723   // verifyFormat("X A::operator++ (T);", SpaceFuncDecl);
14724   verifyFormat("T A::operator()() {}", SpaceFuncDecl);
14725   verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl);
14726   verifyFormat("int x = int(y);", SpaceFuncDecl);
14727   verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
14728                SpaceFuncDecl);
14729 
14730   FormatStyle SpaceFuncDef = getLLVMStyle();
14731   SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14732   SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true;
14733 
14734   verifyFormat("int f();", SpaceFuncDef);
14735   verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
14736   verifyFormat("A::A() : a(1) {}", SpaceFuncDef);
14737   verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
14738   verifyFormat("#define A(x) x", SpaceFuncDef);
14739   verifyFormat("#define A (x) x", SpaceFuncDef);
14740   verifyFormat("#if defined(x)\n"
14741                "#endif",
14742                SpaceFuncDef);
14743   verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef);
14744   verifyFormat("size_t x = sizeof(x);", SpaceFuncDef);
14745   verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef);
14746   verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef);
14747   verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef);
14748   verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef);
14749   verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef);
14750   verifyFormat("alignas(128) char a[128];", SpaceFuncDef);
14751   verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef);
14752   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
14753                SpaceFuncDef);
14754   verifyFormat("int f() throw(Deprecated);", SpaceFuncDef);
14755   verifyFormat("typedef void (*cb)(int);", SpaceFuncDef);
14756   verifyFormat("T A::operator()();", SpaceFuncDef);
14757   verifyFormat("X A::operator++(T);", SpaceFuncDef);
14758   // verifyFormat("T A::operator() () {}", SpaceFuncDef);
14759   verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef);
14760   verifyFormat("int x = int(y);", SpaceFuncDef);
14761   verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
14762                SpaceFuncDef);
14763 
14764   FormatStyle SpaceIfMacros = getLLVMStyle();
14765   SpaceIfMacros.IfMacros.clear();
14766   SpaceIfMacros.IfMacros.push_back("MYIF");
14767   SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14768   SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true;
14769   verifyFormat("MYIF (a)\n  return;", SpaceIfMacros);
14770   verifyFormat("MYIF (a)\n  return;\nelse MYIF (b)\n  return;", SpaceIfMacros);
14771   verifyFormat("MYIF (a)\n  return;\nelse\n  return;", SpaceIfMacros);
14772 
14773   FormatStyle SpaceForeachMacros = getLLVMStyle();
14774   EXPECT_EQ(SpaceForeachMacros.AllowShortBlocksOnASingleLine,
14775             FormatStyle::SBS_Never);
14776   EXPECT_EQ(SpaceForeachMacros.AllowShortLoopsOnASingleLine, false);
14777   SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14778   SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true;
14779   verifyFormat("for (;;) {\n"
14780                "}",
14781                SpaceForeachMacros);
14782   verifyFormat("foreach (Item *item, itemlist) {\n"
14783                "}",
14784                SpaceForeachMacros);
14785   verifyFormat("Q_FOREACH (Item *item, itemlist) {\n"
14786                "}",
14787                SpaceForeachMacros);
14788   verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n"
14789                "}",
14790                SpaceForeachMacros);
14791   verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros);
14792 
14793   FormatStyle SomeSpace2 = getLLVMStyle();
14794   SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14795   SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true;
14796   verifyFormat("[]() -> float {}", SomeSpace2);
14797   verifyFormat("[] (auto foo) {}", SomeSpace2);
14798   verifyFormat("[foo]() -> int {}", SomeSpace2);
14799   verifyFormat("int f();", SomeSpace2);
14800   verifyFormat("void f (int a, T b) {\n"
14801                "  while (true)\n"
14802                "    continue;\n"
14803                "}",
14804                SomeSpace2);
14805   verifyFormat("if (true)\n"
14806                "  f();\n"
14807                "else if (true)\n"
14808                "  f();",
14809                SomeSpace2);
14810   verifyFormat("do {\n"
14811                "  do_something();\n"
14812                "} while (something());",
14813                SomeSpace2);
14814   verifyFormat("switch (x) {\n"
14815                "default:\n"
14816                "  break;\n"
14817                "}",
14818                SomeSpace2);
14819   verifyFormat("A::A() : a (1) {}", SomeSpace2);
14820   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2);
14821   verifyFormat("*(&a + 1);\n"
14822                "&((&a)[1]);\n"
14823                "a[(b + c) * d];\n"
14824                "(((a + 1) * 2) + 3) * 4;",
14825                SomeSpace2);
14826   verifyFormat("#define A(x) x", SomeSpace2);
14827   verifyFormat("#define A (x) x", SomeSpace2);
14828   verifyFormat("#if defined(x)\n"
14829                "#endif",
14830                SomeSpace2);
14831   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2);
14832   verifyFormat("size_t x = sizeof (x);", SomeSpace2);
14833   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2);
14834   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2);
14835   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2);
14836   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2);
14837   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2);
14838   verifyFormat("alignas (128) char a[128];", SomeSpace2);
14839   verifyFormat("size_t x = alignof (MyType);", SomeSpace2);
14840   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
14841                SomeSpace2);
14842   verifyFormat("int f() throw (Deprecated);", SomeSpace2);
14843   verifyFormat("typedef void (*cb) (int);", SomeSpace2);
14844   verifyFormat("T A::operator()();", SomeSpace2);
14845   // verifyFormat("X A::operator++ (T);", SomeSpace2);
14846   verifyFormat("int x = int (y);", SomeSpace2);
14847   verifyFormat("auto lambda = []() { return 0; };", SomeSpace2);
14848 
14849   FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle();
14850   SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
14851   SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
14852       .AfterOverloadedOperator = true;
14853 
14854   verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator);
14855   verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator);
14856   verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator);
14857   verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
14858 
14859   SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
14860       .AfterOverloadedOperator = false;
14861 
14862   verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator);
14863   verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator);
14864   verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator);
14865   verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
14866 }
14867 
14868 TEST_F(FormatTest, SpaceAfterLogicalNot) {
14869   FormatStyle Spaces = getLLVMStyle();
14870   Spaces.SpaceAfterLogicalNot = true;
14871 
14872   verifyFormat("bool x = ! y", Spaces);
14873   verifyFormat("if (! isFailure())", Spaces);
14874   verifyFormat("if (! (a && b))", Spaces);
14875   verifyFormat("\"Error!\"", Spaces);
14876   verifyFormat("! ! x", Spaces);
14877 }
14878 
14879 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
14880   FormatStyle Spaces = getLLVMStyle();
14881 
14882   Spaces.SpacesInParentheses = true;
14883   verifyFormat("do_something( ::globalVar );", Spaces);
14884   verifyFormat("call( x, y, z );", Spaces);
14885   verifyFormat("call();", Spaces);
14886   verifyFormat("std::function<void( int, int )> callback;", Spaces);
14887   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
14888                Spaces);
14889   verifyFormat("while ( (bool)1 )\n"
14890                "  continue;",
14891                Spaces);
14892   verifyFormat("for ( ;; )\n"
14893                "  continue;",
14894                Spaces);
14895   verifyFormat("if ( true )\n"
14896                "  f();\n"
14897                "else if ( true )\n"
14898                "  f();",
14899                Spaces);
14900   verifyFormat("do {\n"
14901                "  do_something( (int)i );\n"
14902                "} while ( something() );",
14903                Spaces);
14904   verifyFormat("switch ( x ) {\n"
14905                "default:\n"
14906                "  break;\n"
14907                "}",
14908                Spaces);
14909 
14910   Spaces.SpacesInParentheses = false;
14911   Spaces.SpacesInCStyleCastParentheses = true;
14912   verifyFormat("Type *A = ( Type * )P;", Spaces);
14913   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
14914   verifyFormat("x = ( int32 )y;", Spaces);
14915   verifyFormat("int a = ( int )(2.0f);", Spaces);
14916   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
14917   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
14918   verifyFormat("#define x (( int )-1)", Spaces);
14919 
14920   // Run the first set of tests again with:
14921   Spaces.SpacesInParentheses = false;
14922   Spaces.SpaceInEmptyParentheses = true;
14923   Spaces.SpacesInCStyleCastParentheses = true;
14924   verifyFormat("call(x, y, z);", Spaces);
14925   verifyFormat("call( );", Spaces);
14926   verifyFormat("std::function<void(int, int)> callback;", Spaces);
14927   verifyFormat("while (( bool )1)\n"
14928                "  continue;",
14929                Spaces);
14930   verifyFormat("for (;;)\n"
14931                "  continue;",
14932                Spaces);
14933   verifyFormat("if (true)\n"
14934                "  f( );\n"
14935                "else if (true)\n"
14936                "  f( );",
14937                Spaces);
14938   verifyFormat("do {\n"
14939                "  do_something(( int )i);\n"
14940                "} while (something( ));",
14941                Spaces);
14942   verifyFormat("switch (x) {\n"
14943                "default:\n"
14944                "  break;\n"
14945                "}",
14946                Spaces);
14947 
14948   // Run the first set of tests again with:
14949   Spaces.SpaceAfterCStyleCast = true;
14950   verifyFormat("call(x, y, z);", Spaces);
14951   verifyFormat("call( );", Spaces);
14952   verifyFormat("std::function<void(int, int)> callback;", Spaces);
14953   verifyFormat("while (( bool ) 1)\n"
14954                "  continue;",
14955                Spaces);
14956   verifyFormat("for (;;)\n"
14957                "  continue;",
14958                Spaces);
14959   verifyFormat("if (true)\n"
14960                "  f( );\n"
14961                "else if (true)\n"
14962                "  f( );",
14963                Spaces);
14964   verifyFormat("do {\n"
14965                "  do_something(( int ) i);\n"
14966                "} while (something( ));",
14967                Spaces);
14968   verifyFormat("switch (x) {\n"
14969                "default:\n"
14970                "  break;\n"
14971                "}",
14972                Spaces);
14973   verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces);
14974   verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces);
14975   verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces);
14976   verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces);
14977   verifyFormat("bool *y = ( bool * ) (x);", Spaces);
14978 
14979   // Run subset of tests again with:
14980   Spaces.SpacesInCStyleCastParentheses = false;
14981   Spaces.SpaceAfterCStyleCast = true;
14982   verifyFormat("while ((bool) 1)\n"
14983                "  continue;",
14984                Spaces);
14985   verifyFormat("do {\n"
14986                "  do_something((int) i);\n"
14987                "} while (something( ));",
14988                Spaces);
14989 
14990   verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
14991   verifyFormat("size_t idx = (size_t) a;", Spaces);
14992   verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
14993   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
14994   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
14995   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
14996   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
14997   verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces);
14998   verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces);
14999   verifyFormat("bool *y = (bool *) (void *) (x);", Spaces);
15000   verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces);
15001   verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces);
15002   Spaces.ColumnLimit = 80;
15003   Spaces.IndentWidth = 4;
15004   Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
15005   verifyFormat("void foo( ) {\n"
15006                "    size_t foo = (*(function))(\n"
15007                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
15008                "BarrrrrrrrrrrrLong,\n"
15009                "        FoooooooooLooooong);\n"
15010                "}",
15011                Spaces);
15012   Spaces.SpaceAfterCStyleCast = false;
15013   verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
15014   verifyFormat("size_t idx = (size_t)a;", Spaces);
15015   verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
15016   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
15017   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
15018   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
15019   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
15020 
15021   verifyFormat("void foo( ) {\n"
15022                "    size_t foo = (*(function))(\n"
15023                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
15024                "BarrrrrrrrrrrrLong,\n"
15025                "        FoooooooooLooooong);\n"
15026                "}",
15027                Spaces);
15028 }
15029 
15030 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
15031   verifyFormat("int a[5];");
15032   verifyFormat("a[3] += 42;");
15033 
15034   FormatStyle Spaces = getLLVMStyle();
15035   Spaces.SpacesInSquareBrackets = true;
15036   // Not lambdas.
15037   verifyFormat("int a[ 5 ];", Spaces);
15038   verifyFormat("a[ 3 ] += 42;", Spaces);
15039   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
15040   verifyFormat("double &operator[](int i) { return 0; }\n"
15041                "int i;",
15042                Spaces);
15043   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
15044   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
15045   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
15046   // Lambdas.
15047   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
15048   verifyFormat("return [ i, args... ] {};", Spaces);
15049   verifyFormat("int foo = [ &bar ]() {};", Spaces);
15050   verifyFormat("int foo = [ = ]() {};", Spaces);
15051   verifyFormat("int foo = [ & ]() {};", Spaces);
15052   verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
15053   verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
15054 }
15055 
15056 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
15057   FormatStyle NoSpaceStyle = getLLVMStyle();
15058   verifyFormat("int a[5];", NoSpaceStyle);
15059   verifyFormat("a[3] += 42;", NoSpaceStyle);
15060 
15061   verifyFormat("int a[1];", NoSpaceStyle);
15062   verifyFormat("int 1 [a];", NoSpaceStyle);
15063   verifyFormat("int a[1][2];", NoSpaceStyle);
15064   verifyFormat("a[7] = 5;", NoSpaceStyle);
15065   verifyFormat("int a = (f())[23];", NoSpaceStyle);
15066   verifyFormat("f([] {})", NoSpaceStyle);
15067 
15068   FormatStyle Space = getLLVMStyle();
15069   Space.SpaceBeforeSquareBrackets = true;
15070   verifyFormat("int c = []() -> int { return 2; }();\n", Space);
15071   verifyFormat("return [i, args...] {};", Space);
15072 
15073   verifyFormat("int a [5];", Space);
15074   verifyFormat("a [3] += 42;", Space);
15075   verifyFormat("constexpr char hello []{\"hello\"};", Space);
15076   verifyFormat("double &operator[](int i) { return 0; }\n"
15077                "int i;",
15078                Space);
15079   verifyFormat("std::unique_ptr<int []> foo() {}", Space);
15080   verifyFormat("int i = a [a][a]->f();", Space);
15081   verifyFormat("int i = (*b) [a]->f();", Space);
15082 
15083   verifyFormat("int a [1];", Space);
15084   verifyFormat("int 1 [a];", Space);
15085   verifyFormat("int a [1][2];", Space);
15086   verifyFormat("a [7] = 5;", Space);
15087   verifyFormat("int a = (f()) [23];", Space);
15088   verifyFormat("f([] {})", Space);
15089 }
15090 
15091 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
15092   verifyFormat("int a = 5;");
15093   verifyFormat("a += 42;");
15094   verifyFormat("a or_eq 8;");
15095 
15096   FormatStyle Spaces = getLLVMStyle();
15097   Spaces.SpaceBeforeAssignmentOperators = false;
15098   verifyFormat("int a= 5;", Spaces);
15099   verifyFormat("a+= 42;", Spaces);
15100   verifyFormat("a or_eq 8;", Spaces);
15101 }
15102 
15103 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
15104   verifyFormat("class Foo : public Bar {};");
15105   verifyFormat("Foo::Foo() : foo(1) {}");
15106   verifyFormat("for (auto a : b) {\n}");
15107   verifyFormat("int x = a ? b : c;");
15108   verifyFormat("{\n"
15109                "label0:\n"
15110                "  int x = 0;\n"
15111                "}");
15112   verifyFormat("switch (x) {\n"
15113                "case 1:\n"
15114                "default:\n"
15115                "}");
15116   verifyFormat("switch (allBraces) {\n"
15117                "case 1: {\n"
15118                "  break;\n"
15119                "}\n"
15120                "case 2: {\n"
15121                "  [[fallthrough]];\n"
15122                "}\n"
15123                "default: {\n"
15124                "  break;\n"
15125                "}\n"
15126                "}");
15127 
15128   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
15129   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
15130   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
15131   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
15132   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
15133   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
15134   verifyFormat("{\n"
15135                "label1:\n"
15136                "  int x = 0;\n"
15137                "}",
15138                CtorInitializerStyle);
15139   verifyFormat("switch (x) {\n"
15140                "case 1:\n"
15141                "default:\n"
15142                "}",
15143                CtorInitializerStyle);
15144   verifyFormat("switch (allBraces) {\n"
15145                "case 1: {\n"
15146                "  break;\n"
15147                "}\n"
15148                "case 2: {\n"
15149                "  [[fallthrough]];\n"
15150                "}\n"
15151                "default: {\n"
15152                "  break;\n"
15153                "}\n"
15154                "}",
15155                CtorInitializerStyle);
15156   CtorInitializerStyle.BreakConstructorInitializers =
15157       FormatStyle::BCIS_AfterColon;
15158   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
15159                "    aaaaaaaaaaaaaaaa(1),\n"
15160                "    bbbbbbbbbbbbbbbb(2) {}",
15161                CtorInitializerStyle);
15162   CtorInitializerStyle.BreakConstructorInitializers =
15163       FormatStyle::BCIS_BeforeComma;
15164   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
15165                "    : aaaaaaaaaaaaaaaa(1)\n"
15166                "    , bbbbbbbbbbbbbbbb(2) {}",
15167                CtorInitializerStyle);
15168   CtorInitializerStyle.BreakConstructorInitializers =
15169       FormatStyle::BCIS_BeforeColon;
15170   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
15171                "    : aaaaaaaaaaaaaaaa(1),\n"
15172                "      bbbbbbbbbbbbbbbb(2) {}",
15173                CtorInitializerStyle);
15174   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
15175   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
15176                ": aaaaaaaaaaaaaaaa(1),\n"
15177                "  bbbbbbbbbbbbbbbb(2) {}",
15178                CtorInitializerStyle);
15179 
15180   FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
15181   InheritanceStyle.SpaceBeforeInheritanceColon = false;
15182   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
15183   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
15184   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
15185   verifyFormat("int x = a ? b : c;", InheritanceStyle);
15186   verifyFormat("{\n"
15187                "label2:\n"
15188                "  int x = 0;\n"
15189                "}",
15190                InheritanceStyle);
15191   verifyFormat("switch (x) {\n"
15192                "case 1:\n"
15193                "default:\n"
15194                "}",
15195                InheritanceStyle);
15196   verifyFormat("switch (allBraces) {\n"
15197                "case 1: {\n"
15198                "  break;\n"
15199                "}\n"
15200                "case 2: {\n"
15201                "  [[fallthrough]];\n"
15202                "}\n"
15203                "default: {\n"
15204                "  break;\n"
15205                "}\n"
15206                "}",
15207                InheritanceStyle);
15208   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma;
15209   verifyFormat("class Foooooooooooooooooooooo\n"
15210                "    : public aaaaaaaaaaaaaaaaaa,\n"
15211                "      public bbbbbbbbbbbbbbbbbb {\n"
15212                "}",
15213                InheritanceStyle);
15214   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
15215   verifyFormat("class Foooooooooooooooooooooo:\n"
15216                "    public aaaaaaaaaaaaaaaaaa,\n"
15217                "    public bbbbbbbbbbbbbbbbbb {\n"
15218                "}",
15219                InheritanceStyle);
15220   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
15221   verifyFormat("class Foooooooooooooooooooooo\n"
15222                "    : public aaaaaaaaaaaaaaaaaa\n"
15223                "    , public bbbbbbbbbbbbbbbbbb {\n"
15224                "}",
15225                InheritanceStyle);
15226   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
15227   verifyFormat("class Foooooooooooooooooooooo\n"
15228                "    : public aaaaaaaaaaaaaaaaaa,\n"
15229                "      public bbbbbbbbbbbbbbbbbb {\n"
15230                "}",
15231                InheritanceStyle);
15232   InheritanceStyle.ConstructorInitializerIndentWidth = 0;
15233   verifyFormat("class Foooooooooooooooooooooo\n"
15234                ": public aaaaaaaaaaaaaaaaaa,\n"
15235                "  public bbbbbbbbbbbbbbbbbb {}",
15236                InheritanceStyle);
15237 
15238   FormatStyle ForLoopStyle = getLLVMStyle();
15239   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
15240   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
15241   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
15242   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
15243   verifyFormat("int x = a ? b : c;", ForLoopStyle);
15244   verifyFormat("{\n"
15245                "label2:\n"
15246                "  int x = 0;\n"
15247                "}",
15248                ForLoopStyle);
15249   verifyFormat("switch (x) {\n"
15250                "case 1:\n"
15251                "default:\n"
15252                "}",
15253                ForLoopStyle);
15254   verifyFormat("switch (allBraces) {\n"
15255                "case 1: {\n"
15256                "  break;\n"
15257                "}\n"
15258                "case 2: {\n"
15259                "  [[fallthrough]];\n"
15260                "}\n"
15261                "default: {\n"
15262                "  break;\n"
15263                "}\n"
15264                "}",
15265                ForLoopStyle);
15266 
15267   FormatStyle CaseStyle = getLLVMStyle();
15268   CaseStyle.SpaceBeforeCaseColon = true;
15269   verifyFormat("class Foo : public Bar {};", CaseStyle);
15270   verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle);
15271   verifyFormat("for (auto a : b) {\n}", CaseStyle);
15272   verifyFormat("int x = a ? b : c;", CaseStyle);
15273   verifyFormat("switch (x) {\n"
15274                "case 1 :\n"
15275                "default :\n"
15276                "}",
15277                CaseStyle);
15278   verifyFormat("switch (allBraces) {\n"
15279                "case 1 : {\n"
15280                "  break;\n"
15281                "}\n"
15282                "case 2 : {\n"
15283                "  [[fallthrough]];\n"
15284                "}\n"
15285                "default : {\n"
15286                "  break;\n"
15287                "}\n"
15288                "}",
15289                CaseStyle);
15290 
15291   FormatStyle NoSpaceStyle = getLLVMStyle();
15292   EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false);
15293   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
15294   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
15295   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
15296   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
15297   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
15298   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
15299   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
15300   verifyFormat("{\n"
15301                "label3:\n"
15302                "  int x = 0;\n"
15303                "}",
15304                NoSpaceStyle);
15305   verifyFormat("switch (x) {\n"
15306                "case 1:\n"
15307                "default:\n"
15308                "}",
15309                NoSpaceStyle);
15310   verifyFormat("switch (allBraces) {\n"
15311                "case 1: {\n"
15312                "  break;\n"
15313                "}\n"
15314                "case 2: {\n"
15315                "  [[fallthrough]];\n"
15316                "}\n"
15317                "default: {\n"
15318                "  break;\n"
15319                "}\n"
15320                "}",
15321                NoSpaceStyle);
15322 
15323   FormatStyle InvertedSpaceStyle = getLLVMStyle();
15324   InvertedSpaceStyle.SpaceBeforeCaseColon = true;
15325   InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false;
15326   InvertedSpaceStyle.SpaceBeforeInheritanceColon = false;
15327   InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
15328   verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle);
15329   verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle);
15330   verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle);
15331   verifyFormat("int x = a ? b : c;", InvertedSpaceStyle);
15332   verifyFormat("{\n"
15333                "label3:\n"
15334                "  int x = 0;\n"
15335                "}",
15336                InvertedSpaceStyle);
15337   verifyFormat("switch (x) {\n"
15338                "case 1 :\n"
15339                "case 2 : {\n"
15340                "  break;\n"
15341                "}\n"
15342                "default :\n"
15343                "  break;\n"
15344                "}",
15345                InvertedSpaceStyle);
15346   verifyFormat("switch (allBraces) {\n"
15347                "case 1 : {\n"
15348                "  break;\n"
15349                "}\n"
15350                "case 2 : {\n"
15351                "  [[fallthrough]];\n"
15352                "}\n"
15353                "default : {\n"
15354                "  break;\n"
15355                "}\n"
15356                "}",
15357                InvertedSpaceStyle);
15358 }
15359 
15360 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
15361   FormatStyle Style = getLLVMStyle();
15362 
15363   Style.PointerAlignment = FormatStyle::PAS_Left;
15364   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
15365   verifyFormat("void* const* x = NULL;", Style);
15366 
15367 #define verifyQualifierSpaces(Code, Pointers, Qualifiers)                      \
15368   do {                                                                         \
15369     Style.PointerAlignment = FormatStyle::Pointers;                            \
15370     Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers;              \
15371     verifyFormat(Code, Style);                                                 \
15372   } while (false)
15373 
15374   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default);
15375   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default);
15376   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default);
15377 
15378   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before);
15379   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before);
15380   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before);
15381 
15382   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After);
15383   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After);
15384   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After);
15385 
15386   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both);
15387   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
15388   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
15389 
15390   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
15391   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
15392                         SAPQ_Default);
15393   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
15394                         SAPQ_Default);
15395 
15396   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
15397   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
15398                         SAPQ_Before);
15399   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
15400                         SAPQ_Before);
15401 
15402   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
15403   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
15404   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
15405                         SAPQ_After);
15406 
15407   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
15408   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
15409   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
15410 
15411 #undef verifyQualifierSpaces
15412 
15413   FormatStyle Spaces = getLLVMStyle();
15414   Spaces.AttributeMacros.push_back("qualified");
15415   Spaces.PointerAlignment = FormatStyle::PAS_Right;
15416   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
15417   verifyFormat("SomeType *volatile *a = NULL;", Spaces);
15418   verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
15419   verifyFormat("std::vector<SomeType *const *> x;", Spaces);
15420   verifyFormat("std::vector<SomeType *qualified *> x;", Spaces);
15421   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15422   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
15423   verifyFormat("SomeType * volatile *a = NULL;", Spaces);
15424   verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces);
15425   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
15426   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
15427   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15428 
15429   // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
15430   Spaces.PointerAlignment = FormatStyle::PAS_Left;
15431   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
15432   verifyFormat("SomeType* volatile* a = NULL;", Spaces);
15433   verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces);
15434   verifyFormat("std::vector<SomeType* const*> x;", Spaces);
15435   verifyFormat("std::vector<SomeType* qualified*> x;", Spaces);
15436   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15437   // However, setting it to SAPQ_After should add spaces after __attribute, etc.
15438   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
15439   verifyFormat("SomeType* volatile * a = NULL;", Spaces);
15440   verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces);
15441   verifyFormat("std::vector<SomeType* const *> x;", Spaces);
15442   verifyFormat("std::vector<SomeType* qualified *> x;", Spaces);
15443   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15444 
15445   // PAS_Middle should not have any noticeable changes even for SAPQ_Both
15446   Spaces.PointerAlignment = FormatStyle::PAS_Middle;
15447   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
15448   verifyFormat("SomeType * volatile * a = NULL;", Spaces);
15449   verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces);
15450   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
15451   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
15452   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
15453 }
15454 
15455 TEST_F(FormatTest, AlignConsecutiveMacros) {
15456   FormatStyle Style = getLLVMStyle();
15457   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15458   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
15459   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
15460 
15461   verifyFormat("#define a 3\n"
15462                "#define bbbb 4\n"
15463                "#define ccc (5)",
15464                Style);
15465 
15466   verifyFormat("#define f(x) (x * x)\n"
15467                "#define fff(x, y, z) (x * y + z)\n"
15468                "#define ffff(x, y) (x - y)",
15469                Style);
15470 
15471   verifyFormat("#define foo(x, y) (x + y)\n"
15472                "#define bar (5, 6)(2 + 2)",
15473                Style);
15474 
15475   verifyFormat("#define a 3\n"
15476                "#define bbbb 4\n"
15477                "#define ccc (5)\n"
15478                "#define f(x) (x * x)\n"
15479                "#define fff(x, y, z) (x * y + z)\n"
15480                "#define ffff(x, y) (x - y)",
15481                Style);
15482 
15483   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15484   verifyFormat("#define a    3\n"
15485                "#define bbbb 4\n"
15486                "#define ccc  (5)",
15487                Style);
15488 
15489   verifyFormat("#define f(x)         (x * x)\n"
15490                "#define fff(x, y, z) (x * y + z)\n"
15491                "#define ffff(x, y)   (x - y)",
15492                Style);
15493 
15494   verifyFormat("#define foo(x, y) (x + y)\n"
15495                "#define bar       (5, 6)(2 + 2)",
15496                Style);
15497 
15498   verifyFormat("#define a            3\n"
15499                "#define bbbb         4\n"
15500                "#define ccc          (5)\n"
15501                "#define f(x)         (x * x)\n"
15502                "#define fff(x, y, z) (x * y + z)\n"
15503                "#define ffff(x, y)   (x - y)",
15504                Style);
15505 
15506   verifyFormat("#define a         5\n"
15507                "#define foo(x, y) (x + y)\n"
15508                "#define CCC       (6)\n"
15509                "auto lambda = []() {\n"
15510                "  auto  ii = 0;\n"
15511                "  float j  = 0;\n"
15512                "  return 0;\n"
15513                "};\n"
15514                "int   i  = 0;\n"
15515                "float i2 = 0;\n"
15516                "auto  v  = type{\n"
15517                "    i = 1,   //\n"
15518                "    (i = 2), //\n"
15519                "    i = 3    //\n"
15520                "};",
15521                Style);
15522 
15523   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
15524   Style.ColumnLimit = 20;
15525 
15526   verifyFormat("#define a          \\\n"
15527                "  \"aabbbbbbbbbbbb\"\n"
15528                "#define D          \\\n"
15529                "  \"aabbbbbbbbbbbb\" \\\n"
15530                "  \"ccddeeeeeeeee\"\n"
15531                "#define B          \\\n"
15532                "  \"QQQQQQQQQQQQQ\"  \\\n"
15533                "  \"FFFFFFFFFFFFF\"  \\\n"
15534                "  \"LLLLLLLL\"\n",
15535                Style);
15536 
15537   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15538   verifyFormat("#define a          \\\n"
15539                "  \"aabbbbbbbbbbbb\"\n"
15540                "#define D          \\\n"
15541                "  \"aabbbbbbbbbbbb\" \\\n"
15542                "  \"ccddeeeeeeeee\"\n"
15543                "#define B          \\\n"
15544                "  \"QQQQQQQQQQQQQ\"  \\\n"
15545                "  \"FFFFFFFFFFFFF\"  \\\n"
15546                "  \"LLLLLLLL\"\n",
15547                Style);
15548 
15549   // Test across comments
15550   Style.MaxEmptyLinesToKeep = 10;
15551   Style.ReflowComments = false;
15552   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments;
15553   EXPECT_EQ("#define a    3\n"
15554             "// line comment\n"
15555             "#define bbbb 4\n"
15556             "#define ccc  (5)",
15557             format("#define a 3\n"
15558                    "// line comment\n"
15559                    "#define bbbb 4\n"
15560                    "#define ccc (5)",
15561                    Style));
15562 
15563   EXPECT_EQ("#define a    3\n"
15564             "/* block comment */\n"
15565             "#define bbbb 4\n"
15566             "#define ccc  (5)",
15567             format("#define a  3\n"
15568                    "/* block comment */\n"
15569                    "#define bbbb 4\n"
15570                    "#define ccc (5)",
15571                    Style));
15572 
15573   EXPECT_EQ("#define a    3\n"
15574             "/* multi-line *\n"
15575             " * block comment */\n"
15576             "#define bbbb 4\n"
15577             "#define ccc  (5)",
15578             format("#define a 3\n"
15579                    "/* multi-line *\n"
15580                    " * block comment */\n"
15581                    "#define bbbb 4\n"
15582                    "#define ccc (5)",
15583                    Style));
15584 
15585   EXPECT_EQ("#define a    3\n"
15586             "// multi-line line comment\n"
15587             "//\n"
15588             "#define bbbb 4\n"
15589             "#define ccc  (5)",
15590             format("#define a  3\n"
15591                    "// multi-line line comment\n"
15592                    "//\n"
15593                    "#define bbbb 4\n"
15594                    "#define ccc (5)",
15595                    Style));
15596 
15597   EXPECT_EQ("#define a 3\n"
15598             "// empty lines still break.\n"
15599             "\n"
15600             "#define bbbb 4\n"
15601             "#define ccc  (5)",
15602             format("#define a     3\n"
15603                    "// empty lines still break.\n"
15604                    "\n"
15605                    "#define bbbb     4\n"
15606                    "#define ccc  (5)",
15607                    Style));
15608 
15609   // Test across empty lines
15610   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines;
15611   EXPECT_EQ("#define a    3\n"
15612             "\n"
15613             "#define bbbb 4\n"
15614             "#define ccc  (5)",
15615             format("#define a 3\n"
15616                    "\n"
15617                    "#define bbbb 4\n"
15618                    "#define ccc (5)",
15619                    Style));
15620 
15621   EXPECT_EQ("#define a    3\n"
15622             "\n"
15623             "\n"
15624             "\n"
15625             "#define bbbb 4\n"
15626             "#define ccc  (5)",
15627             format("#define a        3\n"
15628                    "\n"
15629                    "\n"
15630                    "\n"
15631                    "#define bbbb 4\n"
15632                    "#define ccc (5)",
15633                    Style));
15634 
15635   EXPECT_EQ("#define a 3\n"
15636             "// comments should break alignment\n"
15637             "//\n"
15638             "#define bbbb 4\n"
15639             "#define ccc  (5)",
15640             format("#define a        3\n"
15641                    "// comments should break alignment\n"
15642                    "//\n"
15643                    "#define bbbb 4\n"
15644                    "#define ccc (5)",
15645                    Style));
15646 
15647   // Test across empty lines and comments
15648   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments;
15649   verifyFormat("#define a    3\n"
15650                "\n"
15651                "// line comment\n"
15652                "#define bbbb 4\n"
15653                "#define ccc  (5)",
15654                Style);
15655 
15656   EXPECT_EQ("#define a    3\n"
15657             "\n"
15658             "\n"
15659             "/* multi-line *\n"
15660             " * block comment */\n"
15661             "\n"
15662             "\n"
15663             "#define bbbb 4\n"
15664             "#define ccc  (5)",
15665             format("#define a 3\n"
15666                    "\n"
15667                    "\n"
15668                    "/* multi-line *\n"
15669                    " * block comment */\n"
15670                    "\n"
15671                    "\n"
15672                    "#define bbbb 4\n"
15673                    "#define ccc (5)",
15674                    Style));
15675 
15676   EXPECT_EQ("#define a    3\n"
15677             "\n"
15678             "\n"
15679             "/* multi-line *\n"
15680             " * block comment */\n"
15681             "\n"
15682             "\n"
15683             "#define bbbb 4\n"
15684             "#define ccc  (5)",
15685             format("#define a 3\n"
15686                    "\n"
15687                    "\n"
15688                    "/* multi-line *\n"
15689                    " * block comment */\n"
15690                    "\n"
15691                    "\n"
15692                    "#define bbbb 4\n"
15693                    "#define ccc       (5)",
15694                    Style));
15695 }
15696 
15697 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
15698   FormatStyle Alignment = getLLVMStyle();
15699   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15700   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines;
15701 
15702   Alignment.MaxEmptyLinesToKeep = 10;
15703   /* Test alignment across empty lines */
15704   EXPECT_EQ("int a           = 5;\n"
15705             "\n"
15706             "int oneTwoThree = 123;",
15707             format("int a       = 5;\n"
15708                    "\n"
15709                    "int oneTwoThree= 123;",
15710                    Alignment));
15711   EXPECT_EQ("int a           = 5;\n"
15712             "int one         = 1;\n"
15713             "\n"
15714             "int oneTwoThree = 123;",
15715             format("int a = 5;\n"
15716                    "int one = 1;\n"
15717                    "\n"
15718                    "int oneTwoThree = 123;",
15719                    Alignment));
15720   EXPECT_EQ("int a           = 5;\n"
15721             "int one         = 1;\n"
15722             "\n"
15723             "int oneTwoThree = 123;\n"
15724             "int oneTwo      = 12;",
15725             format("int a = 5;\n"
15726                    "int one = 1;\n"
15727                    "\n"
15728                    "int oneTwoThree = 123;\n"
15729                    "int oneTwo = 12;",
15730                    Alignment));
15731 
15732   /* Test across comments */
15733   EXPECT_EQ("int a = 5;\n"
15734             "/* block comment */\n"
15735             "int oneTwoThree = 123;",
15736             format("int a = 5;\n"
15737                    "/* block comment */\n"
15738                    "int oneTwoThree=123;",
15739                    Alignment));
15740 
15741   EXPECT_EQ("int a = 5;\n"
15742             "// line comment\n"
15743             "int oneTwoThree = 123;",
15744             format("int a = 5;\n"
15745                    "// line comment\n"
15746                    "int oneTwoThree=123;",
15747                    Alignment));
15748 
15749   /* Test across comments and newlines */
15750   EXPECT_EQ("int a = 5;\n"
15751             "\n"
15752             "/* block comment */\n"
15753             "int oneTwoThree = 123;",
15754             format("int a = 5;\n"
15755                    "\n"
15756                    "/* block comment */\n"
15757                    "int oneTwoThree=123;",
15758                    Alignment));
15759 
15760   EXPECT_EQ("int a = 5;\n"
15761             "\n"
15762             "// line comment\n"
15763             "int oneTwoThree = 123;",
15764             format("int a = 5;\n"
15765                    "\n"
15766                    "// line comment\n"
15767                    "int oneTwoThree=123;",
15768                    Alignment));
15769 }
15770 
15771 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
15772   FormatStyle Alignment = getLLVMStyle();
15773   Alignment.AlignConsecutiveDeclarations =
15774       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15775   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15776 
15777   Alignment.MaxEmptyLinesToKeep = 10;
15778   /* Test alignment across empty lines */
15779   EXPECT_EQ("int         a = 5;\n"
15780             "\n"
15781             "float const oneTwoThree = 123;",
15782             format("int a = 5;\n"
15783                    "\n"
15784                    "float const oneTwoThree = 123;",
15785                    Alignment));
15786   EXPECT_EQ("int         a = 5;\n"
15787             "float const one = 1;\n"
15788             "\n"
15789             "int         oneTwoThree = 123;",
15790             format("int a = 5;\n"
15791                    "float const one = 1;\n"
15792                    "\n"
15793                    "int oneTwoThree = 123;",
15794                    Alignment));
15795 
15796   /* Test across comments */
15797   EXPECT_EQ("float const a = 5;\n"
15798             "/* block comment */\n"
15799             "int         oneTwoThree = 123;",
15800             format("float const a = 5;\n"
15801                    "/* block comment */\n"
15802                    "int oneTwoThree=123;",
15803                    Alignment));
15804 
15805   EXPECT_EQ("float const a = 5;\n"
15806             "// line comment\n"
15807             "int         oneTwoThree = 123;",
15808             format("float const a = 5;\n"
15809                    "// line comment\n"
15810                    "int oneTwoThree=123;",
15811                    Alignment));
15812 
15813   /* Test across comments and newlines */
15814   EXPECT_EQ("float const a = 5;\n"
15815             "\n"
15816             "/* block comment */\n"
15817             "int         oneTwoThree = 123;",
15818             format("float const a = 5;\n"
15819                    "\n"
15820                    "/* block comment */\n"
15821                    "int         oneTwoThree=123;",
15822                    Alignment));
15823 
15824   EXPECT_EQ("float const a = 5;\n"
15825             "\n"
15826             "// line comment\n"
15827             "int         oneTwoThree = 123;",
15828             format("float const a = 5;\n"
15829                    "\n"
15830                    "// line comment\n"
15831                    "int oneTwoThree=123;",
15832                    Alignment));
15833 }
15834 
15835 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) {
15836   FormatStyle Alignment = getLLVMStyle();
15837   Alignment.AlignConsecutiveBitFields =
15838       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15839 
15840   Alignment.MaxEmptyLinesToKeep = 10;
15841   /* Test alignment across empty lines */
15842   EXPECT_EQ("int a            : 5;\n"
15843             "\n"
15844             "int longbitfield : 6;",
15845             format("int a : 5;\n"
15846                    "\n"
15847                    "int longbitfield : 6;",
15848                    Alignment));
15849   EXPECT_EQ("int a            : 5;\n"
15850             "int one          : 1;\n"
15851             "\n"
15852             "int longbitfield : 6;",
15853             format("int a : 5;\n"
15854                    "int one : 1;\n"
15855                    "\n"
15856                    "int longbitfield : 6;",
15857                    Alignment));
15858 
15859   /* Test across comments */
15860   EXPECT_EQ("int a            : 5;\n"
15861             "/* block comment */\n"
15862             "int longbitfield : 6;",
15863             format("int a : 5;\n"
15864                    "/* block comment */\n"
15865                    "int longbitfield : 6;",
15866                    Alignment));
15867   EXPECT_EQ("int a            : 5;\n"
15868             "int one          : 1;\n"
15869             "// line comment\n"
15870             "int longbitfield : 6;",
15871             format("int a : 5;\n"
15872                    "int one : 1;\n"
15873                    "// line comment\n"
15874                    "int longbitfield : 6;",
15875                    Alignment));
15876 
15877   /* Test across comments and newlines */
15878   EXPECT_EQ("int a            : 5;\n"
15879             "/* block comment */\n"
15880             "\n"
15881             "int longbitfield : 6;",
15882             format("int a : 5;\n"
15883                    "/* block comment */\n"
15884                    "\n"
15885                    "int longbitfield : 6;",
15886                    Alignment));
15887   EXPECT_EQ("int a            : 5;\n"
15888             "int one          : 1;\n"
15889             "\n"
15890             "// line comment\n"
15891             "\n"
15892             "int longbitfield : 6;",
15893             format("int a : 5;\n"
15894                    "int one : 1;\n"
15895                    "\n"
15896                    "// line comment \n"
15897                    "\n"
15898                    "int longbitfield : 6;",
15899                    Alignment));
15900 }
15901 
15902 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) {
15903   FormatStyle Alignment = getLLVMStyle();
15904   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15905   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments;
15906 
15907   Alignment.MaxEmptyLinesToKeep = 10;
15908   /* Test alignment across empty lines */
15909   EXPECT_EQ("int a = 5;\n"
15910             "\n"
15911             "int oneTwoThree = 123;",
15912             format("int a       = 5;\n"
15913                    "\n"
15914                    "int oneTwoThree= 123;",
15915                    Alignment));
15916   EXPECT_EQ("int a   = 5;\n"
15917             "int one = 1;\n"
15918             "\n"
15919             "int oneTwoThree = 123;",
15920             format("int a = 5;\n"
15921                    "int one = 1;\n"
15922                    "\n"
15923                    "int oneTwoThree = 123;",
15924                    Alignment));
15925 
15926   /* Test across comments */
15927   EXPECT_EQ("int a           = 5;\n"
15928             "/* block comment */\n"
15929             "int oneTwoThree = 123;",
15930             format("int a = 5;\n"
15931                    "/* block comment */\n"
15932                    "int oneTwoThree=123;",
15933                    Alignment));
15934 
15935   EXPECT_EQ("int a           = 5;\n"
15936             "// line comment\n"
15937             "int oneTwoThree = 123;",
15938             format("int a = 5;\n"
15939                    "// line comment\n"
15940                    "int oneTwoThree=123;",
15941                    Alignment));
15942 
15943   EXPECT_EQ("int a           = 5;\n"
15944             "/*\n"
15945             " * multi-line block comment\n"
15946             " */\n"
15947             "int oneTwoThree = 123;",
15948             format("int a = 5;\n"
15949                    "/*\n"
15950                    " * multi-line block comment\n"
15951                    " */\n"
15952                    "int oneTwoThree=123;",
15953                    Alignment));
15954 
15955   EXPECT_EQ("int a           = 5;\n"
15956             "//\n"
15957             "// multi-line line comment\n"
15958             "//\n"
15959             "int oneTwoThree = 123;",
15960             format("int a = 5;\n"
15961                    "//\n"
15962                    "// multi-line line comment\n"
15963                    "//\n"
15964                    "int oneTwoThree=123;",
15965                    Alignment));
15966 
15967   /* Test across comments and newlines */
15968   EXPECT_EQ("int a = 5;\n"
15969             "\n"
15970             "/* block comment */\n"
15971             "int oneTwoThree = 123;",
15972             format("int a = 5;\n"
15973                    "\n"
15974                    "/* block comment */\n"
15975                    "int oneTwoThree=123;",
15976                    Alignment));
15977 
15978   EXPECT_EQ("int a = 5;\n"
15979             "\n"
15980             "// line comment\n"
15981             "int oneTwoThree = 123;",
15982             format("int a = 5;\n"
15983                    "\n"
15984                    "// line comment\n"
15985                    "int oneTwoThree=123;",
15986                    Alignment));
15987 }
15988 
15989 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) {
15990   FormatStyle Alignment = getLLVMStyle();
15991   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
15992   Alignment.AlignConsecutiveAssignments =
15993       FormatStyle::ACS_AcrossEmptyLinesAndComments;
15994   verifyFormat("int a           = 5;\n"
15995                "int oneTwoThree = 123;",
15996                Alignment);
15997   verifyFormat("int a           = method();\n"
15998                "int oneTwoThree = 133;",
15999                Alignment);
16000   verifyFormat("a &= 5;\n"
16001                "bcd *= 5;\n"
16002                "ghtyf += 5;\n"
16003                "dvfvdb -= 5;\n"
16004                "a /= 5;\n"
16005                "vdsvsv %= 5;\n"
16006                "sfdbddfbdfbb ^= 5;\n"
16007                "dvsdsv |= 5;\n"
16008                "int dsvvdvsdvvv = 123;",
16009                Alignment);
16010   verifyFormat("int i = 1, j = 10;\n"
16011                "something = 2000;",
16012                Alignment);
16013   verifyFormat("something = 2000;\n"
16014                "int i = 1, j = 10;\n",
16015                Alignment);
16016   verifyFormat("something = 2000;\n"
16017                "another   = 911;\n"
16018                "int i = 1, j = 10;\n"
16019                "oneMore = 1;\n"
16020                "i       = 2;",
16021                Alignment);
16022   verifyFormat("int a   = 5;\n"
16023                "int one = 1;\n"
16024                "method();\n"
16025                "int oneTwoThree = 123;\n"
16026                "int oneTwo      = 12;",
16027                Alignment);
16028   verifyFormat("int oneTwoThree = 123;\n"
16029                "int oneTwo      = 12;\n"
16030                "method();\n",
16031                Alignment);
16032   verifyFormat("int oneTwoThree = 123; // comment\n"
16033                "int oneTwo      = 12;  // comment",
16034                Alignment);
16035 
16036   // Bug 25167
16037   /* Uncomment when fixed
16038     verifyFormat("#if A\n"
16039                  "#else\n"
16040                  "int aaaaaaaa = 12;\n"
16041                  "#endif\n"
16042                  "#if B\n"
16043                  "#else\n"
16044                  "int a = 12;\n"
16045                  "#endif\n",
16046                  Alignment);
16047     verifyFormat("enum foo {\n"
16048                  "#if A\n"
16049                  "#else\n"
16050                  "  aaaaaaaa = 12;\n"
16051                  "#endif\n"
16052                  "#if B\n"
16053                  "#else\n"
16054                  "  a = 12;\n"
16055                  "#endif\n"
16056                  "};\n",
16057                  Alignment);
16058   */
16059 
16060   Alignment.MaxEmptyLinesToKeep = 10;
16061   /* Test alignment across empty lines */
16062   EXPECT_EQ("int a           = 5;\n"
16063             "\n"
16064             "int oneTwoThree = 123;",
16065             format("int a       = 5;\n"
16066                    "\n"
16067                    "int oneTwoThree= 123;",
16068                    Alignment));
16069   EXPECT_EQ("int a           = 5;\n"
16070             "int one         = 1;\n"
16071             "\n"
16072             "int oneTwoThree = 123;",
16073             format("int a = 5;\n"
16074                    "int one = 1;\n"
16075                    "\n"
16076                    "int oneTwoThree = 123;",
16077                    Alignment));
16078   EXPECT_EQ("int a           = 5;\n"
16079             "int one         = 1;\n"
16080             "\n"
16081             "int oneTwoThree = 123;\n"
16082             "int oneTwo      = 12;",
16083             format("int a = 5;\n"
16084                    "int one = 1;\n"
16085                    "\n"
16086                    "int oneTwoThree = 123;\n"
16087                    "int oneTwo = 12;",
16088                    Alignment));
16089 
16090   /* Test across comments */
16091   EXPECT_EQ("int a           = 5;\n"
16092             "/* block comment */\n"
16093             "int oneTwoThree = 123;",
16094             format("int a = 5;\n"
16095                    "/* block comment */\n"
16096                    "int oneTwoThree=123;",
16097                    Alignment));
16098 
16099   EXPECT_EQ("int a           = 5;\n"
16100             "// line comment\n"
16101             "int oneTwoThree = 123;",
16102             format("int a = 5;\n"
16103                    "// line comment\n"
16104                    "int oneTwoThree=123;",
16105                    Alignment));
16106 
16107   /* Test across comments and newlines */
16108   EXPECT_EQ("int a           = 5;\n"
16109             "\n"
16110             "/* block comment */\n"
16111             "int oneTwoThree = 123;",
16112             format("int a = 5;\n"
16113                    "\n"
16114                    "/* block comment */\n"
16115                    "int oneTwoThree=123;",
16116                    Alignment));
16117 
16118   EXPECT_EQ("int a           = 5;\n"
16119             "\n"
16120             "// line comment\n"
16121             "int oneTwoThree = 123;",
16122             format("int a = 5;\n"
16123                    "\n"
16124                    "// line comment\n"
16125                    "int oneTwoThree=123;",
16126                    Alignment));
16127 
16128   EXPECT_EQ("int a           = 5;\n"
16129             "//\n"
16130             "// multi-line line comment\n"
16131             "//\n"
16132             "int oneTwoThree = 123;",
16133             format("int a = 5;\n"
16134                    "//\n"
16135                    "// multi-line line comment\n"
16136                    "//\n"
16137                    "int oneTwoThree=123;",
16138                    Alignment));
16139 
16140   EXPECT_EQ("int a           = 5;\n"
16141             "/*\n"
16142             " *  multi-line block comment\n"
16143             " */\n"
16144             "int oneTwoThree = 123;",
16145             format("int a = 5;\n"
16146                    "/*\n"
16147                    " *  multi-line block comment\n"
16148                    " */\n"
16149                    "int oneTwoThree=123;",
16150                    Alignment));
16151 
16152   EXPECT_EQ("int a           = 5;\n"
16153             "\n"
16154             "/* block comment */\n"
16155             "\n"
16156             "\n"
16157             "\n"
16158             "int oneTwoThree = 123;",
16159             format("int a = 5;\n"
16160                    "\n"
16161                    "/* block comment */\n"
16162                    "\n"
16163                    "\n"
16164                    "\n"
16165                    "int oneTwoThree=123;",
16166                    Alignment));
16167 
16168   EXPECT_EQ("int a           = 5;\n"
16169             "\n"
16170             "// line comment\n"
16171             "\n"
16172             "\n"
16173             "\n"
16174             "int oneTwoThree = 123;",
16175             format("int a = 5;\n"
16176                    "\n"
16177                    "// line comment\n"
16178                    "\n"
16179                    "\n"
16180                    "\n"
16181                    "int oneTwoThree=123;",
16182                    Alignment));
16183 
16184   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
16185   verifyFormat("#define A \\\n"
16186                "  int aaaa       = 12; \\\n"
16187                "  int b          = 23; \\\n"
16188                "  int ccc        = 234; \\\n"
16189                "  int dddddddddd = 2345;",
16190                Alignment);
16191   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
16192   verifyFormat("#define A               \\\n"
16193                "  int aaaa       = 12;  \\\n"
16194                "  int b          = 23;  \\\n"
16195                "  int ccc        = 234; \\\n"
16196                "  int dddddddddd = 2345;",
16197                Alignment);
16198   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
16199   verifyFormat("#define A                                                      "
16200                "                \\\n"
16201                "  int aaaa       = 12;                                         "
16202                "                \\\n"
16203                "  int b          = 23;                                         "
16204                "                \\\n"
16205                "  int ccc        = 234;                                        "
16206                "                \\\n"
16207                "  int dddddddddd = 2345;",
16208                Alignment);
16209   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
16210                "k = 4, int l = 5,\n"
16211                "                  int m = 6) {\n"
16212                "  int j      = 10;\n"
16213                "  otherThing = 1;\n"
16214                "}",
16215                Alignment);
16216   verifyFormat("void SomeFunction(int parameter = 0) {\n"
16217                "  int i   = 1;\n"
16218                "  int j   = 2;\n"
16219                "  int big = 10000;\n"
16220                "}",
16221                Alignment);
16222   verifyFormat("class C {\n"
16223                "public:\n"
16224                "  int i            = 1;\n"
16225                "  virtual void f() = 0;\n"
16226                "};",
16227                Alignment);
16228   verifyFormat("int i = 1;\n"
16229                "if (SomeType t = getSomething()) {\n"
16230                "}\n"
16231                "int j   = 2;\n"
16232                "int big = 10000;",
16233                Alignment);
16234   verifyFormat("int j = 7;\n"
16235                "for (int k = 0; k < N; ++k) {\n"
16236                "}\n"
16237                "int j   = 2;\n"
16238                "int big = 10000;\n"
16239                "}",
16240                Alignment);
16241   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16242   verifyFormat("int i = 1;\n"
16243                "LooooooooooongType loooooooooooooooooooooongVariable\n"
16244                "    = someLooooooooooooooooongFunction();\n"
16245                "int j = 2;",
16246                Alignment);
16247   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
16248   verifyFormat("int i = 1;\n"
16249                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
16250                "    someLooooooooooooooooongFunction();\n"
16251                "int j = 2;",
16252                Alignment);
16253 
16254   verifyFormat("auto lambda = []() {\n"
16255                "  auto i = 0;\n"
16256                "  return 0;\n"
16257                "};\n"
16258                "int i  = 0;\n"
16259                "auto v = type{\n"
16260                "    i = 1,   //\n"
16261                "    (i = 2), //\n"
16262                "    i = 3    //\n"
16263                "};",
16264                Alignment);
16265 
16266   verifyFormat(
16267       "int i      = 1;\n"
16268       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
16269       "                          loooooooooooooooooooooongParameterB);\n"
16270       "int j      = 2;",
16271       Alignment);
16272 
16273   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
16274                "          typename B   = very_long_type_name_1,\n"
16275                "          typename T_2 = very_long_type_name_2>\n"
16276                "auto foo() {}\n",
16277                Alignment);
16278   verifyFormat("int a, b = 1;\n"
16279                "int c  = 2;\n"
16280                "int dd = 3;\n",
16281                Alignment);
16282   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
16283                "float b[1][] = {{3.f}};\n",
16284                Alignment);
16285   verifyFormat("for (int i = 0; i < 1; i++)\n"
16286                "  int x = 1;\n",
16287                Alignment);
16288   verifyFormat("for (i = 0; i < 1; i++)\n"
16289                "  x = 1;\n"
16290                "y = 1;\n",
16291                Alignment);
16292 
16293   Alignment.ReflowComments = true;
16294   Alignment.ColumnLimit = 50;
16295   EXPECT_EQ("int x   = 0;\n"
16296             "int yy  = 1; /// specificlennospace\n"
16297             "int zzz = 2;\n",
16298             format("int x   = 0;\n"
16299                    "int yy  = 1; ///specificlennospace\n"
16300                    "int zzz = 2;\n",
16301                    Alignment));
16302 }
16303 
16304 TEST_F(FormatTest, AlignConsecutiveAssignments) {
16305   FormatStyle Alignment = getLLVMStyle();
16306   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
16307   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16308   verifyFormat("int a = 5;\n"
16309                "int oneTwoThree = 123;",
16310                Alignment);
16311   verifyFormat("int a = 5;\n"
16312                "int oneTwoThree = 123;",
16313                Alignment);
16314 
16315   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16316   verifyFormat("int a           = 5;\n"
16317                "int oneTwoThree = 123;",
16318                Alignment);
16319   verifyFormat("int a           = method();\n"
16320                "int oneTwoThree = 133;",
16321                Alignment);
16322   verifyFormat("a &= 5;\n"
16323                "bcd *= 5;\n"
16324                "ghtyf += 5;\n"
16325                "dvfvdb -= 5;\n"
16326                "a /= 5;\n"
16327                "vdsvsv %= 5;\n"
16328                "sfdbddfbdfbb ^= 5;\n"
16329                "dvsdsv |= 5;\n"
16330                "int dsvvdvsdvvv = 123;",
16331                Alignment);
16332   verifyFormat("int i = 1, j = 10;\n"
16333                "something = 2000;",
16334                Alignment);
16335   verifyFormat("something = 2000;\n"
16336                "int i = 1, j = 10;\n",
16337                Alignment);
16338   verifyFormat("something = 2000;\n"
16339                "another   = 911;\n"
16340                "int i = 1, j = 10;\n"
16341                "oneMore = 1;\n"
16342                "i       = 2;",
16343                Alignment);
16344   verifyFormat("int a   = 5;\n"
16345                "int one = 1;\n"
16346                "method();\n"
16347                "int oneTwoThree = 123;\n"
16348                "int oneTwo      = 12;",
16349                Alignment);
16350   verifyFormat("int oneTwoThree = 123;\n"
16351                "int oneTwo      = 12;\n"
16352                "method();\n",
16353                Alignment);
16354   verifyFormat("int oneTwoThree = 123; // comment\n"
16355                "int oneTwo      = 12;  // comment",
16356                Alignment);
16357   verifyFormat("int f()         = default;\n"
16358                "int &operator() = default;\n"
16359                "int &operator=() {",
16360                Alignment);
16361   verifyFormat("int f()         = delete;\n"
16362                "int &operator() = delete;\n"
16363                "int &operator=() {",
16364                Alignment);
16365   verifyFormat("int f()         = default; // comment\n"
16366                "int &operator() = default; // comment\n"
16367                "int &operator=() {",
16368                Alignment);
16369   verifyFormat("int f()         = default;\n"
16370                "int &operator() = default;\n"
16371                "int &operator==() {",
16372                Alignment);
16373   verifyFormat("int f()         = default;\n"
16374                "int &operator() = default;\n"
16375                "int &operator<=() {",
16376                Alignment);
16377   verifyFormat("int f()         = default;\n"
16378                "int &operator() = default;\n"
16379                "int &operator!=() {",
16380                Alignment);
16381   verifyFormat("int f()         = default;\n"
16382                "int &operator() = default;\n"
16383                "int &operator=();",
16384                Alignment);
16385   verifyFormat("int f()         = delete;\n"
16386                "int &operator() = delete;\n"
16387                "int &operator=();",
16388                Alignment);
16389   verifyFormat("/* long long padding */ int f() = default;\n"
16390                "int &operator()                 = default;\n"
16391                "int &operator/**/ =();",
16392                Alignment);
16393   // https://llvm.org/PR33697
16394   FormatStyle AlignmentWithPenalty = getLLVMStyle();
16395   AlignmentWithPenalty.AlignConsecutiveAssignments =
16396       FormatStyle::ACS_Consecutive;
16397   AlignmentWithPenalty.PenaltyReturnTypeOnItsOwnLine = 5000;
16398   verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n"
16399                "  void f() = delete;\n"
16400                "  SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n"
16401                "      const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n"
16402                "};\n",
16403                AlignmentWithPenalty);
16404 
16405   // Bug 25167
16406   /* Uncomment when fixed
16407     verifyFormat("#if A\n"
16408                  "#else\n"
16409                  "int aaaaaaaa = 12;\n"
16410                  "#endif\n"
16411                  "#if B\n"
16412                  "#else\n"
16413                  "int a = 12;\n"
16414                  "#endif\n",
16415                  Alignment);
16416     verifyFormat("enum foo {\n"
16417                  "#if A\n"
16418                  "#else\n"
16419                  "  aaaaaaaa = 12;\n"
16420                  "#endif\n"
16421                  "#if B\n"
16422                  "#else\n"
16423                  "  a = 12;\n"
16424                  "#endif\n"
16425                  "};\n",
16426                  Alignment);
16427   */
16428 
16429   EXPECT_EQ("int a = 5;\n"
16430             "\n"
16431             "int oneTwoThree = 123;",
16432             format("int a       = 5;\n"
16433                    "\n"
16434                    "int oneTwoThree= 123;",
16435                    Alignment));
16436   EXPECT_EQ("int a   = 5;\n"
16437             "int one = 1;\n"
16438             "\n"
16439             "int oneTwoThree = 123;",
16440             format("int a = 5;\n"
16441                    "int one = 1;\n"
16442                    "\n"
16443                    "int oneTwoThree = 123;",
16444                    Alignment));
16445   EXPECT_EQ("int a   = 5;\n"
16446             "int one = 1;\n"
16447             "\n"
16448             "int oneTwoThree = 123;\n"
16449             "int oneTwo      = 12;",
16450             format("int a = 5;\n"
16451                    "int one = 1;\n"
16452                    "\n"
16453                    "int oneTwoThree = 123;\n"
16454                    "int oneTwo = 12;",
16455                    Alignment));
16456   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
16457   verifyFormat("#define A \\\n"
16458                "  int aaaa       = 12; \\\n"
16459                "  int b          = 23; \\\n"
16460                "  int ccc        = 234; \\\n"
16461                "  int dddddddddd = 2345;",
16462                Alignment);
16463   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
16464   verifyFormat("#define A               \\\n"
16465                "  int aaaa       = 12;  \\\n"
16466                "  int b          = 23;  \\\n"
16467                "  int ccc        = 234; \\\n"
16468                "  int dddddddddd = 2345;",
16469                Alignment);
16470   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
16471   verifyFormat("#define A                                                      "
16472                "                \\\n"
16473                "  int aaaa       = 12;                                         "
16474                "                \\\n"
16475                "  int b          = 23;                                         "
16476                "                \\\n"
16477                "  int ccc        = 234;                                        "
16478                "                \\\n"
16479                "  int dddddddddd = 2345;",
16480                Alignment);
16481   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
16482                "k = 4, int l = 5,\n"
16483                "                  int m = 6) {\n"
16484                "  int j      = 10;\n"
16485                "  otherThing = 1;\n"
16486                "}",
16487                Alignment);
16488   verifyFormat("void SomeFunction(int parameter = 0) {\n"
16489                "  int i   = 1;\n"
16490                "  int j   = 2;\n"
16491                "  int big = 10000;\n"
16492                "}",
16493                Alignment);
16494   verifyFormat("class C {\n"
16495                "public:\n"
16496                "  int i            = 1;\n"
16497                "  virtual void f() = 0;\n"
16498                "};",
16499                Alignment);
16500   verifyFormat("int i = 1;\n"
16501                "if (SomeType t = getSomething()) {\n"
16502                "}\n"
16503                "int j   = 2;\n"
16504                "int big = 10000;",
16505                Alignment);
16506   verifyFormat("int j = 7;\n"
16507                "for (int k = 0; k < N; ++k) {\n"
16508                "}\n"
16509                "int j   = 2;\n"
16510                "int big = 10000;\n"
16511                "}",
16512                Alignment);
16513   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16514   verifyFormat("int i = 1;\n"
16515                "LooooooooooongType loooooooooooooooooooooongVariable\n"
16516                "    = someLooooooooooooooooongFunction();\n"
16517                "int j = 2;",
16518                Alignment);
16519   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
16520   verifyFormat("int i = 1;\n"
16521                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
16522                "    someLooooooooooooooooongFunction();\n"
16523                "int j = 2;",
16524                Alignment);
16525 
16526   verifyFormat("auto lambda = []() {\n"
16527                "  auto i = 0;\n"
16528                "  return 0;\n"
16529                "};\n"
16530                "int i  = 0;\n"
16531                "auto v = type{\n"
16532                "    i = 1,   //\n"
16533                "    (i = 2), //\n"
16534                "    i = 3    //\n"
16535                "};",
16536                Alignment);
16537 
16538   verifyFormat(
16539       "int i      = 1;\n"
16540       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
16541       "                          loooooooooooooooooooooongParameterB);\n"
16542       "int j      = 2;",
16543       Alignment);
16544 
16545   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
16546                "          typename B   = very_long_type_name_1,\n"
16547                "          typename T_2 = very_long_type_name_2>\n"
16548                "auto foo() {}\n",
16549                Alignment);
16550   verifyFormat("int a, b = 1;\n"
16551                "int c  = 2;\n"
16552                "int dd = 3;\n",
16553                Alignment);
16554   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
16555                "float b[1][] = {{3.f}};\n",
16556                Alignment);
16557   verifyFormat("for (int i = 0; i < 1; i++)\n"
16558                "  int x = 1;\n",
16559                Alignment);
16560   verifyFormat("for (i = 0; i < 1; i++)\n"
16561                "  x = 1;\n"
16562                "y = 1;\n",
16563                Alignment);
16564 
16565   Alignment.ReflowComments = true;
16566   Alignment.ColumnLimit = 50;
16567   EXPECT_EQ("int x   = 0;\n"
16568             "int yy  = 1; /// specificlennospace\n"
16569             "int zzz = 2;\n",
16570             format("int x   = 0;\n"
16571                    "int yy  = 1; ///specificlennospace\n"
16572                    "int zzz = 2;\n",
16573                    Alignment));
16574 }
16575 
16576 TEST_F(FormatTest, AlignConsecutiveBitFields) {
16577   FormatStyle Alignment = getLLVMStyle();
16578   Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
16579   verifyFormat("int const a     : 5;\n"
16580                "int oneTwoThree : 23;",
16581                Alignment);
16582 
16583   // Initializers are allowed starting with c++2a
16584   verifyFormat("int const a     : 5 = 1;\n"
16585                "int oneTwoThree : 23 = 0;",
16586                Alignment);
16587 
16588   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16589   verifyFormat("int const a           : 5;\n"
16590                "int       oneTwoThree : 23;",
16591                Alignment);
16592 
16593   verifyFormat("int const a           : 5;  // comment\n"
16594                "int       oneTwoThree : 23; // comment",
16595                Alignment);
16596 
16597   verifyFormat("int const a           : 5 = 1;\n"
16598                "int       oneTwoThree : 23 = 0;",
16599                Alignment);
16600 
16601   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16602   verifyFormat("int const a           : 5  = 1;\n"
16603                "int       oneTwoThree : 23 = 0;",
16604                Alignment);
16605   verifyFormat("int const a           : 5  = {1};\n"
16606                "int       oneTwoThree : 23 = 0;",
16607                Alignment);
16608 
16609   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None;
16610   verifyFormat("int const a          :5;\n"
16611                "int       oneTwoThree:23;",
16612                Alignment);
16613 
16614   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before;
16615   verifyFormat("int const a           :5;\n"
16616                "int       oneTwoThree :23;",
16617                Alignment);
16618 
16619   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After;
16620   verifyFormat("int const a          : 5;\n"
16621                "int       oneTwoThree: 23;",
16622                Alignment);
16623 
16624   // Known limitations: ':' is only recognized as a bitfield colon when
16625   // followed by a number.
16626   /*
16627   verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
16628                "int a           : 5;",
16629                Alignment);
16630   */
16631 }
16632 
16633 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
16634   FormatStyle Alignment = getLLVMStyle();
16635   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
16636   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
16637   Alignment.PointerAlignment = FormatStyle::PAS_Right;
16638   verifyFormat("float const a = 5;\n"
16639                "int oneTwoThree = 123;",
16640                Alignment);
16641   verifyFormat("int a = 5;\n"
16642                "float const oneTwoThree = 123;",
16643                Alignment);
16644 
16645   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16646   verifyFormat("float const a = 5;\n"
16647                "int         oneTwoThree = 123;",
16648                Alignment);
16649   verifyFormat("int         a = method();\n"
16650                "float const oneTwoThree = 133;",
16651                Alignment);
16652   verifyFormat("int i = 1, j = 10;\n"
16653                "something = 2000;",
16654                Alignment);
16655   verifyFormat("something = 2000;\n"
16656                "int i = 1, j = 10;\n",
16657                Alignment);
16658   verifyFormat("float      something = 2000;\n"
16659                "double     another = 911;\n"
16660                "int        i = 1, j = 10;\n"
16661                "const int *oneMore = 1;\n"
16662                "unsigned   i = 2;",
16663                Alignment);
16664   verifyFormat("float a = 5;\n"
16665                "int   one = 1;\n"
16666                "method();\n"
16667                "const double       oneTwoThree = 123;\n"
16668                "const unsigned int oneTwo = 12;",
16669                Alignment);
16670   verifyFormat("int      oneTwoThree{0}; // comment\n"
16671                "unsigned oneTwo;         // comment",
16672                Alignment);
16673   verifyFormat("unsigned int       *a;\n"
16674                "int                *b;\n"
16675                "unsigned int Const *c;\n"
16676                "unsigned int const *d;\n"
16677                "unsigned int Const &e;\n"
16678                "unsigned int const &f;",
16679                Alignment);
16680   verifyFormat("Const unsigned int *c;\n"
16681                "const unsigned int *d;\n"
16682                "Const unsigned int &e;\n"
16683                "const unsigned int &f;\n"
16684                "const unsigned      g;\n"
16685                "Const unsigned      h;",
16686                Alignment);
16687   EXPECT_EQ("float const a = 5;\n"
16688             "\n"
16689             "int oneTwoThree = 123;",
16690             format("float const   a = 5;\n"
16691                    "\n"
16692                    "int           oneTwoThree= 123;",
16693                    Alignment));
16694   EXPECT_EQ("float a = 5;\n"
16695             "int   one = 1;\n"
16696             "\n"
16697             "unsigned oneTwoThree = 123;",
16698             format("float    a = 5;\n"
16699                    "int      one = 1;\n"
16700                    "\n"
16701                    "unsigned oneTwoThree = 123;",
16702                    Alignment));
16703   EXPECT_EQ("float a = 5;\n"
16704             "int   one = 1;\n"
16705             "\n"
16706             "unsigned oneTwoThree = 123;\n"
16707             "int      oneTwo = 12;",
16708             format("float    a = 5;\n"
16709                    "int one = 1;\n"
16710                    "\n"
16711                    "unsigned oneTwoThree = 123;\n"
16712                    "int oneTwo = 12;",
16713                    Alignment));
16714   // Function prototype alignment
16715   verifyFormat("int    a();\n"
16716                "double b();",
16717                Alignment);
16718   verifyFormat("int    a(int x);\n"
16719                "double b();",
16720                Alignment);
16721   unsigned OldColumnLimit = Alignment.ColumnLimit;
16722   // We need to set ColumnLimit to zero, in order to stress nested alignments,
16723   // otherwise the function parameters will be re-flowed onto a single line.
16724   Alignment.ColumnLimit = 0;
16725   EXPECT_EQ("int    a(int   x,\n"
16726             "         float y);\n"
16727             "double b(int    x,\n"
16728             "         double y);",
16729             format("int a(int x,\n"
16730                    " float y);\n"
16731                    "double b(int x,\n"
16732                    " double y);",
16733                    Alignment));
16734   // This ensures that function parameters of function declarations are
16735   // correctly indented when their owning functions are indented.
16736   // The failure case here is for 'double y' to not be indented enough.
16737   EXPECT_EQ("double a(int x);\n"
16738             "int    b(int    y,\n"
16739             "         double z);",
16740             format("double a(int x);\n"
16741                    "int b(int y,\n"
16742                    " double z);",
16743                    Alignment));
16744   // Set ColumnLimit low so that we induce wrapping immediately after
16745   // the function name and opening paren.
16746   Alignment.ColumnLimit = 13;
16747   verifyFormat("int function(\n"
16748                "    int  x,\n"
16749                "    bool y);",
16750                Alignment);
16751   Alignment.ColumnLimit = OldColumnLimit;
16752   // Ensure function pointers don't screw up recursive alignment
16753   verifyFormat("int    a(int x, void (*fp)(int y));\n"
16754                "double b();",
16755                Alignment);
16756   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16757   // Ensure recursive alignment is broken by function braces, so that the
16758   // "a = 1" does not align with subsequent assignments inside the function
16759   // body.
16760   verifyFormat("int func(int a = 1) {\n"
16761                "  int b  = 2;\n"
16762                "  int cc = 3;\n"
16763                "}",
16764                Alignment);
16765   verifyFormat("float      something = 2000;\n"
16766                "double     another   = 911;\n"
16767                "int        i = 1, j = 10;\n"
16768                "const int *oneMore = 1;\n"
16769                "unsigned   i       = 2;",
16770                Alignment);
16771   verifyFormat("int      oneTwoThree = {0}; // comment\n"
16772                "unsigned oneTwo      = 0;   // comment",
16773                Alignment);
16774   // Make sure that scope is correctly tracked, in the absence of braces
16775   verifyFormat("for (int i = 0; i < n; i++)\n"
16776                "  j = i;\n"
16777                "double x = 1;\n",
16778                Alignment);
16779   verifyFormat("if (int i = 0)\n"
16780                "  j = i;\n"
16781                "double x = 1;\n",
16782                Alignment);
16783   // Ensure operator[] and operator() are comprehended
16784   verifyFormat("struct test {\n"
16785                "  long long int foo();\n"
16786                "  int           operator[](int a);\n"
16787                "  double        bar();\n"
16788                "};\n",
16789                Alignment);
16790   verifyFormat("struct test {\n"
16791                "  long long int foo();\n"
16792                "  int           operator()(int a);\n"
16793                "  double        bar();\n"
16794                "};\n",
16795                Alignment);
16796   // http://llvm.org/PR52914
16797   verifyFormat("char *a[]     = {\"a\", // comment\n"
16798                "                 \"bb\"};\n"
16799                "int   bbbbbbb = 0;",
16800                Alignment);
16801 
16802   // PAS_Right
16803   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16804             "  int const i   = 1;\n"
16805             "  int      *j   = 2;\n"
16806             "  int       big = 10000;\n"
16807             "\n"
16808             "  unsigned oneTwoThree = 123;\n"
16809             "  int      oneTwo      = 12;\n"
16810             "  method();\n"
16811             "  float k  = 2;\n"
16812             "  int   ll = 10000;\n"
16813             "}",
16814             format("void SomeFunction(int parameter= 0) {\n"
16815                    " int const  i= 1;\n"
16816                    "  int *j=2;\n"
16817                    " int big  =  10000;\n"
16818                    "\n"
16819                    "unsigned oneTwoThree  =123;\n"
16820                    "int oneTwo = 12;\n"
16821                    "  method();\n"
16822                    "float k= 2;\n"
16823                    "int ll=10000;\n"
16824                    "}",
16825                    Alignment));
16826   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16827             "  int const i   = 1;\n"
16828             "  int     **j   = 2, ***k;\n"
16829             "  int      &k   = i;\n"
16830             "  int     &&l   = i + j;\n"
16831             "  int       big = 10000;\n"
16832             "\n"
16833             "  unsigned oneTwoThree = 123;\n"
16834             "  int      oneTwo      = 12;\n"
16835             "  method();\n"
16836             "  float k  = 2;\n"
16837             "  int   ll = 10000;\n"
16838             "}",
16839             format("void SomeFunction(int parameter= 0) {\n"
16840                    " int const  i= 1;\n"
16841                    "  int **j=2,***k;\n"
16842                    "int &k=i;\n"
16843                    "int &&l=i+j;\n"
16844                    " int big  =  10000;\n"
16845                    "\n"
16846                    "unsigned oneTwoThree  =123;\n"
16847                    "int oneTwo = 12;\n"
16848                    "  method();\n"
16849                    "float k= 2;\n"
16850                    "int ll=10000;\n"
16851                    "}",
16852                    Alignment));
16853   // variables are aligned at their name, pointers are at the right most
16854   // position
16855   verifyFormat("int   *a;\n"
16856                "int  **b;\n"
16857                "int ***c;\n"
16858                "int    foobar;\n",
16859                Alignment);
16860 
16861   // PAS_Left
16862   FormatStyle AlignmentLeft = Alignment;
16863   AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left;
16864   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16865             "  int const i   = 1;\n"
16866             "  int*      j   = 2;\n"
16867             "  int       big = 10000;\n"
16868             "\n"
16869             "  unsigned oneTwoThree = 123;\n"
16870             "  int      oneTwo      = 12;\n"
16871             "  method();\n"
16872             "  float k  = 2;\n"
16873             "  int   ll = 10000;\n"
16874             "}",
16875             format("void SomeFunction(int parameter= 0) {\n"
16876                    " int const  i= 1;\n"
16877                    "  int *j=2;\n"
16878                    " int big  =  10000;\n"
16879                    "\n"
16880                    "unsigned oneTwoThree  =123;\n"
16881                    "int oneTwo = 12;\n"
16882                    "  method();\n"
16883                    "float k= 2;\n"
16884                    "int ll=10000;\n"
16885                    "}",
16886                    AlignmentLeft));
16887   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16888             "  int const i   = 1;\n"
16889             "  int**     j   = 2;\n"
16890             "  int&      k   = i;\n"
16891             "  int&&     l   = i + j;\n"
16892             "  int       big = 10000;\n"
16893             "\n"
16894             "  unsigned oneTwoThree = 123;\n"
16895             "  int      oneTwo      = 12;\n"
16896             "  method();\n"
16897             "  float k  = 2;\n"
16898             "  int   ll = 10000;\n"
16899             "}",
16900             format("void SomeFunction(int parameter= 0) {\n"
16901                    " int const  i= 1;\n"
16902                    "  int **j=2;\n"
16903                    "int &k=i;\n"
16904                    "int &&l=i+j;\n"
16905                    " int big  =  10000;\n"
16906                    "\n"
16907                    "unsigned oneTwoThree  =123;\n"
16908                    "int oneTwo = 12;\n"
16909                    "  method();\n"
16910                    "float k= 2;\n"
16911                    "int ll=10000;\n"
16912                    "}",
16913                    AlignmentLeft));
16914   // variables are aligned at their name, pointers are at the left most position
16915   verifyFormat("int*   a;\n"
16916                "int**  b;\n"
16917                "int*** c;\n"
16918                "int    foobar;\n",
16919                AlignmentLeft);
16920 
16921   // PAS_Middle
16922   FormatStyle AlignmentMiddle = Alignment;
16923   AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
16924   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16925             "  int const i   = 1;\n"
16926             "  int *     j   = 2;\n"
16927             "  int       big = 10000;\n"
16928             "\n"
16929             "  unsigned oneTwoThree = 123;\n"
16930             "  int      oneTwo      = 12;\n"
16931             "  method();\n"
16932             "  float k  = 2;\n"
16933             "  int   ll = 10000;\n"
16934             "}",
16935             format("void SomeFunction(int parameter= 0) {\n"
16936                    " int const  i= 1;\n"
16937                    "  int *j=2;\n"
16938                    " int big  =  10000;\n"
16939                    "\n"
16940                    "unsigned oneTwoThree  =123;\n"
16941                    "int oneTwo = 12;\n"
16942                    "  method();\n"
16943                    "float k= 2;\n"
16944                    "int ll=10000;\n"
16945                    "}",
16946                    AlignmentMiddle));
16947   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
16948             "  int const i   = 1;\n"
16949             "  int **    j   = 2, ***k;\n"
16950             "  int &     k   = i;\n"
16951             "  int &&    l   = i + j;\n"
16952             "  int       big = 10000;\n"
16953             "\n"
16954             "  unsigned oneTwoThree = 123;\n"
16955             "  int      oneTwo      = 12;\n"
16956             "  method();\n"
16957             "  float k  = 2;\n"
16958             "  int   ll = 10000;\n"
16959             "}",
16960             format("void SomeFunction(int parameter= 0) {\n"
16961                    " int const  i= 1;\n"
16962                    "  int **j=2,***k;\n"
16963                    "int &k=i;\n"
16964                    "int &&l=i+j;\n"
16965                    " int big  =  10000;\n"
16966                    "\n"
16967                    "unsigned oneTwoThree  =123;\n"
16968                    "int oneTwo = 12;\n"
16969                    "  method();\n"
16970                    "float k= 2;\n"
16971                    "int ll=10000;\n"
16972                    "}",
16973                    AlignmentMiddle));
16974   // variables are aligned at their name, pointers are in the middle
16975   verifyFormat("int *   a;\n"
16976                "int *   b;\n"
16977                "int *** c;\n"
16978                "int     foobar;\n",
16979                AlignmentMiddle);
16980 
16981   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
16982   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
16983   verifyFormat("#define A \\\n"
16984                "  int       aaaa = 12; \\\n"
16985                "  float     b = 23; \\\n"
16986                "  const int ccc = 234; \\\n"
16987                "  unsigned  dddddddddd = 2345;",
16988                Alignment);
16989   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
16990   verifyFormat("#define A              \\\n"
16991                "  int       aaaa = 12; \\\n"
16992                "  float     b = 23;    \\\n"
16993                "  const int ccc = 234; \\\n"
16994                "  unsigned  dddddddddd = 2345;",
16995                Alignment);
16996   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
16997   Alignment.ColumnLimit = 30;
16998   verifyFormat("#define A                    \\\n"
16999                "  int       aaaa = 12;       \\\n"
17000                "  float     b = 23;          \\\n"
17001                "  const int ccc = 234;       \\\n"
17002                "  int       dddddddddd = 2345;",
17003                Alignment);
17004   Alignment.ColumnLimit = 80;
17005   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
17006                "k = 4, int l = 5,\n"
17007                "                  int m = 6) {\n"
17008                "  const int j = 10;\n"
17009                "  otherThing = 1;\n"
17010                "}",
17011                Alignment);
17012   verifyFormat("void SomeFunction(int parameter = 0) {\n"
17013                "  int const i = 1;\n"
17014                "  int      *j = 2;\n"
17015                "  int       big = 10000;\n"
17016                "}",
17017                Alignment);
17018   verifyFormat("class C {\n"
17019                "public:\n"
17020                "  int          i = 1;\n"
17021                "  virtual void f() = 0;\n"
17022                "};",
17023                Alignment);
17024   verifyFormat("float i = 1;\n"
17025                "if (SomeType t = getSomething()) {\n"
17026                "}\n"
17027                "const unsigned j = 2;\n"
17028                "int            big = 10000;",
17029                Alignment);
17030   verifyFormat("float j = 7;\n"
17031                "for (int k = 0; k < N; ++k) {\n"
17032                "}\n"
17033                "unsigned j = 2;\n"
17034                "int      big = 10000;\n"
17035                "}",
17036                Alignment);
17037   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
17038   verifyFormat("float              i = 1;\n"
17039                "LooooooooooongType loooooooooooooooooooooongVariable\n"
17040                "    = someLooooooooooooooooongFunction();\n"
17041                "int j = 2;",
17042                Alignment);
17043   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
17044   verifyFormat("int                i = 1;\n"
17045                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
17046                "    someLooooooooooooooooongFunction();\n"
17047                "int j = 2;",
17048                Alignment);
17049 
17050   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17051   verifyFormat("auto lambda = []() {\n"
17052                "  auto  ii = 0;\n"
17053                "  float j  = 0;\n"
17054                "  return 0;\n"
17055                "};\n"
17056                "int   i  = 0;\n"
17057                "float i2 = 0;\n"
17058                "auto  v  = type{\n"
17059                "    i = 1,   //\n"
17060                "    (i = 2), //\n"
17061                "    i = 3    //\n"
17062                "};",
17063                Alignment);
17064   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17065 
17066   verifyFormat(
17067       "int      i = 1;\n"
17068       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
17069       "                          loooooooooooooooooooooongParameterB);\n"
17070       "int      j = 2;",
17071       Alignment);
17072 
17073   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
17074   // We expect declarations and assignments to align, as long as it doesn't
17075   // exceed the column limit, starting a new alignment sequence whenever it
17076   // happens.
17077   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17078   Alignment.ColumnLimit = 30;
17079   verifyFormat("float    ii              = 1;\n"
17080                "unsigned j               = 2;\n"
17081                "int someVerylongVariable = 1;\n"
17082                "AnotherLongType  ll = 123456;\n"
17083                "VeryVeryLongType k  = 2;\n"
17084                "int              myvar = 1;",
17085                Alignment);
17086   Alignment.ColumnLimit = 80;
17087   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17088 
17089   verifyFormat(
17090       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
17091       "          typename LongType, typename B>\n"
17092       "auto foo() {}\n",
17093       Alignment);
17094   verifyFormat("float a, b = 1;\n"
17095                "int   c = 2;\n"
17096                "int   dd = 3;\n",
17097                Alignment);
17098   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
17099                "float b[1][] = {{3.f}};\n",
17100                Alignment);
17101   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17102   verifyFormat("float a, b = 1;\n"
17103                "int   c  = 2;\n"
17104                "int   dd = 3;\n",
17105                Alignment);
17106   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
17107                "float b[1][] = {{3.f}};\n",
17108                Alignment);
17109   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17110 
17111   Alignment.ColumnLimit = 30;
17112   Alignment.BinPackParameters = false;
17113   verifyFormat("void foo(float     a,\n"
17114                "         float     b,\n"
17115                "         int       c,\n"
17116                "         uint32_t *d) {\n"
17117                "  int   *e = 0;\n"
17118                "  float  f = 0;\n"
17119                "  double g = 0;\n"
17120                "}\n"
17121                "void bar(ino_t     a,\n"
17122                "         int       b,\n"
17123                "         uint32_t *c,\n"
17124                "         bool      d) {}\n",
17125                Alignment);
17126   Alignment.BinPackParameters = true;
17127   Alignment.ColumnLimit = 80;
17128 
17129   // Bug 33507
17130   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
17131   verifyFormat(
17132       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
17133       "  static const Version verVs2017;\n"
17134       "  return true;\n"
17135       "});\n",
17136       Alignment);
17137   Alignment.PointerAlignment = FormatStyle::PAS_Right;
17138 
17139   // See llvm.org/PR35641
17140   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17141   verifyFormat("int func() { //\n"
17142                "  int      b;\n"
17143                "  unsigned c;\n"
17144                "}",
17145                Alignment);
17146 
17147   // See PR37175
17148   FormatStyle Style = getMozillaStyle();
17149   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17150   EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
17151             "foo(int a);",
17152             format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style));
17153 
17154   Alignment.PointerAlignment = FormatStyle::PAS_Left;
17155   verifyFormat("unsigned int*       a;\n"
17156                "int*                b;\n"
17157                "unsigned int Const* c;\n"
17158                "unsigned int const* d;\n"
17159                "unsigned int Const& e;\n"
17160                "unsigned int const& f;",
17161                Alignment);
17162   verifyFormat("Const unsigned int* c;\n"
17163                "const unsigned int* d;\n"
17164                "Const unsigned int& e;\n"
17165                "const unsigned int& f;\n"
17166                "const unsigned      g;\n"
17167                "Const unsigned      h;",
17168                Alignment);
17169 
17170   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
17171   verifyFormat("unsigned int *       a;\n"
17172                "int *                b;\n"
17173                "unsigned int Const * c;\n"
17174                "unsigned int const * d;\n"
17175                "unsigned int Const & e;\n"
17176                "unsigned int const & f;",
17177                Alignment);
17178   verifyFormat("Const unsigned int * c;\n"
17179                "const unsigned int * d;\n"
17180                "Const unsigned int & e;\n"
17181                "const unsigned int & f;\n"
17182                "const unsigned       g;\n"
17183                "Const unsigned       h;",
17184                Alignment);
17185 }
17186 
17187 TEST_F(FormatTest, AlignWithLineBreaks) {
17188   auto Style = getLLVMStyleWithColumns(120);
17189 
17190   EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None);
17191   EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None);
17192   verifyFormat("void foo() {\n"
17193                "  int myVar = 5;\n"
17194                "  double x = 3.14;\n"
17195                "  auto str = \"Hello \"\n"
17196                "             \"World\";\n"
17197                "  auto s = \"Hello \"\n"
17198                "           \"Again\";\n"
17199                "}",
17200                Style);
17201 
17202   // clang-format off
17203   verifyFormat("void foo() {\n"
17204                "  const int capacityBefore = Entries.capacity();\n"
17205                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17206                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17207                "  const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17208                "                                          std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17209                "}",
17210                Style);
17211   // clang-format on
17212 
17213   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17214   verifyFormat("void foo() {\n"
17215                "  int myVar = 5;\n"
17216                "  double x  = 3.14;\n"
17217                "  auto str  = \"Hello \"\n"
17218                "              \"World\";\n"
17219                "  auto s    = \"Hello \"\n"
17220                "              \"Again\";\n"
17221                "}",
17222                Style);
17223 
17224   // clang-format off
17225   verifyFormat("void foo() {\n"
17226                "  const int capacityBefore = Entries.capacity();\n"
17227                "  const auto newEntry      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17228                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17229                "  const X newEntry2        = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17230                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17231                "}",
17232                Style);
17233   // clang-format on
17234 
17235   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17236   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17237   verifyFormat("void foo() {\n"
17238                "  int    myVar = 5;\n"
17239                "  double x = 3.14;\n"
17240                "  auto   str = \"Hello \"\n"
17241                "               \"World\";\n"
17242                "  auto   s = \"Hello \"\n"
17243                "             \"Again\";\n"
17244                "}",
17245                Style);
17246 
17247   // clang-format off
17248   verifyFormat("void foo() {\n"
17249                "  const int  capacityBefore = Entries.capacity();\n"
17250                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17251                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17252                "  const X    newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17253                "                                             std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17254                "}",
17255                Style);
17256   // clang-format on
17257 
17258   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17259   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17260 
17261   verifyFormat("void foo() {\n"
17262                "  int    myVar = 5;\n"
17263                "  double x     = 3.14;\n"
17264                "  auto   str   = \"Hello \"\n"
17265                "                 \"World\";\n"
17266                "  auto   s     = \"Hello \"\n"
17267                "                 \"Again\";\n"
17268                "}",
17269                Style);
17270 
17271   // clang-format off
17272   verifyFormat("void foo() {\n"
17273                "  const int  capacityBefore = Entries.capacity();\n"
17274                "  const auto newEntry       = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17275                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17276                "  const X    newEntry2      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
17277                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
17278                "}",
17279                Style);
17280   // clang-format on
17281 
17282   Style = getLLVMStyleWithColumns(120);
17283   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17284   Style.ContinuationIndentWidth = 4;
17285   Style.IndentWidth = 4;
17286 
17287   // clang-format off
17288   verifyFormat("void SomeFunc() {\n"
17289                "    newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
17290                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17291                "    newWatcher.maxAge     = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
17292                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17293                "    newWatcher.max        = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
17294                "                                                        seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17295                "}",
17296                Style);
17297   // clang-format on
17298 
17299   Style.BinPackArguments = false;
17300 
17301   // clang-format off
17302   verifyFormat("void SomeFunc() {\n"
17303                "    newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n"
17304                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17305                "    newWatcher.maxAge     = ToLegacyTimestamp(GetMaxAge(\n"
17306                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17307                "    newWatcher.max        = ToLegacyTimestamp(GetMaxAge(\n"
17308                "        FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
17309                "}",
17310                Style);
17311   // clang-format on
17312 }
17313 
17314 TEST_F(FormatTest, AlignWithInitializerPeriods) {
17315   auto Style = getLLVMStyleWithColumns(60);
17316 
17317   verifyFormat("void foo1(void) {\n"
17318                "  BYTE p[1] = 1;\n"
17319                "  A B = {.one_foooooooooooooooo = 2,\n"
17320                "         .two_fooooooooooooo = 3,\n"
17321                "         .three_fooooooooooooo = 4};\n"
17322                "  BYTE payload = 2;\n"
17323                "}",
17324                Style);
17325 
17326   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17327   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
17328   verifyFormat("void foo2(void) {\n"
17329                "  BYTE p[1]    = 1;\n"
17330                "  A B          = {.one_foooooooooooooooo = 2,\n"
17331                "                  .two_fooooooooooooo    = 3,\n"
17332                "                  .three_fooooooooooooo  = 4};\n"
17333                "  BYTE payload = 2;\n"
17334                "}",
17335                Style);
17336 
17337   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
17338   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17339   verifyFormat("void foo3(void) {\n"
17340                "  BYTE p[1] = 1;\n"
17341                "  A    B = {.one_foooooooooooooooo = 2,\n"
17342                "            .two_fooooooooooooo = 3,\n"
17343                "            .three_fooooooooooooo = 4};\n"
17344                "  BYTE payload = 2;\n"
17345                "}",
17346                Style);
17347 
17348   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17349   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17350   verifyFormat("void foo4(void) {\n"
17351                "  BYTE p[1]    = 1;\n"
17352                "  A    B       = {.one_foooooooooooooooo = 2,\n"
17353                "                  .two_fooooooooooooo    = 3,\n"
17354                "                  .three_fooooooooooooo  = 4};\n"
17355                "  BYTE payload = 2;\n"
17356                "}",
17357                Style);
17358 }
17359 
17360 TEST_F(FormatTest, LinuxBraceBreaking) {
17361   FormatStyle LinuxBraceStyle = getLLVMStyle();
17362   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
17363   verifyFormat("namespace a\n"
17364                "{\n"
17365                "class A\n"
17366                "{\n"
17367                "  void f()\n"
17368                "  {\n"
17369                "    if (true) {\n"
17370                "      a();\n"
17371                "      b();\n"
17372                "    } else {\n"
17373                "      a();\n"
17374                "    }\n"
17375                "  }\n"
17376                "  void g() { return; }\n"
17377                "};\n"
17378                "struct B {\n"
17379                "  int x;\n"
17380                "};\n"
17381                "} // namespace a\n",
17382                LinuxBraceStyle);
17383   verifyFormat("enum X {\n"
17384                "  Y = 0,\n"
17385                "}\n",
17386                LinuxBraceStyle);
17387   verifyFormat("struct S {\n"
17388                "  int Type;\n"
17389                "  union {\n"
17390                "    int x;\n"
17391                "    double y;\n"
17392                "  } Value;\n"
17393                "  class C\n"
17394                "  {\n"
17395                "    MyFavoriteType Value;\n"
17396                "  } Class;\n"
17397                "}\n",
17398                LinuxBraceStyle);
17399 }
17400 
17401 TEST_F(FormatTest, MozillaBraceBreaking) {
17402   FormatStyle MozillaBraceStyle = getLLVMStyle();
17403   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
17404   MozillaBraceStyle.FixNamespaceComments = false;
17405   verifyFormat("namespace a {\n"
17406                "class A\n"
17407                "{\n"
17408                "  void f()\n"
17409                "  {\n"
17410                "    if (true) {\n"
17411                "      a();\n"
17412                "      b();\n"
17413                "    }\n"
17414                "  }\n"
17415                "  void g() { return; }\n"
17416                "};\n"
17417                "enum E\n"
17418                "{\n"
17419                "  A,\n"
17420                "  // foo\n"
17421                "  B,\n"
17422                "  C\n"
17423                "};\n"
17424                "struct B\n"
17425                "{\n"
17426                "  int x;\n"
17427                "};\n"
17428                "}\n",
17429                MozillaBraceStyle);
17430   verifyFormat("struct S\n"
17431                "{\n"
17432                "  int Type;\n"
17433                "  union\n"
17434                "  {\n"
17435                "    int x;\n"
17436                "    double y;\n"
17437                "  } Value;\n"
17438                "  class C\n"
17439                "  {\n"
17440                "    MyFavoriteType Value;\n"
17441                "  } Class;\n"
17442                "}\n",
17443                MozillaBraceStyle);
17444 }
17445 
17446 TEST_F(FormatTest, StroustrupBraceBreaking) {
17447   FormatStyle StroustrupBraceStyle = getLLVMStyle();
17448   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
17449   verifyFormat("namespace a {\n"
17450                "class A {\n"
17451                "  void f()\n"
17452                "  {\n"
17453                "    if (true) {\n"
17454                "      a();\n"
17455                "      b();\n"
17456                "    }\n"
17457                "  }\n"
17458                "  void g() { return; }\n"
17459                "};\n"
17460                "struct B {\n"
17461                "  int x;\n"
17462                "};\n"
17463                "} // namespace a\n",
17464                StroustrupBraceStyle);
17465 
17466   verifyFormat("void foo()\n"
17467                "{\n"
17468                "  if (a) {\n"
17469                "    a();\n"
17470                "  }\n"
17471                "  else {\n"
17472                "    b();\n"
17473                "  }\n"
17474                "}\n",
17475                StroustrupBraceStyle);
17476 
17477   verifyFormat("#ifdef _DEBUG\n"
17478                "int foo(int i = 0)\n"
17479                "#else\n"
17480                "int foo(int i = 5)\n"
17481                "#endif\n"
17482                "{\n"
17483                "  return i;\n"
17484                "}",
17485                StroustrupBraceStyle);
17486 
17487   verifyFormat("void foo() {}\n"
17488                "void bar()\n"
17489                "#ifdef _DEBUG\n"
17490                "{\n"
17491                "  foo();\n"
17492                "}\n"
17493                "#else\n"
17494                "{\n"
17495                "}\n"
17496                "#endif",
17497                StroustrupBraceStyle);
17498 
17499   verifyFormat("void foobar() { int i = 5; }\n"
17500                "#ifdef _DEBUG\n"
17501                "void bar() {}\n"
17502                "#else\n"
17503                "void bar() { foobar(); }\n"
17504                "#endif",
17505                StroustrupBraceStyle);
17506 }
17507 
17508 TEST_F(FormatTest, AllmanBraceBreaking) {
17509   FormatStyle AllmanBraceStyle = getLLVMStyle();
17510   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
17511 
17512   EXPECT_EQ("namespace a\n"
17513             "{\n"
17514             "void f();\n"
17515             "void g();\n"
17516             "} // namespace a\n",
17517             format("namespace a\n"
17518                    "{\n"
17519                    "void f();\n"
17520                    "void g();\n"
17521                    "}\n",
17522                    AllmanBraceStyle));
17523 
17524   verifyFormat("namespace a\n"
17525                "{\n"
17526                "class A\n"
17527                "{\n"
17528                "  void f()\n"
17529                "  {\n"
17530                "    if (true)\n"
17531                "    {\n"
17532                "      a();\n"
17533                "      b();\n"
17534                "    }\n"
17535                "  }\n"
17536                "  void g() { return; }\n"
17537                "};\n"
17538                "struct B\n"
17539                "{\n"
17540                "  int x;\n"
17541                "};\n"
17542                "union C\n"
17543                "{\n"
17544                "};\n"
17545                "} // namespace a",
17546                AllmanBraceStyle);
17547 
17548   verifyFormat("void f()\n"
17549                "{\n"
17550                "  if (true)\n"
17551                "  {\n"
17552                "    a();\n"
17553                "  }\n"
17554                "  else if (false)\n"
17555                "  {\n"
17556                "    b();\n"
17557                "  }\n"
17558                "  else\n"
17559                "  {\n"
17560                "    c();\n"
17561                "  }\n"
17562                "}\n",
17563                AllmanBraceStyle);
17564 
17565   verifyFormat("void f()\n"
17566                "{\n"
17567                "  for (int i = 0; i < 10; ++i)\n"
17568                "  {\n"
17569                "    a();\n"
17570                "  }\n"
17571                "  while (false)\n"
17572                "  {\n"
17573                "    b();\n"
17574                "  }\n"
17575                "  do\n"
17576                "  {\n"
17577                "    c();\n"
17578                "  } while (false)\n"
17579                "}\n",
17580                AllmanBraceStyle);
17581 
17582   verifyFormat("void f(int a)\n"
17583                "{\n"
17584                "  switch (a)\n"
17585                "  {\n"
17586                "  case 0:\n"
17587                "    break;\n"
17588                "  case 1:\n"
17589                "  {\n"
17590                "    break;\n"
17591                "  }\n"
17592                "  case 2:\n"
17593                "  {\n"
17594                "  }\n"
17595                "  break;\n"
17596                "  default:\n"
17597                "    break;\n"
17598                "  }\n"
17599                "}\n",
17600                AllmanBraceStyle);
17601 
17602   verifyFormat("enum X\n"
17603                "{\n"
17604                "  Y = 0,\n"
17605                "}\n",
17606                AllmanBraceStyle);
17607   verifyFormat("enum X\n"
17608                "{\n"
17609                "  Y = 0\n"
17610                "}\n",
17611                AllmanBraceStyle);
17612 
17613   verifyFormat("@interface BSApplicationController ()\n"
17614                "{\n"
17615                "@private\n"
17616                "  id _extraIvar;\n"
17617                "}\n"
17618                "@end\n",
17619                AllmanBraceStyle);
17620 
17621   verifyFormat("#ifdef _DEBUG\n"
17622                "int foo(int i = 0)\n"
17623                "#else\n"
17624                "int foo(int i = 5)\n"
17625                "#endif\n"
17626                "{\n"
17627                "  return i;\n"
17628                "}",
17629                AllmanBraceStyle);
17630 
17631   verifyFormat("void foo() {}\n"
17632                "void bar()\n"
17633                "#ifdef _DEBUG\n"
17634                "{\n"
17635                "  foo();\n"
17636                "}\n"
17637                "#else\n"
17638                "{\n"
17639                "}\n"
17640                "#endif",
17641                AllmanBraceStyle);
17642 
17643   verifyFormat("void foobar() { int i = 5; }\n"
17644                "#ifdef _DEBUG\n"
17645                "void bar() {}\n"
17646                "#else\n"
17647                "void bar() { foobar(); }\n"
17648                "#endif",
17649                AllmanBraceStyle);
17650 
17651   EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine,
17652             FormatStyle::SLS_All);
17653 
17654   verifyFormat("[](int i) { return i + 2; };\n"
17655                "[](int i, int j)\n"
17656                "{\n"
17657                "  auto x = i + j;\n"
17658                "  auto y = i * j;\n"
17659                "  return x ^ y;\n"
17660                "};\n"
17661                "void foo()\n"
17662                "{\n"
17663                "  auto shortLambda = [](int i) { return i + 2; };\n"
17664                "  auto longLambda = [](int i, int j)\n"
17665                "  {\n"
17666                "    auto x = i + j;\n"
17667                "    auto y = i * j;\n"
17668                "    return x ^ y;\n"
17669                "  };\n"
17670                "}",
17671                AllmanBraceStyle);
17672 
17673   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
17674 
17675   verifyFormat("[](int i)\n"
17676                "{\n"
17677                "  return i + 2;\n"
17678                "};\n"
17679                "[](int i, int j)\n"
17680                "{\n"
17681                "  auto x = i + j;\n"
17682                "  auto y = i * j;\n"
17683                "  return x ^ y;\n"
17684                "};\n"
17685                "void foo()\n"
17686                "{\n"
17687                "  auto shortLambda = [](int i)\n"
17688                "  {\n"
17689                "    return i + 2;\n"
17690                "  };\n"
17691                "  auto longLambda = [](int i, int j)\n"
17692                "  {\n"
17693                "    auto x = i + j;\n"
17694                "    auto y = i * j;\n"
17695                "    return x ^ y;\n"
17696                "  };\n"
17697                "}",
17698                AllmanBraceStyle);
17699 
17700   // Reset
17701   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
17702 
17703   // This shouldn't affect ObjC blocks..
17704   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
17705                "  // ...\n"
17706                "  int i;\n"
17707                "}];",
17708                AllmanBraceStyle);
17709   verifyFormat("void (^block)(void) = ^{\n"
17710                "  // ...\n"
17711                "  int i;\n"
17712                "};",
17713                AllmanBraceStyle);
17714   // .. or dict literals.
17715   verifyFormat("void f()\n"
17716                "{\n"
17717                "  // ...\n"
17718                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
17719                "}",
17720                AllmanBraceStyle);
17721   verifyFormat("void f()\n"
17722                "{\n"
17723                "  // ...\n"
17724                "  [object someMethod:@{a : @\"b\"}];\n"
17725                "}",
17726                AllmanBraceStyle);
17727   verifyFormat("int f()\n"
17728                "{ // comment\n"
17729                "  return 42;\n"
17730                "}",
17731                AllmanBraceStyle);
17732 
17733   AllmanBraceStyle.ColumnLimit = 19;
17734   verifyFormat("void f() { int i; }", AllmanBraceStyle);
17735   AllmanBraceStyle.ColumnLimit = 18;
17736   verifyFormat("void f()\n"
17737                "{\n"
17738                "  int i;\n"
17739                "}",
17740                AllmanBraceStyle);
17741   AllmanBraceStyle.ColumnLimit = 80;
17742 
17743   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
17744   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
17745       FormatStyle::SIS_WithoutElse;
17746   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
17747   verifyFormat("void f(bool b)\n"
17748                "{\n"
17749                "  if (b)\n"
17750                "  {\n"
17751                "    return;\n"
17752                "  }\n"
17753                "}\n",
17754                BreakBeforeBraceShortIfs);
17755   verifyFormat("void f(bool b)\n"
17756                "{\n"
17757                "  if constexpr (b)\n"
17758                "  {\n"
17759                "    return;\n"
17760                "  }\n"
17761                "}\n",
17762                BreakBeforeBraceShortIfs);
17763   verifyFormat("void f(bool b)\n"
17764                "{\n"
17765                "  if CONSTEXPR (b)\n"
17766                "  {\n"
17767                "    return;\n"
17768                "  }\n"
17769                "}\n",
17770                BreakBeforeBraceShortIfs);
17771   verifyFormat("void f(bool b)\n"
17772                "{\n"
17773                "  if (b) return;\n"
17774                "}\n",
17775                BreakBeforeBraceShortIfs);
17776   verifyFormat("void f(bool b)\n"
17777                "{\n"
17778                "  if constexpr (b) return;\n"
17779                "}\n",
17780                BreakBeforeBraceShortIfs);
17781   verifyFormat("void f(bool b)\n"
17782                "{\n"
17783                "  if CONSTEXPR (b) return;\n"
17784                "}\n",
17785                BreakBeforeBraceShortIfs);
17786   verifyFormat("void f(bool b)\n"
17787                "{\n"
17788                "  while (b)\n"
17789                "  {\n"
17790                "    return;\n"
17791                "  }\n"
17792                "}\n",
17793                BreakBeforeBraceShortIfs);
17794 }
17795 
17796 TEST_F(FormatTest, WhitesmithsBraceBreaking) {
17797   FormatStyle WhitesmithsBraceStyle = getLLVMStyleWithColumns(0);
17798   WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
17799 
17800   // Make a few changes to the style for testing purposes
17801   WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
17802       FormatStyle::SFS_Empty;
17803   WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
17804 
17805   // FIXME: this test case can't decide whether there should be a blank line
17806   // after the ~D() line or not. It adds one if one doesn't exist in the test
17807   // and it removes the line if one exists.
17808   /*
17809   verifyFormat("class A;\n"
17810                "namespace B\n"
17811                "  {\n"
17812                "class C;\n"
17813                "// Comment\n"
17814                "class D\n"
17815                "  {\n"
17816                "public:\n"
17817                "  D();\n"
17818                "  ~D() {}\n"
17819                "private:\n"
17820                "  enum E\n"
17821                "    {\n"
17822                "    F\n"
17823                "    }\n"
17824                "  };\n"
17825                "  } // namespace B\n",
17826                WhitesmithsBraceStyle);
17827   */
17828 
17829   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None;
17830   verifyFormat("namespace a\n"
17831                "  {\n"
17832                "class A\n"
17833                "  {\n"
17834                "  void f()\n"
17835                "    {\n"
17836                "    if (true)\n"
17837                "      {\n"
17838                "      a();\n"
17839                "      b();\n"
17840                "      }\n"
17841                "    }\n"
17842                "  void g()\n"
17843                "    {\n"
17844                "    return;\n"
17845                "    }\n"
17846                "  };\n"
17847                "struct B\n"
17848                "  {\n"
17849                "  int x;\n"
17850                "  };\n"
17851                "  } // namespace a",
17852                WhitesmithsBraceStyle);
17853 
17854   verifyFormat("namespace a\n"
17855                "  {\n"
17856                "namespace b\n"
17857                "  {\n"
17858                "class A\n"
17859                "  {\n"
17860                "  void f()\n"
17861                "    {\n"
17862                "    if (true)\n"
17863                "      {\n"
17864                "      a();\n"
17865                "      b();\n"
17866                "      }\n"
17867                "    }\n"
17868                "  void g()\n"
17869                "    {\n"
17870                "    return;\n"
17871                "    }\n"
17872                "  };\n"
17873                "struct B\n"
17874                "  {\n"
17875                "  int x;\n"
17876                "  };\n"
17877                "  } // namespace b\n"
17878                "  } // namespace a",
17879                WhitesmithsBraceStyle);
17880 
17881   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner;
17882   verifyFormat("namespace a\n"
17883                "  {\n"
17884                "namespace b\n"
17885                "  {\n"
17886                "  class A\n"
17887                "    {\n"
17888                "    void f()\n"
17889                "      {\n"
17890                "      if (true)\n"
17891                "        {\n"
17892                "        a();\n"
17893                "        b();\n"
17894                "        }\n"
17895                "      }\n"
17896                "    void g()\n"
17897                "      {\n"
17898                "      return;\n"
17899                "      }\n"
17900                "    };\n"
17901                "  struct B\n"
17902                "    {\n"
17903                "    int x;\n"
17904                "    };\n"
17905                "  } // namespace b\n"
17906                "  } // namespace a",
17907                WhitesmithsBraceStyle);
17908 
17909   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All;
17910   verifyFormat("namespace a\n"
17911                "  {\n"
17912                "  namespace b\n"
17913                "    {\n"
17914                "    class A\n"
17915                "      {\n"
17916                "      void f()\n"
17917                "        {\n"
17918                "        if (true)\n"
17919                "          {\n"
17920                "          a();\n"
17921                "          b();\n"
17922                "          }\n"
17923                "        }\n"
17924                "      void g()\n"
17925                "        {\n"
17926                "        return;\n"
17927                "        }\n"
17928                "      };\n"
17929                "    struct B\n"
17930                "      {\n"
17931                "      int x;\n"
17932                "      };\n"
17933                "    } // namespace b\n"
17934                "  }   // namespace a",
17935                WhitesmithsBraceStyle);
17936 
17937   verifyFormat("void f()\n"
17938                "  {\n"
17939                "  if (true)\n"
17940                "    {\n"
17941                "    a();\n"
17942                "    }\n"
17943                "  else if (false)\n"
17944                "    {\n"
17945                "    b();\n"
17946                "    }\n"
17947                "  else\n"
17948                "    {\n"
17949                "    c();\n"
17950                "    }\n"
17951                "  }\n",
17952                WhitesmithsBraceStyle);
17953 
17954   verifyFormat("void f()\n"
17955                "  {\n"
17956                "  for (int i = 0; i < 10; ++i)\n"
17957                "    {\n"
17958                "    a();\n"
17959                "    }\n"
17960                "  while (false)\n"
17961                "    {\n"
17962                "    b();\n"
17963                "    }\n"
17964                "  do\n"
17965                "    {\n"
17966                "    c();\n"
17967                "    } while (false)\n"
17968                "  }\n",
17969                WhitesmithsBraceStyle);
17970 
17971   WhitesmithsBraceStyle.IndentCaseLabels = true;
17972   verifyFormat("void switchTest1(int a)\n"
17973                "  {\n"
17974                "  switch (a)\n"
17975                "    {\n"
17976                "    case 2:\n"
17977                "      {\n"
17978                "      }\n"
17979                "      break;\n"
17980                "    }\n"
17981                "  }\n",
17982                WhitesmithsBraceStyle);
17983 
17984   verifyFormat("void switchTest2(int a)\n"
17985                "  {\n"
17986                "  switch (a)\n"
17987                "    {\n"
17988                "    case 0:\n"
17989                "      break;\n"
17990                "    case 1:\n"
17991                "      {\n"
17992                "      break;\n"
17993                "      }\n"
17994                "    case 2:\n"
17995                "      {\n"
17996                "      }\n"
17997                "      break;\n"
17998                "    default:\n"
17999                "      break;\n"
18000                "    }\n"
18001                "  }\n",
18002                WhitesmithsBraceStyle);
18003 
18004   verifyFormat("void switchTest3(int a)\n"
18005                "  {\n"
18006                "  switch (a)\n"
18007                "    {\n"
18008                "    case 0:\n"
18009                "      {\n"
18010                "      foo(x);\n"
18011                "      }\n"
18012                "      break;\n"
18013                "    default:\n"
18014                "      {\n"
18015                "      foo(1);\n"
18016                "      }\n"
18017                "      break;\n"
18018                "    }\n"
18019                "  }\n",
18020                WhitesmithsBraceStyle);
18021 
18022   WhitesmithsBraceStyle.IndentCaseLabels = false;
18023 
18024   verifyFormat("void switchTest4(int a)\n"
18025                "  {\n"
18026                "  switch (a)\n"
18027                "    {\n"
18028                "  case 2:\n"
18029                "    {\n"
18030                "    }\n"
18031                "    break;\n"
18032                "    }\n"
18033                "  }\n",
18034                WhitesmithsBraceStyle);
18035 
18036   verifyFormat("void switchTest5(int a)\n"
18037                "  {\n"
18038                "  switch (a)\n"
18039                "    {\n"
18040                "  case 0:\n"
18041                "    break;\n"
18042                "  case 1:\n"
18043                "    {\n"
18044                "    foo();\n"
18045                "    break;\n"
18046                "    }\n"
18047                "  case 2:\n"
18048                "    {\n"
18049                "    }\n"
18050                "    break;\n"
18051                "  default:\n"
18052                "    break;\n"
18053                "    }\n"
18054                "  }\n",
18055                WhitesmithsBraceStyle);
18056 
18057   verifyFormat("void switchTest6(int a)\n"
18058                "  {\n"
18059                "  switch (a)\n"
18060                "    {\n"
18061                "  case 0:\n"
18062                "    {\n"
18063                "    foo(x);\n"
18064                "    }\n"
18065                "    break;\n"
18066                "  default:\n"
18067                "    {\n"
18068                "    foo(1);\n"
18069                "    }\n"
18070                "    break;\n"
18071                "    }\n"
18072                "  }\n",
18073                WhitesmithsBraceStyle);
18074 
18075   verifyFormat("enum X\n"
18076                "  {\n"
18077                "  Y = 0, // testing\n"
18078                "  }\n",
18079                WhitesmithsBraceStyle);
18080 
18081   verifyFormat("enum X\n"
18082                "  {\n"
18083                "  Y = 0\n"
18084                "  }\n",
18085                WhitesmithsBraceStyle);
18086   verifyFormat("enum X\n"
18087                "  {\n"
18088                "  Y = 0,\n"
18089                "  Z = 1\n"
18090                "  };\n",
18091                WhitesmithsBraceStyle);
18092 
18093   verifyFormat("@interface BSApplicationController ()\n"
18094                "  {\n"
18095                "@private\n"
18096                "  id _extraIvar;\n"
18097                "  }\n"
18098                "@end\n",
18099                WhitesmithsBraceStyle);
18100 
18101   verifyFormat("#ifdef _DEBUG\n"
18102                "int foo(int i = 0)\n"
18103                "#else\n"
18104                "int foo(int i = 5)\n"
18105                "#endif\n"
18106                "  {\n"
18107                "  return i;\n"
18108                "  }",
18109                WhitesmithsBraceStyle);
18110 
18111   verifyFormat("void foo() {}\n"
18112                "void bar()\n"
18113                "#ifdef _DEBUG\n"
18114                "  {\n"
18115                "  foo();\n"
18116                "  }\n"
18117                "#else\n"
18118                "  {\n"
18119                "  }\n"
18120                "#endif",
18121                WhitesmithsBraceStyle);
18122 
18123   verifyFormat("void foobar()\n"
18124                "  {\n"
18125                "  int i = 5;\n"
18126                "  }\n"
18127                "#ifdef _DEBUG\n"
18128                "void bar()\n"
18129                "  {\n"
18130                "  }\n"
18131                "#else\n"
18132                "void bar()\n"
18133                "  {\n"
18134                "  foobar();\n"
18135                "  }\n"
18136                "#endif",
18137                WhitesmithsBraceStyle);
18138 
18139   // This shouldn't affect ObjC blocks..
18140   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
18141                "  // ...\n"
18142                "  int i;\n"
18143                "}];",
18144                WhitesmithsBraceStyle);
18145   verifyFormat("void (^block)(void) = ^{\n"
18146                "  // ...\n"
18147                "  int i;\n"
18148                "};",
18149                WhitesmithsBraceStyle);
18150   // .. or dict literals.
18151   verifyFormat("void f()\n"
18152                "  {\n"
18153                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
18154                "  }",
18155                WhitesmithsBraceStyle);
18156 
18157   verifyFormat("int f()\n"
18158                "  { // comment\n"
18159                "  return 42;\n"
18160                "  }",
18161                WhitesmithsBraceStyle);
18162 
18163   FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
18164   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
18165       FormatStyle::SIS_OnlyFirstIf;
18166   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
18167   verifyFormat("void f(bool b)\n"
18168                "  {\n"
18169                "  if (b)\n"
18170                "    {\n"
18171                "    return;\n"
18172                "    }\n"
18173                "  }\n",
18174                BreakBeforeBraceShortIfs);
18175   verifyFormat("void f(bool b)\n"
18176                "  {\n"
18177                "  if (b) return;\n"
18178                "  }\n",
18179                BreakBeforeBraceShortIfs);
18180   verifyFormat("void f(bool b)\n"
18181                "  {\n"
18182                "  while (b)\n"
18183                "    {\n"
18184                "    return;\n"
18185                "    }\n"
18186                "  }\n",
18187                BreakBeforeBraceShortIfs);
18188 }
18189 
18190 TEST_F(FormatTest, GNUBraceBreaking) {
18191   FormatStyle GNUBraceStyle = getLLVMStyle();
18192   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
18193   verifyFormat("namespace a\n"
18194                "{\n"
18195                "class A\n"
18196                "{\n"
18197                "  void f()\n"
18198                "  {\n"
18199                "    int a;\n"
18200                "    {\n"
18201                "      int b;\n"
18202                "    }\n"
18203                "    if (true)\n"
18204                "      {\n"
18205                "        a();\n"
18206                "        b();\n"
18207                "      }\n"
18208                "  }\n"
18209                "  void g() { return; }\n"
18210                "}\n"
18211                "} // namespace a",
18212                GNUBraceStyle);
18213 
18214   verifyFormat("void f()\n"
18215                "{\n"
18216                "  if (true)\n"
18217                "    {\n"
18218                "      a();\n"
18219                "    }\n"
18220                "  else if (false)\n"
18221                "    {\n"
18222                "      b();\n"
18223                "    }\n"
18224                "  else\n"
18225                "    {\n"
18226                "      c();\n"
18227                "    }\n"
18228                "}\n",
18229                GNUBraceStyle);
18230 
18231   verifyFormat("void f()\n"
18232                "{\n"
18233                "  for (int i = 0; i < 10; ++i)\n"
18234                "    {\n"
18235                "      a();\n"
18236                "    }\n"
18237                "  while (false)\n"
18238                "    {\n"
18239                "      b();\n"
18240                "    }\n"
18241                "  do\n"
18242                "    {\n"
18243                "      c();\n"
18244                "    }\n"
18245                "  while (false);\n"
18246                "}\n",
18247                GNUBraceStyle);
18248 
18249   verifyFormat("void f(int a)\n"
18250                "{\n"
18251                "  switch (a)\n"
18252                "    {\n"
18253                "    case 0:\n"
18254                "      break;\n"
18255                "    case 1:\n"
18256                "      {\n"
18257                "        break;\n"
18258                "      }\n"
18259                "    case 2:\n"
18260                "      {\n"
18261                "      }\n"
18262                "      break;\n"
18263                "    default:\n"
18264                "      break;\n"
18265                "    }\n"
18266                "}\n",
18267                GNUBraceStyle);
18268 
18269   verifyFormat("enum X\n"
18270                "{\n"
18271                "  Y = 0,\n"
18272                "}\n",
18273                GNUBraceStyle);
18274 
18275   verifyFormat("@interface BSApplicationController ()\n"
18276                "{\n"
18277                "@private\n"
18278                "  id _extraIvar;\n"
18279                "}\n"
18280                "@end\n",
18281                GNUBraceStyle);
18282 
18283   verifyFormat("#ifdef _DEBUG\n"
18284                "int foo(int i = 0)\n"
18285                "#else\n"
18286                "int foo(int i = 5)\n"
18287                "#endif\n"
18288                "{\n"
18289                "  return i;\n"
18290                "}",
18291                GNUBraceStyle);
18292 
18293   verifyFormat("void foo() {}\n"
18294                "void bar()\n"
18295                "#ifdef _DEBUG\n"
18296                "{\n"
18297                "  foo();\n"
18298                "}\n"
18299                "#else\n"
18300                "{\n"
18301                "}\n"
18302                "#endif",
18303                GNUBraceStyle);
18304 
18305   verifyFormat("void foobar() { int i = 5; }\n"
18306                "#ifdef _DEBUG\n"
18307                "void bar() {}\n"
18308                "#else\n"
18309                "void bar() { foobar(); }\n"
18310                "#endif",
18311                GNUBraceStyle);
18312 }
18313 
18314 TEST_F(FormatTest, WebKitBraceBreaking) {
18315   FormatStyle WebKitBraceStyle = getLLVMStyle();
18316   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
18317   WebKitBraceStyle.FixNamespaceComments = false;
18318   verifyFormat("namespace a {\n"
18319                "class A {\n"
18320                "  void f()\n"
18321                "  {\n"
18322                "    if (true) {\n"
18323                "      a();\n"
18324                "      b();\n"
18325                "    }\n"
18326                "  }\n"
18327                "  void g() { return; }\n"
18328                "};\n"
18329                "enum E {\n"
18330                "  A,\n"
18331                "  // foo\n"
18332                "  B,\n"
18333                "  C\n"
18334                "};\n"
18335                "struct B {\n"
18336                "  int x;\n"
18337                "};\n"
18338                "}\n",
18339                WebKitBraceStyle);
18340   verifyFormat("struct S {\n"
18341                "  int Type;\n"
18342                "  union {\n"
18343                "    int x;\n"
18344                "    double y;\n"
18345                "  } Value;\n"
18346                "  class C {\n"
18347                "    MyFavoriteType Value;\n"
18348                "  } Class;\n"
18349                "};\n",
18350                WebKitBraceStyle);
18351 }
18352 
18353 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
18354   verifyFormat("void f() {\n"
18355                "  try {\n"
18356                "  } catch (const Exception &e) {\n"
18357                "  }\n"
18358                "}\n",
18359                getLLVMStyle());
18360 }
18361 
18362 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
18363   auto Style = getLLVMStyle();
18364   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
18365   Style.AlignConsecutiveAssignments =
18366       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18367   Style.AlignConsecutiveDeclarations =
18368       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18369   verifyFormat("struct test demo[] = {\n"
18370                "    {56,    23, \"hello\"},\n"
18371                "    {-1, 93463, \"world\"},\n"
18372                "    { 7,     5,    \"!!\"}\n"
18373                "};\n",
18374                Style);
18375 
18376   verifyFormat("struct test demo[] = {\n"
18377                "    {56,    23, \"hello\"}, // first line\n"
18378                "    {-1, 93463, \"world\"}, // second line\n"
18379                "    { 7,     5,    \"!!\"}  // third line\n"
18380                "};\n",
18381                Style);
18382 
18383   verifyFormat("struct test demo[4] = {\n"
18384                "    { 56,    23, 21,       \"oh\"}, // first line\n"
18385                "    { -1, 93463, 22,       \"my\"}, // second line\n"
18386                "    {  7,     5,  1, \"goodness\"}  // third line\n"
18387                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
18388                "};\n",
18389                Style);
18390 
18391   verifyFormat("struct test demo[3] = {\n"
18392                "    {56,    23, \"hello\"},\n"
18393                "    {-1, 93463, \"world\"},\n"
18394                "    { 7,     5,    \"!!\"}\n"
18395                "};\n",
18396                Style);
18397 
18398   verifyFormat("struct test demo[3] = {\n"
18399                "    {int{56},    23, \"hello\"},\n"
18400                "    {int{-1}, 93463, \"world\"},\n"
18401                "    { int{7},     5,    \"!!\"}\n"
18402                "};\n",
18403                Style);
18404 
18405   verifyFormat("struct test demo[] = {\n"
18406                "    {56,    23, \"hello\"},\n"
18407                "    {-1, 93463, \"world\"},\n"
18408                "    { 7,     5,    \"!!\"},\n"
18409                "};\n",
18410                Style);
18411 
18412   verifyFormat("test demo[] = {\n"
18413                "    {56,    23, \"hello\"},\n"
18414                "    {-1, 93463, \"world\"},\n"
18415                "    { 7,     5,    \"!!\"},\n"
18416                "};\n",
18417                Style);
18418 
18419   verifyFormat("demo = std::array<struct test, 3>{\n"
18420                "    test{56,    23, \"hello\"},\n"
18421                "    test{-1, 93463, \"world\"},\n"
18422                "    test{ 7,     5,    \"!!\"},\n"
18423                "};\n",
18424                Style);
18425 
18426   verifyFormat("test demo[] = {\n"
18427                "    {56,    23, \"hello\"},\n"
18428                "#if X\n"
18429                "    {-1, 93463, \"world\"},\n"
18430                "#endif\n"
18431                "    { 7,     5,    \"!!\"}\n"
18432                "};\n",
18433                Style);
18434 
18435   verifyFormat(
18436       "test demo[] = {\n"
18437       "    { 7,    23,\n"
18438       "     \"hello world i am a very long line that really, in any\"\n"
18439       "     \"just world, ought to be split over multiple lines\"},\n"
18440       "    {-1, 93463,                                  \"world\"},\n"
18441       "    {56,     5,                                     \"!!\"}\n"
18442       "};\n",
18443       Style);
18444 
18445   verifyFormat("return GradForUnaryCwise(g, {\n"
18446                "                                {{\"sign\"}, \"Sign\",  "
18447                "  {\"x\", \"dy\"}},\n"
18448                "                                {  {\"dx\"},  \"Mul\", {\"dy\""
18449                ", \"sign\"}},\n"
18450                "});\n",
18451                Style);
18452 
18453   Style.ColumnLimit = 0;
18454   EXPECT_EQ(
18455       "test demo[] = {\n"
18456       "    {56,    23, \"hello world i am a very long line that really, "
18457       "in any just world, ought to be split over multiple lines\"},\n"
18458       "    {-1, 93463,                                                  "
18459       "                                                 \"world\"},\n"
18460       "    { 7,     5,                                                  "
18461       "                                                    \"!!\"},\n"
18462       "};",
18463       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18464              "that really, in any just world, ought to be split over multiple "
18465              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18466              Style));
18467 
18468   Style.ColumnLimit = 80;
18469   verifyFormat("test demo[] = {\n"
18470                "    {56,    23, /* a comment */ \"hello\"},\n"
18471                "    {-1, 93463,                 \"world\"},\n"
18472                "    { 7,     5,                    \"!!\"}\n"
18473                "};\n",
18474                Style);
18475 
18476   verifyFormat("test demo[] = {\n"
18477                "    {56,    23,                    \"hello\"},\n"
18478                "    {-1, 93463, \"world\" /* comment here */},\n"
18479                "    { 7,     5,                       \"!!\"}\n"
18480                "};\n",
18481                Style);
18482 
18483   verifyFormat("test demo[] = {\n"
18484                "    {56, /* a comment */ 23, \"hello\"},\n"
18485                "    {-1,              93463, \"world\"},\n"
18486                "    { 7,                  5,    \"!!\"}\n"
18487                "};\n",
18488                Style);
18489 
18490   Style.ColumnLimit = 20;
18491   EXPECT_EQ(
18492       "demo = std::array<\n"
18493       "    struct test, 3>{\n"
18494       "    test{\n"
18495       "         56,    23,\n"
18496       "         \"hello \"\n"
18497       "         \"world i \"\n"
18498       "         \"am a very \"\n"
18499       "         \"long line \"\n"
18500       "         \"that \"\n"
18501       "         \"really, \"\n"
18502       "         \"in any \"\n"
18503       "         \"just \"\n"
18504       "         \"world, \"\n"
18505       "         \"ought to \"\n"
18506       "         \"be split \"\n"
18507       "         \"over \"\n"
18508       "         \"multiple \"\n"
18509       "         \"lines\"},\n"
18510       "    test{-1, 93463,\n"
18511       "         \"world\"},\n"
18512       "    test{ 7,     5,\n"
18513       "         \"!!\"   },\n"
18514       "};",
18515       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
18516              "i am a very long line that really, in any just world, ought "
18517              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
18518              "test{7, 5, \"!!\"},};",
18519              Style));
18520   // This caused a core dump by enabling Alignment in the LLVMStyle globally
18521   Style = getLLVMStyleWithColumns(50);
18522   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
18523   verifyFormat("static A x = {\n"
18524                "    {{init1, init2, init3, init4},\n"
18525                "     {init1, init2, init3, init4}}\n"
18526                "};",
18527                Style);
18528   Style.ColumnLimit = 100;
18529   EXPECT_EQ(
18530       "test demo[] = {\n"
18531       "    {56,    23,\n"
18532       "     \"hello world i am a very long line that really, in any just world"
18533       ", ought to be split over \"\n"
18534       "     \"multiple lines\"  },\n"
18535       "    {-1, 93463, \"world\"},\n"
18536       "    { 7,     5,    \"!!\"},\n"
18537       "};",
18538       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18539              "that really, in any just world, ought to be split over multiple "
18540              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18541              Style));
18542 
18543   Style = getLLVMStyleWithColumns(50);
18544   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
18545   Style.AlignConsecutiveAssignments =
18546       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18547   Style.AlignConsecutiveDeclarations =
18548       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
18549   verifyFormat("struct test demo[] = {\n"
18550                "    {56,    23, \"hello\"},\n"
18551                "    {-1, 93463, \"world\"},\n"
18552                "    { 7,     5,    \"!!\"}\n"
18553                "};\n"
18554                "static A x = {\n"
18555                "    {{init1, init2, init3, init4},\n"
18556                "     {init1, init2, init3, init4}}\n"
18557                "};",
18558                Style);
18559   Style.ColumnLimit = 100;
18560   Style.AlignConsecutiveAssignments =
18561       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
18562   Style.AlignConsecutiveDeclarations =
18563       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
18564   verifyFormat("struct test demo[] = {\n"
18565                "    {56,    23, \"hello\"},\n"
18566                "    {-1, 93463, \"world\"},\n"
18567                "    { 7,     5,    \"!!\"}\n"
18568                "};\n"
18569                "struct test demo[4] = {\n"
18570                "    { 56,    23, 21,       \"oh\"}, // first line\n"
18571                "    { -1, 93463, 22,       \"my\"}, // second line\n"
18572                "    {  7,     5,  1, \"goodness\"}  // third line\n"
18573                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
18574                "};\n",
18575                Style);
18576   EXPECT_EQ(
18577       "test demo[] = {\n"
18578       "    {56,\n"
18579       "     \"hello world i am a very long line that really, in any just world"
18580       ", ought to be split over \"\n"
18581       "     \"multiple lines\",    23},\n"
18582       "    {-1,      \"world\", 93463},\n"
18583       "    { 7,         \"!!\",     5},\n"
18584       "};",
18585       format("test demo[] = {{56, \"hello world i am a very long line "
18586              "that really, in any just world, ought to be split over multiple "
18587              "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
18588              Style));
18589 }
18590 
18591 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
18592   auto Style = getLLVMStyle();
18593   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
18594   /* FIXME: This case gets misformatted.
18595   verifyFormat("auto foo = Items{\n"
18596                "    Section{0, bar(), },\n"
18597                "    Section{1, boo()  }\n"
18598                "};\n",
18599                Style);
18600   */
18601   verifyFormat("auto foo = Items{\n"
18602                "    Section{\n"
18603                "            0, bar(),\n"
18604                "            }\n"
18605                "};\n",
18606                Style);
18607   verifyFormat("struct test demo[] = {\n"
18608                "    {56, 23,    \"hello\"},\n"
18609                "    {-1, 93463, \"world\"},\n"
18610                "    {7,  5,     \"!!\"   }\n"
18611                "};\n",
18612                Style);
18613   verifyFormat("struct test demo[] = {\n"
18614                "    {56, 23,    \"hello\"}, // first line\n"
18615                "    {-1, 93463, \"world\"}, // second line\n"
18616                "    {7,  5,     \"!!\"   }  // third line\n"
18617                "};\n",
18618                Style);
18619   verifyFormat("struct test demo[4] = {\n"
18620                "    {56,  23,    21, \"oh\"      }, // first line\n"
18621                "    {-1,  93463, 22, \"my\"      }, // second line\n"
18622                "    {7,   5,     1,  \"goodness\"}  // third line\n"
18623                "    {234, 5,     1,  \"gracious\"}  // fourth line\n"
18624                "};\n",
18625                Style);
18626   verifyFormat("struct test demo[3] = {\n"
18627                "    {56, 23,    \"hello\"},\n"
18628                "    {-1, 93463, \"world\"},\n"
18629                "    {7,  5,     \"!!\"   }\n"
18630                "};\n",
18631                Style);
18632 
18633   verifyFormat("struct test demo[3] = {\n"
18634                "    {int{56}, 23,    \"hello\"},\n"
18635                "    {int{-1}, 93463, \"world\"},\n"
18636                "    {int{7},  5,     \"!!\"   }\n"
18637                "};\n",
18638                Style);
18639   verifyFormat("struct test demo[] = {\n"
18640                "    {56, 23,    \"hello\"},\n"
18641                "    {-1, 93463, \"world\"},\n"
18642                "    {7,  5,     \"!!\"   },\n"
18643                "};\n",
18644                Style);
18645   verifyFormat("test demo[] = {\n"
18646                "    {56, 23,    \"hello\"},\n"
18647                "    {-1, 93463, \"world\"},\n"
18648                "    {7,  5,     \"!!\"   },\n"
18649                "};\n",
18650                Style);
18651   verifyFormat("demo = std::array<struct test, 3>{\n"
18652                "    test{56, 23,    \"hello\"},\n"
18653                "    test{-1, 93463, \"world\"},\n"
18654                "    test{7,  5,     \"!!\"   },\n"
18655                "};\n",
18656                Style);
18657   verifyFormat("test demo[] = {\n"
18658                "    {56, 23,    \"hello\"},\n"
18659                "#if X\n"
18660                "    {-1, 93463, \"world\"},\n"
18661                "#endif\n"
18662                "    {7,  5,     \"!!\"   }\n"
18663                "};\n",
18664                Style);
18665   verifyFormat(
18666       "test demo[] = {\n"
18667       "    {7,  23,\n"
18668       "     \"hello world i am a very long line that really, in any\"\n"
18669       "     \"just world, ought to be split over multiple lines\"},\n"
18670       "    {-1, 93463, \"world\"                                 },\n"
18671       "    {56, 5,     \"!!\"                                    }\n"
18672       "};\n",
18673       Style);
18674 
18675   verifyFormat("return GradForUnaryCwise(g, {\n"
18676                "                                {{\"sign\"}, \"Sign\", {\"x\", "
18677                "\"dy\"}   },\n"
18678                "                                {{\"dx\"},   \"Mul\",  "
18679                "{\"dy\", \"sign\"}},\n"
18680                "});\n",
18681                Style);
18682 
18683   Style.ColumnLimit = 0;
18684   EXPECT_EQ(
18685       "test demo[] = {\n"
18686       "    {56, 23,    \"hello world i am a very long line that really, in any "
18687       "just world, ought to be split over multiple lines\"},\n"
18688       "    {-1, 93463, \"world\"                                               "
18689       "                                                   },\n"
18690       "    {7,  5,     \"!!\"                                                  "
18691       "                                                   },\n"
18692       "};",
18693       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18694              "that really, in any just world, ought to be split over multiple "
18695              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18696              Style));
18697 
18698   Style.ColumnLimit = 80;
18699   verifyFormat("test demo[] = {\n"
18700                "    {56, 23,    /* a comment */ \"hello\"},\n"
18701                "    {-1, 93463, \"world\"                },\n"
18702                "    {7,  5,     \"!!\"                   }\n"
18703                "};\n",
18704                Style);
18705 
18706   verifyFormat("test demo[] = {\n"
18707                "    {56, 23,    \"hello\"                   },\n"
18708                "    {-1, 93463, \"world\" /* comment here */},\n"
18709                "    {7,  5,     \"!!\"                      }\n"
18710                "};\n",
18711                Style);
18712 
18713   verifyFormat("test demo[] = {\n"
18714                "    {56, /* a comment */ 23, \"hello\"},\n"
18715                "    {-1, 93463,              \"world\"},\n"
18716                "    {7,  5,                  \"!!\"   }\n"
18717                "};\n",
18718                Style);
18719 
18720   Style.ColumnLimit = 20;
18721   EXPECT_EQ(
18722       "demo = std::array<\n"
18723       "    struct test, 3>{\n"
18724       "    test{\n"
18725       "         56, 23,\n"
18726       "         \"hello \"\n"
18727       "         \"world i \"\n"
18728       "         \"am a very \"\n"
18729       "         \"long line \"\n"
18730       "         \"that \"\n"
18731       "         \"really, \"\n"
18732       "         \"in any \"\n"
18733       "         \"just \"\n"
18734       "         \"world, \"\n"
18735       "         \"ought to \"\n"
18736       "         \"be split \"\n"
18737       "         \"over \"\n"
18738       "         \"multiple \"\n"
18739       "         \"lines\"},\n"
18740       "    test{-1, 93463,\n"
18741       "         \"world\"},\n"
18742       "    test{7,  5,\n"
18743       "         \"!!\"   },\n"
18744       "};",
18745       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
18746              "i am a very long line that really, in any just world, ought "
18747              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
18748              "test{7, 5, \"!!\"},};",
18749              Style));
18750 
18751   // This caused a core dump by enabling Alignment in the LLVMStyle globally
18752   Style = getLLVMStyleWithColumns(50);
18753   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
18754   verifyFormat("static A x = {\n"
18755                "    {{init1, init2, init3, init4},\n"
18756                "     {init1, init2, init3, init4}}\n"
18757                "};",
18758                Style);
18759   Style.ColumnLimit = 100;
18760   EXPECT_EQ(
18761       "test demo[] = {\n"
18762       "    {56, 23,\n"
18763       "     \"hello world i am a very long line that really, in any just world"
18764       ", ought to be split over \"\n"
18765       "     \"multiple lines\"  },\n"
18766       "    {-1, 93463, \"world\"},\n"
18767       "    {7,  5,     \"!!\"   },\n"
18768       "};",
18769       format("test demo[] = {{56, 23, \"hello world i am a very long line "
18770              "that really, in any just world, ought to be split over multiple "
18771              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
18772              Style));
18773 }
18774 
18775 TEST_F(FormatTest, UnderstandsPragmas) {
18776   verifyFormat("#pragma omp reduction(| : var)");
18777   verifyFormat("#pragma omp reduction(+ : var)");
18778 
18779   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
18780             "(including parentheses).",
18781             format("#pragma    mark   Any non-hyphenated or hyphenated string "
18782                    "(including parentheses)."));
18783 }
18784 
18785 TEST_F(FormatTest, UnderstandPragmaOption) {
18786   verifyFormat("#pragma option -C -A");
18787 
18788   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
18789 }
18790 
18791 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
18792   FormatStyle Style = getLLVMStyleWithColumns(20);
18793 
18794   // See PR41213
18795   EXPECT_EQ("/*\n"
18796             " *\t9012345\n"
18797             " * /8901\n"
18798             " */",
18799             format("/*\n"
18800                    " *\t9012345 /8901\n"
18801                    " */",
18802                    Style));
18803   EXPECT_EQ("/*\n"
18804             " *345678\n"
18805             " *\t/8901\n"
18806             " */",
18807             format("/*\n"
18808                    " *345678\t/8901\n"
18809                    " */",
18810                    Style));
18811 
18812   verifyFormat("int a; // the\n"
18813                "       // comment",
18814                Style);
18815   EXPECT_EQ("int a; /* first line\n"
18816             "        * second\n"
18817             "        * line third\n"
18818             "        * line\n"
18819             "        */",
18820             format("int a; /* first line\n"
18821                    "        * second\n"
18822                    "        * line third\n"
18823                    "        * line\n"
18824                    "        */",
18825                    Style));
18826   EXPECT_EQ("int a; // first line\n"
18827             "       // second\n"
18828             "       // line third\n"
18829             "       // line",
18830             format("int a; // first line\n"
18831                    "       // second line\n"
18832                    "       // third line",
18833                    Style));
18834 
18835   Style.PenaltyExcessCharacter = 90;
18836   verifyFormat("int a; // the comment", Style);
18837   EXPECT_EQ("int a; // the comment\n"
18838             "       // aaa",
18839             format("int a; // the comment aaa", Style));
18840   EXPECT_EQ("int a; /* first line\n"
18841             "        * second line\n"
18842             "        * third line\n"
18843             "        */",
18844             format("int a; /* first line\n"
18845                    "        * second line\n"
18846                    "        * third line\n"
18847                    "        */",
18848                    Style));
18849   EXPECT_EQ("int a; // first line\n"
18850             "       // second line\n"
18851             "       // third line",
18852             format("int a; // first line\n"
18853                    "       // second line\n"
18854                    "       // third line",
18855                    Style));
18856   // FIXME: Investigate why this is not getting the same layout as the test
18857   // above.
18858   EXPECT_EQ("int a; /* first line\n"
18859             "        * second line\n"
18860             "        * third line\n"
18861             "        */",
18862             format("int a; /* first line second line third line"
18863                    "\n*/",
18864                    Style));
18865 
18866   EXPECT_EQ("// foo bar baz bazfoo\n"
18867             "// foo bar foo bar\n",
18868             format("// foo bar baz bazfoo\n"
18869                    "// foo bar foo           bar\n",
18870                    Style));
18871   EXPECT_EQ("// foo bar baz bazfoo\n"
18872             "// foo bar foo bar\n",
18873             format("// foo bar baz      bazfoo\n"
18874                    "// foo            bar foo bar\n",
18875                    Style));
18876 
18877   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
18878   // next one.
18879   EXPECT_EQ("// foo bar baz bazfoo\n"
18880             "// bar foo bar\n",
18881             format("// foo bar baz      bazfoo bar\n"
18882                    "// foo            bar\n",
18883                    Style));
18884 
18885   EXPECT_EQ("// foo bar baz bazfoo\n"
18886             "// foo bar baz bazfoo\n"
18887             "// bar foo bar\n",
18888             format("// foo bar baz      bazfoo\n"
18889                    "// foo bar baz      bazfoo bar\n"
18890                    "// foo bar\n",
18891                    Style));
18892 
18893   EXPECT_EQ("// foo bar baz bazfoo\n"
18894             "// foo bar baz bazfoo\n"
18895             "// bar foo bar\n",
18896             format("// foo bar baz      bazfoo\n"
18897                    "// foo bar baz      bazfoo bar\n"
18898                    "// foo           bar\n",
18899                    Style));
18900 
18901   // Make sure we do not keep protruding characters if strict mode reflow is
18902   // cheaper than keeping protruding characters.
18903   Style.ColumnLimit = 21;
18904   EXPECT_EQ(
18905       "// foo foo foo foo\n"
18906       "// foo foo foo foo\n"
18907       "// foo foo foo foo\n",
18908       format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style));
18909 
18910   EXPECT_EQ("int a = /* long block\n"
18911             "           comment */\n"
18912             "    42;",
18913             format("int a = /* long block comment */ 42;", Style));
18914 }
18915 
18916 TEST_F(FormatTest, BreakPenaltyAfterLParen) {
18917   FormatStyle Style = getLLVMStyle();
18918   Style.ColumnLimit = 8;
18919   Style.PenaltyExcessCharacter = 15;
18920   verifyFormat("int foo(\n"
18921                "    int aaaaaaaaaaaaaaaaaaaaaaaa);",
18922                Style);
18923   Style.PenaltyBreakOpenParenthesis = 200;
18924   EXPECT_EQ("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);",
18925             format("int foo(\n"
18926                    "    int aaaaaaaaaaaaaaaaaaaaaaaa);",
18927                    Style));
18928 }
18929 
18930 TEST_F(FormatTest, BreakPenaltyAfterCastLParen) {
18931   FormatStyle Style = getLLVMStyle();
18932   Style.ColumnLimit = 5;
18933   Style.PenaltyExcessCharacter = 150;
18934   verifyFormat("foo((\n"
18935                "    int)aaaaaaaaaaaaaaaaaaaaaaaa);",
18936 
18937                Style);
18938   Style.PenaltyBreakOpenParenthesis = 100000;
18939   EXPECT_EQ("foo((int)\n"
18940             "        aaaaaaaaaaaaaaaaaaaaaaaa);",
18941             format("foo((\n"
18942                    "int)aaaaaaaaaaaaaaaaaaaaaaaa);",
18943                    Style));
18944 }
18945 
18946 TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) {
18947   FormatStyle Style = getLLVMStyle();
18948   Style.ColumnLimit = 4;
18949   Style.PenaltyExcessCharacter = 100;
18950   verifyFormat("for (\n"
18951                "    int iiiiiiiiiiiiiiiii =\n"
18952                "        0;\n"
18953                "    iiiiiiiiiiiiiiiii <\n"
18954                "    2;\n"
18955                "    iiiiiiiiiiiiiiiii++) {\n"
18956                "}",
18957 
18958                Style);
18959   Style.PenaltyBreakOpenParenthesis = 1250;
18960   EXPECT_EQ("for (int iiiiiiiiiiiiiiiii =\n"
18961             "         0;\n"
18962             "     iiiiiiiiiiiiiiiii <\n"
18963             "     2;\n"
18964             "     iiiiiiiiiiiiiiiii++) {\n"
18965             "}",
18966             format("for (\n"
18967                    "    int iiiiiiiiiiiiiiiii =\n"
18968                    "        0;\n"
18969                    "    iiiiiiiiiiiiiiiii <\n"
18970                    "    2;\n"
18971                    "    iiiiiiiiiiiiiiiii++) {\n"
18972                    "}",
18973                    Style));
18974 }
18975 
18976 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
18977   for (size_t i = 1; i < Styles.size(); ++i)                                   \
18978   EXPECT_EQ(Styles[0], Styles[i])                                              \
18979       << "Style #" << i << " of " << Styles.size() << " differs from Style #0"
18980 
18981 TEST_F(FormatTest, GetsPredefinedStyleByName) {
18982   SmallVector<FormatStyle, 3> Styles;
18983   Styles.resize(3);
18984 
18985   Styles[0] = getLLVMStyle();
18986   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
18987   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
18988   EXPECT_ALL_STYLES_EQUAL(Styles);
18989 
18990   Styles[0] = getGoogleStyle();
18991   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
18992   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
18993   EXPECT_ALL_STYLES_EQUAL(Styles);
18994 
18995   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
18996   EXPECT_TRUE(
18997       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
18998   EXPECT_TRUE(
18999       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
19000   EXPECT_ALL_STYLES_EQUAL(Styles);
19001 
19002   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
19003   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
19004   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
19005   EXPECT_ALL_STYLES_EQUAL(Styles);
19006 
19007   Styles[0] = getMozillaStyle();
19008   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
19009   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
19010   EXPECT_ALL_STYLES_EQUAL(Styles);
19011 
19012   Styles[0] = getWebKitStyle();
19013   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
19014   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
19015   EXPECT_ALL_STYLES_EQUAL(Styles);
19016 
19017   Styles[0] = getGNUStyle();
19018   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
19019   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
19020   EXPECT_ALL_STYLES_EQUAL(Styles);
19021 
19022   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
19023 }
19024 
19025 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
19026   SmallVector<FormatStyle, 8> Styles;
19027   Styles.resize(2);
19028 
19029   Styles[0] = getGoogleStyle();
19030   Styles[1] = getLLVMStyle();
19031   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
19032   EXPECT_ALL_STYLES_EQUAL(Styles);
19033 
19034   Styles.resize(5);
19035   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
19036   Styles[1] = getLLVMStyle();
19037   Styles[1].Language = FormatStyle::LK_JavaScript;
19038   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
19039 
19040   Styles[2] = getLLVMStyle();
19041   Styles[2].Language = FormatStyle::LK_JavaScript;
19042   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
19043                                   "BasedOnStyle: Google",
19044                                   &Styles[2])
19045                    .value());
19046 
19047   Styles[3] = getLLVMStyle();
19048   Styles[3].Language = FormatStyle::LK_JavaScript;
19049   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
19050                                   "Language: JavaScript",
19051                                   &Styles[3])
19052                    .value());
19053 
19054   Styles[4] = getLLVMStyle();
19055   Styles[4].Language = FormatStyle::LK_JavaScript;
19056   EXPECT_EQ(0, parseConfiguration("---\n"
19057                                   "BasedOnStyle: LLVM\n"
19058                                   "IndentWidth: 123\n"
19059                                   "---\n"
19060                                   "BasedOnStyle: Google\n"
19061                                   "Language: JavaScript",
19062                                   &Styles[4])
19063                    .value());
19064   EXPECT_ALL_STYLES_EQUAL(Styles);
19065 }
19066 
19067 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
19068   Style.FIELD = false;                                                         \
19069   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
19070   EXPECT_TRUE(Style.FIELD);                                                    \
19071   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
19072   EXPECT_FALSE(Style.FIELD);
19073 
19074 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
19075 
19076 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
19077   Style.STRUCT.FIELD = false;                                                  \
19078   EXPECT_EQ(0,                                                                 \
19079             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
19080                 .value());                                                     \
19081   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
19082   EXPECT_EQ(0,                                                                 \
19083             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
19084                 .value());                                                     \
19085   EXPECT_FALSE(Style.STRUCT.FIELD);
19086 
19087 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
19088   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
19089 
19090 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
19091   EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!";          \
19092   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
19093   EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!"
19094 
19095 TEST_F(FormatTest, ParsesConfigurationBools) {
19096   FormatStyle Style = {};
19097   Style.Language = FormatStyle::LK_Cpp;
19098   CHECK_PARSE_BOOL(AlignTrailingComments);
19099   CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine);
19100   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
19101   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
19102   CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine);
19103   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
19104   CHECK_PARSE_BOOL(BinPackArguments);
19105   CHECK_PARSE_BOOL(BinPackParameters);
19106   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
19107   CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations);
19108   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
19109   CHECK_PARSE_BOOL(BreakStringLiterals);
19110   CHECK_PARSE_BOOL(CompactNamespaces);
19111   CHECK_PARSE_BOOL(DeriveLineEnding);
19112   CHECK_PARSE_BOOL(DerivePointerAlignment);
19113   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
19114   CHECK_PARSE_BOOL(DisableFormat);
19115   CHECK_PARSE_BOOL(IndentAccessModifiers);
19116   CHECK_PARSE_BOOL(IndentCaseLabels);
19117   CHECK_PARSE_BOOL(IndentCaseBlocks);
19118   CHECK_PARSE_BOOL(IndentGotoLabels);
19119   CHECK_PARSE_BOOL(IndentRequires);
19120   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
19121   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
19122   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
19123   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
19124   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
19125   CHECK_PARSE_BOOL(ReflowComments);
19126   CHECK_PARSE_BOOL(RemoveBracesLLVM);
19127   CHECK_PARSE_BOOL(SortUsingDeclarations);
19128   CHECK_PARSE_BOOL(SpacesInParentheses);
19129   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
19130   CHECK_PARSE_BOOL(SpacesInConditionalStatement);
19131   CHECK_PARSE_BOOL(SpaceInEmptyBlock);
19132   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
19133   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
19134   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
19135   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
19136   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
19137   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
19138   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
19139   CHECK_PARSE_BOOL(SpaceBeforeCaseColon);
19140   CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
19141   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
19142   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
19143   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
19144   CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets);
19145   CHECK_PARSE_BOOL(UseCRLF);
19146 
19147   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel);
19148   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
19149   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
19150   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
19151   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
19152   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
19153   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
19154   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
19155   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
19156   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
19157   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
19158   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody);
19159   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile);
19160   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
19161   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
19162   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
19163   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
19164   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterControlStatements);
19165   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterForeachMacros);
19166   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions,
19167                           AfterFunctionDeclarationName);
19168   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions,
19169                           AfterFunctionDefinitionName);
19170   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros);
19171   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator);
19172   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses);
19173 }
19174 
19175 #undef CHECK_PARSE_BOOL
19176 
19177 TEST_F(FormatTest, ParsesConfiguration) {
19178   FormatStyle Style = {};
19179   Style.Language = FormatStyle::LK_Cpp;
19180   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
19181   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
19182               ConstructorInitializerIndentWidth, 1234u);
19183   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
19184   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
19185   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
19186   CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u);
19187   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
19188               PenaltyBreakBeforeFirstCallParameter, 1234u);
19189   CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
19190               PenaltyBreakTemplateDeclaration, 1234u);
19191   CHECK_PARSE("PenaltyBreakOpenParenthesis: 1234", PenaltyBreakOpenParenthesis,
19192               1234u);
19193   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
19194   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
19195               PenaltyReturnTypeOnItsOwnLine, 1234u);
19196   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
19197               SpacesBeforeTrailingComments, 1234u);
19198   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
19199   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
19200   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
19201 
19202   Style.QualifierAlignment = FormatStyle::QAS_Right;
19203   CHECK_PARSE("QualifierAlignment: Leave", QualifierAlignment,
19204               FormatStyle::QAS_Leave);
19205   CHECK_PARSE("QualifierAlignment: Right", QualifierAlignment,
19206               FormatStyle::QAS_Right);
19207   CHECK_PARSE("QualifierAlignment: Left", QualifierAlignment,
19208               FormatStyle::QAS_Left);
19209   CHECK_PARSE("QualifierAlignment: Custom", QualifierAlignment,
19210               FormatStyle::QAS_Custom);
19211 
19212   Style.QualifierOrder.clear();
19213   CHECK_PARSE("QualifierOrder: [ const, volatile, type ]", QualifierOrder,
19214               std::vector<std::string>({"const", "volatile", "type"}));
19215   Style.QualifierOrder.clear();
19216   CHECK_PARSE("QualifierOrder: [const, type]", QualifierOrder,
19217               std::vector<std::string>({"const", "type"}));
19218   Style.QualifierOrder.clear();
19219   CHECK_PARSE("QualifierOrder: [volatile, type]", QualifierOrder,
19220               std::vector<std::string>({"volatile", "type"}));
19221 
19222   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
19223   CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments,
19224               FormatStyle::ACS_None);
19225   CHECK_PARSE("AlignConsecutiveAssignments: Consecutive",
19226               AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive);
19227   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines",
19228               AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines);
19229   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments",
19230               AlignConsecutiveAssignments,
19231               FormatStyle::ACS_AcrossEmptyLinesAndComments);
19232   // For backwards compability, false / true should still parse
19233   CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments,
19234               FormatStyle::ACS_None);
19235   CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments,
19236               FormatStyle::ACS_Consecutive);
19237 
19238   Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
19239   CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields,
19240               FormatStyle::ACS_None);
19241   CHECK_PARSE("AlignConsecutiveBitFields: Consecutive",
19242               AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive);
19243   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines",
19244               AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines);
19245   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments",
19246               AlignConsecutiveBitFields,
19247               FormatStyle::ACS_AcrossEmptyLinesAndComments);
19248   // For backwards compability, false / true should still parse
19249   CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields,
19250               FormatStyle::ACS_None);
19251   CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields,
19252               FormatStyle::ACS_Consecutive);
19253 
19254   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
19255   CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros,
19256               FormatStyle::ACS_None);
19257   CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros,
19258               FormatStyle::ACS_Consecutive);
19259   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines",
19260               AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines);
19261   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments",
19262               AlignConsecutiveMacros,
19263               FormatStyle::ACS_AcrossEmptyLinesAndComments);
19264   // For backwards compability, false / true should still parse
19265   CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros,
19266               FormatStyle::ACS_None);
19267   CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros,
19268               FormatStyle::ACS_Consecutive);
19269 
19270   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
19271   CHECK_PARSE("AlignConsecutiveDeclarations: None",
19272               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
19273   CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive",
19274               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
19275   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines",
19276               AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines);
19277   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments",
19278               AlignConsecutiveDeclarations,
19279               FormatStyle::ACS_AcrossEmptyLinesAndComments);
19280   // For backwards compability, false / true should still parse
19281   CHECK_PARSE("AlignConsecutiveDeclarations: false",
19282               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
19283   CHECK_PARSE("AlignConsecutiveDeclarations: true",
19284               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
19285 
19286   Style.PointerAlignment = FormatStyle::PAS_Middle;
19287   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
19288               FormatStyle::PAS_Left);
19289   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
19290               FormatStyle::PAS_Right);
19291   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
19292               FormatStyle::PAS_Middle);
19293   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
19294   CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment,
19295               FormatStyle::RAS_Pointer);
19296   CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment,
19297               FormatStyle::RAS_Left);
19298   CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment,
19299               FormatStyle::RAS_Right);
19300   CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment,
19301               FormatStyle::RAS_Middle);
19302   // For backward compatibility:
19303   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
19304               FormatStyle::PAS_Left);
19305   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
19306               FormatStyle::PAS_Right);
19307   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
19308               FormatStyle::PAS_Middle);
19309 
19310   Style.Standard = FormatStyle::LS_Auto;
19311   CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03);
19312   CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11);
19313   CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14);
19314   CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17);
19315   CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20);
19316   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
19317   CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest);
19318   // Legacy aliases:
19319   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
19320   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest);
19321   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
19322   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
19323 
19324   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
19325   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
19326               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
19327   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
19328               FormatStyle::BOS_None);
19329   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
19330               FormatStyle::BOS_All);
19331   // For backward compatibility:
19332   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
19333               FormatStyle::BOS_None);
19334   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
19335               FormatStyle::BOS_All);
19336 
19337   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
19338   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
19339               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
19340   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
19341               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
19342   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
19343               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
19344   // For backward compatibility:
19345   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
19346               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
19347 
19348   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
19349   CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList,
19350               FormatStyle::BILS_AfterComma);
19351   CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList,
19352               FormatStyle::BILS_BeforeComma);
19353   CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList,
19354               FormatStyle::BILS_AfterColon);
19355   CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList,
19356               FormatStyle::BILS_BeforeColon);
19357   // For backward compatibility:
19358   CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList,
19359               FormatStyle::BILS_BeforeComma);
19360 
19361   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
19362   CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers,
19363               FormatStyle::PCIS_Never);
19364   CHECK_PARSE("PackConstructorInitializers: BinPack",
19365               PackConstructorInitializers, FormatStyle::PCIS_BinPack);
19366   CHECK_PARSE("PackConstructorInitializers: CurrentLine",
19367               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
19368   CHECK_PARSE("PackConstructorInitializers: NextLine",
19369               PackConstructorInitializers, FormatStyle::PCIS_NextLine);
19370   // For backward compatibility:
19371   CHECK_PARSE("BasedOnStyle: Google\n"
19372               "ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
19373               "AllowAllConstructorInitializersOnNextLine: false",
19374               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
19375   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
19376   CHECK_PARSE("BasedOnStyle: Google\n"
19377               "ConstructorInitializerAllOnOneLineOrOnePerLine: false",
19378               PackConstructorInitializers, FormatStyle::PCIS_BinPack);
19379   CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
19380               "AllowAllConstructorInitializersOnNextLine: true",
19381               PackConstructorInitializers, FormatStyle::PCIS_NextLine);
19382   Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
19383   CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
19384               "AllowAllConstructorInitializersOnNextLine: false",
19385               PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
19386 
19387   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
19388   CHECK_PARSE("EmptyLineBeforeAccessModifier: Never",
19389               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never);
19390   CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave",
19391               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave);
19392   CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock",
19393               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock);
19394   CHECK_PARSE("EmptyLineBeforeAccessModifier: Always",
19395               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always);
19396 
19397   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
19398   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
19399               FormatStyle::BAS_Align);
19400   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
19401               FormatStyle::BAS_DontAlign);
19402   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
19403               FormatStyle::BAS_AlwaysBreak);
19404   CHECK_PARSE("AlignAfterOpenBracket: BlockIndent", AlignAfterOpenBracket,
19405               FormatStyle::BAS_BlockIndent);
19406   // For backward compatibility:
19407   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
19408               FormatStyle::BAS_DontAlign);
19409   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
19410               FormatStyle::BAS_Align);
19411 
19412   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
19413   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
19414               FormatStyle::ENAS_DontAlign);
19415   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
19416               FormatStyle::ENAS_Left);
19417   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
19418               FormatStyle::ENAS_Right);
19419   // For backward compatibility:
19420   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
19421               FormatStyle::ENAS_Left);
19422   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
19423               FormatStyle::ENAS_Right);
19424 
19425   Style.AlignOperands = FormatStyle::OAS_Align;
19426   CHECK_PARSE("AlignOperands: DontAlign", AlignOperands,
19427               FormatStyle::OAS_DontAlign);
19428   CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align);
19429   CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands,
19430               FormatStyle::OAS_AlignAfterOperator);
19431   // For backward compatibility:
19432   CHECK_PARSE("AlignOperands: false", AlignOperands,
19433               FormatStyle::OAS_DontAlign);
19434   CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align);
19435 
19436   Style.UseTab = FormatStyle::UT_ForIndentation;
19437   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
19438   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
19439   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
19440   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
19441               FormatStyle::UT_ForContinuationAndIndentation);
19442   CHECK_PARSE("UseTab: AlignWithSpaces", UseTab,
19443               FormatStyle::UT_AlignWithSpaces);
19444   // For backward compatibility:
19445   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
19446   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
19447 
19448   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
19449   CHECK_PARSE("AllowShortBlocksOnASingleLine: Never",
19450               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
19451   CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty",
19452               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty);
19453   CHECK_PARSE("AllowShortBlocksOnASingleLine: Always",
19454               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
19455   // For backward compatibility:
19456   CHECK_PARSE("AllowShortBlocksOnASingleLine: false",
19457               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
19458   CHECK_PARSE("AllowShortBlocksOnASingleLine: true",
19459               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
19460 
19461   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
19462   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
19463               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
19464   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
19465               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
19466   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
19467               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
19468   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
19469               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
19470   // For backward compatibility:
19471   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
19472               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
19473   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
19474               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
19475 
19476   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both;
19477   CHECK_PARSE("SpaceAroundPointerQualifiers: Default",
19478               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default);
19479   CHECK_PARSE("SpaceAroundPointerQualifiers: Before",
19480               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before);
19481   CHECK_PARSE("SpaceAroundPointerQualifiers: After",
19482               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After);
19483   CHECK_PARSE("SpaceAroundPointerQualifiers: Both",
19484               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both);
19485 
19486   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
19487   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
19488               FormatStyle::SBPO_Never);
19489   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
19490               FormatStyle::SBPO_Always);
19491   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
19492               FormatStyle::SBPO_ControlStatements);
19493   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros",
19494               SpaceBeforeParens,
19495               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
19496   CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens,
19497               FormatStyle::SBPO_NonEmptyParentheses);
19498   CHECK_PARSE("SpaceBeforeParens: Custom", SpaceBeforeParens,
19499               FormatStyle::SBPO_Custom);
19500   // For backward compatibility:
19501   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
19502               FormatStyle::SBPO_Never);
19503   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
19504               FormatStyle::SBPO_ControlStatements);
19505   CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros",
19506               SpaceBeforeParens,
19507               FormatStyle::SBPO_ControlStatementsExceptControlMacros);
19508 
19509   Style.ColumnLimit = 123;
19510   FormatStyle BaseStyle = getLLVMStyle();
19511   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
19512   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
19513 
19514   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
19515   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
19516               FormatStyle::BS_Attach);
19517   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
19518               FormatStyle::BS_Linux);
19519   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
19520               FormatStyle::BS_Mozilla);
19521   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
19522               FormatStyle::BS_Stroustrup);
19523   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
19524               FormatStyle::BS_Allman);
19525   CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces,
19526               FormatStyle::BS_Whitesmiths);
19527   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
19528   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
19529               FormatStyle::BS_WebKit);
19530   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
19531               FormatStyle::BS_Custom);
19532 
19533   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
19534   CHECK_PARSE("BraceWrapping:\n"
19535               "  AfterControlStatement: MultiLine",
19536               BraceWrapping.AfterControlStatement,
19537               FormatStyle::BWACS_MultiLine);
19538   CHECK_PARSE("BraceWrapping:\n"
19539               "  AfterControlStatement: Always",
19540               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
19541   CHECK_PARSE("BraceWrapping:\n"
19542               "  AfterControlStatement: Never",
19543               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
19544   // For backward compatibility:
19545   CHECK_PARSE("BraceWrapping:\n"
19546               "  AfterControlStatement: true",
19547               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
19548   CHECK_PARSE("BraceWrapping:\n"
19549               "  AfterControlStatement: false",
19550               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
19551 
19552   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
19553   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
19554               FormatStyle::RTBS_None);
19555   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
19556               FormatStyle::RTBS_All);
19557   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
19558               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
19559   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
19560               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
19561   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
19562               AlwaysBreakAfterReturnType,
19563               FormatStyle::RTBS_TopLevelDefinitions);
19564 
19565   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
19566   CHECK_PARSE("AlwaysBreakTemplateDeclarations: No",
19567               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No);
19568   CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine",
19569               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
19570   CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes",
19571               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
19572   CHECK_PARSE("AlwaysBreakTemplateDeclarations: false",
19573               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
19574   CHECK_PARSE("AlwaysBreakTemplateDeclarations: true",
19575               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
19576 
19577   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
19578   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
19579               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
19580   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
19581               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
19582   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
19583               AlwaysBreakAfterDefinitionReturnType,
19584               FormatStyle::DRTBS_TopLevel);
19585 
19586   Style.NamespaceIndentation = FormatStyle::NI_All;
19587   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
19588               FormatStyle::NI_None);
19589   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
19590               FormatStyle::NI_Inner);
19591   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
19592               FormatStyle::NI_All);
19593 
19594   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf;
19595   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never",
19596               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
19597   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse",
19598               AllowShortIfStatementsOnASingleLine,
19599               FormatStyle::SIS_WithoutElse);
19600   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf",
19601               AllowShortIfStatementsOnASingleLine,
19602               FormatStyle::SIS_OnlyFirstIf);
19603   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse",
19604               AllowShortIfStatementsOnASingleLine,
19605               FormatStyle::SIS_AllIfsAndElse);
19606   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always",
19607               AllowShortIfStatementsOnASingleLine,
19608               FormatStyle::SIS_OnlyFirstIf);
19609   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false",
19610               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
19611   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true",
19612               AllowShortIfStatementsOnASingleLine,
19613               FormatStyle::SIS_WithoutElse);
19614 
19615   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
19616   CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock,
19617               FormatStyle::IEBS_AfterExternBlock);
19618   CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock,
19619               FormatStyle::IEBS_Indent);
19620   CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock,
19621               FormatStyle::IEBS_NoIndent);
19622   CHECK_PARSE("IndentExternBlock: true", IndentExternBlock,
19623               FormatStyle::IEBS_Indent);
19624   CHECK_PARSE("IndentExternBlock: false", IndentExternBlock,
19625               FormatStyle::IEBS_NoIndent);
19626 
19627   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
19628   CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing,
19629               FormatStyle::BFCS_Both);
19630   CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing,
19631               FormatStyle::BFCS_None);
19632   CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing,
19633               FormatStyle::BFCS_Before);
19634   CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing,
19635               FormatStyle::BFCS_After);
19636 
19637   Style.SortJavaStaticImport = FormatStyle::SJSIO_Before;
19638   CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport,
19639               FormatStyle::SJSIO_After);
19640   CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport,
19641               FormatStyle::SJSIO_Before);
19642 
19643   // FIXME: This is required because parsing a configuration simply overwrites
19644   // the first N elements of the list instead of resetting it.
19645   Style.ForEachMacros.clear();
19646   std::vector<std::string> BoostForeach;
19647   BoostForeach.push_back("BOOST_FOREACH");
19648   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
19649   std::vector<std::string> BoostAndQForeach;
19650   BoostAndQForeach.push_back("BOOST_FOREACH");
19651   BoostAndQForeach.push_back("Q_FOREACH");
19652   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
19653               BoostAndQForeach);
19654 
19655   Style.IfMacros.clear();
19656   std::vector<std::string> CustomIfs;
19657   CustomIfs.push_back("MYIF");
19658   CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs);
19659 
19660   Style.AttributeMacros.clear();
19661   CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros,
19662               std::vector<std::string>{"__capability"});
19663   CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros,
19664               std::vector<std::string>({"attr1", "attr2"}));
19665 
19666   Style.StatementAttributeLikeMacros.clear();
19667   CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]",
19668               StatementAttributeLikeMacros,
19669               std::vector<std::string>({"emit", "Q_EMIT"}));
19670 
19671   Style.StatementMacros.clear();
19672   CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
19673               std::vector<std::string>{"QUNUSED"});
19674   CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros,
19675               std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
19676 
19677   Style.NamespaceMacros.clear();
19678   CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros,
19679               std::vector<std::string>{"TESTSUITE"});
19680   CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros,
19681               std::vector<std::string>({"TESTSUITE", "SUITE"}));
19682 
19683   Style.WhitespaceSensitiveMacros.clear();
19684   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]",
19685               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
19686   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]",
19687               WhitespaceSensitiveMacros,
19688               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
19689   Style.WhitespaceSensitiveMacros.clear();
19690   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']",
19691               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
19692   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']",
19693               WhitespaceSensitiveMacros,
19694               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
19695 
19696   Style.IncludeStyle.IncludeCategories.clear();
19697   std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
19698       {"abc/.*", 2, 0, false}, {".*", 1, 0, true}};
19699   CHECK_PARSE("IncludeCategories:\n"
19700               "  - Regex: abc/.*\n"
19701               "    Priority: 2\n"
19702               "  - Regex: .*\n"
19703               "    Priority: 1\n"
19704               "    CaseSensitive: true\n",
19705               IncludeStyle.IncludeCategories, ExpectedCategories);
19706   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
19707               "abc$");
19708   CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'",
19709               IncludeStyle.IncludeIsMainSourceRegex, "abc$");
19710 
19711   Style.SortIncludes = FormatStyle::SI_Never;
19712   CHECK_PARSE("SortIncludes: true", SortIncludes,
19713               FormatStyle::SI_CaseSensitive);
19714   CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never);
19715   CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes,
19716               FormatStyle::SI_CaseInsensitive);
19717   CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes,
19718               FormatStyle::SI_CaseSensitive);
19719   CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never);
19720 
19721   Style.RawStringFormats.clear();
19722   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
19723       {
19724           FormatStyle::LK_TextProto,
19725           {"pb", "proto"},
19726           {"PARSE_TEXT_PROTO"},
19727           /*CanonicalDelimiter=*/"",
19728           "llvm",
19729       },
19730       {
19731           FormatStyle::LK_Cpp,
19732           {"cc", "cpp"},
19733           {"C_CODEBLOCK", "CPPEVAL"},
19734           /*CanonicalDelimiter=*/"cc",
19735           /*BasedOnStyle=*/"",
19736       },
19737   };
19738 
19739   CHECK_PARSE("RawStringFormats:\n"
19740               "  - Language: TextProto\n"
19741               "    Delimiters:\n"
19742               "      - 'pb'\n"
19743               "      - 'proto'\n"
19744               "    EnclosingFunctions:\n"
19745               "      - 'PARSE_TEXT_PROTO'\n"
19746               "    BasedOnStyle: llvm\n"
19747               "  - Language: Cpp\n"
19748               "    Delimiters:\n"
19749               "      - 'cc'\n"
19750               "      - 'cpp'\n"
19751               "    EnclosingFunctions:\n"
19752               "      - 'C_CODEBLOCK'\n"
19753               "      - 'CPPEVAL'\n"
19754               "    CanonicalDelimiter: 'cc'",
19755               RawStringFormats, ExpectedRawStringFormats);
19756 
19757   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19758               "  Minimum: 0\n"
19759               "  Maximum: 0",
19760               SpacesInLineCommentPrefix.Minimum, 0u);
19761   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u);
19762   Style.SpacesInLineCommentPrefix.Minimum = 1;
19763   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19764               "  Minimum: 2",
19765               SpacesInLineCommentPrefix.Minimum, 0u);
19766   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19767               "  Maximum: -1",
19768               SpacesInLineCommentPrefix.Maximum, -1u);
19769   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19770               "  Minimum: 2",
19771               SpacesInLineCommentPrefix.Minimum, 2u);
19772   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
19773               "  Maximum: 1",
19774               SpacesInLineCommentPrefix.Maximum, 1u);
19775   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u);
19776 
19777   Style.SpacesInAngles = FormatStyle::SIAS_Always;
19778   CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never);
19779   CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles,
19780               FormatStyle::SIAS_Always);
19781   CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave);
19782   // For backward compatibility:
19783   CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never);
19784   CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always);
19785 }
19786 
19787 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
19788   FormatStyle Style = {};
19789   Style.Language = FormatStyle::LK_Cpp;
19790   CHECK_PARSE("Language: Cpp\n"
19791               "IndentWidth: 12",
19792               IndentWidth, 12u);
19793   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
19794                                "IndentWidth: 34",
19795                                &Style),
19796             ParseError::Unsuitable);
19797   FormatStyle BinPackedTCS = {};
19798   BinPackedTCS.Language = FormatStyle::LK_JavaScript;
19799   EXPECT_EQ(parseConfiguration("BinPackArguments: true\n"
19800                                "InsertTrailingCommas: Wrapped",
19801                                &BinPackedTCS),
19802             ParseError::BinPackTrailingCommaConflict);
19803   EXPECT_EQ(12u, Style.IndentWidth);
19804   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
19805   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
19806 
19807   Style.Language = FormatStyle::LK_JavaScript;
19808   CHECK_PARSE("Language: JavaScript\n"
19809               "IndentWidth: 12",
19810               IndentWidth, 12u);
19811   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
19812   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
19813                                "IndentWidth: 34",
19814                                &Style),
19815             ParseError::Unsuitable);
19816   EXPECT_EQ(23u, Style.IndentWidth);
19817   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
19818   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
19819 
19820   CHECK_PARSE("BasedOnStyle: LLVM\n"
19821               "IndentWidth: 67",
19822               IndentWidth, 67u);
19823 
19824   CHECK_PARSE("---\n"
19825               "Language: JavaScript\n"
19826               "IndentWidth: 12\n"
19827               "---\n"
19828               "Language: Cpp\n"
19829               "IndentWidth: 34\n"
19830               "...\n",
19831               IndentWidth, 12u);
19832 
19833   Style.Language = FormatStyle::LK_Cpp;
19834   CHECK_PARSE("---\n"
19835               "Language: JavaScript\n"
19836               "IndentWidth: 12\n"
19837               "---\n"
19838               "Language: Cpp\n"
19839               "IndentWidth: 34\n"
19840               "...\n",
19841               IndentWidth, 34u);
19842   CHECK_PARSE("---\n"
19843               "IndentWidth: 78\n"
19844               "---\n"
19845               "Language: JavaScript\n"
19846               "IndentWidth: 56\n"
19847               "...\n",
19848               IndentWidth, 78u);
19849 
19850   Style.ColumnLimit = 123;
19851   Style.IndentWidth = 234;
19852   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
19853   Style.TabWidth = 345;
19854   EXPECT_FALSE(parseConfiguration("---\n"
19855                                   "IndentWidth: 456\n"
19856                                   "BreakBeforeBraces: Allman\n"
19857                                   "---\n"
19858                                   "Language: JavaScript\n"
19859                                   "IndentWidth: 111\n"
19860                                   "TabWidth: 111\n"
19861                                   "---\n"
19862                                   "Language: Cpp\n"
19863                                   "BreakBeforeBraces: Stroustrup\n"
19864                                   "TabWidth: 789\n"
19865                                   "...\n",
19866                                   &Style));
19867   EXPECT_EQ(123u, Style.ColumnLimit);
19868   EXPECT_EQ(456u, Style.IndentWidth);
19869   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
19870   EXPECT_EQ(789u, Style.TabWidth);
19871 
19872   EXPECT_EQ(parseConfiguration("---\n"
19873                                "Language: JavaScript\n"
19874                                "IndentWidth: 56\n"
19875                                "---\n"
19876                                "IndentWidth: 78\n"
19877                                "...\n",
19878                                &Style),
19879             ParseError::Error);
19880   EXPECT_EQ(parseConfiguration("---\n"
19881                                "Language: JavaScript\n"
19882                                "IndentWidth: 56\n"
19883                                "---\n"
19884                                "Language: JavaScript\n"
19885                                "IndentWidth: 78\n"
19886                                "...\n",
19887                                &Style),
19888             ParseError::Error);
19889 
19890   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
19891 }
19892 
19893 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
19894   FormatStyle Style = {};
19895   Style.Language = FormatStyle::LK_JavaScript;
19896   Style.BreakBeforeTernaryOperators = true;
19897   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
19898   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
19899 
19900   Style.BreakBeforeTernaryOperators = true;
19901   EXPECT_EQ(0, parseConfiguration("---\n"
19902                                   "BasedOnStyle: Google\n"
19903                                   "---\n"
19904                                   "Language: JavaScript\n"
19905                                   "IndentWidth: 76\n"
19906                                   "...\n",
19907                                   &Style)
19908                    .value());
19909   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
19910   EXPECT_EQ(76u, Style.IndentWidth);
19911   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
19912 }
19913 
19914 TEST_F(FormatTest, ConfigurationRoundTripTest) {
19915   FormatStyle Style = getLLVMStyle();
19916   std::string YAML = configurationAsText(Style);
19917   FormatStyle ParsedStyle = {};
19918   ParsedStyle.Language = FormatStyle::LK_Cpp;
19919   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
19920   EXPECT_EQ(Style, ParsedStyle);
19921 }
19922 
19923 TEST_F(FormatTest, WorksFor8bitEncodings) {
19924   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
19925             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
19926             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
19927             "\"\xef\xee\xf0\xf3...\"",
19928             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
19929                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
19930                    "\xef\xee\xf0\xf3...\"",
19931                    getLLVMStyleWithColumns(12)));
19932 }
19933 
19934 TEST_F(FormatTest, HandlesUTF8BOM) {
19935   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
19936   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
19937             format("\xef\xbb\xbf#include <iostream>"));
19938   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
19939             format("\xef\xbb\xbf\n#include <iostream>"));
19940 }
19941 
19942 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
19943 #if !defined(_MSC_VER)
19944 
19945 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
19946   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
19947                getLLVMStyleWithColumns(35));
19948   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
19949                getLLVMStyleWithColumns(31));
19950   verifyFormat("// Однажды в студёную зимнюю пору...",
19951                getLLVMStyleWithColumns(36));
19952   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
19953   verifyFormat("/* Однажды в студёную зимнюю пору... */",
19954                getLLVMStyleWithColumns(39));
19955   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
19956                getLLVMStyleWithColumns(35));
19957 }
19958 
19959 TEST_F(FormatTest, SplitsUTF8Strings) {
19960   // Non-printable characters' width is currently considered to be the length in
19961   // bytes in UTF8. The characters can be displayed in very different manner
19962   // (zero-width, single width with a substitution glyph, expanded to their code
19963   // (e.g. "<8d>"), so there's no single correct way to handle them.
19964   EXPECT_EQ("\"aaaaÄ\"\n"
19965             "\"\xc2\x8d\";",
19966             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
19967   EXPECT_EQ("\"aaaaaaaÄ\"\n"
19968             "\"\xc2\x8d\";",
19969             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
19970   EXPECT_EQ("\"Однажды, в \"\n"
19971             "\"студёную \"\n"
19972             "\"зимнюю \"\n"
19973             "\"пору,\"",
19974             format("\"Однажды, в студёную зимнюю пору,\"",
19975                    getLLVMStyleWithColumns(13)));
19976   EXPECT_EQ(
19977       "\"一 二 三 \"\n"
19978       "\"四 五六 \"\n"
19979       "\"七 八 九 \"\n"
19980       "\"十\"",
19981       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
19982   EXPECT_EQ("\"一\t\"\n"
19983             "\"二 \t\"\n"
19984             "\"三 四 \"\n"
19985             "\"五\t\"\n"
19986             "\"六 \t\"\n"
19987             "\"七 \"\n"
19988             "\"八九十\tqq\"",
19989             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
19990                    getLLVMStyleWithColumns(11)));
19991 
19992   // UTF8 character in an escape sequence.
19993   EXPECT_EQ("\"aaaaaa\"\n"
19994             "\"\\\xC2\x8D\"",
19995             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
19996 }
19997 
19998 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
19999   EXPECT_EQ("const char *sssss =\n"
20000             "    \"一二三四五六七八\\\n"
20001             " 九 十\";",
20002             format("const char *sssss = \"一二三四五六七八\\\n"
20003                    " 九 十\";",
20004                    getLLVMStyleWithColumns(30)));
20005 }
20006 
20007 TEST_F(FormatTest, SplitsUTF8LineComments) {
20008   EXPECT_EQ("// aaaaÄ\xc2\x8d",
20009             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
20010   EXPECT_EQ("// Я из лесу\n"
20011             "// вышел; был\n"
20012             "// сильный\n"
20013             "// мороз.",
20014             format("// Я из лесу вышел; был сильный мороз.",
20015                    getLLVMStyleWithColumns(13)));
20016   EXPECT_EQ("// 一二三\n"
20017             "// 四五六七\n"
20018             "// 八  九\n"
20019             "// 十",
20020             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
20021 }
20022 
20023 TEST_F(FormatTest, SplitsUTF8BlockComments) {
20024   EXPECT_EQ("/* Гляжу,\n"
20025             " * поднимается\n"
20026             " * медленно в\n"
20027             " * гору\n"
20028             " * Лошадка,\n"
20029             " * везущая\n"
20030             " * хворосту\n"
20031             " * воз. */",
20032             format("/* Гляжу, поднимается медленно в гору\n"
20033                    " * Лошадка, везущая хворосту воз. */",
20034                    getLLVMStyleWithColumns(13)));
20035   EXPECT_EQ(
20036       "/* 一二三\n"
20037       " * 四五六七\n"
20038       " * 八  九\n"
20039       " * 十  */",
20040       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
20041   EXPECT_EQ("/* �������� ��������\n"
20042             " * ��������\n"
20043             " * ������-�� */",
20044             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
20045 }
20046 
20047 #endif // _MSC_VER
20048 
20049 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
20050   FormatStyle Style = getLLVMStyle();
20051 
20052   Style.ConstructorInitializerIndentWidth = 4;
20053   verifyFormat(
20054       "SomeClass::Constructor()\n"
20055       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
20056       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
20057       Style);
20058 
20059   Style.ConstructorInitializerIndentWidth = 2;
20060   verifyFormat(
20061       "SomeClass::Constructor()\n"
20062       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
20063       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
20064       Style);
20065 
20066   Style.ConstructorInitializerIndentWidth = 0;
20067   verifyFormat(
20068       "SomeClass::Constructor()\n"
20069       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
20070       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
20071       Style);
20072   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
20073   verifyFormat(
20074       "SomeLongTemplateVariableName<\n"
20075       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
20076       Style);
20077   verifyFormat("bool smaller = 1 < "
20078                "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
20079                "                       "
20080                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
20081                Style);
20082 
20083   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
20084   verifyFormat("SomeClass::Constructor() :\n"
20085                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
20086                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
20087                Style);
20088 }
20089 
20090 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
20091   FormatStyle Style = getLLVMStyle();
20092   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
20093   Style.ConstructorInitializerIndentWidth = 4;
20094   verifyFormat("SomeClass::Constructor()\n"
20095                "    : a(a)\n"
20096                "    , b(b)\n"
20097                "    , c(c) {}",
20098                Style);
20099   verifyFormat("SomeClass::Constructor()\n"
20100                "    : a(a) {}",
20101                Style);
20102 
20103   Style.ColumnLimit = 0;
20104   verifyFormat("SomeClass::Constructor()\n"
20105                "    : a(a) {}",
20106                Style);
20107   verifyFormat("SomeClass::Constructor() noexcept\n"
20108                "    : a(a) {}",
20109                Style);
20110   verifyFormat("SomeClass::Constructor()\n"
20111                "    : a(a)\n"
20112                "    , b(b)\n"
20113                "    , c(c) {}",
20114                Style);
20115   verifyFormat("SomeClass::Constructor()\n"
20116                "    : a(a) {\n"
20117                "  foo();\n"
20118                "  bar();\n"
20119                "}",
20120                Style);
20121 
20122   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
20123   verifyFormat("SomeClass::Constructor()\n"
20124                "    : a(a)\n"
20125                "    , b(b)\n"
20126                "    , c(c) {\n}",
20127                Style);
20128   verifyFormat("SomeClass::Constructor()\n"
20129                "    : a(a) {\n}",
20130                Style);
20131 
20132   Style.ColumnLimit = 80;
20133   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
20134   Style.ConstructorInitializerIndentWidth = 2;
20135   verifyFormat("SomeClass::Constructor()\n"
20136                "  : a(a)\n"
20137                "  , b(b)\n"
20138                "  , c(c) {}",
20139                Style);
20140 
20141   Style.ConstructorInitializerIndentWidth = 0;
20142   verifyFormat("SomeClass::Constructor()\n"
20143                ": a(a)\n"
20144                ", b(b)\n"
20145                ", c(c) {}",
20146                Style);
20147 
20148   Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
20149   Style.ConstructorInitializerIndentWidth = 4;
20150   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
20151   verifyFormat(
20152       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
20153       Style);
20154   verifyFormat(
20155       "SomeClass::Constructor()\n"
20156       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
20157       Style);
20158   Style.ConstructorInitializerIndentWidth = 4;
20159   Style.ColumnLimit = 60;
20160   verifyFormat("SomeClass::Constructor()\n"
20161                "    : aaaaaaaa(aaaaaaaa)\n"
20162                "    , aaaaaaaa(aaaaaaaa)\n"
20163                "    , aaaaaaaa(aaaaaaaa) {}",
20164                Style);
20165 }
20166 
20167 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) {
20168   FormatStyle Style = getLLVMStyle();
20169   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
20170   Style.ConstructorInitializerIndentWidth = 4;
20171   verifyFormat("SomeClass::Constructor()\n"
20172                "    : a{a}\n"
20173                "    , b{b} {}",
20174                Style);
20175   verifyFormat("SomeClass::Constructor()\n"
20176                "    : a{a}\n"
20177                "#if CONDITION\n"
20178                "    , b{b}\n"
20179                "#endif\n"
20180                "{\n}",
20181                Style);
20182   Style.ConstructorInitializerIndentWidth = 2;
20183   verifyFormat("SomeClass::Constructor()\n"
20184                "#if CONDITION\n"
20185                "  : a{a}\n"
20186                "#endif\n"
20187                "  , b{b}\n"
20188                "  , c{c} {\n}",
20189                Style);
20190   Style.ConstructorInitializerIndentWidth = 0;
20191   verifyFormat("SomeClass::Constructor()\n"
20192                ": a{a}\n"
20193                "#ifdef CONDITION\n"
20194                ", b{b}\n"
20195                "#else\n"
20196                ", c{c}\n"
20197                "#endif\n"
20198                ", d{d} {\n}",
20199                Style);
20200   Style.ConstructorInitializerIndentWidth = 4;
20201   verifyFormat("SomeClass::Constructor()\n"
20202                "    : a{a}\n"
20203                "#if WINDOWS\n"
20204                "#if DEBUG\n"
20205                "    , b{0}\n"
20206                "#else\n"
20207                "    , b{1}\n"
20208                "#endif\n"
20209                "#else\n"
20210                "#if DEBUG\n"
20211                "    , b{2}\n"
20212                "#else\n"
20213                "    , b{3}\n"
20214                "#endif\n"
20215                "#endif\n"
20216                "{\n}",
20217                Style);
20218   verifyFormat("SomeClass::Constructor()\n"
20219                "    : a{a}\n"
20220                "#if WINDOWS\n"
20221                "    , b{0}\n"
20222                "#if DEBUG\n"
20223                "    , c{0}\n"
20224                "#else\n"
20225                "    , c{1}\n"
20226                "#endif\n"
20227                "#else\n"
20228                "#if DEBUG\n"
20229                "    , c{2}\n"
20230                "#else\n"
20231                "    , c{3}\n"
20232                "#endif\n"
20233                "    , b{1}\n"
20234                "#endif\n"
20235                "{\n}",
20236                Style);
20237 }
20238 
20239 TEST_F(FormatTest, Destructors) {
20240   verifyFormat("void F(int &i) { i.~int(); }");
20241   verifyFormat("void F(int &i) { i->~int(); }");
20242 }
20243 
20244 TEST_F(FormatTest, FormatsWithWebKitStyle) {
20245   FormatStyle Style = getWebKitStyle();
20246 
20247   // Don't indent in outer namespaces.
20248   verifyFormat("namespace outer {\n"
20249                "int i;\n"
20250                "namespace inner {\n"
20251                "    int i;\n"
20252                "} // namespace inner\n"
20253                "} // namespace outer\n"
20254                "namespace other_outer {\n"
20255                "int i;\n"
20256                "}",
20257                Style);
20258 
20259   // Don't indent case labels.
20260   verifyFormat("switch (variable) {\n"
20261                "case 1:\n"
20262                "case 2:\n"
20263                "    doSomething();\n"
20264                "    break;\n"
20265                "default:\n"
20266                "    ++variable;\n"
20267                "}",
20268                Style);
20269 
20270   // Wrap before binary operators.
20271   EXPECT_EQ("void f()\n"
20272             "{\n"
20273             "    if (aaaaaaaaaaaaaaaa\n"
20274             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
20275             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
20276             "        return;\n"
20277             "}",
20278             format("void f() {\n"
20279                    "if (aaaaaaaaaaaaaaaa\n"
20280                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
20281                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
20282                    "return;\n"
20283                    "}",
20284                    Style));
20285 
20286   // Allow functions on a single line.
20287   verifyFormat("void f() { return; }", Style);
20288 
20289   // Allow empty blocks on a single line and insert a space in empty blocks.
20290   EXPECT_EQ("void f() { }", format("void f() {}", Style));
20291   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
20292   // However, don't merge non-empty short loops.
20293   EXPECT_EQ("while (true) {\n"
20294             "    continue;\n"
20295             "}",
20296             format("while (true) { continue; }", Style));
20297 
20298   // Constructor initializers are formatted one per line with the "," on the
20299   // new line.
20300   verifyFormat("Constructor()\n"
20301                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
20302                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
20303                "          aaaaaaaaaaaaaa)\n"
20304                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
20305                "{\n"
20306                "}",
20307                Style);
20308   verifyFormat("SomeClass::Constructor()\n"
20309                "    : a(a)\n"
20310                "{\n"
20311                "}",
20312                Style);
20313   EXPECT_EQ("SomeClass::Constructor()\n"
20314             "    : a(a)\n"
20315             "{\n"
20316             "}",
20317             format("SomeClass::Constructor():a(a){}", Style));
20318   verifyFormat("SomeClass::Constructor()\n"
20319                "    : a(a)\n"
20320                "    , b(b)\n"
20321                "    , c(c)\n"
20322                "{\n"
20323                "}",
20324                Style);
20325   verifyFormat("SomeClass::Constructor()\n"
20326                "    : a(a)\n"
20327                "{\n"
20328                "    foo();\n"
20329                "    bar();\n"
20330                "}",
20331                Style);
20332 
20333   // Access specifiers should be aligned left.
20334   verifyFormat("class C {\n"
20335                "public:\n"
20336                "    int i;\n"
20337                "};",
20338                Style);
20339 
20340   // Do not align comments.
20341   verifyFormat("int a; // Do not\n"
20342                "double b; // align comments.",
20343                Style);
20344 
20345   // Do not align operands.
20346   EXPECT_EQ("ASSERT(aaaa\n"
20347             "    || bbbb);",
20348             format("ASSERT ( aaaa\n||bbbb);", Style));
20349 
20350   // Accept input's line breaks.
20351   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
20352             "    || bbbbbbbbbbbbbbb) {\n"
20353             "    i++;\n"
20354             "}",
20355             format("if (aaaaaaaaaaaaaaa\n"
20356                    "|| bbbbbbbbbbbbbbb) { i++; }",
20357                    Style));
20358   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
20359             "    i++;\n"
20360             "}",
20361             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
20362 
20363   // Don't automatically break all macro definitions (llvm.org/PR17842).
20364   verifyFormat("#define aNumber 10", Style);
20365   // However, generally keep the line breaks that the user authored.
20366   EXPECT_EQ("#define aNumber \\\n"
20367             "    10",
20368             format("#define aNumber \\\n"
20369                    " 10",
20370                    Style));
20371 
20372   // Keep empty and one-element array literals on a single line.
20373   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
20374             "                                  copyItems:YES];",
20375             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
20376                    "copyItems:YES];",
20377                    Style));
20378   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
20379             "                                  copyItems:YES];",
20380             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
20381                    "             copyItems:YES];",
20382                    Style));
20383   // FIXME: This does not seem right, there should be more indentation before
20384   // the array literal's entries. Nested blocks have the same problem.
20385   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
20386             "    @\"a\",\n"
20387             "    @\"a\"\n"
20388             "]\n"
20389             "                                  copyItems:YES];",
20390             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
20391                    "     @\"a\",\n"
20392                    "     @\"a\"\n"
20393                    "     ]\n"
20394                    "       copyItems:YES];",
20395                    Style));
20396   EXPECT_EQ(
20397       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
20398       "                                  copyItems:YES];",
20399       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
20400              "   copyItems:YES];",
20401              Style));
20402 
20403   verifyFormat("[self.a b:c c:d];", Style);
20404   EXPECT_EQ("[self.a b:c\n"
20405             "        c:d];",
20406             format("[self.a b:c\n"
20407                    "c:d];",
20408                    Style));
20409 }
20410 
20411 TEST_F(FormatTest, FormatsLambdas) {
20412   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
20413   verifyFormat(
20414       "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n");
20415   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
20416   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
20417   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
20418   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
20419   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
20420   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
20421   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
20422   verifyFormat("int x = f(*+[] {});");
20423   verifyFormat("void f() {\n"
20424                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
20425                "}\n");
20426   verifyFormat("void f() {\n"
20427                "  other(x.begin(), //\n"
20428                "        x.end(),   //\n"
20429                "        [&](int, int) { return 1; });\n"
20430                "}\n");
20431   verifyFormat("void f() {\n"
20432                "  other.other.other.other.other(\n"
20433                "      x.begin(), x.end(),\n"
20434                "      [something, rather](int, int, int, int, int, int, int) { "
20435                "return 1; });\n"
20436                "}\n");
20437   verifyFormat(
20438       "void f() {\n"
20439       "  other.other.other.other.other(\n"
20440       "      x.begin(), x.end(),\n"
20441       "      [something, rather](int, int, int, int, int, int, int) {\n"
20442       "        //\n"
20443       "      });\n"
20444       "}\n");
20445   verifyFormat("SomeFunction([]() { // A cool function...\n"
20446                "  return 43;\n"
20447                "});");
20448   EXPECT_EQ("SomeFunction([]() {\n"
20449             "#define A a\n"
20450             "  return 43;\n"
20451             "});",
20452             format("SomeFunction([](){\n"
20453                    "#define A a\n"
20454                    "return 43;\n"
20455                    "});"));
20456   verifyFormat("void f() {\n"
20457                "  SomeFunction([](decltype(x), A *a) {});\n"
20458                "  SomeFunction([](typeof(x), A *a) {});\n"
20459                "  SomeFunction([](_Atomic(x), A *a) {});\n"
20460                "  SomeFunction([](__underlying_type(x), A *a) {});\n"
20461                "}");
20462   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20463                "    [](const aaaaaaaaaa &a) { return a; });");
20464   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
20465                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
20466                "});");
20467   verifyFormat("Constructor()\n"
20468                "    : Field([] { // comment\n"
20469                "        int i;\n"
20470                "      }) {}");
20471   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
20472                "  return some_parameter.size();\n"
20473                "};");
20474   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
20475                "    [](const string &s) { return s; };");
20476   verifyFormat("int i = aaaaaa ? 1 //\n"
20477                "               : [] {\n"
20478                "                   return 2; //\n"
20479                "                 }();");
20480   verifyFormat("llvm::errs() << \"number of twos is \"\n"
20481                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
20482                "                  return x == 2; // force break\n"
20483                "                });");
20484   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20485                "    [=](int iiiiiiiiiiii) {\n"
20486                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
20487                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
20488                "    });",
20489                getLLVMStyleWithColumns(60));
20490 
20491   verifyFormat("SomeFunction({[&] {\n"
20492                "                // comment\n"
20493                "              },\n"
20494                "              [&] {\n"
20495                "                // comment\n"
20496                "              }});");
20497   verifyFormat("SomeFunction({[&] {\n"
20498                "  // comment\n"
20499                "}});");
20500   verifyFormat(
20501       "virtual aaaaaaaaaaaaaaaa(\n"
20502       "    std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
20503       "    aaaaa aaaaaaaaa);");
20504 
20505   // Lambdas with return types.
20506   verifyFormat("int c = []() -> int { return 2; }();\n");
20507   verifyFormat("int c = []() -> int * { return 2; }();\n");
20508   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
20509   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
20510   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
20511   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
20512   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
20513   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
20514   verifyFormat("[a, a]() -> a<1> {};");
20515   verifyFormat("[]() -> foo<5 + 2> { return {}; };");
20516   verifyFormat("[]() -> foo<5 - 2> { return {}; };");
20517   verifyFormat("[]() -> foo<5 / 2> { return {}; };");
20518   verifyFormat("[]() -> foo<5 * 2> { return {}; };");
20519   verifyFormat("[]() -> foo<5 % 2> { return {}; };");
20520   verifyFormat("[]() -> foo<5 << 2> { return {}; };");
20521   verifyFormat("[]() -> foo<!5> { return {}; };");
20522   verifyFormat("[]() -> foo<~5> { return {}; };");
20523   verifyFormat("[]() -> foo<5 | 2> { return {}; };");
20524   verifyFormat("[]() -> foo<5 || 2> { return {}; };");
20525   verifyFormat("[]() -> foo<5 & 2> { return {}; };");
20526   verifyFormat("[]() -> foo<5 && 2> { return {}; };");
20527   verifyFormat("[]() -> foo<5 == 2> { return {}; };");
20528   verifyFormat("[]() -> foo<5 != 2> { return {}; };");
20529   verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
20530   verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
20531   verifyFormat("[]() -> foo<5 < 2> { return {}; };");
20532   verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
20533   verifyFormat("namespace bar {\n"
20534                "// broken:\n"
20535                "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
20536                "} // namespace bar");
20537   verifyFormat("namespace bar {\n"
20538                "// broken:\n"
20539                "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
20540                "} // namespace bar");
20541   verifyFormat("namespace bar {\n"
20542                "// broken:\n"
20543                "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
20544                "} // namespace bar");
20545   verifyFormat("namespace bar {\n"
20546                "// broken:\n"
20547                "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
20548                "} // namespace bar");
20549   verifyFormat("namespace bar {\n"
20550                "// broken:\n"
20551                "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
20552                "} // namespace bar");
20553   verifyFormat("namespace bar {\n"
20554                "// broken:\n"
20555                "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
20556                "} // namespace bar");
20557   verifyFormat("namespace bar {\n"
20558                "// broken:\n"
20559                "auto foo{[]() -> foo<!5> { return {}; }};\n"
20560                "} // namespace bar");
20561   verifyFormat("namespace bar {\n"
20562                "// broken:\n"
20563                "auto foo{[]() -> foo<~5> { return {}; }};\n"
20564                "} // namespace bar");
20565   verifyFormat("namespace bar {\n"
20566                "// broken:\n"
20567                "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
20568                "} // namespace bar");
20569   verifyFormat("namespace bar {\n"
20570                "// broken:\n"
20571                "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
20572                "} // namespace bar");
20573   verifyFormat("namespace bar {\n"
20574                "// broken:\n"
20575                "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
20576                "} // namespace bar");
20577   verifyFormat("namespace bar {\n"
20578                "// broken:\n"
20579                "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
20580                "} // namespace bar");
20581   verifyFormat("namespace bar {\n"
20582                "// broken:\n"
20583                "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
20584                "} // namespace bar");
20585   verifyFormat("namespace bar {\n"
20586                "// broken:\n"
20587                "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
20588                "} // namespace bar");
20589   verifyFormat("namespace bar {\n"
20590                "// broken:\n"
20591                "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
20592                "} // namespace bar");
20593   verifyFormat("namespace bar {\n"
20594                "// broken:\n"
20595                "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
20596                "} // namespace bar");
20597   verifyFormat("namespace bar {\n"
20598                "// broken:\n"
20599                "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
20600                "} // namespace bar");
20601   verifyFormat("namespace bar {\n"
20602                "// broken:\n"
20603                "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
20604                "} // namespace bar");
20605   verifyFormat("[]() -> a<1> {};");
20606   verifyFormat("[]() -> a<1> { ; };");
20607   verifyFormat("[]() -> a<1> { ; }();");
20608   verifyFormat("[a, a]() -> a<true> {};");
20609   verifyFormat("[]() -> a<true> {};");
20610   verifyFormat("[]() -> a<true> { ; };");
20611   verifyFormat("[]() -> a<true> { ; }();");
20612   verifyFormat("[a, a]() -> a<false> {};");
20613   verifyFormat("[]() -> a<false> {};");
20614   verifyFormat("[]() -> a<false> { ; };");
20615   verifyFormat("[]() -> a<false> { ; }();");
20616   verifyFormat("auto foo{[]() -> foo<false> { ; }};");
20617   verifyFormat("namespace bar {\n"
20618                "auto foo{[]() -> foo<false> { ; }};\n"
20619                "} // namespace bar");
20620   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
20621                "                   int j) -> int {\n"
20622                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
20623                "};");
20624   verifyFormat(
20625       "aaaaaaaaaaaaaaaaaaaaaa(\n"
20626       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
20627       "      return aaaaaaaaaaaaaaaaa;\n"
20628       "    });",
20629       getLLVMStyleWithColumns(70));
20630   verifyFormat("[]() //\n"
20631                "    -> int {\n"
20632                "  return 1; //\n"
20633                "};");
20634   verifyFormat("[]() -> Void<T...> {};");
20635   verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
20636   verifyFormat("SomeFunction({[]() -> int[] { return {}; }});");
20637   verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});");
20638   verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});");
20639   verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});");
20640   verifyFormat("return int{[x = x]() { return x; }()};");
20641 
20642   // Lambdas with explicit template argument lists.
20643   verifyFormat(
20644       "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n");
20645   verifyFormat("auto L = []<class T>(T) {\n"
20646                "  {\n"
20647                "    f();\n"
20648                "    g();\n"
20649                "  }\n"
20650                "};\n");
20651   verifyFormat("auto L = []<class... T>(T...) {\n"
20652                "  {\n"
20653                "    f();\n"
20654                "    g();\n"
20655                "  }\n"
20656                "};\n");
20657   verifyFormat("auto L = []<typename... T>(T...) {\n"
20658                "  {\n"
20659                "    f();\n"
20660                "    g();\n"
20661                "  }\n"
20662                "};\n");
20663   verifyFormat("auto L = []<template <typename...> class T>(T...) {\n"
20664                "  {\n"
20665                "    f();\n"
20666                "    g();\n"
20667                "  }\n"
20668                "};\n");
20669   verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n"
20670                "  {\n"
20671                "    f();\n"
20672                "    g();\n"
20673                "  }\n"
20674                "};\n");
20675 
20676   // Multiple lambdas in the same parentheses change indentation rules. These
20677   // lambdas are forced to start on new lines.
20678   verifyFormat("SomeFunction(\n"
20679                "    []() {\n"
20680                "      //\n"
20681                "    },\n"
20682                "    []() {\n"
20683                "      //\n"
20684                "    });");
20685 
20686   // A lambda passed as arg0 is always pushed to the next line.
20687   verifyFormat("SomeFunction(\n"
20688                "    [this] {\n"
20689                "      //\n"
20690                "    },\n"
20691                "    1);\n");
20692 
20693   // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
20694   // the arg0 case above.
20695   auto Style = getGoogleStyle();
20696   Style.BinPackArguments = false;
20697   verifyFormat("SomeFunction(\n"
20698                "    a,\n"
20699                "    [this] {\n"
20700                "      //\n"
20701                "    },\n"
20702                "    b);\n",
20703                Style);
20704   verifyFormat("SomeFunction(\n"
20705                "    a,\n"
20706                "    [this] {\n"
20707                "      //\n"
20708                "    },\n"
20709                "    b);\n");
20710 
20711   // A lambda with a very long line forces arg0 to be pushed out irrespective of
20712   // the BinPackArguments value (as long as the code is wide enough).
20713   verifyFormat(
20714       "something->SomeFunction(\n"
20715       "    a,\n"
20716       "    [this] {\n"
20717       "      "
20718       "D0000000000000000000000000000000000000000000000000000000000001();\n"
20719       "    },\n"
20720       "    b);\n");
20721 
20722   // A multi-line lambda is pulled up as long as the introducer fits on the
20723   // previous line and there are no further args.
20724   verifyFormat("function(1, [this, that] {\n"
20725                "  //\n"
20726                "});\n");
20727   verifyFormat("function([this, that] {\n"
20728                "  //\n"
20729                "});\n");
20730   // FIXME: this format is not ideal and we should consider forcing the first
20731   // arg onto its own line.
20732   verifyFormat("function(a, b, c, //\n"
20733                "         d, [this, that] {\n"
20734                "           //\n"
20735                "         });\n");
20736 
20737   // Multiple lambdas are treated correctly even when there is a short arg0.
20738   verifyFormat("SomeFunction(\n"
20739                "    1,\n"
20740                "    [this] {\n"
20741                "      //\n"
20742                "    },\n"
20743                "    [this] {\n"
20744                "      //\n"
20745                "    },\n"
20746                "    1);\n");
20747 
20748   // More complex introducers.
20749   verifyFormat("return [i, args...] {};");
20750 
20751   // Not lambdas.
20752   verifyFormat("constexpr char hello[]{\"hello\"};");
20753   verifyFormat("double &operator[](int i) { return 0; }\n"
20754                "int i;");
20755   verifyFormat("std::unique_ptr<int[]> foo() {}");
20756   verifyFormat("int i = a[a][a]->f();");
20757   verifyFormat("int i = (*b)[a]->f();");
20758 
20759   // Other corner cases.
20760   verifyFormat("void f() {\n"
20761                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
20762                "  );\n"
20763                "}");
20764 
20765   // Lambdas created through weird macros.
20766   verifyFormat("void f() {\n"
20767                "  MACRO((const AA &a) { return 1; });\n"
20768                "  MACRO((AA &a) { return 1; });\n"
20769                "}");
20770 
20771   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
20772                "      doo_dah();\n"
20773                "      doo_dah();\n"
20774                "    })) {\n"
20775                "}");
20776   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
20777                "                doo_dah();\n"
20778                "                doo_dah();\n"
20779                "              })) {\n"
20780                "}");
20781   verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
20782                "                doo_dah();\n"
20783                "                doo_dah();\n"
20784                "              })) {\n"
20785                "}");
20786   verifyFormat("auto lambda = []() {\n"
20787                "  int a = 2\n"
20788                "#if A\n"
20789                "          + 2\n"
20790                "#endif\n"
20791                "      ;\n"
20792                "};");
20793 
20794   // Lambdas with complex multiline introducers.
20795   verifyFormat(
20796       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20797       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
20798       "        -> ::std::unordered_set<\n"
20799       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
20800       "      //\n"
20801       "    });");
20802 
20803   FormatStyle DoNotMerge = getLLVMStyle();
20804   DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
20805   verifyFormat("auto c = []() {\n"
20806                "  return b;\n"
20807                "};",
20808                "auto c = []() { return b; };", DoNotMerge);
20809   verifyFormat("auto c = []() {\n"
20810                "};",
20811                " auto c = []() {};", DoNotMerge);
20812 
20813   FormatStyle MergeEmptyOnly = getLLVMStyle();
20814   MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
20815   verifyFormat("auto c = []() {\n"
20816                "  return b;\n"
20817                "};",
20818                "auto c = []() {\n"
20819                "  return b;\n"
20820                " };",
20821                MergeEmptyOnly);
20822   verifyFormat("auto c = []() {};",
20823                "auto c = []() {\n"
20824                "};",
20825                MergeEmptyOnly);
20826 
20827   FormatStyle MergeInline = getLLVMStyle();
20828   MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
20829   verifyFormat("auto c = []() {\n"
20830                "  return b;\n"
20831                "};",
20832                "auto c = []() { return b; };", MergeInline);
20833   verifyFormat("function([]() { return b; })", "function([]() { return b; })",
20834                MergeInline);
20835   verifyFormat("function([]() { return b; }, a)",
20836                "function([]() { return b; }, a)", MergeInline);
20837   verifyFormat("function(a, []() { return b; })",
20838                "function(a, []() { return b; })", MergeInline);
20839 
20840   // Check option "BraceWrapping.BeforeLambdaBody" and different state of
20841   // AllowShortLambdasOnASingleLine
20842   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
20843   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
20844   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
20845   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20846       FormatStyle::ShortLambdaStyle::SLS_None;
20847   verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
20848                "    []()\n"
20849                "    {\n"
20850                "      return 17;\n"
20851                "    });",
20852                LLVMWithBeforeLambdaBody);
20853   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
20854                "    []()\n"
20855                "    {\n"
20856                "    });",
20857                LLVMWithBeforeLambdaBody);
20858   verifyFormat("auto fct_SLS_None = []()\n"
20859                "{\n"
20860                "  return 17;\n"
20861                "};",
20862                LLVMWithBeforeLambdaBody);
20863   verifyFormat("TwoNestedLambdas_SLS_None(\n"
20864                "    []()\n"
20865                "    {\n"
20866                "      return Call(\n"
20867                "          []()\n"
20868                "          {\n"
20869                "            return 17;\n"
20870                "          });\n"
20871                "    });",
20872                LLVMWithBeforeLambdaBody);
20873   verifyFormat("void Fct() {\n"
20874                "  return {[]()\n"
20875                "          {\n"
20876                "            return 17;\n"
20877                "          }};\n"
20878                "}",
20879                LLVMWithBeforeLambdaBody);
20880 
20881   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20882       FormatStyle::ShortLambdaStyle::SLS_Empty;
20883   verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
20884                "    []()\n"
20885                "    {\n"
20886                "      return 17;\n"
20887                "    });",
20888                LLVMWithBeforeLambdaBody);
20889   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
20890                LLVMWithBeforeLambdaBody);
20891   verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
20892                "ongFunctionName_SLS_Empty(\n"
20893                "    []() {});",
20894                LLVMWithBeforeLambdaBody);
20895   verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
20896                "                                []()\n"
20897                "                                {\n"
20898                "                                  return 17;\n"
20899                "                                });",
20900                LLVMWithBeforeLambdaBody);
20901   verifyFormat("auto fct_SLS_Empty = []()\n"
20902                "{\n"
20903                "  return 17;\n"
20904                "};",
20905                LLVMWithBeforeLambdaBody);
20906   verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
20907                "    []()\n"
20908                "    {\n"
20909                "      return Call([]() {});\n"
20910                "    });",
20911                LLVMWithBeforeLambdaBody);
20912   verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
20913                "                           []()\n"
20914                "                           {\n"
20915                "                             return Call([]() {});\n"
20916                "                           });",
20917                LLVMWithBeforeLambdaBody);
20918   verifyFormat(
20919       "FctWithLongLineInLambda_SLS_Empty(\n"
20920       "    []()\n"
20921       "    {\n"
20922       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20923       "                               AndShouldNotBeConsiderAsInline,\n"
20924       "                               LambdaBodyMustBeBreak);\n"
20925       "    });",
20926       LLVMWithBeforeLambdaBody);
20927 
20928   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20929       FormatStyle::ShortLambdaStyle::SLS_Inline;
20930   verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
20931                LLVMWithBeforeLambdaBody);
20932   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
20933                LLVMWithBeforeLambdaBody);
20934   verifyFormat("auto fct_SLS_Inline = []()\n"
20935                "{\n"
20936                "  return 17;\n"
20937                "};",
20938                LLVMWithBeforeLambdaBody);
20939   verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
20940                "17; }); });",
20941                LLVMWithBeforeLambdaBody);
20942   verifyFormat(
20943       "FctWithLongLineInLambda_SLS_Inline(\n"
20944       "    []()\n"
20945       "    {\n"
20946       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20947       "                               AndShouldNotBeConsiderAsInline,\n"
20948       "                               LambdaBodyMustBeBreak);\n"
20949       "    });",
20950       LLVMWithBeforeLambdaBody);
20951   verifyFormat("FctWithMultipleParams_SLS_Inline("
20952                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
20953                "                                 []() { return 17; });",
20954                LLVMWithBeforeLambdaBody);
20955   verifyFormat(
20956       "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
20957       LLVMWithBeforeLambdaBody);
20958 
20959   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
20960       FormatStyle::ShortLambdaStyle::SLS_All;
20961   verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
20962                LLVMWithBeforeLambdaBody);
20963   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
20964                LLVMWithBeforeLambdaBody);
20965   verifyFormat("auto fct_SLS_All = []() { return 17; };",
20966                LLVMWithBeforeLambdaBody);
20967   verifyFormat("FctWithOneParam_SLS_All(\n"
20968                "    []()\n"
20969                "    {\n"
20970                "      // A cool function...\n"
20971                "      return 43;\n"
20972                "    });",
20973                LLVMWithBeforeLambdaBody);
20974   verifyFormat("FctWithMultipleParams_SLS_All("
20975                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
20976                "                              []() { return 17; });",
20977                LLVMWithBeforeLambdaBody);
20978   verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
20979                LLVMWithBeforeLambdaBody);
20980   verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
20981                LLVMWithBeforeLambdaBody);
20982   verifyFormat(
20983       "FctWithLongLineInLambda_SLS_All(\n"
20984       "    []()\n"
20985       "    {\n"
20986       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20987       "                               AndShouldNotBeConsiderAsInline,\n"
20988       "                               LambdaBodyMustBeBreak);\n"
20989       "    });",
20990       LLVMWithBeforeLambdaBody);
20991   verifyFormat(
20992       "auto fct_SLS_All = []()\n"
20993       "{\n"
20994       "  return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
20995       "                           AndShouldNotBeConsiderAsInline,\n"
20996       "                           LambdaBodyMustBeBreak);\n"
20997       "};",
20998       LLVMWithBeforeLambdaBody);
20999   LLVMWithBeforeLambdaBody.BinPackParameters = false;
21000   verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
21001                LLVMWithBeforeLambdaBody);
21002   verifyFormat(
21003       "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
21004       "                                FirstParam,\n"
21005       "                                SecondParam,\n"
21006       "                                ThirdParam,\n"
21007       "                                FourthParam);",
21008       LLVMWithBeforeLambdaBody);
21009   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
21010                "    []() { return "
21011                "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
21012                "    FirstParam,\n"
21013                "    SecondParam,\n"
21014                "    ThirdParam,\n"
21015                "    FourthParam);",
21016                LLVMWithBeforeLambdaBody);
21017   verifyFormat(
21018       "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
21019       "                                SecondParam,\n"
21020       "                                ThirdParam,\n"
21021       "                                FourthParam,\n"
21022       "                                []() { return SomeValueNotSoLong; });",
21023       LLVMWithBeforeLambdaBody);
21024   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
21025                "    []()\n"
21026                "    {\n"
21027                "      return "
21028                "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
21029                "eConsiderAsInline;\n"
21030                "    });",
21031                LLVMWithBeforeLambdaBody);
21032   verifyFormat(
21033       "FctWithLongLineInLambda_SLS_All(\n"
21034       "    []()\n"
21035       "    {\n"
21036       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
21037       "                               AndShouldNotBeConsiderAsInline,\n"
21038       "                               LambdaBodyMustBeBreak);\n"
21039       "    });",
21040       LLVMWithBeforeLambdaBody);
21041   verifyFormat("FctWithTwoParams_SLS_All(\n"
21042                "    []()\n"
21043                "    {\n"
21044                "      // A cool function...\n"
21045                "      return 43;\n"
21046                "    },\n"
21047                "    87);",
21048                LLVMWithBeforeLambdaBody);
21049   verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
21050                LLVMWithBeforeLambdaBody);
21051   verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
21052                LLVMWithBeforeLambdaBody);
21053   verifyFormat(
21054       "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
21055       LLVMWithBeforeLambdaBody);
21056   verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
21057                "}); }, x);",
21058                LLVMWithBeforeLambdaBody);
21059   verifyFormat("TwoNestedLambdas_SLS_All(\n"
21060                "    []()\n"
21061                "    {\n"
21062                "      // A cool function...\n"
21063                "      return Call([]() { return 17; });\n"
21064                "    });",
21065                LLVMWithBeforeLambdaBody);
21066   verifyFormat("TwoNestedLambdas_SLS_All(\n"
21067                "    []()\n"
21068                "    {\n"
21069                "      return Call(\n"
21070                "          []()\n"
21071                "          {\n"
21072                "            // A cool function...\n"
21073                "            return 17;\n"
21074                "          });\n"
21075                "    });",
21076                LLVMWithBeforeLambdaBody);
21077 
21078   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21079       FormatStyle::ShortLambdaStyle::SLS_None;
21080 
21081   verifyFormat("auto select = [this]() -> const Library::Object *\n"
21082                "{\n"
21083                "  return MyAssignment::SelectFromList(this);\n"
21084                "};\n",
21085                LLVMWithBeforeLambdaBody);
21086 
21087   verifyFormat("auto select = [this]() -> const Library::Object &\n"
21088                "{\n"
21089                "  return MyAssignment::SelectFromList(this);\n"
21090                "};\n",
21091                LLVMWithBeforeLambdaBody);
21092 
21093   verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
21094                "{\n"
21095                "  return MyAssignment::SelectFromList(this);\n"
21096                "};\n",
21097                LLVMWithBeforeLambdaBody);
21098 
21099   verifyFormat("namespace test {\n"
21100                "class Test {\n"
21101                "public:\n"
21102                "  Test() = default;\n"
21103                "};\n"
21104                "} // namespace test",
21105                LLVMWithBeforeLambdaBody);
21106 
21107   // Lambdas with different indentation styles.
21108   Style = getLLVMStyleWithColumns(100);
21109   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
21110             "  return promise.then(\n"
21111             "      [this, &someVariable, someObject = "
21112             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21113             "        return someObject.startAsyncAction().then(\n"
21114             "            [this, &someVariable](AsyncActionResult result) "
21115             "mutable { result.processMore(); });\n"
21116             "      });\n"
21117             "}\n",
21118             format("SomeResult doSomething(SomeObject promise) {\n"
21119                    "  return promise.then([this, &someVariable, someObject = "
21120                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21121                    "    return someObject.startAsyncAction().then([this, "
21122                    "&someVariable](AsyncActionResult result) mutable {\n"
21123                    "      result.processMore();\n"
21124                    "    });\n"
21125                    "  });\n"
21126                    "}\n",
21127                    Style));
21128   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
21129   verifyFormat("test() {\n"
21130                "  ([]() -> {\n"
21131                "    int b = 32;\n"
21132                "    return 3;\n"
21133                "  }).foo();\n"
21134                "}",
21135                Style);
21136   verifyFormat("test() {\n"
21137                "  []() -> {\n"
21138                "    int b = 32;\n"
21139                "    return 3;\n"
21140                "  }\n"
21141                "}",
21142                Style);
21143   verifyFormat("std::sort(v.begin(), v.end(),\n"
21144                "          [](const auto &someLongArgumentName, const auto "
21145                "&someOtherLongArgumentName) {\n"
21146                "  return someLongArgumentName.someMemberVariable < "
21147                "someOtherLongArgumentName.someMemberVariable;\n"
21148                "});",
21149                Style);
21150   verifyFormat("test() {\n"
21151                "  (\n"
21152                "      []() -> {\n"
21153                "        int b = 32;\n"
21154                "        return 3;\n"
21155                "      },\n"
21156                "      foo, bar)\n"
21157                "      .foo();\n"
21158                "}",
21159                Style);
21160   verifyFormat("test() {\n"
21161                "  ([]() -> {\n"
21162                "    int b = 32;\n"
21163                "    return 3;\n"
21164                "  })\n"
21165                "      .foo()\n"
21166                "      .bar();\n"
21167                "}",
21168                Style);
21169   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
21170             "  return promise.then(\n"
21171             "      [this, &someVariable, someObject = "
21172             "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21173             "    return someObject.startAsyncAction().then(\n"
21174             "        [this, &someVariable](AsyncActionResult result) mutable { "
21175             "result.processMore(); });\n"
21176             "  });\n"
21177             "}\n",
21178             format("SomeResult doSomething(SomeObject promise) {\n"
21179                    "  return promise.then([this, &someVariable, someObject = "
21180                    "std::mv(s)](std::vector<int> evaluated) mutable {\n"
21181                    "    return someObject.startAsyncAction().then([this, "
21182                    "&someVariable](AsyncActionResult result) mutable {\n"
21183                    "      result.processMore();\n"
21184                    "    });\n"
21185                    "  });\n"
21186                    "}\n",
21187                    Style));
21188   EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
21189             "  return promise.then([this, &someVariable] {\n"
21190             "    return someObject.startAsyncAction().then(\n"
21191             "        [this, &someVariable](AsyncActionResult result) mutable { "
21192             "result.processMore(); });\n"
21193             "  });\n"
21194             "}\n",
21195             format("SomeResult doSomething(SomeObject promise) {\n"
21196                    "  return promise.then([this, &someVariable] {\n"
21197                    "    return someObject.startAsyncAction().then([this, "
21198                    "&someVariable](AsyncActionResult result) mutable {\n"
21199                    "      result.processMore();\n"
21200                    "    });\n"
21201                    "  });\n"
21202                    "}\n",
21203                    Style));
21204   Style = getGoogleStyle();
21205   Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
21206   EXPECT_EQ("#define A                                       \\\n"
21207             "  [] {                                          \\\n"
21208             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
21209             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
21210             "      }",
21211             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
21212                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
21213                    Style));
21214   // TODO: The current formatting has a minor issue that's not worth fixing
21215   // right now whereby the closing brace is indented relative to the signature
21216   // instead of being aligned. This only happens with macros.
21217 }
21218 
21219 TEST_F(FormatTest, LambdaWithLineComments) {
21220   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
21221   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
21222   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
21223   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
21224       FormatStyle::ShortLambdaStyle::SLS_All;
21225 
21226   verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
21227   verifyFormat("auto k = []() // comment\n"
21228                "{ return; }",
21229                LLVMWithBeforeLambdaBody);
21230   verifyFormat("auto k = []() /* comment */ { return; }",
21231                LLVMWithBeforeLambdaBody);
21232   verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
21233                LLVMWithBeforeLambdaBody);
21234   verifyFormat("auto k = []() // X\n"
21235                "{ return; }",
21236                LLVMWithBeforeLambdaBody);
21237   verifyFormat(
21238       "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
21239       "{ return; }",
21240       LLVMWithBeforeLambdaBody);
21241 }
21242 
21243 TEST_F(FormatTest, EmptyLinesInLambdas) {
21244   verifyFormat("auto lambda = []() {\n"
21245                "  x(); //\n"
21246                "};",
21247                "auto lambda = []() {\n"
21248                "\n"
21249                "  x(); //\n"
21250                "\n"
21251                "};");
21252 }
21253 
21254 TEST_F(FormatTest, FormatsBlocks) {
21255   FormatStyle ShortBlocks = getLLVMStyle();
21256   ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
21257   verifyFormat("int (^Block)(int, int);", ShortBlocks);
21258   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
21259   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
21260   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
21261   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
21262   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
21263 
21264   verifyFormat("foo(^{ bar(); });", ShortBlocks);
21265   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
21266   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
21267 
21268   verifyFormat("[operation setCompletionBlock:^{\n"
21269                "  [self onOperationDone];\n"
21270                "}];");
21271   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
21272                "  [self onOperationDone];\n"
21273                "}]};");
21274   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
21275                "  f();\n"
21276                "}];");
21277   verifyFormat("int a = [operation block:^int(int *i) {\n"
21278                "  return 1;\n"
21279                "}];");
21280   verifyFormat("[myObject doSomethingWith:arg1\n"
21281                "                      aaa:^int(int *a) {\n"
21282                "                        return 1;\n"
21283                "                      }\n"
21284                "                      bbb:f(a * bbbbbbbb)];");
21285 
21286   verifyFormat("[operation setCompletionBlock:^{\n"
21287                "  [self.delegate newDataAvailable];\n"
21288                "}];",
21289                getLLVMStyleWithColumns(60));
21290   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
21291                "  NSString *path = [self sessionFilePath];\n"
21292                "  if (path) {\n"
21293                "    // ...\n"
21294                "  }\n"
21295                "});");
21296   verifyFormat("[[SessionService sharedService]\n"
21297                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
21298                "      if (window) {\n"
21299                "        [self windowDidLoad:window];\n"
21300                "      } else {\n"
21301                "        [self errorLoadingWindow];\n"
21302                "      }\n"
21303                "    }];");
21304   verifyFormat("void (^largeBlock)(void) = ^{\n"
21305                "  // ...\n"
21306                "};\n",
21307                getLLVMStyleWithColumns(40));
21308   verifyFormat("[[SessionService sharedService]\n"
21309                "    loadWindowWithCompletionBlock: //\n"
21310                "        ^(SessionWindow *window) {\n"
21311                "          if (window) {\n"
21312                "            [self windowDidLoad:window];\n"
21313                "          } else {\n"
21314                "            [self errorLoadingWindow];\n"
21315                "          }\n"
21316                "        }];",
21317                getLLVMStyleWithColumns(60));
21318   verifyFormat("[myObject doSomethingWith:arg1\n"
21319                "    firstBlock:^(Foo *a) {\n"
21320                "      // ...\n"
21321                "      int i;\n"
21322                "    }\n"
21323                "    secondBlock:^(Bar *b) {\n"
21324                "      // ...\n"
21325                "      int i;\n"
21326                "    }\n"
21327                "    thirdBlock:^Foo(Bar *b) {\n"
21328                "      // ...\n"
21329                "      int i;\n"
21330                "    }];");
21331   verifyFormat("[myObject doSomethingWith:arg1\n"
21332                "               firstBlock:-1\n"
21333                "              secondBlock:^(Bar *b) {\n"
21334                "                // ...\n"
21335                "                int i;\n"
21336                "              }];");
21337 
21338   verifyFormat("f(^{\n"
21339                "  @autoreleasepool {\n"
21340                "    if (a) {\n"
21341                "      g();\n"
21342                "    }\n"
21343                "  }\n"
21344                "});");
21345   verifyFormat("Block b = ^int *(A *a, B *b) {}");
21346   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
21347                "};");
21348 
21349   FormatStyle FourIndent = getLLVMStyle();
21350   FourIndent.ObjCBlockIndentWidth = 4;
21351   verifyFormat("[operation setCompletionBlock:^{\n"
21352                "    [self onOperationDone];\n"
21353                "}];",
21354                FourIndent);
21355 }
21356 
21357 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
21358   FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
21359 
21360   verifyFormat("[[SessionService sharedService] "
21361                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
21362                "  if (window) {\n"
21363                "    [self windowDidLoad:window];\n"
21364                "  } else {\n"
21365                "    [self errorLoadingWindow];\n"
21366                "  }\n"
21367                "}];",
21368                ZeroColumn);
21369   EXPECT_EQ("[[SessionService sharedService]\n"
21370             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
21371             "      if (window) {\n"
21372             "        [self windowDidLoad:window];\n"
21373             "      } else {\n"
21374             "        [self errorLoadingWindow];\n"
21375             "      }\n"
21376             "    }];",
21377             format("[[SessionService sharedService]\n"
21378                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
21379                    "                if (window) {\n"
21380                    "    [self windowDidLoad:window];\n"
21381                    "  } else {\n"
21382                    "    [self errorLoadingWindow];\n"
21383                    "  }\n"
21384                    "}];",
21385                    ZeroColumn));
21386   verifyFormat("[myObject doSomethingWith:arg1\n"
21387                "    firstBlock:^(Foo *a) {\n"
21388                "      // ...\n"
21389                "      int i;\n"
21390                "    }\n"
21391                "    secondBlock:^(Bar *b) {\n"
21392                "      // ...\n"
21393                "      int i;\n"
21394                "    }\n"
21395                "    thirdBlock:^Foo(Bar *b) {\n"
21396                "      // ...\n"
21397                "      int i;\n"
21398                "    }];",
21399                ZeroColumn);
21400   verifyFormat("f(^{\n"
21401                "  @autoreleasepool {\n"
21402                "    if (a) {\n"
21403                "      g();\n"
21404                "    }\n"
21405                "  }\n"
21406                "});",
21407                ZeroColumn);
21408   verifyFormat("void (^largeBlock)(void) = ^{\n"
21409                "  // ...\n"
21410                "};",
21411                ZeroColumn);
21412 
21413   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
21414   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
21415             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
21416   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
21417   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
21418             "  int i;\n"
21419             "};",
21420             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
21421 }
21422 
21423 TEST_F(FormatTest, SupportsCRLF) {
21424   EXPECT_EQ("int a;\r\n"
21425             "int b;\r\n"
21426             "int c;\r\n",
21427             format("int a;\r\n"
21428                    "  int b;\r\n"
21429                    "    int c;\r\n",
21430                    getLLVMStyle()));
21431   EXPECT_EQ("int a;\r\n"
21432             "int b;\r\n"
21433             "int c;\r\n",
21434             format("int a;\r\n"
21435                    "  int b;\n"
21436                    "    int c;\r\n",
21437                    getLLVMStyle()));
21438   EXPECT_EQ("int a;\n"
21439             "int b;\n"
21440             "int c;\n",
21441             format("int a;\r\n"
21442                    "  int b;\n"
21443                    "    int c;\n",
21444                    getLLVMStyle()));
21445   EXPECT_EQ("\"aaaaaaa \"\r\n"
21446             "\"bbbbbbb\";\r\n",
21447             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
21448   EXPECT_EQ("#define A \\\r\n"
21449             "  b;      \\\r\n"
21450             "  c;      \\\r\n"
21451             "  d;\r\n",
21452             format("#define A \\\r\n"
21453                    "  b; \\\r\n"
21454                    "  c; d; \r\n",
21455                    getGoogleStyle()));
21456 
21457   EXPECT_EQ("/*\r\n"
21458             "multi line block comments\r\n"
21459             "should not introduce\r\n"
21460             "an extra carriage return\r\n"
21461             "*/\r\n",
21462             format("/*\r\n"
21463                    "multi line block comments\r\n"
21464                    "should not introduce\r\n"
21465                    "an extra carriage return\r\n"
21466                    "*/\r\n"));
21467   EXPECT_EQ("/*\r\n"
21468             "\r\n"
21469             "*/",
21470             format("/*\r\n"
21471                    "    \r\r\r\n"
21472                    "*/"));
21473 
21474   FormatStyle style = getLLVMStyle();
21475 
21476   style.DeriveLineEnding = true;
21477   style.UseCRLF = false;
21478   EXPECT_EQ("union FooBarBazQux {\n"
21479             "  int foo;\n"
21480             "  int bar;\n"
21481             "  int baz;\n"
21482             "};",
21483             format("union FooBarBazQux {\r\n"
21484                    "  int foo;\n"
21485                    "  int bar;\r\n"
21486                    "  int baz;\n"
21487                    "};",
21488                    style));
21489   style.UseCRLF = true;
21490   EXPECT_EQ("union FooBarBazQux {\r\n"
21491             "  int foo;\r\n"
21492             "  int bar;\r\n"
21493             "  int baz;\r\n"
21494             "};",
21495             format("union FooBarBazQux {\r\n"
21496                    "  int foo;\n"
21497                    "  int bar;\r\n"
21498                    "  int baz;\n"
21499                    "};",
21500                    style));
21501 
21502   style.DeriveLineEnding = false;
21503   style.UseCRLF = false;
21504   EXPECT_EQ("union FooBarBazQux {\n"
21505             "  int foo;\n"
21506             "  int bar;\n"
21507             "  int baz;\n"
21508             "  int qux;\n"
21509             "};",
21510             format("union FooBarBazQux {\r\n"
21511                    "  int foo;\n"
21512                    "  int bar;\r\n"
21513                    "  int baz;\n"
21514                    "  int qux;\r\n"
21515                    "};",
21516                    style));
21517   style.UseCRLF = true;
21518   EXPECT_EQ("union FooBarBazQux {\r\n"
21519             "  int foo;\r\n"
21520             "  int bar;\r\n"
21521             "  int baz;\r\n"
21522             "  int qux;\r\n"
21523             "};",
21524             format("union FooBarBazQux {\r\n"
21525                    "  int foo;\n"
21526                    "  int bar;\r\n"
21527                    "  int baz;\n"
21528                    "  int qux;\n"
21529                    "};",
21530                    style));
21531 
21532   style.DeriveLineEnding = true;
21533   style.UseCRLF = false;
21534   EXPECT_EQ("union FooBarBazQux {\r\n"
21535             "  int foo;\r\n"
21536             "  int bar;\r\n"
21537             "  int baz;\r\n"
21538             "  int qux;\r\n"
21539             "};",
21540             format("union FooBarBazQux {\r\n"
21541                    "  int foo;\n"
21542                    "  int bar;\r\n"
21543                    "  int baz;\n"
21544                    "  int qux;\r\n"
21545                    "};",
21546                    style));
21547   style.UseCRLF = true;
21548   EXPECT_EQ("union FooBarBazQux {\n"
21549             "  int foo;\n"
21550             "  int bar;\n"
21551             "  int baz;\n"
21552             "  int qux;\n"
21553             "};",
21554             format("union FooBarBazQux {\r\n"
21555                    "  int foo;\n"
21556                    "  int bar;\r\n"
21557                    "  int baz;\n"
21558                    "  int qux;\n"
21559                    "};",
21560                    style));
21561 }
21562 
21563 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
21564   verifyFormat("MY_CLASS(C) {\n"
21565                "  int i;\n"
21566                "  int j;\n"
21567                "};");
21568 }
21569 
21570 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
21571   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
21572   TwoIndent.ContinuationIndentWidth = 2;
21573 
21574   EXPECT_EQ("int i =\n"
21575             "  longFunction(\n"
21576             "    arg);",
21577             format("int i = longFunction(arg);", TwoIndent));
21578 
21579   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
21580   SixIndent.ContinuationIndentWidth = 6;
21581 
21582   EXPECT_EQ("int i =\n"
21583             "      longFunction(\n"
21584             "            arg);",
21585             format("int i = longFunction(arg);", SixIndent));
21586 }
21587 
21588 TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
21589   FormatStyle Style = getLLVMStyle();
21590   verifyFormat("int Foo::getter(\n"
21591                "    //\n"
21592                ") const {\n"
21593                "  return foo;\n"
21594                "}",
21595                Style);
21596   verifyFormat("void Foo::setter(\n"
21597                "    //\n"
21598                ") {\n"
21599                "  foo = 1;\n"
21600                "}",
21601                Style);
21602 }
21603 
21604 TEST_F(FormatTest, SpacesInAngles) {
21605   FormatStyle Spaces = getLLVMStyle();
21606   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21607 
21608   verifyFormat("vector< ::std::string > x1;", Spaces);
21609   verifyFormat("Foo< int, Bar > x2;", Spaces);
21610   verifyFormat("Foo< ::int, ::Bar > x3;", Spaces);
21611 
21612   verifyFormat("static_cast< int >(arg);", Spaces);
21613   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
21614   verifyFormat("f< int, float >();", Spaces);
21615   verifyFormat("template <> g() {}", Spaces);
21616   verifyFormat("template < std::vector< int > > f() {}", Spaces);
21617   verifyFormat("std::function< void(int, int) > fct;", Spaces);
21618   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
21619                Spaces);
21620 
21621   Spaces.Standard = FormatStyle::LS_Cpp03;
21622   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21623   verifyFormat("A< A< int > >();", Spaces);
21624 
21625   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
21626   verifyFormat("A<A<int> >();", Spaces);
21627 
21628   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
21629   verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
21630                Spaces);
21631   verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
21632                Spaces);
21633 
21634   verifyFormat("A<A<int> >();", Spaces);
21635   verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces);
21636   verifyFormat("A< A< int > >();", Spaces);
21637 
21638   Spaces.Standard = FormatStyle::LS_Cpp11;
21639   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21640   verifyFormat("A< A< int > >();", Spaces);
21641 
21642   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
21643   verifyFormat("vector<::std::string> x4;", Spaces);
21644   verifyFormat("vector<int> x5;", Spaces);
21645   verifyFormat("Foo<int, Bar> x6;", Spaces);
21646   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
21647 
21648   verifyFormat("A<A<int>>();", Spaces);
21649 
21650   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
21651   verifyFormat("vector<::std::string> x4;", Spaces);
21652   verifyFormat("vector< ::std::string > x4;", Spaces);
21653   verifyFormat("vector<int> x5;", Spaces);
21654   verifyFormat("vector< int > x5;", Spaces);
21655   verifyFormat("Foo<int, Bar> x6;", Spaces);
21656   verifyFormat("Foo< int, Bar > x6;", Spaces);
21657   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
21658   verifyFormat("Foo< ::int, ::Bar > x7;", Spaces);
21659 
21660   verifyFormat("A<A<int>>();", Spaces);
21661   verifyFormat("A< A< int > >();", Spaces);
21662   verifyFormat("A<A<int > >();", Spaces);
21663   verifyFormat("A< A< int>>();", Spaces);
21664 
21665   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
21666   verifyFormat("// clang-format off\n"
21667                "foo<<<1, 1>>>();\n"
21668                "// clang-format on\n",
21669                Spaces);
21670   verifyFormat("// clang-format off\n"
21671                "foo< < <1, 1> > >();\n"
21672                "// clang-format on\n",
21673                Spaces);
21674 }
21675 
21676 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
21677   FormatStyle Style = getLLVMStyle();
21678   Style.SpaceAfterTemplateKeyword = false;
21679   verifyFormat("template<int> void foo();", Style);
21680 }
21681 
21682 TEST_F(FormatTest, TripleAngleBrackets) {
21683   verifyFormat("f<<<1, 1>>>();");
21684   verifyFormat("f<<<1, 1, 1, s>>>();");
21685   verifyFormat("f<<<a, b, c, d>>>();");
21686   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
21687   verifyFormat("f<param><<<1, 1>>>();");
21688   verifyFormat("f<1><<<1, 1>>>();");
21689   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
21690   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21691                "aaaaaaaaaaa<<<\n    1, 1>>>();");
21692   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
21693                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
21694 }
21695 
21696 TEST_F(FormatTest, MergeLessLessAtEnd) {
21697   verifyFormat("<<");
21698   EXPECT_EQ("< < <", format("\\\n<<<"));
21699   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21700                "aaallvm::outs() <<");
21701   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21702                "aaaallvm::outs()\n    <<");
21703 }
21704 
21705 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
21706   std::string code = "#if A\n"
21707                      "#if B\n"
21708                      "a.\n"
21709                      "#endif\n"
21710                      "    a = 1;\n"
21711                      "#else\n"
21712                      "#endif\n"
21713                      "#if C\n"
21714                      "#else\n"
21715                      "#endif\n";
21716   EXPECT_EQ(code, format(code));
21717 }
21718 
21719 TEST_F(FormatTest, HandleConflictMarkers) {
21720   // Git/SVN conflict markers.
21721   EXPECT_EQ("int a;\n"
21722             "void f() {\n"
21723             "  callme(some(parameter1,\n"
21724             "<<<<<<< text by the vcs\n"
21725             "              parameter2),\n"
21726             "||||||| text by the vcs\n"
21727             "              parameter2),\n"
21728             "         parameter3,\n"
21729             "======= text by the vcs\n"
21730             "              parameter2, parameter3),\n"
21731             ">>>>>>> text by the vcs\n"
21732             "         otherparameter);\n",
21733             format("int a;\n"
21734                    "void f() {\n"
21735                    "  callme(some(parameter1,\n"
21736                    "<<<<<<< text by the vcs\n"
21737                    "  parameter2),\n"
21738                    "||||||| text by the vcs\n"
21739                    "  parameter2),\n"
21740                    "  parameter3,\n"
21741                    "======= text by the vcs\n"
21742                    "  parameter2,\n"
21743                    "  parameter3),\n"
21744                    ">>>>>>> text by the vcs\n"
21745                    "  otherparameter);\n"));
21746 
21747   // Perforce markers.
21748   EXPECT_EQ("void f() {\n"
21749             "  function(\n"
21750             ">>>> text by the vcs\n"
21751             "      parameter,\n"
21752             "==== text by the vcs\n"
21753             "      parameter,\n"
21754             "==== text by the vcs\n"
21755             "      parameter,\n"
21756             "<<<< text by the vcs\n"
21757             "      parameter);\n",
21758             format("void f() {\n"
21759                    "  function(\n"
21760                    ">>>> text by the vcs\n"
21761                    "  parameter,\n"
21762                    "==== text by the vcs\n"
21763                    "  parameter,\n"
21764                    "==== text by the vcs\n"
21765                    "  parameter,\n"
21766                    "<<<< text by the vcs\n"
21767                    "  parameter);\n"));
21768 
21769   EXPECT_EQ("<<<<<<<\n"
21770             "|||||||\n"
21771             "=======\n"
21772             ">>>>>>>",
21773             format("<<<<<<<\n"
21774                    "|||||||\n"
21775                    "=======\n"
21776                    ">>>>>>>"));
21777 
21778   EXPECT_EQ("<<<<<<<\n"
21779             "|||||||\n"
21780             "int i;\n"
21781             "=======\n"
21782             ">>>>>>>",
21783             format("<<<<<<<\n"
21784                    "|||||||\n"
21785                    "int i;\n"
21786                    "=======\n"
21787                    ">>>>>>>"));
21788 
21789   // FIXME: Handle parsing of macros around conflict markers correctly:
21790   EXPECT_EQ("#define Macro \\\n"
21791             "<<<<<<<\n"
21792             "Something \\\n"
21793             "|||||||\n"
21794             "Else \\\n"
21795             "=======\n"
21796             "Other \\\n"
21797             ">>>>>>>\n"
21798             "    End int i;\n",
21799             format("#define Macro \\\n"
21800                    "<<<<<<<\n"
21801                    "  Something \\\n"
21802                    "|||||||\n"
21803                    "  Else \\\n"
21804                    "=======\n"
21805                    "  Other \\\n"
21806                    ">>>>>>>\n"
21807                    "  End\n"
21808                    "int i;\n"));
21809 
21810   verifyFormat(R"(====
21811 #ifdef A
21812 a
21813 #else
21814 b
21815 #endif
21816 )");
21817 }
21818 
21819 TEST_F(FormatTest, DisableRegions) {
21820   EXPECT_EQ("int i;\n"
21821             "// clang-format off\n"
21822             "  int j;\n"
21823             "// clang-format on\n"
21824             "int k;",
21825             format(" int  i;\n"
21826                    "   // clang-format off\n"
21827                    "  int j;\n"
21828                    " // clang-format on\n"
21829                    "   int   k;"));
21830   EXPECT_EQ("int i;\n"
21831             "/* clang-format off */\n"
21832             "  int j;\n"
21833             "/* clang-format on */\n"
21834             "int k;",
21835             format(" int  i;\n"
21836                    "   /* clang-format off */\n"
21837                    "  int j;\n"
21838                    " /* clang-format on */\n"
21839                    "   int   k;"));
21840 
21841   // Don't reflow comments within disabled regions.
21842   EXPECT_EQ("// clang-format off\n"
21843             "// long long long long long long line\n"
21844             "/* clang-format on */\n"
21845             "/* long long long\n"
21846             " * long long long\n"
21847             " * line */\n"
21848             "int i;\n"
21849             "/* clang-format off */\n"
21850             "/* long long long long long long line */\n",
21851             format("// clang-format off\n"
21852                    "// long long long long long long line\n"
21853                    "/* clang-format on */\n"
21854                    "/* long long long long long long line */\n"
21855                    "int i;\n"
21856                    "/* clang-format off */\n"
21857                    "/* long long long long long long line */\n",
21858                    getLLVMStyleWithColumns(20)));
21859 }
21860 
21861 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
21862   format("? ) =");
21863   verifyNoCrash("#define a\\\n /**/}");
21864 }
21865 
21866 TEST_F(FormatTest, FormatsTableGenCode) {
21867   FormatStyle Style = getLLVMStyle();
21868   Style.Language = FormatStyle::LK_TableGen;
21869   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
21870 }
21871 
21872 TEST_F(FormatTest, ArrayOfTemplates) {
21873   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
21874             format("auto a = new unique_ptr<int > [ 10];"));
21875 
21876   FormatStyle Spaces = getLLVMStyle();
21877   Spaces.SpacesInSquareBrackets = true;
21878   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
21879             format("auto a = new unique_ptr<int > [10];", Spaces));
21880 }
21881 
21882 TEST_F(FormatTest, ArrayAsTemplateType) {
21883   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
21884             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
21885 
21886   FormatStyle Spaces = getLLVMStyle();
21887   Spaces.SpacesInSquareBrackets = true;
21888   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
21889             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
21890 }
21891 
21892 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
21893 
21894 TEST(FormatStyle, GetStyleWithEmptyFileName) {
21895   llvm::vfs::InMemoryFileSystem FS;
21896   auto Style1 = getStyle("file", "", "Google", "", &FS);
21897   ASSERT_TRUE((bool)Style1);
21898   ASSERT_EQ(*Style1, getGoogleStyle());
21899 }
21900 
21901 TEST(FormatStyle, GetStyleOfFile) {
21902   llvm::vfs::InMemoryFileSystem FS;
21903   // Test 1: format file in the same directory.
21904   ASSERT_TRUE(
21905       FS.addFile("/a/.clang-format", 0,
21906                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
21907   ASSERT_TRUE(
21908       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
21909   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
21910   ASSERT_TRUE((bool)Style1);
21911   ASSERT_EQ(*Style1, getLLVMStyle());
21912 
21913   // Test 2.1: fallback to default.
21914   ASSERT_TRUE(
21915       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
21916   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
21917   ASSERT_TRUE((bool)Style2);
21918   ASSERT_EQ(*Style2, getMozillaStyle());
21919 
21920   // Test 2.2: no format on 'none' fallback style.
21921   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
21922   ASSERT_TRUE((bool)Style2);
21923   ASSERT_EQ(*Style2, getNoStyle());
21924 
21925   // Test 2.3: format if config is found with no based style while fallback is
21926   // 'none'.
21927   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
21928                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
21929   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
21930   ASSERT_TRUE((bool)Style2);
21931   ASSERT_EQ(*Style2, getLLVMStyle());
21932 
21933   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
21934   Style2 = getStyle("{}", "a.h", "none", "", &FS);
21935   ASSERT_TRUE((bool)Style2);
21936   ASSERT_EQ(*Style2, getLLVMStyle());
21937 
21938   // Test 3: format file in parent directory.
21939   ASSERT_TRUE(
21940       FS.addFile("/c/.clang-format", 0,
21941                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
21942   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
21943                          llvm::MemoryBuffer::getMemBuffer("int i;")));
21944   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
21945   ASSERT_TRUE((bool)Style3);
21946   ASSERT_EQ(*Style3, getGoogleStyle());
21947 
21948   // Test 4: error on invalid fallback style
21949   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
21950   ASSERT_FALSE((bool)Style4);
21951   llvm::consumeError(Style4.takeError());
21952 
21953   // Test 5: error on invalid yaml on command line
21954   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
21955   ASSERT_FALSE((bool)Style5);
21956   llvm::consumeError(Style5.takeError());
21957 
21958   // Test 6: error on invalid style
21959   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
21960   ASSERT_FALSE((bool)Style6);
21961   llvm::consumeError(Style6.takeError());
21962 
21963   // Test 7: found config file, error on parsing it
21964   ASSERT_TRUE(
21965       FS.addFile("/d/.clang-format", 0,
21966                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
21967                                                   "InvalidKey: InvalidValue")));
21968   ASSERT_TRUE(
21969       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
21970   auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
21971   ASSERT_FALSE((bool)Style7a);
21972   llvm::consumeError(Style7a.takeError());
21973 
21974   auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true);
21975   ASSERT_TRUE((bool)Style7b);
21976 
21977   // Test 8: inferred per-language defaults apply.
21978   auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS);
21979   ASSERT_TRUE((bool)StyleTd);
21980   ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen));
21981 
21982   // Test 9.1.1: overwriting a file style, when no parent file exists with no
21983   // fallback style.
21984   ASSERT_TRUE(FS.addFile(
21985       "/e/sub/.clang-format", 0,
21986       llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n"
21987                                        "ColumnLimit: 20")));
21988   ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0,
21989                          llvm::MemoryBuffer::getMemBuffer("int i;")));
21990   auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
21991   ASSERT_TRUE(static_cast<bool>(Style9));
21992   ASSERT_EQ(*Style9, [] {
21993     auto Style = getNoStyle();
21994     Style.ColumnLimit = 20;
21995     return Style;
21996   }());
21997 
21998   // Test 9.1.2: propagate more than one level with no parent file.
21999   ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0,
22000                          llvm::MemoryBuffer::getMemBuffer("int i;")));
22001   ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0,
22002                          llvm::MemoryBuffer::getMemBuffer(
22003                              "BasedOnStyle: InheritParentConfig\n"
22004                              "WhitespaceSensitiveMacros: ['FOO', 'BAR']")));
22005   std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"};
22006 
22007   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
22008   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
22009   ASSERT_TRUE(static_cast<bool>(Style9));
22010   ASSERT_EQ(*Style9, [&NonDefaultWhiteSpaceMacros] {
22011     auto Style = getNoStyle();
22012     Style.ColumnLimit = 20;
22013     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
22014     return Style;
22015   }());
22016 
22017   // Test 9.2: with LLVM fallback style
22018   Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS);
22019   ASSERT_TRUE(static_cast<bool>(Style9));
22020   ASSERT_EQ(*Style9, [] {
22021     auto Style = getLLVMStyle();
22022     Style.ColumnLimit = 20;
22023     return Style;
22024   }());
22025 
22026   // Test 9.3: with a parent file
22027   ASSERT_TRUE(
22028       FS.addFile("/e/.clang-format", 0,
22029                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n"
22030                                                   "UseTab: Always")));
22031   Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
22032   ASSERT_TRUE(static_cast<bool>(Style9));
22033   ASSERT_EQ(*Style9, [] {
22034     auto Style = getGoogleStyle();
22035     Style.ColumnLimit = 20;
22036     Style.UseTab = FormatStyle::UT_Always;
22037     return Style;
22038   }());
22039 
22040   // Test 9.4: propagate more than one level with a parent file.
22041   const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] {
22042     auto Style = getGoogleStyle();
22043     Style.ColumnLimit = 20;
22044     Style.UseTab = FormatStyle::UT_Always;
22045     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
22046     return Style;
22047   }();
22048 
22049   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
22050   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
22051   ASSERT_TRUE(static_cast<bool>(Style9));
22052   ASSERT_EQ(*Style9, SubSubStyle);
22053 
22054   // Test 9.5: use InheritParentConfig as style name
22055   Style9 =
22056       getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS);
22057   ASSERT_TRUE(static_cast<bool>(Style9));
22058   ASSERT_EQ(*Style9, SubSubStyle);
22059 
22060   // Test 9.6: use command line style with inheritance
22061   Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp",
22062                     "none", "", &FS);
22063   ASSERT_TRUE(static_cast<bool>(Style9));
22064   ASSERT_EQ(*Style9, SubSubStyle);
22065 
22066   // Test 9.7: use command line style with inheritance and own config
22067   Style9 = getStyle("{BasedOnStyle: InheritParentConfig, "
22068                     "WhitespaceSensitiveMacros: ['FOO', 'BAR']}",
22069                     "/e/sub/code.cpp", "none", "", &FS);
22070   ASSERT_TRUE(static_cast<bool>(Style9));
22071   ASSERT_EQ(*Style9, SubSubStyle);
22072 
22073   // Test 9.8: use inheritance from a file without BasedOnStyle
22074   ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0,
22075                          llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123")));
22076   ASSERT_TRUE(
22077       FS.addFile("/e/withoutbase/sub/.clang-format", 0,
22078                  llvm::MemoryBuffer::getMemBuffer(
22079                      "BasedOnStyle: InheritParentConfig\nIndentWidth: 7")));
22080   // Make sure we do not use the fallback style
22081   Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS);
22082   ASSERT_TRUE(static_cast<bool>(Style9));
22083   ASSERT_EQ(*Style9, [] {
22084     auto Style = getLLVMStyle();
22085     Style.ColumnLimit = 123;
22086     return Style;
22087   }());
22088 
22089   Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS);
22090   ASSERT_TRUE(static_cast<bool>(Style9));
22091   ASSERT_EQ(*Style9, [] {
22092     auto Style = getLLVMStyle();
22093     Style.ColumnLimit = 123;
22094     Style.IndentWidth = 7;
22095     return Style;
22096   }());
22097 
22098   // Test 9.9: use inheritance from a specific config file.
22099   Style9 = getStyle("file:/e/sub/sub/.clang-format", "/e/sub/sub/code.cpp",
22100                     "none", "", &FS);
22101   ASSERT_TRUE(static_cast<bool>(Style9));
22102   ASSERT_EQ(*Style9, SubSubStyle);
22103 }
22104 
22105 TEST(FormatStyle, GetStyleOfSpecificFile) {
22106   llvm::vfs::InMemoryFileSystem FS;
22107   // Specify absolute path to a format file in a parent directory.
22108   ASSERT_TRUE(
22109       FS.addFile("/e/.clang-format", 0,
22110                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
22111   ASSERT_TRUE(
22112       FS.addFile("/e/explicit.clang-format", 0,
22113                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
22114   ASSERT_TRUE(FS.addFile("/e/sub/sub/sub/test.cpp", 0,
22115                          llvm::MemoryBuffer::getMemBuffer("int i;")));
22116   auto Style = getStyle("file:/e/explicit.clang-format",
22117                         "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS);
22118   ASSERT_TRUE(static_cast<bool>(Style));
22119   ASSERT_EQ(*Style, getGoogleStyle());
22120 
22121   // Specify relative path to a format file.
22122   ASSERT_TRUE(
22123       FS.addFile("../../e/explicit.clang-format", 0,
22124                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
22125   Style = getStyle("file:../../e/explicit.clang-format",
22126                    "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS);
22127   ASSERT_TRUE(static_cast<bool>(Style));
22128   ASSERT_EQ(*Style, getGoogleStyle());
22129 
22130   // Specify path to a format file that does not exist.
22131   Style = getStyle("file:/e/missing.clang-format", "/e/sub/sub/sub/test.cpp",
22132                    "LLVM", "", &FS);
22133   ASSERT_FALSE(static_cast<bool>(Style));
22134   llvm::consumeError(Style.takeError());
22135 
22136   // Specify path to a file on the filesystem.
22137   SmallString<128> FormatFilePath;
22138   std::error_code ECF = llvm::sys::fs::createTemporaryFile(
22139       "FormatFileTest", "tpl", FormatFilePath);
22140   EXPECT_FALSE((bool)ECF);
22141   llvm::raw_fd_ostream FormatFileTest(FormatFilePath, ECF);
22142   EXPECT_FALSE((bool)ECF);
22143   FormatFileTest << "BasedOnStyle: Google\n";
22144   FormatFileTest.close();
22145 
22146   SmallString<128> TestFilePath;
22147   std::error_code ECT =
22148       llvm::sys::fs::createTemporaryFile("CodeFileTest", "cc", TestFilePath);
22149   EXPECT_FALSE((bool)ECT);
22150   llvm::raw_fd_ostream CodeFileTest(TestFilePath, ECT);
22151   CodeFileTest << "int i;\n";
22152   CodeFileTest.close();
22153 
22154   std::string format_file_arg = std::string("file:") + FormatFilePath.c_str();
22155   Style = getStyle(format_file_arg, TestFilePath, "LLVM", "", nullptr);
22156 
22157   llvm::sys::fs::remove(FormatFilePath.c_str());
22158   llvm::sys::fs::remove(TestFilePath.c_str());
22159   ASSERT_TRUE(static_cast<bool>(Style));
22160   ASSERT_EQ(*Style, getGoogleStyle());
22161 }
22162 
22163 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
22164   // Column limit is 20.
22165   std::string Code = "Type *a =\n"
22166                      "    new Type();\n"
22167                      "g(iiiii, 0, jjjjj,\n"
22168                      "  0, kkkkk, 0, mm);\n"
22169                      "int  bad     = format   ;";
22170   std::string Expected = "auto a = new Type();\n"
22171                          "g(iiiii, nullptr,\n"
22172                          "  jjjjj, nullptr,\n"
22173                          "  kkkkk, nullptr,\n"
22174                          "  mm);\n"
22175                          "int  bad     = format   ;";
22176   FileID ID = Context.createInMemoryFile("format.cpp", Code);
22177   tooling::Replacements Replaces = toReplacements(
22178       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
22179                             "auto "),
22180        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
22181                             "nullptr"),
22182        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
22183                             "nullptr"),
22184        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
22185                             "nullptr")});
22186 
22187   FormatStyle Style = getLLVMStyle();
22188   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
22189   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
22190   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
22191       << llvm::toString(FormattedReplaces.takeError()) << "\n";
22192   auto Result = applyAllReplacements(Code, *FormattedReplaces);
22193   EXPECT_TRUE(static_cast<bool>(Result));
22194   EXPECT_EQ(Expected, *Result);
22195 }
22196 
22197 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
22198   std::string Code = "#include \"a.h\"\n"
22199                      "#include \"c.h\"\n"
22200                      "\n"
22201                      "int main() {\n"
22202                      "  return 0;\n"
22203                      "}";
22204   std::string Expected = "#include \"a.h\"\n"
22205                          "#include \"b.h\"\n"
22206                          "#include \"c.h\"\n"
22207                          "\n"
22208                          "int main() {\n"
22209                          "  return 0;\n"
22210                          "}";
22211   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
22212   tooling::Replacements Replaces = toReplacements(
22213       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
22214                             "#include \"b.h\"\n")});
22215 
22216   FormatStyle Style = getLLVMStyle();
22217   Style.SortIncludes = FormatStyle::SI_CaseSensitive;
22218   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
22219   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
22220       << llvm::toString(FormattedReplaces.takeError()) << "\n";
22221   auto Result = applyAllReplacements(Code, *FormattedReplaces);
22222   EXPECT_TRUE(static_cast<bool>(Result));
22223   EXPECT_EQ(Expected, *Result);
22224 }
22225 
22226 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
22227   EXPECT_EQ("using std::cin;\n"
22228             "using std::cout;",
22229             format("using std::cout;\n"
22230                    "using std::cin;",
22231                    getGoogleStyle()));
22232 }
22233 
22234 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
22235   FormatStyle Style = getLLVMStyle();
22236   Style.Standard = FormatStyle::LS_Cpp03;
22237   // cpp03 recognize this string as identifier u8 and literal character 'a'
22238   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
22239 }
22240 
22241 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
22242   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
22243   // all modes, including C++11, C++14 and C++17
22244   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
22245 }
22246 
22247 TEST_F(FormatTest, DoNotFormatLikelyXml) {
22248   EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle()));
22249   EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle()));
22250 }
22251 
22252 TEST_F(FormatTest, StructuredBindings) {
22253   // Structured bindings is a C++17 feature.
22254   // all modes, including C++11, C++14 and C++17
22255   verifyFormat("auto [a, b] = f();");
22256   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
22257   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
22258   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
22259   EXPECT_EQ("auto const volatile [a, b] = f();",
22260             format("auto  const   volatile[a, b] = f();"));
22261   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
22262   EXPECT_EQ("auto &[a, b, c] = f();",
22263             format("auto   &[  a  ,  b,c   ] = f();"));
22264   EXPECT_EQ("auto &&[a, b, c] = f();",
22265             format("auto   &&[  a  ,  b,c   ] = f();"));
22266   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
22267   EXPECT_EQ("auto const volatile &&[a, b] = f();",
22268             format("auto  const  volatile  &&[a, b] = f();"));
22269   EXPECT_EQ("auto const &&[a, b] = f();",
22270             format("auto  const   &&  [a, b] = f();"));
22271   EXPECT_EQ("const auto &[a, b] = f();",
22272             format("const  auto  &  [a, b] = f();"));
22273   EXPECT_EQ("const auto volatile &&[a, b] = f();",
22274             format("const  auto   volatile  &&[a, b] = f();"));
22275   EXPECT_EQ("volatile const auto &&[a, b] = f();",
22276             format("volatile  const  auto   &&[a, b] = f();"));
22277   EXPECT_EQ("const auto &&[a, b] = f();",
22278             format("const  auto  &&  [a, b] = f();"));
22279 
22280   // Make sure we don't mistake structured bindings for lambdas.
22281   FormatStyle PointerMiddle = getLLVMStyle();
22282   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
22283   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
22284   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
22285   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
22286   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
22287   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
22288   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
22289   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
22290   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
22291   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
22292   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
22293   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
22294   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
22295 
22296   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
22297             format("for (const auto   &&   [a, b] : some_range) {\n}"));
22298   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
22299             format("for (const auto   &   [a, b] : some_range) {\n}"));
22300   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
22301             format("for (const auto[a, b] : some_range) {\n}"));
22302   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
22303   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
22304   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
22305   EXPECT_EQ("auto const &[x, y](expr);",
22306             format("auto  const  &  [x,y]  (expr);"));
22307   EXPECT_EQ("auto const &&[x, y](expr);",
22308             format("auto  const  &&  [x,y]  (expr);"));
22309   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
22310   EXPECT_EQ("auto const &[x, y]{expr};",
22311             format("auto  const  &  [x,y]  {expr};"));
22312   EXPECT_EQ("auto const &&[x, y]{expr};",
22313             format("auto  const  &&  [x,y]  {expr};"));
22314 
22315   FormatStyle Spaces = getLLVMStyle();
22316   Spaces.SpacesInSquareBrackets = true;
22317   verifyFormat("auto [ a, b ] = f();", Spaces);
22318   verifyFormat("auto &&[ a, b ] = f();", Spaces);
22319   verifyFormat("auto &[ a, b ] = f();", Spaces);
22320   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
22321   verifyFormat("auto const &[ a, b ] = f();", Spaces);
22322 }
22323 
22324 TEST_F(FormatTest, FileAndCode) {
22325   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
22326   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
22327   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
22328   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
22329   EXPECT_EQ(FormatStyle::LK_ObjC,
22330             guessLanguage("foo.h", "@interface Foo\n@end\n"));
22331   EXPECT_EQ(
22332       FormatStyle::LK_ObjC,
22333       guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
22334   EXPECT_EQ(FormatStyle::LK_ObjC,
22335             guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
22336   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
22337   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
22338   EXPECT_EQ(FormatStyle::LK_ObjC,
22339             guessLanguage("foo", "@interface Foo\n@end\n"));
22340   EXPECT_EQ(FormatStyle::LK_ObjC,
22341             guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
22342   EXPECT_EQ(
22343       FormatStyle::LK_ObjC,
22344       guessLanguage("foo.h",
22345                     "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
22346   EXPECT_EQ(
22347       FormatStyle::LK_Cpp,
22348       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
22349 }
22350 
22351 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
22352   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
22353   EXPECT_EQ(FormatStyle::LK_ObjC,
22354             guessLanguage("foo.h", "array[[calculator getIndex]];"));
22355   EXPECT_EQ(FormatStyle::LK_Cpp,
22356             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
22357   EXPECT_EQ(
22358       FormatStyle::LK_Cpp,
22359       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
22360   EXPECT_EQ(FormatStyle::LK_ObjC,
22361             guessLanguage("foo.h", "[[noreturn foo] bar];"));
22362   EXPECT_EQ(FormatStyle::LK_Cpp,
22363             guessLanguage("foo.h", "[[clang::fallthrough]];"));
22364   EXPECT_EQ(FormatStyle::LK_ObjC,
22365             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
22366   EXPECT_EQ(FormatStyle::LK_Cpp,
22367             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
22368   EXPECT_EQ(FormatStyle::LK_Cpp,
22369             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
22370   EXPECT_EQ(FormatStyle::LK_ObjC,
22371             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
22372   EXPECT_EQ(FormatStyle::LK_Cpp,
22373             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
22374   EXPECT_EQ(
22375       FormatStyle::LK_Cpp,
22376       guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
22377   EXPECT_EQ(
22378       FormatStyle::LK_Cpp,
22379       guessLanguage("foo.h",
22380                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
22381   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
22382 }
22383 
22384 TEST_F(FormatTest, GuessLanguageWithCaret) {
22385   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
22386   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
22387   EXPECT_EQ(FormatStyle::LK_ObjC,
22388             guessLanguage("foo.h", "int(^)(char, float);"));
22389   EXPECT_EQ(FormatStyle::LK_ObjC,
22390             guessLanguage("foo.h", "int(^foo)(char, float);"));
22391   EXPECT_EQ(FormatStyle::LK_ObjC,
22392             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
22393   EXPECT_EQ(FormatStyle::LK_ObjC,
22394             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
22395   EXPECT_EQ(
22396       FormatStyle::LK_ObjC,
22397       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
22398 }
22399 
22400 TEST_F(FormatTest, GuessLanguageWithPragmas) {
22401   EXPECT_EQ(FormatStyle::LK_Cpp,
22402             guessLanguage("foo.h", "__pragma(warning(disable:))"));
22403   EXPECT_EQ(FormatStyle::LK_Cpp,
22404             guessLanguage("foo.h", "#pragma(warning(disable:))"));
22405   EXPECT_EQ(FormatStyle::LK_Cpp,
22406             guessLanguage("foo.h", "_Pragma(warning(disable:))"));
22407 }
22408 
22409 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
22410   // ASM symbolic names are identifiers that must be surrounded by [] without
22411   // space in between:
22412   // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
22413 
22414   // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
22415   verifyFormat(R"(//
22416 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
22417 )");
22418 
22419   // A list of several ASM symbolic names.
22420   verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
22421 
22422   // ASM symbolic names in inline ASM with inputs and outputs.
22423   verifyFormat(R"(//
22424 asm("cmoveq %1, %2, %[result]"
22425     : [result] "=r"(result)
22426     : "r"(test), "r"(new), "[result]"(old));
22427 )");
22428 
22429   // ASM symbolic names in inline ASM with no outputs.
22430   verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
22431 }
22432 
22433 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
22434   EXPECT_EQ(FormatStyle::LK_Cpp,
22435             guessLanguage("foo.h", "void f() {\n"
22436                                    "  asm (\"mov %[e], %[d]\"\n"
22437                                    "     : [d] \"=rm\" (d)\n"
22438                                    "       [e] \"rm\" (*e));\n"
22439                                    "}"));
22440   EXPECT_EQ(FormatStyle::LK_Cpp,
22441             guessLanguage("foo.h", "void f() {\n"
22442                                    "  _asm (\"mov %[e], %[d]\"\n"
22443                                    "     : [d] \"=rm\" (d)\n"
22444                                    "       [e] \"rm\" (*e));\n"
22445                                    "}"));
22446   EXPECT_EQ(FormatStyle::LK_Cpp,
22447             guessLanguage("foo.h", "void f() {\n"
22448                                    "  __asm (\"mov %[e], %[d]\"\n"
22449                                    "     : [d] \"=rm\" (d)\n"
22450                                    "       [e] \"rm\" (*e));\n"
22451                                    "}"));
22452   EXPECT_EQ(FormatStyle::LK_Cpp,
22453             guessLanguage("foo.h", "void f() {\n"
22454                                    "  __asm__ (\"mov %[e], %[d]\"\n"
22455                                    "     : [d] \"=rm\" (d)\n"
22456                                    "       [e] \"rm\" (*e));\n"
22457                                    "}"));
22458   EXPECT_EQ(FormatStyle::LK_Cpp,
22459             guessLanguage("foo.h", "void f() {\n"
22460                                    "  asm (\"mov %[e], %[d]\"\n"
22461                                    "     : [d] \"=rm\" (d),\n"
22462                                    "       [e] \"rm\" (*e));\n"
22463                                    "}"));
22464   EXPECT_EQ(FormatStyle::LK_Cpp,
22465             guessLanguage("foo.h", "void f() {\n"
22466                                    "  asm volatile (\"mov %[e], %[d]\"\n"
22467                                    "     : [d] \"=rm\" (d)\n"
22468                                    "       [e] \"rm\" (*e));\n"
22469                                    "}"));
22470 }
22471 
22472 TEST_F(FormatTest, GuessLanguageWithChildLines) {
22473   EXPECT_EQ(FormatStyle::LK_Cpp,
22474             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
22475   EXPECT_EQ(FormatStyle::LK_ObjC,
22476             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
22477   EXPECT_EQ(
22478       FormatStyle::LK_Cpp,
22479       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
22480   EXPECT_EQ(
22481       FormatStyle::LK_ObjC,
22482       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
22483 }
22484 
22485 TEST_F(FormatTest, TypenameMacros) {
22486   std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
22487 
22488   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
22489   FormatStyle Google = getGoogleStyleWithColumns(0);
22490   Google.TypenameMacros = TypenameMacros;
22491   verifyFormat("struct foo {\n"
22492                "  int bar;\n"
22493                "  TAILQ_ENTRY(a) bleh;\n"
22494                "};",
22495                Google);
22496 
22497   FormatStyle Macros = getLLVMStyle();
22498   Macros.TypenameMacros = TypenameMacros;
22499 
22500   verifyFormat("STACK_OF(int) a;", Macros);
22501   verifyFormat("STACK_OF(int) *a;", Macros);
22502   verifyFormat("STACK_OF(int const *) *a;", Macros);
22503   verifyFormat("STACK_OF(int *const) *a;", Macros);
22504   verifyFormat("STACK_OF(int, string) a;", Macros);
22505   verifyFormat("STACK_OF(LIST(int)) a;", Macros);
22506   verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
22507   verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
22508   verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
22509   verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros);
22510   verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros);
22511 
22512   Macros.PointerAlignment = FormatStyle::PAS_Left;
22513   verifyFormat("STACK_OF(int)* a;", Macros);
22514   verifyFormat("STACK_OF(int*)* a;", Macros);
22515   verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
22516   verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
22517   verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros);
22518 }
22519 
22520 TEST_F(FormatTest, AtomicQualifier) {
22521   // Check that we treate _Atomic as a type and not a function call
22522   FormatStyle Google = getGoogleStyleWithColumns(0);
22523   verifyFormat("struct foo {\n"
22524                "  int a1;\n"
22525                "  _Atomic(a) a2;\n"
22526                "  _Atomic(_Atomic(int) *const) a3;\n"
22527                "};",
22528                Google);
22529   verifyFormat("_Atomic(uint64_t) a;");
22530   verifyFormat("_Atomic(uint64_t) *a;");
22531   verifyFormat("_Atomic(uint64_t const *) *a;");
22532   verifyFormat("_Atomic(uint64_t *const) *a;");
22533   verifyFormat("_Atomic(const uint64_t *) *a;");
22534   verifyFormat("_Atomic(uint64_t) a;");
22535   verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
22536   verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
22537   verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
22538   verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
22539 
22540   verifyFormat("_Atomic(uint64_t) *s(InitValue);");
22541   verifyFormat("_Atomic(uint64_t) *s{InitValue};");
22542   FormatStyle Style = getLLVMStyle();
22543   Style.PointerAlignment = FormatStyle::PAS_Left;
22544   verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style);
22545   verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style);
22546   verifyFormat("_Atomic(int)* a;", Style);
22547   verifyFormat("_Atomic(int*)* a;", Style);
22548   verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
22549 
22550   Style.SpacesInCStyleCastParentheses = true;
22551   Style.SpacesInParentheses = false;
22552   verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
22553   Style.SpacesInCStyleCastParentheses = false;
22554   Style.SpacesInParentheses = true;
22555   verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
22556   verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
22557 }
22558 
22559 TEST_F(FormatTest, AmbersandInLamda) {
22560   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
22561   FormatStyle AlignStyle = getLLVMStyle();
22562   AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
22563   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
22564   AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
22565   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
22566 }
22567 
22568 TEST_F(FormatTest, SpacesInConditionalStatement) {
22569   FormatStyle Spaces = getLLVMStyle();
22570   Spaces.IfMacros.clear();
22571   Spaces.IfMacros.push_back("MYIF");
22572   Spaces.SpacesInConditionalStatement = true;
22573   verifyFormat("for ( int i = 0; i; i++ )\n  continue;", Spaces);
22574   verifyFormat("if ( !a )\n  return;", Spaces);
22575   verifyFormat("if ( a )\n  return;", Spaces);
22576   verifyFormat("if constexpr ( a )\n  return;", Spaces);
22577   verifyFormat("MYIF ( a )\n  return;", Spaces);
22578   verifyFormat("MYIF ( a )\n  return;\nelse MYIF ( b )\n  return;", Spaces);
22579   verifyFormat("MYIF ( a )\n  return;\nelse\n  return;", Spaces);
22580   verifyFormat("switch ( a )\ncase 1:\n  return;", Spaces);
22581   verifyFormat("while ( a )\n  return;", Spaces);
22582   verifyFormat("while ( (a && b) )\n  return;", Spaces);
22583   verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
22584   verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
22585   // Check that space on the left of "::" is inserted as expected at beginning
22586   // of condition.
22587   verifyFormat("while ( ::func() )\n  return;", Spaces);
22588 
22589   // Check impact of ControlStatementsExceptControlMacros is honored.
22590   Spaces.SpaceBeforeParens =
22591       FormatStyle::SBPO_ControlStatementsExceptControlMacros;
22592   verifyFormat("MYIF( a )\n  return;", Spaces);
22593   verifyFormat("MYIF( a )\n  return;\nelse MYIF( b )\n  return;", Spaces);
22594   verifyFormat("MYIF( a )\n  return;\nelse\n  return;", Spaces);
22595 }
22596 
22597 TEST_F(FormatTest, AlternativeOperators) {
22598   // Test case for ensuring alternate operators are not
22599   // combined with their right most neighbour.
22600   verifyFormat("int a and b;");
22601   verifyFormat("int a and_eq b;");
22602   verifyFormat("int a bitand b;");
22603   verifyFormat("int a bitor b;");
22604   verifyFormat("int a compl b;");
22605   verifyFormat("int a not b;");
22606   verifyFormat("int a not_eq b;");
22607   verifyFormat("int a or b;");
22608   verifyFormat("int a xor b;");
22609   verifyFormat("int a xor_eq b;");
22610   verifyFormat("return this not_eq bitand other;");
22611   verifyFormat("bool operator not_eq(const X bitand other)");
22612 
22613   verifyFormat("int a and 5;");
22614   verifyFormat("int a and_eq 5;");
22615   verifyFormat("int a bitand 5;");
22616   verifyFormat("int a bitor 5;");
22617   verifyFormat("int a compl 5;");
22618   verifyFormat("int a not 5;");
22619   verifyFormat("int a not_eq 5;");
22620   verifyFormat("int a or 5;");
22621   verifyFormat("int a xor 5;");
22622   verifyFormat("int a xor_eq 5;");
22623 
22624   verifyFormat("int a compl(5);");
22625   verifyFormat("int a not(5);");
22626 
22627   /* FIXME handle alternate tokens
22628    * https://en.cppreference.com/w/cpp/language/operator_alternative
22629   // alternative tokens
22630   verifyFormat("compl foo();");     //  ~foo();
22631   verifyFormat("foo() <%%>;");      // foo();
22632   verifyFormat("void foo() <%%>;"); // void foo(){}
22633   verifyFormat("int a <:1:>;");     // int a[1];[
22634   verifyFormat("%:define ABC abc"); // #define ABC abc
22635   verifyFormat("%:%:");             // ##
22636   */
22637 }
22638 
22639 TEST_F(FormatTest, STLWhileNotDefineChed) {
22640   verifyFormat("#if defined(while)\n"
22641                "#define while EMIT WARNING C4005\n"
22642                "#endif // while");
22643 }
22644 
22645 TEST_F(FormatTest, OperatorSpacing) {
22646   FormatStyle Style = getLLVMStyle();
22647   Style.PointerAlignment = FormatStyle::PAS_Right;
22648   verifyFormat("Foo::operator*();", Style);
22649   verifyFormat("Foo::operator void *();", Style);
22650   verifyFormat("Foo::operator void **();", Style);
22651   verifyFormat("Foo::operator void *&();", Style);
22652   verifyFormat("Foo::operator void *&&();", Style);
22653   verifyFormat("Foo::operator void const *();", Style);
22654   verifyFormat("Foo::operator void const **();", Style);
22655   verifyFormat("Foo::operator void const *&();", Style);
22656   verifyFormat("Foo::operator void const *&&();", Style);
22657   verifyFormat("Foo::operator()(void *);", Style);
22658   verifyFormat("Foo::operator*(void *);", Style);
22659   verifyFormat("Foo::operator*();", Style);
22660   verifyFormat("Foo::operator**();", Style);
22661   verifyFormat("Foo::operator&();", Style);
22662   verifyFormat("Foo::operator<int> *();", Style);
22663   verifyFormat("Foo::operator<Foo> *();", Style);
22664   verifyFormat("Foo::operator<int> **();", Style);
22665   verifyFormat("Foo::operator<Foo> **();", Style);
22666   verifyFormat("Foo::operator<int> &();", Style);
22667   verifyFormat("Foo::operator<Foo> &();", Style);
22668   verifyFormat("Foo::operator<int> &&();", Style);
22669   verifyFormat("Foo::operator<Foo> &&();", Style);
22670   verifyFormat("Foo::operator<int> *&();", Style);
22671   verifyFormat("Foo::operator<Foo> *&();", Style);
22672   verifyFormat("Foo::operator<int> *&&();", Style);
22673   verifyFormat("Foo::operator<Foo> *&&();", Style);
22674   verifyFormat("operator*(int (*)(), class Foo);", Style);
22675 
22676   verifyFormat("Foo::operator&();", Style);
22677   verifyFormat("Foo::operator void &();", Style);
22678   verifyFormat("Foo::operator void const &();", Style);
22679   verifyFormat("Foo::operator()(void &);", Style);
22680   verifyFormat("Foo::operator&(void &);", Style);
22681   verifyFormat("Foo::operator&();", Style);
22682   verifyFormat("operator&(int (&)(), class Foo);", Style);
22683   verifyFormat("operator&&(int (&)(), class Foo);", Style);
22684 
22685   verifyFormat("Foo::operator&&();", Style);
22686   verifyFormat("Foo::operator**();", Style);
22687   verifyFormat("Foo::operator void &&();", Style);
22688   verifyFormat("Foo::operator void const &&();", Style);
22689   verifyFormat("Foo::operator()(void &&);", Style);
22690   verifyFormat("Foo::operator&&(void &&);", Style);
22691   verifyFormat("Foo::operator&&();", Style);
22692   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22693   verifyFormat("operator const nsTArrayRight<E> &()", Style);
22694   verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
22695                Style);
22696   verifyFormat("operator void **()", Style);
22697   verifyFormat("operator const FooRight<Object> &()", Style);
22698   verifyFormat("operator const FooRight<Object> *()", Style);
22699   verifyFormat("operator const FooRight<Object> **()", Style);
22700   verifyFormat("operator const FooRight<Object> *&()", Style);
22701   verifyFormat("operator const FooRight<Object> *&&()", Style);
22702 
22703   Style.PointerAlignment = FormatStyle::PAS_Left;
22704   verifyFormat("Foo::operator*();", Style);
22705   verifyFormat("Foo::operator**();", Style);
22706   verifyFormat("Foo::operator void*();", Style);
22707   verifyFormat("Foo::operator void**();", Style);
22708   verifyFormat("Foo::operator void*&();", Style);
22709   verifyFormat("Foo::operator void*&&();", Style);
22710   verifyFormat("Foo::operator void const*();", Style);
22711   verifyFormat("Foo::operator void const**();", Style);
22712   verifyFormat("Foo::operator void const*&();", Style);
22713   verifyFormat("Foo::operator void const*&&();", Style);
22714   verifyFormat("Foo::operator/*comment*/ void*();", Style);
22715   verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
22716   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
22717   verifyFormat("Foo::operator()(void*);", Style);
22718   verifyFormat("Foo::operator*(void*);", Style);
22719   verifyFormat("Foo::operator*();", Style);
22720   verifyFormat("Foo::operator<int>*();", Style);
22721   verifyFormat("Foo::operator<Foo>*();", Style);
22722   verifyFormat("Foo::operator<int>**();", Style);
22723   verifyFormat("Foo::operator<Foo>**();", Style);
22724   verifyFormat("Foo::operator<Foo>*&();", Style);
22725   verifyFormat("Foo::operator<int>&();", Style);
22726   verifyFormat("Foo::operator<Foo>&();", Style);
22727   verifyFormat("Foo::operator<int>&&();", Style);
22728   verifyFormat("Foo::operator<Foo>&&();", Style);
22729   verifyFormat("Foo::operator<int>*&();", Style);
22730   verifyFormat("Foo::operator<Foo>*&();", Style);
22731   verifyFormat("operator*(int (*)(), class Foo);", Style);
22732 
22733   verifyFormat("Foo::operator&();", Style);
22734   verifyFormat("Foo::operator void&();", Style);
22735   verifyFormat("Foo::operator void const&();", Style);
22736   verifyFormat("Foo::operator/*comment*/ void&();", Style);
22737   verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
22738   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
22739   verifyFormat("Foo::operator()(void&);", Style);
22740   verifyFormat("Foo::operator&(void&);", Style);
22741   verifyFormat("Foo::operator&();", Style);
22742   verifyFormat("operator&(int (&)(), class Foo);", Style);
22743   verifyFormat("operator&(int (&&)(), class Foo);", Style);
22744   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22745 
22746   verifyFormat("Foo::operator&&();", Style);
22747   verifyFormat("Foo::operator void&&();", Style);
22748   verifyFormat("Foo::operator void const&&();", Style);
22749   verifyFormat("Foo::operator/*comment*/ void&&();", Style);
22750   verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
22751   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
22752   verifyFormat("Foo::operator()(void&&);", Style);
22753   verifyFormat("Foo::operator&&(void&&);", Style);
22754   verifyFormat("Foo::operator&&();", Style);
22755   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22756   verifyFormat("operator const nsTArrayLeft<E>&()", Style);
22757   verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
22758                Style);
22759   verifyFormat("operator void**()", Style);
22760   verifyFormat("operator const FooLeft<Object>&()", Style);
22761   verifyFormat("operator const FooLeft<Object>*()", Style);
22762   verifyFormat("operator const FooLeft<Object>**()", Style);
22763   verifyFormat("operator const FooLeft<Object>*&()", Style);
22764   verifyFormat("operator const FooLeft<Object>*&&()", Style);
22765 
22766   // PR45107
22767   verifyFormat("operator Vector<String>&();", Style);
22768   verifyFormat("operator const Vector<String>&();", Style);
22769   verifyFormat("operator foo::Bar*();", Style);
22770   verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
22771   verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
22772                Style);
22773 
22774   Style.PointerAlignment = FormatStyle::PAS_Middle;
22775   verifyFormat("Foo::operator*();", Style);
22776   verifyFormat("Foo::operator void *();", Style);
22777   verifyFormat("Foo::operator()(void *);", Style);
22778   verifyFormat("Foo::operator*(void *);", Style);
22779   verifyFormat("Foo::operator*();", Style);
22780   verifyFormat("operator*(int (*)(), class Foo);", Style);
22781 
22782   verifyFormat("Foo::operator&();", Style);
22783   verifyFormat("Foo::operator void &();", Style);
22784   verifyFormat("Foo::operator void const &();", Style);
22785   verifyFormat("Foo::operator()(void &);", Style);
22786   verifyFormat("Foo::operator&(void &);", Style);
22787   verifyFormat("Foo::operator&();", Style);
22788   verifyFormat("operator&(int (&)(), class Foo);", Style);
22789 
22790   verifyFormat("Foo::operator&&();", Style);
22791   verifyFormat("Foo::operator void &&();", Style);
22792   verifyFormat("Foo::operator void const &&();", Style);
22793   verifyFormat("Foo::operator()(void &&);", Style);
22794   verifyFormat("Foo::operator&&(void &&);", Style);
22795   verifyFormat("Foo::operator&&();", Style);
22796   verifyFormat("operator&&(int (&&)(), class Foo);", Style);
22797 }
22798 
22799 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
22800   FormatStyle Style = getLLVMStyle();
22801   // PR46157
22802   verifyFormat("foo(operator+, -42);", Style);
22803   verifyFormat("foo(operator++, -42);", Style);
22804   verifyFormat("foo(operator--, -42);", Style);
22805   verifyFormat("foo(-42, operator--);", Style);
22806   verifyFormat("foo(-42, operator, );", Style);
22807   verifyFormat("foo(operator, , -42);", Style);
22808 }
22809 
22810 TEST_F(FormatTest, WhitespaceSensitiveMacros) {
22811   FormatStyle Style = getLLVMStyle();
22812   Style.WhitespaceSensitiveMacros.push_back("FOO");
22813 
22814   // Don't use the helpers here, since 'mess up' will change the whitespace
22815   // and these are all whitespace sensitive by definition
22816   EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);",
22817             format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style));
22818   EXPECT_EQ(
22819       "FOO(String-ized&Messy+But\\(: :Still)=Intentional);",
22820       format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style));
22821   EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);",
22822             format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style));
22823   EXPECT_EQ("FOO(String-ized&Messy+But,: :\n"
22824             "       Still=Intentional);",
22825             format("FOO(String-ized&Messy+But,: :\n"
22826                    "       Still=Intentional);",
22827                    Style));
22828   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
22829   EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n"
22830             "       Still=Intentional);",
22831             format("FOO(String-ized=&Messy+But,: :\n"
22832                    "       Still=Intentional);",
22833                    Style));
22834 
22835   Style.ColumnLimit = 21;
22836   EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);",
22837             format("FOO(String-ized&Messy+But: :Still=Intentional);", Style));
22838 }
22839 
22840 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
22841   // These tests are not in NamespaceFixer because that doesn't
22842   // test its interaction with line wrapping
22843   FormatStyle Style = getLLVMStyleWithColumns(80);
22844   verifyFormat("namespace {\n"
22845                "int i;\n"
22846                "int j;\n"
22847                "} // namespace",
22848                Style);
22849 
22850   verifyFormat("namespace AAA {\n"
22851                "int i;\n"
22852                "int j;\n"
22853                "} // namespace AAA",
22854                Style);
22855 
22856   EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n"
22857             "int i;\n"
22858             "int j;\n"
22859             "} // namespace Averyveryveryverylongnamespace",
22860             format("namespace Averyveryveryverylongnamespace {\n"
22861                    "int i;\n"
22862                    "int j;\n"
22863                    "}",
22864                    Style));
22865 
22866   EXPECT_EQ(
22867       "namespace "
22868       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
22869       "    went::mad::now {\n"
22870       "int i;\n"
22871       "int j;\n"
22872       "} // namespace\n"
22873       "  // "
22874       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
22875       "went::mad::now",
22876       format("namespace "
22877              "would::it::save::you::a::lot::of::time::if_::i::"
22878              "just::gave::up::and_::went::mad::now {\n"
22879              "int i;\n"
22880              "int j;\n"
22881              "}",
22882              Style));
22883 
22884   // This used to duplicate the comment again and again on subsequent runs
22885   EXPECT_EQ(
22886       "namespace "
22887       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
22888       "    went::mad::now {\n"
22889       "int i;\n"
22890       "int j;\n"
22891       "} // namespace\n"
22892       "  // "
22893       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
22894       "went::mad::now",
22895       format("namespace "
22896              "would::it::save::you::a::lot::of::time::if_::i::"
22897              "just::gave::up::and_::went::mad::now {\n"
22898              "int i;\n"
22899              "int j;\n"
22900              "} // namespace\n"
22901              "  // "
22902              "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
22903              "and_::went::mad::now",
22904              Style));
22905 }
22906 
22907 TEST_F(FormatTest, LikelyUnlikely) {
22908   FormatStyle Style = getLLVMStyle();
22909 
22910   verifyFormat("if (argc > 5) [[unlikely]] {\n"
22911                "  return 29;\n"
22912                "}",
22913                Style);
22914 
22915   verifyFormat("if (argc > 5) [[likely]] {\n"
22916                "  return 29;\n"
22917                "}",
22918                Style);
22919 
22920   verifyFormat("if (argc > 5) [[unlikely]] {\n"
22921                "  return 29;\n"
22922                "} else [[likely]] {\n"
22923                "  return 42;\n"
22924                "}\n",
22925                Style);
22926 
22927   verifyFormat("if (argc > 5) [[unlikely]] {\n"
22928                "  return 29;\n"
22929                "} else if (argc > 10) [[likely]] {\n"
22930                "  return 99;\n"
22931                "} else {\n"
22932                "  return 42;\n"
22933                "}\n",
22934                Style);
22935 
22936   verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
22937                "  return 29;\n"
22938                "}",
22939                Style);
22940 
22941   verifyFormat("if (argc > 5) [[unlikely]]\n"
22942                "  return 29;\n",
22943                Style);
22944   verifyFormat("if (argc > 5) [[likely]]\n"
22945                "  return 29;\n",
22946                Style);
22947 
22948   Style.AttributeMacros.push_back("UNLIKELY");
22949   Style.AttributeMacros.push_back("LIKELY");
22950   verifyFormat("if (argc > 5) UNLIKELY\n"
22951                "  return 29;\n",
22952                Style);
22953 
22954   verifyFormat("if (argc > 5) UNLIKELY {\n"
22955                "  return 29;\n"
22956                "}",
22957                Style);
22958   verifyFormat("if (argc > 5) UNLIKELY {\n"
22959                "  return 29;\n"
22960                "} else [[likely]] {\n"
22961                "  return 42;\n"
22962                "}\n",
22963                Style);
22964   verifyFormat("if (argc > 5) UNLIKELY {\n"
22965                "  return 29;\n"
22966                "} else LIKELY {\n"
22967                "  return 42;\n"
22968                "}\n",
22969                Style);
22970   verifyFormat("if (argc > 5) [[unlikely]] {\n"
22971                "  return 29;\n"
22972                "} else LIKELY {\n"
22973                "  return 42;\n"
22974                "}\n",
22975                Style);
22976 }
22977 
22978 TEST_F(FormatTest, PenaltyIndentedWhitespace) {
22979   verifyFormat("Constructor()\n"
22980                "    : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22981                "                          aaaa(aaaaaaaaaaaaaaaaaa, "
22982                "aaaaaaaaaaaaaaaaaat))");
22983   verifyFormat("Constructor()\n"
22984                "    : aaaaaaaaaaaaa(aaaaaa), "
22985                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
22986 
22987   FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
22988   StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
22989   verifyFormat("Constructor()\n"
22990                "    : aaaaaa(aaaaaa),\n"
22991                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22992                "          aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
22993                StyleWithWhitespacePenalty);
22994   verifyFormat("Constructor()\n"
22995                "    : aaaaaaaaaaaaa(aaaaaa), "
22996                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
22997                StyleWithWhitespacePenalty);
22998 }
22999 
23000 TEST_F(FormatTest, LLVMDefaultStyle) {
23001   FormatStyle Style = getLLVMStyle();
23002   verifyFormat("extern \"C\" {\n"
23003                "int foo();\n"
23004                "}",
23005                Style);
23006 }
23007 TEST_F(FormatTest, GNUDefaultStyle) {
23008   FormatStyle Style = getGNUStyle();
23009   verifyFormat("extern \"C\"\n"
23010                "{\n"
23011                "  int foo ();\n"
23012                "}",
23013                Style);
23014 }
23015 TEST_F(FormatTest, MozillaDefaultStyle) {
23016   FormatStyle Style = getMozillaStyle();
23017   verifyFormat("extern \"C\"\n"
23018                "{\n"
23019                "  int foo();\n"
23020                "}",
23021                Style);
23022 }
23023 TEST_F(FormatTest, GoogleDefaultStyle) {
23024   FormatStyle Style = getGoogleStyle();
23025   verifyFormat("extern \"C\" {\n"
23026                "int foo();\n"
23027                "}",
23028                Style);
23029 }
23030 TEST_F(FormatTest, ChromiumDefaultStyle) {
23031   FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp);
23032   verifyFormat("extern \"C\" {\n"
23033                "int foo();\n"
23034                "}",
23035                Style);
23036 }
23037 TEST_F(FormatTest, MicrosoftDefaultStyle) {
23038   FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp);
23039   verifyFormat("extern \"C\"\n"
23040                "{\n"
23041                "    int foo();\n"
23042                "}",
23043                Style);
23044 }
23045 TEST_F(FormatTest, WebKitDefaultStyle) {
23046   FormatStyle Style = getWebKitStyle();
23047   verifyFormat("extern \"C\" {\n"
23048                "int foo();\n"
23049                "}",
23050                Style);
23051 }
23052 
23053 TEST_F(FormatTest, ConceptsAndRequires) {
23054   FormatStyle Style = getLLVMStyle();
23055   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
23056 
23057   verifyFormat("template <typename T>\n"
23058                "concept Hashable = requires(T a) {\n"
23059                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
23060                "};",
23061                Style);
23062   verifyFormat("template <typename T>\n"
23063                "concept EqualityComparable = requires(T a, T b) {\n"
23064                "  { a == b } -> bool;\n"
23065                "};",
23066                Style);
23067   verifyFormat("template <typename T>\n"
23068                "concept EqualityComparable = requires(T a, T b) {\n"
23069                "  { a == b } -> bool;\n"
23070                "  { a != b } -> bool;\n"
23071                "};",
23072                Style);
23073   verifyFormat("template <typename T>\n"
23074                "concept EqualityComparable = requires(T a, T b) {\n"
23075                "  { a == b } -> bool;\n"
23076                "  { a != b } -> bool;\n"
23077                "};",
23078                Style);
23079 
23080   verifyFormat("template <typename It>\n"
23081                "requires Iterator<It>\n"
23082                "void sort(It begin, It end) {\n"
23083                "  //....\n"
23084                "}",
23085                Style);
23086 
23087   verifyFormat("template <typename T>\n"
23088                "concept Large = sizeof(T) > 10;",
23089                Style);
23090 
23091   verifyFormat("template <typename T, typename U>\n"
23092                "concept FooableWith = requires(T t, U u) {\n"
23093                "  typename T::foo_type;\n"
23094                "  { t.foo(u) } -> typename T::foo_type;\n"
23095                "  t++;\n"
23096                "};\n"
23097                "void doFoo(FooableWith<int> auto t) {\n"
23098                "  t.foo(3);\n"
23099                "}",
23100                Style);
23101   verifyFormat("template <typename T>\n"
23102                "concept Context = sizeof(T) == 1;",
23103                Style);
23104   verifyFormat("template <typename T>\n"
23105                "concept Context = is_specialization_of_v<context, T>;",
23106                Style);
23107   verifyFormat("template <typename T>\n"
23108                "concept Node = std::is_object_v<T>;",
23109                Style);
23110   verifyFormat("template <typename T>\n"
23111                "concept Tree = true;",
23112                Style);
23113 
23114   verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n"
23115                "  //...\n"
23116                "}",
23117                Style);
23118 
23119   verifyFormat(
23120       "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n"
23121       "  //...\n"
23122       "}",
23123       Style);
23124 
23125   verifyFormat(
23126       "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n"
23127       "  //...\n"
23128       "}",
23129       Style);
23130 
23131   verifyFormat("template <typename T>\n"
23132                "veryveryvery_long_return_type g(T i) requires Concept1<I> || "
23133                "Concept2<I> {\n"
23134                "  //...\n"
23135                "}",
23136                Style);
23137 
23138   verifyFormat("template <typename T>\n"
23139                "veryveryvery_long_return_type g(T i) requires Concept1<I> && "
23140                "Concept2<I> {\n"
23141                "  //...\n"
23142                "}",
23143                Style);
23144 
23145   verifyFormat(
23146       "template <typename T>\n"
23147       "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n"
23148       "  //...\n"
23149       "}",
23150       Style);
23151 
23152   verifyFormat(
23153       "template <typename T>\n"
23154       "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n"
23155       "  //...\n"
23156       "}",
23157       Style);
23158 
23159   verifyFormat("template <typename It>\n"
23160                "requires Foo<It>() && Bar<It> {\n"
23161                "  //....\n"
23162                "}",
23163                Style);
23164 
23165   verifyFormat("template <typename It>\n"
23166                "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n"
23167                "  //....\n"
23168                "}",
23169                Style);
23170 
23171   verifyFormat("template <typename It>\n"
23172                "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n"
23173                "  //....\n"
23174                "}",
23175                Style);
23176 
23177   verifyFormat(
23178       "template <typename It>\n"
23179       "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n"
23180       "  //....\n"
23181       "}",
23182       Style);
23183 
23184   Style.IndentRequires = true;
23185   verifyFormat("template <typename It>\n"
23186                "  requires Iterator<It>\n"
23187                "void sort(It begin, It end) {\n"
23188                "  //....\n"
23189                "}",
23190                Style);
23191   verifyFormat("template <std::size index_>\n"
23192                "  requires(index_ < sizeof...(Children_))\n"
23193                "Tree auto &child() {\n"
23194                "  // ...\n"
23195                "}",
23196                Style);
23197 
23198   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
23199   verifyFormat("template <typename T>\n"
23200                "concept Hashable = requires (T a) {\n"
23201                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
23202                "};",
23203                Style);
23204 
23205   verifyFormat("template <class T = void>\n"
23206                "  requires EqualityComparable<T> || Same<T, void>\n"
23207                "struct equal_to;",
23208                Style);
23209 
23210   verifyFormat("template <class T>\n"
23211                "  requires requires {\n"
23212                "    T{};\n"
23213                "    T (int);\n"
23214                "  }\n",
23215                Style);
23216 
23217   Style.ColumnLimit = 78;
23218   verifyFormat("template <typename T>\n"
23219                "concept Context = Traits<typename T::traits_type> and\n"
23220                "    Interface<typename T::interface_type> and\n"
23221                "    Request<typename T::request_type> and\n"
23222                "    Response<typename T::response_type> and\n"
23223                "    ContextExtension<typename T::extension_type> and\n"
23224                "    ::std::is_copy_constructable<T> and "
23225                "::std::is_move_constructable<T> and\n"
23226                "    requires (T c) {\n"
23227                "  { c.response; } -> Response;\n"
23228                "} and requires (T c) {\n"
23229                "  { c.request; } -> Request;\n"
23230                "}\n",
23231                Style);
23232 
23233   verifyFormat("template <typename T>\n"
23234                "concept Context = Traits<typename T::traits_type> or\n"
23235                "    Interface<typename T::interface_type> or\n"
23236                "    Request<typename T::request_type> or\n"
23237                "    Response<typename T::response_type> or\n"
23238                "    ContextExtension<typename T::extension_type> or\n"
23239                "    ::std::is_copy_constructable<T> or "
23240                "::std::is_move_constructable<T> or\n"
23241                "    requires (T c) {\n"
23242                "  { c.response; } -> Response;\n"
23243                "} or requires (T c) {\n"
23244                "  { c.request; } -> Request;\n"
23245                "}\n",
23246                Style);
23247 
23248   verifyFormat("template <typename T>\n"
23249                "concept Context = Traits<typename T::traits_type> &&\n"
23250                "    Interface<typename T::interface_type> &&\n"
23251                "    Request<typename T::request_type> &&\n"
23252                "    Response<typename T::response_type> &&\n"
23253                "    ContextExtension<typename T::extension_type> &&\n"
23254                "    ::std::is_copy_constructable<T> && "
23255                "::std::is_move_constructable<T> &&\n"
23256                "    requires (T c) {\n"
23257                "  { c.response; } -> Response;\n"
23258                "} && requires (T c) {\n"
23259                "  { c.request; } -> Request;\n"
23260                "}\n",
23261                Style);
23262 
23263   verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && "
23264                "Constraint2<T>;");
23265 
23266   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
23267   Style.BraceWrapping.AfterFunction = true;
23268   Style.BraceWrapping.AfterClass = true;
23269   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
23270   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
23271   verifyFormat("void Foo () requires (std::copyable<T>)\n"
23272                "{\n"
23273                "  return\n"
23274                "}\n",
23275                Style);
23276 
23277   verifyFormat("void Foo () requires std::copyable<T>\n"
23278                "{\n"
23279                "  return\n"
23280                "}\n",
23281                Style);
23282 
23283   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
23284                "  requires (std::invocable<F, std::invoke_result_t<Args>...>)\n"
23285                "struct constant;",
23286                Style);
23287 
23288   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
23289                "  requires std::invocable<F, std::invoke_result_t<Args>...>\n"
23290                "struct constant;",
23291                Style);
23292 
23293   verifyFormat("template <class T>\n"
23294                "class plane_with_very_very_very_long_name\n"
23295                "{\n"
23296                "  constexpr plane_with_very_very_very_long_name () requires "
23297                "std::copyable<T>\n"
23298                "      : plane_with_very_very_very_long_name (1)\n"
23299                "  {\n"
23300                "  }\n"
23301                "}\n",
23302                Style);
23303 
23304   verifyFormat("template <class T>\n"
23305                "class plane_with_long_name\n"
23306                "{\n"
23307                "  constexpr plane_with_long_name () requires std::copyable<T>\n"
23308                "      : plane_with_long_name (1)\n"
23309                "  {\n"
23310                "  }\n"
23311                "}\n",
23312                Style);
23313 
23314   Style.BreakBeforeConceptDeclarations = false;
23315   verifyFormat("template <typename T> concept Tree = true;", Style);
23316 
23317   Style.IndentRequires = false;
23318   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
23319                "requires (std::invocable<F, std::invoke_result_t<Args>...>) "
23320                "struct constant;",
23321                Style);
23322 }
23323 
23324 TEST_F(FormatTest, StatementAttributeLikeMacros) {
23325   FormatStyle Style = getLLVMStyle();
23326   StringRef Source = "void Foo::slot() {\n"
23327                      "  unsigned char MyChar = 'x';\n"
23328                      "  emit signal(MyChar);\n"
23329                      "  Q_EMIT signal(MyChar);\n"
23330                      "}";
23331 
23332   EXPECT_EQ(Source, format(Source, Style));
23333 
23334   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
23335   EXPECT_EQ("void Foo::slot() {\n"
23336             "  unsigned char MyChar = 'x';\n"
23337             "  emit          signal(MyChar);\n"
23338             "  Q_EMIT signal(MyChar);\n"
23339             "}",
23340             format(Source, Style));
23341 
23342   Style.StatementAttributeLikeMacros.push_back("emit");
23343   EXPECT_EQ(Source, format(Source, Style));
23344 
23345   Style.StatementAttributeLikeMacros = {};
23346   EXPECT_EQ("void Foo::slot() {\n"
23347             "  unsigned char MyChar = 'x';\n"
23348             "  emit          signal(MyChar);\n"
23349             "  Q_EMIT        signal(MyChar);\n"
23350             "}",
23351             format(Source, Style));
23352 }
23353 
23354 TEST_F(FormatTest, IndentAccessModifiers) {
23355   FormatStyle Style = getLLVMStyle();
23356   Style.IndentAccessModifiers = true;
23357   // Members are *two* levels below the record;
23358   // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
23359   verifyFormat("class C {\n"
23360                "    int i;\n"
23361                "};\n",
23362                Style);
23363   verifyFormat("union C {\n"
23364                "    int i;\n"
23365                "    unsigned u;\n"
23366                "};\n",
23367                Style);
23368   // Access modifiers should be indented one level below the record.
23369   verifyFormat("class C {\n"
23370                "  public:\n"
23371                "    int i;\n"
23372                "};\n",
23373                Style);
23374   verifyFormat("struct S {\n"
23375                "  private:\n"
23376                "    class C {\n"
23377                "        int j;\n"
23378                "\n"
23379                "      public:\n"
23380                "        C();\n"
23381                "    };\n"
23382                "\n"
23383                "  public:\n"
23384                "    int i;\n"
23385                "};\n",
23386                Style);
23387   // Enumerations are not records and should be unaffected.
23388   Style.AllowShortEnumsOnASingleLine = false;
23389   verifyFormat("enum class E {\n"
23390                "  A,\n"
23391                "  B\n"
23392                "};\n",
23393                Style);
23394   // Test with a different indentation width;
23395   // also proves that the result is Style.AccessModifierOffset agnostic.
23396   Style.IndentWidth = 3;
23397   verifyFormat("class C {\n"
23398                "   public:\n"
23399                "      int i;\n"
23400                "};\n",
23401                Style);
23402 }
23403 
23404 TEST_F(FormatTest, LimitlessStringsAndComments) {
23405   auto Style = getLLVMStyleWithColumns(0);
23406   constexpr StringRef Code =
23407       "/**\n"
23408       " * This is a multiline comment with quite some long lines, at least for "
23409       "the LLVM Style.\n"
23410       " * We will redo this with strings and line comments. Just to  check if "
23411       "everything is working.\n"
23412       " */\n"
23413       "bool foo() {\n"
23414       "  /* Single line multi line comment. */\n"
23415       "  const std::string String = \"This is a multiline string with quite "
23416       "some long lines, at least for the LLVM Style.\"\n"
23417       "                             \"We already did it with multi line "
23418       "comments, and we will do it with line comments. Just to check if "
23419       "everything is working.\";\n"
23420       "  // This is a line comment (block) with quite some long lines, at "
23421       "least for the LLVM Style.\n"
23422       "  // We already did this with multi line comments and strings. Just to "
23423       "check if everything is working.\n"
23424       "  const std::string SmallString = \"Hello World\";\n"
23425       "  // Small line comment\n"
23426       "  return String.size() > SmallString.size();\n"
23427       "}";
23428   EXPECT_EQ(Code, format(Code, Style));
23429 }
23430 
23431 TEST_F(FormatTest, FormatDecayCopy) {
23432   // error cases from unit tests
23433   verifyFormat("foo(auto())");
23434   verifyFormat("foo(auto{})");
23435   verifyFormat("foo(auto({}))");
23436   verifyFormat("foo(auto{{}})");
23437 
23438   verifyFormat("foo(auto(1))");
23439   verifyFormat("foo(auto{1})");
23440   verifyFormat("foo(new auto(1))");
23441   verifyFormat("foo(new auto{1})");
23442   verifyFormat("decltype(auto(1)) x;");
23443   verifyFormat("decltype(auto{1}) x;");
23444   verifyFormat("auto(x);");
23445   verifyFormat("auto{x};");
23446   verifyFormat("new auto{x};");
23447   verifyFormat("auto{x} = y;");
23448   verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly
23449                                 // the user's own fault
23450   verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is
23451                                          // clearly the user's own fault
23452   verifyFormat("auto(*p)() = f;");       // actually a declaration; TODO FIXME
23453 }
23454 
23455 TEST_F(FormatTest, Cpp20ModulesSupport) {
23456   FormatStyle Style = getLLVMStyle();
23457   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
23458   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
23459 
23460   verifyFormat("export import foo;", Style);
23461   verifyFormat("export import foo:bar;", Style);
23462   verifyFormat("export import foo.bar;", Style);
23463   verifyFormat("export import foo.bar:baz;", Style);
23464   verifyFormat("export import :bar;", Style);
23465   verifyFormat("export module foo:bar;", Style);
23466   verifyFormat("export module foo;", Style);
23467   verifyFormat("export module foo.bar;", Style);
23468   verifyFormat("export module foo.bar:baz;", Style);
23469   verifyFormat("export import <string_view>;", Style);
23470 
23471   verifyFormat("export type_name var;", Style);
23472   verifyFormat("template <class T> export using A = B<T>;", Style);
23473   verifyFormat("export using A = B;", Style);
23474   verifyFormat("export int func() {\n"
23475                "  foo();\n"
23476                "}",
23477                Style);
23478   verifyFormat("export struct {\n"
23479                "  int foo;\n"
23480                "};",
23481                Style);
23482   verifyFormat("export {\n"
23483                "  int foo;\n"
23484                "};",
23485                Style);
23486   verifyFormat("export export char const *hello() { return \"hello\"; }");
23487 
23488   verifyFormat("import bar;", Style);
23489   verifyFormat("import foo.bar;", Style);
23490   verifyFormat("import foo:bar;", Style);
23491   verifyFormat("import :bar;", Style);
23492   verifyFormat("import <ctime>;", Style);
23493   verifyFormat("import \"header\";", Style);
23494 
23495   verifyFormat("module foo;", Style);
23496   verifyFormat("module foo:bar;", Style);
23497   verifyFormat("module foo.bar;", Style);
23498   verifyFormat("module;", Style);
23499 
23500   verifyFormat("export namespace hi {\n"
23501                "const char *sayhi();\n"
23502                "}",
23503                Style);
23504 
23505   verifyFormat("module :private;", Style);
23506   verifyFormat("import <foo/bar.h>;", Style);
23507   verifyFormat("import foo...bar;", Style);
23508   verifyFormat("import ..........;", Style);
23509   verifyFormat("module foo:private;", Style);
23510   verifyFormat("import a", Style);
23511   verifyFormat("module a", Style);
23512   verifyFormat("export import a", Style);
23513   verifyFormat("export module a", Style);
23514 
23515   verifyFormat("import", Style);
23516   verifyFormat("module", Style);
23517   verifyFormat("export", Style);
23518 }
23519 
23520 TEST_F(FormatTest, CoroutineForCoawait) {
23521   FormatStyle Style = getLLVMStyle();
23522   verifyFormat("for co_await (auto x : range())\n  ;");
23523   verifyFormat("for (auto i : arr) {\n"
23524                "}",
23525                Style);
23526   verifyFormat("for co_await (auto i : arr) {\n"
23527                "}",
23528                Style);
23529   verifyFormat("for co_await (auto i : foo(T{})) {\n"
23530                "}",
23531                Style);
23532 }
23533 
23534 TEST_F(FormatTest, CoroutineCoAwait) {
23535   verifyFormat("int x = co_await foo();");
23536   verifyFormat("int x = (co_await foo());");
23537   verifyFormat("co_await (42);");
23538   verifyFormat("void operator co_await(int);");
23539   verifyFormat("void operator co_await(a);");
23540   verifyFormat("co_await a;");
23541   verifyFormat("co_await missing_await_resume{};");
23542   verifyFormat("co_await a; // comment");
23543   verifyFormat("void test0() { co_await a; }");
23544   verifyFormat("co_await co_await co_await foo();");
23545   verifyFormat("co_await foo().bar();");
23546   verifyFormat("co_await [this]() -> Task { co_return x; }");
23547   verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await "
23548                "foo(); }(x, y);");
23549 
23550   FormatStyle Style = getLLVMStyleWithColumns(40);
23551   verifyFormat("co_await [this](int a, int b) -> Task {\n"
23552                "  co_return co_await foo();\n"
23553                "}(x, y);",
23554                Style);
23555   verifyFormat("co_await;");
23556 }
23557 
23558 TEST_F(FormatTest, CoroutineCoYield) {
23559   verifyFormat("int x = co_yield foo();");
23560   verifyFormat("int x = (co_yield foo());");
23561   verifyFormat("co_yield (42);");
23562   verifyFormat("co_yield {42};");
23563   verifyFormat("co_yield 42;");
23564   verifyFormat("co_yield n++;");
23565   verifyFormat("co_yield ++n;");
23566   verifyFormat("co_yield;");
23567 }
23568 
23569 TEST_F(FormatTest, CoroutineCoReturn) {
23570   verifyFormat("co_return (42);");
23571   verifyFormat("co_return;");
23572   verifyFormat("co_return {};");
23573   verifyFormat("co_return x;");
23574   verifyFormat("co_return co_await foo();");
23575   verifyFormat("co_return co_yield foo();");
23576 }
23577 
23578 TEST_F(FormatTest, EmptyShortBlock) {
23579   auto Style = getLLVMStyle();
23580   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
23581 
23582   verifyFormat("try {\n"
23583                "  doA();\n"
23584                "} catch (Exception &e) {\n"
23585                "  e.printStackTrace();\n"
23586                "}\n",
23587                Style);
23588 
23589   verifyFormat("try {\n"
23590                "  doA();\n"
23591                "} catch (Exception &e) {}\n",
23592                Style);
23593 }
23594 
23595 TEST_F(FormatTest, ShortTemplatedArgumentLists) {
23596   auto Style = getLLVMStyle();
23597 
23598   verifyFormat("template <> struct S : Template<int (*)[]> {};\n", Style);
23599   verifyFormat("template <> struct S : Template<int (*)[10]> {};\n", Style);
23600   verifyFormat("struct Y : X<[] { return 0; }> {};", Style);
23601   verifyFormat("struct Y<[] { return 0; }> {};", Style);
23602 
23603   verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style);
23604 }
23605 
23606 TEST_F(FormatTest, RemoveBraces) {
23607   FormatStyle Style = getLLVMStyle();
23608   Style.RemoveBracesLLVM = true;
23609 
23610   // The following eight test cases are fully-braced versions of the examples at
23611   // "llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single-
23612   // statement-bodies-of-if-else-loop-statements".
23613 
23614   // 1. Omit the braces, since the body is simple and clearly associated with
23615   // the if.
23616   verifyFormat("if (isa<FunctionDecl>(D))\n"
23617                "  handleFunctionDecl(D);\n"
23618                "else if (isa<VarDecl>(D))\n"
23619                "  handleVarDecl(D);",
23620                "if (isa<FunctionDecl>(D)) {\n"
23621                "  handleFunctionDecl(D);\n"
23622                "} else if (isa<VarDecl>(D)) {\n"
23623                "  handleVarDecl(D);\n"
23624                "}",
23625                Style);
23626 
23627   // 2. Here we document the condition itself and not the body.
23628   verifyFormat("if (isa<VarDecl>(D)) {\n"
23629                "  // It is necessary that we explain the situation with this\n"
23630                "  // surprisingly long comment, so it would be unclear\n"
23631                "  // without the braces whether the following statement is in\n"
23632                "  // the scope of the `if`.\n"
23633                "  // Because the condition is documented, we can't really\n"
23634                "  // hoist this comment that applies to the body above the\n"
23635                "  // if.\n"
23636                "  handleOtherDecl(D);\n"
23637                "}",
23638                Style);
23639 
23640   // 3. Use braces on the outer `if` to avoid a potential dangling else
23641   // situation.
23642   verifyFormat("if (isa<VarDecl>(D)) {\n"
23643                "  for (auto *A : D.attrs())\n"
23644                "    if (shouldProcessAttr(A))\n"
23645                "      handleAttr(A);\n"
23646                "}",
23647                "if (isa<VarDecl>(D)) {\n"
23648                "  for (auto *A : D.attrs()) {\n"
23649                "    if (shouldProcessAttr(A)) {\n"
23650                "      handleAttr(A);\n"
23651                "    }\n"
23652                "  }\n"
23653                "}",
23654                Style);
23655 
23656   // 4. Use braces for the `if` block to keep it uniform with the else block.
23657   verifyFormat("if (isa<FunctionDecl>(D)) {\n"
23658                "  handleFunctionDecl(D);\n"
23659                "} else {\n"
23660                "  // In this else case, it is necessary that we explain the\n"
23661                "  // situation with this surprisingly long comment, so it\n"
23662                "  // would be unclear without the braces whether the\n"
23663                "  // following statement is in the scope of the `if`.\n"
23664                "  handleOtherDecl(D);\n"
23665                "}",
23666                Style);
23667 
23668   // 5. This should also omit braces.  The `for` loop contains only a single
23669   // statement, so it shouldn't have braces.  The `if` also only contains a
23670   // single simple statement (the for loop), so it also should omit braces.
23671   verifyFormat("if (isa<FunctionDecl>(D))\n"
23672                "  for (auto *A : D.attrs())\n"
23673                "    handleAttr(A);",
23674                "if (isa<FunctionDecl>(D)) {\n"
23675                "  for (auto *A : D.attrs()) {\n"
23676                "    handleAttr(A);\n"
23677                "  }\n"
23678                "}",
23679                Style);
23680 
23681   // 6. Use braces for the outer `if` since the nested `for` is braced.
23682   verifyFormat("if (isa<FunctionDecl>(D)) {\n"
23683                "  for (auto *A : D.attrs()) {\n"
23684                "    // In this for loop body, it is necessary that we explain\n"
23685                "    // the situation with this surprisingly long comment,\n"
23686                "    // forcing braces on the `for` block.\n"
23687                "    handleAttr(A);\n"
23688                "  }\n"
23689                "}",
23690                Style);
23691 
23692   // 7. Use braces on the outer block because there are more than two levels of
23693   // nesting.
23694   verifyFormat("if (isa<FunctionDecl>(D)) {\n"
23695                "  for (auto *A : D.attrs())\n"
23696                "    for (ssize_t i : llvm::seq<ssize_t>(count))\n"
23697                "      handleAttrOnDecl(D, A, i);\n"
23698                "}",
23699                "if (isa<FunctionDecl>(D)) {\n"
23700                "  for (auto *A : D.attrs()) {\n"
23701                "    for (ssize_t i : llvm::seq<ssize_t>(count)) {\n"
23702                "      handleAttrOnDecl(D, A, i);\n"
23703                "    }\n"
23704                "  }\n"
23705                "}",
23706                Style);
23707 
23708   // 8. Use braces on the outer block because of a nested `if`, otherwise the
23709   // compiler would warn: `add explicit braces to avoid dangling else`
23710   verifyFormat("if (auto *D = dyn_cast<FunctionDecl>(D)) {\n"
23711                "  if (shouldProcess(D))\n"
23712                "    handleVarDecl(D);\n"
23713                "  else\n"
23714                "    markAsIgnored(D);\n"
23715                "}",
23716                "if (auto *D = dyn_cast<FunctionDecl>(D)) {\n"
23717                "  if (shouldProcess(D)) {\n"
23718                "    handleVarDecl(D);\n"
23719                "  } else {\n"
23720                "    markAsIgnored(D);\n"
23721                "  }\n"
23722                "}",
23723                Style);
23724 
23725   verifyFormat("if (a)\n"
23726                "  b; // comment\n"
23727                "else if (c)\n"
23728                "  d; /* comment */\n"
23729                "else\n"
23730                "  e;",
23731                "if (a) {\n"
23732                "  b; // comment\n"
23733                "} else if (c) {\n"
23734                "  d; /* comment */\n"
23735                "} else {\n"
23736                "  e;\n"
23737                "}",
23738                Style);
23739 
23740   verifyFormat("if (a) {\n"
23741                "  b;\n"
23742                "  c;\n"
23743                "} else if (d) {\n"
23744                "  e;\n"
23745                "}",
23746                Style);
23747 
23748   verifyFormat("if (a) {\n"
23749                "#undef NDEBUG\n"
23750                "  b;\n"
23751                "} else {\n"
23752                "  c;\n"
23753                "}",
23754                Style);
23755 
23756   verifyFormat("if (a) {\n"
23757                "  // comment\n"
23758                "} else if (b) {\n"
23759                "  c;\n"
23760                "}",
23761                Style);
23762 
23763   verifyFormat("if (a) {\n"
23764                "  b;\n"
23765                "} else {\n"
23766                "  { c; }\n"
23767                "}",
23768                Style);
23769 
23770   verifyFormat("if (a) {\n"
23771                "  if (b) // comment\n"
23772                "    c;\n"
23773                "} else if (d) {\n"
23774                "  e;\n"
23775                "}",
23776                "if (a) {\n"
23777                "  if (b) { // comment\n"
23778                "    c;\n"
23779                "  }\n"
23780                "} else if (d) {\n"
23781                "  e;\n"
23782                "}",
23783                Style);
23784 
23785   verifyFormat("if (a) {\n"
23786                "  if (b) {\n"
23787                "    c;\n"
23788                "    // comment\n"
23789                "  } else if (d) {\n"
23790                "    e;\n"
23791                "  }\n"
23792                "}",
23793                Style);
23794 
23795   verifyFormat("if (a) {\n"
23796                "  if (b)\n"
23797                "    c;\n"
23798                "}",
23799                "if (a) {\n"
23800                "  if (b) {\n"
23801                "    c;\n"
23802                "  }\n"
23803                "}",
23804                Style);
23805 
23806   verifyFormat("if (a)\n"
23807                "  if (b)\n"
23808                "    c;\n"
23809                "  else\n"
23810                "    d;\n"
23811                "else\n"
23812                "  e;",
23813                "if (a) {\n"
23814                "  if (b) {\n"
23815                "    c;\n"
23816                "  } else {\n"
23817                "    d;\n"
23818                "  }\n"
23819                "} else {\n"
23820                "  e;\n"
23821                "}",
23822                Style);
23823 
23824   verifyFormat("if (a) {\n"
23825                "  // comment\n"
23826                "  if (b)\n"
23827                "    c;\n"
23828                "  else if (d)\n"
23829                "    e;\n"
23830                "} else {\n"
23831                "  g;\n"
23832                "}",
23833                "if (a) {\n"
23834                "  // comment\n"
23835                "  if (b) {\n"
23836                "    c;\n"
23837                "  } else if (d) {\n"
23838                "    e;\n"
23839                "  }\n"
23840                "} else {\n"
23841                "  g;\n"
23842                "}",
23843                Style);
23844 
23845   verifyFormat("if (a)\n"
23846                "  b;\n"
23847                "else if (c)\n"
23848                "  d;\n"
23849                "else\n"
23850                "  e;",
23851                "if (a) {\n"
23852                "  b;\n"
23853                "} else {\n"
23854                "  if (c) {\n"
23855                "    d;\n"
23856                "  } else {\n"
23857                "    e;\n"
23858                "  }\n"
23859                "}",
23860                Style);
23861 
23862   verifyFormat("if (a) {\n"
23863                "  if (b)\n"
23864                "    c;\n"
23865                "  else if (d)\n"
23866                "    e;\n"
23867                "} else {\n"
23868                "  g;\n"
23869                "}",
23870                "if (a) {\n"
23871                "  if (b)\n"
23872                "    c;\n"
23873                "  else {\n"
23874                "    if (d)\n"
23875                "      e;\n"
23876                "  }\n"
23877                "} else {\n"
23878                "  g;\n"
23879                "}",
23880                Style);
23881 
23882   verifyFormat("if (a)\n"
23883                "  b;\n"
23884                "else if (c)\n"
23885                "  while (d)\n"
23886                "    e;\n"
23887                "// comment",
23888                "if (a)\n"
23889                "{\n"
23890                "  b;\n"
23891                "} else if (c) {\n"
23892                "  while (d) {\n"
23893                "    e;\n"
23894                "  }\n"
23895                "}\n"
23896                "// comment",
23897                Style);
23898 
23899   verifyFormat("if (a) {\n"
23900                "  b;\n"
23901                "} else if (c) {\n"
23902                "  d;\n"
23903                "} else {\n"
23904                "  e;\n"
23905                "  g;\n"
23906                "}",
23907                Style);
23908 
23909   verifyFormat("if (a) {\n"
23910                "  b;\n"
23911                "} else if (c) {\n"
23912                "  d;\n"
23913                "} else {\n"
23914                "  e;\n"
23915                "} // comment",
23916                Style);
23917 
23918   verifyFormat("int abs = [](int i) {\n"
23919                "  if (i >= 0)\n"
23920                "    return i;\n"
23921                "  return -i;\n"
23922                "};",
23923                "int abs = [](int i) {\n"
23924                "  if (i >= 0) {\n"
23925                "    return i;\n"
23926                "  }\n"
23927                "  return -i;\n"
23928                "};",
23929                Style);
23930 
23931   Style.ColumnLimit = 20;
23932 
23933   verifyFormat("if (a) {\n"
23934                "  b = c + // 1 -\n"
23935                "      d;\n"
23936                "}",
23937                Style);
23938 
23939   verifyFormat("if (a) {\n"
23940                "  b = c >= 0 ? d\n"
23941                "             : e;\n"
23942                "}",
23943                "if (a) {\n"
23944                "  b = c >= 0 ? d : e;\n"
23945                "}",
23946                Style);
23947 
23948   verifyFormat("if (a)\n"
23949                "  b = c > 0 ? d : e;",
23950                "if (a) {\n"
23951                "  b = c > 0 ? d : e;\n"
23952                "}",
23953                Style);
23954 
23955   Style.ColumnLimit = 0;
23956 
23957   verifyFormat("if (a)\n"
23958                "  b234567890223456789032345678904234567890 = "
23959                "c234567890223456789032345678904234567890;",
23960                "if (a) {\n"
23961                "  b234567890223456789032345678904234567890 = "
23962                "c234567890223456789032345678904234567890;\n"
23963                "}",
23964                Style);
23965 }
23966 
23967 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) {
23968   auto Style = getLLVMStyle();
23969 
23970   StringRef Short = "functionCall(paramA, paramB, paramC);\n"
23971                     "void functionDecl(int a, int b, int c);";
23972 
23973   StringRef Medium = "functionCall(paramA, paramB, paramC, paramD, paramE, "
23974                      "paramF, paramG, paramH, paramI);\n"
23975                      "void functionDecl(int argumentA, int argumentB, int "
23976                      "argumentC, int argumentD, int argumentE);";
23977 
23978   verifyFormat(Short, Style);
23979 
23980   StringRef NoBreak = "functionCall(paramA, paramB, paramC, paramD, paramE, "
23981                       "paramF, paramG, paramH,\n"
23982                       "             paramI);\n"
23983                       "void functionDecl(int argumentA, int argumentB, int "
23984                       "argumentC, int argumentD,\n"
23985                       "                  int argumentE);";
23986 
23987   verifyFormat(NoBreak, Medium, Style);
23988   verifyFormat(NoBreak,
23989                "functionCall(\n"
23990                "    paramA,\n"
23991                "    paramB,\n"
23992                "    paramC,\n"
23993                "    paramD,\n"
23994                "    paramE,\n"
23995                "    paramF,\n"
23996                "    paramG,\n"
23997                "    paramH,\n"
23998                "    paramI\n"
23999                ");\n"
24000                "void functionDecl(\n"
24001                "    int argumentA,\n"
24002                "    int argumentB,\n"
24003                "    int argumentC,\n"
24004                "    int argumentD,\n"
24005                "    int argumentE\n"
24006                ");",
24007                Style);
24008 
24009   verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n"
24010                "                  nestedLongFunctionCall(argument1, "
24011                "argument2, argument3,\n"
24012                "                                         argument4, "
24013                "argument5));",
24014                Style);
24015 
24016   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
24017 
24018   verifyFormat(Short, Style);
24019   verifyFormat(
24020       "functionCall(\n"
24021       "    paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
24022       "paramI\n"
24023       ");\n"
24024       "void functionDecl(\n"
24025       "    int argumentA, int argumentB, int argumentC, int argumentD, int "
24026       "argumentE\n"
24027       ");",
24028       Medium, Style);
24029 
24030   Style.AllowAllArgumentsOnNextLine = false;
24031   Style.AllowAllParametersOfDeclarationOnNextLine = false;
24032 
24033   verifyFormat(Short, Style);
24034   verifyFormat(
24035       "functionCall(\n"
24036       "    paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
24037       "paramI\n"
24038       ");\n"
24039       "void functionDecl(\n"
24040       "    int argumentA, int argumentB, int argumentC, int argumentD, int "
24041       "argumentE\n"
24042       ");",
24043       Medium, Style);
24044 
24045   Style.BinPackArguments = false;
24046   Style.BinPackParameters = false;
24047 
24048   verifyFormat(Short, Style);
24049 
24050   verifyFormat("functionCall(\n"
24051                "    paramA,\n"
24052                "    paramB,\n"
24053                "    paramC,\n"
24054                "    paramD,\n"
24055                "    paramE,\n"
24056                "    paramF,\n"
24057                "    paramG,\n"
24058                "    paramH,\n"
24059                "    paramI\n"
24060                ");\n"
24061                "void functionDecl(\n"
24062                "    int argumentA,\n"
24063                "    int argumentB,\n"
24064                "    int argumentC,\n"
24065                "    int argumentD,\n"
24066                "    int argumentE\n"
24067                ");",
24068                Medium, Style);
24069 
24070   verifyFormat("outerFunctionCall(\n"
24071                "    nestedFunctionCall(argument1),\n"
24072                "    nestedLongFunctionCall(\n"
24073                "        argument1,\n"
24074                "        argument2,\n"
24075                "        argument3,\n"
24076                "        argument4,\n"
24077                "        argument5\n"
24078                "    )\n"
24079                ");",
24080                Style);
24081 
24082   verifyFormat("int a = (int)b;", Style);
24083   verifyFormat("int a = (int)b;",
24084                "int a = (\n"
24085                "    int\n"
24086                ") b;",
24087                Style);
24088 
24089   verifyFormat("return (true);", Style);
24090   verifyFormat("return (true);",
24091                "return (\n"
24092                "    true\n"
24093                ");",
24094                Style);
24095 
24096   verifyFormat("void foo();", Style);
24097   verifyFormat("void foo();",
24098                "void foo(\n"
24099                ");",
24100                Style);
24101 
24102   verifyFormat("void foo() {}", Style);
24103   verifyFormat("void foo() {}",
24104                "void foo(\n"
24105                ") {\n"
24106                "}",
24107                Style);
24108 
24109   verifyFormat("auto string = std::string();", Style);
24110   verifyFormat("auto string = std::string();",
24111                "auto string = std::string(\n"
24112                ");",
24113                Style);
24114 
24115   verifyFormat("void (*functionPointer)() = nullptr;", Style);
24116   verifyFormat("void (*functionPointer)() = nullptr;",
24117                "void (\n"
24118                "    *functionPointer\n"
24119                ")\n"
24120                "(\n"
24121                ") = nullptr;",
24122                Style);
24123 }
24124 
24125 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) {
24126   auto Style = getLLVMStyle();
24127 
24128   verifyFormat("if (foo()) {\n"
24129                "  return;\n"
24130                "}",
24131                Style);
24132 
24133   verifyFormat("if (quitelongarg !=\n"
24134                "    (alsolongarg - 1)) { // ABC is a very longgggggggggggg "
24135                "comment\n"
24136                "  return;\n"
24137                "}",
24138                Style);
24139 
24140   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
24141 
24142   verifyFormat("if (foo()) {\n"
24143                "  return;\n"
24144                "}",
24145                Style);
24146 
24147   verifyFormat("if (quitelongarg !=\n"
24148                "    (alsolongarg - 1)) { // ABC is a very longgggggggggggg "
24149                "comment\n"
24150                "  return;\n"
24151                "}",
24152                Style);
24153 }
24154 
24155 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
24156   auto Style = getLLVMStyle();
24157 
24158   verifyFormat("for (int i = 0; i < 5; ++i) {\n"
24159                "  doSomething();\n"
24160                "}",
24161                Style);
24162 
24163   verifyFormat("for (int myReallyLongCountVariable = 0; "
24164                "myReallyLongCountVariable < count;\n"
24165                "     myReallyLongCountVariable++) {\n"
24166                "  doSomething();\n"
24167                "}",
24168                Style);
24169 
24170   Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
24171 
24172   verifyFormat("for (int i = 0; i < 5; ++i) {\n"
24173                "  doSomething();\n"
24174                "}",
24175                Style);
24176 
24177   verifyFormat("for (int myReallyLongCountVariable = 0; "
24178                "myReallyLongCountVariable < count;\n"
24179                "     myReallyLongCountVariable++) {\n"
24180                "  doSomething();\n"
24181                "}",
24182                Style);
24183 }
24184 
24185 } // namespace
24186 } // namespace format
24187 } // namespace clang
24188