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 "clang/Frontend/TextDiagnosticPrinter.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/MemoryBuffer.h"
17 #include "gtest/gtest.h"
18 
19 #define DEBUG_TYPE "format-test"
20 
21 using clang::tooling::ReplacementTest;
22 using clang::tooling::toReplacements;
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 {
33     SC_ExpectComplete,
34     SC_ExpectIncomplete,
35     SC_DoNotCheck
36   };
37 
38   std::string format(llvm::StringRef Code,
39                      const FormatStyle &Style = getLLVMStyle(),
40                      StatusCheck CheckComplete = SC_ExpectComplete) {
41     LLVM_DEBUG(llvm::errs() << "---\n");
42     LLVM_DEBUG(llvm::errs() << Code << "\n\n");
43     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
44     FormattingAttemptStatus Status;
45     tooling::Replacements Replaces =
46         reformat(Style, Code, Ranges, "<stdin>", &Status);
47     if (CheckComplete != SC_DoNotCheck) {
48       bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
49       EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
50           << Code << "\n\n";
51     }
52     ReplacementCount = Replaces.size();
53     auto Result = applyAllReplacements(Code, Replaces);
54     EXPECT_TRUE(static_cast<bool>(Result));
55     LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
56     return *Result;
57   }
58 
59   FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) {
60     Style.ColumnLimit = ColumnLimit;
61     return Style;
62   }
63 
64   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
65     return getStyleWithColumns(getLLVMStyle(), ColumnLimit);
66   }
67 
68   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
69     return getStyleWithColumns(getGoogleStyle(), ColumnLimit);
70   }
71 
72   void verifyFormat(llvm::StringRef Expected, llvm::StringRef Code,
73                     const FormatStyle &Style = getLLVMStyle()) {
74     EXPECT_EQ(Expected.str(), format(Expected, Style))
75         << "Expected code is not stable";
76     EXPECT_EQ(Expected.str(), format(Code, Style));
77     if (Style.Language == FormatStyle::LK_Cpp) {
78       // Objective-C++ is a superset of C++, so everything checked for C++
79       // needs to be checked for Objective-C++ as well.
80       FormatStyle ObjCStyle = Style;
81       ObjCStyle.Language = FormatStyle::LK_ObjC;
82       EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle));
83     }
84   }
85 
86   void verifyFormat(llvm::StringRef Code,
87                     const FormatStyle &Style = getLLVMStyle()) {
88     verifyFormat(Code, test::messUp(Code), Style);
89   }
90 
91   void verifyIncompleteFormat(llvm::StringRef Code,
92                               const FormatStyle &Style = getLLVMStyle()) {
93     EXPECT_EQ(Code.str(),
94               format(test::messUp(Code), Style, SC_ExpectIncomplete));
95   }
96 
97   void verifyGoogleFormat(llvm::StringRef Code) {
98     verifyFormat(Code, getGoogleStyle());
99   }
100 
101   void verifyIndependentOfContext(llvm::StringRef text) {
102     verifyFormat(text);
103     verifyFormat(llvm::Twine("void f() { " + text + " }").str());
104   }
105 
106   /// \brief Verify that clang-format does not crash on the given input.
107   void verifyNoCrash(llvm::StringRef Code,
108                      const FormatStyle &Style = getLLVMStyle()) {
109     format(Code, Style, SC_DoNotCheck);
110   }
111 
112   int ReplacementCount;
113 };
114 
115 TEST_F(FormatTest, MessUp) {
116   EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
117   EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
118   EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
119   EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
120   EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
121 }
122 
123 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) {
124   EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language);
125 }
126 
127 TEST_F(FormatTest, LLVMStyleOverride) {
128   EXPECT_EQ(FormatStyle::LK_Proto,
129             getLLVMStyle(FormatStyle::LK_Proto).Language);
130 }
131 
132 //===----------------------------------------------------------------------===//
133 // Basic function tests.
134 //===----------------------------------------------------------------------===//
135 
136 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
137   EXPECT_EQ(";", format(";"));
138 }
139 
140 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
141   EXPECT_EQ("int i;", format("  int i;"));
142   EXPECT_EQ("\nint i;", format(" \n\t \v \f  int i;"));
143   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
144   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
145 }
146 
147 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
148   EXPECT_EQ("int i;", format("int\ni;"));
149 }
150 
151 TEST_F(FormatTest, FormatsNestedBlockStatements) {
152   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
153 }
154 
155 TEST_F(FormatTest, FormatsNestedCall) {
156   verifyFormat("Method(f1, f2(f3));");
157   verifyFormat("Method(f1(f2, f3()));");
158   verifyFormat("Method(f1(f2, (f3())));");
159 }
160 
161 TEST_F(FormatTest, NestedNameSpecifiers) {
162   verifyFormat("vector<::Type> v;");
163   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
164   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
165   verifyFormat("bool a = 2 < ::SomeFunction();");
166   verifyFormat("ALWAYS_INLINE ::std::string getName();");
167   verifyFormat("some::string getName();");
168 }
169 
170 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
171   EXPECT_EQ("if (a) {\n"
172             "  f();\n"
173             "}",
174             format("if(a){f();}"));
175   EXPECT_EQ(4, ReplacementCount);
176   EXPECT_EQ("if (a) {\n"
177             "  f();\n"
178             "}",
179             format("if (a) {\n"
180                    "  f();\n"
181                    "}"));
182   EXPECT_EQ(0, ReplacementCount);
183   EXPECT_EQ("/*\r\n"
184             "\r\n"
185             "*/\r\n",
186             format("/*\r\n"
187                    "\r\n"
188                    "*/\r\n"));
189   EXPECT_EQ(0, ReplacementCount);
190 }
191 
192 TEST_F(FormatTest, RemovesEmptyLines) {
193   EXPECT_EQ("class C {\n"
194             "  int i;\n"
195             "};",
196             format("class C {\n"
197                    " int i;\n"
198                    "\n"
199                    "};"));
200 
201   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
202   EXPECT_EQ("namespace N {\n"
203             "\n"
204             "int i;\n"
205             "}",
206             format("namespace N {\n"
207                    "\n"
208                    "int    i;\n"
209                    "}",
210                    getGoogleStyle()));
211   EXPECT_EQ("/* something */ namespace N {\n"
212             "\n"
213             "int i;\n"
214             "}",
215             format("/* something */ namespace N {\n"
216                    "\n"
217                    "int    i;\n"
218                    "}",
219                    getGoogleStyle()));
220   EXPECT_EQ("inline namespace N {\n"
221             "\n"
222             "int i;\n"
223             "}",
224             format("inline namespace N {\n"
225                    "\n"
226                    "int    i;\n"
227                    "}",
228                    getGoogleStyle()));
229   EXPECT_EQ("/* something */ inline namespace N {\n"
230             "\n"
231             "int i;\n"
232             "}",
233             format("/* something */ inline namespace N {\n"
234                    "\n"
235                    "int    i;\n"
236                    "}",
237                    getGoogleStyle()));
238   EXPECT_EQ("export namespace N {\n"
239             "\n"
240             "int i;\n"
241             "}",
242             format("export namespace N {\n"
243                    "\n"
244                    "int    i;\n"
245                    "}",
246                    getGoogleStyle()));
247   EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
248             "\n"
249             "int i;\n"
250             "}",
251             format("extern /**/ \"C\" /**/ {\n"
252                    "\n"
253                    "int    i;\n"
254                    "}",
255                    getGoogleStyle()));
256 
257   // ...but do keep inlining and removing empty lines for non-block extern "C"
258   // functions.
259   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
260   EXPECT_EQ("extern \"C\" int f() {\n"
261             "  int i = 42;\n"
262             "  return i;\n"
263             "}",
264             format("extern \"C\" int f() {\n"
265                    "\n"
266                    "  int i = 42;\n"
267                    "  return i;\n"
268                    "}",
269                    getGoogleStyle()));
270 
271   // Remove empty lines at the beginning and end of blocks.
272   EXPECT_EQ("void f() {\n"
273             "\n"
274             "  if (a) {\n"
275             "\n"
276             "    f();\n"
277             "  }\n"
278             "}",
279             format("void f() {\n"
280                    "\n"
281                    "  if (a) {\n"
282                    "\n"
283                    "    f();\n"
284                    "\n"
285                    "  }\n"
286                    "\n"
287                    "}",
288                    getLLVMStyle()));
289   EXPECT_EQ("void f() {\n"
290             "  if (a) {\n"
291             "    f();\n"
292             "  }\n"
293             "}",
294             format("void f() {\n"
295                    "\n"
296                    "  if (a) {\n"
297                    "\n"
298                    "    f();\n"
299                    "\n"
300                    "  }\n"
301                    "\n"
302                    "}",
303                    getGoogleStyle()));
304 
305   // Don't remove empty lines in more complex control statements.
306   EXPECT_EQ("void f() {\n"
307             "  if (a) {\n"
308             "    f();\n"
309             "\n"
310             "  } else if (b) {\n"
311             "    f();\n"
312             "  }\n"
313             "}",
314             format("void f() {\n"
315                    "  if (a) {\n"
316                    "    f();\n"
317                    "\n"
318                    "  } else if (b) {\n"
319                    "    f();\n"
320                    "\n"
321                    "  }\n"
322                    "\n"
323                    "}"));
324 
325   // Don't remove empty lines before namespace endings.
326   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
327   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
328   EXPECT_EQ("namespace {\n"
329             "int i;\n"
330             "\n"
331             "}",
332             format("namespace {\n"
333                    "int i;\n"
334                    "\n"
335                    "}", LLVMWithNoNamespaceFix));
336   EXPECT_EQ("namespace {\n"
337             "int i;\n"
338             "}",
339             format("namespace {\n"
340                    "int i;\n"
341                    "}", LLVMWithNoNamespaceFix));
342   EXPECT_EQ("namespace {\n"
343             "int i;\n"
344             "\n"
345             "};",
346             format("namespace {\n"
347                    "int i;\n"
348                    "\n"
349                    "};", LLVMWithNoNamespaceFix));
350   EXPECT_EQ("namespace {\n"
351             "int i;\n"
352             "};",
353             format("namespace {\n"
354                    "int i;\n"
355                    "};", LLVMWithNoNamespaceFix));
356   EXPECT_EQ("namespace {\n"
357             "int i;\n"
358             "\n"
359             "}",
360             format("namespace {\n"
361                    "int i;\n"
362                    "\n"
363                    "}"));
364   EXPECT_EQ("namespace {\n"
365             "int i;\n"
366             "\n"
367             "} // namespace",
368             format("namespace {\n"
369                    "int i;\n"
370                    "\n"
371                    "}  // namespace"));
372 
373   FormatStyle Style = getLLVMStyle();
374   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
375   Style.MaxEmptyLinesToKeep = 2;
376   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
377   Style.BraceWrapping.AfterClass = true;
378   Style.BraceWrapping.AfterFunction = true;
379   Style.KeepEmptyLinesAtTheStartOfBlocks = false;
380 
381   EXPECT_EQ("class Foo\n"
382             "{\n"
383             "  Foo() {}\n"
384             "\n"
385             "  void funk() {}\n"
386             "};",
387             format("class Foo\n"
388                    "{\n"
389                    "  Foo()\n"
390                    "  {\n"
391                    "  }\n"
392                    "\n"
393                    "  void funk() {}\n"
394                    "};",
395                    Style));
396 }
397 
398 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
399   verifyFormat("x = (a) and (b);");
400   verifyFormat("x = (a) or (b);");
401   verifyFormat("x = (a) bitand (b);");
402   verifyFormat("x = (a) bitor (b);");
403   verifyFormat("x = (a) not_eq (b);");
404   verifyFormat("x = (a) and_eq (b);");
405   verifyFormat("x = (a) or_eq (b);");
406   verifyFormat("x = (a) xor (b);");
407 }
408 
409 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) {
410   verifyFormat("x = compl(a);");
411   verifyFormat("x = not(a);");
412   verifyFormat("x = bitand(a);");
413   // Unary operator must not be merged with the next identifier
414   verifyFormat("x = compl a;");
415   verifyFormat("x = not a;");
416   verifyFormat("x = bitand a;");
417 }
418 
419 //===----------------------------------------------------------------------===//
420 // Tests for control statements.
421 //===----------------------------------------------------------------------===//
422 
423 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
424   verifyFormat("if (true)\n  f();\ng();");
425   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
426   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
427   verifyFormat("if constexpr (true)\n"
428                "  f();\ng();");
429   verifyFormat("if constexpr (a)\n"
430                "  if constexpr (b)\n"
431                "    if constexpr (c)\n"
432                "      g();\n"
433                "h();");
434   verifyFormat("if constexpr (a)\n"
435                "  if constexpr (b) {\n"
436                "    f();\n"
437                "  }\n"
438                "g();");
439 
440   FormatStyle AllowsMergedIf = getLLVMStyle();
441   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
442   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
443       FormatStyle::SIS_WithoutElse;
444   verifyFormat("if (a)\n"
445                "  // comment\n"
446                "  f();",
447                AllowsMergedIf);
448   verifyFormat("{\n"
449                "  if (a)\n"
450                "  label:\n"
451                "    f();\n"
452                "}",
453                AllowsMergedIf);
454   verifyFormat("#define A \\\n"
455                "  if (a)  \\\n"
456                "  label:  \\\n"
457                "    f()",
458                AllowsMergedIf);
459   verifyFormat("if (a)\n"
460                "  ;",
461                AllowsMergedIf);
462   verifyFormat("if (a)\n"
463                "  if (b) return;",
464                AllowsMergedIf);
465 
466   verifyFormat("if (a) // Can't merge this\n"
467                "  f();\n",
468                AllowsMergedIf);
469   verifyFormat("if (a) /* still don't merge */\n"
470                "  f();",
471                AllowsMergedIf);
472   verifyFormat("if (a) { // Never merge this\n"
473                "  f();\n"
474                "}",
475                AllowsMergedIf);
476   verifyFormat("if (a) { /* Never merge this */\n"
477                "  f();\n"
478                "}",
479                AllowsMergedIf);
480 
481   AllowsMergedIf.ColumnLimit = 14;
482   verifyFormat("if (a) return;", AllowsMergedIf);
483   verifyFormat("if (aaaaaaaaa)\n"
484                "  return;",
485                AllowsMergedIf);
486 
487   AllowsMergedIf.ColumnLimit = 13;
488   verifyFormat("if (a)\n  return;", AllowsMergedIf);
489 }
490 
491 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) {
492   FormatStyle AllowsMergedIf = getLLVMStyle();
493   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
494   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
495       FormatStyle::SIS_WithoutElse;
496   verifyFormat("if (a)\n"
497                "  f();\n"
498                "else {\n"
499                "  g();\n"
500                "}",
501                AllowsMergedIf);
502   verifyFormat("if (a)\n"
503                "  f();\n"
504                "else\n"
505                "  g();\n",
506                AllowsMergedIf);
507 
508   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Always;
509 
510   verifyFormat("if (a) f();\n"
511                "else {\n"
512                "  g();\n"
513                "}",
514                AllowsMergedIf);
515   verifyFormat("if (a) f();\n"
516                "else {\n"
517                "  if (a) f();\n"
518                "  else {\n"
519                "    g();\n"
520                "  }\n"
521                "  g();\n"
522                "}",
523                AllowsMergedIf);
524 }
525 
526 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
527   FormatStyle AllowsMergedLoops = getLLVMStyle();
528   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
529   verifyFormat("while (true) continue;", AllowsMergedLoops);
530   verifyFormat("for (;;) continue;", AllowsMergedLoops);
531   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
532   verifyFormat("while (true)\n"
533                "  ;",
534                AllowsMergedLoops);
535   verifyFormat("for (;;)\n"
536                "  ;",
537                AllowsMergedLoops);
538   verifyFormat("for (;;)\n"
539                "  for (;;) continue;",
540                AllowsMergedLoops);
541   verifyFormat("for (;;) // Can't merge this\n"
542                "  continue;",
543                AllowsMergedLoops);
544   verifyFormat("for (;;) /* still don't merge */\n"
545                "  continue;",
546                AllowsMergedLoops);
547 }
548 
549 TEST_F(FormatTest, FormatShortBracedStatements) {
550   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
551   AllowSimpleBracedStatements.ColumnLimit = 40;
552   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
553 
554   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
555       FormatStyle::SIS_WithoutElse;
556   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
557 
558   AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
559   AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
560   AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
561 
562   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
563   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
564   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
565   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
566   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
567   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
568   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
569   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
570   verifyFormat("if (true) {\n"
571                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
572                "}",
573                AllowSimpleBracedStatements);
574   verifyFormat("if (true) { //\n"
575                "  f();\n"
576                "}",
577                AllowSimpleBracedStatements);
578   verifyFormat("if (true) {\n"
579                "  f();\n"
580                "  f();\n"
581                "}",
582                AllowSimpleBracedStatements);
583   verifyFormat("if (true) {\n"
584                "  f();\n"
585                "} else {\n"
586                "  f();\n"
587                "}",
588                AllowSimpleBracedStatements);
589 
590   verifyFormat("struct A2 {\n"
591                "  int X;\n"
592                "};",
593                AllowSimpleBracedStatements);
594   verifyFormat("typedef struct A2 {\n"
595                "  int X;\n"
596                "} A2_t;",
597                AllowSimpleBracedStatements);
598   verifyFormat("template <int> struct A2 {\n"
599                "  struct B {};\n"
600                "};",
601                AllowSimpleBracedStatements);
602 
603   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
604       FormatStyle::SIS_Never;
605   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
606   verifyFormat("if (true) {\n"
607                "  f();\n"
608                "}",
609                AllowSimpleBracedStatements);
610   verifyFormat("if (true) {\n"
611                "  f();\n"
612                "} else {\n"
613                "  f();\n"
614                "}",
615                AllowSimpleBracedStatements);
616 
617   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
618   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
619   verifyFormat("while (true) {\n"
620                "  f();\n"
621                "}",
622                AllowSimpleBracedStatements);
623   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
624   verifyFormat("for (;;) {\n"
625                "  f();\n"
626                "}",
627                AllowSimpleBracedStatements);
628 
629   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
630       FormatStyle::SIS_WithoutElse;
631   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
632   AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = true;
633 
634   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
635   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
636   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
637   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
638   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
639   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
640   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
641   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
642   verifyFormat("if (true)\n"
643                "{\n"
644                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
645                "}",
646                AllowSimpleBracedStatements);
647   verifyFormat("if (true)\n"
648                "{ //\n"
649                "  f();\n"
650                "}",
651                AllowSimpleBracedStatements);
652   verifyFormat("if (true)\n"
653                "{\n"
654                "  f();\n"
655                "  f();\n"
656                "}",
657                AllowSimpleBracedStatements);
658   verifyFormat("if (true)\n"
659                "{\n"
660                "  f();\n"
661                "} else\n"
662                "{\n"
663                "  f();\n"
664                "}",
665                AllowSimpleBracedStatements);
666 
667   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
668       FormatStyle::SIS_Never;
669   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
670   verifyFormat("if (true)\n"
671                "{\n"
672                "  f();\n"
673                "}",
674                AllowSimpleBracedStatements);
675   verifyFormat("if (true)\n"
676                "{\n"
677                "  f();\n"
678                "} else\n"
679                "{\n"
680                "  f();\n"
681                "}",
682                AllowSimpleBracedStatements);
683 
684   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
685   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
686   verifyFormat("while (true)\n"
687                "{\n"
688                "  f();\n"
689                "}",
690                AllowSimpleBracedStatements);
691   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
692   verifyFormat("for (;;)\n"
693                "{\n"
694                "  f();\n"
695                "}",
696                AllowSimpleBracedStatements);
697 }
698 
699 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
700   FormatStyle Style = getLLVMStyleWithColumns(60);
701   Style.AllowShortBlocksOnASingleLine = true;
702   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
703   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
704   EXPECT_EQ("#define A                                                  \\\n"
705             "  if (HANDLEwernufrnuLwrmviferuvnierv)                     \\\n"
706             "  { RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; }\n"
707             "X;",
708             format("#define A \\\n"
709                    "   if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
710                    "      RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
711                    "   }\n"
712                    "X;",
713                    Style));
714 }
715 
716 TEST_F(FormatTest, ParseIfElse) {
717   verifyFormat("if (true)\n"
718                "  if (true)\n"
719                "    if (true)\n"
720                "      f();\n"
721                "    else\n"
722                "      g();\n"
723                "  else\n"
724                "    h();\n"
725                "else\n"
726                "  i();");
727   verifyFormat("if (true)\n"
728                "  if (true)\n"
729                "    if (true) {\n"
730                "      if (true)\n"
731                "        f();\n"
732                "    } else {\n"
733                "      g();\n"
734                "    }\n"
735                "  else\n"
736                "    h();\n"
737                "else {\n"
738                "  i();\n"
739                "}");
740   verifyFormat("if (true)\n"
741                "  if constexpr (true)\n"
742                "    if (true) {\n"
743                "      if constexpr (true)\n"
744                "        f();\n"
745                "    } else {\n"
746                "      g();\n"
747                "    }\n"
748                "  else\n"
749                "    h();\n"
750                "else {\n"
751                "  i();\n"
752                "}");
753   verifyFormat("void f() {\n"
754                "  if (a) {\n"
755                "  } else {\n"
756                "  }\n"
757                "}");
758 }
759 
760 TEST_F(FormatTest, ElseIf) {
761   verifyFormat("if (a) {\n} else if (b) {\n}");
762   verifyFormat("if (a)\n"
763                "  f();\n"
764                "else if (b)\n"
765                "  g();\n"
766                "else\n"
767                "  h();");
768   verifyFormat("if constexpr (a)\n"
769                "  f();\n"
770                "else if constexpr (b)\n"
771                "  g();\n"
772                "else\n"
773                "  h();");
774   verifyFormat("if (a) {\n"
775                "  f();\n"
776                "}\n"
777                "// or else ..\n"
778                "else {\n"
779                "  g()\n"
780                "}");
781 
782   verifyFormat("if (a) {\n"
783                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
784                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
785                "}");
786   verifyFormat("if (a) {\n"
787                "} else if (\n"
788                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
789                "}",
790                getLLVMStyleWithColumns(62));
791   verifyFormat("if (a) {\n"
792                "} else if constexpr (\n"
793                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
794                "}",
795                getLLVMStyleWithColumns(62));
796 }
797 
798 TEST_F(FormatTest, FormatsForLoop) {
799   verifyFormat(
800       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
801       "     ++VeryVeryLongLoopVariable)\n"
802       "  ;");
803   verifyFormat("for (;;)\n"
804                "  f();");
805   verifyFormat("for (;;) {\n}");
806   verifyFormat("for (;;) {\n"
807                "  f();\n"
808                "}");
809   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
810 
811   verifyFormat(
812       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
813       "                                          E = UnwrappedLines.end();\n"
814       "     I != E; ++I) {\n}");
815 
816   verifyFormat(
817       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
818       "     ++IIIII) {\n}");
819   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
820                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
821                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
822   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
823                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
824                "         E = FD->getDeclsInPrototypeScope().end();\n"
825                "     I != E; ++I) {\n}");
826   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
827                "         I = Container.begin(),\n"
828                "         E = Container.end();\n"
829                "     I != E; ++I) {\n}",
830                getLLVMStyleWithColumns(76));
831 
832   verifyFormat(
833       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
834       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
835       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
836       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
837       "     ++aaaaaaaaaaa) {\n}");
838   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
839                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
840                "     ++i) {\n}");
841   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
842                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
843                "}");
844   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
845                "         aaaaaaaaaa);\n"
846                "     iter; ++iter) {\n"
847                "}");
848   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
849                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
850                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
851                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
852 
853   // These should not be formatted as Objective-C for-in loops.
854   verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
855   verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
856   verifyFormat("Foo *x;\nfor (x in y) {\n}");
857   verifyFormat("for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
858 
859   FormatStyle NoBinPacking = getLLVMStyle();
860   NoBinPacking.BinPackParameters = false;
861   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
862                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
863                "                                           aaaaaaaaaaaaaaaa,\n"
864                "                                           aaaaaaaaaaaaaaaa,\n"
865                "                                           aaaaaaaaaaaaaaaa);\n"
866                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
867                "}",
868                NoBinPacking);
869   verifyFormat(
870       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
871       "                                          E = UnwrappedLines.end();\n"
872       "     I != E;\n"
873       "     ++I) {\n}",
874       NoBinPacking);
875 
876   FormatStyle AlignLeft = getLLVMStyle();
877   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
878   verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
879 }
880 
881 TEST_F(FormatTest, RangeBasedForLoops) {
882   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
883                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
884   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
885                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
886   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
887                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
888   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
889                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
890 }
891 
892 TEST_F(FormatTest, ForEachLoops) {
893   verifyFormat("void f() {\n"
894                "  foreach (Item *item, itemlist) {}\n"
895                "  Q_FOREACH (Item *item, itemlist) {}\n"
896                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
897                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
898                "}");
899 
900   // As function-like macros.
901   verifyFormat("#define foreach(x, y)\n"
902                "#define Q_FOREACH(x, y)\n"
903                "#define BOOST_FOREACH(x, y)\n"
904                "#define UNKNOWN_FOREACH(x, y)\n");
905 
906   // Not as function-like macros.
907   verifyFormat("#define foreach (x, y)\n"
908                "#define Q_FOREACH (x, y)\n"
909                "#define BOOST_FOREACH (x, y)\n"
910                "#define UNKNOWN_FOREACH (x, y)\n");
911 }
912 
913 TEST_F(FormatTest, FormatsWhileLoop) {
914   verifyFormat("while (true) {\n}");
915   verifyFormat("while (true)\n"
916                "  f();");
917   verifyFormat("while () {\n}");
918   verifyFormat("while () {\n"
919                "  f();\n"
920                "}");
921 }
922 
923 TEST_F(FormatTest, FormatsDoWhile) {
924   verifyFormat("do {\n"
925                "  do_something();\n"
926                "} while (something());");
927   verifyFormat("do\n"
928                "  do_something();\n"
929                "while (something());");
930 }
931 
932 TEST_F(FormatTest, FormatsSwitchStatement) {
933   verifyFormat("switch (x) {\n"
934                "case 1:\n"
935                "  f();\n"
936                "  break;\n"
937                "case kFoo:\n"
938                "case ns::kBar:\n"
939                "case kBaz:\n"
940                "  break;\n"
941                "default:\n"
942                "  g();\n"
943                "  break;\n"
944                "}");
945   verifyFormat("switch (x) {\n"
946                "case 1: {\n"
947                "  f();\n"
948                "  break;\n"
949                "}\n"
950                "case 2: {\n"
951                "  break;\n"
952                "}\n"
953                "}");
954   verifyFormat("switch (x) {\n"
955                "case 1: {\n"
956                "  f();\n"
957                "  {\n"
958                "    g();\n"
959                "    h();\n"
960                "  }\n"
961                "  break;\n"
962                "}\n"
963                "}");
964   verifyFormat("switch (x) {\n"
965                "case 1: {\n"
966                "  f();\n"
967                "  if (foo) {\n"
968                "    g();\n"
969                "    h();\n"
970                "  }\n"
971                "  break;\n"
972                "}\n"
973                "}");
974   verifyFormat("switch (x) {\n"
975                "case 1: {\n"
976                "  f();\n"
977                "  g();\n"
978                "} break;\n"
979                "}");
980   verifyFormat("switch (test)\n"
981                "  ;");
982   verifyFormat("switch (x) {\n"
983                "default: {\n"
984                "  // Do nothing.\n"
985                "}\n"
986                "}");
987   verifyFormat("switch (x) {\n"
988                "// comment\n"
989                "// if 1, do f()\n"
990                "case 1:\n"
991                "  f();\n"
992                "}");
993   verifyFormat("switch (x) {\n"
994                "case 1:\n"
995                "  // Do amazing stuff\n"
996                "  {\n"
997                "    f();\n"
998                "    g();\n"
999                "  }\n"
1000                "  break;\n"
1001                "}");
1002   verifyFormat("#define A          \\\n"
1003                "  switch (x) {     \\\n"
1004                "  case a:          \\\n"
1005                "    foo = b;       \\\n"
1006                "  }",
1007                getLLVMStyleWithColumns(20));
1008   verifyFormat("#define OPERATION_CASE(name)           \\\n"
1009                "  case OP_name:                        \\\n"
1010                "    return operations::Operation##name\n",
1011                getLLVMStyleWithColumns(40));
1012   verifyFormat("switch (x) {\n"
1013                "case 1:;\n"
1014                "default:;\n"
1015                "  int i;\n"
1016                "}");
1017 
1018   verifyGoogleFormat("switch (x) {\n"
1019                      "  case 1:\n"
1020                      "    f();\n"
1021                      "    break;\n"
1022                      "  case kFoo:\n"
1023                      "  case ns::kBar:\n"
1024                      "  case kBaz:\n"
1025                      "    break;\n"
1026                      "  default:\n"
1027                      "    g();\n"
1028                      "    break;\n"
1029                      "}");
1030   verifyGoogleFormat("switch (x) {\n"
1031                      "  case 1: {\n"
1032                      "    f();\n"
1033                      "    break;\n"
1034                      "  }\n"
1035                      "}");
1036   verifyGoogleFormat("switch (test)\n"
1037                      "  ;");
1038 
1039   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
1040                      "  case OP_name:              \\\n"
1041                      "    return operations::Operation##name\n");
1042   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
1043                      "  // Get the correction operation class.\n"
1044                      "  switch (OpCode) {\n"
1045                      "    CASE(Add);\n"
1046                      "    CASE(Subtract);\n"
1047                      "    default:\n"
1048                      "      return operations::Unknown;\n"
1049                      "  }\n"
1050                      "#undef OPERATION_CASE\n"
1051                      "}");
1052   verifyFormat("DEBUG({\n"
1053                "  switch (x) {\n"
1054                "  case A:\n"
1055                "    f();\n"
1056                "    break;\n"
1057                "    // fallthrough\n"
1058                "  case B:\n"
1059                "    g();\n"
1060                "    break;\n"
1061                "  }\n"
1062                "});");
1063   EXPECT_EQ("DEBUG({\n"
1064             "  switch (x) {\n"
1065             "  case A:\n"
1066             "    f();\n"
1067             "    break;\n"
1068             "  // On B:\n"
1069             "  case B:\n"
1070             "    g();\n"
1071             "    break;\n"
1072             "  }\n"
1073             "});",
1074             format("DEBUG({\n"
1075                    "  switch (x) {\n"
1076                    "  case A:\n"
1077                    "    f();\n"
1078                    "    break;\n"
1079                    "  // On B:\n"
1080                    "  case B:\n"
1081                    "    g();\n"
1082                    "    break;\n"
1083                    "  }\n"
1084                    "});",
1085                    getLLVMStyle()));
1086   EXPECT_EQ("switch (n) {\n"
1087             "case 0: {\n"
1088             "  return false;\n"
1089             "}\n"
1090             "default: {\n"
1091             "  return true;\n"
1092             "}\n"
1093             "}",
1094             format("switch (n)\n"
1095                    "{\n"
1096                    "case 0: {\n"
1097                    "  return false;\n"
1098                    "}\n"
1099                    "default: {\n"
1100                    "  return true;\n"
1101                    "}\n"
1102                    "}",
1103                    getLLVMStyle()));
1104   verifyFormat("switch (a) {\n"
1105                "case (b):\n"
1106                "  return;\n"
1107                "}");
1108 
1109   verifyFormat("switch (a) {\n"
1110                "case some_namespace::\n"
1111                "    some_constant:\n"
1112                "  return;\n"
1113                "}",
1114                getLLVMStyleWithColumns(34));
1115 
1116   FormatStyle Style = getLLVMStyle();
1117   Style.IndentCaseLabels = true;
1118   Style.AllowShortBlocksOnASingleLine = false;
1119   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1120   Style.BraceWrapping.AfterControlStatement = true;
1121   EXPECT_EQ("switch (n)\n"
1122             "{\n"
1123             "  case 0:\n"
1124             "  {\n"
1125             "    return false;\n"
1126             "  }\n"
1127             "  default:\n"
1128             "  {\n"
1129             "    return true;\n"
1130             "  }\n"
1131             "}",
1132             format("switch (n) {\n"
1133                    "  case 0: {\n"
1134                    "    return false;\n"
1135                    "  }\n"
1136                    "  default: {\n"
1137                    "    return true;\n"
1138                    "  }\n"
1139                    "}",
1140                    Style));
1141 }
1142 
1143 TEST_F(FormatTest, CaseRanges) {
1144   verifyFormat("switch (x) {\n"
1145                "case 'A' ... 'Z':\n"
1146                "case 1 ... 5:\n"
1147                "case a ... b:\n"
1148                "  break;\n"
1149                "}");
1150 }
1151 
1152 TEST_F(FormatTest, ShortCaseLabels) {
1153   FormatStyle Style = getLLVMStyle();
1154   Style.AllowShortCaseLabelsOnASingleLine = true;
1155   verifyFormat("switch (a) {\n"
1156                "case 1: x = 1; break;\n"
1157                "case 2: return;\n"
1158                "case 3:\n"
1159                "case 4:\n"
1160                "case 5: return;\n"
1161                "case 6: // comment\n"
1162                "  return;\n"
1163                "case 7:\n"
1164                "  // comment\n"
1165                "  return;\n"
1166                "case 8:\n"
1167                "  x = 8; // comment\n"
1168                "  break;\n"
1169                "default: y = 1; break;\n"
1170                "}",
1171                Style);
1172   verifyFormat("switch (a) {\n"
1173                "case 0: return; // comment\n"
1174                "case 1: break;  // comment\n"
1175                "case 2: return;\n"
1176                "// comment\n"
1177                "case 3: return;\n"
1178                "// comment 1\n"
1179                "// comment 2\n"
1180                "// comment 3\n"
1181                "case 4: break; /* comment */\n"
1182                "case 5:\n"
1183                "  // comment\n"
1184                "  break;\n"
1185                "case 6: /* comment */ x = 1; break;\n"
1186                "case 7: x = /* comment */ 1; break;\n"
1187                "case 8:\n"
1188                "  x = 1; /* comment */\n"
1189                "  break;\n"
1190                "case 9:\n"
1191                "  break; // comment line 1\n"
1192                "         // comment line 2\n"
1193                "}",
1194                Style);
1195   EXPECT_EQ("switch (a) {\n"
1196             "case 1:\n"
1197             "  x = 8;\n"
1198             "  // fall through\n"
1199             "case 2: x = 8;\n"
1200             "// comment\n"
1201             "case 3:\n"
1202             "  return; /* comment line 1\n"
1203             "           * comment line 2 */\n"
1204             "case 4: i = 8;\n"
1205             "// something else\n"
1206             "#if FOO\n"
1207             "case 5: break;\n"
1208             "#endif\n"
1209             "}",
1210             format("switch (a) {\n"
1211                    "case 1: x = 8;\n"
1212                    "  // fall through\n"
1213                    "case 2:\n"
1214                    "  x = 8;\n"
1215                    "// comment\n"
1216                    "case 3:\n"
1217                    "  return; /* comment line 1\n"
1218                    "           * comment line 2 */\n"
1219                    "case 4:\n"
1220                    "  i = 8;\n"
1221                    "// something else\n"
1222                    "#if FOO\n"
1223                    "case 5: break;\n"
1224                    "#endif\n"
1225                    "}",
1226                    Style));
1227   EXPECT_EQ("switch (a) {\n" "case 0:\n"
1228             "  return; // long long long long long long long long long long long long comment\n"
1229             "          // line\n" "}",
1230             format("switch (a) {\n"
1231                    "case 0: return; // long long long long long long long long long long long long comment line\n"
1232                    "}",
1233                    Style));
1234   EXPECT_EQ("switch (a) {\n"
1235             "case 0:\n"
1236             "  return; /* long long long long long long long long long long long long comment\n"
1237             "             line */\n"
1238             "}",
1239             format("switch (a) {\n"
1240                    "case 0: return; /* long long long long long long long long long long long long comment line */\n"
1241                    "}",
1242                    Style));
1243   verifyFormat("switch (a) {\n"
1244                "#if FOO\n"
1245                "case 0: return 0;\n"
1246                "#endif\n"
1247                "}",
1248                Style);
1249   verifyFormat("switch (a) {\n"
1250                "case 1: {\n"
1251                "}\n"
1252                "case 2: {\n"
1253                "  return;\n"
1254                "}\n"
1255                "case 3: {\n"
1256                "  x = 1;\n"
1257                "  return;\n"
1258                "}\n"
1259                "case 4:\n"
1260                "  if (x)\n"
1261                "    return;\n"
1262                "}",
1263                Style);
1264   Style.ColumnLimit = 21;
1265   verifyFormat("switch (a) {\n"
1266                "case 1: x = 1; break;\n"
1267                "case 2: return;\n"
1268                "case 3:\n"
1269                "case 4:\n"
1270                "case 5: return;\n"
1271                "default:\n"
1272                "  y = 1;\n"
1273                "  break;\n"
1274                "}",
1275                Style);
1276   Style.ColumnLimit = 80;
1277   Style.AllowShortCaseLabelsOnASingleLine = false;
1278   Style.IndentCaseLabels = true;
1279   EXPECT_EQ("switch (n) {\n"
1280             "  default /*comments*/:\n"
1281             "    return true;\n"
1282             "  case 0:\n"
1283             "    return false;\n"
1284             "}",
1285             format("switch (n) {\n"
1286                    "default/*comments*/:\n"
1287                    "  return true;\n"
1288                    "case 0:\n"
1289                    "  return false;\n"
1290                    "}",
1291                    Style));
1292   Style.AllowShortCaseLabelsOnASingleLine = true;
1293   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1294   Style.BraceWrapping.AfterControlStatement = true;
1295   EXPECT_EQ("switch (n)\n"
1296             "{\n"
1297             "  case 0:\n"
1298             "  {\n"
1299             "    return false;\n"
1300             "  }\n"
1301             "  default:\n"
1302             "  {\n"
1303             "    return true;\n"
1304             "  }\n"
1305             "}",
1306             format("switch (n) {\n"
1307                    "  case 0: {\n"
1308                    "    return false;\n"
1309                    "  }\n"
1310                    "  default:\n"
1311                    "  {\n"
1312                    "    return true;\n"
1313                    "  }\n"
1314                    "}",
1315                    Style));
1316 }
1317 
1318 TEST_F(FormatTest, FormatsLabels) {
1319   verifyFormat("void f() {\n"
1320                "  some_code();\n"
1321                "test_label:\n"
1322                "  some_other_code();\n"
1323                "  {\n"
1324                "    some_more_code();\n"
1325                "  another_label:\n"
1326                "    some_more_code();\n"
1327                "  }\n"
1328                "}");
1329   verifyFormat("{\n"
1330                "  some_code();\n"
1331                "test_label:\n"
1332                "  some_other_code();\n"
1333                "}");
1334   verifyFormat("{\n"
1335                "  some_code();\n"
1336                "test_label:;\n"
1337                "  int i = 0;\n"
1338                "}");
1339 }
1340 
1341 //===----------------------------------------------------------------------===//
1342 // Tests for classes, namespaces, etc.
1343 //===----------------------------------------------------------------------===//
1344 
1345 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1346   verifyFormat("class A {};");
1347 }
1348 
1349 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1350   verifyFormat("class A {\n"
1351                "public:\n"
1352                "public: // comment\n"
1353                "protected:\n"
1354                "private:\n"
1355                "  void f() {}\n"
1356                "};");
1357   verifyFormat("export class A {\n"
1358                "public:\n"
1359                "public: // comment\n"
1360                "protected:\n"
1361                "private:\n"
1362                "  void f() {}\n"
1363                "};");
1364   verifyGoogleFormat("class A {\n"
1365                      " public:\n"
1366                      " protected:\n"
1367                      " private:\n"
1368                      "  void f() {}\n"
1369                      "};");
1370   verifyGoogleFormat("export class A {\n"
1371                      " public:\n"
1372                      " protected:\n"
1373                      " private:\n"
1374                      "  void f() {}\n"
1375                      "};");
1376   verifyFormat("class A {\n"
1377                "public slots:\n"
1378                "  void f1() {}\n"
1379                "public Q_SLOTS:\n"
1380                "  void f2() {}\n"
1381                "protected slots:\n"
1382                "  void f3() {}\n"
1383                "protected Q_SLOTS:\n"
1384                "  void f4() {}\n"
1385                "private slots:\n"
1386                "  void f5() {}\n"
1387                "private Q_SLOTS:\n"
1388                "  void f6() {}\n"
1389                "signals:\n"
1390                "  void g1();\n"
1391                "Q_SIGNALS:\n"
1392                "  void g2();\n"
1393                "};");
1394 
1395   // Don't interpret 'signals' the wrong way.
1396   verifyFormat("signals.set();");
1397   verifyFormat("for (Signals signals : f()) {\n}");
1398   verifyFormat("{\n"
1399                "  signals.set(); // This needs indentation.\n"
1400                "}");
1401   verifyFormat("void f() {\n"
1402                "label:\n"
1403                "  signals.baz();\n"
1404                "}");
1405 }
1406 
1407 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1408   EXPECT_EQ("class A {\n"
1409             "public:\n"
1410             "  void f();\n"
1411             "\n"
1412             "private:\n"
1413             "  void g() {}\n"
1414             "  // test\n"
1415             "protected:\n"
1416             "  int h;\n"
1417             "};",
1418             format("class A {\n"
1419                    "public:\n"
1420                    "void f();\n"
1421                    "private:\n"
1422                    "void g() {}\n"
1423                    "// test\n"
1424                    "protected:\n"
1425                    "int h;\n"
1426                    "};"));
1427   EXPECT_EQ("class A {\n"
1428             "protected:\n"
1429             "public:\n"
1430             "  void f();\n"
1431             "};",
1432             format("class A {\n"
1433                    "protected:\n"
1434                    "\n"
1435                    "public:\n"
1436                    "\n"
1437                    "  void f();\n"
1438                    "};"));
1439 
1440   // Even ensure proper spacing inside macros.
1441   EXPECT_EQ("#define B     \\\n"
1442             "  class A {   \\\n"
1443             "   protected: \\\n"
1444             "   public:    \\\n"
1445             "    void f(); \\\n"
1446             "  };",
1447             format("#define B     \\\n"
1448                    "  class A {   \\\n"
1449                    "   protected: \\\n"
1450                    "              \\\n"
1451                    "   public:    \\\n"
1452                    "              \\\n"
1453                    "    void f(); \\\n"
1454                    "  };",
1455                    getGoogleStyle()));
1456   // But don't remove empty lines after macros ending in access specifiers.
1457   EXPECT_EQ("#define A private:\n"
1458             "\n"
1459             "int i;",
1460             format("#define A         private:\n"
1461                    "\n"
1462                    "int              i;"));
1463 }
1464 
1465 TEST_F(FormatTest, FormatsClasses) {
1466   verifyFormat("class A : public B {};");
1467   verifyFormat("class A : public ::B {};");
1468 
1469   verifyFormat(
1470       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1471       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1472   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1473                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1474                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1475   verifyFormat(
1476       "class A : public B, public C, public D, public E, public F {};");
1477   verifyFormat("class AAAAAAAAAAAA : public B,\n"
1478                "                     public C,\n"
1479                "                     public D,\n"
1480                "                     public E,\n"
1481                "                     public F,\n"
1482                "                     public G {};");
1483 
1484   verifyFormat("class\n"
1485                "    ReallyReallyLongClassName {\n"
1486                "  int i;\n"
1487                "};",
1488                getLLVMStyleWithColumns(32));
1489   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
1490                "                           aaaaaaaaaaaaaaaa> {};");
1491   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
1492                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
1493                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
1494   verifyFormat("template <class R, class C>\n"
1495                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
1496                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
1497   verifyFormat("class ::A::B {};");
1498 }
1499 
1500 TEST_F(FormatTest, BreakInheritanceStyle) {
1501   FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
1502   StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
1503           FormatStyle::BILS_BeforeComma;
1504   verifyFormat("class MyClass : public X {};",
1505                StyleWithInheritanceBreakBeforeComma);
1506   verifyFormat("class MyClass\n"
1507                "    : public X\n"
1508                "    , public Y {};",
1509                StyleWithInheritanceBreakBeforeComma);
1510   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
1511                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
1512                "    , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
1513                StyleWithInheritanceBreakBeforeComma);
1514   verifyFormat("struct aaaaaaaaaaaaa\n"
1515                "    : public aaaaaaaaaaaaaaaaaaa< // break\n"
1516                "          aaaaaaaaaaaaaaaa> {};",
1517                StyleWithInheritanceBreakBeforeComma);
1518 
1519   FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
1520   StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
1521           FormatStyle::BILS_AfterColon;
1522   verifyFormat("class MyClass : public X {};",
1523                StyleWithInheritanceBreakAfterColon);
1524   verifyFormat("class MyClass : public X, public Y {};",
1525                StyleWithInheritanceBreakAfterColon);
1526   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
1527                "    public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1528                "    public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
1529                StyleWithInheritanceBreakAfterColon);
1530   verifyFormat("struct aaaaaaaaaaaaa :\n"
1531                "    public aaaaaaaaaaaaaaaaaaa< // break\n"
1532                "        aaaaaaaaaaaaaaaa> {};",
1533                StyleWithInheritanceBreakAfterColon);
1534 }
1535 
1536 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1537   verifyFormat("class A {\n} a, b;");
1538   verifyFormat("struct A {\n} a, b;");
1539   verifyFormat("union A {\n} a;");
1540 }
1541 
1542 TEST_F(FormatTest, FormatsEnum) {
1543   verifyFormat("enum {\n"
1544                "  Zero,\n"
1545                "  One = 1,\n"
1546                "  Two = One + 1,\n"
1547                "  Three = (One + Two),\n"
1548                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1549                "  Five = (One, Two, Three, Four, 5)\n"
1550                "};");
1551   verifyGoogleFormat("enum {\n"
1552                      "  Zero,\n"
1553                      "  One = 1,\n"
1554                      "  Two = One + 1,\n"
1555                      "  Three = (One + Two),\n"
1556                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1557                      "  Five = (One, Two, Three, Four, 5)\n"
1558                      "};");
1559   verifyFormat("enum Enum {};");
1560   verifyFormat("enum {};");
1561   verifyFormat("enum X E {} d;");
1562   verifyFormat("enum __attribute__((...)) E {} d;");
1563   verifyFormat("enum __declspec__((...)) E {} d;");
1564   verifyFormat("enum {\n"
1565                "  Bar = Foo<int, int>::value\n"
1566                "};",
1567                getLLVMStyleWithColumns(30));
1568 
1569   verifyFormat("enum ShortEnum { A, B, C };");
1570   verifyGoogleFormat("enum ShortEnum { A, B, C };");
1571 
1572   EXPECT_EQ("enum KeepEmptyLines {\n"
1573             "  ONE,\n"
1574             "\n"
1575             "  TWO,\n"
1576             "\n"
1577             "  THREE\n"
1578             "}",
1579             format("enum KeepEmptyLines {\n"
1580                    "  ONE,\n"
1581                    "\n"
1582                    "  TWO,\n"
1583                    "\n"
1584                    "\n"
1585                    "  THREE\n"
1586                    "}"));
1587   verifyFormat("enum E { // comment\n"
1588                "  ONE,\n"
1589                "  TWO\n"
1590                "};\n"
1591                "int i;");
1592   // Not enums.
1593   verifyFormat("enum X f() {\n"
1594                "  a();\n"
1595                "  return 42;\n"
1596                "}");
1597   verifyFormat("enum X Type::f() {\n"
1598                "  a();\n"
1599                "  return 42;\n"
1600                "}");
1601   verifyFormat("enum ::X f() {\n"
1602                "  a();\n"
1603                "  return 42;\n"
1604                "}");
1605   verifyFormat("enum ns::X f() {\n"
1606                "  a();\n"
1607                "  return 42;\n"
1608                "}");
1609 }
1610 
1611 TEST_F(FormatTest, FormatsEnumsWithErrors) {
1612   verifyFormat("enum Type {\n"
1613                "  One = 0; // These semicolons should be commas.\n"
1614                "  Two = 1;\n"
1615                "};");
1616   verifyFormat("namespace n {\n"
1617                "enum Type {\n"
1618                "  One,\n"
1619                "  Two, // missing };\n"
1620                "  int i;\n"
1621                "}\n"
1622                "void g() {}");
1623 }
1624 
1625 TEST_F(FormatTest, FormatsEnumStruct) {
1626   verifyFormat("enum struct {\n"
1627                "  Zero,\n"
1628                "  One = 1,\n"
1629                "  Two = One + 1,\n"
1630                "  Three = (One + Two),\n"
1631                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1632                "  Five = (One, Two, Three, Four, 5)\n"
1633                "};");
1634   verifyFormat("enum struct Enum {};");
1635   verifyFormat("enum struct {};");
1636   verifyFormat("enum struct X E {} d;");
1637   verifyFormat("enum struct __attribute__((...)) E {} d;");
1638   verifyFormat("enum struct __declspec__((...)) E {} d;");
1639   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
1640 }
1641 
1642 TEST_F(FormatTest, FormatsEnumClass) {
1643   verifyFormat("enum class {\n"
1644                "  Zero,\n"
1645                "  One = 1,\n"
1646                "  Two = One + 1,\n"
1647                "  Three = (One + Two),\n"
1648                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1649                "  Five = (One, Two, Three, Four, 5)\n"
1650                "};");
1651   verifyFormat("enum class Enum {};");
1652   verifyFormat("enum class {};");
1653   verifyFormat("enum class X E {} d;");
1654   verifyFormat("enum class __attribute__((...)) E {} d;");
1655   verifyFormat("enum class __declspec__((...)) E {} d;");
1656   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
1657 }
1658 
1659 TEST_F(FormatTest, FormatsEnumTypes) {
1660   verifyFormat("enum X : int {\n"
1661                "  A, // Force multiple lines.\n"
1662                "  B\n"
1663                "};");
1664   verifyFormat("enum X : int { A, B };");
1665   verifyFormat("enum X : std::uint32_t { A, B };");
1666 }
1667 
1668 TEST_F(FormatTest, FormatsTypedefEnum) {
1669   FormatStyle Style = getLLVMStyle();
1670   Style.ColumnLimit = 40;
1671   verifyFormat("typedef enum {} EmptyEnum;");
1672   verifyFormat("typedef enum { A, B, C } ShortEnum;");
1673   verifyFormat("typedef enum {\n"
1674                "  ZERO = 0,\n"
1675                "  ONE = 1,\n"
1676                "  TWO = 2,\n"
1677                "  THREE = 3\n"
1678                "} LongEnum;",
1679                Style);
1680   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1681   Style.BraceWrapping.AfterEnum = true;
1682   verifyFormat("typedef enum {} EmptyEnum;");
1683   verifyFormat("typedef enum { A, B, C } ShortEnum;");
1684   verifyFormat("typedef enum\n"
1685                "{\n"
1686                "  ZERO = 0,\n"
1687                "  ONE = 1,\n"
1688                "  TWO = 2,\n"
1689                "  THREE = 3\n"
1690                "} LongEnum;",
1691                Style);
1692 }
1693 
1694 TEST_F(FormatTest, FormatsNSEnums) {
1695   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
1696   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
1697                      "  // Information about someDecentlyLongValue.\n"
1698                      "  someDecentlyLongValue,\n"
1699                      "  // Information about anotherDecentlyLongValue.\n"
1700                      "  anotherDecentlyLongValue,\n"
1701                      "  // Information about aThirdDecentlyLongValue.\n"
1702                      "  aThirdDecentlyLongValue\n"
1703                      "};");
1704   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
1705                      "  a = 1,\n"
1706                      "  b = 2,\n"
1707                      "  c = 3,\n"
1708                      "};");
1709   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
1710                      "  a = 1,\n"
1711                      "  b = 2,\n"
1712                      "  c = 3,\n"
1713                      "};");
1714   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
1715                      "  a = 1,\n"
1716                      "  b = 2,\n"
1717                      "  c = 3,\n"
1718                      "};");
1719 }
1720 
1721 TEST_F(FormatTest, FormatsBitfields) {
1722   verifyFormat("struct Bitfields {\n"
1723                "  unsigned sClass : 8;\n"
1724                "  unsigned ValueKind : 2;\n"
1725                "};");
1726   verifyFormat("struct A {\n"
1727                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
1728                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
1729                "};");
1730   verifyFormat("struct MyStruct {\n"
1731                "  uchar data;\n"
1732                "  uchar : 8;\n"
1733                "  uchar : 8;\n"
1734                "  uchar other;\n"
1735                "};");
1736 }
1737 
1738 TEST_F(FormatTest, FormatsNamespaces) {
1739   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
1740   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
1741 
1742   verifyFormat("namespace some_namespace {\n"
1743                "class A {};\n"
1744                "void f() { f(); }\n"
1745                "}",
1746                LLVMWithNoNamespaceFix);
1747   verifyFormat("/* something */ namespace some_namespace {\n"
1748                "class A {};\n"
1749                "void f() { f(); }\n"
1750                "}",
1751                LLVMWithNoNamespaceFix);
1752   verifyFormat("namespace {\n"
1753                "class A {};\n"
1754                "void f() { f(); }\n"
1755                "}",
1756                LLVMWithNoNamespaceFix);
1757   verifyFormat("/* something */ namespace {\n"
1758                "class A {};\n"
1759                "void f() { f(); }\n"
1760                "}",
1761                LLVMWithNoNamespaceFix);
1762   verifyFormat("inline namespace X {\n"
1763                "class A {};\n"
1764                "void f() { f(); }\n"
1765                "}",
1766                LLVMWithNoNamespaceFix);
1767   verifyFormat("/* something */ inline namespace X {\n"
1768                "class A {};\n"
1769                "void f() { f(); }\n"
1770                "}",
1771                LLVMWithNoNamespaceFix);
1772   verifyFormat("export namespace X {\n"
1773                "class A {};\n"
1774                "void f() { f(); }\n"
1775                "}",
1776                LLVMWithNoNamespaceFix);
1777   verifyFormat("using namespace some_namespace;\n"
1778                "class A {};\n"
1779                "void f() { f(); }",
1780                LLVMWithNoNamespaceFix);
1781 
1782   // This code is more common than we thought; if we
1783   // layout this correctly the semicolon will go into
1784   // its own line, which is undesirable.
1785   verifyFormat("namespace {};",
1786                LLVMWithNoNamespaceFix);
1787   verifyFormat("namespace {\n"
1788                "class A {};\n"
1789                "};",
1790                LLVMWithNoNamespaceFix);
1791 
1792   verifyFormat("namespace {\n"
1793                "int SomeVariable = 0; // comment\n"
1794                "} // namespace",
1795                LLVMWithNoNamespaceFix);
1796   EXPECT_EQ("#ifndef HEADER_GUARD\n"
1797             "#define HEADER_GUARD\n"
1798             "namespace my_namespace {\n"
1799             "int i;\n"
1800             "} // my_namespace\n"
1801             "#endif // HEADER_GUARD",
1802             format("#ifndef HEADER_GUARD\n"
1803                    " #define HEADER_GUARD\n"
1804                    "   namespace my_namespace {\n"
1805                    "int i;\n"
1806                    "}    // my_namespace\n"
1807                    "#endif    // HEADER_GUARD",
1808                    LLVMWithNoNamespaceFix));
1809 
1810   EXPECT_EQ("namespace A::B {\n"
1811             "class C {};\n"
1812             "}",
1813             format("namespace A::B {\n"
1814                    "class C {};\n"
1815                    "}",
1816                    LLVMWithNoNamespaceFix));
1817 
1818   FormatStyle Style = getLLVMStyle();
1819   Style.NamespaceIndentation = FormatStyle::NI_All;
1820   EXPECT_EQ("namespace out {\n"
1821             "  int i;\n"
1822             "  namespace in {\n"
1823             "    int i;\n"
1824             "  } // namespace in\n"
1825             "} // namespace out",
1826             format("namespace out {\n"
1827                    "int i;\n"
1828                    "namespace in {\n"
1829                    "int i;\n"
1830                    "} // namespace in\n"
1831                    "} // namespace out",
1832                    Style));
1833 
1834   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1835   EXPECT_EQ("namespace out {\n"
1836             "int i;\n"
1837             "namespace in {\n"
1838             "  int i;\n"
1839             "} // namespace in\n"
1840             "} // namespace out",
1841             format("namespace out {\n"
1842                    "int i;\n"
1843                    "namespace in {\n"
1844                    "int i;\n"
1845                    "} // namespace in\n"
1846                    "} // namespace out",
1847                    Style));
1848 }
1849 
1850 TEST_F(FormatTest, FormatsCompactNamespaces) {
1851   FormatStyle Style = getLLVMStyle();
1852   Style.CompactNamespaces = true;
1853 
1854   verifyFormat("namespace A { namespace B {\n"
1855 			   "}} // namespace A::B",
1856 			   Style);
1857 
1858   EXPECT_EQ("namespace out { namespace in {\n"
1859             "}} // namespace out::in",
1860             format("namespace out {\n"
1861                    "namespace in {\n"
1862                    "} // namespace in\n"
1863                    "} // namespace out",
1864                    Style));
1865 
1866   // Only namespaces which have both consecutive opening and end get compacted
1867   EXPECT_EQ("namespace out {\n"
1868             "namespace in1 {\n"
1869             "} // namespace in1\n"
1870             "namespace in2 {\n"
1871             "} // namespace in2\n"
1872             "} // namespace out",
1873             format("namespace out {\n"
1874                    "namespace in1 {\n"
1875                    "} // namespace in1\n"
1876                    "namespace in2 {\n"
1877                    "} // namespace in2\n"
1878                    "} // namespace out",
1879                    Style));
1880 
1881   EXPECT_EQ("namespace out {\n"
1882             "int i;\n"
1883             "namespace in {\n"
1884             "int j;\n"
1885             "} // namespace in\n"
1886             "int k;\n"
1887             "} // namespace out",
1888             format("namespace out { int i;\n"
1889                    "namespace in { int j; } // namespace in\n"
1890                    "int k; } // namespace out",
1891                    Style));
1892 
1893   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
1894             "}}} // namespace A::B::C\n",
1895             format("namespace A { namespace B {\n"
1896                    "namespace C {\n"
1897                    "}} // namespace B::C\n"
1898                    "} // namespace A\n",
1899                    Style));
1900 
1901   Style.ColumnLimit = 40;
1902   EXPECT_EQ("namespace aaaaaaaaaa {\n"
1903             "namespace bbbbbbbbbb {\n"
1904             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
1905             format("namespace aaaaaaaaaa {\n"
1906                    "namespace bbbbbbbbbb {\n"
1907                    "} // namespace bbbbbbbbbb\n"
1908                    "} // namespace aaaaaaaaaa",
1909                    Style));
1910 
1911   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
1912             "namespace cccccc {\n"
1913             "}}} // namespace aaaaaa::bbbbbb::cccccc",
1914             format("namespace aaaaaa {\n"
1915                    "namespace bbbbbb {\n"
1916                    "namespace cccccc {\n"
1917                    "} // namespace cccccc\n"
1918                    "} // namespace bbbbbb\n"
1919                    "} // namespace aaaaaa",
1920                    Style));
1921   Style.ColumnLimit = 80;
1922 
1923   // Extra semicolon after 'inner' closing brace prevents merging
1924   EXPECT_EQ("namespace out { namespace in {\n"
1925             "}; } // namespace out::in",
1926             format("namespace out {\n"
1927                    "namespace in {\n"
1928                    "}; // namespace in\n"
1929                    "} // namespace out",
1930                    Style));
1931 
1932   // Extra semicolon after 'outer' closing brace is conserved
1933   EXPECT_EQ("namespace out { namespace in {\n"
1934             "}}; // namespace out::in",
1935             format("namespace out {\n"
1936                    "namespace in {\n"
1937                    "} // namespace in\n"
1938                    "}; // namespace out",
1939                    Style));
1940 
1941   Style.NamespaceIndentation = FormatStyle::NI_All;
1942   EXPECT_EQ("namespace out { namespace in {\n"
1943             "  int i;\n"
1944             "}} // namespace out::in",
1945             format("namespace out {\n"
1946                    "namespace in {\n"
1947                    "int i;\n"
1948                    "} // namespace in\n"
1949                    "} // namespace out",
1950                    Style));
1951   EXPECT_EQ("namespace out { namespace mid {\n"
1952             "  namespace in {\n"
1953             "    int j;\n"
1954             "  } // namespace in\n"
1955             "  int k;\n"
1956             "}} // namespace out::mid",
1957             format("namespace out { namespace mid {\n"
1958                    "namespace in { int j; } // namespace in\n"
1959                    "int k; }} // namespace out::mid",
1960                    Style));
1961 
1962   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1963   EXPECT_EQ("namespace out { namespace in {\n"
1964             "  int i;\n"
1965             "}} // namespace out::in",
1966             format("namespace out {\n"
1967                    "namespace in {\n"
1968                    "int i;\n"
1969                    "} // namespace in\n"
1970                    "} // namespace out",
1971                    Style));
1972   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
1973             "  int i;\n"
1974             "}}} // namespace out::mid::in",
1975             format("namespace out {\n"
1976                    "namespace mid {\n"
1977                    "namespace in {\n"
1978                    "int i;\n"
1979                    "} // namespace in\n"
1980                    "} // namespace mid\n"
1981                    "} // namespace out",
1982                    Style));
1983 }
1984 
1985 TEST_F(FormatTest, FormatsExternC) {
1986   verifyFormat("extern \"C\" {\nint a;");
1987   verifyFormat("extern \"C\" {}");
1988   verifyFormat("extern \"C\" {\n"
1989                "int foo();\n"
1990                "}");
1991   verifyFormat("extern \"C\" int foo() {}");
1992   verifyFormat("extern \"C\" int foo();");
1993   verifyFormat("extern \"C\" int foo() {\n"
1994                "  int i = 42;\n"
1995                "  return i;\n"
1996                "}");
1997 
1998   FormatStyle Style = getLLVMStyle();
1999   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2000   Style.BraceWrapping.AfterFunction = true;
2001   verifyFormat("extern \"C\" int foo() {}", Style);
2002   verifyFormat("extern \"C\" int foo();", Style);
2003   verifyFormat("extern \"C\" int foo()\n"
2004                "{\n"
2005                "  int i = 42;\n"
2006                "  return i;\n"
2007                "}",
2008                Style);
2009 
2010   Style.BraceWrapping.AfterExternBlock = true;
2011   Style.BraceWrapping.SplitEmptyRecord = false;
2012   verifyFormat("extern \"C\"\n"
2013                "{}",
2014                Style);
2015   verifyFormat("extern \"C\"\n"
2016                "{\n"
2017                "  int foo();\n"
2018                "}",
2019                Style);
2020 }
2021 
2022 TEST_F(FormatTest, FormatsInlineASM) {
2023   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
2024   verifyFormat("asm(\"nop\" ::: \"memory\");");
2025   verifyFormat(
2026       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
2027       "    \"cpuid\\n\\t\"\n"
2028       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
2029       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
2030       "    : \"a\"(value));");
2031   EXPECT_EQ(
2032       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
2033       "  __asm {\n"
2034       "        mov     edx,[that] // vtable in edx\n"
2035       "        mov     eax,methodIndex\n"
2036       "        call    [edx][eax*4] // stdcall\n"
2037       "  }\n"
2038       "}",
2039       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
2040              "    __asm {\n"
2041              "        mov     edx,[that] // vtable in edx\n"
2042              "        mov     eax,methodIndex\n"
2043              "        call    [edx][eax*4] // stdcall\n"
2044              "    }\n"
2045              "}"));
2046   EXPECT_EQ("_asm {\n"
2047             "  xor eax, eax;\n"
2048             "  cpuid;\n"
2049             "}",
2050             format("_asm {\n"
2051                    "  xor eax, eax;\n"
2052                    "  cpuid;\n"
2053                    "}"));
2054   verifyFormat("void function() {\n"
2055                "  // comment\n"
2056                "  asm(\"\");\n"
2057                "}");
2058   EXPECT_EQ("__asm {\n"
2059             "}\n"
2060             "int i;",
2061             format("__asm   {\n"
2062                    "}\n"
2063                    "int   i;"));
2064 }
2065 
2066 TEST_F(FormatTest, FormatTryCatch) {
2067   verifyFormat("try {\n"
2068                "  throw a * b;\n"
2069                "} catch (int a) {\n"
2070                "  // Do nothing.\n"
2071                "} catch (...) {\n"
2072                "  exit(42);\n"
2073                "}");
2074 
2075   // Function-level try statements.
2076   verifyFormat("int f() try { return 4; } catch (...) {\n"
2077                "  return 5;\n"
2078                "}");
2079   verifyFormat("class A {\n"
2080                "  int a;\n"
2081                "  A() try : a(0) {\n"
2082                "  } catch (...) {\n"
2083                "    throw;\n"
2084                "  }\n"
2085                "};\n");
2086 
2087   // Incomplete try-catch blocks.
2088   verifyIncompleteFormat("try {} catch (");
2089 }
2090 
2091 TEST_F(FormatTest, FormatSEHTryCatch) {
2092   verifyFormat("__try {\n"
2093                "  int a = b * c;\n"
2094                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
2095                "  // Do nothing.\n"
2096                "}");
2097 
2098   verifyFormat("__try {\n"
2099                "  int a = b * c;\n"
2100                "} __finally {\n"
2101                "  // Do nothing.\n"
2102                "}");
2103 
2104   verifyFormat("DEBUG({\n"
2105                "  __try {\n"
2106                "  } __finally {\n"
2107                "  }\n"
2108                "});\n");
2109 }
2110 
2111 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
2112   verifyFormat("try {\n"
2113                "  f();\n"
2114                "} catch {\n"
2115                "  g();\n"
2116                "}");
2117   verifyFormat("try {\n"
2118                "  f();\n"
2119                "} catch (A a) MACRO(x) {\n"
2120                "  g();\n"
2121                "} catch (B b) MACRO(x) {\n"
2122                "  g();\n"
2123                "}");
2124 }
2125 
2126 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
2127   FormatStyle Style = getLLVMStyle();
2128   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
2129                           FormatStyle::BS_WebKit}) {
2130     Style.BreakBeforeBraces = BraceStyle;
2131     verifyFormat("try {\n"
2132                  "  // something\n"
2133                  "} catch (...) {\n"
2134                  "  // something\n"
2135                  "}",
2136                  Style);
2137   }
2138   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
2139   verifyFormat("try {\n"
2140                "  // something\n"
2141                "}\n"
2142                "catch (...) {\n"
2143                "  // something\n"
2144                "}",
2145                Style);
2146   verifyFormat("__try {\n"
2147                "  // something\n"
2148                "}\n"
2149                "__finally {\n"
2150                "  // something\n"
2151                "}",
2152                Style);
2153   verifyFormat("@try {\n"
2154                "  // something\n"
2155                "}\n"
2156                "@finally {\n"
2157                "  // something\n"
2158                "}",
2159                Style);
2160   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2161   verifyFormat("try\n"
2162                "{\n"
2163                "  // something\n"
2164                "}\n"
2165                "catch (...)\n"
2166                "{\n"
2167                "  // something\n"
2168                "}",
2169                Style);
2170   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
2171   verifyFormat("try\n"
2172                "  {\n"
2173                "    // something\n"
2174                "  }\n"
2175                "catch (...)\n"
2176                "  {\n"
2177                "    // something\n"
2178                "  }",
2179                Style);
2180   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2181   Style.BraceWrapping.BeforeCatch = true;
2182   verifyFormat("try {\n"
2183                "  // something\n"
2184                "}\n"
2185                "catch (...) {\n"
2186                "  // something\n"
2187                "}",
2188                Style);
2189 }
2190 
2191 TEST_F(FormatTest, StaticInitializers) {
2192   verifyFormat("static SomeClass SC = {1, 'a'};");
2193 
2194   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
2195                "    100000000, "
2196                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
2197 
2198   // Here, everything other than the "}" would fit on a line.
2199   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
2200                "    10000000000000000000000000};");
2201   EXPECT_EQ("S s = {a,\n"
2202             "\n"
2203             "       b};",
2204             format("S s = {\n"
2205                    "  a,\n"
2206                    "\n"
2207                    "  b\n"
2208                    "};"));
2209 
2210   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
2211   // line. However, the formatting looks a bit off and this probably doesn't
2212   // happen often in practice.
2213   verifyFormat("static int Variable[1] = {\n"
2214                "    {1000000000000000000000000000000000000}};",
2215                getLLVMStyleWithColumns(40));
2216 }
2217 
2218 TEST_F(FormatTest, DesignatedInitializers) {
2219   verifyFormat("const struct A a = {.a = 1, .b = 2};");
2220   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
2221                "                    .bbbbbbbbbb = 2,\n"
2222                "                    .cccccccccc = 3,\n"
2223                "                    .dddddddddd = 4,\n"
2224                "                    .eeeeeeeeee = 5};");
2225   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2226                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
2227                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
2228                "    .ccccccccccccccccccccccccccc = 3,\n"
2229                "    .ddddddddddddddddddddddddddd = 4,\n"
2230                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
2231 
2232   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
2233 
2234   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
2235   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
2236                "                    [2] = bbbbbbbbbb,\n"
2237                "                    [3] = cccccccccc,\n"
2238                "                    [4] = dddddddddd,\n"
2239                "                    [5] = eeeeeeeeee};");
2240   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2241                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2242                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
2243                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
2244                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
2245                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
2246 }
2247 
2248 TEST_F(FormatTest, NestedStaticInitializers) {
2249   verifyFormat("static A x = {{{}}};\n");
2250   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
2251                "               {init1, init2, init3, init4}}};",
2252                getLLVMStyleWithColumns(50));
2253 
2254   verifyFormat("somes Status::global_reps[3] = {\n"
2255                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2256                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2257                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
2258                getLLVMStyleWithColumns(60));
2259   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
2260                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2261                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2262                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
2263   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
2264                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
2265                "rect.fTop}};");
2266 
2267   verifyFormat(
2268       "SomeArrayOfSomeType a = {\n"
2269       "    {{1, 2, 3},\n"
2270       "     {1, 2, 3},\n"
2271       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
2272       "      333333333333333333333333333333},\n"
2273       "     {1, 2, 3},\n"
2274       "     {1, 2, 3}}};");
2275   verifyFormat(
2276       "SomeArrayOfSomeType a = {\n"
2277       "    {{1, 2, 3}},\n"
2278       "    {{1, 2, 3}},\n"
2279       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
2280       "      333333333333333333333333333333}},\n"
2281       "    {{1, 2, 3}},\n"
2282       "    {{1, 2, 3}}};");
2283 
2284   verifyFormat("struct {\n"
2285                "  unsigned bit;\n"
2286                "  const char *const name;\n"
2287                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
2288                "                 {kOsWin, \"Windows\"},\n"
2289                "                 {kOsLinux, \"Linux\"},\n"
2290                "                 {kOsCrOS, \"Chrome OS\"}};");
2291   verifyFormat("struct {\n"
2292                "  unsigned bit;\n"
2293                "  const char *const name;\n"
2294                "} kBitsToOs[] = {\n"
2295                "    {kOsMac, \"Mac\"},\n"
2296                "    {kOsWin, \"Windows\"},\n"
2297                "    {kOsLinux, \"Linux\"},\n"
2298                "    {kOsCrOS, \"Chrome OS\"},\n"
2299                "};");
2300 }
2301 
2302 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
2303   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2304                "                      \\\n"
2305                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
2306 }
2307 
2308 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
2309   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
2310                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
2311 
2312   // Do break defaulted and deleted functions.
2313   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2314                "    default;",
2315                getLLVMStyleWithColumns(40));
2316   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2317                "    delete;",
2318                getLLVMStyleWithColumns(40));
2319 }
2320 
2321 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
2322   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
2323                getLLVMStyleWithColumns(40));
2324   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2325                getLLVMStyleWithColumns(40));
2326   EXPECT_EQ("#define Q                              \\\n"
2327             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
2328             "  \"aaaaaaaa.cpp\"",
2329             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2330                    getLLVMStyleWithColumns(40)));
2331 }
2332 
2333 TEST_F(FormatTest, UnderstandsLinePPDirective) {
2334   EXPECT_EQ("# 123 \"A string literal\"",
2335             format("   #     123    \"A string literal\""));
2336 }
2337 
2338 TEST_F(FormatTest, LayoutUnknownPPDirective) {
2339   EXPECT_EQ("#;", format("#;"));
2340   verifyFormat("#\n;\n;\n;");
2341 }
2342 
2343 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
2344   EXPECT_EQ("#line 42 \"test\"\n",
2345             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
2346   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
2347                                     getLLVMStyleWithColumns(12)));
2348 }
2349 
2350 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
2351   EXPECT_EQ("#line 42 \"test\"",
2352             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
2353   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
2354 }
2355 
2356 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
2357   verifyFormat("#define A \\x20");
2358   verifyFormat("#define A \\ x20");
2359   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
2360   verifyFormat("#define A ''");
2361   verifyFormat("#define A ''qqq");
2362   verifyFormat("#define A `qqq");
2363   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
2364   EXPECT_EQ("const char *c = STRINGIFY(\n"
2365             "\\na : b);",
2366             format("const char * c = STRINGIFY(\n"
2367                    "\\na : b);"));
2368 
2369   verifyFormat("a\r\\");
2370   verifyFormat("a\v\\");
2371   verifyFormat("a\f\\");
2372 }
2373 
2374 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
2375   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
2376   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
2377   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
2378   // FIXME: We never break before the macro name.
2379   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
2380 
2381   verifyFormat("#define A A\n#define A A");
2382   verifyFormat("#define A(X) A\n#define A A");
2383 
2384   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
2385   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
2386 }
2387 
2388 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
2389   EXPECT_EQ("// somecomment\n"
2390             "#include \"a.h\"\n"
2391             "#define A(  \\\n"
2392             "    A, B)\n"
2393             "#include \"b.h\"\n"
2394             "// somecomment\n",
2395             format("  // somecomment\n"
2396                    "  #include \"a.h\"\n"
2397                    "#define A(A,\\\n"
2398                    "    B)\n"
2399                    "    #include \"b.h\"\n"
2400                    " // somecomment\n",
2401                    getLLVMStyleWithColumns(13)));
2402 }
2403 
2404 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
2405 
2406 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
2407   EXPECT_EQ("#define A    \\\n"
2408             "  c;         \\\n"
2409             "  e;\n"
2410             "f;",
2411             format("#define A c; e;\n"
2412                    "f;",
2413                    getLLVMStyleWithColumns(14)));
2414 }
2415 
2416 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
2417 
2418 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
2419   EXPECT_EQ("int x,\n"
2420             "#define A\n"
2421             "    y;",
2422             format("int x,\n#define A\ny;"));
2423 }
2424 
2425 TEST_F(FormatTest, HashInMacroDefinition) {
2426   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
2427   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
2428   verifyFormat("#define A  \\\n"
2429                "  {        \\\n"
2430                "    f(#c); \\\n"
2431                "  }",
2432                getLLVMStyleWithColumns(11));
2433 
2434   verifyFormat("#define A(X)         \\\n"
2435                "  void function##X()",
2436                getLLVMStyleWithColumns(22));
2437 
2438   verifyFormat("#define A(a, b, c)   \\\n"
2439                "  void a##b##c()",
2440                getLLVMStyleWithColumns(22));
2441 
2442   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
2443 }
2444 
2445 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
2446   EXPECT_EQ("#define A (x)", format("#define A (x)"));
2447   EXPECT_EQ("#define A(x)", format("#define A(x)"));
2448 }
2449 
2450 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
2451   EXPECT_EQ("#define A b;", format("#define A \\\n"
2452                                    "          \\\n"
2453                                    "  b;",
2454                                    getLLVMStyleWithColumns(25)));
2455   EXPECT_EQ("#define A \\\n"
2456             "          \\\n"
2457             "  a;      \\\n"
2458             "  b;",
2459             format("#define A \\\n"
2460                    "          \\\n"
2461                    "  a;      \\\n"
2462                    "  b;",
2463                    getLLVMStyleWithColumns(11)));
2464   EXPECT_EQ("#define A \\\n"
2465             "  a;      \\\n"
2466             "          \\\n"
2467             "  b;",
2468             format("#define A \\\n"
2469                    "  a;      \\\n"
2470                    "          \\\n"
2471                    "  b;",
2472                    getLLVMStyleWithColumns(11)));
2473 }
2474 
2475 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
2476   verifyIncompleteFormat("#define A :");
2477   verifyFormat("#define SOMECASES  \\\n"
2478                "  case 1:          \\\n"
2479                "  case 2\n",
2480                getLLVMStyleWithColumns(20));
2481   verifyFormat("#define MACRO(a) \\\n"
2482                "  if (a)         \\\n"
2483                "    f();         \\\n"
2484                "  else           \\\n"
2485                "    g()",
2486                getLLVMStyleWithColumns(18));
2487   verifyFormat("#define A template <typename T>");
2488   verifyIncompleteFormat("#define STR(x) #x\n"
2489                          "f(STR(this_is_a_string_literal{));");
2490   verifyFormat("#pragma omp threadprivate( \\\n"
2491                "    y)), // expected-warning",
2492                getLLVMStyleWithColumns(28));
2493   verifyFormat("#d, = };");
2494   verifyFormat("#if \"a");
2495   verifyIncompleteFormat("({\n"
2496                          "#define b     \\\n"
2497                          "  }           \\\n"
2498                          "  a\n"
2499                          "a",
2500                          getLLVMStyleWithColumns(15));
2501   verifyFormat("#define A     \\\n"
2502                "  {           \\\n"
2503                "    {\n"
2504                "#define B     \\\n"
2505                "  }           \\\n"
2506                "  }",
2507                getLLVMStyleWithColumns(15));
2508   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2509   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2510   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2511   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
2512 }
2513 
2514 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2515   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2516   EXPECT_EQ("class A : public QObject {\n"
2517             "  Q_OBJECT\n"
2518             "\n"
2519             "  A() {}\n"
2520             "};",
2521             format("class A  :  public QObject {\n"
2522                    "     Q_OBJECT\n"
2523                    "\n"
2524                    "  A() {\n}\n"
2525                    "}  ;"));
2526   EXPECT_EQ("MACRO\n"
2527             "/*static*/ int i;",
2528             format("MACRO\n"
2529                    " /*static*/ int   i;"));
2530   EXPECT_EQ("SOME_MACRO\n"
2531             "namespace {\n"
2532             "void f();\n"
2533             "} // namespace",
2534             format("SOME_MACRO\n"
2535                    "  namespace    {\n"
2536                    "void   f(  );\n"
2537                    "} // namespace"));
2538   // Only if the identifier contains at least 5 characters.
2539   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
2540   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
2541   // Only if everything is upper case.
2542   EXPECT_EQ("class A : public QObject {\n"
2543             "  Q_Object A() {}\n"
2544             "};",
2545             format("class A  :  public QObject {\n"
2546                    "     Q_Object\n"
2547                    "  A() {\n}\n"
2548                    "}  ;"));
2549 
2550   // Only if the next line can actually start an unwrapped line.
2551   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2552             format("SOME_WEIRD_LOG_MACRO\n"
2553                    "<< SomeThing;"));
2554 
2555   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2556                "(n, buffers))\n",
2557                getChromiumStyle(FormatStyle::LK_Cpp));
2558 }
2559 
2560 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2561   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2562             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2563             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2564             "class X {};\n"
2565             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2566             "int *createScopDetectionPass() { return 0; }",
2567             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2568                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2569                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2570                    "  class X {};\n"
2571                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2572                    "  int *createScopDetectionPass() { return 0; }"));
2573   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2574   // braces, so that inner block is indented one level more.
2575   EXPECT_EQ("int q() {\n"
2576             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2577             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2578             "  IPC_END_MESSAGE_MAP()\n"
2579             "}",
2580             format("int q() {\n"
2581                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2582                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2583                    "  IPC_END_MESSAGE_MAP()\n"
2584                    "}"));
2585 
2586   // Same inside macros.
2587   EXPECT_EQ("#define LIST(L) \\\n"
2588             "  L(A)          \\\n"
2589             "  L(B)          \\\n"
2590             "  L(C)",
2591             format("#define LIST(L) \\\n"
2592                    "  L(A) \\\n"
2593                    "  L(B) \\\n"
2594                    "  L(C)",
2595                    getGoogleStyle()));
2596 
2597   // These must not be recognized as macros.
2598   EXPECT_EQ("int q() {\n"
2599             "  f(x);\n"
2600             "  f(x) {}\n"
2601             "  f(x)->g();\n"
2602             "  f(x)->*g();\n"
2603             "  f(x).g();\n"
2604             "  f(x) = x;\n"
2605             "  f(x) += x;\n"
2606             "  f(x) -= x;\n"
2607             "  f(x) *= x;\n"
2608             "  f(x) /= x;\n"
2609             "  f(x) %= x;\n"
2610             "  f(x) &= x;\n"
2611             "  f(x) |= x;\n"
2612             "  f(x) ^= x;\n"
2613             "  f(x) >>= x;\n"
2614             "  f(x) <<= x;\n"
2615             "  f(x)[y].z();\n"
2616             "  LOG(INFO) << x;\n"
2617             "  ifstream(x) >> x;\n"
2618             "}\n",
2619             format("int q() {\n"
2620                    "  f(x)\n;\n"
2621                    "  f(x)\n {}\n"
2622                    "  f(x)\n->g();\n"
2623                    "  f(x)\n->*g();\n"
2624                    "  f(x)\n.g();\n"
2625                    "  f(x)\n = x;\n"
2626                    "  f(x)\n += x;\n"
2627                    "  f(x)\n -= x;\n"
2628                    "  f(x)\n *= x;\n"
2629                    "  f(x)\n /= x;\n"
2630                    "  f(x)\n %= x;\n"
2631                    "  f(x)\n &= x;\n"
2632                    "  f(x)\n |= x;\n"
2633                    "  f(x)\n ^= x;\n"
2634                    "  f(x)\n >>= x;\n"
2635                    "  f(x)\n <<= x;\n"
2636                    "  f(x)\n[y].z();\n"
2637                    "  LOG(INFO)\n << x;\n"
2638                    "  ifstream(x)\n >> x;\n"
2639                    "}\n"));
2640   EXPECT_EQ("int q() {\n"
2641             "  F(x)\n"
2642             "  if (1) {\n"
2643             "  }\n"
2644             "  F(x)\n"
2645             "  while (1) {\n"
2646             "  }\n"
2647             "  F(x)\n"
2648             "  G(x);\n"
2649             "  F(x)\n"
2650             "  try {\n"
2651             "    Q();\n"
2652             "  } catch (...) {\n"
2653             "  }\n"
2654             "}\n",
2655             format("int q() {\n"
2656                    "F(x)\n"
2657                    "if (1) {}\n"
2658                    "F(x)\n"
2659                    "while (1) {}\n"
2660                    "F(x)\n"
2661                    "G(x);\n"
2662                    "F(x)\n"
2663                    "try { Q(); } catch (...) {}\n"
2664                    "}\n"));
2665   EXPECT_EQ("class A {\n"
2666             "  A() : t(0) {}\n"
2667             "  A(int i) noexcept() : {}\n"
2668             "  A(X x)\n" // FIXME: function-level try blocks are broken.
2669             "  try : t(0) {\n"
2670             "  } catch (...) {\n"
2671             "  }\n"
2672             "};",
2673             format("class A {\n"
2674                    "  A()\n : t(0) {}\n"
2675                    "  A(int i)\n noexcept() : {}\n"
2676                    "  A(X x)\n"
2677                    "  try : t(0) {} catch (...) {}\n"
2678                    "};"));
2679   FormatStyle Style = getLLVMStyle();
2680   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2681   Style.BraceWrapping.AfterControlStatement = true;
2682   Style.BraceWrapping.AfterFunction = true;
2683   EXPECT_EQ("void f()\n"
2684             "try\n"
2685             "{\n"
2686             "}",
2687             format("void f() try {\n"
2688                    "}", Style));
2689   EXPECT_EQ("class SomeClass {\n"
2690             "public:\n"
2691             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2692             "};",
2693             format("class SomeClass {\n"
2694                    "public:\n"
2695                    "  SomeClass()\n"
2696                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2697                    "};"));
2698   EXPECT_EQ("class SomeClass {\n"
2699             "public:\n"
2700             "  SomeClass()\n"
2701             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2702             "};",
2703             format("class SomeClass {\n"
2704                    "public:\n"
2705                    "  SomeClass()\n"
2706                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2707                    "};",
2708                    getLLVMStyleWithColumns(40)));
2709 
2710   verifyFormat("MACRO(>)");
2711 
2712   // Some macros contain an implicit semicolon.
2713   Style = getLLVMStyle();
2714   Style.StatementMacros.push_back("FOO");
2715   verifyFormat("FOO(a) int b = 0;");
2716   verifyFormat("FOO(a)\n"
2717                "int b = 0;",
2718                Style);
2719   verifyFormat("FOO(a);\n"
2720                "int b = 0;",
2721                Style);
2722   verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
2723                "int b = 0;",
2724                Style);
2725   verifyFormat("FOO()\n"
2726                "int b = 0;",
2727                Style);
2728   verifyFormat("FOO\n"
2729                "int b = 0;",
2730                Style);
2731   verifyFormat("void f() {\n"
2732                "  FOO(a)\n"
2733                "  return a;\n"
2734                "}",
2735                Style);
2736   verifyFormat("FOO(a)\n"
2737                "FOO(b)",
2738                Style);
2739   verifyFormat("int a = 0;\n"
2740                "FOO(b)\n"
2741                "int c = 0;",
2742                Style);
2743   verifyFormat("int a = 0;\n"
2744                "int x = FOO(a)\n"
2745                "int b = 0;",
2746                Style);
2747   verifyFormat("void foo(int a) { FOO(a) }\n"
2748                "uint32_t bar() {}",
2749                Style);
2750 }
2751 
2752 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2753   verifyFormat("#define A \\\n"
2754                "  f({     \\\n"
2755                "    g();  \\\n"
2756                "  });",
2757                getLLVMStyleWithColumns(11));
2758 }
2759 
2760 TEST_F(FormatTest, IndentPreprocessorDirectives) {
2761   FormatStyle Style = getLLVMStyle();
2762   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
2763   Style.ColumnLimit = 40;
2764   verifyFormat("#ifdef _WIN32\n"
2765                "#define A 0\n"
2766                "#ifdef VAR2\n"
2767                "#define B 1\n"
2768                "#include <someheader.h>\n"
2769                "#define MACRO                          \\\n"
2770                "  some_very_long_func_aaaaaaaaaa();\n"
2771                "#endif\n"
2772                "#else\n"
2773                "#define A 1\n"
2774                "#endif",
2775                Style);
2776   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
2777   verifyFormat("#ifdef _WIN32\n"
2778                "#  define A 0\n"
2779                "#  ifdef VAR2\n"
2780                "#    define B 1\n"
2781                "#    include <someheader.h>\n"
2782                "#    define MACRO                      \\\n"
2783                "      some_very_long_func_aaaaaaaaaa();\n"
2784                "#  endif\n"
2785                "#else\n"
2786                "#  define A 1\n"
2787                "#endif",
2788                Style);
2789   verifyFormat("#if A\n"
2790                "#  define MACRO                        \\\n"
2791                "    void a(int x) {                    \\\n"
2792                "      b();                             \\\n"
2793                "      c();                             \\\n"
2794                "      d();                             \\\n"
2795                "      e();                             \\\n"
2796                "      f();                             \\\n"
2797                "    }\n"
2798                "#endif",
2799                Style);
2800   // Comments before include guard.
2801   verifyFormat("// file comment\n"
2802                "// file comment\n"
2803                "#ifndef HEADER_H\n"
2804                "#define HEADER_H\n"
2805                "code();\n"
2806                "#endif",
2807                Style);
2808   // Test with include guards.
2809   verifyFormat("#ifndef HEADER_H\n"
2810                "#define HEADER_H\n"
2811                "code();\n"
2812                "#endif",
2813                Style);
2814   // Include guards must have a #define with the same variable immediately
2815   // after #ifndef.
2816   verifyFormat("#ifndef NOT_GUARD\n"
2817                "#  define FOO\n"
2818                "code();\n"
2819                "#endif",
2820                Style);
2821 
2822   // Include guards must cover the entire file.
2823   verifyFormat("code();\n"
2824                "code();\n"
2825                "#ifndef NOT_GUARD\n"
2826                "#  define NOT_GUARD\n"
2827                "code();\n"
2828                "#endif",
2829                Style);
2830   verifyFormat("#ifndef NOT_GUARD\n"
2831                "#  define NOT_GUARD\n"
2832                "code();\n"
2833                "#endif\n"
2834                "code();",
2835                Style);
2836   // Test with trailing blank lines.
2837   verifyFormat("#ifndef HEADER_H\n"
2838                "#define HEADER_H\n"
2839                "code();\n"
2840                "#endif\n",
2841                Style);
2842   // Include guards don't have #else.
2843   verifyFormat("#ifndef NOT_GUARD\n"
2844                "#  define NOT_GUARD\n"
2845                "code();\n"
2846                "#else\n"
2847                "#endif",
2848                Style);
2849   verifyFormat("#ifndef NOT_GUARD\n"
2850                "#  define NOT_GUARD\n"
2851                "code();\n"
2852                "#elif FOO\n"
2853                "#endif",
2854                Style);
2855   // Non-identifier #define after potential include guard.
2856   verifyFormat("#ifndef FOO\n"
2857                "#  define 1\n"
2858                "#endif\n",
2859                Style);
2860   // #if closes past last non-preprocessor line.
2861   verifyFormat("#ifndef FOO\n"
2862                "#define FOO\n"
2863                "#if 1\n"
2864                "int i;\n"
2865                "#  define A 0\n"
2866                "#endif\n"
2867                "#endif\n",
2868                Style);
2869   // FIXME: This doesn't handle the case where there's code between the
2870   // #ifndef and #define but all other conditions hold. This is because when
2871   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
2872   // previous code line yet, so we can't detect it.
2873   EXPECT_EQ("#ifndef NOT_GUARD\n"
2874             "code();\n"
2875             "#define NOT_GUARD\n"
2876             "code();\n"
2877             "#endif",
2878             format("#ifndef NOT_GUARD\n"
2879                    "code();\n"
2880                    "#  define NOT_GUARD\n"
2881                    "code();\n"
2882                    "#endif",
2883                    Style));
2884   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
2885   // be outside an include guard. Examples are #pragma once and
2886   // #pragma GCC diagnostic, or anything else that does not change the meaning
2887   // of the file if it's included multiple times.
2888   EXPECT_EQ("#ifdef WIN32\n"
2889             "#  pragma once\n"
2890             "#endif\n"
2891             "#ifndef HEADER_H\n"
2892             "#  define HEADER_H\n"
2893             "code();\n"
2894             "#endif",
2895             format("#ifdef WIN32\n"
2896                    "#  pragma once\n"
2897                    "#endif\n"
2898                    "#ifndef HEADER_H\n"
2899                    "#define HEADER_H\n"
2900                    "code();\n"
2901                    "#endif",
2902                    Style));
2903   // FIXME: This does not detect when there is a single non-preprocessor line
2904   // in front of an include-guard-like structure where other conditions hold
2905   // because ScopedLineState hides the line.
2906   EXPECT_EQ("code();\n"
2907             "#ifndef HEADER_H\n"
2908             "#define HEADER_H\n"
2909             "code();\n"
2910             "#endif",
2911             format("code();\n"
2912                    "#ifndef HEADER_H\n"
2913                    "#  define HEADER_H\n"
2914                    "code();\n"
2915                    "#endif",
2916                    Style));
2917   // Keep comments aligned with #, otherwise indent comments normally. These
2918   // tests cannot use verifyFormat because messUp manipulates leading
2919   // whitespace.
2920   {
2921     const char *Expected = ""
2922                            "void f() {\n"
2923                            "#if 1\n"
2924                            "// Preprocessor aligned.\n"
2925                            "#  define A 0\n"
2926                            "  // Code. Separated by blank line.\n"
2927                            "\n"
2928                            "#  define B 0\n"
2929                            "  // Code. Not aligned with #\n"
2930                            "#  define C 0\n"
2931                            "#endif";
2932     const char *ToFormat = ""
2933                            "void f() {\n"
2934                            "#if 1\n"
2935                            "// Preprocessor aligned.\n"
2936                            "#  define A 0\n"
2937                            "// Code. Separated by blank line.\n"
2938                            "\n"
2939                            "#  define B 0\n"
2940                            "   // Code. Not aligned with #\n"
2941                            "#  define C 0\n"
2942                            "#endif";
2943     EXPECT_EQ(Expected, format(ToFormat, Style));
2944     EXPECT_EQ(Expected, format(Expected, Style));
2945   }
2946   // Keep block quotes aligned.
2947   {
2948     const char *Expected = ""
2949                            "void f() {\n"
2950                            "#if 1\n"
2951                            "/* Preprocessor aligned. */\n"
2952                            "#  define A 0\n"
2953                            "  /* Code. Separated by blank line. */\n"
2954                            "\n"
2955                            "#  define B 0\n"
2956                            "  /* Code. Not aligned with # */\n"
2957                            "#  define C 0\n"
2958                            "#endif";
2959     const char *ToFormat = ""
2960                            "void f() {\n"
2961                            "#if 1\n"
2962                            "/* Preprocessor aligned. */\n"
2963                            "#  define A 0\n"
2964                            "/* Code. Separated by blank line. */\n"
2965                            "\n"
2966                            "#  define B 0\n"
2967                            "   /* Code. Not aligned with # */\n"
2968                            "#  define C 0\n"
2969                            "#endif";
2970     EXPECT_EQ(Expected, format(ToFormat, Style));
2971     EXPECT_EQ(Expected, format(Expected, Style));
2972   }
2973   // Keep comments aligned with un-indented directives.
2974   {
2975     const char *Expected = ""
2976                            "void f() {\n"
2977                            "// Preprocessor aligned.\n"
2978                            "#define A 0\n"
2979                            "  // Code. Separated by blank line.\n"
2980                            "\n"
2981                            "#define B 0\n"
2982                            "  // Code. Not aligned with #\n"
2983                            "#define C 0\n";
2984     const char *ToFormat = ""
2985                            "void f() {\n"
2986                            "// Preprocessor aligned.\n"
2987                            "#define A 0\n"
2988                            "// Code. Separated by blank line.\n"
2989                            "\n"
2990                            "#define B 0\n"
2991                            "   // Code. Not aligned with #\n"
2992                            "#define C 0\n";
2993     EXPECT_EQ(Expected, format(ToFormat, Style));
2994     EXPECT_EQ(Expected, format(Expected, Style));
2995   }
2996   // Test AfterHash with tabs.
2997   {
2998     FormatStyle Tabbed = Style;
2999     Tabbed.UseTab = FormatStyle::UT_Always;
3000     Tabbed.IndentWidth = 8;
3001     Tabbed.TabWidth = 8;
3002     verifyFormat("#ifdef _WIN32\n"
3003                  "#\tdefine A 0\n"
3004                  "#\tifdef VAR2\n"
3005                  "#\t\tdefine B 1\n"
3006                  "#\t\tinclude <someheader.h>\n"
3007                  "#\t\tdefine MACRO          \\\n"
3008                  "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
3009                  "#\tendif\n"
3010                  "#else\n"
3011                  "#\tdefine A 1\n"
3012                  "#endif",
3013                  Tabbed);
3014   }
3015 
3016   // Regression test: Multiline-macro inside include guards.
3017   verifyFormat("#ifndef HEADER_H\n"
3018                "#define HEADER_H\n"
3019                "#define A()        \\\n"
3020                "  int i;           \\\n"
3021                "  int j;\n"
3022                "#endif // HEADER_H",
3023                getLLVMStyleWithColumns(20));
3024 
3025   Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
3026   // Basic before hash indent tests
3027   verifyFormat("#ifdef _WIN32\n"
3028                "  #define A 0\n"
3029                "  #ifdef VAR2\n"
3030                "    #define B 1\n"
3031                "    #include <someheader.h>\n"
3032                "    #define MACRO                      \\\n"
3033                "      some_very_long_func_aaaaaaaaaa();\n"
3034                "  #endif\n"
3035                "#else\n"
3036                "  #define A 1\n"
3037                "#endif",
3038                Style);
3039   verifyFormat("#if A\n"
3040                "  #define MACRO                        \\\n"
3041                "    void a(int x) {                    \\\n"
3042                "      b();                             \\\n"
3043                "      c();                             \\\n"
3044                "      d();                             \\\n"
3045                "      e();                             \\\n"
3046                "      f();                             \\\n"
3047                "    }\n"
3048                "#endif",
3049                Style);
3050   // Keep comments aligned with indented directives. These
3051   // tests cannot use verifyFormat because messUp manipulates leading
3052   // whitespace.
3053   {
3054     const char *Expected = "void f() {\n"
3055                            "// Aligned to preprocessor.\n"
3056                            "#if 1\n"
3057                            "  // Aligned to code.\n"
3058                            "  int a;\n"
3059                            "  #if 1\n"
3060                            "    // Aligned to preprocessor.\n"
3061                            "    #define A 0\n"
3062                            "  // Aligned to code.\n"
3063                            "  int b;\n"
3064                            "  #endif\n"
3065                            "#endif\n"
3066                            "}";
3067     const char *ToFormat = "void f() {\n"
3068                            "// Aligned to preprocessor.\n"
3069                            "#if 1\n"
3070                            "// Aligned to code.\n"
3071                            "int a;\n"
3072                            "#if 1\n"
3073                            "// Aligned to preprocessor.\n"
3074                            "#define A 0\n"
3075                            "// Aligned to code.\n"
3076                            "int b;\n"
3077                            "#endif\n"
3078                            "#endif\n"
3079                            "}";
3080     EXPECT_EQ(Expected, format(ToFormat, Style));
3081     EXPECT_EQ(Expected, format(Expected, Style));
3082   }
3083   {
3084     const char *Expected = "void f() {\n"
3085                            "/* Aligned to preprocessor. */\n"
3086                            "#if 1\n"
3087                            "  /* Aligned to code. */\n"
3088                            "  int a;\n"
3089                            "  #if 1\n"
3090                            "    /* Aligned to preprocessor. */\n"
3091                            "    #define A 0\n"
3092                            "  /* Aligned to code. */\n"
3093                            "  int b;\n"
3094                            "  #endif\n"
3095                            "#endif\n"
3096                            "}";
3097     const char *ToFormat = "void f() {\n"
3098                            "/* Aligned to preprocessor. */\n"
3099                            "#if 1\n"
3100                            "/* Aligned to code. */\n"
3101                            "int a;\n"
3102                            "#if 1\n"
3103                            "/* Aligned to preprocessor. */\n"
3104                            "#define A 0\n"
3105                            "/* Aligned to code. */\n"
3106                            "int b;\n"
3107                            "#endif\n"
3108                            "#endif\n"
3109                            "}";
3110     EXPECT_EQ(Expected, format(ToFormat, Style));
3111     EXPECT_EQ(Expected, format(Expected, Style));
3112   }
3113 
3114   // Test single comment before preprocessor
3115   verifyFormat("// Comment\n"
3116                "\n"
3117                "#if 1\n"
3118                "#endif",
3119                Style);
3120 }
3121 
3122 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
3123   verifyFormat("{\n  { a #c; }\n}");
3124 }
3125 
3126 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
3127   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
3128             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
3129   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
3130             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
3131 }
3132 
3133 TEST_F(FormatTest, EscapedNewlines) {
3134   FormatStyle Narrow = getLLVMStyleWithColumns(11);
3135   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
3136             format("#define A \\\nint i;\\\n  int j;", Narrow));
3137   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
3138   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
3139   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
3140   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
3141 
3142   FormatStyle AlignLeft = getLLVMStyle();
3143   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
3144   EXPECT_EQ("#define MACRO(x) \\\n"
3145             "private:         \\\n"
3146             "  int x(int a);\n",
3147             format("#define MACRO(x) \\\n"
3148                    "private:         \\\n"
3149                    "  int x(int a);\n",
3150                    AlignLeft));
3151 
3152   // CRLF line endings
3153   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
3154             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
3155   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
3156   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
3157   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
3158   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
3159   EXPECT_EQ("#define MACRO(x) \\\r\n"
3160             "private:         \\\r\n"
3161             "  int x(int a);\r\n",
3162             format("#define MACRO(x) \\\r\n"
3163                    "private:         \\\r\n"
3164                    "  int x(int a);\r\n",
3165                    AlignLeft));
3166 
3167   FormatStyle DontAlign = getLLVMStyle();
3168   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
3169   DontAlign.MaxEmptyLinesToKeep = 3;
3170   // FIXME: can't use verifyFormat here because the newline before
3171   // "public:" is not inserted the first time it's reformatted
3172   EXPECT_EQ("#define A \\\n"
3173             "  class Foo { \\\n"
3174             "    void bar(); \\\n"
3175             "\\\n"
3176             "\\\n"
3177             "\\\n"
3178             "  public: \\\n"
3179             "    void baz(); \\\n"
3180             "  };",
3181             format("#define A \\\n"
3182                    "  class Foo { \\\n"
3183                    "    void bar(); \\\n"
3184                    "\\\n"
3185                    "\\\n"
3186                    "\\\n"
3187                    "  public: \\\n"
3188                    "    void baz(); \\\n"
3189                    "  };",
3190                    DontAlign));
3191 }
3192 
3193 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
3194   verifyFormat("#define A \\\n"
3195                "  int v(  \\\n"
3196                "      a); \\\n"
3197                "  int i;",
3198                getLLVMStyleWithColumns(11));
3199 }
3200 
3201 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
3202   EXPECT_EQ(
3203       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
3204       "                      \\\n"
3205       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
3206       "\n"
3207       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
3208       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
3209       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
3210              "\\\n"
3211              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
3212              "  \n"
3213              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
3214              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
3215 }
3216 
3217 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
3218   EXPECT_EQ("int\n"
3219             "#define A\n"
3220             "    a;",
3221             format("int\n#define A\na;"));
3222   verifyFormat("functionCallTo(\n"
3223                "    someOtherFunction(\n"
3224                "        withSomeParameters, whichInSequence,\n"
3225                "        areLongerThanALine(andAnotherCall,\n"
3226                "#define A B\n"
3227                "                           withMoreParamters,\n"
3228                "                           whichStronglyInfluenceTheLayout),\n"
3229                "        andMoreParameters),\n"
3230                "    trailing);",
3231                getLLVMStyleWithColumns(69));
3232   verifyFormat("Foo::Foo()\n"
3233                "#ifdef BAR\n"
3234                "    : baz(0)\n"
3235                "#endif\n"
3236                "{\n"
3237                "}");
3238   verifyFormat("void f() {\n"
3239                "  if (true)\n"
3240                "#ifdef A\n"
3241                "    f(42);\n"
3242                "  x();\n"
3243                "#else\n"
3244                "    g();\n"
3245                "  x();\n"
3246                "#endif\n"
3247                "}");
3248   verifyFormat("void f(param1, param2,\n"
3249                "       param3,\n"
3250                "#ifdef A\n"
3251                "       param4(param5,\n"
3252                "#ifdef A1\n"
3253                "              param6,\n"
3254                "#ifdef A2\n"
3255                "              param7),\n"
3256                "#else\n"
3257                "              param8),\n"
3258                "       param9,\n"
3259                "#endif\n"
3260                "       param10,\n"
3261                "#endif\n"
3262                "       param11)\n"
3263                "#else\n"
3264                "       param12)\n"
3265                "#endif\n"
3266                "{\n"
3267                "  x();\n"
3268                "}",
3269                getLLVMStyleWithColumns(28));
3270   verifyFormat("#if 1\n"
3271                "int i;");
3272   verifyFormat("#if 1\n"
3273                "#endif\n"
3274                "#if 1\n"
3275                "#else\n"
3276                "#endif\n");
3277   verifyFormat("DEBUG({\n"
3278                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3279                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
3280                "});\n"
3281                "#if a\n"
3282                "#else\n"
3283                "#endif");
3284 
3285   verifyIncompleteFormat("void f(\n"
3286                          "#if A\n"
3287                          ");\n"
3288                          "#else\n"
3289                          "#endif");
3290 }
3291 
3292 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
3293   verifyFormat("#endif\n"
3294                "#if B");
3295 }
3296 
3297 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
3298   FormatStyle SingleLine = getLLVMStyle();
3299   SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
3300   verifyFormat("#if 0\n"
3301                "#elif 1\n"
3302                "#endif\n"
3303                "void foo() {\n"
3304                "  if (test) foo2();\n"
3305                "}",
3306                SingleLine);
3307 }
3308 
3309 TEST_F(FormatTest, LayoutBlockInsideParens) {
3310   verifyFormat("functionCall({ int i; });");
3311   verifyFormat("functionCall({\n"
3312                "  int i;\n"
3313                "  int j;\n"
3314                "});");
3315   verifyFormat("functionCall(\n"
3316                "    {\n"
3317                "      int i;\n"
3318                "      int j;\n"
3319                "    },\n"
3320                "    aaaa, bbbb, cccc);");
3321   verifyFormat("functionA(functionB({\n"
3322                "            int i;\n"
3323                "            int j;\n"
3324                "          }),\n"
3325                "          aaaa, bbbb, cccc);");
3326   verifyFormat("functionCall(\n"
3327                "    {\n"
3328                "      int i;\n"
3329                "      int j;\n"
3330                "    },\n"
3331                "    aaaa, bbbb, // comment\n"
3332                "    cccc);");
3333   verifyFormat("functionA(functionB({\n"
3334                "            int i;\n"
3335                "            int j;\n"
3336                "          }),\n"
3337                "          aaaa, bbbb, // comment\n"
3338                "          cccc);");
3339   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
3340   verifyFormat("functionCall(aaaa, bbbb, {\n"
3341                "  int i;\n"
3342                "  int j;\n"
3343                "});");
3344   verifyFormat(
3345       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
3346       "    {\n"
3347       "      int i; // break\n"
3348       "    },\n"
3349       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
3350       "                                     ccccccccccccccccc));");
3351   verifyFormat("DEBUG({\n"
3352                "  if (a)\n"
3353                "    f();\n"
3354                "});");
3355 }
3356 
3357 TEST_F(FormatTest, LayoutBlockInsideStatement) {
3358   EXPECT_EQ("SOME_MACRO { int i; }\n"
3359             "int i;",
3360             format("  SOME_MACRO  {int i;}  int i;"));
3361 }
3362 
3363 TEST_F(FormatTest, LayoutNestedBlocks) {
3364   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
3365                "  struct s {\n"
3366                "    int i;\n"
3367                "  };\n"
3368                "  s kBitsToOs[] = {{10}};\n"
3369                "  for (int i = 0; i < 10; ++i)\n"
3370                "    return;\n"
3371                "}");
3372   verifyFormat("call(parameter, {\n"
3373                "  something();\n"
3374                "  // Comment using all columns.\n"
3375                "  somethingelse();\n"
3376                "});",
3377                getLLVMStyleWithColumns(40));
3378   verifyFormat("DEBUG( //\n"
3379                "    { f(); }, a);");
3380   verifyFormat("DEBUG( //\n"
3381                "    {\n"
3382                "      f(); //\n"
3383                "    },\n"
3384                "    a);");
3385 
3386   EXPECT_EQ("call(parameter, {\n"
3387             "  something();\n"
3388             "  // Comment too\n"
3389             "  // looooooooooong.\n"
3390             "  somethingElse();\n"
3391             "});",
3392             format("call(parameter, {\n"
3393                    "  something();\n"
3394                    "  // Comment too looooooooooong.\n"
3395                    "  somethingElse();\n"
3396                    "});",
3397                    getLLVMStyleWithColumns(29)));
3398   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
3399   EXPECT_EQ("DEBUG({ // comment\n"
3400             "  int i;\n"
3401             "});",
3402             format("DEBUG({ // comment\n"
3403                    "int  i;\n"
3404                    "});"));
3405   EXPECT_EQ("DEBUG({\n"
3406             "  int i;\n"
3407             "\n"
3408             "  // comment\n"
3409             "  int j;\n"
3410             "});",
3411             format("DEBUG({\n"
3412                    "  int  i;\n"
3413                    "\n"
3414                    "  // comment\n"
3415                    "  int  j;\n"
3416                    "});"));
3417 
3418   verifyFormat("DEBUG({\n"
3419                "  if (a)\n"
3420                "    return;\n"
3421                "});");
3422   verifyGoogleFormat("DEBUG({\n"
3423                      "  if (a) return;\n"
3424                      "});");
3425   FormatStyle Style = getGoogleStyle();
3426   Style.ColumnLimit = 45;
3427   verifyFormat("Debug(\n"
3428                "    aaaaa,\n"
3429                "    {\n"
3430                "      if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
3431                "    },\n"
3432                "    a);",
3433                Style);
3434 
3435   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
3436 
3437   verifyNoCrash("^{v^{a}}");
3438 }
3439 
3440 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
3441   EXPECT_EQ("#define MACRO()                     \\\n"
3442             "  Debug(aaa, /* force line break */ \\\n"
3443             "        {                           \\\n"
3444             "          int i;                    \\\n"
3445             "          int j;                    \\\n"
3446             "        })",
3447             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
3448                    "          {  int   i;  int  j;   })",
3449                    getGoogleStyle()));
3450 
3451   EXPECT_EQ("#define A                                       \\\n"
3452             "  [] {                                          \\\n"
3453             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
3454             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
3455             "  }",
3456             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
3457                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
3458                    getGoogleStyle()));
3459 }
3460 
3461 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
3462   EXPECT_EQ("{}", format("{}"));
3463   verifyFormat("enum E {};");
3464   verifyFormat("enum E {}");
3465 }
3466 
3467 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
3468   FormatStyle Style = getLLVMStyle();
3469   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
3470   Style.MacroBlockEnd = "^[A-Z_]+_END$";
3471   verifyFormat("FOO_BEGIN\n"
3472                "  FOO_ENTRY\n"
3473                "FOO_END", Style);
3474   verifyFormat("FOO_BEGIN\n"
3475                "  NESTED_FOO_BEGIN\n"
3476                "    NESTED_FOO_ENTRY\n"
3477                "  NESTED_FOO_END\n"
3478                "FOO_END", Style);
3479   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
3480                "  int x;\n"
3481                "  x = 1;\n"
3482                "FOO_END(Baz)", Style);
3483 }
3484 
3485 //===----------------------------------------------------------------------===//
3486 // Line break tests.
3487 //===----------------------------------------------------------------------===//
3488 
3489 TEST_F(FormatTest, PreventConfusingIndents) {
3490   verifyFormat(
3491       "void f() {\n"
3492       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
3493       "                         parameter, parameter, parameter)),\n"
3494       "                     SecondLongCall(parameter));\n"
3495       "}");
3496   verifyFormat(
3497       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3498       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3499       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3500       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
3501   verifyFormat(
3502       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3503       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
3504       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
3505       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
3506   verifyFormat(
3507       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3508       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
3509       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
3510       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
3511   verifyFormat("int a = bbbb && ccc &&\n"
3512                "        fffff(\n"
3513                "#define A Just forcing a new line\n"
3514                "            ddd);");
3515 }
3516 
3517 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
3518   verifyFormat(
3519       "bool aaaaaaa =\n"
3520       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
3521       "    bbbbbbbb();");
3522   verifyFormat(
3523       "bool aaaaaaa =\n"
3524       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
3525       "    bbbbbbbb();");
3526 
3527   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3528                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
3529                "    ccccccccc == ddddddddddd;");
3530   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3531                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
3532                "    ccccccccc == ddddddddddd;");
3533   verifyFormat(
3534       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3535       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
3536       "    ccccccccc == ddddddddddd;");
3537 
3538   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3539                "                 aaaaaa) &&\n"
3540                "         bbbbbb && cccccc;");
3541   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3542                "                 aaaaaa) >>\n"
3543                "         bbbbbb;");
3544   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
3545                "    SourceMgr.getSpellingColumnNumber(\n"
3546                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
3547                "    1);");
3548 
3549   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3550                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
3551                "    cccccc) {\n}");
3552   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3553                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
3554                "              cccccc) {\n}");
3555   verifyFormat("b = a &&\n"
3556                "    // Comment\n"
3557                "    b.c && d;");
3558 
3559   // If the LHS of a comparison is not a binary expression itself, the
3560   // additional linebreak confuses many people.
3561   verifyFormat(
3562       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3563       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
3564       "}");
3565   verifyFormat(
3566       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3567       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3568       "}");
3569   verifyFormat(
3570       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
3571       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3572       "}");
3573   verifyFormat(
3574       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3575       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
3576       "}");
3577   // Even explicit parentheses stress the precedence enough to make the
3578   // additional break unnecessary.
3579   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3580                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3581                "}");
3582   // This cases is borderline, but with the indentation it is still readable.
3583   verifyFormat(
3584       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3585       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3586       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
3587       "}",
3588       getLLVMStyleWithColumns(75));
3589 
3590   // If the LHS is a binary expression, we should still use the additional break
3591   // as otherwise the formatting hides the operator precedence.
3592   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3593                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3594                "    5) {\n"
3595                "}");
3596   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3597                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
3598                "    5) {\n"
3599                "}");
3600 
3601   FormatStyle OnePerLine = getLLVMStyle();
3602   OnePerLine.BinPackParameters = false;
3603   verifyFormat(
3604       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3605       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3606       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
3607       OnePerLine);
3608 
3609   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
3610                "                .aaa(aaaaaaaaaaaaa) *\n"
3611                "            aaaaaaa +\n"
3612                "        aaaaaaa;",
3613                getLLVMStyleWithColumns(40));
3614 }
3615 
3616 TEST_F(FormatTest, ExpressionIndentation) {
3617   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3618                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3619                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3620                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3621                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3622                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
3623                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3624                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
3625                "                 ccccccccccccccccccccccccccccccccccccccccc;");
3626   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3627                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3628                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3629                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3630   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3631                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3632                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3633                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3634   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3635                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3636                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3637                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3638   verifyFormat("if () {\n"
3639                "} else if (aaaaa && bbbbb > // break\n"
3640                "                        ccccc) {\n"
3641                "}");
3642   verifyFormat("if () {\n"
3643                "} else if (aaaaa &&\n"
3644                "           bbbbb > // break\n"
3645                "               ccccc &&\n"
3646                "           ddddd) {\n"
3647                "}");
3648 
3649   // Presence of a trailing comment used to change indentation of b.
3650   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
3651                "       b;\n"
3652                "return aaaaaaaaaaaaaaaaaaa +\n"
3653                "       b; //",
3654                getLLVMStyleWithColumns(30));
3655 }
3656 
3657 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
3658   // Not sure what the best system is here. Like this, the LHS can be found
3659   // immediately above an operator (everything with the same or a higher
3660   // indent). The RHS is aligned right of the operator and so compasses
3661   // everything until something with the same indent as the operator is found.
3662   // FIXME: Is this a good system?
3663   FormatStyle Style = getLLVMStyle();
3664   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
3665   verifyFormat(
3666       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3667       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3668       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3669       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3670       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3671       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3672       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3673       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3674       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
3675       Style);
3676   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3677                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3678                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3679                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3680                Style);
3681   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3682                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3683                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3684                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3685                Style);
3686   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3687                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3688                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3689                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3690                Style);
3691   verifyFormat("if () {\n"
3692                "} else if (aaaaa\n"
3693                "           && bbbbb // break\n"
3694                "                  > ccccc) {\n"
3695                "}",
3696                Style);
3697   verifyFormat("return (a)\n"
3698                "       // comment\n"
3699                "       + b;",
3700                Style);
3701   verifyFormat(
3702       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3703       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3704       "             + cc;",
3705       Style);
3706 
3707   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3708                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3709                Style);
3710 
3711   // Forced by comments.
3712   verifyFormat(
3713       "unsigned ContentSize =\n"
3714       "    sizeof(int16_t)   // DWARF ARange version number\n"
3715       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
3716       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
3717       "    + sizeof(int8_t); // Segment Size (in bytes)");
3718 
3719   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
3720                "       == boost::fusion::at_c<1>(iiii).second;",
3721                Style);
3722 
3723   Style.ColumnLimit = 60;
3724   verifyFormat("zzzzzzzzzz\n"
3725                "    = bbbbbbbbbbbbbbbbb\n"
3726                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
3727                Style);
3728 
3729   Style.ColumnLimit = 80;
3730   Style.IndentWidth = 4;
3731   Style.TabWidth = 4;
3732   Style.UseTab = FormatStyle::UT_Always;
3733   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3734   Style.AlignOperands = false;
3735   EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
3736             "\t&& (someOtherLongishConditionPart1\n"
3737             "\t\t|| someOtherEvenLongerNestedConditionPart2);",
3738             format("return someVeryVeryLongConditionThatBarelyFitsOnALine && (someOtherLongishConditionPart1 || someOtherEvenLongerNestedConditionPart2);",
3739                    Style));
3740 }
3741 
3742 TEST_F(FormatTest, EnforcedOperatorWraps) {
3743   // Here we'd like to wrap after the || operators, but a comment is forcing an
3744   // earlier wrap.
3745   verifyFormat("bool x = aaaaa //\n"
3746                "         || bbbbb\n"
3747                "         //\n"
3748                "         || cccc;");
3749 }
3750 
3751 TEST_F(FormatTest, NoOperandAlignment) {
3752   FormatStyle Style = getLLVMStyle();
3753   Style.AlignOperands = false;
3754   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
3755                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3756                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3757                Style);
3758   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3759   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3760                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3761                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3762                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3763                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3764                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3765                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3766                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3767                "        > ccccccccccccccccccccccccccccccccccccccccc;",
3768                Style);
3769 
3770   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3771                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3772                "    + cc;",
3773                Style);
3774   verifyFormat("int a = aa\n"
3775                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3776                "        * cccccccccccccccccccccccccccccccccccc;\n",
3777                Style);
3778 
3779   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3780   verifyFormat("return (a > b\n"
3781                "    // comment1\n"
3782                "    // comment2\n"
3783                "    || c);",
3784                Style);
3785 }
3786 
3787 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
3788   FormatStyle Style = getLLVMStyle();
3789   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3790   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3791                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3792                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
3793                Style);
3794 }
3795 
3796 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
3797   FormatStyle Style = getLLVMStyle();
3798   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3799   Style.BinPackArguments = false;
3800   Style.ColumnLimit = 40;
3801   verifyFormat("void test() {\n"
3802                "  someFunction(\n"
3803                "      this + argument + is + quite\n"
3804                "      + long + so + it + gets + wrapped\n"
3805                "      + but + remains + bin - packed);\n"
3806                "}",
3807                Style);
3808   verifyFormat("void test() {\n"
3809                "  someFunction(arg1,\n"
3810                "               this + argument + is\n"
3811                "                   + quite + long + so\n"
3812                "                   + it + gets + wrapped\n"
3813                "                   + but + remains + bin\n"
3814                "                   - packed,\n"
3815                "               arg3);\n"
3816                "}",
3817                Style);
3818   verifyFormat("void test() {\n"
3819                "  someFunction(\n"
3820                "      arg1,\n"
3821                "      this + argument + has\n"
3822                "          + anotherFunc(nested,\n"
3823                "                        calls + whose\n"
3824                "                            + arguments\n"
3825                "                            + are + also\n"
3826                "                            + wrapped,\n"
3827                "                        in + addition)\n"
3828                "          + to + being + bin - packed,\n"
3829                "      arg3);\n"
3830                "}",
3831                Style);
3832 
3833   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
3834   verifyFormat("void test() {\n"
3835                "  someFunction(\n"
3836                "      arg1,\n"
3837                "      this + argument + has +\n"
3838                "          anotherFunc(nested,\n"
3839                "                      calls + whose +\n"
3840                "                          arguments +\n"
3841                "                          are + also +\n"
3842                "                          wrapped,\n"
3843                "                      in + addition) +\n"
3844                "          to + being + bin - packed,\n"
3845                "      arg3);\n"
3846                "}",
3847                Style);
3848 }
3849 
3850 TEST_F(FormatTest, ConstructorInitializers) {
3851   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3852   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
3853                getLLVMStyleWithColumns(45));
3854   verifyFormat("Constructor()\n"
3855                "    : Inttializer(FitsOnTheLine) {}",
3856                getLLVMStyleWithColumns(44));
3857   verifyFormat("Constructor()\n"
3858                "    : Inttializer(FitsOnTheLine) {}",
3859                getLLVMStyleWithColumns(43));
3860 
3861   verifyFormat("template <typename T>\n"
3862                "Constructor() : Initializer(FitsOnTheLine) {}",
3863                getLLVMStyleWithColumns(45));
3864 
3865   verifyFormat(
3866       "SomeClass::Constructor()\n"
3867       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3868 
3869   verifyFormat(
3870       "SomeClass::Constructor()\n"
3871       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3872       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
3873   verifyFormat(
3874       "SomeClass::Constructor()\n"
3875       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3876       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3877   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3878                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3879                "    : aaaaaaaaaa(aaaaaa) {}");
3880 
3881   verifyFormat("Constructor()\n"
3882                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3883                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3884                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3885                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
3886 
3887   verifyFormat("Constructor()\n"
3888                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3889                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3890 
3891   verifyFormat("Constructor(int Parameter = 0)\n"
3892                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3893                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
3894   verifyFormat("Constructor()\n"
3895                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3896                "}",
3897                getLLVMStyleWithColumns(60));
3898   verifyFormat("Constructor()\n"
3899                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3900                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
3901 
3902   // Here a line could be saved by splitting the second initializer onto two
3903   // lines, but that is not desirable.
3904   verifyFormat("Constructor()\n"
3905                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3906                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
3907                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3908 
3909   FormatStyle OnePerLine = getLLVMStyle();
3910   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3911   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3912   verifyFormat("SomeClass::Constructor()\n"
3913                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3914                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3915                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3916                OnePerLine);
3917   verifyFormat("SomeClass::Constructor()\n"
3918                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3919                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3920                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3921                OnePerLine);
3922   verifyFormat("MyClass::MyClass(int var)\n"
3923                "    : some_var_(var),            // 4 space indent\n"
3924                "      some_other_var_(var + 1) { // lined up\n"
3925                "}",
3926                OnePerLine);
3927   verifyFormat("Constructor()\n"
3928                "    : aaaaa(aaaaaa),\n"
3929                "      aaaaa(aaaaaa),\n"
3930                "      aaaaa(aaaaaa),\n"
3931                "      aaaaa(aaaaaa),\n"
3932                "      aaaaa(aaaaaa) {}",
3933                OnePerLine);
3934   verifyFormat("Constructor()\n"
3935                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3936                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
3937                OnePerLine);
3938   OnePerLine.BinPackParameters = false;
3939   verifyFormat(
3940       "Constructor()\n"
3941       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3942       "          aaaaaaaaaaa().aaa(),\n"
3943       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3944       OnePerLine);
3945   OnePerLine.ColumnLimit = 60;
3946   verifyFormat("Constructor()\n"
3947                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
3948                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3949                OnePerLine);
3950 
3951   EXPECT_EQ("Constructor()\n"
3952             "    : // Comment forcing unwanted break.\n"
3953             "      aaaa(aaaa) {}",
3954             format("Constructor() :\n"
3955                    "    // Comment forcing unwanted break.\n"
3956                    "    aaaa(aaaa) {}"));
3957 }
3958 
3959 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
3960   FormatStyle Style = getLLVMStyle();
3961   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
3962   Style.ColumnLimit = 60;
3963   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3964   Style.AllowAllConstructorInitializersOnNextLine = true;
3965   Style.BinPackParameters = false;
3966 
3967   for (int i = 0; i < 4; ++i) {
3968     // Test all combinations of parameters that should not have an effect.
3969     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
3970     Style.AllowAllArgumentsOnNextLine = i & 2;
3971 
3972     Style.AllowAllConstructorInitializersOnNextLine = true;
3973     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
3974     verifyFormat("Constructor()\n"
3975                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
3976                  Style);
3977     verifyFormat("Constructor() : a(a), b(b) {}", Style);
3978 
3979     Style.AllowAllConstructorInitializersOnNextLine = false;
3980     verifyFormat("Constructor()\n"
3981                  "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
3982                  "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
3983                  Style);
3984     verifyFormat("Constructor() : a(a), b(b) {}", Style);
3985 
3986     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
3987     Style.AllowAllConstructorInitializersOnNextLine = true;
3988     verifyFormat("Constructor()\n"
3989                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
3990                  Style);
3991 
3992     Style.AllowAllConstructorInitializersOnNextLine = false;
3993     verifyFormat("Constructor()\n"
3994                  "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
3995                  "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
3996                  Style);
3997 
3998     Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
3999     Style.AllowAllConstructorInitializersOnNextLine = true;
4000     verifyFormat("Constructor() :\n"
4001                  "    aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
4002                  Style);
4003 
4004     Style.AllowAllConstructorInitializersOnNextLine = false;
4005     verifyFormat("Constructor() :\n"
4006                  "    aaaaaaaaaaaaaaaaaa(a),\n"
4007                  "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
4008                  Style);
4009   }
4010 
4011   // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
4012   // AllowAllConstructorInitializersOnNextLine in all
4013   // BreakConstructorInitializers modes
4014   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
4015   Style.AllowAllParametersOfDeclarationOnNextLine = true;
4016   Style.AllowAllConstructorInitializersOnNextLine = false;
4017   verifyFormat("SomeClassWithALongName::Constructor(\n"
4018                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
4019                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
4020                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
4021                Style);
4022 
4023   Style.AllowAllConstructorInitializersOnNextLine = true;
4024   verifyFormat("SomeClassWithALongName::Constructor(\n"
4025                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
4026                "    int bbbbbbbbbbbbb,\n"
4027                "    int cccccccccccccccc)\n"
4028                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
4029                Style);
4030 
4031   Style.AllowAllParametersOfDeclarationOnNextLine = false;
4032   Style.AllowAllConstructorInitializersOnNextLine = false;
4033   verifyFormat("SomeClassWithALongName::Constructor(\n"
4034                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
4035                "    int bbbbbbbbbbbbb)\n"
4036                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
4037                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
4038                Style);
4039 
4040   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
4041 
4042   Style.AllowAllParametersOfDeclarationOnNextLine = true;
4043   verifyFormat("SomeClassWithALongName::Constructor(\n"
4044                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
4045                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
4046                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
4047                Style);
4048 
4049   Style.AllowAllConstructorInitializersOnNextLine = true;
4050   verifyFormat("SomeClassWithALongName::Constructor(\n"
4051                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
4052                "    int bbbbbbbbbbbbb,\n"
4053                "    int cccccccccccccccc)\n"
4054                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
4055                Style);
4056 
4057   Style.AllowAllParametersOfDeclarationOnNextLine = false;
4058   Style.AllowAllConstructorInitializersOnNextLine = false;
4059   verifyFormat("SomeClassWithALongName::Constructor(\n"
4060                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
4061                "    int bbbbbbbbbbbbb)\n"
4062                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
4063                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
4064                Style);
4065 
4066   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
4067   Style.AllowAllParametersOfDeclarationOnNextLine = true;
4068   verifyFormat("SomeClassWithALongName::Constructor(\n"
4069                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
4070                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
4071                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
4072                Style);
4073 
4074   Style.AllowAllConstructorInitializersOnNextLine = true;
4075   verifyFormat("SomeClassWithALongName::Constructor(\n"
4076                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
4077                "    int bbbbbbbbbbbbb,\n"
4078                "    int cccccccccccccccc) :\n"
4079                "    aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
4080                Style);
4081 
4082   Style.AllowAllParametersOfDeclarationOnNextLine = false;
4083   Style.AllowAllConstructorInitializersOnNextLine = false;
4084   verifyFormat("SomeClassWithALongName::Constructor(\n"
4085                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
4086                "    int bbbbbbbbbbbbb) :\n"
4087                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
4088                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
4089                Style);
4090 }
4091 
4092 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
4093   FormatStyle Style = getLLVMStyle();
4094   Style.ColumnLimit = 60;
4095   Style.BinPackArguments = false;
4096   for (int i = 0; i < 4; ++i) {
4097     // Test all combinations of parameters that should not have an effect.
4098     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
4099     Style.AllowAllConstructorInitializersOnNextLine = i & 2;
4100 
4101     Style.AllowAllArgumentsOnNextLine = true;
4102     verifyFormat("void foo() {\n"
4103                  "  FunctionCallWithReallyLongName(\n"
4104                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
4105                  "}",
4106                  Style);
4107     Style.AllowAllArgumentsOnNextLine = false;
4108     verifyFormat("void foo() {\n"
4109                  "  FunctionCallWithReallyLongName(\n"
4110                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4111                  "      bbbbbbbbbbbb);\n"
4112                  "}",
4113                  Style);
4114 
4115     Style.AllowAllArgumentsOnNextLine = true;
4116     verifyFormat("void foo() {\n"
4117                  "  auto VariableWithReallyLongName = {\n"
4118                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
4119                  "}",
4120                  Style);
4121     Style.AllowAllArgumentsOnNextLine = false;
4122     verifyFormat("void foo() {\n"
4123                  "  auto VariableWithReallyLongName = {\n"
4124                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4125                  "      bbbbbbbbbbbb};\n"
4126                  "}",
4127                  Style);
4128   }
4129 
4130   // This parameter should not affect declarations.
4131   Style.BinPackParameters = false;
4132   Style.AllowAllArgumentsOnNextLine = false;
4133   Style.AllowAllParametersOfDeclarationOnNextLine = true;
4134   verifyFormat("void FunctionCallWithReallyLongName(\n"
4135                "    int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
4136                Style);
4137   Style.AllowAllParametersOfDeclarationOnNextLine = false;
4138   verifyFormat("void FunctionCallWithReallyLongName(\n"
4139                "    int aaaaaaaaaaaaaaaaaaaaaaa,\n"
4140                "    int bbbbbbbbbbbb);",
4141                Style);
4142 }
4143 
4144 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
4145   FormatStyle Style = getLLVMStyle();
4146   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
4147 
4148   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
4149   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
4150                getStyleWithColumns(Style, 45));
4151   verifyFormat("Constructor() :\n"
4152                "    Initializer(FitsOnTheLine) {}",
4153                getStyleWithColumns(Style, 44));
4154   verifyFormat("Constructor() :\n"
4155                "    Initializer(FitsOnTheLine) {}",
4156                getStyleWithColumns(Style, 43));
4157 
4158   verifyFormat("template <typename T>\n"
4159                "Constructor() : Initializer(FitsOnTheLine) {}",
4160                getStyleWithColumns(Style, 50));
4161   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
4162   verifyFormat(
4163       "SomeClass::Constructor() :\n"
4164       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
4165       Style);
4166 
4167   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
4168   verifyFormat(
4169       "SomeClass::Constructor() :\n"
4170       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
4171       Style);
4172 
4173   verifyFormat(
4174       "SomeClass::Constructor() :\n"
4175       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
4176       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
4177       Style);
4178   verifyFormat(
4179       "SomeClass::Constructor() :\n"
4180       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4181       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
4182 	  Style);
4183   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4184                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4185                "    aaaaaaaaaa(aaaaaa) {}",
4186 			   Style);
4187 
4188   verifyFormat("Constructor() :\n"
4189                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4190                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4191                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4192                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
4193 			   Style);
4194 
4195   verifyFormat("Constructor() :\n"
4196                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4197                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4198 			   Style);
4199 
4200   verifyFormat("Constructor(int Parameter = 0) :\n"
4201                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
4202                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
4203 			   Style);
4204   verifyFormat("Constructor() :\n"
4205                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
4206                "}",
4207                getStyleWithColumns(Style, 60));
4208   verifyFormat("Constructor() :\n"
4209                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4210                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
4211 			   Style);
4212 
4213   // Here a line could be saved by splitting the second initializer onto two
4214   // lines, but that is not desirable.
4215   verifyFormat("Constructor() :\n"
4216                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
4217                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
4218                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4219 			   Style);
4220 
4221   FormatStyle OnePerLine = Style;
4222   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
4223   OnePerLine.AllowAllConstructorInitializersOnNextLine = false;
4224   verifyFormat("SomeClass::Constructor() :\n"
4225                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
4226                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
4227                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
4228                OnePerLine);
4229   verifyFormat("SomeClass::Constructor() :\n"
4230                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
4231                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
4232                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
4233                OnePerLine);
4234   verifyFormat("MyClass::MyClass(int var) :\n"
4235                "    some_var_(var),            // 4 space indent\n"
4236                "    some_other_var_(var + 1) { // lined up\n"
4237                "}",
4238                OnePerLine);
4239   verifyFormat("Constructor() :\n"
4240                "    aaaaa(aaaaaa),\n"
4241                "    aaaaa(aaaaaa),\n"
4242                "    aaaaa(aaaaaa),\n"
4243                "    aaaaa(aaaaaa),\n"
4244                "    aaaaa(aaaaaa) {}",
4245                OnePerLine);
4246   verifyFormat("Constructor() :\n"
4247                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
4248                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
4249                OnePerLine);
4250   OnePerLine.BinPackParameters = false;
4251   verifyFormat(
4252       "Constructor() :\n"
4253       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4254       "        aaaaaaaaaaa().aaa(),\n"
4255       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4256       OnePerLine);
4257   OnePerLine.ColumnLimit = 60;
4258   verifyFormat("Constructor() :\n"
4259                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
4260                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
4261                OnePerLine);
4262 
4263   EXPECT_EQ("Constructor() :\n"
4264             "    // Comment forcing unwanted break.\n"
4265             "    aaaa(aaaa) {}",
4266             format("Constructor() :\n"
4267                    "    // Comment forcing unwanted break.\n"
4268                    "    aaaa(aaaa) {}",
4269 				   Style));
4270 
4271   Style.ColumnLimit = 0;
4272   verifyFormat("SomeClass::Constructor() :\n"
4273                "    a(a) {}",
4274                Style);
4275   verifyFormat("SomeClass::Constructor() noexcept :\n"
4276                "    a(a) {}",
4277                Style);
4278   verifyFormat("SomeClass::Constructor() :\n"
4279 			   "    a(a), b(b), c(c) {}",
4280                Style);
4281   verifyFormat("SomeClass::Constructor() :\n"
4282                "    a(a) {\n"
4283                "  foo();\n"
4284                "  bar();\n"
4285                "}",
4286                Style);
4287 
4288   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
4289   verifyFormat("SomeClass::Constructor() :\n"
4290 			   "    a(a), b(b), c(c) {\n"
4291 			   "}",
4292                Style);
4293   verifyFormat("SomeClass::Constructor() :\n"
4294                "    a(a) {\n"
4295 			   "}",
4296                Style);
4297 
4298   Style.ColumnLimit = 80;
4299   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
4300   Style.ConstructorInitializerIndentWidth = 2;
4301   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}",
4302                Style);
4303   verifyFormat("SomeClass::Constructor() :\n"
4304                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4305                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
4306                Style);
4307 
4308   // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as well
4309   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
4310   verifyFormat("class SomeClass\n"
4311                "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4312                "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
4313                Style);
4314   Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
4315   verifyFormat("class SomeClass\n"
4316                "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4317                "  , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
4318                Style);
4319   Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
4320   verifyFormat("class SomeClass :\n"
4321                "  public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4322                "  public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
4323                Style);
4324 }
4325 
4326 #ifndef EXPENSIVE_CHECKS
4327 // Expensive checks enables libstdc++ checking which includes validating the
4328 // state of ranges used in std::priority_queue - this blows out the
4329 // runtime/scalability of the function and makes this test unacceptably slow.
4330 TEST_F(FormatTest, MemoizationTests) {
4331   // This breaks if the memoization lookup does not take \c Indent and
4332   // \c LastSpace into account.
4333   verifyFormat(
4334       "extern CFRunLoopTimerRef\n"
4335       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
4336       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
4337       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
4338       "                     CFRunLoopTimerContext *context) {}");
4339 
4340   // Deep nesting somewhat works around our memoization.
4341   verifyFormat(
4342       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
4343       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
4344       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
4345       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
4346       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
4347       getLLVMStyleWithColumns(65));
4348   verifyFormat(
4349       "aaaaa(\n"
4350       "    aaaaa,\n"
4351       "    aaaaa(\n"
4352       "        aaaaa,\n"
4353       "        aaaaa(\n"
4354       "            aaaaa,\n"
4355       "            aaaaa(\n"
4356       "                aaaaa,\n"
4357       "                aaaaa(\n"
4358       "                    aaaaa,\n"
4359       "                    aaaaa(\n"
4360       "                        aaaaa,\n"
4361       "                        aaaaa(\n"
4362       "                            aaaaa,\n"
4363       "                            aaaaa(\n"
4364       "                                aaaaa,\n"
4365       "                                aaaaa(\n"
4366       "                                    aaaaa,\n"
4367       "                                    aaaaa(\n"
4368       "                                        aaaaa,\n"
4369       "                                        aaaaa(\n"
4370       "                                            aaaaa,\n"
4371       "                                            aaaaa(\n"
4372       "                                                aaaaa,\n"
4373       "                                                aaaaa))))))))))));",
4374       getLLVMStyleWithColumns(65));
4375   verifyFormat(
4376       "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"
4377       "                                  a),\n"
4378       "                                a),\n"
4379       "                              a),\n"
4380       "                            a),\n"
4381       "                          a),\n"
4382       "                        a),\n"
4383       "                      a),\n"
4384       "                    a),\n"
4385       "                  a),\n"
4386       "                a),\n"
4387       "              a),\n"
4388       "            a),\n"
4389       "          a),\n"
4390       "        a),\n"
4391       "      a),\n"
4392       "    a),\n"
4393       "  a)",
4394       getLLVMStyleWithColumns(65));
4395 
4396   // This test takes VERY long when memoization is broken.
4397   FormatStyle OnePerLine = getLLVMStyle();
4398   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
4399   OnePerLine.BinPackParameters = false;
4400   std::string input = "Constructor()\n"
4401                       "    : aaaa(a,\n";
4402   for (unsigned i = 0, e = 80; i != e; ++i) {
4403     input += "           a,\n";
4404   }
4405   input += "           a) {}";
4406   verifyFormat(input, OnePerLine);
4407 }
4408 #endif
4409 
4410 TEST_F(FormatTest, BreaksAsHighAsPossible) {
4411   verifyFormat(
4412       "void f() {\n"
4413       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
4414       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
4415       "    f();\n"
4416       "}");
4417   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
4418                "    Intervals[i - 1].getRange().getLast()) {\n}");
4419 }
4420 
4421 TEST_F(FormatTest, BreaksFunctionDeclarations) {
4422   // Principially, we break function declarations in a certain order:
4423   // 1) break amongst arguments.
4424   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
4425                "                              Cccccccccccccc cccccccccccccc);");
4426   verifyFormat("template <class TemplateIt>\n"
4427                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
4428                "                            TemplateIt *stop) {}");
4429 
4430   // 2) break after return type.
4431   verifyFormat(
4432       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4433       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
4434       getGoogleStyle());
4435 
4436   // 3) break after (.
4437   verifyFormat(
4438       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
4439       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
4440       getGoogleStyle());
4441 
4442   // 4) break before after nested name specifiers.
4443   verifyFormat(
4444       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4445       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
4446       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
4447       getGoogleStyle());
4448 
4449   // However, there are exceptions, if a sufficient amount of lines can be
4450   // saved.
4451   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
4452   // more adjusting.
4453   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
4454                "                                  Cccccccccccccc cccccccccc,\n"
4455                "                                  Cccccccccccccc cccccccccc,\n"
4456                "                                  Cccccccccccccc cccccccccc,\n"
4457                "                                  Cccccccccccccc cccccccccc);");
4458   verifyFormat(
4459       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4460       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
4461       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
4462       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
4463       getGoogleStyle());
4464   verifyFormat(
4465       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
4466       "                                          Cccccccccccccc cccccccccc,\n"
4467       "                                          Cccccccccccccc cccccccccc,\n"
4468       "                                          Cccccccccccccc cccccccccc,\n"
4469       "                                          Cccccccccccccc cccccccccc,\n"
4470       "                                          Cccccccccccccc cccccccccc,\n"
4471       "                                          Cccccccccccccc cccccccccc);");
4472   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4473                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
4474                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
4475                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
4476                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
4477 
4478   // Break after multi-line parameters.
4479   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4480                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4481                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4482                "    bbbb bbbb);");
4483   verifyFormat("void SomeLoooooooooooongFunction(\n"
4484                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
4485                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4486                "    int bbbbbbbbbbbbb);");
4487 
4488   // Treat overloaded operators like other functions.
4489   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
4490                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
4491   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
4492                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
4493   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
4494                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
4495   verifyGoogleFormat(
4496       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
4497       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
4498   verifyGoogleFormat(
4499       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
4500       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
4501   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4502                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
4503   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
4504                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
4505   verifyGoogleFormat(
4506       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
4507       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4508       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
4509   verifyGoogleFormat(
4510       "template <typename T>\n"
4511       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4512       "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
4513       "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
4514 
4515   FormatStyle Style = getLLVMStyle();
4516   Style.PointerAlignment = FormatStyle::PAS_Left;
4517   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4518                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
4519                Style);
4520   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
4521                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4522                Style);
4523 }
4524 
4525 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
4526   // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
4527   // Prefer keeping `::` followed by `operator` together.
4528   EXPECT_EQ("const aaaa::bbbbbbb &\n"
4529             "ccccccccc::operator++() {\n"
4530             "  stuff();\n"
4531             "}",
4532             format("const aaaa::bbbbbbb\n"
4533                    "&ccccccccc::operator++() { stuff(); }",
4534                    getLLVMStyleWithColumns(40)));
4535 }
4536 
4537 TEST_F(FormatTest, TrailingReturnType) {
4538   verifyFormat("auto foo() -> int;\n");
4539   verifyFormat("struct S {\n"
4540                "  auto bar() const -> int;\n"
4541                "};");
4542   verifyFormat("template <size_t Order, typename T>\n"
4543                "auto load_img(const std::string &filename)\n"
4544                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
4545   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
4546                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
4547   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
4548   verifyFormat("template <typename T>\n"
4549                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
4550                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
4551 
4552   // Not trailing return types.
4553   verifyFormat("void f() { auto a = b->c(); }");
4554 }
4555 
4556 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
4557   // Avoid breaking before trailing 'const' or other trailing annotations, if
4558   // they are not function-like.
4559   FormatStyle Style = getGoogleStyle();
4560   Style.ColumnLimit = 47;
4561   verifyFormat("void someLongFunction(\n"
4562                "    int someLoooooooooooooongParameter) const {\n}",
4563                getLLVMStyleWithColumns(47));
4564   verifyFormat("LoooooongReturnType\n"
4565                "someLoooooooongFunction() const {}",
4566                getLLVMStyleWithColumns(47));
4567   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
4568                "    const {}",
4569                Style);
4570   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
4571                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
4572   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
4573                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
4574   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
4575                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
4576   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
4577                "                   aaaaaaaaaaa aaaaa) const override;");
4578   verifyGoogleFormat(
4579       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4580       "    const override;");
4581 
4582   // Even if the first parameter has to be wrapped.
4583   verifyFormat("void someLongFunction(\n"
4584                "    int someLongParameter) const {}",
4585                getLLVMStyleWithColumns(46));
4586   verifyFormat("void someLongFunction(\n"
4587                "    int someLongParameter) const {}",
4588                Style);
4589   verifyFormat("void someLongFunction(\n"
4590                "    int someLongParameter) override {}",
4591                Style);
4592   verifyFormat("void someLongFunction(\n"
4593                "    int someLongParameter) OVERRIDE {}",
4594                Style);
4595   verifyFormat("void someLongFunction(\n"
4596                "    int someLongParameter) final {}",
4597                Style);
4598   verifyFormat("void someLongFunction(\n"
4599                "    int someLongParameter) FINAL {}",
4600                Style);
4601   verifyFormat("void someLongFunction(\n"
4602                "    int parameter) const override {}",
4603                Style);
4604 
4605   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4606   verifyFormat("void someLongFunction(\n"
4607                "    int someLongParameter) const\n"
4608                "{\n"
4609                "}",
4610                Style);
4611 
4612   // Unless these are unknown annotations.
4613   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
4614                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4615                "    LONG_AND_UGLY_ANNOTATION;");
4616 
4617   // Breaking before function-like trailing annotations is fine to keep them
4618   // close to their arguments.
4619   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4620                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
4621   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
4622                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
4623   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
4624                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
4625   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
4626                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
4627   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
4628 
4629   verifyFormat(
4630       "void aaaaaaaaaaaaaaaaaa()\n"
4631       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
4632       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
4633   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4634                "    __attribute__((unused));");
4635   verifyGoogleFormat(
4636       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4637       "    GUARDED_BY(aaaaaaaaaaaa);");
4638   verifyGoogleFormat(
4639       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4640       "    GUARDED_BY(aaaaaaaaaaaa);");
4641   verifyGoogleFormat(
4642       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
4643       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4644   verifyGoogleFormat(
4645       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
4646       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
4647 }
4648 
4649 TEST_F(FormatTest, FunctionAnnotations) {
4650   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4651                "int OldFunction(const string &parameter) {}");
4652   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4653                "string OldFunction(const string &parameter) {}");
4654   verifyFormat("template <typename T>\n"
4655                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4656                "string OldFunction(const string &parameter) {}");
4657 
4658   // Not function annotations.
4659   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4660                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
4661   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
4662                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
4663   verifyFormat("MACRO(abc).function() // wrap\n"
4664                "    << abc;");
4665   verifyFormat("MACRO(abc)->function() // wrap\n"
4666                "    << abc;");
4667   verifyFormat("MACRO(abc)::function() // wrap\n"
4668                "    << abc;");
4669 }
4670 
4671 TEST_F(FormatTest, BreaksDesireably) {
4672   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
4673                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
4674                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
4675   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4676                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
4677                "}");
4678 
4679   verifyFormat(
4680       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4681       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
4682 
4683   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4684                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4685                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4686 
4687   verifyFormat(
4688       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4689       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4690       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4691       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4692       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
4693 
4694   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4695                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4696 
4697   verifyFormat(
4698       "void f() {\n"
4699       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
4700       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4701       "}");
4702   verifyFormat(
4703       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4704       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4705   verifyFormat(
4706       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4707       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4708   verifyFormat(
4709       "aaaaaa(aaa,\n"
4710       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4711       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4712       "       aaaa);");
4713   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4714                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4715                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4716 
4717   // Indent consistently independent of call expression and unary operator.
4718   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4719                "    dddddddddddddddddddddddddddddd));");
4720   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4721                "    dddddddddddddddddddddddddddddd));");
4722   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
4723                "    dddddddddddddddddddddddddddddd));");
4724 
4725   // This test case breaks on an incorrect memoization, i.e. an optimization not
4726   // taking into account the StopAt value.
4727   verifyFormat(
4728       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4729       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4730       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4731       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4732 
4733   verifyFormat("{\n  {\n    {\n"
4734                "      Annotation.SpaceRequiredBefore =\n"
4735                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
4736                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
4737                "    }\n  }\n}");
4738 
4739   // Break on an outer level if there was a break on an inner level.
4740   EXPECT_EQ("f(g(h(a, // comment\n"
4741             "      b, c),\n"
4742             "    d, e),\n"
4743             "  x, y);",
4744             format("f(g(h(a, // comment\n"
4745                    "    b, c), d, e), x, y);"));
4746 
4747   // Prefer breaking similar line breaks.
4748   verifyFormat(
4749       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
4750       "                             NSTrackingMouseEnteredAndExited |\n"
4751       "                             NSTrackingActiveAlways;");
4752 }
4753 
4754 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
4755   FormatStyle NoBinPacking = getGoogleStyle();
4756   NoBinPacking.BinPackParameters = false;
4757   NoBinPacking.BinPackArguments = true;
4758   verifyFormat("void f() {\n"
4759                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
4760                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4761                "}",
4762                NoBinPacking);
4763   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
4764                "       int aaaaaaaaaaaaaaaaaaaa,\n"
4765                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4766                NoBinPacking);
4767 
4768   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4769   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4770                "                        vector<int> bbbbbbbbbbbbbbb);",
4771                NoBinPacking);
4772   // FIXME: This behavior difference is probably not wanted. However, currently
4773   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
4774   // template arguments from BreakBeforeParameter being set because of the
4775   // one-per-line formatting.
4776   verifyFormat(
4777       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4778       "                                             aaaaaaaaaa> aaaaaaaaaa);",
4779       NoBinPacking);
4780   verifyFormat(
4781       "void fffffffffff(\n"
4782       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
4783       "        aaaaaaaaaa);");
4784 }
4785 
4786 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
4787   FormatStyle NoBinPacking = getGoogleStyle();
4788   NoBinPacking.BinPackParameters = false;
4789   NoBinPacking.BinPackArguments = false;
4790   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
4791                "  aaaaaaaaaaaaaaaaaaaa,\n"
4792                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
4793                NoBinPacking);
4794   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
4795                "        aaaaaaaaaaaaa,\n"
4796                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
4797                NoBinPacking);
4798   verifyFormat(
4799       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4800       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4801       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4802       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4803       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
4804       NoBinPacking);
4805   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4806                "    .aaaaaaaaaaaaaaaaaa();",
4807                NoBinPacking);
4808   verifyFormat("void f() {\n"
4809                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4810                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
4811                "}",
4812                NoBinPacking);
4813 
4814   verifyFormat(
4815       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4816       "             aaaaaaaaaaaa,\n"
4817       "             aaaaaaaaaaaa);",
4818       NoBinPacking);
4819   verifyFormat(
4820       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
4821       "                               ddddddddddddddddddddddddddddd),\n"
4822       "             test);",
4823       NoBinPacking);
4824 
4825   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4826                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
4827                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
4828                "    aaaaaaaaaaaaaaaaaa;",
4829                NoBinPacking);
4830   verifyFormat("a(\"a\"\n"
4831                "  \"a\",\n"
4832                "  a);");
4833 
4834   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4835   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
4836                "                aaaaaaaaa,\n"
4837                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4838                NoBinPacking);
4839   verifyFormat(
4840       "void f() {\n"
4841       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4842       "      .aaaaaaa();\n"
4843       "}",
4844       NoBinPacking);
4845   verifyFormat(
4846       "template <class SomeType, class SomeOtherType>\n"
4847       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
4848       NoBinPacking);
4849 }
4850 
4851 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
4852   FormatStyle Style = getLLVMStyleWithColumns(15);
4853   Style.ExperimentalAutoDetectBinPacking = true;
4854   EXPECT_EQ("aaa(aaaa,\n"
4855             "    aaaa,\n"
4856             "    aaaa);\n"
4857             "aaa(aaaa,\n"
4858             "    aaaa,\n"
4859             "    aaaa);",
4860             format("aaa(aaaa,\n" // one-per-line
4861                    "  aaaa,\n"
4862                    "    aaaa  );\n"
4863                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4864                    Style));
4865   EXPECT_EQ("aaa(aaaa, aaaa,\n"
4866             "    aaaa);\n"
4867             "aaa(aaaa, aaaa,\n"
4868             "    aaaa);",
4869             format("aaa(aaaa,  aaaa,\n" // bin-packed
4870                    "    aaaa  );\n"
4871                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4872                    Style));
4873 }
4874 
4875 TEST_F(FormatTest, FormatsBuilderPattern) {
4876   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
4877                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
4878                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
4879                "    .StartsWith(\".init\", ORDER_INIT)\n"
4880                "    .StartsWith(\".fini\", ORDER_FINI)\n"
4881                "    .StartsWith(\".hash\", ORDER_HASH)\n"
4882                "    .Default(ORDER_TEXT);\n");
4883 
4884   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
4885                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
4886   verifyFormat(
4887       "aaaaaaa->aaaaaaa\n"
4888       "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4889       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4890       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4891   verifyFormat(
4892       "aaaaaaa->aaaaaaa\n"
4893       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4894       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4895   verifyFormat(
4896       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
4897       "    aaaaaaaaaaaaaa);");
4898   verifyFormat(
4899       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
4900       "    aaaaaa->aaaaaaaaaaaa()\n"
4901       "        ->aaaaaaaaaaaaaaaa(\n"
4902       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4903       "        ->aaaaaaaaaaaaaaaaa();");
4904   verifyGoogleFormat(
4905       "void f() {\n"
4906       "  someo->Add((new util::filetools::Handler(dir))\n"
4907       "                 ->OnEvent1(NewPermanentCallback(\n"
4908       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
4909       "                 ->OnEvent2(NewPermanentCallback(\n"
4910       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
4911       "                 ->OnEvent3(NewPermanentCallback(\n"
4912       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
4913       "                 ->OnEvent5(NewPermanentCallback(\n"
4914       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
4915       "                 ->OnEvent6(NewPermanentCallback(\n"
4916       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
4917       "}");
4918 
4919   verifyFormat(
4920       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
4921   verifyFormat("aaaaaaaaaaaaaaa()\n"
4922                "    .aaaaaaaaaaaaaaa()\n"
4923                "    .aaaaaaaaaaaaaaa()\n"
4924                "    .aaaaaaaaaaaaaaa()\n"
4925                "    .aaaaaaaaaaaaaaa();");
4926   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4927                "    .aaaaaaaaaaaaaaa()\n"
4928                "    .aaaaaaaaaaaaaaa()\n"
4929                "    .aaaaaaaaaaaaaaa();");
4930   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4931                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4932                "    .aaaaaaaaaaaaaaa();");
4933   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
4934                "    ->aaaaaaaaaaaaaae(0)\n"
4935                "    ->aaaaaaaaaaaaaaa();");
4936 
4937   // Don't linewrap after very short segments.
4938   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4939                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4940                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4941   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4942                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4943                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4944   verifyFormat("aaa()\n"
4945                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4946                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4947                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4948 
4949   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4950                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4951                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
4952   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4953                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
4954                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
4955 
4956   // Prefer not to break after empty parentheses.
4957   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
4958                "    First->LastNewlineOffset);");
4959 
4960   // Prefer not to create "hanging" indents.
4961   verifyFormat(
4962       "return !soooooooooooooome_map\n"
4963       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4964       "            .second;");
4965   verifyFormat(
4966       "return aaaaaaaaaaaaaaaa\n"
4967       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
4968       "    .aaaa(aaaaaaaaaaaaaa);");
4969   // No hanging indent here.
4970   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
4971                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4972   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
4973                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4974   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4975                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4976                getLLVMStyleWithColumns(60));
4977   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
4978                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4979                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4980                getLLVMStyleWithColumns(59));
4981   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4982                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4983                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4984 
4985   // Dont break if only closing statements before member call
4986   verifyFormat("test() {\n"
4987                "  ([]() -> {\n"
4988                "    int b = 32;\n"
4989                "    return 3;\n"
4990                "  }).foo();\n"
4991                "}");
4992   verifyFormat("test() {\n"
4993                "  (\n"
4994                "      []() -> {\n"
4995                "        int b = 32;\n"
4996                "        return 3;\n"
4997                "      },\n"
4998                "      foo, bar)\n"
4999                "      .foo();\n"
5000                "}");
5001   verifyFormat("test() {\n"
5002                "  ([]() -> {\n"
5003                "    int b = 32;\n"
5004                "    return 3;\n"
5005                "  })\n"
5006                "      .foo()\n"
5007                "      .bar();\n"
5008                "}");
5009   verifyFormat("test() {\n"
5010                "  ([]() -> {\n"
5011                "    int b = 32;\n"
5012                "    return 3;\n"
5013                "  })\n"
5014                "      .foo(\"aaaaaaaaaaaaaaaaa\"\n"
5015                "           \"bbbb\");\n"
5016                "}",
5017                getLLVMStyleWithColumns(30));
5018 }
5019 
5020 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
5021   verifyFormat(
5022       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5023       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
5024   verifyFormat(
5025       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
5026       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
5027 
5028   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
5029                "    ccccccccccccccccccccccccc) {\n}");
5030   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
5031                "    ccccccccccccccccccccccccc) {\n}");
5032 
5033   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
5034                "    ccccccccccccccccccccccccc) {\n}");
5035   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
5036                "    ccccccccccccccccccccccccc) {\n}");
5037 
5038   verifyFormat(
5039       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
5040       "    ccccccccccccccccccccccccc) {\n}");
5041   verifyFormat(
5042       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
5043       "    ccccccccccccccccccccccccc) {\n}");
5044 
5045   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
5046                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
5047                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
5048                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
5049   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
5050                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
5051                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
5052                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
5053 
5054   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
5055                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
5056                "    aaaaaaaaaaaaaaa != aa) {\n}");
5057   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
5058                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
5059                "    aaaaaaaaaaaaaaa != aa) {\n}");
5060 }
5061 
5062 TEST_F(FormatTest, BreaksAfterAssignments) {
5063   verifyFormat(
5064       "unsigned Cost =\n"
5065       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
5066       "                        SI->getPointerAddressSpaceee());\n");
5067   verifyFormat(
5068       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
5069       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
5070 
5071   verifyFormat(
5072       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
5073       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
5074   verifyFormat("unsigned OriginalStartColumn =\n"
5075                "    SourceMgr.getSpellingColumnNumber(\n"
5076                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
5077                "    1;");
5078 }
5079 
5080 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
5081   FormatStyle Style = getLLVMStyle();
5082   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5083                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
5084                Style);
5085 
5086   Style.PenaltyBreakAssignment = 20;
5087   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
5088                "                                 cccccccccccccccccccccccccc;",
5089                Style);
5090 }
5091 
5092 TEST_F(FormatTest, AlignsAfterAssignments) {
5093   verifyFormat(
5094       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5095       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
5096   verifyFormat(
5097       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5098       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
5099   verifyFormat(
5100       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5101       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
5102   verifyFormat(
5103       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5104       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
5105   verifyFormat(
5106       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
5107       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
5108       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
5109 }
5110 
5111 TEST_F(FormatTest, AlignsAfterReturn) {
5112   verifyFormat(
5113       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5114       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
5115   verifyFormat(
5116       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5117       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
5118   verifyFormat(
5119       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
5120       "       aaaaaaaaaaaaaaaaaaaaaa();");
5121   verifyFormat(
5122       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
5123       "        aaaaaaaaaaaaaaaaaaaaaa());");
5124   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5125                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5126   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5127                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
5128                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5129   verifyFormat("return\n"
5130                "    // true if code is one of a or b.\n"
5131                "    code == a || code == b;");
5132 }
5133 
5134 TEST_F(FormatTest, AlignsAfterOpenBracket) {
5135   verifyFormat(
5136       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
5137       "                                                aaaaaaaaa aaaaaaa) {}");
5138   verifyFormat(
5139       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
5140       "                                               aaaaaaaaaaa aaaaaaaaa);");
5141   verifyFormat(
5142       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
5143       "                                             aaaaaaaaaaaaaaaaaaaaa));");
5144   FormatStyle Style = getLLVMStyle();
5145   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5146   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5147                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
5148                Style);
5149   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
5150                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
5151                Style);
5152   verifyFormat("SomeLongVariableName->someFunction(\n"
5153                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
5154                Style);
5155   verifyFormat(
5156       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
5157       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
5158       Style);
5159   verifyFormat(
5160       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
5161       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5162       Style);
5163   verifyFormat(
5164       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
5165       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
5166       Style);
5167 
5168   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
5169                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
5170                "        b));",
5171                Style);
5172 
5173   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5174   Style.BinPackArguments = false;
5175   Style.BinPackParameters = false;
5176   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5177                "    aaaaaaaaaaa aaaaaaaa,\n"
5178                "    aaaaaaaaa aaaaaaa,\n"
5179                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
5180                Style);
5181   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
5182                "    aaaaaaaaaaa aaaaaaaaa,\n"
5183                "    aaaaaaaaaaa aaaaaaaaa,\n"
5184                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5185                Style);
5186   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
5187                "    aaaaaaaaaaaaaaa,\n"
5188                "    aaaaaaaaaaaaaaaaaaaaa,\n"
5189                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
5190                Style);
5191   verifyFormat(
5192       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
5193       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
5194       Style);
5195   verifyFormat(
5196       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
5197       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
5198       Style);
5199   verifyFormat(
5200       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5201       "    aaaaaaaaaaaaaaaaaaaaa(\n"
5202       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
5203       "    aaaaaaaaaaaaaaaa);",
5204       Style);
5205   verifyFormat(
5206       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5207       "    aaaaaaaaaaaaaaaaaaaaa(\n"
5208       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
5209       "    aaaaaaaaaaaaaaaa);",
5210       Style);
5211 }
5212 
5213 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
5214   FormatStyle Style = getLLVMStyleWithColumns(40);
5215   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
5216                "          bbbbbbbbbbbbbbbbbbbbbb);",
5217                Style);
5218   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
5219   Style.AlignOperands = false;
5220   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
5221                "          bbbbbbbbbbbbbbbbbbbbbb);",
5222                Style);
5223   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5224   Style.AlignOperands = true;
5225   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
5226                "          bbbbbbbbbbbbbbbbbbbbbb);",
5227                Style);
5228   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5229   Style.AlignOperands = false;
5230   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
5231                "    bbbbbbbbbbbbbbbbbbbbbb);",
5232                Style);
5233 }
5234 
5235 TEST_F(FormatTest, BreaksConditionalExpressions) {
5236   verifyFormat(
5237       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5238       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5239       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5240   verifyFormat(
5241       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
5242       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5243       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5244   verifyFormat(
5245       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5246       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5247   verifyFormat(
5248       "aaaa(aaaaaaaaa, aaaaaaaaa,\n"
5249       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5250       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5251   verifyFormat(
5252       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
5253       "                                                    : aaaaaaaaaaaaa);");
5254   verifyFormat(
5255       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5256       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5257       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5258       "                   aaaaaaaaaaaaa);");
5259   verifyFormat(
5260       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5261       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5262       "                   aaaaaaaaaaaaa);");
5263   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5264                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5265                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5266                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5267                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5268   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5269                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5270                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5271                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5272                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5273                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5274                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5275   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5276                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5277                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5278                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5279                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5280   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5281                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5282                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5283   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
5284                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5285                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5286                "        : aaaaaaaaaaaaaaaa;");
5287   verifyFormat(
5288       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5289       "    ? aaaaaaaaaaaaaaa\n"
5290       "    : aaaaaaaaaaaaaaa;");
5291   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
5292                "          aaaaaaaaa\n"
5293                "      ? b\n"
5294                "      : c);");
5295   verifyFormat("return aaaa == bbbb\n"
5296                "           // comment\n"
5297                "           ? aaaa\n"
5298                "           : bbbb;");
5299   verifyFormat("unsigned Indent =\n"
5300                "    format(TheLine.First,\n"
5301                "           IndentForLevel[TheLine.Level] >= 0\n"
5302                "               ? IndentForLevel[TheLine.Level]\n"
5303                "               : TheLine * 2,\n"
5304                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
5305                getLLVMStyleWithColumns(60));
5306   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
5307                "                  ? aaaaaaaaaaaaaaa\n"
5308                "                  : bbbbbbbbbbbbbbb //\n"
5309                "                        ? ccccccccccccccc\n"
5310                "                        : ddddddddddddddd;");
5311   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
5312                "                  ? aaaaaaaaaaaaaaa\n"
5313                "                  : (bbbbbbbbbbbbbbb //\n"
5314                "                         ? ccccccccccccccc\n"
5315                "                         : ddddddddddddddd);");
5316   verifyFormat(
5317       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5318       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5319       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
5320       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
5321       "                                      : aaaaaaaaaa;");
5322   verifyFormat(
5323       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5324       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
5325       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5326 
5327   FormatStyle NoBinPacking = getLLVMStyle();
5328   NoBinPacking.BinPackArguments = false;
5329   verifyFormat(
5330       "void f() {\n"
5331       "  g(aaa,\n"
5332       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
5333       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5334       "        ? aaaaaaaaaaaaaaa\n"
5335       "        : aaaaaaaaaaaaaaa);\n"
5336       "}",
5337       NoBinPacking);
5338   verifyFormat(
5339       "void f() {\n"
5340       "  g(aaa,\n"
5341       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
5342       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5343       "        ?: aaaaaaaaaaaaaaa);\n"
5344       "}",
5345       NoBinPacking);
5346 
5347   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
5348                "             // comment.\n"
5349                "             ccccccccccccccccccccccccccccccccccccccc\n"
5350                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5351                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
5352 
5353   // Assignments in conditional expressions. Apparently not uncommon :-(.
5354   verifyFormat("return a != b\n"
5355                "           // comment\n"
5356                "           ? a = b\n"
5357                "           : a = b;");
5358   verifyFormat("return a != b\n"
5359                "           // comment\n"
5360                "           ? a = a != b\n"
5361                "                     // comment\n"
5362                "                     ? a = b\n"
5363                "                     : a\n"
5364                "           : a;\n");
5365   verifyFormat("return a != b\n"
5366                "           // comment\n"
5367                "           ? a\n"
5368                "           : a = a != b\n"
5369                "                     // comment\n"
5370                "                     ? a = b\n"
5371                "                     : a;");
5372 }
5373 
5374 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
5375   FormatStyle Style = getLLVMStyle();
5376   Style.BreakBeforeTernaryOperators = false;
5377   Style.ColumnLimit = 70;
5378   verifyFormat(
5379       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5380       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5381       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5382       Style);
5383   verifyFormat(
5384       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
5385       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5386       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5387       Style);
5388   verifyFormat(
5389       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5390       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5391       Style);
5392   verifyFormat(
5393       "aaaa(aaaaaaaa, aaaaaaaaaa,\n"
5394       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5395       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5396       Style);
5397   verifyFormat(
5398       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
5399       "                                                      aaaaaaaaaaaaa);",
5400       Style);
5401   verifyFormat(
5402       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5403       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5404       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5405       "                   aaaaaaaaaaaaa);",
5406       Style);
5407   verifyFormat(
5408       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5409       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5410       "                   aaaaaaaaaaaaa);",
5411       Style);
5412   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5413                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5414                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
5415                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5416                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5417                Style);
5418   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5419                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5420                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5421                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
5422                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5423                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5424                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5425                Style);
5426   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5427                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
5428                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5429                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5430                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5431                Style);
5432   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5433                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5434                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5435                Style);
5436   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
5437                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5438                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5439                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5440                Style);
5441   verifyFormat(
5442       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5443       "    aaaaaaaaaaaaaaa :\n"
5444       "    aaaaaaaaaaaaaaa;",
5445       Style);
5446   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
5447                "          aaaaaaaaa ?\n"
5448                "      b :\n"
5449                "      c);",
5450                Style);
5451   verifyFormat("unsigned Indent =\n"
5452                "    format(TheLine.First,\n"
5453                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
5454                "               IndentForLevel[TheLine.Level] :\n"
5455                "               TheLine * 2,\n"
5456                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
5457                Style);
5458   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
5459                "                  aaaaaaaaaaaaaaa :\n"
5460                "                  bbbbbbbbbbbbbbb ? //\n"
5461                "                      ccccccccccccccc :\n"
5462                "                      ddddddddddddddd;",
5463                Style);
5464   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
5465                "                  aaaaaaaaaaaaaaa :\n"
5466                "                  (bbbbbbbbbbbbbbb ? //\n"
5467                "                       ccccccccccccccc :\n"
5468                "                       ddddddddddddddd);",
5469                Style);
5470   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5471                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
5472                "            ccccccccccccccccccccccccccc;",
5473                Style);
5474   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5475                "           aaaaa :\n"
5476                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
5477                Style);
5478 }
5479 
5480 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
5481   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
5482                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
5483   verifyFormat("bool a = true, b = false;");
5484 
5485   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5486                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
5487                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
5488                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
5489   verifyFormat(
5490       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
5491       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
5492       "     d = e && f;");
5493   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
5494                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
5495   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
5496                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
5497   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
5498                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
5499 
5500   FormatStyle Style = getGoogleStyle();
5501   Style.PointerAlignment = FormatStyle::PAS_Left;
5502   Style.DerivePointerAlignment = false;
5503   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5504                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
5505                "    *b = bbbbbbbbbbbbbbbbbbb;",
5506                Style);
5507   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
5508                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
5509                Style);
5510   verifyFormat("vector<int*> a, b;", Style);
5511   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
5512 }
5513 
5514 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
5515   verifyFormat("arr[foo ? bar : baz];");
5516   verifyFormat("f()[foo ? bar : baz];");
5517   verifyFormat("(a + b)[foo ? bar : baz];");
5518   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
5519 }
5520 
5521 TEST_F(FormatTest, AlignsStringLiterals) {
5522   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
5523                "                                      \"short literal\");");
5524   verifyFormat(
5525       "looooooooooooooooooooooooongFunction(\n"
5526       "    \"short literal\"\n"
5527       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
5528   verifyFormat("someFunction(\"Always break between multi-line\"\n"
5529                "             \" string literals\",\n"
5530                "             and, other, parameters);");
5531   EXPECT_EQ("fun + \"1243\" /* comment */\n"
5532             "      \"5678\";",
5533             format("fun + \"1243\" /* comment */\n"
5534                    "    \"5678\";",
5535                    getLLVMStyleWithColumns(28)));
5536   EXPECT_EQ(
5537       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
5538       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
5539       "         \"aaaaaaaaaaaaaaaa\";",
5540       format("aaaaaa ="
5541              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
5542              "aaaaaaaaaaaaaaaaaaaaa\" "
5543              "\"aaaaaaaaaaaaaaaa\";"));
5544   verifyFormat("a = a + \"a\"\n"
5545                "        \"a\"\n"
5546                "        \"a\";");
5547   verifyFormat("f(\"a\", \"b\"\n"
5548                "       \"c\");");
5549 
5550   verifyFormat(
5551       "#define LL_FORMAT \"ll\"\n"
5552       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
5553       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
5554 
5555   verifyFormat("#define A(X)          \\\n"
5556                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
5557                "  \"ccccc\"",
5558                getLLVMStyleWithColumns(23));
5559   verifyFormat("#define A \"def\"\n"
5560                "f(\"abc\" A \"ghi\"\n"
5561                "  \"jkl\");");
5562 
5563   verifyFormat("f(L\"a\"\n"
5564                "  L\"b\");");
5565   verifyFormat("#define A(X)            \\\n"
5566                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
5567                "  L\"ccccc\"",
5568                getLLVMStyleWithColumns(25));
5569 
5570   verifyFormat("f(@\"a\"\n"
5571                "  @\"b\");");
5572   verifyFormat("NSString s = @\"a\"\n"
5573                "             @\"b\"\n"
5574                "             @\"c\";");
5575   verifyFormat("NSString s = @\"a\"\n"
5576                "              \"b\"\n"
5577                "              \"c\";");
5578 }
5579 
5580 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
5581   FormatStyle Style = getLLVMStyle();
5582   // No declarations or definitions should be moved to own line.
5583   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
5584   verifyFormat("class A {\n"
5585                "  int f() { return 1; }\n"
5586                "  int g();\n"
5587                "};\n"
5588                "int f() { return 1; }\n"
5589                "int g();\n",
5590                Style);
5591 
5592   // All declarations and definitions should have the return type moved to its
5593   // own
5594   // line.
5595   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
5596   verifyFormat("class E {\n"
5597                "  int\n"
5598                "  f() {\n"
5599                "    return 1;\n"
5600                "  }\n"
5601                "  int\n"
5602                "  g();\n"
5603                "};\n"
5604                "int\n"
5605                "f() {\n"
5606                "  return 1;\n"
5607                "}\n"
5608                "int\n"
5609                "g();\n",
5610                Style);
5611 
5612   // Top-level definitions, and no kinds of declarations should have the
5613   // return type moved to its own line.
5614   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
5615   verifyFormat("class B {\n"
5616                "  int f() { return 1; }\n"
5617                "  int g();\n"
5618                "};\n"
5619                "int\n"
5620                "f() {\n"
5621                "  return 1;\n"
5622                "}\n"
5623                "int g();\n",
5624                Style);
5625 
5626   // Top-level definitions and declarations should have the return type moved
5627   // to its own line.
5628   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
5629   verifyFormat("class C {\n"
5630                "  int f() { return 1; }\n"
5631                "  int g();\n"
5632                "};\n"
5633                "int\n"
5634                "f() {\n"
5635                "  return 1;\n"
5636                "}\n"
5637                "int\n"
5638                "g();\n",
5639                Style);
5640 
5641   // All definitions should have the return type moved to its own line, but no
5642   // kinds of declarations.
5643   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
5644   verifyFormat("class D {\n"
5645                "  int\n"
5646                "  f() {\n"
5647                "    return 1;\n"
5648                "  }\n"
5649                "  int g();\n"
5650                "};\n"
5651                "int\n"
5652                "f() {\n"
5653                "  return 1;\n"
5654                "}\n"
5655                "int g();\n",
5656                Style);
5657   verifyFormat("const char *\n"
5658                "f(void) {\n" // Break here.
5659                "  return \"\";\n"
5660                "}\n"
5661                "const char *bar(void);\n", // No break here.
5662                Style);
5663   verifyFormat("template <class T>\n"
5664                "T *\n"
5665                "f(T &c) {\n" // Break here.
5666                "  return NULL;\n"
5667                "}\n"
5668                "template <class T> T *f(T &c);\n", // No break here.
5669                Style);
5670   verifyFormat("class C {\n"
5671                "  int\n"
5672                "  operator+() {\n"
5673                "    return 1;\n"
5674                "  }\n"
5675                "  int\n"
5676                "  operator()() {\n"
5677                "    return 1;\n"
5678                "  }\n"
5679                "};\n",
5680                Style);
5681   verifyFormat("void\n"
5682                "A::operator()() {}\n"
5683                "void\n"
5684                "A::operator>>() {}\n"
5685                "void\n"
5686                "A::operator+() {}\n",
5687                Style);
5688   verifyFormat("void *operator new(std::size_t s);", // No break here.
5689                Style);
5690   verifyFormat("void *\n"
5691                "operator new(std::size_t s) {}",
5692                Style);
5693   verifyFormat("void *\n"
5694                "operator delete[](void *ptr) {}",
5695                Style);
5696   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
5697   verifyFormat("const char *\n"
5698                "f(void)\n" // Break here.
5699                "{\n"
5700                "  return \"\";\n"
5701                "}\n"
5702                "const char *bar(void);\n", // No break here.
5703                Style);
5704   verifyFormat("template <class T>\n"
5705                "T *\n"     // Problem here: no line break
5706                "f(T &c)\n" // Break here.
5707                "{\n"
5708                "  return NULL;\n"
5709                "}\n"
5710                "template <class T> T *f(T &c);\n", // No break here.
5711                Style);
5712 }
5713 
5714 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
5715   FormatStyle NoBreak = getLLVMStyle();
5716   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
5717   FormatStyle Break = getLLVMStyle();
5718   Break.AlwaysBreakBeforeMultilineStrings = true;
5719   verifyFormat("aaaa = \"bbbb\"\n"
5720                "       \"cccc\";",
5721                NoBreak);
5722   verifyFormat("aaaa =\n"
5723                "    \"bbbb\"\n"
5724                "    \"cccc\";",
5725                Break);
5726   verifyFormat("aaaa(\"bbbb\"\n"
5727                "     \"cccc\");",
5728                NoBreak);
5729   verifyFormat("aaaa(\n"
5730                "    \"bbbb\"\n"
5731                "    \"cccc\");",
5732                Break);
5733   verifyFormat("aaaa(qqq, \"bbbb\"\n"
5734                "          \"cccc\");",
5735                NoBreak);
5736   verifyFormat("aaaa(qqq,\n"
5737                "     \"bbbb\"\n"
5738                "     \"cccc\");",
5739                Break);
5740   verifyFormat("aaaa(qqq,\n"
5741                "     L\"bbbb\"\n"
5742                "     L\"cccc\");",
5743                Break);
5744   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
5745                "                      \"bbbb\"));",
5746                Break);
5747   verifyFormat("string s = someFunction(\n"
5748                "    \"abc\"\n"
5749                "    \"abc\");",
5750                Break);
5751 
5752   // As we break before unary operators, breaking right after them is bad.
5753   verifyFormat("string foo = abc ? \"x\"\n"
5754                "                   \"blah blah blah blah blah blah\"\n"
5755                "                 : \"y\";",
5756                Break);
5757 
5758   // Don't break if there is no column gain.
5759   verifyFormat("f(\"aaaa\"\n"
5760                "  \"bbbb\");",
5761                Break);
5762 
5763   // Treat literals with escaped newlines like multi-line string literals.
5764   EXPECT_EQ("x = \"a\\\n"
5765             "b\\\n"
5766             "c\";",
5767             format("x = \"a\\\n"
5768                    "b\\\n"
5769                    "c\";",
5770                    NoBreak));
5771   EXPECT_EQ("xxxx =\n"
5772             "    \"a\\\n"
5773             "b\\\n"
5774             "c\";",
5775             format("xxxx = \"a\\\n"
5776                    "b\\\n"
5777                    "c\";",
5778                    Break));
5779 
5780   EXPECT_EQ("NSString *const kString =\n"
5781             "    @\"aaaa\"\n"
5782             "    @\"bbbb\";",
5783             format("NSString *const kString = @\"aaaa\"\n"
5784                    "@\"bbbb\";",
5785                    Break));
5786 
5787   Break.ColumnLimit = 0;
5788   verifyFormat("const char *hello = \"hello llvm\";", Break);
5789 }
5790 
5791 TEST_F(FormatTest, AlignsPipes) {
5792   verifyFormat(
5793       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5794       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5795       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5796   verifyFormat(
5797       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
5798       "                     << aaaaaaaaaaaaaaaaaaaa;");
5799   verifyFormat(
5800       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5801       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5802   verifyFormat(
5803       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5804       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5805   verifyFormat(
5806       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
5807       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
5808       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
5809   verifyFormat(
5810       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5811       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5812       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5813   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5814                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5815                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5816                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5817   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
5818                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
5819   verifyFormat(
5820       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5821       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5822   verifyFormat(
5823       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
5824       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
5825 
5826   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
5827                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
5828   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5829                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5830                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
5831                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
5832   verifyFormat("LOG_IF(aaa == //\n"
5833                "       bbb)\n"
5834                "    << a << b;");
5835 
5836   // But sometimes, breaking before the first "<<" is desirable.
5837   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5838                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
5839   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
5840                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5841                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5842   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
5843                "    << BEF << IsTemplate << Description << E->getType();");
5844   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5845                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5846                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5847   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5848                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5849                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5850                "    << aaa;");
5851 
5852   verifyFormat(
5853       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5854       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5855 
5856   // Incomplete string literal.
5857   EXPECT_EQ("llvm::errs() << \"\n"
5858             "             << a;",
5859             format("llvm::errs() << \"\n<<a;"));
5860 
5861   verifyFormat("void f() {\n"
5862                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
5863                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
5864                "}");
5865 
5866   // Handle 'endl'.
5867   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
5868                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5869   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5870 
5871   // Handle '\n'.
5872   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
5873                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5874   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
5875                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
5876   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
5877                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
5878   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5879 }
5880 
5881 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
5882   verifyFormat("return out << \"somepacket = {\\n\"\n"
5883                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
5884                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
5885                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
5886                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
5887                "           << \"}\";");
5888 
5889   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5890                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5891                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
5892   verifyFormat(
5893       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
5894       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
5895       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
5896       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
5897       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
5898   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
5899                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5900   verifyFormat(
5901       "void f() {\n"
5902       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
5903       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
5904       "}");
5905 
5906   // Breaking before the first "<<" is generally not desirable.
5907   verifyFormat(
5908       "llvm::errs()\n"
5909       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5910       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5911       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5912       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5913       getLLVMStyleWithColumns(70));
5914   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5915                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5916                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5917                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5918                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5919                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5920                getLLVMStyleWithColumns(70));
5921 
5922   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
5923                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
5924                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
5925   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
5926                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
5927                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
5928   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
5929                "           (aaaa + aaaa);",
5930                getLLVMStyleWithColumns(40));
5931   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
5932                "                  (aaaaaaa + aaaaa));",
5933                getLLVMStyleWithColumns(40));
5934   verifyFormat(
5935       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
5936       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
5937       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
5938 }
5939 
5940 TEST_F(FormatTest, UnderstandsEquals) {
5941   verifyFormat(
5942       "aaaaaaaaaaaaaaaaa =\n"
5943       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5944   verifyFormat(
5945       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5946       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5947   verifyFormat(
5948       "if (a) {\n"
5949       "  f();\n"
5950       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5951       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5952       "}");
5953 
5954   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5955                "        100000000 + 10000000) {\n}");
5956 }
5957 
5958 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
5959   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5960                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
5961 
5962   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5963                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
5964 
5965   verifyFormat(
5966       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
5967       "                                                          Parameter2);");
5968 
5969   verifyFormat(
5970       "ShortObject->shortFunction(\n"
5971       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
5972       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
5973 
5974   verifyFormat("loooooooooooooongFunction(\n"
5975                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
5976 
5977   verifyFormat(
5978       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
5979       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
5980 
5981   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5982                "    .WillRepeatedly(Return(SomeValue));");
5983   verifyFormat("void f() {\n"
5984                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5985                "      .Times(2)\n"
5986                "      .WillRepeatedly(Return(SomeValue));\n"
5987                "}");
5988   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
5989                "    ccccccccccccccccccccccc);");
5990   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5991                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5992                "          .aaaaa(aaaaa),\n"
5993                "      aaaaaaaaaaaaaaaaaaaaa);");
5994   verifyFormat("void f() {\n"
5995                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5996                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
5997                "}");
5998   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5999                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6000                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6001                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6002                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6003   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6004                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6005                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6006                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
6007                "}");
6008 
6009   // Here, it is not necessary to wrap at "." or "->".
6010   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
6011                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
6012   verifyFormat(
6013       "aaaaaaaaaaa->aaaaaaaaa(\n"
6014       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6015       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
6016 
6017   verifyFormat(
6018       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6019       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
6020   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
6021                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
6022   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
6023                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
6024 
6025   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6026                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6027                "    .a();");
6028 
6029   FormatStyle NoBinPacking = getLLVMStyle();
6030   NoBinPacking.BinPackParameters = false;
6031   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
6032                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
6033                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
6034                "                         aaaaaaaaaaaaaaaaaaa,\n"
6035                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6036                NoBinPacking);
6037 
6038   // If there is a subsequent call, change to hanging indentation.
6039   verifyFormat(
6040       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6041       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
6042       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
6043   verifyFormat(
6044       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6045       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
6046   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6047                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6048                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
6049   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6050                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6051                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
6052 }
6053 
6054 TEST_F(FormatTest, WrapsTemplateDeclarations) {
6055   verifyFormat("template <typename T>\n"
6056                "virtual void loooooooooooongFunction(int Param1, int Param2);");
6057   verifyFormat("template <typename T>\n"
6058                "// T should be one of {A, B}.\n"
6059                "virtual void loooooooooooongFunction(int Param1, int Param2);");
6060   verifyFormat(
6061       "template <typename T>\n"
6062       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
6063   verifyFormat("template <typename T>\n"
6064                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
6065                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
6066   verifyFormat(
6067       "template <typename T>\n"
6068       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
6069       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
6070   verifyFormat(
6071       "template <typename T>\n"
6072       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
6073       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
6074       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6075   verifyFormat("template <typename T>\n"
6076                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6077                "    int aaaaaaaaaaaaaaaaaaaaaa);");
6078   verifyFormat(
6079       "template <typename T1, typename T2 = char, typename T3 = char,\n"
6080       "          typename T4 = char>\n"
6081       "void f();");
6082   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
6083                "          template <typename> class cccccccccccccccccccccc,\n"
6084                "          typename ddddddddddddd>\n"
6085                "class C {};");
6086   verifyFormat(
6087       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
6088       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6089 
6090   verifyFormat("void f() {\n"
6091                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
6092                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
6093                "}");
6094 
6095   verifyFormat("template <typename T> class C {};");
6096   verifyFormat("template <typename T> void f();");
6097   verifyFormat("template <typename T> void f() {}");
6098   verifyFormat(
6099       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
6100       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6101       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
6102       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
6103       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6104       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
6105       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
6106       getLLVMStyleWithColumns(72));
6107   EXPECT_EQ("static_cast<A< //\n"
6108             "    B> *>(\n"
6109             "\n"
6110             ");",
6111             format("static_cast<A<//\n"
6112                    "    B>*>(\n"
6113                    "\n"
6114                    "    );"));
6115   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6116                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
6117 
6118   FormatStyle AlwaysBreak = getLLVMStyle();
6119   AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
6120   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
6121   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
6122   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
6123   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6124                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
6125                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
6126   verifyFormat("template <template <typename> class Fooooooo,\n"
6127                "          template <typename> class Baaaaaaar>\n"
6128                "struct C {};",
6129                AlwaysBreak);
6130   verifyFormat("template <typename T> // T can be A, B or C.\n"
6131                "struct C {};",
6132                AlwaysBreak);
6133   verifyFormat("template <enum E> class A {\n"
6134                "public:\n"
6135                "  E *f();\n"
6136                "};");
6137 
6138   FormatStyle NeverBreak = getLLVMStyle();
6139   NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No;
6140   verifyFormat("template <typename T> class C {};", NeverBreak);
6141   verifyFormat("template <typename T> void f();", NeverBreak);
6142   verifyFormat("template <typename T> void f() {}", NeverBreak);
6143   verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb) {}",
6144                NeverBreak);
6145   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6146                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
6147                "    ccccccccccccccccccccccccccccccccccccccccccccccc);",
6148                NeverBreak);
6149   verifyFormat("template <template <typename> class Fooooooo,\n"
6150                "          template <typename> class Baaaaaaar>\n"
6151                "struct C {};",
6152                NeverBreak);
6153   verifyFormat("template <typename T> // T can be A, B or C.\n"
6154                "struct C {};",
6155                NeverBreak);
6156   verifyFormat("template <enum E> class A {\n"
6157                "public:\n"
6158                "  E *f();\n"
6159                "};", NeverBreak);
6160   NeverBreak.PenaltyBreakTemplateDeclaration = 100;
6161   verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb) {}",
6162                NeverBreak);
6163 }
6164 
6165 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
6166   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
6167   Style.ColumnLimit = 60;
6168   EXPECT_EQ("// Baseline - no comments.\n"
6169             "template <\n"
6170             "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
6171             "void f() {}",
6172             format("// Baseline - no comments.\n"
6173                    "template <\n"
6174                    "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
6175                    "void f() {}",
6176                    Style));
6177 
6178   EXPECT_EQ("template <\n"
6179             "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
6180             "void f() {}",
6181             format("template <\n"
6182                    "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
6183                    "void f() {}",
6184                    Style));
6185 
6186   EXPECT_EQ(
6187       "template <\n"
6188       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
6189       "void f() {}",
6190       format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  /* line */\n"
6191              "void f() {}",
6192              Style));
6193 
6194   EXPECT_EQ(
6195       "template <\n"
6196       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
6197       "                                               // multiline\n"
6198       "void f() {}",
6199       format("template <\n"
6200              "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
6201              "                                              // multiline\n"
6202              "void f() {}",
6203              Style));
6204 
6205   EXPECT_EQ(
6206       "template <typename aaaaaaaaaa<\n"
6207       "    bbbbbbbbbbbb>::value>  // trailing loooong\n"
6208       "void f() {}",
6209       format(
6210           "template <\n"
6211           "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
6212           "void f() {}",
6213           Style));
6214 }
6215 
6216 TEST_F(FormatTest, WrapsTemplateParameters) {
6217   FormatStyle Style = getLLVMStyle();
6218   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6219   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
6220   verifyFormat(
6221       "template <typename... a> struct q {};\n"
6222       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
6223       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
6224       "    y;",
6225       Style);
6226   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6227   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
6228   verifyFormat(
6229       "template <typename... a> struct r {};\n"
6230       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
6231       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
6232       "    y;",
6233       Style);
6234   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6235   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
6236   verifyFormat(
6237       "template <typename... a> struct s {};\n"
6238       "extern s<\n"
6239       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6240       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
6241       "    y;",
6242       Style);
6243   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6244   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
6245   verifyFormat(
6246       "template <typename... a> struct t {};\n"
6247       "extern t<\n"
6248       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
6249       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
6250       "    y;",
6251       Style);
6252 }
6253 
6254 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
6255   verifyFormat(
6256       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
6257       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
6258   verifyFormat(
6259       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
6260       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6261       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
6262 
6263   // FIXME: Should we have the extra indent after the second break?
6264   verifyFormat(
6265       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
6266       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
6267       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
6268 
6269   verifyFormat(
6270       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
6271       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
6272 
6273   // Breaking at nested name specifiers is generally not desirable.
6274   verifyFormat(
6275       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6276       "    aaaaaaaaaaaaaaaaaaaaaaa);");
6277 
6278   verifyFormat(
6279       "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
6280       "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
6281       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6282       "                   aaaaaaaaaaaaaaaaaaaaa);",
6283       getLLVMStyleWithColumns(74));
6284 
6285   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
6286                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6287                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
6288 }
6289 
6290 TEST_F(FormatTest, UnderstandsTemplateParameters) {
6291   verifyFormat("A<int> a;");
6292   verifyFormat("A<A<A<int>>> a;");
6293   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
6294   verifyFormat("bool x = a < 1 || 2 > a;");
6295   verifyFormat("bool x = 5 < f<int>();");
6296   verifyFormat("bool x = f<int>() > 5;");
6297   verifyFormat("bool x = 5 < a<int>::x;");
6298   verifyFormat("bool x = a < 4 ? a > 2 : false;");
6299   verifyFormat("bool x = f() ? a < 2 : a > 2;");
6300 
6301   verifyGoogleFormat("A<A<int>> a;");
6302   verifyGoogleFormat("A<A<A<int>>> a;");
6303   verifyGoogleFormat("A<A<A<A<int>>>> a;");
6304   verifyGoogleFormat("A<A<int> > a;");
6305   verifyGoogleFormat("A<A<A<int> > > a;");
6306   verifyGoogleFormat("A<A<A<A<int> > > > a;");
6307   verifyGoogleFormat("A<::A<int>> a;");
6308   verifyGoogleFormat("A<::A> a;");
6309   verifyGoogleFormat("A< ::A> a;");
6310   verifyGoogleFormat("A< ::A<int> > a;");
6311   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
6312   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
6313   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
6314   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
6315   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
6316             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
6317 
6318   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
6319 
6320   verifyFormat("test >> a >> b;");
6321   verifyFormat("test << a >> b;");
6322 
6323   verifyFormat("f<int>();");
6324   verifyFormat("template <typename T> void f() {}");
6325   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
6326   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
6327                "sizeof(char)>::type>;");
6328   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
6329   verifyFormat("f(a.operator()<A>());");
6330   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6331                "      .template operator()<A>());",
6332                getLLVMStyleWithColumns(35));
6333 
6334   // Not template parameters.
6335   verifyFormat("return a < b && c > d;");
6336   verifyFormat("void f() {\n"
6337                "  while (a < b && c > d) {\n"
6338                "  }\n"
6339                "}");
6340   verifyFormat("template <typename... Types>\n"
6341                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
6342 
6343   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6344                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
6345                getLLVMStyleWithColumns(60));
6346   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
6347   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
6348   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
6349 }
6350 
6351 TEST_F(FormatTest, BitshiftOperatorWidth) {
6352   EXPECT_EQ("int a = 1 << 2; /* foo\n"
6353             "                   bar */",
6354             format("int    a=1<<2;  /* foo\n"
6355                    "                   bar */"));
6356 
6357   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
6358             "                     bar */",
6359             format("int  b  =256>>1 ;  /* foo\n"
6360                    "                      bar */"));
6361 }
6362 
6363 TEST_F(FormatTest, UnderstandsBinaryOperators) {
6364   verifyFormat("COMPARE(a, ==, b);");
6365   verifyFormat("auto s = sizeof...(Ts) - 1;");
6366 }
6367 
6368 TEST_F(FormatTest, UnderstandsPointersToMembers) {
6369   verifyFormat("int A::*x;");
6370   verifyFormat("int (S::*func)(void *);");
6371   verifyFormat("void f() { int (S::*func)(void *); }");
6372   verifyFormat("typedef bool *(Class::*Member)() const;");
6373   verifyFormat("void f() {\n"
6374                "  (a->*f)();\n"
6375                "  a->*x;\n"
6376                "  (a.*f)();\n"
6377                "  ((*a).*f)();\n"
6378                "  a.*x;\n"
6379                "}");
6380   verifyFormat("void f() {\n"
6381                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
6382                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
6383                "}");
6384   verifyFormat(
6385       "(aaaaaaaaaa->*bbbbbbb)(\n"
6386       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6387   FormatStyle Style = getLLVMStyle();
6388   Style.PointerAlignment = FormatStyle::PAS_Left;
6389   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
6390 }
6391 
6392 TEST_F(FormatTest, UnderstandsUnaryOperators) {
6393   verifyFormat("int a = -2;");
6394   verifyFormat("f(-1, -2, -3);");
6395   verifyFormat("a[-1] = 5;");
6396   verifyFormat("int a = 5 + -2;");
6397   verifyFormat("if (i == -1) {\n}");
6398   verifyFormat("if (i != -1) {\n}");
6399   verifyFormat("if (i > -1) {\n}");
6400   verifyFormat("if (i < -1) {\n}");
6401   verifyFormat("++(a->f());");
6402   verifyFormat("--(a->f());");
6403   verifyFormat("(a->f())++;");
6404   verifyFormat("a[42]++;");
6405   verifyFormat("if (!(a->f())) {\n}");
6406   verifyFormat("if (!+i) {\n}");
6407   verifyFormat("~&a;");
6408 
6409   verifyFormat("a-- > b;");
6410   verifyFormat("b ? -a : c;");
6411   verifyFormat("n * sizeof char16;");
6412   verifyFormat("n * alignof char16;", getGoogleStyle());
6413   verifyFormat("sizeof(char);");
6414   verifyFormat("alignof(char);", getGoogleStyle());
6415 
6416   verifyFormat("return -1;");
6417   verifyFormat("switch (a) {\n"
6418                "case -1:\n"
6419                "  break;\n"
6420                "}");
6421   verifyFormat("#define X -1");
6422   verifyFormat("#define X -kConstant");
6423 
6424   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
6425   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
6426 
6427   verifyFormat("int a = /* confusing comment */ -1;");
6428   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
6429   verifyFormat("int a = i /* confusing comment */++;");
6430 }
6431 
6432 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
6433   verifyFormat("if (!aaaaaaaaaa( // break\n"
6434                "        aaaaa)) {\n"
6435                "}");
6436   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
6437                "    aaaaa));");
6438   verifyFormat("*aaa = aaaaaaa( // break\n"
6439                "    bbbbbb);");
6440 }
6441 
6442 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
6443   verifyFormat("bool operator<();");
6444   verifyFormat("bool operator>();");
6445   verifyFormat("bool operator=();");
6446   verifyFormat("bool operator==();");
6447   verifyFormat("bool operator!=();");
6448   verifyFormat("int operator+();");
6449   verifyFormat("int operator++();");
6450   verifyFormat("int operator++(int) volatile noexcept;");
6451   verifyFormat("bool operator,();");
6452   verifyFormat("bool operator();");
6453   verifyFormat("bool operator()();");
6454   verifyFormat("bool operator[]();");
6455   verifyFormat("operator bool();");
6456   verifyFormat("operator int();");
6457   verifyFormat("operator void *();");
6458   verifyFormat("operator SomeType<int>();");
6459   verifyFormat("operator SomeType<int, int>();");
6460   verifyFormat("operator SomeType<SomeType<int>>();");
6461   verifyFormat("void *operator new(std::size_t size);");
6462   verifyFormat("void *operator new[](std::size_t size);");
6463   verifyFormat("void operator delete(void *ptr);");
6464   verifyFormat("void operator delete[](void *ptr);");
6465   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
6466                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
6467   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
6468                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
6469 
6470   verifyFormat(
6471       "ostream &operator<<(ostream &OutputStream,\n"
6472       "                    SomeReallyLongType WithSomeReallyLongValue);");
6473   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
6474                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
6475                "  return left.group < right.group;\n"
6476                "}");
6477   verifyFormat("SomeType &operator=(const SomeType &S);");
6478   verifyFormat("f.template operator()<int>();");
6479 
6480   verifyGoogleFormat("operator void*();");
6481   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
6482   verifyGoogleFormat("operator ::A();");
6483 
6484   verifyFormat("using A::operator+;");
6485   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
6486                "int i;");
6487 }
6488 
6489 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
6490   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
6491   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
6492   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
6493   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
6494   verifyFormat("Deleted &operator=(const Deleted &) &;");
6495   verifyFormat("Deleted &operator=(const Deleted &) &&;");
6496   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
6497   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
6498   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
6499   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
6500   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
6501   verifyFormat("void Fn(T const &) const &;");
6502   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
6503   verifyFormat("template <typename T>\n"
6504                "void F(T) && = delete;",
6505                getGoogleStyle());
6506 
6507   FormatStyle AlignLeft = getLLVMStyle();
6508   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
6509   verifyFormat("void A::b() && {}", AlignLeft);
6510   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
6511   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
6512                AlignLeft);
6513   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
6514   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
6515   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
6516   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
6517   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
6518   verifyFormat("auto Function(T) & -> void;", AlignLeft);
6519   verifyFormat("void Fn(T const&) const&;", AlignLeft);
6520   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
6521 
6522   FormatStyle Spaces = getLLVMStyle();
6523   Spaces.SpacesInCStyleCastParentheses = true;
6524   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
6525   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
6526   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
6527   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
6528 
6529   Spaces.SpacesInCStyleCastParentheses = false;
6530   Spaces.SpacesInParentheses = true;
6531   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
6532   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
6533   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
6534   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
6535 }
6536 
6537 TEST_F(FormatTest, UnderstandsNewAndDelete) {
6538   verifyFormat("void f() {\n"
6539                "  A *a = new A;\n"
6540                "  A *a = new (placement) A;\n"
6541                "  delete a;\n"
6542                "  delete (A *)a;\n"
6543                "}");
6544   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
6545                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
6546   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6547                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
6548                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
6549   verifyFormat("delete[] h->p;");
6550 }
6551 
6552 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
6553   verifyFormat("int *f(int *a) {}");
6554   verifyFormat("int main(int argc, char **argv) {}");
6555   verifyFormat("Test::Test(int b) : a(b * b) {}");
6556   verifyIndependentOfContext("f(a, *a);");
6557   verifyFormat("void g() { f(*a); }");
6558   verifyIndependentOfContext("int a = b * 10;");
6559   verifyIndependentOfContext("int a = 10 * b;");
6560   verifyIndependentOfContext("int a = b * c;");
6561   verifyIndependentOfContext("int a += b * c;");
6562   verifyIndependentOfContext("int a -= b * c;");
6563   verifyIndependentOfContext("int a *= b * c;");
6564   verifyIndependentOfContext("int a /= b * c;");
6565   verifyIndependentOfContext("int a = *b;");
6566   verifyIndependentOfContext("int a = *b * c;");
6567   verifyIndependentOfContext("int a = b * *c;");
6568   verifyIndependentOfContext("int a = b * (10);");
6569   verifyIndependentOfContext("S << b * (10);");
6570   verifyIndependentOfContext("return 10 * b;");
6571   verifyIndependentOfContext("return *b * *c;");
6572   verifyIndependentOfContext("return a & ~b;");
6573   verifyIndependentOfContext("f(b ? *c : *d);");
6574   verifyIndependentOfContext("int a = b ? *c : *d;");
6575   verifyIndependentOfContext("*b = a;");
6576   verifyIndependentOfContext("a * ~b;");
6577   verifyIndependentOfContext("a * !b;");
6578   verifyIndependentOfContext("a * +b;");
6579   verifyIndependentOfContext("a * -b;");
6580   verifyIndependentOfContext("a * ++b;");
6581   verifyIndependentOfContext("a * --b;");
6582   verifyIndependentOfContext("a[4] * b;");
6583   verifyIndependentOfContext("a[a * a] = 1;");
6584   verifyIndependentOfContext("f() * b;");
6585   verifyIndependentOfContext("a * [self dostuff];");
6586   verifyIndependentOfContext("int x = a * (a + b);");
6587   verifyIndependentOfContext("(a *)(a + b);");
6588   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
6589   verifyIndependentOfContext("int *pa = (int *)&a;");
6590   verifyIndependentOfContext("return sizeof(int **);");
6591   verifyIndependentOfContext("return sizeof(int ******);");
6592   verifyIndependentOfContext("return (int **&)a;");
6593   verifyIndependentOfContext("f((*PointerToArray)[10]);");
6594   verifyFormat("void f(Type (*parameter)[10]) {}");
6595   verifyFormat("void f(Type (&parameter)[10]) {}");
6596   verifyGoogleFormat("return sizeof(int**);");
6597   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
6598   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
6599   verifyFormat("auto a = [](int **&, int ***) {};");
6600   verifyFormat("auto PointerBinding = [](const char *S) {};");
6601   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
6602   verifyFormat("[](const decltype(*a) &value) {}");
6603   verifyFormat("decltype(a * b) F();");
6604   verifyFormat("#define MACRO() [](A *a) { return 1; }");
6605   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
6606   verifyIndependentOfContext("typedef void (*f)(int *a);");
6607   verifyIndependentOfContext("int i{a * b};");
6608   verifyIndependentOfContext("aaa && aaa->f();");
6609   verifyIndependentOfContext("int x = ~*p;");
6610   verifyFormat("Constructor() : a(a), area(width * height) {}");
6611   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
6612   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
6613   verifyFormat("void f() { f(a, c * d); }");
6614   verifyFormat("void f() { f(new a(), c * d); }");
6615   verifyFormat("void f(const MyOverride &override);");
6616   verifyFormat("void f(const MyFinal &final);");
6617   verifyIndependentOfContext("bool a = f() && override.f();");
6618   verifyIndependentOfContext("bool a = f() && final.f();");
6619 
6620   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
6621 
6622   verifyIndependentOfContext("A<int *> a;");
6623   verifyIndependentOfContext("A<int **> a;");
6624   verifyIndependentOfContext("A<int *, int *> a;");
6625   verifyIndependentOfContext("A<int *[]> a;");
6626   verifyIndependentOfContext(
6627       "const char *const p = reinterpret_cast<const char *const>(q);");
6628   verifyIndependentOfContext("A<int **, int **> a;");
6629   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
6630   verifyFormat("for (char **a = b; *a; ++a) {\n}");
6631   verifyFormat("for (; a && b;) {\n}");
6632   verifyFormat("bool foo = true && [] { return false; }();");
6633 
6634   verifyFormat(
6635       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6636       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6637 
6638   verifyGoogleFormat("int const* a = &b;");
6639   verifyGoogleFormat("**outparam = 1;");
6640   verifyGoogleFormat("*outparam = a * b;");
6641   verifyGoogleFormat("int main(int argc, char** argv) {}");
6642   verifyGoogleFormat("A<int*> a;");
6643   verifyGoogleFormat("A<int**> a;");
6644   verifyGoogleFormat("A<int*, int*> a;");
6645   verifyGoogleFormat("A<int**, int**> a;");
6646   verifyGoogleFormat("f(b ? *c : *d);");
6647   verifyGoogleFormat("int a = b ? *c : *d;");
6648   verifyGoogleFormat("Type* t = **x;");
6649   verifyGoogleFormat("Type* t = *++*x;");
6650   verifyGoogleFormat("*++*x;");
6651   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
6652   verifyGoogleFormat("Type* t = x++ * y;");
6653   verifyGoogleFormat(
6654       "const char* const p = reinterpret_cast<const char* const>(q);");
6655   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
6656   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
6657   verifyGoogleFormat("template <typename T>\n"
6658                      "void f(int i = 0, SomeType** temps = NULL);");
6659 
6660   FormatStyle Left = getLLVMStyle();
6661   Left.PointerAlignment = FormatStyle::PAS_Left;
6662   verifyFormat("x = *a(x) = *a(y);", Left);
6663   verifyFormat("for (;; *a = b) {\n}", Left);
6664   verifyFormat("return *this += 1;", Left);
6665   verifyFormat("throw *x;", Left);
6666   verifyFormat("delete *x;", Left);
6667   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
6668   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
6669   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
6670 
6671   verifyIndependentOfContext("a = *(x + y);");
6672   verifyIndependentOfContext("a = &(x + y);");
6673   verifyIndependentOfContext("*(x + y).call();");
6674   verifyIndependentOfContext("&(x + y)->call();");
6675   verifyFormat("void f() { &(*I).first; }");
6676 
6677   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
6678   verifyFormat(
6679       "int *MyValues = {\n"
6680       "    *A, // Operator detection might be confused by the '{'\n"
6681       "    *BB // Operator detection might be confused by previous comment\n"
6682       "};");
6683 
6684   verifyIndependentOfContext("if (int *a = &b)");
6685   verifyIndependentOfContext("if (int &a = *b)");
6686   verifyIndependentOfContext("if (a & b[i])");
6687   verifyIndependentOfContext("if (a::b::c::d & b[i])");
6688   verifyIndependentOfContext("if (*b[i])");
6689   verifyIndependentOfContext("if (int *a = (&b))");
6690   verifyIndependentOfContext("while (int *a = &b)");
6691   verifyIndependentOfContext("size = sizeof *a;");
6692   verifyIndependentOfContext("if (a && (b = c))");
6693   verifyFormat("void f() {\n"
6694                "  for (const int &v : Values) {\n"
6695                "  }\n"
6696                "}");
6697   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
6698   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
6699   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
6700 
6701   verifyFormat("#define A (!a * b)");
6702   verifyFormat("#define MACRO     \\\n"
6703                "  int *i = a * b; \\\n"
6704                "  void f(a *b);",
6705                getLLVMStyleWithColumns(19));
6706 
6707   verifyIndependentOfContext("A = new SomeType *[Length];");
6708   verifyIndependentOfContext("A = new SomeType *[Length]();");
6709   verifyIndependentOfContext("T **t = new T *;");
6710   verifyIndependentOfContext("T **t = new T *();");
6711   verifyGoogleFormat("A = new SomeType*[Length]();");
6712   verifyGoogleFormat("A = new SomeType*[Length];");
6713   verifyGoogleFormat("T** t = new T*;");
6714   verifyGoogleFormat("T** t = new T*();");
6715 
6716   verifyFormat("STATIC_ASSERT((a & b) == 0);");
6717   verifyFormat("STATIC_ASSERT(0 == (a & b));");
6718   verifyFormat("template <bool a, bool b> "
6719                "typename t::if<x && y>::type f() {}");
6720   verifyFormat("template <int *y> f() {}");
6721   verifyFormat("vector<int *> v;");
6722   verifyFormat("vector<int *const> v;");
6723   verifyFormat("vector<int *const **const *> v;");
6724   verifyFormat("vector<int *volatile> v;");
6725   verifyFormat("vector<a * b> v;");
6726   verifyFormat("foo<b && false>();");
6727   verifyFormat("foo<b & 1>();");
6728   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
6729   verifyFormat(
6730       "template <class T, class = typename std::enable_if<\n"
6731       "                       std::is_integral<T>::value &&\n"
6732       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
6733       "void F();",
6734       getLLVMStyleWithColumns(70));
6735   verifyFormat(
6736       "template <class T,\n"
6737       "          class = typename std::enable_if<\n"
6738       "              std::is_integral<T>::value &&\n"
6739       "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
6740       "          class U>\n"
6741       "void F();",
6742       getLLVMStyleWithColumns(70));
6743   verifyFormat(
6744       "template <class T,\n"
6745       "          class = typename ::std::enable_if<\n"
6746       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
6747       "void F();",
6748       getGoogleStyleWithColumns(68));
6749 
6750   verifyIndependentOfContext("MACRO(int *i);");
6751   verifyIndependentOfContext("MACRO(auto *a);");
6752   verifyIndependentOfContext("MACRO(const A *a);");
6753   verifyIndependentOfContext("MACRO(A *const a);");
6754   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
6755   verifyFormat("void f() { f(float{1}, a * a); }");
6756   // FIXME: Is there a way to make this work?
6757   // verifyIndependentOfContext("MACRO(A *a);");
6758 
6759   verifyFormat("DatumHandle const *operator->() const { return input_; }");
6760   verifyFormat("return options != nullptr && operator==(*options);");
6761 
6762   EXPECT_EQ("#define OP(x)                                    \\\n"
6763             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
6764             "    return s << a.DebugString();                 \\\n"
6765             "  }",
6766             format("#define OP(x) \\\n"
6767                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
6768                    "    return s << a.DebugString(); \\\n"
6769                    "  }",
6770                    getLLVMStyleWithColumns(50)));
6771 
6772   // FIXME: We cannot handle this case yet; we might be able to figure out that
6773   // foo<x> d > v; doesn't make sense.
6774   verifyFormat("foo<a<b && c> d> v;");
6775 
6776   FormatStyle PointerMiddle = getLLVMStyle();
6777   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
6778   verifyFormat("delete *x;", PointerMiddle);
6779   verifyFormat("int * x;", PointerMiddle);
6780   verifyFormat("int *[] x;", PointerMiddle);
6781   verifyFormat("template <int * y> f() {}", PointerMiddle);
6782   verifyFormat("int * f(int * a) {}", PointerMiddle);
6783   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
6784   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
6785   verifyFormat("A<int *> a;", PointerMiddle);
6786   verifyFormat("A<int **> a;", PointerMiddle);
6787   verifyFormat("A<int *, int *> a;", PointerMiddle);
6788   verifyFormat("A<int *[]> a;", PointerMiddle);
6789   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
6790   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
6791   verifyFormat("T ** t = new T *;", PointerMiddle);
6792 
6793   // Member function reference qualifiers aren't binary operators.
6794   verifyFormat("string // break\n"
6795                "operator()() & {}");
6796   verifyFormat("string // break\n"
6797                "operator()() && {}");
6798   verifyGoogleFormat("template <typename T>\n"
6799                      "auto x() & -> int {}");
6800 }
6801 
6802 TEST_F(FormatTest, UnderstandsAttributes) {
6803   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
6804   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
6805                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
6806   FormatStyle AfterType = getLLVMStyle();
6807   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
6808   verifyFormat("__attribute__((nodebug)) void\n"
6809                "foo() {}\n",
6810                AfterType);
6811 }
6812 
6813 TEST_F(FormatTest, UnderstandsSquareAttributes) {
6814   verifyFormat("SomeType s [[unused]] (InitValue);");
6815   verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
6816   verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
6817   verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
6818   verifyFormat("void f() [[deprecated(\"so sorry\")]];");
6819   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6820                "    [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
6821 
6822   // Make sure we do not mistake attributes for array subscripts.
6823   verifyFormat("int a() {}\n"
6824                "[[unused]] int b() {}\n");
6825   verifyFormat("NSArray *arr;\n"
6826                "arr[[Foo() bar]];");
6827 
6828   // On the other hand, we still need to correctly find array subscripts.
6829   verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
6830 
6831   // Make sure we do not parse attributes as lambda introducers.
6832   FormatStyle MultiLineFunctions = getLLVMStyle();
6833   MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6834   verifyFormat("[[unused]] int b() {\n"
6835                "  return 42;\n"
6836                "}\n",
6837                MultiLineFunctions);
6838 }
6839 
6840 TEST_F(FormatTest, UnderstandsEllipsis) {
6841   verifyFormat("int printf(const char *fmt, ...);");
6842   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
6843   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
6844 
6845   FormatStyle PointersLeft = getLLVMStyle();
6846   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
6847   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
6848 }
6849 
6850 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
6851   EXPECT_EQ("int *a;\n"
6852             "int *a;\n"
6853             "int *a;",
6854             format("int *a;\n"
6855                    "int* a;\n"
6856                    "int *a;",
6857                    getGoogleStyle()));
6858   EXPECT_EQ("int* a;\n"
6859             "int* a;\n"
6860             "int* a;",
6861             format("int* a;\n"
6862                    "int* a;\n"
6863                    "int *a;",
6864                    getGoogleStyle()));
6865   EXPECT_EQ("int *a;\n"
6866             "int *a;\n"
6867             "int *a;",
6868             format("int *a;\n"
6869                    "int * a;\n"
6870                    "int *  a;",
6871                    getGoogleStyle()));
6872   EXPECT_EQ("auto x = [] {\n"
6873             "  int *a;\n"
6874             "  int *a;\n"
6875             "  int *a;\n"
6876             "};",
6877             format("auto x=[]{int *a;\n"
6878                    "int * a;\n"
6879                    "int *  a;};",
6880                    getGoogleStyle()));
6881 }
6882 
6883 TEST_F(FormatTest, UnderstandsRvalueReferences) {
6884   verifyFormat("int f(int &&a) {}");
6885   verifyFormat("int f(int a, char &&b) {}");
6886   verifyFormat("void f() { int &&a = b; }");
6887   verifyGoogleFormat("int f(int a, char&& b) {}");
6888   verifyGoogleFormat("void f() { int&& a = b; }");
6889 
6890   verifyIndependentOfContext("A<int &&> a;");
6891   verifyIndependentOfContext("A<int &&, int &&> a;");
6892   verifyGoogleFormat("A<int&&> a;");
6893   verifyGoogleFormat("A<int&&, int&&> a;");
6894 
6895   // Not rvalue references:
6896   verifyFormat("template <bool B, bool C> class A {\n"
6897                "  static_assert(B && C, \"Something is wrong\");\n"
6898                "};");
6899   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
6900   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
6901   verifyFormat("#define A(a, b) (a && b)");
6902 }
6903 
6904 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
6905   verifyFormat("void f() {\n"
6906                "  x[aaaaaaaaa -\n"
6907                "    b] = 23;\n"
6908                "}",
6909                getLLVMStyleWithColumns(15));
6910 }
6911 
6912 TEST_F(FormatTest, FormatsCasts) {
6913   verifyFormat("Type *A = static_cast<Type *>(P);");
6914   verifyFormat("Type *A = (Type *)P;");
6915   verifyFormat("Type *A = (vector<Type *, int *>)P;");
6916   verifyFormat("int a = (int)(2.0f);");
6917   verifyFormat("int a = (int)2.0f;");
6918   verifyFormat("x[(int32)y];");
6919   verifyFormat("x = (int32)y;");
6920   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
6921   verifyFormat("int a = (int)*b;");
6922   verifyFormat("int a = (int)2.0f;");
6923   verifyFormat("int a = (int)~0;");
6924   verifyFormat("int a = (int)++a;");
6925   verifyFormat("int a = (int)sizeof(int);");
6926   verifyFormat("int a = (int)+2;");
6927   verifyFormat("my_int a = (my_int)2.0f;");
6928   verifyFormat("my_int a = (my_int)sizeof(int);");
6929   verifyFormat("return (my_int)aaa;");
6930   verifyFormat("#define x ((int)-1)");
6931   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
6932   verifyFormat("#define p(q) ((int *)&q)");
6933   verifyFormat("fn(a)(b) + 1;");
6934 
6935   verifyFormat("void f() { my_int a = (my_int)*b; }");
6936   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
6937   verifyFormat("my_int a = (my_int)~0;");
6938   verifyFormat("my_int a = (my_int)++a;");
6939   verifyFormat("my_int a = (my_int)-2;");
6940   verifyFormat("my_int a = (my_int)1;");
6941   verifyFormat("my_int a = (my_int *)1;");
6942   verifyFormat("my_int a = (const my_int)-1;");
6943   verifyFormat("my_int a = (const my_int *)-1;");
6944   verifyFormat("my_int a = (my_int)(my_int)-1;");
6945   verifyFormat("my_int a = (ns::my_int)-2;");
6946   verifyFormat("case (my_int)ONE:");
6947   verifyFormat("auto x = (X)this;");
6948 
6949   // FIXME: single value wrapped with paren will be treated as cast.
6950   verifyFormat("void f(int i = (kValue)*kMask) {}");
6951 
6952   verifyFormat("{ (void)F; }");
6953 
6954   // Don't break after a cast's
6955   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6956                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
6957                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
6958 
6959   // These are not casts.
6960   verifyFormat("void f(int *) {}");
6961   verifyFormat("f(foo)->b;");
6962   verifyFormat("f(foo).b;");
6963   verifyFormat("f(foo)(b);");
6964   verifyFormat("f(foo)[b];");
6965   verifyFormat("[](foo) { return 4; }(bar);");
6966   verifyFormat("(*funptr)(foo)[4];");
6967   verifyFormat("funptrs[4](foo)[4];");
6968   verifyFormat("void f(int *);");
6969   verifyFormat("void f(int *) = 0;");
6970   verifyFormat("void f(SmallVector<int>) {}");
6971   verifyFormat("void f(SmallVector<int>);");
6972   verifyFormat("void f(SmallVector<int>) = 0;");
6973   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
6974   verifyFormat("int a = sizeof(int) * b;");
6975   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
6976   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
6977   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
6978   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
6979 
6980   // These are not casts, but at some point were confused with casts.
6981   verifyFormat("virtual void foo(int *) override;");
6982   verifyFormat("virtual void foo(char &) const;");
6983   verifyFormat("virtual void foo(int *a, char *) const;");
6984   verifyFormat("int a = sizeof(int *) + b;");
6985   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
6986   verifyFormat("bool b = f(g<int>) && c;");
6987   verifyFormat("typedef void (*f)(int i) func;");
6988 
6989   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
6990                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
6991   // FIXME: The indentation here is not ideal.
6992   verifyFormat(
6993       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6994       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
6995       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
6996 }
6997 
6998 TEST_F(FormatTest, FormatsFunctionTypes) {
6999   verifyFormat("A<bool()> a;");
7000   verifyFormat("A<SomeType()> a;");
7001   verifyFormat("A<void (*)(int, std::string)> a;");
7002   verifyFormat("A<void *(int)>;");
7003   verifyFormat("void *(*a)(int *, SomeType *);");
7004   verifyFormat("int (*func)(void *);");
7005   verifyFormat("void f() { int (*func)(void *); }");
7006   verifyFormat("template <class CallbackClass>\n"
7007                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
7008 
7009   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
7010   verifyGoogleFormat("void* (*a)(int);");
7011   verifyGoogleFormat(
7012       "template <class CallbackClass>\n"
7013       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
7014 
7015   // Other constructs can look somewhat like function types:
7016   verifyFormat("A<sizeof(*x)> a;");
7017   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
7018   verifyFormat("some_var = function(*some_pointer_var)[0];");
7019   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
7020   verifyFormat("int x = f(&h)();");
7021   verifyFormat("returnsFunction(&param1, &param2)(param);");
7022   verifyFormat("std::function<\n"
7023                "    LooooooooooongTemplatedType<\n"
7024                "        SomeType>*(\n"
7025                "        LooooooooooooooooongType type)>\n"
7026                "    function;",
7027                getGoogleStyleWithColumns(40));
7028 }
7029 
7030 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
7031   verifyFormat("A (*foo_)[6];");
7032   verifyFormat("vector<int> (*foo_)[6];");
7033 }
7034 
7035 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
7036   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
7037                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
7038   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
7039                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
7040   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
7041                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
7042 
7043   // Different ways of ()-initializiation.
7044   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
7045                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
7046   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
7047                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
7048   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
7049                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
7050   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
7051                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
7052 
7053   // Lambdas should not confuse the variable declaration heuristic.
7054   verifyFormat("LooooooooooooooooongType\n"
7055                "    variable(nullptr, [](A *a) {});",
7056                getLLVMStyleWithColumns(40));
7057 }
7058 
7059 TEST_F(FormatTest, BreaksLongDeclarations) {
7060   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
7061                "    AnotherNameForTheLongType;");
7062   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
7063                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7064   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
7065                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
7066   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
7067                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
7068   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
7069                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
7070   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
7071                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
7072   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
7073                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
7074   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
7075                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
7076   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
7077                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
7078   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
7079                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
7080   FormatStyle Indented = getLLVMStyle();
7081   Indented.IndentWrappedFunctionNames = true;
7082   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
7083                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
7084                Indented);
7085   verifyFormat(
7086       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
7087       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
7088       Indented);
7089   verifyFormat(
7090       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
7091       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
7092       Indented);
7093   verifyFormat(
7094       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
7095       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
7096       Indented);
7097 
7098   // FIXME: Without the comment, this breaks after "(".
7099   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
7100                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
7101                getGoogleStyle());
7102 
7103   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
7104                "                  int LoooooooooooooooooooongParam2) {}");
7105   verifyFormat(
7106       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
7107       "                                   SourceLocation L, IdentifierIn *II,\n"
7108       "                                   Type *T) {}");
7109   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
7110                "ReallyReaaallyLongFunctionName(\n"
7111                "    const std::string &SomeParameter,\n"
7112                "    const SomeType<string, SomeOtherTemplateParameter>\n"
7113                "        &ReallyReallyLongParameterName,\n"
7114                "    const SomeType<string, SomeOtherTemplateParameter>\n"
7115                "        &AnotherLongParameterName) {}");
7116   verifyFormat("template <typename A>\n"
7117                "SomeLoooooooooooooooooooooongType<\n"
7118                "    typename some_namespace::SomeOtherType<A>::Type>\n"
7119                "Function() {}");
7120 
7121   verifyGoogleFormat(
7122       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
7123       "    aaaaaaaaaaaaaaaaaaaaaaa;");
7124   verifyGoogleFormat(
7125       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
7126       "                                   SourceLocation L) {}");
7127   verifyGoogleFormat(
7128       "some_namespace::LongReturnType\n"
7129       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
7130       "    int first_long_parameter, int second_parameter) {}");
7131 
7132   verifyGoogleFormat("template <typename T>\n"
7133                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
7134                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
7135   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7136                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
7137 
7138   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
7139                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7140                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7141   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7142                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
7143                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
7144   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7145                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
7146                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
7147                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7148 
7149   verifyFormat("template <typename T> // Templates on own line.\n"
7150                "static int            // Some comment.\n"
7151                "MyFunction(int a);",
7152                getLLVMStyle());
7153 }
7154 
7155 TEST_F(FormatTest, FormatsArrays) {
7156   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
7157                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
7158   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
7159                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
7160   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
7161                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
7162   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7163                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
7164   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7165                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
7166   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7167                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
7168                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
7169   verifyFormat(
7170       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
7171       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
7172       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
7173   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
7174                "    .aaaaaaaaaaaaaaaaaaaaaa();");
7175 
7176   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
7177                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
7178   verifyFormat(
7179       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
7180       "                                  .aaaaaaa[0]\n"
7181       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
7182   verifyFormat("a[::b::c];");
7183 
7184   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
7185 
7186   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
7187   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
7188 }
7189 
7190 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
7191   verifyFormat("(a)->b();");
7192   verifyFormat("--a;");
7193 }
7194 
7195 TEST_F(FormatTest, HandlesIncludeDirectives) {
7196   verifyFormat("#include <string>\n"
7197                "#include <a/b/c.h>\n"
7198                "#include \"a/b/string\"\n"
7199                "#include \"string.h\"\n"
7200                "#include \"string.h\"\n"
7201                "#include <a-a>\n"
7202                "#include < path with space >\n"
7203                "#include_next <test.h>"
7204                "#include \"abc.h\" // this is included for ABC\n"
7205                "#include \"some long include\" // with a comment\n"
7206                "#include \"some very long include path\"\n"
7207                "#include <some/very/long/include/path>\n",
7208                getLLVMStyleWithColumns(35));
7209   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
7210   EXPECT_EQ("#include <a>", format("#include<a>"));
7211 
7212   verifyFormat("#import <string>");
7213   verifyFormat("#import <a/b/c.h>");
7214   verifyFormat("#import \"a/b/string\"");
7215   verifyFormat("#import \"string.h\"");
7216   verifyFormat("#import \"string.h\"");
7217   verifyFormat("#if __has_include(<strstream>)\n"
7218                "#include <strstream>\n"
7219                "#endif");
7220 
7221   verifyFormat("#define MY_IMPORT <a/b>");
7222 
7223   verifyFormat("#if __has_include(<a/b>)");
7224   verifyFormat("#if __has_include_next(<a/b>)");
7225   verifyFormat("#define F __has_include(<a/b>)");
7226   verifyFormat("#define F __has_include_next(<a/b>)");
7227 
7228   // Protocol buffer definition or missing "#".
7229   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
7230                getLLVMStyleWithColumns(30));
7231 
7232   FormatStyle Style = getLLVMStyle();
7233   Style.AlwaysBreakBeforeMultilineStrings = true;
7234   Style.ColumnLimit = 0;
7235   verifyFormat("#import \"abc.h\"", Style);
7236 
7237   // But 'import' might also be a regular C++ namespace.
7238   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7239                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7240 }
7241 
7242 //===----------------------------------------------------------------------===//
7243 // Error recovery tests.
7244 //===----------------------------------------------------------------------===//
7245 
7246 TEST_F(FormatTest, IncompleteParameterLists) {
7247   FormatStyle NoBinPacking = getLLVMStyle();
7248   NoBinPacking.BinPackParameters = false;
7249   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
7250                "                        double *min_x,\n"
7251                "                        double *max_x,\n"
7252                "                        double *min_y,\n"
7253                "                        double *max_y,\n"
7254                "                        double *min_z,\n"
7255                "                        double *max_z, ) {}",
7256                NoBinPacking);
7257 }
7258 
7259 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
7260   verifyFormat("void f() { return; }\n42");
7261   verifyFormat("void f() {\n"
7262                "  if (0)\n"
7263                "    return;\n"
7264                "}\n"
7265                "42");
7266   verifyFormat("void f() { return }\n42");
7267   verifyFormat("void f() {\n"
7268                "  if (0)\n"
7269                "    return\n"
7270                "}\n"
7271                "42");
7272 }
7273 
7274 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
7275   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
7276   EXPECT_EQ("void f() {\n"
7277             "  if (a)\n"
7278             "    return\n"
7279             "}",
7280             format("void  f  (  )  {  if  ( a )  return  }"));
7281   EXPECT_EQ("namespace N {\n"
7282             "void f()\n"
7283             "}",
7284             format("namespace  N  {  void f()  }"));
7285   EXPECT_EQ("namespace N {\n"
7286             "void f() {}\n"
7287             "void g()\n"
7288             "} // namespace N",
7289             format("namespace N  { void f( ) { } void g( ) }"));
7290 }
7291 
7292 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
7293   verifyFormat("int aaaaaaaa =\n"
7294                "    // Overlylongcomment\n"
7295                "    b;",
7296                getLLVMStyleWithColumns(20));
7297   verifyFormat("function(\n"
7298                "    ShortArgument,\n"
7299                "    LoooooooooooongArgument);\n",
7300                getLLVMStyleWithColumns(20));
7301 }
7302 
7303 TEST_F(FormatTest, IncorrectAccessSpecifier) {
7304   verifyFormat("public:");
7305   verifyFormat("class A {\n"
7306                "public\n"
7307                "  void f() {}\n"
7308                "};");
7309   verifyFormat("public\n"
7310                "int qwerty;");
7311   verifyFormat("public\n"
7312                "B {}");
7313   verifyFormat("public\n"
7314                "{}");
7315   verifyFormat("public\n"
7316                "B { int x; }");
7317 }
7318 
7319 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
7320   verifyFormat("{");
7321   verifyFormat("#})");
7322   verifyNoCrash("(/**/[:!] ?[).");
7323 }
7324 
7325 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
7326   // Found by oss-fuzz:
7327   // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
7328   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
7329   Style.ColumnLimit = 60;
7330   verifyNoCrash(
7331       "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
7332       "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
7333       "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
7334       Style);
7335 }
7336 
7337 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
7338   verifyFormat("do {\n}");
7339   verifyFormat("do {\n}\n"
7340                "f();");
7341   verifyFormat("do {\n}\n"
7342                "wheeee(fun);");
7343   verifyFormat("do {\n"
7344                "  f();\n"
7345                "}");
7346 }
7347 
7348 TEST_F(FormatTest, IncorrectCodeMissingParens) {
7349   verifyFormat("if {\n  foo;\n  foo();\n}");
7350   verifyFormat("switch {\n  foo;\n  foo();\n}");
7351   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
7352   verifyFormat("while {\n  foo;\n  foo();\n}");
7353   verifyFormat("do {\n  foo;\n  foo();\n} while;");
7354 }
7355 
7356 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
7357   verifyIncompleteFormat("namespace {\n"
7358                          "class Foo { Foo (\n"
7359                          "};\n"
7360                          "} // namespace");
7361 }
7362 
7363 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
7364   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
7365   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
7366   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
7367   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
7368 
7369   EXPECT_EQ("{\n"
7370             "  {\n"
7371             "    breakme(\n"
7372             "        qwe);\n"
7373             "  }\n",
7374             format("{\n"
7375                    "    {\n"
7376                    " breakme(qwe);\n"
7377                    "}\n",
7378                    getLLVMStyleWithColumns(10)));
7379 }
7380 
7381 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
7382   verifyFormat("int x = {\n"
7383                "    avariable,\n"
7384                "    b(alongervariable)};",
7385                getLLVMStyleWithColumns(25));
7386 }
7387 
7388 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
7389   verifyFormat("return (a)(b){1, 2, 3};");
7390 }
7391 
7392 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
7393   verifyFormat("vector<int> x{1, 2, 3, 4};");
7394   verifyFormat("vector<int> x{\n"
7395                "    1,\n"
7396                "    2,\n"
7397                "    3,\n"
7398                "    4,\n"
7399                "};");
7400   verifyFormat("vector<T> x{{}, {}, {}, {}};");
7401   verifyFormat("f({1, 2});");
7402   verifyFormat("auto v = Foo{-1};");
7403   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
7404   verifyFormat("Class::Class : member{1, 2, 3} {}");
7405   verifyFormat("new vector<int>{1, 2, 3};");
7406   verifyFormat("new int[3]{1, 2, 3};");
7407   verifyFormat("new int{1};");
7408   verifyFormat("return {arg1, arg2};");
7409   verifyFormat("return {arg1, SomeType{parameter}};");
7410   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
7411   verifyFormat("new T{arg1, arg2};");
7412   verifyFormat("f(MyMap[{composite, key}]);");
7413   verifyFormat("class Class {\n"
7414                "  T member = {arg1, arg2};\n"
7415                "};");
7416   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
7417   verifyFormat("const struct A a = {.a = 1, .b = 2};");
7418   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
7419   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
7420   verifyFormat("int a = std::is_integral<int>{} + 0;");
7421 
7422   verifyFormat("int foo(int i) { return fo1{}(i); }");
7423   verifyFormat("int foo(int i) { return fo1{}(i); }");
7424   verifyFormat("auto i = decltype(x){};");
7425   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
7426   verifyFormat("Node n{1, Node{1000}, //\n"
7427                "       2};");
7428   verifyFormat("Aaaa aaaaaaa{\n"
7429                "    {\n"
7430                "        aaaa,\n"
7431                "    },\n"
7432                "};");
7433   verifyFormat("class C : public D {\n"
7434                "  SomeClass SC{2};\n"
7435                "};");
7436   verifyFormat("class C : public A {\n"
7437                "  class D : public B {\n"
7438                "    void f() { int i{2}; }\n"
7439                "  };\n"
7440                "};");
7441   verifyFormat("#define A {a, a},");
7442 
7443   // Avoid breaking between equal sign and opening brace
7444   FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
7445   AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
7446   verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
7447                "    {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
7448                "     {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
7449                "     {\"ccccccccccccccccccccc\", 2}};",
7450                AvoidBreakingFirstArgument);
7451 
7452   // Binpacking only if there is no trailing comma
7453   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
7454                "                      cccccccccc, dddddddddd};",
7455 			   getLLVMStyleWithColumns(50));
7456   verifyFormat("const Aaaaaa aaaaa = {\n"
7457                "    aaaaaaaaaaa,\n"
7458                "    bbbbbbbbbbb,\n"
7459                "    ccccccccccc,\n"
7460                "    ddddddddddd,\n"
7461                "};", getLLVMStyleWithColumns(50));
7462 
7463   // Cases where distinguising braced lists and blocks is hard.
7464   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
7465   verifyFormat("void f() {\n"
7466                "  return; // comment\n"
7467                "}\n"
7468                "SomeType t;");
7469   verifyFormat("void f() {\n"
7470                "  if (a) {\n"
7471                "    f();\n"
7472                "  }\n"
7473                "}\n"
7474                "SomeType t;");
7475 
7476   // In combination with BinPackArguments = false.
7477   FormatStyle NoBinPacking = getLLVMStyle();
7478   NoBinPacking.BinPackArguments = false;
7479   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
7480                "                      bbbbb,\n"
7481                "                      ccccc,\n"
7482                "                      ddddd,\n"
7483                "                      eeeee,\n"
7484                "                      ffffff,\n"
7485                "                      ggggg,\n"
7486                "                      hhhhhh,\n"
7487                "                      iiiiii,\n"
7488                "                      jjjjjj,\n"
7489                "                      kkkkkk};",
7490                NoBinPacking);
7491   verifyFormat("const Aaaaaa aaaaa = {\n"
7492                "    aaaaa,\n"
7493                "    bbbbb,\n"
7494                "    ccccc,\n"
7495                "    ddddd,\n"
7496                "    eeeee,\n"
7497                "    ffffff,\n"
7498                "    ggggg,\n"
7499                "    hhhhhh,\n"
7500                "    iiiiii,\n"
7501                "    jjjjjj,\n"
7502                "    kkkkkk,\n"
7503                "};",
7504                NoBinPacking);
7505   verifyFormat(
7506       "const Aaaaaa aaaaa = {\n"
7507       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
7508       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
7509       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
7510       "};",
7511       NoBinPacking);
7512 
7513   // FIXME: The alignment of these trailing comments might be bad. Then again,
7514   // this might be utterly useless in real code.
7515   verifyFormat("Constructor::Constructor()\n"
7516                "    : some_value{         //\n"
7517                "                 aaaaaaa, //\n"
7518                "                 bbbbbbb} {}");
7519 
7520   // In braced lists, the first comment is always assumed to belong to the
7521   // first element. Thus, it can be moved to the next or previous line as
7522   // appropriate.
7523   EXPECT_EQ("function({// First element:\n"
7524             "          1,\n"
7525             "          // Second element:\n"
7526             "          2});",
7527             format("function({\n"
7528                    "    // First element:\n"
7529                    "    1,\n"
7530                    "    // Second element:\n"
7531                    "    2});"));
7532   EXPECT_EQ("std::vector<int> MyNumbers{\n"
7533             "    // First element:\n"
7534             "    1,\n"
7535             "    // Second element:\n"
7536             "    2};",
7537             format("std::vector<int> MyNumbers{// First element:\n"
7538                    "                           1,\n"
7539                    "                           // Second element:\n"
7540                    "                           2};",
7541                    getLLVMStyleWithColumns(30)));
7542   // A trailing comma should still lead to an enforced line break and no
7543   // binpacking.
7544   EXPECT_EQ("vector<int> SomeVector = {\n"
7545             "    // aaa\n"
7546             "    1,\n"
7547             "    2,\n"
7548             "};",
7549             format("vector<int> SomeVector = { // aaa\n"
7550                    "    1, 2, };"));
7551 
7552   FormatStyle ExtraSpaces = getLLVMStyle();
7553   ExtraSpaces.Cpp11BracedListStyle = false;
7554   ExtraSpaces.ColumnLimit = 75;
7555   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
7556   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
7557   verifyFormat("f({ 1, 2 });", ExtraSpaces);
7558   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
7559   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
7560   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
7561   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
7562   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
7563   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
7564   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
7565   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
7566   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
7567   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
7568   verifyFormat("class Class {\n"
7569                "  T member = { arg1, arg2 };\n"
7570                "};",
7571                ExtraSpaces);
7572   verifyFormat(
7573       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7574       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
7575       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
7576       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
7577       ExtraSpaces);
7578   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
7579   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
7580                ExtraSpaces);
7581   verifyFormat(
7582       "someFunction(OtherParam,\n"
7583       "             BracedList{ // comment 1 (Forcing interesting break)\n"
7584       "                         param1, param2,\n"
7585       "                         // comment 2\n"
7586       "                         param3, param4 });",
7587       ExtraSpaces);
7588   verifyFormat(
7589       "std::this_thread::sleep_for(\n"
7590       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
7591       ExtraSpaces);
7592   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
7593                "    aaaaaaa,\n"
7594                "    aaaaaaaaaa,\n"
7595                "    aaaaa,\n"
7596                "    aaaaaaaaaaaaaaa,\n"
7597                "    aaa,\n"
7598                "    aaaaaaaaaa,\n"
7599                "    a,\n"
7600                "    aaaaaaaaaaaaaaaaaaaaa,\n"
7601                "    aaaaaaaaaaaa,\n"
7602                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
7603                "    aaaaaaa,\n"
7604                "    a};");
7605   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
7606   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
7607   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
7608 
7609   // Avoid breaking between initializer/equal sign and opening brace
7610   ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
7611   verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
7612                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
7613                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
7614                "  { \"ccccccccccccccccccccc\", 2 }\n"
7615                "};",
7616                ExtraSpaces);
7617   verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
7618                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
7619                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
7620                "  { \"ccccccccccccccccccccc\", 2 }\n"
7621                "};",
7622                ExtraSpaces);
7623 
7624   FormatStyle SpaceBeforeBrace = getLLVMStyle();
7625   SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
7626   verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
7627   verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
7628 }
7629 
7630 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
7631   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7632                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7633                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7634                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7635                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7636                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
7637   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
7638                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7639                "                 1, 22, 333, 4444, 55555, //\n"
7640                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7641                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
7642   verifyFormat(
7643       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
7644       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
7645       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
7646       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
7647       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
7648       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
7649       "                 7777777};");
7650   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
7651                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
7652                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
7653   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
7654                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
7655                "    // Separating comment.\n"
7656                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
7657   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
7658                "    // Leading comment\n"
7659                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
7660                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
7661   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
7662                "                 1, 1, 1, 1};",
7663                getLLVMStyleWithColumns(39));
7664   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
7665                "                 1, 1, 1, 1};",
7666                getLLVMStyleWithColumns(38));
7667   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
7668                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
7669                getLLVMStyleWithColumns(43));
7670   verifyFormat(
7671       "static unsigned SomeValues[10][3] = {\n"
7672       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
7673       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
7674   verifyFormat("static auto fields = new vector<string>{\n"
7675                "    \"aaaaaaaaaaaaa\",\n"
7676                "    \"aaaaaaaaaaaaa\",\n"
7677                "    \"aaaaaaaaaaaa\",\n"
7678                "    \"aaaaaaaaaaaaaa\",\n"
7679                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
7680                "    \"aaaaaaaaaaaa\",\n"
7681                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
7682                "};");
7683   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
7684   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
7685                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
7686                "                 3, cccccccccccccccccccccc};",
7687                getLLVMStyleWithColumns(60));
7688 
7689   // Trailing commas.
7690   verifyFormat("vector<int> x = {\n"
7691                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
7692                "};",
7693                getLLVMStyleWithColumns(39));
7694   verifyFormat("vector<int> x = {\n"
7695                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
7696                "};",
7697                getLLVMStyleWithColumns(39));
7698   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
7699                "                 1, 1, 1, 1,\n"
7700                "                 /**/ /**/};",
7701                getLLVMStyleWithColumns(39));
7702 
7703   // Trailing comment in the first line.
7704   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
7705                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
7706                "    111111111,  222222222,  3333333333,  444444444,  //\n"
7707                "    11111111,   22222222,   333333333,   44444444};");
7708   // Trailing comment in the last line.
7709   verifyFormat("int aaaaa[] = {\n"
7710                "    1, 2, 3, // comment\n"
7711                "    4, 5, 6  // comment\n"
7712                "};");
7713 
7714   // With nested lists, we should either format one item per line or all nested
7715   // lists one on line.
7716   // FIXME: For some nested lists, we can do better.
7717   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
7718                "        {aaaaaaaaaaaaaaaaaaa},\n"
7719                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
7720                "        {aaaaaaaaaaaaaaaaa}};",
7721                getLLVMStyleWithColumns(60));
7722   verifyFormat(
7723       "SomeStruct my_struct_array = {\n"
7724       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
7725       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
7726       "    {aaa, aaa},\n"
7727       "    {aaa, aaa},\n"
7728       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
7729       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7730       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
7731 
7732   // No column layout should be used here.
7733   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
7734                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
7735 
7736   verifyNoCrash("a<,");
7737 
7738   // No braced initializer here.
7739   verifyFormat("void f() {\n"
7740                "  struct Dummy {};\n"
7741                "  f(v);\n"
7742                "}");
7743 
7744   // Long lists should be formatted in columns even if they are nested.
7745   verifyFormat(
7746       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7747       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7748       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7749       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7750       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7751       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
7752 
7753   // Allow "single-column" layout even if that violates the column limit. There
7754   // isn't going to be a better way.
7755   verifyFormat("std::vector<int> a = {\n"
7756                "    aaaaaaaa,\n"
7757                "    aaaaaaaa,\n"
7758                "    aaaaaaaa,\n"
7759                "    aaaaaaaa,\n"
7760                "    aaaaaaaaaa,\n"
7761                "    aaaaaaaa,\n"
7762                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
7763                getLLVMStyleWithColumns(30));
7764   verifyFormat("vector<int> aaaa = {\n"
7765                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7766                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7767                "    aaaaaa.aaaaaaa,\n"
7768                "    aaaaaa.aaaaaaa,\n"
7769                "    aaaaaa.aaaaaaa,\n"
7770                "    aaaaaa.aaaaaaa,\n"
7771                "};");
7772 
7773   // Don't create hanging lists.
7774   verifyFormat("someFunction(Param, {List1, List2,\n"
7775                "                     List3});",
7776                getLLVMStyleWithColumns(35));
7777   verifyFormat("someFunction(Param, Param,\n"
7778                "             {List1, List2,\n"
7779                "              List3});",
7780                getLLVMStyleWithColumns(35));
7781   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
7782                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
7783 }
7784 
7785 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
7786   FormatStyle DoNotMerge = getLLVMStyle();
7787   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
7788 
7789   verifyFormat("void f() { return 42; }");
7790   verifyFormat("void f() {\n"
7791                "  return 42;\n"
7792                "}",
7793                DoNotMerge);
7794   verifyFormat("void f() {\n"
7795                "  // Comment\n"
7796                "}");
7797   verifyFormat("{\n"
7798                "#error {\n"
7799                "  int a;\n"
7800                "}");
7801   verifyFormat("{\n"
7802                "  int a;\n"
7803                "#error {\n"
7804                "}");
7805   verifyFormat("void f() {} // comment");
7806   verifyFormat("void f() { int a; } // comment");
7807   verifyFormat("void f() {\n"
7808                "} // comment",
7809                DoNotMerge);
7810   verifyFormat("void f() {\n"
7811                "  int a;\n"
7812                "} // comment",
7813                DoNotMerge);
7814   verifyFormat("void f() {\n"
7815                "} // comment",
7816                getLLVMStyleWithColumns(15));
7817 
7818   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
7819   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
7820 
7821   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
7822   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
7823   verifyFormat("class C {\n"
7824                "  C()\n"
7825                "      : iiiiiiii(nullptr),\n"
7826                "        kkkkkkk(nullptr),\n"
7827                "        mmmmmmm(nullptr),\n"
7828                "        nnnnnnn(nullptr) {}\n"
7829                "};",
7830                getGoogleStyle());
7831 
7832   FormatStyle NoColumnLimit = getLLVMStyle();
7833   NoColumnLimit.ColumnLimit = 0;
7834   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
7835   EXPECT_EQ("class C {\n"
7836             "  A() : b(0) {}\n"
7837             "};",
7838             format("class C{A():b(0){}};", NoColumnLimit));
7839   EXPECT_EQ("A()\n"
7840             "    : b(0) {\n"
7841             "}",
7842             format("A()\n:b(0)\n{\n}", NoColumnLimit));
7843 
7844   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
7845   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
7846       FormatStyle::SFS_None;
7847   EXPECT_EQ("A()\n"
7848             "    : b(0) {\n"
7849             "}",
7850             format("A():b(0){}", DoNotMergeNoColumnLimit));
7851   EXPECT_EQ("A()\n"
7852             "    : b(0) {\n"
7853             "}",
7854             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
7855 
7856   verifyFormat("#define A          \\\n"
7857                "  void f() {       \\\n"
7858                "    int i;         \\\n"
7859                "  }",
7860                getLLVMStyleWithColumns(20));
7861   verifyFormat("#define A           \\\n"
7862                "  void f() { int i; }",
7863                getLLVMStyleWithColumns(21));
7864   verifyFormat("#define A            \\\n"
7865                "  void f() {         \\\n"
7866                "    int i;           \\\n"
7867                "  }                  \\\n"
7868                "  int j;",
7869                getLLVMStyleWithColumns(22));
7870   verifyFormat("#define A             \\\n"
7871                "  void f() { int i; } \\\n"
7872                "  int j;",
7873                getLLVMStyleWithColumns(23));
7874 }
7875 
7876 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
7877   FormatStyle MergeEmptyOnly = getLLVMStyle();
7878   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
7879   verifyFormat("class C {\n"
7880                "  int f() {}\n"
7881                "};",
7882                MergeEmptyOnly);
7883   verifyFormat("class C {\n"
7884                "  int f() {\n"
7885                "    return 42;\n"
7886                "  }\n"
7887                "};",
7888                MergeEmptyOnly);
7889   verifyFormat("int f() {}", MergeEmptyOnly);
7890   verifyFormat("int f() {\n"
7891                "  return 42;\n"
7892                "}",
7893                MergeEmptyOnly);
7894 
7895   // Also verify behavior when BraceWrapping.AfterFunction = true
7896   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7897   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
7898   verifyFormat("int f() {}", MergeEmptyOnly);
7899   verifyFormat("class C {\n"
7900                "  int f() {}\n"
7901                "};",
7902                MergeEmptyOnly);
7903 }
7904 
7905 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
7906   FormatStyle MergeInlineOnly = getLLVMStyle();
7907   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
7908   verifyFormat("class C {\n"
7909                "  int f() { return 42; }\n"
7910                "};",
7911                MergeInlineOnly);
7912   verifyFormat("int f() {\n"
7913                "  return 42;\n"
7914                "}",
7915                MergeInlineOnly);
7916 
7917   // SFS_Inline implies SFS_Empty
7918   verifyFormat("class C {\n"
7919                "  int f() {}\n"
7920                "};",
7921                MergeInlineOnly);
7922   verifyFormat("int f() {}", MergeInlineOnly);
7923 
7924   // Also verify behavior when BraceWrapping.AfterFunction = true
7925   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7926   MergeInlineOnly.BraceWrapping.AfterFunction = true;
7927   verifyFormat("class C {\n"
7928                "  int f() { return 42; }\n"
7929                "};",
7930                MergeInlineOnly);
7931   verifyFormat("int f()\n"
7932                "{\n"
7933                "  return 42;\n"
7934                "}",
7935                MergeInlineOnly);
7936 
7937   // SFS_Inline implies SFS_Empty
7938   verifyFormat("int f() {}", MergeInlineOnly);
7939   verifyFormat("class C {\n"
7940                "  int f() {}\n"
7941                "};",
7942                MergeInlineOnly);
7943 }
7944 
7945 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
7946   FormatStyle MergeInlineOnly = getLLVMStyle();
7947   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
7948       FormatStyle::SFS_InlineOnly;
7949   verifyFormat("class C {\n"
7950                "  int f() { return 42; }\n"
7951                "};",
7952                MergeInlineOnly);
7953   verifyFormat("int f() {\n"
7954                "  return 42;\n"
7955                "}",
7956                MergeInlineOnly);
7957 
7958   // SFS_InlineOnly does not imply SFS_Empty
7959   verifyFormat("class C {\n"
7960                "  int f() {}\n"
7961                "};",
7962                MergeInlineOnly);
7963   verifyFormat("int f() {\n"
7964                "}",
7965                MergeInlineOnly);
7966 
7967   // Also verify behavior when BraceWrapping.AfterFunction = true
7968   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7969   MergeInlineOnly.BraceWrapping.AfterFunction = true;
7970   verifyFormat("class C {\n"
7971                "  int f() { return 42; }\n"
7972                "};",
7973                MergeInlineOnly);
7974   verifyFormat("int f()\n"
7975                "{\n"
7976                "  return 42;\n"
7977                "}",
7978                MergeInlineOnly);
7979 
7980   // SFS_InlineOnly does not imply SFS_Empty
7981   verifyFormat("int f()\n"
7982                "{\n"
7983                "}",
7984                MergeInlineOnly);
7985   verifyFormat("class C {\n"
7986                "  int f() {}\n"
7987                "};",
7988                MergeInlineOnly);
7989 }
7990 
7991 TEST_F(FormatTest, SplitEmptyFunction) {
7992   FormatStyle Style = getLLVMStyle();
7993   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
7994   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7995   Style.BraceWrapping.AfterFunction = true;
7996   Style.BraceWrapping.SplitEmptyFunction = false;
7997   Style.ColumnLimit = 40;
7998 
7999   verifyFormat("int f()\n"
8000                "{}",
8001                Style);
8002   verifyFormat("int f()\n"
8003                "{\n"
8004                "  return 42;\n"
8005                "}",
8006                Style);
8007   verifyFormat("int f()\n"
8008                "{\n"
8009                "  // some comment\n"
8010                "}",
8011                Style);
8012 
8013   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
8014   verifyFormat("int f() {}", Style);
8015   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
8016                "{}",
8017                Style);
8018   verifyFormat("int f()\n"
8019                "{\n"
8020                "  return 0;\n"
8021                "}",
8022                Style);
8023 
8024   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
8025   verifyFormat("class Foo {\n"
8026                "  int f() {}\n"
8027                "};\n",
8028                Style);
8029   verifyFormat("class Foo {\n"
8030                "  int f() { return 0; }\n"
8031                "};\n",
8032                Style);
8033   verifyFormat("class Foo {\n"
8034                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
8035                "  {}\n"
8036                "};\n",
8037                Style);
8038   verifyFormat("class Foo {\n"
8039                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
8040                "  {\n"
8041                "    return 0;\n"
8042                "  }\n"
8043                "};\n",
8044                Style);
8045 
8046   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
8047   verifyFormat("int f() {}", Style);
8048   verifyFormat("int f() { return 0; }", Style);
8049   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
8050                "{}",
8051                Style);
8052   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
8053                "{\n"
8054                "  return 0;\n"
8055                "}",
8056                Style);
8057 }
8058 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
8059   FormatStyle Style = getLLVMStyle();
8060   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
8061   verifyFormat("#ifdef A\n"
8062                "int f() {}\n"
8063                "#else\n"
8064                "int g() {}\n"
8065                "#endif",
8066                Style);
8067 }
8068 
8069 TEST_F(FormatTest, SplitEmptyClass) {
8070   FormatStyle Style = getLLVMStyle();
8071   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
8072   Style.BraceWrapping.AfterClass = true;
8073   Style.BraceWrapping.SplitEmptyRecord = false;
8074 
8075   verifyFormat("class Foo\n"
8076                "{};",
8077                Style);
8078   verifyFormat("/* something */ class Foo\n"
8079                "{};",
8080                Style);
8081   verifyFormat("template <typename X> class Foo\n"
8082                "{};",
8083                Style);
8084   verifyFormat("class Foo\n"
8085                "{\n"
8086                "  Foo();\n"
8087                "};",
8088                Style);
8089   verifyFormat("typedef class Foo\n"
8090                "{\n"
8091                "} Foo_t;",
8092                Style);
8093 }
8094 
8095 TEST_F(FormatTest, SplitEmptyStruct) {
8096   FormatStyle Style = getLLVMStyle();
8097   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
8098   Style.BraceWrapping.AfterStruct = true;
8099   Style.BraceWrapping.SplitEmptyRecord = false;
8100 
8101   verifyFormat("struct Foo\n"
8102                "{};",
8103                Style);
8104   verifyFormat("/* something */ struct Foo\n"
8105                "{};",
8106                Style);
8107   verifyFormat("template <typename X> struct Foo\n"
8108                "{};",
8109                Style);
8110   verifyFormat("struct Foo\n"
8111                "{\n"
8112                "  Foo();\n"
8113                "};",
8114                Style);
8115   verifyFormat("typedef struct Foo\n"
8116                "{\n"
8117                "} Foo_t;",
8118                Style);
8119   //typedef struct Bar {} Bar_t;
8120 }
8121 
8122 TEST_F(FormatTest, SplitEmptyUnion) {
8123   FormatStyle Style = getLLVMStyle();
8124   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
8125   Style.BraceWrapping.AfterUnion = true;
8126   Style.BraceWrapping.SplitEmptyRecord = false;
8127 
8128   verifyFormat("union Foo\n"
8129                "{};",
8130                Style);
8131   verifyFormat("/* something */ union Foo\n"
8132                "{};",
8133                Style);
8134   verifyFormat("union Foo\n"
8135                "{\n"
8136                "  A,\n"
8137                "};",
8138                Style);
8139   verifyFormat("typedef union Foo\n"
8140                "{\n"
8141                "} Foo_t;",
8142                Style);
8143 }
8144 
8145 TEST_F(FormatTest, SplitEmptyNamespace) {
8146   FormatStyle Style = getLLVMStyle();
8147   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
8148   Style.BraceWrapping.AfterNamespace = true;
8149   Style.BraceWrapping.SplitEmptyNamespace = false;
8150 
8151   verifyFormat("namespace Foo\n"
8152                "{};",
8153                Style);
8154   verifyFormat("/* something */ namespace Foo\n"
8155                "{};",
8156                Style);
8157   verifyFormat("inline namespace Foo\n"
8158                "{};",
8159                Style);
8160   verifyFormat("/* something */ inline namespace Foo\n"
8161                "{};",
8162                Style);
8163   verifyFormat("export namespace Foo\n"
8164                "{};",
8165                Style);
8166   verifyFormat("namespace Foo\n"
8167                "{\n"
8168                "void Bar();\n"
8169                "};",
8170                Style);
8171 }
8172 
8173 TEST_F(FormatTest, NeverMergeShortRecords) {
8174   FormatStyle Style = getLLVMStyle();
8175 
8176   verifyFormat("class Foo {\n"
8177                "  Foo();\n"
8178                "};",
8179                Style);
8180   verifyFormat("typedef class Foo {\n"
8181                "  Foo();\n"
8182                "} Foo_t;",
8183                Style);
8184   verifyFormat("struct Foo {\n"
8185                "  Foo();\n"
8186                "};",
8187                Style);
8188   verifyFormat("typedef struct Foo {\n"
8189                "  Foo();\n"
8190                "} Foo_t;",
8191                Style);
8192   verifyFormat("union Foo {\n"
8193                "  A,\n"
8194                "};",
8195                Style);
8196   verifyFormat("typedef union Foo {\n"
8197                "  A,\n"
8198                "} Foo_t;",
8199                Style);
8200   verifyFormat("namespace Foo {\n"
8201                "void Bar();\n"
8202                "};",
8203                Style);
8204 
8205   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
8206   Style.BraceWrapping.AfterClass = true;
8207   Style.BraceWrapping.AfterStruct = true;
8208   Style.BraceWrapping.AfterUnion = true;
8209   Style.BraceWrapping.AfterNamespace = true;
8210   verifyFormat("class Foo\n"
8211                "{\n"
8212                "  Foo();\n"
8213                "};",
8214                Style);
8215   verifyFormat("typedef class Foo\n"
8216                "{\n"
8217                "  Foo();\n"
8218                "} Foo_t;",
8219                Style);
8220   verifyFormat("struct Foo\n"
8221                "{\n"
8222                "  Foo();\n"
8223                "};",
8224                Style);
8225   verifyFormat("typedef struct Foo\n"
8226                "{\n"
8227                "  Foo();\n"
8228                "} Foo_t;",
8229                Style);
8230   verifyFormat("union Foo\n"
8231                "{\n"
8232                "  A,\n"
8233                "};",
8234                Style);
8235   verifyFormat("typedef union Foo\n"
8236                "{\n"
8237                "  A,\n"
8238                "} Foo_t;",
8239                Style);
8240   verifyFormat("namespace Foo\n"
8241                "{\n"
8242                "void Bar();\n"
8243                "};",
8244                Style);
8245 }
8246 
8247 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
8248   // Elaborate type variable declarations.
8249   verifyFormat("struct foo a = {bar};\nint n;");
8250   verifyFormat("class foo a = {bar};\nint n;");
8251   verifyFormat("union foo a = {bar};\nint n;");
8252 
8253   // Elaborate types inside function definitions.
8254   verifyFormat("struct foo f() {}\nint n;");
8255   verifyFormat("class foo f() {}\nint n;");
8256   verifyFormat("union foo f() {}\nint n;");
8257 
8258   // Templates.
8259   verifyFormat("template <class X> void f() {}\nint n;");
8260   verifyFormat("template <struct X> void f() {}\nint n;");
8261   verifyFormat("template <union X> void f() {}\nint n;");
8262 
8263   // Actual definitions...
8264   verifyFormat("struct {\n} n;");
8265   verifyFormat(
8266       "template <template <class T, class Y>, class Z> class X {\n} n;");
8267   verifyFormat("union Z {\n  int n;\n} x;");
8268   verifyFormat("class MACRO Z {\n} n;");
8269   verifyFormat("class MACRO(X) Z {\n} n;");
8270   verifyFormat("class __attribute__(X) Z {\n} n;");
8271   verifyFormat("class __declspec(X) Z {\n} n;");
8272   verifyFormat("class A##B##C {\n} n;");
8273   verifyFormat("class alignas(16) Z {\n} n;");
8274   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
8275   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
8276 
8277   // Redefinition from nested context:
8278   verifyFormat("class A::B::C {\n} n;");
8279 
8280   // Template definitions.
8281   verifyFormat(
8282       "template <typename F>\n"
8283       "Matcher(const Matcher<F> &Other,\n"
8284       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
8285       "                             !is_same<F, T>::value>::type * = 0)\n"
8286       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
8287 
8288   // FIXME: This is still incorrectly handled at the formatter side.
8289   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
8290   verifyFormat("int i = SomeFunction(a<b, a> b);");
8291 
8292   // FIXME:
8293   // This now gets parsed incorrectly as class definition.
8294   // verifyFormat("class A<int> f() {\n}\nint n;");
8295 
8296   // Elaborate types where incorrectly parsing the structural element would
8297   // break the indent.
8298   verifyFormat("if (true)\n"
8299                "  class X x;\n"
8300                "else\n"
8301                "  f();\n");
8302 
8303   // This is simply incomplete. Formatting is not important, but must not crash.
8304   verifyFormat("class A:");
8305 }
8306 
8307 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
8308   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
8309             format("#error Leave     all         white!!!!! space* alone!\n"));
8310   EXPECT_EQ(
8311       "#warning Leave     all         white!!!!! space* alone!\n",
8312       format("#warning Leave     all         white!!!!! space* alone!\n"));
8313   EXPECT_EQ("#error 1", format("  #  error   1"));
8314   EXPECT_EQ("#warning 1", format("  #  warning 1"));
8315 }
8316 
8317 TEST_F(FormatTest, FormatHashIfExpressions) {
8318   verifyFormat("#if AAAA && BBBB");
8319   verifyFormat("#if (AAAA && BBBB)");
8320   verifyFormat("#elif (AAAA && BBBB)");
8321   // FIXME: Come up with a better indentation for #elif.
8322   verifyFormat(
8323       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
8324       "    defined(BBBBBBBB)\n"
8325       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
8326       "    defined(BBBBBBBB)\n"
8327       "#endif",
8328       getLLVMStyleWithColumns(65));
8329 }
8330 
8331 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
8332   FormatStyle AllowsMergedIf = getGoogleStyle();
8333   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
8334       FormatStyle::SIS_WithoutElse;
8335   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
8336   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
8337   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
8338   EXPECT_EQ("if (true) return 42;",
8339             format("if (true)\nreturn 42;", AllowsMergedIf));
8340   FormatStyle ShortMergedIf = AllowsMergedIf;
8341   ShortMergedIf.ColumnLimit = 25;
8342   verifyFormat("#define A \\\n"
8343                "  if (true) return 42;",
8344                ShortMergedIf);
8345   verifyFormat("#define A \\\n"
8346                "  f();    \\\n"
8347                "  if (true)\n"
8348                "#define B",
8349                ShortMergedIf);
8350   verifyFormat("#define A \\\n"
8351                "  f();    \\\n"
8352                "  if (true)\n"
8353                "g();",
8354                ShortMergedIf);
8355   verifyFormat("{\n"
8356                "#ifdef A\n"
8357                "  // Comment\n"
8358                "  if (true) continue;\n"
8359                "#endif\n"
8360                "  // Comment\n"
8361                "  if (true) continue;\n"
8362                "}",
8363                ShortMergedIf);
8364   ShortMergedIf.ColumnLimit = 33;
8365   verifyFormat("#define A \\\n"
8366                "  if constexpr (true) return 42;",
8367                ShortMergedIf);
8368   ShortMergedIf.ColumnLimit = 29;
8369   verifyFormat("#define A                   \\\n"
8370                "  if (aaaaaaaaaa) return 1; \\\n"
8371                "  return 2;",
8372                ShortMergedIf);
8373   ShortMergedIf.ColumnLimit = 28;
8374   verifyFormat("#define A         \\\n"
8375                "  if (aaaaaaaaaa) \\\n"
8376                "    return 1;     \\\n"
8377                "  return 2;",
8378                ShortMergedIf);
8379   verifyFormat("#define A                \\\n"
8380                "  if constexpr (aaaaaaa) \\\n"
8381                "    return 1;            \\\n"
8382                "  return 2;",
8383                ShortMergedIf);
8384 }
8385 
8386 TEST_F(FormatTest, FormatStarDependingOnContext) {
8387   verifyFormat("void f(int *a);");
8388   verifyFormat("void f() { f(fint * b); }");
8389   verifyFormat("class A {\n  void f(int *a);\n};");
8390   verifyFormat("class A {\n  int *a;\n};");
8391   verifyFormat("namespace a {\n"
8392                "namespace b {\n"
8393                "class A {\n"
8394                "  void f() {}\n"
8395                "  int *a;\n"
8396                "};\n"
8397                "} // namespace b\n"
8398                "} // namespace a");
8399 }
8400 
8401 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
8402   verifyFormat("while");
8403   verifyFormat("operator");
8404 }
8405 
8406 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
8407   // This code would be painfully slow to format if we didn't skip it.
8408   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
8409                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
8410                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
8411                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
8412                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
8413                    "A(1, 1)\n"
8414                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
8415                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8416                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8417                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8418                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8419                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8420                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8421                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8422                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8423                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
8424   // Deeply nested part is untouched, rest is formatted.
8425   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
8426             format(std::string("int    i;\n") + Code + "int    j;\n",
8427                    getLLVMStyle(), SC_ExpectIncomplete));
8428 }
8429 
8430 //===----------------------------------------------------------------------===//
8431 // Objective-C tests.
8432 //===----------------------------------------------------------------------===//
8433 
8434 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
8435   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
8436   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
8437             format("-(NSUInteger)indexOfObject:(id)anObject;"));
8438   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
8439   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
8440   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
8441             format("-(NSInteger)Method3:(id)anObject;"));
8442   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
8443             format("-(NSInteger)Method4:(id)anObject;"));
8444   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
8445             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
8446   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
8447             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
8448   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
8449             "forAllCells:(BOOL)flag;",
8450             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
8451                    "forAllCells:(BOOL)flag;"));
8452 
8453   // Very long objectiveC method declaration.
8454   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
8455                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
8456   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
8457                "                    inRange:(NSRange)range\n"
8458                "                   outRange:(NSRange)out_range\n"
8459                "                  outRange1:(NSRange)out_range1\n"
8460                "                  outRange2:(NSRange)out_range2\n"
8461                "                  outRange3:(NSRange)out_range3\n"
8462                "                  outRange4:(NSRange)out_range4\n"
8463                "                  outRange5:(NSRange)out_range5\n"
8464                "                  outRange6:(NSRange)out_range6\n"
8465                "                  outRange7:(NSRange)out_range7\n"
8466                "                  outRange8:(NSRange)out_range8\n"
8467                "                  outRange9:(NSRange)out_range9;");
8468 
8469   // When the function name has to be wrapped.
8470   FormatStyle Style = getLLVMStyle();
8471   // ObjC ignores IndentWrappedFunctionNames when wrapping methods
8472   // and always indents instead.
8473   Style.IndentWrappedFunctionNames = false;
8474   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
8475                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
8476                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
8477                "}",
8478                Style);
8479   Style.IndentWrappedFunctionNames = true;
8480   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
8481                "    veryLooooooooooongName:(NSString)cccccccccccccc\n"
8482                "               anotherName:(NSString)dddddddddddddd {\n"
8483                "}",
8484                Style);
8485 
8486   verifyFormat("- (int)sum:(vector<int>)numbers;");
8487   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
8488   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
8489   // protocol lists (but not for template classes):
8490   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
8491 
8492   verifyFormat("- (int (*)())foo:(int (*)())f;");
8493   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
8494 
8495   // If there's no return type (very rare in practice!), LLVM and Google style
8496   // agree.
8497   verifyFormat("- foo;");
8498   verifyFormat("- foo:(int)f;");
8499   verifyGoogleFormat("- foo:(int)foo;");
8500 }
8501 
8502 
8503 TEST_F(FormatTest, BreaksStringLiterals) {
8504   EXPECT_EQ("\"some text \"\n"
8505             "\"other\";",
8506             format("\"some text other\";", getLLVMStyleWithColumns(12)));
8507   EXPECT_EQ("\"some text \"\n"
8508             "\"other\";",
8509             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
8510   EXPECT_EQ(
8511       "#define A  \\\n"
8512       "  \"some \"  \\\n"
8513       "  \"text \"  \\\n"
8514       "  \"other\";",
8515       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
8516   EXPECT_EQ(
8517       "#define A  \\\n"
8518       "  \"so \"    \\\n"
8519       "  \"text \"  \\\n"
8520       "  \"other\";",
8521       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
8522 
8523   EXPECT_EQ("\"some text\"",
8524             format("\"some text\"", getLLVMStyleWithColumns(1)));
8525   EXPECT_EQ("\"some text\"",
8526             format("\"some text\"", getLLVMStyleWithColumns(11)));
8527   EXPECT_EQ("\"some \"\n"
8528             "\"text\"",
8529             format("\"some text\"", getLLVMStyleWithColumns(10)));
8530   EXPECT_EQ("\"some \"\n"
8531             "\"text\"",
8532             format("\"some text\"", getLLVMStyleWithColumns(7)));
8533   EXPECT_EQ("\"some\"\n"
8534             "\" tex\"\n"
8535             "\"t\"",
8536             format("\"some text\"", getLLVMStyleWithColumns(6)));
8537   EXPECT_EQ("\"some\"\n"
8538             "\" tex\"\n"
8539             "\" and\"",
8540             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
8541   EXPECT_EQ("\"some\"\n"
8542             "\"/tex\"\n"
8543             "\"/and\"",
8544             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
8545 
8546   EXPECT_EQ("variable =\n"
8547             "    \"long string \"\n"
8548             "    \"literal\";",
8549             format("variable = \"long string literal\";",
8550                    getLLVMStyleWithColumns(20)));
8551 
8552   EXPECT_EQ("variable = f(\n"
8553             "    \"long string \"\n"
8554             "    \"literal\",\n"
8555             "    short,\n"
8556             "    loooooooooooooooooooong);",
8557             format("variable = f(\"long string literal\", short, "
8558                    "loooooooooooooooooooong);",
8559                    getLLVMStyleWithColumns(20)));
8560 
8561   EXPECT_EQ(
8562       "f(g(\"long string \"\n"
8563       "    \"literal\"),\n"
8564       "  b);",
8565       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
8566   EXPECT_EQ("f(g(\"long string \"\n"
8567             "    \"literal\",\n"
8568             "    a),\n"
8569             "  b);",
8570             format("f(g(\"long string literal\", a), b);",
8571                    getLLVMStyleWithColumns(20)));
8572   EXPECT_EQ(
8573       "f(\"one two\".split(\n"
8574       "    variable));",
8575       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
8576   EXPECT_EQ("f(\"one two three four five six \"\n"
8577             "  \"seven\".split(\n"
8578             "      really_looooong_variable));",
8579             format("f(\"one two three four five six seven\"."
8580                    "split(really_looooong_variable));",
8581                    getLLVMStyleWithColumns(33)));
8582 
8583   EXPECT_EQ("f(\"some \"\n"
8584             "  \"text\",\n"
8585             "  other);",
8586             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
8587 
8588   // Only break as a last resort.
8589   verifyFormat(
8590       "aaaaaaaaaaaaaaaaaaaa(\n"
8591       "    aaaaaaaaaaaaaaaaaaaa,\n"
8592       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
8593 
8594   EXPECT_EQ("\"splitmea\"\n"
8595             "\"trandomp\"\n"
8596             "\"oint\"",
8597             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
8598 
8599   EXPECT_EQ("\"split/\"\n"
8600             "\"pathat/\"\n"
8601             "\"slashes\"",
8602             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
8603 
8604   EXPECT_EQ("\"split/\"\n"
8605             "\"pathat/\"\n"
8606             "\"slashes\"",
8607             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
8608   EXPECT_EQ("\"split at \"\n"
8609             "\"spaces/at/\"\n"
8610             "\"slashes.at.any$\"\n"
8611             "\"non-alphanumeric%\"\n"
8612             "\"1111111111characte\"\n"
8613             "\"rs\"",
8614             format("\"split at "
8615                    "spaces/at/"
8616                    "slashes.at."
8617                    "any$non-"
8618                    "alphanumeric%"
8619                    "1111111111characte"
8620                    "rs\"",
8621                    getLLVMStyleWithColumns(20)));
8622 
8623   // Verify that splitting the strings understands
8624   // Style::AlwaysBreakBeforeMultilineStrings.
8625   EXPECT_EQ(
8626       "aaaaaaaaaaaa(\n"
8627       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
8628       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
8629       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
8630              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
8631              "aaaaaaaaaaaaaaaaaaaaaa\");",
8632              getGoogleStyle()));
8633   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8634             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
8635             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
8636                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
8637                    "aaaaaaaaaaaaaaaaaaaaaa\";",
8638                    getGoogleStyle()));
8639   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8640             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
8641             format("llvm::outs() << "
8642                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
8643                    "aaaaaaaaaaaaaaaaaaa\";"));
8644   EXPECT_EQ("ffff(\n"
8645             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8646             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
8647             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
8648                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
8649                    getGoogleStyle()));
8650 
8651   FormatStyle Style = getLLVMStyleWithColumns(12);
8652   Style.BreakStringLiterals = false;
8653   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
8654 
8655   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
8656   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
8657   EXPECT_EQ("#define A \\\n"
8658             "  \"some \" \\\n"
8659             "  \"text \" \\\n"
8660             "  \"other\";",
8661             format("#define A \"some text other\";", AlignLeft));
8662 }
8663 
8664 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
8665   EXPECT_EQ("C a = \"some more \"\n"
8666             "      \"text\";",
8667             format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
8668 }
8669 
8670 TEST_F(FormatTest, FullyRemoveEmptyLines) {
8671   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
8672   NoEmptyLines.MaxEmptyLinesToKeep = 0;
8673   EXPECT_EQ("int i = a(b());",
8674             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
8675 }
8676 
8677 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
8678   EXPECT_EQ(
8679       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
8680       "(\n"
8681       "    \"x\t\");",
8682       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
8683              "aaaaaaa("
8684              "\"x\t\");"));
8685 }
8686 
8687 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
8688   EXPECT_EQ(
8689       "u8\"utf8 string \"\n"
8690       "u8\"literal\";",
8691       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
8692   EXPECT_EQ(
8693       "u\"utf16 string \"\n"
8694       "u\"literal\";",
8695       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
8696   EXPECT_EQ(
8697       "U\"utf32 string \"\n"
8698       "U\"literal\";",
8699       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
8700   EXPECT_EQ("L\"wide string \"\n"
8701             "L\"literal\";",
8702             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
8703   EXPECT_EQ("@\"NSString \"\n"
8704             "@\"literal\";",
8705             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
8706   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
8707 
8708   // This input makes clang-format try to split the incomplete unicode escape
8709   // sequence, which used to lead to a crasher.
8710   verifyNoCrash(
8711       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
8712       getLLVMStyleWithColumns(60));
8713 }
8714 
8715 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
8716   FormatStyle Style = getGoogleStyleWithColumns(15);
8717   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
8718   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
8719   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
8720   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
8721   EXPECT_EQ("u8R\"x(raw literal)x\";",
8722             format("u8R\"x(raw literal)x\";", Style));
8723 }
8724 
8725 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
8726   FormatStyle Style = getLLVMStyleWithColumns(20);
8727   EXPECT_EQ(
8728       "_T(\"aaaaaaaaaaaaaa\")\n"
8729       "_T(\"aaaaaaaaaaaaaa\")\n"
8730       "_T(\"aaaaaaaaaaaa\")",
8731       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
8732   EXPECT_EQ("f(x,\n"
8733             "  _T(\"aaaaaaaaaaaa\")\n"
8734             "  _T(\"aaa\"),\n"
8735             "  z);",
8736             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
8737 
8738   // FIXME: Handle embedded spaces in one iteration.
8739   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
8740   //            "_T(\"aaaaaaaaaaaaa\")\n"
8741   //            "_T(\"aaaaaaaaaaaaa\")\n"
8742   //            "_T(\"a\")",
8743   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
8744   //                   getLLVMStyleWithColumns(20)));
8745   EXPECT_EQ(
8746       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
8747       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
8748   EXPECT_EQ("f(\n"
8749             "#if !TEST\n"
8750             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
8751             "#endif\n"
8752             ");",
8753             format("f(\n"
8754                    "#if !TEST\n"
8755                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
8756                    "#endif\n"
8757                    ");"));
8758   EXPECT_EQ("f(\n"
8759             "\n"
8760             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
8761             format("f(\n"
8762                    "\n"
8763                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
8764 }
8765 
8766 TEST_F(FormatTest, BreaksStringLiteralOperands) {
8767   // In a function call with two operands, the second can be broken with no line
8768   // break before it.
8769   EXPECT_EQ("func(a, \"long long \"\n"
8770             "        \"long long\");",
8771             format("func(a, \"long long long long\");",
8772                    getLLVMStyleWithColumns(24)));
8773   // In a function call with three operands, the second must be broken with a
8774   // line break before it.
8775   EXPECT_EQ("func(a,\n"
8776             "     \"long long long \"\n"
8777             "     \"long\",\n"
8778             "     c);",
8779             format("func(a, \"long long long long\", c);",
8780                    getLLVMStyleWithColumns(24)));
8781   // In a function call with three operands, the third must be broken with a
8782   // line break before it.
8783   EXPECT_EQ("func(a, b,\n"
8784             "     \"long long long \"\n"
8785             "     \"long\");",
8786             format("func(a, b, \"long long long long\");",
8787                    getLLVMStyleWithColumns(24)));
8788   // In a function call with three operands, both the second and the third must
8789   // be broken with a line break before them.
8790   EXPECT_EQ("func(a,\n"
8791             "     \"long long long \"\n"
8792             "     \"long\",\n"
8793             "     \"long long long \"\n"
8794             "     \"long\");",
8795             format("func(a, \"long long long long\", \"long long long long\");",
8796                    getLLVMStyleWithColumns(24)));
8797   // In a chain of << with two operands, the second can be broken with no line
8798   // break before it.
8799   EXPECT_EQ("a << \"line line \"\n"
8800             "     \"line\";",
8801             format("a << \"line line line\";",
8802                    getLLVMStyleWithColumns(20)));
8803   // In a chain of << with three operands, the second can be broken with no line
8804   // break before it.
8805   EXPECT_EQ("abcde << \"line \"\n"
8806             "         \"line line\"\n"
8807             "      << c;",
8808             format("abcde << \"line line line\" << c;",
8809                    getLLVMStyleWithColumns(20)));
8810   // In a chain of << with three operands, the third must be broken with a line
8811   // break before it.
8812   EXPECT_EQ("a << b\n"
8813             "  << \"line line \"\n"
8814             "     \"line\";",
8815             format("a << b << \"line line line\";",
8816                    getLLVMStyleWithColumns(20)));
8817   // In a chain of << with three operands, the second can be broken with no line
8818   // break before it and the third must be broken with a line break before it.
8819   EXPECT_EQ("abcd << \"line line \"\n"
8820             "        \"line\"\n"
8821             "     << \"line line \"\n"
8822             "        \"line\";",
8823             format("abcd << \"line line line\" << \"line line line\";",
8824                    getLLVMStyleWithColumns(20)));
8825   // In a chain of binary operators with two operands, the second can be broken
8826   // with no line break before it.
8827   EXPECT_EQ("abcd + \"line line \"\n"
8828             "       \"line line\";",
8829             format("abcd + \"line line line line\";",
8830                    getLLVMStyleWithColumns(20)));
8831   // In a chain of binary operators with three operands, the second must be
8832   // broken with a line break before it.
8833   EXPECT_EQ("abcd +\n"
8834             "    \"line line \"\n"
8835             "    \"line line\" +\n"
8836             "    e;",
8837             format("abcd + \"line line line line\" + e;",
8838                    getLLVMStyleWithColumns(20)));
8839   // In a function call with two operands, with AlignAfterOpenBracket enabled,
8840   // the first must be broken with a line break before it.
8841   FormatStyle Style = getLLVMStyleWithColumns(25);
8842   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8843   EXPECT_EQ("someFunction(\n"
8844             "    \"long long long \"\n"
8845             "    \"long\",\n"
8846             "    a);",
8847             format("someFunction(\"long long long long\", a);", Style));
8848 }
8849 
8850 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
8851   EXPECT_EQ(
8852       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8853       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8854       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
8855       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8856              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8857              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
8858 }
8859 
8860 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
8861   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
8862             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
8863   EXPECT_EQ("fffffffffff(g(R\"x(\n"
8864             "multiline raw string literal xxxxxxxxxxxxxx\n"
8865             ")x\",\n"
8866             "              a),\n"
8867             "            b);",
8868             format("fffffffffff(g(R\"x(\n"
8869                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8870                    ")x\", a), b);",
8871                    getGoogleStyleWithColumns(20)));
8872   EXPECT_EQ("fffffffffff(\n"
8873             "    g(R\"x(qqq\n"
8874             "multiline raw string literal xxxxxxxxxxxxxx\n"
8875             ")x\",\n"
8876             "      a),\n"
8877             "    b);",
8878             format("fffffffffff(g(R\"x(qqq\n"
8879                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8880                    ")x\", a), b);",
8881                    getGoogleStyleWithColumns(20)));
8882 
8883   EXPECT_EQ("fffffffffff(R\"x(\n"
8884             "multiline raw string literal xxxxxxxxxxxxxx\n"
8885             ")x\");",
8886             format("fffffffffff(R\"x(\n"
8887                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8888                    ")x\");",
8889                    getGoogleStyleWithColumns(20)));
8890   EXPECT_EQ("fffffffffff(R\"x(\n"
8891             "multiline raw string literal xxxxxxxxxxxxxx\n"
8892             ")x\" + bbbbbb);",
8893             format("fffffffffff(R\"x(\n"
8894                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8895                    ")x\" +   bbbbbb);",
8896                    getGoogleStyleWithColumns(20)));
8897   EXPECT_EQ("fffffffffff(\n"
8898             "    R\"x(\n"
8899             "multiline raw string literal xxxxxxxxxxxxxx\n"
8900             ")x\" +\n"
8901             "    bbbbbb);",
8902             format("fffffffffff(\n"
8903                    " R\"x(\n"
8904                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8905                    ")x\" + bbbbbb);",
8906                    getGoogleStyleWithColumns(20)));
8907   EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
8908             format("fffffffffff(\n"
8909                    " R\"(single line raw string)\" + bbbbbb);"));
8910 }
8911 
8912 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
8913   verifyFormat("string a = \"unterminated;");
8914   EXPECT_EQ("function(\"unterminated,\n"
8915             "         OtherParameter);",
8916             format("function(  \"unterminated,\n"
8917                    "    OtherParameter);"));
8918 }
8919 
8920 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
8921   FormatStyle Style = getLLVMStyle();
8922   Style.Standard = FormatStyle::LS_Cpp03;
8923   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
8924             format("#define x(_a) printf(\"foo\"_a);", Style));
8925 }
8926 
8927 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
8928 
8929 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
8930   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
8931             "             \"ddeeefff\");",
8932             format("someFunction(\"aaabbbcccdddeeefff\");",
8933                    getLLVMStyleWithColumns(25)));
8934   EXPECT_EQ("someFunction1234567890(\n"
8935             "    \"aaabbbcccdddeeefff\");",
8936             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8937                    getLLVMStyleWithColumns(26)));
8938   EXPECT_EQ("someFunction1234567890(\n"
8939             "    \"aaabbbcccdddeeeff\"\n"
8940             "    \"f\");",
8941             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8942                    getLLVMStyleWithColumns(25)));
8943   EXPECT_EQ("someFunction1234567890(\n"
8944             "    \"aaabbbcccdddeeeff\"\n"
8945             "    \"f\");",
8946             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8947                    getLLVMStyleWithColumns(24)));
8948   EXPECT_EQ("someFunction(\n"
8949             "    \"aaabbbcc ddde \"\n"
8950             "    \"efff\");",
8951             format("someFunction(\"aaabbbcc ddde efff\");",
8952                    getLLVMStyleWithColumns(25)));
8953   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
8954             "             \"ddeeefff\");",
8955             format("someFunction(\"aaabbbccc ddeeefff\");",
8956                    getLLVMStyleWithColumns(25)));
8957   EXPECT_EQ("someFunction1234567890(\n"
8958             "    \"aaabb \"\n"
8959             "    \"cccdddeeefff\");",
8960             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
8961                    getLLVMStyleWithColumns(25)));
8962   EXPECT_EQ("#define A          \\\n"
8963             "  string s =       \\\n"
8964             "      \"123456789\"  \\\n"
8965             "      \"0\";         \\\n"
8966             "  int i;",
8967             format("#define A string s = \"1234567890\"; int i;",
8968                    getLLVMStyleWithColumns(20)));
8969   EXPECT_EQ("someFunction(\n"
8970             "    \"aaabbbcc \"\n"
8971             "    \"dddeeefff\");",
8972             format("someFunction(\"aaabbbcc dddeeefff\");",
8973                    getLLVMStyleWithColumns(25)));
8974 }
8975 
8976 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
8977   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
8978   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
8979   EXPECT_EQ("\"test\"\n"
8980             "\"\\n\"",
8981             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
8982   EXPECT_EQ("\"tes\\\\\"\n"
8983             "\"n\"",
8984             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
8985   EXPECT_EQ("\"\\\\\\\\\"\n"
8986             "\"\\n\"",
8987             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
8988   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
8989   EXPECT_EQ("\"\\uff01\"\n"
8990             "\"test\"",
8991             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
8992   EXPECT_EQ("\"\\Uff01ff02\"",
8993             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
8994   EXPECT_EQ("\"\\x000000000001\"\n"
8995             "\"next\"",
8996             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
8997   EXPECT_EQ("\"\\x000000000001next\"",
8998             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
8999   EXPECT_EQ("\"\\x000000000001\"",
9000             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
9001   EXPECT_EQ("\"test\"\n"
9002             "\"\\000000\"\n"
9003             "\"000001\"",
9004             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
9005   EXPECT_EQ("\"test\\000\"\n"
9006             "\"00000000\"\n"
9007             "\"1\"",
9008             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
9009 }
9010 
9011 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
9012   verifyFormat("void f() {\n"
9013                "  return g() {}\n"
9014                "  void h() {}");
9015   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
9016                "g();\n"
9017                "}");
9018 }
9019 
9020 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
9021   verifyFormat(
9022       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
9023 }
9024 
9025 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
9026   verifyFormat("class X {\n"
9027                "  void f() {\n"
9028                "  }\n"
9029                "};",
9030                getLLVMStyleWithColumns(12));
9031 }
9032 
9033 TEST_F(FormatTest, ConfigurableIndentWidth) {
9034   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
9035   EightIndent.IndentWidth = 8;
9036   EightIndent.ContinuationIndentWidth = 8;
9037   verifyFormat("void f() {\n"
9038                "        someFunction();\n"
9039                "        if (true) {\n"
9040                "                f();\n"
9041                "        }\n"
9042                "}",
9043                EightIndent);
9044   verifyFormat("class X {\n"
9045                "        void f() {\n"
9046                "        }\n"
9047                "};",
9048                EightIndent);
9049   verifyFormat("int x[] = {\n"
9050                "        call(),\n"
9051                "        call()};",
9052                EightIndent);
9053 }
9054 
9055 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
9056   verifyFormat("double\n"
9057                "f();",
9058                getLLVMStyleWithColumns(8));
9059 }
9060 
9061 TEST_F(FormatTest, ConfigurableUseOfTab) {
9062   FormatStyle Tab = getLLVMStyleWithColumns(42);
9063   Tab.IndentWidth = 8;
9064   Tab.UseTab = FormatStyle::UT_Always;
9065   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
9066 
9067   EXPECT_EQ("if (aaaaaaaa && // q\n"
9068             "    bb)\t\t// w\n"
9069             "\t;",
9070             format("if (aaaaaaaa &&// q\n"
9071                    "bb)// w\n"
9072                    ";",
9073                    Tab));
9074   EXPECT_EQ("if (aaa && bbb) // w\n"
9075             "\t;",
9076             format("if(aaa&&bbb)// w\n"
9077                    ";",
9078                    Tab));
9079 
9080   verifyFormat("class X {\n"
9081                "\tvoid f() {\n"
9082                "\t\tsomeFunction(parameter1,\n"
9083                "\t\t\t     parameter2);\n"
9084                "\t}\n"
9085                "};",
9086                Tab);
9087   verifyFormat("#define A                        \\\n"
9088                "\tvoid f() {               \\\n"
9089                "\t\tsomeFunction(    \\\n"
9090                "\t\t    parameter1,  \\\n"
9091                "\t\t    parameter2); \\\n"
9092                "\t}",
9093                Tab);
9094   verifyFormat("int a;\t      // x\n"
9095                "int bbbbbbbb; // x\n",
9096                Tab);
9097 
9098   Tab.TabWidth = 4;
9099   Tab.IndentWidth = 8;
9100   verifyFormat("class TabWidth4Indent8 {\n"
9101                "\t\tvoid f() {\n"
9102                "\t\t\t\tsomeFunction(parameter1,\n"
9103                "\t\t\t\t\t\t\t parameter2);\n"
9104                "\t\t}\n"
9105                "};",
9106                Tab);
9107 
9108   Tab.TabWidth = 4;
9109   Tab.IndentWidth = 4;
9110   verifyFormat("class TabWidth4Indent4 {\n"
9111                "\tvoid f() {\n"
9112                "\t\tsomeFunction(parameter1,\n"
9113                "\t\t\t\t\t parameter2);\n"
9114                "\t}\n"
9115                "};",
9116                Tab);
9117 
9118   Tab.TabWidth = 8;
9119   Tab.IndentWidth = 4;
9120   verifyFormat("class TabWidth8Indent4 {\n"
9121                "    void f() {\n"
9122                "\tsomeFunction(parameter1,\n"
9123                "\t\t     parameter2);\n"
9124                "    }\n"
9125                "};",
9126                Tab);
9127 
9128   Tab.TabWidth = 8;
9129   Tab.IndentWidth = 8;
9130   EXPECT_EQ("/*\n"
9131             "\t      a\t\tcomment\n"
9132             "\t      in multiple lines\n"
9133             "       */",
9134             format("   /*\t \t \n"
9135                    " \t \t a\t\tcomment\t \t\n"
9136                    " \t \t in multiple lines\t\n"
9137                    " \t  */",
9138                    Tab));
9139 
9140   Tab.UseTab = FormatStyle::UT_ForIndentation;
9141   verifyFormat("{\n"
9142                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9143                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9144                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9145                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9146                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9147                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9148                "};",
9149                Tab);
9150   verifyFormat("enum AA {\n"
9151                "\ta1, // Force multiple lines\n"
9152                "\ta2,\n"
9153                "\ta3\n"
9154                "};",
9155                Tab);
9156   EXPECT_EQ("if (aaaaaaaa && // q\n"
9157             "    bb)         // w\n"
9158             "\t;",
9159             format("if (aaaaaaaa &&// q\n"
9160                    "bb)// w\n"
9161                    ";",
9162                    Tab));
9163   verifyFormat("class X {\n"
9164                "\tvoid f() {\n"
9165                "\t\tsomeFunction(parameter1,\n"
9166                "\t\t             parameter2);\n"
9167                "\t}\n"
9168                "};",
9169                Tab);
9170   verifyFormat("{\n"
9171                "\tQ(\n"
9172                "\t    {\n"
9173                "\t\t    int a;\n"
9174                "\t\t    someFunction(aaaaaaaa,\n"
9175                "\t\t                 bbbbbbb);\n"
9176                "\t    },\n"
9177                "\t    p);\n"
9178                "}",
9179                Tab);
9180   EXPECT_EQ("{\n"
9181             "\t/* aaaa\n"
9182             "\t   bbbb */\n"
9183             "}",
9184             format("{\n"
9185                    "/* aaaa\n"
9186                    "   bbbb */\n"
9187                    "}",
9188                    Tab));
9189   EXPECT_EQ("{\n"
9190             "\t/*\n"
9191             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9192             "\t  bbbbbbbbbbbbb\n"
9193             "\t*/\n"
9194             "}",
9195             format("{\n"
9196                    "/*\n"
9197                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
9198                    "*/\n"
9199                    "}",
9200                    Tab));
9201   EXPECT_EQ("{\n"
9202             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9203             "\t// bbbbbbbbbbbbb\n"
9204             "}",
9205             format("{\n"
9206                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
9207                    "}",
9208                    Tab));
9209   EXPECT_EQ("{\n"
9210             "\t/*\n"
9211             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9212             "\t  bbbbbbbbbbbbb\n"
9213             "\t*/\n"
9214             "}",
9215             format("{\n"
9216                    "\t/*\n"
9217                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
9218                    "\t*/\n"
9219                    "}",
9220                    Tab));
9221   EXPECT_EQ("{\n"
9222             "\t/*\n"
9223             "\n"
9224             "\t*/\n"
9225             "}",
9226             format("{\n"
9227                    "\t/*\n"
9228                    "\n"
9229                    "\t*/\n"
9230                    "}",
9231                    Tab));
9232   EXPECT_EQ("{\n"
9233             "\t/*\n"
9234             " asdf\n"
9235             "\t*/\n"
9236             "}",
9237             format("{\n"
9238                    "\t/*\n"
9239                    " asdf\n"
9240                    "\t*/\n"
9241                    "}",
9242                    Tab));
9243 
9244   Tab.UseTab = FormatStyle::UT_Never;
9245   EXPECT_EQ("/*\n"
9246             "              a\t\tcomment\n"
9247             "              in multiple lines\n"
9248             "       */",
9249             format("   /*\t \t \n"
9250                    " \t \t a\t\tcomment\t \t\n"
9251                    " \t \t in multiple lines\t\n"
9252                    " \t  */",
9253                    Tab));
9254   EXPECT_EQ("/* some\n"
9255             "   comment */",
9256             format(" \t \t /* some\n"
9257                    " \t \t    comment */",
9258                    Tab));
9259   EXPECT_EQ("int a; /* some\n"
9260             "   comment */",
9261             format(" \t \t int a; /* some\n"
9262                    " \t \t    comment */",
9263                    Tab));
9264 
9265   EXPECT_EQ("int a; /* some\n"
9266             "comment */",
9267             format(" \t \t int\ta; /* some\n"
9268                    " \t \t    comment */",
9269                    Tab));
9270   EXPECT_EQ("f(\"\t\t\"); /* some\n"
9271             "    comment */",
9272             format(" \t \t f(\"\t\t\"); /* some\n"
9273                    " \t \t    comment */",
9274                    Tab));
9275   EXPECT_EQ("{\n"
9276             "  /*\n"
9277             "   * Comment\n"
9278             "   */\n"
9279             "  int i;\n"
9280             "}",
9281             format("{\n"
9282                    "\t/*\n"
9283                    "\t * Comment\n"
9284                    "\t */\n"
9285                    "\t int i;\n"
9286                    "}"));
9287 
9288   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
9289   Tab.TabWidth = 8;
9290   Tab.IndentWidth = 8;
9291   EXPECT_EQ("if (aaaaaaaa && // q\n"
9292             "    bb)         // w\n"
9293             "\t;",
9294             format("if (aaaaaaaa &&// q\n"
9295                    "bb)// w\n"
9296                    ";",
9297                    Tab));
9298   EXPECT_EQ("if (aaa && bbb) // w\n"
9299             "\t;",
9300             format("if(aaa&&bbb)// w\n"
9301                    ";",
9302                    Tab));
9303   verifyFormat("class X {\n"
9304                "\tvoid f() {\n"
9305                "\t\tsomeFunction(parameter1,\n"
9306                "\t\t\t     parameter2);\n"
9307                "\t}\n"
9308                "};",
9309                Tab);
9310   verifyFormat("#define A                        \\\n"
9311                "\tvoid f() {               \\\n"
9312                "\t\tsomeFunction(    \\\n"
9313                "\t\t    parameter1,  \\\n"
9314                "\t\t    parameter2); \\\n"
9315                "\t}",
9316                Tab);
9317   Tab.TabWidth = 4;
9318   Tab.IndentWidth = 8;
9319   verifyFormat("class TabWidth4Indent8 {\n"
9320                "\t\tvoid f() {\n"
9321                "\t\t\t\tsomeFunction(parameter1,\n"
9322                "\t\t\t\t\t\t\t parameter2);\n"
9323                "\t\t}\n"
9324                "};",
9325                Tab);
9326   Tab.TabWidth = 4;
9327   Tab.IndentWidth = 4;
9328   verifyFormat("class TabWidth4Indent4 {\n"
9329                "\tvoid f() {\n"
9330                "\t\tsomeFunction(parameter1,\n"
9331                "\t\t\t\t\t parameter2);\n"
9332                "\t}\n"
9333                "};",
9334                Tab);
9335   Tab.TabWidth = 8;
9336   Tab.IndentWidth = 4;
9337   verifyFormat("class TabWidth8Indent4 {\n"
9338                "    void f() {\n"
9339                "\tsomeFunction(parameter1,\n"
9340                "\t\t     parameter2);\n"
9341                "    }\n"
9342                "};",
9343                Tab);
9344   Tab.TabWidth = 8;
9345   Tab.IndentWidth = 8;
9346   EXPECT_EQ("/*\n"
9347             "\t      a\t\tcomment\n"
9348             "\t      in multiple lines\n"
9349             "       */",
9350             format("   /*\t \t \n"
9351                    " \t \t a\t\tcomment\t \t\n"
9352                    " \t \t in multiple lines\t\n"
9353                    " \t  */",
9354                    Tab));
9355   verifyFormat("{\n"
9356                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9357                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9358                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9359                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9360                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9361                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9362                "};",
9363                Tab);
9364   verifyFormat("enum AA {\n"
9365                "\ta1, // Force multiple lines\n"
9366                "\ta2,\n"
9367                "\ta3\n"
9368                "};",
9369                Tab);
9370   EXPECT_EQ("if (aaaaaaaa && // q\n"
9371             "    bb)         // w\n"
9372             "\t;",
9373             format("if (aaaaaaaa &&// q\n"
9374                    "bb)// w\n"
9375                    ";",
9376                    Tab));
9377   verifyFormat("class X {\n"
9378                "\tvoid f() {\n"
9379                "\t\tsomeFunction(parameter1,\n"
9380                "\t\t\t     parameter2);\n"
9381                "\t}\n"
9382                "};",
9383                Tab);
9384   verifyFormat("{\n"
9385                "\tQ(\n"
9386                "\t    {\n"
9387                "\t\t    int a;\n"
9388                "\t\t    someFunction(aaaaaaaa,\n"
9389                "\t\t\t\t bbbbbbb);\n"
9390                "\t    },\n"
9391                "\t    p);\n"
9392                "}",
9393                Tab);
9394   EXPECT_EQ("{\n"
9395             "\t/* aaaa\n"
9396             "\t   bbbb */\n"
9397             "}",
9398             format("{\n"
9399                    "/* aaaa\n"
9400                    "   bbbb */\n"
9401                    "}",
9402                    Tab));
9403   EXPECT_EQ("{\n"
9404             "\t/*\n"
9405             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9406             "\t  bbbbbbbbbbbbb\n"
9407             "\t*/\n"
9408             "}",
9409             format("{\n"
9410                    "/*\n"
9411                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
9412                    "*/\n"
9413                    "}",
9414                    Tab));
9415   EXPECT_EQ("{\n"
9416             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9417             "\t// bbbbbbbbbbbbb\n"
9418             "}",
9419             format("{\n"
9420                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
9421                    "}",
9422                    Tab));
9423   EXPECT_EQ("{\n"
9424             "\t/*\n"
9425             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9426             "\t  bbbbbbbbbbbbb\n"
9427             "\t*/\n"
9428             "}",
9429             format("{\n"
9430                    "\t/*\n"
9431                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
9432                    "\t*/\n"
9433                    "}",
9434                    Tab));
9435   EXPECT_EQ("{\n"
9436             "\t/*\n"
9437             "\n"
9438             "\t*/\n"
9439             "}",
9440             format("{\n"
9441                    "\t/*\n"
9442                    "\n"
9443                    "\t*/\n"
9444                    "}",
9445                    Tab));
9446   EXPECT_EQ("{\n"
9447             "\t/*\n"
9448             " asdf\n"
9449             "\t*/\n"
9450             "}",
9451             format("{\n"
9452                    "\t/*\n"
9453                    " asdf\n"
9454                    "\t*/\n"
9455                    "}",
9456                    Tab));
9457   EXPECT_EQ("/*\n"
9458             "\t      a\t\tcomment\n"
9459             "\t      in multiple lines\n"
9460             "       */",
9461             format("   /*\t \t \n"
9462                    " \t \t a\t\tcomment\t \t\n"
9463                    " \t \t in multiple lines\t\n"
9464                    " \t  */",
9465                    Tab));
9466   EXPECT_EQ("/* some\n"
9467             "   comment */",
9468             format(" \t \t /* some\n"
9469                    " \t \t    comment */",
9470                    Tab));
9471   EXPECT_EQ("int a; /* some\n"
9472             "   comment */",
9473             format(" \t \t int a; /* some\n"
9474                    " \t \t    comment */",
9475                    Tab));
9476   EXPECT_EQ("int a; /* some\n"
9477             "comment */",
9478             format(" \t \t int\ta; /* some\n"
9479                    " \t \t    comment */",
9480                    Tab));
9481   EXPECT_EQ("f(\"\t\t\"); /* some\n"
9482             "    comment */",
9483             format(" \t \t f(\"\t\t\"); /* some\n"
9484                    " \t \t    comment */",
9485                    Tab));
9486   EXPECT_EQ("{\n"
9487             "  /*\n"
9488             "   * Comment\n"
9489             "   */\n"
9490             "  int i;\n"
9491             "}",
9492             format("{\n"
9493                    "\t/*\n"
9494                    "\t * Comment\n"
9495                    "\t */\n"
9496                    "\t int i;\n"
9497                    "}"));
9498   Tab.AlignConsecutiveAssignments = true;
9499   Tab.AlignConsecutiveDeclarations = true;
9500   Tab.TabWidth = 4;
9501   Tab.IndentWidth = 4;
9502   verifyFormat("class Assign {\n"
9503                "\tvoid f() {\n"
9504                "\t\tint         x      = 123;\n"
9505                "\t\tint         random = 4;\n"
9506                "\t\tstd::string alphabet =\n"
9507                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
9508                "\t}\n"
9509                "};",
9510                Tab);
9511 }
9512 
9513 TEST_F(FormatTest, CalculatesOriginalColumn) {
9514   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9515             "q\"; /* some\n"
9516             "       comment */",
9517             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9518                    "q\"; /* some\n"
9519                    "       comment */",
9520                    getLLVMStyle()));
9521   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
9522             "/* some\n"
9523             "   comment */",
9524             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
9525                    " /* some\n"
9526                    "    comment */",
9527                    getLLVMStyle()));
9528   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9529             "qqq\n"
9530             "/* some\n"
9531             "   comment */",
9532             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9533                    "qqq\n"
9534                    " /* some\n"
9535                    "    comment */",
9536                    getLLVMStyle()));
9537   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9538             "wwww; /* some\n"
9539             "         comment */",
9540             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9541                    "wwww; /* some\n"
9542                    "         comment */",
9543                    getLLVMStyle()));
9544 }
9545 
9546 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
9547   FormatStyle NoSpace = getLLVMStyle();
9548   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
9549 
9550   verifyFormat("while(true)\n"
9551                "  continue;",
9552                NoSpace);
9553   verifyFormat("for(;;)\n"
9554                "  continue;",
9555                NoSpace);
9556   verifyFormat("if(true)\n"
9557                "  f();\n"
9558                "else if(true)\n"
9559                "  f();",
9560                NoSpace);
9561   verifyFormat("do {\n"
9562                "  do_something();\n"
9563                "} while(something());",
9564                NoSpace);
9565   verifyFormat("switch(x) {\n"
9566                "default:\n"
9567                "  break;\n"
9568                "}",
9569                NoSpace);
9570   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
9571   verifyFormat("size_t x = sizeof(x);", NoSpace);
9572   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
9573   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
9574   verifyFormat("alignas(128) char a[128];", NoSpace);
9575   verifyFormat("size_t x = alignof(MyType);", NoSpace);
9576   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
9577   verifyFormat("int f() throw(Deprecated);", NoSpace);
9578   verifyFormat("typedef void (*cb)(int);", NoSpace);
9579   verifyFormat("T A::operator()();", NoSpace);
9580   verifyFormat("X A::operator++(T);", NoSpace);
9581   verifyFormat("auto lambda = []() { return 0; };", NoSpace);
9582 
9583   FormatStyle Space = getLLVMStyle();
9584   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
9585 
9586   verifyFormat("int f ();", Space);
9587   verifyFormat("void f (int a, T b) {\n"
9588                "  while (true)\n"
9589                "    continue;\n"
9590                "}",
9591                Space);
9592   verifyFormat("if (true)\n"
9593                "  f ();\n"
9594                "else if (true)\n"
9595                "  f ();",
9596                Space);
9597   verifyFormat("do {\n"
9598                "  do_something ();\n"
9599                "} while (something ());",
9600                Space);
9601   verifyFormat("switch (x) {\n"
9602                "default:\n"
9603                "  break;\n"
9604                "}",
9605                Space);
9606   verifyFormat("A::A () : a (1) {}", Space);
9607   verifyFormat("void f () __attribute__ ((asdf));", Space);
9608   verifyFormat("*(&a + 1);\n"
9609                "&((&a)[1]);\n"
9610                "a[(b + c) * d];\n"
9611                "(((a + 1) * 2) + 3) * 4;",
9612                Space);
9613   verifyFormat("#define A(x) x", Space);
9614   verifyFormat("#define A (x) x", Space);
9615   verifyFormat("#if defined(x)\n"
9616                "#endif",
9617                Space);
9618   verifyFormat("auto i = std::make_unique<int> (5);", Space);
9619   verifyFormat("size_t x = sizeof (x);", Space);
9620   verifyFormat("auto f (int x) -> decltype (x);", Space);
9621   verifyFormat("int f (T x) noexcept (x.create ());", Space);
9622   verifyFormat("alignas (128) char a[128];", Space);
9623   verifyFormat("size_t x = alignof (MyType);", Space);
9624   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
9625   verifyFormat("int f () throw (Deprecated);", Space);
9626   verifyFormat("typedef void (*cb) (int);", Space);
9627   verifyFormat("T A::operator() ();", Space);
9628   verifyFormat("X A::operator++ (T);", Space);
9629   verifyFormat("auto lambda = [] () { return 0; };", Space);
9630 }
9631 
9632 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
9633   FormatStyle Spaces = getLLVMStyle();
9634 
9635   Spaces.SpacesInParentheses = true;
9636   verifyFormat("do_something( ::globalVar );", Spaces);
9637   verifyFormat("call( x, y, z );", Spaces);
9638   verifyFormat("call();", Spaces);
9639   verifyFormat("std::function<void( int, int )> callback;", Spaces);
9640   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
9641                Spaces);
9642   verifyFormat("while ( (bool)1 )\n"
9643                "  continue;",
9644                Spaces);
9645   verifyFormat("for ( ;; )\n"
9646                "  continue;",
9647                Spaces);
9648   verifyFormat("if ( true )\n"
9649                "  f();\n"
9650                "else if ( true )\n"
9651                "  f();",
9652                Spaces);
9653   verifyFormat("do {\n"
9654                "  do_something( (int)i );\n"
9655                "} while ( something() );",
9656                Spaces);
9657   verifyFormat("switch ( x ) {\n"
9658                "default:\n"
9659                "  break;\n"
9660                "}",
9661                Spaces);
9662 
9663   Spaces.SpacesInParentheses = false;
9664   Spaces.SpacesInCStyleCastParentheses = true;
9665   verifyFormat("Type *A = ( Type * )P;", Spaces);
9666   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
9667   verifyFormat("x = ( int32 )y;", Spaces);
9668   verifyFormat("int a = ( int )(2.0f);", Spaces);
9669   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
9670   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
9671   verifyFormat("#define x (( int )-1)", Spaces);
9672 
9673   // Run the first set of tests again with:
9674   Spaces.SpacesInParentheses = false;
9675   Spaces.SpaceInEmptyParentheses = true;
9676   Spaces.SpacesInCStyleCastParentheses = true;
9677   verifyFormat("call(x, y, z);", Spaces);
9678   verifyFormat("call( );", Spaces);
9679   verifyFormat("std::function<void(int, int)> callback;", Spaces);
9680   verifyFormat("while (( bool )1)\n"
9681                "  continue;",
9682                Spaces);
9683   verifyFormat("for (;;)\n"
9684                "  continue;",
9685                Spaces);
9686   verifyFormat("if (true)\n"
9687                "  f( );\n"
9688                "else if (true)\n"
9689                "  f( );",
9690                Spaces);
9691   verifyFormat("do {\n"
9692                "  do_something(( int )i);\n"
9693                "} while (something( ));",
9694                Spaces);
9695   verifyFormat("switch (x) {\n"
9696                "default:\n"
9697                "  break;\n"
9698                "}",
9699                Spaces);
9700 
9701   // Run the first set of tests again with:
9702   Spaces.SpaceAfterCStyleCast = true;
9703   verifyFormat("call(x, y, z);", Spaces);
9704   verifyFormat("call( );", Spaces);
9705   verifyFormat("std::function<void(int, int)> callback;", Spaces);
9706   verifyFormat("while (( bool ) 1)\n"
9707                "  continue;",
9708                Spaces);
9709   verifyFormat("for (;;)\n"
9710                "  continue;",
9711                Spaces);
9712   verifyFormat("if (true)\n"
9713                "  f( );\n"
9714                "else if (true)\n"
9715                "  f( );",
9716                Spaces);
9717   verifyFormat("do {\n"
9718                "  do_something(( int ) i);\n"
9719                "} while (something( ));",
9720                Spaces);
9721   verifyFormat("switch (x) {\n"
9722                "default:\n"
9723                "  break;\n"
9724                "}",
9725                Spaces);
9726 
9727   // Run subset of tests again with:
9728   Spaces.SpacesInCStyleCastParentheses = false;
9729   Spaces.SpaceAfterCStyleCast = true;
9730   verifyFormat("while ((bool) 1)\n"
9731                "  continue;",
9732                Spaces);
9733   verifyFormat("do {\n"
9734                "  do_something((int) i);\n"
9735                "} while (something( ));",
9736                Spaces);
9737 }
9738 
9739 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
9740   verifyFormat("int a[5];");
9741   verifyFormat("a[3] += 42;");
9742 
9743   FormatStyle Spaces = getLLVMStyle();
9744   Spaces.SpacesInSquareBrackets = true;
9745   // Lambdas unchanged.
9746   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
9747   verifyFormat("return [i, args...] {};", Spaces);
9748 
9749   // Not lambdas.
9750   verifyFormat("int a[ 5 ];", Spaces);
9751   verifyFormat("a[ 3 ] += 42;", Spaces);
9752   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
9753   verifyFormat("double &operator[](int i) { return 0; }\n"
9754                "int i;",
9755                Spaces);
9756   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
9757   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
9758   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
9759 }
9760 
9761 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
9762   verifyFormat("int a = 5;");
9763   verifyFormat("a += 42;");
9764   verifyFormat("a or_eq 8;");
9765 
9766   FormatStyle Spaces = getLLVMStyle();
9767   Spaces.SpaceBeforeAssignmentOperators = false;
9768   verifyFormat("int a= 5;", Spaces);
9769   verifyFormat("a+= 42;", Spaces);
9770   verifyFormat("a or_eq 8;", Spaces);
9771 }
9772 
9773 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
9774   verifyFormat("class Foo : public Bar {};");
9775   verifyFormat("Foo::Foo() : foo(1) {}");
9776   verifyFormat("for (auto a : b) {\n}");
9777   verifyFormat("int x = a ? b : c;");
9778   verifyFormat("{\n"
9779                "label0:\n"
9780                "  int x = 0;\n"
9781                "}");
9782   verifyFormat("switch (x) {\n"
9783                "case 1:\n"
9784                "default:\n"
9785                "}");
9786 
9787   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
9788   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
9789   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
9790   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
9791   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
9792   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
9793   verifyFormat("{\n"
9794                "label1:\n"
9795                "  int x = 0;\n"
9796                "}",
9797                CtorInitializerStyle);
9798   verifyFormat("switch (x) {\n"
9799                "case 1:\n"
9800                "default:\n"
9801                "}",
9802                CtorInitializerStyle);
9803   CtorInitializerStyle.BreakConstructorInitializers =
9804       FormatStyle::BCIS_AfterColon;
9805   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
9806                "    aaaaaaaaaaaaaaaa(1),\n"
9807                "    bbbbbbbbbbbbbbbb(2) {}",
9808                CtorInitializerStyle);
9809   CtorInitializerStyle.BreakConstructorInitializers =
9810       FormatStyle::BCIS_BeforeComma;
9811   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
9812                "    : aaaaaaaaaaaaaaaa(1)\n"
9813                "    , bbbbbbbbbbbbbbbb(2) {}",
9814                CtorInitializerStyle);
9815   CtorInitializerStyle.BreakConstructorInitializers =
9816       FormatStyle::BCIS_BeforeColon;
9817   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
9818                "    : aaaaaaaaaaaaaaaa(1),\n"
9819                "      bbbbbbbbbbbbbbbb(2) {}",
9820                CtorInitializerStyle);
9821   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
9822   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
9823                ": aaaaaaaaaaaaaaaa(1),\n"
9824                "  bbbbbbbbbbbbbbbb(2) {}",
9825                CtorInitializerStyle);
9826 
9827   FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
9828   InheritanceStyle.SpaceBeforeInheritanceColon = false;
9829   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
9830   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
9831   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
9832   verifyFormat("int x = a ? b : c;", InheritanceStyle);
9833   verifyFormat("{\n"
9834                "label2:\n"
9835                "  int x = 0;\n"
9836                "}",
9837                InheritanceStyle);
9838   verifyFormat("switch (x) {\n"
9839                "case 1:\n"
9840                "default:\n"
9841                "}",
9842                InheritanceStyle);
9843   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
9844   verifyFormat("class Foooooooooooooooooooooo:\n"
9845                "    public aaaaaaaaaaaaaaaaaa,\n"
9846                "    public bbbbbbbbbbbbbbbbbb {\n"
9847                "}",
9848                InheritanceStyle);
9849   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
9850   verifyFormat("class Foooooooooooooooooooooo\n"
9851                "    : public aaaaaaaaaaaaaaaaaa\n"
9852                "    , public bbbbbbbbbbbbbbbbbb {\n"
9853                "}",
9854                InheritanceStyle);
9855   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
9856   verifyFormat("class Foooooooooooooooooooooo\n"
9857                "    : public aaaaaaaaaaaaaaaaaa,\n"
9858                "      public bbbbbbbbbbbbbbbbbb {\n"
9859                "}",
9860                InheritanceStyle);
9861   InheritanceStyle.ConstructorInitializerIndentWidth = 0;
9862   verifyFormat("class Foooooooooooooooooooooo\n"
9863                ": public aaaaaaaaaaaaaaaaaa,\n"
9864                "  public bbbbbbbbbbbbbbbbbb {}",
9865                InheritanceStyle);
9866 
9867   FormatStyle ForLoopStyle = getLLVMStyle();
9868   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
9869   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
9870   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
9871   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
9872   verifyFormat("int x = a ? b : c;", ForLoopStyle);
9873   verifyFormat("{\n"
9874                "label2:\n"
9875                "  int x = 0;\n"
9876                "}",
9877                ForLoopStyle);
9878   verifyFormat("switch (x) {\n"
9879                "case 1:\n"
9880                "default:\n"
9881                "}",
9882                ForLoopStyle);
9883 
9884   FormatStyle NoSpaceStyle = getLLVMStyle();
9885   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
9886   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
9887   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
9888   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
9889   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
9890   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
9891   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
9892   verifyFormat("{\n"
9893                "label3:\n"
9894                "  int x = 0;\n"
9895                "}",
9896                NoSpaceStyle);
9897   verifyFormat("switch (x) {\n"
9898                "case 1:\n"
9899                "default:\n"
9900                "}",
9901                NoSpaceStyle);
9902 }
9903 
9904 TEST_F(FormatTest, AlignConsecutiveAssignments) {
9905   FormatStyle Alignment = getLLVMStyle();
9906   Alignment.AlignConsecutiveAssignments = false;
9907   verifyFormat("int a = 5;\n"
9908                "int oneTwoThree = 123;",
9909                Alignment);
9910   verifyFormat("int a = 5;\n"
9911                "int oneTwoThree = 123;",
9912                Alignment);
9913 
9914   Alignment.AlignConsecutiveAssignments = true;
9915   verifyFormat("int a           = 5;\n"
9916                "int oneTwoThree = 123;",
9917                Alignment);
9918   verifyFormat("int a           = method();\n"
9919                "int oneTwoThree = 133;",
9920                Alignment);
9921   verifyFormat("a &= 5;\n"
9922                "bcd *= 5;\n"
9923                "ghtyf += 5;\n"
9924                "dvfvdb -= 5;\n"
9925                "a /= 5;\n"
9926                "vdsvsv %= 5;\n"
9927                "sfdbddfbdfbb ^= 5;\n"
9928                "dvsdsv |= 5;\n"
9929                "int dsvvdvsdvvv = 123;",
9930                Alignment);
9931   verifyFormat("int i = 1, j = 10;\n"
9932                "something = 2000;",
9933                Alignment);
9934   verifyFormat("something = 2000;\n"
9935                "int i = 1, j = 10;\n",
9936                Alignment);
9937   verifyFormat("something = 2000;\n"
9938                "another   = 911;\n"
9939                "int i = 1, j = 10;\n"
9940                "oneMore = 1;\n"
9941                "i       = 2;",
9942                Alignment);
9943   verifyFormat("int a   = 5;\n"
9944                "int one = 1;\n"
9945                "method();\n"
9946                "int oneTwoThree = 123;\n"
9947                "int oneTwo      = 12;",
9948                Alignment);
9949   verifyFormat("int oneTwoThree = 123;\n"
9950                "int oneTwo      = 12;\n"
9951                "method();\n",
9952                Alignment);
9953   verifyFormat("int oneTwoThree = 123; // comment\n"
9954                "int oneTwo      = 12;  // comment",
9955                Alignment);
9956   EXPECT_EQ("int a = 5;\n"
9957             "\n"
9958             "int oneTwoThree = 123;",
9959             format("int a       = 5;\n"
9960                    "\n"
9961                    "int oneTwoThree= 123;",
9962                    Alignment));
9963   EXPECT_EQ("int a   = 5;\n"
9964             "int one = 1;\n"
9965             "\n"
9966             "int oneTwoThree = 123;",
9967             format("int a = 5;\n"
9968                    "int one = 1;\n"
9969                    "\n"
9970                    "int oneTwoThree = 123;",
9971                    Alignment));
9972   EXPECT_EQ("int a   = 5;\n"
9973             "int one = 1;\n"
9974             "\n"
9975             "int oneTwoThree = 123;\n"
9976             "int oneTwo      = 12;",
9977             format("int a = 5;\n"
9978                    "int one = 1;\n"
9979                    "\n"
9980                    "int oneTwoThree = 123;\n"
9981                    "int oneTwo = 12;",
9982                    Alignment));
9983   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
9984   verifyFormat("#define A \\\n"
9985                "  int aaaa       = 12; \\\n"
9986                "  int b          = 23; \\\n"
9987                "  int ccc        = 234; \\\n"
9988                "  int dddddddddd = 2345;",
9989                Alignment);
9990   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
9991   verifyFormat("#define A               \\\n"
9992                "  int aaaa       = 12;  \\\n"
9993                "  int b          = 23;  \\\n"
9994                "  int ccc        = 234; \\\n"
9995                "  int dddddddddd = 2345;",
9996                Alignment);
9997   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
9998   verifyFormat("#define A                                                      "
9999                "                \\\n"
10000                "  int aaaa       = 12;                                         "
10001                "                \\\n"
10002                "  int b          = 23;                                         "
10003                "                \\\n"
10004                "  int ccc        = 234;                                        "
10005                "                \\\n"
10006                "  int dddddddddd = 2345;",
10007                Alignment);
10008   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
10009                "k = 4, int l = 5,\n"
10010                "                  int m = 6) {\n"
10011                "  int j      = 10;\n"
10012                "  otherThing = 1;\n"
10013                "}",
10014                Alignment);
10015   verifyFormat("void SomeFunction(int parameter = 0) {\n"
10016                "  int i   = 1;\n"
10017                "  int j   = 2;\n"
10018                "  int big = 10000;\n"
10019                "}",
10020                Alignment);
10021   verifyFormat("class C {\n"
10022                "public:\n"
10023                "  int i            = 1;\n"
10024                "  virtual void f() = 0;\n"
10025                "};",
10026                Alignment);
10027   verifyFormat("int i = 1;\n"
10028                "if (SomeType t = getSomething()) {\n"
10029                "}\n"
10030                "int j   = 2;\n"
10031                "int big = 10000;",
10032                Alignment);
10033   verifyFormat("int j = 7;\n"
10034                "for (int k = 0; k < N; ++k) {\n"
10035                "}\n"
10036                "int j   = 2;\n"
10037                "int big = 10000;\n"
10038                "}",
10039                Alignment);
10040   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
10041   verifyFormat("int i = 1;\n"
10042                "LooooooooooongType loooooooooooooooooooooongVariable\n"
10043                "    = someLooooooooooooooooongFunction();\n"
10044                "int j = 2;",
10045                Alignment);
10046   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
10047   verifyFormat("int i = 1;\n"
10048                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
10049                "    someLooooooooooooooooongFunction();\n"
10050                "int j = 2;",
10051                Alignment);
10052 
10053   verifyFormat("auto lambda = []() {\n"
10054                "  auto i = 0;\n"
10055                "  return 0;\n"
10056                "};\n"
10057                "int i  = 0;\n"
10058                "auto v = type{\n"
10059                "    i = 1,   //\n"
10060                "    (i = 2), //\n"
10061                "    i = 3    //\n"
10062                "};",
10063                Alignment);
10064 
10065   verifyFormat(
10066       "int i      = 1;\n"
10067       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
10068       "                          loooooooooooooooooooooongParameterB);\n"
10069       "int j      = 2;",
10070       Alignment);
10071 
10072   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
10073                "          typename B   = very_long_type_name_1,\n"
10074                "          typename T_2 = very_long_type_name_2>\n"
10075                "auto foo() {}\n",
10076                Alignment);
10077   verifyFormat("int a, b = 1;\n"
10078                "int c  = 2;\n"
10079                "int dd = 3;\n",
10080                Alignment);
10081   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
10082                "float b[1][] = {{3.f}};\n",
10083                Alignment);
10084   verifyFormat("for (int i = 0; i < 1; i++)\n"
10085                "  int x = 1;\n",
10086                Alignment);
10087   verifyFormat("for (i = 0; i < 1; i++)\n"
10088                "  x = 1;\n"
10089                "y = 1;\n",
10090                Alignment);
10091 }
10092 
10093 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
10094   FormatStyle Alignment = getLLVMStyle();
10095   Alignment.AlignConsecutiveDeclarations = false;
10096   verifyFormat("float const a = 5;\n"
10097                "int oneTwoThree = 123;",
10098                Alignment);
10099   verifyFormat("int a = 5;\n"
10100                "float const oneTwoThree = 123;",
10101                Alignment);
10102 
10103   Alignment.AlignConsecutiveDeclarations = true;
10104   verifyFormat("float const a = 5;\n"
10105                "int         oneTwoThree = 123;",
10106                Alignment);
10107   verifyFormat("int         a = method();\n"
10108                "float const oneTwoThree = 133;",
10109                Alignment);
10110   verifyFormat("int i = 1, j = 10;\n"
10111                "something = 2000;",
10112                Alignment);
10113   verifyFormat("something = 2000;\n"
10114                "int i = 1, j = 10;\n",
10115                Alignment);
10116   verifyFormat("float      something = 2000;\n"
10117                "double     another = 911;\n"
10118                "int        i = 1, j = 10;\n"
10119                "const int *oneMore = 1;\n"
10120                "unsigned   i = 2;",
10121                Alignment);
10122   verifyFormat("float a = 5;\n"
10123                "int   one = 1;\n"
10124                "method();\n"
10125                "const double       oneTwoThree = 123;\n"
10126                "const unsigned int oneTwo = 12;",
10127                Alignment);
10128   verifyFormat("int      oneTwoThree{0}; // comment\n"
10129                "unsigned oneTwo;         // comment",
10130                Alignment);
10131   EXPECT_EQ("float const a = 5;\n"
10132             "\n"
10133             "int oneTwoThree = 123;",
10134             format("float const   a = 5;\n"
10135                    "\n"
10136                    "int           oneTwoThree= 123;",
10137                    Alignment));
10138   EXPECT_EQ("float a = 5;\n"
10139             "int   one = 1;\n"
10140             "\n"
10141             "unsigned oneTwoThree = 123;",
10142             format("float    a = 5;\n"
10143                    "int      one = 1;\n"
10144                    "\n"
10145                    "unsigned oneTwoThree = 123;",
10146                    Alignment));
10147   EXPECT_EQ("float a = 5;\n"
10148             "int   one = 1;\n"
10149             "\n"
10150             "unsigned oneTwoThree = 123;\n"
10151             "int      oneTwo = 12;",
10152             format("float    a = 5;\n"
10153                    "int one = 1;\n"
10154                    "\n"
10155                    "unsigned oneTwoThree = 123;\n"
10156                    "int oneTwo = 12;",
10157                    Alignment));
10158   // Function prototype alignment
10159   verifyFormat("int    a();\n"
10160                "double b();",
10161                Alignment);
10162   verifyFormat("int    a(int x);\n"
10163                "double b();",
10164                Alignment);
10165   unsigned OldColumnLimit = Alignment.ColumnLimit;
10166   // We need to set ColumnLimit to zero, in order to stress nested alignments,
10167   // otherwise the function parameters will be re-flowed onto a single line.
10168   Alignment.ColumnLimit = 0;
10169   EXPECT_EQ("int    a(int   x,\n"
10170             "         float y);\n"
10171             "double b(int    x,\n"
10172             "         double y);",
10173             format("int a(int x,\n"
10174                    " float y);\n"
10175                    "double b(int x,\n"
10176                    " double y);",
10177                    Alignment));
10178   // This ensures that function parameters of function declarations are
10179   // correctly indented when their owning functions are indented.
10180   // The failure case here is for 'double y' to not be indented enough.
10181   EXPECT_EQ("double a(int x);\n"
10182             "int    b(int    y,\n"
10183             "         double z);",
10184             format("double a(int x);\n"
10185                    "int b(int y,\n"
10186                    " double z);",
10187                    Alignment));
10188   // Set ColumnLimit low so that we induce wrapping immediately after
10189   // the function name and opening paren.
10190   Alignment.ColumnLimit = 13;
10191   verifyFormat("int function(\n"
10192                "    int  x,\n"
10193                "    bool y);",
10194                Alignment);
10195   Alignment.ColumnLimit = OldColumnLimit;
10196   // Ensure function pointers don't screw up recursive alignment
10197   verifyFormat("int    a(int x, void (*fp)(int y));\n"
10198                "double b();",
10199                Alignment);
10200   Alignment.AlignConsecutiveAssignments = true;
10201   // Ensure recursive alignment is broken by function braces, so that the
10202   // "a = 1" does not align with subsequent assignments inside the function
10203   // body.
10204   verifyFormat("int func(int a = 1) {\n"
10205                "  int b  = 2;\n"
10206                "  int cc = 3;\n"
10207                "}",
10208                Alignment);
10209   verifyFormat("float      something = 2000;\n"
10210                "double     another   = 911;\n"
10211                "int        i = 1, j = 10;\n"
10212                "const int *oneMore = 1;\n"
10213                "unsigned   i       = 2;",
10214                Alignment);
10215   verifyFormat("int      oneTwoThree = {0}; // comment\n"
10216                "unsigned oneTwo      = 0;   // comment",
10217                Alignment);
10218   // Make sure that scope is correctly tracked, in the absence of braces
10219   verifyFormat("for (int i = 0; i < n; i++)\n"
10220                "  j = i;\n"
10221                "double x = 1;\n",
10222                Alignment);
10223   verifyFormat("if (int i = 0)\n"
10224                "  j = i;\n"
10225                "double x = 1;\n",
10226                Alignment);
10227   // Ensure operator[] and operator() are comprehended
10228   verifyFormat("struct test {\n"
10229                "  long long int foo();\n"
10230                "  int           operator[](int a);\n"
10231                "  double        bar();\n"
10232                "};\n",
10233                Alignment);
10234   verifyFormat("struct test {\n"
10235                "  long long int foo();\n"
10236                "  int           operator()(int a);\n"
10237                "  double        bar();\n"
10238                "};\n",
10239                Alignment);
10240   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
10241             "  int const i   = 1;\n"
10242             "  int *     j   = 2;\n"
10243             "  int       big = 10000;\n"
10244             "\n"
10245             "  unsigned oneTwoThree = 123;\n"
10246             "  int      oneTwo      = 12;\n"
10247             "  method();\n"
10248             "  float k  = 2;\n"
10249             "  int   ll = 10000;\n"
10250             "}",
10251             format("void SomeFunction(int parameter= 0) {\n"
10252                    " int const  i= 1;\n"
10253                    "  int *j=2;\n"
10254                    " int big  =  10000;\n"
10255                    "\n"
10256                    "unsigned oneTwoThree  =123;\n"
10257                    "int oneTwo = 12;\n"
10258                    "  method();\n"
10259                    "float k= 2;\n"
10260                    "int ll=10000;\n"
10261                    "}",
10262                    Alignment));
10263   Alignment.AlignConsecutiveAssignments = false;
10264   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
10265   verifyFormat("#define A \\\n"
10266                "  int       aaaa = 12; \\\n"
10267                "  float     b = 23; \\\n"
10268                "  const int ccc = 234; \\\n"
10269                "  unsigned  dddddddddd = 2345;",
10270                Alignment);
10271   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
10272   verifyFormat("#define A              \\\n"
10273                "  int       aaaa = 12; \\\n"
10274                "  float     b = 23;    \\\n"
10275                "  const int ccc = 234; \\\n"
10276                "  unsigned  dddddddddd = 2345;",
10277                Alignment);
10278   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
10279   Alignment.ColumnLimit = 30;
10280   verifyFormat("#define A                    \\\n"
10281                "  int       aaaa = 12;       \\\n"
10282                "  float     b = 23;          \\\n"
10283                "  const int ccc = 234;       \\\n"
10284                "  int       dddddddddd = 2345;",
10285                Alignment);
10286   Alignment.ColumnLimit = 80;
10287   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
10288                "k = 4, int l = 5,\n"
10289                "                  int m = 6) {\n"
10290                "  const int j = 10;\n"
10291                "  otherThing = 1;\n"
10292                "}",
10293                Alignment);
10294   verifyFormat("void SomeFunction(int parameter = 0) {\n"
10295                "  int const i = 1;\n"
10296                "  int *     j = 2;\n"
10297                "  int       big = 10000;\n"
10298                "}",
10299                Alignment);
10300   verifyFormat("class C {\n"
10301                "public:\n"
10302                "  int          i = 1;\n"
10303                "  virtual void f() = 0;\n"
10304                "};",
10305                Alignment);
10306   verifyFormat("float i = 1;\n"
10307                "if (SomeType t = getSomething()) {\n"
10308                "}\n"
10309                "const unsigned j = 2;\n"
10310                "int            big = 10000;",
10311                Alignment);
10312   verifyFormat("float j = 7;\n"
10313                "for (int k = 0; k < N; ++k) {\n"
10314                "}\n"
10315                "unsigned j = 2;\n"
10316                "int      big = 10000;\n"
10317                "}",
10318                Alignment);
10319   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
10320   verifyFormat("float              i = 1;\n"
10321                "LooooooooooongType loooooooooooooooooooooongVariable\n"
10322                "    = someLooooooooooooooooongFunction();\n"
10323                "int j = 2;",
10324                Alignment);
10325   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
10326   verifyFormat("int                i = 1;\n"
10327                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
10328                "    someLooooooooooooooooongFunction();\n"
10329                "int j = 2;",
10330                Alignment);
10331 
10332   Alignment.AlignConsecutiveAssignments = true;
10333   verifyFormat("auto lambda = []() {\n"
10334                "  auto  ii = 0;\n"
10335                "  float j  = 0;\n"
10336                "  return 0;\n"
10337                "};\n"
10338                "int   i  = 0;\n"
10339                "float i2 = 0;\n"
10340                "auto  v  = type{\n"
10341                "    i = 1,   //\n"
10342                "    (i = 2), //\n"
10343                "    i = 3    //\n"
10344                "};",
10345                Alignment);
10346   Alignment.AlignConsecutiveAssignments = false;
10347 
10348   verifyFormat(
10349       "int      i = 1;\n"
10350       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
10351       "                          loooooooooooooooooooooongParameterB);\n"
10352       "int      j = 2;",
10353       Alignment);
10354 
10355   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
10356   // We expect declarations and assignments to align, as long as it doesn't
10357   // exceed the column limit, starting a new alignment sequence whenever it
10358   // happens.
10359   Alignment.AlignConsecutiveAssignments = true;
10360   Alignment.ColumnLimit = 30;
10361   verifyFormat("float    ii              = 1;\n"
10362                "unsigned j               = 2;\n"
10363                "int someVerylongVariable = 1;\n"
10364                "AnotherLongType  ll = 123456;\n"
10365                "VeryVeryLongType k  = 2;\n"
10366                "int              myvar = 1;",
10367                Alignment);
10368   Alignment.ColumnLimit = 80;
10369   Alignment.AlignConsecutiveAssignments = false;
10370 
10371   verifyFormat(
10372       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
10373       "          typename LongType, typename B>\n"
10374       "auto foo() {}\n",
10375       Alignment);
10376   verifyFormat("float a, b = 1;\n"
10377                "int   c = 2;\n"
10378                "int   dd = 3;\n",
10379                Alignment);
10380   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
10381                "float b[1][] = {{3.f}};\n",
10382                Alignment);
10383   Alignment.AlignConsecutiveAssignments = true;
10384   verifyFormat("float a, b = 1;\n"
10385                "int   c  = 2;\n"
10386                "int   dd = 3;\n",
10387                Alignment);
10388   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
10389                "float b[1][] = {{3.f}};\n",
10390                Alignment);
10391   Alignment.AlignConsecutiveAssignments = false;
10392 
10393   Alignment.ColumnLimit = 30;
10394   Alignment.BinPackParameters = false;
10395   verifyFormat("void foo(float     a,\n"
10396                "         float     b,\n"
10397                "         int       c,\n"
10398                "         uint32_t *d) {\n"
10399                "  int *  e = 0;\n"
10400                "  float  f = 0;\n"
10401                "  double g = 0;\n"
10402                "}\n"
10403                "void bar(ino_t     a,\n"
10404                "         int       b,\n"
10405                "         uint32_t *c,\n"
10406                "         bool      d) {}\n",
10407                Alignment);
10408   Alignment.BinPackParameters = true;
10409   Alignment.ColumnLimit = 80;
10410 
10411   // Bug 33507
10412   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
10413   verifyFormat(
10414       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
10415       "  static const Version verVs2017;\n"
10416       "  return true;\n"
10417       "});\n",
10418       Alignment);
10419   Alignment.PointerAlignment = FormatStyle::PAS_Right;
10420 
10421   // See llvm.org/PR35641
10422   Alignment.AlignConsecutiveDeclarations = true;
10423   verifyFormat("int func() { //\n"
10424                "  int      b;\n"
10425                "  unsigned c;\n"
10426                "}",
10427                Alignment);
10428 }
10429 
10430 TEST_F(FormatTest, LinuxBraceBreaking) {
10431   FormatStyle LinuxBraceStyle = getLLVMStyle();
10432   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
10433   verifyFormat("namespace a\n"
10434                "{\n"
10435                "class A\n"
10436                "{\n"
10437                "  void f()\n"
10438                "  {\n"
10439                "    if (true) {\n"
10440                "      a();\n"
10441                "      b();\n"
10442                "    } else {\n"
10443                "      a();\n"
10444                "    }\n"
10445                "  }\n"
10446                "  void g() { return; }\n"
10447                "};\n"
10448                "struct B {\n"
10449                "  int x;\n"
10450                "};\n"
10451                "} // namespace a\n",
10452                LinuxBraceStyle);
10453   verifyFormat("enum X {\n"
10454                "  Y = 0,\n"
10455                "}\n",
10456                LinuxBraceStyle);
10457   verifyFormat("struct S {\n"
10458                "  int Type;\n"
10459                "  union {\n"
10460                "    int x;\n"
10461                "    double y;\n"
10462                "  } Value;\n"
10463                "  class C\n"
10464                "  {\n"
10465                "    MyFavoriteType Value;\n"
10466                "  } Class;\n"
10467                "}\n",
10468                LinuxBraceStyle);
10469 }
10470 
10471 TEST_F(FormatTest, MozillaBraceBreaking) {
10472   FormatStyle MozillaBraceStyle = getLLVMStyle();
10473   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
10474   MozillaBraceStyle.FixNamespaceComments = false;
10475   verifyFormat("namespace a {\n"
10476                "class A\n"
10477                "{\n"
10478                "  void f()\n"
10479                "  {\n"
10480                "    if (true) {\n"
10481                "      a();\n"
10482                "      b();\n"
10483                "    }\n"
10484                "  }\n"
10485                "  void g() { return; }\n"
10486                "};\n"
10487                "enum E\n"
10488                "{\n"
10489                "  A,\n"
10490                "  // foo\n"
10491                "  B,\n"
10492                "  C\n"
10493                "};\n"
10494                "struct B\n"
10495                "{\n"
10496                "  int x;\n"
10497                "};\n"
10498                "}\n",
10499                MozillaBraceStyle);
10500   verifyFormat("struct S\n"
10501                "{\n"
10502                "  int Type;\n"
10503                "  union\n"
10504                "  {\n"
10505                "    int x;\n"
10506                "    double y;\n"
10507                "  } Value;\n"
10508                "  class C\n"
10509                "  {\n"
10510                "    MyFavoriteType Value;\n"
10511                "  } Class;\n"
10512                "}\n",
10513                MozillaBraceStyle);
10514 }
10515 
10516 TEST_F(FormatTest, StroustrupBraceBreaking) {
10517   FormatStyle StroustrupBraceStyle = getLLVMStyle();
10518   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
10519   verifyFormat("namespace a {\n"
10520                "class A {\n"
10521                "  void f()\n"
10522                "  {\n"
10523                "    if (true) {\n"
10524                "      a();\n"
10525                "      b();\n"
10526                "    }\n"
10527                "  }\n"
10528                "  void g() { return; }\n"
10529                "};\n"
10530                "struct B {\n"
10531                "  int x;\n"
10532                "};\n"
10533                "} // namespace a\n",
10534                StroustrupBraceStyle);
10535 
10536   verifyFormat("void foo()\n"
10537                "{\n"
10538                "  if (a) {\n"
10539                "    a();\n"
10540                "  }\n"
10541                "  else {\n"
10542                "    b();\n"
10543                "  }\n"
10544                "}\n",
10545                StroustrupBraceStyle);
10546 
10547   verifyFormat("#ifdef _DEBUG\n"
10548                "int foo(int i = 0)\n"
10549                "#else\n"
10550                "int foo(int i = 5)\n"
10551                "#endif\n"
10552                "{\n"
10553                "  return i;\n"
10554                "}",
10555                StroustrupBraceStyle);
10556 
10557   verifyFormat("void foo() {}\n"
10558                "void bar()\n"
10559                "#ifdef _DEBUG\n"
10560                "{\n"
10561                "  foo();\n"
10562                "}\n"
10563                "#else\n"
10564                "{\n"
10565                "}\n"
10566                "#endif",
10567                StroustrupBraceStyle);
10568 
10569   verifyFormat("void foobar() { int i = 5; }\n"
10570                "#ifdef _DEBUG\n"
10571                "void bar() {}\n"
10572                "#else\n"
10573                "void bar() { foobar(); }\n"
10574                "#endif",
10575                StroustrupBraceStyle);
10576 }
10577 
10578 TEST_F(FormatTest, AllmanBraceBreaking) {
10579   FormatStyle AllmanBraceStyle = getLLVMStyle();
10580   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
10581 
10582   EXPECT_EQ("namespace a\n"
10583             "{\n"
10584             "void f();\n"
10585             "void g();\n"
10586             "} // namespace a\n",
10587             format("namespace a\n"
10588                    "{\n"
10589                    "void f();\n"
10590                    "void g();\n"
10591                    "}\n",
10592                    AllmanBraceStyle));
10593 
10594   verifyFormat("namespace a\n"
10595                "{\n"
10596                "class A\n"
10597                "{\n"
10598                "  void f()\n"
10599                "  {\n"
10600                "    if (true)\n"
10601                "    {\n"
10602                "      a();\n"
10603                "      b();\n"
10604                "    }\n"
10605                "  }\n"
10606                "  void g() { return; }\n"
10607                "};\n"
10608                "struct B\n"
10609                "{\n"
10610                "  int x;\n"
10611                "};\n"
10612                "} // namespace a",
10613                AllmanBraceStyle);
10614 
10615   verifyFormat("void f()\n"
10616                "{\n"
10617                "  if (true)\n"
10618                "  {\n"
10619                "    a();\n"
10620                "  }\n"
10621                "  else if (false)\n"
10622                "  {\n"
10623                "    b();\n"
10624                "  }\n"
10625                "  else\n"
10626                "  {\n"
10627                "    c();\n"
10628                "  }\n"
10629                "}\n",
10630                AllmanBraceStyle);
10631 
10632   verifyFormat("void f()\n"
10633                "{\n"
10634                "  for (int i = 0; i < 10; ++i)\n"
10635                "  {\n"
10636                "    a();\n"
10637                "  }\n"
10638                "  while (false)\n"
10639                "  {\n"
10640                "    b();\n"
10641                "  }\n"
10642                "  do\n"
10643                "  {\n"
10644                "    c();\n"
10645                "  } while (false)\n"
10646                "}\n",
10647                AllmanBraceStyle);
10648 
10649   verifyFormat("void f(int a)\n"
10650                "{\n"
10651                "  switch (a)\n"
10652                "  {\n"
10653                "  case 0:\n"
10654                "    break;\n"
10655                "  case 1:\n"
10656                "  {\n"
10657                "    break;\n"
10658                "  }\n"
10659                "  case 2:\n"
10660                "  {\n"
10661                "  }\n"
10662                "  break;\n"
10663                "  default:\n"
10664                "    break;\n"
10665                "  }\n"
10666                "}\n",
10667                AllmanBraceStyle);
10668 
10669   verifyFormat("enum X\n"
10670                "{\n"
10671                "  Y = 0,\n"
10672                "}\n",
10673                AllmanBraceStyle);
10674   verifyFormat("enum X\n"
10675                "{\n"
10676                "  Y = 0\n"
10677                "}\n",
10678                AllmanBraceStyle);
10679 
10680   verifyFormat("@interface BSApplicationController ()\n"
10681                "{\n"
10682                "@private\n"
10683                "  id _extraIvar;\n"
10684                "}\n"
10685                "@end\n",
10686                AllmanBraceStyle);
10687 
10688   verifyFormat("#ifdef _DEBUG\n"
10689                "int foo(int i = 0)\n"
10690                "#else\n"
10691                "int foo(int i = 5)\n"
10692                "#endif\n"
10693                "{\n"
10694                "  return i;\n"
10695                "}",
10696                AllmanBraceStyle);
10697 
10698   verifyFormat("void foo() {}\n"
10699                "void bar()\n"
10700                "#ifdef _DEBUG\n"
10701                "{\n"
10702                "  foo();\n"
10703                "}\n"
10704                "#else\n"
10705                "{\n"
10706                "}\n"
10707                "#endif",
10708                AllmanBraceStyle);
10709 
10710   verifyFormat("void foobar() { int i = 5; }\n"
10711                "#ifdef _DEBUG\n"
10712                "void bar() {}\n"
10713                "#else\n"
10714                "void bar() { foobar(); }\n"
10715                "#endif",
10716                AllmanBraceStyle);
10717 
10718   // This shouldn't affect ObjC blocks..
10719   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
10720                "  // ...\n"
10721                "  int i;\n"
10722                "}];",
10723                AllmanBraceStyle);
10724   verifyFormat("void (^block)(void) = ^{\n"
10725                "  // ...\n"
10726                "  int i;\n"
10727                "};",
10728                AllmanBraceStyle);
10729   // .. or dict literals.
10730   verifyFormat("void f()\n"
10731                "{\n"
10732                "  // ...\n"
10733                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
10734                "}",
10735                AllmanBraceStyle);
10736   verifyFormat("void f()\n"
10737                "{\n"
10738                "  // ...\n"
10739                "  [object someMethod:@{a : @\"b\"}];\n"
10740                "}",
10741                AllmanBraceStyle);
10742   verifyFormat("int f()\n"
10743                "{ // comment\n"
10744                "  return 42;\n"
10745                "}",
10746                AllmanBraceStyle);
10747 
10748   AllmanBraceStyle.ColumnLimit = 19;
10749   verifyFormat("void f() { int i; }", AllmanBraceStyle);
10750   AllmanBraceStyle.ColumnLimit = 18;
10751   verifyFormat("void f()\n"
10752                "{\n"
10753                "  int i;\n"
10754                "}",
10755                AllmanBraceStyle);
10756   AllmanBraceStyle.ColumnLimit = 80;
10757 
10758   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
10759   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
10760       FormatStyle::SIS_WithoutElse;
10761   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
10762   verifyFormat("void f(bool b)\n"
10763                "{\n"
10764                "  if (b)\n"
10765                "  {\n"
10766                "    return;\n"
10767                "  }\n"
10768                "}\n",
10769                BreakBeforeBraceShortIfs);
10770   verifyFormat("void f(bool b)\n"
10771                "{\n"
10772                "  if constexpr (b)\n"
10773                "  {\n"
10774                "    return;\n"
10775                "  }\n"
10776                "}\n",
10777                BreakBeforeBraceShortIfs);
10778   verifyFormat("void f(bool b)\n"
10779                "{\n"
10780                "  if (b) return;\n"
10781                "}\n",
10782                BreakBeforeBraceShortIfs);
10783   verifyFormat("void f(bool b)\n"
10784                "{\n"
10785                "  if constexpr (b) return;\n"
10786                "}\n",
10787                BreakBeforeBraceShortIfs);
10788   verifyFormat("void f(bool b)\n"
10789                "{\n"
10790                "  while (b)\n"
10791                "  {\n"
10792                "    return;\n"
10793                "  }\n"
10794                "}\n",
10795                BreakBeforeBraceShortIfs);
10796 }
10797 
10798 TEST_F(FormatTest, GNUBraceBreaking) {
10799   FormatStyle GNUBraceStyle = getLLVMStyle();
10800   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
10801   verifyFormat("namespace a\n"
10802                "{\n"
10803                "class A\n"
10804                "{\n"
10805                "  void f()\n"
10806                "  {\n"
10807                "    int a;\n"
10808                "    {\n"
10809                "      int b;\n"
10810                "    }\n"
10811                "    if (true)\n"
10812                "      {\n"
10813                "        a();\n"
10814                "        b();\n"
10815                "      }\n"
10816                "  }\n"
10817                "  void g() { return; }\n"
10818                "}\n"
10819                "} // namespace a",
10820                GNUBraceStyle);
10821 
10822   verifyFormat("void f()\n"
10823                "{\n"
10824                "  if (true)\n"
10825                "    {\n"
10826                "      a();\n"
10827                "    }\n"
10828                "  else if (false)\n"
10829                "    {\n"
10830                "      b();\n"
10831                "    }\n"
10832                "  else\n"
10833                "    {\n"
10834                "      c();\n"
10835                "    }\n"
10836                "}\n",
10837                GNUBraceStyle);
10838 
10839   verifyFormat("void f()\n"
10840                "{\n"
10841                "  for (int i = 0; i < 10; ++i)\n"
10842                "    {\n"
10843                "      a();\n"
10844                "    }\n"
10845                "  while (false)\n"
10846                "    {\n"
10847                "      b();\n"
10848                "    }\n"
10849                "  do\n"
10850                "    {\n"
10851                "      c();\n"
10852                "    }\n"
10853                "  while (false);\n"
10854                "}\n",
10855                GNUBraceStyle);
10856 
10857   verifyFormat("void f(int a)\n"
10858                "{\n"
10859                "  switch (a)\n"
10860                "    {\n"
10861                "    case 0:\n"
10862                "      break;\n"
10863                "    case 1:\n"
10864                "      {\n"
10865                "        break;\n"
10866                "      }\n"
10867                "    case 2:\n"
10868                "      {\n"
10869                "      }\n"
10870                "      break;\n"
10871                "    default:\n"
10872                "      break;\n"
10873                "    }\n"
10874                "}\n",
10875                GNUBraceStyle);
10876 
10877   verifyFormat("enum X\n"
10878                "{\n"
10879                "  Y = 0,\n"
10880                "}\n",
10881                GNUBraceStyle);
10882 
10883   verifyFormat("@interface BSApplicationController ()\n"
10884                "{\n"
10885                "@private\n"
10886                "  id _extraIvar;\n"
10887                "}\n"
10888                "@end\n",
10889                GNUBraceStyle);
10890 
10891   verifyFormat("#ifdef _DEBUG\n"
10892                "int foo(int i = 0)\n"
10893                "#else\n"
10894                "int foo(int i = 5)\n"
10895                "#endif\n"
10896                "{\n"
10897                "  return i;\n"
10898                "}",
10899                GNUBraceStyle);
10900 
10901   verifyFormat("void foo() {}\n"
10902                "void bar()\n"
10903                "#ifdef _DEBUG\n"
10904                "{\n"
10905                "  foo();\n"
10906                "}\n"
10907                "#else\n"
10908                "{\n"
10909                "}\n"
10910                "#endif",
10911                GNUBraceStyle);
10912 
10913   verifyFormat("void foobar() { int i = 5; }\n"
10914                "#ifdef _DEBUG\n"
10915                "void bar() {}\n"
10916                "#else\n"
10917                "void bar() { foobar(); }\n"
10918                "#endif",
10919                GNUBraceStyle);
10920 }
10921 
10922 TEST_F(FormatTest, WebKitBraceBreaking) {
10923   FormatStyle WebKitBraceStyle = getLLVMStyle();
10924   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
10925   WebKitBraceStyle.FixNamespaceComments = false;
10926   verifyFormat("namespace a {\n"
10927                "class A {\n"
10928                "  void f()\n"
10929                "  {\n"
10930                "    if (true) {\n"
10931                "      a();\n"
10932                "      b();\n"
10933                "    }\n"
10934                "  }\n"
10935                "  void g() { return; }\n"
10936                "};\n"
10937                "enum E {\n"
10938                "  A,\n"
10939                "  // foo\n"
10940                "  B,\n"
10941                "  C\n"
10942                "};\n"
10943                "struct B {\n"
10944                "  int x;\n"
10945                "};\n"
10946                "}\n",
10947                WebKitBraceStyle);
10948   verifyFormat("struct S {\n"
10949                "  int Type;\n"
10950                "  union {\n"
10951                "    int x;\n"
10952                "    double y;\n"
10953                "  } Value;\n"
10954                "  class C {\n"
10955                "    MyFavoriteType Value;\n"
10956                "  } Class;\n"
10957                "};\n",
10958                WebKitBraceStyle);
10959 }
10960 
10961 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
10962   verifyFormat("void f() {\n"
10963                "  try {\n"
10964                "  } catch (const Exception &e) {\n"
10965                "  }\n"
10966                "}\n",
10967                getLLVMStyle());
10968 }
10969 
10970 TEST_F(FormatTest, UnderstandsPragmas) {
10971   verifyFormat("#pragma omp reduction(| : var)");
10972   verifyFormat("#pragma omp reduction(+ : var)");
10973 
10974   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
10975             "(including parentheses).",
10976             format("#pragma    mark   Any non-hyphenated or hyphenated string "
10977                    "(including parentheses)."));
10978 }
10979 
10980 TEST_F(FormatTest, UnderstandPragmaOption) {
10981   verifyFormat("#pragma option -C -A");
10982 
10983   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
10984 }
10985 
10986 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
10987   FormatStyle Style = getLLVMStyle();
10988   Style.ColumnLimit = 20;
10989 
10990   verifyFormat("int a; // the\n"
10991                "       // comment", Style);
10992   EXPECT_EQ("int a; /* first line\n"
10993             "        * second\n"
10994             "        * line third\n"
10995             "        * line\n"
10996             "        */",
10997             format("int a; /* first line\n"
10998                    "        * second\n"
10999                    "        * line third\n"
11000                    "        * line\n"
11001                    "        */",
11002                    Style));
11003   EXPECT_EQ("int a; // first line\n"
11004             "       // second\n"
11005             "       // line third\n"
11006             "       // line",
11007             format("int a; // first line\n"
11008                    "       // second line\n"
11009                    "       // third line",
11010                    Style));
11011 
11012   Style.PenaltyExcessCharacter = 90;
11013   verifyFormat("int a; // the comment", Style);
11014   EXPECT_EQ("int a; // the comment\n"
11015             "       // aaa",
11016             format("int a; // the comment aaa", Style));
11017   EXPECT_EQ("int a; /* first line\n"
11018             "        * second line\n"
11019             "        * third line\n"
11020             "        */",
11021             format("int a; /* first line\n"
11022                    "        * second line\n"
11023                    "        * third line\n"
11024                    "        */",
11025                    Style));
11026   EXPECT_EQ("int a; // first line\n"
11027             "       // second line\n"
11028             "       // third line",
11029             format("int a; // first line\n"
11030                    "       // second line\n"
11031                    "       // third line",
11032                    Style));
11033   // FIXME: Investigate why this is not getting the same layout as the test
11034   // above.
11035   EXPECT_EQ("int a; /* first line\n"
11036             "        * second line\n"
11037             "        * third line\n"
11038             "        */",
11039             format("int a; /* first line second line third line"
11040                    "\n*/",
11041                    Style));
11042 
11043   EXPECT_EQ("// foo bar baz bazfoo\n"
11044             "// foo bar foo bar\n",
11045             format("// foo bar baz bazfoo\n"
11046                    "// foo bar foo           bar\n",
11047                    Style));
11048   EXPECT_EQ("// foo bar baz bazfoo\n"
11049             "// foo bar foo bar\n",
11050             format("// foo bar baz      bazfoo\n"
11051                    "// foo            bar foo bar\n",
11052                    Style));
11053 
11054   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
11055   // next one.
11056   EXPECT_EQ("// foo bar baz bazfoo\n"
11057             "// bar foo bar\n",
11058             format("// foo bar baz      bazfoo bar\n"
11059                    "// foo            bar\n",
11060                    Style));
11061 
11062   EXPECT_EQ("// foo bar baz bazfoo\n"
11063             "// foo bar baz bazfoo\n"
11064             "// bar foo bar\n",
11065             format("// foo bar baz      bazfoo\n"
11066                    "// foo bar baz      bazfoo bar\n"
11067                    "// foo bar\n",
11068                    Style));
11069 
11070   EXPECT_EQ("// foo bar baz bazfoo\n"
11071             "// foo bar baz bazfoo\n"
11072             "// bar foo bar\n",
11073             format("// foo bar baz      bazfoo\n"
11074                    "// foo bar baz      bazfoo bar\n"
11075                    "// foo           bar\n",
11076                    Style));
11077 
11078   // Make sure we do not keep protruding characters if strict mode reflow is
11079   // cheaper than keeping protruding characters.
11080   Style.ColumnLimit = 21;
11081   EXPECT_EQ("// foo foo foo foo\n"
11082             "// foo foo foo foo\n"
11083             "// foo foo foo foo\n",
11084             format("// foo foo foo foo foo foo foo foo foo foo foo foo\n",
11085                            Style));
11086 
11087   EXPECT_EQ("int a = /* long block\n"
11088             "           comment */\n"
11089             "    42;",
11090             format("int a = /* long block comment */ 42;", Style));
11091 }
11092 
11093 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
11094   for (size_t i = 1; i < Styles.size(); ++i)                                   \
11095   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
11096                                   << " differs from Style #0"
11097 
11098 TEST_F(FormatTest, GetsPredefinedStyleByName) {
11099   SmallVector<FormatStyle, 3> Styles;
11100   Styles.resize(3);
11101 
11102   Styles[0] = getLLVMStyle();
11103   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
11104   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
11105   EXPECT_ALL_STYLES_EQUAL(Styles);
11106 
11107   Styles[0] = getGoogleStyle();
11108   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
11109   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
11110   EXPECT_ALL_STYLES_EQUAL(Styles);
11111 
11112   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
11113   EXPECT_TRUE(
11114       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
11115   EXPECT_TRUE(
11116       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
11117   EXPECT_ALL_STYLES_EQUAL(Styles);
11118 
11119   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
11120   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
11121   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
11122   EXPECT_ALL_STYLES_EQUAL(Styles);
11123 
11124   Styles[0] = getMozillaStyle();
11125   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
11126   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
11127   EXPECT_ALL_STYLES_EQUAL(Styles);
11128 
11129   Styles[0] = getWebKitStyle();
11130   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
11131   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
11132   EXPECT_ALL_STYLES_EQUAL(Styles);
11133 
11134   Styles[0] = getGNUStyle();
11135   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
11136   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
11137   EXPECT_ALL_STYLES_EQUAL(Styles);
11138 
11139   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
11140 }
11141 
11142 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
11143   SmallVector<FormatStyle, 8> Styles;
11144   Styles.resize(2);
11145 
11146   Styles[0] = getGoogleStyle();
11147   Styles[1] = getLLVMStyle();
11148   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
11149   EXPECT_ALL_STYLES_EQUAL(Styles);
11150 
11151   Styles.resize(5);
11152   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
11153   Styles[1] = getLLVMStyle();
11154   Styles[1].Language = FormatStyle::LK_JavaScript;
11155   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
11156 
11157   Styles[2] = getLLVMStyle();
11158   Styles[2].Language = FormatStyle::LK_JavaScript;
11159   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
11160                                   "BasedOnStyle: Google",
11161                                   &Styles[2])
11162                    .value());
11163 
11164   Styles[3] = getLLVMStyle();
11165   Styles[3].Language = FormatStyle::LK_JavaScript;
11166   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
11167                                   "Language: JavaScript",
11168                                   &Styles[3])
11169                    .value());
11170 
11171   Styles[4] = getLLVMStyle();
11172   Styles[4].Language = FormatStyle::LK_JavaScript;
11173   EXPECT_EQ(0, parseConfiguration("---\n"
11174                                   "BasedOnStyle: LLVM\n"
11175                                   "IndentWidth: 123\n"
11176                                   "---\n"
11177                                   "BasedOnStyle: Google\n"
11178                                   "Language: JavaScript",
11179                                   &Styles[4])
11180                    .value());
11181   EXPECT_ALL_STYLES_EQUAL(Styles);
11182 }
11183 
11184 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
11185   Style.FIELD = false;                                                         \
11186   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
11187   EXPECT_TRUE(Style.FIELD);                                                    \
11188   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
11189   EXPECT_FALSE(Style.FIELD);
11190 
11191 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
11192 
11193 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
11194   Style.STRUCT.FIELD = false;                                                  \
11195   EXPECT_EQ(0,                                                                 \
11196             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
11197                 .value());                                                     \
11198   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
11199   EXPECT_EQ(0,                                                                 \
11200             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
11201                 .value());                                                     \
11202   EXPECT_FALSE(Style.STRUCT.FIELD);
11203 
11204 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
11205   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
11206 
11207 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
11208   EXPECT_NE(VALUE, Style.FIELD);                                               \
11209   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
11210   EXPECT_EQ(VALUE, Style.FIELD)
11211 
11212 TEST_F(FormatTest, ParsesConfigurationBools) {
11213   FormatStyle Style = {};
11214   Style.Language = FormatStyle::LK_Cpp;
11215   CHECK_PARSE_BOOL(AlignOperands);
11216   CHECK_PARSE_BOOL(AlignTrailingComments);
11217   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
11218   CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
11219   CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine);
11220   CHECK_PARSE_BOOL(AllowAllConstructorInitializersOnNextLine);
11221   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
11222   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
11223   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
11224   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
11225   CHECK_PARSE_BOOL(BinPackArguments);
11226   CHECK_PARSE_BOOL(BinPackParameters);
11227   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
11228   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
11229   CHECK_PARSE_BOOL(BreakStringLiterals);
11230   CHECK_PARSE_BOOL(CompactNamespaces);
11231   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
11232   CHECK_PARSE_BOOL(DerivePointerAlignment);
11233   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
11234   CHECK_PARSE_BOOL(DisableFormat);
11235   CHECK_PARSE_BOOL(IndentCaseLabels);
11236   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
11237   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
11238   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
11239   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
11240   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
11241   CHECK_PARSE_BOOL(ReflowComments);
11242   CHECK_PARSE_BOOL(SortIncludes);
11243   CHECK_PARSE_BOOL(SortUsingDeclarations);
11244   CHECK_PARSE_BOOL(SpacesInParentheses);
11245   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
11246   CHECK_PARSE_BOOL(SpacesInAngles);
11247   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
11248   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
11249   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
11250   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
11251   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
11252   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
11253   CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
11254   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
11255   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
11256   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
11257 
11258   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
11259   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
11260   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
11261   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
11262   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
11263   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
11264   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
11265   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
11266   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
11267   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
11268   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
11269   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
11270   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
11271   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
11272   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
11273 }
11274 
11275 #undef CHECK_PARSE_BOOL
11276 
11277 TEST_F(FormatTest, ParsesConfiguration) {
11278   FormatStyle Style = {};
11279   Style.Language = FormatStyle::LK_Cpp;
11280   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
11281   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
11282               ConstructorInitializerIndentWidth, 1234u);
11283   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
11284   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
11285   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
11286   CHECK_PARSE("PenaltyBreakAssignment: 1234",
11287               PenaltyBreakAssignment, 1234u);
11288   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
11289               PenaltyBreakBeforeFirstCallParameter, 1234u);
11290   CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
11291               PenaltyBreakTemplateDeclaration, 1234u);
11292   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
11293   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
11294               PenaltyReturnTypeOnItsOwnLine, 1234u);
11295   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
11296               SpacesBeforeTrailingComments, 1234u);
11297   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
11298   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
11299   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
11300 
11301   Style.PointerAlignment = FormatStyle::PAS_Middle;
11302   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
11303               FormatStyle::PAS_Left);
11304   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
11305               FormatStyle::PAS_Right);
11306   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
11307               FormatStyle::PAS_Middle);
11308   // For backward compatibility:
11309   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
11310               FormatStyle::PAS_Left);
11311   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
11312               FormatStyle::PAS_Right);
11313   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
11314               FormatStyle::PAS_Middle);
11315 
11316   Style.Standard = FormatStyle::LS_Auto;
11317   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
11318   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
11319   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
11320   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
11321   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
11322 
11323   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
11324   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
11325               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
11326   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
11327               FormatStyle::BOS_None);
11328   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
11329               FormatStyle::BOS_All);
11330   // For backward compatibility:
11331   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
11332               FormatStyle::BOS_None);
11333   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
11334               FormatStyle::BOS_All);
11335 
11336   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
11337   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
11338               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
11339   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
11340               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
11341   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
11342               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
11343   // For backward compatibility:
11344   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
11345               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
11346 
11347   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
11348   CHECK_PARSE("BreakInheritanceList: BeforeComma",
11349               BreakInheritanceList, FormatStyle::BILS_BeforeComma);
11350   CHECK_PARSE("BreakInheritanceList: AfterColon",
11351               BreakInheritanceList, FormatStyle::BILS_AfterColon);
11352   CHECK_PARSE("BreakInheritanceList: BeforeColon",
11353               BreakInheritanceList, FormatStyle::BILS_BeforeColon);
11354   // For backward compatibility:
11355   CHECK_PARSE("BreakBeforeInheritanceComma: true",
11356               BreakInheritanceList, FormatStyle::BILS_BeforeComma);
11357 
11358   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11359   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
11360               FormatStyle::BAS_Align);
11361   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
11362               FormatStyle::BAS_DontAlign);
11363   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
11364               FormatStyle::BAS_AlwaysBreak);
11365   // For backward compatibility:
11366   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
11367               FormatStyle::BAS_DontAlign);
11368   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
11369               FormatStyle::BAS_Align);
11370 
11371   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
11372   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
11373               FormatStyle::ENAS_DontAlign);
11374   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
11375               FormatStyle::ENAS_Left);
11376   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
11377               FormatStyle::ENAS_Right);
11378   // For backward compatibility:
11379   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
11380               FormatStyle::ENAS_Left);
11381   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
11382               FormatStyle::ENAS_Right);
11383 
11384   Style.UseTab = FormatStyle::UT_ForIndentation;
11385   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
11386   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
11387   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
11388   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
11389               FormatStyle::UT_ForContinuationAndIndentation);
11390   // For backward compatibility:
11391   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
11392   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
11393 
11394   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
11395   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
11396               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
11397   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
11398               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
11399   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
11400               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
11401   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
11402               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
11403   // For backward compatibility:
11404   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
11405               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
11406   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
11407               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
11408 
11409   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
11410   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
11411               FormatStyle::SBPO_Never);
11412   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
11413               FormatStyle::SBPO_Always);
11414   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
11415               FormatStyle::SBPO_ControlStatements);
11416   // For backward compatibility:
11417   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
11418               FormatStyle::SBPO_Never);
11419   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
11420               FormatStyle::SBPO_ControlStatements);
11421 
11422   Style.ColumnLimit = 123;
11423   FormatStyle BaseStyle = getLLVMStyle();
11424   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
11425   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
11426 
11427   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
11428   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
11429               FormatStyle::BS_Attach);
11430   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
11431               FormatStyle::BS_Linux);
11432   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
11433               FormatStyle::BS_Mozilla);
11434   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
11435               FormatStyle::BS_Stroustrup);
11436   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
11437               FormatStyle::BS_Allman);
11438   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
11439   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
11440               FormatStyle::BS_WebKit);
11441   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
11442               FormatStyle::BS_Custom);
11443 
11444   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
11445   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
11446               FormatStyle::RTBS_None);
11447   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
11448               FormatStyle::RTBS_All);
11449   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
11450               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
11451   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
11452               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
11453   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
11454               AlwaysBreakAfterReturnType,
11455               FormatStyle::RTBS_TopLevelDefinitions);
11456 
11457   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
11458   CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", AlwaysBreakTemplateDeclarations,
11459               FormatStyle::BTDS_No);
11460   CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", AlwaysBreakTemplateDeclarations,
11461               FormatStyle::BTDS_MultiLine);
11462   CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", AlwaysBreakTemplateDeclarations,
11463               FormatStyle::BTDS_Yes);
11464   CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", AlwaysBreakTemplateDeclarations,
11465               FormatStyle::BTDS_MultiLine);
11466   CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", AlwaysBreakTemplateDeclarations,
11467               FormatStyle::BTDS_Yes);
11468 
11469   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
11470   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
11471               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
11472   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
11473               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
11474   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
11475               AlwaysBreakAfterDefinitionReturnType,
11476               FormatStyle::DRTBS_TopLevel);
11477 
11478   Style.NamespaceIndentation = FormatStyle::NI_All;
11479   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
11480               FormatStyle::NI_None);
11481   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
11482               FormatStyle::NI_Inner);
11483   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
11484               FormatStyle::NI_All);
11485 
11486   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Always;
11487   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never",
11488               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
11489   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse",
11490               AllowShortIfStatementsOnASingleLine,
11491               FormatStyle::SIS_WithoutElse);
11492   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always",
11493               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Always);
11494   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false",
11495               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
11496   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true",
11497               AllowShortIfStatementsOnASingleLine,
11498               FormatStyle::SIS_WithoutElse);
11499 
11500   // FIXME: This is required because parsing a configuration simply overwrites
11501   // the first N elements of the list instead of resetting it.
11502   Style.ForEachMacros.clear();
11503   std::vector<std::string> BoostForeach;
11504   BoostForeach.push_back("BOOST_FOREACH");
11505   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
11506   std::vector<std::string> BoostAndQForeach;
11507   BoostAndQForeach.push_back("BOOST_FOREACH");
11508   BoostAndQForeach.push_back("Q_FOREACH");
11509   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
11510               BoostAndQForeach);
11511 
11512   Style.StatementMacros.clear();
11513   CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
11514               std::vector<std::string>{"QUNUSED"});
11515   CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros,
11516               std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
11517 
11518   Style.IncludeStyle.IncludeCategories.clear();
11519   std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
11520       {"abc/.*", 2}, {".*", 1}};
11521   CHECK_PARSE("IncludeCategories:\n"
11522               "  - Regex: abc/.*\n"
11523               "    Priority: 2\n"
11524               "  - Regex: .*\n"
11525               "    Priority: 1",
11526               IncludeStyle.IncludeCategories, ExpectedCategories);
11527   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
11528               "abc$");
11529 
11530   Style.RawStringFormats.clear();
11531   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
11532       {
11533           FormatStyle::LK_TextProto,
11534           {"pb", "proto"},
11535           {"PARSE_TEXT_PROTO"},
11536           /*CanonicalDelimiter=*/"",
11537           "llvm",
11538       },
11539       {
11540           FormatStyle::LK_Cpp,
11541           {"cc", "cpp"},
11542           {"C_CODEBLOCK", "CPPEVAL"},
11543           /*CanonicalDelimiter=*/"cc",
11544           /*BasedOnStyle=*/"",
11545       },
11546   };
11547 
11548   CHECK_PARSE("RawStringFormats:\n"
11549               "  - Language: TextProto\n"
11550               "    Delimiters:\n"
11551               "      - 'pb'\n"
11552               "      - 'proto'\n"
11553               "    EnclosingFunctions:\n"
11554               "      - 'PARSE_TEXT_PROTO'\n"
11555               "    BasedOnStyle: llvm\n"
11556               "  - Language: Cpp\n"
11557               "    Delimiters:\n"
11558               "      - 'cc'\n"
11559               "      - 'cpp'\n"
11560               "    EnclosingFunctions:\n"
11561               "      - 'C_CODEBLOCK'\n"
11562               "      - 'CPPEVAL'\n"
11563               "    CanonicalDelimiter: 'cc'",
11564               RawStringFormats, ExpectedRawStringFormats);
11565 }
11566 
11567 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
11568   FormatStyle Style = {};
11569   Style.Language = FormatStyle::LK_Cpp;
11570   CHECK_PARSE("Language: Cpp\n"
11571               "IndentWidth: 12",
11572               IndentWidth, 12u);
11573   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
11574                                "IndentWidth: 34",
11575                                &Style),
11576             ParseError::Unsuitable);
11577   EXPECT_EQ(12u, Style.IndentWidth);
11578   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
11579   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
11580 
11581   Style.Language = FormatStyle::LK_JavaScript;
11582   CHECK_PARSE("Language: JavaScript\n"
11583               "IndentWidth: 12",
11584               IndentWidth, 12u);
11585   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
11586   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
11587                                "IndentWidth: 34",
11588                                &Style),
11589             ParseError::Unsuitable);
11590   EXPECT_EQ(23u, Style.IndentWidth);
11591   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
11592   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
11593 
11594   CHECK_PARSE("BasedOnStyle: LLVM\n"
11595               "IndentWidth: 67",
11596               IndentWidth, 67u);
11597 
11598   CHECK_PARSE("---\n"
11599               "Language: JavaScript\n"
11600               "IndentWidth: 12\n"
11601               "---\n"
11602               "Language: Cpp\n"
11603               "IndentWidth: 34\n"
11604               "...\n",
11605               IndentWidth, 12u);
11606 
11607   Style.Language = FormatStyle::LK_Cpp;
11608   CHECK_PARSE("---\n"
11609               "Language: JavaScript\n"
11610               "IndentWidth: 12\n"
11611               "---\n"
11612               "Language: Cpp\n"
11613               "IndentWidth: 34\n"
11614               "...\n",
11615               IndentWidth, 34u);
11616   CHECK_PARSE("---\n"
11617               "IndentWidth: 78\n"
11618               "---\n"
11619               "Language: JavaScript\n"
11620               "IndentWidth: 56\n"
11621               "...\n",
11622               IndentWidth, 78u);
11623 
11624   Style.ColumnLimit = 123;
11625   Style.IndentWidth = 234;
11626   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
11627   Style.TabWidth = 345;
11628   EXPECT_FALSE(parseConfiguration("---\n"
11629                                   "IndentWidth: 456\n"
11630                                   "BreakBeforeBraces: Allman\n"
11631                                   "---\n"
11632                                   "Language: JavaScript\n"
11633                                   "IndentWidth: 111\n"
11634                                   "TabWidth: 111\n"
11635                                   "---\n"
11636                                   "Language: Cpp\n"
11637                                   "BreakBeforeBraces: Stroustrup\n"
11638                                   "TabWidth: 789\n"
11639                                   "...\n",
11640                                   &Style));
11641   EXPECT_EQ(123u, Style.ColumnLimit);
11642   EXPECT_EQ(456u, Style.IndentWidth);
11643   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
11644   EXPECT_EQ(789u, Style.TabWidth);
11645 
11646   EXPECT_EQ(parseConfiguration("---\n"
11647                                "Language: JavaScript\n"
11648                                "IndentWidth: 56\n"
11649                                "---\n"
11650                                "IndentWidth: 78\n"
11651                                "...\n",
11652                                &Style),
11653             ParseError::Error);
11654   EXPECT_EQ(parseConfiguration("---\n"
11655                                "Language: JavaScript\n"
11656                                "IndentWidth: 56\n"
11657                                "---\n"
11658                                "Language: JavaScript\n"
11659                                "IndentWidth: 78\n"
11660                                "...\n",
11661                                &Style),
11662             ParseError::Error);
11663 
11664   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
11665 }
11666 
11667 #undef CHECK_PARSE
11668 
11669 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
11670   FormatStyle Style = {};
11671   Style.Language = FormatStyle::LK_JavaScript;
11672   Style.BreakBeforeTernaryOperators = true;
11673   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
11674   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
11675 
11676   Style.BreakBeforeTernaryOperators = true;
11677   EXPECT_EQ(0, parseConfiguration("---\n"
11678                                   "BasedOnStyle: Google\n"
11679                                   "---\n"
11680                                   "Language: JavaScript\n"
11681                                   "IndentWidth: 76\n"
11682                                   "...\n",
11683                                   &Style)
11684                    .value());
11685   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
11686   EXPECT_EQ(76u, Style.IndentWidth);
11687   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
11688 }
11689 
11690 TEST_F(FormatTest, ConfigurationRoundTripTest) {
11691   FormatStyle Style = getLLVMStyle();
11692   std::string YAML = configurationAsText(Style);
11693   FormatStyle ParsedStyle = {};
11694   ParsedStyle.Language = FormatStyle::LK_Cpp;
11695   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
11696   EXPECT_EQ(Style, ParsedStyle);
11697 }
11698 
11699 TEST_F(FormatTest, WorksFor8bitEncodings) {
11700   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
11701             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
11702             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
11703             "\"\xef\xee\xf0\xf3...\"",
11704             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
11705                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
11706                    "\xef\xee\xf0\xf3...\"",
11707                    getLLVMStyleWithColumns(12)));
11708 }
11709 
11710 TEST_F(FormatTest, HandlesUTF8BOM) {
11711   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
11712   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
11713             format("\xef\xbb\xbf#include <iostream>"));
11714   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
11715             format("\xef\xbb\xbf\n#include <iostream>"));
11716 }
11717 
11718 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
11719 #if !defined(_MSC_VER)
11720 
11721 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
11722   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
11723                getLLVMStyleWithColumns(35));
11724   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
11725                getLLVMStyleWithColumns(31));
11726   verifyFormat("// Однажды в студёную зимнюю пору...",
11727                getLLVMStyleWithColumns(36));
11728   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
11729   verifyFormat("/* Однажды в студёную зимнюю пору... */",
11730                getLLVMStyleWithColumns(39));
11731   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
11732                getLLVMStyleWithColumns(35));
11733 }
11734 
11735 TEST_F(FormatTest, SplitsUTF8Strings) {
11736   // Non-printable characters' width is currently considered to be the length in
11737   // bytes in UTF8. The characters can be displayed in very different manner
11738   // (zero-width, single width with a substitution glyph, expanded to their code
11739   // (e.g. "<8d>"), so there's no single correct way to handle them.
11740   EXPECT_EQ("\"aaaaÄ\"\n"
11741             "\"\xc2\x8d\";",
11742             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
11743   EXPECT_EQ("\"aaaaaaaÄ\"\n"
11744             "\"\xc2\x8d\";",
11745             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
11746   EXPECT_EQ("\"Однажды, в \"\n"
11747             "\"студёную \"\n"
11748             "\"зимнюю \"\n"
11749             "\"пору,\"",
11750             format("\"Однажды, в студёную зимнюю пору,\"",
11751                    getLLVMStyleWithColumns(13)));
11752   EXPECT_EQ(
11753       "\"一 二 三 \"\n"
11754       "\"四 五六 \"\n"
11755       "\"七 八 九 \"\n"
11756       "\"十\"",
11757       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
11758   EXPECT_EQ("\"一\t\"\n"
11759             "\"二 \t\"\n"
11760             "\"三 四 \"\n"
11761             "\"五\t\"\n"
11762             "\"六 \t\"\n"
11763             "\"七 \"\n"
11764             "\"八九十\tqq\"",
11765             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
11766                    getLLVMStyleWithColumns(11)));
11767 
11768   // UTF8 character in an escape sequence.
11769   EXPECT_EQ("\"aaaaaa\"\n"
11770             "\"\\\xC2\x8D\"",
11771             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
11772 }
11773 
11774 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
11775   EXPECT_EQ("const char *sssss =\n"
11776             "    \"一二三四五六七八\\\n"
11777             " 九 十\";",
11778             format("const char *sssss = \"一二三四五六七八\\\n"
11779                    " 九 十\";",
11780                    getLLVMStyleWithColumns(30)));
11781 }
11782 
11783 TEST_F(FormatTest, SplitsUTF8LineComments) {
11784   EXPECT_EQ("// aaaaÄ\xc2\x8d",
11785             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
11786   EXPECT_EQ("// Я из лесу\n"
11787             "// вышел; был\n"
11788             "// сильный\n"
11789             "// мороз.",
11790             format("// Я из лесу вышел; был сильный мороз.",
11791                    getLLVMStyleWithColumns(13)));
11792   EXPECT_EQ("// 一二三\n"
11793             "// 四五六七\n"
11794             "// 八  九\n"
11795             "// 十",
11796             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
11797 }
11798 
11799 TEST_F(FormatTest, SplitsUTF8BlockComments) {
11800   EXPECT_EQ("/* Гляжу,\n"
11801             " * поднимается\n"
11802             " * медленно в\n"
11803             " * гору\n"
11804             " * Лошадка,\n"
11805             " * везущая\n"
11806             " * хворосту\n"
11807             " * воз. */",
11808             format("/* Гляжу, поднимается медленно в гору\n"
11809                    " * Лошадка, везущая хворосту воз. */",
11810                    getLLVMStyleWithColumns(13)));
11811   EXPECT_EQ(
11812       "/* 一二三\n"
11813       " * 四五六七\n"
11814       " * 八  九\n"
11815       " * 十  */",
11816       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
11817   EXPECT_EQ("/* �������� ��������\n"
11818             " * ��������\n"
11819             " * ������-�� */",
11820             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
11821 }
11822 
11823 #endif // _MSC_VER
11824 
11825 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
11826   FormatStyle Style = getLLVMStyle();
11827 
11828   Style.ConstructorInitializerIndentWidth = 4;
11829   verifyFormat(
11830       "SomeClass::Constructor()\n"
11831       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
11832       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
11833       Style);
11834 
11835   Style.ConstructorInitializerIndentWidth = 2;
11836   verifyFormat(
11837       "SomeClass::Constructor()\n"
11838       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
11839       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
11840       Style);
11841 
11842   Style.ConstructorInitializerIndentWidth = 0;
11843   verifyFormat(
11844       "SomeClass::Constructor()\n"
11845       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
11846       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
11847       Style);
11848   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11849   verifyFormat(
11850       "SomeLongTemplateVariableName<\n"
11851       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
11852       Style);
11853   verifyFormat(
11854       "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
11855       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
11856       Style);
11857 }
11858 
11859 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
11860   FormatStyle Style = getLLVMStyle();
11861   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
11862   Style.ConstructorInitializerIndentWidth = 4;
11863   verifyFormat("SomeClass::Constructor()\n"
11864                "    : a(a)\n"
11865                "    , b(b)\n"
11866                "    , c(c) {}",
11867                Style);
11868   verifyFormat("SomeClass::Constructor()\n"
11869                "    : a(a) {}",
11870                Style);
11871 
11872   Style.ColumnLimit = 0;
11873   verifyFormat("SomeClass::Constructor()\n"
11874                "    : a(a) {}",
11875                Style);
11876   verifyFormat("SomeClass::Constructor() noexcept\n"
11877                "    : a(a) {}",
11878                Style);
11879   verifyFormat("SomeClass::Constructor()\n"
11880                "    : a(a)\n"
11881                "    , b(b)\n"
11882                "    , c(c) {}",
11883                Style);
11884   verifyFormat("SomeClass::Constructor()\n"
11885                "    : a(a) {\n"
11886                "  foo();\n"
11887                "  bar();\n"
11888                "}",
11889                Style);
11890 
11891   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11892   verifyFormat("SomeClass::Constructor()\n"
11893                "    : a(a)\n"
11894                "    , b(b)\n"
11895                "    , c(c) {\n}",
11896                Style);
11897   verifyFormat("SomeClass::Constructor()\n"
11898                "    : a(a) {\n}",
11899                Style);
11900 
11901   Style.ColumnLimit = 80;
11902   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
11903   Style.ConstructorInitializerIndentWidth = 2;
11904   verifyFormat("SomeClass::Constructor()\n"
11905                "  : a(a)\n"
11906                "  , b(b)\n"
11907                "  , c(c) {}",
11908                Style);
11909 
11910   Style.ConstructorInitializerIndentWidth = 0;
11911   verifyFormat("SomeClass::Constructor()\n"
11912                ": a(a)\n"
11913                ", b(b)\n"
11914                ", c(c) {}",
11915                Style);
11916 
11917   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
11918   Style.ConstructorInitializerIndentWidth = 4;
11919   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
11920   verifyFormat(
11921       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
11922       Style);
11923   verifyFormat(
11924       "SomeClass::Constructor()\n"
11925       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
11926       Style);
11927   Style.ConstructorInitializerIndentWidth = 4;
11928   Style.ColumnLimit = 60;
11929   verifyFormat("SomeClass::Constructor()\n"
11930                "    : aaaaaaaa(aaaaaaaa)\n"
11931                "    , aaaaaaaa(aaaaaaaa)\n"
11932                "    , aaaaaaaa(aaaaaaaa) {}",
11933                Style);
11934 }
11935 
11936 TEST_F(FormatTest, Destructors) {
11937   verifyFormat("void F(int &i) { i.~int(); }");
11938   verifyFormat("void F(int &i) { i->~int(); }");
11939 }
11940 
11941 TEST_F(FormatTest, FormatsWithWebKitStyle) {
11942   FormatStyle Style = getWebKitStyle();
11943 
11944   // Don't indent in outer namespaces.
11945   verifyFormat("namespace outer {\n"
11946                "int i;\n"
11947                "namespace inner {\n"
11948                "    int i;\n"
11949                "} // namespace inner\n"
11950                "} // namespace outer\n"
11951                "namespace other_outer {\n"
11952                "int i;\n"
11953                "}",
11954                Style);
11955 
11956   // Don't indent case labels.
11957   verifyFormat("switch (variable) {\n"
11958                "case 1:\n"
11959                "case 2:\n"
11960                "    doSomething();\n"
11961                "    break;\n"
11962                "default:\n"
11963                "    ++variable;\n"
11964                "}",
11965                Style);
11966 
11967   // Wrap before binary operators.
11968   EXPECT_EQ("void f()\n"
11969             "{\n"
11970             "    if (aaaaaaaaaaaaaaaa\n"
11971             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
11972             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
11973             "        return;\n"
11974             "}",
11975             format("void f() {\n"
11976                    "if (aaaaaaaaaaaaaaaa\n"
11977                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
11978                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
11979                    "return;\n"
11980                    "}",
11981                    Style));
11982 
11983   // Allow functions on a single line.
11984   verifyFormat("void f() { return; }", Style);
11985 
11986   // Constructor initializers are formatted one per line with the "," on the
11987   // new line.
11988   verifyFormat("Constructor()\n"
11989                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
11990                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
11991                "          aaaaaaaaaaaaaa)\n"
11992                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
11993                "{\n"
11994                "}",
11995                Style);
11996   verifyFormat("SomeClass::Constructor()\n"
11997                "    : a(a)\n"
11998                "{\n"
11999                "}",
12000                Style);
12001   EXPECT_EQ("SomeClass::Constructor()\n"
12002             "    : a(a)\n"
12003             "{\n"
12004             "}",
12005             format("SomeClass::Constructor():a(a){}", Style));
12006   verifyFormat("SomeClass::Constructor()\n"
12007                "    : a(a)\n"
12008                "    , b(b)\n"
12009                "    , c(c)\n"
12010                "{\n"
12011                "}",
12012                Style);
12013   verifyFormat("SomeClass::Constructor()\n"
12014                "    : a(a)\n"
12015                "{\n"
12016                "    foo();\n"
12017                "    bar();\n"
12018                "}",
12019                Style);
12020 
12021   // Access specifiers should be aligned left.
12022   verifyFormat("class C {\n"
12023                "public:\n"
12024                "    int i;\n"
12025                "};",
12026                Style);
12027 
12028   // Do not align comments.
12029   verifyFormat("int a; // Do not\n"
12030                "double b; // align comments.",
12031                Style);
12032 
12033   // Do not align operands.
12034   EXPECT_EQ("ASSERT(aaaa\n"
12035             "    || bbbb);",
12036             format("ASSERT ( aaaa\n||bbbb);", Style));
12037 
12038   // Accept input's line breaks.
12039   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
12040             "    || bbbbbbbbbbbbbbb) {\n"
12041             "    i++;\n"
12042             "}",
12043             format("if (aaaaaaaaaaaaaaa\n"
12044                    "|| bbbbbbbbbbbbbbb) { i++; }",
12045                    Style));
12046   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
12047             "    i++;\n"
12048             "}",
12049             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
12050 
12051   // Don't automatically break all macro definitions (llvm.org/PR17842).
12052   verifyFormat("#define aNumber 10", Style);
12053   // However, generally keep the line breaks that the user authored.
12054   EXPECT_EQ("#define aNumber \\\n"
12055             "    10",
12056             format("#define aNumber \\\n"
12057                    " 10",
12058                    Style));
12059 
12060   // Keep empty and one-element array literals on a single line.
12061   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
12062             "                                  copyItems:YES];",
12063             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
12064                    "copyItems:YES];",
12065                    Style));
12066   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
12067             "                                  copyItems:YES];",
12068             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
12069                    "             copyItems:YES];",
12070                    Style));
12071   // FIXME: This does not seem right, there should be more indentation before
12072   // the array literal's entries. Nested blocks have the same problem.
12073   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
12074             "    @\"a\",\n"
12075             "    @\"a\"\n"
12076             "]\n"
12077             "                                  copyItems:YES];",
12078             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
12079                    "     @\"a\",\n"
12080                    "     @\"a\"\n"
12081                    "     ]\n"
12082                    "       copyItems:YES];",
12083                    Style));
12084   EXPECT_EQ(
12085       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
12086       "                                  copyItems:YES];",
12087       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
12088              "   copyItems:YES];",
12089              Style));
12090 
12091   verifyFormat("[self.a b:c c:d];", Style);
12092   EXPECT_EQ("[self.a b:c\n"
12093             "        c:d];",
12094             format("[self.a b:c\n"
12095                    "c:d];",
12096                    Style));
12097 }
12098 
12099 TEST_F(FormatTest, FormatsLambdas) {
12100   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
12101   verifyFormat(
12102       "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n");
12103   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
12104   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
12105   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
12106   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
12107   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
12108   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
12109   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
12110   verifyFormat("int x = f(*+[] {});");
12111   verifyFormat("void f() {\n"
12112                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
12113                "}\n");
12114   verifyFormat("void f() {\n"
12115                "  other(x.begin(), //\n"
12116                "        x.end(),   //\n"
12117                "        [&](int, int) { return 1; });\n"
12118                "}\n");
12119   verifyFormat("void f() {\n"
12120                "  other.other.other.other.other(\n"
12121                "      x.begin(), x.end(),\n"
12122                "      [something, rather](int, int, int, int, int, int, int) { return 1; });\n"
12123                "}\n");
12124   verifyFormat("void f() {\n"
12125                "  other.other.other.other.other(\n"
12126                "      x.begin(), x.end(),\n"
12127                "      [something, rather](int, int, int, int, int, int, int) {\n"
12128                "        //\n"
12129                "      });\n"
12130                "}\n");
12131   verifyFormat("SomeFunction([]() { // A cool function...\n"
12132                "  return 43;\n"
12133                "});");
12134   EXPECT_EQ("SomeFunction([]() {\n"
12135             "#define A a\n"
12136             "  return 43;\n"
12137             "});",
12138             format("SomeFunction([](){\n"
12139                    "#define A a\n"
12140                    "return 43;\n"
12141                    "});"));
12142   verifyFormat("void f() {\n"
12143                "  SomeFunction([](decltype(x), A *a) {});\n"
12144                "}");
12145   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12146                "    [](const aaaaaaaaaa &a) { return a; });");
12147   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
12148                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
12149                "});");
12150   verifyFormat("Constructor()\n"
12151                "    : Field([] { // comment\n"
12152                "        int i;\n"
12153                "      }) {}");
12154   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
12155                "  return some_parameter.size();\n"
12156                "};");
12157   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
12158                "    [](const string &s) { return s; };");
12159   verifyFormat("int i = aaaaaa ? 1 //\n"
12160                "               : [] {\n"
12161                "                   return 2; //\n"
12162                "                 }();");
12163   verifyFormat("llvm::errs() << \"number of twos is \"\n"
12164                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
12165                "                  return x == 2; // force break\n"
12166                "                });");
12167   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12168                "    [=](int iiiiiiiiiiii) {\n"
12169                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
12170                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
12171                "    });",
12172                getLLVMStyleWithColumns(60));
12173   verifyFormat("SomeFunction({[&] {\n"
12174                "                // comment\n"
12175                "              },\n"
12176                "              [&] {\n"
12177                "                // comment\n"
12178                "              }});");
12179   verifyFormat("SomeFunction({[&] {\n"
12180                "  // comment\n"
12181                "}});");
12182   verifyFormat("virtual aaaaaaaaaaaaaaaa(\n"
12183                "    std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
12184                "    aaaaa aaaaaaaaa);");
12185 
12186   // Lambdas with return types.
12187   verifyFormat("int c = []() -> int { return 2; }();\n");
12188   verifyFormat("int c = []() -> int * { return 2; }();\n");
12189   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
12190   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
12191   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
12192   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
12193   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
12194   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
12195   verifyFormat("[a, a]() -> a<1> {};");
12196   verifyFormat("[]() -> foo<5 + 2> { return {}; };");
12197   verifyFormat("[]() -> foo<5 - 2> { return {}; };");
12198   verifyFormat("[]() -> foo<5 / 2> { return {}; };");
12199   verifyFormat("[]() -> foo<5 * 2> { return {}; };");
12200   verifyFormat("[]() -> foo<5 % 2> { return {}; };");
12201   verifyFormat("[]() -> foo<5 << 2> { return {}; };");
12202   verifyFormat("[]() -> foo<!5> { return {}; };");
12203   verifyFormat("[]() -> foo<~5> { return {}; };");
12204   verifyFormat("[]() -> foo<5 | 2> { return {}; };");
12205   verifyFormat("[]() -> foo<5 || 2> { return {}; };");
12206   verifyFormat("[]() -> foo<5 & 2> { return {}; };");
12207   verifyFormat("[]() -> foo<5 && 2> { return {}; };");
12208   verifyFormat("[]() -> foo<5 == 2> { return {}; };");
12209   verifyFormat("[]() -> foo<5 != 2> { return {}; };");
12210   verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
12211   verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
12212   verifyFormat("[]() -> foo<5 < 2> { return {}; };");
12213   verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
12214   verifyFormat("namespace bar {\n"
12215               "// broken:\n"
12216               "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
12217               "} // namespace bar");
12218   verifyFormat("namespace bar {\n"
12219               "// broken:\n"
12220               "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
12221               "} // namespace bar");
12222   verifyFormat("namespace bar {\n"
12223               "// broken:\n"
12224               "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
12225               "} // namespace bar");
12226   verifyFormat("namespace bar {\n"
12227               "// broken:\n"
12228               "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
12229               "} // namespace bar");
12230   verifyFormat("namespace bar {\n"
12231               "// broken:\n"
12232               "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
12233               "} // namespace bar");
12234   verifyFormat("namespace bar {\n"
12235               "// broken:\n"
12236               "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
12237               "} // namespace bar");
12238   verifyFormat("namespace bar {\n"
12239               "// broken:\n"
12240               "auto foo{[]() -> foo<!5> { return {}; }};\n"
12241               "} // namespace bar");
12242   verifyFormat("namespace bar {\n"
12243               "// broken:\n"
12244               "auto foo{[]() -> foo<~5> { return {}; }};\n"
12245               "} // namespace bar");
12246   verifyFormat("namespace bar {\n"
12247               "// broken:\n"
12248               "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
12249               "} // namespace bar");
12250   verifyFormat("namespace bar {\n"
12251               "// broken:\n"
12252               "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
12253               "} // namespace bar");
12254   verifyFormat("namespace bar {\n"
12255               "// broken:\n"
12256               "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
12257               "} // namespace bar");
12258   verifyFormat("namespace bar {\n"
12259               "// broken:\n"
12260               "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
12261               "} // namespace bar");
12262   verifyFormat("namespace bar {\n"
12263               "// broken:\n"
12264               "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
12265               "} // namespace bar");
12266   verifyFormat("namespace bar {\n"
12267               "// broken:\n"
12268               "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
12269               "} // namespace bar");
12270   verifyFormat("namespace bar {\n"
12271               "// broken:\n"
12272               "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
12273               "} // namespace bar");
12274   verifyFormat("namespace bar {\n"
12275               "// broken:\n"
12276               "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
12277               "} // namespace bar");
12278   verifyFormat("namespace bar {\n"
12279               "// broken:\n"
12280               "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
12281               "} // namespace bar");
12282   verifyFormat("namespace bar {\n"
12283               "// broken:\n"
12284               "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
12285               "} // namespace bar");
12286   verifyFormat("[]() -> a<1> {};");
12287   verifyFormat("[]() -> a<1> { ; };");
12288   verifyFormat("[]() -> a<1> { ; }();");
12289   verifyFormat("[a, a]() -> a<true> {};");
12290   verifyFormat("[]() -> a<true> {};");
12291   verifyFormat("[]() -> a<true> { ; };");
12292   verifyFormat("[]() -> a<true> { ; }();");
12293   verifyFormat("[a, a]() -> a<false> {};");
12294   verifyFormat("[]() -> a<false> {};");
12295   verifyFormat("[]() -> a<false> { ; };");
12296   verifyFormat("[]() -> a<false> { ; }();");
12297   verifyFormat("auto foo{[]() -> foo<false> { ; }};");
12298   verifyFormat("namespace bar {\n"
12299                "auto foo{[]() -> foo<false> { ; }};\n"
12300                "} // namespace bar");
12301   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
12302                "                   int j) -> int {\n"
12303                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
12304                "};");
12305   verifyFormat(
12306       "aaaaaaaaaaaaaaaaaaaaaa(\n"
12307       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
12308       "      return aaaaaaaaaaaaaaaaa;\n"
12309       "    });",
12310       getLLVMStyleWithColumns(70));
12311   verifyFormat("[]() //\n"
12312                "    -> int {\n"
12313                "  return 1; //\n"
12314                "};");
12315 
12316   // Multiple lambdas in the same parentheses change indentation rules. These
12317   // lambdas are forced to start on new lines.
12318   verifyFormat("SomeFunction(\n"
12319                "    []() {\n"
12320                "      //\n"
12321                "    },\n"
12322                "    []() {\n"
12323                "      //\n"
12324                "    });");
12325 
12326   // A lambda passed as arg0 is always pushed to the next line.
12327   verifyFormat("SomeFunction(\n"
12328                "    [this] {\n"
12329                "      //\n"
12330                "    },\n"
12331                "    1);\n");
12332 
12333   // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like the arg0
12334   // case above.
12335   auto Style = getGoogleStyle();
12336   Style.BinPackArguments = false;
12337   verifyFormat("SomeFunction(\n"
12338                "    a,\n"
12339                "    [this] {\n"
12340                "      //\n"
12341                "    },\n"
12342                "    b);\n",
12343                Style);
12344   verifyFormat("SomeFunction(\n"
12345                "    a,\n"
12346                "    [this] {\n"
12347                "      //\n"
12348                "    },\n"
12349                "    b);\n");
12350 
12351   // A lambda with a very long line forces arg0 to be pushed out irrespective of
12352   // the BinPackArguments value (as long as the code is wide enough).
12353   verifyFormat("something->SomeFunction(\n"
12354                "    a,\n"
12355                "    [this] {\n"
12356                "      D0000000000000000000000000000000000000000000000000000000000001();\n"
12357                "    },\n"
12358                "    b);\n");
12359 
12360   // A multi-line lambda is pulled up as long as the introducer fits on the previous
12361   // line and there are no further args.
12362   verifyFormat("function(1, [this, that] {\n"
12363                "  //\n"
12364                "});\n");
12365   verifyFormat("function([this, that] {\n"
12366                "  //\n"
12367                "});\n");
12368   // FIXME: this format is not ideal and we should consider forcing the first arg
12369   // onto its own line.
12370   verifyFormat("function(a, b, c, //\n"
12371                "         d, [this, that] {\n"
12372                "           //\n"
12373                "         });\n");
12374 
12375   // Multiple lambdas are treated correctly even when there is a short arg0.
12376   verifyFormat("SomeFunction(\n"
12377                "    1,\n"
12378                "    [this] {\n"
12379                "      //\n"
12380                "    },\n"
12381                "    [this] {\n"
12382                "      //\n"
12383                "    },\n"
12384                "    1);\n");
12385 
12386   // More complex introducers.
12387   verifyFormat("return [i, args...] {};");
12388 
12389   // Not lambdas.
12390   verifyFormat("constexpr char hello[]{\"hello\"};");
12391   verifyFormat("double &operator[](int i) { return 0; }\n"
12392                "int i;");
12393   verifyFormat("std::unique_ptr<int[]> foo() {}");
12394   verifyFormat("int i = a[a][a]->f();");
12395   verifyFormat("int i = (*b)[a]->f();");
12396 
12397   // Other corner cases.
12398   verifyFormat("void f() {\n"
12399                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
12400                "  );\n"
12401                "}");
12402 
12403   // Lambdas created through weird macros.
12404   verifyFormat("void f() {\n"
12405                "  MACRO((const AA &a) { return 1; });\n"
12406                "  MACRO((AA &a) { return 1; });\n"
12407                "}");
12408 
12409   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
12410                "      doo_dah();\n"
12411                "      doo_dah();\n"
12412                "    })) {\n"
12413                "}");
12414   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
12415                "                doo_dah();\n"
12416                "                doo_dah();\n"
12417                "              })) {\n"
12418                "}");
12419   verifyFormat("auto lambda = []() {\n"
12420                "  int a = 2\n"
12421                "#if A\n"
12422                "          + 2\n"
12423                "#endif\n"
12424                "      ;\n"
12425                "};");
12426 
12427   // Lambdas with complex multiline introducers.
12428   verifyFormat(
12429       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12430       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
12431       "        -> ::std::unordered_set<\n"
12432       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
12433       "      //\n"
12434       "    });");
12435 }
12436 
12437 TEST_F(FormatTest, EmptyLinesInLambdas) {
12438   verifyFormat("auto lambda = []() {\n"
12439                "  x(); //\n"
12440                "};",
12441                "auto lambda = []() {\n"
12442                "\n"
12443                "  x(); //\n"
12444                "\n"
12445                "};");
12446 }
12447 
12448 TEST_F(FormatTest, FormatsBlocks) {
12449   FormatStyle ShortBlocks = getLLVMStyle();
12450   ShortBlocks.AllowShortBlocksOnASingleLine = true;
12451   verifyFormat("int (^Block)(int, int);", ShortBlocks);
12452   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
12453   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
12454   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
12455   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
12456   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
12457 
12458   verifyFormat("foo(^{ bar(); });", ShortBlocks);
12459   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
12460   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
12461 
12462   verifyFormat("[operation setCompletionBlock:^{\n"
12463                "  [self onOperationDone];\n"
12464                "}];");
12465   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
12466                "  [self onOperationDone];\n"
12467                "}]};");
12468   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
12469                "  f();\n"
12470                "}];");
12471   verifyFormat("int a = [operation block:^int(int *i) {\n"
12472                "  return 1;\n"
12473                "}];");
12474   verifyFormat("[myObject doSomethingWith:arg1\n"
12475                "                      aaa:^int(int *a) {\n"
12476                "                        return 1;\n"
12477                "                      }\n"
12478                "                      bbb:f(a * bbbbbbbb)];");
12479 
12480   verifyFormat("[operation setCompletionBlock:^{\n"
12481                "  [self.delegate newDataAvailable];\n"
12482                "}];",
12483                getLLVMStyleWithColumns(60));
12484   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
12485                "  NSString *path = [self sessionFilePath];\n"
12486                "  if (path) {\n"
12487                "    // ...\n"
12488                "  }\n"
12489                "});");
12490   verifyFormat("[[SessionService sharedService]\n"
12491                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
12492                "      if (window) {\n"
12493                "        [self windowDidLoad:window];\n"
12494                "      } else {\n"
12495                "        [self errorLoadingWindow];\n"
12496                "      }\n"
12497                "    }];");
12498   verifyFormat("void (^largeBlock)(void) = ^{\n"
12499                "  // ...\n"
12500                "};\n",
12501                getLLVMStyleWithColumns(40));
12502   verifyFormat("[[SessionService sharedService]\n"
12503                "    loadWindowWithCompletionBlock: //\n"
12504                "        ^(SessionWindow *window) {\n"
12505                "          if (window) {\n"
12506                "            [self windowDidLoad:window];\n"
12507                "          } else {\n"
12508                "            [self errorLoadingWindow];\n"
12509                "          }\n"
12510                "        }];",
12511                getLLVMStyleWithColumns(60));
12512   verifyFormat("[myObject doSomethingWith:arg1\n"
12513                "    firstBlock:^(Foo *a) {\n"
12514                "      // ...\n"
12515                "      int i;\n"
12516                "    }\n"
12517                "    secondBlock:^(Bar *b) {\n"
12518                "      // ...\n"
12519                "      int i;\n"
12520                "    }\n"
12521                "    thirdBlock:^Foo(Bar *b) {\n"
12522                "      // ...\n"
12523                "      int i;\n"
12524                "    }];");
12525   verifyFormat("[myObject doSomethingWith:arg1\n"
12526                "               firstBlock:-1\n"
12527                "              secondBlock:^(Bar *b) {\n"
12528                "                // ...\n"
12529                "                int i;\n"
12530                "              }];");
12531 
12532   verifyFormat("f(^{\n"
12533                "  @autoreleasepool {\n"
12534                "    if (a) {\n"
12535                "      g();\n"
12536                "    }\n"
12537                "  }\n"
12538                "});");
12539   verifyFormat("Block b = ^int *(A *a, B *b) {}");
12540   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
12541                "};");
12542 
12543   FormatStyle FourIndent = getLLVMStyle();
12544   FourIndent.ObjCBlockIndentWidth = 4;
12545   verifyFormat("[operation setCompletionBlock:^{\n"
12546                "    [self onOperationDone];\n"
12547                "}];",
12548                FourIndent);
12549 }
12550 
12551 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
12552   FormatStyle ZeroColumn = getLLVMStyle();
12553   ZeroColumn.ColumnLimit = 0;
12554 
12555   verifyFormat("[[SessionService sharedService] "
12556                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
12557                "  if (window) {\n"
12558                "    [self windowDidLoad:window];\n"
12559                "  } else {\n"
12560                "    [self errorLoadingWindow];\n"
12561                "  }\n"
12562                "}];",
12563                ZeroColumn);
12564   EXPECT_EQ("[[SessionService sharedService]\n"
12565             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
12566             "      if (window) {\n"
12567             "        [self windowDidLoad:window];\n"
12568             "      } else {\n"
12569             "        [self errorLoadingWindow];\n"
12570             "      }\n"
12571             "    }];",
12572             format("[[SessionService sharedService]\n"
12573                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
12574                    "                if (window) {\n"
12575                    "    [self windowDidLoad:window];\n"
12576                    "  } else {\n"
12577                    "    [self errorLoadingWindow];\n"
12578                    "  }\n"
12579                    "}];",
12580                    ZeroColumn));
12581   verifyFormat("[myObject doSomethingWith:arg1\n"
12582                "    firstBlock:^(Foo *a) {\n"
12583                "      // ...\n"
12584                "      int i;\n"
12585                "    }\n"
12586                "    secondBlock:^(Bar *b) {\n"
12587                "      // ...\n"
12588                "      int i;\n"
12589                "    }\n"
12590                "    thirdBlock:^Foo(Bar *b) {\n"
12591                "      // ...\n"
12592                "      int i;\n"
12593                "    }];",
12594                ZeroColumn);
12595   verifyFormat("f(^{\n"
12596                "  @autoreleasepool {\n"
12597                "    if (a) {\n"
12598                "      g();\n"
12599                "    }\n"
12600                "  }\n"
12601                "});",
12602                ZeroColumn);
12603   verifyFormat("void (^largeBlock)(void) = ^{\n"
12604                "  // ...\n"
12605                "};",
12606                ZeroColumn);
12607 
12608   ZeroColumn.AllowShortBlocksOnASingleLine = true;
12609   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
12610             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
12611   ZeroColumn.AllowShortBlocksOnASingleLine = false;
12612   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
12613             "  int i;\n"
12614             "};",
12615             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
12616 }
12617 
12618 TEST_F(FormatTest, SupportsCRLF) {
12619   EXPECT_EQ("int a;\r\n"
12620             "int b;\r\n"
12621             "int c;\r\n",
12622             format("int a;\r\n"
12623                    "  int b;\r\n"
12624                    "    int c;\r\n",
12625                    getLLVMStyle()));
12626   EXPECT_EQ("int a;\r\n"
12627             "int b;\r\n"
12628             "int c;\r\n",
12629             format("int a;\r\n"
12630                    "  int b;\n"
12631                    "    int c;\r\n",
12632                    getLLVMStyle()));
12633   EXPECT_EQ("int a;\n"
12634             "int b;\n"
12635             "int c;\n",
12636             format("int a;\r\n"
12637                    "  int b;\n"
12638                    "    int c;\n",
12639                    getLLVMStyle()));
12640   EXPECT_EQ("\"aaaaaaa \"\r\n"
12641             "\"bbbbbbb\";\r\n",
12642             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
12643   EXPECT_EQ("#define A \\\r\n"
12644             "  b;      \\\r\n"
12645             "  c;      \\\r\n"
12646             "  d;\r\n",
12647             format("#define A \\\r\n"
12648                    "  b; \\\r\n"
12649                    "  c; d; \r\n",
12650                    getGoogleStyle()));
12651 
12652   EXPECT_EQ("/*\r\n"
12653             "multi line block comments\r\n"
12654             "should not introduce\r\n"
12655             "an extra carriage return\r\n"
12656             "*/\r\n",
12657             format("/*\r\n"
12658                    "multi line block comments\r\n"
12659                    "should not introduce\r\n"
12660                    "an extra carriage return\r\n"
12661                    "*/\r\n"));
12662 }
12663 
12664 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
12665   verifyFormat("MY_CLASS(C) {\n"
12666                "  int i;\n"
12667                "  int j;\n"
12668                "};");
12669 }
12670 
12671 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
12672   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
12673   TwoIndent.ContinuationIndentWidth = 2;
12674 
12675   EXPECT_EQ("int i =\n"
12676             "  longFunction(\n"
12677             "    arg);",
12678             format("int i = longFunction(arg);", TwoIndent));
12679 
12680   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
12681   SixIndent.ContinuationIndentWidth = 6;
12682 
12683   EXPECT_EQ("int i =\n"
12684             "      longFunction(\n"
12685             "            arg);",
12686             format("int i = longFunction(arg);", SixIndent));
12687 }
12688 
12689 TEST_F(FormatTest, SpacesInAngles) {
12690   FormatStyle Spaces = getLLVMStyle();
12691   Spaces.SpacesInAngles = true;
12692 
12693   verifyFormat("static_cast< int >(arg);", Spaces);
12694   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
12695   verifyFormat("f< int, float >();", Spaces);
12696   verifyFormat("template <> g() {}", Spaces);
12697   verifyFormat("template < std::vector< int > > f() {}", Spaces);
12698   verifyFormat("std::function< void(int, int) > fct;", Spaces);
12699   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
12700                Spaces);
12701 
12702   Spaces.Standard = FormatStyle::LS_Cpp03;
12703   Spaces.SpacesInAngles = true;
12704   verifyFormat("A< A< int > >();", Spaces);
12705 
12706   Spaces.SpacesInAngles = false;
12707   verifyFormat("A<A<int> >();", Spaces);
12708 
12709   Spaces.Standard = FormatStyle::LS_Cpp11;
12710   Spaces.SpacesInAngles = true;
12711   verifyFormat("A< A< int > >();", Spaces);
12712 
12713   Spaces.SpacesInAngles = false;
12714   verifyFormat("A<A<int>>();", Spaces);
12715 }
12716 
12717 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
12718   FormatStyle Style = getLLVMStyle();
12719   Style.SpaceAfterTemplateKeyword = false;
12720   verifyFormat("template<int> void foo();", Style);
12721 }
12722 
12723 TEST_F(FormatTest, TripleAngleBrackets) {
12724   verifyFormat("f<<<1, 1>>>();");
12725   verifyFormat("f<<<1, 1, 1, s>>>();");
12726   verifyFormat("f<<<a, b, c, d>>>();");
12727   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
12728   verifyFormat("f<param><<<1, 1>>>();");
12729   verifyFormat("f<1><<<1, 1>>>();");
12730   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
12731   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12732                "aaaaaaaaaaa<<<\n    1, 1>>>();");
12733   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
12734                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
12735 }
12736 
12737 TEST_F(FormatTest, MergeLessLessAtEnd) {
12738   verifyFormat("<<");
12739   EXPECT_EQ("< < <", format("\\\n<<<"));
12740   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12741                "aaallvm::outs() <<");
12742   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12743                "aaaallvm::outs()\n    <<");
12744 }
12745 
12746 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
12747   std::string code = "#if A\n"
12748                      "#if B\n"
12749                      "a.\n"
12750                      "#endif\n"
12751                      "    a = 1;\n"
12752                      "#else\n"
12753                      "#endif\n"
12754                      "#if C\n"
12755                      "#else\n"
12756                      "#endif\n";
12757   EXPECT_EQ(code, format(code));
12758 }
12759 
12760 TEST_F(FormatTest, HandleConflictMarkers) {
12761   // Git/SVN conflict markers.
12762   EXPECT_EQ("int a;\n"
12763             "void f() {\n"
12764             "  callme(some(parameter1,\n"
12765             "<<<<<<< text by the vcs\n"
12766             "              parameter2),\n"
12767             "||||||| text by the vcs\n"
12768             "              parameter2),\n"
12769             "         parameter3,\n"
12770             "======= text by the vcs\n"
12771             "              parameter2, parameter3),\n"
12772             ">>>>>>> text by the vcs\n"
12773             "         otherparameter);\n",
12774             format("int a;\n"
12775                    "void f() {\n"
12776                    "  callme(some(parameter1,\n"
12777                    "<<<<<<< text by the vcs\n"
12778                    "  parameter2),\n"
12779                    "||||||| text by the vcs\n"
12780                    "  parameter2),\n"
12781                    "  parameter3,\n"
12782                    "======= text by the vcs\n"
12783                    "  parameter2,\n"
12784                    "  parameter3),\n"
12785                    ">>>>>>> text by the vcs\n"
12786                    "  otherparameter);\n"));
12787 
12788   // Perforce markers.
12789   EXPECT_EQ("void f() {\n"
12790             "  function(\n"
12791             ">>>> text by the vcs\n"
12792             "      parameter,\n"
12793             "==== text by the vcs\n"
12794             "      parameter,\n"
12795             "==== text by the vcs\n"
12796             "      parameter,\n"
12797             "<<<< text by the vcs\n"
12798             "      parameter);\n",
12799             format("void f() {\n"
12800                    "  function(\n"
12801                    ">>>> text by the vcs\n"
12802                    "  parameter,\n"
12803                    "==== text by the vcs\n"
12804                    "  parameter,\n"
12805                    "==== text by the vcs\n"
12806                    "  parameter,\n"
12807                    "<<<< text by the vcs\n"
12808                    "  parameter);\n"));
12809 
12810   EXPECT_EQ("<<<<<<<\n"
12811             "|||||||\n"
12812             "=======\n"
12813             ">>>>>>>",
12814             format("<<<<<<<\n"
12815                    "|||||||\n"
12816                    "=======\n"
12817                    ">>>>>>>"));
12818 
12819   EXPECT_EQ("<<<<<<<\n"
12820             "|||||||\n"
12821             "int i;\n"
12822             "=======\n"
12823             ">>>>>>>",
12824             format("<<<<<<<\n"
12825                    "|||||||\n"
12826                    "int i;\n"
12827                    "=======\n"
12828                    ">>>>>>>"));
12829 
12830   // FIXME: Handle parsing of macros around conflict markers correctly:
12831   EXPECT_EQ("#define Macro \\\n"
12832             "<<<<<<<\n"
12833             "Something \\\n"
12834             "|||||||\n"
12835             "Else \\\n"
12836             "=======\n"
12837             "Other \\\n"
12838             ">>>>>>>\n"
12839             "    End int i;\n",
12840             format("#define Macro \\\n"
12841                    "<<<<<<<\n"
12842                    "  Something \\\n"
12843                    "|||||||\n"
12844                    "  Else \\\n"
12845                    "=======\n"
12846                    "  Other \\\n"
12847                    ">>>>>>>\n"
12848                    "  End\n"
12849                    "int i;\n"));
12850 }
12851 
12852 TEST_F(FormatTest, DisableRegions) {
12853   EXPECT_EQ("int i;\n"
12854             "// clang-format off\n"
12855             "  int j;\n"
12856             "// clang-format on\n"
12857             "int k;",
12858             format(" int  i;\n"
12859                    "   // clang-format off\n"
12860                    "  int j;\n"
12861                    " // clang-format on\n"
12862                    "   int   k;"));
12863   EXPECT_EQ("int i;\n"
12864             "/* clang-format off */\n"
12865             "  int j;\n"
12866             "/* clang-format on */\n"
12867             "int k;",
12868             format(" int  i;\n"
12869                    "   /* clang-format off */\n"
12870                    "  int j;\n"
12871                    " /* clang-format on */\n"
12872                    "   int   k;"));
12873 
12874   // Don't reflow comments within disabled regions.
12875   EXPECT_EQ(
12876       "// clang-format off\n"
12877       "// long long long long long long line\n"
12878       "/* clang-format on */\n"
12879       "/* long long long\n"
12880       " * long long long\n"
12881       " * line */\n"
12882       "int i;\n"
12883       "/* clang-format off */\n"
12884       "/* long long long long long long line */\n",
12885       format("// clang-format off\n"
12886              "// long long long long long long line\n"
12887              "/* clang-format on */\n"
12888              "/* long long long long long long line */\n"
12889              "int i;\n"
12890              "/* clang-format off */\n"
12891              "/* long long long long long long line */\n",
12892              getLLVMStyleWithColumns(20)));
12893 }
12894 
12895 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
12896   format("? ) =");
12897   verifyNoCrash("#define a\\\n /**/}");
12898 }
12899 
12900 TEST_F(FormatTest, FormatsTableGenCode) {
12901   FormatStyle Style = getLLVMStyle();
12902   Style.Language = FormatStyle::LK_TableGen;
12903   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
12904 }
12905 
12906 TEST_F(FormatTest, ArrayOfTemplates) {
12907   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
12908             format("auto a = new unique_ptr<int > [ 10];"));
12909 
12910   FormatStyle Spaces = getLLVMStyle();
12911   Spaces.SpacesInSquareBrackets = true;
12912   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
12913             format("auto a = new unique_ptr<int > [10];", Spaces));
12914 }
12915 
12916 TEST_F(FormatTest, ArrayAsTemplateType) {
12917   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
12918             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
12919 
12920   FormatStyle Spaces = getLLVMStyle();
12921   Spaces.SpacesInSquareBrackets = true;
12922   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
12923             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
12924 }
12925 
12926 TEST_F(FormatTest, NoSpaceAfterSuper) {
12927     verifyFormat("__super::FooBar();");
12928 }
12929 
12930 TEST(FormatStyle, GetStyleWithEmptyFileName) {
12931   llvm::vfs::InMemoryFileSystem FS;
12932   auto Style1 = getStyle("file", "", "Google", "", &FS);
12933   ASSERT_TRUE((bool)Style1);
12934   ASSERT_EQ(*Style1, getGoogleStyle());
12935 }
12936 
12937 TEST(FormatStyle, GetStyleOfFile) {
12938   llvm::vfs::InMemoryFileSystem FS;
12939   // Test 1: format file in the same directory.
12940   ASSERT_TRUE(
12941       FS.addFile("/a/.clang-format", 0,
12942                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
12943   ASSERT_TRUE(
12944       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
12945   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
12946   ASSERT_TRUE((bool)Style1);
12947   ASSERT_EQ(*Style1, getLLVMStyle());
12948 
12949   // Test 2.1: fallback to default.
12950   ASSERT_TRUE(
12951       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
12952   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
12953   ASSERT_TRUE((bool)Style2);
12954   ASSERT_EQ(*Style2, getMozillaStyle());
12955 
12956   // Test 2.2: no format on 'none' fallback style.
12957   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
12958   ASSERT_TRUE((bool)Style2);
12959   ASSERT_EQ(*Style2, getNoStyle());
12960 
12961   // Test 2.3: format if config is found with no based style while fallback is
12962   // 'none'.
12963   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
12964                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
12965   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
12966   ASSERT_TRUE((bool)Style2);
12967   ASSERT_EQ(*Style2, getLLVMStyle());
12968 
12969   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
12970   Style2 = getStyle("{}", "a.h", "none", "", &FS);
12971   ASSERT_TRUE((bool)Style2);
12972   ASSERT_EQ(*Style2, getLLVMStyle());
12973 
12974   // Test 3: format file in parent directory.
12975   ASSERT_TRUE(
12976       FS.addFile("/c/.clang-format", 0,
12977                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
12978   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
12979                          llvm::MemoryBuffer::getMemBuffer("int i;")));
12980   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
12981   ASSERT_TRUE((bool)Style3);
12982   ASSERT_EQ(*Style3, getGoogleStyle());
12983 
12984   // Test 4: error on invalid fallback style
12985   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
12986   ASSERT_FALSE((bool)Style4);
12987   llvm::consumeError(Style4.takeError());
12988 
12989   // Test 5: error on invalid yaml on command line
12990   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
12991   ASSERT_FALSE((bool)Style5);
12992   llvm::consumeError(Style5.takeError());
12993 
12994   // Test 6: error on invalid style
12995   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
12996   ASSERT_FALSE((bool)Style6);
12997   llvm::consumeError(Style6.takeError());
12998 
12999   // Test 7: found config file, error on parsing it
13000   ASSERT_TRUE(
13001       FS.addFile("/d/.clang-format", 0,
13002                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
13003                                                   "InvalidKey: InvalidValue")));
13004   ASSERT_TRUE(
13005       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
13006   auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
13007   ASSERT_FALSE((bool)Style7);
13008   llvm::consumeError(Style7.takeError());
13009 
13010   // Test 8: inferred per-language defaults apply.
13011   auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS);
13012   ASSERT_TRUE((bool)StyleTd);
13013   ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen));
13014 }
13015 
13016 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
13017   // Column limit is 20.
13018   std::string Code = "Type *a =\n"
13019                      "    new Type();\n"
13020                      "g(iiiii, 0, jjjjj,\n"
13021                      "  0, kkkkk, 0, mm);\n"
13022                      "int  bad     = format   ;";
13023   std::string Expected = "auto a = new Type();\n"
13024                          "g(iiiii, nullptr,\n"
13025                          "  jjjjj, nullptr,\n"
13026                          "  kkkkk, nullptr,\n"
13027                          "  mm);\n"
13028                          "int  bad     = format   ;";
13029   FileID ID = Context.createInMemoryFile("format.cpp", Code);
13030   tooling::Replacements Replaces = toReplacements(
13031       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
13032                             "auto "),
13033        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
13034                             "nullptr"),
13035        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
13036                             "nullptr"),
13037        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
13038                             "nullptr")});
13039 
13040   format::FormatStyle Style = format::getLLVMStyle();
13041   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
13042   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
13043   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
13044       << llvm::toString(FormattedReplaces.takeError()) << "\n";
13045   auto Result = applyAllReplacements(Code, *FormattedReplaces);
13046   EXPECT_TRUE(static_cast<bool>(Result));
13047   EXPECT_EQ(Expected, *Result);
13048 }
13049 
13050 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
13051   std::string Code = "#include \"a.h\"\n"
13052                      "#include \"c.h\"\n"
13053                      "\n"
13054                      "int main() {\n"
13055                      "  return 0;\n"
13056                      "}";
13057   std::string Expected = "#include \"a.h\"\n"
13058                          "#include \"b.h\"\n"
13059                          "#include \"c.h\"\n"
13060                          "\n"
13061                          "int main() {\n"
13062                          "  return 0;\n"
13063                          "}";
13064   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
13065   tooling::Replacements Replaces = toReplacements(
13066       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
13067                             "#include \"b.h\"\n")});
13068 
13069   format::FormatStyle Style = format::getLLVMStyle();
13070   Style.SortIncludes = true;
13071   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
13072   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
13073       << llvm::toString(FormattedReplaces.takeError()) << "\n";
13074   auto Result = applyAllReplacements(Code, *FormattedReplaces);
13075   EXPECT_TRUE(static_cast<bool>(Result));
13076   EXPECT_EQ(Expected, *Result);
13077 }
13078 
13079 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
13080   EXPECT_EQ("using std::cin;\n"
13081             "using std::cout;",
13082             format("using std::cout;\n"
13083                    "using std::cin;", getGoogleStyle()));
13084 }
13085 
13086 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
13087   format::FormatStyle Style = format::getLLVMStyle();
13088   Style.Standard = FormatStyle::LS_Cpp03;
13089   // cpp03 recognize this string as identifier u8 and literal character 'a'
13090   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
13091 }
13092 
13093 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
13094   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
13095   // all modes, including C++11, C++14 and C++17
13096   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
13097 }
13098 
13099 TEST_F(FormatTest, DoNotFormatLikelyXml) {
13100   EXPECT_EQ("<!-- ;> -->",
13101             format("<!-- ;> -->", getGoogleStyle()));
13102   EXPECT_EQ(" <!-- >; -->",
13103             format(" <!-- >; -->", getGoogleStyle()));
13104 }
13105 
13106 TEST_F(FormatTest, StructuredBindings) {
13107   // Structured bindings is a C++17 feature.
13108   // all modes, including C++11, C++14 and C++17
13109   verifyFormat("auto [a, b] = f();");
13110   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
13111   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
13112   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
13113   EXPECT_EQ("auto const volatile [a, b] = f();",
13114             format("auto  const   volatile[a, b] = f();"));
13115   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
13116   EXPECT_EQ("auto &[a, b, c] = f();",
13117             format("auto   &[  a  ,  b,c   ] = f();"));
13118   EXPECT_EQ("auto &&[a, b, c] = f();",
13119             format("auto   &&[  a  ,  b,c   ] = f();"));
13120   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
13121   EXPECT_EQ("auto const volatile &&[a, b] = f();",
13122             format("auto  const  volatile  &&[a, b] = f();"));
13123   EXPECT_EQ("auto const &&[a, b] = f();", format("auto  const   &&  [a, b] = f();"));
13124   EXPECT_EQ("const auto &[a, b] = f();", format("const  auto  &  [a, b] = f();"));
13125   EXPECT_EQ("const auto volatile &&[a, b] = f();",
13126             format("const  auto   volatile  &&[a, b] = f();"));
13127   EXPECT_EQ("volatile const auto &&[a, b] = f();",
13128             format("volatile  const  auto   &&[a, b] = f();"));
13129   EXPECT_EQ("const auto &&[a, b] = f();", format("const  auto  &&  [a, b] = f();"));
13130 
13131   // Make sure we don't mistake structured bindings for lambdas.
13132   FormatStyle PointerMiddle = getLLVMStyle();
13133   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
13134   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
13135   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
13136   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
13137   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
13138   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
13139   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
13140   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
13141   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
13142   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
13143   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
13144   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
13145   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
13146 
13147   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
13148             format("for (const auto   &&   [a, b] : some_range) {\n}"));
13149   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
13150             format("for (const auto   &   [a, b] : some_range) {\n}"));
13151   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
13152             format("for (const auto[a, b] : some_range) {\n}"));
13153   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
13154   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
13155   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
13156   EXPECT_EQ("auto const &[x, y](expr);", format("auto  const  &  [x,y]  (expr);"));
13157   EXPECT_EQ("auto const &&[x, y](expr);", format("auto  const  &&  [x,y]  (expr);"));
13158   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
13159   EXPECT_EQ("auto const &[x, y]{expr};", format("auto  const  &  [x,y]  {expr};"));
13160   EXPECT_EQ("auto const &&[x, y]{expr};", format("auto  const  &&  [x,y]  {expr};"));
13161 
13162   format::FormatStyle Spaces = format::getLLVMStyle();
13163   Spaces.SpacesInSquareBrackets = true;
13164   verifyFormat("auto [ a, b ] = f();", Spaces);
13165   verifyFormat("auto &&[ a, b ] = f();", Spaces);
13166   verifyFormat("auto &[ a, b ] = f();", Spaces);
13167   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
13168   verifyFormat("auto const &[ a, b ] = f();", Spaces);
13169 }
13170 
13171 TEST_F(FormatTest, FileAndCode) {
13172   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
13173   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
13174   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
13175   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
13176   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@interface Foo\n@end\n"));
13177   EXPECT_EQ(
13178       FormatStyle::LK_ObjC,
13179       guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
13180   EXPECT_EQ(FormatStyle::LK_ObjC,
13181             guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
13182   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
13183   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
13184   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end\n"));
13185   EXPECT_EQ(FormatStyle::LK_ObjC,
13186             guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
13187   EXPECT_EQ(
13188       FormatStyle::LK_ObjC,
13189       guessLanguage("foo.h",
13190                     "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
13191   EXPECT_EQ(
13192       FormatStyle::LK_Cpp,
13193       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
13194 }
13195 
13196 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
13197   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
13198   EXPECT_EQ(FormatStyle::LK_ObjC,
13199             guessLanguage("foo.h", "array[[calculator getIndex]];"));
13200   EXPECT_EQ(FormatStyle::LK_Cpp,
13201             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
13202   EXPECT_EQ(
13203       FormatStyle::LK_Cpp,
13204       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
13205   EXPECT_EQ(FormatStyle::LK_ObjC,
13206             guessLanguage("foo.h", "[[noreturn foo] bar];"));
13207   EXPECT_EQ(FormatStyle::LK_Cpp,
13208             guessLanguage("foo.h", "[[clang::fallthrough]];"));
13209   EXPECT_EQ(FormatStyle::LK_ObjC,
13210             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
13211   EXPECT_EQ(FormatStyle::LK_Cpp,
13212             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
13213   EXPECT_EQ(FormatStyle::LK_Cpp,
13214             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
13215   EXPECT_EQ(FormatStyle::LK_ObjC,
13216             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
13217   EXPECT_EQ(FormatStyle::LK_Cpp,
13218             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
13219   EXPECT_EQ(
13220       FormatStyle::LK_Cpp,
13221       guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
13222   EXPECT_EQ(
13223       FormatStyle::LK_Cpp,
13224       guessLanguage("foo.h",
13225                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
13226   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
13227 }
13228 
13229 TEST_F(FormatTest, GuessLanguageWithCaret) {
13230   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
13231   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
13232   EXPECT_EQ(FormatStyle::LK_ObjC,
13233             guessLanguage("foo.h", "int(^)(char, float);"));
13234   EXPECT_EQ(FormatStyle::LK_ObjC,
13235             guessLanguage("foo.h", "int(^foo)(char, float);"));
13236   EXPECT_EQ(FormatStyle::LK_ObjC,
13237             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
13238   EXPECT_EQ(FormatStyle::LK_ObjC,
13239             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
13240   EXPECT_EQ(
13241       FormatStyle::LK_ObjC,
13242       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
13243 }
13244 
13245 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
13246   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h",
13247                                                "void f() {\n"
13248                                                "  asm (\"mov %[e], %[d]\"\n"
13249                                                "     : [d] \"=rm\" (d)\n"
13250                                                "       [e] \"rm\" (*e));\n"
13251                                                "}"));
13252   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h",
13253                                                "void f() {\n"
13254                                                "  _asm (\"mov %[e], %[d]\"\n"
13255                                                "     : [d] \"=rm\" (d)\n"
13256                                                "       [e] \"rm\" (*e));\n"
13257                                                "}"));
13258   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h",
13259                                                "void f() {\n"
13260                                                "  __asm (\"mov %[e], %[d]\"\n"
13261                                                "     : [d] \"=rm\" (d)\n"
13262                                                "       [e] \"rm\" (*e));\n"
13263                                                "}"));
13264   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h",
13265                                                "void f() {\n"
13266                                                "  __asm__ (\"mov %[e], %[d]\"\n"
13267                                                "     : [d] \"=rm\" (d)\n"
13268                                                "       [e] \"rm\" (*e));\n"
13269                                                "}"));
13270   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h",
13271                                                "void f() {\n"
13272                                                "  asm (\"mov %[e], %[d]\"\n"
13273                                                "     : [d] \"=rm\" (d),\n"
13274                                                "       [e] \"rm\" (*e));\n"
13275                                                "}"));
13276   EXPECT_EQ(FormatStyle::LK_Cpp,
13277             guessLanguage("foo.h", "void f() {\n"
13278                                    "  asm volatile (\"mov %[e], %[d]\"\n"
13279                                    "     : [d] \"=rm\" (d)\n"
13280                                    "       [e] \"rm\" (*e));\n"
13281                                    "}"));
13282 }
13283 
13284 TEST_F(FormatTest, GuessLanguageWithChildLines) {
13285   EXPECT_EQ(FormatStyle::LK_Cpp,
13286             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
13287   EXPECT_EQ(FormatStyle::LK_ObjC,
13288             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
13289   EXPECT_EQ(
13290       FormatStyle::LK_Cpp,
13291       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
13292   EXPECT_EQ(
13293       FormatStyle::LK_ObjC,
13294       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
13295 }
13296 
13297 } // end namespace
13298 } // end namespace format
13299 } // end namespace clang
13300