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 //===----------------------------------------------------------------------===//
337 // Tests for control statements.
338 //===----------------------------------------------------------------------===//
339 
340 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
341   verifyFormat("if (true)\n  f();\ng();");
342   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
343   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
344 
345   FormatStyle AllowsMergedIf = getLLVMStyle();
346   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
347   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
348   verifyFormat("if (a)\n"
349                "  // comment\n"
350                "  f();",
351                AllowsMergedIf);
352   verifyFormat("{\n"
353                "  if (a)\n"
354                "  label:\n"
355                "    f();\n"
356                "}",
357                AllowsMergedIf);
358   verifyFormat("#define A \\\n"
359                "  if (a)  \\\n"
360                "  label:  \\\n"
361                "    f()",
362                AllowsMergedIf);
363   verifyFormat("if (a)\n"
364                "  ;",
365                AllowsMergedIf);
366   verifyFormat("if (a)\n"
367                "  if (b) return;",
368                AllowsMergedIf);
369 
370   verifyFormat("if (a) // Can't merge this\n"
371                "  f();\n",
372                AllowsMergedIf);
373   verifyFormat("if (a) /* still don't merge */\n"
374                "  f();",
375                AllowsMergedIf);
376   verifyFormat("if (a) { // Never merge this\n"
377                "  f();\n"
378                "}",
379                AllowsMergedIf);
380   verifyFormat("if (a) { /* Never merge this */\n"
381                "  f();\n"
382                "}",
383                AllowsMergedIf);
384 
385   AllowsMergedIf.ColumnLimit = 14;
386   verifyFormat("if (a) return;", AllowsMergedIf);
387   verifyFormat("if (aaaaaaaaa)\n"
388                "  return;",
389                AllowsMergedIf);
390 
391   AllowsMergedIf.ColumnLimit = 13;
392   verifyFormat("if (a)\n  return;", AllowsMergedIf);
393 }
394 
395 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
396   FormatStyle AllowsMergedLoops = getLLVMStyle();
397   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
398   verifyFormat("while (true) continue;", AllowsMergedLoops);
399   verifyFormat("for (;;) continue;", AllowsMergedLoops);
400   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
401   verifyFormat("while (true)\n"
402                "  ;",
403                AllowsMergedLoops);
404   verifyFormat("for (;;)\n"
405                "  ;",
406                AllowsMergedLoops);
407   verifyFormat("for (;;)\n"
408                "  for (;;) continue;",
409                AllowsMergedLoops);
410   verifyFormat("for (;;) // Can't merge this\n"
411                "  continue;",
412                AllowsMergedLoops);
413   verifyFormat("for (;;) /* still don't merge */\n"
414                "  continue;",
415                AllowsMergedLoops);
416 }
417 
418 TEST_F(FormatTest, FormatShortBracedStatements) {
419   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
420   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
421 
422   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
423   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
424 
425   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
426   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
427   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
428   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
429   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
430   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
431   verifyFormat("if (true) { //\n"
432                "  f();\n"
433                "}",
434                AllowSimpleBracedStatements);
435   verifyFormat("if (true) {\n"
436                "  f();\n"
437                "  f();\n"
438                "}",
439                AllowSimpleBracedStatements);
440   verifyFormat("if (true) {\n"
441                "  f();\n"
442                "} else {\n"
443                "  f();\n"
444                "}",
445                AllowSimpleBracedStatements);
446 
447   verifyFormat("template <int> struct A2 {\n"
448                "  struct B {};\n"
449                "};",
450                AllowSimpleBracedStatements);
451 
452   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
453   verifyFormat("if (true) {\n"
454                "  f();\n"
455                "}",
456                AllowSimpleBracedStatements);
457   verifyFormat("if (true) {\n"
458                "  f();\n"
459                "} else {\n"
460                "  f();\n"
461                "}",
462                AllowSimpleBracedStatements);
463 
464   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
465   verifyFormat("while (true) {\n"
466                "  f();\n"
467                "}",
468                AllowSimpleBracedStatements);
469   verifyFormat("for (;;) {\n"
470                "  f();\n"
471                "}",
472                AllowSimpleBracedStatements);
473 }
474 
475 TEST_F(FormatTest, ParseIfElse) {
476   verifyFormat("if (true)\n"
477                "  if (true)\n"
478                "    if (true)\n"
479                "      f();\n"
480                "    else\n"
481                "      g();\n"
482                "  else\n"
483                "    h();\n"
484                "else\n"
485                "  i();");
486   verifyFormat("if (true)\n"
487                "  if (true)\n"
488                "    if (true) {\n"
489                "      if (true)\n"
490                "        f();\n"
491                "    } else {\n"
492                "      g();\n"
493                "    }\n"
494                "  else\n"
495                "    h();\n"
496                "else {\n"
497                "  i();\n"
498                "}");
499   verifyFormat("void f() {\n"
500                "  if (a) {\n"
501                "  } else {\n"
502                "  }\n"
503                "}");
504 }
505 
506 TEST_F(FormatTest, ElseIf) {
507   verifyFormat("if (a) {\n} else if (b) {\n}");
508   verifyFormat("if (a)\n"
509                "  f();\n"
510                "else if (b)\n"
511                "  g();\n"
512                "else\n"
513                "  h();");
514   verifyFormat("if (a) {\n"
515                "  f();\n"
516                "}\n"
517                "// or else ..\n"
518                "else {\n"
519                "  g()\n"
520                "}");
521 
522   verifyFormat("if (a) {\n"
523                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
524                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
525                "}");
526   verifyFormat("if (a) {\n"
527                "} else if (\n"
528                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
529                "}",
530                getLLVMStyleWithColumns(62));
531 }
532 
533 TEST_F(FormatTest, FormatsForLoop) {
534   verifyFormat(
535       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
536       "     ++VeryVeryLongLoopVariable)\n"
537       "  ;");
538   verifyFormat("for (;;)\n"
539                "  f();");
540   verifyFormat("for (;;) {\n}");
541   verifyFormat("for (;;) {\n"
542                "  f();\n"
543                "}");
544   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
545 
546   verifyFormat(
547       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
548       "                                          E = UnwrappedLines.end();\n"
549       "     I != E; ++I) {\n}");
550 
551   verifyFormat(
552       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
553       "     ++IIIII) {\n}");
554   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
555                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
556                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
557   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
558                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
559                "         E = FD->getDeclsInPrototypeScope().end();\n"
560                "     I != E; ++I) {\n}");
561   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
562                "         I = Container.begin(),\n"
563                "         E = Container.end();\n"
564                "     I != E; ++I) {\n}",
565                getLLVMStyleWithColumns(76));
566 
567   verifyFormat(
568       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
569       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
570       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
571       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
572       "     ++aaaaaaaaaaa) {\n}");
573   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
574                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
575                "     ++i) {\n}");
576   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
577                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
578                "}");
579   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
580                "         aaaaaaaaaa);\n"
581                "     iter; ++iter) {\n"
582                "}");
583   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
584                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
585                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
586                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
587 
588   FormatStyle NoBinPacking = getLLVMStyle();
589   NoBinPacking.BinPackParameters = false;
590   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
591                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
592                "                                           aaaaaaaaaaaaaaaa,\n"
593                "                                           aaaaaaaaaaaaaaaa,\n"
594                "                                           aaaaaaaaaaaaaaaa);\n"
595                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
596                "}",
597                NoBinPacking);
598   verifyFormat(
599       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
600       "                                          E = UnwrappedLines.end();\n"
601       "     I != E;\n"
602       "     ++I) {\n}",
603       NoBinPacking);
604 }
605 
606 TEST_F(FormatTest, RangeBasedForLoops) {
607   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
608                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
609   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
610                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
611   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
612                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
613   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
614                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
615 }
616 
617 TEST_F(FormatTest, ForEachLoops) {
618   verifyFormat("void f() {\n"
619                "  foreach (Item *item, itemlist) {}\n"
620                "  Q_FOREACH (Item *item, itemlist) {}\n"
621                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
622                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
623                "}");
624 
625   // As function-like macros.
626   verifyFormat("#define foreach(x, y)\n"
627                "#define Q_FOREACH(x, y)\n"
628                "#define BOOST_FOREACH(x, y)\n"
629                "#define UNKNOWN_FOREACH(x, y)\n");
630 
631   // Not as function-like macros.
632   verifyFormat("#define foreach (x, y)\n"
633                "#define Q_FOREACH (x, y)\n"
634                "#define BOOST_FOREACH (x, y)\n"
635                "#define UNKNOWN_FOREACH (x, y)\n");
636 }
637 
638 TEST_F(FormatTest, FormatsWhileLoop) {
639   verifyFormat("while (true) {\n}");
640   verifyFormat("while (true)\n"
641                "  f();");
642   verifyFormat("while () {\n}");
643   verifyFormat("while () {\n"
644                "  f();\n"
645                "}");
646 }
647 
648 TEST_F(FormatTest, FormatsDoWhile) {
649   verifyFormat("do {\n"
650                "  do_something();\n"
651                "} while (something());");
652   verifyFormat("do\n"
653                "  do_something();\n"
654                "while (something());");
655 }
656 
657 TEST_F(FormatTest, FormatsSwitchStatement) {
658   verifyFormat("switch (x) {\n"
659                "case 1:\n"
660                "  f();\n"
661                "  break;\n"
662                "case kFoo:\n"
663                "case ns::kBar:\n"
664                "case kBaz:\n"
665                "  break;\n"
666                "default:\n"
667                "  g();\n"
668                "  break;\n"
669                "}");
670   verifyFormat("switch (x) {\n"
671                "case 1: {\n"
672                "  f();\n"
673                "  break;\n"
674                "}\n"
675                "case 2: {\n"
676                "  break;\n"
677                "}\n"
678                "}");
679   verifyFormat("switch (x) {\n"
680                "case 1: {\n"
681                "  f();\n"
682                "  {\n"
683                "    g();\n"
684                "    h();\n"
685                "  }\n"
686                "  break;\n"
687                "}\n"
688                "}");
689   verifyFormat("switch (x) {\n"
690                "case 1: {\n"
691                "  f();\n"
692                "  if (foo) {\n"
693                "    g();\n"
694                "    h();\n"
695                "  }\n"
696                "  break;\n"
697                "}\n"
698                "}");
699   verifyFormat("switch (x) {\n"
700                "case 1: {\n"
701                "  f();\n"
702                "  g();\n"
703                "} break;\n"
704                "}");
705   verifyFormat("switch (test)\n"
706                "  ;");
707   verifyFormat("switch (x) {\n"
708                "default: {\n"
709                "  // Do nothing.\n"
710                "}\n"
711                "}");
712   verifyFormat("switch (x) {\n"
713                "// comment\n"
714                "// if 1, do f()\n"
715                "case 1:\n"
716                "  f();\n"
717                "}");
718   verifyFormat("switch (x) {\n"
719                "case 1:\n"
720                "  // Do amazing stuff\n"
721                "  {\n"
722                "    f();\n"
723                "    g();\n"
724                "  }\n"
725                "  break;\n"
726                "}");
727   verifyFormat("#define A          \\\n"
728                "  switch (x) {     \\\n"
729                "  case a:          \\\n"
730                "    foo = b;       \\\n"
731                "  }",
732                getLLVMStyleWithColumns(20));
733   verifyFormat("#define OPERATION_CASE(name)           \\\n"
734                "  case OP_name:                        \\\n"
735                "    return operations::Operation##name\n",
736                getLLVMStyleWithColumns(40));
737   verifyFormat("switch (x) {\n"
738                "case 1:;\n"
739                "default:;\n"
740                "  int i;\n"
741                "}");
742 
743   verifyGoogleFormat("switch (x) {\n"
744                      "  case 1:\n"
745                      "    f();\n"
746                      "    break;\n"
747                      "  case kFoo:\n"
748                      "  case ns::kBar:\n"
749                      "  case kBaz:\n"
750                      "    break;\n"
751                      "  default:\n"
752                      "    g();\n"
753                      "    break;\n"
754                      "}");
755   verifyGoogleFormat("switch (x) {\n"
756                      "  case 1: {\n"
757                      "    f();\n"
758                      "    break;\n"
759                      "  }\n"
760                      "}");
761   verifyGoogleFormat("switch (test)\n"
762                      "  ;");
763 
764   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
765                      "  case OP_name:              \\\n"
766                      "    return operations::Operation##name\n");
767   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
768                      "  // Get the correction operation class.\n"
769                      "  switch (OpCode) {\n"
770                      "    CASE(Add);\n"
771                      "    CASE(Subtract);\n"
772                      "    default:\n"
773                      "      return operations::Unknown;\n"
774                      "  }\n"
775                      "#undef OPERATION_CASE\n"
776                      "}");
777   verifyFormat("DEBUG({\n"
778                "  switch (x) {\n"
779                "  case A:\n"
780                "    f();\n"
781                "    break;\n"
782                "  // On B:\n"
783                "  case B:\n"
784                "    g();\n"
785                "    break;\n"
786                "  }\n"
787                "});");
788   verifyFormat("switch (a) {\n"
789                "case (b):\n"
790                "  return;\n"
791                "}");
792 
793   verifyFormat("switch (a) {\n"
794                "case some_namespace::\n"
795                "    some_constant:\n"
796                "  return;\n"
797                "}",
798                getLLVMStyleWithColumns(34));
799 }
800 
801 TEST_F(FormatTest, CaseRanges) {
802   verifyFormat("switch (x) {\n"
803                "case 'A' ... 'Z':\n"
804                "case 1 ... 5:\n"
805                "case a ... b:\n"
806                "  break;\n"
807                "}");
808 }
809 
810 TEST_F(FormatTest, ShortCaseLabels) {
811   FormatStyle Style = getLLVMStyle();
812   Style.AllowShortCaseLabelsOnASingleLine = true;
813   verifyFormat("switch (a) {\n"
814                "case 1: x = 1; break;\n"
815                "case 2: return;\n"
816                "case 3:\n"
817                "case 4:\n"
818                "case 5: return;\n"
819                "case 6: // comment\n"
820                "  return;\n"
821                "case 7:\n"
822                "  // comment\n"
823                "  return;\n"
824                "case 8:\n"
825                "  x = 8; // comment\n"
826                "  break;\n"
827                "default: y = 1; break;\n"
828                "}",
829                Style);
830   verifyFormat("switch (a) {\n"
831                "#if FOO\n"
832                "case 0: return 0;\n"
833                "#endif\n"
834                "}",
835                Style);
836   verifyFormat("switch (a) {\n"
837                "case 1: {\n"
838                "}\n"
839                "case 2: {\n"
840                "  return;\n"
841                "}\n"
842                "case 3: {\n"
843                "  x = 1;\n"
844                "  return;\n"
845                "}\n"
846                "case 4:\n"
847                "  if (x)\n"
848                "    return;\n"
849                "}",
850                Style);
851   Style.ColumnLimit = 21;
852   verifyFormat("switch (a) {\n"
853                "case 1: x = 1; break;\n"
854                "case 2: return;\n"
855                "case 3:\n"
856                "case 4:\n"
857                "case 5: return;\n"
858                "default:\n"
859                "  y = 1;\n"
860                "  break;\n"
861                "}",
862                Style);
863 }
864 
865 TEST_F(FormatTest, FormatsLabels) {
866   verifyFormat("void f() {\n"
867                "  some_code();\n"
868                "test_label:\n"
869                "  some_other_code();\n"
870                "  {\n"
871                "    some_more_code();\n"
872                "  another_label:\n"
873                "    some_more_code();\n"
874                "  }\n"
875                "}");
876   verifyFormat("{\n"
877                "  some_code();\n"
878                "test_label:\n"
879                "  some_other_code();\n"
880                "}");
881   verifyFormat("{\n"
882                "  some_code();\n"
883                "test_label:;\n"
884                "  int i = 0;\n"
885                "}");
886 }
887 
888 //===----------------------------------------------------------------------===//
889 // Tests for classes, namespaces, etc.
890 //===----------------------------------------------------------------------===//
891 
892 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
893   verifyFormat("class A {};");
894 }
895 
896 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
897   verifyFormat("class A {\n"
898                "public:\n"
899                "public: // comment\n"
900                "protected:\n"
901                "private:\n"
902                "  void f() {}\n"
903                "};");
904   verifyGoogleFormat("class A {\n"
905                      " public:\n"
906                      " protected:\n"
907                      " private:\n"
908                      "  void f() {}\n"
909                      "};");
910   verifyFormat("class A {\n"
911                "public slots:\n"
912                "  void f1() {}\n"
913                "public Q_SLOTS:\n"
914                "  void f2() {}\n"
915                "protected slots:\n"
916                "  void f3() {}\n"
917                "protected Q_SLOTS:\n"
918                "  void f4() {}\n"
919                "private slots:\n"
920                "  void f5() {}\n"
921                "private Q_SLOTS:\n"
922                "  void f6() {}\n"
923                "signals:\n"
924                "  void g1();\n"
925                "Q_SIGNALS:\n"
926                "  void g2();\n"
927                "};");
928 
929   // Don't interpret 'signals' the wrong way.
930   verifyFormat("signals.set();");
931   verifyFormat("for (Signals signals : f()) {\n}");
932   verifyFormat("{\n"
933                "  signals.set(); // This needs indentation.\n"
934                "}");
935   verifyFormat("void f() {\n"
936                "label:\n"
937                "  signals.baz();\n"
938                "}");
939 }
940 
941 TEST_F(FormatTest, SeparatesLogicalBlocks) {
942   EXPECT_EQ("class A {\n"
943             "public:\n"
944             "  void f();\n"
945             "\n"
946             "private:\n"
947             "  void g() {}\n"
948             "  // test\n"
949             "protected:\n"
950             "  int h;\n"
951             "};",
952             format("class A {\n"
953                    "public:\n"
954                    "void f();\n"
955                    "private:\n"
956                    "void g() {}\n"
957                    "// test\n"
958                    "protected:\n"
959                    "int h;\n"
960                    "};"));
961   EXPECT_EQ("class A {\n"
962             "protected:\n"
963             "public:\n"
964             "  void f();\n"
965             "};",
966             format("class A {\n"
967                    "protected:\n"
968                    "\n"
969                    "public:\n"
970                    "\n"
971                    "  void f();\n"
972                    "};"));
973 
974   // Even ensure proper spacing inside macros.
975   EXPECT_EQ("#define B     \\\n"
976             "  class A {   \\\n"
977             "   protected: \\\n"
978             "   public:    \\\n"
979             "    void f(); \\\n"
980             "  };",
981             format("#define B     \\\n"
982                    "  class A {   \\\n"
983                    "   protected: \\\n"
984                    "              \\\n"
985                    "   public:    \\\n"
986                    "              \\\n"
987                    "    void f(); \\\n"
988                    "  };",
989                    getGoogleStyle()));
990   // But don't remove empty lines after macros ending in access specifiers.
991   EXPECT_EQ("#define A private:\n"
992             "\n"
993             "int i;",
994             format("#define A         private:\n"
995                    "\n"
996                    "int              i;"));
997 }
998 
999 TEST_F(FormatTest, FormatsClasses) {
1000   verifyFormat("class A : public B {};");
1001   verifyFormat("class A : public ::B {};");
1002 
1003   verifyFormat(
1004       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1005       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1006   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1007                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1008                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1009   verifyFormat(
1010       "class A : public B, public C, public D, public E, public F {};");
1011   verifyFormat("class AAAAAAAAAAAA : public B,\n"
1012                "                     public C,\n"
1013                "                     public D,\n"
1014                "                     public E,\n"
1015                "                     public F,\n"
1016                "                     public G {};");
1017 
1018   verifyFormat("class\n"
1019                "    ReallyReallyLongClassName {\n"
1020                "  int i;\n"
1021                "};",
1022                getLLVMStyleWithColumns(32));
1023   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
1024                "                           aaaaaaaaaaaaaaaa> {};");
1025   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
1026                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
1027                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
1028   verifyFormat("template <class R, class C>\n"
1029                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
1030                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
1031   verifyFormat("class ::A::B {};");
1032 }
1033 
1034 TEST_F(FormatTest, BreakBeforeInheritanceComma) {
1035   FormatStyle StyleWithInheritanceBreak = getLLVMStyle();
1036   StyleWithInheritanceBreak.BreakBeforeInheritanceComma = true;
1037 
1038   verifyFormat("class MyClass : public X {};", StyleWithInheritanceBreak);
1039   verifyFormat("class MyClass\n"
1040                "    : public X\n"
1041                "    , public Y {};",
1042                StyleWithInheritanceBreak);
1043 }
1044 
1045 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1046   verifyFormat("class A {\n} a, b;");
1047   verifyFormat("struct A {\n} a, b;");
1048   verifyFormat("union A {\n} a;");
1049 }
1050 
1051 TEST_F(FormatTest, FormatsEnum) {
1052   verifyFormat("enum {\n"
1053                "  Zero,\n"
1054                "  One = 1,\n"
1055                "  Two = One + 1,\n"
1056                "  Three = (One + Two),\n"
1057                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1058                "  Five = (One, Two, Three, Four, 5)\n"
1059                "};");
1060   verifyGoogleFormat("enum {\n"
1061                      "  Zero,\n"
1062                      "  One = 1,\n"
1063                      "  Two = One + 1,\n"
1064                      "  Three = (One + Two),\n"
1065                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1066                      "  Five = (One, Two, Three, Four, 5)\n"
1067                      "};");
1068   verifyFormat("enum Enum {};");
1069   verifyFormat("enum {};");
1070   verifyFormat("enum X E {} d;");
1071   verifyFormat("enum __attribute__((...)) E {} d;");
1072   verifyFormat("enum __declspec__((...)) E {} d;");
1073   verifyFormat("enum {\n"
1074                "  Bar = Foo<int, int>::value\n"
1075                "};",
1076                getLLVMStyleWithColumns(30));
1077 
1078   verifyFormat("enum ShortEnum { A, B, C };");
1079   verifyGoogleFormat("enum ShortEnum { A, B, C };");
1080 
1081   EXPECT_EQ("enum KeepEmptyLines {\n"
1082             "  ONE,\n"
1083             "\n"
1084             "  TWO,\n"
1085             "\n"
1086             "  THREE\n"
1087             "}",
1088             format("enum KeepEmptyLines {\n"
1089                    "  ONE,\n"
1090                    "\n"
1091                    "  TWO,\n"
1092                    "\n"
1093                    "\n"
1094                    "  THREE\n"
1095                    "}"));
1096   verifyFormat("enum E { // comment\n"
1097                "  ONE,\n"
1098                "  TWO\n"
1099                "};\n"
1100                "int i;");
1101   // Not enums.
1102   verifyFormat("enum X f() {\n"
1103                "  a();\n"
1104                "  return 42;\n"
1105                "}");
1106   verifyFormat("enum X Type::f() {\n"
1107                "  a();\n"
1108                "  return 42;\n"
1109                "}");
1110   verifyFormat("enum ::X f() {\n"
1111                "  a();\n"
1112                "  return 42;\n"
1113                "}");
1114   verifyFormat("enum ns::X f() {\n"
1115                "  a();\n"
1116                "  return 42;\n"
1117                "}");
1118 }
1119 
1120 TEST_F(FormatTest, FormatsEnumsWithErrors) {
1121   verifyFormat("enum Type {\n"
1122                "  One = 0; // These semicolons should be commas.\n"
1123                "  Two = 1;\n"
1124                "};");
1125   verifyFormat("namespace n {\n"
1126                "enum Type {\n"
1127                "  One,\n"
1128                "  Two, // missing };\n"
1129                "  int i;\n"
1130                "}\n"
1131                "void g() {}");
1132 }
1133 
1134 TEST_F(FormatTest, FormatsEnumStruct) {
1135   verifyFormat("enum struct {\n"
1136                "  Zero,\n"
1137                "  One = 1,\n"
1138                "  Two = One + 1,\n"
1139                "  Three = (One + Two),\n"
1140                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1141                "  Five = (One, Two, Three, Four, 5)\n"
1142                "};");
1143   verifyFormat("enum struct Enum {};");
1144   verifyFormat("enum struct {};");
1145   verifyFormat("enum struct X E {} d;");
1146   verifyFormat("enum struct __attribute__((...)) E {} d;");
1147   verifyFormat("enum struct __declspec__((...)) E {} d;");
1148   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
1149 }
1150 
1151 TEST_F(FormatTest, FormatsEnumClass) {
1152   verifyFormat("enum class {\n"
1153                "  Zero,\n"
1154                "  One = 1,\n"
1155                "  Two = One + 1,\n"
1156                "  Three = (One + Two),\n"
1157                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1158                "  Five = (One, Two, Three, Four, 5)\n"
1159                "};");
1160   verifyFormat("enum class Enum {};");
1161   verifyFormat("enum class {};");
1162   verifyFormat("enum class X E {} d;");
1163   verifyFormat("enum class __attribute__((...)) E {} d;");
1164   verifyFormat("enum class __declspec__((...)) E {} d;");
1165   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
1166 }
1167 
1168 TEST_F(FormatTest, FormatsEnumTypes) {
1169   verifyFormat("enum X : int {\n"
1170                "  A, // Force multiple lines.\n"
1171                "  B\n"
1172                "};");
1173   verifyFormat("enum X : int { A, B };");
1174   verifyFormat("enum X : std::uint32_t { A, B };");
1175 }
1176 
1177 TEST_F(FormatTest, FormatsNSEnums) {
1178   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
1179   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
1180                      "  // Information about someDecentlyLongValue.\n"
1181                      "  someDecentlyLongValue,\n"
1182                      "  // Information about anotherDecentlyLongValue.\n"
1183                      "  anotherDecentlyLongValue,\n"
1184                      "  // Information about aThirdDecentlyLongValue.\n"
1185                      "  aThirdDecentlyLongValue\n"
1186                      "};");
1187   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
1188                      "  a = 1,\n"
1189                      "  b = 2,\n"
1190                      "  c = 3,\n"
1191                      "};");
1192   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
1193                      "  a = 1,\n"
1194                      "  b = 2,\n"
1195                      "  c = 3,\n"
1196                      "};");
1197   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
1198                      "  a = 1,\n"
1199                      "  b = 2,\n"
1200                      "  c = 3,\n"
1201                      "};");
1202 }
1203 
1204 TEST_F(FormatTest, FormatsBitfields) {
1205   verifyFormat("struct Bitfields {\n"
1206                "  unsigned sClass : 8;\n"
1207                "  unsigned ValueKind : 2;\n"
1208                "};");
1209   verifyFormat("struct A {\n"
1210                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
1211                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
1212                "};");
1213   verifyFormat("struct MyStruct {\n"
1214                "  uchar data;\n"
1215                "  uchar : 8;\n"
1216                "  uchar : 8;\n"
1217                "  uchar other;\n"
1218                "};");
1219 }
1220 
1221 TEST_F(FormatTest, FormatsNamespaces) {
1222   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
1223   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
1224 
1225   verifyFormat("namespace some_namespace {\n"
1226                "class A {};\n"
1227                "void f() { f(); }\n"
1228                "}",
1229                LLVMWithNoNamespaceFix);
1230   verifyFormat("namespace {\n"
1231                "class A {};\n"
1232                "void f() { f(); }\n"
1233                "}",
1234                LLVMWithNoNamespaceFix);
1235   verifyFormat("inline namespace X {\n"
1236                "class A {};\n"
1237                "void f() { f(); }\n"
1238                "}",
1239                LLVMWithNoNamespaceFix);
1240   verifyFormat("using namespace some_namespace;\n"
1241                "class A {};\n"
1242                "void f() { f(); }",
1243                LLVMWithNoNamespaceFix);
1244 
1245   // This code is more common than we thought; if we
1246   // layout this correctly the semicolon will go into
1247   // its own line, which is undesirable.
1248   verifyFormat("namespace {};",
1249                LLVMWithNoNamespaceFix);
1250   verifyFormat("namespace {\n"
1251                "class A {};\n"
1252                "};",
1253                LLVMWithNoNamespaceFix);
1254 
1255   verifyFormat("namespace {\n"
1256                "int SomeVariable = 0; // comment\n"
1257                "} // namespace",
1258                LLVMWithNoNamespaceFix);
1259   EXPECT_EQ("#ifndef HEADER_GUARD\n"
1260             "#define HEADER_GUARD\n"
1261             "namespace my_namespace {\n"
1262             "int i;\n"
1263             "} // my_namespace\n"
1264             "#endif // HEADER_GUARD",
1265             format("#ifndef HEADER_GUARD\n"
1266                    " #define HEADER_GUARD\n"
1267                    "   namespace my_namespace {\n"
1268                    "int i;\n"
1269                    "}    // my_namespace\n"
1270                    "#endif    // HEADER_GUARD",
1271                    LLVMWithNoNamespaceFix));
1272 
1273   EXPECT_EQ("namespace A::B {\n"
1274             "class C {};\n"
1275             "}",
1276             format("namespace A::B {\n"
1277                    "class C {};\n"
1278                    "}",
1279                    LLVMWithNoNamespaceFix));
1280 
1281   FormatStyle Style = getLLVMStyle();
1282   Style.NamespaceIndentation = FormatStyle::NI_All;
1283   EXPECT_EQ("namespace out {\n"
1284             "  int i;\n"
1285             "  namespace in {\n"
1286             "    int i;\n"
1287             "  } // namespace in\n"
1288             "} // namespace out",
1289             format("namespace out {\n"
1290                    "int i;\n"
1291                    "namespace in {\n"
1292                    "int i;\n"
1293                    "} // namespace in\n"
1294                    "} // namespace out",
1295                    Style));
1296 
1297   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1298   EXPECT_EQ("namespace out {\n"
1299             "int i;\n"
1300             "namespace in {\n"
1301             "  int i;\n"
1302             "} // namespace in\n"
1303             "} // namespace out",
1304             format("namespace out {\n"
1305                    "int i;\n"
1306                    "namespace in {\n"
1307                    "int i;\n"
1308                    "} // namespace in\n"
1309                    "} // namespace out",
1310                    Style));
1311 }
1312 
1313 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
1314 
1315 TEST_F(FormatTest, FormatsInlineASM) {
1316   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
1317   verifyFormat("asm(\"nop\" ::: \"memory\");");
1318   verifyFormat(
1319       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
1320       "    \"cpuid\\n\\t\"\n"
1321       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
1322       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
1323       "    : \"a\"(value));");
1324   EXPECT_EQ(
1325       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
1326       "  __asm {\n"
1327       "        mov     edx,[that] // vtable in edx\n"
1328       "        mov     eax,methodIndex\n"
1329       "        call    [edx][eax*4] // stdcall\n"
1330       "  }\n"
1331       "}",
1332       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
1333              "    __asm {\n"
1334              "        mov     edx,[that] // vtable in edx\n"
1335              "        mov     eax,methodIndex\n"
1336              "        call    [edx][eax*4] // stdcall\n"
1337              "    }\n"
1338              "}"));
1339   EXPECT_EQ("_asm {\n"
1340             "  xor eax, eax;\n"
1341             "  cpuid;\n"
1342             "}",
1343             format("_asm {\n"
1344                    "  xor eax, eax;\n"
1345                    "  cpuid;\n"
1346                    "}"));
1347   verifyFormat("void function() {\n"
1348                "  // comment\n"
1349                "  asm(\"\");\n"
1350                "}");
1351   EXPECT_EQ("__asm {\n"
1352             "}\n"
1353             "int i;",
1354             format("__asm   {\n"
1355                    "}\n"
1356                    "int   i;"));
1357 }
1358 
1359 TEST_F(FormatTest, FormatTryCatch) {
1360   verifyFormat("try {\n"
1361                "  throw a * b;\n"
1362                "} catch (int a) {\n"
1363                "  // Do nothing.\n"
1364                "} catch (...) {\n"
1365                "  exit(42);\n"
1366                "}");
1367 
1368   // Function-level try statements.
1369   verifyFormat("int f() try { return 4; } catch (...) {\n"
1370                "  return 5;\n"
1371                "}");
1372   verifyFormat("class A {\n"
1373                "  int a;\n"
1374                "  A() try : a(0) {\n"
1375                "  } catch (...) {\n"
1376                "    throw;\n"
1377                "  }\n"
1378                "};\n");
1379 
1380   // Incomplete try-catch blocks.
1381   verifyIncompleteFormat("try {} catch (");
1382 }
1383 
1384 TEST_F(FormatTest, FormatSEHTryCatch) {
1385   verifyFormat("__try {\n"
1386                "  int a = b * c;\n"
1387                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
1388                "  // Do nothing.\n"
1389                "}");
1390 
1391   verifyFormat("__try {\n"
1392                "  int a = b * c;\n"
1393                "} __finally {\n"
1394                "  // Do nothing.\n"
1395                "}");
1396 
1397   verifyFormat("DEBUG({\n"
1398                "  __try {\n"
1399                "  } __finally {\n"
1400                "  }\n"
1401                "});\n");
1402 }
1403 
1404 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
1405   verifyFormat("try {\n"
1406                "  f();\n"
1407                "} catch {\n"
1408                "  g();\n"
1409                "}");
1410   verifyFormat("try {\n"
1411                "  f();\n"
1412                "} catch (A a) MACRO(x) {\n"
1413                "  g();\n"
1414                "} catch (B b) MACRO(x) {\n"
1415                "  g();\n"
1416                "}");
1417 }
1418 
1419 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
1420   FormatStyle Style = getLLVMStyle();
1421   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
1422                           FormatStyle::BS_WebKit}) {
1423     Style.BreakBeforeBraces = BraceStyle;
1424     verifyFormat("try {\n"
1425                  "  // something\n"
1426                  "} catch (...) {\n"
1427                  "  // something\n"
1428                  "}",
1429                  Style);
1430   }
1431   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
1432   verifyFormat("try {\n"
1433                "  // something\n"
1434                "}\n"
1435                "catch (...) {\n"
1436                "  // something\n"
1437                "}",
1438                Style);
1439   verifyFormat("__try {\n"
1440                "  // something\n"
1441                "}\n"
1442                "__finally {\n"
1443                "  // something\n"
1444                "}",
1445                Style);
1446   verifyFormat("@try {\n"
1447                "  // something\n"
1448                "}\n"
1449                "@finally {\n"
1450                "  // something\n"
1451                "}",
1452                Style);
1453   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1454   verifyFormat("try\n"
1455                "{\n"
1456                "  // something\n"
1457                "}\n"
1458                "catch (...)\n"
1459                "{\n"
1460                "  // something\n"
1461                "}",
1462                Style);
1463   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
1464   verifyFormat("try\n"
1465                "  {\n"
1466                "    // something\n"
1467                "  }\n"
1468                "catch (...)\n"
1469                "  {\n"
1470                "    // something\n"
1471                "  }",
1472                Style);
1473   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1474   Style.BraceWrapping.BeforeCatch = true;
1475   verifyFormat("try {\n"
1476                "  // something\n"
1477                "}\n"
1478                "catch (...) {\n"
1479                "  // something\n"
1480                "}",
1481                Style);
1482 }
1483 
1484 TEST_F(FormatTest, StaticInitializers) {
1485   verifyFormat("static SomeClass SC = {1, 'a'};");
1486 
1487   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
1488                "    100000000, "
1489                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
1490 
1491   // Here, everything other than the "}" would fit on a line.
1492   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
1493                "    10000000000000000000000000};");
1494   EXPECT_EQ("S s = {a,\n"
1495             "\n"
1496             "       b};",
1497             format("S s = {\n"
1498                    "  a,\n"
1499                    "\n"
1500                    "  b\n"
1501                    "};"));
1502 
1503   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
1504   // line. However, the formatting looks a bit off and this probably doesn't
1505   // happen often in practice.
1506   verifyFormat("static int Variable[1] = {\n"
1507                "    {1000000000000000000000000000000000000}};",
1508                getLLVMStyleWithColumns(40));
1509 }
1510 
1511 TEST_F(FormatTest, DesignatedInitializers) {
1512   verifyFormat("const struct A a = {.a = 1, .b = 2};");
1513   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
1514                "                    .bbbbbbbbbb = 2,\n"
1515                "                    .cccccccccc = 3,\n"
1516                "                    .dddddddddd = 4,\n"
1517                "                    .eeeeeeeeee = 5};");
1518   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
1519                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
1520                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
1521                "    .ccccccccccccccccccccccccccc = 3,\n"
1522                "    .ddddddddddddddddddddddddddd = 4,\n"
1523                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
1524 
1525   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
1526 }
1527 
1528 TEST_F(FormatTest, NestedStaticInitializers) {
1529   verifyFormat("static A x = {{{}}};\n");
1530   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
1531                "               {init1, init2, init3, init4}}};",
1532                getLLVMStyleWithColumns(50));
1533 
1534   verifyFormat("somes Status::global_reps[3] = {\n"
1535                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
1536                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
1537                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
1538                getLLVMStyleWithColumns(60));
1539   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
1540                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
1541                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
1542                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
1543   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
1544                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
1545                "rect.fTop}};");
1546 
1547   verifyFormat(
1548       "SomeArrayOfSomeType a = {\n"
1549       "    {{1, 2, 3},\n"
1550       "     {1, 2, 3},\n"
1551       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
1552       "      333333333333333333333333333333},\n"
1553       "     {1, 2, 3},\n"
1554       "     {1, 2, 3}}};");
1555   verifyFormat(
1556       "SomeArrayOfSomeType a = {\n"
1557       "    {{1, 2, 3}},\n"
1558       "    {{1, 2, 3}},\n"
1559       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
1560       "      333333333333333333333333333333}},\n"
1561       "    {{1, 2, 3}},\n"
1562       "    {{1, 2, 3}}};");
1563 
1564   verifyFormat("struct {\n"
1565                "  unsigned bit;\n"
1566                "  const char *const name;\n"
1567                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
1568                "                 {kOsWin, \"Windows\"},\n"
1569                "                 {kOsLinux, \"Linux\"},\n"
1570                "                 {kOsCrOS, \"Chrome OS\"}};");
1571   verifyFormat("struct {\n"
1572                "  unsigned bit;\n"
1573                "  const char *const name;\n"
1574                "} kBitsToOs[] = {\n"
1575                "    {kOsMac, \"Mac\"},\n"
1576                "    {kOsWin, \"Windows\"},\n"
1577                "    {kOsLinux, \"Linux\"},\n"
1578                "    {kOsCrOS, \"Chrome OS\"},\n"
1579                "};");
1580 }
1581 
1582 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
1583   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
1584                "                      \\\n"
1585                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
1586 }
1587 
1588 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
1589   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
1590                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
1591 
1592   // Do break defaulted and deleted functions.
1593   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
1594                "    default;",
1595                getLLVMStyleWithColumns(40));
1596   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
1597                "    delete;",
1598                getLLVMStyleWithColumns(40));
1599 }
1600 
1601 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
1602   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
1603                getLLVMStyleWithColumns(40));
1604   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
1605                getLLVMStyleWithColumns(40));
1606   EXPECT_EQ("#define Q                              \\\n"
1607             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
1608             "  \"aaaaaaaa.cpp\"",
1609             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
1610                    getLLVMStyleWithColumns(40)));
1611 }
1612 
1613 TEST_F(FormatTest, UnderstandsLinePPDirective) {
1614   EXPECT_EQ("# 123 \"A string literal\"",
1615             format("   #     123    \"A string literal\""));
1616 }
1617 
1618 TEST_F(FormatTest, LayoutUnknownPPDirective) {
1619   EXPECT_EQ("#;", format("#;"));
1620   verifyFormat("#\n;\n;\n;");
1621 }
1622 
1623 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
1624   EXPECT_EQ("#line 42 \"test\"\n",
1625             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
1626   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
1627                                     getLLVMStyleWithColumns(12)));
1628 }
1629 
1630 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
1631   EXPECT_EQ("#line 42 \"test\"",
1632             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
1633   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
1634 }
1635 
1636 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
1637   verifyFormat("#define A \\x20");
1638   verifyFormat("#define A \\ x20");
1639   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
1640   verifyFormat("#define A ''");
1641   verifyFormat("#define A ''qqq");
1642   verifyFormat("#define A `qqq");
1643   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
1644   EXPECT_EQ("const char *c = STRINGIFY(\n"
1645             "\\na : b);",
1646             format("const char * c = STRINGIFY(\n"
1647                    "\\na : b);"));
1648 
1649   verifyFormat("a\r\\");
1650   verifyFormat("a\v\\");
1651   verifyFormat("a\f\\");
1652 }
1653 
1654 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
1655   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
1656   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
1657   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
1658   // FIXME: We never break before the macro name.
1659   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
1660 
1661   verifyFormat("#define A A\n#define A A");
1662   verifyFormat("#define A(X) A\n#define A A");
1663 
1664   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
1665   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
1666 }
1667 
1668 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
1669   EXPECT_EQ("// somecomment\n"
1670             "#include \"a.h\"\n"
1671             "#define A(  \\\n"
1672             "    A, B)\n"
1673             "#include \"b.h\"\n"
1674             "// somecomment\n",
1675             format("  // somecomment\n"
1676                    "  #include \"a.h\"\n"
1677                    "#define A(A,\\\n"
1678                    "    B)\n"
1679                    "    #include \"b.h\"\n"
1680                    " // somecomment\n",
1681                    getLLVMStyleWithColumns(13)));
1682 }
1683 
1684 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
1685 
1686 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
1687   EXPECT_EQ("#define A    \\\n"
1688             "  c;         \\\n"
1689             "  e;\n"
1690             "f;",
1691             format("#define A c; e;\n"
1692                    "f;",
1693                    getLLVMStyleWithColumns(14)));
1694 }
1695 
1696 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
1697 
1698 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
1699   EXPECT_EQ("int x,\n"
1700             "#define A\n"
1701             "    y;",
1702             format("int x,\n#define A\ny;"));
1703 }
1704 
1705 TEST_F(FormatTest, HashInMacroDefinition) {
1706   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
1707   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
1708   verifyFormat("#define A  \\\n"
1709                "  {        \\\n"
1710                "    f(#c); \\\n"
1711                "  }",
1712                getLLVMStyleWithColumns(11));
1713 
1714   verifyFormat("#define A(X)         \\\n"
1715                "  void function##X()",
1716                getLLVMStyleWithColumns(22));
1717 
1718   verifyFormat("#define A(a, b, c)   \\\n"
1719                "  void a##b##c()",
1720                getLLVMStyleWithColumns(22));
1721 
1722   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
1723 }
1724 
1725 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
1726   EXPECT_EQ("#define A (x)", format("#define A (x)"));
1727   EXPECT_EQ("#define A(x)", format("#define A(x)"));
1728 }
1729 
1730 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
1731   EXPECT_EQ("#define A b;", format("#define A \\\n"
1732                                    "          \\\n"
1733                                    "  b;",
1734                                    getLLVMStyleWithColumns(25)));
1735   EXPECT_EQ("#define A \\\n"
1736             "          \\\n"
1737             "  a;      \\\n"
1738             "  b;",
1739             format("#define A \\\n"
1740                    "          \\\n"
1741                    "  a;      \\\n"
1742                    "  b;",
1743                    getLLVMStyleWithColumns(11)));
1744   EXPECT_EQ("#define A \\\n"
1745             "  a;      \\\n"
1746             "          \\\n"
1747             "  b;",
1748             format("#define A \\\n"
1749                    "  a;      \\\n"
1750                    "          \\\n"
1751                    "  b;",
1752                    getLLVMStyleWithColumns(11)));
1753 }
1754 
1755 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
1756   verifyIncompleteFormat("#define A :");
1757   verifyFormat("#define SOMECASES  \\\n"
1758                "  case 1:          \\\n"
1759                "  case 2\n",
1760                getLLVMStyleWithColumns(20));
1761   verifyFormat("#define MACRO(a) \\\n"
1762                "  if (a)         \\\n"
1763                "    f();         \\\n"
1764                "  else           \\\n"
1765                "    g()",
1766                getLLVMStyleWithColumns(18));
1767   verifyFormat("#define A template <typename T>");
1768   verifyIncompleteFormat("#define STR(x) #x\n"
1769                          "f(STR(this_is_a_string_literal{));");
1770   verifyFormat("#pragma omp threadprivate( \\\n"
1771                "    y)), // expected-warning",
1772                getLLVMStyleWithColumns(28));
1773   verifyFormat("#d, = };");
1774   verifyFormat("#if \"a");
1775   verifyIncompleteFormat("({\n"
1776                          "#define b     \\\n"
1777                          "  }           \\\n"
1778                          "  a\n"
1779                          "a",
1780                          getLLVMStyleWithColumns(15));
1781   verifyFormat("#define A     \\\n"
1782                "  {           \\\n"
1783                "    {\n"
1784                "#define B     \\\n"
1785                "  }           \\\n"
1786                "  }",
1787                getLLVMStyleWithColumns(15));
1788   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
1789   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
1790   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
1791   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
1792 }
1793 
1794 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
1795   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
1796   EXPECT_EQ("class A : public QObject {\n"
1797             "  Q_OBJECT\n"
1798             "\n"
1799             "  A() {}\n"
1800             "};",
1801             format("class A  :  public QObject {\n"
1802                    "     Q_OBJECT\n"
1803                    "\n"
1804                    "  A() {\n}\n"
1805                    "}  ;"));
1806   EXPECT_EQ("MACRO\n"
1807             "/*static*/ int i;",
1808             format("MACRO\n"
1809                    " /*static*/ int   i;"));
1810   EXPECT_EQ("SOME_MACRO\n"
1811             "namespace {\n"
1812             "void f();\n"
1813             "} // namespace",
1814             format("SOME_MACRO\n"
1815                    "  namespace    {\n"
1816                    "void   f(  );\n"
1817                    "} // namespace"));
1818   // Only if the identifier contains at least 5 characters.
1819   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
1820   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
1821   // Only if everything is upper case.
1822   EXPECT_EQ("class A : public QObject {\n"
1823             "  Q_Object A() {}\n"
1824             "};",
1825             format("class A  :  public QObject {\n"
1826                    "     Q_Object\n"
1827                    "  A() {\n}\n"
1828                    "}  ;"));
1829 
1830   // Only if the next line can actually start an unwrapped line.
1831   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
1832             format("SOME_WEIRD_LOG_MACRO\n"
1833                    "<< SomeThing;"));
1834 
1835   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
1836                "(n, buffers))\n",
1837                getChromiumStyle(FormatStyle::LK_Cpp));
1838 }
1839 
1840 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
1841   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
1842             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
1843             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
1844             "class X {};\n"
1845             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
1846             "int *createScopDetectionPass() { return 0; }",
1847             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
1848                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
1849                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
1850                    "  class X {};\n"
1851                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
1852                    "  int *createScopDetectionPass() { return 0; }"));
1853   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
1854   // braces, so that inner block is indented one level more.
1855   EXPECT_EQ("int q() {\n"
1856             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
1857             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
1858             "  IPC_END_MESSAGE_MAP()\n"
1859             "}",
1860             format("int q() {\n"
1861                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
1862                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
1863                    "  IPC_END_MESSAGE_MAP()\n"
1864                    "}"));
1865 
1866   // Same inside macros.
1867   EXPECT_EQ("#define LIST(L) \\\n"
1868             "  L(A)          \\\n"
1869             "  L(B)          \\\n"
1870             "  L(C)",
1871             format("#define LIST(L) \\\n"
1872                    "  L(A) \\\n"
1873                    "  L(B) \\\n"
1874                    "  L(C)",
1875                    getGoogleStyle()));
1876 
1877   // These must not be recognized as macros.
1878   EXPECT_EQ("int q() {\n"
1879             "  f(x);\n"
1880             "  f(x) {}\n"
1881             "  f(x)->g();\n"
1882             "  f(x)->*g();\n"
1883             "  f(x).g();\n"
1884             "  f(x) = x;\n"
1885             "  f(x) += x;\n"
1886             "  f(x) -= x;\n"
1887             "  f(x) *= x;\n"
1888             "  f(x) /= x;\n"
1889             "  f(x) %= x;\n"
1890             "  f(x) &= x;\n"
1891             "  f(x) |= x;\n"
1892             "  f(x) ^= x;\n"
1893             "  f(x) >>= x;\n"
1894             "  f(x) <<= x;\n"
1895             "  f(x)[y].z();\n"
1896             "  LOG(INFO) << x;\n"
1897             "  ifstream(x) >> x;\n"
1898             "}\n",
1899             format("int q() {\n"
1900                    "  f(x)\n;\n"
1901                    "  f(x)\n {}\n"
1902                    "  f(x)\n->g();\n"
1903                    "  f(x)\n->*g();\n"
1904                    "  f(x)\n.g();\n"
1905                    "  f(x)\n = x;\n"
1906                    "  f(x)\n += x;\n"
1907                    "  f(x)\n -= x;\n"
1908                    "  f(x)\n *= x;\n"
1909                    "  f(x)\n /= x;\n"
1910                    "  f(x)\n %= x;\n"
1911                    "  f(x)\n &= x;\n"
1912                    "  f(x)\n |= x;\n"
1913                    "  f(x)\n ^= x;\n"
1914                    "  f(x)\n >>= x;\n"
1915                    "  f(x)\n <<= x;\n"
1916                    "  f(x)\n[y].z();\n"
1917                    "  LOG(INFO)\n << x;\n"
1918                    "  ifstream(x)\n >> x;\n"
1919                    "}\n"));
1920   EXPECT_EQ("int q() {\n"
1921             "  F(x)\n"
1922             "  if (1) {\n"
1923             "  }\n"
1924             "  F(x)\n"
1925             "  while (1) {\n"
1926             "  }\n"
1927             "  F(x)\n"
1928             "  G(x);\n"
1929             "  F(x)\n"
1930             "  try {\n"
1931             "    Q();\n"
1932             "  } catch (...) {\n"
1933             "  }\n"
1934             "}\n",
1935             format("int q() {\n"
1936                    "F(x)\n"
1937                    "if (1) {}\n"
1938                    "F(x)\n"
1939                    "while (1) {}\n"
1940                    "F(x)\n"
1941                    "G(x);\n"
1942                    "F(x)\n"
1943                    "try { Q(); } catch (...) {}\n"
1944                    "}\n"));
1945   EXPECT_EQ("class A {\n"
1946             "  A() : t(0) {}\n"
1947             "  A(int i) noexcept() : {}\n"
1948             "  A(X x)\n" // FIXME: function-level try blocks are broken.
1949             "  try : t(0) {\n"
1950             "  } catch (...) {\n"
1951             "  }\n"
1952             "};",
1953             format("class A {\n"
1954                    "  A()\n : t(0) {}\n"
1955                    "  A(int i)\n noexcept() : {}\n"
1956                    "  A(X x)\n"
1957                    "  try : t(0) {} catch (...) {}\n"
1958                    "};"));
1959   EXPECT_EQ("class SomeClass {\n"
1960             "public:\n"
1961             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
1962             "};",
1963             format("class SomeClass {\n"
1964                    "public:\n"
1965                    "  SomeClass()\n"
1966                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
1967                    "};"));
1968   EXPECT_EQ("class SomeClass {\n"
1969             "public:\n"
1970             "  SomeClass()\n"
1971             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
1972             "};",
1973             format("class SomeClass {\n"
1974                    "public:\n"
1975                    "  SomeClass()\n"
1976                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
1977                    "};",
1978                    getLLVMStyleWithColumns(40)));
1979 
1980   verifyFormat("MACRO(>)");
1981 }
1982 
1983 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
1984   verifyFormat("#define A \\\n"
1985                "  f({     \\\n"
1986                "    g();  \\\n"
1987                "  });",
1988                getLLVMStyleWithColumns(11));
1989 }
1990 
1991 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
1992   EXPECT_EQ("{\n  {\n#define A\n  }\n}", format("{{\n#define A\n}}"));
1993 }
1994 
1995 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
1996   verifyFormat("{\n  { a #c; }\n}");
1997 }
1998 
1999 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
2000   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
2001             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
2002   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
2003             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
2004 }
2005 
2006 TEST_F(FormatTest, EscapedNewlines) {
2007   EXPECT_EQ(
2008       "#define A \\\n  int i;  \\\n  int j;",
2009       format("#define A \\\nint i;\\\n  int j;", getLLVMStyleWithColumns(11)));
2010   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
2011   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2012   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
2013   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
2014 }
2015 
2016 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
2017   verifyFormat("#define A \\\n"
2018                "  int v(  \\\n"
2019                "      a); \\\n"
2020                "  int i;",
2021                getLLVMStyleWithColumns(11));
2022 }
2023 
2024 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
2025   EXPECT_EQ(
2026       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2027       "                      \\\n"
2028       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2029       "\n"
2030       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2031       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
2032       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
2033              "\\\n"
2034              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2035              "  \n"
2036              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2037              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
2038 }
2039 
2040 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
2041   EXPECT_EQ("int\n"
2042             "#define A\n"
2043             "    a;",
2044             format("int\n#define A\na;"));
2045   verifyFormat("functionCallTo(\n"
2046                "    someOtherFunction(\n"
2047                "        withSomeParameters, whichInSequence,\n"
2048                "        areLongerThanALine(andAnotherCall,\n"
2049                "#define A B\n"
2050                "                           withMoreParamters,\n"
2051                "                           whichStronglyInfluenceTheLayout),\n"
2052                "        andMoreParameters),\n"
2053                "    trailing);",
2054                getLLVMStyleWithColumns(69));
2055   verifyFormat("Foo::Foo()\n"
2056                "#ifdef BAR\n"
2057                "    : baz(0)\n"
2058                "#endif\n"
2059                "{\n"
2060                "}");
2061   verifyFormat("void f() {\n"
2062                "  if (true)\n"
2063                "#ifdef A\n"
2064                "    f(42);\n"
2065                "  x();\n"
2066                "#else\n"
2067                "    g();\n"
2068                "  x();\n"
2069                "#endif\n"
2070                "}");
2071   verifyFormat("void f(param1, param2,\n"
2072                "       param3,\n"
2073                "#ifdef A\n"
2074                "       param4(param5,\n"
2075                "#ifdef A1\n"
2076                "              param6,\n"
2077                "#ifdef A2\n"
2078                "              param7),\n"
2079                "#else\n"
2080                "              param8),\n"
2081                "       param9,\n"
2082                "#endif\n"
2083                "       param10,\n"
2084                "#endif\n"
2085                "       param11)\n"
2086                "#else\n"
2087                "       param12)\n"
2088                "#endif\n"
2089                "{\n"
2090                "  x();\n"
2091                "}",
2092                getLLVMStyleWithColumns(28));
2093   verifyFormat("#if 1\n"
2094                "int i;");
2095   verifyFormat("#if 1\n"
2096                "#endif\n"
2097                "#if 1\n"
2098                "#else\n"
2099                "#endif\n");
2100   verifyFormat("DEBUG({\n"
2101                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2102                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2103                "});\n"
2104                "#if a\n"
2105                "#else\n"
2106                "#endif");
2107 
2108   verifyIncompleteFormat("void f(\n"
2109                          "#if A\n"
2110                          ");\n"
2111                          "#else\n"
2112                          "#endif");
2113 }
2114 
2115 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
2116   verifyFormat("#endif\n"
2117                "#if B");
2118 }
2119 
2120 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
2121   FormatStyle SingleLine = getLLVMStyle();
2122   SingleLine.AllowShortIfStatementsOnASingleLine = true;
2123   verifyFormat("#if 0\n"
2124                "#elif 1\n"
2125                "#endif\n"
2126                "void foo() {\n"
2127                "  if (test) foo2();\n"
2128                "}",
2129                SingleLine);
2130 }
2131 
2132 TEST_F(FormatTest, LayoutBlockInsideParens) {
2133   verifyFormat("functionCall({ int i; });");
2134   verifyFormat("functionCall({\n"
2135                "  int i;\n"
2136                "  int j;\n"
2137                "});");
2138   verifyFormat("functionCall(\n"
2139                "    {\n"
2140                "      int i;\n"
2141                "      int j;\n"
2142                "    },\n"
2143                "    aaaa, bbbb, cccc);");
2144   verifyFormat("functionA(functionB({\n"
2145                "            int i;\n"
2146                "            int j;\n"
2147                "          }),\n"
2148                "          aaaa, bbbb, cccc);");
2149   verifyFormat("functionCall(\n"
2150                "    {\n"
2151                "      int i;\n"
2152                "      int j;\n"
2153                "    },\n"
2154                "    aaaa, bbbb, // comment\n"
2155                "    cccc);");
2156   verifyFormat("functionA(functionB({\n"
2157                "            int i;\n"
2158                "            int j;\n"
2159                "          }),\n"
2160                "          aaaa, bbbb, // comment\n"
2161                "          cccc);");
2162   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
2163   verifyFormat("functionCall(aaaa, bbbb, {\n"
2164                "  int i;\n"
2165                "  int j;\n"
2166                "});");
2167   verifyFormat(
2168       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
2169       "    {\n"
2170       "      int i; // break\n"
2171       "    },\n"
2172       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
2173       "                                     ccccccccccccccccc));");
2174   verifyFormat("DEBUG({\n"
2175                "  if (a)\n"
2176                "    f();\n"
2177                "});");
2178 }
2179 
2180 TEST_F(FormatTest, LayoutBlockInsideStatement) {
2181   EXPECT_EQ("SOME_MACRO { int i; }\n"
2182             "int i;",
2183             format("  SOME_MACRO  {int i;}  int i;"));
2184 }
2185 
2186 TEST_F(FormatTest, LayoutNestedBlocks) {
2187   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
2188                "  struct s {\n"
2189                "    int i;\n"
2190                "  };\n"
2191                "  s kBitsToOs[] = {{10}};\n"
2192                "  for (int i = 0; i < 10; ++i)\n"
2193                "    return;\n"
2194                "}");
2195   verifyFormat("call(parameter, {\n"
2196                "  something();\n"
2197                "  // Comment using all columns.\n"
2198                "  somethingelse();\n"
2199                "});",
2200                getLLVMStyleWithColumns(40));
2201   verifyFormat("DEBUG( //\n"
2202                "    { f(); }, a);");
2203   verifyFormat("DEBUG( //\n"
2204                "    {\n"
2205                "      f(); //\n"
2206                "    },\n"
2207                "    a);");
2208 
2209   EXPECT_EQ("call(parameter, {\n"
2210             "  something();\n"
2211             "  // Comment too\n"
2212             "  // looooooooooong.\n"
2213             "  somethingElse();\n"
2214             "});",
2215             format("call(parameter, {\n"
2216                    "  something();\n"
2217                    "  // Comment too looooooooooong.\n"
2218                    "  somethingElse();\n"
2219                    "});",
2220                    getLLVMStyleWithColumns(29)));
2221   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
2222   EXPECT_EQ("DEBUG({ // comment\n"
2223             "  int i;\n"
2224             "});",
2225             format("DEBUG({ // comment\n"
2226                    "int  i;\n"
2227                    "});"));
2228   EXPECT_EQ("DEBUG({\n"
2229             "  int i;\n"
2230             "\n"
2231             "  // comment\n"
2232             "  int j;\n"
2233             "});",
2234             format("DEBUG({\n"
2235                    "  int  i;\n"
2236                    "\n"
2237                    "  // comment\n"
2238                    "  int  j;\n"
2239                    "});"));
2240 
2241   verifyFormat("DEBUG({\n"
2242                "  if (a)\n"
2243                "    return;\n"
2244                "});");
2245   verifyGoogleFormat("DEBUG({\n"
2246                      "  if (a) return;\n"
2247                      "});");
2248   FormatStyle Style = getGoogleStyle();
2249   Style.ColumnLimit = 45;
2250   verifyFormat("Debug(aaaaa,\n"
2251                "      {\n"
2252                "        if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
2253                "      },\n"
2254                "      a);",
2255                Style);
2256 
2257   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
2258 
2259   verifyNoCrash("^{v^{a}}");
2260 }
2261 
2262 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
2263   EXPECT_EQ("#define MACRO()                     \\\n"
2264             "  Debug(aaa, /* force line break */ \\\n"
2265             "        {                           \\\n"
2266             "          int i;                    \\\n"
2267             "          int j;                    \\\n"
2268             "        })",
2269             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
2270                    "          {  int   i;  int  j;   })",
2271                    getGoogleStyle()));
2272 
2273   EXPECT_EQ("#define A                                       \\\n"
2274             "  [] {                                          \\\n"
2275             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
2276             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
2277             "  }",
2278             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
2279                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
2280                    getGoogleStyle()));
2281 }
2282 
2283 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
2284   EXPECT_EQ("{}", format("{}"));
2285   verifyFormat("enum E {};");
2286   verifyFormat("enum E {}");
2287 }
2288 
2289 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
2290   FormatStyle Style = getLLVMStyle();
2291   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
2292   Style.MacroBlockEnd = "^[A-Z_]+_END$";
2293   verifyFormat("FOO_BEGIN\n"
2294                "  FOO_ENTRY\n"
2295                "FOO_END", Style);
2296   verifyFormat("FOO_BEGIN\n"
2297                "  NESTED_FOO_BEGIN\n"
2298                "    NESTED_FOO_ENTRY\n"
2299                "  NESTED_FOO_END\n"
2300                "FOO_END", Style);
2301   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
2302                "  int x;\n"
2303                "  x = 1;\n"
2304                "FOO_END(Baz)", Style);
2305 }
2306 
2307 //===----------------------------------------------------------------------===//
2308 // Line break tests.
2309 //===----------------------------------------------------------------------===//
2310 
2311 TEST_F(FormatTest, PreventConfusingIndents) {
2312   verifyFormat(
2313       "void f() {\n"
2314       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
2315       "                         parameter, parameter, parameter)),\n"
2316       "                     SecondLongCall(parameter));\n"
2317       "}");
2318   verifyFormat(
2319       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2320       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
2321       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2322       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
2323   verifyFormat(
2324       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2325       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
2326       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
2327       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
2328   verifyFormat(
2329       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
2330       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
2331       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
2332       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
2333   verifyFormat("int a = bbbb && ccc &&\n"
2334                "        fffff(\n"
2335                "#define A Just forcing a new line\n"
2336                "            ddd);");
2337 }
2338 
2339 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
2340   verifyFormat(
2341       "bool aaaaaaa =\n"
2342       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
2343       "    bbbbbbbb();");
2344   verifyFormat(
2345       "bool aaaaaaa =\n"
2346       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
2347       "    bbbbbbbb();");
2348 
2349   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
2350                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
2351                "    ccccccccc == ddddddddddd;");
2352   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
2353                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
2354                "    ccccccccc == ddddddddddd;");
2355   verifyFormat(
2356       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
2357       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
2358       "    ccccccccc == ddddddddddd;");
2359 
2360   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
2361                "                 aaaaaa) &&\n"
2362                "         bbbbbb && cccccc;");
2363   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
2364                "                 aaaaaa) >>\n"
2365                "         bbbbbb;");
2366   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
2367                "    SourceMgr.getSpellingColumnNumber(\n"
2368                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
2369                "    1);");
2370 
2371   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2372                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
2373                "    cccccc) {\n}");
2374   verifyFormat("b = a &&\n"
2375                "    // Comment\n"
2376                "    b.c && d;");
2377 
2378   // If the LHS of a comparison is not a binary expression itself, the
2379   // additional linebreak confuses many people.
2380   verifyFormat(
2381       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2382       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
2383       "}");
2384   verifyFormat(
2385       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2386       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
2387       "}");
2388   verifyFormat(
2389       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
2390       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
2391       "}");
2392   // Even explicit parentheses stress the precedence enough to make the
2393   // additional break unnecessary.
2394   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2395                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
2396                "}");
2397   // This cases is borderline, but with the indentation it is still readable.
2398   verifyFormat(
2399       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2400       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2401       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2402       "}",
2403       getLLVMStyleWithColumns(75));
2404 
2405   // If the LHS is a binary expression, we should still use the additional break
2406   // as otherwise the formatting hides the operator precedence.
2407   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2408                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2409                "    5) {\n"
2410                "}");
2411 
2412   FormatStyle OnePerLine = getLLVMStyle();
2413   OnePerLine.BinPackParameters = false;
2414   verifyFormat(
2415       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2416       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2417       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
2418       OnePerLine);
2419 
2420   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
2421                "                .aaa(aaaaaaaaaaaaa) *\n"
2422                "            aaaaaaa +\n"
2423                "        aaaaaaa;",
2424                getLLVMStyleWithColumns(40));
2425 }
2426 
2427 TEST_F(FormatTest, ExpressionIndentation) {
2428   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2429                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2430                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2431                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2432                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
2433                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
2434                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2435                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
2436                "                 ccccccccccccccccccccccccccccccccccccccccc;");
2437   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2438                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2439                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2440                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
2441   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2442                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2443                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2444                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
2445   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2446                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2447                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2448                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
2449   verifyFormat("if () {\n"
2450                "} else if (aaaaa && bbbbb > // break\n"
2451                "                        ccccc) {\n"
2452                "}");
2453   verifyFormat("if () {\n"
2454                "} else if (aaaaa &&\n"
2455                "           bbbbb > // break\n"
2456                "               ccccc &&\n"
2457                "           ddddd) {\n"
2458                "}");
2459 
2460   // Presence of a trailing comment used to change indentation of b.
2461   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
2462                "       b;\n"
2463                "return aaaaaaaaaaaaaaaaaaa +\n"
2464                "       b; //",
2465                getLLVMStyleWithColumns(30));
2466 }
2467 
2468 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
2469   // Not sure what the best system is here. Like this, the LHS can be found
2470   // immediately above an operator (everything with the same or a higher
2471   // indent). The RHS is aligned right of the operator and so compasses
2472   // everything until something with the same indent as the operator is found.
2473   // FIXME: Is this a good system?
2474   FormatStyle Style = getLLVMStyle();
2475   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
2476   verifyFormat(
2477       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2478       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2479       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2480       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2481       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2482       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2483       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2484       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2485       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
2486       Style);
2487   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2488                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2489                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2490                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
2491                Style);
2492   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2493                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2494                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2495                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
2496                Style);
2497   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2498                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2499                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2500                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
2501                Style);
2502   verifyFormat("if () {\n"
2503                "} else if (aaaaa\n"
2504                "           && bbbbb // break\n"
2505                "                  > ccccc) {\n"
2506                "}",
2507                Style);
2508   verifyFormat("return (a)\n"
2509                "       // comment\n"
2510                "       + b;",
2511                Style);
2512   verifyFormat(
2513       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2514       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2515       "             + cc;",
2516       Style);
2517 
2518   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2519                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
2520                Style);
2521 
2522   // Forced by comments.
2523   verifyFormat(
2524       "unsigned ContentSize =\n"
2525       "    sizeof(int16_t)   // DWARF ARange version number\n"
2526       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
2527       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
2528       "    + sizeof(int8_t); // Segment Size (in bytes)");
2529 
2530   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
2531                "       == boost::fusion::at_c<1>(iiii).second;",
2532                Style);
2533 
2534   Style.ColumnLimit = 60;
2535   verifyFormat("zzzzzzzzzz\n"
2536                "    = bbbbbbbbbbbbbbbbb\n"
2537                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
2538                Style);
2539 }
2540 
2541 TEST_F(FormatTest, EnforcedOperatorWraps) {
2542   // Here we'd like to wrap after the || operators, but a comment is forcing an
2543   // earlier wrap.
2544   verifyFormat("bool x = aaaaa //\n"
2545                "         || bbbbb\n"
2546                "         //\n"
2547                "         || cccc;");
2548 }
2549 
2550 TEST_F(FormatTest, NoOperandAlignment) {
2551   FormatStyle Style = getLLVMStyle();
2552   Style.AlignOperands = false;
2553   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
2554                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2555                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
2556                Style);
2557   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
2558   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2559                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2560                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2561                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2562                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2563                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2564                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2565                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2566                "        > ccccccccccccccccccccccccccccccccccccccccc;",
2567                Style);
2568 
2569   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2570                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2571                "    + cc;",
2572                Style);
2573   verifyFormat("int a = aa\n"
2574                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2575                "        * cccccccccccccccccccccccccccccccccccc;\n",
2576                Style);
2577 
2578   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
2579   verifyFormat("return (a > b\n"
2580                "    // comment1\n"
2581                "    // comment2\n"
2582                "    || c);",
2583                Style);
2584 }
2585 
2586 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
2587   FormatStyle Style = getLLVMStyle();
2588   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
2589   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2590                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2591                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
2592                Style);
2593 }
2594 
2595 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
2596   FormatStyle Style = getLLVMStyle();
2597   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
2598   Style.BinPackArguments = false;
2599   Style.ColumnLimit = 40;
2600   verifyFormat("void test() {\n"
2601                "  someFunction(\n"
2602                "      this + argument + is + quite\n"
2603                "      + long + so + it + gets + wrapped\n"
2604                "      + but + remains + bin - packed);\n"
2605                "}",
2606                Style);
2607   verifyFormat("void test() {\n"
2608                "  someFunction(arg1,\n"
2609                "               this + argument + is\n"
2610                "                   + quite + long + so\n"
2611                "                   + it + gets + wrapped\n"
2612                "                   + but + remains + bin\n"
2613                "                   - packed,\n"
2614                "               arg3);\n"
2615                "}",
2616                Style);
2617   verifyFormat("void test() {\n"
2618                "  someFunction(\n"
2619                "      arg1,\n"
2620                "      this + argument + has\n"
2621                "          + anotherFunc(nested,\n"
2622                "                        calls + whose\n"
2623                "                            + arguments\n"
2624                "                            + are + also\n"
2625                "                            + wrapped,\n"
2626                "                        in + addition)\n"
2627                "          + to + being + bin - packed,\n"
2628                "      arg3);\n"
2629                "}",
2630                Style);
2631 
2632   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
2633   verifyFormat("void test() {\n"
2634                "  someFunction(\n"
2635                "      arg1,\n"
2636                "      this + argument + has +\n"
2637                "          anotherFunc(nested,\n"
2638                "                      calls + whose +\n"
2639                "                          arguments +\n"
2640                "                          are + also +\n"
2641                "                          wrapped,\n"
2642                "                      in + addition) +\n"
2643                "          to + being + bin - packed,\n"
2644                "      arg3);\n"
2645                "}",
2646                Style);
2647 }
2648 
2649 TEST_F(FormatTest, ConstructorInitializers) {
2650   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
2651   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
2652                getLLVMStyleWithColumns(45));
2653   verifyFormat("Constructor()\n"
2654                "    : Inttializer(FitsOnTheLine) {}",
2655                getLLVMStyleWithColumns(44));
2656   verifyFormat("Constructor()\n"
2657                "    : Inttializer(FitsOnTheLine) {}",
2658                getLLVMStyleWithColumns(43));
2659 
2660   verifyFormat("template <typename T>\n"
2661                "Constructor() : Initializer(FitsOnTheLine) {}",
2662                getLLVMStyleWithColumns(45));
2663 
2664   verifyFormat(
2665       "SomeClass::Constructor()\n"
2666       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
2667 
2668   verifyFormat(
2669       "SomeClass::Constructor()\n"
2670       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2671       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
2672   verifyFormat(
2673       "SomeClass::Constructor()\n"
2674       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2675       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
2676   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2677                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
2678                "    : aaaaaaaaaa(aaaaaa) {}");
2679 
2680   verifyFormat("Constructor()\n"
2681                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2682                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2683                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2684                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
2685 
2686   verifyFormat("Constructor()\n"
2687                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2688                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
2689 
2690   verifyFormat("Constructor(int Parameter = 0)\n"
2691                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
2692                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
2693   verifyFormat("Constructor()\n"
2694                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
2695                "}",
2696                getLLVMStyleWithColumns(60));
2697   verifyFormat("Constructor()\n"
2698                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2699                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
2700 
2701   // Here a line could be saved by splitting the second initializer onto two
2702   // lines, but that is not desirable.
2703   verifyFormat("Constructor()\n"
2704                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
2705                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
2706                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
2707 
2708   FormatStyle OnePerLine = getLLVMStyle();
2709   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
2710   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
2711   verifyFormat("SomeClass::Constructor()\n"
2712                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2713                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2714                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
2715                OnePerLine);
2716   verifyFormat("SomeClass::Constructor()\n"
2717                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
2718                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2719                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
2720                OnePerLine);
2721   verifyFormat("MyClass::MyClass(int var)\n"
2722                "    : some_var_(var),            // 4 space indent\n"
2723                "      some_other_var_(var + 1) { // lined up\n"
2724                "}",
2725                OnePerLine);
2726   verifyFormat("Constructor()\n"
2727                "    : aaaaa(aaaaaa),\n"
2728                "      aaaaa(aaaaaa),\n"
2729                "      aaaaa(aaaaaa),\n"
2730                "      aaaaa(aaaaaa),\n"
2731                "      aaaaa(aaaaaa) {}",
2732                OnePerLine);
2733   verifyFormat("Constructor()\n"
2734                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
2735                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
2736                OnePerLine);
2737   OnePerLine.BinPackParameters = false;
2738   verifyFormat(
2739       "Constructor()\n"
2740       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
2741       "          aaaaaaaaaaa().aaa(),\n"
2742       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
2743       OnePerLine);
2744   OnePerLine.ColumnLimit = 60;
2745   verifyFormat("Constructor()\n"
2746                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
2747                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
2748                OnePerLine);
2749 
2750   EXPECT_EQ("Constructor()\n"
2751             "    : // Comment forcing unwanted break.\n"
2752             "      aaaa(aaaa) {}",
2753             format("Constructor() :\n"
2754                    "    // Comment forcing unwanted break.\n"
2755                    "    aaaa(aaaa) {}"));
2756 }
2757 
2758 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
2759   FormatStyle Style = getLLVMStyle();
2760   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
2761 
2762   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
2763   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
2764                getStyleWithColumns(Style, 45));
2765   verifyFormat("Constructor() :\n"
2766                "    Initializer(FitsOnTheLine) {}",
2767                getStyleWithColumns(Style, 44));
2768   verifyFormat("Constructor() :\n"
2769                "    Initializer(FitsOnTheLine) {}",
2770                getStyleWithColumns(Style, 43));
2771 
2772   verifyFormat("template <typename T>\n"
2773                "Constructor() : Initializer(FitsOnTheLine) {}",
2774                getStyleWithColumns(Style, 50));
2775 
2776   verifyFormat(
2777       "SomeClass::Constructor() :\n"
2778       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
2779 	  Style);
2780 
2781   verifyFormat(
2782       "SomeClass::Constructor() :\n"
2783       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2784       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
2785 	  Style);
2786   verifyFormat(
2787       "SomeClass::Constructor() :\n"
2788       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2789       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
2790 	  Style);
2791   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2792                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
2793                "    aaaaaaaaaa(aaaaaa) {}",
2794 			   Style);
2795 
2796   verifyFormat("Constructor() :\n"
2797                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2798                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2799                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2800                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
2801 			   Style);
2802 
2803   verifyFormat("Constructor() :\n"
2804                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2805                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
2806 			   Style);
2807 
2808   verifyFormat("Constructor(int Parameter = 0) :\n"
2809                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
2810                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
2811 			   Style);
2812   verifyFormat("Constructor() :\n"
2813                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
2814                "}",
2815                getStyleWithColumns(Style, 60));
2816   verifyFormat("Constructor() :\n"
2817                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2818                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
2819 			   Style);
2820 
2821   // Here a line could be saved by splitting the second initializer onto two
2822   // lines, but that is not desirable.
2823   verifyFormat("Constructor() :\n"
2824                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
2825                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
2826                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
2827 			   Style);
2828 
2829   FormatStyle OnePerLine = Style;
2830   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
2831   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
2832   verifyFormat("SomeClass::Constructor() :\n"
2833                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2834                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2835                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
2836                OnePerLine);
2837   verifyFormat("SomeClass::Constructor() :\n"
2838                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
2839                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2840                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
2841                OnePerLine);
2842   verifyFormat("MyClass::MyClass(int var) :\n"
2843                "    some_var_(var),            // 4 space indent\n"
2844                "    some_other_var_(var + 1) { // lined up\n"
2845                "}",
2846                OnePerLine);
2847   verifyFormat("Constructor() :\n"
2848                "    aaaaa(aaaaaa),\n"
2849                "    aaaaa(aaaaaa),\n"
2850                "    aaaaa(aaaaaa),\n"
2851                "    aaaaa(aaaaaa),\n"
2852                "    aaaaa(aaaaaa) {}",
2853                OnePerLine);
2854   verifyFormat("Constructor() :\n"
2855                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
2856                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
2857                OnePerLine);
2858   OnePerLine.BinPackParameters = false;
2859   verifyFormat(
2860       "Constructor() :\n"
2861       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
2862       "        aaaaaaaaaaa().aaa(),\n"
2863       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
2864       OnePerLine);
2865   OnePerLine.ColumnLimit = 60;
2866   verifyFormat("Constructor() :\n"
2867                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
2868                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
2869                OnePerLine);
2870 
2871   EXPECT_EQ("Constructor() :\n"
2872             "    // Comment forcing unwanted break.\n"
2873             "    aaaa(aaaa) {}",
2874             format("Constructor() :\n"
2875                    "    // Comment forcing unwanted break.\n"
2876                    "    aaaa(aaaa) {}",
2877 				   Style));
2878 
2879   Style.ColumnLimit = 0;
2880   verifyFormat("SomeClass::Constructor() :\n"
2881                "    a(a) {}",
2882                Style);
2883   verifyFormat("SomeClass::Constructor() noexcept :\n"
2884                "    a(a) {}",
2885                Style);
2886   verifyFormat("SomeClass::Constructor() :\n"
2887 			   "    a(a), b(b), c(c) {}",
2888                Style);
2889   verifyFormat("SomeClass::Constructor() :\n"
2890                "    a(a) {\n"
2891                "  foo();\n"
2892                "  bar();\n"
2893                "}",
2894                Style);
2895 
2896   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
2897   verifyFormat("SomeClass::Constructor() :\n"
2898 			   "    a(a), b(b), c(c) {\n"
2899 			   "}",
2900                Style);
2901   verifyFormat("SomeClass::Constructor() :\n"
2902                "    a(a) {\n"
2903 			   "}",
2904                Style);
2905 
2906   Style.ColumnLimit = 80;
2907   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
2908   Style.ConstructorInitializerIndentWidth = 2;
2909   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}",
2910                Style);
2911   verifyFormat("SomeClass::Constructor() :\n"
2912                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2913                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
2914                Style);
2915 }
2916 
2917 TEST_F(FormatTest, MemoizationTests) {
2918   // This breaks if the memoization lookup does not take \c Indent and
2919   // \c LastSpace into account.
2920   verifyFormat(
2921       "extern CFRunLoopTimerRef\n"
2922       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
2923       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
2924       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
2925       "                     CFRunLoopTimerContext *context) {}");
2926 
2927   // Deep nesting somewhat works around our memoization.
2928   verifyFormat(
2929       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
2930       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
2931       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
2932       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
2933       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
2934       getLLVMStyleWithColumns(65));
2935   verifyFormat(
2936       "aaaaa(\n"
2937       "    aaaaa,\n"
2938       "    aaaaa(\n"
2939       "        aaaaa,\n"
2940       "        aaaaa(\n"
2941       "            aaaaa,\n"
2942       "            aaaaa(\n"
2943       "                aaaaa,\n"
2944       "                aaaaa(\n"
2945       "                    aaaaa,\n"
2946       "                    aaaaa(\n"
2947       "                        aaaaa,\n"
2948       "                        aaaaa(\n"
2949       "                            aaaaa,\n"
2950       "                            aaaaa(\n"
2951       "                                aaaaa,\n"
2952       "                                aaaaa(\n"
2953       "                                    aaaaa,\n"
2954       "                                    aaaaa(\n"
2955       "                                        aaaaa,\n"
2956       "                                        aaaaa(\n"
2957       "                                            aaaaa,\n"
2958       "                                            aaaaa(\n"
2959       "                                                aaaaa,\n"
2960       "                                                aaaaa))))))))))));",
2961       getLLVMStyleWithColumns(65));
2962   verifyFormat(
2963       "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"
2964       "                                  a),\n"
2965       "                                a),\n"
2966       "                              a),\n"
2967       "                            a),\n"
2968       "                          a),\n"
2969       "                        a),\n"
2970       "                      a),\n"
2971       "                    a),\n"
2972       "                  a),\n"
2973       "                a),\n"
2974       "              a),\n"
2975       "            a),\n"
2976       "          a),\n"
2977       "        a),\n"
2978       "      a),\n"
2979       "    a),\n"
2980       "  a)",
2981       getLLVMStyleWithColumns(65));
2982 
2983   // This test takes VERY long when memoization is broken.
2984   FormatStyle OnePerLine = getLLVMStyle();
2985   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
2986   OnePerLine.BinPackParameters = false;
2987   std::string input = "Constructor()\n"
2988                       "    : aaaa(a,\n";
2989   for (unsigned i = 0, e = 80; i != e; ++i) {
2990     input += "           a,\n";
2991   }
2992   input += "           a) {}";
2993   verifyFormat(input, OnePerLine);
2994 }
2995 
2996 TEST_F(FormatTest, BreaksAsHighAsPossible) {
2997   verifyFormat(
2998       "void f() {\n"
2999       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
3000       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
3001       "    f();\n"
3002       "}");
3003   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
3004                "    Intervals[i - 1].getRange().getLast()) {\n}");
3005 }
3006 
3007 TEST_F(FormatTest, BreaksFunctionDeclarations) {
3008   // Principially, we break function declarations in a certain order:
3009   // 1) break amongst arguments.
3010   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
3011                "                              Cccccccccccccc cccccccccccccc);");
3012   verifyFormat("template <class TemplateIt>\n"
3013                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
3014                "                            TemplateIt *stop) {}");
3015 
3016   // 2) break after return type.
3017   verifyFormat(
3018       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3019       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
3020       getGoogleStyle());
3021 
3022   // 3) break after (.
3023   verifyFormat(
3024       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
3025       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
3026       getGoogleStyle());
3027 
3028   // 4) break before after nested name specifiers.
3029   verifyFormat(
3030       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3031       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
3032       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
3033       getGoogleStyle());
3034 
3035   // However, there are exceptions, if a sufficient amount of lines can be
3036   // saved.
3037   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
3038   // more adjusting.
3039   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3040                "                                  Cccccccccccccc cccccccccc,\n"
3041                "                                  Cccccccccccccc cccccccccc,\n"
3042                "                                  Cccccccccccccc cccccccccc,\n"
3043                "                                  Cccccccccccccc cccccccccc);");
3044   verifyFormat(
3045       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3046       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3047       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3048       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
3049       getGoogleStyle());
3050   verifyFormat(
3051       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3052       "                                          Cccccccccccccc cccccccccc,\n"
3053       "                                          Cccccccccccccc cccccccccc,\n"
3054       "                                          Cccccccccccccc cccccccccc,\n"
3055       "                                          Cccccccccccccc cccccccccc,\n"
3056       "                                          Cccccccccccccc cccccccccc,\n"
3057       "                                          Cccccccccccccc cccccccccc);");
3058   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3059                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3060                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3061                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3062                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
3063 
3064   // Break after multi-line parameters.
3065   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3066                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3067                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3068                "    bbbb bbbb);");
3069   verifyFormat("void SomeLoooooooooooongFunction(\n"
3070                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
3071                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3072                "    int bbbbbbbbbbbbb);");
3073 
3074   // Treat overloaded operators like other functions.
3075   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3076                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
3077   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3078                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
3079   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3080                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
3081   verifyGoogleFormat(
3082       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
3083       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3084   verifyGoogleFormat(
3085       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
3086       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3087   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3088                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3089   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
3090                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3091   verifyGoogleFormat(
3092       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
3093       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3094       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
3095   verifyGoogleFormat(
3096       "template <typename T>\n"
3097       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3098       "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
3099       "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
3100 
3101   FormatStyle Style = getLLVMStyle();
3102   Style.PointerAlignment = FormatStyle::PAS_Left;
3103   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3104                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
3105                Style);
3106   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
3107                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3108                Style);
3109 }
3110 
3111 TEST_F(FormatTest, TrailingReturnType) {
3112   verifyFormat("auto foo() -> int;\n");
3113   verifyFormat("struct S {\n"
3114                "  auto bar() const -> int;\n"
3115                "};");
3116   verifyFormat("template <size_t Order, typename T>\n"
3117                "auto load_img(const std::string &filename)\n"
3118                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
3119   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
3120                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
3121   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
3122   verifyFormat("template <typename T>\n"
3123                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
3124                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
3125 
3126   // Not trailing return types.
3127   verifyFormat("void f() { auto a = b->c(); }");
3128 }
3129 
3130 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
3131   // Avoid breaking before trailing 'const' or other trailing annotations, if
3132   // they are not function-like.
3133   FormatStyle Style = getGoogleStyle();
3134   Style.ColumnLimit = 47;
3135   verifyFormat("void someLongFunction(\n"
3136                "    int someLoooooooooooooongParameter) const {\n}",
3137                getLLVMStyleWithColumns(47));
3138   verifyFormat("LoooooongReturnType\n"
3139                "someLoooooooongFunction() const {}",
3140                getLLVMStyleWithColumns(47));
3141   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
3142                "    const {}",
3143                Style);
3144   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3145                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
3146   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3147                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
3148   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3149                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
3150   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
3151                "                   aaaaaaaaaaa aaaaa) const override;");
3152   verifyGoogleFormat(
3153       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3154       "    const override;");
3155 
3156   // Even if the first parameter has to be wrapped.
3157   verifyFormat("void someLongFunction(\n"
3158                "    int someLongParameter) const {}",
3159                getLLVMStyleWithColumns(46));
3160   verifyFormat("void someLongFunction(\n"
3161                "    int someLongParameter) const {}",
3162                Style);
3163   verifyFormat("void someLongFunction(\n"
3164                "    int someLongParameter) override {}",
3165                Style);
3166   verifyFormat("void someLongFunction(\n"
3167                "    int someLongParameter) OVERRIDE {}",
3168                Style);
3169   verifyFormat("void someLongFunction(\n"
3170                "    int someLongParameter) final {}",
3171                Style);
3172   verifyFormat("void someLongFunction(\n"
3173                "    int someLongParameter) FINAL {}",
3174                Style);
3175   verifyFormat("void someLongFunction(\n"
3176                "    int parameter) const override {}",
3177                Style);
3178 
3179   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
3180   verifyFormat("void someLongFunction(\n"
3181                "    int someLongParameter) const\n"
3182                "{\n"
3183                "}",
3184                Style);
3185 
3186   // Unless these are unknown annotations.
3187   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
3188                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3189                "    LONG_AND_UGLY_ANNOTATION;");
3190 
3191   // Breaking before function-like trailing annotations is fine to keep them
3192   // close to their arguments.
3193   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3194                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3195   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3196                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3197   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3198                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
3199   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
3200                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
3201   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
3202 
3203   verifyFormat(
3204       "void aaaaaaaaaaaaaaaaaa()\n"
3205       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
3206       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
3207   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3208                "    __attribute__((unused));");
3209   verifyGoogleFormat(
3210       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3211       "    GUARDED_BY(aaaaaaaaaaaa);");
3212   verifyGoogleFormat(
3213       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3214       "    GUARDED_BY(aaaaaaaaaaaa);");
3215   verifyGoogleFormat(
3216       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3217       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3218   verifyGoogleFormat(
3219       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3220       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
3221 }
3222 
3223 TEST_F(FormatTest, FunctionAnnotations) {
3224   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3225                "int OldFunction(const string &parameter) {}");
3226   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3227                "string OldFunction(const string &parameter) {}");
3228   verifyFormat("template <typename T>\n"
3229                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3230                "string OldFunction(const string &parameter) {}");
3231 
3232   // Not function annotations.
3233   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3234                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
3235   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
3236                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
3237   verifyFormat("MACRO(abc).function() // wrap\n"
3238                "    << abc;");
3239   verifyFormat("MACRO(abc)->function() // wrap\n"
3240                "    << abc;");
3241   verifyFormat("MACRO(abc)::function() // wrap\n"
3242                "    << abc;");
3243 }
3244 
3245 TEST_F(FormatTest, BreaksDesireably) {
3246   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3247                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3248                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
3249   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3250                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
3251                "}");
3252 
3253   verifyFormat(
3254       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3255       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3256 
3257   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3258                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3259                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3260 
3261   verifyFormat(
3262       "aaaaaaaa(aaaaaaaaaaaaa,\n"
3263       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3264       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
3265       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3266       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
3267 
3268   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3269                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3270 
3271   verifyFormat(
3272       "void f() {\n"
3273       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
3274       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3275       "}");
3276   verifyFormat(
3277       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3278       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3279   verifyFormat(
3280       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3281       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3282   verifyFormat(
3283       "aaaaaa(aaa,\n"
3284       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3285       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3286       "       aaaa);");
3287   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3288                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3289                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3290 
3291   // Indent consistently independent of call expression and unary operator.
3292   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3293                "    dddddddddddddddddddddddddddddd));");
3294   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3295                "    dddddddddddddddddddddddddddddd));");
3296   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
3297                "    dddddddddddddddddddddddddddddd));");
3298 
3299   // This test case breaks on an incorrect memoization, i.e. an optimization not
3300   // taking into account the StopAt value.
3301   verifyFormat(
3302       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3303       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3304       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3305       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3306 
3307   verifyFormat("{\n  {\n    {\n"
3308                "      Annotation.SpaceRequiredBefore =\n"
3309                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
3310                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
3311                "    }\n  }\n}");
3312 
3313   // Break on an outer level if there was a break on an inner level.
3314   EXPECT_EQ("f(g(h(a, // comment\n"
3315             "      b, c),\n"
3316             "    d, e),\n"
3317             "  x, y);",
3318             format("f(g(h(a, // comment\n"
3319                    "    b, c), d, e), x, y);"));
3320 
3321   // Prefer breaking similar line breaks.
3322   verifyFormat(
3323       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
3324       "                             NSTrackingMouseEnteredAndExited |\n"
3325       "                             NSTrackingActiveAlways;");
3326 }
3327 
3328 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
3329   FormatStyle NoBinPacking = getGoogleStyle();
3330   NoBinPacking.BinPackParameters = false;
3331   NoBinPacking.BinPackArguments = true;
3332   verifyFormat("void f() {\n"
3333                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
3334                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3335                "}",
3336                NoBinPacking);
3337   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
3338                "       int aaaaaaaaaaaaaaaaaaaa,\n"
3339                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3340                NoBinPacking);
3341 
3342   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
3343   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3344                "                        vector<int> bbbbbbbbbbbbbbb);",
3345                NoBinPacking);
3346   // FIXME: This behavior difference is probably not wanted. However, currently
3347   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
3348   // template arguments from BreakBeforeParameter being set because of the
3349   // one-per-line formatting.
3350   verifyFormat(
3351       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
3352       "                                             aaaaaaaaaa> aaaaaaaaaa);",
3353       NoBinPacking);
3354   verifyFormat(
3355       "void fffffffffff(\n"
3356       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
3357       "        aaaaaaaaaa);");
3358 }
3359 
3360 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
3361   FormatStyle NoBinPacking = getGoogleStyle();
3362   NoBinPacking.BinPackParameters = false;
3363   NoBinPacking.BinPackArguments = false;
3364   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
3365                "  aaaaaaaaaaaaaaaaaaaa,\n"
3366                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
3367                NoBinPacking);
3368   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
3369                "        aaaaaaaaaaaaa,\n"
3370                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
3371                NoBinPacking);
3372   verifyFormat(
3373       "aaaaaaaa(aaaaaaaaaaaaa,\n"
3374       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3375       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
3376       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3377       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
3378       NoBinPacking);
3379   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
3380                "    .aaaaaaaaaaaaaaaaaa();",
3381                NoBinPacking);
3382   verifyFormat("void f() {\n"
3383                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3384                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
3385                "}",
3386                NoBinPacking);
3387 
3388   verifyFormat(
3389       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3390       "             aaaaaaaaaaaa,\n"
3391       "             aaaaaaaaaaaa);",
3392       NoBinPacking);
3393   verifyFormat(
3394       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
3395       "                               ddddddddddddddddddddddddddddd),\n"
3396       "             test);",
3397       NoBinPacking);
3398 
3399   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
3400                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
3401                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
3402                "    aaaaaaaaaaaaaaaaaa;",
3403                NoBinPacking);
3404   verifyFormat("a(\"a\"\n"
3405                "  \"a\",\n"
3406                "  a);");
3407 
3408   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
3409   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
3410                "                aaaaaaaaa,\n"
3411                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3412                NoBinPacking);
3413   verifyFormat(
3414       "void f() {\n"
3415       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
3416       "      .aaaaaaa();\n"
3417       "}",
3418       NoBinPacking);
3419   verifyFormat(
3420       "template <class SomeType, class SomeOtherType>\n"
3421       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
3422       NoBinPacking);
3423 }
3424 
3425 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
3426   FormatStyle Style = getLLVMStyleWithColumns(15);
3427   Style.ExperimentalAutoDetectBinPacking = true;
3428   EXPECT_EQ("aaa(aaaa,\n"
3429             "    aaaa,\n"
3430             "    aaaa);\n"
3431             "aaa(aaaa,\n"
3432             "    aaaa,\n"
3433             "    aaaa);",
3434             format("aaa(aaaa,\n" // one-per-line
3435                    "  aaaa,\n"
3436                    "    aaaa  );\n"
3437                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
3438                    Style));
3439   EXPECT_EQ("aaa(aaaa, aaaa,\n"
3440             "    aaaa);\n"
3441             "aaa(aaaa, aaaa,\n"
3442             "    aaaa);",
3443             format("aaa(aaaa,  aaaa,\n" // bin-packed
3444                    "    aaaa  );\n"
3445                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
3446                    Style));
3447 }
3448 
3449 TEST_F(FormatTest, FormatsBuilderPattern) {
3450   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
3451                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
3452                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
3453                "    .StartsWith(\".init\", ORDER_INIT)\n"
3454                "    .StartsWith(\".fini\", ORDER_FINI)\n"
3455                "    .StartsWith(\".hash\", ORDER_HASH)\n"
3456                "    .Default(ORDER_TEXT);\n");
3457 
3458   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
3459                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
3460   verifyFormat(
3461       "aaaaaaa->aaaaaaa\n"
3462       "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3463       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3464       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
3465   verifyFormat(
3466       "aaaaaaa->aaaaaaa\n"
3467       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3468       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
3469   verifyFormat(
3470       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
3471       "    aaaaaaaaaaaaaa);");
3472   verifyFormat(
3473       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
3474       "    aaaaaa->aaaaaaaaaaaa()\n"
3475       "        ->aaaaaaaaaaaaaaaa(\n"
3476       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3477       "        ->aaaaaaaaaaaaaaaaa();");
3478   verifyGoogleFormat(
3479       "void f() {\n"
3480       "  someo->Add((new util::filetools::Handler(dir))\n"
3481       "                 ->OnEvent1(NewPermanentCallback(\n"
3482       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
3483       "                 ->OnEvent2(NewPermanentCallback(\n"
3484       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
3485       "                 ->OnEvent3(NewPermanentCallback(\n"
3486       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
3487       "                 ->OnEvent5(NewPermanentCallback(\n"
3488       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
3489       "                 ->OnEvent6(NewPermanentCallback(\n"
3490       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
3491       "}");
3492 
3493   verifyFormat(
3494       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
3495   verifyFormat("aaaaaaaaaaaaaaa()\n"
3496                "    .aaaaaaaaaaaaaaa()\n"
3497                "    .aaaaaaaaaaaaaaa()\n"
3498                "    .aaaaaaaaaaaaaaa()\n"
3499                "    .aaaaaaaaaaaaaaa();");
3500   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
3501                "    .aaaaaaaaaaaaaaa()\n"
3502                "    .aaaaaaaaaaaaaaa()\n"
3503                "    .aaaaaaaaaaaaaaa();");
3504   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
3505                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
3506                "    .aaaaaaaaaaaaaaa();");
3507   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
3508                "    ->aaaaaaaaaaaaaae(0)\n"
3509                "    ->aaaaaaaaaaaaaaa();");
3510 
3511   // Don't linewrap after very short segments.
3512   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3513                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3514                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
3515   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3516                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3517                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
3518   verifyFormat("aaa()\n"
3519                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3520                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3521                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
3522 
3523   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
3524                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3525                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
3526   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
3527                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3528                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
3529 
3530   // Prefer not to break after empty parentheses.
3531   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
3532                "    First->LastNewlineOffset);");
3533 
3534   // Prefer not to create "hanging" indents.
3535   verifyFormat(
3536       "return !soooooooooooooome_map\n"
3537       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3538       "            .second;");
3539   verifyFormat(
3540       "return aaaaaaaaaaaaaaaa\n"
3541       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
3542       "    .aaaa(aaaaaaaaaaaaaa);");
3543   // No hanging indent here.
3544   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
3545                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3546   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
3547                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3548   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
3549                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3550                getLLVMStyleWithColumns(60));
3551   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
3552                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
3553                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3554                getLLVMStyleWithColumns(59));
3555   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3556                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3557                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3558 }
3559 
3560 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
3561   verifyFormat(
3562       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3563       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
3564   verifyFormat(
3565       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
3566       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
3567 
3568   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
3569                "    ccccccccccccccccccccccccc) {\n}");
3570   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
3571                "    ccccccccccccccccccccccccc) {\n}");
3572 
3573   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
3574                "    ccccccccccccccccccccccccc) {\n}");
3575   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
3576                "    ccccccccccccccccccccccccc) {\n}");
3577 
3578   verifyFormat(
3579       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
3580       "    ccccccccccccccccccccccccc) {\n}");
3581   verifyFormat(
3582       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
3583       "    ccccccccccccccccccccccccc) {\n}");
3584 
3585   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
3586                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
3587                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
3588                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
3589   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
3590                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
3591                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
3592                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
3593 
3594   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
3595                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
3596                "    aaaaaaaaaaaaaaa != aa) {\n}");
3597   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
3598                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
3599                "    aaaaaaaaaaaaaaa != aa) {\n}");
3600 }
3601 
3602 TEST_F(FormatTest, BreaksAfterAssignments) {
3603   verifyFormat(
3604       "unsigned Cost =\n"
3605       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
3606       "                        SI->getPointerAddressSpaceee());\n");
3607   verifyFormat(
3608       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
3609       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
3610 
3611   verifyFormat(
3612       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
3613       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
3614   verifyFormat("unsigned OriginalStartColumn =\n"
3615                "    SourceMgr.getSpellingColumnNumber(\n"
3616                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
3617                "    1;");
3618 }
3619 
3620 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
3621   FormatStyle Style = getLLVMStyle();
3622   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3623                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
3624                Style);
3625 
3626   Style.PenaltyBreakAssignment = 20;
3627   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3628                "                                 cccccccccccccccccccccccccc;",
3629                Style);
3630 }
3631 
3632 TEST_F(FormatTest, AlignsAfterAssignments) {
3633   verifyFormat(
3634       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3635       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
3636   verifyFormat(
3637       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3638       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
3639   verifyFormat(
3640       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3641       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
3642   verifyFormat(
3643       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3644       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
3645   verifyFormat(
3646       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
3647       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
3648       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
3649 }
3650 
3651 TEST_F(FormatTest, AlignsAfterReturn) {
3652   verifyFormat(
3653       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3654       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
3655   verifyFormat(
3656       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3657       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
3658   verifyFormat(
3659       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
3660       "       aaaaaaaaaaaaaaaaaaaaaa();");
3661   verifyFormat(
3662       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
3663       "        aaaaaaaaaaaaaaaaaaaaaa());");
3664   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3665                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3666   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3667                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
3668                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3669   verifyFormat("return\n"
3670                "    // true if code is one of a or b.\n"
3671                "    code == a || code == b;");
3672 }
3673 
3674 TEST_F(FormatTest, AlignsAfterOpenBracket) {
3675   verifyFormat(
3676       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
3677       "                                                aaaaaaaaa aaaaaaa) {}");
3678   verifyFormat(
3679       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
3680       "                                               aaaaaaaaaaa aaaaaaaaa);");
3681   verifyFormat(
3682       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
3683       "                                             aaaaaaaaaaaaaaaaaaaaa));");
3684   FormatStyle Style = getLLVMStyle();
3685   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3686   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3687                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
3688                Style);
3689   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
3690                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
3691                Style);
3692   verifyFormat("SomeLongVariableName->someFunction(\n"
3693                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
3694                Style);
3695   verifyFormat(
3696       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
3697       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3698       Style);
3699   verifyFormat(
3700       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
3701       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3702       Style);
3703   verifyFormat(
3704       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
3705       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
3706       Style);
3707 
3708   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
3709                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
3710                "        b));",
3711                Style);
3712 
3713   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
3714   Style.BinPackArguments = false;
3715   Style.BinPackParameters = false;
3716   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3717                "    aaaaaaaaaaa aaaaaaaa,\n"
3718                "    aaaaaaaaa aaaaaaa,\n"
3719                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3720                Style);
3721   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
3722                "    aaaaaaaaaaa aaaaaaaaa,\n"
3723                "    aaaaaaaaaaa aaaaaaaaa,\n"
3724                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3725                Style);
3726   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
3727                "    aaaaaaaaaaaaaaa,\n"
3728                "    aaaaaaaaaaaaaaaaaaaaa,\n"
3729                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
3730                Style);
3731   verifyFormat(
3732       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
3733       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
3734       Style);
3735   verifyFormat(
3736       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
3737       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
3738       Style);
3739   verifyFormat(
3740       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3741       "    aaaaaaaaaaaaaaaaaaaaa(\n"
3742       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
3743       "    aaaaaaaaaaaaaaaa);",
3744       Style);
3745   verifyFormat(
3746       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3747       "    aaaaaaaaaaaaaaaaaaaaa(\n"
3748       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
3749       "    aaaaaaaaaaaaaaaa);",
3750       Style);
3751 }
3752 
3753 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
3754   FormatStyle Style = getLLVMStyleWithColumns(40);
3755   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
3756                "          bbbbbbbbbbbbbbbbbbbbbb);",
3757                Style);
3758   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
3759   Style.AlignOperands = false;
3760   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
3761                "          bbbbbbbbbbbbbbbbbbbbbb);",
3762                Style);
3763   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3764   Style.AlignOperands = true;
3765   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
3766                "          bbbbbbbbbbbbbbbbbbbbbb);",
3767                Style);
3768   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3769   Style.AlignOperands = false;
3770   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
3771                "    bbbbbbbbbbbbbbbbbbbbbb);",
3772                Style);
3773 }
3774 
3775 TEST_F(FormatTest, BreaksConditionalExpressions) {
3776   verifyFormat(
3777       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3778       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3779       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3780   verifyFormat(
3781       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
3782       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3783       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3784   verifyFormat(
3785       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3786       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3787   verifyFormat(
3788       "aaaa(aaaaaaaaa, aaaaaaaaa,\n"
3789       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3790       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3791   verifyFormat(
3792       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
3793       "                                                    : aaaaaaaaaaaaa);");
3794   verifyFormat(
3795       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3796       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3797       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3798       "                   aaaaaaaaaaaaa);");
3799   verifyFormat(
3800       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3801       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3802       "                   aaaaaaaaaaaaa);");
3803   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3804                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3805                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3806                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3807                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3808   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3809                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3810                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3811                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3812                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3813                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3814                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3815   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3816                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3817                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3818                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3819                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3820   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3821                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3822                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3823   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
3824                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3825                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3826                "        : aaaaaaaaaaaaaaaa;");
3827   verifyFormat(
3828       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3829       "    ? aaaaaaaaaaaaaaa\n"
3830       "    : aaaaaaaaaaaaaaa;");
3831   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
3832                "          aaaaaaaaa\n"
3833                "      ? b\n"
3834                "      : c);");
3835   verifyFormat("return aaaa == bbbb\n"
3836                "           // comment\n"
3837                "           ? aaaa\n"
3838                "           : bbbb;");
3839   verifyFormat("unsigned Indent =\n"
3840                "    format(TheLine.First,\n"
3841                "           IndentForLevel[TheLine.Level] >= 0\n"
3842                "               ? IndentForLevel[TheLine.Level]\n"
3843                "               : TheLine * 2,\n"
3844                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
3845                getLLVMStyleWithColumns(60));
3846   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
3847                "                  ? aaaaaaaaaaaaaaa\n"
3848                "                  : bbbbbbbbbbbbbbb //\n"
3849                "                        ? ccccccccccccccc\n"
3850                "                        : ddddddddddddddd;");
3851   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
3852                "                  ? aaaaaaaaaaaaaaa\n"
3853                "                  : (bbbbbbbbbbbbbbb //\n"
3854                "                         ? ccccccccccccccc\n"
3855                "                         : ddddddddddddddd);");
3856   verifyFormat(
3857       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3858       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3859       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
3860       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
3861       "                                      : aaaaaaaaaa;");
3862   verifyFormat(
3863       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3864       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
3865       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3866 
3867   FormatStyle NoBinPacking = getLLVMStyle();
3868   NoBinPacking.BinPackArguments = false;
3869   verifyFormat(
3870       "void f() {\n"
3871       "  g(aaa,\n"
3872       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
3873       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3874       "        ? aaaaaaaaaaaaaaa\n"
3875       "        : aaaaaaaaaaaaaaa);\n"
3876       "}",
3877       NoBinPacking);
3878   verifyFormat(
3879       "void f() {\n"
3880       "  g(aaa,\n"
3881       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
3882       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3883       "        ?: aaaaaaaaaaaaaaa);\n"
3884       "}",
3885       NoBinPacking);
3886 
3887   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
3888                "             // comment.\n"
3889                "             ccccccccccccccccccccccccccccccccccccccc\n"
3890                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3891                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
3892 
3893   // Assignments in conditional expressions. Apparently not uncommon :-(.
3894   verifyFormat("return a != b\n"
3895                "           // comment\n"
3896                "           ? a = b\n"
3897                "           : a = b;");
3898   verifyFormat("return a != b\n"
3899                "           // comment\n"
3900                "           ? a = a != b\n"
3901                "                     // comment\n"
3902                "                     ? a = b\n"
3903                "                     : a\n"
3904                "           : a;\n");
3905   verifyFormat("return a != b\n"
3906                "           // comment\n"
3907                "           ? a\n"
3908                "           : a = a != b\n"
3909                "                     // comment\n"
3910                "                     ? a = b\n"
3911                "                     : a;");
3912 }
3913 
3914 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
3915   FormatStyle Style = getLLVMStyle();
3916   Style.BreakBeforeTernaryOperators = false;
3917   Style.ColumnLimit = 70;
3918   verifyFormat(
3919       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3920       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3921       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3922       Style);
3923   verifyFormat(
3924       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
3925       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3926       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3927       Style);
3928   verifyFormat(
3929       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3930       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3931       Style);
3932   verifyFormat(
3933       "aaaa(aaaaaaaa, aaaaaaaaaa,\n"
3934       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3935       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3936       Style);
3937   verifyFormat(
3938       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
3939       "                                                      aaaaaaaaaaaaa);",
3940       Style);
3941   verifyFormat(
3942       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3943       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3944       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3945       "                   aaaaaaaaaaaaa);",
3946       Style);
3947   verifyFormat(
3948       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3949       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3950       "                   aaaaaaaaaaaaa);",
3951       Style);
3952   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3953                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3954                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
3955                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3956                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3957                Style);
3958   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3959                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3960                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3961                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
3962                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3963                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3964                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3965                Style);
3966   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3967                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
3968                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3969                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3970                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3971                Style);
3972   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3973                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3974                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3975                Style);
3976   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
3977                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3978                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3979                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3980                Style);
3981   verifyFormat(
3982       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3983       "    aaaaaaaaaaaaaaa :\n"
3984       "    aaaaaaaaaaaaaaa;",
3985       Style);
3986   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
3987                "          aaaaaaaaa ?\n"
3988                "      b :\n"
3989                "      c);",
3990                Style);
3991   verifyFormat("unsigned Indent =\n"
3992                "    format(TheLine.First,\n"
3993                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
3994                "               IndentForLevel[TheLine.Level] :\n"
3995                "               TheLine * 2,\n"
3996                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
3997                Style);
3998   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
3999                "                  aaaaaaaaaaaaaaa :\n"
4000                "                  bbbbbbbbbbbbbbb ? //\n"
4001                "                      ccccccccccccccc :\n"
4002                "                      ddddddddddddddd;",
4003                Style);
4004   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4005                "                  aaaaaaaaaaaaaaa :\n"
4006                "                  (bbbbbbbbbbbbbbb ? //\n"
4007                "                       ccccccccccccccc :\n"
4008                "                       ddddddddddddddd);",
4009                Style);
4010   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4011                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
4012                "            ccccccccccccccccccccccccccc;",
4013                Style);
4014   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4015                "           aaaaa :\n"
4016                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
4017                Style);
4018 }
4019 
4020 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
4021   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
4022                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
4023   verifyFormat("bool a = true, b = false;");
4024 
4025   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4026                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
4027                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
4028                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
4029   verifyFormat(
4030       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
4031       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
4032       "     d = e && f;");
4033   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
4034                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
4035   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4036                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
4037   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
4038                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
4039 
4040   FormatStyle Style = getGoogleStyle();
4041   Style.PointerAlignment = FormatStyle::PAS_Left;
4042   Style.DerivePointerAlignment = false;
4043   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4044                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
4045                "    *b = bbbbbbbbbbbbbbbbbbb;",
4046                Style);
4047   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4048                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
4049                Style);
4050   verifyFormat("vector<int*> a, b;", Style);
4051   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
4052 }
4053 
4054 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
4055   verifyFormat("arr[foo ? bar : baz];");
4056   verifyFormat("f()[foo ? bar : baz];");
4057   verifyFormat("(a + b)[foo ? bar : baz];");
4058   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
4059 }
4060 
4061 TEST_F(FormatTest, AlignsStringLiterals) {
4062   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
4063                "                                      \"short literal\");");
4064   verifyFormat(
4065       "looooooooooooooooooooooooongFunction(\n"
4066       "    \"short literal\"\n"
4067       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
4068   verifyFormat("someFunction(\"Always break between multi-line\"\n"
4069                "             \" string literals\",\n"
4070                "             and, other, parameters);");
4071   EXPECT_EQ("fun + \"1243\" /* comment */\n"
4072             "      \"5678\";",
4073             format("fun + \"1243\" /* comment */\n"
4074                    "    \"5678\";",
4075                    getLLVMStyleWithColumns(28)));
4076   EXPECT_EQ(
4077       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
4078       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
4079       "         \"aaaaaaaaaaaaaaaa\";",
4080       format("aaaaaa ="
4081              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
4082              "aaaaaaaaaaaaaaaaaaaaa\" "
4083              "\"aaaaaaaaaaaaaaaa\";"));
4084   verifyFormat("a = a + \"a\"\n"
4085                "        \"a\"\n"
4086                "        \"a\";");
4087   verifyFormat("f(\"a\", \"b\"\n"
4088                "       \"c\");");
4089 
4090   verifyFormat(
4091       "#define LL_FORMAT \"ll\"\n"
4092       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
4093       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
4094 
4095   verifyFormat("#define A(X)          \\\n"
4096                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
4097                "  \"ccccc\"",
4098                getLLVMStyleWithColumns(23));
4099   verifyFormat("#define A \"def\"\n"
4100                "f(\"abc\" A \"ghi\"\n"
4101                "  \"jkl\");");
4102 
4103   verifyFormat("f(L\"a\"\n"
4104                "  L\"b\");");
4105   verifyFormat("#define A(X)            \\\n"
4106                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
4107                "  L\"ccccc\"",
4108                getLLVMStyleWithColumns(25));
4109 
4110   verifyFormat("f(@\"a\"\n"
4111                "  @\"b\");");
4112   verifyFormat("NSString s = @\"a\"\n"
4113                "             @\"b\"\n"
4114                "             @\"c\";");
4115   verifyFormat("NSString s = @\"a\"\n"
4116                "              \"b\"\n"
4117                "              \"c\";");
4118 }
4119 
4120 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
4121   FormatStyle Style = getLLVMStyle();
4122   // No declarations or definitions should be moved to own line.
4123   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
4124   verifyFormat("class A {\n"
4125                "  int f() { return 1; }\n"
4126                "  int g();\n"
4127                "};\n"
4128                "int f() { return 1; }\n"
4129                "int g();\n",
4130                Style);
4131 
4132   // All declarations and definitions should have the return type moved to its
4133   // own
4134   // line.
4135   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
4136   verifyFormat("class E {\n"
4137                "  int\n"
4138                "  f() {\n"
4139                "    return 1;\n"
4140                "  }\n"
4141                "  int\n"
4142                "  g();\n"
4143                "};\n"
4144                "int\n"
4145                "f() {\n"
4146                "  return 1;\n"
4147                "}\n"
4148                "int\n"
4149                "g();\n",
4150                Style);
4151 
4152   // Top-level definitions, and no kinds of declarations should have the
4153   // return type moved to its own line.
4154   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
4155   verifyFormat("class B {\n"
4156                "  int f() { return 1; }\n"
4157                "  int g();\n"
4158                "};\n"
4159                "int\n"
4160                "f() {\n"
4161                "  return 1;\n"
4162                "}\n"
4163                "int g();\n",
4164                Style);
4165 
4166   // Top-level definitions and declarations should have the return type moved
4167   // to its own line.
4168   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
4169   verifyFormat("class C {\n"
4170                "  int f() { return 1; }\n"
4171                "  int g();\n"
4172                "};\n"
4173                "int\n"
4174                "f() {\n"
4175                "  return 1;\n"
4176                "}\n"
4177                "int\n"
4178                "g();\n",
4179                Style);
4180 
4181   // All definitions should have the return type moved to its own line, but no
4182   // kinds of declarations.
4183   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
4184   verifyFormat("class D {\n"
4185                "  int\n"
4186                "  f() {\n"
4187                "    return 1;\n"
4188                "  }\n"
4189                "  int g();\n"
4190                "};\n"
4191                "int\n"
4192                "f() {\n"
4193                "  return 1;\n"
4194                "}\n"
4195                "int g();\n",
4196                Style);
4197   verifyFormat("const char *\n"
4198                "f(void) {\n" // Break here.
4199                "  return \"\";\n"
4200                "}\n"
4201                "const char *bar(void);\n", // No break here.
4202                Style);
4203   verifyFormat("template <class T>\n"
4204                "T *\n"
4205                "f(T &c) {\n" // Break here.
4206                "  return NULL;\n"
4207                "}\n"
4208                "template <class T> T *f(T &c);\n", // No break here.
4209                Style);
4210   verifyFormat("class C {\n"
4211                "  int\n"
4212                "  operator+() {\n"
4213                "    return 1;\n"
4214                "  }\n"
4215                "  int\n"
4216                "  operator()() {\n"
4217                "    return 1;\n"
4218                "  }\n"
4219                "};\n",
4220                Style);
4221   verifyFormat("void\n"
4222                "A::operator()() {}\n"
4223                "void\n"
4224                "A::operator>>() {}\n"
4225                "void\n"
4226                "A::operator+() {}\n",
4227                Style);
4228   verifyFormat("void *operator new(std::size_t s);", // No break here.
4229                Style);
4230   verifyFormat("void *\n"
4231                "operator new(std::size_t s) {}",
4232                Style);
4233   verifyFormat("void *\n"
4234                "operator delete[](void *ptr) {}",
4235                Style);
4236   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4237   verifyFormat("const char *\n"
4238                "f(void)\n" // Break here.
4239                "{\n"
4240                "  return \"\";\n"
4241                "}\n"
4242                "const char *bar(void);\n", // No break here.
4243                Style);
4244   verifyFormat("template <class T>\n"
4245                "T *\n"     // Problem here: no line break
4246                "f(T &c)\n" // Break here.
4247                "{\n"
4248                "  return NULL;\n"
4249                "}\n"
4250                "template <class T> T *f(T &c);\n", // No break here.
4251                Style);
4252 }
4253 
4254 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
4255   FormatStyle NoBreak = getLLVMStyle();
4256   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
4257   FormatStyle Break = getLLVMStyle();
4258   Break.AlwaysBreakBeforeMultilineStrings = true;
4259   verifyFormat("aaaa = \"bbbb\"\n"
4260                "       \"cccc\";",
4261                NoBreak);
4262   verifyFormat("aaaa =\n"
4263                "    \"bbbb\"\n"
4264                "    \"cccc\";",
4265                Break);
4266   verifyFormat("aaaa(\"bbbb\"\n"
4267                "     \"cccc\");",
4268                NoBreak);
4269   verifyFormat("aaaa(\n"
4270                "    \"bbbb\"\n"
4271                "    \"cccc\");",
4272                Break);
4273   verifyFormat("aaaa(qqq, \"bbbb\"\n"
4274                "          \"cccc\");",
4275                NoBreak);
4276   verifyFormat("aaaa(qqq,\n"
4277                "     \"bbbb\"\n"
4278                "     \"cccc\");",
4279                Break);
4280   verifyFormat("aaaa(qqq,\n"
4281                "     L\"bbbb\"\n"
4282                "     L\"cccc\");",
4283                Break);
4284   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
4285                "                      \"bbbb\"));",
4286                Break);
4287   verifyFormat("string s = someFunction(\n"
4288                "    \"abc\"\n"
4289                "    \"abc\");",
4290                Break);
4291 
4292   // As we break before unary operators, breaking right after them is bad.
4293   verifyFormat("string foo = abc ? \"x\"\n"
4294                "                   \"blah blah blah blah blah blah\"\n"
4295                "                 : \"y\";",
4296                Break);
4297 
4298   // Don't break if there is no column gain.
4299   verifyFormat("f(\"aaaa\"\n"
4300                "  \"bbbb\");",
4301                Break);
4302 
4303   // Treat literals with escaped newlines like multi-line string literals.
4304   EXPECT_EQ("x = \"a\\\n"
4305             "b\\\n"
4306             "c\";",
4307             format("x = \"a\\\n"
4308                    "b\\\n"
4309                    "c\";",
4310                    NoBreak));
4311   EXPECT_EQ("xxxx =\n"
4312             "    \"a\\\n"
4313             "b\\\n"
4314             "c\";",
4315             format("xxxx = \"a\\\n"
4316                    "b\\\n"
4317                    "c\";",
4318                    Break));
4319 
4320   EXPECT_EQ("NSString *const kString =\n"
4321             "    @\"aaaa\"\n"
4322             "    @\"bbbb\";",
4323             format("NSString *const kString = @\"aaaa\"\n"
4324                    "@\"bbbb\";",
4325                    Break));
4326 
4327   Break.ColumnLimit = 0;
4328   verifyFormat("const char *hello = \"hello llvm\";", Break);
4329 }
4330 
4331 TEST_F(FormatTest, AlignsPipes) {
4332   verifyFormat(
4333       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4334       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4335       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4336   verifyFormat(
4337       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
4338       "                     << aaaaaaaaaaaaaaaaaaaa;");
4339   verifyFormat(
4340       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4341       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4342   verifyFormat(
4343       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4344       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4345   verifyFormat(
4346       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
4347       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
4348       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
4349   verifyFormat(
4350       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4351       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4352       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4353   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4354                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4355                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4356                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4357   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
4358                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
4359   verifyFormat(
4360       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4361       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4362   verifyFormat(
4363       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
4364       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
4365 
4366   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
4367                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
4368   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4369                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4370                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
4371                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
4372   verifyFormat("LOG_IF(aaa == //\n"
4373                "       bbb)\n"
4374                "    << a << b;");
4375 
4376   // But sometimes, breaking before the first "<<" is desirable.
4377   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4378                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
4379   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
4380                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4381                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4382   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
4383                "    << BEF << IsTemplate << Description << E->getType();");
4384   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4385                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4386                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4387   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4388                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4389                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4390                "    << aaa;");
4391 
4392   verifyFormat(
4393       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4394       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4395 
4396   // Incomplete string literal.
4397   EXPECT_EQ("llvm::errs() << \"\n"
4398             "             << a;",
4399             format("llvm::errs() << \"\n<<a;"));
4400 
4401   verifyFormat("void f() {\n"
4402                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
4403                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
4404                "}");
4405 
4406   // Handle 'endl'.
4407   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
4408                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4409   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4410 
4411   // Handle '\n'.
4412   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
4413                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
4414   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
4415                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
4416   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
4417                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
4418   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
4419 }
4420 
4421 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
4422   verifyFormat("return out << \"somepacket = {\\n\"\n"
4423                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
4424                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
4425                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
4426                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
4427                "           << \"}\";");
4428 
4429   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4430                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4431                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
4432   verifyFormat(
4433       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
4434       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
4435       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
4436       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
4437       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
4438   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
4439                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4440   verifyFormat(
4441       "void f() {\n"
4442       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
4443       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4444       "}");
4445 
4446   // Breaking before the first "<<" is generally not desirable.
4447   verifyFormat(
4448       "llvm::errs()\n"
4449       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4450       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4451       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4452       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4453       getLLVMStyleWithColumns(70));
4454   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4455                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4456                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4457                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4458                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4459                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4460                getLLVMStyleWithColumns(70));
4461 
4462   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
4463                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
4464                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
4465   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
4466                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
4467                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
4468   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
4469                "           (aaaa + aaaa);",
4470                getLLVMStyleWithColumns(40));
4471   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
4472                "                  (aaaaaaa + aaaaa));",
4473                getLLVMStyleWithColumns(40));
4474   verifyFormat(
4475       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
4476       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
4477       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
4478 }
4479 
4480 TEST_F(FormatTest, UnderstandsEquals) {
4481   verifyFormat(
4482       "aaaaaaaaaaaaaaaaa =\n"
4483       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4484   verifyFormat(
4485       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4486       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4487   verifyFormat(
4488       "if (a) {\n"
4489       "  f();\n"
4490       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4491       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
4492       "}");
4493 
4494   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4495                "        100000000 + 10000000) {\n}");
4496 }
4497 
4498 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
4499   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4500                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
4501 
4502   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4503                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
4504 
4505   verifyFormat(
4506       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
4507       "                                                          Parameter2);");
4508 
4509   verifyFormat(
4510       "ShortObject->shortFunction(\n"
4511       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
4512       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
4513 
4514   verifyFormat("loooooooooooooongFunction(\n"
4515                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
4516 
4517   verifyFormat(
4518       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
4519       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
4520 
4521   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
4522                "    .WillRepeatedly(Return(SomeValue));");
4523   verifyFormat("void f() {\n"
4524                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
4525                "      .Times(2)\n"
4526                "      .WillRepeatedly(Return(SomeValue));\n"
4527                "}");
4528   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
4529                "    ccccccccccccccccccccccc);");
4530   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4531                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4532                "          .aaaaa(aaaaa),\n"
4533                "      aaaaaaaaaaaaaaaaaaaaa);");
4534   verifyFormat("void f() {\n"
4535                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4536                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
4537                "}");
4538   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4539                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4540                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4541                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4542                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4543   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4544                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4545                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4546                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
4547                "}");
4548 
4549   // Here, it is not necessary to wrap at "." or "->".
4550   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
4551                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4552   verifyFormat(
4553       "aaaaaaaaaaa->aaaaaaaaa(\n"
4554       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4555       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
4556 
4557   verifyFormat(
4558       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4559       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
4560   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
4561                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
4562   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
4563                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
4564 
4565   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4566                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4567                "    .a();");
4568 
4569   FormatStyle NoBinPacking = getLLVMStyle();
4570   NoBinPacking.BinPackParameters = false;
4571   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
4572                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
4573                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
4574                "                         aaaaaaaaaaaaaaaaaaa,\n"
4575                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4576                NoBinPacking);
4577 
4578   // If there is a subsequent call, change to hanging indentation.
4579   verifyFormat(
4580       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4581       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
4582       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4583   verifyFormat(
4584       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4585       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
4586   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4587                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4588                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4589   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4590                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4591                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
4592 }
4593 
4594 TEST_F(FormatTest, WrapsTemplateDeclarations) {
4595   verifyFormat("template <typename T>\n"
4596                "virtual void loooooooooooongFunction(int Param1, int Param2);");
4597   verifyFormat("template <typename T>\n"
4598                "// T should be one of {A, B}.\n"
4599                "virtual void loooooooooooongFunction(int Param1, int Param2);");
4600   verifyFormat(
4601       "template <typename T>\n"
4602       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
4603   verifyFormat("template <typename T>\n"
4604                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
4605                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
4606   verifyFormat(
4607       "template <typename T>\n"
4608       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
4609       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
4610   verifyFormat(
4611       "template <typename T>\n"
4612       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
4613       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
4614       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4615   verifyFormat("template <typename T>\n"
4616                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4617                "    int aaaaaaaaaaaaaaaaaaaaaa);");
4618   verifyFormat(
4619       "template <typename T1, typename T2 = char, typename T3 = char,\n"
4620       "          typename T4 = char>\n"
4621       "void f();");
4622   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
4623                "          template <typename> class cccccccccccccccccccccc,\n"
4624                "          typename ddddddddddddd>\n"
4625                "class C {};");
4626   verifyFormat(
4627       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
4628       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4629 
4630   verifyFormat("void f() {\n"
4631                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
4632                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
4633                "}");
4634 
4635   verifyFormat("template <typename T> class C {};");
4636   verifyFormat("template <typename T> void f();");
4637   verifyFormat("template <typename T> void f() {}");
4638   verifyFormat(
4639       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
4640       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4641       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
4642       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
4643       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4644       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
4645       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
4646       getLLVMStyleWithColumns(72));
4647   EXPECT_EQ("static_cast<A< //\n"
4648             "    B> *>(\n"
4649             "\n"
4650             ");",
4651             format("static_cast<A<//\n"
4652                    "    B>*>(\n"
4653                    "\n"
4654                    "    );"));
4655   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4656                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
4657 
4658   FormatStyle AlwaysBreak = getLLVMStyle();
4659   AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
4660   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
4661   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
4662   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
4663   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4664                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
4665                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
4666   verifyFormat("template <template <typename> class Fooooooo,\n"
4667                "          template <typename> class Baaaaaaar>\n"
4668                "struct C {};",
4669                AlwaysBreak);
4670   verifyFormat("template <typename T> // T can be A, B or C.\n"
4671                "struct C {};",
4672                AlwaysBreak);
4673   verifyFormat("template <enum E> class A {\n"
4674                "public:\n"
4675                "  E *f();\n"
4676                "};");
4677 }
4678 
4679 TEST_F(FormatTest, WrapsTemplateParameters) {
4680   FormatStyle Style = getLLVMStyle();
4681   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4682   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
4683   verifyFormat(
4684       "template <typename... a> struct q {};\n"
4685       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
4686       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
4687       "    y;",
4688       Style);
4689   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4690   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
4691   verifyFormat(
4692       "template <typename... a> struct r {};\n"
4693       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
4694       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
4695       "    y;",
4696       Style);
4697   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4698   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
4699   verifyFormat(
4700       "template <typename... a> struct s {};\n"
4701       "extern s<\n"
4702       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
4703       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
4704       "    y;",
4705       Style);
4706   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4707   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
4708   verifyFormat(
4709       "template <typename... a> struct t {};\n"
4710       "extern t<\n"
4711       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
4712       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
4713       "    y;",
4714       Style);
4715 }
4716 
4717 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
4718   verifyFormat(
4719       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4720       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4721   verifyFormat(
4722       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4723       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4724       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
4725 
4726   // FIXME: Should we have the extra indent after the second break?
4727   verifyFormat(
4728       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4729       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4730       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4731 
4732   verifyFormat(
4733       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
4734       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
4735 
4736   // Breaking at nested name specifiers is generally not desirable.
4737   verifyFormat(
4738       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4739       "    aaaaaaaaaaaaaaaaaaaaaaa);");
4740 
4741   verifyFormat(
4742       "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
4743       "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4744       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4745       "                   aaaaaaaaaaaaaaaaaaaaa);",
4746       getLLVMStyleWithColumns(74));
4747 
4748   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4749                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4750                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4751 }
4752 
4753 TEST_F(FormatTest, UnderstandsTemplateParameters) {
4754   verifyFormat("A<int> a;");
4755   verifyFormat("A<A<A<int>>> a;");
4756   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
4757   verifyFormat("bool x = a < 1 || 2 > a;");
4758   verifyFormat("bool x = 5 < f<int>();");
4759   verifyFormat("bool x = f<int>() > 5;");
4760   verifyFormat("bool x = 5 < a<int>::x;");
4761   verifyFormat("bool x = a < 4 ? a > 2 : false;");
4762   verifyFormat("bool x = f() ? a < 2 : a > 2;");
4763 
4764   verifyGoogleFormat("A<A<int>> a;");
4765   verifyGoogleFormat("A<A<A<int>>> a;");
4766   verifyGoogleFormat("A<A<A<A<int>>>> a;");
4767   verifyGoogleFormat("A<A<int> > a;");
4768   verifyGoogleFormat("A<A<A<int> > > a;");
4769   verifyGoogleFormat("A<A<A<A<int> > > > a;");
4770   verifyGoogleFormat("A<::A<int>> a;");
4771   verifyGoogleFormat("A<::A> a;");
4772   verifyGoogleFormat("A< ::A> a;");
4773   verifyGoogleFormat("A< ::A<int> > a;");
4774   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
4775   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
4776   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
4777   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
4778   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
4779             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
4780 
4781   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
4782 
4783   verifyFormat("test >> a >> b;");
4784   verifyFormat("test << a >> b;");
4785 
4786   verifyFormat("f<int>();");
4787   verifyFormat("template <typename T> void f() {}");
4788   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
4789   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
4790                "sizeof(char)>::type>;");
4791   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
4792   verifyFormat("f(a.operator()<A>());");
4793   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4794                "      .template operator()<A>());",
4795                getLLVMStyleWithColumns(35));
4796 
4797   // Not template parameters.
4798   verifyFormat("return a < b && c > d;");
4799   verifyFormat("void f() {\n"
4800                "  while (a < b && c > d) {\n"
4801                "  }\n"
4802                "}");
4803   verifyFormat("template <typename... Types>\n"
4804                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
4805 
4806   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4807                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
4808                getLLVMStyleWithColumns(60));
4809   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
4810   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
4811   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
4812 }
4813 
4814 TEST_F(FormatTest, BitshiftOperatorWidth) {
4815   EXPECT_EQ("int a = 1 << 2; /* foo\n"
4816             "                   bar */",
4817             format("int    a=1<<2;  /* foo\n"
4818                    "                   bar */"));
4819 
4820   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
4821             "                     bar */",
4822             format("int  b  =256>>1 ;  /* foo\n"
4823                    "                      bar */"));
4824 }
4825 
4826 TEST_F(FormatTest, UnderstandsBinaryOperators) {
4827   verifyFormat("COMPARE(a, ==, b);");
4828   verifyFormat("auto s = sizeof...(Ts) - 1;");
4829 }
4830 
4831 TEST_F(FormatTest, UnderstandsPointersToMembers) {
4832   verifyFormat("int A::*x;");
4833   verifyFormat("int (S::*func)(void *);");
4834   verifyFormat("void f() { int (S::*func)(void *); }");
4835   verifyFormat("typedef bool *(Class::*Member)() const;");
4836   verifyFormat("void f() {\n"
4837                "  (a->*f)();\n"
4838                "  a->*x;\n"
4839                "  (a.*f)();\n"
4840                "  ((*a).*f)();\n"
4841                "  a.*x;\n"
4842                "}");
4843   verifyFormat("void f() {\n"
4844                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
4845                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
4846                "}");
4847   verifyFormat(
4848       "(aaaaaaaaaa->*bbbbbbb)(\n"
4849       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4850   FormatStyle Style = getLLVMStyle();
4851   Style.PointerAlignment = FormatStyle::PAS_Left;
4852   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
4853 }
4854 
4855 TEST_F(FormatTest, UnderstandsUnaryOperators) {
4856   verifyFormat("int a = -2;");
4857   verifyFormat("f(-1, -2, -3);");
4858   verifyFormat("a[-1] = 5;");
4859   verifyFormat("int a = 5 + -2;");
4860   verifyFormat("if (i == -1) {\n}");
4861   verifyFormat("if (i != -1) {\n}");
4862   verifyFormat("if (i > -1) {\n}");
4863   verifyFormat("if (i < -1) {\n}");
4864   verifyFormat("++(a->f());");
4865   verifyFormat("--(a->f());");
4866   verifyFormat("(a->f())++;");
4867   verifyFormat("a[42]++;");
4868   verifyFormat("if (!(a->f())) {\n}");
4869 
4870   verifyFormat("a-- > b;");
4871   verifyFormat("b ? -a : c;");
4872   verifyFormat("n * sizeof char16;");
4873   verifyFormat("n * alignof char16;", getGoogleStyle());
4874   verifyFormat("sizeof(char);");
4875   verifyFormat("alignof(char);", getGoogleStyle());
4876 
4877   verifyFormat("return -1;");
4878   verifyFormat("switch (a) {\n"
4879                "case -1:\n"
4880                "  break;\n"
4881                "}");
4882   verifyFormat("#define X -1");
4883   verifyFormat("#define X -kConstant");
4884 
4885   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
4886   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
4887 
4888   verifyFormat("int a = /* confusing comment */ -1;");
4889   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
4890   verifyFormat("int a = i /* confusing comment */++;");
4891 }
4892 
4893 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
4894   verifyFormat("if (!aaaaaaaaaa( // break\n"
4895                "        aaaaa)) {\n"
4896                "}");
4897   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
4898                "    aaaaa));");
4899   verifyFormat("*aaa = aaaaaaa( // break\n"
4900                "    bbbbbb);");
4901 }
4902 
4903 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
4904   verifyFormat("bool operator<();");
4905   verifyFormat("bool operator>();");
4906   verifyFormat("bool operator=();");
4907   verifyFormat("bool operator==();");
4908   verifyFormat("bool operator!=();");
4909   verifyFormat("int operator+();");
4910   verifyFormat("int operator++();");
4911   verifyFormat("bool operator,();");
4912   verifyFormat("bool operator();");
4913   verifyFormat("bool operator()();");
4914   verifyFormat("bool operator[]();");
4915   verifyFormat("operator bool();");
4916   verifyFormat("operator int();");
4917   verifyFormat("operator void *();");
4918   verifyFormat("operator SomeType<int>();");
4919   verifyFormat("operator SomeType<int, int>();");
4920   verifyFormat("operator SomeType<SomeType<int>>();");
4921   verifyFormat("void *operator new(std::size_t size);");
4922   verifyFormat("void *operator new[](std::size_t size);");
4923   verifyFormat("void operator delete(void *ptr);");
4924   verifyFormat("void operator delete[](void *ptr);");
4925   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
4926                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
4927   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
4928                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
4929 
4930   verifyFormat(
4931       "ostream &operator<<(ostream &OutputStream,\n"
4932       "                    SomeReallyLongType WithSomeReallyLongValue);");
4933   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
4934                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
4935                "  return left.group < right.group;\n"
4936                "}");
4937   verifyFormat("SomeType &operator=(const SomeType &S);");
4938   verifyFormat("f.template operator()<int>();");
4939 
4940   verifyGoogleFormat("operator void*();");
4941   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
4942   verifyGoogleFormat("operator ::A();");
4943 
4944   verifyFormat("using A::operator+;");
4945   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
4946                "int i;");
4947 }
4948 
4949 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
4950   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
4951   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
4952   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
4953   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
4954   verifyFormat("Deleted &operator=(const Deleted &) &;");
4955   verifyFormat("Deleted &operator=(const Deleted &) &&;");
4956   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
4957   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
4958   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
4959   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
4960   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
4961   verifyFormat("SomeType MemberFunction(const Deleted &) const &;");
4962   verifyFormat("template <typename T>\n"
4963                "void F(T) && = delete;",
4964                getGoogleStyle());
4965 
4966   FormatStyle AlignLeft = getLLVMStyle();
4967   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
4968   verifyFormat("void A::b() && {}", AlignLeft);
4969   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
4970   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
4971                AlignLeft);
4972   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
4973   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
4974   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
4975   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
4976   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
4977   verifyFormat("auto Function(T) & -> void;", AlignLeft);
4978   verifyFormat("SomeType MemberFunction(const Deleted&) const &;", AlignLeft);
4979 
4980   FormatStyle Spaces = getLLVMStyle();
4981   Spaces.SpacesInCStyleCastParentheses = true;
4982   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
4983   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
4984   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
4985   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
4986 
4987   Spaces.SpacesInCStyleCastParentheses = false;
4988   Spaces.SpacesInParentheses = true;
4989   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
4990   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
4991   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
4992   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
4993 }
4994 
4995 TEST_F(FormatTest, UnderstandsNewAndDelete) {
4996   verifyFormat("void f() {\n"
4997                "  A *a = new A;\n"
4998                "  A *a = new (placement) A;\n"
4999                "  delete a;\n"
5000                "  delete (A *)a;\n"
5001                "}");
5002   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5003                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5004   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5005                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5006                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5007   verifyFormat("delete[] h->p;");
5008 }
5009 
5010 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
5011   verifyFormat("int *f(int *a) {}");
5012   verifyFormat("int main(int argc, char **argv) {}");
5013   verifyFormat("Test::Test(int b) : a(b * b) {}");
5014   verifyIndependentOfContext("f(a, *a);");
5015   verifyFormat("void g() { f(*a); }");
5016   verifyIndependentOfContext("int a = b * 10;");
5017   verifyIndependentOfContext("int a = 10 * b;");
5018   verifyIndependentOfContext("int a = b * c;");
5019   verifyIndependentOfContext("int a += b * c;");
5020   verifyIndependentOfContext("int a -= b * c;");
5021   verifyIndependentOfContext("int a *= b * c;");
5022   verifyIndependentOfContext("int a /= b * c;");
5023   verifyIndependentOfContext("int a = *b;");
5024   verifyIndependentOfContext("int a = *b * c;");
5025   verifyIndependentOfContext("int a = b * *c;");
5026   verifyIndependentOfContext("int a = b * (10);");
5027   verifyIndependentOfContext("S << b * (10);");
5028   verifyIndependentOfContext("return 10 * b;");
5029   verifyIndependentOfContext("return *b * *c;");
5030   verifyIndependentOfContext("return a & ~b;");
5031   verifyIndependentOfContext("f(b ? *c : *d);");
5032   verifyIndependentOfContext("int a = b ? *c : *d;");
5033   verifyIndependentOfContext("*b = a;");
5034   verifyIndependentOfContext("a * ~b;");
5035   verifyIndependentOfContext("a * !b;");
5036   verifyIndependentOfContext("a * +b;");
5037   verifyIndependentOfContext("a * -b;");
5038   verifyIndependentOfContext("a * ++b;");
5039   verifyIndependentOfContext("a * --b;");
5040   verifyIndependentOfContext("a[4] * b;");
5041   verifyIndependentOfContext("a[a * a] = 1;");
5042   verifyIndependentOfContext("f() * b;");
5043   verifyIndependentOfContext("a * [self dostuff];");
5044   verifyIndependentOfContext("int x = a * (a + b);");
5045   verifyIndependentOfContext("(a *)(a + b);");
5046   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
5047   verifyIndependentOfContext("int *pa = (int *)&a;");
5048   verifyIndependentOfContext("return sizeof(int **);");
5049   verifyIndependentOfContext("return sizeof(int ******);");
5050   verifyIndependentOfContext("return (int **&)a;");
5051   verifyIndependentOfContext("f((*PointerToArray)[10]);");
5052   verifyFormat("void f(Type (*parameter)[10]) {}");
5053   verifyFormat("void f(Type (&parameter)[10]) {}");
5054   verifyGoogleFormat("return sizeof(int**);");
5055   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
5056   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
5057   verifyFormat("auto a = [](int **&, int ***) {};");
5058   verifyFormat("auto PointerBinding = [](const char *S) {};");
5059   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
5060   verifyFormat("[](const decltype(*a) &value) {}");
5061   verifyFormat("decltype(a * b) F();");
5062   verifyFormat("#define MACRO() [](A *a) { return 1; }");
5063   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
5064   verifyIndependentOfContext("typedef void (*f)(int *a);");
5065   verifyIndependentOfContext("int i{a * b};");
5066   verifyIndependentOfContext("aaa && aaa->f();");
5067   verifyIndependentOfContext("int x = ~*p;");
5068   verifyFormat("Constructor() : a(a), area(width * height) {}");
5069   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
5070   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
5071   verifyFormat("void f() { f(a, c * d); }");
5072   verifyFormat("void f() { f(new a(), c * d); }");
5073   verifyFormat("void f(const MyOverride &override);");
5074   verifyFormat("void f(const MyFinal &final);");
5075   verifyIndependentOfContext("bool a = f() && override.f();");
5076   verifyIndependentOfContext("bool a = f() && final.f();");
5077 
5078   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
5079 
5080   verifyIndependentOfContext("A<int *> a;");
5081   verifyIndependentOfContext("A<int **> a;");
5082   verifyIndependentOfContext("A<int *, int *> a;");
5083   verifyIndependentOfContext("A<int *[]> a;");
5084   verifyIndependentOfContext(
5085       "const char *const p = reinterpret_cast<const char *const>(q);");
5086   verifyIndependentOfContext("A<int **, int **> a;");
5087   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
5088   verifyFormat("for (char **a = b; *a; ++a) {\n}");
5089   verifyFormat("for (; a && b;) {\n}");
5090   verifyFormat("bool foo = true && [] { return false; }();");
5091 
5092   verifyFormat(
5093       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5094       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5095 
5096   verifyGoogleFormat("int const* a = &b;");
5097   verifyGoogleFormat("**outparam = 1;");
5098   verifyGoogleFormat("*outparam = a * b;");
5099   verifyGoogleFormat("int main(int argc, char** argv) {}");
5100   verifyGoogleFormat("A<int*> a;");
5101   verifyGoogleFormat("A<int**> a;");
5102   verifyGoogleFormat("A<int*, int*> a;");
5103   verifyGoogleFormat("A<int**, int**> a;");
5104   verifyGoogleFormat("f(b ? *c : *d);");
5105   verifyGoogleFormat("int a = b ? *c : *d;");
5106   verifyGoogleFormat("Type* t = **x;");
5107   verifyGoogleFormat("Type* t = *++*x;");
5108   verifyGoogleFormat("*++*x;");
5109   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
5110   verifyGoogleFormat("Type* t = x++ * y;");
5111   verifyGoogleFormat(
5112       "const char* const p = reinterpret_cast<const char* const>(q);");
5113   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
5114   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
5115   verifyGoogleFormat("template <typename T>\n"
5116                      "void f(int i = 0, SomeType** temps = NULL);");
5117 
5118   FormatStyle Left = getLLVMStyle();
5119   Left.PointerAlignment = FormatStyle::PAS_Left;
5120   verifyFormat("x = *a(x) = *a(y);", Left);
5121   verifyFormat("for (;; *a = b) {\n}", Left);
5122   verifyFormat("return *this += 1;", Left);
5123 
5124   verifyIndependentOfContext("a = *(x + y);");
5125   verifyIndependentOfContext("a = &(x + y);");
5126   verifyIndependentOfContext("*(x + y).call();");
5127   verifyIndependentOfContext("&(x + y)->call();");
5128   verifyFormat("void f() { &(*I).first; }");
5129 
5130   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
5131   verifyFormat(
5132       "int *MyValues = {\n"
5133       "    *A, // Operator detection might be confused by the '{'\n"
5134       "    *BB // Operator detection might be confused by previous comment\n"
5135       "};");
5136 
5137   verifyIndependentOfContext("if (int *a = &b)");
5138   verifyIndependentOfContext("if (int &a = *b)");
5139   verifyIndependentOfContext("if (a & b[i])");
5140   verifyIndependentOfContext("if (a::b::c::d & b[i])");
5141   verifyIndependentOfContext("if (*b[i])");
5142   verifyIndependentOfContext("if (int *a = (&b))");
5143   verifyIndependentOfContext("while (int *a = &b)");
5144   verifyIndependentOfContext("size = sizeof *a;");
5145   verifyIndependentOfContext("if (a && (b = c))");
5146   verifyFormat("void f() {\n"
5147                "  for (const int &v : Values) {\n"
5148                "  }\n"
5149                "}");
5150   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
5151   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
5152   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
5153 
5154   verifyFormat("#define A (!a * b)");
5155   verifyFormat("#define MACRO     \\\n"
5156                "  int *i = a * b; \\\n"
5157                "  void f(a *b);",
5158                getLLVMStyleWithColumns(19));
5159 
5160   verifyIndependentOfContext("A = new SomeType *[Length];");
5161   verifyIndependentOfContext("A = new SomeType *[Length]();");
5162   verifyIndependentOfContext("T **t = new T *;");
5163   verifyIndependentOfContext("T **t = new T *();");
5164   verifyGoogleFormat("A = new SomeType*[Length]();");
5165   verifyGoogleFormat("A = new SomeType*[Length];");
5166   verifyGoogleFormat("T** t = new T*;");
5167   verifyGoogleFormat("T** t = new T*();");
5168 
5169   FormatStyle PointerLeft = getLLVMStyle();
5170   PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
5171   verifyFormat("delete *x;", PointerLeft);
5172   verifyFormat("STATIC_ASSERT((a & b) == 0);");
5173   verifyFormat("STATIC_ASSERT(0 == (a & b));");
5174   verifyFormat("template <bool a, bool b> "
5175                "typename t::if<x && y>::type f() {}");
5176   verifyFormat("template <int *y> f() {}");
5177   verifyFormat("vector<int *> v;");
5178   verifyFormat("vector<int *const> v;");
5179   verifyFormat("vector<int *const **const *> v;");
5180   verifyFormat("vector<int *volatile> v;");
5181   verifyFormat("vector<a * b> v;");
5182   verifyFormat("foo<b && false>();");
5183   verifyFormat("foo<b & 1>();");
5184   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
5185   verifyFormat(
5186       "template <class T, class = typename std::enable_if<\n"
5187       "                       std::is_integral<T>::value &&\n"
5188       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
5189       "void F();",
5190       getLLVMStyleWithColumns(70));
5191   verifyFormat(
5192       "template <class T,\n"
5193       "          class = typename std::enable_if<\n"
5194       "              std::is_integral<T>::value &&\n"
5195       "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
5196       "          class U>\n"
5197       "void F();",
5198       getLLVMStyleWithColumns(70));
5199   verifyFormat(
5200       "template <class T,\n"
5201       "          class = typename ::std::enable_if<\n"
5202       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
5203       "void F();",
5204       getGoogleStyleWithColumns(68));
5205 
5206   verifyIndependentOfContext("MACRO(int *i);");
5207   verifyIndependentOfContext("MACRO(auto *a);");
5208   verifyIndependentOfContext("MACRO(const A *a);");
5209   verifyIndependentOfContext("MACRO(A *const a);");
5210   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
5211   verifyFormat("void f() { f(float{1}, a * a); }");
5212   // FIXME: Is there a way to make this work?
5213   // verifyIndependentOfContext("MACRO(A *a);");
5214 
5215   verifyFormat("DatumHandle const *operator->() const { return input_; }");
5216   verifyFormat("return options != nullptr && operator==(*options);");
5217 
5218   EXPECT_EQ("#define OP(x)                                    \\\n"
5219             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
5220             "    return s << a.DebugString();                 \\\n"
5221             "  }",
5222             format("#define OP(x) \\\n"
5223                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
5224                    "    return s << a.DebugString(); \\\n"
5225                    "  }",
5226                    getLLVMStyleWithColumns(50)));
5227 
5228   // FIXME: We cannot handle this case yet; we might be able to figure out that
5229   // foo<x> d > v; doesn't make sense.
5230   verifyFormat("foo<a<b && c> d> v;");
5231 
5232   FormatStyle PointerMiddle = getLLVMStyle();
5233   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
5234   verifyFormat("delete *x;", PointerMiddle);
5235   verifyFormat("int * x;", PointerMiddle);
5236   verifyFormat("template <int * y> f() {}", PointerMiddle);
5237   verifyFormat("int * f(int * a) {}", PointerMiddle);
5238   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
5239   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
5240   verifyFormat("A<int *> a;", PointerMiddle);
5241   verifyFormat("A<int **> a;", PointerMiddle);
5242   verifyFormat("A<int *, int *> a;", PointerMiddle);
5243   verifyFormat("A<int * []> a;", PointerMiddle);
5244   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
5245   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
5246   verifyFormat("T ** t = new T *;", PointerMiddle);
5247 
5248   // Member function reference qualifiers aren't binary operators.
5249   verifyFormat("string // break\n"
5250                "operator()() & {}");
5251   verifyFormat("string // break\n"
5252                "operator()() && {}");
5253   verifyGoogleFormat("template <typename T>\n"
5254                      "auto x() & -> int {}");
5255 }
5256 
5257 TEST_F(FormatTest, UnderstandsAttributes) {
5258   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
5259   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
5260                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
5261   FormatStyle AfterType = getLLVMStyle();
5262   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
5263   verifyFormat("__attribute__((nodebug)) void\n"
5264                "foo() {}\n",
5265                AfterType);
5266 }
5267 
5268 TEST_F(FormatTest, UnderstandsEllipsis) {
5269   verifyFormat("int printf(const char *fmt, ...);");
5270   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
5271   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
5272 
5273   FormatStyle PointersLeft = getLLVMStyle();
5274   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
5275   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
5276 }
5277 
5278 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
5279   EXPECT_EQ("int *a;\n"
5280             "int *a;\n"
5281             "int *a;",
5282             format("int *a;\n"
5283                    "int* a;\n"
5284                    "int *a;",
5285                    getGoogleStyle()));
5286   EXPECT_EQ("int* a;\n"
5287             "int* a;\n"
5288             "int* a;",
5289             format("int* a;\n"
5290                    "int* a;\n"
5291                    "int *a;",
5292                    getGoogleStyle()));
5293   EXPECT_EQ("int *a;\n"
5294             "int *a;\n"
5295             "int *a;",
5296             format("int *a;\n"
5297                    "int * a;\n"
5298                    "int *  a;",
5299                    getGoogleStyle()));
5300   EXPECT_EQ("auto x = [] {\n"
5301             "  int *a;\n"
5302             "  int *a;\n"
5303             "  int *a;\n"
5304             "};",
5305             format("auto x=[]{int *a;\n"
5306                    "int * a;\n"
5307                    "int *  a;};",
5308                    getGoogleStyle()));
5309 }
5310 
5311 TEST_F(FormatTest, UnderstandsRvalueReferences) {
5312   verifyFormat("int f(int &&a) {}");
5313   verifyFormat("int f(int a, char &&b) {}");
5314   verifyFormat("void f() { int &&a = b; }");
5315   verifyGoogleFormat("int f(int a, char&& b) {}");
5316   verifyGoogleFormat("void f() { int&& a = b; }");
5317 
5318   verifyIndependentOfContext("A<int &&> a;");
5319   verifyIndependentOfContext("A<int &&, int &&> a;");
5320   verifyGoogleFormat("A<int&&> a;");
5321   verifyGoogleFormat("A<int&&, int&&> a;");
5322 
5323   // Not rvalue references:
5324   verifyFormat("template <bool B, bool C> class A {\n"
5325                "  static_assert(B && C, \"Something is wrong\");\n"
5326                "};");
5327   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
5328   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
5329   verifyFormat("#define A(a, b) (a && b)");
5330 }
5331 
5332 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
5333   verifyFormat("void f() {\n"
5334                "  x[aaaaaaaaa -\n"
5335                "    b] = 23;\n"
5336                "}",
5337                getLLVMStyleWithColumns(15));
5338 }
5339 
5340 TEST_F(FormatTest, FormatsCasts) {
5341   verifyFormat("Type *A = static_cast<Type *>(P);");
5342   verifyFormat("Type *A = (Type *)P;");
5343   verifyFormat("Type *A = (vector<Type *, int *>)P;");
5344   verifyFormat("int a = (int)(2.0f);");
5345   verifyFormat("int a = (int)2.0f;");
5346   verifyFormat("x[(int32)y];");
5347   verifyFormat("x = (int32)y;");
5348   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
5349   verifyFormat("int a = (int)*b;");
5350   verifyFormat("int a = (int)2.0f;");
5351   verifyFormat("int a = (int)~0;");
5352   verifyFormat("int a = (int)++a;");
5353   verifyFormat("int a = (int)sizeof(int);");
5354   verifyFormat("int a = (int)+2;");
5355   verifyFormat("my_int a = (my_int)2.0f;");
5356   verifyFormat("my_int a = (my_int)sizeof(int);");
5357   verifyFormat("return (my_int)aaa;");
5358   verifyFormat("#define x ((int)-1)");
5359   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
5360   verifyFormat("#define p(q) ((int *)&q)");
5361   verifyFormat("fn(a)(b) + 1;");
5362 
5363   verifyFormat("void f() { my_int a = (my_int)*b; }");
5364   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
5365   verifyFormat("my_int a = (my_int)~0;");
5366   verifyFormat("my_int a = (my_int)++a;");
5367   verifyFormat("my_int a = (my_int)-2;");
5368   verifyFormat("my_int a = (my_int)1;");
5369   verifyFormat("my_int a = (my_int *)1;");
5370   verifyFormat("my_int a = (const my_int)-1;");
5371   verifyFormat("my_int a = (const my_int *)-1;");
5372   verifyFormat("my_int a = (my_int)(my_int)-1;");
5373   verifyFormat("my_int a = (ns::my_int)-2;");
5374   verifyFormat("case (my_int)ONE:");
5375   verifyFormat("auto x = (X)this;");
5376 
5377   // FIXME: single value wrapped with paren will be treated as cast.
5378   verifyFormat("void f(int i = (kValue)*kMask) {}");
5379 
5380   verifyFormat("{ (void)F; }");
5381 
5382   // Don't break after a cast's
5383   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5384                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
5385                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
5386 
5387   // These are not casts.
5388   verifyFormat("void f(int *) {}");
5389   verifyFormat("f(foo)->b;");
5390   verifyFormat("f(foo).b;");
5391   verifyFormat("f(foo)(b);");
5392   verifyFormat("f(foo)[b];");
5393   verifyFormat("[](foo) { return 4; }(bar);");
5394   verifyFormat("(*funptr)(foo)[4];");
5395   verifyFormat("funptrs[4](foo)[4];");
5396   verifyFormat("void f(int *);");
5397   verifyFormat("void f(int *) = 0;");
5398   verifyFormat("void f(SmallVector<int>) {}");
5399   verifyFormat("void f(SmallVector<int>);");
5400   verifyFormat("void f(SmallVector<int>) = 0;");
5401   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
5402   verifyFormat("int a = sizeof(int) * b;");
5403   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
5404   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
5405   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
5406   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
5407 
5408   // These are not casts, but at some point were confused with casts.
5409   verifyFormat("virtual void foo(int *) override;");
5410   verifyFormat("virtual void foo(char &) const;");
5411   verifyFormat("virtual void foo(int *a, char *) const;");
5412   verifyFormat("int a = sizeof(int *) + b;");
5413   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
5414   verifyFormat("bool b = f(g<int>) && c;");
5415   verifyFormat("typedef void (*f)(int i) func;");
5416 
5417   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
5418                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5419   // FIXME: The indentation here is not ideal.
5420   verifyFormat(
5421       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5422       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
5423       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
5424 }
5425 
5426 TEST_F(FormatTest, FormatsFunctionTypes) {
5427   verifyFormat("A<bool()> a;");
5428   verifyFormat("A<SomeType()> a;");
5429   verifyFormat("A<void (*)(int, std::string)> a;");
5430   verifyFormat("A<void *(int)>;");
5431   verifyFormat("void *(*a)(int *, SomeType *);");
5432   verifyFormat("int (*func)(void *);");
5433   verifyFormat("void f() { int (*func)(void *); }");
5434   verifyFormat("template <class CallbackClass>\n"
5435                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
5436 
5437   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
5438   verifyGoogleFormat("void* (*a)(int);");
5439   verifyGoogleFormat(
5440       "template <class CallbackClass>\n"
5441       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
5442 
5443   // Other constructs can look somewhat like function types:
5444   verifyFormat("A<sizeof(*x)> a;");
5445   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
5446   verifyFormat("some_var = function(*some_pointer_var)[0];");
5447   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
5448   verifyFormat("int x = f(&h)();");
5449   verifyFormat("returnsFunction(&param1, &param2)(param);");
5450   verifyFormat("std::function<\n"
5451                "    LooooooooooongTemplatedType<\n"
5452                "        SomeType>*(\n"
5453                "        LooooooooooooooooongType type)>\n"
5454                "    function;",
5455                getGoogleStyleWithColumns(40));
5456 }
5457 
5458 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
5459   verifyFormat("A (*foo_)[6];");
5460   verifyFormat("vector<int> (*foo_)[6];");
5461 }
5462 
5463 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
5464   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5465                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5466   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
5467                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5468   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5469                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
5470 
5471   // Different ways of ()-initializiation.
5472   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5473                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
5474   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5475                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
5476   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5477                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
5478   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5479                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
5480 
5481   // Lambdas should not confuse the variable declaration heuristic.
5482   verifyFormat("LooooooooooooooooongType\n"
5483                "    variable(nullptr, [](A *a) {});",
5484                getLLVMStyleWithColumns(40));
5485 }
5486 
5487 TEST_F(FormatTest, BreaksLongDeclarations) {
5488   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
5489                "    AnotherNameForTheLongType;");
5490   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
5491                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5492   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5493                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5494   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
5495                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5496   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5497                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5498   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
5499                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5500   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5501                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5502   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5503                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5504   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5505                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
5506   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5507                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
5508   FormatStyle Indented = getLLVMStyle();
5509   Indented.IndentWrappedFunctionNames = true;
5510   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5511                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
5512                Indented);
5513   verifyFormat(
5514       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5515       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5516       Indented);
5517   verifyFormat(
5518       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5519       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5520       Indented);
5521   verifyFormat(
5522       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5523       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5524       Indented);
5525 
5526   // FIXME: Without the comment, this breaks after "(".
5527   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
5528                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
5529                getGoogleStyle());
5530 
5531   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
5532                "                  int LoooooooooooooooooooongParam2) {}");
5533   verifyFormat(
5534       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
5535       "                                   SourceLocation L, IdentifierIn *II,\n"
5536       "                                   Type *T) {}");
5537   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
5538                "ReallyReaaallyLongFunctionName(\n"
5539                "    const std::string &SomeParameter,\n"
5540                "    const SomeType<string, SomeOtherTemplateParameter>\n"
5541                "        &ReallyReallyLongParameterName,\n"
5542                "    const SomeType<string, SomeOtherTemplateParameter>\n"
5543                "        &AnotherLongParameterName) {}");
5544   verifyFormat("template <typename A>\n"
5545                "SomeLoooooooooooooooooooooongType<\n"
5546                "    typename some_namespace::SomeOtherType<A>::Type>\n"
5547                "Function() {}");
5548 
5549   verifyGoogleFormat(
5550       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
5551       "    aaaaaaaaaaaaaaaaaaaaaaa;");
5552   verifyGoogleFormat(
5553       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
5554       "                                   SourceLocation L) {}");
5555   verifyGoogleFormat(
5556       "some_namespace::LongReturnType\n"
5557       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
5558       "    int first_long_parameter, int second_parameter) {}");
5559 
5560   verifyGoogleFormat("template <typename T>\n"
5561                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5562                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
5563   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5564                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
5565 
5566   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5567                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5568                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5569   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5570                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5571                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
5572   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5573                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5574                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
5575                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5576 
5577   verifyFormat("template <typename T> // Templates on own line.\n"
5578                "static int            // Some comment.\n"
5579                "MyFunction(int a);",
5580                getLLVMStyle());
5581 }
5582 
5583 TEST_F(FormatTest, FormatsArrays) {
5584   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5585                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
5586   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
5587                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
5588   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
5589                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
5590   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5591                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5592   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5593                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
5594   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5595                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5596                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5597   verifyFormat(
5598       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
5599       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5600       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
5601   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
5602                "    .aaaaaaaaaaaaaaaaaaaaaa();");
5603 
5604   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
5605                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
5606   verifyFormat(
5607       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
5608       "                                  .aaaaaaa[0]\n"
5609       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
5610   verifyFormat("a[::b::c];");
5611 
5612   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
5613 
5614   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
5615   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
5616 }
5617 
5618 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
5619   verifyFormat("(a)->b();");
5620   verifyFormat("--a;");
5621 }
5622 
5623 TEST_F(FormatTest, HandlesIncludeDirectives) {
5624   verifyFormat("#include <string>\n"
5625                "#include <a/b/c.h>\n"
5626                "#include \"a/b/string\"\n"
5627                "#include \"string.h\"\n"
5628                "#include \"string.h\"\n"
5629                "#include <a-a>\n"
5630                "#include < path with space >\n"
5631                "#include_next <test.h>"
5632                "#include \"abc.h\" // this is included for ABC\n"
5633                "#include \"some long include\" // with a comment\n"
5634                "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
5635                getLLVMStyleWithColumns(35));
5636   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
5637   EXPECT_EQ("#include <a>", format("#include<a>"));
5638 
5639   verifyFormat("#import <string>");
5640   verifyFormat("#import <a/b/c.h>");
5641   verifyFormat("#import \"a/b/string\"");
5642   verifyFormat("#import \"string.h\"");
5643   verifyFormat("#import \"string.h\"");
5644   verifyFormat("#if __has_include(<strstream>)\n"
5645                "#include <strstream>\n"
5646                "#endif");
5647 
5648   verifyFormat("#define MY_IMPORT <a/b>");
5649 
5650   verifyFormat("#if __has_include(<a/b>)");
5651   verifyFormat("#if __has_include_next(<a/b>)");
5652   verifyFormat("#define F __has_include(<a/b>)");
5653   verifyFormat("#define F __has_include_next(<a/b>)");
5654 
5655   // Protocol buffer definition or missing "#".
5656   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
5657                getLLVMStyleWithColumns(30));
5658 
5659   FormatStyle Style = getLLVMStyle();
5660   Style.AlwaysBreakBeforeMultilineStrings = true;
5661   Style.ColumnLimit = 0;
5662   verifyFormat("#import \"abc.h\"", Style);
5663 
5664   // But 'import' might also be a regular C++ namespace.
5665   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5666                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5667 }
5668 
5669 //===----------------------------------------------------------------------===//
5670 // Error recovery tests.
5671 //===----------------------------------------------------------------------===//
5672 
5673 TEST_F(FormatTest, IncompleteParameterLists) {
5674   FormatStyle NoBinPacking = getLLVMStyle();
5675   NoBinPacking.BinPackParameters = false;
5676   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
5677                "                        double *min_x,\n"
5678                "                        double *max_x,\n"
5679                "                        double *min_y,\n"
5680                "                        double *max_y,\n"
5681                "                        double *min_z,\n"
5682                "                        double *max_z, ) {}",
5683                NoBinPacking);
5684 }
5685 
5686 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
5687   verifyFormat("void f() { return; }\n42");
5688   verifyFormat("void f() {\n"
5689                "  if (0)\n"
5690                "    return;\n"
5691                "}\n"
5692                "42");
5693   verifyFormat("void f() { return }\n42");
5694   verifyFormat("void f() {\n"
5695                "  if (0)\n"
5696                "    return\n"
5697                "}\n"
5698                "42");
5699 }
5700 
5701 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
5702   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
5703   EXPECT_EQ("void f() {\n"
5704             "  if (a)\n"
5705             "    return\n"
5706             "}",
5707             format("void  f  (  )  {  if  ( a )  return  }"));
5708   EXPECT_EQ("namespace N {\n"
5709             "void f()\n"
5710             "}",
5711             format("namespace  N  {  void f()  }"));
5712   EXPECT_EQ("namespace N {\n"
5713             "void f() {}\n"
5714             "void g()\n"
5715             "} // namespace N",
5716             format("namespace N  { void f( ) { } void g( ) }"));
5717 }
5718 
5719 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
5720   verifyFormat("int aaaaaaaa =\n"
5721                "    // Overlylongcomment\n"
5722                "    b;",
5723                getLLVMStyleWithColumns(20));
5724   verifyFormat("function(\n"
5725                "    ShortArgument,\n"
5726                "    LoooooooooooongArgument);\n",
5727                getLLVMStyleWithColumns(20));
5728 }
5729 
5730 TEST_F(FormatTest, IncorrectAccessSpecifier) {
5731   verifyFormat("public:");
5732   verifyFormat("class A {\n"
5733                "public\n"
5734                "  void f() {}\n"
5735                "};");
5736   verifyFormat("public\n"
5737                "int qwerty;");
5738   verifyFormat("public\n"
5739                "B {}");
5740   verifyFormat("public\n"
5741                "{}");
5742   verifyFormat("public\n"
5743                "B { int x; }");
5744 }
5745 
5746 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
5747   verifyFormat("{");
5748   verifyFormat("#})");
5749   verifyNoCrash("(/**/[:!] ?[).");
5750 }
5751 
5752 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
5753   verifyFormat("do {\n}");
5754   verifyFormat("do {\n}\n"
5755                "f();");
5756   verifyFormat("do {\n}\n"
5757                "wheeee(fun);");
5758   verifyFormat("do {\n"
5759                "  f();\n"
5760                "}");
5761 }
5762 
5763 TEST_F(FormatTest, IncorrectCodeMissingParens) {
5764   verifyFormat("if {\n  foo;\n  foo();\n}");
5765   verifyFormat("switch {\n  foo;\n  foo();\n}");
5766   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
5767   verifyFormat("while {\n  foo;\n  foo();\n}");
5768   verifyFormat("do {\n  foo;\n  foo();\n} while;");
5769 }
5770 
5771 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
5772   verifyIncompleteFormat("namespace {\n"
5773                          "class Foo { Foo (\n"
5774                          "};\n"
5775                          "} // namespace");
5776 }
5777 
5778 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
5779   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
5780   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
5781   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
5782   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
5783 
5784   EXPECT_EQ("{\n"
5785             "  {\n"
5786             "    breakme(\n"
5787             "        qwe);\n"
5788             "  }\n",
5789             format("{\n"
5790                    "    {\n"
5791                    " breakme(qwe);\n"
5792                    "}\n",
5793                    getLLVMStyleWithColumns(10)));
5794 }
5795 
5796 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
5797   verifyFormat("int x = {\n"
5798                "    avariable,\n"
5799                "    b(alongervariable)};",
5800                getLLVMStyleWithColumns(25));
5801 }
5802 
5803 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
5804   verifyFormat("return (a)(b){1, 2, 3};");
5805 }
5806 
5807 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
5808   verifyFormat("vector<int> x{1, 2, 3, 4};");
5809   verifyFormat("vector<int> x{\n"
5810                "    1, 2, 3, 4,\n"
5811                "};");
5812   verifyFormat("vector<T> x{{}, {}, {}, {}};");
5813   verifyFormat("f({1, 2});");
5814   verifyFormat("auto v = Foo{-1};");
5815   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
5816   verifyFormat("Class::Class : member{1, 2, 3} {}");
5817   verifyFormat("new vector<int>{1, 2, 3};");
5818   verifyFormat("new int[3]{1, 2, 3};");
5819   verifyFormat("new int{1};");
5820   verifyFormat("return {arg1, arg2};");
5821   verifyFormat("return {arg1, SomeType{parameter}};");
5822   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
5823   verifyFormat("new T{arg1, arg2};");
5824   verifyFormat("f(MyMap[{composite, key}]);");
5825   verifyFormat("class Class {\n"
5826                "  T member = {arg1, arg2};\n"
5827                "};");
5828   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
5829   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
5830   verifyFormat("int a = std::is_integral<int>{} + 0;");
5831 
5832   verifyFormat("int foo(int i) { return fo1{}(i); }");
5833   verifyFormat("int foo(int i) { return fo1{}(i); }");
5834   verifyFormat("auto i = decltype(x){};");
5835   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
5836   verifyFormat("Node n{1, Node{1000}, //\n"
5837                "       2};");
5838   verifyFormat("Aaaa aaaaaaa{\n"
5839                "    {\n"
5840                "        aaaa,\n"
5841                "    },\n"
5842                "};");
5843   verifyFormat("class C : public D {\n"
5844                "  SomeClass SC{2};\n"
5845                "};");
5846   verifyFormat("class C : public A {\n"
5847                "  class D : public B {\n"
5848                "    void f() { int i{2}; }\n"
5849                "  };\n"
5850                "};");
5851   verifyFormat("#define A {a, a},");
5852 
5853   // Cases where distinguising braced lists and blocks is hard.
5854   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
5855   verifyFormat("void f() {\n"
5856                "  return; // comment\n"
5857                "}\n"
5858                "SomeType t;");
5859   verifyFormat("void f() {\n"
5860                "  if (a) {\n"
5861                "    f();\n"
5862                "  }\n"
5863                "}\n"
5864                "SomeType t;");
5865 
5866   // In combination with BinPackArguments = false.
5867   FormatStyle NoBinPacking = getLLVMStyle();
5868   NoBinPacking.BinPackArguments = false;
5869   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
5870                "                      bbbbb,\n"
5871                "                      ccccc,\n"
5872                "                      ddddd,\n"
5873                "                      eeeee,\n"
5874                "                      ffffff,\n"
5875                "                      ggggg,\n"
5876                "                      hhhhhh,\n"
5877                "                      iiiiii,\n"
5878                "                      jjjjjj,\n"
5879                "                      kkkkkk};",
5880                NoBinPacking);
5881   verifyFormat("const Aaaaaa aaaaa = {\n"
5882                "    aaaaa,\n"
5883                "    bbbbb,\n"
5884                "    ccccc,\n"
5885                "    ddddd,\n"
5886                "    eeeee,\n"
5887                "    ffffff,\n"
5888                "    ggggg,\n"
5889                "    hhhhhh,\n"
5890                "    iiiiii,\n"
5891                "    jjjjjj,\n"
5892                "    kkkkkk,\n"
5893                "};",
5894                NoBinPacking);
5895   verifyFormat(
5896       "const Aaaaaa aaaaa = {\n"
5897       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
5898       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
5899       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
5900       "};",
5901       NoBinPacking);
5902 
5903   // FIXME: The alignment of these trailing comments might be bad. Then again,
5904   // this might be utterly useless in real code.
5905   verifyFormat("Constructor::Constructor()\n"
5906                "    : some_value{         //\n"
5907                "                 aaaaaaa, //\n"
5908                "                 bbbbbbb} {}");
5909 
5910   // In braced lists, the first comment is always assumed to belong to the
5911   // first element. Thus, it can be moved to the next or previous line as
5912   // appropriate.
5913   EXPECT_EQ("function({// First element:\n"
5914             "          1,\n"
5915             "          // Second element:\n"
5916             "          2});",
5917             format("function({\n"
5918                    "    // First element:\n"
5919                    "    1,\n"
5920                    "    // Second element:\n"
5921                    "    2});"));
5922   EXPECT_EQ("std::vector<int> MyNumbers{\n"
5923             "    // First element:\n"
5924             "    1,\n"
5925             "    // Second element:\n"
5926             "    2};",
5927             format("std::vector<int> MyNumbers{// First element:\n"
5928                    "                           1,\n"
5929                    "                           // Second element:\n"
5930                    "                           2};",
5931                    getLLVMStyleWithColumns(30)));
5932   // A trailing comma should still lead to an enforced line break.
5933   EXPECT_EQ("vector<int> SomeVector = {\n"
5934             "    // aaa\n"
5935             "    1, 2,\n"
5936             "};",
5937             format("vector<int> SomeVector = { // aaa\n"
5938                    "    1, 2, };"));
5939 
5940   FormatStyle ExtraSpaces = getLLVMStyle();
5941   ExtraSpaces.Cpp11BracedListStyle = false;
5942   ExtraSpaces.ColumnLimit = 75;
5943   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
5944   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
5945   verifyFormat("f({ 1, 2 });", ExtraSpaces);
5946   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
5947   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
5948   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
5949   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
5950   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
5951   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
5952   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
5953   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
5954   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
5955   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
5956   verifyFormat("class Class {\n"
5957                "  T member = { arg1, arg2 };\n"
5958                "};",
5959                ExtraSpaces);
5960   verifyFormat(
5961       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5962       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
5963       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
5964       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
5965       ExtraSpaces);
5966   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
5967   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
5968                ExtraSpaces);
5969   verifyFormat(
5970       "someFunction(OtherParam,\n"
5971       "             BracedList{ // comment 1 (Forcing interesting break)\n"
5972       "                         param1, param2,\n"
5973       "                         // comment 2\n"
5974       "                         param3, param4 });",
5975       ExtraSpaces);
5976   verifyFormat(
5977       "std::this_thread::sleep_for(\n"
5978       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
5979       ExtraSpaces);
5980   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
5981                "    aaaaaaa,\n"
5982                "    aaaaaaaaaa,\n"
5983                "    aaaaa,\n"
5984                "    aaaaaaaaaaaaaaa,\n"
5985                "    aaa,\n"
5986                "    aaaaaaaaaa,\n"
5987                "    a,\n"
5988                "    aaaaaaaaaaaaaaaaaaaaa,\n"
5989                "    aaaaaaaaaaaa,\n"
5990                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
5991                "    aaaaaaa,\n"
5992                "    a};");
5993   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
5994 }
5995 
5996 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
5997   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5998                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5999                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6000                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6001                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6002                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6003   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
6004                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6005                "                 1, 22, 333, 4444, 55555, //\n"
6006                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6007                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6008   verifyFormat(
6009       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6010       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6011       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
6012       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6013       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6014       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6015       "                 7777777};");
6016   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6017                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6018                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6019   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6020                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6021                "    // Separating comment.\n"
6022                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
6023   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6024                "    // Leading comment\n"
6025                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6026                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6027   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6028                "                 1, 1, 1, 1};",
6029                getLLVMStyleWithColumns(39));
6030   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6031                "                 1, 1, 1, 1};",
6032                getLLVMStyleWithColumns(38));
6033   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
6034                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
6035                getLLVMStyleWithColumns(43));
6036   verifyFormat(
6037       "static unsigned SomeValues[10][3] = {\n"
6038       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
6039       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
6040   verifyFormat("static auto fields = new vector<string>{\n"
6041                "    \"aaaaaaaaaaaaa\",\n"
6042                "    \"aaaaaaaaaaaaa\",\n"
6043                "    \"aaaaaaaaaaaa\",\n"
6044                "    \"aaaaaaaaaaaaaa\",\n"
6045                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6046                "    \"aaaaaaaaaaaa\",\n"
6047                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6048                "};");
6049   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
6050   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
6051                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
6052                "                 3, cccccccccccccccccccccc};",
6053                getLLVMStyleWithColumns(60));
6054 
6055   // Trailing commas.
6056   verifyFormat("vector<int> x = {\n"
6057                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
6058                "};",
6059                getLLVMStyleWithColumns(39));
6060   verifyFormat("vector<int> x = {\n"
6061                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
6062                "};",
6063                getLLVMStyleWithColumns(39));
6064   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6065                "                 1, 1, 1, 1,\n"
6066                "                 /**/ /**/};",
6067                getLLVMStyleWithColumns(39));
6068 
6069   // Trailing comment in the first line.
6070   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
6071                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
6072                "    111111111,  222222222,  3333333333,  444444444,  //\n"
6073                "    11111111,   22222222,   333333333,   44444444};");
6074   // Trailing comment in the last line.
6075   verifyFormat("int aaaaa[] = {\n"
6076                "    1, 2, 3, // comment\n"
6077                "    4, 5, 6  // comment\n"
6078                "};");
6079 
6080   // With nested lists, we should either format one item per line or all nested
6081   // lists one on line.
6082   // FIXME: For some nested lists, we can do better.
6083   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
6084                "        {aaaaaaaaaaaaaaaaaaa},\n"
6085                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
6086                "        {aaaaaaaaaaaaaaaaa}};",
6087                getLLVMStyleWithColumns(60));
6088   verifyFormat(
6089       "SomeStruct my_struct_array = {\n"
6090       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
6091       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
6092       "    {aaa, aaa},\n"
6093       "    {aaa, aaa},\n"
6094       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
6095       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6096       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
6097 
6098   // No column layout should be used here.
6099   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
6100                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
6101 
6102   verifyNoCrash("a<,");
6103 
6104   // No braced initializer here.
6105   verifyFormat("void f() {\n"
6106                "  struct Dummy {};\n"
6107                "  f(v);\n"
6108                "}");
6109 
6110   // Long lists should be formatted in columns even if they are nested.
6111   verifyFormat(
6112       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6113       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6114       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6115       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6116       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6117       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
6118 
6119   // Allow "single-column" layout even if that violates the column limit. There
6120   // isn't going to be a better way.
6121   verifyFormat("std::vector<int> a = {\n"
6122                "    aaaaaaaa,\n"
6123                "    aaaaaaaa,\n"
6124                "    aaaaaaaa,\n"
6125                "    aaaaaaaa,\n"
6126                "    aaaaaaaaaa,\n"
6127                "    aaaaaaaa,\n"
6128                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
6129                getLLVMStyleWithColumns(30));
6130   verifyFormat("vector<int> aaaa = {\n"
6131                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6132                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6133                "    aaaaaa.aaaaaaa,\n"
6134                "    aaaaaa.aaaaaaa,\n"
6135                "    aaaaaa.aaaaaaa,\n"
6136                "    aaaaaa.aaaaaaa,\n"
6137                "};");
6138 
6139   // Don't create hanging lists.
6140   verifyFormat("someFunction(Param, {List1, List2,\n"
6141                "                     List3});",
6142                getLLVMStyleWithColumns(35));
6143   verifyFormat("someFunction(Param, Param,\n"
6144                "             {List1, List2,\n"
6145                "              List3});",
6146                getLLVMStyleWithColumns(35));
6147   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
6148                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
6149 }
6150 
6151 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
6152   FormatStyle DoNotMerge = getLLVMStyle();
6153   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6154 
6155   verifyFormat("void f() { return 42; }");
6156   verifyFormat("void f() {\n"
6157                "  return 42;\n"
6158                "}",
6159                DoNotMerge);
6160   verifyFormat("void f() {\n"
6161                "  // Comment\n"
6162                "}");
6163   verifyFormat("{\n"
6164                "#error {\n"
6165                "  int a;\n"
6166                "}");
6167   verifyFormat("{\n"
6168                "  int a;\n"
6169                "#error {\n"
6170                "}");
6171   verifyFormat("void f() {} // comment");
6172   verifyFormat("void f() { int a; } // comment");
6173   verifyFormat("void f() {\n"
6174                "} // comment",
6175                DoNotMerge);
6176   verifyFormat("void f() {\n"
6177                "  int a;\n"
6178                "} // comment",
6179                DoNotMerge);
6180   verifyFormat("void f() {\n"
6181                "} // comment",
6182                getLLVMStyleWithColumns(15));
6183 
6184   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
6185   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
6186 
6187   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
6188   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
6189   verifyFormat("class C {\n"
6190                "  C()\n"
6191                "      : iiiiiiii(nullptr),\n"
6192                "        kkkkkkk(nullptr),\n"
6193                "        mmmmmmm(nullptr),\n"
6194                "        nnnnnnn(nullptr) {}\n"
6195                "};",
6196                getGoogleStyle());
6197 
6198   FormatStyle NoColumnLimit = getLLVMStyle();
6199   NoColumnLimit.ColumnLimit = 0;
6200   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
6201   EXPECT_EQ("class C {\n"
6202             "  A() : b(0) {}\n"
6203             "};",
6204             format("class C{A():b(0){}};", NoColumnLimit));
6205   EXPECT_EQ("A()\n"
6206             "    : b(0) {\n"
6207             "}",
6208             format("A()\n:b(0)\n{\n}", NoColumnLimit));
6209 
6210   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
6211   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
6212       FormatStyle::SFS_None;
6213   EXPECT_EQ("A()\n"
6214             "    : b(0) {\n"
6215             "}",
6216             format("A():b(0){}", DoNotMergeNoColumnLimit));
6217   EXPECT_EQ("A()\n"
6218             "    : b(0) {\n"
6219             "}",
6220             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
6221 
6222   verifyFormat("#define A          \\\n"
6223                "  void f() {       \\\n"
6224                "    int i;         \\\n"
6225                "  }",
6226                getLLVMStyleWithColumns(20));
6227   verifyFormat("#define A           \\\n"
6228                "  void f() { int i; }",
6229                getLLVMStyleWithColumns(21));
6230   verifyFormat("#define A            \\\n"
6231                "  void f() {         \\\n"
6232                "    int i;           \\\n"
6233                "  }                  \\\n"
6234                "  int j;",
6235                getLLVMStyleWithColumns(22));
6236   verifyFormat("#define A             \\\n"
6237                "  void f() { int i; } \\\n"
6238                "  int j;",
6239                getLLVMStyleWithColumns(23));
6240 }
6241 
6242 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
6243   FormatStyle MergeInlineOnly = getLLVMStyle();
6244   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
6245   verifyFormat("class C {\n"
6246                "  int f() { return 42; }\n"
6247                "};",
6248                MergeInlineOnly);
6249   verifyFormat("int f() {\n"
6250                "  return 42;\n"
6251                "}",
6252                MergeInlineOnly);
6253 }
6254 
6255 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
6256   // Elaborate type variable declarations.
6257   verifyFormat("struct foo a = {bar};\nint n;");
6258   verifyFormat("class foo a = {bar};\nint n;");
6259   verifyFormat("union foo a = {bar};\nint n;");
6260 
6261   // Elaborate types inside function definitions.
6262   verifyFormat("struct foo f() {}\nint n;");
6263   verifyFormat("class foo f() {}\nint n;");
6264   verifyFormat("union foo f() {}\nint n;");
6265 
6266   // Templates.
6267   verifyFormat("template <class X> void f() {}\nint n;");
6268   verifyFormat("template <struct X> void f() {}\nint n;");
6269   verifyFormat("template <union X> void f() {}\nint n;");
6270 
6271   // Actual definitions...
6272   verifyFormat("struct {\n} n;");
6273   verifyFormat(
6274       "template <template <class T, class Y>, class Z> class X {\n} n;");
6275   verifyFormat("union Z {\n  int n;\n} x;");
6276   verifyFormat("class MACRO Z {\n} n;");
6277   verifyFormat("class MACRO(X) Z {\n} n;");
6278   verifyFormat("class __attribute__(X) Z {\n} n;");
6279   verifyFormat("class __declspec(X) Z {\n} n;");
6280   verifyFormat("class A##B##C {\n} n;");
6281   verifyFormat("class alignas(16) Z {\n} n;");
6282   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
6283   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
6284 
6285   // Redefinition from nested context:
6286   verifyFormat("class A::B::C {\n} n;");
6287 
6288   // Template definitions.
6289   verifyFormat(
6290       "template <typename F>\n"
6291       "Matcher(const Matcher<F> &Other,\n"
6292       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
6293       "                             !is_same<F, T>::value>::type * = 0)\n"
6294       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
6295 
6296   // FIXME: This is still incorrectly handled at the formatter side.
6297   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
6298   verifyFormat("int i = SomeFunction(a<b, a> b);");
6299 
6300   // FIXME:
6301   // This now gets parsed incorrectly as class definition.
6302   // verifyFormat("class A<int> f() {\n}\nint n;");
6303 
6304   // Elaborate types where incorrectly parsing the structural element would
6305   // break the indent.
6306   verifyFormat("if (true)\n"
6307                "  class X x;\n"
6308                "else\n"
6309                "  f();\n");
6310 
6311   // This is simply incomplete. Formatting is not important, but must not crash.
6312   verifyFormat("class A:");
6313 }
6314 
6315 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
6316   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
6317             format("#error Leave     all         white!!!!! space* alone!\n"));
6318   EXPECT_EQ(
6319       "#warning Leave     all         white!!!!! space* alone!\n",
6320       format("#warning Leave     all         white!!!!! space* alone!\n"));
6321   EXPECT_EQ("#error 1", format("  #  error   1"));
6322   EXPECT_EQ("#warning 1", format("  #  warning 1"));
6323 }
6324 
6325 TEST_F(FormatTest, FormatHashIfExpressions) {
6326   verifyFormat("#if AAAA && BBBB");
6327   verifyFormat("#if (AAAA && BBBB)");
6328   verifyFormat("#elif (AAAA && BBBB)");
6329   // FIXME: Come up with a better indentation for #elif.
6330   verifyFormat(
6331       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
6332       "    defined(BBBBBBBB)\n"
6333       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
6334       "    defined(BBBBBBBB)\n"
6335       "#endif",
6336       getLLVMStyleWithColumns(65));
6337 }
6338 
6339 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
6340   FormatStyle AllowsMergedIf = getGoogleStyle();
6341   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
6342   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
6343   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
6344   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
6345   EXPECT_EQ("if (true) return 42;",
6346             format("if (true)\nreturn 42;", AllowsMergedIf));
6347   FormatStyle ShortMergedIf = AllowsMergedIf;
6348   ShortMergedIf.ColumnLimit = 25;
6349   verifyFormat("#define A \\\n"
6350                "  if (true) return 42;",
6351                ShortMergedIf);
6352   verifyFormat("#define A \\\n"
6353                "  f();    \\\n"
6354                "  if (true)\n"
6355                "#define B",
6356                ShortMergedIf);
6357   verifyFormat("#define A \\\n"
6358                "  f();    \\\n"
6359                "  if (true)\n"
6360                "g();",
6361                ShortMergedIf);
6362   verifyFormat("{\n"
6363                "#ifdef A\n"
6364                "  // Comment\n"
6365                "  if (true) continue;\n"
6366                "#endif\n"
6367                "  // Comment\n"
6368                "  if (true) continue;\n"
6369                "}",
6370                ShortMergedIf);
6371   ShortMergedIf.ColumnLimit = 29;
6372   verifyFormat("#define A                   \\\n"
6373                "  if (aaaaaaaaaa) return 1; \\\n"
6374                "  return 2;",
6375                ShortMergedIf);
6376   ShortMergedIf.ColumnLimit = 28;
6377   verifyFormat("#define A         \\\n"
6378                "  if (aaaaaaaaaa) \\\n"
6379                "    return 1;     \\\n"
6380                "  return 2;",
6381                ShortMergedIf);
6382 }
6383 
6384 TEST_F(FormatTest, FormatStarDependingOnContext) {
6385   verifyFormat("void f(int *a);");
6386   verifyFormat("void f() { f(fint * b); }");
6387   verifyFormat("class A {\n  void f(int *a);\n};");
6388   verifyFormat("class A {\n  int *a;\n};");
6389   verifyFormat("namespace a {\n"
6390                "namespace b {\n"
6391                "class A {\n"
6392                "  void f() {}\n"
6393                "  int *a;\n"
6394                "};\n"
6395                "} // namespace b\n"
6396                "} // namespace a");
6397 }
6398 
6399 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
6400   verifyFormat("while");
6401   verifyFormat("operator");
6402 }
6403 
6404 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
6405   // This code would be painfully slow to format if we didn't skip it.
6406   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
6407                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
6408                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
6409                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
6410                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
6411                    "A(1, 1)\n"
6412                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
6413                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
6414                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
6415                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
6416                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
6417                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
6418                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
6419                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
6420                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
6421                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
6422   // Deeply nested part is untouched, rest is formatted.
6423   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
6424             format(std::string("int    i;\n") + Code + "int    j;\n",
6425                    getLLVMStyle(), SC_ExpectIncomplete));
6426 }
6427 
6428 //===----------------------------------------------------------------------===//
6429 // Objective-C tests.
6430 //===----------------------------------------------------------------------===//
6431 
6432 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
6433   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
6434   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
6435             format("-(NSUInteger)indexOfObject:(id)anObject;"));
6436   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
6437   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
6438   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
6439             format("-(NSInteger)Method3:(id)anObject;"));
6440   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
6441             format("-(NSInteger)Method4:(id)anObject;"));
6442   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
6443             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
6444   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
6445             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
6446   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
6447             "forAllCells:(BOOL)flag;",
6448             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
6449                    "forAllCells:(BOOL)flag;"));
6450 
6451   // Very long objectiveC method declaration.
6452   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
6453                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
6454   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
6455                "                    inRange:(NSRange)range\n"
6456                "                   outRange:(NSRange)out_range\n"
6457                "                  outRange1:(NSRange)out_range1\n"
6458                "                  outRange2:(NSRange)out_range2\n"
6459                "                  outRange3:(NSRange)out_range3\n"
6460                "                  outRange4:(NSRange)out_range4\n"
6461                "                  outRange5:(NSRange)out_range5\n"
6462                "                  outRange6:(NSRange)out_range6\n"
6463                "                  outRange7:(NSRange)out_range7\n"
6464                "                  outRange8:(NSRange)out_range8\n"
6465                "                  outRange9:(NSRange)out_range9;");
6466 
6467   // When the function name has to be wrapped.
6468   FormatStyle Style = getLLVMStyle();
6469   Style.IndentWrappedFunctionNames = false;
6470   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
6471                "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
6472                "           anotherName:(NSString)bbbbbbbbbbbbbb {\n"
6473                "}",
6474                Style);
6475   Style.IndentWrappedFunctionNames = true;
6476   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
6477                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
6478                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
6479                "}",
6480                Style);
6481 
6482   verifyFormat("- (int)sum:(vector<int>)numbers;");
6483   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
6484   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
6485   // protocol lists (but not for template classes):
6486   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
6487 
6488   verifyFormat("- (int (*)())foo:(int (*)())f;");
6489   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
6490 
6491   // If there's no return type (very rare in practice!), LLVM and Google style
6492   // agree.
6493   verifyFormat("- foo;");
6494   verifyFormat("- foo:(int)f;");
6495   verifyGoogleFormat("- foo:(int)foo;");
6496 }
6497 
6498 
6499 TEST_F(FormatTest, BreaksStringLiterals) {
6500   EXPECT_EQ("\"some text \"\n"
6501             "\"other\";",
6502             format("\"some text other\";", getLLVMStyleWithColumns(12)));
6503   EXPECT_EQ("\"some text \"\n"
6504             "\"other\";",
6505             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
6506   EXPECT_EQ(
6507       "#define A  \\\n"
6508       "  \"some \"  \\\n"
6509       "  \"text \"  \\\n"
6510       "  \"other\";",
6511       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
6512   EXPECT_EQ(
6513       "#define A  \\\n"
6514       "  \"so \"    \\\n"
6515       "  \"text \"  \\\n"
6516       "  \"other\";",
6517       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
6518 
6519   EXPECT_EQ("\"some text\"",
6520             format("\"some text\"", getLLVMStyleWithColumns(1)));
6521   EXPECT_EQ("\"some text\"",
6522             format("\"some text\"", getLLVMStyleWithColumns(11)));
6523   EXPECT_EQ("\"some \"\n"
6524             "\"text\"",
6525             format("\"some text\"", getLLVMStyleWithColumns(10)));
6526   EXPECT_EQ("\"some \"\n"
6527             "\"text\"",
6528             format("\"some text\"", getLLVMStyleWithColumns(7)));
6529   EXPECT_EQ("\"some\"\n"
6530             "\" tex\"\n"
6531             "\"t\"",
6532             format("\"some text\"", getLLVMStyleWithColumns(6)));
6533   EXPECT_EQ("\"some\"\n"
6534             "\" tex\"\n"
6535             "\" and\"",
6536             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
6537   EXPECT_EQ("\"some\"\n"
6538             "\"/tex\"\n"
6539             "\"/and\"",
6540             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
6541 
6542   EXPECT_EQ("variable =\n"
6543             "    \"long string \"\n"
6544             "    \"literal\";",
6545             format("variable = \"long string literal\";",
6546                    getLLVMStyleWithColumns(20)));
6547 
6548   EXPECT_EQ("variable = f(\n"
6549             "    \"long string \"\n"
6550             "    \"literal\",\n"
6551             "    short,\n"
6552             "    loooooooooooooooooooong);",
6553             format("variable = f(\"long string literal\", short, "
6554                    "loooooooooooooooooooong);",
6555                    getLLVMStyleWithColumns(20)));
6556 
6557   EXPECT_EQ(
6558       "f(g(\"long string \"\n"
6559       "    \"literal\"),\n"
6560       "  b);",
6561       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
6562   EXPECT_EQ("f(g(\"long string \"\n"
6563             "    \"literal\",\n"
6564             "    a),\n"
6565             "  b);",
6566             format("f(g(\"long string literal\", a), b);",
6567                    getLLVMStyleWithColumns(20)));
6568   EXPECT_EQ(
6569       "f(\"one two\".split(\n"
6570       "    variable));",
6571       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
6572   EXPECT_EQ("f(\"one two three four five six \"\n"
6573             "  \"seven\".split(\n"
6574             "      really_looooong_variable));",
6575             format("f(\"one two three four five six seven\"."
6576                    "split(really_looooong_variable));",
6577                    getLLVMStyleWithColumns(33)));
6578 
6579   EXPECT_EQ("f(\"some \"\n"
6580             "  \"text\",\n"
6581             "  other);",
6582             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
6583 
6584   // Only break as a last resort.
6585   verifyFormat(
6586       "aaaaaaaaaaaaaaaaaaaa(\n"
6587       "    aaaaaaaaaaaaaaaaaaaa,\n"
6588       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
6589 
6590   EXPECT_EQ("\"splitmea\"\n"
6591             "\"trandomp\"\n"
6592             "\"oint\"",
6593             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
6594 
6595   EXPECT_EQ("\"split/\"\n"
6596             "\"pathat/\"\n"
6597             "\"slashes\"",
6598             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
6599 
6600   EXPECT_EQ("\"split/\"\n"
6601             "\"pathat/\"\n"
6602             "\"slashes\"",
6603             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
6604   EXPECT_EQ("\"split at \"\n"
6605             "\"spaces/at/\"\n"
6606             "\"slashes.at.any$\"\n"
6607             "\"non-alphanumeric%\"\n"
6608             "\"1111111111characte\"\n"
6609             "\"rs\"",
6610             format("\"split at "
6611                    "spaces/at/"
6612                    "slashes.at."
6613                    "any$non-"
6614                    "alphanumeric%"
6615                    "1111111111characte"
6616                    "rs\"",
6617                    getLLVMStyleWithColumns(20)));
6618 
6619   // Verify that splitting the strings understands
6620   // Style::AlwaysBreakBeforeMultilineStrings.
6621   EXPECT_EQ(
6622       "aaaaaaaaaaaa(\n"
6623       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
6624       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
6625       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
6626              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
6627              "aaaaaaaaaaaaaaaaaaaaaa\");",
6628              getGoogleStyle()));
6629   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
6630             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
6631             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
6632                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
6633                    "aaaaaaaaaaaaaaaaaaaaaa\";",
6634                    getGoogleStyle()));
6635   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
6636             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
6637             format("llvm::outs() << "
6638                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
6639                    "aaaaaaaaaaaaaaaaaaa\";"));
6640   EXPECT_EQ("ffff(\n"
6641             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
6642             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
6643             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
6644                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
6645                    getGoogleStyle()));
6646 
6647   FormatStyle Style = getLLVMStyleWithColumns(12);
6648   Style.BreakStringLiterals = false;
6649   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
6650 
6651   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
6652   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
6653   EXPECT_EQ("#define A \\\n"
6654             "  \"some \" \\\n"
6655             "  \"text \" \\\n"
6656             "  \"other\";",
6657             format("#define A \"some text other\";", AlignLeft));
6658 }
6659 
6660 TEST_F(FormatTest, FullyRemoveEmptyLines) {
6661   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
6662   NoEmptyLines.MaxEmptyLinesToKeep = 0;
6663   EXPECT_EQ("int i = a(b());",
6664             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
6665 }
6666 
6667 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
6668   EXPECT_EQ(
6669       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
6670       "(\n"
6671       "    \"x\t\");",
6672       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
6673              "aaaaaaa("
6674              "\"x\t\");"));
6675 }
6676 
6677 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
6678   EXPECT_EQ(
6679       "u8\"utf8 string \"\n"
6680       "u8\"literal\";",
6681       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
6682   EXPECT_EQ(
6683       "u\"utf16 string \"\n"
6684       "u\"literal\";",
6685       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
6686   EXPECT_EQ(
6687       "U\"utf32 string \"\n"
6688       "U\"literal\";",
6689       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
6690   EXPECT_EQ("L\"wide string \"\n"
6691             "L\"literal\";",
6692             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
6693   EXPECT_EQ("@\"NSString \"\n"
6694             "@\"literal\";",
6695             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
6696   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
6697 
6698   // This input makes clang-format try to split the incomplete unicode escape
6699   // sequence, which used to lead to a crasher.
6700   verifyNoCrash(
6701       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
6702       getLLVMStyleWithColumns(60));
6703 }
6704 
6705 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
6706   FormatStyle Style = getGoogleStyleWithColumns(15);
6707   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
6708   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
6709   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
6710   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
6711   EXPECT_EQ("u8R\"x(raw literal)x\";",
6712             format("u8R\"x(raw literal)x\";", Style));
6713 }
6714 
6715 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
6716   FormatStyle Style = getLLVMStyleWithColumns(20);
6717   EXPECT_EQ(
6718       "_T(\"aaaaaaaaaaaaaa\")\n"
6719       "_T(\"aaaaaaaaaaaaaa\")\n"
6720       "_T(\"aaaaaaaaaaaa\")",
6721       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
6722   EXPECT_EQ("f(x,\n"
6723             "  _T(\"aaaaaaaaaaaa\")\n"
6724             "  _T(\"aaa\"),\n"
6725             "  z);",
6726             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
6727 
6728   // FIXME: Handle embedded spaces in one iteration.
6729   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
6730   //            "_T(\"aaaaaaaaaaaaa\")\n"
6731   //            "_T(\"aaaaaaaaaaaaa\")\n"
6732   //            "_T(\"a\")",
6733   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
6734   //                   getLLVMStyleWithColumns(20)));
6735   EXPECT_EQ(
6736       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
6737       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
6738   EXPECT_EQ("f(\n"
6739             "#if !TEST\n"
6740             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
6741             "#endif\n"
6742             ");",
6743             format("f(\n"
6744                    "#if !TEST\n"
6745                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
6746                    "#endif\n"
6747                    ");"));
6748   EXPECT_EQ("f(\n"
6749             "\n"
6750             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
6751             format("f(\n"
6752                    "\n"
6753                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
6754 }
6755 
6756 TEST_F(FormatTest, BreaksStringLiteralOperands) {
6757   // In a function call with two operands, the second can be broken with no line
6758   // break before it.
6759   EXPECT_EQ("func(a, \"long long \"\n"
6760             "        \"long long\");",
6761             format("func(a, \"long long long long\");",
6762                    getLLVMStyleWithColumns(24)));
6763   // In a function call with three operands, the second must be broken with a
6764   // line break before it.
6765   EXPECT_EQ("func(a,\n"
6766             "     \"long long long \"\n"
6767             "     \"long\",\n"
6768             "     c);",
6769             format("func(a, \"long long long long\", c);",
6770                    getLLVMStyleWithColumns(24)));
6771   // In a function call with three operands, the third must be broken with a
6772   // line break before it.
6773   EXPECT_EQ("func(a, b,\n"
6774             "     \"long long long \"\n"
6775             "     \"long\");",
6776             format("func(a, b, \"long long long long\");",
6777                    getLLVMStyleWithColumns(24)));
6778   // In a function call with three operands, both the second and the third must
6779   // be broken with a line break before them.
6780   EXPECT_EQ("func(a,\n"
6781             "     \"long long long \"\n"
6782             "     \"long\",\n"
6783             "     \"long long long \"\n"
6784             "     \"long\");",
6785             format("func(a, \"long long long long\", \"long long long long\");",
6786                    getLLVMStyleWithColumns(24)));
6787   // In a chain of << with two operands, the second can be broken with no line
6788   // break before it.
6789   EXPECT_EQ("a << \"line line \"\n"
6790             "     \"line\";",
6791             format("a << \"line line line\";",
6792                    getLLVMStyleWithColumns(20)));
6793   // In a chain of << with three operands, the second can be broken with no line
6794   // break before it.
6795   EXPECT_EQ("abcde << \"line \"\n"
6796             "         \"line line\"\n"
6797             "      << c;",
6798             format("abcde << \"line line line\" << c;",
6799                    getLLVMStyleWithColumns(20)));
6800   // In a chain of << with three operands, the third must be broken with a line
6801   // break before it.
6802   EXPECT_EQ("a << b\n"
6803             "  << \"line line \"\n"
6804             "     \"line\";",
6805             format("a << b << \"line line line\";",
6806                    getLLVMStyleWithColumns(20)));
6807   // In a chain of << with three operands, the second can be broken with no line
6808   // break before it and the third must be broken with a line break before it.
6809   EXPECT_EQ("abcd << \"line line \"\n"
6810             "        \"line\"\n"
6811             "     << \"line line \"\n"
6812             "        \"line\";",
6813             format("abcd << \"line line line\" << \"line line line\";",
6814                    getLLVMStyleWithColumns(20)));
6815   // In a chain of binary operators with two operands, the second can be broken
6816   // with no line break before it.
6817   EXPECT_EQ("abcd + \"line line \"\n"
6818             "       \"line line\";",
6819             format("abcd + \"line line line line\";",
6820                    getLLVMStyleWithColumns(20)));
6821   // In a chain of binary operators with three operands, the second must be
6822   // broken with a line break before it.
6823   EXPECT_EQ("abcd +\n"
6824             "    \"line line \"\n"
6825             "    \"line line\" +\n"
6826             "    e;",
6827             format("abcd + \"line line line line\" + e;",
6828                    getLLVMStyleWithColumns(20)));
6829   // In a function call with two operands, with AlignAfterOpenBracket enabled,
6830   // the first must be broken with a line break before it.
6831   FormatStyle Style = getLLVMStyleWithColumns(25);
6832   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6833   EXPECT_EQ("someFunction(\n"
6834             "    \"long long long \"\n"
6835             "    \"long\",\n"
6836             "    a);",
6837             format("someFunction(\"long long long long\", a);", Style));
6838 }
6839 
6840 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
6841   EXPECT_EQ(
6842       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
6843       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
6844       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
6845       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
6846              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
6847              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
6848 }
6849 
6850 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
6851   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
6852             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
6853   EXPECT_EQ("fffffffffff(g(R\"x(\n"
6854             "multiline raw string literal xxxxxxxxxxxxxx\n"
6855             ")x\",\n"
6856             "              a),\n"
6857             "            b);",
6858             format("fffffffffff(g(R\"x(\n"
6859                    "multiline raw string literal xxxxxxxxxxxxxx\n"
6860                    ")x\", a), b);",
6861                    getGoogleStyleWithColumns(20)));
6862   EXPECT_EQ("fffffffffff(\n"
6863             "    g(R\"x(qqq\n"
6864             "multiline raw string literal xxxxxxxxxxxxxx\n"
6865             ")x\",\n"
6866             "      a),\n"
6867             "    b);",
6868             format("fffffffffff(g(R\"x(qqq\n"
6869                    "multiline raw string literal xxxxxxxxxxxxxx\n"
6870                    ")x\", a), b);",
6871                    getGoogleStyleWithColumns(20)));
6872 
6873   EXPECT_EQ("fffffffffff(R\"x(\n"
6874             "multiline raw string literal xxxxxxxxxxxxxx\n"
6875             ")x\");",
6876             format("fffffffffff(R\"x(\n"
6877                    "multiline raw string literal xxxxxxxxxxxxxx\n"
6878                    ")x\");",
6879                    getGoogleStyleWithColumns(20)));
6880   EXPECT_EQ("fffffffffff(R\"x(\n"
6881             "multiline raw string literal xxxxxxxxxxxxxx\n"
6882             ")x\" + bbbbbb);",
6883             format("fffffffffff(R\"x(\n"
6884                    "multiline raw string literal xxxxxxxxxxxxxx\n"
6885                    ")x\" +   bbbbbb);",
6886                    getGoogleStyleWithColumns(20)));
6887   EXPECT_EQ("fffffffffff(\n"
6888             "    R\"x(\n"
6889             "multiline raw string literal xxxxxxxxxxxxxx\n"
6890             ")x\" +\n"
6891             "    bbbbbb);",
6892             format("fffffffffff(\n"
6893                    " R\"x(\n"
6894                    "multiline raw string literal xxxxxxxxxxxxxx\n"
6895                    ")x\" + bbbbbb);",
6896                    getGoogleStyleWithColumns(20)));
6897 }
6898 
6899 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
6900   verifyFormat("string a = \"unterminated;");
6901   EXPECT_EQ("function(\"unterminated,\n"
6902             "         OtherParameter);",
6903             format("function(  \"unterminated,\n"
6904                    "    OtherParameter);"));
6905 }
6906 
6907 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
6908   FormatStyle Style = getLLVMStyle();
6909   Style.Standard = FormatStyle::LS_Cpp03;
6910   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
6911             format("#define x(_a) printf(\"foo\"_a);", Style));
6912 }
6913 
6914 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
6915 
6916 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
6917   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
6918             "             \"ddeeefff\");",
6919             format("someFunction(\"aaabbbcccdddeeefff\");",
6920                    getLLVMStyleWithColumns(25)));
6921   EXPECT_EQ("someFunction1234567890(\n"
6922             "    \"aaabbbcccdddeeefff\");",
6923             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
6924                    getLLVMStyleWithColumns(26)));
6925   EXPECT_EQ("someFunction1234567890(\n"
6926             "    \"aaabbbcccdddeeeff\"\n"
6927             "    \"f\");",
6928             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
6929                    getLLVMStyleWithColumns(25)));
6930   EXPECT_EQ("someFunction1234567890(\n"
6931             "    \"aaabbbcccdddeeeff\"\n"
6932             "    \"f\");",
6933             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
6934                    getLLVMStyleWithColumns(24)));
6935   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
6936             "             \"ddde \"\n"
6937             "             \"efff\");",
6938             format("someFunction(\"aaabbbcc ddde efff\");",
6939                    getLLVMStyleWithColumns(25)));
6940   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
6941             "             \"ddeeefff\");",
6942             format("someFunction(\"aaabbbccc ddeeefff\");",
6943                    getLLVMStyleWithColumns(25)));
6944   EXPECT_EQ("someFunction1234567890(\n"
6945             "    \"aaabb \"\n"
6946             "    \"cccdddeeefff\");",
6947             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
6948                    getLLVMStyleWithColumns(25)));
6949   EXPECT_EQ("#define A          \\\n"
6950             "  string s =       \\\n"
6951             "      \"123456789\"  \\\n"
6952             "      \"0\";         \\\n"
6953             "  int i;",
6954             format("#define A string s = \"1234567890\"; int i;",
6955                    getLLVMStyleWithColumns(20)));
6956   // FIXME: Put additional penalties on breaking at non-whitespace locations.
6957   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
6958             "             \"dddeeeff\"\n"
6959             "             \"f\");",
6960             format("someFunction(\"aaabbbcc dddeeefff\");",
6961                    getLLVMStyleWithColumns(25)));
6962 }
6963 
6964 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
6965   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
6966   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
6967   EXPECT_EQ("\"test\"\n"
6968             "\"\\n\"",
6969             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
6970   EXPECT_EQ("\"tes\\\\\"\n"
6971             "\"n\"",
6972             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
6973   EXPECT_EQ("\"\\\\\\\\\"\n"
6974             "\"\\n\"",
6975             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
6976   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
6977   EXPECT_EQ("\"\\uff01\"\n"
6978             "\"test\"",
6979             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
6980   EXPECT_EQ("\"\\Uff01ff02\"",
6981             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
6982   EXPECT_EQ("\"\\x000000000001\"\n"
6983             "\"next\"",
6984             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
6985   EXPECT_EQ("\"\\x000000000001next\"",
6986             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
6987   EXPECT_EQ("\"\\x000000000001\"",
6988             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
6989   EXPECT_EQ("\"test\"\n"
6990             "\"\\000000\"\n"
6991             "\"000001\"",
6992             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
6993   EXPECT_EQ("\"test\\000\"\n"
6994             "\"00000000\"\n"
6995             "\"1\"",
6996             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
6997 }
6998 
6999 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
7000   verifyFormat("void f() {\n"
7001                "  return g() {}\n"
7002                "  void h() {}");
7003   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
7004                "g();\n"
7005                "}");
7006 }
7007 
7008 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
7009   verifyFormat(
7010       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
7011 }
7012 
7013 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
7014   verifyFormat("class X {\n"
7015                "  void f() {\n"
7016                "  }\n"
7017                "};",
7018                getLLVMStyleWithColumns(12));
7019 }
7020 
7021 TEST_F(FormatTest, ConfigurableIndentWidth) {
7022   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
7023   EightIndent.IndentWidth = 8;
7024   EightIndent.ContinuationIndentWidth = 8;
7025   verifyFormat("void f() {\n"
7026                "        someFunction();\n"
7027                "        if (true) {\n"
7028                "                f();\n"
7029                "        }\n"
7030                "}",
7031                EightIndent);
7032   verifyFormat("class X {\n"
7033                "        void f() {\n"
7034                "        }\n"
7035                "};",
7036                EightIndent);
7037   verifyFormat("int x[] = {\n"
7038                "        call(),\n"
7039                "        call()};",
7040                EightIndent);
7041 }
7042 
7043 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
7044   verifyFormat("double\n"
7045                "f();",
7046                getLLVMStyleWithColumns(8));
7047 }
7048 
7049 TEST_F(FormatTest, ConfigurableUseOfTab) {
7050   FormatStyle Tab = getLLVMStyleWithColumns(42);
7051   Tab.IndentWidth = 8;
7052   Tab.UseTab = FormatStyle::UT_Always;
7053   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
7054 
7055   EXPECT_EQ("if (aaaaaaaa && // q\n"
7056             "    bb)\t\t// w\n"
7057             "\t;",
7058             format("if (aaaaaaaa &&// q\n"
7059                    "bb)// w\n"
7060                    ";",
7061                    Tab));
7062   EXPECT_EQ("if (aaa && bbb) // w\n"
7063             "\t;",
7064             format("if(aaa&&bbb)// w\n"
7065                    ";",
7066                    Tab));
7067 
7068   verifyFormat("class X {\n"
7069                "\tvoid f() {\n"
7070                "\t\tsomeFunction(parameter1,\n"
7071                "\t\t\t     parameter2);\n"
7072                "\t}\n"
7073                "};",
7074                Tab);
7075   verifyFormat("#define A                        \\\n"
7076                "\tvoid f() {               \\\n"
7077                "\t\tsomeFunction(    \\\n"
7078                "\t\t    parameter1,  \\\n"
7079                "\t\t    parameter2); \\\n"
7080                "\t}",
7081                Tab);
7082 
7083   Tab.TabWidth = 4;
7084   Tab.IndentWidth = 8;
7085   verifyFormat("class TabWidth4Indent8 {\n"
7086                "\t\tvoid f() {\n"
7087                "\t\t\t\tsomeFunction(parameter1,\n"
7088                "\t\t\t\t\t\t\t parameter2);\n"
7089                "\t\t}\n"
7090                "};",
7091                Tab);
7092 
7093   Tab.TabWidth = 4;
7094   Tab.IndentWidth = 4;
7095   verifyFormat("class TabWidth4Indent4 {\n"
7096                "\tvoid f() {\n"
7097                "\t\tsomeFunction(parameter1,\n"
7098                "\t\t\t\t\t parameter2);\n"
7099                "\t}\n"
7100                "};",
7101                Tab);
7102 
7103   Tab.TabWidth = 8;
7104   Tab.IndentWidth = 4;
7105   verifyFormat("class TabWidth8Indent4 {\n"
7106                "    void f() {\n"
7107                "\tsomeFunction(parameter1,\n"
7108                "\t\t     parameter2);\n"
7109                "    }\n"
7110                "};",
7111                Tab);
7112 
7113   Tab.TabWidth = 8;
7114   Tab.IndentWidth = 8;
7115   EXPECT_EQ("/*\n"
7116             "\t      a\t\tcomment\n"
7117             "\t      in multiple lines\n"
7118             "       */",
7119             format("   /*\t \t \n"
7120                    " \t \t a\t\tcomment\t \t\n"
7121                    " \t \t in multiple lines\t\n"
7122                    " \t  */",
7123                    Tab));
7124 
7125   Tab.UseTab = FormatStyle::UT_ForIndentation;
7126   verifyFormat("{\n"
7127                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7128                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7129                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7130                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7131                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7132                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7133                "};",
7134                Tab);
7135   verifyFormat("enum AA {\n"
7136                "\ta1, // Force multiple lines\n"
7137                "\ta2,\n"
7138                "\ta3\n"
7139                "};",
7140                Tab);
7141   EXPECT_EQ("if (aaaaaaaa && // q\n"
7142             "    bb)         // w\n"
7143             "\t;",
7144             format("if (aaaaaaaa &&// q\n"
7145                    "bb)// w\n"
7146                    ";",
7147                    Tab));
7148   verifyFormat("class X {\n"
7149                "\tvoid f() {\n"
7150                "\t\tsomeFunction(parameter1,\n"
7151                "\t\t             parameter2);\n"
7152                "\t}\n"
7153                "};",
7154                Tab);
7155   verifyFormat("{\n"
7156                "\tQ(\n"
7157                "\t    {\n"
7158                "\t\t    int a;\n"
7159                "\t\t    someFunction(aaaaaaaa,\n"
7160                "\t\t                 bbbbbbb);\n"
7161                "\t    },\n"
7162                "\t    p);\n"
7163                "}",
7164                Tab);
7165   EXPECT_EQ("{\n"
7166             "\t/* aaaa\n"
7167             "\t   bbbb */\n"
7168             "}",
7169             format("{\n"
7170                    "/* aaaa\n"
7171                    "   bbbb */\n"
7172                    "}",
7173                    Tab));
7174   EXPECT_EQ("{\n"
7175             "\t/*\n"
7176             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7177             "\t  bbbbbbbbbbbbb\n"
7178             "\t*/\n"
7179             "}",
7180             format("{\n"
7181                    "/*\n"
7182                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7183                    "*/\n"
7184                    "}",
7185                    Tab));
7186   EXPECT_EQ("{\n"
7187             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7188             "\t// bbbbbbbbbbbbb\n"
7189             "}",
7190             format("{\n"
7191                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7192                    "}",
7193                    Tab));
7194   EXPECT_EQ("{\n"
7195             "\t/*\n"
7196             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7197             "\t  bbbbbbbbbbbbb\n"
7198             "\t*/\n"
7199             "}",
7200             format("{\n"
7201                    "\t/*\n"
7202                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7203                    "\t*/\n"
7204                    "}",
7205                    Tab));
7206   EXPECT_EQ("{\n"
7207             "\t/*\n"
7208             "\n"
7209             "\t*/\n"
7210             "}",
7211             format("{\n"
7212                    "\t/*\n"
7213                    "\n"
7214                    "\t*/\n"
7215                    "}",
7216                    Tab));
7217   EXPECT_EQ("{\n"
7218             "\t/*\n"
7219             " asdf\n"
7220             "\t*/\n"
7221             "}",
7222             format("{\n"
7223                    "\t/*\n"
7224                    " asdf\n"
7225                    "\t*/\n"
7226                    "}",
7227                    Tab));
7228 
7229   Tab.UseTab = FormatStyle::UT_Never;
7230   EXPECT_EQ("/*\n"
7231             "              a\t\tcomment\n"
7232             "              in multiple lines\n"
7233             "       */",
7234             format("   /*\t \t \n"
7235                    " \t \t a\t\tcomment\t \t\n"
7236                    " \t \t in multiple lines\t\n"
7237                    " \t  */",
7238                    Tab));
7239   EXPECT_EQ("/* some\n"
7240             "   comment */",
7241             format(" \t \t /* some\n"
7242                    " \t \t    comment */",
7243                    Tab));
7244   EXPECT_EQ("int a; /* some\n"
7245             "   comment */",
7246             format(" \t \t int a; /* some\n"
7247                    " \t \t    comment */",
7248                    Tab));
7249 
7250   EXPECT_EQ("int a; /* some\n"
7251             "comment */",
7252             format(" \t \t int\ta; /* some\n"
7253                    " \t \t    comment */",
7254                    Tab));
7255   EXPECT_EQ("f(\"\t\t\"); /* some\n"
7256             "    comment */",
7257             format(" \t \t f(\"\t\t\"); /* some\n"
7258                    " \t \t    comment */",
7259                    Tab));
7260   EXPECT_EQ("{\n"
7261             "  /*\n"
7262             "   * Comment\n"
7263             "   */\n"
7264             "  int i;\n"
7265             "}",
7266             format("{\n"
7267                    "\t/*\n"
7268                    "\t * Comment\n"
7269                    "\t */\n"
7270                    "\t int i;\n"
7271                    "}"));
7272 
7273   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
7274   Tab.TabWidth = 8;
7275   Tab.IndentWidth = 8;
7276   EXPECT_EQ("if (aaaaaaaa && // q\n"
7277             "    bb)         // w\n"
7278             "\t;",
7279             format("if (aaaaaaaa &&// q\n"
7280                    "bb)// w\n"
7281                    ";",
7282                    Tab));
7283   EXPECT_EQ("if (aaa && bbb) // w\n"
7284             "\t;",
7285             format("if(aaa&&bbb)// w\n"
7286                    ";",
7287                    Tab));
7288   verifyFormat("class X {\n"
7289                "\tvoid f() {\n"
7290                "\t\tsomeFunction(parameter1,\n"
7291                "\t\t\t     parameter2);\n"
7292                "\t}\n"
7293                "};",
7294                Tab);
7295   verifyFormat("#define A                        \\\n"
7296                "\tvoid f() {               \\\n"
7297                "\t\tsomeFunction(    \\\n"
7298                "\t\t    parameter1,  \\\n"
7299                "\t\t    parameter2); \\\n"
7300                "\t}",
7301                Tab);
7302   Tab.TabWidth = 4;
7303   Tab.IndentWidth = 8;
7304   verifyFormat("class TabWidth4Indent8 {\n"
7305                "\t\tvoid f() {\n"
7306                "\t\t\t\tsomeFunction(parameter1,\n"
7307                "\t\t\t\t\t\t\t parameter2);\n"
7308                "\t\t}\n"
7309                "};",
7310                Tab);
7311   Tab.TabWidth = 4;
7312   Tab.IndentWidth = 4;
7313   verifyFormat("class TabWidth4Indent4 {\n"
7314                "\tvoid f() {\n"
7315                "\t\tsomeFunction(parameter1,\n"
7316                "\t\t\t\t\t parameter2);\n"
7317                "\t}\n"
7318                "};",
7319                Tab);
7320   Tab.TabWidth = 8;
7321   Tab.IndentWidth = 4;
7322   verifyFormat("class TabWidth8Indent4 {\n"
7323                "    void f() {\n"
7324                "\tsomeFunction(parameter1,\n"
7325                "\t\t     parameter2);\n"
7326                "    }\n"
7327                "};",
7328                Tab);
7329   Tab.TabWidth = 8;
7330   Tab.IndentWidth = 8;
7331   EXPECT_EQ("/*\n"
7332             "\t      a\t\tcomment\n"
7333             "\t      in multiple lines\n"
7334             "       */",
7335             format("   /*\t \t \n"
7336                    " \t \t a\t\tcomment\t \t\n"
7337                    " \t \t in multiple lines\t\n"
7338                    " \t  */",
7339                    Tab));
7340   verifyFormat("{\n"
7341                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7342                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7343                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7344                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7345                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7346                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7347                "};",
7348                Tab);
7349   verifyFormat("enum AA {\n"
7350                "\ta1, // Force multiple lines\n"
7351                "\ta2,\n"
7352                "\ta3\n"
7353                "};",
7354                Tab);
7355   EXPECT_EQ("if (aaaaaaaa && // q\n"
7356             "    bb)         // w\n"
7357             "\t;",
7358             format("if (aaaaaaaa &&// q\n"
7359                    "bb)// w\n"
7360                    ";",
7361                    Tab));
7362   verifyFormat("class X {\n"
7363                "\tvoid f() {\n"
7364                "\t\tsomeFunction(parameter1,\n"
7365                "\t\t\t     parameter2);\n"
7366                "\t}\n"
7367                "};",
7368                Tab);
7369   verifyFormat("{\n"
7370                "\tQ(\n"
7371                "\t    {\n"
7372                "\t\t    int a;\n"
7373                "\t\t    someFunction(aaaaaaaa,\n"
7374                "\t\t\t\t bbbbbbb);\n"
7375                "\t    },\n"
7376                "\t    p);\n"
7377                "}",
7378                Tab);
7379   EXPECT_EQ("{\n"
7380             "\t/* aaaa\n"
7381             "\t   bbbb */\n"
7382             "}",
7383             format("{\n"
7384                    "/* aaaa\n"
7385                    "   bbbb */\n"
7386                    "}",
7387                    Tab));
7388   EXPECT_EQ("{\n"
7389             "\t/*\n"
7390             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7391             "\t  bbbbbbbbbbbbb\n"
7392             "\t*/\n"
7393             "}",
7394             format("{\n"
7395                    "/*\n"
7396                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7397                    "*/\n"
7398                    "}",
7399                    Tab));
7400   EXPECT_EQ("{\n"
7401             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7402             "\t// bbbbbbbbbbbbb\n"
7403             "}",
7404             format("{\n"
7405                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7406                    "}",
7407                    Tab));
7408   EXPECT_EQ("{\n"
7409             "\t/*\n"
7410             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7411             "\t  bbbbbbbbbbbbb\n"
7412             "\t*/\n"
7413             "}",
7414             format("{\n"
7415                    "\t/*\n"
7416                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7417                    "\t*/\n"
7418                    "}",
7419                    Tab));
7420   EXPECT_EQ("{\n"
7421             "\t/*\n"
7422             "\n"
7423             "\t*/\n"
7424             "}",
7425             format("{\n"
7426                    "\t/*\n"
7427                    "\n"
7428                    "\t*/\n"
7429                    "}",
7430                    Tab));
7431   EXPECT_EQ("{\n"
7432             "\t/*\n"
7433             " asdf\n"
7434             "\t*/\n"
7435             "}",
7436             format("{\n"
7437                    "\t/*\n"
7438                    " asdf\n"
7439                    "\t*/\n"
7440                    "}",
7441                    Tab));
7442   EXPECT_EQ("/*\n"
7443             "\t      a\t\tcomment\n"
7444             "\t      in multiple lines\n"
7445             "       */",
7446             format("   /*\t \t \n"
7447                    " \t \t a\t\tcomment\t \t\n"
7448                    " \t \t in multiple lines\t\n"
7449                    " \t  */",
7450                    Tab));
7451   EXPECT_EQ("/* some\n"
7452             "   comment */",
7453             format(" \t \t /* some\n"
7454                    " \t \t    comment */",
7455                    Tab));
7456   EXPECT_EQ("int a; /* some\n"
7457             "   comment */",
7458             format(" \t \t int a; /* some\n"
7459                    " \t \t    comment */",
7460                    Tab));
7461   EXPECT_EQ("int a; /* some\n"
7462             "comment */",
7463             format(" \t \t int\ta; /* some\n"
7464                    " \t \t    comment */",
7465                    Tab));
7466   EXPECT_EQ("f(\"\t\t\"); /* some\n"
7467             "    comment */",
7468             format(" \t \t f(\"\t\t\"); /* some\n"
7469                    " \t \t    comment */",
7470                    Tab));
7471   EXPECT_EQ("{\n"
7472             "  /*\n"
7473             "   * Comment\n"
7474             "   */\n"
7475             "  int i;\n"
7476             "}",
7477             format("{\n"
7478                    "\t/*\n"
7479                    "\t * Comment\n"
7480                    "\t */\n"
7481                    "\t int i;\n"
7482                    "}"));
7483   Tab.AlignConsecutiveAssignments = true;
7484   Tab.AlignConsecutiveDeclarations = true;
7485   Tab.TabWidth = 4;
7486   Tab.IndentWidth = 4;
7487   verifyFormat("class Assign {\n"
7488                "\tvoid f() {\n"
7489                "\t\tint         x      = 123;\n"
7490                "\t\tint         random = 4;\n"
7491                "\t\tstd::string alphabet =\n"
7492                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
7493                "\t}\n"
7494                "};",
7495                Tab);
7496 }
7497 
7498 TEST_F(FormatTest, CalculatesOriginalColumn) {
7499   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7500             "q\"; /* some\n"
7501             "       comment */",
7502             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7503                    "q\"; /* some\n"
7504                    "       comment */",
7505                    getLLVMStyle()));
7506   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
7507             "/* some\n"
7508             "   comment */",
7509             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
7510                    " /* some\n"
7511                    "    comment */",
7512                    getLLVMStyle()));
7513   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7514             "qqq\n"
7515             "/* some\n"
7516             "   comment */",
7517             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7518                    "qqq\n"
7519                    " /* some\n"
7520                    "    comment */",
7521                    getLLVMStyle()));
7522   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7523             "wwww; /* some\n"
7524             "         comment */",
7525             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7526                    "wwww; /* some\n"
7527                    "         comment */",
7528                    getLLVMStyle()));
7529 }
7530 
7531 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
7532   FormatStyle NoSpace = getLLVMStyle();
7533   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
7534 
7535   verifyFormat("while(true)\n"
7536                "  continue;",
7537                NoSpace);
7538   verifyFormat("for(;;)\n"
7539                "  continue;",
7540                NoSpace);
7541   verifyFormat("if(true)\n"
7542                "  f();\n"
7543                "else if(true)\n"
7544                "  f();",
7545                NoSpace);
7546   verifyFormat("do {\n"
7547                "  do_something();\n"
7548                "} while(something());",
7549                NoSpace);
7550   verifyFormat("switch(x) {\n"
7551                "default:\n"
7552                "  break;\n"
7553                "}",
7554                NoSpace);
7555   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
7556   verifyFormat("size_t x = sizeof(x);", NoSpace);
7557   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
7558   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
7559   verifyFormat("alignas(128) char a[128];", NoSpace);
7560   verifyFormat("size_t x = alignof(MyType);", NoSpace);
7561   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
7562   verifyFormat("int f() throw(Deprecated);", NoSpace);
7563   verifyFormat("typedef void (*cb)(int);", NoSpace);
7564   verifyFormat("T A::operator()();", NoSpace);
7565   verifyFormat("X A::operator++(T);", NoSpace);
7566 
7567   FormatStyle Space = getLLVMStyle();
7568   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
7569 
7570   verifyFormat("int f ();", Space);
7571   verifyFormat("void f (int a, T b) {\n"
7572                "  while (true)\n"
7573                "    continue;\n"
7574                "}",
7575                Space);
7576   verifyFormat("if (true)\n"
7577                "  f ();\n"
7578                "else if (true)\n"
7579                "  f ();",
7580                Space);
7581   verifyFormat("do {\n"
7582                "  do_something ();\n"
7583                "} while (something ());",
7584                Space);
7585   verifyFormat("switch (x) {\n"
7586                "default:\n"
7587                "  break;\n"
7588                "}",
7589                Space);
7590   verifyFormat("A::A () : a (1) {}", Space);
7591   verifyFormat("void f () __attribute__ ((asdf));", Space);
7592   verifyFormat("*(&a + 1);\n"
7593                "&((&a)[1]);\n"
7594                "a[(b + c) * d];\n"
7595                "(((a + 1) * 2) + 3) * 4;",
7596                Space);
7597   verifyFormat("#define A(x) x", Space);
7598   verifyFormat("#define A (x) x", Space);
7599   verifyFormat("#if defined(x)\n"
7600                "#endif",
7601                Space);
7602   verifyFormat("auto i = std::make_unique<int> (5);", Space);
7603   verifyFormat("size_t x = sizeof (x);", Space);
7604   verifyFormat("auto f (int x) -> decltype (x);", Space);
7605   verifyFormat("int f (T x) noexcept (x.create ());", Space);
7606   verifyFormat("alignas (128) char a[128];", Space);
7607   verifyFormat("size_t x = alignof (MyType);", Space);
7608   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
7609   verifyFormat("int f () throw (Deprecated);", Space);
7610   verifyFormat("typedef void (*cb) (int);", Space);
7611   verifyFormat("T A::operator() ();", Space);
7612   verifyFormat("X A::operator++ (T);", Space);
7613 }
7614 
7615 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
7616   FormatStyle Spaces = getLLVMStyle();
7617 
7618   Spaces.SpacesInParentheses = true;
7619   verifyFormat("call( x, y, z );", Spaces);
7620   verifyFormat("call();", Spaces);
7621   verifyFormat("std::function<void( int, int )> callback;", Spaces);
7622   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
7623                Spaces);
7624   verifyFormat("while ( (bool)1 )\n"
7625                "  continue;",
7626                Spaces);
7627   verifyFormat("for ( ;; )\n"
7628                "  continue;",
7629                Spaces);
7630   verifyFormat("if ( true )\n"
7631                "  f();\n"
7632                "else if ( true )\n"
7633                "  f();",
7634                Spaces);
7635   verifyFormat("do {\n"
7636                "  do_something( (int)i );\n"
7637                "} while ( something() );",
7638                Spaces);
7639   verifyFormat("switch ( x ) {\n"
7640                "default:\n"
7641                "  break;\n"
7642                "}",
7643                Spaces);
7644 
7645   Spaces.SpacesInParentheses = false;
7646   Spaces.SpacesInCStyleCastParentheses = true;
7647   verifyFormat("Type *A = ( Type * )P;", Spaces);
7648   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
7649   verifyFormat("x = ( int32 )y;", Spaces);
7650   verifyFormat("int a = ( int )(2.0f);", Spaces);
7651   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
7652   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
7653   verifyFormat("#define x (( int )-1)", Spaces);
7654 
7655   // Run the first set of tests again with:
7656   Spaces.SpacesInParentheses = false;
7657   Spaces.SpaceInEmptyParentheses = true;
7658   Spaces.SpacesInCStyleCastParentheses = true;
7659   verifyFormat("call(x, y, z);", Spaces);
7660   verifyFormat("call( );", Spaces);
7661   verifyFormat("std::function<void(int, int)> callback;", Spaces);
7662   verifyFormat("while (( bool )1)\n"
7663                "  continue;",
7664                Spaces);
7665   verifyFormat("for (;;)\n"
7666                "  continue;",
7667                Spaces);
7668   verifyFormat("if (true)\n"
7669                "  f( );\n"
7670                "else if (true)\n"
7671                "  f( );",
7672                Spaces);
7673   verifyFormat("do {\n"
7674                "  do_something(( int )i);\n"
7675                "} while (something( ));",
7676                Spaces);
7677   verifyFormat("switch (x) {\n"
7678                "default:\n"
7679                "  break;\n"
7680                "}",
7681                Spaces);
7682 
7683   // Run the first set of tests again with:
7684   Spaces.SpaceAfterCStyleCast = true;
7685   verifyFormat("call(x, y, z);", Spaces);
7686   verifyFormat("call( );", Spaces);
7687   verifyFormat("std::function<void(int, int)> callback;", Spaces);
7688   verifyFormat("while (( bool ) 1)\n"
7689                "  continue;",
7690                Spaces);
7691   verifyFormat("for (;;)\n"
7692                "  continue;",
7693                Spaces);
7694   verifyFormat("if (true)\n"
7695                "  f( );\n"
7696                "else if (true)\n"
7697                "  f( );",
7698                Spaces);
7699   verifyFormat("do {\n"
7700                "  do_something(( int ) i);\n"
7701                "} while (something( ));",
7702                Spaces);
7703   verifyFormat("switch (x) {\n"
7704                "default:\n"
7705                "  break;\n"
7706                "}",
7707                Spaces);
7708 
7709   // Run subset of tests again with:
7710   Spaces.SpacesInCStyleCastParentheses = false;
7711   Spaces.SpaceAfterCStyleCast = true;
7712   verifyFormat("while ((bool) 1)\n"
7713                "  continue;",
7714                Spaces);
7715   verifyFormat("do {\n"
7716                "  do_something((int) i);\n"
7717                "} while (something( ));",
7718                Spaces);
7719 }
7720 
7721 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
7722   verifyFormat("int a[5];");
7723   verifyFormat("a[3] += 42;");
7724 
7725   FormatStyle Spaces = getLLVMStyle();
7726   Spaces.SpacesInSquareBrackets = true;
7727   // Lambdas unchanged.
7728   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
7729   verifyFormat("return [i, args...] {};", Spaces);
7730 
7731   // Not lambdas.
7732   verifyFormat("int a[ 5 ];", Spaces);
7733   verifyFormat("a[ 3 ] += 42;", Spaces);
7734   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
7735   verifyFormat("double &operator[](int i) { return 0; }\n"
7736                "int i;",
7737                Spaces);
7738   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
7739   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
7740   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
7741 }
7742 
7743 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
7744   verifyFormat("int a = 5;");
7745   verifyFormat("a += 42;");
7746   verifyFormat("a or_eq 8;");
7747 
7748   FormatStyle Spaces = getLLVMStyle();
7749   Spaces.SpaceBeforeAssignmentOperators = false;
7750   verifyFormat("int a= 5;", Spaces);
7751   verifyFormat("a+= 42;", Spaces);
7752   verifyFormat("a or_eq 8;", Spaces);
7753 }
7754 
7755 TEST_F(FormatTest, AlignConsecutiveAssignments) {
7756   FormatStyle Alignment = getLLVMStyle();
7757   Alignment.AlignConsecutiveAssignments = false;
7758   verifyFormat("int a = 5;\n"
7759                "int oneTwoThree = 123;",
7760                Alignment);
7761   verifyFormat("int a = 5;\n"
7762                "int oneTwoThree = 123;",
7763                Alignment);
7764 
7765   Alignment.AlignConsecutiveAssignments = true;
7766   verifyFormat("int a           = 5;\n"
7767                "int oneTwoThree = 123;",
7768                Alignment);
7769   verifyFormat("int a           = method();\n"
7770                "int oneTwoThree = 133;",
7771                Alignment);
7772   verifyFormat("a &= 5;\n"
7773                "bcd *= 5;\n"
7774                "ghtyf += 5;\n"
7775                "dvfvdb -= 5;\n"
7776                "a /= 5;\n"
7777                "vdsvsv %= 5;\n"
7778                "sfdbddfbdfbb ^= 5;\n"
7779                "dvsdsv |= 5;\n"
7780                "int dsvvdvsdvvv = 123;",
7781                Alignment);
7782   verifyFormat("int i = 1, j = 10;\n"
7783                "something = 2000;",
7784                Alignment);
7785   verifyFormat("something = 2000;\n"
7786                "int i = 1, j = 10;\n",
7787                Alignment);
7788   verifyFormat("something = 2000;\n"
7789                "another   = 911;\n"
7790                "int i = 1, j = 10;\n"
7791                "oneMore = 1;\n"
7792                "i       = 2;",
7793                Alignment);
7794   verifyFormat("int a   = 5;\n"
7795                "int one = 1;\n"
7796                "method();\n"
7797                "int oneTwoThree = 123;\n"
7798                "int oneTwo      = 12;",
7799                Alignment);
7800   verifyFormat("int oneTwoThree = 123;\n"
7801                "int oneTwo      = 12;\n"
7802                "method();\n",
7803                Alignment);
7804   verifyFormat("int oneTwoThree = 123; // comment\n"
7805                "int oneTwo      = 12;  // comment",
7806                Alignment);
7807   EXPECT_EQ("int a = 5;\n"
7808             "\n"
7809             "int oneTwoThree = 123;",
7810             format("int a       = 5;\n"
7811                    "\n"
7812                    "int oneTwoThree= 123;",
7813                    Alignment));
7814   EXPECT_EQ("int a   = 5;\n"
7815             "int one = 1;\n"
7816             "\n"
7817             "int oneTwoThree = 123;",
7818             format("int a = 5;\n"
7819                    "int one = 1;\n"
7820                    "\n"
7821                    "int oneTwoThree = 123;",
7822                    Alignment));
7823   EXPECT_EQ("int a   = 5;\n"
7824             "int one = 1;\n"
7825             "\n"
7826             "int oneTwoThree = 123;\n"
7827             "int oneTwo      = 12;",
7828             format("int a = 5;\n"
7829                    "int one = 1;\n"
7830                    "\n"
7831                    "int oneTwoThree = 123;\n"
7832                    "int oneTwo = 12;",
7833                    Alignment));
7834   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
7835   verifyFormat("#define A \\\n"
7836                "  int aaaa       = 12; \\\n"
7837                "  int b          = 23; \\\n"
7838                "  int ccc        = 234; \\\n"
7839                "  int dddddddddd = 2345;",
7840                Alignment);
7841   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
7842   verifyFormat("#define A               \\\n"
7843                "  int aaaa       = 12;  \\\n"
7844                "  int b          = 23;  \\\n"
7845                "  int ccc        = 234; \\\n"
7846                "  int dddddddddd = 2345;",
7847                Alignment);
7848   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
7849   verifyFormat("#define A                                                      "
7850                "                \\\n"
7851                "  int aaaa       = 12;                                         "
7852                "                \\\n"
7853                "  int b          = 23;                                         "
7854                "                \\\n"
7855                "  int ccc        = 234;                                        "
7856                "                \\\n"
7857                "  int dddddddddd = 2345;",
7858                Alignment);
7859   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
7860                "k = 4, int l = 5,\n"
7861                "                  int m = 6) {\n"
7862                "  int j      = 10;\n"
7863                "  otherThing = 1;\n"
7864                "}",
7865                Alignment);
7866   verifyFormat("void SomeFunction(int parameter = 0) {\n"
7867                "  int i   = 1;\n"
7868                "  int j   = 2;\n"
7869                "  int big = 10000;\n"
7870                "}",
7871                Alignment);
7872   verifyFormat("class C {\n"
7873                "public:\n"
7874                "  int i            = 1;\n"
7875                "  virtual void f() = 0;\n"
7876                "};",
7877                Alignment);
7878   verifyFormat("int i = 1;\n"
7879                "if (SomeType t = getSomething()) {\n"
7880                "}\n"
7881                "int j   = 2;\n"
7882                "int big = 10000;",
7883                Alignment);
7884   verifyFormat("int j = 7;\n"
7885                "for (int k = 0; k < N; ++k) {\n"
7886                "}\n"
7887                "int j   = 2;\n"
7888                "int big = 10000;\n"
7889                "}",
7890                Alignment);
7891   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7892   verifyFormat("int i = 1;\n"
7893                "LooooooooooongType loooooooooooooooooooooongVariable\n"
7894                "    = someLooooooooooooooooongFunction();\n"
7895                "int j = 2;",
7896                Alignment);
7897   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7898   verifyFormat("int i = 1;\n"
7899                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
7900                "    someLooooooooooooooooongFunction();\n"
7901                "int j = 2;",
7902                Alignment);
7903 
7904   verifyFormat("auto lambda = []() {\n"
7905                "  auto i = 0;\n"
7906                "  return 0;\n"
7907                "};\n"
7908                "int i  = 0;\n"
7909                "auto v = type{\n"
7910                "    i = 1,   //\n"
7911                "    (i = 2), //\n"
7912                "    i = 3    //\n"
7913                "};",
7914                Alignment);
7915 
7916   verifyFormat(
7917       "int i      = 1;\n"
7918       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
7919       "                          loooooooooooooooooooooongParameterB);\n"
7920       "int j      = 2;",
7921       Alignment);
7922 
7923   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
7924                "          typename B   = very_long_type_name_1,\n"
7925                "          typename T_2 = very_long_type_name_2>\n"
7926                "auto foo() {}\n",
7927                Alignment);
7928   verifyFormat("int a, b = 1;\n"
7929                "int c  = 2;\n"
7930                "int dd = 3;\n",
7931                Alignment);
7932   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
7933                "float b[1][] = {{3.f}};\n",
7934                Alignment);
7935   verifyFormat("for (int i = 0; i < 1; i++)\n"
7936                "  int x = 1;\n",
7937                Alignment);
7938   verifyFormat("for (i = 0; i < 1; i++)\n"
7939                "  x = 1;\n"
7940                "y = 1;\n",
7941                Alignment);
7942 }
7943 
7944 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
7945   FormatStyle Alignment = getLLVMStyle();
7946   Alignment.AlignConsecutiveDeclarations = false;
7947   verifyFormat("float const a = 5;\n"
7948                "int oneTwoThree = 123;",
7949                Alignment);
7950   verifyFormat("int a = 5;\n"
7951                "float const oneTwoThree = 123;",
7952                Alignment);
7953 
7954   Alignment.AlignConsecutiveDeclarations = true;
7955   verifyFormat("float const a = 5;\n"
7956                "int         oneTwoThree = 123;",
7957                Alignment);
7958   verifyFormat("int         a = method();\n"
7959                "float const oneTwoThree = 133;",
7960                Alignment);
7961   verifyFormat("int i = 1, j = 10;\n"
7962                "something = 2000;",
7963                Alignment);
7964   verifyFormat("something = 2000;\n"
7965                "int i = 1, j = 10;\n",
7966                Alignment);
7967   verifyFormat("float      something = 2000;\n"
7968                "double     another = 911;\n"
7969                "int        i = 1, j = 10;\n"
7970                "const int *oneMore = 1;\n"
7971                "unsigned   i = 2;",
7972                Alignment);
7973   verifyFormat("float a = 5;\n"
7974                "int   one = 1;\n"
7975                "method();\n"
7976                "const double       oneTwoThree = 123;\n"
7977                "const unsigned int oneTwo = 12;",
7978                Alignment);
7979   verifyFormat("int      oneTwoThree{0}; // comment\n"
7980                "unsigned oneTwo;         // comment",
7981                Alignment);
7982   EXPECT_EQ("float const a = 5;\n"
7983             "\n"
7984             "int oneTwoThree = 123;",
7985             format("float const   a = 5;\n"
7986                    "\n"
7987                    "int           oneTwoThree= 123;",
7988                    Alignment));
7989   EXPECT_EQ("float a = 5;\n"
7990             "int   one = 1;\n"
7991             "\n"
7992             "unsigned oneTwoThree = 123;",
7993             format("float    a = 5;\n"
7994                    "int      one = 1;\n"
7995                    "\n"
7996                    "unsigned oneTwoThree = 123;",
7997                    Alignment));
7998   EXPECT_EQ("float a = 5;\n"
7999             "int   one = 1;\n"
8000             "\n"
8001             "unsigned oneTwoThree = 123;\n"
8002             "int      oneTwo = 12;",
8003             format("float    a = 5;\n"
8004                    "int one = 1;\n"
8005                    "\n"
8006                    "unsigned oneTwoThree = 123;\n"
8007                    "int oneTwo = 12;",
8008                    Alignment));
8009   // Function prototype alignment
8010   verifyFormat("int    a();\n"
8011                "double b();",
8012                Alignment);
8013   verifyFormat("int    a(int x);\n"
8014                "double b();",
8015                Alignment);
8016   unsigned OldColumnLimit = Alignment.ColumnLimit;
8017   // We need to set ColumnLimit to zero, in order to stress nested alignments,
8018   // otherwise the function parameters will be re-flowed onto a single line.
8019   Alignment.ColumnLimit = 0;
8020   EXPECT_EQ("int    a(int   x,\n"
8021             "         float y);\n"
8022             "double b(int    x,\n"
8023             "         double y);",
8024             format("int a(int x,\n"
8025                    " float y);\n"
8026                    "double b(int x,\n"
8027                    " double y);",
8028                    Alignment));
8029   // This ensures that function parameters of function declarations are
8030   // correctly indented when their owning functions are indented.
8031   // The failure case here is for 'double y' to not be indented enough.
8032   EXPECT_EQ("double a(int x);\n"
8033             "int    b(int    y,\n"
8034             "         double z);",
8035             format("double a(int x);\n"
8036                    "int b(int y,\n"
8037                    " double z);",
8038                    Alignment));
8039   // Set ColumnLimit low so that we induce wrapping immediately after
8040   // the function name and opening paren.
8041   Alignment.ColumnLimit = 13;
8042   verifyFormat("int function(\n"
8043                "    int  x,\n"
8044                "    bool y);",
8045                Alignment);
8046   Alignment.ColumnLimit = OldColumnLimit;
8047   // Ensure function pointers don't screw up recursive alignment
8048   verifyFormat("int    a(int x, void (*fp)(int y));\n"
8049                "double b();",
8050                Alignment);
8051   Alignment.AlignConsecutiveAssignments = true;
8052   // Ensure recursive alignment is broken by function braces, so that the
8053   // "a = 1" does not align with subsequent assignments inside the function
8054   // body.
8055   verifyFormat("int func(int a = 1) {\n"
8056                "  int b  = 2;\n"
8057                "  int cc = 3;\n"
8058                "}",
8059                Alignment);
8060   verifyFormat("float      something = 2000;\n"
8061                "double     another   = 911;\n"
8062                "int        i = 1, j = 10;\n"
8063                "const int *oneMore = 1;\n"
8064                "unsigned   i       = 2;",
8065                Alignment);
8066   verifyFormat("int      oneTwoThree = {0}; // comment\n"
8067                "unsigned oneTwo      = 0;   // comment",
8068                Alignment);
8069   // Make sure that scope is correctly tracked, in the absence of braces
8070   verifyFormat("for (int i = 0; i < n; i++)\n"
8071                "  j = i;\n"
8072                "double x = 1;\n",
8073                Alignment);
8074   verifyFormat("if (int i = 0)\n"
8075                "  j = i;\n"
8076                "double x = 1;\n",
8077                Alignment);
8078   // Ensure operator[] and operator() are comprehended
8079   verifyFormat("struct test {\n"
8080                "  long long int foo();\n"
8081                "  int           operator[](int a);\n"
8082                "  double        bar();\n"
8083                "};\n",
8084                Alignment);
8085   verifyFormat("struct test {\n"
8086                "  long long int foo();\n"
8087                "  int           operator()(int a);\n"
8088                "  double        bar();\n"
8089                "};\n",
8090                Alignment);
8091   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
8092             "  int const i   = 1;\n"
8093             "  int *     j   = 2;\n"
8094             "  int       big = 10000;\n"
8095             "\n"
8096             "  unsigned oneTwoThree = 123;\n"
8097             "  int      oneTwo      = 12;\n"
8098             "  method();\n"
8099             "  float k  = 2;\n"
8100             "  int   ll = 10000;\n"
8101             "}",
8102             format("void SomeFunction(int parameter= 0) {\n"
8103                    " int const  i= 1;\n"
8104                    "  int *j=2;\n"
8105                    " int big  =  10000;\n"
8106                    "\n"
8107                    "unsigned oneTwoThree  =123;\n"
8108                    "int oneTwo = 12;\n"
8109                    "  method();\n"
8110                    "float k= 2;\n"
8111                    "int ll=10000;\n"
8112                    "}",
8113                    Alignment));
8114   Alignment.AlignConsecutiveAssignments = false;
8115   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
8116   verifyFormat("#define A \\\n"
8117                "  int       aaaa = 12; \\\n"
8118                "  float     b = 23; \\\n"
8119                "  const int ccc = 234; \\\n"
8120                "  unsigned  dddddddddd = 2345;",
8121                Alignment);
8122   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
8123   verifyFormat("#define A              \\\n"
8124                "  int       aaaa = 12; \\\n"
8125                "  float     b = 23;    \\\n"
8126                "  const int ccc = 234; \\\n"
8127                "  unsigned  dddddddddd = 2345;",
8128                Alignment);
8129   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
8130   Alignment.ColumnLimit = 30;
8131   verifyFormat("#define A                    \\\n"
8132                "  int       aaaa = 12;       \\\n"
8133                "  float     b = 23;          \\\n"
8134                "  const int ccc = 234;       \\\n"
8135                "  int       dddddddddd = 2345;",
8136                Alignment);
8137   Alignment.ColumnLimit = 80;
8138   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
8139                "k = 4, int l = 5,\n"
8140                "                  int m = 6) {\n"
8141                "  const int j = 10;\n"
8142                "  otherThing = 1;\n"
8143                "}",
8144                Alignment);
8145   verifyFormat("void SomeFunction(int parameter = 0) {\n"
8146                "  int const i = 1;\n"
8147                "  int *     j = 2;\n"
8148                "  int       big = 10000;\n"
8149                "}",
8150                Alignment);
8151   verifyFormat("class C {\n"
8152                "public:\n"
8153                "  int          i = 1;\n"
8154                "  virtual void f() = 0;\n"
8155                "};",
8156                Alignment);
8157   verifyFormat("float i = 1;\n"
8158                "if (SomeType t = getSomething()) {\n"
8159                "}\n"
8160                "const unsigned j = 2;\n"
8161                "int            big = 10000;",
8162                Alignment);
8163   verifyFormat("float j = 7;\n"
8164                "for (int k = 0; k < N; ++k) {\n"
8165                "}\n"
8166                "unsigned j = 2;\n"
8167                "int      big = 10000;\n"
8168                "}",
8169                Alignment);
8170   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8171   verifyFormat("float              i = 1;\n"
8172                "LooooooooooongType loooooooooooooooooooooongVariable\n"
8173                "    = someLooooooooooooooooongFunction();\n"
8174                "int j = 2;",
8175                Alignment);
8176   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8177   verifyFormat("int                i = 1;\n"
8178                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
8179                "    someLooooooooooooooooongFunction();\n"
8180                "int j = 2;",
8181                Alignment);
8182 
8183   Alignment.AlignConsecutiveAssignments = true;
8184   verifyFormat("auto lambda = []() {\n"
8185                "  auto  ii = 0;\n"
8186                "  float j  = 0;\n"
8187                "  return 0;\n"
8188                "};\n"
8189                "int   i  = 0;\n"
8190                "float i2 = 0;\n"
8191                "auto  v  = type{\n"
8192                "    i = 1,   //\n"
8193                "    (i = 2), //\n"
8194                "    i = 3    //\n"
8195                "};",
8196                Alignment);
8197   Alignment.AlignConsecutiveAssignments = false;
8198 
8199   verifyFormat(
8200       "int      i = 1;\n"
8201       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
8202       "                          loooooooooooooooooooooongParameterB);\n"
8203       "int      j = 2;",
8204       Alignment);
8205 
8206   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
8207   // We expect declarations and assignments to align, as long as it doesn't
8208   // exceed the column limit, starting a new alignment sequence whenever it
8209   // happens.
8210   Alignment.AlignConsecutiveAssignments = true;
8211   Alignment.ColumnLimit = 30;
8212   verifyFormat("float    ii              = 1;\n"
8213                "unsigned j               = 2;\n"
8214                "int someVerylongVariable = 1;\n"
8215                "AnotherLongType  ll = 123456;\n"
8216                "VeryVeryLongType k  = 2;\n"
8217                "int              myvar = 1;",
8218                Alignment);
8219   Alignment.ColumnLimit = 80;
8220   Alignment.AlignConsecutiveAssignments = false;
8221 
8222   verifyFormat(
8223       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
8224       "          typename LongType, typename B>\n"
8225       "auto foo() {}\n",
8226       Alignment);
8227   verifyFormat("float a, b = 1;\n"
8228                "int   c = 2;\n"
8229                "int   dd = 3;\n",
8230                Alignment);
8231   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
8232                "float b[1][] = {{3.f}};\n",
8233                Alignment);
8234   Alignment.AlignConsecutiveAssignments = true;
8235   verifyFormat("float a, b = 1;\n"
8236                "int   c  = 2;\n"
8237                "int   dd = 3;\n",
8238                Alignment);
8239   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
8240                "float b[1][] = {{3.f}};\n",
8241                Alignment);
8242   Alignment.AlignConsecutiveAssignments = false;
8243 
8244   Alignment.ColumnLimit = 30;
8245   Alignment.BinPackParameters = false;
8246   verifyFormat("void foo(float     a,\n"
8247                "         float     b,\n"
8248                "         int       c,\n"
8249                "         uint32_t *d) {\n"
8250                "  int *  e = 0;\n"
8251                "  float  f = 0;\n"
8252                "  double g = 0;\n"
8253                "}\n"
8254                "void bar(ino_t     a,\n"
8255                "         int       b,\n"
8256                "         uint32_t *c,\n"
8257                "         bool      d) {}\n",
8258                Alignment);
8259   Alignment.BinPackParameters = true;
8260   Alignment.ColumnLimit = 80;
8261 }
8262 
8263 TEST_F(FormatTest, LinuxBraceBreaking) {
8264   FormatStyle LinuxBraceStyle = getLLVMStyle();
8265   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
8266   verifyFormat("namespace a\n"
8267                "{\n"
8268                "class A\n"
8269                "{\n"
8270                "  void f()\n"
8271                "  {\n"
8272                "    if (true) {\n"
8273                "      a();\n"
8274                "      b();\n"
8275                "    } else {\n"
8276                "      a();\n"
8277                "    }\n"
8278                "  }\n"
8279                "  void g() { return; }\n"
8280                "};\n"
8281                "struct B {\n"
8282                "  int x;\n"
8283                "};\n"
8284                "}\n",
8285                LinuxBraceStyle);
8286   verifyFormat("enum X {\n"
8287                "  Y = 0,\n"
8288                "}\n",
8289                LinuxBraceStyle);
8290   verifyFormat("struct S {\n"
8291                "  int Type;\n"
8292                "  union {\n"
8293                "    int x;\n"
8294                "    double y;\n"
8295                "  } Value;\n"
8296                "  class C\n"
8297                "  {\n"
8298                "    MyFavoriteType Value;\n"
8299                "  } Class;\n"
8300                "}\n",
8301                LinuxBraceStyle);
8302 }
8303 
8304 TEST_F(FormatTest, MozillaBraceBreaking) {
8305   FormatStyle MozillaBraceStyle = getLLVMStyle();
8306   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
8307   MozillaBraceStyle.FixNamespaceComments = false;
8308   verifyFormat("namespace a {\n"
8309                "class A\n"
8310                "{\n"
8311                "  void f()\n"
8312                "  {\n"
8313                "    if (true) {\n"
8314                "      a();\n"
8315                "      b();\n"
8316                "    }\n"
8317                "  }\n"
8318                "  void g() { return; }\n"
8319                "};\n"
8320                "enum E\n"
8321                "{\n"
8322                "  A,\n"
8323                "  // foo\n"
8324                "  B,\n"
8325                "  C\n"
8326                "};\n"
8327                "struct B\n"
8328                "{\n"
8329                "  int x;\n"
8330                "};\n"
8331                "}\n",
8332                MozillaBraceStyle);
8333   verifyFormat("struct S\n"
8334                "{\n"
8335                "  int Type;\n"
8336                "  union\n"
8337                "  {\n"
8338                "    int x;\n"
8339                "    double y;\n"
8340                "  } Value;\n"
8341                "  class C\n"
8342                "  {\n"
8343                "    MyFavoriteType Value;\n"
8344                "  } Class;\n"
8345                "}\n",
8346                MozillaBraceStyle);
8347 }
8348 
8349 TEST_F(FormatTest, StroustrupBraceBreaking) {
8350   FormatStyle StroustrupBraceStyle = getLLVMStyle();
8351   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8352   verifyFormat("namespace a {\n"
8353                "class A {\n"
8354                "  void f()\n"
8355                "  {\n"
8356                "    if (true) {\n"
8357                "      a();\n"
8358                "      b();\n"
8359                "    }\n"
8360                "  }\n"
8361                "  void g() { return; }\n"
8362                "};\n"
8363                "struct B {\n"
8364                "  int x;\n"
8365                "};\n"
8366                "} // namespace a\n",
8367                StroustrupBraceStyle);
8368 
8369   verifyFormat("void foo()\n"
8370                "{\n"
8371                "  if (a) {\n"
8372                "    a();\n"
8373                "  }\n"
8374                "  else {\n"
8375                "    b();\n"
8376                "  }\n"
8377                "}\n",
8378                StroustrupBraceStyle);
8379 
8380   verifyFormat("#ifdef _DEBUG\n"
8381                "int foo(int i = 0)\n"
8382                "#else\n"
8383                "int foo(int i = 5)\n"
8384                "#endif\n"
8385                "{\n"
8386                "  return i;\n"
8387                "}",
8388                StroustrupBraceStyle);
8389 
8390   verifyFormat("void foo() {}\n"
8391                "void bar()\n"
8392                "#ifdef _DEBUG\n"
8393                "{\n"
8394                "  foo();\n"
8395                "}\n"
8396                "#else\n"
8397                "{\n"
8398                "}\n"
8399                "#endif",
8400                StroustrupBraceStyle);
8401 
8402   verifyFormat("void foobar() { int i = 5; }\n"
8403                "#ifdef _DEBUG\n"
8404                "void bar() {}\n"
8405                "#else\n"
8406                "void bar() { foobar(); }\n"
8407                "#endif",
8408                StroustrupBraceStyle);
8409 }
8410 
8411 TEST_F(FormatTest, AllmanBraceBreaking) {
8412   FormatStyle AllmanBraceStyle = getLLVMStyle();
8413   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
8414   verifyFormat("namespace a\n"
8415                "{\n"
8416                "class A\n"
8417                "{\n"
8418                "  void f()\n"
8419                "  {\n"
8420                "    if (true)\n"
8421                "    {\n"
8422                "      a();\n"
8423                "      b();\n"
8424                "    }\n"
8425                "  }\n"
8426                "  void g() { return; }\n"
8427                "};\n"
8428                "struct B\n"
8429                "{\n"
8430                "  int x;\n"
8431                "};\n"
8432                "}",
8433                AllmanBraceStyle);
8434 
8435   verifyFormat("void f()\n"
8436                "{\n"
8437                "  if (true)\n"
8438                "  {\n"
8439                "    a();\n"
8440                "  }\n"
8441                "  else if (false)\n"
8442                "  {\n"
8443                "    b();\n"
8444                "  }\n"
8445                "  else\n"
8446                "  {\n"
8447                "    c();\n"
8448                "  }\n"
8449                "}\n",
8450                AllmanBraceStyle);
8451 
8452   verifyFormat("void f()\n"
8453                "{\n"
8454                "  for (int i = 0; i < 10; ++i)\n"
8455                "  {\n"
8456                "    a();\n"
8457                "  }\n"
8458                "  while (false)\n"
8459                "  {\n"
8460                "    b();\n"
8461                "  }\n"
8462                "  do\n"
8463                "  {\n"
8464                "    c();\n"
8465                "  } while (false)\n"
8466                "}\n",
8467                AllmanBraceStyle);
8468 
8469   verifyFormat("void f(int a)\n"
8470                "{\n"
8471                "  switch (a)\n"
8472                "  {\n"
8473                "  case 0:\n"
8474                "    break;\n"
8475                "  case 1:\n"
8476                "  {\n"
8477                "    break;\n"
8478                "  }\n"
8479                "  case 2:\n"
8480                "  {\n"
8481                "  }\n"
8482                "  break;\n"
8483                "  default:\n"
8484                "    break;\n"
8485                "  }\n"
8486                "}\n",
8487                AllmanBraceStyle);
8488 
8489   verifyFormat("enum X\n"
8490                "{\n"
8491                "  Y = 0,\n"
8492                "}\n",
8493                AllmanBraceStyle);
8494   verifyFormat("enum X\n"
8495                "{\n"
8496                "  Y = 0\n"
8497                "}\n",
8498                AllmanBraceStyle);
8499 
8500   verifyFormat("@interface BSApplicationController ()\n"
8501                "{\n"
8502                "@private\n"
8503                "  id _extraIvar;\n"
8504                "}\n"
8505                "@end\n",
8506                AllmanBraceStyle);
8507 
8508   verifyFormat("#ifdef _DEBUG\n"
8509                "int foo(int i = 0)\n"
8510                "#else\n"
8511                "int foo(int i = 5)\n"
8512                "#endif\n"
8513                "{\n"
8514                "  return i;\n"
8515                "}",
8516                AllmanBraceStyle);
8517 
8518   verifyFormat("void foo() {}\n"
8519                "void bar()\n"
8520                "#ifdef _DEBUG\n"
8521                "{\n"
8522                "  foo();\n"
8523                "}\n"
8524                "#else\n"
8525                "{\n"
8526                "}\n"
8527                "#endif",
8528                AllmanBraceStyle);
8529 
8530   verifyFormat("void foobar() { int i = 5; }\n"
8531                "#ifdef _DEBUG\n"
8532                "void bar() {}\n"
8533                "#else\n"
8534                "void bar() { foobar(); }\n"
8535                "#endif",
8536                AllmanBraceStyle);
8537 
8538   // This shouldn't affect ObjC blocks..
8539   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
8540                "  // ...\n"
8541                "  int i;\n"
8542                "}];",
8543                AllmanBraceStyle);
8544   verifyFormat("void (^block)(void) = ^{\n"
8545                "  // ...\n"
8546                "  int i;\n"
8547                "};",
8548                AllmanBraceStyle);
8549   // .. or dict literals.
8550   verifyFormat("void f()\n"
8551                "{\n"
8552                "  // ...\n"
8553                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
8554                "}",
8555                AllmanBraceStyle);
8556   verifyFormat("void f()\n"
8557                "{\n"
8558                "  // ...\n"
8559                "  [object someMethod:@{a : @\"b\"}];\n"
8560                "}",
8561                AllmanBraceStyle);
8562   verifyFormat("int f()\n"
8563                "{ // comment\n"
8564                "  return 42;\n"
8565                "}",
8566                AllmanBraceStyle);
8567 
8568   AllmanBraceStyle.ColumnLimit = 19;
8569   verifyFormat("void f() { int i; }", AllmanBraceStyle);
8570   AllmanBraceStyle.ColumnLimit = 18;
8571   verifyFormat("void f()\n"
8572                "{\n"
8573                "  int i;\n"
8574                "}",
8575                AllmanBraceStyle);
8576   AllmanBraceStyle.ColumnLimit = 80;
8577 
8578   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
8579   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
8580   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
8581   verifyFormat("void f(bool b)\n"
8582                "{\n"
8583                "  if (b)\n"
8584                "  {\n"
8585                "    return;\n"
8586                "  }\n"
8587                "}\n",
8588                BreakBeforeBraceShortIfs);
8589   verifyFormat("void f(bool b)\n"
8590                "{\n"
8591                "  if (b) return;\n"
8592                "}\n",
8593                BreakBeforeBraceShortIfs);
8594   verifyFormat("void f(bool b)\n"
8595                "{\n"
8596                "  while (b)\n"
8597                "  {\n"
8598                "    return;\n"
8599                "  }\n"
8600                "}\n",
8601                BreakBeforeBraceShortIfs);
8602 }
8603 
8604 TEST_F(FormatTest, GNUBraceBreaking) {
8605   FormatStyle GNUBraceStyle = getLLVMStyle();
8606   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
8607   verifyFormat("namespace a\n"
8608                "{\n"
8609                "class A\n"
8610                "{\n"
8611                "  void f()\n"
8612                "  {\n"
8613                "    int a;\n"
8614                "    {\n"
8615                "      int b;\n"
8616                "    }\n"
8617                "    if (true)\n"
8618                "      {\n"
8619                "        a();\n"
8620                "        b();\n"
8621                "      }\n"
8622                "  }\n"
8623                "  void g() { return; }\n"
8624                "}\n"
8625                "}",
8626                GNUBraceStyle);
8627 
8628   verifyFormat("void f()\n"
8629                "{\n"
8630                "  if (true)\n"
8631                "    {\n"
8632                "      a();\n"
8633                "    }\n"
8634                "  else if (false)\n"
8635                "    {\n"
8636                "      b();\n"
8637                "    }\n"
8638                "  else\n"
8639                "    {\n"
8640                "      c();\n"
8641                "    }\n"
8642                "}\n",
8643                GNUBraceStyle);
8644 
8645   verifyFormat("void f()\n"
8646                "{\n"
8647                "  for (int i = 0; i < 10; ++i)\n"
8648                "    {\n"
8649                "      a();\n"
8650                "    }\n"
8651                "  while (false)\n"
8652                "    {\n"
8653                "      b();\n"
8654                "    }\n"
8655                "  do\n"
8656                "    {\n"
8657                "      c();\n"
8658                "    }\n"
8659                "  while (false);\n"
8660                "}\n",
8661                GNUBraceStyle);
8662 
8663   verifyFormat("void f(int a)\n"
8664                "{\n"
8665                "  switch (a)\n"
8666                "    {\n"
8667                "    case 0:\n"
8668                "      break;\n"
8669                "    case 1:\n"
8670                "      {\n"
8671                "        break;\n"
8672                "      }\n"
8673                "    case 2:\n"
8674                "      {\n"
8675                "      }\n"
8676                "      break;\n"
8677                "    default:\n"
8678                "      break;\n"
8679                "    }\n"
8680                "}\n",
8681                GNUBraceStyle);
8682 
8683   verifyFormat("enum X\n"
8684                "{\n"
8685                "  Y = 0,\n"
8686                "}\n",
8687                GNUBraceStyle);
8688 
8689   verifyFormat("@interface BSApplicationController ()\n"
8690                "{\n"
8691                "@private\n"
8692                "  id _extraIvar;\n"
8693                "}\n"
8694                "@end\n",
8695                GNUBraceStyle);
8696 
8697   verifyFormat("#ifdef _DEBUG\n"
8698                "int foo(int i = 0)\n"
8699                "#else\n"
8700                "int foo(int i = 5)\n"
8701                "#endif\n"
8702                "{\n"
8703                "  return i;\n"
8704                "}",
8705                GNUBraceStyle);
8706 
8707   verifyFormat("void foo() {}\n"
8708                "void bar()\n"
8709                "#ifdef _DEBUG\n"
8710                "{\n"
8711                "  foo();\n"
8712                "}\n"
8713                "#else\n"
8714                "{\n"
8715                "}\n"
8716                "#endif",
8717                GNUBraceStyle);
8718 
8719   verifyFormat("void foobar() { int i = 5; }\n"
8720                "#ifdef _DEBUG\n"
8721                "void bar() {}\n"
8722                "#else\n"
8723                "void bar() { foobar(); }\n"
8724                "#endif",
8725                GNUBraceStyle);
8726 }
8727 
8728 TEST_F(FormatTest, WebKitBraceBreaking) {
8729   FormatStyle WebKitBraceStyle = getLLVMStyle();
8730   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
8731   WebKitBraceStyle.FixNamespaceComments = false;
8732   verifyFormat("namespace a {\n"
8733                "class A {\n"
8734                "  void f()\n"
8735                "  {\n"
8736                "    if (true) {\n"
8737                "      a();\n"
8738                "      b();\n"
8739                "    }\n"
8740                "  }\n"
8741                "  void g() { return; }\n"
8742                "};\n"
8743                "enum E {\n"
8744                "  A,\n"
8745                "  // foo\n"
8746                "  B,\n"
8747                "  C\n"
8748                "};\n"
8749                "struct B {\n"
8750                "  int x;\n"
8751                "};\n"
8752                "}\n",
8753                WebKitBraceStyle);
8754   verifyFormat("struct S {\n"
8755                "  int Type;\n"
8756                "  union {\n"
8757                "    int x;\n"
8758                "    double y;\n"
8759                "  } Value;\n"
8760                "  class C {\n"
8761                "    MyFavoriteType Value;\n"
8762                "  } Class;\n"
8763                "};\n",
8764                WebKitBraceStyle);
8765 }
8766 
8767 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
8768   verifyFormat("void f() {\n"
8769                "  try {\n"
8770                "  } catch (const Exception &e) {\n"
8771                "  }\n"
8772                "}\n",
8773                getLLVMStyle());
8774 }
8775 
8776 TEST_F(FormatTest, UnderstandsPragmas) {
8777   verifyFormat("#pragma omp reduction(| : var)");
8778   verifyFormat("#pragma omp reduction(+ : var)");
8779 
8780   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
8781             "(including parentheses).",
8782             format("#pragma    mark   Any non-hyphenated or hyphenated string "
8783                    "(including parentheses)."));
8784 }
8785 
8786 TEST_F(FormatTest, UnderstandPragmaOption) {
8787   verifyFormat("#pragma option -C -A");
8788 
8789   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
8790 }
8791 
8792 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
8793   for (size_t i = 1; i < Styles.size(); ++i)                                   \
8794   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
8795                                   << " differs from Style #0"
8796 
8797 TEST_F(FormatTest, GetsPredefinedStyleByName) {
8798   SmallVector<FormatStyle, 3> Styles;
8799   Styles.resize(3);
8800 
8801   Styles[0] = getLLVMStyle();
8802   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
8803   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
8804   EXPECT_ALL_STYLES_EQUAL(Styles);
8805 
8806   Styles[0] = getGoogleStyle();
8807   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
8808   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
8809   EXPECT_ALL_STYLES_EQUAL(Styles);
8810 
8811   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
8812   EXPECT_TRUE(
8813       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
8814   EXPECT_TRUE(
8815       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
8816   EXPECT_ALL_STYLES_EQUAL(Styles);
8817 
8818   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
8819   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
8820   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
8821   EXPECT_ALL_STYLES_EQUAL(Styles);
8822 
8823   Styles[0] = getMozillaStyle();
8824   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
8825   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
8826   EXPECT_ALL_STYLES_EQUAL(Styles);
8827 
8828   Styles[0] = getWebKitStyle();
8829   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
8830   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
8831   EXPECT_ALL_STYLES_EQUAL(Styles);
8832 
8833   Styles[0] = getGNUStyle();
8834   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
8835   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
8836   EXPECT_ALL_STYLES_EQUAL(Styles);
8837 
8838   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
8839 }
8840 
8841 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
8842   SmallVector<FormatStyle, 8> Styles;
8843   Styles.resize(2);
8844 
8845   Styles[0] = getGoogleStyle();
8846   Styles[1] = getLLVMStyle();
8847   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
8848   EXPECT_ALL_STYLES_EQUAL(Styles);
8849 
8850   Styles.resize(5);
8851   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
8852   Styles[1] = getLLVMStyle();
8853   Styles[1].Language = FormatStyle::LK_JavaScript;
8854   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
8855 
8856   Styles[2] = getLLVMStyle();
8857   Styles[2].Language = FormatStyle::LK_JavaScript;
8858   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
8859                                   "BasedOnStyle: Google",
8860                                   &Styles[2])
8861                    .value());
8862 
8863   Styles[3] = getLLVMStyle();
8864   Styles[3].Language = FormatStyle::LK_JavaScript;
8865   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
8866                                   "Language: JavaScript",
8867                                   &Styles[3])
8868                    .value());
8869 
8870   Styles[4] = getLLVMStyle();
8871   Styles[4].Language = FormatStyle::LK_JavaScript;
8872   EXPECT_EQ(0, parseConfiguration("---\n"
8873                                   "BasedOnStyle: LLVM\n"
8874                                   "IndentWidth: 123\n"
8875                                   "---\n"
8876                                   "BasedOnStyle: Google\n"
8877                                   "Language: JavaScript",
8878                                   &Styles[4])
8879                    .value());
8880   EXPECT_ALL_STYLES_EQUAL(Styles);
8881 }
8882 
8883 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
8884   Style.FIELD = false;                                                         \
8885   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
8886   EXPECT_TRUE(Style.FIELD);                                                    \
8887   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
8888   EXPECT_FALSE(Style.FIELD);
8889 
8890 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
8891 
8892 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
8893   Style.STRUCT.FIELD = false;                                                  \
8894   EXPECT_EQ(0,                                                                 \
8895             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
8896                 .value());                                                     \
8897   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
8898   EXPECT_EQ(0,                                                                 \
8899             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
8900                 .value());                                                     \
8901   EXPECT_FALSE(Style.STRUCT.FIELD);
8902 
8903 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
8904   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
8905 
8906 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
8907   EXPECT_NE(VALUE, Style.FIELD);                                               \
8908   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
8909   EXPECT_EQ(VALUE, Style.FIELD)
8910 
8911 TEST_F(FormatTest, ParsesConfigurationBools) {
8912   FormatStyle Style = {};
8913   Style.Language = FormatStyle::LK_Cpp;
8914   CHECK_PARSE_BOOL(AlignOperands);
8915   CHECK_PARSE_BOOL(AlignTrailingComments);
8916   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
8917   CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
8918   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
8919   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
8920   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
8921   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
8922   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
8923   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
8924   CHECK_PARSE_BOOL(BinPackArguments);
8925   CHECK_PARSE_BOOL(BinPackParameters);
8926   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
8927   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
8928   CHECK_PARSE_BOOL(BreakStringLiterals);
8929   CHECK_PARSE_BOOL(BreakBeforeInheritanceComma)
8930   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
8931   CHECK_PARSE_BOOL(DerivePointerAlignment);
8932   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
8933   CHECK_PARSE_BOOL(DisableFormat);
8934   CHECK_PARSE_BOOL(IndentCaseLabels);
8935   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
8936   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
8937   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
8938   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
8939   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
8940   CHECK_PARSE_BOOL(ReflowComments);
8941   CHECK_PARSE_BOOL(SortIncludes);
8942   CHECK_PARSE_BOOL(SpacesInParentheses);
8943   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
8944   CHECK_PARSE_BOOL(SpacesInAngles);
8945   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
8946   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
8947   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
8948   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
8949   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
8950   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
8951 
8952   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
8953   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
8954   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
8955   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
8956   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
8957   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
8958   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
8959   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
8960   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
8961   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
8962   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
8963 }
8964 
8965 #undef CHECK_PARSE_BOOL
8966 
8967 TEST_F(FormatTest, ParsesConfiguration) {
8968   FormatStyle Style = {};
8969   Style.Language = FormatStyle::LK_Cpp;
8970   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
8971   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
8972               ConstructorInitializerIndentWidth, 1234u);
8973   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
8974   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
8975   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
8976   CHECK_PARSE("PenaltyBreakAssignment: 1234",
8977               PenaltyBreakAssignment, 1234u);
8978   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
8979               PenaltyBreakBeforeFirstCallParameter, 1234u);
8980   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
8981   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
8982               PenaltyReturnTypeOnItsOwnLine, 1234u);
8983   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
8984               SpacesBeforeTrailingComments, 1234u);
8985   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
8986   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
8987   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
8988 
8989   Style.PointerAlignment = FormatStyle::PAS_Middle;
8990   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
8991               FormatStyle::PAS_Left);
8992   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
8993               FormatStyle::PAS_Right);
8994   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
8995               FormatStyle::PAS_Middle);
8996   // For backward compatibility:
8997   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
8998               FormatStyle::PAS_Left);
8999   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
9000               FormatStyle::PAS_Right);
9001   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
9002               FormatStyle::PAS_Middle);
9003 
9004   Style.Standard = FormatStyle::LS_Auto;
9005   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
9006   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
9007   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
9008   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
9009   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
9010 
9011   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9012   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
9013               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
9014   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
9015               FormatStyle::BOS_None);
9016   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
9017               FormatStyle::BOS_All);
9018   // For backward compatibility:
9019   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
9020               FormatStyle::BOS_None);
9021   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
9022               FormatStyle::BOS_All);
9023 
9024   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
9025   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
9026               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
9027   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
9028               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
9029   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
9030               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
9031   // For backward compatibility:
9032   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
9033               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
9034 
9035   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9036   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
9037               FormatStyle::BAS_Align);
9038   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
9039               FormatStyle::BAS_DontAlign);
9040   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
9041               FormatStyle::BAS_AlwaysBreak);
9042   // For backward compatibility:
9043   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
9044               FormatStyle::BAS_DontAlign);
9045   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
9046               FormatStyle::BAS_Align);
9047 
9048   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
9049   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
9050               FormatStyle::ENAS_DontAlign);
9051   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
9052               FormatStyle::ENAS_Left);
9053   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
9054               FormatStyle::ENAS_Right);
9055   // For backward compatibility:
9056   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
9057               FormatStyle::ENAS_Left);
9058   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
9059               FormatStyle::ENAS_Right);
9060 
9061   Style.UseTab = FormatStyle::UT_ForIndentation;
9062   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
9063   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
9064   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
9065   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
9066               FormatStyle::UT_ForContinuationAndIndentation);
9067   // For backward compatibility:
9068   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
9069   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
9070 
9071   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
9072   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
9073               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9074   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
9075               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
9076   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
9077               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
9078   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
9079               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9080   // For backward compatibility:
9081   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
9082               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9083   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
9084               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9085 
9086   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
9087   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
9088               FormatStyle::SBPO_Never);
9089   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
9090               FormatStyle::SBPO_Always);
9091   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
9092               FormatStyle::SBPO_ControlStatements);
9093   // For backward compatibility:
9094   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
9095               FormatStyle::SBPO_Never);
9096   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
9097               FormatStyle::SBPO_ControlStatements);
9098 
9099   Style.ColumnLimit = 123;
9100   FormatStyle BaseStyle = getLLVMStyle();
9101   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
9102   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
9103 
9104   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9105   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
9106               FormatStyle::BS_Attach);
9107   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
9108               FormatStyle::BS_Linux);
9109   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
9110               FormatStyle::BS_Mozilla);
9111   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
9112               FormatStyle::BS_Stroustrup);
9113   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
9114               FormatStyle::BS_Allman);
9115   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
9116   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
9117               FormatStyle::BS_WebKit);
9118   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
9119               FormatStyle::BS_Custom);
9120 
9121   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
9122   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
9123               FormatStyle::RTBS_None);
9124   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
9125               FormatStyle::RTBS_All);
9126   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
9127               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
9128   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
9129               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
9130   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
9131               AlwaysBreakAfterReturnType,
9132               FormatStyle::RTBS_TopLevelDefinitions);
9133 
9134   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
9135   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
9136               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
9137   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
9138               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
9139   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
9140               AlwaysBreakAfterDefinitionReturnType,
9141               FormatStyle::DRTBS_TopLevel);
9142 
9143   Style.NamespaceIndentation = FormatStyle::NI_All;
9144   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
9145               FormatStyle::NI_None);
9146   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
9147               FormatStyle::NI_Inner);
9148   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
9149               FormatStyle::NI_All);
9150 
9151   // FIXME: This is required because parsing a configuration simply overwrites
9152   // the first N elements of the list instead of resetting it.
9153   Style.ForEachMacros.clear();
9154   std::vector<std::string> BoostForeach;
9155   BoostForeach.push_back("BOOST_FOREACH");
9156   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
9157   std::vector<std::string> BoostAndQForeach;
9158   BoostAndQForeach.push_back("BOOST_FOREACH");
9159   BoostAndQForeach.push_back("Q_FOREACH");
9160   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
9161               BoostAndQForeach);
9162 
9163   Style.IncludeCategories.clear();
9164   std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
9165                                                                   {".*", 1}};
9166   CHECK_PARSE("IncludeCategories:\n"
9167               "  - Regex: abc/.*\n"
9168               "    Priority: 2\n"
9169               "  - Regex: .*\n"
9170               "    Priority: 1",
9171               IncludeCategories, ExpectedCategories);
9172   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$");
9173 }
9174 
9175 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
9176   FormatStyle Style = {};
9177   Style.Language = FormatStyle::LK_Cpp;
9178   CHECK_PARSE("Language: Cpp\n"
9179               "IndentWidth: 12",
9180               IndentWidth, 12u);
9181   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
9182                                "IndentWidth: 34",
9183                                &Style),
9184             ParseError::Unsuitable);
9185   EXPECT_EQ(12u, Style.IndentWidth);
9186   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
9187   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9188 
9189   Style.Language = FormatStyle::LK_JavaScript;
9190   CHECK_PARSE("Language: JavaScript\n"
9191               "IndentWidth: 12",
9192               IndentWidth, 12u);
9193   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
9194   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
9195                                "IndentWidth: 34",
9196                                &Style),
9197             ParseError::Unsuitable);
9198   EXPECT_EQ(23u, Style.IndentWidth);
9199   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
9200   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9201 
9202   CHECK_PARSE("BasedOnStyle: LLVM\n"
9203               "IndentWidth: 67",
9204               IndentWidth, 67u);
9205 
9206   CHECK_PARSE("---\n"
9207               "Language: JavaScript\n"
9208               "IndentWidth: 12\n"
9209               "---\n"
9210               "Language: Cpp\n"
9211               "IndentWidth: 34\n"
9212               "...\n",
9213               IndentWidth, 12u);
9214 
9215   Style.Language = FormatStyle::LK_Cpp;
9216   CHECK_PARSE("---\n"
9217               "Language: JavaScript\n"
9218               "IndentWidth: 12\n"
9219               "---\n"
9220               "Language: Cpp\n"
9221               "IndentWidth: 34\n"
9222               "...\n",
9223               IndentWidth, 34u);
9224   CHECK_PARSE("---\n"
9225               "IndentWidth: 78\n"
9226               "---\n"
9227               "Language: JavaScript\n"
9228               "IndentWidth: 56\n"
9229               "...\n",
9230               IndentWidth, 78u);
9231 
9232   Style.ColumnLimit = 123;
9233   Style.IndentWidth = 234;
9234   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
9235   Style.TabWidth = 345;
9236   EXPECT_FALSE(parseConfiguration("---\n"
9237                                   "IndentWidth: 456\n"
9238                                   "BreakBeforeBraces: Allman\n"
9239                                   "---\n"
9240                                   "Language: JavaScript\n"
9241                                   "IndentWidth: 111\n"
9242                                   "TabWidth: 111\n"
9243                                   "---\n"
9244                                   "Language: Cpp\n"
9245                                   "BreakBeforeBraces: Stroustrup\n"
9246                                   "TabWidth: 789\n"
9247                                   "...\n",
9248                                   &Style));
9249   EXPECT_EQ(123u, Style.ColumnLimit);
9250   EXPECT_EQ(456u, Style.IndentWidth);
9251   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
9252   EXPECT_EQ(789u, Style.TabWidth);
9253 
9254   EXPECT_EQ(parseConfiguration("---\n"
9255                                "Language: JavaScript\n"
9256                                "IndentWidth: 56\n"
9257                                "---\n"
9258                                "IndentWidth: 78\n"
9259                                "...\n",
9260                                &Style),
9261             ParseError::Error);
9262   EXPECT_EQ(parseConfiguration("---\n"
9263                                "Language: JavaScript\n"
9264                                "IndentWidth: 56\n"
9265                                "---\n"
9266                                "Language: JavaScript\n"
9267                                "IndentWidth: 78\n"
9268                                "...\n",
9269                                &Style),
9270             ParseError::Error);
9271 
9272   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9273 }
9274 
9275 #undef CHECK_PARSE
9276 
9277 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
9278   FormatStyle Style = {};
9279   Style.Language = FormatStyle::LK_JavaScript;
9280   Style.BreakBeforeTernaryOperators = true;
9281   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
9282   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9283 
9284   Style.BreakBeforeTernaryOperators = true;
9285   EXPECT_EQ(0, parseConfiguration("---\n"
9286                                   "BasedOnStyle: Google\n"
9287                                   "---\n"
9288                                   "Language: JavaScript\n"
9289                                   "IndentWidth: 76\n"
9290                                   "...\n",
9291                                   &Style)
9292                    .value());
9293   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9294   EXPECT_EQ(76u, Style.IndentWidth);
9295   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9296 }
9297 
9298 TEST_F(FormatTest, ConfigurationRoundTripTest) {
9299   FormatStyle Style = getLLVMStyle();
9300   std::string YAML = configurationAsText(Style);
9301   FormatStyle ParsedStyle = {};
9302   ParsedStyle.Language = FormatStyle::LK_Cpp;
9303   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
9304   EXPECT_EQ(Style, ParsedStyle);
9305 }
9306 
9307 TEST_F(FormatTest, WorksFor8bitEncodings) {
9308   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
9309             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
9310             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
9311             "\"\xef\xee\xf0\xf3...\"",
9312             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
9313                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
9314                    "\xef\xee\xf0\xf3...\"",
9315                    getLLVMStyleWithColumns(12)));
9316 }
9317 
9318 TEST_F(FormatTest, HandlesUTF8BOM) {
9319   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
9320   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
9321             format("\xef\xbb\xbf#include <iostream>"));
9322   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
9323             format("\xef\xbb\xbf\n#include <iostream>"));
9324 }
9325 
9326 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
9327 #if !defined(_MSC_VER)
9328 
9329 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
9330   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
9331                getLLVMStyleWithColumns(35));
9332   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
9333                getLLVMStyleWithColumns(31));
9334   verifyFormat("// Однажды в студёную зимнюю пору...",
9335                getLLVMStyleWithColumns(36));
9336   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
9337   verifyFormat("/* Однажды в студёную зимнюю пору... */",
9338                getLLVMStyleWithColumns(39));
9339   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
9340                getLLVMStyleWithColumns(35));
9341 }
9342 
9343 TEST_F(FormatTest, SplitsUTF8Strings) {
9344   // Non-printable characters' width is currently considered to be the length in
9345   // bytes in UTF8. The characters can be displayed in very different manner
9346   // (zero-width, single width with a substitution glyph, expanded to their code
9347   // (e.g. "<8d>"), so there's no single correct way to handle them.
9348   EXPECT_EQ("\"aaaaÄ\"\n"
9349             "\"\xc2\x8d\";",
9350             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9351   EXPECT_EQ("\"aaaaaaaÄ\"\n"
9352             "\"\xc2\x8d\";",
9353             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9354   EXPECT_EQ("\"Однажды, в \"\n"
9355             "\"студёную \"\n"
9356             "\"зимнюю \"\n"
9357             "\"пору,\"",
9358             format("\"Однажды, в студёную зимнюю пору,\"",
9359                    getLLVMStyleWithColumns(13)));
9360   EXPECT_EQ(
9361       "\"一 二 三 \"\n"
9362       "\"四 五六 \"\n"
9363       "\"七 八 九 \"\n"
9364       "\"十\"",
9365       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
9366   EXPECT_EQ("\"一\t二 \"\n"
9367             "\"\t三 \"\n"
9368             "\"四 五\t六 \"\n"
9369             "\"\t七 \"\n"
9370             "\"八九十\tqq\"",
9371             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
9372                    getLLVMStyleWithColumns(11)));
9373 
9374   // UTF8 character in an escape sequence.
9375   EXPECT_EQ("\"aaaaaa\"\n"
9376             "\"\\\xC2\x8D\"",
9377             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
9378 }
9379 
9380 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
9381   EXPECT_EQ("const char *sssss =\n"
9382             "    \"一二三四五六七八\\\n"
9383             " 九 十\";",
9384             format("const char *sssss = \"一二三四五六七八\\\n"
9385                    " 九 十\";",
9386                    getLLVMStyleWithColumns(30)));
9387 }
9388 
9389 TEST_F(FormatTest, SplitsUTF8LineComments) {
9390   EXPECT_EQ("// aaaaÄ\xc2\x8d",
9391             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
9392   EXPECT_EQ("// Я из лесу\n"
9393             "// вышел; был\n"
9394             "// сильный\n"
9395             "// мороз.",
9396             format("// Я из лесу вышел; был сильный мороз.",
9397                    getLLVMStyleWithColumns(13)));
9398   EXPECT_EQ("// 一二三\n"
9399             "// 四五六七\n"
9400             "// 八  九\n"
9401             "// 十",
9402             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
9403 }
9404 
9405 TEST_F(FormatTest, SplitsUTF8BlockComments) {
9406   EXPECT_EQ("/* Гляжу,\n"
9407             " * поднимается\n"
9408             " * медленно в\n"
9409             " * гору\n"
9410             " * Лошадка,\n"
9411             " * везущая\n"
9412             " * хворосту\n"
9413             " * воз. */",
9414             format("/* Гляжу, поднимается медленно в гору\n"
9415                    " * Лошадка, везущая хворосту воз. */",
9416                    getLLVMStyleWithColumns(13)));
9417   EXPECT_EQ(
9418       "/* 一二三\n"
9419       " * 四五六七\n"
9420       " * 八  九\n"
9421       " * 十  */",
9422       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
9423   EXPECT_EQ("/* �������� ��������\n"
9424             " * ��������\n"
9425             " * ������-�� */",
9426             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
9427 }
9428 
9429 #endif // _MSC_VER
9430 
9431 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
9432   FormatStyle Style = getLLVMStyle();
9433 
9434   Style.ConstructorInitializerIndentWidth = 4;
9435   verifyFormat(
9436       "SomeClass::Constructor()\n"
9437       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9438       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9439       Style);
9440 
9441   Style.ConstructorInitializerIndentWidth = 2;
9442   verifyFormat(
9443       "SomeClass::Constructor()\n"
9444       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9445       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9446       Style);
9447 
9448   Style.ConstructorInitializerIndentWidth = 0;
9449   verifyFormat(
9450       "SomeClass::Constructor()\n"
9451       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9452       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9453       Style);
9454   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9455   verifyFormat(
9456       "SomeLongTemplateVariableName<\n"
9457       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
9458       Style);
9459   verifyFormat(
9460       "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
9461       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9462       Style);
9463 }
9464 
9465 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
9466   FormatStyle Style = getLLVMStyle();
9467   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
9468   Style.ConstructorInitializerIndentWidth = 4;
9469   verifyFormat("SomeClass::Constructor()\n"
9470                "    : a(a)\n"
9471                "    , b(b)\n"
9472                "    , c(c) {}",
9473                Style);
9474   verifyFormat("SomeClass::Constructor()\n"
9475                "    : a(a) {}",
9476                Style);
9477 
9478   Style.ColumnLimit = 0;
9479   verifyFormat("SomeClass::Constructor()\n"
9480                "    : a(a) {}",
9481                Style);
9482   verifyFormat("SomeClass::Constructor() noexcept\n"
9483                "    : a(a) {}",
9484                Style);
9485   verifyFormat("SomeClass::Constructor()\n"
9486                "    : a(a)\n"
9487                "    , b(b)\n"
9488                "    , c(c) {}",
9489                Style);
9490   verifyFormat("SomeClass::Constructor()\n"
9491                "    : a(a) {\n"
9492                "  foo();\n"
9493                "  bar();\n"
9494                "}",
9495                Style);
9496 
9497   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9498   verifyFormat("SomeClass::Constructor()\n"
9499                "    : a(a)\n"
9500                "    , b(b)\n"
9501                "    , c(c) {\n}",
9502                Style);
9503   verifyFormat("SomeClass::Constructor()\n"
9504                "    : a(a) {\n}",
9505                Style);
9506 
9507   Style.ColumnLimit = 80;
9508   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
9509   Style.ConstructorInitializerIndentWidth = 2;
9510   verifyFormat("SomeClass::Constructor()\n"
9511                "  : a(a)\n"
9512                "  , b(b)\n"
9513                "  , c(c) {}",
9514                Style);
9515 
9516   Style.ConstructorInitializerIndentWidth = 0;
9517   verifyFormat("SomeClass::Constructor()\n"
9518                ": a(a)\n"
9519                ", b(b)\n"
9520                ", c(c) {}",
9521                Style);
9522 
9523   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
9524   Style.ConstructorInitializerIndentWidth = 4;
9525   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
9526   verifyFormat(
9527       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
9528       Style);
9529   verifyFormat(
9530       "SomeClass::Constructor()\n"
9531       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
9532       Style);
9533   Style.ConstructorInitializerIndentWidth = 4;
9534   Style.ColumnLimit = 60;
9535   verifyFormat("SomeClass::Constructor()\n"
9536                "    : aaaaaaaa(aaaaaaaa)\n"
9537                "    , aaaaaaaa(aaaaaaaa)\n"
9538                "    , aaaaaaaa(aaaaaaaa) {}",
9539                Style);
9540 }
9541 
9542 TEST_F(FormatTest, Destructors) {
9543   verifyFormat("void F(int &i) { i.~int(); }");
9544   verifyFormat("void F(int &i) { i->~int(); }");
9545 }
9546 
9547 TEST_F(FormatTest, FormatsWithWebKitStyle) {
9548   FormatStyle Style = getWebKitStyle();
9549 
9550   // Don't indent in outer namespaces.
9551   verifyFormat("namespace outer {\n"
9552                "int i;\n"
9553                "namespace inner {\n"
9554                "    int i;\n"
9555                "} // namespace inner\n"
9556                "} // namespace outer\n"
9557                "namespace other_outer {\n"
9558                "int i;\n"
9559                "}",
9560                Style);
9561 
9562   // Don't indent case labels.
9563   verifyFormat("switch (variable) {\n"
9564                "case 1:\n"
9565                "case 2:\n"
9566                "    doSomething();\n"
9567                "    break;\n"
9568                "default:\n"
9569                "    ++variable;\n"
9570                "}",
9571                Style);
9572 
9573   // Wrap before binary operators.
9574   EXPECT_EQ("void f()\n"
9575             "{\n"
9576             "    if (aaaaaaaaaaaaaaaa\n"
9577             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
9578             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
9579             "        return;\n"
9580             "}",
9581             format("void f() {\n"
9582                    "if (aaaaaaaaaaaaaaaa\n"
9583                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
9584                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
9585                    "return;\n"
9586                    "}",
9587                    Style));
9588 
9589   // Allow functions on a single line.
9590   verifyFormat("void f() { return; }", Style);
9591 
9592   // Constructor initializers are formatted one per line with the "," on the
9593   // new line.
9594   verifyFormat("Constructor()\n"
9595                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9596                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
9597                "          aaaaaaaaaaaaaa)\n"
9598                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
9599                "{\n"
9600                "}",
9601                Style);
9602   verifyFormat("SomeClass::Constructor()\n"
9603                "    : a(a)\n"
9604                "{\n"
9605                "}",
9606                Style);
9607   EXPECT_EQ("SomeClass::Constructor()\n"
9608             "    : a(a)\n"
9609             "{\n"
9610             "}",
9611             format("SomeClass::Constructor():a(a){}", Style));
9612   verifyFormat("SomeClass::Constructor()\n"
9613                "    : a(a)\n"
9614                "    , b(b)\n"
9615                "    , c(c)\n"
9616                "{\n"
9617                "}",
9618                Style);
9619   verifyFormat("SomeClass::Constructor()\n"
9620                "    : a(a)\n"
9621                "{\n"
9622                "    foo();\n"
9623                "    bar();\n"
9624                "}",
9625                Style);
9626 
9627   // Access specifiers should be aligned left.
9628   verifyFormat("class C {\n"
9629                "public:\n"
9630                "    int i;\n"
9631                "};",
9632                Style);
9633 
9634   // Do not align comments.
9635   verifyFormat("int a; // Do not\n"
9636                "double b; // align comments.",
9637                Style);
9638 
9639   // Do not align operands.
9640   EXPECT_EQ("ASSERT(aaaa\n"
9641             "    || bbbb);",
9642             format("ASSERT ( aaaa\n||bbbb);", Style));
9643 
9644   // Accept input's line breaks.
9645   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
9646             "    || bbbbbbbbbbbbbbb) {\n"
9647             "    i++;\n"
9648             "}",
9649             format("if (aaaaaaaaaaaaaaa\n"
9650                    "|| bbbbbbbbbbbbbbb) { i++; }",
9651                    Style));
9652   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
9653             "    i++;\n"
9654             "}",
9655             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
9656 
9657   // Don't automatically break all macro definitions (llvm.org/PR17842).
9658   verifyFormat("#define aNumber 10", Style);
9659   // However, generally keep the line breaks that the user authored.
9660   EXPECT_EQ("#define aNumber \\\n"
9661             "    10",
9662             format("#define aNumber \\\n"
9663                    " 10",
9664                    Style));
9665 
9666   // Keep empty and one-element array literals on a single line.
9667   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
9668             "                                  copyItems:YES];",
9669             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
9670                    "copyItems:YES];",
9671                    Style));
9672   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
9673             "                                  copyItems:YES];",
9674             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
9675                    "             copyItems:YES];",
9676                    Style));
9677   // FIXME: This does not seem right, there should be more indentation before
9678   // the array literal's entries. Nested blocks have the same problem.
9679   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
9680             "    @\"a\",\n"
9681             "    @\"a\"\n"
9682             "]\n"
9683             "                                  copyItems:YES];",
9684             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
9685                    "     @\"a\",\n"
9686                    "     @\"a\"\n"
9687                    "     ]\n"
9688                    "       copyItems:YES];",
9689                    Style));
9690   EXPECT_EQ(
9691       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
9692       "                                  copyItems:YES];",
9693       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
9694              "   copyItems:YES];",
9695              Style));
9696 
9697   verifyFormat("[self.a b:c c:d];", Style);
9698   EXPECT_EQ("[self.a b:c\n"
9699             "        c:d];",
9700             format("[self.a b:c\n"
9701                    "c:d];",
9702                    Style));
9703 }
9704 
9705 TEST_F(FormatTest, FormatsLambdas) {
9706   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
9707   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
9708   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
9709   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
9710   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
9711   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
9712   verifyFormat("int x = f(*+[] {});");
9713   verifyFormat("void f() {\n"
9714                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
9715                "}\n");
9716   verifyFormat("void f() {\n"
9717                "  other(x.begin(), //\n"
9718                "        x.end(),   //\n"
9719                "        [&](int, int) { return 1; });\n"
9720                "}\n");
9721   verifyFormat("SomeFunction([]() { // A cool function...\n"
9722                "  return 43;\n"
9723                "});");
9724   EXPECT_EQ("SomeFunction([]() {\n"
9725             "#define A a\n"
9726             "  return 43;\n"
9727             "});",
9728             format("SomeFunction([](){\n"
9729                    "#define A a\n"
9730                    "return 43;\n"
9731                    "});"));
9732   verifyFormat("void f() {\n"
9733                "  SomeFunction([](decltype(x), A *a) {});\n"
9734                "}");
9735   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9736                "    [](const aaaaaaaaaa &a) { return a; });");
9737   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
9738                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
9739                "});");
9740   verifyFormat("Constructor()\n"
9741                "    : Field([] { // comment\n"
9742                "        int i;\n"
9743                "      }) {}");
9744   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
9745                "  return some_parameter.size();\n"
9746                "};");
9747   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
9748                "    [](const string &s) { return s; };");
9749   verifyFormat("int i = aaaaaa ? 1 //\n"
9750                "               : [] {\n"
9751                "                   return 2; //\n"
9752                "                 }();");
9753   verifyFormat("llvm::errs() << \"number of twos is \"\n"
9754                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
9755                "                  return x == 2; // force break\n"
9756                "                });");
9757   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9758                "    [=](int iiiiiiiiiiii) {\n"
9759                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
9760                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
9761                "    });",
9762                getLLVMStyleWithColumns(60));
9763   verifyFormat("SomeFunction({[&] {\n"
9764                "                // comment\n"
9765                "              },\n"
9766                "              [&] {\n"
9767                "                // comment\n"
9768                "              }});");
9769   verifyFormat("SomeFunction({[&] {\n"
9770                "  // comment\n"
9771                "}});");
9772   verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
9773                "                             [&]() { return true; },\n"
9774                "                         aaaaa aaaaaaaaa);");
9775 
9776   // Lambdas with return types.
9777   verifyFormat("int c = []() -> int { return 2; }();\n");
9778   verifyFormat("int c = []() -> int * { return 2; }();\n");
9779   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
9780   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
9781   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
9782   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
9783   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
9784   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
9785   verifyFormat("[a, a]() -> a<1> {};");
9786   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
9787                "                   int j) -> int {\n"
9788                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
9789                "};");
9790   verifyFormat(
9791       "aaaaaaaaaaaaaaaaaaaaaa(\n"
9792       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
9793       "      return aaaaaaaaaaaaaaaaa;\n"
9794       "    });",
9795       getLLVMStyleWithColumns(70));
9796   verifyFormat("[]() //\n"
9797                "    -> int {\n"
9798                "  return 1; //\n"
9799                "};");
9800 
9801   // Multiple lambdas in the same parentheses change indentation rules.
9802   verifyFormat("SomeFunction(\n"
9803                "    []() {\n"
9804                "      int i = 42;\n"
9805                "      return i;\n"
9806                "    },\n"
9807                "    []() {\n"
9808                "      int j = 43;\n"
9809                "      return j;\n"
9810                "    });");
9811 
9812   // More complex introducers.
9813   verifyFormat("return [i, args...] {};");
9814 
9815   // Not lambdas.
9816   verifyFormat("constexpr char hello[]{\"hello\"};");
9817   verifyFormat("double &operator[](int i) { return 0; }\n"
9818                "int i;");
9819   verifyFormat("std::unique_ptr<int[]> foo() {}");
9820   verifyFormat("int i = a[a][a]->f();");
9821   verifyFormat("int i = (*b)[a]->f();");
9822 
9823   // Other corner cases.
9824   verifyFormat("void f() {\n"
9825                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
9826                "  );\n"
9827                "}");
9828 
9829   // Lambdas created through weird macros.
9830   verifyFormat("void f() {\n"
9831                "  MACRO((const AA &a) { return 1; });\n"
9832                "  MACRO((AA &a) { return 1; });\n"
9833                "}");
9834 
9835   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
9836                "      doo_dah();\n"
9837                "      doo_dah();\n"
9838                "    })) {\n"
9839                "}");
9840   verifyFormat("auto lambda = []() {\n"
9841                "  int a = 2\n"
9842                "#if A\n"
9843                "          + 2\n"
9844                "#endif\n"
9845                "      ;\n"
9846                "};");
9847 
9848   // Lambdas with complex multiline introducers.
9849   verifyFormat(
9850       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9851       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
9852       "        -> ::std::unordered_set<\n"
9853       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
9854       "      //\n"
9855       "    });");
9856 }
9857 
9858 TEST_F(FormatTest, FormatsBlocks) {
9859   FormatStyle ShortBlocks = getLLVMStyle();
9860   ShortBlocks.AllowShortBlocksOnASingleLine = true;
9861   verifyFormat("int (^Block)(int, int);", ShortBlocks);
9862   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
9863   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
9864   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
9865   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
9866   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
9867 
9868   verifyFormat("foo(^{ bar(); });", ShortBlocks);
9869   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
9870   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
9871 
9872   verifyFormat("[operation setCompletionBlock:^{\n"
9873                "  [self onOperationDone];\n"
9874                "}];");
9875   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
9876                "  [self onOperationDone];\n"
9877                "}]};");
9878   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
9879                "  f();\n"
9880                "}];");
9881   verifyFormat("int a = [operation block:^int(int *i) {\n"
9882                "  return 1;\n"
9883                "}];");
9884   verifyFormat("[myObject doSomethingWith:arg1\n"
9885                "                      aaa:^int(int *a) {\n"
9886                "                        return 1;\n"
9887                "                      }\n"
9888                "                      bbb:f(a * bbbbbbbb)];");
9889 
9890   verifyFormat("[operation setCompletionBlock:^{\n"
9891                "  [self.delegate newDataAvailable];\n"
9892                "}];",
9893                getLLVMStyleWithColumns(60));
9894   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
9895                "  NSString *path = [self sessionFilePath];\n"
9896                "  if (path) {\n"
9897                "    // ...\n"
9898                "  }\n"
9899                "});");
9900   verifyFormat("[[SessionService sharedService]\n"
9901                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
9902                "      if (window) {\n"
9903                "        [self windowDidLoad:window];\n"
9904                "      } else {\n"
9905                "        [self errorLoadingWindow];\n"
9906                "      }\n"
9907                "    }];");
9908   verifyFormat("void (^largeBlock)(void) = ^{\n"
9909                "  // ...\n"
9910                "};\n",
9911                getLLVMStyleWithColumns(40));
9912   verifyFormat("[[SessionService sharedService]\n"
9913                "    loadWindowWithCompletionBlock: //\n"
9914                "        ^(SessionWindow *window) {\n"
9915                "          if (window) {\n"
9916                "            [self windowDidLoad:window];\n"
9917                "          } else {\n"
9918                "            [self errorLoadingWindow];\n"
9919                "          }\n"
9920                "        }];",
9921                getLLVMStyleWithColumns(60));
9922   verifyFormat("[myObject doSomethingWith:arg1\n"
9923                "    firstBlock:^(Foo *a) {\n"
9924                "      // ...\n"
9925                "      int i;\n"
9926                "    }\n"
9927                "    secondBlock:^(Bar *b) {\n"
9928                "      // ...\n"
9929                "      int i;\n"
9930                "    }\n"
9931                "    thirdBlock:^Foo(Bar *b) {\n"
9932                "      // ...\n"
9933                "      int i;\n"
9934                "    }];");
9935   verifyFormat("[myObject doSomethingWith:arg1\n"
9936                "               firstBlock:-1\n"
9937                "              secondBlock:^(Bar *b) {\n"
9938                "                // ...\n"
9939                "                int i;\n"
9940                "              }];");
9941 
9942   verifyFormat("f(^{\n"
9943                "  @autoreleasepool {\n"
9944                "    if (a) {\n"
9945                "      g();\n"
9946                "    }\n"
9947                "  }\n"
9948                "});");
9949   verifyFormat("Block b = ^int *(A *a, B *b) {}");
9950   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
9951                "};");
9952 
9953   FormatStyle FourIndent = getLLVMStyle();
9954   FourIndent.ObjCBlockIndentWidth = 4;
9955   verifyFormat("[operation setCompletionBlock:^{\n"
9956                "    [self onOperationDone];\n"
9957                "}];",
9958                FourIndent);
9959 }
9960 
9961 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
9962   FormatStyle ZeroColumn = getLLVMStyle();
9963   ZeroColumn.ColumnLimit = 0;
9964 
9965   verifyFormat("[[SessionService sharedService] "
9966                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
9967                "  if (window) {\n"
9968                "    [self windowDidLoad:window];\n"
9969                "  } else {\n"
9970                "    [self errorLoadingWindow];\n"
9971                "  }\n"
9972                "}];",
9973                ZeroColumn);
9974   EXPECT_EQ("[[SessionService sharedService]\n"
9975             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
9976             "      if (window) {\n"
9977             "        [self windowDidLoad:window];\n"
9978             "      } else {\n"
9979             "        [self errorLoadingWindow];\n"
9980             "      }\n"
9981             "    }];",
9982             format("[[SessionService sharedService]\n"
9983                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
9984                    "                if (window) {\n"
9985                    "    [self windowDidLoad:window];\n"
9986                    "  } else {\n"
9987                    "    [self errorLoadingWindow];\n"
9988                    "  }\n"
9989                    "}];",
9990                    ZeroColumn));
9991   verifyFormat("[myObject doSomethingWith:arg1\n"
9992                "    firstBlock:^(Foo *a) {\n"
9993                "      // ...\n"
9994                "      int i;\n"
9995                "    }\n"
9996                "    secondBlock:^(Bar *b) {\n"
9997                "      // ...\n"
9998                "      int i;\n"
9999                "    }\n"
10000                "    thirdBlock:^Foo(Bar *b) {\n"
10001                "      // ...\n"
10002                "      int i;\n"
10003                "    }];",
10004                ZeroColumn);
10005   verifyFormat("f(^{\n"
10006                "  @autoreleasepool {\n"
10007                "    if (a) {\n"
10008                "      g();\n"
10009                "    }\n"
10010                "  }\n"
10011                "});",
10012                ZeroColumn);
10013   verifyFormat("void (^largeBlock)(void) = ^{\n"
10014                "  // ...\n"
10015                "};",
10016                ZeroColumn);
10017 
10018   ZeroColumn.AllowShortBlocksOnASingleLine = true;
10019   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
10020             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
10021   ZeroColumn.AllowShortBlocksOnASingleLine = false;
10022   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
10023             "  int i;\n"
10024             "};",
10025             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
10026 }
10027 
10028 TEST_F(FormatTest, SupportsCRLF) {
10029   EXPECT_EQ("int a;\r\n"
10030             "int b;\r\n"
10031             "int c;\r\n",
10032             format("int a;\r\n"
10033                    "  int b;\r\n"
10034                    "    int c;\r\n",
10035                    getLLVMStyle()));
10036   EXPECT_EQ("int a;\r\n"
10037             "int b;\r\n"
10038             "int c;\r\n",
10039             format("int a;\r\n"
10040                    "  int b;\n"
10041                    "    int c;\r\n",
10042                    getLLVMStyle()));
10043   EXPECT_EQ("int a;\n"
10044             "int b;\n"
10045             "int c;\n",
10046             format("int a;\r\n"
10047                    "  int b;\n"
10048                    "    int c;\n",
10049                    getLLVMStyle()));
10050   EXPECT_EQ("\"aaaaaaa \"\r\n"
10051             "\"bbbbbbb\";\r\n",
10052             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
10053   EXPECT_EQ("#define A \\\r\n"
10054             "  b;      \\\r\n"
10055             "  c;      \\\r\n"
10056             "  d;\r\n",
10057             format("#define A \\\r\n"
10058                    "  b; \\\r\n"
10059                    "  c; d; \r\n",
10060                    getGoogleStyle()));
10061 
10062   EXPECT_EQ("/*\r\n"
10063             "multi line block comments\r\n"
10064             "should not introduce\r\n"
10065             "an extra carriage return\r\n"
10066             "*/\r\n",
10067             format("/*\r\n"
10068                    "multi line block comments\r\n"
10069                    "should not introduce\r\n"
10070                    "an extra carriage return\r\n"
10071                    "*/\r\n"));
10072 }
10073 
10074 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
10075   verifyFormat("MY_CLASS(C) {\n"
10076                "  int i;\n"
10077                "  int j;\n"
10078                "};");
10079 }
10080 
10081 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
10082   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
10083   TwoIndent.ContinuationIndentWidth = 2;
10084 
10085   EXPECT_EQ("int i =\n"
10086             "  longFunction(\n"
10087             "    arg);",
10088             format("int i = longFunction(arg);", TwoIndent));
10089 
10090   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
10091   SixIndent.ContinuationIndentWidth = 6;
10092 
10093   EXPECT_EQ("int i =\n"
10094             "      longFunction(\n"
10095             "            arg);",
10096             format("int i = longFunction(arg);", SixIndent));
10097 }
10098 
10099 TEST_F(FormatTest, SpacesInAngles) {
10100   FormatStyle Spaces = getLLVMStyle();
10101   Spaces.SpacesInAngles = true;
10102 
10103   verifyFormat("static_cast< int >(arg);", Spaces);
10104   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
10105   verifyFormat("f< int, float >();", Spaces);
10106   verifyFormat("template <> g() {}", Spaces);
10107   verifyFormat("template < std::vector< int > > f() {}", Spaces);
10108   verifyFormat("std::function< void(int, int) > fct;", Spaces);
10109   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
10110                Spaces);
10111 
10112   Spaces.Standard = FormatStyle::LS_Cpp03;
10113   Spaces.SpacesInAngles = true;
10114   verifyFormat("A< A< int > >();", Spaces);
10115 
10116   Spaces.SpacesInAngles = false;
10117   verifyFormat("A<A<int> >();", Spaces);
10118 
10119   Spaces.Standard = FormatStyle::LS_Cpp11;
10120   Spaces.SpacesInAngles = true;
10121   verifyFormat("A< A< int > >();", Spaces);
10122 
10123   Spaces.SpacesInAngles = false;
10124   verifyFormat("A<A<int>>();", Spaces);
10125 }
10126 
10127 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
10128   FormatStyle Style = getLLVMStyle();
10129   Style.SpaceAfterTemplateKeyword = false;
10130   verifyFormat("template<int> void foo();", Style);
10131 }
10132 
10133 TEST_F(FormatTest, TripleAngleBrackets) {
10134   verifyFormat("f<<<1, 1>>>();");
10135   verifyFormat("f<<<1, 1, 1, s>>>();");
10136   verifyFormat("f<<<a, b, c, d>>>();");
10137   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
10138   verifyFormat("f<param><<<1, 1>>>();");
10139   verifyFormat("f<1><<<1, 1>>>();");
10140   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
10141   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10142                "aaaaaaaaaaa<<<\n    1, 1>>>();");
10143   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
10144                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
10145 }
10146 
10147 TEST_F(FormatTest, MergeLessLessAtEnd) {
10148   verifyFormat("<<");
10149   EXPECT_EQ("< < <", format("\\\n<<<"));
10150   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10151                "aaallvm::outs() <<");
10152   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10153                "aaaallvm::outs()\n    <<");
10154 }
10155 
10156 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
10157   std::string code = "#if A\n"
10158                      "#if B\n"
10159                      "a.\n"
10160                      "#endif\n"
10161                      "    a = 1;\n"
10162                      "#else\n"
10163                      "#endif\n"
10164                      "#if C\n"
10165                      "#else\n"
10166                      "#endif\n";
10167   EXPECT_EQ(code, format(code));
10168 }
10169 
10170 TEST_F(FormatTest, HandleConflictMarkers) {
10171   // Git/SVN conflict markers.
10172   EXPECT_EQ("int a;\n"
10173             "void f() {\n"
10174             "  callme(some(parameter1,\n"
10175             "<<<<<<< text by the vcs\n"
10176             "              parameter2),\n"
10177             "||||||| text by the vcs\n"
10178             "              parameter2),\n"
10179             "         parameter3,\n"
10180             "======= text by the vcs\n"
10181             "              parameter2, parameter3),\n"
10182             ">>>>>>> text by the vcs\n"
10183             "         otherparameter);\n",
10184             format("int a;\n"
10185                    "void f() {\n"
10186                    "  callme(some(parameter1,\n"
10187                    "<<<<<<< text by the vcs\n"
10188                    "  parameter2),\n"
10189                    "||||||| text by the vcs\n"
10190                    "  parameter2),\n"
10191                    "  parameter3,\n"
10192                    "======= text by the vcs\n"
10193                    "  parameter2,\n"
10194                    "  parameter3),\n"
10195                    ">>>>>>> text by the vcs\n"
10196                    "  otherparameter);\n"));
10197 
10198   // Perforce markers.
10199   EXPECT_EQ("void f() {\n"
10200             "  function(\n"
10201             ">>>> text by the vcs\n"
10202             "      parameter,\n"
10203             "==== text by the vcs\n"
10204             "      parameter,\n"
10205             "==== text by the vcs\n"
10206             "      parameter,\n"
10207             "<<<< text by the vcs\n"
10208             "      parameter);\n",
10209             format("void f() {\n"
10210                    "  function(\n"
10211                    ">>>> text by the vcs\n"
10212                    "  parameter,\n"
10213                    "==== text by the vcs\n"
10214                    "  parameter,\n"
10215                    "==== text by the vcs\n"
10216                    "  parameter,\n"
10217                    "<<<< text by the vcs\n"
10218                    "  parameter);\n"));
10219 
10220   EXPECT_EQ("<<<<<<<\n"
10221             "|||||||\n"
10222             "=======\n"
10223             ">>>>>>>",
10224             format("<<<<<<<\n"
10225                    "|||||||\n"
10226                    "=======\n"
10227                    ">>>>>>>"));
10228 
10229   EXPECT_EQ("<<<<<<<\n"
10230             "|||||||\n"
10231             "int i;\n"
10232             "=======\n"
10233             ">>>>>>>",
10234             format("<<<<<<<\n"
10235                    "|||||||\n"
10236                    "int i;\n"
10237                    "=======\n"
10238                    ">>>>>>>"));
10239 
10240   // FIXME: Handle parsing of macros around conflict markers correctly:
10241   EXPECT_EQ("#define Macro \\\n"
10242             "<<<<<<<\n"
10243             "Something \\\n"
10244             "|||||||\n"
10245             "Else \\\n"
10246             "=======\n"
10247             "Other \\\n"
10248             ">>>>>>>\n"
10249             "    End int i;\n",
10250             format("#define Macro \\\n"
10251                    "<<<<<<<\n"
10252                    "  Something \\\n"
10253                    "|||||||\n"
10254                    "  Else \\\n"
10255                    "=======\n"
10256                    "  Other \\\n"
10257                    ">>>>>>>\n"
10258                    "  End\n"
10259                    "int i;\n"));
10260 }
10261 
10262 TEST_F(FormatTest, DisableRegions) {
10263   EXPECT_EQ("int i;\n"
10264             "// clang-format off\n"
10265             "  int j;\n"
10266             "// clang-format on\n"
10267             "int k;",
10268             format(" int  i;\n"
10269                    "   // clang-format off\n"
10270                    "  int j;\n"
10271                    " // clang-format on\n"
10272                    "   int   k;"));
10273   EXPECT_EQ("int i;\n"
10274             "/* clang-format off */\n"
10275             "  int j;\n"
10276             "/* clang-format on */\n"
10277             "int k;",
10278             format(" int  i;\n"
10279                    "   /* clang-format off */\n"
10280                    "  int j;\n"
10281                    " /* clang-format on */\n"
10282                    "   int   k;"));
10283 
10284   // Don't reflow comments within disabled regions.
10285   EXPECT_EQ(
10286       "// clang-format off\n"
10287       "// long long long long long long line\n"
10288       "/* clang-format on */\n"
10289       "/* long long long\n"
10290       " * long long long\n"
10291       " * line */\n"
10292       "int i;\n"
10293       "/* clang-format off */\n"
10294       "/* long long long long long long line */\n",
10295       format("// clang-format off\n"
10296              "// long long long long long long line\n"
10297              "/* clang-format on */\n"
10298              "/* long long long long long long line */\n"
10299              "int i;\n"
10300              "/* clang-format off */\n"
10301              "/* long long long long long long line */\n",
10302              getLLVMStyleWithColumns(20)));
10303 }
10304 
10305 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
10306   format("? ) =");
10307   verifyNoCrash("#define a\\\n /**/}");
10308 }
10309 
10310 TEST_F(FormatTest, FormatsTableGenCode) {
10311   FormatStyle Style = getLLVMStyle();
10312   Style.Language = FormatStyle::LK_TableGen;
10313   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
10314 }
10315 
10316 TEST_F(FormatTest, ArrayOfTemplates) {
10317   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
10318             format("auto a = new unique_ptr<int > [ 10];"));
10319 
10320   FormatStyle Spaces = getLLVMStyle();
10321   Spaces.SpacesInSquareBrackets = true;
10322   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
10323             format("auto a = new unique_ptr<int > [10];", Spaces));
10324 }
10325 
10326 TEST_F(FormatTest, ArrayAsTemplateType) {
10327   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
10328             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
10329 
10330   FormatStyle Spaces = getLLVMStyle();
10331   Spaces.SpacesInSquareBrackets = true;
10332   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
10333             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
10334 }
10335 
10336 TEST_F(FormatTest, NoSpaceAfterSuper) {
10337     verifyFormat("__super::FooBar();");
10338 }
10339 
10340 TEST(FormatStyle, GetStyleOfFile) {
10341   vfs::InMemoryFileSystem FS;
10342   // Test 1: format file in the same directory.
10343   ASSERT_TRUE(
10344       FS.addFile("/a/.clang-format", 0,
10345                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
10346   ASSERT_TRUE(
10347       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
10348   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
10349   ASSERT_TRUE((bool)Style1);
10350   ASSERT_EQ(*Style1, getLLVMStyle());
10351 
10352   // Test 2.1: fallback to default.
10353   ASSERT_TRUE(
10354       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
10355   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
10356   ASSERT_TRUE((bool)Style2);
10357   ASSERT_EQ(*Style2, getMozillaStyle());
10358 
10359   // Test 2.2: no format on 'none' fallback style.
10360   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
10361   ASSERT_TRUE((bool)Style2);
10362   ASSERT_EQ(*Style2, getNoStyle());
10363 
10364   // Test 2.3: format if config is found with no based style while fallback is
10365   // 'none'.
10366   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
10367                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
10368   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
10369   ASSERT_TRUE((bool)Style2);
10370   ASSERT_EQ(*Style2, getLLVMStyle());
10371 
10372   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
10373   Style2 = getStyle("{}", "a.h", "none", "", &FS);
10374   ASSERT_TRUE((bool)Style2);
10375   ASSERT_EQ(*Style2, getLLVMStyle());
10376 
10377   // Test 3: format file in parent directory.
10378   ASSERT_TRUE(
10379       FS.addFile("/c/.clang-format", 0,
10380                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
10381   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
10382                          llvm::MemoryBuffer::getMemBuffer("int i;")));
10383   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
10384   ASSERT_TRUE((bool)Style3);
10385   ASSERT_EQ(*Style3, getGoogleStyle());
10386 
10387   // Test 4: error on invalid fallback style
10388   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
10389   ASSERT_FALSE((bool)Style4);
10390   llvm::consumeError(Style4.takeError());
10391 
10392   // Test 5: error on invalid yaml on command line
10393   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
10394   ASSERT_FALSE((bool)Style5);
10395   llvm::consumeError(Style5.takeError());
10396 
10397   // Test 6: error on invalid style
10398   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
10399   ASSERT_FALSE((bool)Style6);
10400   llvm::consumeError(Style6.takeError());
10401 
10402   // Test 7: found config file, error on parsing it
10403   ASSERT_TRUE(
10404       FS.addFile("/d/.clang-format", 0,
10405                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
10406                                                   "InvalidKey: InvalidValue")));
10407   ASSERT_TRUE(
10408       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
10409   auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
10410   ASSERT_FALSE((bool)Style7);
10411   llvm::consumeError(Style7.takeError());
10412 }
10413 
10414 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
10415   // Column limit is 20.
10416   std::string Code = "Type *a =\n"
10417                      "    new Type();\n"
10418                      "g(iiiii, 0, jjjjj,\n"
10419                      "  0, kkkkk, 0, mm);\n"
10420                      "int  bad     = format   ;";
10421   std::string Expected = "auto a = new Type();\n"
10422                          "g(iiiii, nullptr,\n"
10423                          "  jjjjj, nullptr,\n"
10424                          "  kkkkk, nullptr,\n"
10425                          "  mm);\n"
10426                          "int  bad     = format   ;";
10427   FileID ID = Context.createInMemoryFile("format.cpp", Code);
10428   tooling::Replacements Replaces = toReplacements(
10429       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
10430                             "auto "),
10431        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
10432                             "nullptr"),
10433        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
10434                             "nullptr"),
10435        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
10436                             "nullptr")});
10437 
10438   format::FormatStyle Style = format::getLLVMStyle();
10439   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
10440   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
10441   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
10442       << llvm::toString(FormattedReplaces.takeError()) << "\n";
10443   auto Result = applyAllReplacements(Code, *FormattedReplaces);
10444   EXPECT_TRUE(static_cast<bool>(Result));
10445   EXPECT_EQ(Expected, *Result);
10446 }
10447 
10448 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
10449   std::string Code = "#include \"a.h\"\n"
10450                      "#include \"c.h\"\n"
10451                      "\n"
10452                      "int main() {\n"
10453                      "  return 0;\n"
10454                      "}";
10455   std::string Expected = "#include \"a.h\"\n"
10456                          "#include \"b.h\"\n"
10457                          "#include \"c.h\"\n"
10458                          "\n"
10459                          "int main() {\n"
10460                          "  return 0;\n"
10461                          "}";
10462   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
10463   tooling::Replacements Replaces = toReplacements(
10464       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
10465                             "#include \"b.h\"\n")});
10466 
10467   format::FormatStyle Style = format::getLLVMStyle();
10468   Style.SortIncludes = true;
10469   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
10470   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
10471       << llvm::toString(FormattedReplaces.takeError()) << "\n";
10472   auto Result = applyAllReplacements(Code, *FormattedReplaces);
10473   EXPECT_TRUE(static_cast<bool>(Result));
10474   EXPECT_EQ(Expected, *Result);
10475 }
10476 
10477 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
10478   format::FormatStyle Style = format::getLLVMStyle();
10479   Style.Standard = FormatStyle::LS_Cpp03;
10480   // cpp03 recognize this string as identifier u8 and literal character 'a'
10481   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
10482 }
10483 
10484 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
10485   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
10486   // all modes, including C++11, C++14 and C++17
10487   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
10488 }
10489 
10490 } // end namespace
10491 } // end namespace format
10492 } // end namespace clang
10493