1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "clang/Format/Format.h"
11 
12 #include "../Tooling/ReplacementTest.h"
13 #include "FormatTestUtils.h"
14 
15 #include "clang/Frontend/TextDiagnosticPrinter.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "gtest/gtest.h"
19 
20 #define DEBUG_TYPE "format-test"
21 
22 using clang::tooling::ReplacementTest;
23 using clang::tooling::toReplacements;
24 
25 namespace clang {
26 namespace format {
27 namespace {
28 
29 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
30 
31 class FormatTest : public ::testing::Test {
32 protected:
33   enum StatusCheck {
34     SC_ExpectComplete,
35     SC_ExpectIncomplete,
36     SC_DoNotCheck
37   };
38 
39   std::string format(llvm::StringRef Code,
40                      const FormatStyle &Style = getLLVMStyle(),
41                      StatusCheck CheckComplete = SC_ExpectComplete) {
42     DEBUG(llvm::errs() << "---\n");
43     DEBUG(llvm::errs() << Code << "\n\n");
44     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
45     FormattingAttemptStatus Status;
46     tooling::Replacements Replaces =
47         reformat(Style, Code, Ranges, "<stdin>", &Status);
48     if (CheckComplete != SC_DoNotCheck) {
49       bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
50       EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
51           << Code << "\n\n";
52     }
53     ReplacementCount = Replaces.size();
54     auto Result = applyAllReplacements(Code, Replaces);
55     EXPECT_TRUE(static_cast<bool>(Result));
56     DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
57     return *Result;
58   }
59 
60   FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) {
61     Style.ColumnLimit = ColumnLimit;
62     return Style;
63   }
64 
65   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
66     return getStyleWithColumns(getLLVMStyle(), ColumnLimit);
67   }
68 
69   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
70     return getStyleWithColumns(getGoogleStyle(), ColumnLimit);
71   }
72 
73   void verifyFormat(llvm::StringRef Code,
74                     const FormatStyle &Style = getLLVMStyle()) {
75     EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
76     if (Style.Language == FormatStyle::LK_Cpp) {
77       // Objective-C++ is a superset of C++, so everything checked for C++
78       // needs to be checked for Objective-C++ as well.
79       FormatStyle ObjCStyle = Style;
80       ObjCStyle.Language = FormatStyle::LK_ObjC;
81       EXPECT_EQ(Code.str(), format(test::messUp(Code), ObjCStyle));
82     }
83   }
84 
85   void verifyIncompleteFormat(llvm::StringRef Code,
86                               const FormatStyle &Style = getLLVMStyle()) {
87     EXPECT_EQ(Code.str(),
88               format(test::messUp(Code), Style, SC_ExpectIncomplete));
89   }
90 
91   void verifyGoogleFormat(llvm::StringRef Code) {
92     verifyFormat(Code, getGoogleStyle());
93   }
94 
95   void verifyIndependentOfContext(llvm::StringRef text) {
96     verifyFormat(text);
97     verifyFormat(llvm::Twine("void f() { " + text + " }").str());
98   }
99 
100   /// \brief Verify that clang-format does not crash on the given input.
101   void verifyNoCrash(llvm::StringRef Code,
102                      const FormatStyle &Style = getLLVMStyle()) {
103     format(Code, Style, SC_DoNotCheck);
104   }
105 
106   int ReplacementCount;
107 };
108 
109 TEST_F(FormatTest, MessUp) {
110   EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
111   EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
112   EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
113   EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
114   EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
115 }
116 
117 //===----------------------------------------------------------------------===//
118 // Basic function tests.
119 //===----------------------------------------------------------------------===//
120 
121 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
122   EXPECT_EQ(";", format(";"));
123 }
124 
125 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
126   EXPECT_EQ("int i;", format("  int i;"));
127   EXPECT_EQ("\nint i;", format(" \n\t \v \f  int i;"));
128   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
129   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
130 }
131 
132 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
133   EXPECT_EQ("int i;", format("int\ni;"));
134 }
135 
136 TEST_F(FormatTest, FormatsNestedBlockStatements) {
137   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
138 }
139 
140 TEST_F(FormatTest, FormatsNestedCall) {
141   verifyFormat("Method(f1, f2(f3));");
142   verifyFormat("Method(f1(f2, f3()));");
143   verifyFormat("Method(f1(f2, (f3())));");
144 }
145 
146 TEST_F(FormatTest, NestedNameSpecifiers) {
147   verifyFormat("vector<::Type> v;");
148   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
149   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
150   verifyFormat("bool a = 2 < ::SomeFunction();");
151   verifyFormat("ALWAYS_INLINE ::std::string getName();");
152   verifyFormat("some::string getName();");
153 }
154 
155 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
156   EXPECT_EQ("if (a) {\n"
157             "  f();\n"
158             "}",
159             format("if(a){f();}"));
160   EXPECT_EQ(4, ReplacementCount);
161   EXPECT_EQ("if (a) {\n"
162             "  f();\n"
163             "}",
164             format("if (a) {\n"
165                    "  f();\n"
166                    "}"));
167   EXPECT_EQ(0, ReplacementCount);
168   EXPECT_EQ("/*\r\n"
169             "\r\n"
170             "*/\r\n",
171             format("/*\r\n"
172                    "\r\n"
173                    "*/\r\n"));
174   EXPECT_EQ(0, ReplacementCount);
175 }
176 
177 TEST_F(FormatTest, RemovesEmptyLines) {
178   EXPECT_EQ("class C {\n"
179             "  int i;\n"
180             "};",
181             format("class C {\n"
182                    " int i;\n"
183                    "\n"
184                    "};"));
185 
186   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
187   EXPECT_EQ("namespace N {\n"
188             "\n"
189             "int i;\n"
190             "}",
191             format("namespace N {\n"
192                    "\n"
193                    "int    i;\n"
194                    "}",
195                    getGoogleStyle()));
196   EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
197             "\n"
198             "int i;\n"
199             "}",
200             format("extern /**/ \"C\" /**/ {\n"
201                    "\n"
202                    "int    i;\n"
203                    "}",
204                    getGoogleStyle()));
205 
206   // ...but do keep inlining and removing empty lines for non-block extern "C"
207   // functions.
208   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
209   EXPECT_EQ("extern \"C\" int f() {\n"
210             "  int i = 42;\n"
211             "  return i;\n"
212             "}",
213             format("extern \"C\" int f() {\n"
214                    "\n"
215                    "  int i = 42;\n"
216                    "  return i;\n"
217                    "}",
218                    getGoogleStyle()));
219 
220   // Remove empty lines at the beginning and end of blocks.
221   EXPECT_EQ("void f() {\n"
222             "\n"
223             "  if (a) {\n"
224             "\n"
225             "    f();\n"
226             "  }\n"
227             "}",
228             format("void f() {\n"
229                    "\n"
230                    "  if (a) {\n"
231                    "\n"
232                    "    f();\n"
233                    "\n"
234                    "  }\n"
235                    "\n"
236                    "}",
237                    getLLVMStyle()));
238   EXPECT_EQ("void f() {\n"
239             "  if (a) {\n"
240             "    f();\n"
241             "  }\n"
242             "}",
243             format("void f() {\n"
244                    "\n"
245                    "  if (a) {\n"
246                    "\n"
247                    "    f();\n"
248                    "\n"
249                    "  }\n"
250                    "\n"
251                    "}",
252                    getGoogleStyle()));
253 
254   // Don't remove empty lines in more complex control statements.
255   EXPECT_EQ("void f() {\n"
256             "  if (a) {\n"
257             "    f();\n"
258             "\n"
259             "  } else if (b) {\n"
260             "    f();\n"
261             "  }\n"
262             "}",
263             format("void f() {\n"
264                    "  if (a) {\n"
265                    "    f();\n"
266                    "\n"
267                    "  } else if (b) {\n"
268                    "    f();\n"
269                    "\n"
270                    "  }\n"
271                    "\n"
272                    "}"));
273 
274   // FIXME: This is slightly inconsistent.
275   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
276   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
277   EXPECT_EQ("namespace {\n"
278             "int i;\n"
279             "}",
280             format("namespace {\n"
281                    "int i;\n"
282                    "\n"
283                    "}", LLVMWithNoNamespaceFix));
284   EXPECT_EQ("namespace {\n"
285             "int i;\n"
286             "}",
287             format("namespace {\n"
288                    "int i;\n"
289                    "\n"
290                    "}"));
291   EXPECT_EQ("namespace {\n"
292             "int i;\n"
293             "\n"
294             "} // namespace",
295             format("namespace {\n"
296                    "int i;\n"
297                    "\n"
298                    "}  // namespace"));
299 
300   FormatStyle Style = getLLVMStyle();
301   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
302   Style.MaxEmptyLinesToKeep = 2;
303   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
304   Style.BraceWrapping.AfterClass = true;
305   Style.BraceWrapping.AfterFunction = true;
306   Style.KeepEmptyLinesAtTheStartOfBlocks = false;
307 
308   EXPECT_EQ("class Foo\n"
309             "{\n"
310             "  Foo() {}\n"
311             "\n"
312             "  void funk() {}\n"
313             "};",
314             format("class Foo\n"
315                    "{\n"
316                    "  Foo()\n"
317                    "  {\n"
318                    "  }\n"
319                    "\n"
320                    "  void funk() {}\n"
321                    "};",
322                    Style));
323 }
324 
325 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
326   verifyFormat("x = (a) and (b);");
327   verifyFormat("x = (a) or (b);");
328   verifyFormat("x = (a) bitand (b);");
329   verifyFormat("x = (a) bitor (b);");
330   verifyFormat("x = (a) not_eq (b);");
331   verifyFormat("x = (a) and_eq (b);");
332   verifyFormat("x = (a) or_eq (b);");
333   verifyFormat("x = (a) xor (b);");
334 }
335 
336 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) {
337   verifyFormat("x = compl(a);");
338   verifyFormat("x = not(a);");
339   verifyFormat("x = bitand(a);");
340   // Unary operator must not be merged with the next identifier
341   verifyFormat("x = compl a;");
342   verifyFormat("x = not a;");
343   verifyFormat("x = bitand a;");
344 }
345 
346 //===----------------------------------------------------------------------===//
347 // Tests for control statements.
348 //===----------------------------------------------------------------------===//
349 
350 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
351   verifyFormat("if (true)\n  f();\ng();");
352   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
353   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
354   verifyFormat("if constexpr (true)\n"
355                "  f();\ng();");
356   verifyFormat("if constexpr (a)\n"
357                "  if constexpr (b)\n"
358                "    if constexpr (c)\n"
359                "      g();\n"
360                "h();");
361   verifyFormat("if constexpr (a)\n"
362                "  if constexpr (b) {\n"
363                "    f();\n"
364                "  }\n"
365                "g();");
366 
367   FormatStyle AllowsMergedIf = getLLVMStyle();
368   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
369   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
370   verifyFormat("if (a)\n"
371                "  // comment\n"
372                "  f();",
373                AllowsMergedIf);
374   verifyFormat("{\n"
375                "  if (a)\n"
376                "  label:\n"
377                "    f();\n"
378                "}",
379                AllowsMergedIf);
380   verifyFormat("#define A \\\n"
381                "  if (a)  \\\n"
382                "  label:  \\\n"
383                "    f()",
384                AllowsMergedIf);
385   verifyFormat("if (a)\n"
386                "  ;",
387                AllowsMergedIf);
388   verifyFormat("if (a)\n"
389                "  if (b) return;",
390                AllowsMergedIf);
391 
392   verifyFormat("if (a) // Can't merge this\n"
393                "  f();\n",
394                AllowsMergedIf);
395   verifyFormat("if (a) /* still don't merge */\n"
396                "  f();",
397                AllowsMergedIf);
398   verifyFormat("if (a) { // Never merge this\n"
399                "  f();\n"
400                "}",
401                AllowsMergedIf);
402   verifyFormat("if (a) { /* Never merge this */\n"
403                "  f();\n"
404                "}",
405                AllowsMergedIf);
406 
407   AllowsMergedIf.ColumnLimit = 14;
408   verifyFormat("if (a) return;", AllowsMergedIf);
409   verifyFormat("if (aaaaaaaaa)\n"
410                "  return;",
411                AllowsMergedIf);
412 
413   AllowsMergedIf.ColumnLimit = 13;
414   verifyFormat("if (a)\n  return;", AllowsMergedIf);
415 }
416 
417 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
418   FormatStyle AllowsMergedLoops = getLLVMStyle();
419   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
420   verifyFormat("while (true) continue;", AllowsMergedLoops);
421   verifyFormat("for (;;) continue;", AllowsMergedLoops);
422   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
423   verifyFormat("while (true)\n"
424                "  ;",
425                AllowsMergedLoops);
426   verifyFormat("for (;;)\n"
427                "  ;",
428                AllowsMergedLoops);
429   verifyFormat("for (;;)\n"
430                "  for (;;) continue;",
431                AllowsMergedLoops);
432   verifyFormat("for (;;) // Can't merge this\n"
433                "  continue;",
434                AllowsMergedLoops);
435   verifyFormat("for (;;) /* still don't merge */\n"
436                "  continue;",
437                AllowsMergedLoops);
438 }
439 
440 TEST_F(FormatTest, FormatShortBracedStatements) {
441   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
442   AllowSimpleBracedStatements.ColumnLimit = 40;
443   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
444 
445   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
446   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
447 
448   AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
449   AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
450   AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
451 
452   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
453   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
454   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
455   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
456   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
457   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
458   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
459   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
460   verifyFormat("if (true) {\n"
461                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
462                "}",
463                AllowSimpleBracedStatements);
464   verifyFormat("if (true) { //\n"
465                "  f();\n"
466                "}",
467                AllowSimpleBracedStatements);
468   verifyFormat("if (true) {\n"
469                "  f();\n"
470                "  f();\n"
471                "}",
472                AllowSimpleBracedStatements);
473   verifyFormat("if (true) {\n"
474                "  f();\n"
475                "} else {\n"
476                "  f();\n"
477                "}",
478                AllowSimpleBracedStatements);
479 
480   verifyFormat("struct A2 {\n"
481                "  int X;\n"
482                "};",
483                AllowSimpleBracedStatements);
484   verifyFormat("typedef struct A2 {\n"
485                "  int X;\n"
486                "} A2_t;",
487                AllowSimpleBracedStatements);
488   verifyFormat("template <int> struct A2 {\n"
489                "  struct B {};\n"
490                "};",
491                AllowSimpleBracedStatements);
492 
493   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
494   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
495   verifyFormat("if (true) {\n"
496                "  f();\n"
497                "}",
498                AllowSimpleBracedStatements);
499   verifyFormat("if (true) {\n"
500                "  f();\n"
501                "} else {\n"
502                "  f();\n"
503                "}",
504                AllowSimpleBracedStatements);
505 
506   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
507   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
508   verifyFormat("while (true) {\n"
509                "  f();\n"
510                "}",
511                AllowSimpleBracedStatements);
512   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
513   verifyFormat("for (;;) {\n"
514                "  f();\n"
515                "}",
516                AllowSimpleBracedStatements);
517 
518   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
519   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
520   AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = true;
521 
522   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
523   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
524   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
525   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
526   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
527   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
528   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
529   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
530   verifyFormat("if (true)\n"
531                "{\n"
532                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
533                "}",
534                AllowSimpleBracedStatements);
535   verifyFormat("if (true)\n"
536                "{ //\n"
537                "  f();\n"
538                "}",
539                AllowSimpleBracedStatements);
540   verifyFormat("if (true)\n"
541                "{\n"
542                "  f();\n"
543                "  f();\n"
544                "}",
545                AllowSimpleBracedStatements);
546   verifyFormat("if (true)\n"
547                "{\n"
548                "  f();\n"
549                "} else\n"
550                "{\n"
551                "  f();\n"
552                "}",
553                AllowSimpleBracedStatements);
554 
555   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
556   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
557   verifyFormat("if (true)\n"
558                "{\n"
559                "  f();\n"
560                "}",
561                AllowSimpleBracedStatements);
562   verifyFormat("if (true)\n"
563                "{\n"
564                "  f();\n"
565                "} else\n"
566                "{\n"
567                "  f();\n"
568                "}",
569                AllowSimpleBracedStatements);
570 
571   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
572   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
573   verifyFormat("while (true)\n"
574                "{\n"
575                "  f();\n"
576                "}",
577                AllowSimpleBracedStatements);
578   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
579   verifyFormat("for (;;)\n"
580                "{\n"
581                "  f();\n"
582                "}",
583                AllowSimpleBracedStatements);
584 }
585 
586 TEST_F(FormatTest, ParseIfElse) {
587   verifyFormat("if (true)\n"
588                "  if (true)\n"
589                "    if (true)\n"
590                "      f();\n"
591                "    else\n"
592                "      g();\n"
593                "  else\n"
594                "    h();\n"
595                "else\n"
596                "  i();");
597   verifyFormat("if (true)\n"
598                "  if (true)\n"
599                "    if (true) {\n"
600                "      if (true)\n"
601                "        f();\n"
602                "    } else {\n"
603                "      g();\n"
604                "    }\n"
605                "  else\n"
606                "    h();\n"
607                "else {\n"
608                "  i();\n"
609                "}");
610   verifyFormat("if (true)\n"
611                "  if constexpr (true)\n"
612                "    if (true) {\n"
613                "      if constexpr (true)\n"
614                "        f();\n"
615                "    } else {\n"
616                "      g();\n"
617                "    }\n"
618                "  else\n"
619                "    h();\n"
620                "else {\n"
621                "  i();\n"
622                "}");
623   verifyFormat("void f() {\n"
624                "  if (a) {\n"
625                "  } else {\n"
626                "  }\n"
627                "}");
628 }
629 
630 TEST_F(FormatTest, ElseIf) {
631   verifyFormat("if (a) {\n} else if (b) {\n}");
632   verifyFormat("if (a)\n"
633                "  f();\n"
634                "else if (b)\n"
635                "  g();\n"
636                "else\n"
637                "  h();");
638   verifyFormat("if constexpr (a)\n"
639                "  f();\n"
640                "else if constexpr (b)\n"
641                "  g();\n"
642                "else\n"
643                "  h();");
644   verifyFormat("if (a) {\n"
645                "  f();\n"
646                "}\n"
647                "// or else ..\n"
648                "else {\n"
649                "  g()\n"
650                "}");
651 
652   verifyFormat("if (a) {\n"
653                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
654                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
655                "}");
656   verifyFormat("if (a) {\n"
657                "} else if (\n"
658                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
659                "}",
660                getLLVMStyleWithColumns(62));
661   verifyFormat("if (a) {\n"
662                "} else if constexpr (\n"
663                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
664                "}",
665                getLLVMStyleWithColumns(62));
666 }
667 
668 TEST_F(FormatTest, FormatsForLoop) {
669   verifyFormat(
670       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
671       "     ++VeryVeryLongLoopVariable)\n"
672       "  ;");
673   verifyFormat("for (;;)\n"
674                "  f();");
675   verifyFormat("for (;;) {\n}");
676   verifyFormat("for (;;) {\n"
677                "  f();\n"
678                "}");
679   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
680 
681   verifyFormat(
682       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
683       "                                          E = UnwrappedLines.end();\n"
684       "     I != E; ++I) {\n}");
685 
686   verifyFormat(
687       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
688       "     ++IIIII) {\n}");
689   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
690                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
691                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
692   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
693                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
694                "         E = FD->getDeclsInPrototypeScope().end();\n"
695                "     I != E; ++I) {\n}");
696   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
697                "         I = Container.begin(),\n"
698                "         E = Container.end();\n"
699                "     I != E; ++I) {\n}",
700                getLLVMStyleWithColumns(76));
701 
702   verifyFormat(
703       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
704       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
705       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
706       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
707       "     ++aaaaaaaaaaa) {\n}");
708   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
709                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
710                "     ++i) {\n}");
711   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
712                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
713                "}");
714   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
715                "         aaaaaaaaaa);\n"
716                "     iter; ++iter) {\n"
717                "}");
718   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
719                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
720                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
721                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
722 
723   FormatStyle NoBinPacking = getLLVMStyle();
724   NoBinPacking.BinPackParameters = false;
725   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
726                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
727                "                                           aaaaaaaaaaaaaaaa,\n"
728                "                                           aaaaaaaaaaaaaaaa,\n"
729                "                                           aaaaaaaaaaaaaaaa);\n"
730                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
731                "}",
732                NoBinPacking);
733   verifyFormat(
734       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
735       "                                          E = UnwrappedLines.end();\n"
736       "     I != E;\n"
737       "     ++I) {\n}",
738       NoBinPacking);
739 
740   FormatStyle AlignLeft = getLLVMStyle();
741   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
742   verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
743 }
744 
745 TEST_F(FormatTest, RangeBasedForLoops) {
746   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
747                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
748   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
749                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
750   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
751                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
752   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
753                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
754 }
755 
756 TEST_F(FormatTest, ForEachLoops) {
757   verifyFormat("void f() {\n"
758                "  foreach (Item *item, itemlist) {}\n"
759                "  Q_FOREACH (Item *item, itemlist) {}\n"
760                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
761                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
762                "}");
763 
764   // As function-like macros.
765   verifyFormat("#define foreach(x, y)\n"
766                "#define Q_FOREACH(x, y)\n"
767                "#define BOOST_FOREACH(x, y)\n"
768                "#define UNKNOWN_FOREACH(x, y)\n");
769 
770   // Not as function-like macros.
771   verifyFormat("#define foreach (x, y)\n"
772                "#define Q_FOREACH (x, y)\n"
773                "#define BOOST_FOREACH (x, y)\n"
774                "#define UNKNOWN_FOREACH (x, y)\n");
775 }
776 
777 TEST_F(FormatTest, FormatsWhileLoop) {
778   verifyFormat("while (true) {\n}");
779   verifyFormat("while (true)\n"
780                "  f();");
781   verifyFormat("while () {\n}");
782   verifyFormat("while () {\n"
783                "  f();\n"
784                "}");
785 }
786 
787 TEST_F(FormatTest, FormatsDoWhile) {
788   verifyFormat("do {\n"
789                "  do_something();\n"
790                "} while (something());");
791   verifyFormat("do\n"
792                "  do_something();\n"
793                "while (something());");
794 }
795 
796 TEST_F(FormatTest, FormatsSwitchStatement) {
797   verifyFormat("switch (x) {\n"
798                "case 1:\n"
799                "  f();\n"
800                "  break;\n"
801                "case kFoo:\n"
802                "case ns::kBar:\n"
803                "case kBaz:\n"
804                "  break;\n"
805                "default:\n"
806                "  g();\n"
807                "  break;\n"
808                "}");
809   verifyFormat("switch (x) {\n"
810                "case 1: {\n"
811                "  f();\n"
812                "  break;\n"
813                "}\n"
814                "case 2: {\n"
815                "  break;\n"
816                "}\n"
817                "}");
818   verifyFormat("switch (x) {\n"
819                "case 1: {\n"
820                "  f();\n"
821                "  {\n"
822                "    g();\n"
823                "    h();\n"
824                "  }\n"
825                "  break;\n"
826                "}\n"
827                "}");
828   verifyFormat("switch (x) {\n"
829                "case 1: {\n"
830                "  f();\n"
831                "  if (foo) {\n"
832                "    g();\n"
833                "    h();\n"
834                "  }\n"
835                "  break;\n"
836                "}\n"
837                "}");
838   verifyFormat("switch (x) {\n"
839                "case 1: {\n"
840                "  f();\n"
841                "  g();\n"
842                "} break;\n"
843                "}");
844   verifyFormat("switch (test)\n"
845                "  ;");
846   verifyFormat("switch (x) {\n"
847                "default: {\n"
848                "  // Do nothing.\n"
849                "}\n"
850                "}");
851   verifyFormat("switch (x) {\n"
852                "// comment\n"
853                "// if 1, do f()\n"
854                "case 1:\n"
855                "  f();\n"
856                "}");
857   verifyFormat("switch (x) {\n"
858                "case 1:\n"
859                "  // Do amazing stuff\n"
860                "  {\n"
861                "    f();\n"
862                "    g();\n"
863                "  }\n"
864                "  break;\n"
865                "}");
866   verifyFormat("#define A          \\\n"
867                "  switch (x) {     \\\n"
868                "  case a:          \\\n"
869                "    foo = b;       \\\n"
870                "  }",
871                getLLVMStyleWithColumns(20));
872   verifyFormat("#define OPERATION_CASE(name)           \\\n"
873                "  case OP_name:                        \\\n"
874                "    return operations::Operation##name\n",
875                getLLVMStyleWithColumns(40));
876   verifyFormat("switch (x) {\n"
877                "case 1:;\n"
878                "default:;\n"
879                "  int i;\n"
880                "}");
881 
882   verifyGoogleFormat("switch (x) {\n"
883                      "  case 1:\n"
884                      "    f();\n"
885                      "    break;\n"
886                      "  case kFoo:\n"
887                      "  case ns::kBar:\n"
888                      "  case kBaz:\n"
889                      "    break;\n"
890                      "  default:\n"
891                      "    g();\n"
892                      "    break;\n"
893                      "}");
894   verifyGoogleFormat("switch (x) {\n"
895                      "  case 1: {\n"
896                      "    f();\n"
897                      "    break;\n"
898                      "  }\n"
899                      "}");
900   verifyGoogleFormat("switch (test)\n"
901                      "  ;");
902 
903   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
904                      "  case OP_name:              \\\n"
905                      "    return operations::Operation##name\n");
906   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
907                      "  // Get the correction operation class.\n"
908                      "  switch (OpCode) {\n"
909                      "    CASE(Add);\n"
910                      "    CASE(Subtract);\n"
911                      "    default:\n"
912                      "      return operations::Unknown;\n"
913                      "  }\n"
914                      "#undef OPERATION_CASE\n"
915                      "}");
916   verifyFormat("DEBUG({\n"
917                "  switch (x) {\n"
918                "  case A:\n"
919                "    f();\n"
920                "    break;\n"
921                "    // fallthrough\n"
922                "  case B:\n"
923                "    g();\n"
924                "    break;\n"
925                "  }\n"
926                "});");
927   EXPECT_EQ("DEBUG({\n"
928             "  switch (x) {\n"
929             "  case A:\n"
930             "    f();\n"
931             "    break;\n"
932             "  // On B:\n"
933             "  case B:\n"
934             "    g();\n"
935             "    break;\n"
936             "  }\n"
937             "});",
938             format("DEBUG({\n"
939                    "  switch (x) {\n"
940                    "  case A:\n"
941                    "    f();\n"
942                    "    break;\n"
943                    "  // On B:\n"
944                    "  case B:\n"
945                    "    g();\n"
946                    "    break;\n"
947                    "  }\n"
948                    "});",
949                    getLLVMStyle()));
950   verifyFormat("switch (a) {\n"
951                "case (b):\n"
952                "  return;\n"
953                "}");
954 
955   verifyFormat("switch (a) {\n"
956                "case some_namespace::\n"
957                "    some_constant:\n"
958                "  return;\n"
959                "}",
960                getLLVMStyleWithColumns(34));
961 }
962 
963 TEST_F(FormatTest, CaseRanges) {
964   verifyFormat("switch (x) {\n"
965                "case 'A' ... 'Z':\n"
966                "case 1 ... 5:\n"
967                "case a ... b:\n"
968                "  break;\n"
969                "}");
970 }
971 
972 TEST_F(FormatTest, ShortCaseLabels) {
973   FormatStyle Style = getLLVMStyle();
974   Style.AllowShortCaseLabelsOnASingleLine = true;
975   verifyFormat("switch (a) {\n"
976                "case 1: x = 1; break;\n"
977                "case 2: return;\n"
978                "case 3:\n"
979                "case 4:\n"
980                "case 5: return;\n"
981                "case 6: // comment\n"
982                "  return;\n"
983                "case 7:\n"
984                "  // comment\n"
985                "  return;\n"
986                "case 8:\n"
987                "  x = 8; // comment\n"
988                "  break;\n"
989                "default: y = 1; break;\n"
990                "}",
991                Style);
992   verifyFormat("switch (a) {\n"
993                "case 0: return; // comment\n"
994                "case 1: break;  // comment\n"
995                "case 2: return;\n"
996                "// comment\n"
997                "case 3: return;\n"
998                "// comment 1\n"
999                "// comment 2\n"
1000                "// comment 3\n"
1001                "case 4: break; /* comment */\n"
1002                "case 5:\n"
1003                "  // comment\n"
1004                "  break;\n"
1005                "case 6: /* comment */ x = 1; break;\n"
1006                "case 7: x = /* comment */ 1; break;\n"
1007                "case 8:\n"
1008                "  x = 1; /* comment */\n"
1009                "  break;\n"
1010                "case 9:\n"
1011                "  break; // comment line 1\n"
1012                "         // comment line 2\n"
1013                "}",
1014                Style);
1015   EXPECT_EQ("switch (a) {\n"
1016             "case 1:\n"
1017             "  x = 8;\n"
1018             "  // fall through\n"
1019             "case 2: x = 8;\n"
1020             "// comment\n"
1021             "case 3:\n"
1022             "  return; /* comment line 1\n"
1023             "           * comment line 2 */\n"
1024             "case 4: i = 8;\n"
1025             "// something else\n"
1026             "#if FOO\n"
1027             "case 5: break;\n"
1028             "#endif\n"
1029             "}",
1030             format("switch (a) {\n"
1031                    "case 1: x = 8;\n"
1032                    "  // fall through\n"
1033                    "case 2:\n"
1034                    "  x = 8;\n"
1035                    "// comment\n"
1036                    "case 3:\n"
1037                    "  return; /* comment line 1\n"
1038                    "           * comment line 2 */\n"
1039                    "case 4:\n"
1040                    "  i = 8;\n"
1041                    "// something else\n"
1042                    "#if FOO\n"
1043                    "case 5: break;\n"
1044                    "#endif\n"
1045                    "}",
1046                    Style));
1047   EXPECT_EQ("switch (a) {\n" "case 0:\n"
1048             "  return; // long long long long long long long long long long long long comment\n"
1049             "          // line\n" "}",
1050             format("switch (a) {\n"
1051                    "case 0: return; // long long long long long long long long long long long long comment line\n"
1052                    "}",
1053                    Style));
1054   EXPECT_EQ("switch (a) {\n"
1055             "case 0:\n"
1056             "  return; /* long long long long long long long long long long long long comment\n"
1057             "             line */\n"
1058             "}",
1059             format("switch (a) {\n"
1060                    "case 0: return; /* long long long long long long long long long long long long comment line */\n"
1061                    "}",
1062                    Style));
1063   verifyFormat("switch (a) {\n"
1064                "#if FOO\n"
1065                "case 0: return 0;\n"
1066                "#endif\n"
1067                "}",
1068                Style);
1069   verifyFormat("switch (a) {\n"
1070                "case 1: {\n"
1071                "}\n"
1072                "case 2: {\n"
1073                "  return;\n"
1074                "}\n"
1075                "case 3: {\n"
1076                "  x = 1;\n"
1077                "  return;\n"
1078                "}\n"
1079                "case 4:\n"
1080                "  if (x)\n"
1081                "    return;\n"
1082                "}",
1083                Style);
1084   Style.ColumnLimit = 21;
1085   verifyFormat("switch (a) {\n"
1086                "case 1: x = 1; break;\n"
1087                "case 2: return;\n"
1088                "case 3:\n"
1089                "case 4:\n"
1090                "case 5: return;\n"
1091                "default:\n"
1092                "  y = 1;\n"
1093                "  break;\n"
1094                "}",
1095                Style);
1096 }
1097 
1098 TEST_F(FormatTest, FormatsLabels) {
1099   verifyFormat("void f() {\n"
1100                "  some_code();\n"
1101                "test_label:\n"
1102                "  some_other_code();\n"
1103                "  {\n"
1104                "    some_more_code();\n"
1105                "  another_label:\n"
1106                "    some_more_code();\n"
1107                "  }\n"
1108                "}");
1109   verifyFormat("{\n"
1110                "  some_code();\n"
1111                "test_label:\n"
1112                "  some_other_code();\n"
1113                "}");
1114   verifyFormat("{\n"
1115                "  some_code();\n"
1116                "test_label:;\n"
1117                "  int i = 0;\n"
1118                "}");
1119 }
1120 
1121 //===----------------------------------------------------------------------===//
1122 // Tests for classes, namespaces, etc.
1123 //===----------------------------------------------------------------------===//
1124 
1125 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1126   verifyFormat("class A {};");
1127 }
1128 
1129 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1130   verifyFormat("class A {\n"
1131                "public:\n"
1132                "public: // comment\n"
1133                "protected:\n"
1134                "private:\n"
1135                "  void f() {}\n"
1136                "};");
1137   verifyGoogleFormat("class A {\n"
1138                      " public:\n"
1139                      " protected:\n"
1140                      " private:\n"
1141                      "  void f() {}\n"
1142                      "};");
1143   verifyFormat("class A {\n"
1144                "public slots:\n"
1145                "  void f1() {}\n"
1146                "public Q_SLOTS:\n"
1147                "  void f2() {}\n"
1148                "protected slots:\n"
1149                "  void f3() {}\n"
1150                "protected Q_SLOTS:\n"
1151                "  void f4() {}\n"
1152                "private slots:\n"
1153                "  void f5() {}\n"
1154                "private Q_SLOTS:\n"
1155                "  void f6() {}\n"
1156                "signals:\n"
1157                "  void g1();\n"
1158                "Q_SIGNALS:\n"
1159                "  void g2();\n"
1160                "};");
1161 
1162   // Don't interpret 'signals' the wrong way.
1163   verifyFormat("signals.set();");
1164   verifyFormat("for (Signals signals : f()) {\n}");
1165   verifyFormat("{\n"
1166                "  signals.set(); // This needs indentation.\n"
1167                "}");
1168   verifyFormat("void f() {\n"
1169                "label:\n"
1170                "  signals.baz();\n"
1171                "}");
1172 }
1173 
1174 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1175   EXPECT_EQ("class A {\n"
1176             "public:\n"
1177             "  void f();\n"
1178             "\n"
1179             "private:\n"
1180             "  void g() {}\n"
1181             "  // test\n"
1182             "protected:\n"
1183             "  int h;\n"
1184             "};",
1185             format("class A {\n"
1186                    "public:\n"
1187                    "void f();\n"
1188                    "private:\n"
1189                    "void g() {}\n"
1190                    "// test\n"
1191                    "protected:\n"
1192                    "int h;\n"
1193                    "};"));
1194   EXPECT_EQ("class A {\n"
1195             "protected:\n"
1196             "public:\n"
1197             "  void f();\n"
1198             "};",
1199             format("class A {\n"
1200                    "protected:\n"
1201                    "\n"
1202                    "public:\n"
1203                    "\n"
1204                    "  void f();\n"
1205                    "};"));
1206 
1207   // Even ensure proper spacing inside macros.
1208   EXPECT_EQ("#define B     \\\n"
1209             "  class A {   \\\n"
1210             "   protected: \\\n"
1211             "   public:    \\\n"
1212             "    void f(); \\\n"
1213             "  };",
1214             format("#define B     \\\n"
1215                    "  class A {   \\\n"
1216                    "   protected: \\\n"
1217                    "              \\\n"
1218                    "   public:    \\\n"
1219                    "              \\\n"
1220                    "    void f(); \\\n"
1221                    "  };",
1222                    getGoogleStyle()));
1223   // But don't remove empty lines after macros ending in access specifiers.
1224   EXPECT_EQ("#define A private:\n"
1225             "\n"
1226             "int i;",
1227             format("#define A         private:\n"
1228                    "\n"
1229                    "int              i;"));
1230 }
1231 
1232 TEST_F(FormatTest, FormatsClasses) {
1233   verifyFormat("class A : public B {};");
1234   verifyFormat("class A : public ::B {};");
1235 
1236   verifyFormat(
1237       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1238       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1239   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1240                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1241                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1242   verifyFormat(
1243       "class A : public B, public C, public D, public E, public F {};");
1244   verifyFormat("class AAAAAAAAAAAA : public B,\n"
1245                "                     public C,\n"
1246                "                     public D,\n"
1247                "                     public E,\n"
1248                "                     public F,\n"
1249                "                     public G {};");
1250 
1251   verifyFormat("class\n"
1252                "    ReallyReallyLongClassName {\n"
1253                "  int i;\n"
1254                "};",
1255                getLLVMStyleWithColumns(32));
1256   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
1257                "                           aaaaaaaaaaaaaaaa> {};");
1258   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
1259                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
1260                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
1261   verifyFormat("template <class R, class C>\n"
1262                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
1263                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
1264   verifyFormat("class ::A::B {};");
1265 }
1266 
1267 TEST_F(FormatTest, BreakBeforeInheritanceComma) {
1268   FormatStyle StyleWithInheritanceBreak = getLLVMStyle();
1269   StyleWithInheritanceBreak.BreakBeforeInheritanceComma = true;
1270 
1271   verifyFormat("class MyClass : public X {};", StyleWithInheritanceBreak);
1272   verifyFormat("class MyClass\n"
1273                "    : public X\n"
1274                "    , public Y {};",
1275                StyleWithInheritanceBreak);
1276 }
1277 
1278 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1279   verifyFormat("class A {\n} a, b;");
1280   verifyFormat("struct A {\n} a, b;");
1281   verifyFormat("union A {\n} a;");
1282 }
1283 
1284 TEST_F(FormatTest, FormatsEnum) {
1285   verifyFormat("enum {\n"
1286                "  Zero,\n"
1287                "  One = 1,\n"
1288                "  Two = One + 1,\n"
1289                "  Three = (One + Two),\n"
1290                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1291                "  Five = (One, Two, Three, Four, 5)\n"
1292                "};");
1293   verifyGoogleFormat("enum {\n"
1294                      "  Zero,\n"
1295                      "  One = 1,\n"
1296                      "  Two = One + 1,\n"
1297                      "  Three = (One + Two),\n"
1298                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1299                      "  Five = (One, Two, Three, Four, 5)\n"
1300                      "};");
1301   verifyFormat("enum Enum {};");
1302   verifyFormat("enum {};");
1303   verifyFormat("enum X E {} d;");
1304   verifyFormat("enum __attribute__((...)) E {} d;");
1305   verifyFormat("enum __declspec__((...)) E {} d;");
1306   verifyFormat("enum {\n"
1307                "  Bar = Foo<int, int>::value\n"
1308                "};",
1309                getLLVMStyleWithColumns(30));
1310 
1311   verifyFormat("enum ShortEnum { A, B, C };");
1312   verifyGoogleFormat("enum ShortEnum { A, B, C };");
1313 
1314   EXPECT_EQ("enum KeepEmptyLines {\n"
1315             "  ONE,\n"
1316             "\n"
1317             "  TWO,\n"
1318             "\n"
1319             "  THREE\n"
1320             "}",
1321             format("enum KeepEmptyLines {\n"
1322                    "  ONE,\n"
1323                    "\n"
1324                    "  TWO,\n"
1325                    "\n"
1326                    "\n"
1327                    "  THREE\n"
1328                    "}"));
1329   verifyFormat("enum E { // comment\n"
1330                "  ONE,\n"
1331                "  TWO\n"
1332                "};\n"
1333                "int i;");
1334   // Not enums.
1335   verifyFormat("enum X f() {\n"
1336                "  a();\n"
1337                "  return 42;\n"
1338                "}");
1339   verifyFormat("enum X Type::f() {\n"
1340                "  a();\n"
1341                "  return 42;\n"
1342                "}");
1343   verifyFormat("enum ::X f() {\n"
1344                "  a();\n"
1345                "  return 42;\n"
1346                "}");
1347   verifyFormat("enum ns::X f() {\n"
1348                "  a();\n"
1349                "  return 42;\n"
1350                "}");
1351 }
1352 
1353 TEST_F(FormatTest, FormatsEnumsWithErrors) {
1354   verifyFormat("enum Type {\n"
1355                "  One = 0; // These semicolons should be commas.\n"
1356                "  Two = 1;\n"
1357                "};");
1358   verifyFormat("namespace n {\n"
1359                "enum Type {\n"
1360                "  One,\n"
1361                "  Two, // missing };\n"
1362                "  int i;\n"
1363                "}\n"
1364                "void g() {}");
1365 }
1366 
1367 TEST_F(FormatTest, FormatsEnumStruct) {
1368   verifyFormat("enum struct {\n"
1369                "  Zero,\n"
1370                "  One = 1,\n"
1371                "  Two = One + 1,\n"
1372                "  Three = (One + Two),\n"
1373                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1374                "  Five = (One, Two, Three, Four, 5)\n"
1375                "};");
1376   verifyFormat("enum struct Enum {};");
1377   verifyFormat("enum struct {};");
1378   verifyFormat("enum struct X E {} d;");
1379   verifyFormat("enum struct __attribute__((...)) E {} d;");
1380   verifyFormat("enum struct __declspec__((...)) E {} d;");
1381   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
1382 }
1383 
1384 TEST_F(FormatTest, FormatsEnumClass) {
1385   verifyFormat("enum class {\n"
1386                "  Zero,\n"
1387                "  One = 1,\n"
1388                "  Two = One + 1,\n"
1389                "  Three = (One + Two),\n"
1390                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1391                "  Five = (One, Two, Three, Four, 5)\n"
1392                "};");
1393   verifyFormat("enum class Enum {};");
1394   verifyFormat("enum class {};");
1395   verifyFormat("enum class X E {} d;");
1396   verifyFormat("enum class __attribute__((...)) E {} d;");
1397   verifyFormat("enum class __declspec__((...)) E {} d;");
1398   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
1399 }
1400 
1401 TEST_F(FormatTest, FormatsEnumTypes) {
1402   verifyFormat("enum X : int {\n"
1403                "  A, // Force multiple lines.\n"
1404                "  B\n"
1405                "};");
1406   verifyFormat("enum X : int { A, B };");
1407   verifyFormat("enum X : std::uint32_t { A, B };");
1408 }
1409 
1410 TEST_F(FormatTest, FormatsTypedefEnum) {
1411   FormatStyle Style = getLLVMStyle();
1412   Style.ColumnLimit = 40;
1413   verifyFormat("typedef enum {} EmptyEnum;");
1414   verifyFormat("typedef enum { A, B, C } ShortEnum;");
1415   verifyFormat("typedef enum {\n"
1416                "  ZERO = 0,\n"
1417                "  ONE = 1,\n"
1418                "  TWO = 2,\n"
1419                "  THREE = 3\n"
1420                "} LongEnum;",
1421                Style);
1422   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1423   Style.BraceWrapping.AfterEnum = true;
1424   verifyFormat("typedef enum {} EmptyEnum;");
1425   verifyFormat("typedef enum { A, B, C } ShortEnum;");
1426   verifyFormat("typedef enum\n"
1427                "{\n"
1428                "  ZERO = 0,\n"
1429                "  ONE = 1,\n"
1430                "  TWO = 2,\n"
1431                "  THREE = 3\n"
1432                "} LongEnum;",
1433                Style);
1434 }
1435 
1436 TEST_F(FormatTest, FormatsNSEnums) {
1437   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
1438   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
1439                      "  // Information about someDecentlyLongValue.\n"
1440                      "  someDecentlyLongValue,\n"
1441                      "  // Information about anotherDecentlyLongValue.\n"
1442                      "  anotherDecentlyLongValue,\n"
1443                      "  // Information about aThirdDecentlyLongValue.\n"
1444                      "  aThirdDecentlyLongValue\n"
1445                      "};");
1446   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
1447                      "  a = 1,\n"
1448                      "  b = 2,\n"
1449                      "  c = 3,\n"
1450                      "};");
1451   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
1452                      "  a = 1,\n"
1453                      "  b = 2,\n"
1454                      "  c = 3,\n"
1455                      "};");
1456   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
1457                      "  a = 1,\n"
1458                      "  b = 2,\n"
1459                      "  c = 3,\n"
1460                      "};");
1461 }
1462 
1463 TEST_F(FormatTest, FormatsBitfields) {
1464   verifyFormat("struct Bitfields {\n"
1465                "  unsigned sClass : 8;\n"
1466                "  unsigned ValueKind : 2;\n"
1467                "};");
1468   verifyFormat("struct A {\n"
1469                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
1470                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
1471                "};");
1472   verifyFormat("struct MyStruct {\n"
1473                "  uchar data;\n"
1474                "  uchar : 8;\n"
1475                "  uchar : 8;\n"
1476                "  uchar other;\n"
1477                "};");
1478 }
1479 
1480 TEST_F(FormatTest, FormatsNamespaces) {
1481   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
1482   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
1483 
1484   verifyFormat("namespace some_namespace {\n"
1485                "class A {};\n"
1486                "void f() { f(); }\n"
1487                "}",
1488                LLVMWithNoNamespaceFix);
1489   verifyFormat("namespace {\n"
1490                "class A {};\n"
1491                "void f() { f(); }\n"
1492                "}",
1493                LLVMWithNoNamespaceFix);
1494   verifyFormat("inline namespace X {\n"
1495                "class A {};\n"
1496                "void f() { f(); }\n"
1497                "}",
1498                LLVMWithNoNamespaceFix);
1499   verifyFormat("using namespace some_namespace;\n"
1500                "class A {};\n"
1501                "void f() { f(); }",
1502                LLVMWithNoNamespaceFix);
1503 
1504   // This code is more common than we thought; if we
1505   // layout this correctly the semicolon will go into
1506   // its own line, which is undesirable.
1507   verifyFormat("namespace {};",
1508                LLVMWithNoNamespaceFix);
1509   verifyFormat("namespace {\n"
1510                "class A {};\n"
1511                "};",
1512                LLVMWithNoNamespaceFix);
1513 
1514   verifyFormat("namespace {\n"
1515                "int SomeVariable = 0; // comment\n"
1516                "} // namespace",
1517                LLVMWithNoNamespaceFix);
1518   EXPECT_EQ("#ifndef HEADER_GUARD\n"
1519             "#define HEADER_GUARD\n"
1520             "namespace my_namespace {\n"
1521             "int i;\n"
1522             "} // my_namespace\n"
1523             "#endif // HEADER_GUARD",
1524             format("#ifndef HEADER_GUARD\n"
1525                    " #define HEADER_GUARD\n"
1526                    "   namespace my_namespace {\n"
1527                    "int i;\n"
1528                    "}    // my_namespace\n"
1529                    "#endif    // HEADER_GUARD",
1530                    LLVMWithNoNamespaceFix));
1531 
1532   EXPECT_EQ("namespace A::B {\n"
1533             "class C {};\n"
1534             "}",
1535             format("namespace A::B {\n"
1536                    "class C {};\n"
1537                    "}",
1538                    LLVMWithNoNamespaceFix));
1539 
1540   FormatStyle Style = getLLVMStyle();
1541   Style.NamespaceIndentation = FormatStyle::NI_All;
1542   EXPECT_EQ("namespace out {\n"
1543             "  int i;\n"
1544             "  namespace in {\n"
1545             "    int i;\n"
1546             "  } // namespace in\n"
1547             "} // namespace out",
1548             format("namespace out {\n"
1549                    "int i;\n"
1550                    "namespace in {\n"
1551                    "int i;\n"
1552                    "} // namespace in\n"
1553                    "} // namespace out",
1554                    Style));
1555 
1556   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1557   EXPECT_EQ("namespace out {\n"
1558             "int i;\n"
1559             "namespace in {\n"
1560             "  int i;\n"
1561             "} // namespace in\n"
1562             "} // namespace out",
1563             format("namespace out {\n"
1564                    "int i;\n"
1565                    "namespace in {\n"
1566                    "int i;\n"
1567                    "} // namespace in\n"
1568                    "} // namespace out",
1569                    Style));
1570 }
1571 
1572 TEST_F(FormatTest, FormatsCompactNamespaces) {
1573   FormatStyle Style = getLLVMStyle();
1574   Style.CompactNamespaces = true;
1575 
1576   verifyFormat("namespace A { namespace B {\n"
1577 			   "}} // namespace A::B",
1578 			   Style);
1579 
1580   EXPECT_EQ("namespace out { namespace in {\n"
1581             "}} // namespace out::in",
1582             format("namespace out {\n"
1583                    "namespace in {\n"
1584                    "} // namespace in\n"
1585                    "} // namespace out",
1586                    Style));
1587 
1588   // Only namespaces which have both consecutive opening and end get compacted
1589   EXPECT_EQ("namespace out {\n"
1590             "namespace in1 {\n"
1591             "} // namespace in1\n"
1592             "namespace in2 {\n"
1593             "} // namespace in2\n"
1594             "} // namespace out",
1595             format("namespace out {\n"
1596                    "namespace in1 {\n"
1597                    "} // namespace in1\n"
1598                    "namespace in2 {\n"
1599                    "} // namespace in2\n"
1600                    "} // namespace out",
1601                    Style));
1602 
1603   EXPECT_EQ("namespace out {\n"
1604             "int i;\n"
1605             "namespace in {\n"
1606             "int j;\n"
1607             "} // namespace in\n"
1608             "int k;\n"
1609             "} // namespace out",
1610             format("namespace out { int i;\n"
1611                    "namespace in { int j; } // namespace in\n"
1612                    "int k; } // namespace out",
1613                    Style));
1614 
1615   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
1616             "}}} // namespace A::B::C\n",
1617             format("namespace A { namespace B {\n"
1618                    "namespace C {\n"
1619                    "}} // namespace B::C\n"
1620                    "} // namespace A\n",
1621                    Style));
1622 
1623   Style.ColumnLimit = 40;
1624   EXPECT_EQ("namespace aaaaaaaaaa {\n"
1625             "namespace bbbbbbbbbb {\n"
1626             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
1627             format("namespace aaaaaaaaaa {\n"
1628                    "namespace bbbbbbbbbb {\n"
1629                    "} // namespace bbbbbbbbbb\n"
1630                    "} // namespace aaaaaaaaaa",
1631                    Style));
1632 
1633   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
1634             "namespace cccccc {\n"
1635             "}}} // namespace aaaaaa::bbbbbb::cccccc",
1636             format("namespace aaaaaa {\n"
1637                    "namespace bbbbbb {\n"
1638                    "namespace cccccc {\n"
1639                    "} // namespace cccccc\n"
1640                    "} // namespace bbbbbb\n"
1641                    "} // namespace aaaaaa",
1642                    Style));
1643   Style.ColumnLimit = 80;
1644 
1645   // Extra semicolon after 'inner' closing brace prevents merging
1646   EXPECT_EQ("namespace out { namespace in {\n"
1647             "}; } // namespace out::in",
1648             format("namespace out {\n"
1649                    "namespace in {\n"
1650                    "}; // namespace in\n"
1651                    "} // namespace out",
1652                    Style));
1653 
1654   // Extra semicolon after 'outer' closing brace is conserved
1655   EXPECT_EQ("namespace out { namespace in {\n"
1656             "}}; // namespace out::in",
1657             format("namespace out {\n"
1658                    "namespace in {\n"
1659                    "} // namespace in\n"
1660                    "}; // namespace out",
1661                    Style));
1662 
1663   Style.NamespaceIndentation = FormatStyle::NI_All;
1664   EXPECT_EQ("namespace out { namespace in {\n"
1665             "  int i;\n"
1666             "}} // namespace out::in",
1667             format("namespace out {\n"
1668                    "namespace in {\n"
1669                    "int i;\n"
1670                    "} // namespace in\n"
1671                    "} // namespace out",
1672                    Style));
1673   EXPECT_EQ("namespace out { namespace mid {\n"
1674             "  namespace in {\n"
1675             "    int j;\n"
1676             "  } // namespace in\n"
1677             "  int k;\n"
1678             "}} // namespace out::mid",
1679             format("namespace out { namespace mid {\n"
1680                    "namespace in { int j; } // namespace in\n"
1681                    "int k; }} // namespace out::mid",
1682                    Style));
1683 
1684   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1685   EXPECT_EQ("namespace out { namespace in {\n"
1686             "  int i;\n"
1687             "}} // namespace out::in",
1688             format("namespace out {\n"
1689                    "namespace in {\n"
1690                    "int i;\n"
1691                    "} // namespace in\n"
1692                    "} // namespace out",
1693                    Style));
1694   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
1695             "  int i;\n"
1696             "}}} // namespace out::mid::in",
1697             format("namespace out {\n"
1698                    "namespace mid {\n"
1699                    "namespace in {\n"
1700                    "int i;\n"
1701                    "} // namespace in\n"
1702                    "} // namespace mid\n"
1703                    "} // namespace out",
1704                    Style));
1705 }
1706 
1707 TEST_F(FormatTest, FormatsExternC) {
1708   verifyFormat("extern \"C\" {\nint a;");
1709   verifyFormat("extern \"C\" {}");
1710   verifyFormat("extern \"C\" {\n"
1711                "int foo();\n"
1712                "}");
1713   verifyFormat("extern \"C\" int foo() {}");
1714   verifyFormat("extern \"C\" int foo();");
1715   verifyFormat("extern \"C\" int foo() {\n"
1716                "  int i = 42;\n"
1717                "  return i;\n"
1718                "}");
1719 
1720   FormatStyle Style = getLLVMStyle();
1721   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1722   Style.BraceWrapping.AfterFunction = true;
1723   verifyFormat("extern \"C\" int foo() {}", Style);
1724   verifyFormat("extern \"C\" int foo();", Style);
1725   verifyFormat("extern \"C\" int foo()\n"
1726                "{\n"
1727                "  int i = 42;\n"
1728                "  return i;\n"
1729                "}",
1730                Style);
1731 
1732   Style.BraceWrapping.AfterExternBlock = true;
1733   Style.BraceWrapping.SplitEmptyRecord = false;
1734   verifyFormat("extern \"C\"\n"
1735                "{}",
1736                Style);
1737   verifyFormat("extern \"C\"\n"
1738                "{\n"
1739                "  int foo();\n"
1740                "}",
1741                Style);
1742 }
1743 
1744 TEST_F(FormatTest, FormatsInlineASM) {
1745   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
1746   verifyFormat("asm(\"nop\" ::: \"memory\");");
1747   verifyFormat(
1748       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
1749       "    \"cpuid\\n\\t\"\n"
1750       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
1751       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
1752       "    : \"a\"(value));");
1753   EXPECT_EQ(
1754       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
1755       "  __asm {\n"
1756       "        mov     edx,[that] // vtable in edx\n"
1757       "        mov     eax,methodIndex\n"
1758       "        call    [edx][eax*4] // stdcall\n"
1759       "  }\n"
1760       "}",
1761       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
1762              "    __asm {\n"
1763              "        mov     edx,[that] // vtable in edx\n"
1764              "        mov     eax,methodIndex\n"
1765              "        call    [edx][eax*4] // stdcall\n"
1766              "    }\n"
1767              "}"));
1768   EXPECT_EQ("_asm {\n"
1769             "  xor eax, eax;\n"
1770             "  cpuid;\n"
1771             "}",
1772             format("_asm {\n"
1773                    "  xor eax, eax;\n"
1774                    "  cpuid;\n"
1775                    "}"));
1776   verifyFormat("void function() {\n"
1777                "  // comment\n"
1778                "  asm(\"\");\n"
1779                "}");
1780   EXPECT_EQ("__asm {\n"
1781             "}\n"
1782             "int i;",
1783             format("__asm   {\n"
1784                    "}\n"
1785                    "int   i;"));
1786 }
1787 
1788 TEST_F(FormatTest, FormatTryCatch) {
1789   verifyFormat("try {\n"
1790                "  throw a * b;\n"
1791                "} catch (int a) {\n"
1792                "  // Do nothing.\n"
1793                "} catch (...) {\n"
1794                "  exit(42);\n"
1795                "}");
1796 
1797   // Function-level try statements.
1798   verifyFormat("int f() try { return 4; } catch (...) {\n"
1799                "  return 5;\n"
1800                "}");
1801   verifyFormat("class A {\n"
1802                "  int a;\n"
1803                "  A() try : a(0) {\n"
1804                "  } catch (...) {\n"
1805                "    throw;\n"
1806                "  }\n"
1807                "};\n");
1808 
1809   // Incomplete try-catch blocks.
1810   verifyIncompleteFormat("try {} catch (");
1811 }
1812 
1813 TEST_F(FormatTest, FormatSEHTryCatch) {
1814   verifyFormat("__try {\n"
1815                "  int a = b * c;\n"
1816                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
1817                "  // Do nothing.\n"
1818                "}");
1819 
1820   verifyFormat("__try {\n"
1821                "  int a = b * c;\n"
1822                "} __finally {\n"
1823                "  // Do nothing.\n"
1824                "}");
1825 
1826   verifyFormat("DEBUG({\n"
1827                "  __try {\n"
1828                "  } __finally {\n"
1829                "  }\n"
1830                "});\n");
1831 }
1832 
1833 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
1834   verifyFormat("try {\n"
1835                "  f();\n"
1836                "} catch {\n"
1837                "  g();\n"
1838                "}");
1839   verifyFormat("try {\n"
1840                "  f();\n"
1841                "} catch (A a) MACRO(x) {\n"
1842                "  g();\n"
1843                "} catch (B b) MACRO(x) {\n"
1844                "  g();\n"
1845                "}");
1846 }
1847 
1848 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
1849   FormatStyle Style = getLLVMStyle();
1850   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
1851                           FormatStyle::BS_WebKit}) {
1852     Style.BreakBeforeBraces = BraceStyle;
1853     verifyFormat("try {\n"
1854                  "  // something\n"
1855                  "} catch (...) {\n"
1856                  "  // something\n"
1857                  "}",
1858                  Style);
1859   }
1860   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
1861   verifyFormat("try {\n"
1862                "  // something\n"
1863                "}\n"
1864                "catch (...) {\n"
1865                "  // something\n"
1866                "}",
1867                Style);
1868   verifyFormat("__try {\n"
1869                "  // something\n"
1870                "}\n"
1871                "__finally {\n"
1872                "  // something\n"
1873                "}",
1874                Style);
1875   verifyFormat("@try {\n"
1876                "  // something\n"
1877                "}\n"
1878                "@finally {\n"
1879                "  // something\n"
1880                "}",
1881                Style);
1882   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1883   verifyFormat("try\n"
1884                "{\n"
1885                "  // something\n"
1886                "}\n"
1887                "catch (...)\n"
1888                "{\n"
1889                "  // something\n"
1890                "}",
1891                Style);
1892   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
1893   verifyFormat("try\n"
1894                "  {\n"
1895                "    // something\n"
1896                "  }\n"
1897                "catch (...)\n"
1898                "  {\n"
1899                "    // something\n"
1900                "  }",
1901                Style);
1902   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1903   Style.BraceWrapping.BeforeCatch = true;
1904   verifyFormat("try {\n"
1905                "  // something\n"
1906                "}\n"
1907                "catch (...) {\n"
1908                "  // something\n"
1909                "}",
1910                Style);
1911 }
1912 
1913 TEST_F(FormatTest, StaticInitializers) {
1914   verifyFormat("static SomeClass SC = {1, 'a'};");
1915 
1916   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
1917                "    100000000, "
1918                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
1919 
1920   // Here, everything other than the "}" would fit on a line.
1921   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
1922                "    10000000000000000000000000};");
1923   EXPECT_EQ("S s = {a,\n"
1924             "\n"
1925             "       b};",
1926             format("S s = {\n"
1927                    "  a,\n"
1928                    "\n"
1929                    "  b\n"
1930                    "};"));
1931 
1932   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
1933   // line. However, the formatting looks a bit off and this probably doesn't
1934   // happen often in practice.
1935   verifyFormat("static int Variable[1] = {\n"
1936                "    {1000000000000000000000000000000000000}};",
1937                getLLVMStyleWithColumns(40));
1938 }
1939 
1940 TEST_F(FormatTest, DesignatedInitializers) {
1941   verifyFormat("const struct A a = {.a = 1, .b = 2};");
1942   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
1943                "                    .bbbbbbbbbb = 2,\n"
1944                "                    .cccccccccc = 3,\n"
1945                "                    .dddddddddd = 4,\n"
1946                "                    .eeeeeeeeee = 5};");
1947   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
1948                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
1949                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
1950                "    .ccccccccccccccccccccccccccc = 3,\n"
1951                "    .ddddddddddddddddddddddddddd = 4,\n"
1952                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
1953 
1954   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
1955 
1956   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
1957   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
1958                "                    [2] = bbbbbbbbbb,\n"
1959                "                    [3] = cccccccccc,\n"
1960                "                    [4] = dddddddddd,\n"
1961                "                    [5] = eeeeeeeeee};");
1962   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
1963                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1964                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1965                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
1966                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
1967                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
1968 }
1969 
1970 TEST_F(FormatTest, NestedStaticInitializers) {
1971   verifyFormat("static A x = {{{}}};\n");
1972   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
1973                "               {init1, init2, init3, init4}}};",
1974                getLLVMStyleWithColumns(50));
1975 
1976   verifyFormat("somes Status::global_reps[3] = {\n"
1977                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
1978                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
1979                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
1980                getLLVMStyleWithColumns(60));
1981   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
1982                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
1983                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
1984                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
1985   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
1986                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
1987                "rect.fTop}};");
1988 
1989   verifyFormat(
1990       "SomeArrayOfSomeType a = {\n"
1991       "    {{1, 2, 3},\n"
1992       "     {1, 2, 3},\n"
1993       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
1994       "      333333333333333333333333333333},\n"
1995       "     {1, 2, 3},\n"
1996       "     {1, 2, 3}}};");
1997   verifyFormat(
1998       "SomeArrayOfSomeType a = {\n"
1999       "    {{1, 2, 3}},\n"
2000       "    {{1, 2, 3}},\n"
2001       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
2002       "      333333333333333333333333333333}},\n"
2003       "    {{1, 2, 3}},\n"
2004       "    {{1, 2, 3}}};");
2005 
2006   verifyFormat("struct {\n"
2007                "  unsigned bit;\n"
2008                "  const char *const name;\n"
2009                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
2010                "                 {kOsWin, \"Windows\"},\n"
2011                "                 {kOsLinux, \"Linux\"},\n"
2012                "                 {kOsCrOS, \"Chrome OS\"}};");
2013   verifyFormat("struct {\n"
2014                "  unsigned bit;\n"
2015                "  const char *const name;\n"
2016                "} kBitsToOs[] = {\n"
2017                "    {kOsMac, \"Mac\"},\n"
2018                "    {kOsWin, \"Windows\"},\n"
2019                "    {kOsLinux, \"Linux\"},\n"
2020                "    {kOsCrOS, \"Chrome OS\"},\n"
2021                "};");
2022 }
2023 
2024 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
2025   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2026                "                      \\\n"
2027                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
2028 }
2029 
2030 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
2031   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
2032                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
2033 
2034   // Do break defaulted and deleted functions.
2035   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2036                "    default;",
2037                getLLVMStyleWithColumns(40));
2038   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2039                "    delete;",
2040                getLLVMStyleWithColumns(40));
2041 }
2042 
2043 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
2044   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
2045                getLLVMStyleWithColumns(40));
2046   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2047                getLLVMStyleWithColumns(40));
2048   EXPECT_EQ("#define Q                              \\\n"
2049             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
2050             "  \"aaaaaaaa.cpp\"",
2051             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2052                    getLLVMStyleWithColumns(40)));
2053 }
2054 
2055 TEST_F(FormatTest, UnderstandsLinePPDirective) {
2056   EXPECT_EQ("# 123 \"A string literal\"",
2057             format("   #     123    \"A string literal\""));
2058 }
2059 
2060 TEST_F(FormatTest, LayoutUnknownPPDirective) {
2061   EXPECT_EQ("#;", format("#;"));
2062   verifyFormat("#\n;\n;\n;");
2063 }
2064 
2065 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
2066   EXPECT_EQ("#line 42 \"test\"\n",
2067             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
2068   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
2069                                     getLLVMStyleWithColumns(12)));
2070 }
2071 
2072 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
2073   EXPECT_EQ("#line 42 \"test\"",
2074             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
2075   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
2076 }
2077 
2078 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
2079   verifyFormat("#define A \\x20");
2080   verifyFormat("#define A \\ x20");
2081   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
2082   verifyFormat("#define A ''");
2083   verifyFormat("#define A ''qqq");
2084   verifyFormat("#define A `qqq");
2085   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
2086   EXPECT_EQ("const char *c = STRINGIFY(\n"
2087             "\\na : b);",
2088             format("const char * c = STRINGIFY(\n"
2089                    "\\na : b);"));
2090 
2091   verifyFormat("a\r\\");
2092   verifyFormat("a\v\\");
2093   verifyFormat("a\f\\");
2094 }
2095 
2096 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
2097   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
2098   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
2099   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
2100   // FIXME: We never break before the macro name.
2101   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
2102 
2103   verifyFormat("#define A A\n#define A A");
2104   verifyFormat("#define A(X) A\n#define A A");
2105 
2106   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
2107   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
2108 }
2109 
2110 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
2111   EXPECT_EQ("// somecomment\n"
2112             "#include \"a.h\"\n"
2113             "#define A(  \\\n"
2114             "    A, B)\n"
2115             "#include \"b.h\"\n"
2116             "// somecomment\n",
2117             format("  // somecomment\n"
2118                    "  #include \"a.h\"\n"
2119                    "#define A(A,\\\n"
2120                    "    B)\n"
2121                    "    #include \"b.h\"\n"
2122                    " // somecomment\n",
2123                    getLLVMStyleWithColumns(13)));
2124 }
2125 
2126 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
2127 
2128 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
2129   EXPECT_EQ("#define A    \\\n"
2130             "  c;         \\\n"
2131             "  e;\n"
2132             "f;",
2133             format("#define A c; e;\n"
2134                    "f;",
2135                    getLLVMStyleWithColumns(14)));
2136 }
2137 
2138 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
2139 
2140 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
2141   EXPECT_EQ("int x,\n"
2142             "#define A\n"
2143             "    y;",
2144             format("int x,\n#define A\ny;"));
2145 }
2146 
2147 TEST_F(FormatTest, HashInMacroDefinition) {
2148   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
2149   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
2150   verifyFormat("#define A  \\\n"
2151                "  {        \\\n"
2152                "    f(#c); \\\n"
2153                "  }",
2154                getLLVMStyleWithColumns(11));
2155 
2156   verifyFormat("#define A(X)         \\\n"
2157                "  void function##X()",
2158                getLLVMStyleWithColumns(22));
2159 
2160   verifyFormat("#define A(a, b, c)   \\\n"
2161                "  void a##b##c()",
2162                getLLVMStyleWithColumns(22));
2163 
2164   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
2165 }
2166 
2167 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
2168   EXPECT_EQ("#define A (x)", format("#define A (x)"));
2169   EXPECT_EQ("#define A(x)", format("#define A(x)"));
2170 }
2171 
2172 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
2173   EXPECT_EQ("#define A b;", format("#define A \\\n"
2174                                    "          \\\n"
2175                                    "  b;",
2176                                    getLLVMStyleWithColumns(25)));
2177   EXPECT_EQ("#define A \\\n"
2178             "          \\\n"
2179             "  a;      \\\n"
2180             "  b;",
2181             format("#define A \\\n"
2182                    "          \\\n"
2183                    "  a;      \\\n"
2184                    "  b;",
2185                    getLLVMStyleWithColumns(11)));
2186   EXPECT_EQ("#define A \\\n"
2187             "  a;      \\\n"
2188             "          \\\n"
2189             "  b;",
2190             format("#define A \\\n"
2191                    "  a;      \\\n"
2192                    "          \\\n"
2193                    "  b;",
2194                    getLLVMStyleWithColumns(11)));
2195 }
2196 
2197 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
2198   verifyIncompleteFormat("#define A :");
2199   verifyFormat("#define SOMECASES  \\\n"
2200                "  case 1:          \\\n"
2201                "  case 2\n",
2202                getLLVMStyleWithColumns(20));
2203   verifyFormat("#define MACRO(a) \\\n"
2204                "  if (a)         \\\n"
2205                "    f();         \\\n"
2206                "  else           \\\n"
2207                "    g()",
2208                getLLVMStyleWithColumns(18));
2209   verifyFormat("#define A template <typename T>");
2210   verifyIncompleteFormat("#define STR(x) #x\n"
2211                          "f(STR(this_is_a_string_literal{));");
2212   verifyFormat("#pragma omp threadprivate( \\\n"
2213                "    y)), // expected-warning",
2214                getLLVMStyleWithColumns(28));
2215   verifyFormat("#d, = };");
2216   verifyFormat("#if \"a");
2217   verifyIncompleteFormat("({\n"
2218                          "#define b     \\\n"
2219                          "  }           \\\n"
2220                          "  a\n"
2221                          "a",
2222                          getLLVMStyleWithColumns(15));
2223   verifyFormat("#define A     \\\n"
2224                "  {           \\\n"
2225                "    {\n"
2226                "#define B     \\\n"
2227                "  }           \\\n"
2228                "  }",
2229                getLLVMStyleWithColumns(15));
2230   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2231   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2232   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2233   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
2234 }
2235 
2236 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2237   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2238   EXPECT_EQ("class A : public QObject {\n"
2239             "  Q_OBJECT\n"
2240             "\n"
2241             "  A() {}\n"
2242             "};",
2243             format("class A  :  public QObject {\n"
2244                    "     Q_OBJECT\n"
2245                    "\n"
2246                    "  A() {\n}\n"
2247                    "}  ;"));
2248   EXPECT_EQ("MACRO\n"
2249             "/*static*/ int i;",
2250             format("MACRO\n"
2251                    " /*static*/ int   i;"));
2252   EXPECT_EQ("SOME_MACRO\n"
2253             "namespace {\n"
2254             "void f();\n"
2255             "} // namespace",
2256             format("SOME_MACRO\n"
2257                    "  namespace    {\n"
2258                    "void   f(  );\n"
2259                    "} // namespace"));
2260   // Only if the identifier contains at least 5 characters.
2261   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
2262   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
2263   // Only if everything is upper case.
2264   EXPECT_EQ("class A : public QObject {\n"
2265             "  Q_Object A() {}\n"
2266             "};",
2267             format("class A  :  public QObject {\n"
2268                    "     Q_Object\n"
2269                    "  A() {\n}\n"
2270                    "}  ;"));
2271 
2272   // Only if the next line can actually start an unwrapped line.
2273   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2274             format("SOME_WEIRD_LOG_MACRO\n"
2275                    "<< SomeThing;"));
2276 
2277   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2278                "(n, buffers))\n",
2279                getChromiumStyle(FormatStyle::LK_Cpp));
2280 }
2281 
2282 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2283   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2284             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2285             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2286             "class X {};\n"
2287             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2288             "int *createScopDetectionPass() { return 0; }",
2289             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2290                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2291                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2292                    "  class X {};\n"
2293                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2294                    "  int *createScopDetectionPass() { return 0; }"));
2295   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2296   // braces, so that inner block is indented one level more.
2297   EXPECT_EQ("int q() {\n"
2298             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2299             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2300             "  IPC_END_MESSAGE_MAP()\n"
2301             "}",
2302             format("int q() {\n"
2303                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2304                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2305                    "  IPC_END_MESSAGE_MAP()\n"
2306                    "}"));
2307 
2308   // Same inside macros.
2309   EXPECT_EQ("#define LIST(L) \\\n"
2310             "  L(A)          \\\n"
2311             "  L(B)          \\\n"
2312             "  L(C)",
2313             format("#define LIST(L) \\\n"
2314                    "  L(A) \\\n"
2315                    "  L(B) \\\n"
2316                    "  L(C)",
2317                    getGoogleStyle()));
2318 
2319   // These must not be recognized as macros.
2320   EXPECT_EQ("int q() {\n"
2321             "  f(x);\n"
2322             "  f(x) {}\n"
2323             "  f(x)->g();\n"
2324             "  f(x)->*g();\n"
2325             "  f(x).g();\n"
2326             "  f(x) = x;\n"
2327             "  f(x) += x;\n"
2328             "  f(x) -= x;\n"
2329             "  f(x) *= x;\n"
2330             "  f(x) /= x;\n"
2331             "  f(x) %= x;\n"
2332             "  f(x) &= x;\n"
2333             "  f(x) |= x;\n"
2334             "  f(x) ^= x;\n"
2335             "  f(x) >>= x;\n"
2336             "  f(x) <<= x;\n"
2337             "  f(x)[y].z();\n"
2338             "  LOG(INFO) << x;\n"
2339             "  ifstream(x) >> x;\n"
2340             "}\n",
2341             format("int q() {\n"
2342                    "  f(x)\n;\n"
2343                    "  f(x)\n {}\n"
2344                    "  f(x)\n->g();\n"
2345                    "  f(x)\n->*g();\n"
2346                    "  f(x)\n.g();\n"
2347                    "  f(x)\n = x;\n"
2348                    "  f(x)\n += x;\n"
2349                    "  f(x)\n -= x;\n"
2350                    "  f(x)\n *= x;\n"
2351                    "  f(x)\n /= x;\n"
2352                    "  f(x)\n %= x;\n"
2353                    "  f(x)\n &= x;\n"
2354                    "  f(x)\n |= x;\n"
2355                    "  f(x)\n ^= x;\n"
2356                    "  f(x)\n >>= x;\n"
2357                    "  f(x)\n <<= x;\n"
2358                    "  f(x)\n[y].z();\n"
2359                    "  LOG(INFO)\n << x;\n"
2360                    "  ifstream(x)\n >> x;\n"
2361                    "}\n"));
2362   EXPECT_EQ("int q() {\n"
2363             "  F(x)\n"
2364             "  if (1) {\n"
2365             "  }\n"
2366             "  F(x)\n"
2367             "  while (1) {\n"
2368             "  }\n"
2369             "  F(x)\n"
2370             "  G(x);\n"
2371             "  F(x)\n"
2372             "  try {\n"
2373             "    Q();\n"
2374             "  } catch (...) {\n"
2375             "  }\n"
2376             "}\n",
2377             format("int q() {\n"
2378                    "F(x)\n"
2379                    "if (1) {}\n"
2380                    "F(x)\n"
2381                    "while (1) {}\n"
2382                    "F(x)\n"
2383                    "G(x);\n"
2384                    "F(x)\n"
2385                    "try { Q(); } catch (...) {}\n"
2386                    "}\n"));
2387   EXPECT_EQ("class A {\n"
2388             "  A() : t(0) {}\n"
2389             "  A(int i) noexcept() : {}\n"
2390             "  A(X x)\n" // FIXME: function-level try blocks are broken.
2391             "  try : t(0) {\n"
2392             "  } catch (...) {\n"
2393             "  }\n"
2394             "};",
2395             format("class A {\n"
2396                    "  A()\n : t(0) {}\n"
2397                    "  A(int i)\n noexcept() : {}\n"
2398                    "  A(X x)\n"
2399                    "  try : t(0) {} catch (...) {}\n"
2400                    "};"));
2401   EXPECT_EQ("class SomeClass {\n"
2402             "public:\n"
2403             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2404             "};",
2405             format("class SomeClass {\n"
2406                    "public:\n"
2407                    "  SomeClass()\n"
2408                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2409                    "};"));
2410   EXPECT_EQ("class SomeClass {\n"
2411             "public:\n"
2412             "  SomeClass()\n"
2413             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2414             "};",
2415             format("class SomeClass {\n"
2416                    "public:\n"
2417                    "  SomeClass()\n"
2418                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2419                    "};",
2420                    getLLVMStyleWithColumns(40)));
2421 
2422   verifyFormat("MACRO(>)");
2423 }
2424 
2425 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2426   verifyFormat("#define A \\\n"
2427                "  f({     \\\n"
2428                "    g();  \\\n"
2429                "  });",
2430                getLLVMStyleWithColumns(11));
2431 }
2432 
2433 TEST_F(FormatTest, IndentPreprocessorDirectives) {
2434   FormatStyle Style = getLLVMStyle();
2435   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
2436   Style.ColumnLimit = 40;
2437   verifyFormat("#ifdef _WIN32\n"
2438                "#define A 0\n"
2439                "#ifdef VAR2\n"
2440                "#define B 1\n"
2441                "#include <someheader.h>\n"
2442                "#define MACRO                          \\\n"
2443                "  some_very_long_func_aaaaaaaaaa();\n"
2444                "#endif\n"
2445                "#else\n"
2446                "#define A 1\n"
2447                "#endif",
2448                Style);
2449   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
2450   verifyFormat("#ifdef _WIN32\n"
2451                "#  define A 0\n"
2452                "#  ifdef VAR2\n"
2453                "#    define B 1\n"
2454                "#    include <someheader.h>\n"
2455                "#    define MACRO                      \\\n"
2456                "      some_very_long_func_aaaaaaaaaa();\n"
2457                "#  endif\n"
2458                "#else\n"
2459                "#  define A 1\n"
2460                "#endif",
2461                Style);
2462   verifyFormat("#if A\n"
2463                "#  define MACRO                        \\\n"
2464                "    void a(int x) {                    \\\n"
2465                "      b();                             \\\n"
2466                "      c();                             \\\n"
2467                "      d();                             \\\n"
2468                "      e();                             \\\n"
2469                "      f();                             \\\n"
2470                "    }\n"
2471                "#endif",
2472                Style);
2473   // Comments before include guard.
2474   verifyFormat("// file comment\n"
2475                "// file comment\n"
2476                "#ifndef HEADER_H\n"
2477                "#define HEADER_H\n"
2478                "code();\n"
2479                "#endif",
2480                Style);
2481   // Test with include guards.
2482   // EXPECT_EQ is used because verifyFormat() calls messUp() which incorrectly
2483   // merges lines.
2484   verifyFormat("#ifndef HEADER_H\n"
2485                "#define HEADER_H\n"
2486                "code();\n"
2487                "#endif",
2488                Style);
2489   // Include guards must have a #define with the same variable immediately
2490   // after #ifndef.
2491   verifyFormat("#ifndef NOT_GUARD\n"
2492                "#  define FOO\n"
2493                "code();\n"
2494                "#endif",
2495                Style);
2496 
2497   // Include guards must cover the entire file.
2498   verifyFormat("code();\n"
2499                "code();\n"
2500                "#ifndef NOT_GUARD\n"
2501                "#  define NOT_GUARD\n"
2502                "code();\n"
2503                "#endif",
2504                Style);
2505   verifyFormat("#ifndef NOT_GUARD\n"
2506                "#  define NOT_GUARD\n"
2507                "code();\n"
2508                "#endif\n"
2509                "code();",
2510                Style);
2511   // Test with trailing blank lines.
2512   verifyFormat("#ifndef HEADER_H\n"
2513                "#define HEADER_H\n"
2514                "code();\n"
2515                "#endif\n",
2516                Style);
2517   // Include guards don't have #else.
2518   verifyFormat("#ifndef NOT_GUARD\n"
2519                "#  define NOT_GUARD\n"
2520                "code();\n"
2521                "#else\n"
2522                "#endif",
2523                Style);
2524   verifyFormat("#ifndef NOT_GUARD\n"
2525                "#  define NOT_GUARD\n"
2526                "code();\n"
2527                "#elif FOO\n"
2528                "#endif",
2529                Style);
2530   // FIXME: This doesn't handle the case where there's code between the
2531   // #ifndef and #define but all other conditions hold. This is because when
2532   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
2533   // previous code line yet, so we can't detect it.
2534   EXPECT_EQ("#ifndef NOT_GUARD\n"
2535             "code();\n"
2536             "#define NOT_GUARD\n"
2537             "code();\n"
2538             "#endif",
2539             format("#ifndef NOT_GUARD\n"
2540                    "code();\n"
2541                    "#  define NOT_GUARD\n"
2542                    "code();\n"
2543                    "#endif",
2544                    Style));
2545   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
2546   // be outside an include guard. Examples are #pragma once and
2547   // #pragma GCC diagnostic, or anything else that does not change the meaning
2548   // of the file if it's included multiple times.
2549   EXPECT_EQ("#ifdef WIN32\n"
2550             "#  pragma once\n"
2551             "#endif\n"
2552             "#ifndef HEADER_H\n"
2553             "#  define HEADER_H\n"
2554             "code();\n"
2555             "#endif",
2556             format("#ifdef WIN32\n"
2557                    "#  pragma once\n"
2558                    "#endif\n"
2559                    "#ifndef HEADER_H\n"
2560                    "#define HEADER_H\n"
2561                    "code();\n"
2562                    "#endif",
2563                    Style));
2564   // FIXME: This does not detect when there is a single non-preprocessor line
2565   // in front of an include-guard-like structure where other conditions hold
2566   // because ScopedLineState hides the line.
2567   EXPECT_EQ("code();\n"
2568             "#ifndef HEADER_H\n"
2569             "#define HEADER_H\n"
2570             "code();\n"
2571             "#endif",
2572             format("code();\n"
2573                    "#ifndef HEADER_H\n"
2574                    "#  define HEADER_H\n"
2575                    "code();\n"
2576                    "#endif",
2577                    Style));
2578   // FIXME: The comment indent corrector in TokenAnnotator gets thrown off by
2579   // preprocessor indentation.
2580   EXPECT_EQ("#if 1\n"
2581             "  // comment\n"
2582             "#  define A 0\n"
2583             "// comment\n"
2584             "#  define B 0\n"
2585             "#endif",
2586             format("#if 1\n"
2587                    "// comment\n"
2588                    "#  define A 0\n"
2589                    "   // comment\n"
2590                    "#  define B 0\n"
2591                    "#endif",
2592                    Style));
2593   // Test with tabs.
2594   Style.UseTab = FormatStyle::UT_Always;
2595   Style.IndentWidth = 8;
2596   Style.TabWidth = 8;
2597   verifyFormat("#ifdef _WIN32\n"
2598                "#\tdefine A 0\n"
2599                "#\tifdef VAR2\n"
2600                "#\t\tdefine B 1\n"
2601                "#\t\tinclude <someheader.h>\n"
2602                "#\t\tdefine MACRO          \\\n"
2603                "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
2604                "#\tendif\n"
2605                "#else\n"
2606                "#\tdefine A 1\n"
2607                "#endif",
2608                Style);
2609 
2610   // Regression test: Multiline-macro inside include guards.
2611   verifyFormat("#ifndef HEADER_H\n"
2612                "#define HEADER_H\n"
2613                "#define A()        \\\n"
2614                "  int i;           \\\n"
2615                "  int j;\n"
2616                "#endif // HEADER_H",
2617                getLLVMStyleWithColumns(20));
2618 }
2619 
2620 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
2621   verifyFormat("{\n  { a #c; }\n}");
2622 }
2623 
2624 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
2625   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
2626             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
2627   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
2628             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
2629 }
2630 
2631 TEST_F(FormatTest, EscapedNewlines) {
2632   FormatStyle Narrow = getLLVMStyleWithColumns(11);
2633   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
2634             format("#define A \\\nint i;\\\n  int j;", Narrow));
2635   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
2636   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2637   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
2638   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
2639 
2640   FormatStyle AlignLeft = getLLVMStyle();
2641   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
2642   EXPECT_EQ("#define MACRO(x) \\\n"
2643             "private:         \\\n"
2644             "  int x(int a);\n",
2645             format("#define MACRO(x) \\\n"
2646                    "private:         \\\n"
2647                    "  int x(int a);\n",
2648                    AlignLeft));
2649 
2650   // CRLF line endings
2651   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
2652             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
2653   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
2654   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2655   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
2656   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
2657   EXPECT_EQ("#define MACRO(x) \\\r\n"
2658             "private:         \\\r\n"
2659             "  int x(int a);\r\n",
2660             format("#define MACRO(x) \\\r\n"
2661                    "private:         \\\r\n"
2662                    "  int x(int a);\r\n",
2663                    AlignLeft));
2664 
2665   FormatStyle DontAlign = getLLVMStyle();
2666   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
2667   DontAlign.MaxEmptyLinesToKeep = 3;
2668   // FIXME: can't use verifyFormat here because the newline before
2669   // "public:" is not inserted the first time it's reformatted
2670   EXPECT_EQ("#define A \\\n"
2671             "  class Foo { \\\n"
2672             "    void bar(); \\\n"
2673             "\\\n"
2674             "\\\n"
2675             "\\\n"
2676             "  public: \\\n"
2677             "    void baz(); \\\n"
2678             "  };",
2679             format("#define A \\\n"
2680                    "  class Foo { \\\n"
2681                    "    void bar(); \\\n"
2682                    "\\\n"
2683                    "\\\n"
2684                    "\\\n"
2685                    "  public: \\\n"
2686                    "    void baz(); \\\n"
2687                    "  };",
2688                    DontAlign));
2689 }
2690 
2691 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
2692   verifyFormat("#define A \\\n"
2693                "  int v(  \\\n"
2694                "      a); \\\n"
2695                "  int i;",
2696                getLLVMStyleWithColumns(11));
2697 }
2698 
2699 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
2700   EXPECT_EQ(
2701       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2702       "                      \\\n"
2703       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2704       "\n"
2705       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2706       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
2707       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
2708              "\\\n"
2709              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2710              "  \n"
2711              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2712              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
2713 }
2714 
2715 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
2716   EXPECT_EQ("int\n"
2717             "#define A\n"
2718             "    a;",
2719             format("int\n#define A\na;"));
2720   verifyFormat("functionCallTo(\n"
2721                "    someOtherFunction(\n"
2722                "        withSomeParameters, whichInSequence,\n"
2723                "        areLongerThanALine(andAnotherCall,\n"
2724                "#define A B\n"
2725                "                           withMoreParamters,\n"
2726                "                           whichStronglyInfluenceTheLayout),\n"
2727                "        andMoreParameters),\n"
2728                "    trailing);",
2729                getLLVMStyleWithColumns(69));
2730   verifyFormat("Foo::Foo()\n"
2731                "#ifdef BAR\n"
2732                "    : baz(0)\n"
2733                "#endif\n"
2734                "{\n"
2735                "}");
2736   verifyFormat("void f() {\n"
2737                "  if (true)\n"
2738                "#ifdef A\n"
2739                "    f(42);\n"
2740                "  x();\n"
2741                "#else\n"
2742                "    g();\n"
2743                "  x();\n"
2744                "#endif\n"
2745                "}");
2746   verifyFormat("void f(param1, param2,\n"
2747                "       param3,\n"
2748                "#ifdef A\n"
2749                "       param4(param5,\n"
2750                "#ifdef A1\n"
2751                "              param6,\n"
2752                "#ifdef A2\n"
2753                "              param7),\n"
2754                "#else\n"
2755                "              param8),\n"
2756                "       param9,\n"
2757                "#endif\n"
2758                "       param10,\n"
2759                "#endif\n"
2760                "       param11)\n"
2761                "#else\n"
2762                "       param12)\n"
2763                "#endif\n"
2764                "{\n"
2765                "  x();\n"
2766                "}",
2767                getLLVMStyleWithColumns(28));
2768   verifyFormat("#if 1\n"
2769                "int i;");
2770   verifyFormat("#if 1\n"
2771                "#endif\n"
2772                "#if 1\n"
2773                "#else\n"
2774                "#endif\n");
2775   verifyFormat("DEBUG({\n"
2776                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2777                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2778                "});\n"
2779                "#if a\n"
2780                "#else\n"
2781                "#endif");
2782 
2783   verifyIncompleteFormat("void f(\n"
2784                          "#if A\n"
2785                          ");\n"
2786                          "#else\n"
2787                          "#endif");
2788 }
2789 
2790 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
2791   verifyFormat("#endif\n"
2792                "#if B");
2793 }
2794 
2795 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
2796   FormatStyle SingleLine = getLLVMStyle();
2797   SingleLine.AllowShortIfStatementsOnASingleLine = true;
2798   verifyFormat("#if 0\n"
2799                "#elif 1\n"
2800                "#endif\n"
2801                "void foo() {\n"
2802                "  if (test) foo2();\n"
2803                "}",
2804                SingleLine);
2805 }
2806 
2807 TEST_F(FormatTest, LayoutBlockInsideParens) {
2808   verifyFormat("functionCall({ int i; });");
2809   verifyFormat("functionCall({\n"
2810                "  int i;\n"
2811                "  int j;\n"
2812                "});");
2813   verifyFormat("functionCall(\n"
2814                "    {\n"
2815                "      int i;\n"
2816                "      int j;\n"
2817                "    },\n"
2818                "    aaaa, bbbb, cccc);");
2819   verifyFormat("functionA(functionB({\n"
2820                "            int i;\n"
2821                "            int j;\n"
2822                "          }),\n"
2823                "          aaaa, bbbb, cccc);");
2824   verifyFormat("functionCall(\n"
2825                "    {\n"
2826                "      int i;\n"
2827                "      int j;\n"
2828                "    },\n"
2829                "    aaaa, bbbb, // comment\n"
2830                "    cccc);");
2831   verifyFormat("functionA(functionB({\n"
2832                "            int i;\n"
2833                "            int j;\n"
2834                "          }),\n"
2835                "          aaaa, bbbb, // comment\n"
2836                "          cccc);");
2837   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
2838   verifyFormat("functionCall(aaaa, bbbb, {\n"
2839                "  int i;\n"
2840                "  int j;\n"
2841                "});");
2842   verifyFormat(
2843       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
2844       "    {\n"
2845       "      int i; // break\n"
2846       "    },\n"
2847       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
2848       "                                     ccccccccccccccccc));");
2849   verifyFormat("DEBUG({\n"
2850                "  if (a)\n"
2851                "    f();\n"
2852                "});");
2853 }
2854 
2855 TEST_F(FormatTest, LayoutBlockInsideStatement) {
2856   EXPECT_EQ("SOME_MACRO { int i; }\n"
2857             "int i;",
2858             format("  SOME_MACRO  {int i;}  int i;"));
2859 }
2860 
2861 TEST_F(FormatTest, LayoutNestedBlocks) {
2862   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
2863                "  struct s {\n"
2864                "    int i;\n"
2865                "  };\n"
2866                "  s kBitsToOs[] = {{10}};\n"
2867                "  for (int i = 0; i < 10; ++i)\n"
2868                "    return;\n"
2869                "}");
2870   verifyFormat("call(parameter, {\n"
2871                "  something();\n"
2872                "  // Comment using all columns.\n"
2873                "  somethingelse();\n"
2874                "});",
2875                getLLVMStyleWithColumns(40));
2876   verifyFormat("DEBUG( //\n"
2877                "    { f(); }, a);");
2878   verifyFormat("DEBUG( //\n"
2879                "    {\n"
2880                "      f(); //\n"
2881                "    },\n"
2882                "    a);");
2883 
2884   EXPECT_EQ("call(parameter, {\n"
2885             "  something();\n"
2886             "  // Comment too\n"
2887             "  // looooooooooong.\n"
2888             "  somethingElse();\n"
2889             "});",
2890             format("call(parameter, {\n"
2891                    "  something();\n"
2892                    "  // Comment too looooooooooong.\n"
2893                    "  somethingElse();\n"
2894                    "});",
2895                    getLLVMStyleWithColumns(29)));
2896   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
2897   EXPECT_EQ("DEBUG({ // comment\n"
2898             "  int i;\n"
2899             "});",
2900             format("DEBUG({ // comment\n"
2901                    "int  i;\n"
2902                    "});"));
2903   EXPECT_EQ("DEBUG({\n"
2904             "  int i;\n"
2905             "\n"
2906             "  // comment\n"
2907             "  int j;\n"
2908             "});",
2909             format("DEBUG({\n"
2910                    "  int  i;\n"
2911                    "\n"
2912                    "  // comment\n"
2913                    "  int  j;\n"
2914                    "});"));
2915 
2916   verifyFormat("DEBUG({\n"
2917                "  if (a)\n"
2918                "    return;\n"
2919                "});");
2920   verifyGoogleFormat("DEBUG({\n"
2921                      "  if (a) return;\n"
2922                      "});");
2923   FormatStyle Style = getGoogleStyle();
2924   Style.ColumnLimit = 45;
2925   verifyFormat("Debug(aaaaa,\n"
2926                "      {\n"
2927                "        if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
2928                "      },\n"
2929                "      a);",
2930                Style);
2931 
2932   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
2933 
2934   verifyNoCrash("^{v^{a}}");
2935 }
2936 
2937 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
2938   EXPECT_EQ("#define MACRO()                     \\\n"
2939             "  Debug(aaa, /* force line break */ \\\n"
2940             "        {                           \\\n"
2941             "          int i;                    \\\n"
2942             "          int j;                    \\\n"
2943             "        })",
2944             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
2945                    "          {  int   i;  int  j;   })",
2946                    getGoogleStyle()));
2947 
2948   EXPECT_EQ("#define A                                       \\\n"
2949             "  [] {                                          \\\n"
2950             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
2951             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
2952             "  }",
2953             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
2954                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
2955                    getGoogleStyle()));
2956 }
2957 
2958 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
2959   EXPECT_EQ("{}", format("{}"));
2960   verifyFormat("enum E {};");
2961   verifyFormat("enum E {}");
2962 }
2963 
2964 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
2965   FormatStyle Style = getLLVMStyle();
2966   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
2967   Style.MacroBlockEnd = "^[A-Z_]+_END$";
2968   verifyFormat("FOO_BEGIN\n"
2969                "  FOO_ENTRY\n"
2970                "FOO_END", Style);
2971   verifyFormat("FOO_BEGIN\n"
2972                "  NESTED_FOO_BEGIN\n"
2973                "    NESTED_FOO_ENTRY\n"
2974                "  NESTED_FOO_END\n"
2975                "FOO_END", Style);
2976   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
2977                "  int x;\n"
2978                "  x = 1;\n"
2979                "FOO_END(Baz)", Style);
2980 }
2981 
2982 //===----------------------------------------------------------------------===//
2983 // Line break tests.
2984 //===----------------------------------------------------------------------===//
2985 
2986 TEST_F(FormatTest, PreventConfusingIndents) {
2987   verifyFormat(
2988       "void f() {\n"
2989       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
2990       "                         parameter, parameter, parameter)),\n"
2991       "                     SecondLongCall(parameter));\n"
2992       "}");
2993   verifyFormat(
2994       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2995       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
2996       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2997       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
2998   verifyFormat(
2999       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3000       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
3001       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
3002       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
3003   verifyFormat(
3004       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3005       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
3006       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
3007       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
3008   verifyFormat("int a = bbbb && ccc &&\n"
3009                "        fffff(\n"
3010                "#define A Just forcing a new line\n"
3011                "            ddd);");
3012 }
3013 
3014 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
3015   verifyFormat(
3016       "bool aaaaaaa =\n"
3017       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
3018       "    bbbbbbbb();");
3019   verifyFormat(
3020       "bool aaaaaaa =\n"
3021       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
3022       "    bbbbbbbb();");
3023 
3024   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3025                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
3026                "    ccccccccc == ddddddddddd;");
3027   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3028                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
3029                "    ccccccccc == ddddddddddd;");
3030   verifyFormat(
3031       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3032       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
3033       "    ccccccccc == ddddddddddd;");
3034 
3035   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3036                "                 aaaaaa) &&\n"
3037                "         bbbbbb && cccccc;");
3038   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3039                "                 aaaaaa) >>\n"
3040                "         bbbbbb;");
3041   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
3042                "    SourceMgr.getSpellingColumnNumber(\n"
3043                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
3044                "    1);");
3045 
3046   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3047                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
3048                "    cccccc) {\n}");
3049   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3050                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
3051                "              cccccc) {\n}");
3052   verifyFormat("b = a &&\n"
3053                "    // Comment\n"
3054                "    b.c && d;");
3055 
3056   // If the LHS of a comparison is not a binary expression itself, the
3057   // additional linebreak confuses many people.
3058   verifyFormat(
3059       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3060       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
3061       "}");
3062   verifyFormat(
3063       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3064       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3065       "}");
3066   verifyFormat(
3067       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
3068       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3069       "}");
3070   // Even explicit parentheses stress the precedence enough to make the
3071   // additional break unnecessary.
3072   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3073                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3074                "}");
3075   // This cases is borderline, but with the indentation it is still readable.
3076   verifyFormat(
3077       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3078       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3079       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
3080       "}",
3081       getLLVMStyleWithColumns(75));
3082 
3083   // If the LHS is a binary expression, we should still use the additional break
3084   // as otherwise the formatting hides the operator precedence.
3085   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3086                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3087                "    5) {\n"
3088                "}");
3089 
3090   FormatStyle OnePerLine = getLLVMStyle();
3091   OnePerLine.BinPackParameters = false;
3092   verifyFormat(
3093       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3094       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3095       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
3096       OnePerLine);
3097 
3098   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
3099                "                .aaa(aaaaaaaaaaaaa) *\n"
3100                "            aaaaaaa +\n"
3101                "        aaaaaaa;",
3102                getLLVMStyleWithColumns(40));
3103 }
3104 
3105 TEST_F(FormatTest, ExpressionIndentation) {
3106   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3107                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3108                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3109                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3110                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3111                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
3112                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3113                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
3114                "                 ccccccccccccccccccccccccccccccccccccccccc;");
3115   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3116                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3117                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3118                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3119   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3120                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3121                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3122                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3123   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3124                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3125                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3126                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3127   verifyFormat("if () {\n"
3128                "} else if (aaaaa && bbbbb > // break\n"
3129                "                        ccccc) {\n"
3130                "}");
3131   verifyFormat("if () {\n"
3132                "} else if (aaaaa &&\n"
3133                "           bbbbb > // break\n"
3134                "               ccccc &&\n"
3135                "           ddddd) {\n"
3136                "}");
3137 
3138   // Presence of a trailing comment used to change indentation of b.
3139   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
3140                "       b;\n"
3141                "return aaaaaaaaaaaaaaaaaaa +\n"
3142                "       b; //",
3143                getLLVMStyleWithColumns(30));
3144 }
3145 
3146 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
3147   // Not sure what the best system is here. Like this, the LHS can be found
3148   // immediately above an operator (everything with the same or a higher
3149   // indent). The RHS is aligned right of the operator and so compasses
3150   // everything until something with the same indent as the operator is found.
3151   // FIXME: Is this a good system?
3152   FormatStyle Style = getLLVMStyle();
3153   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
3154   verifyFormat(
3155       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3156       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3157       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3158       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3159       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3160       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3161       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3162       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3163       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
3164       Style);
3165   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3166                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3167                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3168                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3169                Style);
3170   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3171                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3172                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3173                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3174                Style);
3175   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3176                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3177                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3178                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3179                Style);
3180   verifyFormat("if () {\n"
3181                "} else if (aaaaa\n"
3182                "           && bbbbb // break\n"
3183                "                  > ccccc) {\n"
3184                "}",
3185                Style);
3186   verifyFormat("return (a)\n"
3187                "       // comment\n"
3188                "       + b;",
3189                Style);
3190   verifyFormat(
3191       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3192       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3193       "             + cc;",
3194       Style);
3195 
3196   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3197                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3198                Style);
3199 
3200   // Forced by comments.
3201   verifyFormat(
3202       "unsigned ContentSize =\n"
3203       "    sizeof(int16_t)   // DWARF ARange version number\n"
3204       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
3205       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
3206       "    + sizeof(int8_t); // Segment Size (in bytes)");
3207 
3208   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
3209                "       == boost::fusion::at_c<1>(iiii).second;",
3210                Style);
3211 
3212   Style.ColumnLimit = 60;
3213   verifyFormat("zzzzzzzzzz\n"
3214                "    = bbbbbbbbbbbbbbbbb\n"
3215                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
3216                Style);
3217 }
3218 
3219 TEST_F(FormatTest, EnforcedOperatorWraps) {
3220   // Here we'd like to wrap after the || operators, but a comment is forcing an
3221   // earlier wrap.
3222   verifyFormat("bool x = aaaaa //\n"
3223                "         || bbbbb\n"
3224                "         //\n"
3225                "         || cccc;");
3226 }
3227 
3228 TEST_F(FormatTest, NoOperandAlignment) {
3229   FormatStyle Style = getLLVMStyle();
3230   Style.AlignOperands = false;
3231   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
3232                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3233                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3234                Style);
3235   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3236   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3237                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3238                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3239                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3240                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3241                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3242                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3243                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3244                "        > ccccccccccccccccccccccccccccccccccccccccc;",
3245                Style);
3246 
3247   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3248                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3249                "    + cc;",
3250                Style);
3251   verifyFormat("int a = aa\n"
3252                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3253                "        * cccccccccccccccccccccccccccccccccccc;\n",
3254                Style);
3255 
3256   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3257   verifyFormat("return (a > b\n"
3258                "    // comment1\n"
3259                "    // comment2\n"
3260                "    || c);",
3261                Style);
3262 }
3263 
3264 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
3265   FormatStyle Style = getLLVMStyle();
3266   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3267   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3268                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3269                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
3270                Style);
3271 }
3272 
3273 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
3274   FormatStyle Style = getLLVMStyle();
3275   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3276   Style.BinPackArguments = false;
3277   Style.ColumnLimit = 40;
3278   verifyFormat("void test() {\n"
3279                "  someFunction(\n"
3280                "      this + argument + is + quite\n"
3281                "      + long + so + it + gets + wrapped\n"
3282                "      + but + remains + bin - packed);\n"
3283                "}",
3284                Style);
3285   verifyFormat("void test() {\n"
3286                "  someFunction(arg1,\n"
3287                "               this + argument + is\n"
3288                "                   + quite + long + so\n"
3289                "                   + it + gets + wrapped\n"
3290                "                   + but + remains + bin\n"
3291                "                   - packed,\n"
3292                "               arg3);\n"
3293                "}",
3294                Style);
3295   verifyFormat("void test() {\n"
3296                "  someFunction(\n"
3297                "      arg1,\n"
3298                "      this + argument + has\n"
3299                "          + anotherFunc(nested,\n"
3300                "                        calls + whose\n"
3301                "                            + arguments\n"
3302                "                            + are + also\n"
3303                "                            + wrapped,\n"
3304                "                        in + addition)\n"
3305                "          + to + being + bin - packed,\n"
3306                "      arg3);\n"
3307                "}",
3308                Style);
3309 
3310   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
3311   verifyFormat("void test() {\n"
3312                "  someFunction(\n"
3313                "      arg1,\n"
3314                "      this + argument + has +\n"
3315                "          anotherFunc(nested,\n"
3316                "                      calls + whose +\n"
3317                "                          arguments +\n"
3318                "                          are + also +\n"
3319                "                          wrapped,\n"
3320                "                      in + addition) +\n"
3321                "          to + being + bin - packed,\n"
3322                "      arg3);\n"
3323                "}",
3324                Style);
3325 }
3326 
3327 TEST_F(FormatTest, ConstructorInitializers) {
3328   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3329   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
3330                getLLVMStyleWithColumns(45));
3331   verifyFormat("Constructor()\n"
3332                "    : Inttializer(FitsOnTheLine) {}",
3333                getLLVMStyleWithColumns(44));
3334   verifyFormat("Constructor()\n"
3335                "    : Inttializer(FitsOnTheLine) {}",
3336                getLLVMStyleWithColumns(43));
3337 
3338   verifyFormat("template <typename T>\n"
3339                "Constructor() : Initializer(FitsOnTheLine) {}",
3340                getLLVMStyleWithColumns(45));
3341 
3342   verifyFormat(
3343       "SomeClass::Constructor()\n"
3344       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3345 
3346   verifyFormat(
3347       "SomeClass::Constructor()\n"
3348       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3349       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
3350   verifyFormat(
3351       "SomeClass::Constructor()\n"
3352       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3353       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3354   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3355                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3356                "    : aaaaaaaaaa(aaaaaa) {}");
3357 
3358   verifyFormat("Constructor()\n"
3359                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3360                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3361                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3362                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
3363 
3364   verifyFormat("Constructor()\n"
3365                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3366                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3367 
3368   verifyFormat("Constructor(int Parameter = 0)\n"
3369                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3370                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
3371   verifyFormat("Constructor()\n"
3372                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3373                "}",
3374                getLLVMStyleWithColumns(60));
3375   verifyFormat("Constructor()\n"
3376                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3377                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
3378 
3379   // Here a line could be saved by splitting the second initializer onto two
3380   // lines, but that is not desirable.
3381   verifyFormat("Constructor()\n"
3382                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3383                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
3384                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3385 
3386   FormatStyle OnePerLine = getLLVMStyle();
3387   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3388   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3389   verifyFormat("SomeClass::Constructor()\n"
3390                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3391                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3392                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3393                OnePerLine);
3394   verifyFormat("SomeClass::Constructor()\n"
3395                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3396                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3397                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3398                OnePerLine);
3399   verifyFormat("MyClass::MyClass(int var)\n"
3400                "    : some_var_(var),            // 4 space indent\n"
3401                "      some_other_var_(var + 1) { // lined up\n"
3402                "}",
3403                OnePerLine);
3404   verifyFormat("Constructor()\n"
3405                "    : aaaaa(aaaaaa),\n"
3406                "      aaaaa(aaaaaa),\n"
3407                "      aaaaa(aaaaaa),\n"
3408                "      aaaaa(aaaaaa),\n"
3409                "      aaaaa(aaaaaa) {}",
3410                OnePerLine);
3411   verifyFormat("Constructor()\n"
3412                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3413                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
3414                OnePerLine);
3415   OnePerLine.BinPackParameters = false;
3416   verifyFormat(
3417       "Constructor()\n"
3418       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3419       "          aaaaaaaaaaa().aaa(),\n"
3420       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3421       OnePerLine);
3422   OnePerLine.ColumnLimit = 60;
3423   verifyFormat("Constructor()\n"
3424                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
3425                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3426                OnePerLine);
3427 
3428   EXPECT_EQ("Constructor()\n"
3429             "    : // Comment forcing unwanted break.\n"
3430             "      aaaa(aaaa) {}",
3431             format("Constructor() :\n"
3432                    "    // Comment forcing unwanted break.\n"
3433                    "    aaaa(aaaa) {}"));
3434 }
3435 
3436 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
3437   FormatStyle Style = getLLVMStyle();
3438   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
3439 
3440   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3441   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
3442                getStyleWithColumns(Style, 45));
3443   verifyFormat("Constructor() :\n"
3444                "    Initializer(FitsOnTheLine) {}",
3445                getStyleWithColumns(Style, 44));
3446   verifyFormat("Constructor() :\n"
3447                "    Initializer(FitsOnTheLine) {}",
3448                getStyleWithColumns(Style, 43));
3449 
3450   verifyFormat("template <typename T>\n"
3451                "Constructor() : Initializer(FitsOnTheLine) {}",
3452                getStyleWithColumns(Style, 50));
3453 
3454   verifyFormat(
3455       "SomeClass::Constructor() :\n"
3456       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
3457 	  Style);
3458 
3459   verifyFormat(
3460       "SomeClass::Constructor() :\n"
3461       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3462       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3463 	  Style);
3464   verifyFormat(
3465       "SomeClass::Constructor() :\n"
3466       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3467       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
3468 	  Style);
3469   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3470                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
3471                "    aaaaaaaaaa(aaaaaa) {}",
3472 			   Style);
3473 
3474   verifyFormat("Constructor() :\n"
3475                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3476                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3477                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3478                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
3479 			   Style);
3480 
3481   verifyFormat("Constructor() :\n"
3482                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3483                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3484 			   Style);
3485 
3486   verifyFormat("Constructor(int Parameter = 0) :\n"
3487                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3488                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
3489 			   Style);
3490   verifyFormat("Constructor() :\n"
3491                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3492                "}",
3493                getStyleWithColumns(Style, 60));
3494   verifyFormat("Constructor() :\n"
3495                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3496                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
3497 			   Style);
3498 
3499   // Here a line could be saved by splitting the second initializer onto two
3500   // lines, but that is not desirable.
3501   verifyFormat("Constructor() :\n"
3502                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3503                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
3504                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3505 			   Style);
3506 
3507   FormatStyle OnePerLine = Style;
3508   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3509   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3510   verifyFormat("SomeClass::Constructor() :\n"
3511                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3512                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3513                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3514                OnePerLine);
3515   verifyFormat("SomeClass::Constructor() :\n"
3516                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3517                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3518                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3519                OnePerLine);
3520   verifyFormat("MyClass::MyClass(int var) :\n"
3521                "    some_var_(var),            // 4 space indent\n"
3522                "    some_other_var_(var + 1) { // lined up\n"
3523                "}",
3524                OnePerLine);
3525   verifyFormat("Constructor() :\n"
3526                "    aaaaa(aaaaaa),\n"
3527                "    aaaaa(aaaaaa),\n"
3528                "    aaaaa(aaaaaa),\n"
3529                "    aaaaa(aaaaaa),\n"
3530                "    aaaaa(aaaaaa) {}",
3531                OnePerLine);
3532   verifyFormat("Constructor() :\n"
3533                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3534                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
3535                OnePerLine);
3536   OnePerLine.BinPackParameters = false;
3537   verifyFormat(
3538       "Constructor() :\n"
3539       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3540       "        aaaaaaaaaaa().aaa(),\n"
3541       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3542       OnePerLine);
3543   OnePerLine.ColumnLimit = 60;
3544   verifyFormat("Constructor() :\n"
3545                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
3546                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3547                OnePerLine);
3548 
3549   EXPECT_EQ("Constructor() :\n"
3550             "    // Comment forcing unwanted break.\n"
3551             "    aaaa(aaaa) {}",
3552             format("Constructor() :\n"
3553                    "    // Comment forcing unwanted break.\n"
3554                    "    aaaa(aaaa) {}",
3555 				   Style));
3556 
3557   Style.ColumnLimit = 0;
3558   verifyFormat("SomeClass::Constructor() :\n"
3559                "    a(a) {}",
3560                Style);
3561   verifyFormat("SomeClass::Constructor() noexcept :\n"
3562                "    a(a) {}",
3563                Style);
3564   verifyFormat("SomeClass::Constructor() :\n"
3565 			   "    a(a), b(b), c(c) {}",
3566                Style);
3567   verifyFormat("SomeClass::Constructor() :\n"
3568                "    a(a) {\n"
3569                "  foo();\n"
3570                "  bar();\n"
3571                "}",
3572                Style);
3573 
3574   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3575   verifyFormat("SomeClass::Constructor() :\n"
3576 			   "    a(a), b(b), c(c) {\n"
3577 			   "}",
3578                Style);
3579   verifyFormat("SomeClass::Constructor() :\n"
3580                "    a(a) {\n"
3581 			   "}",
3582                Style);
3583 
3584   Style.ColumnLimit = 80;
3585   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3586   Style.ConstructorInitializerIndentWidth = 2;
3587   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}",
3588                Style);
3589   verifyFormat("SomeClass::Constructor() :\n"
3590                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3591                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
3592                Style);
3593 }
3594 
3595 #ifndef EXPENSIVE_CHECKS
3596 // Expensive checks enables libstdc++ checking which includes validating the
3597 // state of ranges used in std::priority_queue - this blows out the
3598 // runtime/scalability of the function and makes this test unacceptably slow.
3599 TEST_F(FormatTest, MemoizationTests) {
3600   // This breaks if the memoization lookup does not take \c Indent and
3601   // \c LastSpace into account.
3602   verifyFormat(
3603       "extern CFRunLoopTimerRef\n"
3604       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
3605       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
3606       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
3607       "                     CFRunLoopTimerContext *context) {}");
3608 
3609   // Deep nesting somewhat works around our memoization.
3610   verifyFormat(
3611       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3612       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3613       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3614       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3615       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
3616       getLLVMStyleWithColumns(65));
3617   verifyFormat(
3618       "aaaaa(\n"
3619       "    aaaaa,\n"
3620       "    aaaaa(\n"
3621       "        aaaaa,\n"
3622       "        aaaaa(\n"
3623       "            aaaaa,\n"
3624       "            aaaaa(\n"
3625       "                aaaaa,\n"
3626       "                aaaaa(\n"
3627       "                    aaaaa,\n"
3628       "                    aaaaa(\n"
3629       "                        aaaaa,\n"
3630       "                        aaaaa(\n"
3631       "                            aaaaa,\n"
3632       "                            aaaaa(\n"
3633       "                                aaaaa,\n"
3634       "                                aaaaa(\n"
3635       "                                    aaaaa,\n"
3636       "                                    aaaaa(\n"
3637       "                                        aaaaa,\n"
3638       "                                        aaaaa(\n"
3639       "                                            aaaaa,\n"
3640       "                                            aaaaa(\n"
3641       "                                                aaaaa,\n"
3642       "                                                aaaaa))))))))))));",
3643       getLLVMStyleWithColumns(65));
3644   verifyFormat(
3645       "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"
3646       "                                  a),\n"
3647       "                                a),\n"
3648       "                              a),\n"
3649       "                            a),\n"
3650       "                          a),\n"
3651       "                        a),\n"
3652       "                      a),\n"
3653       "                    a),\n"
3654       "                  a),\n"
3655       "                a),\n"
3656       "              a),\n"
3657       "            a),\n"
3658       "          a),\n"
3659       "        a),\n"
3660       "      a),\n"
3661       "    a),\n"
3662       "  a)",
3663       getLLVMStyleWithColumns(65));
3664 
3665   // This test takes VERY long when memoization is broken.
3666   FormatStyle OnePerLine = getLLVMStyle();
3667   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3668   OnePerLine.BinPackParameters = false;
3669   std::string input = "Constructor()\n"
3670                       "    : aaaa(a,\n";
3671   for (unsigned i = 0, e = 80; i != e; ++i) {
3672     input += "           a,\n";
3673   }
3674   input += "           a) {}";
3675   verifyFormat(input, OnePerLine);
3676 }
3677 #endif
3678 
3679 TEST_F(FormatTest, BreaksAsHighAsPossible) {
3680   verifyFormat(
3681       "void f() {\n"
3682       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
3683       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
3684       "    f();\n"
3685       "}");
3686   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
3687                "    Intervals[i - 1].getRange().getLast()) {\n}");
3688 }
3689 
3690 TEST_F(FormatTest, BreaksFunctionDeclarations) {
3691   // Principially, we break function declarations in a certain order:
3692   // 1) break amongst arguments.
3693   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
3694                "                              Cccccccccccccc cccccccccccccc);");
3695   verifyFormat("template <class TemplateIt>\n"
3696                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
3697                "                            TemplateIt *stop) {}");
3698 
3699   // 2) break after return type.
3700   verifyFormat(
3701       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3702       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
3703       getGoogleStyle());
3704 
3705   // 3) break after (.
3706   verifyFormat(
3707       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
3708       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
3709       getGoogleStyle());
3710 
3711   // 4) break before after nested name specifiers.
3712   verifyFormat(
3713       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3714       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
3715       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
3716       getGoogleStyle());
3717 
3718   // However, there are exceptions, if a sufficient amount of lines can be
3719   // saved.
3720   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
3721   // more adjusting.
3722   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3723                "                                  Cccccccccccccc cccccccccc,\n"
3724                "                                  Cccccccccccccc cccccccccc,\n"
3725                "                                  Cccccccccccccc cccccccccc,\n"
3726                "                                  Cccccccccccccc cccccccccc);");
3727   verifyFormat(
3728       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3729       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3730       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3731       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
3732       getGoogleStyle());
3733   verifyFormat(
3734       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3735       "                                          Cccccccccccccc cccccccccc,\n"
3736       "                                          Cccccccccccccc cccccccccc,\n"
3737       "                                          Cccccccccccccc cccccccccc,\n"
3738       "                                          Cccccccccccccc cccccccccc,\n"
3739       "                                          Cccccccccccccc cccccccccc,\n"
3740       "                                          Cccccccccccccc cccccccccc);");
3741   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3742                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3743                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3744                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3745                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
3746 
3747   // Break after multi-line parameters.
3748   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3749                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3750                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3751                "    bbbb bbbb);");
3752   verifyFormat("void SomeLoooooooooooongFunction(\n"
3753                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
3754                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3755                "    int bbbbbbbbbbbbb);");
3756 
3757   // Treat overloaded operators like other functions.
3758   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3759                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
3760   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3761                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
3762   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3763                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
3764   verifyGoogleFormat(
3765       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
3766       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3767   verifyGoogleFormat(
3768       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
3769       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3770   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3771                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3772   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
3773                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3774   verifyGoogleFormat(
3775       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
3776       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3777       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
3778   verifyGoogleFormat(
3779       "template <typename T>\n"
3780       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3781       "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
3782       "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
3783 
3784   FormatStyle Style = getLLVMStyle();
3785   Style.PointerAlignment = FormatStyle::PAS_Left;
3786   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3787                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
3788                Style);
3789   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
3790                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3791                Style);
3792 }
3793 
3794 TEST_F(FormatTest, TrailingReturnType) {
3795   verifyFormat("auto foo() -> int;\n");
3796   verifyFormat("struct S {\n"
3797                "  auto bar() const -> int;\n"
3798                "};");
3799   verifyFormat("template <size_t Order, typename T>\n"
3800                "auto load_img(const std::string &filename)\n"
3801                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
3802   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
3803                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
3804   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
3805   verifyFormat("template <typename T>\n"
3806                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
3807                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
3808 
3809   // Not trailing return types.
3810   verifyFormat("void f() { auto a = b->c(); }");
3811 }
3812 
3813 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
3814   // Avoid breaking before trailing 'const' or other trailing annotations, if
3815   // they are not function-like.
3816   FormatStyle Style = getGoogleStyle();
3817   Style.ColumnLimit = 47;
3818   verifyFormat("void someLongFunction(\n"
3819                "    int someLoooooooooooooongParameter) const {\n}",
3820                getLLVMStyleWithColumns(47));
3821   verifyFormat("LoooooongReturnType\n"
3822                "someLoooooooongFunction() const {}",
3823                getLLVMStyleWithColumns(47));
3824   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
3825                "    const {}",
3826                Style);
3827   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3828                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
3829   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3830                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
3831   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3832                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
3833   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
3834                "                   aaaaaaaaaaa aaaaa) const override;");
3835   verifyGoogleFormat(
3836       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3837       "    const override;");
3838 
3839   // Even if the first parameter has to be wrapped.
3840   verifyFormat("void someLongFunction(\n"
3841                "    int someLongParameter) const {}",
3842                getLLVMStyleWithColumns(46));
3843   verifyFormat("void someLongFunction(\n"
3844                "    int someLongParameter) const {}",
3845                Style);
3846   verifyFormat("void someLongFunction(\n"
3847                "    int someLongParameter) override {}",
3848                Style);
3849   verifyFormat("void someLongFunction(\n"
3850                "    int someLongParameter) OVERRIDE {}",
3851                Style);
3852   verifyFormat("void someLongFunction(\n"
3853                "    int someLongParameter) final {}",
3854                Style);
3855   verifyFormat("void someLongFunction(\n"
3856                "    int someLongParameter) FINAL {}",
3857                Style);
3858   verifyFormat("void someLongFunction(\n"
3859                "    int parameter) const override {}",
3860                Style);
3861 
3862   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
3863   verifyFormat("void someLongFunction(\n"
3864                "    int someLongParameter) const\n"
3865                "{\n"
3866                "}",
3867                Style);
3868 
3869   // Unless these are unknown annotations.
3870   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
3871                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3872                "    LONG_AND_UGLY_ANNOTATION;");
3873 
3874   // Breaking before function-like trailing annotations is fine to keep them
3875   // close to their arguments.
3876   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3877                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3878   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3879                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3880   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3881                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
3882   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
3883                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
3884   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
3885 
3886   verifyFormat(
3887       "void aaaaaaaaaaaaaaaaaa()\n"
3888       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
3889       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
3890   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3891                "    __attribute__((unused));");
3892   verifyGoogleFormat(
3893       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3894       "    GUARDED_BY(aaaaaaaaaaaa);");
3895   verifyGoogleFormat(
3896       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3897       "    GUARDED_BY(aaaaaaaaaaaa);");
3898   verifyGoogleFormat(
3899       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3900       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3901   verifyGoogleFormat(
3902       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3903       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
3904 }
3905 
3906 TEST_F(FormatTest, FunctionAnnotations) {
3907   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3908                "int OldFunction(const string &parameter) {}");
3909   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3910                "string OldFunction(const string &parameter) {}");
3911   verifyFormat("template <typename T>\n"
3912                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3913                "string OldFunction(const string &parameter) {}");
3914 
3915   // Not function annotations.
3916   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3917                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
3918   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
3919                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
3920   verifyFormat("MACRO(abc).function() // wrap\n"
3921                "    << abc;");
3922   verifyFormat("MACRO(abc)->function() // wrap\n"
3923                "    << abc;");
3924   verifyFormat("MACRO(abc)::function() // wrap\n"
3925                "    << abc;");
3926 }
3927 
3928 TEST_F(FormatTest, BreaksDesireably) {
3929   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3930                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3931                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
3932   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3933                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
3934                "}");
3935 
3936   verifyFormat(
3937       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3938       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3939 
3940   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3941                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3942                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3943 
3944   verifyFormat(
3945       "aaaaaaaa(aaaaaaaaaaaaa,\n"
3946       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3947       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
3948       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3949       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
3950 
3951   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3952                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3953 
3954   verifyFormat(
3955       "void f() {\n"
3956       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
3957       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3958       "}");
3959   verifyFormat(
3960       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3961       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3962   verifyFormat(
3963       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3964       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3965   verifyFormat(
3966       "aaaaaa(aaa,\n"
3967       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3968       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3969       "       aaaa);");
3970   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3971                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3972                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3973 
3974   // Indent consistently independent of call expression and unary operator.
3975   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3976                "    dddddddddddddddddddddddddddddd));");
3977   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3978                "    dddddddddddddddddddddddddddddd));");
3979   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
3980                "    dddddddddddddddddddddddddddddd));");
3981 
3982   // This test case breaks on an incorrect memoization, i.e. an optimization not
3983   // taking into account the StopAt value.
3984   verifyFormat(
3985       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3986       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3987       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3988       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3989 
3990   verifyFormat("{\n  {\n    {\n"
3991                "      Annotation.SpaceRequiredBefore =\n"
3992                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
3993                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
3994                "    }\n  }\n}");
3995 
3996   // Break on an outer level if there was a break on an inner level.
3997   EXPECT_EQ("f(g(h(a, // comment\n"
3998             "      b, c),\n"
3999             "    d, e),\n"
4000             "  x, y);",
4001             format("f(g(h(a, // comment\n"
4002                    "    b, c), d, e), x, y);"));
4003 
4004   // Prefer breaking similar line breaks.
4005   verifyFormat(
4006       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
4007       "                             NSTrackingMouseEnteredAndExited |\n"
4008       "                             NSTrackingActiveAlways;");
4009 }
4010 
4011 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
4012   FormatStyle NoBinPacking = getGoogleStyle();
4013   NoBinPacking.BinPackParameters = false;
4014   NoBinPacking.BinPackArguments = true;
4015   verifyFormat("void f() {\n"
4016                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
4017                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4018                "}",
4019                NoBinPacking);
4020   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
4021                "       int aaaaaaaaaaaaaaaaaaaa,\n"
4022                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4023                NoBinPacking);
4024 
4025   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4026   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4027                "                        vector<int> bbbbbbbbbbbbbbb);",
4028                NoBinPacking);
4029   // FIXME: This behavior difference is probably not wanted. However, currently
4030   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
4031   // template arguments from BreakBeforeParameter being set because of the
4032   // one-per-line formatting.
4033   verifyFormat(
4034       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4035       "                                             aaaaaaaaaa> aaaaaaaaaa);",
4036       NoBinPacking);
4037   verifyFormat(
4038       "void fffffffffff(\n"
4039       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
4040       "        aaaaaaaaaa);");
4041 }
4042 
4043 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
4044   FormatStyle NoBinPacking = getGoogleStyle();
4045   NoBinPacking.BinPackParameters = false;
4046   NoBinPacking.BinPackArguments = false;
4047   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
4048                "  aaaaaaaaaaaaaaaaaaaa,\n"
4049                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
4050                NoBinPacking);
4051   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
4052                "        aaaaaaaaaaaaa,\n"
4053                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
4054                NoBinPacking);
4055   verifyFormat(
4056       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4057       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4058       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4059       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4060       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
4061       NoBinPacking);
4062   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4063                "    .aaaaaaaaaaaaaaaaaa();",
4064                NoBinPacking);
4065   verifyFormat("void f() {\n"
4066                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4067                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
4068                "}",
4069                NoBinPacking);
4070 
4071   verifyFormat(
4072       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4073       "             aaaaaaaaaaaa,\n"
4074       "             aaaaaaaaaaaa);",
4075       NoBinPacking);
4076   verifyFormat(
4077       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
4078       "                               ddddddddddddddddddddddddddddd),\n"
4079       "             test);",
4080       NoBinPacking);
4081 
4082   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4083                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
4084                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
4085                "    aaaaaaaaaaaaaaaaaa;",
4086                NoBinPacking);
4087   verifyFormat("a(\"a\"\n"
4088                "  \"a\",\n"
4089                "  a);");
4090 
4091   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4092   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
4093                "                aaaaaaaaa,\n"
4094                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4095                NoBinPacking);
4096   verifyFormat(
4097       "void f() {\n"
4098       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4099       "      .aaaaaaa();\n"
4100       "}",
4101       NoBinPacking);
4102   verifyFormat(
4103       "template <class SomeType, class SomeOtherType>\n"
4104       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
4105       NoBinPacking);
4106 }
4107 
4108 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
4109   FormatStyle Style = getLLVMStyleWithColumns(15);
4110   Style.ExperimentalAutoDetectBinPacking = true;
4111   EXPECT_EQ("aaa(aaaa,\n"
4112             "    aaaa,\n"
4113             "    aaaa);\n"
4114             "aaa(aaaa,\n"
4115             "    aaaa,\n"
4116             "    aaaa);",
4117             format("aaa(aaaa,\n" // one-per-line
4118                    "  aaaa,\n"
4119                    "    aaaa  );\n"
4120                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4121                    Style));
4122   EXPECT_EQ("aaa(aaaa, aaaa,\n"
4123             "    aaaa);\n"
4124             "aaa(aaaa, aaaa,\n"
4125             "    aaaa);",
4126             format("aaa(aaaa,  aaaa,\n" // bin-packed
4127                    "    aaaa  );\n"
4128                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4129                    Style));
4130 }
4131 
4132 TEST_F(FormatTest, FormatsBuilderPattern) {
4133   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
4134                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
4135                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
4136                "    .StartsWith(\".init\", ORDER_INIT)\n"
4137                "    .StartsWith(\".fini\", ORDER_FINI)\n"
4138                "    .StartsWith(\".hash\", ORDER_HASH)\n"
4139                "    .Default(ORDER_TEXT);\n");
4140 
4141   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
4142                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
4143   verifyFormat(
4144       "aaaaaaa->aaaaaaa\n"
4145       "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4146       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4147       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4148   verifyFormat(
4149       "aaaaaaa->aaaaaaa\n"
4150       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4151       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4152   verifyFormat(
4153       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
4154       "    aaaaaaaaaaaaaa);");
4155   verifyFormat(
4156       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
4157       "    aaaaaa->aaaaaaaaaaaa()\n"
4158       "        ->aaaaaaaaaaaaaaaa(\n"
4159       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4160       "        ->aaaaaaaaaaaaaaaaa();");
4161   verifyGoogleFormat(
4162       "void f() {\n"
4163       "  someo->Add((new util::filetools::Handler(dir))\n"
4164       "                 ->OnEvent1(NewPermanentCallback(\n"
4165       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
4166       "                 ->OnEvent2(NewPermanentCallback(\n"
4167       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
4168       "                 ->OnEvent3(NewPermanentCallback(\n"
4169       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
4170       "                 ->OnEvent5(NewPermanentCallback(\n"
4171       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
4172       "                 ->OnEvent6(NewPermanentCallback(\n"
4173       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
4174       "}");
4175 
4176   verifyFormat(
4177       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
4178   verifyFormat("aaaaaaaaaaaaaaa()\n"
4179                "    .aaaaaaaaaaaaaaa()\n"
4180                "    .aaaaaaaaaaaaaaa()\n"
4181                "    .aaaaaaaaaaaaaaa()\n"
4182                "    .aaaaaaaaaaaaaaa();");
4183   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4184                "    .aaaaaaaaaaaaaaa()\n"
4185                "    .aaaaaaaaaaaaaaa()\n"
4186                "    .aaaaaaaaaaaaaaa();");
4187   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4188                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4189                "    .aaaaaaaaaaaaaaa();");
4190   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
4191                "    ->aaaaaaaaaaaaaae(0)\n"
4192                "    ->aaaaaaaaaaaaaaa();");
4193 
4194   // Don't linewrap after very short segments.
4195   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4196                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4197                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4198   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4199                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4200                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4201   verifyFormat("aaa()\n"
4202                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4203                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4204                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4205 
4206   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4207                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4208                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
4209   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4210                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
4211                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
4212 
4213   // Prefer not to break after empty parentheses.
4214   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
4215                "    First->LastNewlineOffset);");
4216 
4217   // Prefer not to create "hanging" indents.
4218   verifyFormat(
4219       "return !soooooooooooooome_map\n"
4220       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4221       "            .second;");
4222   verifyFormat(
4223       "return aaaaaaaaaaaaaaaa\n"
4224       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
4225       "    .aaaa(aaaaaaaaaaaaaa);");
4226   // No hanging indent here.
4227   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
4228                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4229   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
4230                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4231   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4232                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4233                getLLVMStyleWithColumns(60));
4234   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
4235                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4236                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4237                getLLVMStyleWithColumns(59));
4238   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4239                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4240                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4241 }
4242 
4243 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
4244   verifyFormat(
4245       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4246       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
4247   verifyFormat(
4248       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
4249       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
4250 
4251   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4252                "    ccccccccccccccccccccccccc) {\n}");
4253   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
4254                "    ccccccccccccccccccccccccc) {\n}");
4255 
4256   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4257                "    ccccccccccccccccccccccccc) {\n}");
4258   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
4259                "    ccccccccccccccccccccccccc) {\n}");
4260 
4261   verifyFormat(
4262       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
4263       "    ccccccccccccccccccccccccc) {\n}");
4264   verifyFormat(
4265       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
4266       "    ccccccccccccccccccccccccc) {\n}");
4267 
4268   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
4269                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
4270                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
4271                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4272   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
4273                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
4274                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
4275                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4276 
4277   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
4278                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
4279                "    aaaaaaaaaaaaaaa != aa) {\n}");
4280   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
4281                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
4282                "    aaaaaaaaaaaaaaa != aa) {\n}");
4283 }
4284 
4285 TEST_F(FormatTest, BreaksAfterAssignments) {
4286   verifyFormat(
4287       "unsigned Cost =\n"
4288       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
4289       "                        SI->getPointerAddressSpaceee());\n");
4290   verifyFormat(
4291       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
4292       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
4293 
4294   verifyFormat(
4295       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
4296       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
4297   verifyFormat("unsigned OriginalStartColumn =\n"
4298                "    SourceMgr.getSpellingColumnNumber(\n"
4299                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
4300                "    1;");
4301 }
4302 
4303 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
4304   FormatStyle Style = getLLVMStyle();
4305   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4306                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
4307                Style);
4308 
4309   Style.PenaltyBreakAssignment = 20;
4310   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
4311                "                                 cccccccccccccccccccccccccc;",
4312                Style);
4313 }
4314 
4315 TEST_F(FormatTest, AlignsAfterAssignments) {
4316   verifyFormat(
4317       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4318       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
4319   verifyFormat(
4320       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4321       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
4322   verifyFormat(
4323       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4324       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
4325   verifyFormat(
4326       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4327       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
4328   verifyFormat(
4329       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4330       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4331       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
4332 }
4333 
4334 TEST_F(FormatTest, AlignsAfterReturn) {
4335   verifyFormat(
4336       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4337       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
4338   verifyFormat(
4339       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4340       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
4341   verifyFormat(
4342       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4343       "       aaaaaaaaaaaaaaaaaaaaaa();");
4344   verifyFormat(
4345       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4346       "        aaaaaaaaaaaaaaaaaaaaaa());");
4347   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4348                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4349   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4350                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
4351                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4352   verifyFormat("return\n"
4353                "    // true if code is one of a or b.\n"
4354                "    code == a || code == b;");
4355 }
4356 
4357 TEST_F(FormatTest, AlignsAfterOpenBracket) {
4358   verifyFormat(
4359       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4360       "                                                aaaaaaaaa aaaaaaa) {}");
4361   verifyFormat(
4362       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4363       "                                               aaaaaaaaaaa aaaaaaaaa);");
4364   verifyFormat(
4365       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4366       "                                             aaaaaaaaaaaaaaaaaaaaa));");
4367   FormatStyle Style = getLLVMStyle();
4368   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4369   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4370                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
4371                Style);
4372   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4373                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
4374                Style);
4375   verifyFormat("SomeLongVariableName->someFunction(\n"
4376                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
4377                Style);
4378   verifyFormat(
4379       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4380       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4381       Style);
4382   verifyFormat(
4383       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4384       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4385       Style);
4386   verifyFormat(
4387       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4388       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4389       Style);
4390 
4391   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
4392                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
4393                "        b));",
4394                Style);
4395 
4396   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4397   Style.BinPackArguments = false;
4398   Style.BinPackParameters = false;
4399   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4400                "    aaaaaaaaaaa aaaaaaaa,\n"
4401                "    aaaaaaaaa aaaaaaa,\n"
4402                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4403                Style);
4404   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4405                "    aaaaaaaaaaa aaaaaaaaa,\n"
4406                "    aaaaaaaaaaa aaaaaaaaa,\n"
4407                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4408                Style);
4409   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
4410                "    aaaaaaaaaaaaaaa,\n"
4411                "    aaaaaaaaaaaaaaaaaaaaa,\n"
4412                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4413                Style);
4414   verifyFormat(
4415       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
4416       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4417       Style);
4418   verifyFormat(
4419       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
4420       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4421       Style);
4422   verifyFormat(
4423       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4424       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4425       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
4426       "    aaaaaaaaaaaaaaaa);",
4427       Style);
4428   verifyFormat(
4429       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4430       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4431       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
4432       "    aaaaaaaaaaaaaaaa);",
4433       Style);
4434 }
4435 
4436 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
4437   FormatStyle Style = getLLVMStyleWithColumns(40);
4438   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4439                "          bbbbbbbbbbbbbbbbbbbbbb);",
4440                Style);
4441   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
4442   Style.AlignOperands = false;
4443   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4444                "          bbbbbbbbbbbbbbbbbbbbbb);",
4445                Style);
4446   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4447   Style.AlignOperands = true;
4448   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4449                "          bbbbbbbbbbbbbbbbbbbbbb);",
4450                Style);
4451   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4452   Style.AlignOperands = false;
4453   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4454                "    bbbbbbbbbbbbbbbbbbbbbb);",
4455                Style);
4456 }
4457 
4458 TEST_F(FormatTest, BreaksConditionalExpressions) {
4459   verifyFormat(
4460       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4461       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4462       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4463   verifyFormat(
4464       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
4465       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4466       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4467   verifyFormat(
4468       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4469       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4470   verifyFormat(
4471       "aaaa(aaaaaaaaa, aaaaaaaaa,\n"
4472       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4473       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4474   verifyFormat(
4475       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
4476       "                                                    : aaaaaaaaaaaaa);");
4477   verifyFormat(
4478       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4479       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4480       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4481       "                   aaaaaaaaaaaaa);");
4482   verifyFormat(
4483       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4484       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4485       "                   aaaaaaaaaaaaa);");
4486   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4487                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4488                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4489                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4490                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4491   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4492                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4493                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4494                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4495                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4496                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4497                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4498   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4499                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4500                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4501                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4502                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4503   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4504                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4505                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4506   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4507                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4508                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4509                "        : aaaaaaaaaaaaaaaa;");
4510   verifyFormat(
4511       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4512       "    ? aaaaaaaaaaaaaaa\n"
4513       "    : aaaaaaaaaaaaaaa;");
4514   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4515                "          aaaaaaaaa\n"
4516                "      ? b\n"
4517                "      : c);");
4518   verifyFormat("return aaaa == bbbb\n"
4519                "           // comment\n"
4520                "           ? aaaa\n"
4521                "           : bbbb;");
4522   verifyFormat("unsigned Indent =\n"
4523                "    format(TheLine.First,\n"
4524                "           IndentForLevel[TheLine.Level] >= 0\n"
4525                "               ? IndentForLevel[TheLine.Level]\n"
4526                "               : TheLine * 2,\n"
4527                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4528                getLLVMStyleWithColumns(60));
4529   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4530                "                  ? aaaaaaaaaaaaaaa\n"
4531                "                  : bbbbbbbbbbbbbbb //\n"
4532                "                        ? ccccccccccccccc\n"
4533                "                        : ddddddddddddddd;");
4534   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4535                "                  ? aaaaaaaaaaaaaaa\n"
4536                "                  : (bbbbbbbbbbbbbbb //\n"
4537                "                         ? ccccccccccccccc\n"
4538                "                         : ddddddddddddddd);");
4539   verifyFormat(
4540       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4541       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4542       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
4543       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
4544       "                                      : aaaaaaaaaa;");
4545   verifyFormat(
4546       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4547       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
4548       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4549 
4550   FormatStyle NoBinPacking = getLLVMStyle();
4551   NoBinPacking.BinPackArguments = false;
4552   verifyFormat(
4553       "void f() {\n"
4554       "  g(aaa,\n"
4555       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4556       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4557       "        ? aaaaaaaaaaaaaaa\n"
4558       "        : aaaaaaaaaaaaaaa);\n"
4559       "}",
4560       NoBinPacking);
4561   verifyFormat(
4562       "void f() {\n"
4563       "  g(aaa,\n"
4564       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4565       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4566       "        ?: aaaaaaaaaaaaaaa);\n"
4567       "}",
4568       NoBinPacking);
4569 
4570   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
4571                "             // comment.\n"
4572                "             ccccccccccccccccccccccccccccccccccccccc\n"
4573                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4574                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
4575 
4576   // Assignments in conditional expressions. Apparently not uncommon :-(.
4577   verifyFormat("return a != b\n"
4578                "           // comment\n"
4579                "           ? a = b\n"
4580                "           : a = b;");
4581   verifyFormat("return a != b\n"
4582                "           // comment\n"
4583                "           ? a = a != b\n"
4584                "                     // comment\n"
4585                "                     ? a = b\n"
4586                "                     : a\n"
4587                "           : a;\n");
4588   verifyFormat("return a != b\n"
4589                "           // comment\n"
4590                "           ? a\n"
4591                "           : a = a != b\n"
4592                "                     // comment\n"
4593                "                     ? a = b\n"
4594                "                     : a;");
4595 }
4596 
4597 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
4598   FormatStyle Style = getLLVMStyle();
4599   Style.BreakBeforeTernaryOperators = false;
4600   Style.ColumnLimit = 70;
4601   verifyFormat(
4602       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4603       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4604       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4605       Style);
4606   verifyFormat(
4607       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
4608       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4609       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4610       Style);
4611   verifyFormat(
4612       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4613       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4614       Style);
4615   verifyFormat(
4616       "aaaa(aaaaaaaa, aaaaaaaaaa,\n"
4617       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4618       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4619       Style);
4620   verifyFormat(
4621       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
4622       "                                                      aaaaaaaaaaaaa);",
4623       Style);
4624   verifyFormat(
4625       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4626       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4627       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4628       "                   aaaaaaaaaaaaa);",
4629       Style);
4630   verifyFormat(
4631       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4632       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4633       "                   aaaaaaaaaaaaa);",
4634       Style);
4635   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4636                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4637                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4638                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4639                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4640                Style);
4641   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4642                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4643                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4644                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4645                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4646                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4647                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4648                Style);
4649   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4650                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
4651                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4652                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4653                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4654                Style);
4655   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4656                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4657                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4658                Style);
4659   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4660                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4661                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4662                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4663                Style);
4664   verifyFormat(
4665       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4666       "    aaaaaaaaaaaaaaa :\n"
4667       "    aaaaaaaaaaaaaaa;",
4668       Style);
4669   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4670                "          aaaaaaaaa ?\n"
4671                "      b :\n"
4672                "      c);",
4673                Style);
4674   verifyFormat("unsigned Indent =\n"
4675                "    format(TheLine.First,\n"
4676                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
4677                "               IndentForLevel[TheLine.Level] :\n"
4678                "               TheLine * 2,\n"
4679                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4680                Style);
4681   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4682                "                  aaaaaaaaaaaaaaa :\n"
4683                "                  bbbbbbbbbbbbbbb ? //\n"
4684                "                      ccccccccccccccc :\n"
4685                "                      ddddddddddddddd;",
4686                Style);
4687   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4688                "                  aaaaaaaaaaaaaaa :\n"
4689                "                  (bbbbbbbbbbbbbbb ? //\n"
4690                "                       ccccccccccccccc :\n"
4691                "                       ddddddddddddddd);",
4692                Style);
4693   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4694                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
4695                "            ccccccccccccccccccccccccccc;",
4696                Style);
4697   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4698                "           aaaaa :\n"
4699                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
4700                Style);
4701 }
4702 
4703 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
4704   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
4705                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
4706   verifyFormat("bool a = true, b = false;");
4707 
4708   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4709                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
4710                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
4711                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
4712   verifyFormat(
4713       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
4714       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
4715       "     d = e && f;");
4716   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
4717                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
4718   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4719                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
4720   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
4721                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
4722 
4723   FormatStyle Style = getGoogleStyle();
4724   Style.PointerAlignment = FormatStyle::PAS_Left;
4725   Style.DerivePointerAlignment = false;
4726   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4727                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
4728                "    *b = bbbbbbbbbbbbbbbbbbb;",
4729                Style);
4730   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4731                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
4732                Style);
4733   verifyFormat("vector<int*> a, b;", Style);
4734   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
4735 }
4736 
4737 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
4738   verifyFormat("arr[foo ? bar : baz];");
4739   verifyFormat("f()[foo ? bar : baz];");
4740   verifyFormat("(a + b)[foo ? bar : baz];");
4741   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
4742 }
4743 
4744 TEST_F(FormatTest, AlignsStringLiterals) {
4745   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
4746                "                                      \"short literal\");");
4747   verifyFormat(
4748       "looooooooooooooooooooooooongFunction(\n"
4749       "    \"short literal\"\n"
4750       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
4751   verifyFormat("someFunction(\"Always break between multi-line\"\n"
4752                "             \" string literals\",\n"
4753                "             and, other, parameters);");
4754   EXPECT_EQ("fun + \"1243\" /* comment */\n"
4755             "      \"5678\";",
4756             format("fun + \"1243\" /* comment */\n"
4757                    "    \"5678\";",
4758                    getLLVMStyleWithColumns(28)));
4759   EXPECT_EQ(
4760       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
4761       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
4762       "         \"aaaaaaaaaaaaaaaa\";",
4763       format("aaaaaa ="
4764              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
4765              "aaaaaaaaaaaaaaaaaaaaa\" "
4766              "\"aaaaaaaaaaaaaaaa\";"));
4767   verifyFormat("a = a + \"a\"\n"
4768                "        \"a\"\n"
4769                "        \"a\";");
4770   verifyFormat("f(\"a\", \"b\"\n"
4771                "       \"c\");");
4772 
4773   verifyFormat(
4774       "#define LL_FORMAT \"ll\"\n"
4775       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
4776       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
4777 
4778   verifyFormat("#define A(X)          \\\n"
4779                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
4780                "  \"ccccc\"",
4781                getLLVMStyleWithColumns(23));
4782   verifyFormat("#define A \"def\"\n"
4783                "f(\"abc\" A \"ghi\"\n"
4784                "  \"jkl\");");
4785 
4786   verifyFormat("f(L\"a\"\n"
4787                "  L\"b\");");
4788   verifyFormat("#define A(X)            \\\n"
4789                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
4790                "  L\"ccccc\"",
4791                getLLVMStyleWithColumns(25));
4792 
4793   verifyFormat("f(@\"a\"\n"
4794                "  @\"b\");");
4795   verifyFormat("NSString s = @\"a\"\n"
4796                "             @\"b\"\n"
4797                "             @\"c\";");
4798   verifyFormat("NSString s = @\"a\"\n"
4799                "              \"b\"\n"
4800                "              \"c\";");
4801 }
4802 
4803 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
4804   FormatStyle Style = getLLVMStyle();
4805   // No declarations or definitions should be moved to own line.
4806   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
4807   verifyFormat("class A {\n"
4808                "  int f() { return 1; }\n"
4809                "  int g();\n"
4810                "};\n"
4811                "int f() { return 1; }\n"
4812                "int g();\n",
4813                Style);
4814 
4815   // All declarations and definitions should have the return type moved to its
4816   // own
4817   // line.
4818   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
4819   verifyFormat("class E {\n"
4820                "  int\n"
4821                "  f() {\n"
4822                "    return 1;\n"
4823                "  }\n"
4824                "  int\n"
4825                "  g();\n"
4826                "};\n"
4827                "int\n"
4828                "f() {\n"
4829                "  return 1;\n"
4830                "}\n"
4831                "int\n"
4832                "g();\n",
4833                Style);
4834 
4835   // Top-level definitions, and no kinds of declarations should have the
4836   // return type moved to its own line.
4837   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
4838   verifyFormat("class B {\n"
4839                "  int f() { return 1; }\n"
4840                "  int g();\n"
4841                "};\n"
4842                "int\n"
4843                "f() {\n"
4844                "  return 1;\n"
4845                "}\n"
4846                "int g();\n",
4847                Style);
4848 
4849   // Top-level definitions and declarations should have the return type moved
4850   // to its own line.
4851   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
4852   verifyFormat("class C {\n"
4853                "  int f() { return 1; }\n"
4854                "  int g();\n"
4855                "};\n"
4856                "int\n"
4857                "f() {\n"
4858                "  return 1;\n"
4859                "}\n"
4860                "int\n"
4861                "g();\n",
4862                Style);
4863 
4864   // All definitions should have the return type moved to its own line, but no
4865   // kinds of declarations.
4866   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
4867   verifyFormat("class D {\n"
4868                "  int\n"
4869                "  f() {\n"
4870                "    return 1;\n"
4871                "  }\n"
4872                "  int g();\n"
4873                "};\n"
4874                "int\n"
4875                "f() {\n"
4876                "  return 1;\n"
4877                "}\n"
4878                "int g();\n",
4879                Style);
4880   verifyFormat("const char *\n"
4881                "f(void) {\n" // Break here.
4882                "  return \"\";\n"
4883                "}\n"
4884                "const char *bar(void);\n", // No break here.
4885                Style);
4886   verifyFormat("template <class T>\n"
4887                "T *\n"
4888                "f(T &c) {\n" // Break here.
4889                "  return NULL;\n"
4890                "}\n"
4891                "template <class T> T *f(T &c);\n", // No break here.
4892                Style);
4893   verifyFormat("class C {\n"
4894                "  int\n"
4895                "  operator+() {\n"
4896                "    return 1;\n"
4897                "  }\n"
4898                "  int\n"
4899                "  operator()() {\n"
4900                "    return 1;\n"
4901                "  }\n"
4902                "};\n",
4903                Style);
4904   verifyFormat("void\n"
4905                "A::operator()() {}\n"
4906                "void\n"
4907                "A::operator>>() {}\n"
4908                "void\n"
4909                "A::operator+() {}\n",
4910                Style);
4911   verifyFormat("void *operator new(std::size_t s);", // No break here.
4912                Style);
4913   verifyFormat("void *\n"
4914                "operator new(std::size_t s) {}",
4915                Style);
4916   verifyFormat("void *\n"
4917                "operator delete[](void *ptr) {}",
4918                Style);
4919   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4920   verifyFormat("const char *\n"
4921                "f(void)\n" // Break here.
4922                "{\n"
4923                "  return \"\";\n"
4924                "}\n"
4925                "const char *bar(void);\n", // No break here.
4926                Style);
4927   verifyFormat("template <class T>\n"
4928                "T *\n"     // Problem here: no line break
4929                "f(T &c)\n" // Break here.
4930                "{\n"
4931                "  return NULL;\n"
4932                "}\n"
4933                "template <class T> T *f(T &c);\n", // No break here.
4934                Style);
4935 }
4936 
4937 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
4938   FormatStyle NoBreak = getLLVMStyle();
4939   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
4940   FormatStyle Break = getLLVMStyle();
4941   Break.AlwaysBreakBeforeMultilineStrings = true;
4942   verifyFormat("aaaa = \"bbbb\"\n"
4943                "       \"cccc\";",
4944                NoBreak);
4945   verifyFormat("aaaa =\n"
4946                "    \"bbbb\"\n"
4947                "    \"cccc\";",
4948                Break);
4949   verifyFormat("aaaa(\"bbbb\"\n"
4950                "     \"cccc\");",
4951                NoBreak);
4952   verifyFormat("aaaa(\n"
4953                "    \"bbbb\"\n"
4954                "    \"cccc\");",
4955                Break);
4956   verifyFormat("aaaa(qqq, \"bbbb\"\n"
4957                "          \"cccc\");",
4958                NoBreak);
4959   verifyFormat("aaaa(qqq,\n"
4960                "     \"bbbb\"\n"
4961                "     \"cccc\");",
4962                Break);
4963   verifyFormat("aaaa(qqq,\n"
4964                "     L\"bbbb\"\n"
4965                "     L\"cccc\");",
4966                Break);
4967   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
4968                "                      \"bbbb\"));",
4969                Break);
4970   verifyFormat("string s = someFunction(\n"
4971                "    \"abc\"\n"
4972                "    \"abc\");",
4973                Break);
4974 
4975   // As we break before unary operators, breaking right after them is bad.
4976   verifyFormat("string foo = abc ? \"x\"\n"
4977                "                   \"blah blah blah blah blah blah\"\n"
4978                "                 : \"y\";",
4979                Break);
4980 
4981   // Don't break if there is no column gain.
4982   verifyFormat("f(\"aaaa\"\n"
4983                "  \"bbbb\");",
4984                Break);
4985 
4986   // Treat literals with escaped newlines like multi-line string literals.
4987   EXPECT_EQ("x = \"a\\\n"
4988             "b\\\n"
4989             "c\";",
4990             format("x = \"a\\\n"
4991                    "b\\\n"
4992                    "c\";",
4993                    NoBreak));
4994   EXPECT_EQ("xxxx =\n"
4995             "    \"a\\\n"
4996             "b\\\n"
4997             "c\";",
4998             format("xxxx = \"a\\\n"
4999                    "b\\\n"
5000                    "c\";",
5001                    Break));
5002 
5003   EXPECT_EQ("NSString *const kString =\n"
5004             "    @\"aaaa\"\n"
5005             "    @\"bbbb\";",
5006             format("NSString *const kString = @\"aaaa\"\n"
5007                    "@\"bbbb\";",
5008                    Break));
5009 
5010   Break.ColumnLimit = 0;
5011   verifyFormat("const char *hello = \"hello llvm\";", Break);
5012 }
5013 
5014 TEST_F(FormatTest, AlignsPipes) {
5015   verifyFormat(
5016       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5017       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5018       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5019   verifyFormat(
5020       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
5021       "                     << aaaaaaaaaaaaaaaaaaaa;");
5022   verifyFormat(
5023       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5024       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5025   verifyFormat(
5026       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5027       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5028   verifyFormat(
5029       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
5030       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
5031       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
5032   verifyFormat(
5033       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5034       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5035       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5036   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5037                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5038                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5039                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5040   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
5041                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
5042   verifyFormat(
5043       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5044       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5045   verifyFormat(
5046       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
5047       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
5048 
5049   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
5050                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
5051   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5052                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5053                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
5054                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
5055   verifyFormat("LOG_IF(aaa == //\n"
5056                "       bbb)\n"
5057                "    << a << b;");
5058 
5059   // But sometimes, breaking before the first "<<" is desirable.
5060   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5061                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
5062   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
5063                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5064                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5065   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
5066                "    << BEF << IsTemplate << Description << E->getType();");
5067   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5068                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5069                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5070   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5071                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5072                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5073                "    << aaa;");
5074 
5075   verifyFormat(
5076       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5077       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5078 
5079   // Incomplete string literal.
5080   EXPECT_EQ("llvm::errs() << \"\n"
5081             "             << a;",
5082             format("llvm::errs() << \"\n<<a;"));
5083 
5084   verifyFormat("void f() {\n"
5085                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
5086                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
5087                "}");
5088 
5089   // Handle 'endl'.
5090   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
5091                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5092   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5093 
5094   // Handle '\n'.
5095   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
5096                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5097   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
5098                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
5099   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
5100                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
5101   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5102 }
5103 
5104 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
5105   verifyFormat("return out << \"somepacket = {\\n\"\n"
5106                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
5107                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
5108                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
5109                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
5110                "           << \"}\";");
5111 
5112   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5113                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5114                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
5115   verifyFormat(
5116       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
5117       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
5118       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
5119       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
5120       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
5121   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
5122                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5123   verifyFormat(
5124       "void f() {\n"
5125       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
5126       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
5127       "}");
5128 
5129   // Breaking before the first "<<" is generally not desirable.
5130   verifyFormat(
5131       "llvm::errs()\n"
5132       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5133       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5134       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5135       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5136       getLLVMStyleWithColumns(70));
5137   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5138                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5139                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5140                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5141                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5142                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5143                getLLVMStyleWithColumns(70));
5144 
5145   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
5146                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
5147                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
5148   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
5149                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
5150                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
5151   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
5152                "           (aaaa + aaaa);",
5153                getLLVMStyleWithColumns(40));
5154   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
5155                "                  (aaaaaaa + aaaaa));",
5156                getLLVMStyleWithColumns(40));
5157   verifyFormat(
5158       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
5159       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
5160       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
5161 }
5162 
5163 TEST_F(FormatTest, UnderstandsEquals) {
5164   verifyFormat(
5165       "aaaaaaaaaaaaaaaaa =\n"
5166       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5167   verifyFormat(
5168       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5169       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5170   verifyFormat(
5171       "if (a) {\n"
5172       "  f();\n"
5173       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5174       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5175       "}");
5176 
5177   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5178                "        100000000 + 10000000) {\n}");
5179 }
5180 
5181 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
5182   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5183                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
5184 
5185   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5186                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
5187 
5188   verifyFormat(
5189       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
5190       "                                                          Parameter2);");
5191 
5192   verifyFormat(
5193       "ShortObject->shortFunction(\n"
5194       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
5195       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
5196 
5197   verifyFormat("loooooooooooooongFunction(\n"
5198                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
5199 
5200   verifyFormat(
5201       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
5202       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
5203 
5204   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5205                "    .WillRepeatedly(Return(SomeValue));");
5206   verifyFormat("void f() {\n"
5207                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5208                "      .Times(2)\n"
5209                "      .WillRepeatedly(Return(SomeValue));\n"
5210                "}");
5211   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
5212                "    ccccccccccccccccccccccc);");
5213   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5214                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5215                "          .aaaaa(aaaaa),\n"
5216                "      aaaaaaaaaaaaaaaaaaaaa);");
5217   verifyFormat("void f() {\n"
5218                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5219                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
5220                "}");
5221   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5222                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5223                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5224                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5225                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5226   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5227                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5228                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5229                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
5230                "}");
5231 
5232   // Here, it is not necessary to wrap at "." or "->".
5233   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
5234                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5235   verifyFormat(
5236       "aaaaaaaaaaa->aaaaaaaaa(\n"
5237       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5238       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
5239 
5240   verifyFormat(
5241       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5242       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
5243   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
5244                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5245   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
5246                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5247 
5248   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5249                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5250                "    .a();");
5251 
5252   FormatStyle NoBinPacking = getLLVMStyle();
5253   NoBinPacking.BinPackParameters = false;
5254   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5255                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5256                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
5257                "                         aaaaaaaaaaaaaaaaaaa,\n"
5258                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5259                NoBinPacking);
5260 
5261   // If there is a subsequent call, change to hanging indentation.
5262   verifyFormat(
5263       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5264       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
5265       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5266   verifyFormat(
5267       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5268       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
5269   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5270                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5271                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5272   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5273                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5274                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5275 }
5276 
5277 TEST_F(FormatTest, WrapsTemplateDeclarations) {
5278   verifyFormat("template <typename T>\n"
5279                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5280   verifyFormat("template <typename T>\n"
5281                "// T should be one of {A, B}.\n"
5282                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5283   verifyFormat(
5284       "template <typename T>\n"
5285       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
5286   verifyFormat("template <typename T>\n"
5287                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
5288                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
5289   verifyFormat(
5290       "template <typename T>\n"
5291       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
5292       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
5293   verifyFormat(
5294       "template <typename T>\n"
5295       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
5296       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
5297       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5298   verifyFormat("template <typename T>\n"
5299                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5300                "    int aaaaaaaaaaaaaaaaaaaaaa);");
5301   verifyFormat(
5302       "template <typename T1, typename T2 = char, typename T3 = char,\n"
5303       "          typename T4 = char>\n"
5304       "void f();");
5305   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
5306                "          template <typename> class cccccccccccccccccccccc,\n"
5307                "          typename ddddddddddddd>\n"
5308                "class C {};");
5309   verifyFormat(
5310       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
5311       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5312 
5313   verifyFormat("void f() {\n"
5314                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
5315                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
5316                "}");
5317 
5318   verifyFormat("template <typename T> class C {};");
5319   verifyFormat("template <typename T> void f();");
5320   verifyFormat("template <typename T> void f() {}");
5321   verifyFormat(
5322       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5323       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5324       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
5325       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5326       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5327       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
5328       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
5329       getLLVMStyleWithColumns(72));
5330   EXPECT_EQ("static_cast<A< //\n"
5331             "    B> *>(\n"
5332             "\n"
5333             ");",
5334             format("static_cast<A<//\n"
5335                    "    B>*>(\n"
5336                    "\n"
5337                    "    );"));
5338   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5339                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
5340 
5341   FormatStyle AlwaysBreak = getLLVMStyle();
5342   AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
5343   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
5344   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
5345   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
5346   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5347                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5348                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
5349   verifyFormat("template <template <typename> class Fooooooo,\n"
5350                "          template <typename> class Baaaaaaar>\n"
5351                "struct C {};",
5352                AlwaysBreak);
5353   verifyFormat("template <typename T> // T can be A, B or C.\n"
5354                "struct C {};",
5355                AlwaysBreak);
5356   verifyFormat("template <enum E> class A {\n"
5357                "public:\n"
5358                "  E *f();\n"
5359                "};");
5360 }
5361 
5362 TEST_F(FormatTest, WrapsTemplateParameters) {
5363   FormatStyle Style = getLLVMStyle();
5364   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5365   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5366   verifyFormat(
5367       "template <typename... a> struct q {};\n"
5368       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
5369       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
5370       "    y;",
5371       Style);
5372   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5373   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5374   verifyFormat(
5375       "template <typename... a> struct r {};\n"
5376       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
5377       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
5378       "    y;",
5379       Style);
5380   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5381   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5382   verifyFormat(
5383       "template <typename... a> struct s {};\n"
5384       "extern s<\n"
5385       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5386       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
5387       "    y;",
5388       Style);
5389   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5390   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5391   verifyFormat(
5392       "template <typename... a> struct t {};\n"
5393       "extern t<\n"
5394       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5395       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
5396       "    y;",
5397       Style);
5398 }
5399 
5400 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
5401   verifyFormat(
5402       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5403       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5404   verifyFormat(
5405       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5406       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5407       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5408 
5409   // FIXME: Should we have the extra indent after the second break?
5410   verifyFormat(
5411       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5412       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5413       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5414 
5415   verifyFormat(
5416       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
5417       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
5418 
5419   // Breaking at nested name specifiers is generally not desirable.
5420   verifyFormat(
5421       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5422       "    aaaaaaaaaaaaaaaaaaaaaaa);");
5423 
5424   verifyFormat(
5425       "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
5426       "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5427       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5428       "                   aaaaaaaaaaaaaaaaaaaaa);",
5429       getLLVMStyleWithColumns(74));
5430 
5431   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5432                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5433                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5434 }
5435 
5436 TEST_F(FormatTest, UnderstandsTemplateParameters) {
5437   verifyFormat("A<int> a;");
5438   verifyFormat("A<A<A<int>>> a;");
5439   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
5440   verifyFormat("bool x = a < 1 || 2 > a;");
5441   verifyFormat("bool x = 5 < f<int>();");
5442   verifyFormat("bool x = f<int>() > 5;");
5443   verifyFormat("bool x = 5 < a<int>::x;");
5444   verifyFormat("bool x = a < 4 ? a > 2 : false;");
5445   verifyFormat("bool x = f() ? a < 2 : a > 2;");
5446 
5447   verifyGoogleFormat("A<A<int>> a;");
5448   verifyGoogleFormat("A<A<A<int>>> a;");
5449   verifyGoogleFormat("A<A<A<A<int>>>> a;");
5450   verifyGoogleFormat("A<A<int> > a;");
5451   verifyGoogleFormat("A<A<A<int> > > a;");
5452   verifyGoogleFormat("A<A<A<A<int> > > > a;");
5453   verifyGoogleFormat("A<::A<int>> a;");
5454   verifyGoogleFormat("A<::A> a;");
5455   verifyGoogleFormat("A< ::A> a;");
5456   verifyGoogleFormat("A< ::A<int> > a;");
5457   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
5458   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
5459   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
5460   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
5461   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
5462             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
5463 
5464   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
5465 
5466   verifyFormat("test >> a >> b;");
5467   verifyFormat("test << a >> b;");
5468 
5469   verifyFormat("f<int>();");
5470   verifyFormat("template <typename T> void f() {}");
5471   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
5472   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
5473                "sizeof(char)>::type>;");
5474   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
5475   verifyFormat("f(a.operator()<A>());");
5476   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5477                "      .template operator()<A>());",
5478                getLLVMStyleWithColumns(35));
5479 
5480   // Not template parameters.
5481   verifyFormat("return a < b && c > d;");
5482   verifyFormat("void f() {\n"
5483                "  while (a < b && c > d) {\n"
5484                "  }\n"
5485                "}");
5486   verifyFormat("template <typename... Types>\n"
5487                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
5488 
5489   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5490                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
5491                getLLVMStyleWithColumns(60));
5492   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
5493   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
5494   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
5495 }
5496 
5497 TEST_F(FormatTest, BitshiftOperatorWidth) {
5498   EXPECT_EQ("int a = 1 << 2; /* foo\n"
5499             "                   bar */",
5500             format("int    a=1<<2;  /* foo\n"
5501                    "                   bar */"));
5502 
5503   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
5504             "                     bar */",
5505             format("int  b  =256>>1 ;  /* foo\n"
5506                    "                      bar */"));
5507 }
5508 
5509 TEST_F(FormatTest, UnderstandsBinaryOperators) {
5510   verifyFormat("COMPARE(a, ==, b);");
5511   verifyFormat("auto s = sizeof...(Ts) - 1;");
5512 }
5513 
5514 TEST_F(FormatTest, UnderstandsPointersToMembers) {
5515   verifyFormat("int A::*x;");
5516   verifyFormat("int (S::*func)(void *);");
5517   verifyFormat("void f() { int (S::*func)(void *); }");
5518   verifyFormat("typedef bool *(Class::*Member)() const;");
5519   verifyFormat("void f() {\n"
5520                "  (a->*f)();\n"
5521                "  a->*x;\n"
5522                "  (a.*f)();\n"
5523                "  ((*a).*f)();\n"
5524                "  a.*x;\n"
5525                "}");
5526   verifyFormat("void f() {\n"
5527                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5528                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
5529                "}");
5530   verifyFormat(
5531       "(aaaaaaaaaa->*bbbbbbb)(\n"
5532       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5533   FormatStyle Style = getLLVMStyle();
5534   Style.PointerAlignment = FormatStyle::PAS_Left;
5535   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
5536 }
5537 
5538 TEST_F(FormatTest, UnderstandsUnaryOperators) {
5539   verifyFormat("int a = -2;");
5540   verifyFormat("f(-1, -2, -3);");
5541   verifyFormat("a[-1] = 5;");
5542   verifyFormat("int a = 5 + -2;");
5543   verifyFormat("if (i == -1) {\n}");
5544   verifyFormat("if (i != -1) {\n}");
5545   verifyFormat("if (i > -1) {\n}");
5546   verifyFormat("if (i < -1) {\n}");
5547   verifyFormat("++(a->f());");
5548   verifyFormat("--(a->f());");
5549   verifyFormat("(a->f())++;");
5550   verifyFormat("a[42]++;");
5551   verifyFormat("if (!(a->f())) {\n}");
5552 
5553   verifyFormat("a-- > b;");
5554   verifyFormat("b ? -a : c;");
5555   verifyFormat("n * sizeof char16;");
5556   verifyFormat("n * alignof char16;", getGoogleStyle());
5557   verifyFormat("sizeof(char);");
5558   verifyFormat("alignof(char);", getGoogleStyle());
5559 
5560   verifyFormat("return -1;");
5561   verifyFormat("switch (a) {\n"
5562                "case -1:\n"
5563                "  break;\n"
5564                "}");
5565   verifyFormat("#define X -1");
5566   verifyFormat("#define X -kConstant");
5567 
5568   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
5569   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
5570 
5571   verifyFormat("int a = /* confusing comment */ -1;");
5572   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
5573   verifyFormat("int a = i /* confusing comment */++;");
5574 }
5575 
5576 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
5577   verifyFormat("if (!aaaaaaaaaa( // break\n"
5578                "        aaaaa)) {\n"
5579                "}");
5580   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
5581                "    aaaaa));");
5582   verifyFormat("*aaa = aaaaaaa( // break\n"
5583                "    bbbbbb);");
5584 }
5585 
5586 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
5587   verifyFormat("bool operator<();");
5588   verifyFormat("bool operator>();");
5589   verifyFormat("bool operator=();");
5590   verifyFormat("bool operator==();");
5591   verifyFormat("bool operator!=();");
5592   verifyFormat("int operator+();");
5593   verifyFormat("int operator++();");
5594   verifyFormat("int operator++(int) volatile noexcept;");
5595   verifyFormat("bool operator,();");
5596   verifyFormat("bool operator();");
5597   verifyFormat("bool operator()();");
5598   verifyFormat("bool operator[]();");
5599   verifyFormat("operator bool();");
5600   verifyFormat("operator int();");
5601   verifyFormat("operator void *();");
5602   verifyFormat("operator SomeType<int>();");
5603   verifyFormat("operator SomeType<int, int>();");
5604   verifyFormat("operator SomeType<SomeType<int>>();");
5605   verifyFormat("void *operator new(std::size_t size);");
5606   verifyFormat("void *operator new[](std::size_t size);");
5607   verifyFormat("void operator delete(void *ptr);");
5608   verifyFormat("void operator delete[](void *ptr);");
5609   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
5610                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
5611   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
5612                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
5613 
5614   verifyFormat(
5615       "ostream &operator<<(ostream &OutputStream,\n"
5616       "                    SomeReallyLongType WithSomeReallyLongValue);");
5617   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
5618                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
5619                "  return left.group < right.group;\n"
5620                "}");
5621   verifyFormat("SomeType &operator=(const SomeType &S);");
5622   verifyFormat("f.template operator()<int>();");
5623 
5624   verifyGoogleFormat("operator void*();");
5625   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
5626   verifyGoogleFormat("operator ::A();");
5627 
5628   verifyFormat("using A::operator+;");
5629   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
5630                "int i;");
5631 }
5632 
5633 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
5634   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
5635   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
5636   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
5637   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
5638   verifyFormat("Deleted &operator=(const Deleted &) &;");
5639   verifyFormat("Deleted &operator=(const Deleted &) &&;");
5640   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
5641   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
5642   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
5643   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
5644   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
5645   verifyFormat("void Fn(T const &) const &;");
5646   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
5647   verifyFormat("template <typename T>\n"
5648                "void F(T) && = delete;",
5649                getGoogleStyle());
5650 
5651   FormatStyle AlignLeft = getLLVMStyle();
5652   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
5653   verifyFormat("void A::b() && {}", AlignLeft);
5654   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
5655   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
5656                AlignLeft);
5657   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
5658   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
5659   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
5660   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
5661   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
5662   verifyFormat("auto Function(T) & -> void;", AlignLeft);
5663   verifyFormat("void Fn(T const&) const&;", AlignLeft);
5664   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
5665 
5666   FormatStyle Spaces = getLLVMStyle();
5667   Spaces.SpacesInCStyleCastParentheses = true;
5668   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
5669   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
5670   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
5671   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
5672 
5673   Spaces.SpacesInCStyleCastParentheses = false;
5674   Spaces.SpacesInParentheses = true;
5675   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
5676   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
5677   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
5678   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
5679 }
5680 
5681 TEST_F(FormatTest, UnderstandsNewAndDelete) {
5682   verifyFormat("void f() {\n"
5683                "  A *a = new A;\n"
5684                "  A *a = new (placement) A;\n"
5685                "  delete a;\n"
5686                "  delete (A *)a;\n"
5687                "}");
5688   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5689                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5690   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5691                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5692                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5693   verifyFormat("delete[] h->p;");
5694 }
5695 
5696 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
5697   verifyFormat("int *f(int *a) {}");
5698   verifyFormat("int main(int argc, char **argv) {}");
5699   verifyFormat("Test::Test(int b) : a(b * b) {}");
5700   verifyIndependentOfContext("f(a, *a);");
5701   verifyFormat("void g() { f(*a); }");
5702   verifyIndependentOfContext("int a = b * 10;");
5703   verifyIndependentOfContext("int a = 10 * b;");
5704   verifyIndependentOfContext("int a = b * c;");
5705   verifyIndependentOfContext("int a += b * c;");
5706   verifyIndependentOfContext("int a -= b * c;");
5707   verifyIndependentOfContext("int a *= b * c;");
5708   verifyIndependentOfContext("int a /= b * c;");
5709   verifyIndependentOfContext("int a = *b;");
5710   verifyIndependentOfContext("int a = *b * c;");
5711   verifyIndependentOfContext("int a = b * *c;");
5712   verifyIndependentOfContext("int a = b * (10);");
5713   verifyIndependentOfContext("S << b * (10);");
5714   verifyIndependentOfContext("return 10 * b;");
5715   verifyIndependentOfContext("return *b * *c;");
5716   verifyIndependentOfContext("return a & ~b;");
5717   verifyIndependentOfContext("f(b ? *c : *d);");
5718   verifyIndependentOfContext("int a = b ? *c : *d;");
5719   verifyIndependentOfContext("*b = a;");
5720   verifyIndependentOfContext("a * ~b;");
5721   verifyIndependentOfContext("a * !b;");
5722   verifyIndependentOfContext("a * +b;");
5723   verifyIndependentOfContext("a * -b;");
5724   verifyIndependentOfContext("a * ++b;");
5725   verifyIndependentOfContext("a * --b;");
5726   verifyIndependentOfContext("a[4] * b;");
5727   verifyIndependentOfContext("a[a * a] = 1;");
5728   verifyIndependentOfContext("f() * b;");
5729   verifyIndependentOfContext("a * [self dostuff];");
5730   verifyIndependentOfContext("int x = a * (a + b);");
5731   verifyIndependentOfContext("(a *)(a + b);");
5732   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
5733   verifyIndependentOfContext("int *pa = (int *)&a;");
5734   verifyIndependentOfContext("return sizeof(int **);");
5735   verifyIndependentOfContext("return sizeof(int ******);");
5736   verifyIndependentOfContext("return (int **&)a;");
5737   verifyIndependentOfContext("f((*PointerToArray)[10]);");
5738   verifyFormat("void f(Type (*parameter)[10]) {}");
5739   verifyFormat("void f(Type (&parameter)[10]) {}");
5740   verifyGoogleFormat("return sizeof(int**);");
5741   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
5742   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
5743   verifyFormat("auto a = [](int **&, int ***) {};");
5744   verifyFormat("auto PointerBinding = [](const char *S) {};");
5745   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
5746   verifyFormat("[](const decltype(*a) &value) {}");
5747   verifyFormat("decltype(a * b) F();");
5748   verifyFormat("#define MACRO() [](A *a) { return 1; }");
5749   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
5750   verifyIndependentOfContext("typedef void (*f)(int *a);");
5751   verifyIndependentOfContext("int i{a * b};");
5752   verifyIndependentOfContext("aaa && aaa->f();");
5753   verifyIndependentOfContext("int x = ~*p;");
5754   verifyFormat("Constructor() : a(a), area(width * height) {}");
5755   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
5756   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
5757   verifyFormat("void f() { f(a, c * d); }");
5758   verifyFormat("void f() { f(new a(), c * d); }");
5759   verifyFormat("void f(const MyOverride &override);");
5760   verifyFormat("void f(const MyFinal &final);");
5761   verifyIndependentOfContext("bool a = f() && override.f();");
5762   verifyIndependentOfContext("bool a = f() && final.f();");
5763 
5764   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
5765 
5766   verifyIndependentOfContext("A<int *> a;");
5767   verifyIndependentOfContext("A<int **> a;");
5768   verifyIndependentOfContext("A<int *, int *> a;");
5769   verifyIndependentOfContext("A<int *[]> a;");
5770   verifyIndependentOfContext(
5771       "const char *const p = reinterpret_cast<const char *const>(q);");
5772   verifyIndependentOfContext("A<int **, int **> a;");
5773   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
5774   verifyFormat("for (char **a = b; *a; ++a) {\n}");
5775   verifyFormat("for (; a && b;) {\n}");
5776   verifyFormat("bool foo = true && [] { return false; }();");
5777 
5778   verifyFormat(
5779       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5780       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5781 
5782   verifyGoogleFormat("int const* a = &b;");
5783   verifyGoogleFormat("**outparam = 1;");
5784   verifyGoogleFormat("*outparam = a * b;");
5785   verifyGoogleFormat("int main(int argc, char** argv) {}");
5786   verifyGoogleFormat("A<int*> a;");
5787   verifyGoogleFormat("A<int**> a;");
5788   verifyGoogleFormat("A<int*, int*> a;");
5789   verifyGoogleFormat("A<int**, int**> a;");
5790   verifyGoogleFormat("f(b ? *c : *d);");
5791   verifyGoogleFormat("int a = b ? *c : *d;");
5792   verifyGoogleFormat("Type* t = **x;");
5793   verifyGoogleFormat("Type* t = *++*x;");
5794   verifyGoogleFormat("*++*x;");
5795   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
5796   verifyGoogleFormat("Type* t = x++ * y;");
5797   verifyGoogleFormat(
5798       "const char* const p = reinterpret_cast<const char* const>(q);");
5799   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
5800   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
5801   verifyGoogleFormat("template <typename T>\n"
5802                      "void f(int i = 0, SomeType** temps = NULL);");
5803 
5804   FormatStyle Left = getLLVMStyle();
5805   Left.PointerAlignment = FormatStyle::PAS_Left;
5806   verifyFormat("x = *a(x) = *a(y);", Left);
5807   verifyFormat("for (;; *a = b) {\n}", Left);
5808   verifyFormat("return *this += 1;", Left);
5809   verifyFormat("throw *x;", Left);
5810   verifyFormat("delete *x;", Left);
5811   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
5812   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
5813   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
5814 
5815   verifyIndependentOfContext("a = *(x + y);");
5816   verifyIndependentOfContext("a = &(x + y);");
5817   verifyIndependentOfContext("*(x + y).call();");
5818   verifyIndependentOfContext("&(x + y)->call();");
5819   verifyFormat("void f() { &(*I).first; }");
5820 
5821   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
5822   verifyFormat(
5823       "int *MyValues = {\n"
5824       "    *A, // Operator detection might be confused by the '{'\n"
5825       "    *BB // Operator detection might be confused by previous comment\n"
5826       "};");
5827 
5828   verifyIndependentOfContext("if (int *a = &b)");
5829   verifyIndependentOfContext("if (int &a = *b)");
5830   verifyIndependentOfContext("if (a & b[i])");
5831   verifyIndependentOfContext("if (a::b::c::d & b[i])");
5832   verifyIndependentOfContext("if (*b[i])");
5833   verifyIndependentOfContext("if (int *a = (&b))");
5834   verifyIndependentOfContext("while (int *a = &b)");
5835   verifyIndependentOfContext("size = sizeof *a;");
5836   verifyIndependentOfContext("if (a && (b = c))");
5837   verifyFormat("void f() {\n"
5838                "  for (const int &v : Values) {\n"
5839                "  }\n"
5840                "}");
5841   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
5842   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
5843   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
5844 
5845   verifyFormat("#define A (!a * b)");
5846   verifyFormat("#define MACRO     \\\n"
5847                "  int *i = a * b; \\\n"
5848                "  void f(a *b);",
5849                getLLVMStyleWithColumns(19));
5850 
5851   verifyIndependentOfContext("A = new SomeType *[Length];");
5852   verifyIndependentOfContext("A = new SomeType *[Length]();");
5853   verifyIndependentOfContext("T **t = new T *;");
5854   verifyIndependentOfContext("T **t = new T *();");
5855   verifyGoogleFormat("A = new SomeType*[Length]();");
5856   verifyGoogleFormat("A = new SomeType*[Length];");
5857   verifyGoogleFormat("T** t = new T*;");
5858   verifyGoogleFormat("T** t = new T*();");
5859 
5860   verifyFormat("STATIC_ASSERT((a & b) == 0);");
5861   verifyFormat("STATIC_ASSERT(0 == (a & b));");
5862   verifyFormat("template <bool a, bool b> "
5863                "typename t::if<x && y>::type f() {}");
5864   verifyFormat("template <int *y> f() {}");
5865   verifyFormat("vector<int *> v;");
5866   verifyFormat("vector<int *const> v;");
5867   verifyFormat("vector<int *const **const *> v;");
5868   verifyFormat("vector<int *volatile> v;");
5869   verifyFormat("vector<a * b> v;");
5870   verifyFormat("foo<b && false>();");
5871   verifyFormat("foo<b & 1>();");
5872   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
5873   verifyFormat(
5874       "template <class T, class = typename std::enable_if<\n"
5875       "                       std::is_integral<T>::value &&\n"
5876       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
5877       "void F();",
5878       getLLVMStyleWithColumns(70));
5879   verifyFormat(
5880       "template <class T,\n"
5881       "          class = typename std::enable_if<\n"
5882       "              std::is_integral<T>::value &&\n"
5883       "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
5884       "          class U>\n"
5885       "void F();",
5886       getLLVMStyleWithColumns(70));
5887   verifyFormat(
5888       "template <class T,\n"
5889       "          class = typename ::std::enable_if<\n"
5890       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
5891       "void F();",
5892       getGoogleStyleWithColumns(68));
5893 
5894   verifyIndependentOfContext("MACRO(int *i);");
5895   verifyIndependentOfContext("MACRO(auto *a);");
5896   verifyIndependentOfContext("MACRO(const A *a);");
5897   verifyIndependentOfContext("MACRO(A *const a);");
5898   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
5899   verifyFormat("void f() { f(float{1}, a * a); }");
5900   // FIXME: Is there a way to make this work?
5901   // verifyIndependentOfContext("MACRO(A *a);");
5902 
5903   verifyFormat("DatumHandle const *operator->() const { return input_; }");
5904   verifyFormat("return options != nullptr && operator==(*options);");
5905 
5906   EXPECT_EQ("#define OP(x)                                    \\\n"
5907             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
5908             "    return s << a.DebugString();                 \\\n"
5909             "  }",
5910             format("#define OP(x) \\\n"
5911                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
5912                    "    return s << a.DebugString(); \\\n"
5913                    "  }",
5914                    getLLVMStyleWithColumns(50)));
5915 
5916   // FIXME: We cannot handle this case yet; we might be able to figure out that
5917   // foo<x> d > v; doesn't make sense.
5918   verifyFormat("foo<a<b && c> d> v;");
5919 
5920   FormatStyle PointerMiddle = getLLVMStyle();
5921   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
5922   verifyFormat("delete *x;", PointerMiddle);
5923   verifyFormat("int * x;", PointerMiddle);
5924   verifyFormat("template <int * y> f() {}", PointerMiddle);
5925   verifyFormat("int * f(int * a) {}", PointerMiddle);
5926   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
5927   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
5928   verifyFormat("A<int *> a;", PointerMiddle);
5929   verifyFormat("A<int **> a;", PointerMiddle);
5930   verifyFormat("A<int *, int *> a;", PointerMiddle);
5931   verifyFormat("A<int * []> a;", PointerMiddle);
5932   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
5933   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
5934   verifyFormat("T ** t = new T *;", PointerMiddle);
5935 
5936   // Member function reference qualifiers aren't binary operators.
5937   verifyFormat("string // break\n"
5938                "operator()() & {}");
5939   verifyFormat("string // break\n"
5940                "operator()() && {}");
5941   verifyGoogleFormat("template <typename T>\n"
5942                      "auto x() & -> int {}");
5943 }
5944 
5945 TEST_F(FormatTest, UnderstandsAttributes) {
5946   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
5947   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
5948                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
5949   FormatStyle AfterType = getLLVMStyle();
5950   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
5951   verifyFormat("__attribute__((nodebug)) void\n"
5952                "foo() {}\n",
5953                AfterType);
5954 }
5955 
5956 TEST_F(FormatTest, UnderstandsEllipsis) {
5957   verifyFormat("int printf(const char *fmt, ...);");
5958   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
5959   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
5960 
5961   FormatStyle PointersLeft = getLLVMStyle();
5962   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
5963   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
5964 }
5965 
5966 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
5967   EXPECT_EQ("int *a;\n"
5968             "int *a;\n"
5969             "int *a;",
5970             format("int *a;\n"
5971                    "int* a;\n"
5972                    "int *a;",
5973                    getGoogleStyle()));
5974   EXPECT_EQ("int* a;\n"
5975             "int* a;\n"
5976             "int* a;",
5977             format("int* a;\n"
5978                    "int* a;\n"
5979                    "int *a;",
5980                    getGoogleStyle()));
5981   EXPECT_EQ("int *a;\n"
5982             "int *a;\n"
5983             "int *a;",
5984             format("int *a;\n"
5985                    "int * a;\n"
5986                    "int *  a;",
5987                    getGoogleStyle()));
5988   EXPECT_EQ("auto x = [] {\n"
5989             "  int *a;\n"
5990             "  int *a;\n"
5991             "  int *a;\n"
5992             "};",
5993             format("auto x=[]{int *a;\n"
5994                    "int * a;\n"
5995                    "int *  a;};",
5996                    getGoogleStyle()));
5997 }
5998 
5999 TEST_F(FormatTest, UnderstandsRvalueReferences) {
6000   verifyFormat("int f(int &&a) {}");
6001   verifyFormat("int f(int a, char &&b) {}");
6002   verifyFormat("void f() { int &&a = b; }");
6003   verifyGoogleFormat("int f(int a, char&& b) {}");
6004   verifyGoogleFormat("void f() { int&& a = b; }");
6005 
6006   verifyIndependentOfContext("A<int &&> a;");
6007   verifyIndependentOfContext("A<int &&, int &&> a;");
6008   verifyGoogleFormat("A<int&&> a;");
6009   verifyGoogleFormat("A<int&&, int&&> a;");
6010 
6011   // Not rvalue references:
6012   verifyFormat("template <bool B, bool C> class A {\n"
6013                "  static_assert(B && C, \"Something is wrong\");\n"
6014                "};");
6015   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
6016   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
6017   verifyFormat("#define A(a, b) (a && b)");
6018 }
6019 
6020 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
6021   verifyFormat("void f() {\n"
6022                "  x[aaaaaaaaa -\n"
6023                "    b] = 23;\n"
6024                "}",
6025                getLLVMStyleWithColumns(15));
6026 }
6027 
6028 TEST_F(FormatTest, FormatsCasts) {
6029   verifyFormat("Type *A = static_cast<Type *>(P);");
6030   verifyFormat("Type *A = (Type *)P;");
6031   verifyFormat("Type *A = (vector<Type *, int *>)P;");
6032   verifyFormat("int a = (int)(2.0f);");
6033   verifyFormat("int a = (int)2.0f;");
6034   verifyFormat("x[(int32)y];");
6035   verifyFormat("x = (int32)y;");
6036   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
6037   verifyFormat("int a = (int)*b;");
6038   verifyFormat("int a = (int)2.0f;");
6039   verifyFormat("int a = (int)~0;");
6040   verifyFormat("int a = (int)++a;");
6041   verifyFormat("int a = (int)sizeof(int);");
6042   verifyFormat("int a = (int)+2;");
6043   verifyFormat("my_int a = (my_int)2.0f;");
6044   verifyFormat("my_int a = (my_int)sizeof(int);");
6045   verifyFormat("return (my_int)aaa;");
6046   verifyFormat("#define x ((int)-1)");
6047   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
6048   verifyFormat("#define p(q) ((int *)&q)");
6049   verifyFormat("fn(a)(b) + 1;");
6050 
6051   verifyFormat("void f() { my_int a = (my_int)*b; }");
6052   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
6053   verifyFormat("my_int a = (my_int)~0;");
6054   verifyFormat("my_int a = (my_int)++a;");
6055   verifyFormat("my_int a = (my_int)-2;");
6056   verifyFormat("my_int a = (my_int)1;");
6057   verifyFormat("my_int a = (my_int *)1;");
6058   verifyFormat("my_int a = (const my_int)-1;");
6059   verifyFormat("my_int a = (const my_int *)-1;");
6060   verifyFormat("my_int a = (my_int)(my_int)-1;");
6061   verifyFormat("my_int a = (ns::my_int)-2;");
6062   verifyFormat("case (my_int)ONE:");
6063   verifyFormat("auto x = (X)this;");
6064 
6065   // FIXME: single value wrapped with paren will be treated as cast.
6066   verifyFormat("void f(int i = (kValue)*kMask) {}");
6067 
6068   verifyFormat("{ (void)F; }");
6069 
6070   // Don't break after a cast's
6071   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6072                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
6073                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
6074 
6075   // These are not casts.
6076   verifyFormat("void f(int *) {}");
6077   verifyFormat("f(foo)->b;");
6078   verifyFormat("f(foo).b;");
6079   verifyFormat("f(foo)(b);");
6080   verifyFormat("f(foo)[b];");
6081   verifyFormat("[](foo) { return 4; }(bar);");
6082   verifyFormat("(*funptr)(foo)[4];");
6083   verifyFormat("funptrs[4](foo)[4];");
6084   verifyFormat("void f(int *);");
6085   verifyFormat("void f(int *) = 0;");
6086   verifyFormat("void f(SmallVector<int>) {}");
6087   verifyFormat("void f(SmallVector<int>);");
6088   verifyFormat("void f(SmallVector<int>) = 0;");
6089   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
6090   verifyFormat("int a = sizeof(int) * b;");
6091   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
6092   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
6093   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
6094   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
6095 
6096   // These are not casts, but at some point were confused with casts.
6097   verifyFormat("virtual void foo(int *) override;");
6098   verifyFormat("virtual void foo(char &) const;");
6099   verifyFormat("virtual void foo(int *a, char *) const;");
6100   verifyFormat("int a = sizeof(int *) + b;");
6101   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
6102   verifyFormat("bool b = f(g<int>) && c;");
6103   verifyFormat("typedef void (*f)(int i) func;");
6104 
6105   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
6106                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
6107   // FIXME: The indentation here is not ideal.
6108   verifyFormat(
6109       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6110       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
6111       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
6112 }
6113 
6114 TEST_F(FormatTest, FormatsFunctionTypes) {
6115   verifyFormat("A<bool()> a;");
6116   verifyFormat("A<SomeType()> a;");
6117   verifyFormat("A<void (*)(int, std::string)> a;");
6118   verifyFormat("A<void *(int)>;");
6119   verifyFormat("void *(*a)(int *, SomeType *);");
6120   verifyFormat("int (*func)(void *);");
6121   verifyFormat("void f() { int (*func)(void *); }");
6122   verifyFormat("template <class CallbackClass>\n"
6123                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
6124 
6125   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
6126   verifyGoogleFormat("void* (*a)(int);");
6127   verifyGoogleFormat(
6128       "template <class CallbackClass>\n"
6129       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
6130 
6131   // Other constructs can look somewhat like function types:
6132   verifyFormat("A<sizeof(*x)> a;");
6133   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
6134   verifyFormat("some_var = function(*some_pointer_var)[0];");
6135   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
6136   verifyFormat("int x = f(&h)();");
6137   verifyFormat("returnsFunction(&param1, &param2)(param);");
6138   verifyFormat("std::function<\n"
6139                "    LooooooooooongTemplatedType<\n"
6140                "        SomeType>*(\n"
6141                "        LooooooooooooooooongType type)>\n"
6142                "    function;",
6143                getGoogleStyleWithColumns(40));
6144 }
6145 
6146 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
6147   verifyFormat("A (*foo_)[6];");
6148   verifyFormat("vector<int> (*foo_)[6];");
6149 }
6150 
6151 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
6152   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6153                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6154   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
6155                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6156   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6157                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
6158 
6159   // Different ways of ()-initializiation.
6160   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6161                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
6162   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6163                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
6164   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6165                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
6166   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6167                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
6168 
6169   // Lambdas should not confuse the variable declaration heuristic.
6170   verifyFormat("LooooooooooooooooongType\n"
6171                "    variable(nullptr, [](A *a) {});",
6172                getLLVMStyleWithColumns(40));
6173 }
6174 
6175 TEST_F(FormatTest, BreaksLongDeclarations) {
6176   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
6177                "    AnotherNameForTheLongType;");
6178   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
6179                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6180   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6181                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6182   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
6183                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6184   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6185                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6186   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
6187                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6188   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6189                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6190   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6191                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6192   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6193                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
6194   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6195                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
6196   FormatStyle Indented = getLLVMStyle();
6197   Indented.IndentWrappedFunctionNames = true;
6198   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6199                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
6200                Indented);
6201   verifyFormat(
6202       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6203       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6204       Indented);
6205   verifyFormat(
6206       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6207       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6208       Indented);
6209   verifyFormat(
6210       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6211       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6212       Indented);
6213 
6214   // FIXME: Without the comment, this breaks after "(".
6215   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
6216                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
6217                getGoogleStyle());
6218 
6219   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
6220                "                  int LoooooooooooooooooooongParam2) {}");
6221   verifyFormat(
6222       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
6223       "                                   SourceLocation L, IdentifierIn *II,\n"
6224       "                                   Type *T) {}");
6225   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
6226                "ReallyReaaallyLongFunctionName(\n"
6227                "    const std::string &SomeParameter,\n"
6228                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6229                "        &ReallyReallyLongParameterName,\n"
6230                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6231                "        &AnotherLongParameterName) {}");
6232   verifyFormat("template <typename A>\n"
6233                "SomeLoooooooooooooooooooooongType<\n"
6234                "    typename some_namespace::SomeOtherType<A>::Type>\n"
6235                "Function() {}");
6236 
6237   verifyGoogleFormat(
6238       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
6239       "    aaaaaaaaaaaaaaaaaaaaaaa;");
6240   verifyGoogleFormat(
6241       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
6242       "                                   SourceLocation L) {}");
6243   verifyGoogleFormat(
6244       "some_namespace::LongReturnType\n"
6245       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
6246       "    int first_long_parameter, int second_parameter) {}");
6247 
6248   verifyGoogleFormat("template <typename T>\n"
6249                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6250                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
6251   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6252                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
6253 
6254   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
6255                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6256                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6257   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6258                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6259                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
6260   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6261                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
6262                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
6263                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6264 
6265   verifyFormat("template <typename T> // Templates on own line.\n"
6266                "static int            // Some comment.\n"
6267                "MyFunction(int a);",
6268                getLLVMStyle());
6269 }
6270 
6271 TEST_F(FormatTest, FormatsArrays) {
6272   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6273                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
6274   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
6275                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
6276   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
6277                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
6278   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6279                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6280   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6281                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
6282   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6283                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6284                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6285   verifyFormat(
6286       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
6287       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6288       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
6289   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
6290                "    .aaaaaaaaaaaaaaaaaaaaaa();");
6291 
6292   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
6293                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
6294   verifyFormat(
6295       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
6296       "                                  .aaaaaaa[0]\n"
6297       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
6298   verifyFormat("a[::b::c];");
6299 
6300   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
6301 
6302   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
6303   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
6304 }
6305 
6306 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
6307   verifyFormat("(a)->b();");
6308   verifyFormat("--a;");
6309 }
6310 
6311 TEST_F(FormatTest, HandlesIncludeDirectives) {
6312   verifyFormat("#include <string>\n"
6313                "#include <a/b/c.h>\n"
6314                "#include \"a/b/string\"\n"
6315                "#include \"string.h\"\n"
6316                "#include \"string.h\"\n"
6317                "#include <a-a>\n"
6318                "#include < path with space >\n"
6319                "#include_next <test.h>"
6320                "#include \"abc.h\" // this is included for ABC\n"
6321                "#include \"some long include\" // with a comment\n"
6322                "#include \"some very long include path\"\n"
6323                "#include <some/very/long/include/path>\n",
6324                getLLVMStyleWithColumns(35));
6325   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
6326   EXPECT_EQ("#include <a>", format("#include<a>"));
6327 
6328   verifyFormat("#import <string>");
6329   verifyFormat("#import <a/b/c.h>");
6330   verifyFormat("#import \"a/b/string\"");
6331   verifyFormat("#import \"string.h\"");
6332   verifyFormat("#import \"string.h\"");
6333   verifyFormat("#if __has_include(<strstream>)\n"
6334                "#include <strstream>\n"
6335                "#endif");
6336 
6337   verifyFormat("#define MY_IMPORT <a/b>");
6338 
6339   verifyFormat("#if __has_include(<a/b>)");
6340   verifyFormat("#if __has_include_next(<a/b>)");
6341   verifyFormat("#define F __has_include(<a/b>)");
6342   verifyFormat("#define F __has_include_next(<a/b>)");
6343 
6344   // Protocol buffer definition or missing "#".
6345   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
6346                getLLVMStyleWithColumns(30));
6347 
6348   FormatStyle Style = getLLVMStyle();
6349   Style.AlwaysBreakBeforeMultilineStrings = true;
6350   Style.ColumnLimit = 0;
6351   verifyFormat("#import \"abc.h\"", Style);
6352 
6353   // But 'import' might also be a regular C++ namespace.
6354   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6355                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6356 }
6357 
6358 //===----------------------------------------------------------------------===//
6359 // Error recovery tests.
6360 //===----------------------------------------------------------------------===//
6361 
6362 TEST_F(FormatTest, IncompleteParameterLists) {
6363   FormatStyle NoBinPacking = getLLVMStyle();
6364   NoBinPacking.BinPackParameters = false;
6365   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
6366                "                        double *min_x,\n"
6367                "                        double *max_x,\n"
6368                "                        double *min_y,\n"
6369                "                        double *max_y,\n"
6370                "                        double *min_z,\n"
6371                "                        double *max_z, ) {}",
6372                NoBinPacking);
6373 }
6374 
6375 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
6376   verifyFormat("void f() { return; }\n42");
6377   verifyFormat("void f() {\n"
6378                "  if (0)\n"
6379                "    return;\n"
6380                "}\n"
6381                "42");
6382   verifyFormat("void f() { return }\n42");
6383   verifyFormat("void f() {\n"
6384                "  if (0)\n"
6385                "    return\n"
6386                "}\n"
6387                "42");
6388 }
6389 
6390 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
6391   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
6392   EXPECT_EQ("void f() {\n"
6393             "  if (a)\n"
6394             "    return\n"
6395             "}",
6396             format("void  f  (  )  {  if  ( a )  return  }"));
6397   EXPECT_EQ("namespace N {\n"
6398             "void f()\n"
6399             "}",
6400             format("namespace  N  {  void f()  }"));
6401   EXPECT_EQ("namespace N {\n"
6402             "void f() {}\n"
6403             "void g()\n"
6404             "} // namespace N",
6405             format("namespace N  { void f( ) { } void g( ) }"));
6406 }
6407 
6408 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
6409   verifyFormat("int aaaaaaaa =\n"
6410                "    // Overlylongcomment\n"
6411                "    b;",
6412                getLLVMStyleWithColumns(20));
6413   verifyFormat("function(\n"
6414                "    ShortArgument,\n"
6415                "    LoooooooooooongArgument);\n",
6416                getLLVMStyleWithColumns(20));
6417 }
6418 
6419 TEST_F(FormatTest, IncorrectAccessSpecifier) {
6420   verifyFormat("public:");
6421   verifyFormat("class A {\n"
6422                "public\n"
6423                "  void f() {}\n"
6424                "};");
6425   verifyFormat("public\n"
6426                "int qwerty;");
6427   verifyFormat("public\n"
6428                "B {}");
6429   verifyFormat("public\n"
6430                "{}");
6431   verifyFormat("public\n"
6432                "B { int x; }");
6433 }
6434 
6435 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
6436   verifyFormat("{");
6437   verifyFormat("#})");
6438   verifyNoCrash("(/**/[:!] ?[).");
6439 }
6440 
6441 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
6442   verifyFormat("do {\n}");
6443   verifyFormat("do {\n}\n"
6444                "f();");
6445   verifyFormat("do {\n}\n"
6446                "wheeee(fun);");
6447   verifyFormat("do {\n"
6448                "  f();\n"
6449                "}");
6450 }
6451 
6452 TEST_F(FormatTest, IncorrectCodeMissingParens) {
6453   verifyFormat("if {\n  foo;\n  foo();\n}");
6454   verifyFormat("switch {\n  foo;\n  foo();\n}");
6455   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
6456   verifyFormat("while {\n  foo;\n  foo();\n}");
6457   verifyFormat("do {\n  foo;\n  foo();\n} while;");
6458 }
6459 
6460 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
6461   verifyIncompleteFormat("namespace {\n"
6462                          "class Foo { Foo (\n"
6463                          "};\n"
6464                          "} // namespace");
6465 }
6466 
6467 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
6468   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
6469   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
6470   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
6471   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
6472 
6473   EXPECT_EQ("{\n"
6474             "  {\n"
6475             "    breakme(\n"
6476             "        qwe);\n"
6477             "  }\n",
6478             format("{\n"
6479                    "    {\n"
6480                    " breakme(qwe);\n"
6481                    "}\n",
6482                    getLLVMStyleWithColumns(10)));
6483 }
6484 
6485 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
6486   verifyFormat("int x = {\n"
6487                "    avariable,\n"
6488                "    b(alongervariable)};",
6489                getLLVMStyleWithColumns(25));
6490 }
6491 
6492 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
6493   verifyFormat("return (a)(b){1, 2, 3};");
6494 }
6495 
6496 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
6497   verifyFormat("vector<int> x{1, 2, 3, 4};");
6498   verifyFormat("vector<int> x{\n"
6499                "    1,\n"
6500                "    2,\n"
6501                "    3,\n"
6502                "    4,\n"
6503                "};");
6504   verifyFormat("vector<T> x{{}, {}, {}, {}};");
6505   verifyFormat("f({1, 2});");
6506   verifyFormat("auto v = Foo{-1};");
6507   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
6508   verifyFormat("Class::Class : member{1, 2, 3} {}");
6509   verifyFormat("new vector<int>{1, 2, 3};");
6510   verifyFormat("new int[3]{1, 2, 3};");
6511   verifyFormat("new int{1};");
6512   verifyFormat("return {arg1, arg2};");
6513   verifyFormat("return {arg1, SomeType{parameter}};");
6514   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
6515   verifyFormat("new T{arg1, arg2};");
6516   verifyFormat("f(MyMap[{composite, key}]);");
6517   verifyFormat("class Class {\n"
6518                "  T member = {arg1, arg2};\n"
6519                "};");
6520   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
6521   verifyFormat("const struct A a = {.a = 1, .b = 2};");
6522   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
6523   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
6524   verifyFormat("int a = std::is_integral<int>{} + 0;");
6525 
6526   verifyFormat("int foo(int i) { return fo1{}(i); }");
6527   verifyFormat("int foo(int i) { return fo1{}(i); }");
6528   verifyFormat("auto i = decltype(x){};");
6529   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
6530   verifyFormat("Node n{1, Node{1000}, //\n"
6531                "       2};");
6532   verifyFormat("Aaaa aaaaaaa{\n"
6533                "    {\n"
6534                "        aaaa,\n"
6535                "    },\n"
6536                "};");
6537   verifyFormat("class C : public D {\n"
6538                "  SomeClass SC{2};\n"
6539                "};");
6540   verifyFormat("class C : public A {\n"
6541                "  class D : public B {\n"
6542                "    void f() { int i{2}; }\n"
6543                "  };\n"
6544                "};");
6545   verifyFormat("#define A {a, a},");
6546 
6547   // Binpacking only if there is no trailing comma
6548   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
6549                "                      cccccccccc, dddddddddd};",
6550 			   getLLVMStyleWithColumns(50));
6551   verifyFormat("const Aaaaaa aaaaa = {\n"
6552                "    aaaaaaaaaaa,\n"
6553                "    bbbbbbbbbbb,\n"
6554                "    ccccccccccc,\n"
6555                "    ddddddddddd,\n"
6556                "};", getLLVMStyleWithColumns(50));
6557 
6558   // Cases where distinguising braced lists and blocks is hard.
6559   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
6560   verifyFormat("void f() {\n"
6561                "  return; // comment\n"
6562                "}\n"
6563                "SomeType t;");
6564   verifyFormat("void f() {\n"
6565                "  if (a) {\n"
6566                "    f();\n"
6567                "  }\n"
6568                "}\n"
6569                "SomeType t;");
6570 
6571   // In combination with BinPackArguments = false.
6572   FormatStyle NoBinPacking = getLLVMStyle();
6573   NoBinPacking.BinPackArguments = false;
6574   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
6575                "                      bbbbb,\n"
6576                "                      ccccc,\n"
6577                "                      ddddd,\n"
6578                "                      eeeee,\n"
6579                "                      ffffff,\n"
6580                "                      ggggg,\n"
6581                "                      hhhhhh,\n"
6582                "                      iiiiii,\n"
6583                "                      jjjjjj,\n"
6584                "                      kkkkkk};",
6585                NoBinPacking);
6586   verifyFormat("const Aaaaaa aaaaa = {\n"
6587                "    aaaaa,\n"
6588                "    bbbbb,\n"
6589                "    ccccc,\n"
6590                "    ddddd,\n"
6591                "    eeeee,\n"
6592                "    ffffff,\n"
6593                "    ggggg,\n"
6594                "    hhhhhh,\n"
6595                "    iiiiii,\n"
6596                "    jjjjjj,\n"
6597                "    kkkkkk,\n"
6598                "};",
6599                NoBinPacking);
6600   verifyFormat(
6601       "const Aaaaaa aaaaa = {\n"
6602       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
6603       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
6604       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
6605       "};",
6606       NoBinPacking);
6607 
6608   // FIXME: The alignment of these trailing comments might be bad. Then again,
6609   // this might be utterly useless in real code.
6610   verifyFormat("Constructor::Constructor()\n"
6611                "    : some_value{         //\n"
6612                "                 aaaaaaa, //\n"
6613                "                 bbbbbbb} {}");
6614 
6615   // In braced lists, the first comment is always assumed to belong to the
6616   // first element. Thus, it can be moved to the next or previous line as
6617   // appropriate.
6618   EXPECT_EQ("function({// First element:\n"
6619             "          1,\n"
6620             "          // Second element:\n"
6621             "          2});",
6622             format("function({\n"
6623                    "    // First element:\n"
6624                    "    1,\n"
6625                    "    // Second element:\n"
6626                    "    2});"));
6627   EXPECT_EQ("std::vector<int> MyNumbers{\n"
6628             "    // First element:\n"
6629             "    1,\n"
6630             "    // Second element:\n"
6631             "    2};",
6632             format("std::vector<int> MyNumbers{// First element:\n"
6633                    "                           1,\n"
6634                    "                           // Second element:\n"
6635                    "                           2};",
6636                    getLLVMStyleWithColumns(30)));
6637   // A trailing comma should still lead to an enforced line break and no
6638   // binpacking.
6639   EXPECT_EQ("vector<int> SomeVector = {\n"
6640             "    // aaa\n"
6641             "    1,\n"
6642             "    2,\n"
6643             "};",
6644             format("vector<int> SomeVector = { // aaa\n"
6645                    "    1, 2, };"));
6646 
6647   FormatStyle ExtraSpaces = getLLVMStyle();
6648   ExtraSpaces.Cpp11BracedListStyle = false;
6649   ExtraSpaces.ColumnLimit = 75;
6650   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
6651   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
6652   verifyFormat("f({ 1, 2 });", ExtraSpaces);
6653   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
6654   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
6655   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
6656   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
6657   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
6658   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
6659   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
6660   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
6661   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
6662   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
6663   verifyFormat("class Class {\n"
6664                "  T member = { arg1, arg2 };\n"
6665                "};",
6666                ExtraSpaces);
6667   verifyFormat(
6668       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6669       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
6670       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6671       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
6672       ExtraSpaces);
6673   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
6674   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
6675                ExtraSpaces);
6676   verifyFormat(
6677       "someFunction(OtherParam,\n"
6678       "             BracedList{ // comment 1 (Forcing interesting break)\n"
6679       "                         param1, param2,\n"
6680       "                         // comment 2\n"
6681       "                         param3, param4 });",
6682       ExtraSpaces);
6683   verifyFormat(
6684       "std::this_thread::sleep_for(\n"
6685       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
6686       ExtraSpaces);
6687   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
6688                "    aaaaaaa,\n"
6689                "    aaaaaaaaaa,\n"
6690                "    aaaaa,\n"
6691                "    aaaaaaaaaaaaaaa,\n"
6692                "    aaa,\n"
6693                "    aaaaaaaaaa,\n"
6694                "    a,\n"
6695                "    aaaaaaaaaaaaaaaaaaaaa,\n"
6696                "    aaaaaaaaaaaa,\n"
6697                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
6698                "    aaaaaaa,\n"
6699                "    a};");
6700   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
6701   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
6702   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
6703 }
6704 
6705 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
6706   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6707                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6708                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6709                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6710                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6711                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6712   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
6713                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6714                "                 1, 22, 333, 4444, 55555, //\n"
6715                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6716                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6717   verifyFormat(
6718       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6719       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6720       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
6721       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6722       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6723       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6724       "                 7777777};");
6725   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6726                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6727                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6728   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6729                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6730                "    // Separating comment.\n"
6731                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
6732   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6733                "    // Leading comment\n"
6734                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6735                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6736   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6737                "                 1, 1, 1, 1};",
6738                getLLVMStyleWithColumns(39));
6739   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6740                "                 1, 1, 1, 1};",
6741                getLLVMStyleWithColumns(38));
6742   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
6743                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
6744                getLLVMStyleWithColumns(43));
6745   verifyFormat(
6746       "static unsigned SomeValues[10][3] = {\n"
6747       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
6748       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
6749   verifyFormat("static auto fields = new vector<string>{\n"
6750                "    \"aaaaaaaaaaaaa\",\n"
6751                "    \"aaaaaaaaaaaaa\",\n"
6752                "    \"aaaaaaaaaaaa\",\n"
6753                "    \"aaaaaaaaaaaaaa\",\n"
6754                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6755                "    \"aaaaaaaaaaaa\",\n"
6756                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6757                "};");
6758   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
6759   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
6760                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
6761                "                 3, cccccccccccccccccccccc};",
6762                getLLVMStyleWithColumns(60));
6763 
6764   // Trailing commas.
6765   verifyFormat("vector<int> x = {\n"
6766                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
6767                "};",
6768                getLLVMStyleWithColumns(39));
6769   verifyFormat("vector<int> x = {\n"
6770                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
6771                "};",
6772                getLLVMStyleWithColumns(39));
6773   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6774                "                 1, 1, 1, 1,\n"
6775                "                 /**/ /**/};",
6776                getLLVMStyleWithColumns(39));
6777 
6778   // Trailing comment in the first line.
6779   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
6780                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
6781                "    111111111,  222222222,  3333333333,  444444444,  //\n"
6782                "    11111111,   22222222,   333333333,   44444444};");
6783   // Trailing comment in the last line.
6784   verifyFormat("int aaaaa[] = {\n"
6785                "    1, 2, 3, // comment\n"
6786                "    4, 5, 6  // comment\n"
6787                "};");
6788 
6789   // With nested lists, we should either format one item per line or all nested
6790   // lists one on line.
6791   // FIXME: For some nested lists, we can do better.
6792   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
6793                "        {aaaaaaaaaaaaaaaaaaa},\n"
6794                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
6795                "        {aaaaaaaaaaaaaaaaa}};",
6796                getLLVMStyleWithColumns(60));
6797   verifyFormat(
6798       "SomeStruct my_struct_array = {\n"
6799       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
6800       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
6801       "    {aaa, aaa},\n"
6802       "    {aaa, aaa},\n"
6803       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
6804       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6805       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
6806 
6807   // No column layout should be used here.
6808   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
6809                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
6810 
6811   verifyNoCrash("a<,");
6812 
6813   // No braced initializer here.
6814   verifyFormat("void f() {\n"
6815                "  struct Dummy {};\n"
6816                "  f(v);\n"
6817                "}");
6818 
6819   // Long lists should be formatted in columns even if they are nested.
6820   verifyFormat(
6821       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6822       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6823       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6824       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6825       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6826       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
6827 
6828   // Allow "single-column" layout even if that violates the column limit. There
6829   // isn't going to be a better way.
6830   verifyFormat("std::vector<int> a = {\n"
6831                "    aaaaaaaa,\n"
6832                "    aaaaaaaa,\n"
6833                "    aaaaaaaa,\n"
6834                "    aaaaaaaa,\n"
6835                "    aaaaaaaaaa,\n"
6836                "    aaaaaaaa,\n"
6837                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
6838                getLLVMStyleWithColumns(30));
6839   verifyFormat("vector<int> aaaa = {\n"
6840                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6841                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6842                "    aaaaaa.aaaaaaa,\n"
6843                "    aaaaaa.aaaaaaa,\n"
6844                "    aaaaaa.aaaaaaa,\n"
6845                "    aaaaaa.aaaaaaa,\n"
6846                "};");
6847 
6848   // Don't create hanging lists.
6849   verifyFormat("someFunction(Param, {List1, List2,\n"
6850                "                     List3});",
6851                getLLVMStyleWithColumns(35));
6852   verifyFormat("someFunction(Param, Param,\n"
6853                "             {List1, List2,\n"
6854                "              List3});",
6855                getLLVMStyleWithColumns(35));
6856   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
6857                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
6858 }
6859 
6860 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
6861   FormatStyle DoNotMerge = getLLVMStyle();
6862   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6863 
6864   verifyFormat("void f() { return 42; }");
6865   verifyFormat("void f() {\n"
6866                "  return 42;\n"
6867                "}",
6868                DoNotMerge);
6869   verifyFormat("void f() {\n"
6870                "  // Comment\n"
6871                "}");
6872   verifyFormat("{\n"
6873                "#error {\n"
6874                "  int a;\n"
6875                "}");
6876   verifyFormat("{\n"
6877                "  int a;\n"
6878                "#error {\n"
6879                "}");
6880   verifyFormat("void f() {} // comment");
6881   verifyFormat("void f() { int a; } // comment");
6882   verifyFormat("void f() {\n"
6883                "} // comment",
6884                DoNotMerge);
6885   verifyFormat("void f() {\n"
6886                "  int a;\n"
6887                "} // comment",
6888                DoNotMerge);
6889   verifyFormat("void f() {\n"
6890                "} // comment",
6891                getLLVMStyleWithColumns(15));
6892 
6893   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
6894   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
6895 
6896   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
6897   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
6898   verifyFormat("class C {\n"
6899                "  C()\n"
6900                "      : iiiiiiii(nullptr),\n"
6901                "        kkkkkkk(nullptr),\n"
6902                "        mmmmmmm(nullptr),\n"
6903                "        nnnnnnn(nullptr) {}\n"
6904                "};",
6905                getGoogleStyle());
6906 
6907   FormatStyle NoColumnLimit = getLLVMStyle();
6908   NoColumnLimit.ColumnLimit = 0;
6909   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
6910   EXPECT_EQ("class C {\n"
6911             "  A() : b(0) {}\n"
6912             "};",
6913             format("class C{A():b(0){}};", NoColumnLimit));
6914   EXPECT_EQ("A()\n"
6915             "    : b(0) {\n"
6916             "}",
6917             format("A()\n:b(0)\n{\n}", NoColumnLimit));
6918 
6919   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
6920   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
6921       FormatStyle::SFS_None;
6922   EXPECT_EQ("A()\n"
6923             "    : b(0) {\n"
6924             "}",
6925             format("A():b(0){}", DoNotMergeNoColumnLimit));
6926   EXPECT_EQ("A()\n"
6927             "    : b(0) {\n"
6928             "}",
6929             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
6930 
6931   verifyFormat("#define A          \\\n"
6932                "  void f() {       \\\n"
6933                "    int i;         \\\n"
6934                "  }",
6935                getLLVMStyleWithColumns(20));
6936   verifyFormat("#define A           \\\n"
6937                "  void f() { int i; }",
6938                getLLVMStyleWithColumns(21));
6939   verifyFormat("#define A            \\\n"
6940                "  void f() {         \\\n"
6941                "    int i;           \\\n"
6942                "  }                  \\\n"
6943                "  int j;",
6944                getLLVMStyleWithColumns(22));
6945   verifyFormat("#define A             \\\n"
6946                "  void f() { int i; } \\\n"
6947                "  int j;",
6948                getLLVMStyleWithColumns(23));
6949 }
6950 
6951 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
6952   FormatStyle MergeEmptyOnly = getLLVMStyle();
6953   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
6954   verifyFormat("class C {\n"
6955                "  int f() {}\n"
6956                "};",
6957                MergeEmptyOnly);
6958   verifyFormat("class C {\n"
6959                "  int f() {\n"
6960                "    return 42;\n"
6961                "  }\n"
6962                "};",
6963                MergeEmptyOnly);
6964   verifyFormat("int f() {}", MergeEmptyOnly);
6965   verifyFormat("int f() {\n"
6966                "  return 42;\n"
6967                "}",
6968                MergeEmptyOnly);
6969 
6970   // Also verify behavior when BraceWrapping.AfterFunction = true
6971   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
6972   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
6973   verifyFormat("int f() {}", MergeEmptyOnly);
6974   verifyFormat("class C {\n"
6975                "  int f() {}\n"
6976                "};",
6977                MergeEmptyOnly);
6978 }
6979 
6980 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
6981   FormatStyle MergeInlineOnly = getLLVMStyle();
6982   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
6983   verifyFormat("class C {\n"
6984                "  int f() { return 42; }\n"
6985                "};",
6986                MergeInlineOnly);
6987   verifyFormat("int f() {\n"
6988                "  return 42;\n"
6989                "}",
6990                MergeInlineOnly);
6991 
6992   // SFS_Inline implies SFS_Empty
6993   verifyFormat("class C {\n"
6994                "  int f() {}\n"
6995                "};",
6996                MergeInlineOnly);
6997   verifyFormat("int f() {}", MergeInlineOnly);
6998 
6999   // Also verify behavior when BraceWrapping.AfterFunction = true
7000   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7001   MergeInlineOnly.BraceWrapping.AfterFunction = true;
7002   verifyFormat("class C {\n"
7003                "  int f() { return 42; }\n"
7004                "};",
7005                MergeInlineOnly);
7006   verifyFormat("int f()\n"
7007                "{\n"
7008                "  return 42;\n"
7009                "}",
7010                MergeInlineOnly);
7011 
7012   // SFS_Inline implies SFS_Empty
7013   verifyFormat("int f() {}", MergeInlineOnly);
7014   verifyFormat("class C {\n"
7015                "  int f() {}\n"
7016                "};",
7017                MergeInlineOnly);
7018 }
7019 
7020 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
7021   FormatStyle MergeInlineOnly = getLLVMStyle();
7022   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
7023       FormatStyle::SFS_InlineOnly;
7024   verifyFormat("class C {\n"
7025                "  int f() { return 42; }\n"
7026                "};",
7027                MergeInlineOnly);
7028   verifyFormat("int f() {\n"
7029                "  return 42;\n"
7030                "}",
7031                MergeInlineOnly);
7032 
7033   // SFS_InlineOnly does not imply SFS_Empty
7034   verifyFormat("class C {\n"
7035                "  int f() {}\n"
7036                "};",
7037                MergeInlineOnly);
7038   verifyFormat("int f() {\n"
7039                "}",
7040                MergeInlineOnly);
7041 
7042   // Also verify behavior when BraceWrapping.AfterFunction = true
7043   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7044   MergeInlineOnly.BraceWrapping.AfterFunction = true;
7045   verifyFormat("class C {\n"
7046                "  int f() { return 42; }\n"
7047                "};",
7048                MergeInlineOnly);
7049   verifyFormat("int f()\n"
7050                "{\n"
7051                "  return 42;\n"
7052                "}",
7053                MergeInlineOnly);
7054 
7055   // SFS_InlineOnly does not imply SFS_Empty
7056   verifyFormat("int f()\n"
7057                "{\n"
7058                "}",
7059                MergeInlineOnly);
7060   verifyFormat("class C {\n"
7061                "  int f() {}\n"
7062                "};",
7063                MergeInlineOnly);
7064 }
7065 
7066 TEST_F(FormatTest, SplitEmptyFunction) {
7067   FormatStyle Style = getLLVMStyle();
7068   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
7069   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7070   Style.BraceWrapping.AfterFunction = true;
7071   Style.BraceWrapping.SplitEmptyFunction = false;
7072   Style.ColumnLimit = 40;
7073 
7074   verifyFormat("int f()\n"
7075                "{}",
7076                Style);
7077   verifyFormat("int f()\n"
7078                "{\n"
7079                "  return 42;\n"
7080                "}",
7081                Style);
7082   verifyFormat("int f()\n"
7083                "{\n"
7084                "  // some comment\n"
7085                "}",
7086                Style);
7087 
7088   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
7089   verifyFormat("int f() {}", Style);
7090   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7091                "{}",
7092                Style);
7093   verifyFormat("int f()\n"
7094                "{\n"
7095                "  return 0;\n"
7096                "}",
7097                Style);
7098 
7099   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
7100   verifyFormat("class Foo {\n"
7101                "  int f() {}\n"
7102                "};\n",
7103                Style);
7104   verifyFormat("class Foo {\n"
7105                "  int f() { return 0; }\n"
7106                "};\n",
7107                Style);
7108   verifyFormat("class Foo {\n"
7109                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7110                "  {}\n"
7111                "};\n",
7112                Style);
7113   verifyFormat("class Foo {\n"
7114                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7115                "  {\n"
7116                "    return 0;\n"
7117                "  }\n"
7118                "};\n",
7119                Style);
7120 
7121   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
7122   verifyFormat("int f() {}", Style);
7123   verifyFormat("int f() { return 0; }", Style);
7124   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7125                "{}",
7126                Style);
7127   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7128                "{\n"
7129                "  return 0;\n"
7130                "}",
7131                Style);
7132 }
7133 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
7134   FormatStyle Style = getLLVMStyle();
7135   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
7136   verifyFormat("#ifdef A\n"
7137                "int f() {}\n"
7138                "#else\n"
7139                "int g() {}\n"
7140                "#endif",
7141                Style);
7142 }
7143 
7144 TEST_F(FormatTest, SplitEmptyClass) {
7145   FormatStyle Style = getLLVMStyle();
7146   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7147   Style.BraceWrapping.AfterClass = true;
7148   Style.BraceWrapping.SplitEmptyRecord = false;
7149 
7150   verifyFormat("class Foo\n"
7151                "{};",
7152                Style);
7153   verifyFormat("/* something */ class Foo\n"
7154                "{};",
7155                Style);
7156   verifyFormat("template <typename X> class Foo\n"
7157                "{};",
7158                Style);
7159   verifyFormat("class Foo\n"
7160                "{\n"
7161                "  Foo();\n"
7162                "};",
7163                Style);
7164   verifyFormat("typedef class Foo\n"
7165                "{\n"
7166                "} Foo_t;",
7167                Style);
7168 }
7169 
7170 TEST_F(FormatTest, SplitEmptyStruct) {
7171   FormatStyle Style = getLLVMStyle();
7172   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7173   Style.BraceWrapping.AfterStruct = true;
7174   Style.BraceWrapping.SplitEmptyRecord = false;
7175 
7176   verifyFormat("struct Foo\n"
7177                "{};",
7178                Style);
7179   verifyFormat("/* something */ struct Foo\n"
7180                "{};",
7181                Style);
7182   verifyFormat("template <typename X> struct Foo\n"
7183                "{};",
7184                Style);
7185   verifyFormat("struct Foo\n"
7186                "{\n"
7187                "  Foo();\n"
7188                "};",
7189                Style);
7190   verifyFormat("typedef struct Foo\n"
7191                "{\n"
7192                "} Foo_t;",
7193                Style);
7194   //typedef struct Bar {} Bar_t;
7195 }
7196 
7197 TEST_F(FormatTest, SplitEmptyUnion) {
7198   FormatStyle Style = getLLVMStyle();
7199   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7200   Style.BraceWrapping.AfterUnion = true;
7201   Style.BraceWrapping.SplitEmptyRecord = false;
7202 
7203   verifyFormat("union Foo\n"
7204                "{};",
7205                Style);
7206   verifyFormat("/* something */ union Foo\n"
7207                "{};",
7208                Style);
7209   verifyFormat("union Foo\n"
7210                "{\n"
7211                "  A,\n"
7212                "};",
7213                Style);
7214   verifyFormat("typedef union Foo\n"
7215                "{\n"
7216                "} Foo_t;",
7217                Style);
7218 }
7219 
7220 TEST_F(FormatTest, SplitEmptyNamespace) {
7221   FormatStyle Style = getLLVMStyle();
7222   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7223   Style.BraceWrapping.AfterNamespace = true;
7224   Style.BraceWrapping.SplitEmptyNamespace = false;
7225 
7226   verifyFormat("namespace Foo\n"
7227                "{};",
7228                Style);
7229   verifyFormat("/* something */ namespace Foo\n"
7230                "{};",
7231                Style);
7232   verifyFormat("inline namespace Foo\n"
7233                "{};",
7234                Style);
7235   verifyFormat("namespace Foo\n"
7236                "{\n"
7237                "void Bar();\n"
7238                "};",
7239                Style);
7240 }
7241 
7242 TEST_F(FormatTest, NeverMergeShortRecords) {
7243   FormatStyle Style = getLLVMStyle();
7244 
7245   verifyFormat("class Foo {\n"
7246                "  Foo();\n"
7247                "};",
7248                Style);
7249   verifyFormat("typedef class Foo {\n"
7250                "  Foo();\n"
7251                "} Foo_t;",
7252                Style);
7253   verifyFormat("struct Foo {\n"
7254                "  Foo();\n"
7255                "};",
7256                Style);
7257   verifyFormat("typedef struct Foo {\n"
7258                "  Foo();\n"
7259                "} Foo_t;",
7260                Style);
7261   verifyFormat("union Foo {\n"
7262                "  A,\n"
7263                "};",
7264                Style);
7265   verifyFormat("typedef union Foo {\n"
7266                "  A,\n"
7267                "} Foo_t;",
7268                Style);
7269   verifyFormat("namespace Foo {\n"
7270                "void Bar();\n"
7271                "};",
7272                Style);
7273 
7274   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7275   Style.BraceWrapping.AfterClass = true;
7276   Style.BraceWrapping.AfterStruct = true;
7277   Style.BraceWrapping.AfterUnion = true;
7278   Style.BraceWrapping.AfterNamespace = true;
7279   verifyFormat("class Foo\n"
7280                "{\n"
7281                "  Foo();\n"
7282                "};",
7283                Style);
7284   verifyFormat("typedef class Foo\n"
7285                "{\n"
7286                "  Foo();\n"
7287                "} Foo_t;",
7288                Style);
7289   verifyFormat("struct Foo\n"
7290                "{\n"
7291                "  Foo();\n"
7292                "};",
7293                Style);
7294   verifyFormat("typedef struct Foo\n"
7295                "{\n"
7296                "  Foo();\n"
7297                "} Foo_t;",
7298                Style);
7299   verifyFormat("union Foo\n"
7300                "{\n"
7301                "  A,\n"
7302                "};",
7303                Style);
7304   verifyFormat("typedef union Foo\n"
7305                "{\n"
7306                "  A,\n"
7307                "} Foo_t;",
7308                Style);
7309   verifyFormat("namespace Foo\n"
7310                "{\n"
7311                "void Bar();\n"
7312                "};",
7313                Style);
7314 }
7315 
7316 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
7317   // Elaborate type variable declarations.
7318   verifyFormat("struct foo a = {bar};\nint n;");
7319   verifyFormat("class foo a = {bar};\nint n;");
7320   verifyFormat("union foo a = {bar};\nint n;");
7321 
7322   // Elaborate types inside function definitions.
7323   verifyFormat("struct foo f() {}\nint n;");
7324   verifyFormat("class foo f() {}\nint n;");
7325   verifyFormat("union foo f() {}\nint n;");
7326 
7327   // Templates.
7328   verifyFormat("template <class X> void f() {}\nint n;");
7329   verifyFormat("template <struct X> void f() {}\nint n;");
7330   verifyFormat("template <union X> void f() {}\nint n;");
7331 
7332   // Actual definitions...
7333   verifyFormat("struct {\n} n;");
7334   verifyFormat(
7335       "template <template <class T, class Y>, class Z> class X {\n} n;");
7336   verifyFormat("union Z {\n  int n;\n} x;");
7337   verifyFormat("class MACRO Z {\n} n;");
7338   verifyFormat("class MACRO(X) Z {\n} n;");
7339   verifyFormat("class __attribute__(X) Z {\n} n;");
7340   verifyFormat("class __declspec(X) Z {\n} n;");
7341   verifyFormat("class A##B##C {\n} n;");
7342   verifyFormat("class alignas(16) Z {\n} n;");
7343   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
7344   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
7345 
7346   // Redefinition from nested context:
7347   verifyFormat("class A::B::C {\n} n;");
7348 
7349   // Template definitions.
7350   verifyFormat(
7351       "template <typename F>\n"
7352       "Matcher(const Matcher<F> &Other,\n"
7353       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
7354       "                             !is_same<F, T>::value>::type * = 0)\n"
7355       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
7356 
7357   // FIXME: This is still incorrectly handled at the formatter side.
7358   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
7359   verifyFormat("int i = SomeFunction(a<b, a> b);");
7360 
7361   // FIXME:
7362   // This now gets parsed incorrectly as class definition.
7363   // verifyFormat("class A<int> f() {\n}\nint n;");
7364 
7365   // Elaborate types where incorrectly parsing the structural element would
7366   // break the indent.
7367   verifyFormat("if (true)\n"
7368                "  class X x;\n"
7369                "else\n"
7370                "  f();\n");
7371 
7372   // This is simply incomplete. Formatting is not important, but must not crash.
7373   verifyFormat("class A:");
7374 }
7375 
7376 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
7377   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
7378             format("#error Leave     all         white!!!!! space* alone!\n"));
7379   EXPECT_EQ(
7380       "#warning Leave     all         white!!!!! space* alone!\n",
7381       format("#warning Leave     all         white!!!!! space* alone!\n"));
7382   EXPECT_EQ("#error 1", format("  #  error   1"));
7383   EXPECT_EQ("#warning 1", format("  #  warning 1"));
7384 }
7385 
7386 TEST_F(FormatTest, FormatHashIfExpressions) {
7387   verifyFormat("#if AAAA && BBBB");
7388   verifyFormat("#if (AAAA && BBBB)");
7389   verifyFormat("#elif (AAAA && BBBB)");
7390   // FIXME: Come up with a better indentation for #elif.
7391   verifyFormat(
7392       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
7393       "    defined(BBBBBBBB)\n"
7394       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
7395       "    defined(BBBBBBBB)\n"
7396       "#endif",
7397       getLLVMStyleWithColumns(65));
7398 }
7399 
7400 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
7401   FormatStyle AllowsMergedIf = getGoogleStyle();
7402   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
7403   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
7404   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
7405   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
7406   EXPECT_EQ("if (true) return 42;",
7407             format("if (true)\nreturn 42;", AllowsMergedIf));
7408   FormatStyle ShortMergedIf = AllowsMergedIf;
7409   ShortMergedIf.ColumnLimit = 25;
7410   verifyFormat("#define A \\\n"
7411                "  if (true) return 42;",
7412                ShortMergedIf);
7413   verifyFormat("#define A \\\n"
7414                "  f();    \\\n"
7415                "  if (true)\n"
7416                "#define B",
7417                ShortMergedIf);
7418   verifyFormat("#define A \\\n"
7419                "  f();    \\\n"
7420                "  if (true)\n"
7421                "g();",
7422                ShortMergedIf);
7423   verifyFormat("{\n"
7424                "#ifdef A\n"
7425                "  // Comment\n"
7426                "  if (true) continue;\n"
7427                "#endif\n"
7428                "  // Comment\n"
7429                "  if (true) continue;\n"
7430                "}",
7431                ShortMergedIf);
7432   ShortMergedIf.ColumnLimit = 33;
7433   verifyFormat("#define A \\\n"
7434                "  if constexpr (true) return 42;",
7435                ShortMergedIf);
7436   ShortMergedIf.ColumnLimit = 29;
7437   verifyFormat("#define A                   \\\n"
7438                "  if (aaaaaaaaaa) return 1; \\\n"
7439                "  return 2;",
7440                ShortMergedIf);
7441   ShortMergedIf.ColumnLimit = 28;
7442   verifyFormat("#define A         \\\n"
7443                "  if (aaaaaaaaaa) \\\n"
7444                "    return 1;     \\\n"
7445                "  return 2;",
7446                ShortMergedIf);
7447   verifyFormat("#define A                \\\n"
7448                "  if constexpr (aaaaaaa) \\\n"
7449                "    return 1;            \\\n"
7450                "  return 2;",
7451                ShortMergedIf);
7452 }
7453 
7454 TEST_F(FormatTest, FormatStarDependingOnContext) {
7455   verifyFormat("void f(int *a);");
7456   verifyFormat("void f() { f(fint * b); }");
7457   verifyFormat("class A {\n  void f(int *a);\n};");
7458   verifyFormat("class A {\n  int *a;\n};");
7459   verifyFormat("namespace a {\n"
7460                "namespace b {\n"
7461                "class A {\n"
7462                "  void f() {}\n"
7463                "  int *a;\n"
7464                "};\n"
7465                "} // namespace b\n"
7466                "} // namespace a");
7467 }
7468 
7469 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
7470   verifyFormat("while");
7471   verifyFormat("operator");
7472 }
7473 
7474 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
7475   // This code would be painfully slow to format if we didn't skip it.
7476   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
7477                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7478                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7479                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7480                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7481                    "A(1, 1)\n"
7482                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
7483                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7484                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7485                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7486                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7487                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7488                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7489                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7490                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7491                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
7492   // Deeply nested part is untouched, rest is formatted.
7493   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
7494             format(std::string("int    i;\n") + Code + "int    j;\n",
7495                    getLLVMStyle(), SC_ExpectIncomplete));
7496 }
7497 
7498 //===----------------------------------------------------------------------===//
7499 // Objective-C tests.
7500 //===----------------------------------------------------------------------===//
7501 
7502 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
7503   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
7504   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
7505             format("-(NSUInteger)indexOfObject:(id)anObject;"));
7506   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
7507   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
7508   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
7509             format("-(NSInteger)Method3:(id)anObject;"));
7510   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
7511             format("-(NSInteger)Method4:(id)anObject;"));
7512   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
7513             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
7514   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
7515             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
7516   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7517             "forAllCells:(BOOL)flag;",
7518             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7519                    "forAllCells:(BOOL)flag;"));
7520 
7521   // Very long objectiveC method declaration.
7522   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
7523                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
7524   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
7525                "                    inRange:(NSRange)range\n"
7526                "                   outRange:(NSRange)out_range\n"
7527                "                  outRange1:(NSRange)out_range1\n"
7528                "                  outRange2:(NSRange)out_range2\n"
7529                "                  outRange3:(NSRange)out_range3\n"
7530                "                  outRange4:(NSRange)out_range4\n"
7531                "                  outRange5:(NSRange)out_range5\n"
7532                "                  outRange6:(NSRange)out_range6\n"
7533                "                  outRange7:(NSRange)out_range7\n"
7534                "                  outRange8:(NSRange)out_range8\n"
7535                "                  outRange9:(NSRange)out_range9;");
7536 
7537   // When the function name has to be wrapped.
7538   FormatStyle Style = getLLVMStyle();
7539   Style.IndentWrappedFunctionNames = false;
7540   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7541                "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7542                "           anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7543                "}",
7544                Style);
7545   Style.IndentWrappedFunctionNames = true;
7546   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7547                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7548                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7549                "}",
7550                Style);
7551 
7552   verifyFormat("- (int)sum:(vector<int>)numbers;");
7553   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
7554   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
7555   // protocol lists (but not for template classes):
7556   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
7557 
7558   verifyFormat("- (int (*)())foo:(int (*)())f;");
7559   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
7560 
7561   // If there's no return type (very rare in practice!), LLVM and Google style
7562   // agree.
7563   verifyFormat("- foo;");
7564   verifyFormat("- foo:(int)f;");
7565   verifyGoogleFormat("- foo:(int)foo;");
7566 }
7567 
7568 
7569 TEST_F(FormatTest, BreaksStringLiterals) {
7570   EXPECT_EQ("\"some text \"\n"
7571             "\"other\";",
7572             format("\"some text other\";", getLLVMStyleWithColumns(12)));
7573   EXPECT_EQ("\"some text \"\n"
7574             "\"other\";",
7575             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
7576   EXPECT_EQ(
7577       "#define A  \\\n"
7578       "  \"some \"  \\\n"
7579       "  \"text \"  \\\n"
7580       "  \"other\";",
7581       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
7582   EXPECT_EQ(
7583       "#define A  \\\n"
7584       "  \"so \"    \\\n"
7585       "  \"text \"  \\\n"
7586       "  \"other\";",
7587       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
7588 
7589   EXPECT_EQ("\"some text\"",
7590             format("\"some text\"", getLLVMStyleWithColumns(1)));
7591   EXPECT_EQ("\"some text\"",
7592             format("\"some text\"", getLLVMStyleWithColumns(11)));
7593   EXPECT_EQ("\"some \"\n"
7594             "\"text\"",
7595             format("\"some text\"", getLLVMStyleWithColumns(10)));
7596   EXPECT_EQ("\"some \"\n"
7597             "\"text\"",
7598             format("\"some text\"", getLLVMStyleWithColumns(7)));
7599   EXPECT_EQ("\"some\"\n"
7600             "\" tex\"\n"
7601             "\"t\"",
7602             format("\"some text\"", getLLVMStyleWithColumns(6)));
7603   EXPECT_EQ("\"some\"\n"
7604             "\" tex\"\n"
7605             "\" and\"",
7606             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
7607   EXPECT_EQ("\"some\"\n"
7608             "\"/tex\"\n"
7609             "\"/and\"",
7610             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
7611 
7612   EXPECT_EQ("variable =\n"
7613             "    \"long string \"\n"
7614             "    \"literal\";",
7615             format("variable = \"long string literal\";",
7616                    getLLVMStyleWithColumns(20)));
7617 
7618   EXPECT_EQ("variable = f(\n"
7619             "    \"long string \"\n"
7620             "    \"literal\",\n"
7621             "    short,\n"
7622             "    loooooooooooooooooooong);",
7623             format("variable = f(\"long string literal\", short, "
7624                    "loooooooooooooooooooong);",
7625                    getLLVMStyleWithColumns(20)));
7626 
7627   EXPECT_EQ(
7628       "f(g(\"long string \"\n"
7629       "    \"literal\"),\n"
7630       "  b);",
7631       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
7632   EXPECT_EQ("f(g(\"long string \"\n"
7633             "    \"literal\",\n"
7634             "    a),\n"
7635             "  b);",
7636             format("f(g(\"long string literal\", a), b);",
7637                    getLLVMStyleWithColumns(20)));
7638   EXPECT_EQ(
7639       "f(\"one two\".split(\n"
7640       "    variable));",
7641       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
7642   EXPECT_EQ("f(\"one two three four five six \"\n"
7643             "  \"seven\".split(\n"
7644             "      really_looooong_variable));",
7645             format("f(\"one two three four five six seven\"."
7646                    "split(really_looooong_variable));",
7647                    getLLVMStyleWithColumns(33)));
7648 
7649   EXPECT_EQ("f(\"some \"\n"
7650             "  \"text\",\n"
7651             "  other);",
7652             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
7653 
7654   // Only break as a last resort.
7655   verifyFormat(
7656       "aaaaaaaaaaaaaaaaaaaa(\n"
7657       "    aaaaaaaaaaaaaaaaaaaa,\n"
7658       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
7659 
7660   EXPECT_EQ("\"splitmea\"\n"
7661             "\"trandomp\"\n"
7662             "\"oint\"",
7663             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
7664 
7665   EXPECT_EQ("\"split/\"\n"
7666             "\"pathat/\"\n"
7667             "\"slashes\"",
7668             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7669 
7670   EXPECT_EQ("\"split/\"\n"
7671             "\"pathat/\"\n"
7672             "\"slashes\"",
7673             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7674   EXPECT_EQ("\"split at \"\n"
7675             "\"spaces/at/\"\n"
7676             "\"slashes.at.any$\"\n"
7677             "\"non-alphanumeric%\"\n"
7678             "\"1111111111characte\"\n"
7679             "\"rs\"",
7680             format("\"split at "
7681                    "spaces/at/"
7682                    "slashes.at."
7683                    "any$non-"
7684                    "alphanumeric%"
7685                    "1111111111characte"
7686                    "rs\"",
7687                    getLLVMStyleWithColumns(20)));
7688 
7689   // Verify that splitting the strings understands
7690   // Style::AlwaysBreakBeforeMultilineStrings.
7691   EXPECT_EQ(
7692       "aaaaaaaaaaaa(\n"
7693       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
7694       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
7695       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
7696              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7697              "aaaaaaaaaaaaaaaaaaaaaa\");",
7698              getGoogleStyle()));
7699   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7700             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
7701             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
7702                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7703                    "aaaaaaaaaaaaaaaaaaaaaa\";",
7704                    getGoogleStyle()));
7705   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7706             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7707             format("llvm::outs() << "
7708                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
7709                    "aaaaaaaaaaaaaaaaaaa\";"));
7710   EXPECT_EQ("ffff(\n"
7711             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7712             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7713             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
7714                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7715                    getGoogleStyle()));
7716 
7717   FormatStyle Style = getLLVMStyleWithColumns(12);
7718   Style.BreakStringLiterals = false;
7719   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
7720 
7721   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
7722   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
7723   EXPECT_EQ("#define A \\\n"
7724             "  \"some \" \\\n"
7725             "  \"text \" \\\n"
7726             "  \"other\";",
7727             format("#define A \"some text other\";", AlignLeft));
7728 }
7729 
7730 TEST_F(FormatTest, FullyRemoveEmptyLines) {
7731   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
7732   NoEmptyLines.MaxEmptyLinesToKeep = 0;
7733   EXPECT_EQ("int i = a(b());",
7734             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
7735 }
7736 
7737 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
7738   EXPECT_EQ(
7739       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7740       "(\n"
7741       "    \"x\t\");",
7742       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7743              "aaaaaaa("
7744              "\"x\t\");"));
7745 }
7746 
7747 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
7748   EXPECT_EQ(
7749       "u8\"utf8 string \"\n"
7750       "u8\"literal\";",
7751       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
7752   EXPECT_EQ(
7753       "u\"utf16 string \"\n"
7754       "u\"literal\";",
7755       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
7756   EXPECT_EQ(
7757       "U\"utf32 string \"\n"
7758       "U\"literal\";",
7759       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
7760   EXPECT_EQ("L\"wide string \"\n"
7761             "L\"literal\";",
7762             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
7763   EXPECT_EQ("@\"NSString \"\n"
7764             "@\"literal\";",
7765             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
7766   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
7767 
7768   // This input makes clang-format try to split the incomplete unicode escape
7769   // sequence, which used to lead to a crasher.
7770   verifyNoCrash(
7771       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
7772       getLLVMStyleWithColumns(60));
7773 }
7774 
7775 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
7776   FormatStyle Style = getGoogleStyleWithColumns(15);
7777   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
7778   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
7779   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
7780   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
7781   EXPECT_EQ("u8R\"x(raw literal)x\";",
7782             format("u8R\"x(raw literal)x\";", Style));
7783 }
7784 
7785 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
7786   FormatStyle Style = getLLVMStyleWithColumns(20);
7787   EXPECT_EQ(
7788       "_T(\"aaaaaaaaaaaaaa\")\n"
7789       "_T(\"aaaaaaaaaaaaaa\")\n"
7790       "_T(\"aaaaaaaaaaaa\")",
7791       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
7792   EXPECT_EQ("f(x,\n"
7793             "  _T(\"aaaaaaaaaaaa\")\n"
7794             "  _T(\"aaa\"),\n"
7795             "  z);",
7796             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
7797 
7798   // FIXME: Handle embedded spaces in one iteration.
7799   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
7800   //            "_T(\"aaaaaaaaaaaaa\")\n"
7801   //            "_T(\"aaaaaaaaaaaaa\")\n"
7802   //            "_T(\"a\")",
7803   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7804   //                   getLLVMStyleWithColumns(20)));
7805   EXPECT_EQ(
7806       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7807       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
7808   EXPECT_EQ("f(\n"
7809             "#if !TEST\n"
7810             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7811             "#endif\n"
7812             ");",
7813             format("f(\n"
7814                    "#if !TEST\n"
7815                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7816                    "#endif\n"
7817                    ");"));
7818   EXPECT_EQ("f(\n"
7819             "\n"
7820             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
7821             format("f(\n"
7822                    "\n"
7823                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
7824 }
7825 
7826 TEST_F(FormatTest, BreaksStringLiteralOperands) {
7827   // In a function call with two operands, the second can be broken with no line
7828   // break before it.
7829   EXPECT_EQ("func(a, \"long long \"\n"
7830             "        \"long long\");",
7831             format("func(a, \"long long long long\");",
7832                    getLLVMStyleWithColumns(24)));
7833   // In a function call with three operands, the second must be broken with a
7834   // line break before it.
7835   EXPECT_EQ("func(a,\n"
7836             "     \"long long long \"\n"
7837             "     \"long\",\n"
7838             "     c);",
7839             format("func(a, \"long long long long\", c);",
7840                    getLLVMStyleWithColumns(24)));
7841   // In a function call with three operands, the third must be broken with a
7842   // line break before it.
7843   EXPECT_EQ("func(a, b,\n"
7844             "     \"long long long \"\n"
7845             "     \"long\");",
7846             format("func(a, b, \"long long long long\");",
7847                    getLLVMStyleWithColumns(24)));
7848   // In a function call with three operands, both the second and the third must
7849   // be broken with a line break before them.
7850   EXPECT_EQ("func(a,\n"
7851             "     \"long long long \"\n"
7852             "     \"long\",\n"
7853             "     \"long long long \"\n"
7854             "     \"long\");",
7855             format("func(a, \"long long long long\", \"long long long long\");",
7856                    getLLVMStyleWithColumns(24)));
7857   // In a chain of << with two operands, the second can be broken with no line
7858   // break before it.
7859   EXPECT_EQ("a << \"line line \"\n"
7860             "     \"line\";",
7861             format("a << \"line line line\";",
7862                    getLLVMStyleWithColumns(20)));
7863   // In a chain of << with three operands, the second can be broken with no line
7864   // break before it.
7865   EXPECT_EQ("abcde << \"line \"\n"
7866             "         \"line line\"\n"
7867             "      << c;",
7868             format("abcde << \"line line line\" << c;",
7869                    getLLVMStyleWithColumns(20)));
7870   // In a chain of << with three operands, the third must be broken with a line
7871   // break before it.
7872   EXPECT_EQ("a << b\n"
7873             "  << \"line line \"\n"
7874             "     \"line\";",
7875             format("a << b << \"line line line\";",
7876                    getLLVMStyleWithColumns(20)));
7877   // In a chain of << with three operands, the second can be broken with no line
7878   // break before it and the third must be broken with a line break before it.
7879   EXPECT_EQ("abcd << \"line line \"\n"
7880             "        \"line\"\n"
7881             "     << \"line line \"\n"
7882             "        \"line\";",
7883             format("abcd << \"line line line\" << \"line line line\";",
7884                    getLLVMStyleWithColumns(20)));
7885   // In a chain of binary operators with two operands, the second can be broken
7886   // with no line break before it.
7887   EXPECT_EQ("abcd + \"line line \"\n"
7888             "       \"line line\";",
7889             format("abcd + \"line line line line\";",
7890                    getLLVMStyleWithColumns(20)));
7891   // In a chain of binary operators with three operands, the second must be
7892   // broken with a line break before it.
7893   EXPECT_EQ("abcd +\n"
7894             "    \"line line \"\n"
7895             "    \"line line\" +\n"
7896             "    e;",
7897             format("abcd + \"line line line line\" + e;",
7898                    getLLVMStyleWithColumns(20)));
7899   // In a function call with two operands, with AlignAfterOpenBracket enabled,
7900   // the first must be broken with a line break before it.
7901   FormatStyle Style = getLLVMStyleWithColumns(25);
7902   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7903   EXPECT_EQ("someFunction(\n"
7904             "    \"long long long \"\n"
7905             "    \"long\",\n"
7906             "    a);",
7907             format("someFunction(\"long long long long\", a);", Style));
7908 }
7909 
7910 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
7911   EXPECT_EQ(
7912       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7913       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7914       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7915       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7916              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7917              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
7918 }
7919 
7920 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
7921   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
7922             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
7923   EXPECT_EQ("fffffffffff(g(R\"x(\n"
7924             "multiline raw string literal xxxxxxxxxxxxxx\n"
7925             ")x\",\n"
7926             "              a),\n"
7927             "            b);",
7928             format("fffffffffff(g(R\"x(\n"
7929                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7930                    ")x\", a), b);",
7931                    getGoogleStyleWithColumns(20)));
7932   EXPECT_EQ("fffffffffff(\n"
7933             "    g(R\"x(qqq\n"
7934             "multiline raw string literal xxxxxxxxxxxxxx\n"
7935             ")x\",\n"
7936             "      a),\n"
7937             "    b);",
7938             format("fffffffffff(g(R\"x(qqq\n"
7939                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7940                    ")x\", a), b);",
7941                    getGoogleStyleWithColumns(20)));
7942 
7943   EXPECT_EQ("fffffffffff(R\"x(\n"
7944             "multiline raw string literal xxxxxxxxxxxxxx\n"
7945             ")x\");",
7946             format("fffffffffff(R\"x(\n"
7947                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7948                    ")x\");",
7949                    getGoogleStyleWithColumns(20)));
7950   EXPECT_EQ("fffffffffff(R\"x(\n"
7951             "multiline raw string literal xxxxxxxxxxxxxx\n"
7952             ")x\" + bbbbbb);",
7953             format("fffffffffff(R\"x(\n"
7954                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7955                    ")x\" +   bbbbbb);",
7956                    getGoogleStyleWithColumns(20)));
7957   EXPECT_EQ("fffffffffff(\n"
7958             "    R\"x(\n"
7959             "multiline raw string literal xxxxxxxxxxxxxx\n"
7960             ")x\" +\n"
7961             "    bbbbbb);",
7962             format("fffffffffff(\n"
7963                    " R\"x(\n"
7964                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7965                    ")x\" + bbbbbb);",
7966                    getGoogleStyleWithColumns(20)));
7967 }
7968 
7969 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
7970   verifyFormat("string a = \"unterminated;");
7971   EXPECT_EQ("function(\"unterminated,\n"
7972             "         OtherParameter);",
7973             format("function(  \"unterminated,\n"
7974                    "    OtherParameter);"));
7975 }
7976 
7977 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
7978   FormatStyle Style = getLLVMStyle();
7979   Style.Standard = FormatStyle::LS_Cpp03;
7980   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
7981             format("#define x(_a) printf(\"foo\"_a);", Style));
7982 }
7983 
7984 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
7985 
7986 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
7987   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
7988             "             \"ddeeefff\");",
7989             format("someFunction(\"aaabbbcccdddeeefff\");",
7990                    getLLVMStyleWithColumns(25)));
7991   EXPECT_EQ("someFunction1234567890(\n"
7992             "    \"aaabbbcccdddeeefff\");",
7993             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7994                    getLLVMStyleWithColumns(26)));
7995   EXPECT_EQ("someFunction1234567890(\n"
7996             "    \"aaabbbcccdddeeeff\"\n"
7997             "    \"f\");",
7998             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7999                    getLLVMStyleWithColumns(25)));
8000   EXPECT_EQ("someFunction1234567890(\n"
8001             "    \"aaabbbcccdddeeeff\"\n"
8002             "    \"f\");",
8003             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8004                    getLLVMStyleWithColumns(24)));
8005   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
8006             "             \"ddde \"\n"
8007             "             \"efff\");",
8008             format("someFunction(\"aaabbbcc ddde efff\");",
8009                    getLLVMStyleWithColumns(25)));
8010   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
8011             "             \"ddeeefff\");",
8012             format("someFunction(\"aaabbbccc ddeeefff\");",
8013                    getLLVMStyleWithColumns(25)));
8014   EXPECT_EQ("someFunction1234567890(\n"
8015             "    \"aaabb \"\n"
8016             "    \"cccdddeeefff\");",
8017             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
8018                    getLLVMStyleWithColumns(25)));
8019   EXPECT_EQ("#define A          \\\n"
8020             "  string s =       \\\n"
8021             "      \"123456789\"  \\\n"
8022             "      \"0\";         \\\n"
8023             "  int i;",
8024             format("#define A string s = \"1234567890\"; int i;",
8025                    getLLVMStyleWithColumns(20)));
8026   // FIXME: Put additional penalties on breaking at non-whitespace locations.
8027   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
8028             "             \"dddeeeff\"\n"
8029             "             \"f\");",
8030             format("someFunction(\"aaabbbcc dddeeefff\");",
8031                    getLLVMStyleWithColumns(25)));
8032 }
8033 
8034 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
8035   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
8036   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
8037   EXPECT_EQ("\"test\"\n"
8038             "\"\\n\"",
8039             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
8040   EXPECT_EQ("\"tes\\\\\"\n"
8041             "\"n\"",
8042             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
8043   EXPECT_EQ("\"\\\\\\\\\"\n"
8044             "\"\\n\"",
8045             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
8046   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
8047   EXPECT_EQ("\"\\uff01\"\n"
8048             "\"test\"",
8049             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
8050   EXPECT_EQ("\"\\Uff01ff02\"",
8051             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
8052   EXPECT_EQ("\"\\x000000000001\"\n"
8053             "\"next\"",
8054             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
8055   EXPECT_EQ("\"\\x000000000001next\"",
8056             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
8057   EXPECT_EQ("\"\\x000000000001\"",
8058             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
8059   EXPECT_EQ("\"test\"\n"
8060             "\"\\000000\"\n"
8061             "\"000001\"",
8062             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
8063   EXPECT_EQ("\"test\\000\"\n"
8064             "\"00000000\"\n"
8065             "\"1\"",
8066             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
8067 }
8068 
8069 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
8070   verifyFormat("void f() {\n"
8071                "  return g() {}\n"
8072                "  void h() {}");
8073   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
8074                "g();\n"
8075                "}");
8076 }
8077 
8078 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
8079   verifyFormat(
8080       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
8081 }
8082 
8083 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
8084   verifyFormat("class X {\n"
8085                "  void f() {\n"
8086                "  }\n"
8087                "};",
8088                getLLVMStyleWithColumns(12));
8089 }
8090 
8091 TEST_F(FormatTest, ConfigurableIndentWidth) {
8092   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
8093   EightIndent.IndentWidth = 8;
8094   EightIndent.ContinuationIndentWidth = 8;
8095   verifyFormat("void f() {\n"
8096                "        someFunction();\n"
8097                "        if (true) {\n"
8098                "                f();\n"
8099                "        }\n"
8100                "}",
8101                EightIndent);
8102   verifyFormat("class X {\n"
8103                "        void f() {\n"
8104                "        }\n"
8105                "};",
8106                EightIndent);
8107   verifyFormat("int x[] = {\n"
8108                "        call(),\n"
8109                "        call()};",
8110                EightIndent);
8111 }
8112 
8113 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
8114   verifyFormat("double\n"
8115                "f();",
8116                getLLVMStyleWithColumns(8));
8117 }
8118 
8119 TEST_F(FormatTest, ConfigurableUseOfTab) {
8120   FormatStyle Tab = getLLVMStyleWithColumns(42);
8121   Tab.IndentWidth = 8;
8122   Tab.UseTab = FormatStyle::UT_Always;
8123   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
8124 
8125   EXPECT_EQ("if (aaaaaaaa && // q\n"
8126             "    bb)\t\t// w\n"
8127             "\t;",
8128             format("if (aaaaaaaa &&// q\n"
8129                    "bb)// w\n"
8130                    ";",
8131                    Tab));
8132   EXPECT_EQ("if (aaa && bbb) // w\n"
8133             "\t;",
8134             format("if(aaa&&bbb)// w\n"
8135                    ";",
8136                    Tab));
8137 
8138   verifyFormat("class X {\n"
8139                "\tvoid f() {\n"
8140                "\t\tsomeFunction(parameter1,\n"
8141                "\t\t\t     parameter2);\n"
8142                "\t}\n"
8143                "};",
8144                Tab);
8145   verifyFormat("#define A                        \\\n"
8146                "\tvoid f() {               \\\n"
8147                "\t\tsomeFunction(    \\\n"
8148                "\t\t    parameter1,  \\\n"
8149                "\t\t    parameter2); \\\n"
8150                "\t}",
8151                Tab);
8152 
8153   Tab.TabWidth = 4;
8154   Tab.IndentWidth = 8;
8155   verifyFormat("class TabWidth4Indent8 {\n"
8156                "\t\tvoid f() {\n"
8157                "\t\t\t\tsomeFunction(parameter1,\n"
8158                "\t\t\t\t\t\t\t parameter2);\n"
8159                "\t\t}\n"
8160                "};",
8161                Tab);
8162 
8163   Tab.TabWidth = 4;
8164   Tab.IndentWidth = 4;
8165   verifyFormat("class TabWidth4Indent4 {\n"
8166                "\tvoid f() {\n"
8167                "\t\tsomeFunction(parameter1,\n"
8168                "\t\t\t\t\t parameter2);\n"
8169                "\t}\n"
8170                "};",
8171                Tab);
8172 
8173   Tab.TabWidth = 8;
8174   Tab.IndentWidth = 4;
8175   verifyFormat("class TabWidth8Indent4 {\n"
8176                "    void f() {\n"
8177                "\tsomeFunction(parameter1,\n"
8178                "\t\t     parameter2);\n"
8179                "    }\n"
8180                "};",
8181                Tab);
8182 
8183   Tab.TabWidth = 8;
8184   Tab.IndentWidth = 8;
8185   EXPECT_EQ("/*\n"
8186             "\t      a\t\tcomment\n"
8187             "\t      in multiple lines\n"
8188             "       */",
8189             format("   /*\t \t \n"
8190                    " \t \t a\t\tcomment\t \t\n"
8191                    " \t \t in multiple lines\t\n"
8192                    " \t  */",
8193                    Tab));
8194 
8195   Tab.UseTab = FormatStyle::UT_ForIndentation;
8196   verifyFormat("{\n"
8197                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8198                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8199                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8200                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8201                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8202                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8203                "};",
8204                Tab);
8205   verifyFormat("enum AA {\n"
8206                "\ta1, // Force multiple lines\n"
8207                "\ta2,\n"
8208                "\ta3\n"
8209                "};",
8210                Tab);
8211   EXPECT_EQ("if (aaaaaaaa && // q\n"
8212             "    bb)         // w\n"
8213             "\t;",
8214             format("if (aaaaaaaa &&// q\n"
8215                    "bb)// w\n"
8216                    ";",
8217                    Tab));
8218   verifyFormat("class X {\n"
8219                "\tvoid f() {\n"
8220                "\t\tsomeFunction(parameter1,\n"
8221                "\t\t             parameter2);\n"
8222                "\t}\n"
8223                "};",
8224                Tab);
8225   verifyFormat("{\n"
8226                "\tQ(\n"
8227                "\t    {\n"
8228                "\t\t    int a;\n"
8229                "\t\t    someFunction(aaaaaaaa,\n"
8230                "\t\t                 bbbbbbb);\n"
8231                "\t    },\n"
8232                "\t    p);\n"
8233                "}",
8234                Tab);
8235   EXPECT_EQ("{\n"
8236             "\t/* aaaa\n"
8237             "\t   bbbb */\n"
8238             "}",
8239             format("{\n"
8240                    "/* aaaa\n"
8241                    "   bbbb */\n"
8242                    "}",
8243                    Tab));
8244   EXPECT_EQ("{\n"
8245             "\t/*\n"
8246             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8247             "\t  bbbbbbbbbbbbb\n"
8248             "\t*/\n"
8249             "}",
8250             format("{\n"
8251                    "/*\n"
8252                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8253                    "*/\n"
8254                    "}",
8255                    Tab));
8256   EXPECT_EQ("{\n"
8257             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8258             "\t// bbbbbbbbbbbbb\n"
8259             "}",
8260             format("{\n"
8261                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8262                    "}",
8263                    Tab));
8264   EXPECT_EQ("{\n"
8265             "\t/*\n"
8266             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8267             "\t  bbbbbbbbbbbbb\n"
8268             "\t*/\n"
8269             "}",
8270             format("{\n"
8271                    "\t/*\n"
8272                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8273                    "\t*/\n"
8274                    "}",
8275                    Tab));
8276   EXPECT_EQ("{\n"
8277             "\t/*\n"
8278             "\n"
8279             "\t*/\n"
8280             "}",
8281             format("{\n"
8282                    "\t/*\n"
8283                    "\n"
8284                    "\t*/\n"
8285                    "}",
8286                    Tab));
8287   EXPECT_EQ("{\n"
8288             "\t/*\n"
8289             " asdf\n"
8290             "\t*/\n"
8291             "}",
8292             format("{\n"
8293                    "\t/*\n"
8294                    " asdf\n"
8295                    "\t*/\n"
8296                    "}",
8297                    Tab));
8298 
8299   Tab.UseTab = FormatStyle::UT_Never;
8300   EXPECT_EQ("/*\n"
8301             "              a\t\tcomment\n"
8302             "              in multiple lines\n"
8303             "       */",
8304             format("   /*\t \t \n"
8305                    " \t \t a\t\tcomment\t \t\n"
8306                    " \t \t in multiple lines\t\n"
8307                    " \t  */",
8308                    Tab));
8309   EXPECT_EQ("/* some\n"
8310             "   comment */",
8311             format(" \t \t /* some\n"
8312                    " \t \t    comment */",
8313                    Tab));
8314   EXPECT_EQ("int a; /* some\n"
8315             "   comment */",
8316             format(" \t \t int a; /* some\n"
8317                    " \t \t    comment */",
8318                    Tab));
8319 
8320   EXPECT_EQ("int a; /* some\n"
8321             "comment */",
8322             format(" \t \t int\ta; /* some\n"
8323                    " \t \t    comment */",
8324                    Tab));
8325   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8326             "    comment */",
8327             format(" \t \t f(\"\t\t\"); /* some\n"
8328                    " \t \t    comment */",
8329                    Tab));
8330   EXPECT_EQ("{\n"
8331             "  /*\n"
8332             "   * Comment\n"
8333             "   */\n"
8334             "  int i;\n"
8335             "}",
8336             format("{\n"
8337                    "\t/*\n"
8338                    "\t * Comment\n"
8339                    "\t */\n"
8340                    "\t int i;\n"
8341                    "}"));
8342 
8343   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
8344   Tab.TabWidth = 8;
8345   Tab.IndentWidth = 8;
8346   EXPECT_EQ("if (aaaaaaaa && // q\n"
8347             "    bb)         // w\n"
8348             "\t;",
8349             format("if (aaaaaaaa &&// q\n"
8350                    "bb)// w\n"
8351                    ";",
8352                    Tab));
8353   EXPECT_EQ("if (aaa && bbb) // w\n"
8354             "\t;",
8355             format("if(aaa&&bbb)// w\n"
8356                    ";",
8357                    Tab));
8358   verifyFormat("class X {\n"
8359                "\tvoid f() {\n"
8360                "\t\tsomeFunction(parameter1,\n"
8361                "\t\t\t     parameter2);\n"
8362                "\t}\n"
8363                "};",
8364                Tab);
8365   verifyFormat("#define A                        \\\n"
8366                "\tvoid f() {               \\\n"
8367                "\t\tsomeFunction(    \\\n"
8368                "\t\t    parameter1,  \\\n"
8369                "\t\t    parameter2); \\\n"
8370                "\t}",
8371                Tab);
8372   Tab.TabWidth = 4;
8373   Tab.IndentWidth = 8;
8374   verifyFormat("class TabWidth4Indent8 {\n"
8375                "\t\tvoid f() {\n"
8376                "\t\t\t\tsomeFunction(parameter1,\n"
8377                "\t\t\t\t\t\t\t parameter2);\n"
8378                "\t\t}\n"
8379                "};",
8380                Tab);
8381   Tab.TabWidth = 4;
8382   Tab.IndentWidth = 4;
8383   verifyFormat("class TabWidth4Indent4 {\n"
8384                "\tvoid f() {\n"
8385                "\t\tsomeFunction(parameter1,\n"
8386                "\t\t\t\t\t parameter2);\n"
8387                "\t}\n"
8388                "};",
8389                Tab);
8390   Tab.TabWidth = 8;
8391   Tab.IndentWidth = 4;
8392   verifyFormat("class TabWidth8Indent4 {\n"
8393                "    void f() {\n"
8394                "\tsomeFunction(parameter1,\n"
8395                "\t\t     parameter2);\n"
8396                "    }\n"
8397                "};",
8398                Tab);
8399   Tab.TabWidth = 8;
8400   Tab.IndentWidth = 8;
8401   EXPECT_EQ("/*\n"
8402             "\t      a\t\tcomment\n"
8403             "\t      in multiple lines\n"
8404             "       */",
8405             format("   /*\t \t \n"
8406                    " \t \t a\t\tcomment\t \t\n"
8407                    " \t \t in multiple lines\t\n"
8408                    " \t  */",
8409                    Tab));
8410   verifyFormat("{\n"
8411                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8412                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8413                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8414                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8415                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8416                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8417                "};",
8418                Tab);
8419   verifyFormat("enum AA {\n"
8420                "\ta1, // Force multiple lines\n"
8421                "\ta2,\n"
8422                "\ta3\n"
8423                "};",
8424                Tab);
8425   EXPECT_EQ("if (aaaaaaaa && // q\n"
8426             "    bb)         // w\n"
8427             "\t;",
8428             format("if (aaaaaaaa &&// q\n"
8429                    "bb)// w\n"
8430                    ";",
8431                    Tab));
8432   verifyFormat("class X {\n"
8433                "\tvoid f() {\n"
8434                "\t\tsomeFunction(parameter1,\n"
8435                "\t\t\t     parameter2);\n"
8436                "\t}\n"
8437                "};",
8438                Tab);
8439   verifyFormat("{\n"
8440                "\tQ(\n"
8441                "\t    {\n"
8442                "\t\t    int a;\n"
8443                "\t\t    someFunction(aaaaaaaa,\n"
8444                "\t\t\t\t bbbbbbb);\n"
8445                "\t    },\n"
8446                "\t    p);\n"
8447                "}",
8448                Tab);
8449   EXPECT_EQ("{\n"
8450             "\t/* aaaa\n"
8451             "\t   bbbb */\n"
8452             "}",
8453             format("{\n"
8454                    "/* aaaa\n"
8455                    "   bbbb */\n"
8456                    "}",
8457                    Tab));
8458   EXPECT_EQ("{\n"
8459             "\t/*\n"
8460             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8461             "\t  bbbbbbbbbbbbb\n"
8462             "\t*/\n"
8463             "}",
8464             format("{\n"
8465                    "/*\n"
8466                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8467                    "*/\n"
8468                    "}",
8469                    Tab));
8470   EXPECT_EQ("{\n"
8471             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8472             "\t// bbbbbbbbbbbbb\n"
8473             "}",
8474             format("{\n"
8475                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8476                    "}",
8477                    Tab));
8478   EXPECT_EQ("{\n"
8479             "\t/*\n"
8480             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8481             "\t  bbbbbbbbbbbbb\n"
8482             "\t*/\n"
8483             "}",
8484             format("{\n"
8485                    "\t/*\n"
8486                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8487                    "\t*/\n"
8488                    "}",
8489                    Tab));
8490   EXPECT_EQ("{\n"
8491             "\t/*\n"
8492             "\n"
8493             "\t*/\n"
8494             "}",
8495             format("{\n"
8496                    "\t/*\n"
8497                    "\n"
8498                    "\t*/\n"
8499                    "}",
8500                    Tab));
8501   EXPECT_EQ("{\n"
8502             "\t/*\n"
8503             " asdf\n"
8504             "\t*/\n"
8505             "}",
8506             format("{\n"
8507                    "\t/*\n"
8508                    " asdf\n"
8509                    "\t*/\n"
8510                    "}",
8511                    Tab));
8512   EXPECT_EQ("/*\n"
8513             "\t      a\t\tcomment\n"
8514             "\t      in multiple lines\n"
8515             "       */",
8516             format("   /*\t \t \n"
8517                    " \t \t a\t\tcomment\t \t\n"
8518                    " \t \t in multiple lines\t\n"
8519                    " \t  */",
8520                    Tab));
8521   EXPECT_EQ("/* some\n"
8522             "   comment */",
8523             format(" \t \t /* some\n"
8524                    " \t \t    comment */",
8525                    Tab));
8526   EXPECT_EQ("int a; /* some\n"
8527             "   comment */",
8528             format(" \t \t int a; /* some\n"
8529                    " \t \t    comment */",
8530                    Tab));
8531   EXPECT_EQ("int a; /* some\n"
8532             "comment */",
8533             format(" \t \t int\ta; /* some\n"
8534                    " \t \t    comment */",
8535                    Tab));
8536   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8537             "    comment */",
8538             format(" \t \t f(\"\t\t\"); /* some\n"
8539                    " \t \t    comment */",
8540                    Tab));
8541   EXPECT_EQ("{\n"
8542             "  /*\n"
8543             "   * Comment\n"
8544             "   */\n"
8545             "  int i;\n"
8546             "}",
8547             format("{\n"
8548                    "\t/*\n"
8549                    "\t * Comment\n"
8550                    "\t */\n"
8551                    "\t int i;\n"
8552                    "}"));
8553   Tab.AlignConsecutiveAssignments = true;
8554   Tab.AlignConsecutiveDeclarations = true;
8555   Tab.TabWidth = 4;
8556   Tab.IndentWidth = 4;
8557   verifyFormat("class Assign {\n"
8558                "\tvoid f() {\n"
8559                "\t\tint         x      = 123;\n"
8560                "\t\tint         random = 4;\n"
8561                "\t\tstd::string alphabet =\n"
8562                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
8563                "\t}\n"
8564                "};",
8565                Tab);
8566 }
8567 
8568 TEST_F(FormatTest, CalculatesOriginalColumn) {
8569   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8570             "q\"; /* some\n"
8571             "       comment */",
8572             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8573                    "q\"; /* some\n"
8574                    "       comment */",
8575                    getLLVMStyle()));
8576   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8577             "/* some\n"
8578             "   comment */",
8579             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8580                    " /* some\n"
8581                    "    comment */",
8582                    getLLVMStyle()));
8583   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8584             "qqq\n"
8585             "/* some\n"
8586             "   comment */",
8587             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8588                    "qqq\n"
8589                    " /* some\n"
8590                    "    comment */",
8591                    getLLVMStyle()));
8592   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8593             "wwww; /* some\n"
8594             "         comment */",
8595             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8596                    "wwww; /* some\n"
8597                    "         comment */",
8598                    getLLVMStyle()));
8599 }
8600 
8601 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
8602   FormatStyle NoSpace = getLLVMStyle();
8603   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
8604 
8605   verifyFormat("while(true)\n"
8606                "  continue;",
8607                NoSpace);
8608   verifyFormat("for(;;)\n"
8609                "  continue;",
8610                NoSpace);
8611   verifyFormat("if(true)\n"
8612                "  f();\n"
8613                "else if(true)\n"
8614                "  f();",
8615                NoSpace);
8616   verifyFormat("do {\n"
8617                "  do_something();\n"
8618                "} while(something());",
8619                NoSpace);
8620   verifyFormat("switch(x) {\n"
8621                "default:\n"
8622                "  break;\n"
8623                "}",
8624                NoSpace);
8625   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
8626   verifyFormat("size_t x = sizeof(x);", NoSpace);
8627   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
8628   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
8629   verifyFormat("alignas(128) char a[128];", NoSpace);
8630   verifyFormat("size_t x = alignof(MyType);", NoSpace);
8631   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
8632   verifyFormat("int f() throw(Deprecated);", NoSpace);
8633   verifyFormat("typedef void (*cb)(int);", NoSpace);
8634   verifyFormat("T A::operator()();", NoSpace);
8635   verifyFormat("X A::operator++(T);", NoSpace);
8636 
8637   FormatStyle Space = getLLVMStyle();
8638   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
8639 
8640   verifyFormat("int f ();", Space);
8641   verifyFormat("void f (int a, T b) {\n"
8642                "  while (true)\n"
8643                "    continue;\n"
8644                "}",
8645                Space);
8646   verifyFormat("if (true)\n"
8647                "  f ();\n"
8648                "else if (true)\n"
8649                "  f ();",
8650                Space);
8651   verifyFormat("do {\n"
8652                "  do_something ();\n"
8653                "} while (something ());",
8654                Space);
8655   verifyFormat("switch (x) {\n"
8656                "default:\n"
8657                "  break;\n"
8658                "}",
8659                Space);
8660   verifyFormat("A::A () : a (1) {}", Space);
8661   verifyFormat("void f () __attribute__ ((asdf));", Space);
8662   verifyFormat("*(&a + 1);\n"
8663                "&((&a)[1]);\n"
8664                "a[(b + c) * d];\n"
8665                "(((a + 1) * 2) + 3) * 4;",
8666                Space);
8667   verifyFormat("#define A(x) x", Space);
8668   verifyFormat("#define A (x) x", Space);
8669   verifyFormat("#if defined(x)\n"
8670                "#endif",
8671                Space);
8672   verifyFormat("auto i = std::make_unique<int> (5);", Space);
8673   verifyFormat("size_t x = sizeof (x);", Space);
8674   verifyFormat("auto f (int x) -> decltype (x);", Space);
8675   verifyFormat("int f (T x) noexcept (x.create ());", Space);
8676   verifyFormat("alignas (128) char a[128];", Space);
8677   verifyFormat("size_t x = alignof (MyType);", Space);
8678   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
8679   verifyFormat("int f () throw (Deprecated);", Space);
8680   verifyFormat("typedef void (*cb) (int);", Space);
8681   verifyFormat("T A::operator() ();", Space);
8682   verifyFormat("X A::operator++ (T);", Space);
8683 }
8684 
8685 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
8686   FormatStyle Spaces = getLLVMStyle();
8687 
8688   Spaces.SpacesInParentheses = true;
8689   verifyFormat("call( x, y, z );", Spaces);
8690   verifyFormat("call();", Spaces);
8691   verifyFormat("std::function<void( int, int )> callback;", Spaces);
8692   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
8693                Spaces);
8694   verifyFormat("while ( (bool)1 )\n"
8695                "  continue;",
8696                Spaces);
8697   verifyFormat("for ( ;; )\n"
8698                "  continue;",
8699                Spaces);
8700   verifyFormat("if ( true )\n"
8701                "  f();\n"
8702                "else if ( true )\n"
8703                "  f();",
8704                Spaces);
8705   verifyFormat("do {\n"
8706                "  do_something( (int)i );\n"
8707                "} while ( something() );",
8708                Spaces);
8709   verifyFormat("switch ( x ) {\n"
8710                "default:\n"
8711                "  break;\n"
8712                "}",
8713                Spaces);
8714 
8715   Spaces.SpacesInParentheses = false;
8716   Spaces.SpacesInCStyleCastParentheses = true;
8717   verifyFormat("Type *A = ( Type * )P;", Spaces);
8718   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
8719   verifyFormat("x = ( int32 )y;", Spaces);
8720   verifyFormat("int a = ( int )(2.0f);", Spaces);
8721   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
8722   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
8723   verifyFormat("#define x (( int )-1)", Spaces);
8724 
8725   // Run the first set of tests again with:
8726   Spaces.SpacesInParentheses = false;
8727   Spaces.SpaceInEmptyParentheses = true;
8728   Spaces.SpacesInCStyleCastParentheses = true;
8729   verifyFormat("call(x, y, z);", Spaces);
8730   verifyFormat("call( );", Spaces);
8731   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8732   verifyFormat("while (( bool )1)\n"
8733                "  continue;",
8734                Spaces);
8735   verifyFormat("for (;;)\n"
8736                "  continue;",
8737                Spaces);
8738   verifyFormat("if (true)\n"
8739                "  f( );\n"
8740                "else if (true)\n"
8741                "  f( );",
8742                Spaces);
8743   verifyFormat("do {\n"
8744                "  do_something(( int )i);\n"
8745                "} while (something( ));",
8746                Spaces);
8747   verifyFormat("switch (x) {\n"
8748                "default:\n"
8749                "  break;\n"
8750                "}",
8751                Spaces);
8752 
8753   // Run the first set of tests again with:
8754   Spaces.SpaceAfterCStyleCast = true;
8755   verifyFormat("call(x, y, z);", Spaces);
8756   verifyFormat("call( );", Spaces);
8757   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8758   verifyFormat("while (( bool ) 1)\n"
8759                "  continue;",
8760                Spaces);
8761   verifyFormat("for (;;)\n"
8762                "  continue;",
8763                Spaces);
8764   verifyFormat("if (true)\n"
8765                "  f( );\n"
8766                "else if (true)\n"
8767                "  f( );",
8768                Spaces);
8769   verifyFormat("do {\n"
8770                "  do_something(( int ) i);\n"
8771                "} while (something( ));",
8772                Spaces);
8773   verifyFormat("switch (x) {\n"
8774                "default:\n"
8775                "  break;\n"
8776                "}",
8777                Spaces);
8778 
8779   // Run subset of tests again with:
8780   Spaces.SpacesInCStyleCastParentheses = false;
8781   Spaces.SpaceAfterCStyleCast = true;
8782   verifyFormat("while ((bool) 1)\n"
8783                "  continue;",
8784                Spaces);
8785   verifyFormat("do {\n"
8786                "  do_something((int) i);\n"
8787                "} while (something( ));",
8788                Spaces);
8789 }
8790 
8791 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
8792   verifyFormat("int a[5];");
8793   verifyFormat("a[3] += 42;");
8794 
8795   FormatStyle Spaces = getLLVMStyle();
8796   Spaces.SpacesInSquareBrackets = true;
8797   // Lambdas unchanged.
8798   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
8799   verifyFormat("return [i, args...] {};", Spaces);
8800 
8801   // Not lambdas.
8802   verifyFormat("int a[ 5 ];", Spaces);
8803   verifyFormat("a[ 3 ] += 42;", Spaces);
8804   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
8805   verifyFormat("double &operator[](int i) { return 0; }\n"
8806                "int i;",
8807                Spaces);
8808   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
8809   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
8810   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
8811 }
8812 
8813 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
8814   verifyFormat("int a = 5;");
8815   verifyFormat("a += 42;");
8816   verifyFormat("a or_eq 8;");
8817 
8818   FormatStyle Spaces = getLLVMStyle();
8819   Spaces.SpaceBeforeAssignmentOperators = false;
8820   verifyFormat("int a= 5;", Spaces);
8821   verifyFormat("a+= 42;", Spaces);
8822   verifyFormat("a or_eq 8;", Spaces);
8823 }
8824 
8825 TEST_F(FormatTest, AlignConsecutiveAssignments) {
8826   FormatStyle Alignment = getLLVMStyle();
8827   Alignment.AlignConsecutiveAssignments = false;
8828   verifyFormat("int a = 5;\n"
8829                "int oneTwoThree = 123;",
8830                Alignment);
8831   verifyFormat("int a = 5;\n"
8832                "int oneTwoThree = 123;",
8833                Alignment);
8834 
8835   Alignment.AlignConsecutiveAssignments = true;
8836   verifyFormat("int a           = 5;\n"
8837                "int oneTwoThree = 123;",
8838                Alignment);
8839   verifyFormat("int a           = method();\n"
8840                "int oneTwoThree = 133;",
8841                Alignment);
8842   verifyFormat("a &= 5;\n"
8843                "bcd *= 5;\n"
8844                "ghtyf += 5;\n"
8845                "dvfvdb -= 5;\n"
8846                "a /= 5;\n"
8847                "vdsvsv %= 5;\n"
8848                "sfdbddfbdfbb ^= 5;\n"
8849                "dvsdsv |= 5;\n"
8850                "int dsvvdvsdvvv = 123;",
8851                Alignment);
8852   verifyFormat("int i = 1, j = 10;\n"
8853                "something = 2000;",
8854                Alignment);
8855   verifyFormat("something = 2000;\n"
8856                "int i = 1, j = 10;\n",
8857                Alignment);
8858   verifyFormat("something = 2000;\n"
8859                "another   = 911;\n"
8860                "int i = 1, j = 10;\n"
8861                "oneMore = 1;\n"
8862                "i       = 2;",
8863                Alignment);
8864   verifyFormat("int a   = 5;\n"
8865                "int one = 1;\n"
8866                "method();\n"
8867                "int oneTwoThree = 123;\n"
8868                "int oneTwo      = 12;",
8869                Alignment);
8870   verifyFormat("int oneTwoThree = 123;\n"
8871                "int oneTwo      = 12;\n"
8872                "method();\n",
8873                Alignment);
8874   verifyFormat("int oneTwoThree = 123; // comment\n"
8875                "int oneTwo      = 12;  // comment",
8876                Alignment);
8877   EXPECT_EQ("int a = 5;\n"
8878             "\n"
8879             "int oneTwoThree = 123;",
8880             format("int a       = 5;\n"
8881                    "\n"
8882                    "int oneTwoThree= 123;",
8883                    Alignment));
8884   EXPECT_EQ("int a   = 5;\n"
8885             "int one = 1;\n"
8886             "\n"
8887             "int oneTwoThree = 123;",
8888             format("int a = 5;\n"
8889                    "int one = 1;\n"
8890                    "\n"
8891                    "int oneTwoThree = 123;",
8892                    Alignment));
8893   EXPECT_EQ("int a   = 5;\n"
8894             "int one = 1;\n"
8895             "\n"
8896             "int oneTwoThree = 123;\n"
8897             "int oneTwo      = 12;",
8898             format("int a = 5;\n"
8899                    "int one = 1;\n"
8900                    "\n"
8901                    "int oneTwoThree = 123;\n"
8902                    "int oneTwo = 12;",
8903                    Alignment));
8904   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
8905   verifyFormat("#define A \\\n"
8906                "  int aaaa       = 12; \\\n"
8907                "  int b          = 23; \\\n"
8908                "  int ccc        = 234; \\\n"
8909                "  int dddddddddd = 2345;",
8910                Alignment);
8911   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
8912   verifyFormat("#define A               \\\n"
8913                "  int aaaa       = 12;  \\\n"
8914                "  int b          = 23;  \\\n"
8915                "  int ccc        = 234; \\\n"
8916                "  int dddddddddd = 2345;",
8917                Alignment);
8918   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
8919   verifyFormat("#define A                                                      "
8920                "                \\\n"
8921                "  int aaaa       = 12;                                         "
8922                "                \\\n"
8923                "  int b          = 23;                                         "
8924                "                \\\n"
8925                "  int ccc        = 234;                                        "
8926                "                \\\n"
8927                "  int dddddddddd = 2345;",
8928                Alignment);
8929   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
8930                "k = 4, int l = 5,\n"
8931                "                  int m = 6) {\n"
8932                "  int j      = 10;\n"
8933                "  otherThing = 1;\n"
8934                "}",
8935                Alignment);
8936   verifyFormat("void SomeFunction(int parameter = 0) {\n"
8937                "  int i   = 1;\n"
8938                "  int j   = 2;\n"
8939                "  int big = 10000;\n"
8940                "}",
8941                Alignment);
8942   verifyFormat("class C {\n"
8943                "public:\n"
8944                "  int i            = 1;\n"
8945                "  virtual void f() = 0;\n"
8946                "};",
8947                Alignment);
8948   verifyFormat("int i = 1;\n"
8949                "if (SomeType t = getSomething()) {\n"
8950                "}\n"
8951                "int j   = 2;\n"
8952                "int big = 10000;",
8953                Alignment);
8954   verifyFormat("int j = 7;\n"
8955                "for (int k = 0; k < N; ++k) {\n"
8956                "}\n"
8957                "int j   = 2;\n"
8958                "int big = 10000;\n"
8959                "}",
8960                Alignment);
8961   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8962   verifyFormat("int i = 1;\n"
8963                "LooooooooooongType loooooooooooooooooooooongVariable\n"
8964                "    = someLooooooooooooooooongFunction();\n"
8965                "int j = 2;",
8966                Alignment);
8967   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8968   verifyFormat("int i = 1;\n"
8969                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
8970                "    someLooooooooooooooooongFunction();\n"
8971                "int j = 2;",
8972                Alignment);
8973 
8974   verifyFormat("auto lambda = []() {\n"
8975                "  auto i = 0;\n"
8976                "  return 0;\n"
8977                "};\n"
8978                "int i  = 0;\n"
8979                "auto v = type{\n"
8980                "    i = 1,   //\n"
8981                "    (i = 2), //\n"
8982                "    i = 3    //\n"
8983                "};",
8984                Alignment);
8985 
8986   verifyFormat(
8987       "int i      = 1;\n"
8988       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
8989       "                          loooooooooooooooooooooongParameterB);\n"
8990       "int j      = 2;",
8991       Alignment);
8992 
8993   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
8994                "          typename B   = very_long_type_name_1,\n"
8995                "          typename T_2 = very_long_type_name_2>\n"
8996                "auto foo() {}\n",
8997                Alignment);
8998   verifyFormat("int a, b = 1;\n"
8999                "int c  = 2;\n"
9000                "int dd = 3;\n",
9001                Alignment);
9002   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
9003                "float b[1][] = {{3.f}};\n",
9004                Alignment);
9005   verifyFormat("for (int i = 0; i < 1; i++)\n"
9006                "  int x = 1;\n",
9007                Alignment);
9008   verifyFormat("for (i = 0; i < 1; i++)\n"
9009                "  x = 1;\n"
9010                "y = 1;\n",
9011                Alignment);
9012 }
9013 
9014 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
9015   FormatStyle Alignment = getLLVMStyle();
9016   Alignment.AlignConsecutiveDeclarations = false;
9017   verifyFormat("float const a = 5;\n"
9018                "int oneTwoThree = 123;",
9019                Alignment);
9020   verifyFormat("int a = 5;\n"
9021                "float const oneTwoThree = 123;",
9022                Alignment);
9023 
9024   Alignment.AlignConsecutiveDeclarations = true;
9025   verifyFormat("float const a = 5;\n"
9026                "int         oneTwoThree = 123;",
9027                Alignment);
9028   verifyFormat("int         a = method();\n"
9029                "float const oneTwoThree = 133;",
9030                Alignment);
9031   verifyFormat("int i = 1, j = 10;\n"
9032                "something = 2000;",
9033                Alignment);
9034   verifyFormat("something = 2000;\n"
9035                "int i = 1, j = 10;\n",
9036                Alignment);
9037   verifyFormat("float      something = 2000;\n"
9038                "double     another = 911;\n"
9039                "int        i = 1, j = 10;\n"
9040                "const int *oneMore = 1;\n"
9041                "unsigned   i = 2;",
9042                Alignment);
9043   verifyFormat("float a = 5;\n"
9044                "int   one = 1;\n"
9045                "method();\n"
9046                "const double       oneTwoThree = 123;\n"
9047                "const unsigned int oneTwo = 12;",
9048                Alignment);
9049   verifyFormat("int      oneTwoThree{0}; // comment\n"
9050                "unsigned oneTwo;         // comment",
9051                Alignment);
9052   EXPECT_EQ("float const a = 5;\n"
9053             "\n"
9054             "int oneTwoThree = 123;",
9055             format("float const   a = 5;\n"
9056                    "\n"
9057                    "int           oneTwoThree= 123;",
9058                    Alignment));
9059   EXPECT_EQ("float a = 5;\n"
9060             "int   one = 1;\n"
9061             "\n"
9062             "unsigned oneTwoThree = 123;",
9063             format("float    a = 5;\n"
9064                    "int      one = 1;\n"
9065                    "\n"
9066                    "unsigned oneTwoThree = 123;",
9067                    Alignment));
9068   EXPECT_EQ("float a = 5;\n"
9069             "int   one = 1;\n"
9070             "\n"
9071             "unsigned oneTwoThree = 123;\n"
9072             "int      oneTwo = 12;",
9073             format("float    a = 5;\n"
9074                    "int one = 1;\n"
9075                    "\n"
9076                    "unsigned oneTwoThree = 123;\n"
9077                    "int oneTwo = 12;",
9078                    Alignment));
9079   // Function prototype alignment
9080   verifyFormat("int    a();\n"
9081                "double b();",
9082                Alignment);
9083   verifyFormat("int    a(int x);\n"
9084                "double b();",
9085                Alignment);
9086   unsigned OldColumnLimit = Alignment.ColumnLimit;
9087   // We need to set ColumnLimit to zero, in order to stress nested alignments,
9088   // otherwise the function parameters will be re-flowed onto a single line.
9089   Alignment.ColumnLimit = 0;
9090   EXPECT_EQ("int    a(int   x,\n"
9091             "         float y);\n"
9092             "double b(int    x,\n"
9093             "         double y);",
9094             format("int a(int x,\n"
9095                    " float y);\n"
9096                    "double b(int x,\n"
9097                    " double y);",
9098                    Alignment));
9099   // This ensures that function parameters of function declarations are
9100   // correctly indented when their owning functions are indented.
9101   // The failure case here is for 'double y' to not be indented enough.
9102   EXPECT_EQ("double a(int x);\n"
9103             "int    b(int    y,\n"
9104             "         double z);",
9105             format("double a(int x);\n"
9106                    "int b(int y,\n"
9107                    " double z);",
9108                    Alignment));
9109   // Set ColumnLimit low so that we induce wrapping immediately after
9110   // the function name and opening paren.
9111   Alignment.ColumnLimit = 13;
9112   verifyFormat("int function(\n"
9113                "    int  x,\n"
9114                "    bool y);",
9115                Alignment);
9116   Alignment.ColumnLimit = OldColumnLimit;
9117   // Ensure function pointers don't screw up recursive alignment
9118   verifyFormat("int    a(int x, void (*fp)(int y));\n"
9119                "double b();",
9120                Alignment);
9121   Alignment.AlignConsecutiveAssignments = true;
9122   // Ensure recursive alignment is broken by function braces, so that the
9123   // "a = 1" does not align with subsequent assignments inside the function
9124   // body.
9125   verifyFormat("int func(int a = 1) {\n"
9126                "  int b  = 2;\n"
9127                "  int cc = 3;\n"
9128                "}",
9129                Alignment);
9130   verifyFormat("float      something = 2000;\n"
9131                "double     another   = 911;\n"
9132                "int        i = 1, j = 10;\n"
9133                "const int *oneMore = 1;\n"
9134                "unsigned   i       = 2;",
9135                Alignment);
9136   verifyFormat("int      oneTwoThree = {0}; // comment\n"
9137                "unsigned oneTwo      = 0;   // comment",
9138                Alignment);
9139   // Make sure that scope is correctly tracked, in the absence of braces
9140   verifyFormat("for (int i = 0; i < n; i++)\n"
9141                "  j = i;\n"
9142                "double x = 1;\n",
9143                Alignment);
9144   verifyFormat("if (int i = 0)\n"
9145                "  j = i;\n"
9146                "double x = 1;\n",
9147                Alignment);
9148   // Ensure operator[] and operator() are comprehended
9149   verifyFormat("struct test {\n"
9150                "  long long int foo();\n"
9151                "  int           operator[](int a);\n"
9152                "  double        bar();\n"
9153                "};\n",
9154                Alignment);
9155   verifyFormat("struct test {\n"
9156                "  long long int foo();\n"
9157                "  int           operator()(int a);\n"
9158                "  double        bar();\n"
9159                "};\n",
9160                Alignment);
9161   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
9162             "  int const i   = 1;\n"
9163             "  int *     j   = 2;\n"
9164             "  int       big = 10000;\n"
9165             "\n"
9166             "  unsigned oneTwoThree = 123;\n"
9167             "  int      oneTwo      = 12;\n"
9168             "  method();\n"
9169             "  float k  = 2;\n"
9170             "  int   ll = 10000;\n"
9171             "}",
9172             format("void SomeFunction(int parameter= 0) {\n"
9173                    " int const  i= 1;\n"
9174                    "  int *j=2;\n"
9175                    " int big  =  10000;\n"
9176                    "\n"
9177                    "unsigned oneTwoThree  =123;\n"
9178                    "int oneTwo = 12;\n"
9179                    "  method();\n"
9180                    "float k= 2;\n"
9181                    "int ll=10000;\n"
9182                    "}",
9183                    Alignment));
9184   Alignment.AlignConsecutiveAssignments = false;
9185   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
9186   verifyFormat("#define A \\\n"
9187                "  int       aaaa = 12; \\\n"
9188                "  float     b = 23; \\\n"
9189                "  const int ccc = 234; \\\n"
9190                "  unsigned  dddddddddd = 2345;",
9191                Alignment);
9192   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
9193   verifyFormat("#define A              \\\n"
9194                "  int       aaaa = 12; \\\n"
9195                "  float     b = 23;    \\\n"
9196                "  const int ccc = 234; \\\n"
9197                "  unsigned  dddddddddd = 2345;",
9198                Alignment);
9199   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
9200   Alignment.ColumnLimit = 30;
9201   verifyFormat("#define A                    \\\n"
9202                "  int       aaaa = 12;       \\\n"
9203                "  float     b = 23;          \\\n"
9204                "  const int ccc = 234;       \\\n"
9205                "  int       dddddddddd = 2345;",
9206                Alignment);
9207   Alignment.ColumnLimit = 80;
9208   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
9209                "k = 4, int l = 5,\n"
9210                "                  int m = 6) {\n"
9211                "  const int j = 10;\n"
9212                "  otherThing = 1;\n"
9213                "}",
9214                Alignment);
9215   verifyFormat("void SomeFunction(int parameter = 0) {\n"
9216                "  int const i = 1;\n"
9217                "  int *     j = 2;\n"
9218                "  int       big = 10000;\n"
9219                "}",
9220                Alignment);
9221   verifyFormat("class C {\n"
9222                "public:\n"
9223                "  int          i = 1;\n"
9224                "  virtual void f() = 0;\n"
9225                "};",
9226                Alignment);
9227   verifyFormat("float i = 1;\n"
9228                "if (SomeType t = getSomething()) {\n"
9229                "}\n"
9230                "const unsigned j = 2;\n"
9231                "int            big = 10000;",
9232                Alignment);
9233   verifyFormat("float j = 7;\n"
9234                "for (int k = 0; k < N; ++k) {\n"
9235                "}\n"
9236                "unsigned j = 2;\n"
9237                "int      big = 10000;\n"
9238                "}",
9239                Alignment);
9240   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9241   verifyFormat("float              i = 1;\n"
9242                "LooooooooooongType loooooooooooooooooooooongVariable\n"
9243                "    = someLooooooooooooooooongFunction();\n"
9244                "int j = 2;",
9245                Alignment);
9246   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9247   verifyFormat("int                i = 1;\n"
9248                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
9249                "    someLooooooooooooooooongFunction();\n"
9250                "int j = 2;",
9251                Alignment);
9252 
9253   Alignment.AlignConsecutiveAssignments = true;
9254   verifyFormat("auto lambda = []() {\n"
9255                "  auto  ii = 0;\n"
9256                "  float j  = 0;\n"
9257                "  return 0;\n"
9258                "};\n"
9259                "int   i  = 0;\n"
9260                "float i2 = 0;\n"
9261                "auto  v  = type{\n"
9262                "    i = 1,   //\n"
9263                "    (i = 2), //\n"
9264                "    i = 3    //\n"
9265                "};",
9266                Alignment);
9267   Alignment.AlignConsecutiveAssignments = false;
9268 
9269   verifyFormat(
9270       "int      i = 1;\n"
9271       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
9272       "                          loooooooooooooooooooooongParameterB);\n"
9273       "int      j = 2;",
9274       Alignment);
9275 
9276   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
9277   // We expect declarations and assignments to align, as long as it doesn't
9278   // exceed the column limit, starting a new alignment sequence whenever it
9279   // happens.
9280   Alignment.AlignConsecutiveAssignments = true;
9281   Alignment.ColumnLimit = 30;
9282   verifyFormat("float    ii              = 1;\n"
9283                "unsigned j               = 2;\n"
9284                "int someVerylongVariable = 1;\n"
9285                "AnotherLongType  ll = 123456;\n"
9286                "VeryVeryLongType k  = 2;\n"
9287                "int              myvar = 1;",
9288                Alignment);
9289   Alignment.ColumnLimit = 80;
9290   Alignment.AlignConsecutiveAssignments = false;
9291 
9292   verifyFormat(
9293       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
9294       "          typename LongType, typename B>\n"
9295       "auto foo() {}\n",
9296       Alignment);
9297   verifyFormat("float a, b = 1;\n"
9298                "int   c = 2;\n"
9299                "int   dd = 3;\n",
9300                Alignment);
9301   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
9302                "float b[1][] = {{3.f}};\n",
9303                Alignment);
9304   Alignment.AlignConsecutiveAssignments = true;
9305   verifyFormat("float a, b = 1;\n"
9306                "int   c  = 2;\n"
9307                "int   dd = 3;\n",
9308                Alignment);
9309   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
9310                "float b[1][] = {{3.f}};\n",
9311                Alignment);
9312   Alignment.AlignConsecutiveAssignments = false;
9313 
9314   Alignment.ColumnLimit = 30;
9315   Alignment.BinPackParameters = false;
9316   verifyFormat("void foo(float     a,\n"
9317                "         float     b,\n"
9318                "         int       c,\n"
9319                "         uint32_t *d) {\n"
9320                "  int *  e = 0;\n"
9321                "  float  f = 0;\n"
9322                "  double g = 0;\n"
9323                "}\n"
9324                "void bar(ino_t     a,\n"
9325                "         int       b,\n"
9326                "         uint32_t *c,\n"
9327                "         bool      d) {}\n",
9328                Alignment);
9329   Alignment.BinPackParameters = true;
9330   Alignment.ColumnLimit = 80;
9331 
9332   // Bug 33507
9333   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
9334   verifyFormat(
9335       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
9336       "  static const Version verVs2017;\n"
9337       "  return true;\n"
9338       "});\n",
9339       Alignment);
9340   Alignment.PointerAlignment = FormatStyle::PAS_Right;
9341 }
9342 
9343 TEST_F(FormatTest, LinuxBraceBreaking) {
9344   FormatStyle LinuxBraceStyle = getLLVMStyle();
9345   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
9346   verifyFormat("namespace a\n"
9347                "{\n"
9348                "class A\n"
9349                "{\n"
9350                "  void f()\n"
9351                "  {\n"
9352                "    if (true) {\n"
9353                "      a();\n"
9354                "      b();\n"
9355                "    } else {\n"
9356                "      a();\n"
9357                "    }\n"
9358                "  }\n"
9359                "  void g() { return; }\n"
9360                "};\n"
9361                "struct B {\n"
9362                "  int x;\n"
9363                "};\n"
9364                "} // namespace a\n",
9365                LinuxBraceStyle);
9366   verifyFormat("enum X {\n"
9367                "  Y = 0,\n"
9368                "}\n",
9369                LinuxBraceStyle);
9370   verifyFormat("struct S {\n"
9371                "  int Type;\n"
9372                "  union {\n"
9373                "    int x;\n"
9374                "    double y;\n"
9375                "  } Value;\n"
9376                "  class C\n"
9377                "  {\n"
9378                "    MyFavoriteType Value;\n"
9379                "  } Class;\n"
9380                "}\n",
9381                LinuxBraceStyle);
9382 }
9383 
9384 TEST_F(FormatTest, MozillaBraceBreaking) {
9385   FormatStyle MozillaBraceStyle = getLLVMStyle();
9386   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
9387   MozillaBraceStyle.FixNamespaceComments = false;
9388   verifyFormat("namespace a {\n"
9389                "class A\n"
9390                "{\n"
9391                "  void f()\n"
9392                "  {\n"
9393                "    if (true) {\n"
9394                "      a();\n"
9395                "      b();\n"
9396                "    }\n"
9397                "  }\n"
9398                "  void g() { return; }\n"
9399                "};\n"
9400                "enum E\n"
9401                "{\n"
9402                "  A,\n"
9403                "  // foo\n"
9404                "  B,\n"
9405                "  C\n"
9406                "};\n"
9407                "struct B\n"
9408                "{\n"
9409                "  int x;\n"
9410                "};\n"
9411                "}\n",
9412                MozillaBraceStyle);
9413   verifyFormat("struct S\n"
9414                "{\n"
9415                "  int Type;\n"
9416                "  union\n"
9417                "  {\n"
9418                "    int x;\n"
9419                "    double y;\n"
9420                "  } Value;\n"
9421                "  class C\n"
9422                "  {\n"
9423                "    MyFavoriteType Value;\n"
9424                "  } Class;\n"
9425                "}\n",
9426                MozillaBraceStyle);
9427 }
9428 
9429 TEST_F(FormatTest, StroustrupBraceBreaking) {
9430   FormatStyle StroustrupBraceStyle = getLLVMStyle();
9431   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9432   verifyFormat("namespace a {\n"
9433                "class A {\n"
9434                "  void f()\n"
9435                "  {\n"
9436                "    if (true) {\n"
9437                "      a();\n"
9438                "      b();\n"
9439                "    }\n"
9440                "  }\n"
9441                "  void g() { return; }\n"
9442                "};\n"
9443                "struct B {\n"
9444                "  int x;\n"
9445                "};\n"
9446                "} // namespace a\n",
9447                StroustrupBraceStyle);
9448 
9449   verifyFormat("void foo()\n"
9450                "{\n"
9451                "  if (a) {\n"
9452                "    a();\n"
9453                "  }\n"
9454                "  else {\n"
9455                "    b();\n"
9456                "  }\n"
9457                "}\n",
9458                StroustrupBraceStyle);
9459 
9460   verifyFormat("#ifdef _DEBUG\n"
9461                "int foo(int i = 0)\n"
9462                "#else\n"
9463                "int foo(int i = 5)\n"
9464                "#endif\n"
9465                "{\n"
9466                "  return i;\n"
9467                "}",
9468                StroustrupBraceStyle);
9469 
9470   verifyFormat("void foo() {}\n"
9471                "void bar()\n"
9472                "#ifdef _DEBUG\n"
9473                "{\n"
9474                "  foo();\n"
9475                "}\n"
9476                "#else\n"
9477                "{\n"
9478                "}\n"
9479                "#endif",
9480                StroustrupBraceStyle);
9481 
9482   verifyFormat("void foobar() { int i = 5; }\n"
9483                "#ifdef _DEBUG\n"
9484                "void bar() {}\n"
9485                "#else\n"
9486                "void bar() { foobar(); }\n"
9487                "#endif",
9488                StroustrupBraceStyle);
9489 }
9490 
9491 TEST_F(FormatTest, AllmanBraceBreaking) {
9492   FormatStyle AllmanBraceStyle = getLLVMStyle();
9493   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
9494 
9495   EXPECT_EQ("namespace a\n"
9496             "{\n"
9497             "void f();\n"
9498             "void g();\n"
9499             "} // namespace a\n",
9500             format("namespace a\n"
9501                    "{\n"
9502                    "void f();\n"
9503                    "void g();\n"
9504                    "}\n",
9505                    AllmanBraceStyle));
9506 
9507   verifyFormat("namespace a\n"
9508                "{\n"
9509                "class A\n"
9510                "{\n"
9511                "  void f()\n"
9512                "  {\n"
9513                "    if (true)\n"
9514                "    {\n"
9515                "      a();\n"
9516                "      b();\n"
9517                "    }\n"
9518                "  }\n"
9519                "  void g() { return; }\n"
9520                "};\n"
9521                "struct B\n"
9522                "{\n"
9523                "  int x;\n"
9524                "};\n"
9525                "} // namespace a",
9526                AllmanBraceStyle);
9527 
9528   verifyFormat("void f()\n"
9529                "{\n"
9530                "  if (true)\n"
9531                "  {\n"
9532                "    a();\n"
9533                "  }\n"
9534                "  else if (false)\n"
9535                "  {\n"
9536                "    b();\n"
9537                "  }\n"
9538                "  else\n"
9539                "  {\n"
9540                "    c();\n"
9541                "  }\n"
9542                "}\n",
9543                AllmanBraceStyle);
9544 
9545   verifyFormat("void f()\n"
9546                "{\n"
9547                "  for (int i = 0; i < 10; ++i)\n"
9548                "  {\n"
9549                "    a();\n"
9550                "  }\n"
9551                "  while (false)\n"
9552                "  {\n"
9553                "    b();\n"
9554                "  }\n"
9555                "  do\n"
9556                "  {\n"
9557                "    c();\n"
9558                "  } while (false)\n"
9559                "}\n",
9560                AllmanBraceStyle);
9561 
9562   verifyFormat("void f(int a)\n"
9563                "{\n"
9564                "  switch (a)\n"
9565                "  {\n"
9566                "  case 0:\n"
9567                "    break;\n"
9568                "  case 1:\n"
9569                "  {\n"
9570                "    break;\n"
9571                "  }\n"
9572                "  case 2:\n"
9573                "  {\n"
9574                "  }\n"
9575                "  break;\n"
9576                "  default:\n"
9577                "    break;\n"
9578                "  }\n"
9579                "}\n",
9580                AllmanBraceStyle);
9581 
9582   verifyFormat("enum X\n"
9583                "{\n"
9584                "  Y = 0,\n"
9585                "}\n",
9586                AllmanBraceStyle);
9587   verifyFormat("enum X\n"
9588                "{\n"
9589                "  Y = 0\n"
9590                "}\n",
9591                AllmanBraceStyle);
9592 
9593   verifyFormat("@interface BSApplicationController ()\n"
9594                "{\n"
9595                "@private\n"
9596                "  id _extraIvar;\n"
9597                "}\n"
9598                "@end\n",
9599                AllmanBraceStyle);
9600 
9601   verifyFormat("#ifdef _DEBUG\n"
9602                "int foo(int i = 0)\n"
9603                "#else\n"
9604                "int foo(int i = 5)\n"
9605                "#endif\n"
9606                "{\n"
9607                "  return i;\n"
9608                "}",
9609                AllmanBraceStyle);
9610 
9611   verifyFormat("void foo() {}\n"
9612                "void bar()\n"
9613                "#ifdef _DEBUG\n"
9614                "{\n"
9615                "  foo();\n"
9616                "}\n"
9617                "#else\n"
9618                "{\n"
9619                "}\n"
9620                "#endif",
9621                AllmanBraceStyle);
9622 
9623   verifyFormat("void foobar() { int i = 5; }\n"
9624                "#ifdef _DEBUG\n"
9625                "void bar() {}\n"
9626                "#else\n"
9627                "void bar() { foobar(); }\n"
9628                "#endif",
9629                AllmanBraceStyle);
9630 
9631   // This shouldn't affect ObjC blocks..
9632   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
9633                "  // ...\n"
9634                "  int i;\n"
9635                "}];",
9636                AllmanBraceStyle);
9637   verifyFormat("void (^block)(void) = ^{\n"
9638                "  // ...\n"
9639                "  int i;\n"
9640                "};",
9641                AllmanBraceStyle);
9642   // .. or dict literals.
9643   verifyFormat("void f()\n"
9644                "{\n"
9645                "  // ...\n"
9646                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
9647                "}",
9648                AllmanBraceStyle);
9649   verifyFormat("void f()\n"
9650                "{\n"
9651                "  // ...\n"
9652                "  [object someMethod:@{a : @\"b\"}];\n"
9653                "}",
9654                AllmanBraceStyle);
9655   verifyFormat("int f()\n"
9656                "{ // comment\n"
9657                "  return 42;\n"
9658                "}",
9659                AllmanBraceStyle);
9660 
9661   AllmanBraceStyle.ColumnLimit = 19;
9662   verifyFormat("void f() { int i; }", AllmanBraceStyle);
9663   AllmanBraceStyle.ColumnLimit = 18;
9664   verifyFormat("void f()\n"
9665                "{\n"
9666                "  int i;\n"
9667                "}",
9668                AllmanBraceStyle);
9669   AllmanBraceStyle.ColumnLimit = 80;
9670 
9671   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
9672   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
9673   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
9674   verifyFormat("void f(bool b)\n"
9675                "{\n"
9676                "  if (b)\n"
9677                "  {\n"
9678                "    return;\n"
9679                "  }\n"
9680                "}\n",
9681                BreakBeforeBraceShortIfs);
9682   verifyFormat("void f(bool b)\n"
9683                "{\n"
9684                "  if constexpr (b)\n"
9685                "  {\n"
9686                "    return;\n"
9687                "  }\n"
9688                "}\n",
9689                BreakBeforeBraceShortIfs);
9690   verifyFormat("void f(bool b)\n"
9691                "{\n"
9692                "  if (b) return;\n"
9693                "}\n",
9694                BreakBeforeBraceShortIfs);
9695   verifyFormat("void f(bool b)\n"
9696                "{\n"
9697                "  if constexpr (b) return;\n"
9698                "}\n",
9699                BreakBeforeBraceShortIfs);
9700   verifyFormat("void f(bool b)\n"
9701                "{\n"
9702                "  while (b)\n"
9703                "  {\n"
9704                "    return;\n"
9705                "  }\n"
9706                "}\n",
9707                BreakBeforeBraceShortIfs);
9708 }
9709 
9710 TEST_F(FormatTest, GNUBraceBreaking) {
9711   FormatStyle GNUBraceStyle = getLLVMStyle();
9712   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
9713   verifyFormat("namespace a\n"
9714                "{\n"
9715                "class A\n"
9716                "{\n"
9717                "  void f()\n"
9718                "  {\n"
9719                "    int a;\n"
9720                "    {\n"
9721                "      int b;\n"
9722                "    }\n"
9723                "    if (true)\n"
9724                "      {\n"
9725                "        a();\n"
9726                "        b();\n"
9727                "      }\n"
9728                "  }\n"
9729                "  void g() { return; }\n"
9730                "}\n"
9731                "} // namespace a",
9732                GNUBraceStyle);
9733 
9734   verifyFormat("void f()\n"
9735                "{\n"
9736                "  if (true)\n"
9737                "    {\n"
9738                "      a();\n"
9739                "    }\n"
9740                "  else if (false)\n"
9741                "    {\n"
9742                "      b();\n"
9743                "    }\n"
9744                "  else\n"
9745                "    {\n"
9746                "      c();\n"
9747                "    }\n"
9748                "}\n",
9749                GNUBraceStyle);
9750 
9751   verifyFormat("void f()\n"
9752                "{\n"
9753                "  for (int i = 0; i < 10; ++i)\n"
9754                "    {\n"
9755                "      a();\n"
9756                "    }\n"
9757                "  while (false)\n"
9758                "    {\n"
9759                "      b();\n"
9760                "    }\n"
9761                "  do\n"
9762                "    {\n"
9763                "      c();\n"
9764                "    }\n"
9765                "  while (false);\n"
9766                "}\n",
9767                GNUBraceStyle);
9768 
9769   verifyFormat("void f(int a)\n"
9770                "{\n"
9771                "  switch (a)\n"
9772                "    {\n"
9773                "    case 0:\n"
9774                "      break;\n"
9775                "    case 1:\n"
9776                "      {\n"
9777                "        break;\n"
9778                "      }\n"
9779                "    case 2:\n"
9780                "      {\n"
9781                "      }\n"
9782                "      break;\n"
9783                "    default:\n"
9784                "      break;\n"
9785                "    }\n"
9786                "}\n",
9787                GNUBraceStyle);
9788 
9789   verifyFormat("enum X\n"
9790                "{\n"
9791                "  Y = 0,\n"
9792                "}\n",
9793                GNUBraceStyle);
9794 
9795   verifyFormat("@interface BSApplicationController ()\n"
9796                "{\n"
9797                "@private\n"
9798                "  id _extraIvar;\n"
9799                "}\n"
9800                "@end\n",
9801                GNUBraceStyle);
9802 
9803   verifyFormat("#ifdef _DEBUG\n"
9804                "int foo(int i = 0)\n"
9805                "#else\n"
9806                "int foo(int i = 5)\n"
9807                "#endif\n"
9808                "{\n"
9809                "  return i;\n"
9810                "}",
9811                GNUBraceStyle);
9812 
9813   verifyFormat("void foo() {}\n"
9814                "void bar()\n"
9815                "#ifdef _DEBUG\n"
9816                "{\n"
9817                "  foo();\n"
9818                "}\n"
9819                "#else\n"
9820                "{\n"
9821                "}\n"
9822                "#endif",
9823                GNUBraceStyle);
9824 
9825   verifyFormat("void foobar() { int i = 5; }\n"
9826                "#ifdef _DEBUG\n"
9827                "void bar() {}\n"
9828                "#else\n"
9829                "void bar() { foobar(); }\n"
9830                "#endif",
9831                GNUBraceStyle);
9832 }
9833 
9834 TEST_F(FormatTest, WebKitBraceBreaking) {
9835   FormatStyle WebKitBraceStyle = getLLVMStyle();
9836   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
9837   WebKitBraceStyle.FixNamespaceComments = false;
9838   verifyFormat("namespace a {\n"
9839                "class A {\n"
9840                "  void f()\n"
9841                "  {\n"
9842                "    if (true) {\n"
9843                "      a();\n"
9844                "      b();\n"
9845                "    }\n"
9846                "  }\n"
9847                "  void g() { return; }\n"
9848                "};\n"
9849                "enum E {\n"
9850                "  A,\n"
9851                "  // foo\n"
9852                "  B,\n"
9853                "  C\n"
9854                "};\n"
9855                "struct B {\n"
9856                "  int x;\n"
9857                "};\n"
9858                "}\n",
9859                WebKitBraceStyle);
9860   verifyFormat("struct S {\n"
9861                "  int Type;\n"
9862                "  union {\n"
9863                "    int x;\n"
9864                "    double y;\n"
9865                "  } Value;\n"
9866                "  class C {\n"
9867                "    MyFavoriteType Value;\n"
9868                "  } Class;\n"
9869                "};\n",
9870                WebKitBraceStyle);
9871 }
9872 
9873 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
9874   verifyFormat("void f() {\n"
9875                "  try {\n"
9876                "  } catch (const Exception &e) {\n"
9877                "  }\n"
9878                "}\n",
9879                getLLVMStyle());
9880 }
9881 
9882 TEST_F(FormatTest, UnderstandsPragmas) {
9883   verifyFormat("#pragma omp reduction(| : var)");
9884   verifyFormat("#pragma omp reduction(+ : var)");
9885 
9886   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
9887             "(including parentheses).",
9888             format("#pragma    mark   Any non-hyphenated or hyphenated string "
9889                    "(including parentheses)."));
9890 }
9891 
9892 TEST_F(FormatTest, UnderstandPragmaOption) {
9893   verifyFormat("#pragma option -C -A");
9894 
9895   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
9896 }
9897 
9898 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
9899   for (size_t i = 1; i < Styles.size(); ++i)                                   \
9900   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
9901                                   << " differs from Style #0"
9902 
9903 TEST_F(FormatTest, GetsPredefinedStyleByName) {
9904   SmallVector<FormatStyle, 3> Styles;
9905   Styles.resize(3);
9906 
9907   Styles[0] = getLLVMStyle();
9908   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
9909   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
9910   EXPECT_ALL_STYLES_EQUAL(Styles);
9911 
9912   Styles[0] = getGoogleStyle();
9913   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
9914   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
9915   EXPECT_ALL_STYLES_EQUAL(Styles);
9916 
9917   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9918   EXPECT_TRUE(
9919       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
9920   EXPECT_TRUE(
9921       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
9922   EXPECT_ALL_STYLES_EQUAL(Styles);
9923 
9924   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
9925   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
9926   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
9927   EXPECT_ALL_STYLES_EQUAL(Styles);
9928 
9929   Styles[0] = getMozillaStyle();
9930   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
9931   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
9932   EXPECT_ALL_STYLES_EQUAL(Styles);
9933 
9934   Styles[0] = getWebKitStyle();
9935   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
9936   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
9937   EXPECT_ALL_STYLES_EQUAL(Styles);
9938 
9939   Styles[0] = getGNUStyle();
9940   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
9941   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
9942   EXPECT_ALL_STYLES_EQUAL(Styles);
9943 
9944   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
9945 }
9946 
9947 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
9948   SmallVector<FormatStyle, 8> Styles;
9949   Styles.resize(2);
9950 
9951   Styles[0] = getGoogleStyle();
9952   Styles[1] = getLLVMStyle();
9953   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9954   EXPECT_ALL_STYLES_EQUAL(Styles);
9955 
9956   Styles.resize(5);
9957   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9958   Styles[1] = getLLVMStyle();
9959   Styles[1].Language = FormatStyle::LK_JavaScript;
9960   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9961 
9962   Styles[2] = getLLVMStyle();
9963   Styles[2].Language = FormatStyle::LK_JavaScript;
9964   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
9965                                   "BasedOnStyle: Google",
9966                                   &Styles[2])
9967                    .value());
9968 
9969   Styles[3] = getLLVMStyle();
9970   Styles[3].Language = FormatStyle::LK_JavaScript;
9971   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
9972                                   "Language: JavaScript",
9973                                   &Styles[3])
9974                    .value());
9975 
9976   Styles[4] = getLLVMStyle();
9977   Styles[4].Language = FormatStyle::LK_JavaScript;
9978   EXPECT_EQ(0, parseConfiguration("---\n"
9979                                   "BasedOnStyle: LLVM\n"
9980                                   "IndentWidth: 123\n"
9981                                   "---\n"
9982                                   "BasedOnStyle: Google\n"
9983                                   "Language: JavaScript",
9984                                   &Styles[4])
9985                    .value());
9986   EXPECT_ALL_STYLES_EQUAL(Styles);
9987 }
9988 
9989 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
9990   Style.FIELD = false;                                                         \
9991   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
9992   EXPECT_TRUE(Style.FIELD);                                                    \
9993   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
9994   EXPECT_FALSE(Style.FIELD);
9995 
9996 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
9997 
9998 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
9999   Style.STRUCT.FIELD = false;                                                  \
10000   EXPECT_EQ(0,                                                                 \
10001             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
10002                 .value());                                                     \
10003   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
10004   EXPECT_EQ(0,                                                                 \
10005             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
10006                 .value());                                                     \
10007   EXPECT_FALSE(Style.STRUCT.FIELD);
10008 
10009 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
10010   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
10011 
10012 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
10013   EXPECT_NE(VALUE, Style.FIELD);                                               \
10014   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
10015   EXPECT_EQ(VALUE, Style.FIELD)
10016 
10017 TEST_F(FormatTest, ParsesConfigurationBools) {
10018   FormatStyle Style = {};
10019   Style.Language = FormatStyle::LK_Cpp;
10020   CHECK_PARSE_BOOL(AlignOperands);
10021   CHECK_PARSE_BOOL(AlignTrailingComments);
10022   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
10023   CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
10024   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
10025   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
10026   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
10027   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
10028   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
10029   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
10030   CHECK_PARSE_BOOL(BinPackArguments);
10031   CHECK_PARSE_BOOL(BinPackParameters);
10032   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
10033   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
10034   CHECK_PARSE_BOOL(BreakStringLiterals);
10035   CHECK_PARSE_BOOL(BreakBeforeInheritanceComma)
10036   CHECK_PARSE_BOOL(CompactNamespaces);
10037   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
10038   CHECK_PARSE_BOOL(DerivePointerAlignment);
10039   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
10040   CHECK_PARSE_BOOL(DisableFormat);
10041   CHECK_PARSE_BOOL(IndentCaseLabels);
10042   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
10043   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
10044   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
10045   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
10046   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
10047   CHECK_PARSE_BOOL(ReflowComments);
10048   CHECK_PARSE_BOOL(SortIncludes);
10049   CHECK_PARSE_BOOL(SortUsingDeclarations);
10050   CHECK_PARSE_BOOL(SpacesInParentheses);
10051   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
10052   CHECK_PARSE_BOOL(SpacesInAngles);
10053   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
10054   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
10055   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
10056   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
10057   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
10058   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
10059 
10060   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
10061   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
10062   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
10063   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
10064   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
10065   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
10066   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
10067   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
10068   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
10069   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
10070   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
10071   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
10072   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
10073   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
10074   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
10075 }
10076 
10077 #undef CHECK_PARSE_BOOL
10078 
10079 TEST_F(FormatTest, ParsesConfiguration) {
10080   FormatStyle Style = {};
10081   Style.Language = FormatStyle::LK_Cpp;
10082   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
10083   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
10084               ConstructorInitializerIndentWidth, 1234u);
10085   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
10086   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
10087   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
10088   CHECK_PARSE("PenaltyBreakAssignment: 1234",
10089               PenaltyBreakAssignment, 1234u);
10090   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
10091               PenaltyBreakBeforeFirstCallParameter, 1234u);
10092   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
10093   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
10094               PenaltyReturnTypeOnItsOwnLine, 1234u);
10095   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
10096               SpacesBeforeTrailingComments, 1234u);
10097   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
10098   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
10099   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
10100 
10101   Style.PointerAlignment = FormatStyle::PAS_Middle;
10102   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
10103               FormatStyle::PAS_Left);
10104   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
10105               FormatStyle::PAS_Right);
10106   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
10107               FormatStyle::PAS_Middle);
10108   // For backward compatibility:
10109   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
10110               FormatStyle::PAS_Left);
10111   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
10112               FormatStyle::PAS_Right);
10113   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
10114               FormatStyle::PAS_Middle);
10115 
10116   Style.Standard = FormatStyle::LS_Auto;
10117   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
10118   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
10119   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
10120   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
10121   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
10122 
10123   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
10124   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
10125               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
10126   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
10127               FormatStyle::BOS_None);
10128   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
10129               FormatStyle::BOS_All);
10130   // For backward compatibility:
10131   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
10132               FormatStyle::BOS_None);
10133   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
10134               FormatStyle::BOS_All);
10135 
10136   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
10137   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
10138               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
10139   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
10140               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
10141   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
10142               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
10143   // For backward compatibility:
10144   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
10145               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
10146 
10147   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10148   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
10149               FormatStyle::BAS_Align);
10150   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
10151               FormatStyle::BAS_DontAlign);
10152   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
10153               FormatStyle::BAS_AlwaysBreak);
10154   // For backward compatibility:
10155   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
10156               FormatStyle::BAS_DontAlign);
10157   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
10158               FormatStyle::BAS_Align);
10159 
10160   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
10161   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
10162               FormatStyle::ENAS_DontAlign);
10163   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
10164               FormatStyle::ENAS_Left);
10165   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
10166               FormatStyle::ENAS_Right);
10167   // For backward compatibility:
10168   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
10169               FormatStyle::ENAS_Left);
10170   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
10171               FormatStyle::ENAS_Right);
10172 
10173   Style.UseTab = FormatStyle::UT_ForIndentation;
10174   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
10175   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
10176   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
10177   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
10178               FormatStyle::UT_ForContinuationAndIndentation);
10179   // For backward compatibility:
10180   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
10181   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
10182 
10183   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
10184   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
10185               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
10186   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
10187               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
10188   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
10189               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
10190   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
10191               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
10192   // For backward compatibility:
10193   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
10194               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
10195   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
10196               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
10197 
10198   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
10199   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
10200               FormatStyle::SBPO_Never);
10201   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
10202               FormatStyle::SBPO_Always);
10203   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
10204               FormatStyle::SBPO_ControlStatements);
10205   // For backward compatibility:
10206   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
10207               FormatStyle::SBPO_Never);
10208   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
10209               FormatStyle::SBPO_ControlStatements);
10210 
10211   Style.ColumnLimit = 123;
10212   FormatStyle BaseStyle = getLLVMStyle();
10213   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
10214   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
10215 
10216   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
10217   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
10218               FormatStyle::BS_Attach);
10219   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
10220               FormatStyle::BS_Linux);
10221   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
10222               FormatStyle::BS_Mozilla);
10223   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
10224               FormatStyle::BS_Stroustrup);
10225   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
10226               FormatStyle::BS_Allman);
10227   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
10228   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
10229               FormatStyle::BS_WebKit);
10230   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
10231               FormatStyle::BS_Custom);
10232 
10233   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
10234   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
10235               FormatStyle::RTBS_None);
10236   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
10237               FormatStyle::RTBS_All);
10238   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
10239               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
10240   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
10241               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
10242   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
10243               AlwaysBreakAfterReturnType,
10244               FormatStyle::RTBS_TopLevelDefinitions);
10245 
10246   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
10247   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
10248               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
10249   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
10250               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
10251   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
10252               AlwaysBreakAfterDefinitionReturnType,
10253               FormatStyle::DRTBS_TopLevel);
10254 
10255   Style.NamespaceIndentation = FormatStyle::NI_All;
10256   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
10257               FormatStyle::NI_None);
10258   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
10259               FormatStyle::NI_Inner);
10260   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
10261               FormatStyle::NI_All);
10262 
10263   // FIXME: This is required because parsing a configuration simply overwrites
10264   // the first N elements of the list instead of resetting it.
10265   Style.ForEachMacros.clear();
10266   std::vector<std::string> BoostForeach;
10267   BoostForeach.push_back("BOOST_FOREACH");
10268   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
10269   std::vector<std::string> BoostAndQForeach;
10270   BoostAndQForeach.push_back("BOOST_FOREACH");
10271   BoostAndQForeach.push_back("Q_FOREACH");
10272   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
10273               BoostAndQForeach);
10274 
10275   Style.IncludeCategories.clear();
10276   std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
10277                                                                   {".*", 1}};
10278   CHECK_PARSE("IncludeCategories:\n"
10279               "  - Regex: abc/.*\n"
10280               "    Priority: 2\n"
10281               "  - Regex: .*\n"
10282               "    Priority: 1",
10283               IncludeCategories, ExpectedCategories);
10284   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$");
10285 
10286   Style.RawStringFormats.clear();
10287   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
10288       {"pb", FormatStyle::LK_TextProto, "llvm"},
10289       {"cpp", FormatStyle::LK_Cpp, "google"}};
10290 
10291   CHECK_PARSE("RawStringFormats:\n"
10292               "  - Delimiter: 'pb'\n"
10293               "    Language: TextProto\n"
10294               "    BasedOnStyle: llvm\n"
10295               "  - Delimiter: 'cpp'\n"
10296               "    Language: Cpp\n"
10297               "    BasedOnStyle: google",
10298               RawStringFormats, ExpectedRawStringFormats);
10299 }
10300 
10301 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
10302   FormatStyle Style = {};
10303   Style.Language = FormatStyle::LK_Cpp;
10304   CHECK_PARSE("Language: Cpp\n"
10305               "IndentWidth: 12",
10306               IndentWidth, 12u);
10307   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
10308                                "IndentWidth: 34",
10309                                &Style),
10310             ParseError::Unsuitable);
10311   EXPECT_EQ(12u, Style.IndentWidth);
10312   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
10313   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
10314 
10315   Style.Language = FormatStyle::LK_JavaScript;
10316   CHECK_PARSE("Language: JavaScript\n"
10317               "IndentWidth: 12",
10318               IndentWidth, 12u);
10319   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
10320   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
10321                                "IndentWidth: 34",
10322                                &Style),
10323             ParseError::Unsuitable);
10324   EXPECT_EQ(23u, Style.IndentWidth);
10325   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
10326   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
10327 
10328   CHECK_PARSE("BasedOnStyle: LLVM\n"
10329               "IndentWidth: 67",
10330               IndentWidth, 67u);
10331 
10332   CHECK_PARSE("---\n"
10333               "Language: JavaScript\n"
10334               "IndentWidth: 12\n"
10335               "---\n"
10336               "Language: Cpp\n"
10337               "IndentWidth: 34\n"
10338               "...\n",
10339               IndentWidth, 12u);
10340 
10341   Style.Language = FormatStyle::LK_Cpp;
10342   CHECK_PARSE("---\n"
10343               "Language: JavaScript\n"
10344               "IndentWidth: 12\n"
10345               "---\n"
10346               "Language: Cpp\n"
10347               "IndentWidth: 34\n"
10348               "...\n",
10349               IndentWidth, 34u);
10350   CHECK_PARSE("---\n"
10351               "IndentWidth: 78\n"
10352               "---\n"
10353               "Language: JavaScript\n"
10354               "IndentWidth: 56\n"
10355               "...\n",
10356               IndentWidth, 78u);
10357 
10358   Style.ColumnLimit = 123;
10359   Style.IndentWidth = 234;
10360   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
10361   Style.TabWidth = 345;
10362   EXPECT_FALSE(parseConfiguration("---\n"
10363                                   "IndentWidth: 456\n"
10364                                   "BreakBeforeBraces: Allman\n"
10365                                   "---\n"
10366                                   "Language: JavaScript\n"
10367                                   "IndentWidth: 111\n"
10368                                   "TabWidth: 111\n"
10369                                   "---\n"
10370                                   "Language: Cpp\n"
10371                                   "BreakBeforeBraces: Stroustrup\n"
10372                                   "TabWidth: 789\n"
10373                                   "...\n",
10374                                   &Style));
10375   EXPECT_EQ(123u, Style.ColumnLimit);
10376   EXPECT_EQ(456u, Style.IndentWidth);
10377   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
10378   EXPECT_EQ(789u, Style.TabWidth);
10379 
10380   EXPECT_EQ(parseConfiguration("---\n"
10381                                "Language: JavaScript\n"
10382                                "IndentWidth: 56\n"
10383                                "---\n"
10384                                "IndentWidth: 78\n"
10385                                "...\n",
10386                                &Style),
10387             ParseError::Error);
10388   EXPECT_EQ(parseConfiguration("---\n"
10389                                "Language: JavaScript\n"
10390                                "IndentWidth: 56\n"
10391                                "---\n"
10392                                "Language: JavaScript\n"
10393                                "IndentWidth: 78\n"
10394                                "...\n",
10395                                &Style),
10396             ParseError::Error);
10397 
10398   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
10399 }
10400 
10401 #undef CHECK_PARSE
10402 
10403 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
10404   FormatStyle Style = {};
10405   Style.Language = FormatStyle::LK_JavaScript;
10406   Style.BreakBeforeTernaryOperators = true;
10407   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
10408   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
10409 
10410   Style.BreakBeforeTernaryOperators = true;
10411   EXPECT_EQ(0, parseConfiguration("---\n"
10412                                   "BasedOnStyle: Google\n"
10413                                   "---\n"
10414                                   "Language: JavaScript\n"
10415                                   "IndentWidth: 76\n"
10416                                   "...\n",
10417                                   &Style)
10418                    .value());
10419   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
10420   EXPECT_EQ(76u, Style.IndentWidth);
10421   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
10422 }
10423 
10424 TEST_F(FormatTest, ConfigurationRoundTripTest) {
10425   FormatStyle Style = getLLVMStyle();
10426   std::string YAML = configurationAsText(Style);
10427   FormatStyle ParsedStyle = {};
10428   ParsedStyle.Language = FormatStyle::LK_Cpp;
10429   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
10430   EXPECT_EQ(Style, ParsedStyle);
10431 }
10432 
10433 TEST_F(FormatTest, WorksFor8bitEncodings) {
10434   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
10435             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
10436             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
10437             "\"\xef\xee\xf0\xf3...\"",
10438             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
10439                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
10440                    "\xef\xee\xf0\xf3...\"",
10441                    getLLVMStyleWithColumns(12)));
10442 }
10443 
10444 TEST_F(FormatTest, HandlesUTF8BOM) {
10445   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
10446   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
10447             format("\xef\xbb\xbf#include <iostream>"));
10448   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
10449             format("\xef\xbb\xbf\n#include <iostream>"));
10450 }
10451 
10452 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
10453 #if !defined(_MSC_VER)
10454 
10455 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
10456   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
10457                getLLVMStyleWithColumns(35));
10458   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
10459                getLLVMStyleWithColumns(31));
10460   verifyFormat("// Однажды в студёную зимнюю пору...",
10461                getLLVMStyleWithColumns(36));
10462   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
10463   verifyFormat("/* Однажды в студёную зимнюю пору... */",
10464                getLLVMStyleWithColumns(39));
10465   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
10466                getLLVMStyleWithColumns(35));
10467 }
10468 
10469 TEST_F(FormatTest, SplitsUTF8Strings) {
10470   // Non-printable characters' width is currently considered to be the length in
10471   // bytes in UTF8. The characters can be displayed in very different manner
10472   // (zero-width, single width with a substitution glyph, expanded to their code
10473   // (e.g. "<8d>"), so there's no single correct way to handle them.
10474   EXPECT_EQ("\"aaaaÄ\"\n"
10475             "\"\xc2\x8d\";",
10476             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
10477   EXPECT_EQ("\"aaaaaaaÄ\"\n"
10478             "\"\xc2\x8d\";",
10479             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
10480   EXPECT_EQ("\"Однажды, в \"\n"
10481             "\"студёную \"\n"
10482             "\"зимнюю \"\n"
10483             "\"пору,\"",
10484             format("\"Однажды, в студёную зимнюю пору,\"",
10485                    getLLVMStyleWithColumns(13)));
10486   EXPECT_EQ(
10487       "\"一 二 三 \"\n"
10488       "\"四 五六 \"\n"
10489       "\"七 八 九 \"\n"
10490       "\"十\"",
10491       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
10492   EXPECT_EQ("\"一\t二 \"\n"
10493             "\"\t三 \"\n"
10494             "\"四 五\t六 \"\n"
10495             "\"\t七 \"\n"
10496             "\"八九十\tqq\"",
10497             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
10498                    getLLVMStyleWithColumns(11)));
10499 
10500   // UTF8 character in an escape sequence.
10501   EXPECT_EQ("\"aaaaaa\"\n"
10502             "\"\\\xC2\x8D\"",
10503             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
10504 }
10505 
10506 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
10507   EXPECT_EQ("const char *sssss =\n"
10508             "    \"一二三四五六七八\\\n"
10509             " 九 十\";",
10510             format("const char *sssss = \"一二三四五六七八\\\n"
10511                    " 九 十\";",
10512                    getLLVMStyleWithColumns(30)));
10513 }
10514 
10515 TEST_F(FormatTest, SplitsUTF8LineComments) {
10516   EXPECT_EQ("// aaaaÄ\xc2\x8d",
10517             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
10518   EXPECT_EQ("// Я из лесу\n"
10519             "// вышел; был\n"
10520             "// сильный\n"
10521             "// мороз.",
10522             format("// Я из лесу вышел; был сильный мороз.",
10523                    getLLVMStyleWithColumns(13)));
10524   EXPECT_EQ("// 一二三\n"
10525             "// 四五六七\n"
10526             "// 八  九\n"
10527             "// 十",
10528             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
10529 }
10530 
10531 TEST_F(FormatTest, SplitsUTF8BlockComments) {
10532   EXPECT_EQ("/* Гляжу,\n"
10533             " * поднимается\n"
10534             " * медленно в\n"
10535             " * гору\n"
10536             " * Лошадка,\n"
10537             " * везущая\n"
10538             " * хворосту\n"
10539             " * воз. */",
10540             format("/* Гляжу, поднимается медленно в гору\n"
10541                    " * Лошадка, везущая хворосту воз. */",
10542                    getLLVMStyleWithColumns(13)));
10543   EXPECT_EQ(
10544       "/* 一二三\n"
10545       " * 四五六七\n"
10546       " * 八  九\n"
10547       " * 十  */",
10548       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
10549   EXPECT_EQ("/* �������� ��������\n"
10550             " * ��������\n"
10551             " * ������-�� */",
10552             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
10553 }
10554 
10555 #endif // _MSC_VER
10556 
10557 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
10558   FormatStyle Style = getLLVMStyle();
10559 
10560   Style.ConstructorInitializerIndentWidth = 4;
10561   verifyFormat(
10562       "SomeClass::Constructor()\n"
10563       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10564       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10565       Style);
10566 
10567   Style.ConstructorInitializerIndentWidth = 2;
10568   verifyFormat(
10569       "SomeClass::Constructor()\n"
10570       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10571       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10572       Style);
10573 
10574   Style.ConstructorInitializerIndentWidth = 0;
10575   verifyFormat(
10576       "SomeClass::Constructor()\n"
10577       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10578       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10579       Style);
10580   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10581   verifyFormat(
10582       "SomeLongTemplateVariableName<\n"
10583       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
10584       Style);
10585   verifyFormat(
10586       "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
10587       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10588       Style);
10589 }
10590 
10591 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
10592   FormatStyle Style = getLLVMStyle();
10593   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
10594   Style.ConstructorInitializerIndentWidth = 4;
10595   verifyFormat("SomeClass::Constructor()\n"
10596                "    : a(a)\n"
10597                "    , b(b)\n"
10598                "    , c(c) {}",
10599                Style);
10600   verifyFormat("SomeClass::Constructor()\n"
10601                "    : a(a) {}",
10602                Style);
10603 
10604   Style.ColumnLimit = 0;
10605   verifyFormat("SomeClass::Constructor()\n"
10606                "    : a(a) {}",
10607                Style);
10608   verifyFormat("SomeClass::Constructor() noexcept\n"
10609                "    : a(a) {}",
10610                Style);
10611   verifyFormat("SomeClass::Constructor()\n"
10612                "    : a(a)\n"
10613                "    , b(b)\n"
10614                "    , c(c) {}",
10615                Style);
10616   verifyFormat("SomeClass::Constructor()\n"
10617                "    : a(a) {\n"
10618                "  foo();\n"
10619                "  bar();\n"
10620                "}",
10621                Style);
10622 
10623   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10624   verifyFormat("SomeClass::Constructor()\n"
10625                "    : a(a)\n"
10626                "    , b(b)\n"
10627                "    , c(c) {\n}",
10628                Style);
10629   verifyFormat("SomeClass::Constructor()\n"
10630                "    : a(a) {\n}",
10631                Style);
10632 
10633   Style.ColumnLimit = 80;
10634   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
10635   Style.ConstructorInitializerIndentWidth = 2;
10636   verifyFormat("SomeClass::Constructor()\n"
10637                "  : a(a)\n"
10638                "  , b(b)\n"
10639                "  , c(c) {}",
10640                Style);
10641 
10642   Style.ConstructorInitializerIndentWidth = 0;
10643   verifyFormat("SomeClass::Constructor()\n"
10644                ": a(a)\n"
10645                ", b(b)\n"
10646                ", c(c) {}",
10647                Style);
10648 
10649   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
10650   Style.ConstructorInitializerIndentWidth = 4;
10651   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
10652   verifyFormat(
10653       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
10654       Style);
10655   verifyFormat(
10656       "SomeClass::Constructor()\n"
10657       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
10658       Style);
10659   Style.ConstructorInitializerIndentWidth = 4;
10660   Style.ColumnLimit = 60;
10661   verifyFormat("SomeClass::Constructor()\n"
10662                "    : aaaaaaaa(aaaaaaaa)\n"
10663                "    , aaaaaaaa(aaaaaaaa)\n"
10664                "    , aaaaaaaa(aaaaaaaa) {}",
10665                Style);
10666 }
10667 
10668 TEST_F(FormatTest, Destructors) {
10669   verifyFormat("void F(int &i) { i.~int(); }");
10670   verifyFormat("void F(int &i) { i->~int(); }");
10671 }
10672 
10673 TEST_F(FormatTest, FormatsWithWebKitStyle) {
10674   FormatStyle Style = getWebKitStyle();
10675 
10676   // Don't indent in outer namespaces.
10677   verifyFormat("namespace outer {\n"
10678                "int i;\n"
10679                "namespace inner {\n"
10680                "    int i;\n"
10681                "} // namespace inner\n"
10682                "} // namespace outer\n"
10683                "namespace other_outer {\n"
10684                "int i;\n"
10685                "}",
10686                Style);
10687 
10688   // Don't indent case labels.
10689   verifyFormat("switch (variable) {\n"
10690                "case 1:\n"
10691                "case 2:\n"
10692                "    doSomething();\n"
10693                "    break;\n"
10694                "default:\n"
10695                "    ++variable;\n"
10696                "}",
10697                Style);
10698 
10699   // Wrap before binary operators.
10700   EXPECT_EQ("void f()\n"
10701             "{\n"
10702             "    if (aaaaaaaaaaaaaaaa\n"
10703             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
10704             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10705             "        return;\n"
10706             "}",
10707             format("void f() {\n"
10708                    "if (aaaaaaaaaaaaaaaa\n"
10709                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
10710                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10711                    "return;\n"
10712                    "}",
10713                    Style));
10714 
10715   // Allow functions on a single line.
10716   verifyFormat("void f() { return; }", Style);
10717 
10718   // Constructor initializers are formatted one per line with the "," on the
10719   // new line.
10720   verifyFormat("Constructor()\n"
10721                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10722                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
10723                "          aaaaaaaaaaaaaa)\n"
10724                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
10725                "{\n"
10726                "}",
10727                Style);
10728   verifyFormat("SomeClass::Constructor()\n"
10729                "    : a(a)\n"
10730                "{\n"
10731                "}",
10732                Style);
10733   EXPECT_EQ("SomeClass::Constructor()\n"
10734             "    : a(a)\n"
10735             "{\n"
10736             "}",
10737             format("SomeClass::Constructor():a(a){}", Style));
10738   verifyFormat("SomeClass::Constructor()\n"
10739                "    : a(a)\n"
10740                "    , b(b)\n"
10741                "    , c(c)\n"
10742                "{\n"
10743                "}",
10744                Style);
10745   verifyFormat("SomeClass::Constructor()\n"
10746                "    : a(a)\n"
10747                "{\n"
10748                "    foo();\n"
10749                "    bar();\n"
10750                "}",
10751                Style);
10752 
10753   // Access specifiers should be aligned left.
10754   verifyFormat("class C {\n"
10755                "public:\n"
10756                "    int i;\n"
10757                "};",
10758                Style);
10759 
10760   // Do not align comments.
10761   verifyFormat("int a; // Do not\n"
10762                "double b; // align comments.",
10763                Style);
10764 
10765   // Do not align operands.
10766   EXPECT_EQ("ASSERT(aaaa\n"
10767             "    || bbbb);",
10768             format("ASSERT ( aaaa\n||bbbb);", Style));
10769 
10770   // Accept input's line breaks.
10771   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
10772             "    || bbbbbbbbbbbbbbb) {\n"
10773             "    i++;\n"
10774             "}",
10775             format("if (aaaaaaaaaaaaaaa\n"
10776                    "|| bbbbbbbbbbbbbbb) { i++; }",
10777                    Style));
10778   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
10779             "    i++;\n"
10780             "}",
10781             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
10782 
10783   // Don't automatically break all macro definitions (llvm.org/PR17842).
10784   verifyFormat("#define aNumber 10", Style);
10785   // However, generally keep the line breaks that the user authored.
10786   EXPECT_EQ("#define aNumber \\\n"
10787             "    10",
10788             format("#define aNumber \\\n"
10789                    " 10",
10790                    Style));
10791 
10792   // Keep empty and one-element array literals on a single line.
10793   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
10794             "                                  copyItems:YES];",
10795             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
10796                    "copyItems:YES];",
10797                    Style));
10798   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
10799             "                                  copyItems:YES];",
10800             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
10801                    "             copyItems:YES];",
10802                    Style));
10803   // FIXME: This does not seem right, there should be more indentation before
10804   // the array literal's entries. Nested blocks have the same problem.
10805   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10806             "    @\"a\",\n"
10807             "    @\"a\"\n"
10808             "]\n"
10809             "                                  copyItems:YES];",
10810             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10811                    "     @\"a\",\n"
10812                    "     @\"a\"\n"
10813                    "     ]\n"
10814                    "       copyItems:YES];",
10815                    Style));
10816   EXPECT_EQ(
10817       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10818       "                                  copyItems:YES];",
10819       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10820              "   copyItems:YES];",
10821              Style));
10822 
10823   verifyFormat("[self.a b:c c:d];", Style);
10824   EXPECT_EQ("[self.a b:c\n"
10825             "        c:d];",
10826             format("[self.a b:c\n"
10827                    "c:d];",
10828                    Style));
10829 }
10830 
10831 TEST_F(FormatTest, FormatsLambdas) {
10832   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
10833   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
10834   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
10835   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
10836   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
10837   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
10838   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
10839   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
10840   verifyFormat("int x = f(*+[] {});");
10841   verifyFormat("void f() {\n"
10842                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
10843                "}\n");
10844   verifyFormat("void f() {\n"
10845                "  other(x.begin(), //\n"
10846                "        x.end(),   //\n"
10847                "        [&](int, int) { return 1; });\n"
10848                "}\n");
10849   verifyFormat("SomeFunction([]() { // A cool function...\n"
10850                "  return 43;\n"
10851                "});");
10852   EXPECT_EQ("SomeFunction([]() {\n"
10853             "#define A a\n"
10854             "  return 43;\n"
10855             "});",
10856             format("SomeFunction([](){\n"
10857                    "#define A a\n"
10858                    "return 43;\n"
10859                    "});"));
10860   verifyFormat("void f() {\n"
10861                "  SomeFunction([](decltype(x), A *a) {});\n"
10862                "}");
10863   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10864                "    [](const aaaaaaaaaa &a) { return a; });");
10865   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
10866                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
10867                "});");
10868   verifyFormat("Constructor()\n"
10869                "    : Field([] { // comment\n"
10870                "        int i;\n"
10871                "      }) {}");
10872   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
10873                "  return some_parameter.size();\n"
10874                "};");
10875   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
10876                "    [](const string &s) { return s; };");
10877   verifyFormat("int i = aaaaaa ? 1 //\n"
10878                "               : [] {\n"
10879                "                   return 2; //\n"
10880                "                 }();");
10881   verifyFormat("llvm::errs() << \"number of twos is \"\n"
10882                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
10883                "                  return x == 2; // force break\n"
10884                "                });");
10885   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10886                "    [=](int iiiiiiiiiiii) {\n"
10887                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
10888                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
10889                "    });",
10890                getLLVMStyleWithColumns(60));
10891   verifyFormat("SomeFunction({[&] {\n"
10892                "                // comment\n"
10893                "              },\n"
10894                "              [&] {\n"
10895                "                // comment\n"
10896                "              }});");
10897   verifyFormat("SomeFunction({[&] {\n"
10898                "  // comment\n"
10899                "}});");
10900   verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
10901                "                             [&]() { return true; },\n"
10902                "                         aaaaa aaaaaaaaa);");
10903 
10904   // Lambdas with return types.
10905   verifyFormat("int c = []() -> int { return 2; }();\n");
10906   verifyFormat("int c = []() -> int * { return 2; }();\n");
10907   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
10908   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
10909   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
10910   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
10911   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
10912   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
10913   verifyFormat("[a, a]() -> a<1> {};");
10914   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
10915                "                   int j) -> int {\n"
10916                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
10917                "};");
10918   verifyFormat(
10919       "aaaaaaaaaaaaaaaaaaaaaa(\n"
10920       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
10921       "      return aaaaaaaaaaaaaaaaa;\n"
10922       "    });",
10923       getLLVMStyleWithColumns(70));
10924   verifyFormat("[]() //\n"
10925                "    -> int {\n"
10926                "  return 1; //\n"
10927                "};");
10928 
10929   // Multiple lambdas in the same parentheses change indentation rules.
10930   verifyFormat("SomeFunction(\n"
10931                "    []() {\n"
10932                "      int i = 42;\n"
10933                "      return i;\n"
10934                "    },\n"
10935                "    []() {\n"
10936                "      int j = 43;\n"
10937                "      return j;\n"
10938                "    });");
10939 
10940   // More complex introducers.
10941   verifyFormat("return [i, args...] {};");
10942 
10943   // Not lambdas.
10944   verifyFormat("constexpr char hello[]{\"hello\"};");
10945   verifyFormat("double &operator[](int i) { return 0; }\n"
10946                "int i;");
10947   verifyFormat("std::unique_ptr<int[]> foo() {}");
10948   verifyFormat("int i = a[a][a]->f();");
10949   verifyFormat("int i = (*b)[a]->f();");
10950 
10951   // Other corner cases.
10952   verifyFormat("void f() {\n"
10953                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
10954                "  );\n"
10955                "}");
10956 
10957   // Lambdas created through weird macros.
10958   verifyFormat("void f() {\n"
10959                "  MACRO((const AA &a) { return 1; });\n"
10960                "  MACRO((AA &a) { return 1; });\n"
10961                "}");
10962 
10963   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
10964                "      doo_dah();\n"
10965                "      doo_dah();\n"
10966                "    })) {\n"
10967                "}");
10968   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
10969                "                doo_dah();\n"
10970                "                doo_dah();\n"
10971                "              })) {\n"
10972                "}");
10973   verifyFormat("auto lambda = []() {\n"
10974                "  int a = 2\n"
10975                "#if A\n"
10976                "          + 2\n"
10977                "#endif\n"
10978                "      ;\n"
10979                "};");
10980 
10981   // Lambdas with complex multiline introducers.
10982   verifyFormat(
10983       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10984       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
10985       "        -> ::std::unordered_set<\n"
10986       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
10987       "      //\n"
10988       "    });");
10989 }
10990 
10991 TEST_F(FormatTest, FormatsBlocks) {
10992   FormatStyle ShortBlocks = getLLVMStyle();
10993   ShortBlocks.AllowShortBlocksOnASingleLine = true;
10994   verifyFormat("int (^Block)(int, int);", ShortBlocks);
10995   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
10996   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
10997   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
10998   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
10999   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
11000 
11001   verifyFormat("foo(^{ bar(); });", ShortBlocks);
11002   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
11003   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
11004 
11005   verifyFormat("[operation setCompletionBlock:^{\n"
11006                "  [self onOperationDone];\n"
11007                "}];");
11008   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
11009                "  [self onOperationDone];\n"
11010                "}]};");
11011   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
11012                "  f();\n"
11013                "}];");
11014   verifyFormat("int a = [operation block:^int(int *i) {\n"
11015                "  return 1;\n"
11016                "}];");
11017   verifyFormat("[myObject doSomethingWith:arg1\n"
11018                "                      aaa:^int(int *a) {\n"
11019                "                        return 1;\n"
11020                "                      }\n"
11021                "                      bbb:f(a * bbbbbbbb)];");
11022 
11023   verifyFormat("[operation setCompletionBlock:^{\n"
11024                "  [self.delegate newDataAvailable];\n"
11025                "}];",
11026                getLLVMStyleWithColumns(60));
11027   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
11028                "  NSString *path = [self sessionFilePath];\n"
11029                "  if (path) {\n"
11030                "    // ...\n"
11031                "  }\n"
11032                "});");
11033   verifyFormat("[[SessionService sharedService]\n"
11034                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11035                "      if (window) {\n"
11036                "        [self windowDidLoad:window];\n"
11037                "      } else {\n"
11038                "        [self errorLoadingWindow];\n"
11039                "      }\n"
11040                "    }];");
11041   verifyFormat("void (^largeBlock)(void) = ^{\n"
11042                "  // ...\n"
11043                "};\n",
11044                getLLVMStyleWithColumns(40));
11045   verifyFormat("[[SessionService sharedService]\n"
11046                "    loadWindowWithCompletionBlock: //\n"
11047                "        ^(SessionWindow *window) {\n"
11048                "          if (window) {\n"
11049                "            [self windowDidLoad:window];\n"
11050                "          } else {\n"
11051                "            [self errorLoadingWindow];\n"
11052                "          }\n"
11053                "        }];",
11054                getLLVMStyleWithColumns(60));
11055   verifyFormat("[myObject doSomethingWith:arg1\n"
11056                "    firstBlock:^(Foo *a) {\n"
11057                "      // ...\n"
11058                "      int i;\n"
11059                "    }\n"
11060                "    secondBlock:^(Bar *b) {\n"
11061                "      // ...\n"
11062                "      int i;\n"
11063                "    }\n"
11064                "    thirdBlock:^Foo(Bar *b) {\n"
11065                "      // ...\n"
11066                "      int i;\n"
11067                "    }];");
11068   verifyFormat("[myObject doSomethingWith:arg1\n"
11069                "               firstBlock:-1\n"
11070                "              secondBlock:^(Bar *b) {\n"
11071                "                // ...\n"
11072                "                int i;\n"
11073                "              }];");
11074 
11075   verifyFormat("f(^{\n"
11076                "  @autoreleasepool {\n"
11077                "    if (a) {\n"
11078                "      g();\n"
11079                "    }\n"
11080                "  }\n"
11081                "});");
11082   verifyFormat("Block b = ^int *(A *a, B *b) {}");
11083   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
11084                "};");
11085 
11086   FormatStyle FourIndent = getLLVMStyle();
11087   FourIndent.ObjCBlockIndentWidth = 4;
11088   verifyFormat("[operation setCompletionBlock:^{\n"
11089                "    [self onOperationDone];\n"
11090                "}];",
11091                FourIndent);
11092 }
11093 
11094 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
11095   FormatStyle ZeroColumn = getLLVMStyle();
11096   ZeroColumn.ColumnLimit = 0;
11097 
11098   verifyFormat("[[SessionService sharedService] "
11099                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11100                "  if (window) {\n"
11101                "    [self windowDidLoad:window];\n"
11102                "  } else {\n"
11103                "    [self errorLoadingWindow];\n"
11104                "  }\n"
11105                "}];",
11106                ZeroColumn);
11107   EXPECT_EQ("[[SessionService sharedService]\n"
11108             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11109             "      if (window) {\n"
11110             "        [self windowDidLoad:window];\n"
11111             "      } else {\n"
11112             "        [self errorLoadingWindow];\n"
11113             "      }\n"
11114             "    }];",
11115             format("[[SessionService sharedService]\n"
11116                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11117                    "                if (window) {\n"
11118                    "    [self windowDidLoad:window];\n"
11119                    "  } else {\n"
11120                    "    [self errorLoadingWindow];\n"
11121                    "  }\n"
11122                    "}];",
11123                    ZeroColumn));
11124   verifyFormat("[myObject doSomethingWith:arg1\n"
11125                "    firstBlock:^(Foo *a) {\n"
11126                "      // ...\n"
11127                "      int i;\n"
11128                "    }\n"
11129                "    secondBlock:^(Bar *b) {\n"
11130                "      // ...\n"
11131                "      int i;\n"
11132                "    }\n"
11133                "    thirdBlock:^Foo(Bar *b) {\n"
11134                "      // ...\n"
11135                "      int i;\n"
11136                "    }];",
11137                ZeroColumn);
11138   verifyFormat("f(^{\n"
11139                "  @autoreleasepool {\n"
11140                "    if (a) {\n"
11141                "      g();\n"
11142                "    }\n"
11143                "  }\n"
11144                "});",
11145                ZeroColumn);
11146   verifyFormat("void (^largeBlock)(void) = ^{\n"
11147                "  // ...\n"
11148                "};",
11149                ZeroColumn);
11150 
11151   ZeroColumn.AllowShortBlocksOnASingleLine = true;
11152   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
11153             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
11154   ZeroColumn.AllowShortBlocksOnASingleLine = false;
11155   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
11156             "  int i;\n"
11157             "};",
11158             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
11159 }
11160 
11161 TEST_F(FormatTest, SupportsCRLF) {
11162   EXPECT_EQ("int a;\r\n"
11163             "int b;\r\n"
11164             "int c;\r\n",
11165             format("int a;\r\n"
11166                    "  int b;\r\n"
11167                    "    int c;\r\n",
11168                    getLLVMStyle()));
11169   EXPECT_EQ("int a;\r\n"
11170             "int b;\r\n"
11171             "int c;\r\n",
11172             format("int a;\r\n"
11173                    "  int b;\n"
11174                    "    int c;\r\n",
11175                    getLLVMStyle()));
11176   EXPECT_EQ("int a;\n"
11177             "int b;\n"
11178             "int c;\n",
11179             format("int a;\r\n"
11180                    "  int b;\n"
11181                    "    int c;\n",
11182                    getLLVMStyle()));
11183   EXPECT_EQ("\"aaaaaaa \"\r\n"
11184             "\"bbbbbbb\";\r\n",
11185             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
11186   EXPECT_EQ("#define A \\\r\n"
11187             "  b;      \\\r\n"
11188             "  c;      \\\r\n"
11189             "  d;\r\n",
11190             format("#define A \\\r\n"
11191                    "  b; \\\r\n"
11192                    "  c; d; \r\n",
11193                    getGoogleStyle()));
11194 
11195   EXPECT_EQ("/*\r\n"
11196             "multi line block comments\r\n"
11197             "should not introduce\r\n"
11198             "an extra carriage return\r\n"
11199             "*/\r\n",
11200             format("/*\r\n"
11201                    "multi line block comments\r\n"
11202                    "should not introduce\r\n"
11203                    "an extra carriage return\r\n"
11204                    "*/\r\n"));
11205 }
11206 
11207 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
11208   verifyFormat("MY_CLASS(C) {\n"
11209                "  int i;\n"
11210                "  int j;\n"
11211                "};");
11212 }
11213 
11214 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
11215   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
11216   TwoIndent.ContinuationIndentWidth = 2;
11217 
11218   EXPECT_EQ("int i =\n"
11219             "  longFunction(\n"
11220             "    arg);",
11221             format("int i = longFunction(arg);", TwoIndent));
11222 
11223   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
11224   SixIndent.ContinuationIndentWidth = 6;
11225 
11226   EXPECT_EQ("int i =\n"
11227             "      longFunction(\n"
11228             "            arg);",
11229             format("int i = longFunction(arg);", SixIndent));
11230 }
11231 
11232 TEST_F(FormatTest, SpacesInAngles) {
11233   FormatStyle Spaces = getLLVMStyle();
11234   Spaces.SpacesInAngles = true;
11235 
11236   verifyFormat("static_cast< int >(arg);", Spaces);
11237   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
11238   verifyFormat("f< int, float >();", Spaces);
11239   verifyFormat("template <> g() {}", Spaces);
11240   verifyFormat("template < std::vector< int > > f() {}", Spaces);
11241   verifyFormat("std::function< void(int, int) > fct;", Spaces);
11242   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
11243                Spaces);
11244 
11245   Spaces.Standard = FormatStyle::LS_Cpp03;
11246   Spaces.SpacesInAngles = true;
11247   verifyFormat("A< A< int > >();", Spaces);
11248 
11249   Spaces.SpacesInAngles = false;
11250   verifyFormat("A<A<int> >();", Spaces);
11251 
11252   Spaces.Standard = FormatStyle::LS_Cpp11;
11253   Spaces.SpacesInAngles = true;
11254   verifyFormat("A< A< int > >();", Spaces);
11255 
11256   Spaces.SpacesInAngles = false;
11257   verifyFormat("A<A<int>>();", Spaces);
11258 }
11259 
11260 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
11261   FormatStyle Style = getLLVMStyle();
11262   Style.SpaceAfterTemplateKeyword = false;
11263   verifyFormat("template<int> void foo();", Style);
11264 }
11265 
11266 TEST_F(FormatTest, TripleAngleBrackets) {
11267   verifyFormat("f<<<1, 1>>>();");
11268   verifyFormat("f<<<1, 1, 1, s>>>();");
11269   verifyFormat("f<<<a, b, c, d>>>();");
11270   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
11271   verifyFormat("f<param><<<1, 1>>>();");
11272   verifyFormat("f<1><<<1, 1>>>();");
11273   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
11274   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11275                "aaaaaaaaaaa<<<\n    1, 1>>>();");
11276   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
11277                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
11278 }
11279 
11280 TEST_F(FormatTest, MergeLessLessAtEnd) {
11281   verifyFormat("<<");
11282   EXPECT_EQ("< < <", format("\\\n<<<"));
11283   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11284                "aaallvm::outs() <<");
11285   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11286                "aaaallvm::outs()\n    <<");
11287 }
11288 
11289 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
11290   std::string code = "#if A\n"
11291                      "#if B\n"
11292                      "a.\n"
11293                      "#endif\n"
11294                      "    a = 1;\n"
11295                      "#else\n"
11296                      "#endif\n"
11297                      "#if C\n"
11298                      "#else\n"
11299                      "#endif\n";
11300   EXPECT_EQ(code, format(code));
11301 }
11302 
11303 TEST_F(FormatTest, HandleConflictMarkers) {
11304   // Git/SVN conflict markers.
11305   EXPECT_EQ("int a;\n"
11306             "void f() {\n"
11307             "  callme(some(parameter1,\n"
11308             "<<<<<<< text by the vcs\n"
11309             "              parameter2),\n"
11310             "||||||| text by the vcs\n"
11311             "              parameter2),\n"
11312             "         parameter3,\n"
11313             "======= text by the vcs\n"
11314             "              parameter2, parameter3),\n"
11315             ">>>>>>> text by the vcs\n"
11316             "         otherparameter);\n",
11317             format("int a;\n"
11318                    "void f() {\n"
11319                    "  callme(some(parameter1,\n"
11320                    "<<<<<<< text by the vcs\n"
11321                    "  parameter2),\n"
11322                    "||||||| text by the vcs\n"
11323                    "  parameter2),\n"
11324                    "  parameter3,\n"
11325                    "======= text by the vcs\n"
11326                    "  parameter2,\n"
11327                    "  parameter3),\n"
11328                    ">>>>>>> text by the vcs\n"
11329                    "  otherparameter);\n"));
11330 
11331   // Perforce markers.
11332   EXPECT_EQ("void f() {\n"
11333             "  function(\n"
11334             ">>>> text by the vcs\n"
11335             "      parameter,\n"
11336             "==== text by the vcs\n"
11337             "      parameter,\n"
11338             "==== text by the vcs\n"
11339             "      parameter,\n"
11340             "<<<< text by the vcs\n"
11341             "      parameter);\n",
11342             format("void f() {\n"
11343                    "  function(\n"
11344                    ">>>> text by the vcs\n"
11345                    "  parameter,\n"
11346                    "==== text by the vcs\n"
11347                    "  parameter,\n"
11348                    "==== text by the vcs\n"
11349                    "  parameter,\n"
11350                    "<<<< text by the vcs\n"
11351                    "  parameter);\n"));
11352 
11353   EXPECT_EQ("<<<<<<<\n"
11354             "|||||||\n"
11355             "=======\n"
11356             ">>>>>>>",
11357             format("<<<<<<<\n"
11358                    "|||||||\n"
11359                    "=======\n"
11360                    ">>>>>>>"));
11361 
11362   EXPECT_EQ("<<<<<<<\n"
11363             "|||||||\n"
11364             "int i;\n"
11365             "=======\n"
11366             ">>>>>>>",
11367             format("<<<<<<<\n"
11368                    "|||||||\n"
11369                    "int i;\n"
11370                    "=======\n"
11371                    ">>>>>>>"));
11372 
11373   // FIXME: Handle parsing of macros around conflict markers correctly:
11374   EXPECT_EQ("#define Macro \\\n"
11375             "<<<<<<<\n"
11376             "Something \\\n"
11377             "|||||||\n"
11378             "Else \\\n"
11379             "=======\n"
11380             "Other \\\n"
11381             ">>>>>>>\n"
11382             "    End int i;\n",
11383             format("#define Macro \\\n"
11384                    "<<<<<<<\n"
11385                    "  Something \\\n"
11386                    "|||||||\n"
11387                    "  Else \\\n"
11388                    "=======\n"
11389                    "  Other \\\n"
11390                    ">>>>>>>\n"
11391                    "  End\n"
11392                    "int i;\n"));
11393 }
11394 
11395 TEST_F(FormatTest, DisableRegions) {
11396   EXPECT_EQ("int i;\n"
11397             "// clang-format off\n"
11398             "  int j;\n"
11399             "// clang-format on\n"
11400             "int k;",
11401             format(" int  i;\n"
11402                    "   // clang-format off\n"
11403                    "  int j;\n"
11404                    " // clang-format on\n"
11405                    "   int   k;"));
11406   EXPECT_EQ("int i;\n"
11407             "/* clang-format off */\n"
11408             "  int j;\n"
11409             "/* clang-format on */\n"
11410             "int k;",
11411             format(" int  i;\n"
11412                    "   /* clang-format off */\n"
11413                    "  int j;\n"
11414                    " /* clang-format on */\n"
11415                    "   int   k;"));
11416 
11417   // Don't reflow comments within disabled regions.
11418   EXPECT_EQ(
11419       "// clang-format off\n"
11420       "// long long long long long long line\n"
11421       "/* clang-format on */\n"
11422       "/* long long long\n"
11423       " * long long long\n"
11424       " * line */\n"
11425       "int i;\n"
11426       "/* clang-format off */\n"
11427       "/* long long long long long long line */\n",
11428       format("// clang-format off\n"
11429              "// long long long long long long line\n"
11430              "/* clang-format on */\n"
11431              "/* long long long long long long line */\n"
11432              "int i;\n"
11433              "/* clang-format off */\n"
11434              "/* long long long long long long line */\n",
11435              getLLVMStyleWithColumns(20)));
11436 }
11437 
11438 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
11439   format("? ) =");
11440   verifyNoCrash("#define a\\\n /**/}");
11441 }
11442 
11443 TEST_F(FormatTest, FormatsTableGenCode) {
11444   FormatStyle Style = getLLVMStyle();
11445   Style.Language = FormatStyle::LK_TableGen;
11446   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
11447 }
11448 
11449 TEST_F(FormatTest, ArrayOfTemplates) {
11450   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
11451             format("auto a = new unique_ptr<int > [ 10];"));
11452 
11453   FormatStyle Spaces = getLLVMStyle();
11454   Spaces.SpacesInSquareBrackets = true;
11455   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
11456             format("auto a = new unique_ptr<int > [10];", Spaces));
11457 }
11458 
11459 TEST_F(FormatTest, ArrayAsTemplateType) {
11460   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
11461             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
11462 
11463   FormatStyle Spaces = getLLVMStyle();
11464   Spaces.SpacesInSquareBrackets = true;
11465   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
11466             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
11467 }
11468 
11469 TEST_F(FormatTest, NoSpaceAfterSuper) {
11470     verifyFormat("__super::FooBar();");
11471 }
11472 
11473 TEST(FormatStyle, GetStyleOfFile) {
11474   vfs::InMemoryFileSystem FS;
11475   // Test 1: format file in the same directory.
11476   ASSERT_TRUE(
11477       FS.addFile("/a/.clang-format", 0,
11478                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
11479   ASSERT_TRUE(
11480       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
11481   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
11482   ASSERT_TRUE((bool)Style1);
11483   ASSERT_EQ(*Style1, getLLVMStyle());
11484 
11485   // Test 2.1: fallback to default.
11486   ASSERT_TRUE(
11487       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
11488   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
11489   ASSERT_TRUE((bool)Style2);
11490   ASSERT_EQ(*Style2, getMozillaStyle());
11491 
11492   // Test 2.2: no format on 'none' fallback style.
11493   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
11494   ASSERT_TRUE((bool)Style2);
11495   ASSERT_EQ(*Style2, getNoStyle());
11496 
11497   // Test 2.3: format if config is found with no based style while fallback is
11498   // 'none'.
11499   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
11500                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
11501   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
11502   ASSERT_TRUE((bool)Style2);
11503   ASSERT_EQ(*Style2, getLLVMStyle());
11504 
11505   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
11506   Style2 = getStyle("{}", "a.h", "none", "", &FS);
11507   ASSERT_TRUE((bool)Style2);
11508   ASSERT_EQ(*Style2, getLLVMStyle());
11509 
11510   // Test 3: format file in parent directory.
11511   ASSERT_TRUE(
11512       FS.addFile("/c/.clang-format", 0,
11513                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
11514   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
11515                          llvm::MemoryBuffer::getMemBuffer("int i;")));
11516   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
11517   ASSERT_TRUE((bool)Style3);
11518   ASSERT_EQ(*Style3, getGoogleStyle());
11519 
11520   // Test 4: error on invalid fallback style
11521   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
11522   ASSERT_FALSE((bool)Style4);
11523   llvm::consumeError(Style4.takeError());
11524 
11525   // Test 5: error on invalid yaml on command line
11526   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
11527   ASSERT_FALSE((bool)Style5);
11528   llvm::consumeError(Style5.takeError());
11529 
11530   // Test 6: error on invalid style
11531   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
11532   ASSERT_FALSE((bool)Style6);
11533   llvm::consumeError(Style6.takeError());
11534 
11535   // Test 7: found config file, error on parsing it
11536   ASSERT_TRUE(
11537       FS.addFile("/d/.clang-format", 0,
11538                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
11539                                                   "InvalidKey: InvalidValue")));
11540   ASSERT_TRUE(
11541       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
11542   auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
11543   ASSERT_FALSE((bool)Style7);
11544   llvm::consumeError(Style7.takeError());
11545 }
11546 
11547 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
11548   // Column limit is 20.
11549   std::string Code = "Type *a =\n"
11550                      "    new Type();\n"
11551                      "g(iiiii, 0, jjjjj,\n"
11552                      "  0, kkkkk, 0, mm);\n"
11553                      "int  bad     = format   ;";
11554   std::string Expected = "auto a = new Type();\n"
11555                          "g(iiiii, nullptr,\n"
11556                          "  jjjjj, nullptr,\n"
11557                          "  kkkkk, nullptr,\n"
11558                          "  mm);\n"
11559                          "int  bad     = format   ;";
11560   FileID ID = Context.createInMemoryFile("format.cpp", Code);
11561   tooling::Replacements Replaces = toReplacements(
11562       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
11563                             "auto "),
11564        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
11565                             "nullptr"),
11566        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
11567                             "nullptr"),
11568        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
11569                             "nullptr")});
11570 
11571   format::FormatStyle Style = format::getLLVMStyle();
11572   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
11573   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
11574   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
11575       << llvm::toString(FormattedReplaces.takeError()) << "\n";
11576   auto Result = applyAllReplacements(Code, *FormattedReplaces);
11577   EXPECT_TRUE(static_cast<bool>(Result));
11578   EXPECT_EQ(Expected, *Result);
11579 }
11580 
11581 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
11582   std::string Code = "#include \"a.h\"\n"
11583                      "#include \"c.h\"\n"
11584                      "\n"
11585                      "int main() {\n"
11586                      "  return 0;\n"
11587                      "}";
11588   std::string Expected = "#include \"a.h\"\n"
11589                          "#include \"b.h\"\n"
11590                          "#include \"c.h\"\n"
11591                          "\n"
11592                          "int main() {\n"
11593                          "  return 0;\n"
11594                          "}";
11595   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
11596   tooling::Replacements Replaces = toReplacements(
11597       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
11598                             "#include \"b.h\"\n")});
11599 
11600   format::FormatStyle Style = format::getLLVMStyle();
11601   Style.SortIncludes = true;
11602   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
11603   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
11604       << llvm::toString(FormattedReplaces.takeError()) << "\n";
11605   auto Result = applyAllReplacements(Code, *FormattedReplaces);
11606   EXPECT_TRUE(static_cast<bool>(Result));
11607   EXPECT_EQ(Expected, *Result);
11608 }
11609 
11610 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
11611   EXPECT_EQ("using std::cin;\n"
11612             "using std::cout;",
11613             format("using std::cout;\n"
11614                    "using std::cin;", getGoogleStyle()));
11615 }
11616 
11617 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
11618   format::FormatStyle Style = format::getLLVMStyle();
11619   Style.Standard = FormatStyle::LS_Cpp03;
11620   // cpp03 recognize this string as identifier u8 and literal character 'a'
11621   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
11622 }
11623 
11624 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
11625   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
11626   // all modes, including C++11, C++14 and C++17
11627   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
11628 }
11629 
11630 TEST_F(FormatTest, DoNotFormatLikelyXml) {
11631   EXPECT_EQ("<!-- ;> -->",
11632             format("<!-- ;> -->", getGoogleStyle()));
11633   EXPECT_EQ(" <!-- >; -->",
11634             format(" <!-- >; -->", getGoogleStyle()));
11635 }
11636 
11637 TEST_F(FormatTest, StructuredBindings) {
11638   // Structured bindings is a C++17 feature.
11639   // all modes, including C++11, C++14 and C++17
11640   verifyFormat("auto [a, b] = f();");
11641   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
11642   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
11643   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
11644   EXPECT_EQ("auto const volatile [a, b] = f();",
11645             format("auto  const   volatile[a, b] = f();"));
11646   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
11647   EXPECT_EQ("auto &[a, b, c] = f();",
11648             format("auto   &[  a  ,  b,c   ] = f();"));
11649   EXPECT_EQ("auto &&[a, b, c] = f();",
11650             format("auto   &&[  a  ,  b,c   ] = f();"));
11651   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
11652   EXPECT_EQ("auto const volatile &&[a, b] = f();",
11653             format("auto  const  volatile  &&[a, b] = f();"));
11654   EXPECT_EQ("auto const &&[a, b] = f();", format("auto  const   &&  [a, b] = f();"));
11655   EXPECT_EQ("const auto &[a, b] = f();", format("const  auto  &  [a, b] = f();"));
11656   EXPECT_EQ("const auto volatile &&[a, b] = f();",
11657             format("const  auto   volatile  &&[a, b] = f();"));
11658   EXPECT_EQ("volatile const auto &&[a, b] = f();",
11659             format("volatile  const  auto   &&[a, b] = f();"));
11660   EXPECT_EQ("const auto &&[a, b] = f();", format("const  auto  &&  [a, b] = f();"));
11661 
11662   // Make sure we don't mistake structured bindings for lambdas.
11663   FormatStyle PointerMiddle = getLLVMStyle();
11664   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
11665   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
11666   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
11667   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
11668   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
11669   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
11670   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
11671   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
11672   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
11673   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
11674   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
11675   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
11676   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
11677 
11678   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
11679             format("for (const auto   &&   [a, b] : some_range) {\n}"));
11680   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
11681             format("for (const auto   &   [a, b] : some_range) {\n}"));
11682   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
11683             format("for (const auto[a, b] : some_range) {\n}"));
11684   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
11685   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
11686   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
11687   EXPECT_EQ("auto const &[x, y](expr);", format("auto  const  &  [x,y]  (expr);"));
11688   EXPECT_EQ("auto const &&[x, y](expr);", format("auto  const  &&  [x,y]  (expr);"));
11689   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
11690   EXPECT_EQ("auto const &[x, y]{expr};", format("auto  const  &  [x,y]  {expr};"));
11691   EXPECT_EQ("auto const &&[x, y]{expr};", format("auto  const  &&  [x,y]  {expr};"));
11692 
11693   format::FormatStyle Spaces = format::getLLVMStyle();
11694   Spaces.SpacesInSquareBrackets = true;
11695   verifyFormat("auto [ a, b ] = f();", Spaces);
11696   verifyFormat("auto &&[ a, b ] = f();", Spaces);
11697   verifyFormat("auto &[ a, b ] = f();", Spaces);
11698   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
11699   verifyFormat("auto const &[ a, b ] = f();", Spaces);
11700 }
11701 
11702 } // end namespace
11703 } // end namespace format
11704 } // end namespace clang
11705