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 "FormatTestUtils.h"
11 #include "clang/Format/Format.h"
12 #include "llvm/Support/Debug.h"
13 #include "gtest/gtest.h"
14 
15 #define DEBUG_TYPE "format-test"
16 
17 namespace clang {
18 namespace format {
19 namespace {
20 
21 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
22 
23 class FormatTest : public ::testing::Test {
24 protected:
25   enum IncompleteCheck {
26     IC_ExpectComplete,
27     IC_ExpectIncomplete,
28     IC_DoNotCheck
29   };
30 
31   std::string format(llvm::StringRef Code,
32                      const FormatStyle &Style = getLLVMStyle(),
33                      IncompleteCheck CheckIncomplete = IC_ExpectComplete) {
34     DEBUG(llvm::errs() << "---\n");
35     DEBUG(llvm::errs() << Code << "\n\n");
36     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
37     bool IncompleteFormat = false;
38     tooling::Replacements Replaces =
39         reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
40     if (CheckIncomplete != IC_DoNotCheck) {
41       bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete;
42       EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n";
43     }
44     ReplacementCount = Replaces.size();
45     std::string Result = applyAllReplacements(Code, Replaces);
46     EXPECT_NE("", Result);
47     DEBUG(llvm::errs() << "\n" << Result << "\n\n");
48     return Result;
49   }
50 
51   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
52     FormatStyle Style = getLLVMStyle();
53     Style.ColumnLimit = ColumnLimit;
54     return Style;
55   }
56 
57   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
58     FormatStyle Style = getGoogleStyle();
59     Style.ColumnLimit = ColumnLimit;
60     return Style;
61   }
62 
63   void verifyFormat(llvm::StringRef Code,
64                     const FormatStyle &Style = getLLVMStyle()) {
65     EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
66   }
67 
68   void verifyIncompleteFormat(llvm::StringRef Code,
69                               const FormatStyle &Style = getLLVMStyle()) {
70     EXPECT_EQ(Code.str(),
71               format(test::messUp(Code), Style, IC_ExpectIncomplete));
72   }
73 
74   void verifyGoogleFormat(llvm::StringRef Code) {
75     verifyFormat(Code, getGoogleStyle());
76   }
77 
78   void verifyIndependentOfContext(llvm::StringRef text) {
79     verifyFormat(text);
80     verifyFormat(llvm::Twine("void f() { " + text + " }").str());
81   }
82 
83   /// \brief Verify that clang-format does not crash on the given input.
84   void verifyNoCrash(llvm::StringRef Code,
85                      const FormatStyle &Style = getLLVMStyle()) {
86     format(Code, Style, IC_DoNotCheck);
87   }
88 
89   int ReplacementCount;
90 };
91 
92 TEST_F(FormatTest, MessUp) {
93   EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
94   EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
95   EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
96   EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
97   EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
98 }
99 
100 //===----------------------------------------------------------------------===//
101 // Basic function tests.
102 //===----------------------------------------------------------------------===//
103 
104 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
105   EXPECT_EQ(";", format(";"));
106 }
107 
108 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
109   EXPECT_EQ("int i;", format("  int i;"));
110   EXPECT_EQ("\nint i;", format(" \n\t \v \f  int i;"));
111   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
112   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
113 }
114 
115 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
116   EXPECT_EQ("int i;", format("int\ni;"));
117 }
118 
119 TEST_F(FormatTest, FormatsNestedBlockStatements) {
120   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
121 }
122 
123 TEST_F(FormatTest, FormatsNestedCall) {
124   verifyFormat("Method(f1, f2(f3));");
125   verifyFormat("Method(f1(f2, f3()));");
126   verifyFormat("Method(f1(f2, (f3())));");
127 }
128 
129 TEST_F(FormatTest, NestedNameSpecifiers) {
130   verifyFormat("vector<::Type> v;");
131   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
132   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
133   verifyFormat("bool a = 2 < ::SomeFunction();");
134 }
135 
136 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
137   EXPECT_EQ("if (a) {\n"
138             "  f();\n"
139             "}",
140             format("if(a){f();}"));
141   EXPECT_EQ(4, ReplacementCount);
142   EXPECT_EQ("if (a) {\n"
143             "  f();\n"
144             "}",
145             format("if (a) {\n"
146                    "  f();\n"
147                    "}"));
148   EXPECT_EQ(0, ReplacementCount);
149   EXPECT_EQ("/*\r\n"
150             "\r\n"
151             "*/\r\n",
152             format("/*\r\n"
153                    "\r\n"
154                    "*/\r\n"));
155   EXPECT_EQ(0, ReplacementCount);
156 }
157 
158 TEST_F(FormatTest, RemovesEmptyLines) {
159   EXPECT_EQ("class C {\n"
160             "  int i;\n"
161             "};",
162             format("class C {\n"
163                    " int i;\n"
164                    "\n"
165                    "};"));
166 
167   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
168   EXPECT_EQ("namespace N {\n"
169             "\n"
170             "int i;\n"
171             "}",
172             format("namespace N {\n"
173                    "\n"
174                    "int    i;\n"
175                    "}",
176                    getGoogleStyle()));
177   EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
178             "\n"
179             "int i;\n"
180             "}",
181             format("extern /**/ \"C\" /**/ {\n"
182                    "\n"
183                    "int    i;\n"
184                    "}",
185                    getGoogleStyle()));
186 
187   // ...but do keep inlining and removing empty lines for non-block extern "C"
188   // functions.
189   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
190   EXPECT_EQ("extern \"C\" int f() {\n"
191             "  int i = 42;\n"
192             "  return i;\n"
193             "}",
194             format("extern \"C\" int f() {\n"
195                    "\n"
196                    "  int i = 42;\n"
197                    "  return i;\n"
198                    "}",
199                    getGoogleStyle()));
200 
201   // Remove empty lines at the beginning and end of blocks.
202   EXPECT_EQ("void f() {\n"
203             "\n"
204             "  if (a) {\n"
205             "\n"
206             "    f();\n"
207             "  }\n"
208             "}",
209             format("void f() {\n"
210                    "\n"
211                    "  if (a) {\n"
212                    "\n"
213                    "    f();\n"
214                    "\n"
215                    "  }\n"
216                    "\n"
217                    "}",
218                    getLLVMStyle()));
219   EXPECT_EQ("void f() {\n"
220             "  if (a) {\n"
221             "    f();\n"
222             "  }\n"
223             "}",
224             format("void f() {\n"
225                    "\n"
226                    "  if (a) {\n"
227                    "\n"
228                    "    f();\n"
229                    "\n"
230                    "  }\n"
231                    "\n"
232                    "}",
233                    getGoogleStyle()));
234 
235   // Don't remove empty lines in more complex control statements.
236   EXPECT_EQ("void f() {\n"
237             "  if (a) {\n"
238             "    f();\n"
239             "\n"
240             "  } else if (b) {\n"
241             "    f();\n"
242             "  }\n"
243             "}",
244             format("void f() {\n"
245                    "  if (a) {\n"
246                    "    f();\n"
247                    "\n"
248                    "  } else if (b) {\n"
249                    "    f();\n"
250                    "\n"
251                    "  }\n"
252                    "\n"
253                    "}"));
254 
255   // FIXME: This is slightly inconsistent.
256   EXPECT_EQ("namespace {\n"
257             "int i;\n"
258             "}",
259             format("namespace {\n"
260                    "int i;\n"
261                    "\n"
262                    "}"));
263   EXPECT_EQ("namespace {\n"
264             "int i;\n"
265             "\n"
266             "} // namespace",
267             format("namespace {\n"
268                    "int i;\n"
269                    "\n"
270                    "}  // namespace"));
271 }
272 
273 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
274   verifyFormat("x = (a) and (b);");
275   verifyFormat("x = (a) or (b);");
276   verifyFormat("x = (a) bitand (b);");
277   verifyFormat("x = (a) bitor (b);");
278   verifyFormat("x = (a) not_eq (b);");
279   verifyFormat("x = (a) and_eq (b);");
280   verifyFormat("x = (a) or_eq (b);");
281   verifyFormat("x = (a) xor (b);");
282 }
283 
284 //===----------------------------------------------------------------------===//
285 // Tests for control statements.
286 //===----------------------------------------------------------------------===//
287 
288 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
289   verifyFormat("if (true)\n  f();\ng();");
290   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
291   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
292 
293   FormatStyle AllowsMergedIf = getLLVMStyle();
294   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
295   verifyFormat("if (a)\n"
296                "  // comment\n"
297                "  f();",
298                AllowsMergedIf);
299   verifyFormat("if (a)\n"
300                "  ;",
301                AllowsMergedIf);
302   verifyFormat("if (a)\n"
303                "  if (b) return;",
304                AllowsMergedIf);
305 
306   verifyFormat("if (a) // Can't merge this\n"
307                "  f();\n",
308                AllowsMergedIf);
309   verifyFormat("if (a) /* still don't merge */\n"
310                "  f();",
311                AllowsMergedIf);
312   verifyFormat("if (a) { // Never merge this\n"
313                "  f();\n"
314                "}",
315                AllowsMergedIf);
316   verifyFormat("if (a) {/* Never merge this */\n"
317                "  f();\n"
318                "}",
319                AllowsMergedIf);
320 
321   AllowsMergedIf.ColumnLimit = 14;
322   verifyFormat("if (a) return;", AllowsMergedIf);
323   verifyFormat("if (aaaaaaaaa)\n"
324                "  return;",
325                AllowsMergedIf);
326 
327   AllowsMergedIf.ColumnLimit = 13;
328   verifyFormat("if (a)\n  return;", AllowsMergedIf);
329 }
330 
331 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
332   FormatStyle AllowsMergedLoops = getLLVMStyle();
333   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
334   verifyFormat("while (true) continue;", AllowsMergedLoops);
335   verifyFormat("for (;;) continue;", AllowsMergedLoops);
336   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
337   verifyFormat("while (true)\n"
338                "  ;",
339                AllowsMergedLoops);
340   verifyFormat("for (;;)\n"
341                "  ;",
342                AllowsMergedLoops);
343   verifyFormat("for (;;)\n"
344                "  for (;;) continue;",
345                AllowsMergedLoops);
346   verifyFormat("for (;;) // Can't merge this\n"
347                "  continue;",
348                AllowsMergedLoops);
349   verifyFormat("for (;;) /* still don't merge */\n"
350                "  continue;",
351                AllowsMergedLoops);
352 }
353 
354 TEST_F(FormatTest, FormatShortBracedStatements) {
355   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
356   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
357 
358   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
359   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
360 
361   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
362   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
363   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
364   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
365   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
366   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
367   verifyFormat("if (true) { //\n"
368                "  f();\n"
369                "}",
370                AllowSimpleBracedStatements);
371   verifyFormat("if (true) {\n"
372                "  f();\n"
373                "  f();\n"
374                "}",
375                AllowSimpleBracedStatements);
376   verifyFormat("if (true) {\n"
377                "  f();\n"
378                "} else {\n"
379                "  f();\n"
380                "}",
381                AllowSimpleBracedStatements);
382 
383   verifyFormat("template <int> struct A2 {\n"
384                "  struct B {};\n"
385                "};",
386                AllowSimpleBracedStatements);
387 
388   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
389   verifyFormat("if (true) {\n"
390                "  f();\n"
391                "}",
392                AllowSimpleBracedStatements);
393   verifyFormat("if (true) {\n"
394                "  f();\n"
395                "} else {\n"
396                "  f();\n"
397                "}",
398                AllowSimpleBracedStatements);
399 
400   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
401   verifyFormat("while (true) {\n"
402                "  f();\n"
403                "}",
404                AllowSimpleBracedStatements);
405   verifyFormat("for (;;) {\n"
406                "  f();\n"
407                "}",
408                AllowSimpleBracedStatements);
409 }
410 
411 TEST_F(FormatTest, ParseIfElse) {
412   verifyFormat("if (true)\n"
413                "  if (true)\n"
414                "    if (true)\n"
415                "      f();\n"
416                "    else\n"
417                "      g();\n"
418                "  else\n"
419                "    h();\n"
420                "else\n"
421                "  i();");
422   verifyFormat("if (true)\n"
423                "  if (true)\n"
424                "    if (true) {\n"
425                "      if (true)\n"
426                "        f();\n"
427                "    } else {\n"
428                "      g();\n"
429                "    }\n"
430                "  else\n"
431                "    h();\n"
432                "else {\n"
433                "  i();\n"
434                "}");
435   verifyFormat("void f() {\n"
436                "  if (a) {\n"
437                "  } else {\n"
438                "  }\n"
439                "}");
440 }
441 
442 TEST_F(FormatTest, ElseIf) {
443   verifyFormat("if (a) {\n} else if (b) {\n}");
444   verifyFormat("if (a)\n"
445                "  f();\n"
446                "else if (b)\n"
447                "  g();\n"
448                "else\n"
449                "  h();");
450   verifyFormat("if (a) {\n"
451                "  f();\n"
452                "}\n"
453                "// or else ..\n"
454                "else {\n"
455                "  g()\n"
456                "}");
457 
458   verifyFormat("if (a) {\n"
459                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
460                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
461                "}");
462   verifyFormat("if (a) {\n"
463                "} else if (\n"
464                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
465                "}",
466                getLLVMStyleWithColumns(62));
467 }
468 
469 TEST_F(FormatTest, FormatsForLoop) {
470   verifyFormat(
471       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
472       "     ++VeryVeryLongLoopVariable)\n"
473       "  ;");
474   verifyFormat("for (;;)\n"
475                "  f();");
476   verifyFormat("for (;;) {\n}");
477   verifyFormat("for (;;) {\n"
478                "  f();\n"
479                "}");
480   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
481 
482   verifyFormat(
483       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
484       "                                          E = UnwrappedLines.end();\n"
485       "     I != E; ++I) {\n}");
486 
487   verifyFormat(
488       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
489       "     ++IIIII) {\n}");
490   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
491                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
492                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
493   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
494                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
495                "         E = FD->getDeclsInPrototypeScope().end();\n"
496                "     I != E; ++I) {\n}");
497   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
498                "         I = Container.begin(),\n"
499                "         E = Container.end();\n"
500                "     I != E; ++I) {\n}",
501                getLLVMStyleWithColumns(76));
502 
503   verifyFormat(
504       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
505       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
506       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
507       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
508       "     ++aaaaaaaaaaa) {\n}");
509   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
510                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
511                "     ++i) {\n}");
512   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
513                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
514                "}");
515   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
516                "         aaaaaaaaaa);\n"
517                "     iter; ++iter) {\n"
518                "}");
519   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
520                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
521                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
522                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
523 
524   FormatStyle NoBinPacking = getLLVMStyle();
525   NoBinPacking.BinPackParameters = false;
526   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
527                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
528                "                                           aaaaaaaaaaaaaaaa,\n"
529                "                                           aaaaaaaaaaaaaaaa,\n"
530                "                                           aaaaaaaaaaaaaaaa);\n"
531                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
532                "}",
533                NoBinPacking);
534   verifyFormat(
535       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
536       "                                          E = UnwrappedLines.end();\n"
537       "     I != E;\n"
538       "     ++I) {\n}",
539       NoBinPacking);
540 }
541 
542 TEST_F(FormatTest, RangeBasedForLoops) {
543   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
544                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
545   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
546                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
547   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
548                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
549   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
550                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
551 }
552 
553 TEST_F(FormatTest, ForEachLoops) {
554   verifyFormat("void f() {\n"
555                "  foreach (Item *item, itemlist) {}\n"
556                "  Q_FOREACH (Item *item, itemlist) {}\n"
557                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
558                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
559                "}");
560 
561   // As function-like macros.
562   verifyFormat("#define foreach(x, y)\n"
563                "#define Q_FOREACH(x, y)\n"
564                "#define BOOST_FOREACH(x, y)\n"
565                "#define UNKNOWN_FOREACH(x, y)\n");
566 
567   // Not as function-like macros.
568   verifyFormat("#define foreach (x, y)\n"
569                "#define Q_FOREACH (x, y)\n"
570                "#define BOOST_FOREACH (x, y)\n"
571                "#define UNKNOWN_FOREACH (x, y)\n");
572 }
573 
574 TEST_F(FormatTest, FormatsWhileLoop) {
575   verifyFormat("while (true) {\n}");
576   verifyFormat("while (true)\n"
577                "  f();");
578   verifyFormat("while () {\n}");
579   verifyFormat("while () {\n"
580                "  f();\n"
581                "}");
582 }
583 
584 TEST_F(FormatTest, FormatsDoWhile) {
585   verifyFormat("do {\n"
586                "  do_something();\n"
587                "} while (something());");
588   verifyFormat("do\n"
589                "  do_something();\n"
590                "while (something());");
591 }
592 
593 TEST_F(FormatTest, FormatsSwitchStatement) {
594   verifyFormat("switch (x) {\n"
595                "case 1:\n"
596                "  f();\n"
597                "  break;\n"
598                "case kFoo:\n"
599                "case ns::kBar:\n"
600                "case kBaz:\n"
601                "  break;\n"
602                "default:\n"
603                "  g();\n"
604                "  break;\n"
605                "}");
606   verifyFormat("switch (x) {\n"
607                "case 1: {\n"
608                "  f();\n"
609                "  break;\n"
610                "}\n"
611                "case 2: {\n"
612                "  break;\n"
613                "}\n"
614                "}");
615   verifyFormat("switch (x) {\n"
616                "case 1: {\n"
617                "  f();\n"
618                "  {\n"
619                "    g();\n"
620                "    h();\n"
621                "  }\n"
622                "  break;\n"
623                "}\n"
624                "}");
625   verifyFormat("switch (x) {\n"
626                "case 1: {\n"
627                "  f();\n"
628                "  if (foo) {\n"
629                "    g();\n"
630                "    h();\n"
631                "  }\n"
632                "  break;\n"
633                "}\n"
634                "}");
635   verifyFormat("switch (x) {\n"
636                "case 1: {\n"
637                "  f();\n"
638                "  g();\n"
639                "} break;\n"
640                "}");
641   verifyFormat("switch (test)\n"
642                "  ;");
643   verifyFormat("switch (x) {\n"
644                "default: {\n"
645                "  // Do nothing.\n"
646                "}\n"
647                "}");
648   verifyFormat("switch (x) {\n"
649                "// comment\n"
650                "// if 1, do f()\n"
651                "case 1:\n"
652                "  f();\n"
653                "}");
654   verifyFormat("switch (x) {\n"
655                "case 1:\n"
656                "  // Do amazing stuff\n"
657                "  {\n"
658                "    f();\n"
659                "    g();\n"
660                "  }\n"
661                "  break;\n"
662                "}");
663   verifyFormat("#define A          \\\n"
664                "  switch (x) {     \\\n"
665                "  case a:          \\\n"
666                "    foo = b;       \\\n"
667                "  }",
668                getLLVMStyleWithColumns(20));
669   verifyFormat("#define OPERATION_CASE(name)           \\\n"
670                "  case OP_name:                        \\\n"
671                "    return operations::Operation##name\n",
672                getLLVMStyleWithColumns(40));
673   verifyFormat("switch (x) {\n"
674                "case 1:;\n"
675                "default:;\n"
676                "  int i;\n"
677                "}");
678 
679   verifyGoogleFormat("switch (x) {\n"
680                      "  case 1:\n"
681                      "    f();\n"
682                      "    break;\n"
683                      "  case kFoo:\n"
684                      "  case ns::kBar:\n"
685                      "  case kBaz:\n"
686                      "    break;\n"
687                      "  default:\n"
688                      "    g();\n"
689                      "    break;\n"
690                      "}");
691   verifyGoogleFormat("switch (x) {\n"
692                      "  case 1: {\n"
693                      "    f();\n"
694                      "    break;\n"
695                      "  }\n"
696                      "}");
697   verifyGoogleFormat("switch (test)\n"
698                      "  ;");
699 
700   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
701                      "  case OP_name:              \\\n"
702                      "    return operations::Operation##name\n");
703   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
704                      "  // Get the correction operation class.\n"
705                      "  switch (OpCode) {\n"
706                      "    CASE(Add);\n"
707                      "    CASE(Subtract);\n"
708                      "    default:\n"
709                      "      return operations::Unknown;\n"
710                      "  }\n"
711                      "#undef OPERATION_CASE\n"
712                      "}");
713   verifyFormat("DEBUG({\n"
714                "  switch (x) {\n"
715                "  case A:\n"
716                "    f();\n"
717                "    break;\n"
718                "  // On B:\n"
719                "  case B:\n"
720                "    g();\n"
721                "    break;\n"
722                "  }\n"
723                "});");
724   verifyFormat("switch (a) {\n"
725                "case (b):\n"
726                "  return;\n"
727                "}");
728 
729   verifyFormat("switch (a) {\n"
730                "case some_namespace::\n"
731                "    some_constant:\n"
732                "  return;\n"
733                "}",
734                getLLVMStyleWithColumns(34));
735 }
736 
737 TEST_F(FormatTest, CaseRanges) {
738   verifyFormat("switch (x) {\n"
739                "case 'A' ... 'Z':\n"
740                "case 1 ... 5:\n"
741                "  break;\n"
742                "}");
743 }
744 
745 TEST_F(FormatTest, ShortCaseLabels) {
746   FormatStyle Style = getLLVMStyle();
747   Style.AllowShortCaseLabelsOnASingleLine = true;
748   verifyFormat("switch (a) {\n"
749                "case 1: x = 1; break;\n"
750                "case 2: return;\n"
751                "case 3:\n"
752                "case 4:\n"
753                "case 5: return;\n"
754                "case 6: // comment\n"
755                "  return;\n"
756                "case 7:\n"
757                "  // comment\n"
758                "  return;\n"
759                "default: y = 1; break;\n"
760                "}",
761                Style);
762   verifyFormat("switch (a) {\n"
763                "#if FOO\n"
764                "case 0: return 0;\n"
765                "#endif\n"
766                "}",
767                Style);
768   verifyFormat("switch (a) {\n"
769                "case 1: {\n"
770                "}\n"
771                "case 2: {\n"
772                "  return;\n"
773                "}\n"
774                "case 3: {\n"
775                "  x = 1;\n"
776                "  return;\n"
777                "}\n"
778                "case 4:\n"
779                "  if (x)\n"
780                "    return;\n"
781                "}",
782                Style);
783   Style.ColumnLimit = 21;
784   verifyFormat("switch (a) {\n"
785                "case 1: x = 1; break;\n"
786                "case 2: return;\n"
787                "case 3:\n"
788                "case 4:\n"
789                "case 5: return;\n"
790                "default:\n"
791                "  y = 1;\n"
792                "  break;\n"
793                "}",
794                Style);
795 }
796 
797 TEST_F(FormatTest, FormatsLabels) {
798   verifyFormat("void f() {\n"
799                "  some_code();\n"
800                "test_label:\n"
801                "  some_other_code();\n"
802                "  {\n"
803                "    some_more_code();\n"
804                "  another_label:\n"
805                "    some_more_code();\n"
806                "  }\n"
807                "}");
808   verifyFormat("{\n"
809                "  some_code();\n"
810                "test_label:\n"
811                "  some_other_code();\n"
812                "}");
813   verifyFormat("{\n"
814                "  some_code();\n"
815                "test_label:;\n"
816                "  int i = 0;\n"
817                "}");
818 }
819 
820 //===----------------------------------------------------------------------===//
821 // Tests for comments.
822 //===----------------------------------------------------------------------===//
823 
824 TEST_F(FormatTest, UnderstandsSingleLineComments) {
825   verifyFormat("//* */");
826   verifyFormat("// line 1\n"
827                "// line 2\n"
828                "void f() {}\n");
829 
830   verifyFormat("void f() {\n"
831                "  // Doesn't do anything\n"
832                "}");
833   verifyFormat("SomeObject\n"
834                "    // Calling someFunction on SomeObject\n"
835                "    .someFunction();");
836   verifyFormat("auto result = SomeObject\n"
837                "                  // Calling someFunction on SomeObject\n"
838                "                  .someFunction();");
839   verifyFormat("void f(int i,  // some comment (probably for i)\n"
840                "       int j,  // some comment (probably for j)\n"
841                "       int k); // some comment (probably for k)");
842   verifyFormat("void f(int i,\n"
843                "       // some comment (probably for j)\n"
844                "       int j,\n"
845                "       // some comment (probably for k)\n"
846                "       int k);");
847 
848   verifyFormat("int i    // This is a fancy variable\n"
849                "    = 5; // with nicely aligned comment.");
850 
851   verifyFormat("// Leading comment.\n"
852                "int a; // Trailing comment.");
853   verifyFormat("int a; // Trailing comment\n"
854                "       // on 2\n"
855                "       // or 3 lines.\n"
856                "int b;");
857   verifyFormat("int a; // Trailing comment\n"
858                "\n"
859                "// Leading comment.\n"
860                "int b;");
861   verifyFormat("int a;    // Comment.\n"
862                "          // More details.\n"
863                "int bbbb; // Another comment.");
864   verifyFormat(
865       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
866       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   // comment\n"
867       "int cccccccccccccccccccccccccccccc;       // comment\n"
868       "int ddd;                     // looooooooooooooooooooooooong comment\n"
869       "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
870       "int bbbbbbbbbbbbbbbbbbbbb;   // comment\n"
871       "int ccccccccccccccccccc;     // comment");
872 
873   verifyFormat("#include \"a\"     // comment\n"
874                "#include \"a/b/c\" // comment");
875   verifyFormat("#include <a>     // comment\n"
876                "#include <a/b/c> // comment");
877   EXPECT_EQ("#include \"a\"     // comment\n"
878             "#include \"a/b/c\" // comment",
879             format("#include \\\n"
880                    "  \"a\" // comment\n"
881                    "#include \"a/b/c\" // comment"));
882 
883   verifyFormat("enum E {\n"
884                "  // comment\n"
885                "  VAL_A, // comment\n"
886                "  VAL_B\n"
887                "};");
888 
889   verifyFormat(
890       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
891       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
892   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
893                "    // Comment inside a statement.\n"
894                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
895   verifyFormat("SomeFunction(a,\n"
896                "             // comment\n"
897                "             b + x);");
898   verifyFormat("SomeFunction(a, a,\n"
899                "             // comment\n"
900                "             b + x);");
901   verifyFormat(
902       "bool aaaaaaaaaaaaa = // comment\n"
903       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
904       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
905 
906   verifyFormat("int aaaa; // aaaaa\n"
907                "int aa;   // aaaaaaa",
908                getLLVMStyleWithColumns(20));
909 
910   EXPECT_EQ("void f() { // This does something ..\n"
911             "}\n"
912             "int a; // This is unrelated",
913             format("void f()    {     // This does something ..\n"
914                    "  }\n"
915                    "int   a;     // This is unrelated"));
916   EXPECT_EQ("class C {\n"
917             "  void f() { // This does something ..\n"
918             "  }          // awesome..\n"
919             "\n"
920             "  int a; // This is unrelated\n"
921             "};",
922             format("class C{void f()    { // This does something ..\n"
923                    "      } // awesome..\n"
924                    " \n"
925                    "int a;    // This is unrelated\n"
926                    "};"));
927 
928   EXPECT_EQ("int i; // single line trailing comment",
929             format("int i;\\\n// single line trailing comment"));
930 
931   verifyGoogleFormat("int a;  // Trailing comment.");
932 
933   verifyFormat("someFunction(anotherFunction( // Force break.\n"
934                "    parameter));");
935 
936   verifyGoogleFormat("#endif  // HEADER_GUARD");
937 
938   verifyFormat("const char *test[] = {\n"
939                "    // A\n"
940                "    \"aaaa\",\n"
941                "    // B\n"
942                "    \"aaaaa\"};");
943   verifyGoogleFormat(
944       "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
945       "    aaaaaaaaaaaaaaaaaaaaaa);  // 81_cols_with_this_comment");
946   EXPECT_EQ("D(a, {\n"
947             "  // test\n"
948             "  int a;\n"
949             "});",
950             format("D(a, {\n"
951                    "// test\n"
952                    "int a;\n"
953                    "});"));
954 
955   EXPECT_EQ("lineWith(); // comment\n"
956             "// at start\n"
957             "otherLine();",
958             format("lineWith();   // comment\n"
959                    "// at start\n"
960                    "otherLine();"));
961   EXPECT_EQ("lineWith(); // comment\n"
962             "            // at start\n"
963             "otherLine();",
964             format("lineWith();   // comment\n"
965                    " // at start\n"
966                    "otherLine();"));
967 
968   EXPECT_EQ("lineWith(); // comment\n"
969             "// at start\n"
970             "otherLine(); // comment",
971             format("lineWith();   // comment\n"
972                    "// at start\n"
973                    "otherLine();   // comment"));
974   EXPECT_EQ("lineWith();\n"
975             "// at start\n"
976             "otherLine(); // comment",
977             format("lineWith();\n"
978                    " // at start\n"
979                    "otherLine();   // comment"));
980   EXPECT_EQ("// first\n"
981             "// at start\n"
982             "otherLine(); // comment",
983             format("// first\n"
984                    " // at start\n"
985                    "otherLine();   // comment"));
986   EXPECT_EQ("f();\n"
987             "// first\n"
988             "// at start\n"
989             "otherLine(); // comment",
990             format("f();\n"
991                    "// first\n"
992                    " // at start\n"
993                    "otherLine();   // comment"));
994   verifyFormat("f(); // comment\n"
995                "// first\n"
996                "// at start\n"
997                "otherLine();");
998   EXPECT_EQ("f(); // comment\n"
999             "// first\n"
1000             "// at start\n"
1001             "otherLine();",
1002             format("f();   // comment\n"
1003                    "// first\n"
1004                    " // at start\n"
1005                    "otherLine();"));
1006   EXPECT_EQ("f(); // comment\n"
1007             "     // first\n"
1008             "// at start\n"
1009             "otherLine();",
1010             format("f();   // comment\n"
1011                    " // first\n"
1012                    "// at start\n"
1013                    "otherLine();"));
1014   EXPECT_EQ("void f() {\n"
1015             "  lineWith(); // comment\n"
1016             "  // at start\n"
1017             "}",
1018             format("void              f() {\n"
1019                    "  lineWith(); // comment\n"
1020                    "  // at start\n"
1021                    "}"));
1022 
1023   verifyFormat("#define A                                                  \\\n"
1024                "  int i; /* iiiiiiiiiiiiiiiiiiiii */                       \\\n"
1025                "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
1026                getLLVMStyleWithColumns(60));
1027   verifyFormat(
1028       "#define A                                                   \\\n"
1029       "  int i;                        /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
1030       "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
1031       getLLVMStyleWithColumns(61));
1032 
1033   verifyFormat("if ( // This is some comment\n"
1034                "    x + 3) {\n"
1035                "}");
1036   EXPECT_EQ("if ( // This is some comment\n"
1037             "     // spanning two lines\n"
1038             "    x + 3) {\n"
1039             "}",
1040             format("if( // This is some comment\n"
1041                    "     // spanning two lines\n"
1042                    " x + 3) {\n"
1043                    "}"));
1044 
1045   verifyNoCrash("/\\\n/");
1046   verifyNoCrash("/\\\n* */");
1047   // The 0-character somehow makes the lexer return a proper comment.
1048   verifyNoCrash(StringRef("/*\\\0\n/", 6));
1049 }
1050 
1051 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
1052   EXPECT_EQ("SomeFunction(a,\n"
1053             "             b, // comment\n"
1054             "             c);",
1055             format("SomeFunction(a,\n"
1056                    "          b, // comment\n"
1057                    "      c);"));
1058   EXPECT_EQ("SomeFunction(a, b,\n"
1059             "             // comment\n"
1060             "             c);",
1061             format("SomeFunction(a,\n"
1062                    "          b,\n"
1063                    "  // comment\n"
1064                    "      c);"));
1065   EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
1066             "             c);",
1067             format("SomeFunction(a, b, // comment (unclear relation)\n"
1068                    "      c);"));
1069   EXPECT_EQ("SomeFunction(a, // comment\n"
1070             "             b,\n"
1071             "             c); // comment",
1072             format("SomeFunction(a,     // comment\n"
1073                    "          b,\n"
1074                    "      c); // comment"));
1075 }
1076 
1077 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
1078   EXPECT_EQ("// comment", format("// comment  "));
1079   EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
1080             format("int aaaaaaa, bbbbbbb; // comment                   ",
1081                    getLLVMStyleWithColumns(33)));
1082   EXPECT_EQ("// comment\\\n", format("// comment\\\n  \t \v   \f   "));
1083   EXPECT_EQ("// comment    \\\n", format("// comment    \\\n  \t \v   \f   "));
1084 }
1085 
1086 TEST_F(FormatTest, UnderstandsBlockComments) {
1087   verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
1088   verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
1089   EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
1090             "  bbbbbbbbbbbbbbbbbbbbbbbbb);",
1091             format("f(aaaaaaaaaaaaaaaaaaaaaaaaa ,   \\\n"
1092                    "/* Trailing comment for aa... */\n"
1093                    "  bbbbbbbbbbbbbbbbbbbbbbbbb);"));
1094   EXPECT_EQ(
1095       "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1096       "  /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
1097       format("f(aaaaaaaaaaaaaaaaaaaaaaaaa    ,   \n"
1098              "/* Leading comment for bb... */   bbbbbbbbbbbbbbbbbbbbbbbbb);"));
1099   EXPECT_EQ(
1100       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1101       "    aaaaaaaaaaaaaaaaaa,\n"
1102       "    aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
1103       "}",
1104       format("void      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1105              "                      aaaaaaaaaaaaaaaaaa  ,\n"
1106              "    aaaaaaaaaaaaaaaaaa) {   /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
1107              "}"));
1108 
1109   FormatStyle NoBinPacking = getLLVMStyle();
1110   NoBinPacking.BinPackParameters = false;
1111   verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
1112                "         /* parameter 2 */ aaaaaa,\n"
1113                "         /* parameter 3 */ aaaaaa,\n"
1114                "         /* parameter 4 */ aaaaaa);",
1115                NoBinPacking);
1116 
1117   // Aligning block comments in macros.
1118   verifyGoogleFormat("#define A        \\\n"
1119                      "  int i;   /*a*/ \\\n"
1120                      "  int jjj; /*b*/");
1121 }
1122 
1123 TEST_F(FormatTest, AlignsBlockComments) {
1124   EXPECT_EQ("/*\n"
1125             " * Really multi-line\n"
1126             " * comment.\n"
1127             " */\n"
1128             "void f() {}",
1129             format("  /*\n"
1130                    "   * Really multi-line\n"
1131                    "   * comment.\n"
1132                    "   */\n"
1133                    "  void f() {}"));
1134   EXPECT_EQ("class C {\n"
1135             "  /*\n"
1136             "   * Another multi-line\n"
1137             "   * comment.\n"
1138             "   */\n"
1139             "  void f() {}\n"
1140             "};",
1141             format("class C {\n"
1142                    "/*\n"
1143                    " * Another multi-line\n"
1144                    " * comment.\n"
1145                    " */\n"
1146                    "void f() {}\n"
1147                    "};"));
1148   EXPECT_EQ("/*\n"
1149             "  1. This is a comment with non-trivial formatting.\n"
1150             "     1.1. We have to indent/outdent all lines equally\n"
1151             "         1.1.1. to keep the formatting.\n"
1152             " */",
1153             format("  /*\n"
1154                    "    1. This is a comment with non-trivial formatting.\n"
1155                    "       1.1. We have to indent/outdent all lines equally\n"
1156                    "           1.1.1. to keep the formatting.\n"
1157                    "   */"));
1158   EXPECT_EQ("/*\n"
1159             "Don't try to outdent if there's not enough indentation.\n"
1160             "*/",
1161             format("  /*\n"
1162                    " Don't try to outdent if there's not enough indentation.\n"
1163                    " */"));
1164 
1165   EXPECT_EQ("int i; /* Comment with empty...\n"
1166             "        *\n"
1167             "        * line. */",
1168             format("int i; /* Comment with empty...\n"
1169                    "        *\n"
1170                    "        * line. */"));
1171   EXPECT_EQ("int foobar = 0; /* comment */\n"
1172             "int bar = 0;    /* multiline\n"
1173             "                   comment 1 */\n"
1174             "int baz = 0;    /* multiline\n"
1175             "                   comment 2 */\n"
1176             "int bzz = 0;    /* multiline\n"
1177             "                   comment 3 */",
1178             format("int foobar = 0; /* comment */\n"
1179                    "int bar = 0;    /* multiline\n"
1180                    "                   comment 1 */\n"
1181                    "int baz = 0; /* multiline\n"
1182                    "                comment 2 */\n"
1183                    "int bzz = 0;         /* multiline\n"
1184                    "                        comment 3 */"));
1185   EXPECT_EQ("int foobar = 0; /* comment */\n"
1186             "int bar = 0;    /* multiline\n"
1187             "   comment */\n"
1188             "int baz = 0;    /* multiline\n"
1189             "comment */",
1190             format("int foobar = 0; /* comment */\n"
1191                    "int bar = 0; /* multiline\n"
1192                    "comment */\n"
1193                    "int baz = 0;        /* multiline\n"
1194                    "comment */"));
1195 }
1196 
1197 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
1198   EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1199             "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
1200             format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1201                    "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
1202   EXPECT_EQ(
1203       "void ffffffffffff(\n"
1204       "    int aaaaaaaa, int bbbbbbbb,\n"
1205       "    int cccccccccccc) { /*\n"
1206       "                           aaaaaaaaaa\n"
1207       "                           aaaaaaaaaaaaa\n"
1208       "                           bbbbbbbbbbbbbb\n"
1209       "                           bbbbbbbbbb\n"
1210       "                         */\n"
1211       "}",
1212       format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
1213              "{ /*\n"
1214              "     aaaaaaaaaa aaaaaaaaaaaaa\n"
1215              "     bbbbbbbbbbbbbb bbbbbbbbbb\n"
1216              "   */\n"
1217              "}",
1218              getLLVMStyleWithColumns(40)));
1219 }
1220 
1221 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
1222   EXPECT_EQ("void ffffffffff(\n"
1223             "    int aaaaa /* test */);",
1224             format("void ffffffffff(int aaaaa /* test */);",
1225                    getLLVMStyleWithColumns(35)));
1226 }
1227 
1228 TEST_F(FormatTest, SplitsLongCxxComments) {
1229   EXPECT_EQ("// A comment that\n"
1230             "// doesn't fit on\n"
1231             "// one line",
1232             format("// A comment that doesn't fit on one line",
1233                    getLLVMStyleWithColumns(20)));
1234   EXPECT_EQ("/// A comment that\n"
1235             "/// doesn't fit on\n"
1236             "/// one line",
1237             format("/// A comment that doesn't fit on one line",
1238                    getLLVMStyleWithColumns(20)));
1239   EXPECT_EQ("//! A comment that\n"
1240             "//! doesn't fit on\n"
1241             "//! one line",
1242             format("//! A comment that doesn't fit on one line",
1243                    getLLVMStyleWithColumns(20)));
1244   EXPECT_EQ("// a b c d\n"
1245             "// e f  g\n"
1246             "// h i j k",
1247             format("// a b c d e f  g h i j k", getLLVMStyleWithColumns(10)));
1248   EXPECT_EQ(
1249       "// a b c d\n"
1250       "// e f  g\n"
1251       "// h i j k",
1252       format("\\\n// a b c d e f  g h i j k", getLLVMStyleWithColumns(10)));
1253   EXPECT_EQ("if (true) // A comment that\n"
1254             "          // doesn't fit on\n"
1255             "          // one line",
1256             format("if (true) // A comment that doesn't fit on one line   ",
1257                    getLLVMStyleWithColumns(30)));
1258   EXPECT_EQ("//    Don't_touch_leading_whitespace",
1259             format("//    Don't_touch_leading_whitespace",
1260                    getLLVMStyleWithColumns(20)));
1261   EXPECT_EQ("// Add leading\n"
1262             "// whitespace",
1263             format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
1264   EXPECT_EQ("/// Add leading\n"
1265             "/// whitespace",
1266             format("///Add leading whitespace", getLLVMStyleWithColumns(20)));
1267   EXPECT_EQ("//! Add leading\n"
1268             "//! whitespace",
1269             format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
1270   EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
1271   EXPECT_EQ("// Even if it makes the line exceed the column\n"
1272             "// limit",
1273             format("//Even if it makes the line exceed the column limit",
1274                    getLLVMStyleWithColumns(51)));
1275   EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
1276 
1277   EXPECT_EQ("// aa bb cc dd",
1278             format("// aa bb             cc dd                   ",
1279                    getLLVMStyleWithColumns(15)));
1280 
1281   EXPECT_EQ("// A comment before\n"
1282             "// a macro\n"
1283             "// definition\n"
1284             "#define a b",
1285             format("// A comment before a macro definition\n"
1286                    "#define a b",
1287                    getLLVMStyleWithColumns(20)));
1288   EXPECT_EQ("void ffffff(\n"
1289             "    int aaaaaaaaa,  // wwww\n"
1290             "    int bbbbbbbbbb, // xxxxxxx\n"
1291             "                    // yyyyyyyyyy\n"
1292             "    int c, int d, int e) {}",
1293             format("void ffffff(\n"
1294                    "    int aaaaaaaaa, // wwww\n"
1295                    "    int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
1296                    "    int c, int d, int e) {}",
1297                    getLLVMStyleWithColumns(40)));
1298   EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1299             format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1300                    getLLVMStyleWithColumns(20)));
1301   EXPECT_EQ(
1302       "#define XXX // a b c d\n"
1303       "            // e f g h",
1304       format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
1305   EXPECT_EQ(
1306       "#define XXX // q w e r\n"
1307       "            // t y u i",
1308       format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
1309 }
1310 
1311 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
1312   EXPECT_EQ("//     A comment\n"
1313             "//     that doesn't\n"
1314             "//     fit on one\n"
1315             "//     line",
1316             format("//     A comment that doesn't fit on one line",
1317                    getLLVMStyleWithColumns(20)));
1318   EXPECT_EQ("///     A comment\n"
1319             "///     that doesn't\n"
1320             "///     fit on one\n"
1321             "///     line",
1322             format("///     A comment that doesn't fit on one line",
1323                    getLLVMStyleWithColumns(20)));
1324 }
1325 
1326 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
1327   EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1328             "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1329             "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1330             format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1331                    "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1332                    "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
1333   EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1334             "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1335             "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1336             format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1337                    "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1338                    "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1339                    getLLVMStyleWithColumns(50)));
1340   // FIXME: One day we might want to implement adjustment of leading whitespace
1341   // of the consecutive lines in this kind of comment:
1342   EXPECT_EQ("double\n"
1343             "    a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1344             "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1345             "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1346             format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1347                    "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1348                    "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1349                    getLLVMStyleWithColumns(49)));
1350 }
1351 
1352 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
1353   FormatStyle Pragmas = getLLVMStyleWithColumns(30);
1354   Pragmas.CommentPragmas = "^ IWYU pragma:";
1355   EXPECT_EQ(
1356       "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
1357       format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
1358   EXPECT_EQ(
1359       "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
1360       format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
1361 }
1362 
1363 TEST_F(FormatTest, PriorityOfCommentBreaking) {
1364   EXPECT_EQ("if (xxx ==\n"
1365             "        yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
1366             "    zzz)\n"
1367             "  q();",
1368             format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
1369                    "    zzz) q();",
1370                    getLLVMStyleWithColumns(40)));
1371   EXPECT_EQ("if (xxxxxxxxxx ==\n"
1372             "        yyy && // aaaaaa bbbbbbbb cccc\n"
1373             "    zzz)\n"
1374             "  q();",
1375             format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
1376                    "    zzz) q();",
1377                    getLLVMStyleWithColumns(40)));
1378   EXPECT_EQ("if (xxxxxxxxxx &&\n"
1379             "        yyy || // aaaaaa bbbbbbbb cccc\n"
1380             "    zzz)\n"
1381             "  q();",
1382             format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
1383                    "    zzz) q();",
1384                    getLLVMStyleWithColumns(40)));
1385   EXPECT_EQ("fffffffff(\n"
1386             "    &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
1387             "    zzz);",
1388             format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
1389                    " zzz);",
1390                    getLLVMStyleWithColumns(40)));
1391 }
1392 
1393 TEST_F(FormatTest, MultiLineCommentsInDefines) {
1394   EXPECT_EQ("#define A(x) /* \\\n"
1395             "  a comment     \\\n"
1396             "  inside */     \\\n"
1397             "  f();",
1398             format("#define A(x) /* \\\n"
1399                    "  a comment     \\\n"
1400                    "  inside */     \\\n"
1401                    "  f();",
1402                    getLLVMStyleWithColumns(17)));
1403   EXPECT_EQ("#define A(      \\\n"
1404             "    x) /*       \\\n"
1405             "  a comment     \\\n"
1406             "  inside */     \\\n"
1407             "  f();",
1408             format("#define A(      \\\n"
1409                    "    x) /*       \\\n"
1410                    "  a comment     \\\n"
1411                    "  inside */     \\\n"
1412                    "  f();",
1413                    getLLVMStyleWithColumns(17)));
1414 }
1415 
1416 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
1417   EXPECT_EQ("namespace {}\n// Test\n#define A",
1418             format("namespace {}\n   // Test\n#define A"));
1419   EXPECT_EQ("namespace {}\n/* Test */\n#define A",
1420             format("namespace {}\n   /* Test */\n#define A"));
1421   EXPECT_EQ("namespace {}\n/* Test */ #define A",
1422             format("namespace {}\n   /* Test */    #define A"));
1423 }
1424 
1425 TEST_F(FormatTest, SplitsLongLinesInComments) {
1426   EXPECT_EQ("/* This is a long\n"
1427             " * comment that\n"
1428             " * doesn't\n"
1429             " * fit on one line.\n"
1430             " */",
1431             format("/* "
1432                    "This is a long                                         "
1433                    "comment that "
1434                    "doesn't                                    "
1435                    "fit on one line.  */",
1436                    getLLVMStyleWithColumns(20)));
1437   EXPECT_EQ(
1438       "/* a b c d\n"
1439       " * e f  g\n"
1440       " * h i j k\n"
1441       " */",
1442       format("/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
1443   EXPECT_EQ(
1444       "/* a b c d\n"
1445       " * e f  g\n"
1446       " * h i j k\n"
1447       " */",
1448       format("\\\n/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
1449   EXPECT_EQ("/*\n"
1450             "This is a long\n"
1451             "comment that doesn't\n"
1452             "fit on one line.\n"
1453             "*/",
1454             format("/*\n"
1455                    "This is a long                                         "
1456                    "comment that doesn't                                    "
1457                    "fit on one line.                                      \n"
1458                    "*/",
1459                    getLLVMStyleWithColumns(20)));
1460   EXPECT_EQ("/*\n"
1461             " * This is a long\n"
1462             " * comment that\n"
1463             " * doesn't fit on\n"
1464             " * one line.\n"
1465             " */",
1466             format("/*      \n"
1467                    " * This is a long "
1468                    "   comment that     "
1469                    "   doesn't fit on   "
1470                    "   one line.                                            \n"
1471                    " */",
1472                    getLLVMStyleWithColumns(20)));
1473   EXPECT_EQ("/*\n"
1474             " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
1475             " * so_it_should_be_broken\n"
1476             " * wherever_a_space_occurs\n"
1477             " */",
1478             format("/*\n"
1479                    " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
1480                    "   so_it_should_be_broken "
1481                    "   wherever_a_space_occurs                             \n"
1482                    " */",
1483                    getLLVMStyleWithColumns(20)));
1484   EXPECT_EQ("/*\n"
1485             " *    This_comment_can_not_be_broken_into_lines\n"
1486             " */",
1487             format("/*\n"
1488                    " *    This_comment_can_not_be_broken_into_lines\n"
1489                    " */",
1490                    getLLVMStyleWithColumns(20)));
1491   EXPECT_EQ("{\n"
1492             "  /*\n"
1493             "  This is another\n"
1494             "  long comment that\n"
1495             "  doesn't fit on one\n"
1496             "  line    1234567890\n"
1497             "  */\n"
1498             "}",
1499             format("{\n"
1500                    "/*\n"
1501                    "This is another     "
1502                    "  long comment that "
1503                    "  doesn't fit on one"
1504                    "  line    1234567890\n"
1505                    "*/\n"
1506                    "}",
1507                    getLLVMStyleWithColumns(20)));
1508   EXPECT_EQ("{\n"
1509             "  /*\n"
1510             "   * This        i s\n"
1511             "   * another comment\n"
1512             "   * t hat  doesn' t\n"
1513             "   * fit on one l i\n"
1514             "   * n e\n"
1515             "   */\n"
1516             "}",
1517             format("{\n"
1518                    "/*\n"
1519                    " * This        i s"
1520                    "   another comment"
1521                    "   t hat  doesn' t"
1522                    "   fit on one l i"
1523                    "   n e\n"
1524                    " */\n"
1525                    "}",
1526                    getLLVMStyleWithColumns(20)));
1527   EXPECT_EQ("/*\n"
1528             " * This is a long\n"
1529             " * comment that\n"
1530             " * doesn't fit on\n"
1531             " * one line\n"
1532             " */",
1533             format("   /*\n"
1534                    "    * This is a long comment that doesn't fit on one line\n"
1535                    "    */",
1536                    getLLVMStyleWithColumns(20)));
1537   EXPECT_EQ("{\n"
1538             "  if (something) /* This is a\n"
1539             "                    long\n"
1540             "                    comment */\n"
1541             "    ;\n"
1542             "}",
1543             format("{\n"
1544                    "  if (something) /* This is a long comment */\n"
1545                    "    ;\n"
1546                    "}",
1547                    getLLVMStyleWithColumns(30)));
1548 
1549   EXPECT_EQ("/* A comment before\n"
1550             " * a macro\n"
1551             " * definition */\n"
1552             "#define a b",
1553             format("/* A comment before a macro definition */\n"
1554                    "#define a b",
1555                    getLLVMStyleWithColumns(20)));
1556 
1557   EXPECT_EQ("/* some comment\n"
1558             "     *   a comment\n"
1559             "* that we break\n"
1560             " * another comment\n"
1561             "* we have to break\n"
1562             "* a left comment\n"
1563             " */",
1564             format("  /* some comment\n"
1565                    "       *   a comment that we break\n"
1566                    "   * another comment we have to break\n"
1567                    "* a left comment\n"
1568                    "   */",
1569                    getLLVMStyleWithColumns(20)));
1570 
1571   EXPECT_EQ("/**\n"
1572             " * multiline block\n"
1573             " * comment\n"
1574             " *\n"
1575             " */",
1576             format("/**\n"
1577                    " * multiline block comment\n"
1578                    " *\n"
1579                    " */",
1580                    getLLVMStyleWithColumns(20)));
1581 
1582   EXPECT_EQ("/*\n"
1583             "\n"
1584             "\n"
1585             "    */\n",
1586             format("  /*       \n"
1587                    "      \n"
1588                    "               \n"
1589                    "      */\n"));
1590 
1591   EXPECT_EQ("/* a a */",
1592             format("/* a a            */", getLLVMStyleWithColumns(15)));
1593   EXPECT_EQ("/* a a bc  */",
1594             format("/* a a            bc  */", getLLVMStyleWithColumns(15)));
1595   EXPECT_EQ("/* aaa aaa\n"
1596             " * aaaaa */",
1597             format("/* aaa aaa aaaaa       */", getLLVMStyleWithColumns(15)));
1598   EXPECT_EQ("/* aaa aaa\n"
1599             " * aaaaa     */",
1600             format("/* aaa aaa aaaaa     */", getLLVMStyleWithColumns(15)));
1601 }
1602 
1603 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
1604   EXPECT_EQ("#define X          \\\n"
1605             "  /*               \\\n"
1606             "   Test            \\\n"
1607             "   Macro comment   \\\n"
1608             "   with a long     \\\n"
1609             "   line            \\\n"
1610             "   */              \\\n"
1611             "  A + B",
1612             format("#define X \\\n"
1613                    "  /*\n"
1614                    "   Test\n"
1615                    "   Macro comment with a long  line\n"
1616                    "   */ \\\n"
1617                    "  A + B",
1618                    getLLVMStyleWithColumns(20)));
1619   EXPECT_EQ("#define X          \\\n"
1620             "  /* Macro comment \\\n"
1621             "     with a long   \\\n"
1622             "     line */       \\\n"
1623             "  A + B",
1624             format("#define X \\\n"
1625                    "  /* Macro comment with a long\n"
1626                    "     line */ \\\n"
1627                    "  A + B",
1628                    getLLVMStyleWithColumns(20)));
1629   EXPECT_EQ("#define X          \\\n"
1630             "  /* Macro comment \\\n"
1631             "   * with a long   \\\n"
1632             "   * line */       \\\n"
1633             "  A + B",
1634             format("#define X \\\n"
1635                    "  /* Macro comment with a long  line */ \\\n"
1636                    "  A + B",
1637                    getLLVMStyleWithColumns(20)));
1638 }
1639 
1640 TEST_F(FormatTest, CommentsInStaticInitializers) {
1641   EXPECT_EQ(
1642       "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
1643       "                        aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
1644       "                        /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
1645       "                        aaaaaaaaaaaaaaaaaaaa, // comment\n"
1646       "                        aaaaaaaaaaaaaaaaaaaa};",
1647       format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa  ,  /* comment */\n"
1648              "                   aaaaaaaaaaaaaaaaaaaa   /* comment */ ,\n"
1649              "                     /* comment */   aaaaaaaaaaaaaaaaaaaa ,\n"
1650              "              aaaaaaaaaaaaaaaaaaaa ,   // comment\n"
1651              "                  aaaaaaaaaaaaaaaaaaaa };"));
1652   verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1653                "                        bbbbbbbbbbb, ccccccccccc};");
1654   verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
1655                "                        // comment for bb....\n"
1656                "                        bbbbbbbbbbb, ccccccccccc};");
1657   verifyGoogleFormat(
1658       "static SomeType type = {aaaaaaaaaaa,  // comment for aa...\n"
1659       "                        bbbbbbbbbbb, ccccccccccc};");
1660   verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
1661                      "                        // comment for bb....\n"
1662                      "                        bbbbbbbbbbb, ccccccccccc};");
1663 
1664   verifyFormat("S s = {{a, b, c},  // Group #1\n"
1665                "       {d, e, f},  // Group #2\n"
1666                "       {g, h, i}}; // Group #3");
1667   verifyFormat("S s = {{// Group #1\n"
1668                "        a, b, c},\n"
1669                "       {// Group #2\n"
1670                "        d, e, f},\n"
1671                "       {// Group #3\n"
1672                "        g, h, i}};");
1673 
1674   EXPECT_EQ("S s = {\n"
1675             "    // Some comment\n"
1676             "    a,\n"
1677             "\n"
1678             "    // Comment after empty line\n"
1679             "    b}",
1680             format("S s =    {\n"
1681                    "      // Some comment\n"
1682                    "  a,\n"
1683                    "  \n"
1684                    "     // Comment after empty line\n"
1685                    "      b\n"
1686                    "}"));
1687   EXPECT_EQ("S s = {\n"
1688             "    /* Some comment */\n"
1689             "    a,\n"
1690             "\n"
1691             "    /* Comment after empty line */\n"
1692             "    b}",
1693             format("S s =    {\n"
1694                    "      /* Some comment */\n"
1695                    "  a,\n"
1696                    "  \n"
1697                    "     /* Comment after empty line */\n"
1698                    "      b\n"
1699                    "}"));
1700   verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
1701                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1702                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1703                "    0x00, 0x00, 0x00, 0x00};            // comment\n");
1704 }
1705 
1706 TEST_F(FormatTest, IgnoresIf0Contents) {
1707   EXPECT_EQ("#if 0\n"
1708             "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1709             "#endif\n"
1710             "void f() {}",
1711             format("#if 0\n"
1712                    "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1713                    "#endif\n"
1714                    "void f(  ) {  }"));
1715   EXPECT_EQ("#if false\n"
1716             "void f(  ) {  }\n"
1717             "#endif\n"
1718             "void g() {}\n",
1719             format("#if false\n"
1720                    "void f(  ) {  }\n"
1721                    "#endif\n"
1722                    "void g(  ) {  }\n"));
1723   EXPECT_EQ("enum E {\n"
1724             "  One,\n"
1725             "  Two,\n"
1726             "#if 0\n"
1727             "Three,\n"
1728             "      Four,\n"
1729             "#endif\n"
1730             "  Five\n"
1731             "};",
1732             format("enum E {\n"
1733                    "  One,Two,\n"
1734                    "#if 0\n"
1735                    "Three,\n"
1736                    "      Four,\n"
1737                    "#endif\n"
1738                    "  Five};"));
1739   EXPECT_EQ("enum F {\n"
1740             "  One,\n"
1741             "#if 1\n"
1742             "  Two,\n"
1743             "#if 0\n"
1744             "Three,\n"
1745             "      Four,\n"
1746             "#endif\n"
1747             "  Five\n"
1748             "#endif\n"
1749             "};",
1750             format("enum F {\n"
1751                    "One,\n"
1752                    "#if 1\n"
1753                    "Two,\n"
1754                    "#if 0\n"
1755                    "Three,\n"
1756                    "      Four,\n"
1757                    "#endif\n"
1758                    "Five\n"
1759                    "#endif\n"
1760                    "};"));
1761   EXPECT_EQ("enum G {\n"
1762             "  One,\n"
1763             "#if 0\n"
1764             "Two,\n"
1765             "#else\n"
1766             "  Three,\n"
1767             "#endif\n"
1768             "  Four\n"
1769             "};",
1770             format("enum G {\n"
1771                    "One,\n"
1772                    "#if 0\n"
1773                    "Two,\n"
1774                    "#else\n"
1775                    "Three,\n"
1776                    "#endif\n"
1777                    "Four\n"
1778                    "};"));
1779   EXPECT_EQ("enum H {\n"
1780             "  One,\n"
1781             "#if 0\n"
1782             "#ifdef Q\n"
1783             "Two,\n"
1784             "#else\n"
1785             "Three,\n"
1786             "#endif\n"
1787             "#endif\n"
1788             "  Four\n"
1789             "};",
1790             format("enum H {\n"
1791                    "One,\n"
1792                    "#if 0\n"
1793                    "#ifdef Q\n"
1794                    "Two,\n"
1795                    "#else\n"
1796                    "Three,\n"
1797                    "#endif\n"
1798                    "#endif\n"
1799                    "Four\n"
1800                    "};"));
1801   EXPECT_EQ("enum I {\n"
1802             "  One,\n"
1803             "#if /* test */ 0 || 1\n"
1804             "Two,\n"
1805             "Three,\n"
1806             "#endif\n"
1807             "  Four\n"
1808             "};",
1809             format("enum I {\n"
1810                    "One,\n"
1811                    "#if /* test */ 0 || 1\n"
1812                    "Two,\n"
1813                    "Three,\n"
1814                    "#endif\n"
1815                    "Four\n"
1816                    "};"));
1817   EXPECT_EQ("enum J {\n"
1818             "  One,\n"
1819             "#if 0\n"
1820             "#if 0\n"
1821             "Two,\n"
1822             "#else\n"
1823             "Three,\n"
1824             "#endif\n"
1825             "Four,\n"
1826             "#endif\n"
1827             "  Five\n"
1828             "};",
1829             format("enum J {\n"
1830                    "One,\n"
1831                    "#if 0\n"
1832                    "#if 0\n"
1833                    "Two,\n"
1834                    "#else\n"
1835                    "Three,\n"
1836                    "#endif\n"
1837                    "Four,\n"
1838                    "#endif\n"
1839                    "Five\n"
1840                    "};"));
1841 }
1842 
1843 //===----------------------------------------------------------------------===//
1844 // Tests for classes, namespaces, etc.
1845 //===----------------------------------------------------------------------===//
1846 
1847 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1848   verifyFormat("class A {};");
1849 }
1850 
1851 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1852   verifyFormat("class A {\n"
1853                "public:\n"
1854                "public: // comment\n"
1855                "protected:\n"
1856                "private:\n"
1857                "  void f() {}\n"
1858                "};");
1859   verifyGoogleFormat("class A {\n"
1860                      " public:\n"
1861                      " protected:\n"
1862                      " private:\n"
1863                      "  void f() {}\n"
1864                      "};");
1865   verifyFormat("class A {\n"
1866                "public slots:\n"
1867                "  void f() {}\n"
1868                "public Q_SLOTS:\n"
1869                "  void f() {}\n"
1870                "signals:\n"
1871                "  void g();\n"
1872                "};");
1873 
1874   // Don't interpret 'signals' the wrong way.
1875   verifyFormat("signals.set();");
1876   verifyFormat("for (Signals signals : f()) {\n}");
1877   verifyFormat("{\n"
1878                "  signals.set(); // This needs indentation.\n"
1879                "}");
1880 }
1881 
1882 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1883   EXPECT_EQ("class A {\n"
1884             "public:\n"
1885             "  void f();\n"
1886             "\n"
1887             "private:\n"
1888             "  void g() {}\n"
1889             "  // test\n"
1890             "protected:\n"
1891             "  int h;\n"
1892             "};",
1893             format("class A {\n"
1894                    "public:\n"
1895                    "void f();\n"
1896                    "private:\n"
1897                    "void g() {}\n"
1898                    "// test\n"
1899                    "protected:\n"
1900                    "int h;\n"
1901                    "};"));
1902   EXPECT_EQ("class A {\n"
1903             "protected:\n"
1904             "public:\n"
1905             "  void f();\n"
1906             "};",
1907             format("class A {\n"
1908                    "protected:\n"
1909                    "\n"
1910                    "public:\n"
1911                    "\n"
1912                    "  void f();\n"
1913                    "};"));
1914 
1915   // Even ensure proper spacing inside macros.
1916   EXPECT_EQ("#define B     \\\n"
1917             "  class A {   \\\n"
1918             "   protected: \\\n"
1919             "   public:    \\\n"
1920             "    void f(); \\\n"
1921             "  };",
1922             format("#define B     \\\n"
1923                    "  class A {   \\\n"
1924                    "   protected: \\\n"
1925                    "              \\\n"
1926                    "   public:    \\\n"
1927                    "              \\\n"
1928                    "    void f(); \\\n"
1929                    "  };",
1930                    getGoogleStyle()));
1931   // But don't remove empty lines after macros ending in access specifiers.
1932   EXPECT_EQ("#define A private:\n"
1933             "\n"
1934             "int i;",
1935             format("#define A         private:\n"
1936                    "\n"
1937                    "int              i;"));
1938 }
1939 
1940 TEST_F(FormatTest, FormatsClasses) {
1941   verifyFormat("class A : public B {};");
1942   verifyFormat("class A : public ::B {};");
1943 
1944   verifyFormat(
1945       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1946       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1947   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1948                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1949                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1950   verifyFormat(
1951       "class A : public B, public C, public D, public E, public F {};");
1952   verifyFormat("class AAAAAAAAAAAA : public B,\n"
1953                "                     public C,\n"
1954                "                     public D,\n"
1955                "                     public E,\n"
1956                "                     public F,\n"
1957                "                     public G {};");
1958 
1959   verifyFormat("class\n"
1960                "    ReallyReallyLongClassName {\n"
1961                "  int i;\n"
1962                "};",
1963                getLLVMStyleWithColumns(32));
1964   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
1965                "                           aaaaaaaaaaaaaaaa> {};");
1966   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
1967                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
1968                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
1969   verifyFormat("template <class R, class C>\n"
1970                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
1971                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
1972   verifyFormat("class ::A::B {};");
1973 }
1974 
1975 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1976   verifyFormat("class A {\n} a, b;");
1977   verifyFormat("struct A {\n} a, b;");
1978   verifyFormat("union A {\n} a;");
1979 }
1980 
1981 TEST_F(FormatTest, FormatsEnum) {
1982   verifyFormat("enum {\n"
1983                "  Zero,\n"
1984                "  One = 1,\n"
1985                "  Two = One + 1,\n"
1986                "  Three = (One + Two),\n"
1987                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1988                "  Five = (One, Two, Three, Four, 5)\n"
1989                "};");
1990   verifyGoogleFormat("enum {\n"
1991                      "  Zero,\n"
1992                      "  One = 1,\n"
1993                      "  Two = One + 1,\n"
1994                      "  Three = (One + Two),\n"
1995                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1996                      "  Five = (One, Two, Three, Four, 5)\n"
1997                      "};");
1998   verifyFormat("enum Enum {};");
1999   verifyFormat("enum {};");
2000   verifyFormat("enum X E {} d;");
2001   verifyFormat("enum __attribute__((...)) E {} d;");
2002   verifyFormat("enum __declspec__((...)) E {} d;");
2003   verifyFormat("enum {\n"
2004                "  Bar = Foo<int, int>::value\n"
2005                "};",
2006                getLLVMStyleWithColumns(30));
2007 
2008   verifyFormat("enum ShortEnum { A, B, C };");
2009   verifyGoogleFormat("enum ShortEnum { A, B, C };");
2010 
2011   EXPECT_EQ("enum KeepEmptyLines {\n"
2012             "  ONE,\n"
2013             "\n"
2014             "  TWO,\n"
2015             "\n"
2016             "  THREE\n"
2017             "}",
2018             format("enum KeepEmptyLines {\n"
2019                    "  ONE,\n"
2020                    "\n"
2021                    "  TWO,\n"
2022                    "\n"
2023                    "\n"
2024                    "  THREE\n"
2025                    "}"));
2026   verifyFormat("enum E { // comment\n"
2027                "  ONE,\n"
2028                "  TWO\n"
2029                "};\n"
2030                "int i;");
2031   // Not enums.
2032   verifyFormat("enum X f() {\n"
2033                "  a();\n"
2034                "  return 42;\n"
2035                "}");
2036   verifyFormat("enum X Type::f() {\n"
2037                "  a();\n"
2038                "  return 42;\n"
2039                "}");
2040   verifyFormat("enum ::X f() {\n"
2041                "  a();\n"
2042                "  return 42;\n"
2043                "}");
2044   verifyFormat("enum ns::X f() {\n"
2045                "  a();\n"
2046                "  return 42;\n"
2047                "}");
2048 }
2049 
2050 TEST_F(FormatTest, FormatsEnumsWithErrors) {
2051   verifyFormat("enum Type {\n"
2052                "  One = 0; // These semicolons should be commas.\n"
2053                "  Two = 1;\n"
2054                "};");
2055   verifyFormat("namespace n {\n"
2056                "enum Type {\n"
2057                "  One,\n"
2058                "  Two, // missing };\n"
2059                "  int i;\n"
2060                "}\n"
2061                "void g() {}");
2062 }
2063 
2064 TEST_F(FormatTest, FormatsEnumStruct) {
2065   verifyFormat("enum struct {\n"
2066                "  Zero,\n"
2067                "  One = 1,\n"
2068                "  Two = One + 1,\n"
2069                "  Three = (One + Two),\n"
2070                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2071                "  Five = (One, Two, Three, Four, 5)\n"
2072                "};");
2073   verifyFormat("enum struct Enum {};");
2074   verifyFormat("enum struct {};");
2075   verifyFormat("enum struct X E {} d;");
2076   verifyFormat("enum struct __attribute__((...)) E {} d;");
2077   verifyFormat("enum struct __declspec__((...)) E {} d;");
2078   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
2079 }
2080 
2081 TEST_F(FormatTest, FormatsEnumClass) {
2082   verifyFormat("enum class {\n"
2083                "  Zero,\n"
2084                "  One = 1,\n"
2085                "  Two = One + 1,\n"
2086                "  Three = (One + Two),\n"
2087                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2088                "  Five = (One, Two, Three, Four, 5)\n"
2089                "};");
2090   verifyFormat("enum class Enum {};");
2091   verifyFormat("enum class {};");
2092   verifyFormat("enum class X E {} d;");
2093   verifyFormat("enum class __attribute__((...)) E {} d;");
2094   verifyFormat("enum class __declspec__((...)) E {} d;");
2095   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
2096 }
2097 
2098 TEST_F(FormatTest, FormatsEnumTypes) {
2099   verifyFormat("enum X : int {\n"
2100                "  A, // Force multiple lines.\n"
2101                "  B\n"
2102                "};");
2103   verifyFormat("enum X : int { A, B };");
2104   verifyFormat("enum X : std::uint32_t { A, B };");
2105 }
2106 
2107 TEST_F(FormatTest, FormatsNSEnums) {
2108   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
2109   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
2110                      "  // Information about someDecentlyLongValue.\n"
2111                      "  someDecentlyLongValue,\n"
2112                      "  // Information about anotherDecentlyLongValue.\n"
2113                      "  anotherDecentlyLongValue,\n"
2114                      "  // Information about aThirdDecentlyLongValue.\n"
2115                      "  aThirdDecentlyLongValue\n"
2116                      "};");
2117   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
2118                      "  a = 1,\n"
2119                      "  b = 2,\n"
2120                      "  c = 3,\n"
2121                      "};");
2122   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
2123                      "  a = 1,\n"
2124                      "  b = 2,\n"
2125                      "  c = 3,\n"
2126                      "};");
2127   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
2128                      "  a = 1,\n"
2129                      "  b = 2,\n"
2130                      "  c = 3,\n"
2131                      "};");
2132 }
2133 
2134 TEST_F(FormatTest, FormatsBitfields) {
2135   verifyFormat("struct Bitfields {\n"
2136                "  unsigned sClass : 8;\n"
2137                "  unsigned ValueKind : 2;\n"
2138                "};");
2139   verifyFormat("struct A {\n"
2140                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
2141                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
2142                "};");
2143   verifyFormat("struct MyStruct {\n"
2144                "  uchar data;\n"
2145                "  uchar : 8;\n"
2146                "  uchar : 8;\n"
2147                "  uchar other;\n"
2148                "};");
2149 }
2150 
2151 TEST_F(FormatTest, FormatsNamespaces) {
2152   verifyFormat("namespace some_namespace {\n"
2153                "class A {};\n"
2154                "void f() { f(); }\n"
2155                "}");
2156   verifyFormat("namespace {\n"
2157                "class A {};\n"
2158                "void f() { f(); }\n"
2159                "}");
2160   verifyFormat("inline namespace X {\n"
2161                "class A {};\n"
2162                "void f() { f(); }\n"
2163                "}");
2164   verifyFormat("using namespace some_namespace;\n"
2165                "class A {};\n"
2166                "void f() { f(); }");
2167 
2168   // This code is more common than we thought; if we
2169   // layout this correctly the semicolon will go into
2170   // its own line, which is undesirable.
2171   verifyFormat("namespace {};");
2172   verifyFormat("namespace {\n"
2173                "class A {};\n"
2174                "};");
2175 
2176   verifyFormat("namespace {\n"
2177                "int SomeVariable = 0; // comment\n"
2178                "} // namespace");
2179   EXPECT_EQ("#ifndef HEADER_GUARD\n"
2180             "#define HEADER_GUARD\n"
2181             "namespace my_namespace {\n"
2182             "int i;\n"
2183             "} // my_namespace\n"
2184             "#endif // HEADER_GUARD",
2185             format("#ifndef HEADER_GUARD\n"
2186                    " #define HEADER_GUARD\n"
2187                    "   namespace my_namespace {\n"
2188                    "int i;\n"
2189                    "}    // my_namespace\n"
2190                    "#endif    // HEADER_GUARD"));
2191 
2192   FormatStyle Style = getLLVMStyle();
2193   Style.NamespaceIndentation = FormatStyle::NI_All;
2194   EXPECT_EQ("namespace out {\n"
2195             "  int i;\n"
2196             "  namespace in {\n"
2197             "    int i;\n"
2198             "  } // namespace\n"
2199             "} // namespace",
2200             format("namespace out {\n"
2201                    "int i;\n"
2202                    "namespace in {\n"
2203                    "int i;\n"
2204                    "} // namespace\n"
2205                    "} // namespace",
2206                    Style));
2207 
2208   Style.NamespaceIndentation = FormatStyle::NI_Inner;
2209   EXPECT_EQ("namespace out {\n"
2210             "int i;\n"
2211             "namespace in {\n"
2212             "  int i;\n"
2213             "} // namespace\n"
2214             "} // namespace",
2215             format("namespace out {\n"
2216                    "int i;\n"
2217                    "namespace in {\n"
2218                    "int i;\n"
2219                    "} // namespace\n"
2220                    "} // namespace",
2221                    Style));
2222 }
2223 
2224 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
2225 
2226 TEST_F(FormatTest, FormatsInlineASM) {
2227   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
2228   verifyFormat("asm(\"nop\" ::: \"memory\");");
2229   verifyFormat(
2230       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
2231       "    \"cpuid\\n\\t\"\n"
2232       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
2233       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
2234       "    : \"a\"(value));");
2235   EXPECT_EQ(
2236       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
2237       "  __asm {\n"
2238       "        mov     edx,[that] // vtable in edx\n"
2239       "        mov     eax,methodIndex\n"
2240       "        call    [edx][eax*4] // stdcall\n"
2241       "  }\n"
2242       "}",
2243       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
2244              "    __asm {\n"
2245              "        mov     edx,[that] // vtable in edx\n"
2246              "        mov     eax,methodIndex\n"
2247              "        call    [edx][eax*4] // stdcall\n"
2248              "    }\n"
2249              "}"));
2250   EXPECT_EQ("_asm {\n"
2251             "  xor eax, eax;\n"
2252             "  cpuid;\n"
2253             "}",
2254             format("_asm {\n"
2255                    "  xor eax, eax;\n"
2256                    "  cpuid;\n"
2257                    "}"));
2258   verifyFormat("void function() {\n"
2259                "  // comment\n"
2260                "  asm(\"\");\n"
2261                "}");
2262   EXPECT_EQ("__asm {\n"
2263             "}\n"
2264             "int i;",
2265             format("__asm   {\n"
2266                    "}\n"
2267                    "int   i;"));
2268 }
2269 
2270 TEST_F(FormatTest, FormatTryCatch) {
2271   verifyFormat("try {\n"
2272                "  throw a * b;\n"
2273                "} catch (int a) {\n"
2274                "  // Do nothing.\n"
2275                "} catch (...) {\n"
2276                "  exit(42);\n"
2277                "}");
2278 
2279   // Function-level try statements.
2280   verifyFormat("int f() try { return 4; } catch (...) {\n"
2281                "  return 5;\n"
2282                "}");
2283   verifyFormat("class A {\n"
2284                "  int a;\n"
2285                "  A() try : a(0) {\n"
2286                "  } catch (...) {\n"
2287                "    throw;\n"
2288                "  }\n"
2289                "};\n");
2290 
2291   // Incomplete try-catch blocks.
2292   verifyIncompleteFormat("try {} catch (");
2293 }
2294 
2295 TEST_F(FormatTest, FormatSEHTryCatch) {
2296   verifyFormat("__try {\n"
2297                "  int a = b * c;\n"
2298                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
2299                "  // Do nothing.\n"
2300                "}");
2301 
2302   verifyFormat("__try {\n"
2303                "  int a = b * c;\n"
2304                "} __finally {\n"
2305                "  // Do nothing.\n"
2306                "}");
2307 
2308   verifyFormat("DEBUG({\n"
2309                "  __try {\n"
2310                "  } __finally {\n"
2311                "  }\n"
2312                "});\n");
2313 }
2314 
2315 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
2316   verifyFormat("try {\n"
2317                "  f();\n"
2318                "} catch {\n"
2319                "  g();\n"
2320                "}");
2321   verifyFormat("try {\n"
2322                "  f();\n"
2323                "} catch (A a) MACRO(x) {\n"
2324                "  g();\n"
2325                "} catch (B b) MACRO(x) {\n"
2326                "  g();\n"
2327                "}");
2328 }
2329 
2330 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
2331   FormatStyle Style = getLLVMStyle();
2332   for (auto BraceStyle :
2333        {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, FormatStyle::BS_WebKit}) {
2334     Style.BreakBeforeBraces = BraceStyle;
2335     verifyFormat("try {\n"
2336                  "  // something\n"
2337                  "} catch (...) {\n"
2338                  "  // something\n"
2339                  "}",
2340                  Style);
2341   }
2342   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
2343   verifyFormat("try {\n"
2344                "  // something\n"
2345                "}\n"
2346                "catch (...) {\n"
2347                "  // something\n"
2348                "}",
2349                Style);
2350   verifyFormat("__try {\n"
2351                "  // something\n"
2352                "}\n"
2353                "__finally {\n"
2354                "  // something\n"
2355                "}",
2356                Style);
2357   verifyFormat("@try {\n"
2358                "  // something\n"
2359                "}\n"
2360                "@finally {\n"
2361                "  // something\n"
2362                "}",
2363                Style);
2364   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2365   verifyFormat("try\n"
2366                "{\n"
2367                "  // something\n"
2368                "}\n"
2369                "catch (...)\n"
2370                "{\n"
2371                "  // something\n"
2372                "}",
2373                Style);
2374   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
2375   verifyFormat("try\n"
2376                "  {\n"
2377                "    // something\n"
2378                "  }\n"
2379                "catch (...)\n"
2380                "  {\n"
2381                "    // something\n"
2382                "  }",
2383                Style);
2384 }
2385 
2386 TEST_F(FormatTest, FormatObjCTryCatch) {
2387   verifyFormat("@try {\n"
2388                "  f();\n"
2389                "} @catch (NSException e) {\n"
2390                "  @throw;\n"
2391                "} @finally {\n"
2392                "  exit(42);\n"
2393                "}");
2394   verifyFormat("DEBUG({\n"
2395                "  @try {\n"
2396                "  } @finally {\n"
2397                "  }\n"
2398                "});\n");
2399 }
2400 
2401 TEST_F(FormatTest, FormatObjCAutoreleasepool) {
2402   FormatStyle Style = getLLVMStyle();
2403   verifyFormat("@autoreleasepool {\n"
2404                "  f();\n"
2405                "}\n"
2406                "@autoreleasepool {\n"
2407                "  f();\n"
2408                "}\n",
2409                Style);
2410   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2411   verifyFormat("@autoreleasepool\n"
2412                "{\n"
2413                "  f();\n"
2414                "}\n"
2415                "@autoreleasepool\n"
2416                "{\n"
2417                "  f();\n"
2418                "}\n",
2419                Style);
2420 }
2421 
2422 TEST_F(FormatTest, StaticInitializers) {
2423   verifyFormat("static SomeClass SC = {1, 'a'};");
2424 
2425   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
2426                "    100000000, "
2427                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
2428 
2429   // Here, everything other than the "}" would fit on a line.
2430   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
2431                "    10000000000000000000000000};");
2432   EXPECT_EQ("S s = {a,\n"
2433             "\n"
2434             "       b};",
2435             format("S s = {\n"
2436                    "  a,\n"
2437                    "\n"
2438                    "  b\n"
2439                    "};"));
2440 
2441   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
2442   // line. However, the formatting looks a bit off and this probably doesn't
2443   // happen often in practice.
2444   verifyFormat("static int Variable[1] = {\n"
2445                "    {1000000000000000000000000000000000000}};",
2446                getLLVMStyleWithColumns(40));
2447 }
2448 
2449 TEST_F(FormatTest, DesignatedInitializers) {
2450   verifyFormat("const struct A a = {.a = 1, .b = 2};");
2451   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
2452                "                    .bbbbbbbbbb = 2,\n"
2453                "                    .cccccccccc = 3,\n"
2454                "                    .dddddddddd = 4,\n"
2455                "                    .eeeeeeeeee = 5};");
2456   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2457                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
2458                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
2459                "    .ccccccccccccccccccccccccccc = 3,\n"
2460                "    .ddddddddddddddddddddddddddd = 4,\n"
2461                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
2462 
2463   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
2464 }
2465 
2466 TEST_F(FormatTest, NestedStaticInitializers) {
2467   verifyFormat("static A x = {{{}}};\n");
2468   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
2469                "               {init1, init2, init3, init4}}};",
2470                getLLVMStyleWithColumns(50));
2471 
2472   verifyFormat("somes Status::global_reps[3] = {\n"
2473                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2474                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2475                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
2476                getLLVMStyleWithColumns(60));
2477   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
2478                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2479                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2480                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
2481   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
2482                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
2483                "rect.fTop}};");
2484 
2485   verifyFormat(
2486       "SomeArrayOfSomeType a = {\n"
2487       "    {{1, 2, 3},\n"
2488       "     {1, 2, 3},\n"
2489       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
2490       "      333333333333333333333333333333},\n"
2491       "     {1, 2, 3},\n"
2492       "     {1, 2, 3}}};");
2493   verifyFormat(
2494       "SomeArrayOfSomeType a = {\n"
2495       "    {{1, 2, 3}},\n"
2496       "    {{1, 2, 3}},\n"
2497       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
2498       "      333333333333333333333333333333}},\n"
2499       "    {{1, 2, 3}},\n"
2500       "    {{1, 2, 3}}};");
2501 
2502   verifyFormat("struct {\n"
2503                "  unsigned bit;\n"
2504                "  const char *const name;\n"
2505                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
2506                "                 {kOsWin, \"Windows\"},\n"
2507                "                 {kOsLinux, \"Linux\"},\n"
2508                "                 {kOsCrOS, \"Chrome OS\"}};");
2509   verifyFormat("struct {\n"
2510                "  unsigned bit;\n"
2511                "  const char *const name;\n"
2512                "} kBitsToOs[] = {\n"
2513                "    {kOsMac, \"Mac\"},\n"
2514                "    {kOsWin, \"Windows\"},\n"
2515                "    {kOsLinux, \"Linux\"},\n"
2516                "    {kOsCrOS, \"Chrome OS\"},\n"
2517                "};");
2518 }
2519 
2520 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
2521   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2522                "                      \\\n"
2523                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
2524 }
2525 
2526 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
2527   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
2528                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
2529 
2530   // Do break defaulted and deleted functions.
2531   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2532                "    default;",
2533                getLLVMStyleWithColumns(40));
2534   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2535                "    delete;",
2536                getLLVMStyleWithColumns(40));
2537 }
2538 
2539 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
2540   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
2541                getLLVMStyleWithColumns(40));
2542   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2543                getLLVMStyleWithColumns(40));
2544   EXPECT_EQ("#define Q                              \\\n"
2545             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
2546             "  \"aaaaaaaa.cpp\"",
2547             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2548                    getLLVMStyleWithColumns(40)));
2549 }
2550 
2551 TEST_F(FormatTest, UnderstandsLinePPDirective) {
2552   EXPECT_EQ("# 123 \"A string literal\"",
2553             format("   #     123    \"A string literal\""));
2554 }
2555 
2556 TEST_F(FormatTest, LayoutUnknownPPDirective) {
2557   EXPECT_EQ("#;", format("#;"));
2558   verifyFormat("#\n;\n;\n;");
2559 }
2560 
2561 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
2562   EXPECT_EQ("#line 42 \"test\"\n",
2563             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
2564   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
2565                                     getLLVMStyleWithColumns(12)));
2566 }
2567 
2568 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
2569   EXPECT_EQ("#line 42 \"test\"",
2570             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
2571   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
2572 }
2573 
2574 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
2575   verifyFormat("#define A \\x20");
2576   verifyFormat("#define A \\ x20");
2577   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
2578   verifyFormat("#define A ''");
2579   verifyFormat("#define A ''qqq");
2580   verifyFormat("#define A `qqq");
2581   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
2582   EXPECT_EQ("const char *c = STRINGIFY(\n"
2583             "\\na : b);",
2584             format("const char * c = STRINGIFY(\n"
2585                    "\\na : b);"));
2586 
2587   verifyFormat("a\r\\");
2588   verifyFormat("a\v\\");
2589   verifyFormat("a\f\\");
2590 }
2591 
2592 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
2593   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
2594   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
2595   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
2596   // FIXME: We never break before the macro name.
2597   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
2598 
2599   verifyFormat("#define A A\n#define A A");
2600   verifyFormat("#define A(X) A\n#define A A");
2601 
2602   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
2603   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
2604 }
2605 
2606 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
2607   EXPECT_EQ("// somecomment\n"
2608             "#include \"a.h\"\n"
2609             "#define A(  \\\n"
2610             "    A, B)\n"
2611             "#include \"b.h\"\n"
2612             "// somecomment\n",
2613             format("  // somecomment\n"
2614                    "  #include \"a.h\"\n"
2615                    "#define A(A,\\\n"
2616                    "    B)\n"
2617                    "    #include \"b.h\"\n"
2618                    " // somecomment\n",
2619                    getLLVMStyleWithColumns(13)));
2620 }
2621 
2622 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
2623 
2624 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
2625   EXPECT_EQ("#define A    \\\n"
2626             "  c;         \\\n"
2627             "  e;\n"
2628             "f;",
2629             format("#define A c; e;\n"
2630                    "f;",
2631                    getLLVMStyleWithColumns(14)));
2632 }
2633 
2634 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
2635 
2636 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
2637   EXPECT_EQ("int x,\n"
2638             "#define A\n"
2639             "    y;",
2640             format("int x,\n#define A\ny;"));
2641 }
2642 
2643 TEST_F(FormatTest, HashInMacroDefinition) {
2644   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
2645   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
2646   verifyFormat("#define A  \\\n"
2647                "  {        \\\n"
2648                "    f(#c); \\\n"
2649                "  }",
2650                getLLVMStyleWithColumns(11));
2651 
2652   verifyFormat("#define A(X)         \\\n"
2653                "  void function##X()",
2654                getLLVMStyleWithColumns(22));
2655 
2656   verifyFormat("#define A(a, b, c)   \\\n"
2657                "  void a##b##c()",
2658                getLLVMStyleWithColumns(22));
2659 
2660   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
2661 }
2662 
2663 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
2664   EXPECT_EQ("#define A (x)", format("#define A (x)"));
2665   EXPECT_EQ("#define A(x)", format("#define A(x)"));
2666 }
2667 
2668 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
2669   EXPECT_EQ("#define A b;", format("#define A \\\n"
2670                                    "          \\\n"
2671                                    "  b;",
2672                                    getLLVMStyleWithColumns(25)));
2673   EXPECT_EQ("#define A \\\n"
2674             "          \\\n"
2675             "  a;      \\\n"
2676             "  b;",
2677             format("#define A \\\n"
2678                    "          \\\n"
2679                    "  a;      \\\n"
2680                    "  b;",
2681                    getLLVMStyleWithColumns(11)));
2682   EXPECT_EQ("#define A \\\n"
2683             "  a;      \\\n"
2684             "          \\\n"
2685             "  b;",
2686             format("#define A \\\n"
2687                    "  a;      \\\n"
2688                    "          \\\n"
2689                    "  b;",
2690                    getLLVMStyleWithColumns(11)));
2691 }
2692 
2693 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
2694   verifyIncompleteFormat("#define A :");
2695   verifyFormat("#define SOMECASES  \\\n"
2696                "  case 1:          \\\n"
2697                "  case 2\n",
2698                getLLVMStyleWithColumns(20));
2699   verifyFormat("#define A template <typename T>");
2700   verifyIncompleteFormat("#define STR(x) #x\n"
2701                          "f(STR(this_is_a_string_literal{));");
2702   verifyFormat("#pragma omp threadprivate( \\\n"
2703                "    y)), // expected-warning",
2704                getLLVMStyleWithColumns(28));
2705   verifyFormat("#d, = };");
2706   verifyFormat("#if \"a");
2707   verifyIncompleteFormat("({\n"
2708                          "#define b     \\\n"
2709                          "  }           \\\n"
2710                          "  a\n"
2711                          "a",
2712                          getLLVMStyleWithColumns(15));
2713   verifyFormat("#define A     \\\n"
2714                "  {           \\\n"
2715                "    {\n"
2716                "#define B     \\\n"
2717                "  }           \\\n"
2718                "  }",
2719                getLLVMStyleWithColumns(15));
2720   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2721   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2722   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2723   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
2724 }
2725 
2726 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2727   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2728   EXPECT_EQ("class A : public QObject {\n"
2729             "  Q_OBJECT\n"
2730             "\n"
2731             "  A() {}\n"
2732             "};",
2733             format("class A  :  public QObject {\n"
2734                    "     Q_OBJECT\n"
2735                    "\n"
2736                    "  A() {\n}\n"
2737                    "}  ;"));
2738   EXPECT_EQ("MACRO\n"
2739             "/*static*/ int i;",
2740             format("MACRO\n"
2741                    " /*static*/ int   i;"));
2742   EXPECT_EQ("SOME_MACRO\n"
2743             "namespace {\n"
2744             "void f();\n"
2745             "}",
2746             format("SOME_MACRO\n"
2747                    "  namespace    {\n"
2748                    "void   f(  );\n"
2749                    "}"));
2750   // Only if the identifier contains at least 5 characters.
2751   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
2752   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
2753   // Only if everything is upper case.
2754   EXPECT_EQ("class A : public QObject {\n"
2755             "  Q_Object A() {}\n"
2756             "};",
2757             format("class A  :  public QObject {\n"
2758                    "     Q_Object\n"
2759                    "  A() {\n}\n"
2760                    "}  ;"));
2761 
2762   // Only if the next line can actually start an unwrapped line.
2763   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2764             format("SOME_WEIRD_LOG_MACRO\n"
2765                    "<< SomeThing;"));
2766 
2767   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2768                "(n, buffers))\n",
2769                getChromiumStyle(FormatStyle::LK_Cpp));
2770 }
2771 
2772 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2773   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2774             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2775             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2776             "class X {};\n"
2777             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2778             "int *createScopDetectionPass() { return 0; }",
2779             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2780                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2781                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2782                    "  class X {};\n"
2783                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2784                    "  int *createScopDetectionPass() { return 0; }"));
2785   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2786   // braces, so that inner block is indented one level more.
2787   EXPECT_EQ("int q() {\n"
2788             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2789             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2790             "  IPC_END_MESSAGE_MAP()\n"
2791             "}",
2792             format("int q() {\n"
2793                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2794                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2795                    "  IPC_END_MESSAGE_MAP()\n"
2796                    "}"));
2797 
2798   // Same inside macros.
2799   EXPECT_EQ("#define LIST(L) \\\n"
2800             "  L(A)          \\\n"
2801             "  L(B)          \\\n"
2802             "  L(C)",
2803             format("#define LIST(L) \\\n"
2804                    "  L(A) \\\n"
2805                    "  L(B) \\\n"
2806                    "  L(C)",
2807                    getGoogleStyle()));
2808 
2809   // These must not be recognized as macros.
2810   EXPECT_EQ("int q() {\n"
2811             "  f(x);\n"
2812             "  f(x) {}\n"
2813             "  f(x)->g();\n"
2814             "  f(x)->*g();\n"
2815             "  f(x).g();\n"
2816             "  f(x) = x;\n"
2817             "  f(x) += x;\n"
2818             "  f(x) -= x;\n"
2819             "  f(x) *= x;\n"
2820             "  f(x) /= x;\n"
2821             "  f(x) %= x;\n"
2822             "  f(x) &= x;\n"
2823             "  f(x) |= x;\n"
2824             "  f(x) ^= x;\n"
2825             "  f(x) >>= x;\n"
2826             "  f(x) <<= x;\n"
2827             "  f(x)[y].z();\n"
2828             "  LOG(INFO) << x;\n"
2829             "  ifstream(x) >> x;\n"
2830             "}\n",
2831             format("int q() {\n"
2832                    "  f(x)\n;\n"
2833                    "  f(x)\n {}\n"
2834                    "  f(x)\n->g();\n"
2835                    "  f(x)\n->*g();\n"
2836                    "  f(x)\n.g();\n"
2837                    "  f(x)\n = x;\n"
2838                    "  f(x)\n += x;\n"
2839                    "  f(x)\n -= x;\n"
2840                    "  f(x)\n *= x;\n"
2841                    "  f(x)\n /= x;\n"
2842                    "  f(x)\n %= x;\n"
2843                    "  f(x)\n &= x;\n"
2844                    "  f(x)\n |= x;\n"
2845                    "  f(x)\n ^= x;\n"
2846                    "  f(x)\n >>= x;\n"
2847                    "  f(x)\n <<= x;\n"
2848                    "  f(x)\n[y].z();\n"
2849                    "  LOG(INFO)\n << x;\n"
2850                    "  ifstream(x)\n >> x;\n"
2851                    "}\n"));
2852   EXPECT_EQ("int q() {\n"
2853             "  F(x)\n"
2854             "  if (1) {\n"
2855             "  }\n"
2856             "  F(x)\n"
2857             "  while (1) {\n"
2858             "  }\n"
2859             "  F(x)\n"
2860             "  G(x);\n"
2861             "  F(x)\n"
2862             "  try {\n"
2863             "    Q();\n"
2864             "  } catch (...) {\n"
2865             "  }\n"
2866             "}\n",
2867             format("int q() {\n"
2868                    "F(x)\n"
2869                    "if (1) {}\n"
2870                    "F(x)\n"
2871                    "while (1) {}\n"
2872                    "F(x)\n"
2873                    "G(x);\n"
2874                    "F(x)\n"
2875                    "try { Q(); } catch (...) {}\n"
2876                    "}\n"));
2877   EXPECT_EQ("class A {\n"
2878             "  A() : t(0) {}\n"
2879             "  A(int i) noexcept() : {}\n"
2880             "  A(X x)\n" // FIXME: function-level try blocks are broken.
2881             "  try : t(0) {\n"
2882             "  } catch (...) {\n"
2883             "  }\n"
2884             "};",
2885             format("class A {\n"
2886                    "  A()\n : t(0) {}\n"
2887                    "  A(int i)\n noexcept() : {}\n"
2888                    "  A(X x)\n"
2889                    "  try : t(0) {} catch (...) {}\n"
2890                    "};"));
2891   EXPECT_EQ("class SomeClass {\n"
2892             "public:\n"
2893             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2894             "};",
2895             format("class SomeClass {\n"
2896                    "public:\n"
2897                    "  SomeClass()\n"
2898                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2899                    "};"));
2900   EXPECT_EQ("class SomeClass {\n"
2901             "public:\n"
2902             "  SomeClass()\n"
2903             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2904             "};",
2905             format("class SomeClass {\n"
2906                    "public:\n"
2907                    "  SomeClass()\n"
2908                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2909                    "};",
2910                    getLLVMStyleWithColumns(40)));
2911 }
2912 
2913 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2914   verifyFormat("#define A \\\n"
2915                "  f({     \\\n"
2916                "    g();  \\\n"
2917                "  });",
2918                getLLVMStyleWithColumns(11));
2919 }
2920 
2921 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
2922   EXPECT_EQ("{\n  {\n#define A\n  }\n}", format("{{\n#define A\n}}"));
2923 }
2924 
2925 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
2926   verifyFormat("{\n  { a #c; }\n}");
2927 }
2928 
2929 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
2930   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
2931             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
2932   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
2933             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
2934 }
2935 
2936 TEST_F(FormatTest, EscapedNewlines) {
2937   EXPECT_EQ(
2938       "#define A \\\n  int i;  \\\n  int j;",
2939       format("#define A \\\nint i;\\\n  int j;", getLLVMStyleWithColumns(11)));
2940   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
2941   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2942   EXPECT_EQ("/* \\  \\  \\\n*/", format("\\\n/* \\  \\  \\\n*/"));
2943   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
2944 }
2945 
2946 TEST_F(FormatTest, DontCrashOnBlockComments) {
2947   EXPECT_EQ(
2948       "int xxxxxxxxx; /* "
2949       "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
2950       "zzzzzz\n"
2951       "0*/",
2952       format("int xxxxxxxxx;                          /* "
2953              "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
2954              "0*/"));
2955 }
2956 
2957 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
2958   verifyFormat("#define A \\\n"
2959                "  int v(  \\\n"
2960                "      a); \\\n"
2961                "  int i;",
2962                getLLVMStyleWithColumns(11));
2963 }
2964 
2965 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
2966   EXPECT_EQ(
2967       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2968       "                      \\\n"
2969       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2970       "\n"
2971       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2972       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
2973       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
2974              "\\\n"
2975              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2976              "  \n"
2977              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2978              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
2979 }
2980 
2981 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
2982   EXPECT_EQ("int\n"
2983             "#define A\n"
2984             "    a;",
2985             format("int\n#define A\na;"));
2986   verifyFormat("functionCallTo(\n"
2987                "    someOtherFunction(\n"
2988                "        withSomeParameters, whichInSequence,\n"
2989                "        areLongerThanALine(andAnotherCall,\n"
2990                "#define A B\n"
2991                "                           withMoreParamters,\n"
2992                "                           whichStronglyInfluenceTheLayout),\n"
2993                "        andMoreParameters),\n"
2994                "    trailing);",
2995                getLLVMStyleWithColumns(69));
2996   verifyFormat("Foo::Foo()\n"
2997                "#ifdef BAR\n"
2998                "    : baz(0)\n"
2999                "#endif\n"
3000                "{\n"
3001                "}");
3002   verifyFormat("void f() {\n"
3003                "  if (true)\n"
3004                "#ifdef A\n"
3005                "    f(42);\n"
3006                "  x();\n"
3007                "#else\n"
3008                "    g();\n"
3009                "  x();\n"
3010                "#endif\n"
3011                "}");
3012   verifyFormat("void f(param1, param2,\n"
3013                "       param3,\n"
3014                "#ifdef A\n"
3015                "       param4(param5,\n"
3016                "#ifdef A1\n"
3017                "              param6,\n"
3018                "#ifdef A2\n"
3019                "              param7),\n"
3020                "#else\n"
3021                "              param8),\n"
3022                "       param9,\n"
3023                "#endif\n"
3024                "       param10,\n"
3025                "#endif\n"
3026                "       param11)\n"
3027                "#else\n"
3028                "       param12)\n"
3029                "#endif\n"
3030                "{\n"
3031                "  x();\n"
3032                "}",
3033                getLLVMStyleWithColumns(28));
3034   verifyFormat("#if 1\n"
3035                "int i;");
3036   verifyFormat("#if 1\n"
3037                "#endif\n"
3038                "#if 1\n"
3039                "#else\n"
3040                "#endif\n");
3041   verifyFormat("DEBUG({\n"
3042                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3043                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
3044                "});\n"
3045                "#if a\n"
3046                "#else\n"
3047                "#endif");
3048 
3049   verifyIncompleteFormat("void f(\n"
3050                          "#if A\n"
3051                          "    );\n"
3052                          "#else\n"
3053                          "#endif");
3054 }
3055 
3056 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
3057   verifyFormat("#endif\n"
3058                "#if B");
3059 }
3060 
3061 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
3062   FormatStyle SingleLine = getLLVMStyle();
3063   SingleLine.AllowShortIfStatementsOnASingleLine = true;
3064   verifyFormat("#if 0\n"
3065                "#elif 1\n"
3066                "#endif\n"
3067                "void foo() {\n"
3068                "  if (test) foo2();\n"
3069                "}",
3070                SingleLine);
3071 }
3072 
3073 TEST_F(FormatTest, LayoutBlockInsideParens) {
3074   verifyFormat("functionCall({ int i; });");
3075   verifyFormat("functionCall({\n"
3076                "  int i;\n"
3077                "  int j;\n"
3078                "});");
3079   verifyFormat("functionCall({\n"
3080                "  int i;\n"
3081                "  int j;\n"
3082                "}, aaaa, bbbb, cccc);");
3083   verifyFormat("functionA(functionB({\n"
3084                "            int i;\n"
3085                "            int j;\n"
3086                "          }),\n"
3087                "          aaaa, bbbb, cccc);");
3088   verifyFormat("functionCall(\n"
3089                "    {\n"
3090                "      int i;\n"
3091                "      int j;\n"
3092                "    },\n"
3093                "    aaaa, bbbb, // comment\n"
3094                "    cccc);");
3095   verifyFormat("functionA(functionB({\n"
3096                "            int i;\n"
3097                "            int j;\n"
3098                "          }),\n"
3099                "          aaaa, bbbb, // comment\n"
3100                "          cccc);");
3101   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
3102   verifyFormat("functionCall(aaaa, bbbb, {\n"
3103                "  int i;\n"
3104                "  int j;\n"
3105                "});");
3106   verifyFormat(
3107       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
3108       "    {\n"
3109       "      int i; // break\n"
3110       "    },\n"
3111       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
3112       "                                     ccccccccccccccccc));");
3113   verifyFormat("DEBUG({\n"
3114                "  if (a)\n"
3115                "    f();\n"
3116                "});");
3117 }
3118 
3119 TEST_F(FormatTest, LayoutBlockInsideStatement) {
3120   EXPECT_EQ("SOME_MACRO { int i; }\n"
3121             "int i;",
3122             format("  SOME_MACRO  {int i;}  int i;"));
3123 }
3124 
3125 TEST_F(FormatTest, LayoutNestedBlocks) {
3126   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
3127                "  struct s {\n"
3128                "    int i;\n"
3129                "  };\n"
3130                "  s kBitsToOs[] = {{10}};\n"
3131                "  for (int i = 0; i < 10; ++i)\n"
3132                "    return;\n"
3133                "}");
3134   verifyFormat("call(parameter, {\n"
3135                "  something();\n"
3136                "  // Comment using all columns.\n"
3137                "  somethingelse();\n"
3138                "});",
3139                getLLVMStyleWithColumns(40));
3140   verifyFormat("DEBUG( //\n"
3141                "    { f(); }, a);");
3142   verifyFormat("DEBUG( //\n"
3143                "    {\n"
3144                "      f(); //\n"
3145                "    },\n"
3146                "    a);");
3147 
3148   EXPECT_EQ("call(parameter, {\n"
3149             "  something();\n"
3150             "  // Comment too\n"
3151             "  // looooooooooong.\n"
3152             "  somethingElse();\n"
3153             "});",
3154             format("call(parameter, {\n"
3155                    "  something();\n"
3156                    "  // Comment too looooooooooong.\n"
3157                    "  somethingElse();\n"
3158                    "});",
3159                    getLLVMStyleWithColumns(29)));
3160   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
3161   EXPECT_EQ("DEBUG({ // comment\n"
3162             "  int i;\n"
3163             "});",
3164             format("DEBUG({ // comment\n"
3165                    "int  i;\n"
3166                    "});"));
3167   EXPECT_EQ("DEBUG({\n"
3168             "  int i;\n"
3169             "\n"
3170             "  // comment\n"
3171             "  int j;\n"
3172             "});",
3173             format("DEBUG({\n"
3174                    "  int  i;\n"
3175                    "\n"
3176                    "  // comment\n"
3177                    "  int  j;\n"
3178                    "});"));
3179 
3180   verifyFormat("DEBUG({\n"
3181                "  if (a)\n"
3182                "    return;\n"
3183                "});");
3184   verifyGoogleFormat("DEBUG({\n"
3185                      "  if (a) return;\n"
3186                      "});");
3187   FormatStyle Style = getGoogleStyle();
3188   Style.ColumnLimit = 45;
3189   verifyFormat("Debug(aaaaa, {\n"
3190                "  if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
3191                "}, a);",
3192                Style);
3193 
3194   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
3195 
3196   verifyNoCrash("^{v^{a}}");
3197 }
3198 
3199 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
3200   EXPECT_EQ("#define MACRO()                     \\\n"
3201             "  Debug(aaa, /* force line break */ \\\n"
3202             "        {                           \\\n"
3203             "          int i;                    \\\n"
3204             "          int j;                    \\\n"
3205             "        })",
3206             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
3207                    "          {  int   i;  int  j;   })",
3208                    getGoogleStyle()));
3209 
3210   EXPECT_EQ("#define A                                       \\\n"
3211             "  [] {                                          \\\n"
3212             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
3213             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
3214             "  }",
3215             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
3216                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
3217                    getGoogleStyle()));
3218 }
3219 
3220 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
3221   EXPECT_EQ("{}", format("{}"));
3222   verifyFormat("enum E {};");
3223   verifyFormat("enum E {}");
3224 }
3225 
3226 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
3227   FormatStyle Style = getLLVMStyle();
3228   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
3229   Style.MacroBlockEnd = "^[A-Z_]+_END$";
3230   verifyFormat("FOO_BEGIN\n"
3231                "  FOO_ENTRY\n"
3232                "FOO_END", Style);
3233   verifyFormat("FOO_BEGIN\n"
3234                "  NESTED_FOO_BEGIN\n"
3235                "    NESTED_FOO_ENTRY\n"
3236                "  NESTED_FOO_END\n"
3237                "FOO_END", Style);
3238   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
3239                "  int x;\n"
3240                "  x = 1;\n"
3241                "FOO_END(Baz)", Style);
3242 }
3243 
3244 //===----------------------------------------------------------------------===//
3245 // Line break tests.
3246 //===----------------------------------------------------------------------===//
3247 
3248 TEST_F(FormatTest, PreventConfusingIndents) {
3249   verifyFormat(
3250       "void f() {\n"
3251       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
3252       "                         parameter, parameter, parameter)),\n"
3253       "                     SecondLongCall(parameter));\n"
3254       "}");
3255   verifyFormat(
3256       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3257       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3258       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3259       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
3260   verifyFormat(
3261       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3262       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
3263       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
3264       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
3265   verifyFormat(
3266       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3267       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
3268       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
3269       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
3270   verifyFormat("int a = bbbb && ccc && fffff(\n"
3271                "#define A Just forcing a new line\n"
3272                "                           ddd);");
3273 }
3274 
3275 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
3276   verifyFormat(
3277       "bool aaaaaaa =\n"
3278       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
3279       "    bbbbbbbb();");
3280   verifyFormat(
3281       "bool aaaaaaa =\n"
3282       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
3283       "    bbbbbbbb();");
3284 
3285   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3286                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
3287                "    ccccccccc == ddddddddddd;");
3288   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3289                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
3290                "    ccccccccc == ddddddddddd;");
3291   verifyFormat(
3292       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3293       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
3294       "    ccccccccc == ddddddddddd;");
3295 
3296   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3297                "                 aaaaaa) &&\n"
3298                "         bbbbbb && cccccc;");
3299   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3300                "                 aaaaaa) >>\n"
3301                "         bbbbbb;");
3302   verifyFormat("Whitespaces.addUntouchableComment(\n"
3303                "    SourceMgr.getSpellingColumnNumber(\n"
3304                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
3305                "    1);");
3306 
3307   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3308                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
3309                "    cccccc) {\n}");
3310   verifyFormat("b = a &&\n"
3311                "    // Comment\n"
3312                "    b.c && d;");
3313 
3314   // If the LHS of a comparison is not a binary expression itself, the
3315   // additional linebreak confuses many people.
3316   verifyFormat(
3317       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3318       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
3319       "}");
3320   verifyFormat(
3321       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3322       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3323       "}");
3324   verifyFormat(
3325       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
3326       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3327       "}");
3328   // Even explicit parentheses stress the precedence enough to make the
3329   // additional break unnecessary.
3330   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3331                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3332                "}");
3333   // This cases is borderline, but with the indentation it is still readable.
3334   verifyFormat(
3335       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3336       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3337       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
3338       "}",
3339       getLLVMStyleWithColumns(75));
3340 
3341   // If the LHS is a binary expression, we should still use the additional break
3342   // as otherwise the formatting hides the operator precedence.
3343   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3344                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3345                "    5) {\n"
3346                "}");
3347 
3348   FormatStyle OnePerLine = getLLVMStyle();
3349   OnePerLine.BinPackParameters = false;
3350   verifyFormat(
3351       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3352       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3353       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
3354       OnePerLine);
3355 }
3356 
3357 TEST_F(FormatTest, ExpressionIndentation) {
3358   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3359                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3360                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3361                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3362                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3363                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
3364                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3365                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
3366                "                 ccccccccccccccccccccccccccccccccccccccccc;");
3367   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3368                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3369                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3370                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3371   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3372                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3373                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3374                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3375   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3376                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3377                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3378                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3379   verifyFormat("if () {\n"
3380                "} else if (aaaaa &&\n"
3381                "           bbbbb > // break\n"
3382                "               ccccc) {\n"
3383                "}");
3384 
3385   // Presence of a trailing comment used to change indentation of b.
3386   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
3387                "       b;\n"
3388                "return aaaaaaaaaaaaaaaaaaa +\n"
3389                "       b; //",
3390                getLLVMStyleWithColumns(30));
3391 }
3392 
3393 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
3394   // Not sure what the best system is here. Like this, the LHS can be found
3395   // immediately above an operator (everything with the same or a higher
3396   // indent). The RHS is aligned right of the operator and so compasses
3397   // everything until something with the same indent as the operator is found.
3398   // FIXME: Is this a good system?
3399   FormatStyle Style = getLLVMStyle();
3400   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
3401   verifyFormat(
3402       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3403       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3404       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3405       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3406       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3407       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3408       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3409       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3410       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
3411       Style);
3412   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3413                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3414                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3415                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3416                Style);
3417   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3418                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3419                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3420                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3421                Style);
3422   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3423                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3424                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3425                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3426                Style);
3427   verifyFormat("if () {\n"
3428                "} else if (aaaaa\n"
3429                "           && bbbbb // break\n"
3430                "                  > ccccc) {\n"
3431                "}",
3432                Style);
3433   verifyFormat("return (a)\n"
3434                "       // comment\n"
3435                "       + b;",
3436                Style);
3437   verifyFormat(
3438       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3439       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3440       "             + cc;",
3441       Style);
3442 
3443   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3444                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3445                Style);
3446 
3447   // Forced by comments.
3448   verifyFormat(
3449       "unsigned ContentSize =\n"
3450       "    sizeof(int16_t)   // DWARF ARange version number\n"
3451       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
3452       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
3453       "    + sizeof(int8_t); // Segment Size (in bytes)");
3454 
3455   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
3456                "       == boost::fusion::at_c<1>(iiii).second;",
3457                Style);
3458 
3459   Style.ColumnLimit = 60;
3460   verifyFormat("zzzzzzzzzz\n"
3461                "    = bbbbbbbbbbbbbbbbb\n"
3462                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
3463                Style);
3464 }
3465 
3466 TEST_F(FormatTest, NoOperandAlignment) {
3467   FormatStyle Style = getLLVMStyle();
3468   Style.AlignOperands = false;
3469   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3470   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3471                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3472                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3473                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3474                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3475                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3476                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3477                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3478                "        > ccccccccccccccccccccccccccccccccccccccccc;",
3479                Style);
3480 
3481   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3482                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3483                "    + cc;",
3484                Style);
3485   verifyFormat("int a = aa\n"
3486                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3487                "        * cccccccccccccccccccccccccccccccccccc;",
3488                Style);
3489 
3490   Style.AlignAfterOpenBracket = false;
3491   verifyFormat("return (a > b\n"
3492                "    // comment1\n"
3493                "    // comment2\n"
3494                "    || c);",
3495                Style);
3496 }
3497 
3498 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
3499   FormatStyle Style = getLLVMStyle();
3500   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3501   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3502                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3503                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
3504                Style);
3505 }
3506 
3507 TEST_F(FormatTest, ConstructorInitializers) {
3508   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3509   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
3510                getLLVMStyleWithColumns(45));
3511   verifyFormat("Constructor()\n"
3512                "    : Inttializer(FitsOnTheLine) {}",
3513                getLLVMStyleWithColumns(44));
3514   verifyFormat("Constructor()\n"
3515                "    : Inttializer(FitsOnTheLine) {}",
3516                getLLVMStyleWithColumns(43));
3517 
3518   verifyFormat(
3519       "SomeClass::Constructor()\n"
3520       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3521 
3522   verifyFormat(
3523       "SomeClass::Constructor()\n"
3524       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3525       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
3526   verifyFormat(
3527       "SomeClass::Constructor()\n"
3528       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3529       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3530 
3531   verifyFormat("Constructor()\n"
3532                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3533                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3534                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3535                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
3536 
3537   verifyFormat("Constructor()\n"
3538                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3539                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3540 
3541   verifyFormat("Constructor(int Parameter = 0)\n"
3542                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3543                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
3544   verifyFormat("Constructor()\n"
3545                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3546                "}",
3547                getLLVMStyleWithColumns(60));
3548   verifyFormat("Constructor()\n"
3549                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3550                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
3551 
3552   // Here a line could be saved by splitting the second initializer onto two
3553   // lines, but that is not desirable.
3554   verifyFormat("Constructor()\n"
3555                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3556                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
3557                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3558 
3559   FormatStyle OnePerLine = getLLVMStyle();
3560   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3561   verifyFormat("SomeClass::Constructor()\n"
3562                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3563                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3564                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3565                OnePerLine);
3566   verifyFormat("SomeClass::Constructor()\n"
3567                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3568                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3569                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3570                OnePerLine);
3571   verifyFormat("MyClass::MyClass(int var)\n"
3572                "    : some_var_(var),            // 4 space indent\n"
3573                "      some_other_var_(var + 1) { // lined up\n"
3574                "}",
3575                OnePerLine);
3576   verifyFormat("Constructor()\n"
3577                "    : aaaaa(aaaaaa),\n"
3578                "      aaaaa(aaaaaa),\n"
3579                "      aaaaa(aaaaaa),\n"
3580                "      aaaaa(aaaaaa),\n"
3581                "      aaaaa(aaaaaa) {}",
3582                OnePerLine);
3583   verifyFormat("Constructor()\n"
3584                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3585                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
3586                OnePerLine);
3587 
3588   EXPECT_EQ("Constructor()\n"
3589             "    : // Comment forcing unwanted break.\n"
3590             "      aaaa(aaaa) {}",
3591             format("Constructor() :\n"
3592                    "    // Comment forcing unwanted break.\n"
3593                    "    aaaa(aaaa) {}"));
3594 }
3595 
3596 TEST_F(FormatTest, MemoizationTests) {
3597   // This breaks if the memoization lookup does not take \c Indent and
3598   // \c LastSpace into account.
3599   verifyFormat(
3600       "extern CFRunLoopTimerRef\n"
3601       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
3602       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
3603       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
3604       "                     CFRunLoopTimerContext *context) {}");
3605 
3606   // Deep nesting somewhat works around our memoization.
3607   verifyFormat(
3608       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3609       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3610       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3611       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3612       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
3613       getLLVMStyleWithColumns(65));
3614   verifyFormat(
3615       "aaaaa(\n"
3616       "    aaaaa,\n"
3617       "    aaaaa(\n"
3618       "        aaaaa,\n"
3619       "        aaaaa(\n"
3620       "            aaaaa,\n"
3621       "            aaaaa(\n"
3622       "                aaaaa,\n"
3623       "                aaaaa(\n"
3624       "                    aaaaa,\n"
3625       "                    aaaaa(\n"
3626       "                        aaaaa,\n"
3627       "                        aaaaa(\n"
3628       "                            aaaaa,\n"
3629       "                            aaaaa(\n"
3630       "                                aaaaa,\n"
3631       "                                aaaaa(\n"
3632       "                                    aaaaa,\n"
3633       "                                    aaaaa(\n"
3634       "                                        aaaaa,\n"
3635       "                                        aaaaa(\n"
3636       "                                            aaaaa,\n"
3637       "                                            aaaaa(\n"
3638       "                                                aaaaa,\n"
3639       "                                                aaaaa))))))))))));",
3640       getLLVMStyleWithColumns(65));
3641   verifyFormat(
3642       "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"
3643       "                                  a),\n"
3644       "                                a),\n"
3645       "                              a),\n"
3646       "                            a),\n"
3647       "                          a),\n"
3648       "                        a),\n"
3649       "                      a),\n"
3650       "                    a),\n"
3651       "                  a),\n"
3652       "                a),\n"
3653       "              a),\n"
3654       "            a),\n"
3655       "          a),\n"
3656       "        a),\n"
3657       "      a),\n"
3658       "    a),\n"
3659       "  a)",
3660       getLLVMStyleWithColumns(65));
3661 
3662   // This test takes VERY long when memoization is broken.
3663   FormatStyle OnePerLine = getLLVMStyle();
3664   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3665   OnePerLine.BinPackParameters = false;
3666   std::string input = "Constructor()\n"
3667                       "    : aaaa(a,\n";
3668   for (unsigned i = 0, e = 80; i != e; ++i) {
3669     input += "           a,\n";
3670   }
3671   input += "           a) {}";
3672   verifyFormat(input, OnePerLine);
3673 }
3674 
3675 TEST_F(FormatTest, BreaksAsHighAsPossible) {
3676   verifyFormat(
3677       "void f() {\n"
3678       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
3679       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
3680       "    f();\n"
3681       "}");
3682   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
3683                "    Intervals[i - 1].getRange().getLast()) {\n}");
3684 }
3685 
3686 TEST_F(FormatTest, BreaksFunctionDeclarations) {
3687   // Principially, we break function declarations in a certain order:
3688   // 1) break amongst arguments.
3689   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
3690                "                              Cccccccccccccc cccccccccccccc);");
3691   verifyFormat("template <class TemplateIt>\n"
3692                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
3693                "                            TemplateIt *stop) {}");
3694 
3695   // 2) break after return type.
3696   verifyFormat(
3697       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3698       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
3699       getGoogleStyle());
3700 
3701   // 3) break after (.
3702   verifyFormat(
3703       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
3704       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
3705       getGoogleStyle());
3706 
3707   // 4) break before after nested name specifiers.
3708   verifyFormat(
3709       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3710       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
3711       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
3712       getGoogleStyle());
3713 
3714   // However, there are exceptions, if a sufficient amount of lines can be
3715   // saved.
3716   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
3717   // more adjusting.
3718   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3719                "                                  Cccccccccccccc cccccccccc,\n"
3720                "                                  Cccccccccccccc cccccccccc,\n"
3721                "                                  Cccccccccccccc cccccccccc,\n"
3722                "                                  Cccccccccccccc cccccccccc);");
3723   verifyFormat(
3724       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3725       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3726       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3727       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
3728       getGoogleStyle());
3729   verifyFormat(
3730       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3731       "                                          Cccccccccccccc cccccccccc,\n"
3732       "                                          Cccccccccccccc cccccccccc,\n"
3733       "                                          Cccccccccccccc cccccccccc,\n"
3734       "                                          Cccccccccccccc cccccccccc,\n"
3735       "                                          Cccccccccccccc cccccccccc,\n"
3736       "                                          Cccccccccccccc cccccccccc);");
3737   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3738                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3739                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3740                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3741                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
3742 
3743   // Break after multi-line parameters.
3744   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3745                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3746                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3747                "    bbbb bbbb);");
3748   verifyFormat("void SomeLoooooooooooongFunction(\n"
3749                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
3750                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3751                "    int bbbbbbbbbbbbb);");
3752 
3753   // Treat overloaded operators like other functions.
3754   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3755                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
3756   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3757                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
3758   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3759                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
3760   verifyGoogleFormat(
3761       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
3762       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3763   verifyGoogleFormat(
3764       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
3765       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3766   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3767                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3768   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
3769                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3770   verifyGoogleFormat(
3771       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
3772       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3773       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
3774 
3775   FormatStyle Style = getLLVMStyle();
3776   Style.PointerAlignment = FormatStyle::PAS_Left;
3777   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3778                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
3779                Style);
3780   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
3781                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3782                Style);
3783 }
3784 
3785 TEST_F(FormatTest, TrailingReturnType) {
3786   verifyFormat("auto foo() -> int;\n");
3787   verifyFormat("struct S {\n"
3788                "  auto bar() const -> int;\n"
3789                "};");
3790   verifyFormat("template <size_t Order, typename T>\n"
3791                "auto load_img(const std::string &filename)\n"
3792                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
3793   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
3794                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
3795   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
3796   verifyFormat("template <typename T>\n"
3797                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
3798                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
3799 
3800   // Not trailing return types.
3801   verifyFormat("void f() { auto a = b->c(); }");
3802 }
3803 
3804 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
3805   // Avoid breaking before trailing 'const' or other trailing annotations, if
3806   // they are not function-like.
3807   FormatStyle Style = getGoogleStyle();
3808   Style.ColumnLimit = 47;
3809   verifyFormat("void someLongFunction(\n"
3810                "    int someLoooooooooooooongParameter) const {\n}",
3811                getLLVMStyleWithColumns(47));
3812   verifyFormat("LoooooongReturnType\n"
3813                "someLoooooooongFunction() const {}",
3814                getLLVMStyleWithColumns(47));
3815   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
3816                "    const {}",
3817                Style);
3818   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3819                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
3820   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3821                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
3822   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3823                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
3824   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
3825                "                   aaaaaaaaaaa aaaaa) const override;");
3826   verifyGoogleFormat(
3827       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3828       "    const override;");
3829 
3830   // Even if the first parameter has to be wrapped.
3831   verifyFormat("void someLongFunction(\n"
3832                "    int someLongParameter) const {}",
3833                getLLVMStyleWithColumns(46));
3834   verifyFormat("void someLongFunction(\n"
3835                "    int someLongParameter) const {}",
3836                Style);
3837   verifyFormat("void someLongFunction(\n"
3838                "    int someLongParameter) override {}",
3839                Style);
3840   verifyFormat("void someLongFunction(\n"
3841                "    int someLongParameter) OVERRIDE {}",
3842                Style);
3843   verifyFormat("void someLongFunction(\n"
3844                "    int someLongParameter) final {}",
3845                Style);
3846   verifyFormat("void someLongFunction(\n"
3847                "    int someLongParameter) FINAL {}",
3848                Style);
3849   verifyFormat("void someLongFunction(\n"
3850                "    int parameter) const override {}",
3851                Style);
3852 
3853   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
3854   verifyFormat("void someLongFunction(\n"
3855                "    int someLongParameter) const\n"
3856                "{\n"
3857                "}",
3858                Style);
3859 
3860   // Unless these are unknown annotations.
3861   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
3862                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3863                "    LONG_AND_UGLY_ANNOTATION;");
3864 
3865   // Breaking before function-like trailing annotations is fine to keep them
3866   // close to their arguments.
3867   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3868                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3869   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3870                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3871   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3872                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
3873   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
3874                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
3875   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
3876 
3877   verifyFormat(
3878       "void aaaaaaaaaaaaaaaaaa()\n"
3879       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
3880       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
3881   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3882                "    __attribute__((unused));");
3883   verifyGoogleFormat(
3884       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3885       "    GUARDED_BY(aaaaaaaaaaaa);");
3886   verifyGoogleFormat(
3887       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3888       "    GUARDED_BY(aaaaaaaaaaaa);");
3889   verifyGoogleFormat(
3890       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3891       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3892   verifyGoogleFormat(
3893       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3894       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
3895 }
3896 
3897 TEST_F(FormatTest, FunctionAnnotations) {
3898   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3899                "string OldFunction(const string &parameter) {}");
3900   verifyFormat("template <typename T>\n"
3901                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3902                "string OldFunction(const string &parameter) {}");
3903 
3904   // Not function annotations.
3905   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3906                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
3907   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
3908                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
3909 }
3910 
3911 TEST_F(FormatTest, BreaksDesireably) {
3912   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3913                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3914                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
3915   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3916                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
3917                "}");
3918 
3919   verifyFormat(
3920       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3921       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3922 
3923   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3924                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3925                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3926 
3927   verifyFormat(
3928       "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3929       "                            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
3930       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3931       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
3932 
3933   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3934                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3935 
3936   verifyFormat(
3937       "void f() {\n"
3938       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
3939       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3940       "}");
3941   verifyFormat(
3942       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3943       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3944   verifyFormat(
3945       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3946       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3947   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3948                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3949                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3950 
3951   // Indent consistently independent of call expression and unary operator.
3952   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3953                "    dddddddddddddddddddddddddddddd));");
3954   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3955                "    dddddddddddddddddddddddddddddd));");
3956   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
3957                "    dddddddddddddddddddddddddddddd));");
3958 
3959   // This test case breaks on an incorrect memoization, i.e. an optimization not
3960   // taking into account the StopAt value.
3961   verifyFormat(
3962       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3963       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3964       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3965       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3966 
3967   verifyFormat("{\n  {\n    {\n"
3968                "      Annotation.SpaceRequiredBefore =\n"
3969                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
3970                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
3971                "    }\n  }\n}");
3972 
3973   // Break on an outer level if there was a break on an inner level.
3974   EXPECT_EQ("f(g(h(a, // comment\n"
3975             "      b, c),\n"
3976             "    d, e),\n"
3977             "  x, y);",
3978             format("f(g(h(a, // comment\n"
3979                    "    b, c), d, e), x, y);"));
3980 
3981   // Prefer breaking similar line breaks.
3982   verifyFormat(
3983       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
3984       "                             NSTrackingMouseEnteredAndExited |\n"
3985       "                             NSTrackingActiveAlways;");
3986 }
3987 
3988 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
3989   FormatStyle NoBinPacking = getGoogleStyle();
3990   NoBinPacking.BinPackParameters = false;
3991   NoBinPacking.BinPackArguments = true;
3992   verifyFormat("void f() {\n"
3993                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
3994                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3995                "}",
3996                NoBinPacking);
3997   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
3998                "       int aaaaaaaaaaaaaaaaaaaa,\n"
3999                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4000                NoBinPacking);
4001 }
4002 
4003 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
4004   FormatStyle NoBinPacking = getGoogleStyle();
4005   NoBinPacking.BinPackParameters = false;
4006   NoBinPacking.BinPackArguments = false;
4007   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
4008                "  aaaaaaaaaaaaaaaaaaaa,\n"
4009                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
4010                NoBinPacking);
4011   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
4012                "        aaaaaaaaaaaaa,\n"
4013                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
4014                NoBinPacking);
4015   verifyFormat(
4016       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4017       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4018       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4019       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4020       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
4021       NoBinPacking);
4022   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4023                "    .aaaaaaaaaaaaaaaaaa();",
4024                NoBinPacking);
4025   verifyFormat("void f() {\n"
4026                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4027                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
4028                "}",
4029                NoBinPacking);
4030 
4031   verifyFormat(
4032       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4033       "             aaaaaaaaaaaa,\n"
4034       "             aaaaaaaaaaaa);",
4035       NoBinPacking);
4036   verifyFormat(
4037       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
4038       "                               ddddddddddddddddddddddddddddd),\n"
4039       "             test);",
4040       NoBinPacking);
4041 
4042   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4043                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
4044                "            aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;",
4045                NoBinPacking);
4046   verifyFormat("a(\"a\"\n"
4047                "  \"a\",\n"
4048                "  a);");
4049 
4050   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4051   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
4052                "                aaaaaaaaa,\n"
4053                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4054                NoBinPacking);
4055   verifyFormat(
4056       "void f() {\n"
4057       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4058       "      .aaaaaaa();\n"
4059       "}",
4060       NoBinPacking);
4061   verifyFormat(
4062       "template <class SomeType, class SomeOtherType>\n"
4063       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
4064       NoBinPacking);
4065 }
4066 
4067 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
4068   FormatStyle Style = getLLVMStyleWithColumns(15);
4069   Style.ExperimentalAutoDetectBinPacking = true;
4070   EXPECT_EQ("aaa(aaaa,\n"
4071             "    aaaa,\n"
4072             "    aaaa);\n"
4073             "aaa(aaaa,\n"
4074             "    aaaa,\n"
4075             "    aaaa);",
4076             format("aaa(aaaa,\n" // one-per-line
4077                    "  aaaa,\n"
4078                    "    aaaa  );\n"
4079                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4080                    Style));
4081   EXPECT_EQ("aaa(aaaa, aaaa,\n"
4082             "    aaaa);\n"
4083             "aaa(aaaa, aaaa,\n"
4084             "    aaaa);",
4085             format("aaa(aaaa,  aaaa,\n" // bin-packed
4086                    "    aaaa  );\n"
4087                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4088                    Style));
4089 }
4090 
4091 TEST_F(FormatTest, FormatsBuilderPattern) {
4092   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
4093                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
4094                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
4095                "    .StartsWith(\".init\", ORDER_INIT)\n"
4096                "    .StartsWith(\".fini\", ORDER_FINI)\n"
4097                "    .StartsWith(\".hash\", ORDER_HASH)\n"
4098                "    .Default(ORDER_TEXT);\n");
4099 
4100   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
4101                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
4102   verifyFormat(
4103       "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n"
4104       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4105       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4106   verifyFormat(
4107       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
4108       "    aaaaaaaaaaaaaa);");
4109   verifyFormat(
4110       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
4111       "    aaaaaa->aaaaaaaaaaaa()\n"
4112       "        ->aaaaaaaaaaaaaaaa(\n"
4113       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4114       "        ->aaaaaaaaaaaaaaaaa();");
4115   verifyGoogleFormat(
4116       "void f() {\n"
4117       "  someo->Add((new util::filetools::Handler(dir))\n"
4118       "                 ->OnEvent1(NewPermanentCallback(\n"
4119       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
4120       "                 ->OnEvent2(NewPermanentCallback(\n"
4121       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
4122       "                 ->OnEvent3(NewPermanentCallback(\n"
4123       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
4124       "                 ->OnEvent5(NewPermanentCallback(\n"
4125       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
4126       "                 ->OnEvent6(NewPermanentCallback(\n"
4127       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
4128       "}");
4129 
4130   verifyFormat(
4131       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
4132   verifyFormat("aaaaaaaaaaaaaaa()\n"
4133                "    .aaaaaaaaaaaaaaa()\n"
4134                "    .aaaaaaaaaaaaaaa()\n"
4135                "    .aaaaaaaaaaaaaaa()\n"
4136                "    .aaaaaaaaaaaaaaa();");
4137   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4138                "    .aaaaaaaaaaaaaaa()\n"
4139                "    .aaaaaaaaaaaaaaa()\n"
4140                "    .aaaaaaaaaaaaaaa();");
4141   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4142                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4143                "    .aaaaaaaaaaaaaaa();");
4144   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
4145                "    ->aaaaaaaaaaaaaae(0)\n"
4146                "    ->aaaaaaaaaaaaaaa();");
4147 
4148   // Don't linewrap after very short segments.
4149   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4150                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4151                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4152   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4153                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4154                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4155   verifyFormat("aaa()\n"
4156                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4157                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4158                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4159 
4160   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4161                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4162                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
4163   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4164                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
4165                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
4166 
4167   // Prefer not to break after empty parentheses.
4168   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
4169                "    First->LastNewlineOffset);");
4170 }
4171 
4172 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
4173   verifyFormat(
4174       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4175       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
4176   verifyFormat(
4177       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
4178       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
4179 
4180   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4181                "    ccccccccccccccccccccccccc) {\n}");
4182   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
4183                "    ccccccccccccccccccccccccc) {\n}");
4184 
4185   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4186                "    ccccccccccccccccccccccccc) {\n}");
4187   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
4188                "    ccccccccccccccccccccccccc) {\n}");
4189 
4190   verifyFormat(
4191       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
4192       "    ccccccccccccccccccccccccc) {\n}");
4193   verifyFormat(
4194       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
4195       "    ccccccccccccccccccccccccc) {\n}");
4196 
4197   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
4198                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
4199                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
4200                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4201   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
4202                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
4203                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
4204                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4205 
4206   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
4207                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
4208                "    aaaaaaaaaaaaaaa != aa) {\n}");
4209   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
4210                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
4211                "    aaaaaaaaaaaaaaa != aa) {\n}");
4212 }
4213 
4214 TEST_F(FormatTest, BreaksAfterAssignments) {
4215   verifyFormat(
4216       "unsigned Cost =\n"
4217       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
4218       "                        SI->getPointerAddressSpaceee());\n");
4219   verifyFormat(
4220       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
4221       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
4222 
4223   verifyFormat(
4224       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
4225       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
4226   verifyFormat("unsigned OriginalStartColumn =\n"
4227                "    SourceMgr.getSpellingColumnNumber(\n"
4228                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
4229                "    1;");
4230 }
4231 
4232 TEST_F(FormatTest, AlignsAfterAssignments) {
4233   verifyFormat(
4234       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4235       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
4236   verifyFormat(
4237       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4238       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
4239   verifyFormat(
4240       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4241       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
4242   verifyFormat(
4243       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4244       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
4245   verifyFormat(
4246       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4247       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4248       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
4249 }
4250 
4251 TEST_F(FormatTest, AlignsAfterReturn) {
4252   verifyFormat(
4253       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4254       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
4255   verifyFormat(
4256       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4257       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
4258   verifyFormat(
4259       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4260       "       aaaaaaaaaaaaaaaaaaaaaa();");
4261   verifyFormat(
4262       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4263       "        aaaaaaaaaaaaaaaaaaaaaa());");
4264   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4265                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4266   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4267                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
4268                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4269   verifyFormat("return\n"
4270                "    // true if code is one of a or b.\n"
4271                "    code == a || code == b;");
4272 }
4273 
4274 TEST_F(FormatTest, AlignsAfterOpenBracket) {
4275   verifyFormat(
4276       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4277       "                                                aaaaaaaaa aaaaaaa) {}");
4278   verifyFormat(
4279       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4280       "                                               aaaaaaaaaaa aaaaaaaaa);");
4281   verifyFormat(
4282       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4283       "                                             aaaaaaaaaaaaaaaaaaaaa));");
4284   FormatStyle Style = getLLVMStyle();
4285   Style.AlignAfterOpenBracket = false;
4286   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4287                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
4288                Style);
4289   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4290                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
4291                Style);
4292   verifyFormat("SomeLongVariableName->someFunction(\n"
4293                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
4294                Style);
4295   verifyFormat(
4296       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4297       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4298       Style);
4299   verifyFormat(
4300       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4301       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4302       Style);
4303   verifyFormat(
4304       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4305       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4306       Style);
4307 }
4308 
4309 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
4310   FormatStyle Style = getLLVMStyleWithColumns(40);
4311   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4312                "          bbbbbbbbbbbbbbbbbbbbbb);",
4313                Style);
4314   Style.AlignAfterOpenBracket = true;
4315   Style.AlignOperands = false;
4316   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4317                "          bbbbbbbbbbbbbbbbbbbbbb);",
4318                Style);
4319   Style.AlignAfterOpenBracket = false;
4320   Style.AlignOperands = true;
4321   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4322                "          bbbbbbbbbbbbbbbbbbbbbb);",
4323                Style);
4324   Style.AlignAfterOpenBracket = false;
4325   Style.AlignOperands = false;
4326   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4327                "    bbbbbbbbbbbbbbbbbbbbbb);",
4328                Style);
4329 }
4330 
4331 TEST_F(FormatTest, BreaksConditionalExpressions) {
4332   verifyFormat(
4333       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4334       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4335       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4336   verifyFormat(
4337       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4338       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4339   verifyFormat(
4340       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
4341       "                                                    : aaaaaaaaaaaaa);");
4342   verifyFormat(
4343       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4344       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4345       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4346       "                   aaaaaaaaaaaaa);");
4347   verifyFormat(
4348       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4349       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4350       "                   aaaaaaaaaaaaa);");
4351   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4352                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4353                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4354                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4355                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4356   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4357                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4358                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4359                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4360                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4361                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4362                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4363   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4364                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4365                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4366                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4367                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4368   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4369                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4370                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4371   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4372                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4373                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4374                "        : aaaaaaaaaaaaaaaa;");
4375   verifyFormat(
4376       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4377       "    ? aaaaaaaaaaaaaaa\n"
4378       "    : aaaaaaaaaaaaaaa;");
4379   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4380                "          aaaaaaaaa\n"
4381                "      ? b\n"
4382                "      : c);");
4383   verifyFormat("return aaaa == bbbb\n"
4384                "           // comment\n"
4385                "           ? aaaa\n"
4386                "           : bbbb;");
4387   verifyFormat("unsigned Indent =\n"
4388                "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
4389                "                              ? IndentForLevel[TheLine.Level]\n"
4390                "                              : TheLine * 2,\n"
4391                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4392                getLLVMStyleWithColumns(70));
4393   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4394                "                  ? aaaaaaaaaaaaaaa\n"
4395                "                  : bbbbbbbbbbbbbbb //\n"
4396                "                        ? ccccccccccccccc\n"
4397                "                        : ddddddddddddddd;");
4398   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4399                "                  ? aaaaaaaaaaaaaaa\n"
4400                "                  : (bbbbbbbbbbbbbbb //\n"
4401                "                         ? ccccccccccccccc\n"
4402                "                         : ddddddddddddddd);");
4403   verifyFormat(
4404       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4405       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4406       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
4407       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
4408       "                                      : aaaaaaaaaa;");
4409   verifyFormat(
4410       "aaaaaa = aaaaaaaaaaaa\n"
4411       "             ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4412       "                          : aaaaaaaaaaaaaaaaaaaaaa\n"
4413       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4414 
4415   FormatStyle NoBinPacking = getLLVMStyle();
4416   NoBinPacking.BinPackArguments = false;
4417   verifyFormat(
4418       "void f() {\n"
4419       "  g(aaa,\n"
4420       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4421       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4422       "        ? aaaaaaaaaaaaaaa\n"
4423       "        : aaaaaaaaaaaaaaa);\n"
4424       "}",
4425       NoBinPacking);
4426   verifyFormat(
4427       "void f() {\n"
4428       "  g(aaa,\n"
4429       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4430       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4431       "        ?: aaaaaaaaaaaaaaa);\n"
4432       "}",
4433       NoBinPacking);
4434 
4435   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
4436                "             // comment.\n"
4437                "             ccccccccccccccccccccccccccccccccccccccc\n"
4438                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4439                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
4440 
4441   // Assignments in conditional expressions. Apparently not uncommon :-(.
4442   verifyFormat("return a != b\n"
4443                "           // comment\n"
4444                "           ? a = b\n"
4445                "           : a = b;");
4446   verifyFormat("return a != b\n"
4447                "           // comment\n"
4448                "           ? a = a != b\n"
4449                "                     // comment\n"
4450                "                     ? a = b\n"
4451                "                     : a\n"
4452                "           : a;\n");
4453   verifyFormat("return a != b\n"
4454                "           // comment\n"
4455                "           ? a\n"
4456                "           : a = a != b\n"
4457                "                     // comment\n"
4458                "                     ? a = b\n"
4459                "                     : a;");
4460 }
4461 
4462 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
4463   FormatStyle Style = getLLVMStyle();
4464   Style.BreakBeforeTernaryOperators = false;
4465   Style.ColumnLimit = 70;
4466   verifyFormat(
4467       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4468       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4469       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4470       Style);
4471   verifyFormat(
4472       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4473       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4474       Style);
4475   verifyFormat(
4476       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
4477       "                                                      aaaaaaaaaaaaa);",
4478       Style);
4479   verifyFormat(
4480       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4481       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4482       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4483       "                   aaaaaaaaaaaaa);",
4484       Style);
4485   verifyFormat(
4486       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4487       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4488       "                   aaaaaaaaaaaaa);",
4489       Style);
4490   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4491                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4492                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4493                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4494                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4495                Style);
4496   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4497                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4498                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4499                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4500                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4501                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4502                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4503                Style);
4504   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4505                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
4506                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4507                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4508                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4509                Style);
4510   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4511                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4512                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4513                Style);
4514   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4515                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4516                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4517                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4518                Style);
4519   verifyFormat(
4520       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4521       "    aaaaaaaaaaaaaaa :\n"
4522       "    aaaaaaaaaaaaaaa;",
4523       Style);
4524   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4525                "          aaaaaaaaa ?\n"
4526                "      b :\n"
4527                "      c);",
4528                Style);
4529   verifyFormat(
4530       "unsigned Indent =\n"
4531       "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
4532       "                              IndentForLevel[TheLine.Level] :\n"
4533       "                              TheLine * 2,\n"
4534       "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4535       Style);
4536   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4537                "                  aaaaaaaaaaaaaaa :\n"
4538                "                  bbbbbbbbbbbbbbb ? //\n"
4539                "                      ccccccccccccccc :\n"
4540                "                      ddddddddddddddd;",
4541                Style);
4542   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4543                "                  aaaaaaaaaaaaaaa :\n"
4544                "                  (bbbbbbbbbbbbbbb ? //\n"
4545                "                       ccccccccccccccc :\n"
4546                "                       ddddddddddddddd);",
4547                Style);
4548 }
4549 
4550 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
4551   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
4552                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
4553   verifyFormat("bool a = true, b = false;");
4554 
4555   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4556                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
4557                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
4558                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
4559   verifyFormat(
4560       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
4561       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
4562       "     d = e && f;");
4563   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
4564                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
4565   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4566                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
4567   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
4568                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
4569 
4570   FormatStyle Style = getGoogleStyle();
4571   Style.PointerAlignment = FormatStyle::PAS_Left;
4572   Style.DerivePointerAlignment = false;
4573   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4574                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
4575                "    *b = bbbbbbbbbbbbbbbbbbb;",
4576                Style);
4577   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4578                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
4579                Style);
4580 }
4581 
4582 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
4583   verifyFormat("arr[foo ? bar : baz];");
4584   verifyFormat("f()[foo ? bar : baz];");
4585   verifyFormat("(a + b)[foo ? bar : baz];");
4586   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
4587 }
4588 
4589 TEST_F(FormatTest, AlignsStringLiterals) {
4590   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
4591                "                                      \"short literal\");");
4592   verifyFormat(
4593       "looooooooooooooooooooooooongFunction(\n"
4594       "    \"short literal\"\n"
4595       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
4596   verifyFormat("someFunction(\"Always break between multi-line\"\n"
4597                "             \" string literals\",\n"
4598                "             and, other, parameters);");
4599   EXPECT_EQ("fun + \"1243\" /* comment */\n"
4600             "      \"5678\";",
4601             format("fun + \"1243\" /* comment */\n"
4602                    "      \"5678\";",
4603                    getLLVMStyleWithColumns(28)));
4604   EXPECT_EQ(
4605       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
4606       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
4607       "         \"aaaaaaaaaaaaaaaa\";",
4608       format("aaaaaa ="
4609              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
4610              "aaaaaaaaaaaaaaaaaaaaa\" "
4611              "\"aaaaaaaaaaaaaaaa\";"));
4612   verifyFormat("a = a + \"a\"\n"
4613                "        \"a\"\n"
4614                "        \"a\";");
4615   verifyFormat("f(\"a\", \"b\"\n"
4616                "       \"c\");");
4617 
4618   verifyFormat(
4619       "#define LL_FORMAT \"ll\"\n"
4620       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
4621       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
4622 
4623   verifyFormat("#define A(X)          \\\n"
4624                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
4625                "  \"ccccc\"",
4626                getLLVMStyleWithColumns(23));
4627   verifyFormat("#define A \"def\"\n"
4628                "f(\"abc\" A \"ghi\"\n"
4629                "  \"jkl\");");
4630 
4631   verifyFormat("f(L\"a\"\n"
4632                "  L\"b\");");
4633   verifyFormat("#define A(X)            \\\n"
4634                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
4635                "  L\"ccccc\"",
4636                getLLVMStyleWithColumns(25));
4637 
4638   verifyFormat("f(@\"a\"\n"
4639                "  @\"b\");");
4640   verifyFormat("NSString s = @\"a\"\n"
4641                "             @\"b\"\n"
4642                "             @\"c\";");
4643   verifyFormat("NSString s = @\"a\"\n"
4644                "              \"b\"\n"
4645                "              \"c\";");
4646 }
4647 
4648 TEST_F(FormatTest, DefinitionReturnTypeBreakingStyle) {
4649   FormatStyle Style = getLLVMStyle();
4650   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_TopLevel;
4651   verifyFormat("class C {\n"
4652                "  int f() { return 1; }\n"
4653                "};\n"
4654                "int\n"
4655                "f() {\n"
4656                "  return 1;\n"
4657                "}",
4658                Style);
4659   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
4660   verifyFormat("class C {\n"
4661                "  int\n"
4662                "  f() {\n"
4663                "    return 1;\n"
4664                "  }\n"
4665                "};\n"
4666                "int\n"
4667                "f() {\n"
4668                "  return 1;\n"
4669                "}",
4670                Style);
4671   verifyFormat("const char *\n"
4672                "f(void) {\n" // Break here.
4673                "  return \"\";\n"
4674                "}\n"
4675                "const char *bar(void);\n", // No break here.
4676                Style);
4677   verifyFormat("template <class T>\n"
4678                "T *\n"
4679                "f(T &c) {\n" // Break here.
4680                "  return NULL;\n"
4681                "}\n"
4682                "template <class T> T *f(T &c);\n", // No break here.
4683                Style);
4684   verifyFormat("class C {\n"
4685                "  int\n"
4686                "  operator+() {\n"
4687                "    return 1;\n"
4688                "  }\n"
4689                "  int\n"
4690                "  operator()() {\n"
4691                "    return 1;\n"
4692                "  }\n"
4693                "};\n",
4694                Style);
4695   verifyFormat("void\n"
4696                "A::operator()() {}\n"
4697                "void\n"
4698                "A::operator>>() {}\n"
4699                "void\n"
4700                "A::operator+() {}\n",
4701                Style);
4702   verifyFormat("void *operator new(std::size_t s);", // No break here.
4703                Style);
4704   verifyFormat("void *\n"
4705                "operator new(std::size_t s) {}",
4706                Style);
4707   verifyFormat("void *\n"
4708                "operator delete[](void *ptr) {}",
4709                Style);
4710   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4711   verifyFormat("const char *\n"
4712                "f(void)\n" // Break here.
4713                "{\n"
4714                "  return \"\";\n"
4715                "}\n"
4716                "const char *bar(void);\n", // No break here.
4717                Style);
4718   verifyFormat("template <class T>\n"
4719                "T *\n"     // Problem here: no line break
4720                "f(T &c)\n" // Break here.
4721                "{\n"
4722                "  return NULL;\n"
4723                "}\n"
4724                "template <class T> T *f(T &c);\n", // No break here.
4725                Style);
4726 }
4727 
4728 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
4729   FormatStyle NoBreak = getLLVMStyle();
4730   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
4731   FormatStyle Break = getLLVMStyle();
4732   Break.AlwaysBreakBeforeMultilineStrings = true;
4733   verifyFormat("aaaa = \"bbbb\"\n"
4734                "       \"cccc\";",
4735                NoBreak);
4736   verifyFormat("aaaa =\n"
4737                "    \"bbbb\"\n"
4738                "    \"cccc\";",
4739                Break);
4740   verifyFormat("aaaa(\"bbbb\"\n"
4741                "     \"cccc\");",
4742                NoBreak);
4743   verifyFormat("aaaa(\n"
4744                "    \"bbbb\"\n"
4745                "    \"cccc\");",
4746                Break);
4747   verifyFormat("aaaa(qqq, \"bbbb\"\n"
4748                "          \"cccc\");",
4749                NoBreak);
4750   verifyFormat("aaaa(qqq,\n"
4751                "     \"bbbb\"\n"
4752                "     \"cccc\");",
4753                Break);
4754   verifyFormat("aaaa(qqq,\n"
4755                "     L\"bbbb\"\n"
4756                "     L\"cccc\");",
4757                Break);
4758   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
4759                "                      \"bbbb\"));",
4760                Break);
4761   verifyFormat("string s = someFunction(\n"
4762                "    \"abc\"\n"
4763                "    \"abc\");",
4764                Break);
4765 
4766   // As we break before unary operators, breaking right after them is bad.
4767   verifyFormat("string foo = abc ? \"x\"\n"
4768                "                   \"blah blah blah blah blah blah\"\n"
4769                "                 : \"y\";",
4770                Break);
4771 
4772   // Don't break if there is no column gain.
4773   verifyFormat("f(\"aaaa\"\n"
4774                "  \"bbbb\");",
4775                Break);
4776 
4777   // Treat literals with escaped newlines like multi-line string literals.
4778   EXPECT_EQ("x = \"a\\\n"
4779             "b\\\n"
4780             "c\";",
4781             format("x = \"a\\\n"
4782                    "b\\\n"
4783                    "c\";",
4784                    NoBreak));
4785   EXPECT_EQ("xxxx =\n"
4786             "    \"a\\\n"
4787             "b\\\n"
4788             "c\";",
4789             format("xxxx = \"a\\\n"
4790                    "b\\\n"
4791                    "c\";",
4792                    Break));
4793 
4794   // Exempt ObjC strings for now.
4795   EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
4796             "                          @\"bbbb\";",
4797             format("NSString *const kString = @\"aaaa\"\n"
4798                    "@\"bbbb\";",
4799                    Break));
4800 
4801   Break.ColumnLimit = 0;
4802   verifyFormat("const char *hello = \"hello llvm\";", Break);
4803 }
4804 
4805 TEST_F(FormatTest, AlignsPipes) {
4806   verifyFormat(
4807       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4808       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4809       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4810   verifyFormat(
4811       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
4812       "                     << aaaaaaaaaaaaaaaaaaaa;");
4813   verifyFormat(
4814       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4815       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4816   verifyFormat(
4817       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
4818       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
4819       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
4820   verifyFormat(
4821       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4822       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4823       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4824   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4825                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4826                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4827                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4828   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
4829                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
4830   verifyFormat(
4831       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4832       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4833 
4834   verifyFormat("return out << \"somepacket = {\\n\"\n"
4835                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
4836                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
4837                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
4838                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
4839                "           << \"}\";");
4840 
4841   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4842                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4843                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
4844   verifyFormat(
4845       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
4846       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
4847       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
4848       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
4849       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
4850   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
4851                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4852   verifyFormat(
4853       "void f() {\n"
4854       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
4855       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4856       "}");
4857   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
4858                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
4859   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4860                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4861                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
4862                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
4863   verifyFormat("LOG_IF(aaa == //\n"
4864                "       bbb)\n"
4865                "    << a << b;");
4866 
4867   // Breaking before the first "<<" is generally not desirable.
4868   verifyFormat(
4869       "llvm::errs()\n"
4870       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4871       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4872       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4873       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4874       getLLVMStyleWithColumns(70));
4875   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4876                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4877                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4878                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4879                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4880                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4881                getLLVMStyleWithColumns(70));
4882 
4883   // But sometimes, breaking before the first "<<" is desirable.
4884   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4885                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
4886   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
4887                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4888                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4889   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
4890                "    << BEF << IsTemplate << Description << E->getType();");
4891 
4892   verifyFormat(
4893       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4894       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4895 
4896   // Incomplete string literal.
4897   EXPECT_EQ("llvm::errs() << \"\n"
4898             "             << a;",
4899             format("llvm::errs() << \"\n<<a;"));
4900 
4901   verifyFormat("void f() {\n"
4902                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
4903                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
4904                "}");
4905 
4906   // Handle 'endl'.
4907   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
4908                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4909   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4910 }
4911 
4912 TEST_F(FormatTest, UnderstandsEquals) {
4913   verifyFormat(
4914       "aaaaaaaaaaaaaaaaa =\n"
4915       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4916   verifyFormat(
4917       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4918       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4919   verifyFormat(
4920       "if (a) {\n"
4921       "  f();\n"
4922       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4923       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
4924       "}");
4925 
4926   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4927                "        100000000 + 10000000) {\n}");
4928 }
4929 
4930 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
4931   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4932                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
4933 
4934   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4935                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
4936 
4937   verifyFormat(
4938       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
4939       "                                                          Parameter2);");
4940 
4941   verifyFormat(
4942       "ShortObject->shortFunction(\n"
4943       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
4944       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
4945 
4946   verifyFormat("loooooooooooooongFunction(\n"
4947                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
4948 
4949   verifyFormat(
4950       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
4951       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
4952 
4953   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
4954                "    .WillRepeatedly(Return(SomeValue));");
4955   verifyFormat("void f() {\n"
4956                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
4957                "      .Times(2)\n"
4958                "      .WillRepeatedly(Return(SomeValue));\n"
4959                "}");
4960   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
4961                "    ccccccccccccccccccccccc);");
4962   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4963                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4964                "          .aaaaa(aaaaa),\n"
4965                "      aaaaaaaaaaaaaaaaaaaaa);");
4966   verifyFormat("void f() {\n"
4967                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4968                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
4969                "}");
4970   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4971                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4972                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4973                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4974                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4975   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4976                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4977                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4978                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
4979                "}");
4980 
4981   // Here, it is not necessary to wrap at "." or "->".
4982   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
4983                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4984   verifyFormat(
4985       "aaaaaaaaaaa->aaaaaaaaa(\n"
4986       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4987       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
4988 
4989   verifyFormat(
4990       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4991       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
4992   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
4993                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
4994   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
4995                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
4996 
4997   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4998                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4999                "    .a();");
5000 
5001   FormatStyle NoBinPacking = getLLVMStyle();
5002   NoBinPacking.BinPackParameters = false;
5003   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5004                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5005                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
5006                "                         aaaaaaaaaaaaaaaaaaa,\n"
5007                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5008                NoBinPacking);
5009 
5010   // If there is a subsequent call, change to hanging indentation.
5011   verifyFormat(
5012       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5013       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
5014       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5015   verifyFormat(
5016       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5017       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
5018   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5019                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5020                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5021   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5022                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5023                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5024 }
5025 
5026 TEST_F(FormatTest, WrapsTemplateDeclarations) {
5027   verifyFormat("template <typename T>\n"
5028                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5029   verifyFormat("template <typename T>\n"
5030                "// T should be one of {A, B}.\n"
5031                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5032   verifyFormat(
5033       "template <typename T>\n"
5034       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
5035   verifyFormat("template <typename T>\n"
5036                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
5037                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
5038   verifyFormat(
5039       "template <typename T>\n"
5040       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
5041       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
5042   verifyFormat(
5043       "template <typename T>\n"
5044       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
5045       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
5046       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5047   verifyFormat("template <typename T>\n"
5048                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5049                "    int aaaaaaaaaaaaaaaaaaaaaa);");
5050   verifyFormat(
5051       "template <typename T1, typename T2 = char, typename T3 = char,\n"
5052       "          typename T4 = char>\n"
5053       "void f();");
5054   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
5055                "          template <typename> class cccccccccccccccccccccc,\n"
5056                "          typename ddddddddddddd>\n"
5057                "class C {};");
5058   verifyFormat(
5059       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
5060       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5061 
5062   verifyFormat("void f() {\n"
5063                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
5064                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
5065                "}");
5066 
5067   verifyFormat("template <typename T> class C {};");
5068   verifyFormat("template <typename T> void f();");
5069   verifyFormat("template <typename T> void f() {}");
5070   verifyFormat(
5071       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5072       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5073       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
5074       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5075       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5076       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
5077       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
5078       getLLVMStyleWithColumns(72));
5079   EXPECT_EQ("static_cast<A< //\n"
5080             "    B> *>(\n"
5081             "\n"
5082             "    );",
5083             format("static_cast<A<//\n"
5084                    "    B>*>(\n"
5085                    "\n"
5086                    "    );"));
5087   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5088                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
5089 
5090   FormatStyle AlwaysBreak = getLLVMStyle();
5091   AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
5092   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
5093   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
5094   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
5095   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5096                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5097                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
5098   verifyFormat("template <template <typename> class Fooooooo,\n"
5099                "          template <typename> class Baaaaaaar>\n"
5100                "struct C {};",
5101                AlwaysBreak);
5102   verifyFormat("template <typename T> // T can be A, B or C.\n"
5103                "struct C {};",
5104                AlwaysBreak);
5105 }
5106 
5107 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
5108   verifyFormat(
5109       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5110       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5111   verifyFormat(
5112       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5113       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5114       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5115 
5116   // FIXME: Should we have the extra indent after the second break?
5117   verifyFormat(
5118       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5119       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5120       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5121 
5122   verifyFormat(
5123       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
5124       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
5125 
5126   // Breaking at nested name specifiers is generally not desirable.
5127   verifyFormat(
5128       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5129       "    aaaaaaaaaaaaaaaaaaaaaaa);");
5130 
5131   verifyFormat(
5132       "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5133       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5134       "                   aaaaaaaaaaaaaaaaaaaaa);",
5135       getLLVMStyleWithColumns(74));
5136 
5137   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5138                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5139                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5140 }
5141 
5142 TEST_F(FormatTest, UnderstandsTemplateParameters) {
5143   verifyFormat("A<int> a;");
5144   verifyFormat("A<A<A<int>>> a;");
5145   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
5146   verifyFormat("bool x = a < 1 || 2 > a;");
5147   verifyFormat("bool x = 5 < f<int>();");
5148   verifyFormat("bool x = f<int>() > 5;");
5149   verifyFormat("bool x = 5 < a<int>::x;");
5150   verifyFormat("bool x = a < 4 ? a > 2 : false;");
5151   verifyFormat("bool x = f() ? a < 2 : a > 2;");
5152 
5153   verifyGoogleFormat("A<A<int>> a;");
5154   verifyGoogleFormat("A<A<A<int>>> a;");
5155   verifyGoogleFormat("A<A<A<A<int>>>> a;");
5156   verifyGoogleFormat("A<A<int> > a;");
5157   verifyGoogleFormat("A<A<A<int> > > a;");
5158   verifyGoogleFormat("A<A<A<A<int> > > > a;");
5159   verifyGoogleFormat("A<::A<int>> a;");
5160   verifyGoogleFormat("A<::A> a;");
5161   verifyGoogleFormat("A< ::A> a;");
5162   verifyGoogleFormat("A< ::A<int> > a;");
5163   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
5164   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
5165   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
5166   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
5167   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
5168             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
5169 
5170   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
5171 
5172   verifyFormat("test >> a >> b;");
5173   verifyFormat("test << a >> b;");
5174 
5175   verifyFormat("f<int>();");
5176   verifyFormat("template <typename T> void f() {}");
5177   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
5178   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
5179                "sizeof(char)>::type>;");
5180   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
5181 
5182   // Not template parameters.
5183   verifyFormat("return a < b && c > d;");
5184   verifyFormat("void f() {\n"
5185                "  while (a < b && c > d) {\n"
5186                "  }\n"
5187                "}");
5188   verifyFormat("template <typename... Types>\n"
5189                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
5190 
5191   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5192                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
5193                getLLVMStyleWithColumns(60));
5194   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
5195   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
5196   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
5197 }
5198 
5199 TEST_F(FormatTest, UnderstandsBinaryOperators) {
5200   verifyFormat("COMPARE(a, ==, b);");
5201 }
5202 
5203 TEST_F(FormatTest, UnderstandsPointersToMembers) {
5204   verifyFormat("int A::*x;");
5205   verifyFormat("int (S::*func)(void *);");
5206   verifyFormat("void f() { int (S::*func)(void *); }");
5207   verifyFormat("typedef bool *(Class::*Member)() const;");
5208   verifyFormat("void f() {\n"
5209                "  (a->*f)();\n"
5210                "  a->*x;\n"
5211                "  (a.*f)();\n"
5212                "  ((*a).*f)();\n"
5213                "  a.*x;\n"
5214                "}");
5215   verifyFormat("void f() {\n"
5216                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5217                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
5218                "}");
5219   verifyFormat(
5220       "(aaaaaaaaaa->*bbbbbbb)(\n"
5221       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5222   FormatStyle Style = getLLVMStyle();
5223   Style.PointerAlignment = FormatStyle::PAS_Left;
5224   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
5225 }
5226 
5227 TEST_F(FormatTest, UnderstandsUnaryOperators) {
5228   verifyFormat("int a = -2;");
5229   verifyFormat("f(-1, -2, -3);");
5230   verifyFormat("a[-1] = 5;");
5231   verifyFormat("int a = 5 + -2;");
5232   verifyFormat("if (i == -1) {\n}");
5233   verifyFormat("if (i != -1) {\n}");
5234   verifyFormat("if (i > -1) {\n}");
5235   verifyFormat("if (i < -1) {\n}");
5236   verifyFormat("++(a->f());");
5237   verifyFormat("--(a->f());");
5238   verifyFormat("(a->f())++;");
5239   verifyFormat("a[42]++;");
5240   verifyFormat("if (!(a->f())) {\n}");
5241 
5242   verifyFormat("a-- > b;");
5243   verifyFormat("b ? -a : c;");
5244   verifyFormat("n * sizeof char16;");
5245   verifyFormat("n * alignof char16;", getGoogleStyle());
5246   verifyFormat("sizeof(char);");
5247   verifyFormat("alignof(char);", getGoogleStyle());
5248 
5249   verifyFormat("return -1;");
5250   verifyFormat("switch (a) {\n"
5251                "case -1:\n"
5252                "  break;\n"
5253                "}");
5254   verifyFormat("#define X -1");
5255   verifyFormat("#define X -kConstant");
5256 
5257   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
5258   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
5259 
5260   verifyFormat("int a = /* confusing comment */ -1;");
5261   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
5262   verifyFormat("int a = i /* confusing comment */++;");
5263 }
5264 
5265 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
5266   verifyFormat("if (!aaaaaaaaaa( // break\n"
5267                "        aaaaa)) {\n"
5268                "}");
5269   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
5270                "    aaaaa));");
5271   verifyFormat("*aaa = aaaaaaa( // break\n"
5272                "    bbbbbb);");
5273 }
5274 
5275 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
5276   verifyFormat("bool operator<();");
5277   verifyFormat("bool operator>();");
5278   verifyFormat("bool operator=();");
5279   verifyFormat("bool operator==();");
5280   verifyFormat("bool operator!=();");
5281   verifyFormat("int operator+();");
5282   verifyFormat("int operator++();");
5283   verifyFormat("bool operator();");
5284   verifyFormat("bool operator()();");
5285   verifyFormat("bool operator[]();");
5286   verifyFormat("operator bool();");
5287   verifyFormat("operator int();");
5288   verifyFormat("operator void *();");
5289   verifyFormat("operator SomeType<int>();");
5290   verifyFormat("operator SomeType<int, int>();");
5291   verifyFormat("operator SomeType<SomeType<int>>();");
5292   verifyFormat("void *operator new(std::size_t size);");
5293   verifyFormat("void *operator new[](std::size_t size);");
5294   verifyFormat("void operator delete(void *ptr);");
5295   verifyFormat("void operator delete[](void *ptr);");
5296   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
5297                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
5298 
5299   verifyFormat(
5300       "ostream &operator<<(ostream &OutputStream,\n"
5301       "                    SomeReallyLongType WithSomeReallyLongValue);");
5302   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
5303                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
5304                "  return left.group < right.group;\n"
5305                "}");
5306   verifyFormat("SomeType &operator=(const SomeType &S);");
5307   verifyFormat("f.template operator()<int>();");
5308 
5309   verifyGoogleFormat("operator void*();");
5310   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
5311   verifyGoogleFormat("operator ::A();");
5312 
5313   verifyFormat("using A::operator+;");
5314 }
5315 
5316 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
5317   verifyFormat("Deleted &operator=(const Deleted &)& = default;");
5318   verifyFormat("Deleted &operator=(const Deleted &)&& = delete;");
5319   verifyFormat("SomeType MemberFunction(const Deleted &)& = delete;");
5320   verifyFormat("SomeType MemberFunction(const Deleted &)&& = delete;");
5321   verifyFormat("Deleted &operator=(const Deleted &)&;");
5322   verifyFormat("Deleted &operator=(const Deleted &)&&;");
5323   verifyFormat("SomeType MemberFunction(const Deleted &)&;");
5324   verifyFormat("SomeType MemberFunction(const Deleted &)&&;");
5325 
5326   verifyGoogleFormat("Deleted& operator=(const Deleted&)& = default;");
5327   verifyGoogleFormat("SomeType MemberFunction(const Deleted&)& = delete;");
5328   verifyGoogleFormat("Deleted& operator=(const Deleted&)&;");
5329   verifyGoogleFormat("SomeType MemberFunction(const Deleted&)&;");
5330 
5331   FormatStyle Spaces = getLLVMStyle();
5332   Spaces.SpacesInCStyleCastParentheses = true;
5333   verifyFormat("Deleted &operator=(const Deleted &)& = default;", Spaces);
5334   verifyFormat("SomeType MemberFunction(const Deleted &)& = delete;", Spaces);
5335   verifyFormat("Deleted &operator=(const Deleted &)&;", Spaces);
5336   verifyFormat("SomeType MemberFunction(const Deleted &)&;", Spaces);
5337 
5338   Spaces.SpacesInCStyleCastParentheses = false;
5339   Spaces.SpacesInParentheses = true;
5340   verifyFormat("Deleted &operator=( const Deleted & )& = default;", Spaces);
5341   verifyFormat("SomeType MemberFunction( const Deleted & )& = delete;", Spaces);
5342   verifyFormat("Deleted &operator=( const Deleted & )&;", Spaces);
5343   verifyFormat("SomeType MemberFunction( const Deleted & )&;", Spaces);
5344 }
5345 
5346 TEST_F(FormatTest, UnderstandsNewAndDelete) {
5347   verifyFormat("void f() {\n"
5348                "  A *a = new A;\n"
5349                "  A *a = new (placement) A;\n"
5350                "  delete a;\n"
5351                "  delete (A *)a;\n"
5352                "}");
5353   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5354                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5355   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5356                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5357                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5358   verifyFormat("delete[] h->p;");
5359 }
5360 
5361 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
5362   verifyFormat("int *f(int *a) {}");
5363   verifyFormat("int main(int argc, char **argv) {}");
5364   verifyFormat("Test::Test(int b) : a(b * b) {}");
5365   verifyIndependentOfContext("f(a, *a);");
5366   verifyFormat("void g() { f(*a); }");
5367   verifyIndependentOfContext("int a = b * 10;");
5368   verifyIndependentOfContext("int a = 10 * b;");
5369   verifyIndependentOfContext("int a = b * c;");
5370   verifyIndependentOfContext("int a += b * c;");
5371   verifyIndependentOfContext("int a -= b * c;");
5372   verifyIndependentOfContext("int a *= b * c;");
5373   verifyIndependentOfContext("int a /= b * c;");
5374   verifyIndependentOfContext("int a = *b;");
5375   verifyIndependentOfContext("int a = *b * c;");
5376   verifyIndependentOfContext("int a = b * *c;");
5377   verifyIndependentOfContext("int a = b * (10);");
5378   verifyIndependentOfContext("S << b * (10);");
5379   verifyIndependentOfContext("return 10 * b;");
5380   verifyIndependentOfContext("return *b * *c;");
5381   verifyIndependentOfContext("return a & ~b;");
5382   verifyIndependentOfContext("f(b ? *c : *d);");
5383   verifyIndependentOfContext("int a = b ? *c : *d;");
5384   verifyIndependentOfContext("*b = a;");
5385   verifyIndependentOfContext("a * ~b;");
5386   verifyIndependentOfContext("a * !b;");
5387   verifyIndependentOfContext("a * +b;");
5388   verifyIndependentOfContext("a * -b;");
5389   verifyIndependentOfContext("a * ++b;");
5390   verifyIndependentOfContext("a * --b;");
5391   verifyIndependentOfContext("a[4] * b;");
5392   verifyIndependentOfContext("a[a * a] = 1;");
5393   verifyIndependentOfContext("f() * b;");
5394   verifyIndependentOfContext("a * [self dostuff];");
5395   verifyIndependentOfContext("int x = a * (a + b);");
5396   verifyIndependentOfContext("(a *)(a + b);");
5397   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
5398   verifyIndependentOfContext("int *pa = (int *)&a;");
5399   verifyIndependentOfContext("return sizeof(int **);");
5400   verifyIndependentOfContext("return sizeof(int ******);");
5401   verifyIndependentOfContext("return (int **&)a;");
5402   verifyIndependentOfContext("f((*PointerToArray)[10]);");
5403   verifyFormat("void f(Type (*parameter)[10]) {}");
5404   verifyGoogleFormat("return sizeof(int**);");
5405   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
5406   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
5407   verifyFormat("auto a = [](int **&, int ***) {};");
5408   verifyFormat("auto PointerBinding = [](const char *S) {};");
5409   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
5410   verifyFormat("[](const decltype(*a) &value) {}");
5411   verifyFormat("decltype(a * b) F();");
5412   verifyFormat("#define MACRO() [](A *a) { return 1; }");
5413   verifyIndependentOfContext("typedef void (*f)(int *a);");
5414   verifyIndependentOfContext("int i{a * b};");
5415   verifyIndependentOfContext("aaa && aaa->f();");
5416   verifyIndependentOfContext("int x = ~*p;");
5417   verifyFormat("Constructor() : a(a), area(width * height) {}");
5418   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
5419   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
5420   verifyFormat("void f() { f(a, c * d); }");
5421   verifyFormat("void f() { f(new a(), c * d); }");
5422 
5423   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
5424 
5425   verifyIndependentOfContext("A<int *> a;");
5426   verifyIndependentOfContext("A<int **> a;");
5427   verifyIndependentOfContext("A<int *, int *> a;");
5428   verifyIndependentOfContext("A<int *[]> a;");
5429   verifyIndependentOfContext(
5430       "const char *const p = reinterpret_cast<const char *const>(q);");
5431   verifyIndependentOfContext("A<int **, int **> a;");
5432   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
5433   verifyFormat("for (char **a = b; *a; ++a) {\n}");
5434   verifyFormat("for (; a && b;) {\n}");
5435   verifyFormat("bool foo = true && [] { return false; }();");
5436 
5437   verifyFormat(
5438       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5439       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5440 
5441   verifyGoogleFormat("**outparam = 1;");
5442   verifyGoogleFormat("*outparam = a * b;");
5443   verifyGoogleFormat("int main(int argc, char** argv) {}");
5444   verifyGoogleFormat("A<int*> a;");
5445   verifyGoogleFormat("A<int**> a;");
5446   verifyGoogleFormat("A<int*, int*> a;");
5447   verifyGoogleFormat("A<int**, int**> a;");
5448   verifyGoogleFormat("f(b ? *c : *d);");
5449   verifyGoogleFormat("int a = b ? *c : *d;");
5450   verifyGoogleFormat("Type* t = **x;");
5451   verifyGoogleFormat("Type* t = *++*x;");
5452   verifyGoogleFormat("*++*x;");
5453   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
5454   verifyGoogleFormat("Type* t = x++ * y;");
5455   verifyGoogleFormat(
5456       "const char* const p = reinterpret_cast<const char* const>(q);");
5457   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
5458   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
5459   verifyGoogleFormat("template <typename T>\n"
5460                      "void f(int i = 0, SomeType** temps = NULL);");
5461 
5462   FormatStyle Left = getLLVMStyle();
5463   Left.PointerAlignment = FormatStyle::PAS_Left;
5464   verifyFormat("x = *a(x) = *a(y);", Left);
5465   verifyFormat("for (;; * = b) {\n}", Left);
5466 
5467   verifyIndependentOfContext("a = *(x + y);");
5468   verifyIndependentOfContext("a = &(x + y);");
5469   verifyIndependentOfContext("*(x + y).call();");
5470   verifyIndependentOfContext("&(x + y)->call();");
5471   verifyFormat("void f() { &(*I).first; }");
5472 
5473   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
5474   verifyFormat(
5475       "int *MyValues = {\n"
5476       "    *A, // Operator detection might be confused by the '{'\n"
5477       "    *BB // Operator detection might be confused by previous comment\n"
5478       "};");
5479 
5480   verifyIndependentOfContext("if (int *a = &b)");
5481   verifyIndependentOfContext("if (int &a = *b)");
5482   verifyIndependentOfContext("if (a & b[i])");
5483   verifyIndependentOfContext("if (a::b::c::d & b[i])");
5484   verifyIndependentOfContext("if (*b[i])");
5485   verifyIndependentOfContext("if (int *a = (&b))");
5486   verifyIndependentOfContext("while (int *a = &b)");
5487   verifyIndependentOfContext("size = sizeof *a;");
5488   verifyIndependentOfContext("if (a && (b = c))");
5489   verifyFormat("void f() {\n"
5490                "  for (const int &v : Values) {\n"
5491                "  }\n"
5492                "}");
5493   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
5494   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
5495   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
5496 
5497   verifyFormat("#define A (!a * b)");
5498   verifyFormat("#define MACRO     \\\n"
5499                "  int *i = a * b; \\\n"
5500                "  void f(a *b);",
5501                getLLVMStyleWithColumns(19));
5502 
5503   verifyIndependentOfContext("A = new SomeType *[Length];");
5504   verifyIndependentOfContext("A = new SomeType *[Length]();");
5505   verifyIndependentOfContext("T **t = new T *;");
5506   verifyIndependentOfContext("T **t = new T *();");
5507   verifyGoogleFormat("A = new SomeType*[Length]();");
5508   verifyGoogleFormat("A = new SomeType*[Length];");
5509   verifyGoogleFormat("T** t = new T*;");
5510   verifyGoogleFormat("T** t = new T*();");
5511 
5512   FormatStyle PointerLeft = getLLVMStyle();
5513   PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
5514   verifyFormat("delete *x;", PointerLeft);
5515   verifyFormat("STATIC_ASSERT((a & b) == 0);");
5516   verifyFormat("STATIC_ASSERT(0 == (a & b));");
5517   verifyFormat("template <bool a, bool b> "
5518                "typename t::if<x && y>::type f() {}");
5519   verifyFormat("template <int *y> f() {}");
5520   verifyFormat("vector<int *> v;");
5521   verifyFormat("vector<int *const> v;");
5522   verifyFormat("vector<int *const **const *> v;");
5523   verifyFormat("vector<int *volatile> v;");
5524   verifyFormat("vector<a * b> v;");
5525   verifyFormat("foo<b && false>();");
5526   verifyFormat("foo<b & 1>();");
5527   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
5528   verifyFormat(
5529       "template <class T, class = typename std::enable_if<\n"
5530       "                       std::is_integral<T>::value &&\n"
5531       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
5532       "void F();",
5533       getLLVMStyleWithColumns(76));
5534   verifyFormat(
5535       "template <class T,\n"
5536       "          class = typename ::std::enable_if<\n"
5537       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
5538       "void F();",
5539       getGoogleStyleWithColumns(68));
5540 
5541   verifyIndependentOfContext("MACRO(int *i);");
5542   verifyIndependentOfContext("MACRO(auto *a);");
5543   verifyIndependentOfContext("MACRO(const A *a);");
5544   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
5545   // FIXME: Is there a way to make this work?
5546   // verifyIndependentOfContext("MACRO(A *a);");
5547 
5548   verifyFormat("DatumHandle const *operator->() const { return input_; }");
5549 
5550   EXPECT_EQ("#define OP(x)                                    \\\n"
5551             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
5552             "    return s << a.DebugString();                 \\\n"
5553             "  }",
5554             format("#define OP(x) \\\n"
5555                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
5556                    "    return s << a.DebugString(); \\\n"
5557                    "  }",
5558                    getLLVMStyleWithColumns(50)));
5559 
5560   // FIXME: We cannot handle this case yet; we might be able to figure out that
5561   // foo<x> d > v; doesn't make sense.
5562   verifyFormat("foo<a<b && c> d> v;");
5563 
5564   FormatStyle PointerMiddle = getLLVMStyle();
5565   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
5566   verifyFormat("delete *x;", PointerMiddle);
5567   verifyFormat("int * x;", PointerMiddle);
5568   verifyFormat("template <int * y> f() {}", PointerMiddle);
5569   verifyFormat("int * f(int * a) {}", PointerMiddle);
5570   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
5571   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
5572   verifyFormat("A<int *> a;", PointerMiddle);
5573   verifyFormat("A<int **> a;", PointerMiddle);
5574   verifyFormat("A<int *, int *> a;", PointerMiddle);
5575   verifyFormat("A<int * []> a;", PointerMiddle);
5576   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
5577   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
5578   verifyFormat("T ** t = new T *;", PointerMiddle);
5579 
5580   // Member function reference qualifiers aren't binary operators.
5581   verifyFormat("string // break\n"
5582                "operator()() & {}");
5583   verifyFormat("string // break\n"
5584                "operator()() && {}");
5585   verifyGoogleFormat("template <typename T>\n"
5586                      "auto x() & -> int {}");
5587 }
5588 
5589 TEST_F(FormatTest, UnderstandsAttributes) {
5590   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
5591   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
5592                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
5593   FormatStyle AfterType = getLLVMStyle();
5594   AfterType.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
5595   verifyFormat("__attribute__((nodebug)) void\n"
5596                "foo() {}\n",
5597                AfterType);
5598 }
5599 
5600 TEST_F(FormatTest, UnderstandsEllipsis) {
5601   verifyFormat("int printf(const char *fmt, ...);");
5602   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
5603   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
5604 
5605   FormatStyle PointersLeft = getLLVMStyle();
5606   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
5607   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
5608 }
5609 
5610 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
5611   EXPECT_EQ("int *a;\n"
5612             "int *a;\n"
5613             "int *a;",
5614             format("int *a;\n"
5615                    "int* a;\n"
5616                    "int *a;",
5617                    getGoogleStyle()));
5618   EXPECT_EQ("int* a;\n"
5619             "int* a;\n"
5620             "int* a;",
5621             format("int* a;\n"
5622                    "int* a;\n"
5623                    "int *a;",
5624                    getGoogleStyle()));
5625   EXPECT_EQ("int *a;\n"
5626             "int *a;\n"
5627             "int *a;",
5628             format("int *a;\n"
5629                    "int * a;\n"
5630                    "int *  a;",
5631                    getGoogleStyle()));
5632   EXPECT_EQ("auto x = [] {\n"
5633             "  int *a;\n"
5634             "  int *a;\n"
5635             "  int *a;\n"
5636             "};",
5637             format("auto x=[]{int *a;\n"
5638                    "int * a;\n"
5639                    "int *  a;};",
5640                    getGoogleStyle()));
5641 }
5642 
5643 TEST_F(FormatTest, UnderstandsRvalueReferences) {
5644   verifyFormat("int f(int &&a) {}");
5645   verifyFormat("int f(int a, char &&b) {}");
5646   verifyFormat("void f() { int &&a = b; }");
5647   verifyGoogleFormat("int f(int a, char&& b) {}");
5648   verifyGoogleFormat("void f() { int&& a = b; }");
5649 
5650   verifyIndependentOfContext("A<int &&> a;");
5651   verifyIndependentOfContext("A<int &&, int &&> a;");
5652   verifyGoogleFormat("A<int&&> a;");
5653   verifyGoogleFormat("A<int&&, int&&> a;");
5654 
5655   // Not rvalue references:
5656   verifyFormat("template <bool B, bool C> class A {\n"
5657                "  static_assert(B && C, \"Something is wrong\");\n"
5658                "};");
5659   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
5660   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
5661   verifyFormat("#define A(a, b) (a && b)");
5662 }
5663 
5664 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
5665   verifyFormat("void f() {\n"
5666                "  x[aaaaaaaaa -\n"
5667                "    b] = 23;\n"
5668                "}",
5669                getLLVMStyleWithColumns(15));
5670 }
5671 
5672 TEST_F(FormatTest, FormatsCasts) {
5673   verifyFormat("Type *A = static_cast<Type *>(P);");
5674   verifyFormat("Type *A = (Type *)P;");
5675   verifyFormat("Type *A = (vector<Type *, int *>)P;");
5676   verifyFormat("int a = (int)(2.0f);");
5677   verifyFormat("int a = (int)2.0f;");
5678   verifyFormat("x[(int32)y];");
5679   verifyFormat("x = (int32)y;");
5680   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
5681   verifyFormat("int a = (int)*b;");
5682   verifyFormat("int a = (int)2.0f;");
5683   verifyFormat("int a = (int)~0;");
5684   verifyFormat("int a = (int)++a;");
5685   verifyFormat("int a = (int)sizeof(int);");
5686   verifyFormat("int a = (int)+2;");
5687   verifyFormat("my_int a = (my_int)2.0f;");
5688   verifyFormat("my_int a = (my_int)sizeof(int);");
5689   verifyFormat("return (my_int)aaa;");
5690   verifyFormat("#define x ((int)-1)");
5691   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
5692   verifyFormat("#define p(q) ((int *)&q)");
5693   verifyFormat("fn(a)(b) + 1;");
5694 
5695   verifyFormat("void f() { my_int a = (my_int)*b; }");
5696   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
5697   verifyFormat("my_int a = (my_int)~0;");
5698   verifyFormat("my_int a = (my_int)++a;");
5699   verifyFormat("my_int a = (my_int)-2;");
5700   verifyFormat("my_int a = (my_int)1;");
5701   verifyFormat("my_int a = (my_int *)1;");
5702   verifyFormat("my_int a = (const my_int)-1;");
5703   verifyFormat("my_int a = (const my_int *)-1;");
5704   verifyFormat("my_int a = (my_int)(my_int)-1;");
5705   verifyFormat("my_int a = (ns::my_int)-2;");
5706   verifyFormat("case (my_int)ONE:");
5707 
5708   // FIXME: single value wrapped with paren will be treated as cast.
5709   verifyFormat("void f(int i = (kValue)*kMask) {}");
5710 
5711   verifyFormat("{ (void)F; }");
5712 
5713   // Don't break after a cast's
5714   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5715                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
5716                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
5717 
5718   // These are not casts.
5719   verifyFormat("void f(int *) {}");
5720   verifyFormat("f(foo)->b;");
5721   verifyFormat("f(foo).b;");
5722   verifyFormat("f(foo)(b);");
5723   verifyFormat("f(foo)[b];");
5724   verifyFormat("[](foo) { return 4; }(bar);");
5725   verifyFormat("(*funptr)(foo)[4];");
5726   verifyFormat("funptrs[4](foo)[4];");
5727   verifyFormat("void f(int *);");
5728   verifyFormat("void f(int *) = 0;");
5729   verifyFormat("void f(SmallVector<int>) {}");
5730   verifyFormat("void f(SmallVector<int>);");
5731   verifyFormat("void f(SmallVector<int>) = 0;");
5732   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
5733   verifyFormat("int a = sizeof(int) * b;");
5734   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
5735   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
5736   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
5737   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
5738 
5739   // These are not casts, but at some point were confused with casts.
5740   verifyFormat("virtual void foo(int *) override;");
5741   verifyFormat("virtual void foo(char &) const;");
5742   verifyFormat("virtual void foo(int *a, char *) const;");
5743   verifyFormat("int a = sizeof(int *) + b;");
5744   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
5745 
5746   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
5747                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5748   // FIXME: The indentation here is not ideal.
5749   verifyFormat(
5750       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5751       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
5752       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
5753 }
5754 
5755 TEST_F(FormatTest, FormatsFunctionTypes) {
5756   verifyFormat("A<bool()> a;");
5757   verifyFormat("A<SomeType()> a;");
5758   verifyFormat("A<void (*)(int, std::string)> a;");
5759   verifyFormat("A<void *(int)>;");
5760   verifyFormat("void *(*a)(int *, SomeType *);");
5761   verifyFormat("int (*func)(void *);");
5762   verifyFormat("void f() { int (*func)(void *); }");
5763   verifyFormat("template <class CallbackClass>\n"
5764                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
5765 
5766   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
5767   verifyGoogleFormat("void* (*a)(int);");
5768   verifyGoogleFormat(
5769       "template <class CallbackClass>\n"
5770       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
5771 
5772   // Other constructs can look somewhat like function types:
5773   verifyFormat("A<sizeof(*x)> a;");
5774   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
5775   verifyFormat("some_var = function(*some_pointer_var)[0];");
5776   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
5777 }
5778 
5779 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
5780   verifyFormat("A (*foo_)[6];");
5781   verifyFormat("vector<int> (*foo_)[6];");
5782 }
5783 
5784 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
5785   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5786                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5787   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
5788                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5789   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5790                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
5791 
5792   // Different ways of ()-initializiation.
5793   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5794                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
5795   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5796                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
5797   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5798                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
5799   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5800                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
5801 }
5802 
5803 TEST_F(FormatTest, BreaksLongDeclarations) {
5804   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
5805                "    AnotherNameForTheLongType;");
5806   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
5807                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5808   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5809                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5810   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
5811                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5812   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5813                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5814   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
5815                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5816   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5817                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5818   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5819                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5820   FormatStyle Indented = getLLVMStyle();
5821   Indented.IndentWrappedFunctionNames = true;
5822   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5823                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
5824                Indented);
5825   verifyFormat(
5826       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5827       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5828       Indented);
5829   verifyFormat(
5830       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5831       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5832       Indented);
5833   verifyFormat(
5834       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5835       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5836       Indented);
5837 
5838   // FIXME: Without the comment, this breaks after "(".
5839   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
5840                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
5841                getGoogleStyle());
5842 
5843   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
5844                "                  int LoooooooooooooooooooongParam2) {}");
5845   verifyFormat(
5846       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
5847       "                                   SourceLocation L, IdentifierIn *II,\n"
5848       "                                   Type *T) {}");
5849   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
5850                "ReallyReaaallyLongFunctionName(\n"
5851                "    const std::string &SomeParameter,\n"
5852                "    const SomeType<string, SomeOtherTemplateParameter>\n"
5853                "        &ReallyReallyLongParameterName,\n"
5854                "    const SomeType<string, SomeOtherTemplateParameter>\n"
5855                "        &AnotherLongParameterName) {}");
5856   verifyFormat("template <typename A>\n"
5857                "SomeLoooooooooooooooooooooongType<\n"
5858                "    typename some_namespace::SomeOtherType<A>::Type>\n"
5859                "Function() {}");
5860 
5861   verifyGoogleFormat(
5862       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
5863       "    aaaaaaaaaaaaaaaaaaaaaaa;");
5864   verifyGoogleFormat(
5865       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
5866       "                                   SourceLocation L) {}");
5867   verifyGoogleFormat(
5868       "some_namespace::LongReturnType\n"
5869       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
5870       "    int first_long_parameter, int second_parameter) {}");
5871 
5872   verifyGoogleFormat("template <typename T>\n"
5873                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5874                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
5875   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5876                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
5877 
5878   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5879                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5880                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5881   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5882                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5883                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
5884   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5885                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5886                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
5887                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5888 }
5889 
5890 TEST_F(FormatTest, FormatsArrays) {
5891   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5892                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
5893   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5894                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5895   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5896                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
5897   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5898                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5899                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5900   verifyFormat(
5901       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
5902       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5903       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
5904 
5905   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
5906                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
5907   verifyFormat(
5908       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
5909       "                                  .aaaaaaa[0]\n"
5910       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
5911 
5912   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
5913 }
5914 
5915 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
5916   verifyFormat("(a)->b();");
5917   verifyFormat("--a;");
5918 }
5919 
5920 TEST_F(FormatTest, HandlesIncludeDirectives) {
5921   verifyFormat("#include <string>\n"
5922                "#include <a/b/c.h>\n"
5923                "#include \"a/b/string\"\n"
5924                "#include \"string.h\"\n"
5925                "#include \"string.h\"\n"
5926                "#include <a-a>\n"
5927                "#include < path with space >\n"
5928                "#include_next <test.h>"
5929                "#include \"abc.h\" // this is included for ABC\n"
5930                "#include \"some long include\" // with a comment\n"
5931                "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
5932                getLLVMStyleWithColumns(35));
5933   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
5934   EXPECT_EQ("#include <a>", format("#include<a>"));
5935 
5936   verifyFormat("#import <string>");
5937   verifyFormat("#import <a/b/c.h>");
5938   verifyFormat("#import \"a/b/string\"");
5939   verifyFormat("#import \"string.h\"");
5940   verifyFormat("#import \"string.h\"");
5941   verifyFormat("#if __has_include(<strstream>)\n"
5942                "#include <strstream>\n"
5943                "#endif");
5944 
5945   verifyFormat("#define MY_IMPORT <a/b>");
5946 
5947   // Protocol buffer definition or missing "#".
5948   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
5949                getLLVMStyleWithColumns(30));
5950 
5951   FormatStyle Style = getLLVMStyle();
5952   Style.AlwaysBreakBeforeMultilineStrings = true;
5953   Style.ColumnLimit = 0;
5954   verifyFormat("#import \"abc.h\"", Style);
5955 
5956   // But 'import' might also be a regular C++ namespace.
5957   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5958                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5959 }
5960 
5961 //===----------------------------------------------------------------------===//
5962 // Error recovery tests.
5963 //===----------------------------------------------------------------------===//
5964 
5965 TEST_F(FormatTest, IncompleteParameterLists) {
5966   FormatStyle NoBinPacking = getLLVMStyle();
5967   NoBinPacking.BinPackParameters = false;
5968   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
5969                "                        double *min_x,\n"
5970                "                        double *max_x,\n"
5971                "                        double *min_y,\n"
5972                "                        double *max_y,\n"
5973                "                        double *min_z,\n"
5974                "                        double *max_z, ) {}",
5975                NoBinPacking);
5976 }
5977 
5978 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
5979   verifyFormat("void f() { return; }\n42");
5980   verifyFormat("void f() {\n"
5981                "  if (0)\n"
5982                "    return;\n"
5983                "}\n"
5984                "42");
5985   verifyFormat("void f() { return }\n42");
5986   verifyFormat("void f() {\n"
5987                "  if (0)\n"
5988                "    return\n"
5989                "}\n"
5990                "42");
5991 }
5992 
5993 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
5994   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
5995   EXPECT_EQ("void f() {\n"
5996             "  if (a)\n"
5997             "    return\n"
5998             "}",
5999             format("void  f  (  )  {  if  ( a )  return  }"));
6000   EXPECT_EQ("namespace N {\n"
6001             "void f()\n"
6002             "}",
6003             format("namespace  N  {  void f()  }"));
6004   EXPECT_EQ("namespace N {\n"
6005             "void f() {}\n"
6006             "void g()\n"
6007             "}",
6008             format("namespace N  { void f( ) { } void g( ) }"));
6009 }
6010 
6011 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
6012   verifyFormat("int aaaaaaaa =\n"
6013                "    // Overlylongcomment\n"
6014                "    b;",
6015                getLLVMStyleWithColumns(20));
6016   verifyFormat("function(\n"
6017                "    ShortArgument,\n"
6018                "    LoooooooooooongArgument);\n",
6019                getLLVMStyleWithColumns(20));
6020 }
6021 
6022 TEST_F(FormatTest, IncorrectAccessSpecifier) {
6023   verifyFormat("public:");
6024   verifyFormat("class A {\n"
6025                "public\n"
6026                "  void f() {}\n"
6027                "};");
6028   verifyFormat("public\n"
6029                "int qwerty;");
6030   verifyFormat("public\n"
6031                "B {}");
6032   verifyFormat("public\n"
6033                "{}");
6034   verifyFormat("public\n"
6035                "B { int x; }");
6036 }
6037 
6038 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
6039   verifyFormat("{");
6040   verifyFormat("#})");
6041   verifyNoCrash("(/**/[:!] ?[).");
6042 }
6043 
6044 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
6045   verifyFormat("do {\n}");
6046   verifyFormat("do {\n}\n"
6047                "f();");
6048   verifyFormat("do {\n}\n"
6049                "wheeee(fun);");
6050   verifyFormat("do {\n"
6051                "  f();\n"
6052                "}");
6053 }
6054 
6055 TEST_F(FormatTest, IncorrectCodeMissingParens) {
6056   verifyFormat("if {\n  foo;\n  foo();\n}");
6057   verifyFormat("switch {\n  foo;\n  foo();\n}");
6058   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
6059   verifyFormat("while {\n  foo;\n  foo();\n}");
6060   verifyFormat("do {\n  foo;\n  foo();\n} while;");
6061 }
6062 
6063 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
6064   verifyIncompleteFormat("namespace {\n"
6065                          "class Foo { Foo (\n"
6066                          "};\n"
6067                          "} // comment");
6068 }
6069 
6070 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
6071   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
6072   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
6073   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
6074   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
6075 
6076   EXPECT_EQ("{\n"
6077             "  {\n"
6078             "    breakme(\n"
6079             "        qwe);\n"
6080             "  }\n",
6081             format("{\n"
6082                    "    {\n"
6083                    " breakme(qwe);\n"
6084                    "}\n",
6085                    getLLVMStyleWithColumns(10)));
6086 }
6087 
6088 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
6089   verifyFormat("int x = {\n"
6090                "    avariable,\n"
6091                "    b(alongervariable)};",
6092                getLLVMStyleWithColumns(25));
6093 }
6094 
6095 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
6096   verifyFormat("return (a)(b){1, 2, 3};");
6097 }
6098 
6099 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
6100   verifyFormat("vector<int> x{1, 2, 3, 4};");
6101   verifyFormat("vector<int> x{\n"
6102                "    1, 2, 3, 4,\n"
6103                "};");
6104   verifyFormat("vector<T> x{{}, {}, {}, {}};");
6105   verifyFormat("f({1, 2});");
6106   verifyFormat("auto v = Foo{-1};");
6107   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
6108   verifyFormat("Class::Class : member{1, 2, 3} {}");
6109   verifyFormat("new vector<int>{1, 2, 3};");
6110   verifyFormat("new int[3]{1, 2, 3};");
6111   verifyFormat("new int{1};");
6112   verifyFormat("return {arg1, arg2};");
6113   verifyFormat("return {arg1, SomeType{parameter}};");
6114   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
6115   verifyFormat("new T{arg1, arg2};");
6116   verifyFormat("f(MyMap[{composite, key}]);");
6117   verifyFormat("class Class {\n"
6118                "  T member = {arg1, arg2};\n"
6119                "};");
6120   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
6121   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
6122   verifyFormat("int a = std::is_integral<int>{} + 0;");
6123 
6124   verifyFormat("int foo(int i) { return fo1{}(i); }");
6125   verifyFormat("int foo(int i) { return fo1{}(i); }");
6126   verifyFormat("auto i = decltype(x){};");
6127   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
6128   verifyFormat("Node n{1, Node{1000}, //\n"
6129                "       2};");
6130   verifyFormat("Aaaa aaaaaaa{\n"
6131                "    {\n"
6132                "        aaaa,\n"
6133                "    },\n"
6134                "};");
6135   verifyFormat("class C : public D {\n"
6136                "  SomeClass SC{2};\n"
6137                "};");
6138   verifyFormat("class C : public A {\n"
6139                "  class D : public B {\n"
6140                "    void f() { int i{2}; }\n"
6141                "  };\n"
6142                "};");
6143 
6144   // In combination with BinPackArguments = false.
6145   FormatStyle NoBinPacking = getLLVMStyle();
6146   NoBinPacking.BinPackArguments = false;
6147   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
6148                "                      bbbbb,\n"
6149                "                      ccccc,\n"
6150                "                      ddddd,\n"
6151                "                      eeeee,\n"
6152                "                      ffffff,\n"
6153                "                      ggggg,\n"
6154                "                      hhhhhh,\n"
6155                "                      iiiiii,\n"
6156                "                      jjjjjj,\n"
6157                "                      kkkkkk};",
6158                NoBinPacking);
6159   verifyFormat("const Aaaaaa aaaaa = {\n"
6160                "    aaaaa,\n"
6161                "    bbbbb,\n"
6162                "    ccccc,\n"
6163                "    ddddd,\n"
6164                "    eeeee,\n"
6165                "    ffffff,\n"
6166                "    ggggg,\n"
6167                "    hhhhhh,\n"
6168                "    iiiiii,\n"
6169                "    jjjjjj,\n"
6170                "    kkkkkk,\n"
6171                "};",
6172                NoBinPacking);
6173   verifyFormat(
6174       "const Aaaaaa aaaaa = {\n"
6175       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
6176       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
6177       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
6178       "};",
6179       NoBinPacking);
6180 
6181   // FIXME: The alignment of these trailing comments might be bad. Then again,
6182   // this might be utterly useless in real code.
6183   verifyFormat("Constructor::Constructor()\n"
6184                "    : some_value{         //\n"
6185                "                 aaaaaaa, //\n"
6186                "                 bbbbbbb} {}");
6187 
6188   // In braced lists, the first comment is always assumed to belong to the
6189   // first element. Thus, it can be moved to the next or previous line as
6190   // appropriate.
6191   EXPECT_EQ("function({// First element:\n"
6192             "          1,\n"
6193             "          // Second element:\n"
6194             "          2});",
6195             format("function({\n"
6196                    "    // First element:\n"
6197                    "    1,\n"
6198                    "    // Second element:\n"
6199                    "    2});"));
6200   EXPECT_EQ("std::vector<int> MyNumbers{\n"
6201             "    // First element:\n"
6202             "    1,\n"
6203             "    // Second element:\n"
6204             "    2};",
6205             format("std::vector<int> MyNumbers{// First element:\n"
6206                    "                           1,\n"
6207                    "                           // Second element:\n"
6208                    "                           2};",
6209                    getLLVMStyleWithColumns(30)));
6210   // A trailing comma should still lead to an enforced line break.
6211   EXPECT_EQ("vector<int> SomeVector = {\n"
6212             "    // aaa\n"
6213             "    1, 2,\n"
6214             "};",
6215             format("vector<int> SomeVector = { // aaa\n"
6216                    "    1, 2, };"));
6217 
6218   FormatStyle ExtraSpaces = getLLVMStyle();
6219   ExtraSpaces.Cpp11BracedListStyle = false;
6220   ExtraSpaces.ColumnLimit = 75;
6221   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
6222   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
6223   verifyFormat("f({ 1, 2 });", ExtraSpaces);
6224   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
6225   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
6226   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
6227   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
6228   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
6229   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
6230   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
6231   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
6232   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
6233   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
6234   verifyFormat("class Class {\n"
6235                "  T member = { arg1, arg2 };\n"
6236                "};",
6237                ExtraSpaces);
6238   verifyFormat(
6239       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6240       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
6241       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6242       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
6243       ExtraSpaces);
6244   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
6245   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
6246                ExtraSpaces);
6247   verifyFormat(
6248       "someFunction(OtherParam,\n"
6249       "             BracedList{ // comment 1 (Forcing interesting break)\n"
6250       "                         param1, param2,\n"
6251       "                         // comment 2\n"
6252       "                         param3, param4 });",
6253       ExtraSpaces);
6254   verifyFormat(
6255       "std::this_thread::sleep_for(\n"
6256       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
6257       ExtraSpaces);
6258   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
6259                "    aaaaaaa,\n"
6260                "    aaaaaaaaaa,\n"
6261                "    aaaaa,\n"
6262                "    aaaaaaaaaaaaaaa,\n"
6263                "    aaa,\n"
6264                "    aaaaaaaaaa,\n"
6265                "    a,\n"
6266                "    aaaaaaaaaaaaaaaaaaaaa,\n"
6267                "    aaaaaaaaaaaa,\n"
6268                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
6269                "    aaaaaaa,\n"
6270                "    a};");
6271   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
6272 }
6273 
6274 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
6275   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6276                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6277                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6278                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6279                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6280                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6281   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
6282                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6283                "                 1, 22, 333, 4444, 55555, //\n"
6284                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6285                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6286   verifyFormat(
6287       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6288       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6289       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
6290       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6291       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6292       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6293       "                 7777777};");
6294   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6295                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6296                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6297   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6298                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6299                "    // Separating comment.\n"
6300                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
6301   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6302                "    // Leading comment\n"
6303                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6304                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6305   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6306                "                 1, 1, 1, 1};",
6307                getLLVMStyleWithColumns(39));
6308   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6309                "                 1, 1, 1, 1};",
6310                getLLVMStyleWithColumns(38));
6311   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
6312                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
6313                getLLVMStyleWithColumns(43));
6314   verifyFormat(
6315       "static unsigned SomeValues[10][3] = {\n"
6316       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
6317       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
6318   verifyFormat("static auto fields = new vector<string>{\n"
6319                "    \"aaaaaaaaaaaaa\",\n"
6320                "    \"aaaaaaaaaaaaa\",\n"
6321                "    \"aaaaaaaaaaaa\",\n"
6322                "    \"aaaaaaaaaaaaaa\",\n"
6323                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6324                "    \"aaaaaaaaaaaa\",\n"
6325                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6326                "};");
6327   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
6328   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
6329                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
6330                "                 3, cccccccccccccccccccccc};",
6331                getLLVMStyleWithColumns(60));
6332 
6333   // Trailing commas.
6334   verifyFormat("vector<int> x = {\n"
6335                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
6336                "};",
6337                getLLVMStyleWithColumns(39));
6338   verifyFormat("vector<int> x = {\n"
6339                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
6340                "};",
6341                getLLVMStyleWithColumns(39));
6342   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6343                "                 1, 1, 1, 1,\n"
6344                "                 /**/ /**/};",
6345                getLLVMStyleWithColumns(39));
6346 
6347   // Trailing comment in the first line.
6348   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
6349                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
6350                "    111111111,  222222222,  3333333333,  444444444,  //\n"
6351                "    11111111,   22222222,   333333333,   44444444};");
6352   // Trailing comment in the last line.
6353   verifyFormat("int aaaaa[] = {\n"
6354                "    1, 2, 3, // comment\n"
6355                "    4, 5, 6  // comment\n"
6356                "};");
6357 
6358   // With nested lists, we should either format one item per line or all nested
6359   // lists one on line.
6360   // FIXME: For some nested lists, we can do better.
6361   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
6362                "        {aaaaaaaaaaaaaaaaaaa},\n"
6363                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
6364                "        {aaaaaaaaaaaaaaaaa}};",
6365                getLLVMStyleWithColumns(60));
6366   verifyFormat(
6367       "SomeStruct my_struct_array = {\n"
6368       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
6369       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
6370       "    {aaa, aaa},\n"
6371       "    {aaa, aaa},\n"
6372       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
6373       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6374       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
6375 
6376   // No column layout should be used here.
6377   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
6378                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
6379 
6380   verifyNoCrash("a<,");
6381 }
6382 
6383 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
6384   FormatStyle DoNotMerge = getLLVMStyle();
6385   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6386 
6387   verifyFormat("void f() { return 42; }");
6388   verifyFormat("void f() {\n"
6389                "  return 42;\n"
6390                "}",
6391                DoNotMerge);
6392   verifyFormat("void f() {\n"
6393                "  // Comment\n"
6394                "}");
6395   verifyFormat("{\n"
6396                "#error {\n"
6397                "  int a;\n"
6398                "}");
6399   verifyFormat("{\n"
6400                "  int a;\n"
6401                "#error {\n"
6402                "}");
6403   verifyFormat("void f() {} // comment");
6404   verifyFormat("void f() { int a; } // comment");
6405   verifyFormat("void f() {\n"
6406                "} // comment",
6407                DoNotMerge);
6408   verifyFormat("void f() {\n"
6409                "  int a;\n"
6410                "} // comment",
6411                DoNotMerge);
6412   verifyFormat("void f() {\n"
6413                "} // comment",
6414                getLLVMStyleWithColumns(15));
6415 
6416   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
6417   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
6418 
6419   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
6420   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
6421   verifyFormat("class C {\n"
6422                "  C()\n"
6423                "      : iiiiiiii(nullptr),\n"
6424                "        kkkkkkk(nullptr),\n"
6425                "        mmmmmmm(nullptr),\n"
6426                "        nnnnnnn(nullptr) {}\n"
6427                "};",
6428                getGoogleStyle());
6429 
6430   FormatStyle NoColumnLimit = getLLVMStyle();
6431   NoColumnLimit.ColumnLimit = 0;
6432   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
6433   EXPECT_EQ("class C {\n"
6434             "  A() : b(0) {}\n"
6435             "};",
6436             format("class C{A():b(0){}};", NoColumnLimit));
6437   EXPECT_EQ("A()\n"
6438             "    : b(0) {\n"
6439             "}",
6440             format("A()\n:b(0)\n{\n}", NoColumnLimit));
6441 
6442   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
6443   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
6444       FormatStyle::SFS_None;
6445   EXPECT_EQ("A()\n"
6446             "    : b(0) {\n"
6447             "}",
6448             format("A():b(0){}", DoNotMergeNoColumnLimit));
6449   EXPECT_EQ("A()\n"
6450             "    : b(0) {\n"
6451             "}",
6452             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
6453 
6454   verifyFormat("#define A          \\\n"
6455                "  void f() {       \\\n"
6456                "    int i;         \\\n"
6457                "  }",
6458                getLLVMStyleWithColumns(20));
6459   verifyFormat("#define A           \\\n"
6460                "  void f() { int i; }",
6461                getLLVMStyleWithColumns(21));
6462   verifyFormat("#define A            \\\n"
6463                "  void f() {         \\\n"
6464                "    int i;           \\\n"
6465                "  }                  \\\n"
6466                "  int j;",
6467                getLLVMStyleWithColumns(22));
6468   verifyFormat("#define A             \\\n"
6469                "  void f() { int i; } \\\n"
6470                "  int j;",
6471                getLLVMStyleWithColumns(23));
6472 }
6473 
6474 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
6475   FormatStyle MergeInlineOnly = getLLVMStyle();
6476   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
6477   verifyFormat("class C {\n"
6478                "  int f() { return 42; }\n"
6479                "};",
6480                MergeInlineOnly);
6481   verifyFormat("int f() {\n"
6482                "  return 42;\n"
6483                "}",
6484                MergeInlineOnly);
6485 }
6486 
6487 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
6488   // Elaborate type variable declarations.
6489   verifyFormat("struct foo a = {bar};\nint n;");
6490   verifyFormat("class foo a = {bar};\nint n;");
6491   verifyFormat("union foo a = {bar};\nint n;");
6492 
6493   // Elaborate types inside function definitions.
6494   verifyFormat("struct foo f() {}\nint n;");
6495   verifyFormat("class foo f() {}\nint n;");
6496   verifyFormat("union foo f() {}\nint n;");
6497 
6498   // Templates.
6499   verifyFormat("template <class X> void f() {}\nint n;");
6500   verifyFormat("template <struct X> void f() {}\nint n;");
6501   verifyFormat("template <union X> void f() {}\nint n;");
6502 
6503   // Actual definitions...
6504   verifyFormat("struct {\n} n;");
6505   verifyFormat(
6506       "template <template <class T, class Y>, class Z> class X {\n} n;");
6507   verifyFormat("union Z {\n  int n;\n} x;");
6508   verifyFormat("class MACRO Z {\n} n;");
6509   verifyFormat("class MACRO(X) Z {\n} n;");
6510   verifyFormat("class __attribute__(X) Z {\n} n;");
6511   verifyFormat("class __declspec(X) Z {\n} n;");
6512   verifyFormat("class A##B##C {\n} n;");
6513   verifyFormat("class alignas(16) Z {\n} n;");
6514   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
6515   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
6516 
6517   // Redefinition from nested context:
6518   verifyFormat("class A::B::C {\n} n;");
6519 
6520   // Template definitions.
6521   verifyFormat(
6522       "template <typename F>\n"
6523       "Matcher(const Matcher<F> &Other,\n"
6524       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
6525       "                             !is_same<F, T>::value>::type * = 0)\n"
6526       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
6527 
6528   // FIXME: This is still incorrectly handled at the formatter side.
6529   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
6530   verifyFormat("int i = SomeFunction(a<b, a> b);");
6531 
6532   // FIXME:
6533   // This now gets parsed incorrectly as class definition.
6534   // verifyFormat("class A<int> f() {\n}\nint n;");
6535 
6536   // Elaborate types where incorrectly parsing the structural element would
6537   // break the indent.
6538   verifyFormat("if (true)\n"
6539                "  class X x;\n"
6540                "else\n"
6541                "  f();\n");
6542 
6543   // This is simply incomplete. Formatting is not important, but must not crash.
6544   verifyFormat("class A:");
6545 }
6546 
6547 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
6548   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
6549             format("#error Leave     all         white!!!!! space* alone!\n"));
6550   EXPECT_EQ(
6551       "#warning Leave     all         white!!!!! space* alone!\n",
6552       format("#warning Leave     all         white!!!!! space* alone!\n"));
6553   EXPECT_EQ("#error 1", format("  #  error   1"));
6554   EXPECT_EQ("#warning 1", format("  #  warning 1"));
6555 }
6556 
6557 TEST_F(FormatTest, FormatHashIfExpressions) {
6558   verifyFormat("#if AAAA && BBBB");
6559   // FIXME: Come up with a better indentation for #elif.
6560   verifyFormat(
6561       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
6562       "    defined(BBBBBBBB)\n"
6563       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
6564       "    defined(BBBBBBBB)\n"
6565       "#endif",
6566       getLLVMStyleWithColumns(65));
6567 }
6568 
6569 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
6570   FormatStyle AllowsMergedIf = getGoogleStyle();
6571   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
6572   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
6573   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
6574   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
6575   EXPECT_EQ("if (true) return 42;",
6576             format("if (true)\nreturn 42;", AllowsMergedIf));
6577   FormatStyle ShortMergedIf = AllowsMergedIf;
6578   ShortMergedIf.ColumnLimit = 25;
6579   verifyFormat("#define A \\\n"
6580                "  if (true) return 42;",
6581                ShortMergedIf);
6582   verifyFormat("#define A \\\n"
6583                "  f();    \\\n"
6584                "  if (true)\n"
6585                "#define B",
6586                ShortMergedIf);
6587   verifyFormat("#define A \\\n"
6588                "  f();    \\\n"
6589                "  if (true)\n"
6590                "g();",
6591                ShortMergedIf);
6592   verifyFormat("{\n"
6593                "#ifdef A\n"
6594                "  // Comment\n"
6595                "  if (true) continue;\n"
6596                "#endif\n"
6597                "  // Comment\n"
6598                "  if (true) continue;\n"
6599                "}",
6600                ShortMergedIf);
6601   ShortMergedIf.ColumnLimit = 29;
6602   verifyFormat("#define A                   \\\n"
6603                "  if (aaaaaaaaaa) return 1; \\\n"
6604                "  return 2;",
6605                ShortMergedIf);
6606   ShortMergedIf.ColumnLimit = 28;
6607   verifyFormat("#define A         \\\n"
6608                "  if (aaaaaaaaaa) \\\n"
6609                "    return 1;     \\\n"
6610                "  return 2;",
6611                ShortMergedIf);
6612 }
6613 
6614 TEST_F(FormatTest, BlockCommentsInControlLoops) {
6615   verifyFormat("if (0) /* a comment in a strange place */ {\n"
6616                "  f();\n"
6617                "}");
6618   verifyFormat("if (0) /* a comment in a strange place */ {\n"
6619                "  f();\n"
6620                "} /* another comment */ else /* comment #3 */ {\n"
6621                "  g();\n"
6622                "}");
6623   verifyFormat("while (0) /* a comment in a strange place */ {\n"
6624                "  f();\n"
6625                "}");
6626   verifyFormat("for (;;) /* a comment in a strange place */ {\n"
6627                "  f();\n"
6628                "}");
6629   verifyFormat("do /* a comment in a strange place */ {\n"
6630                "  f();\n"
6631                "} /* another comment */ while (0);");
6632 }
6633 
6634 TEST_F(FormatTest, BlockComments) {
6635   EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
6636             format("/* *//* */  /* */\n/* *//* */  /* */"));
6637   EXPECT_EQ("/* */ a /* */ b;", format("  /* */  a/* */  b;"));
6638   EXPECT_EQ("#define A /*123*/ \\\n"
6639             "  b\n"
6640             "/* */\n"
6641             "someCall(\n"
6642             "    parameter);",
6643             format("#define A /*123*/ b\n"
6644                    "/* */\n"
6645                    "someCall(parameter);",
6646                    getLLVMStyleWithColumns(15)));
6647 
6648   EXPECT_EQ("#define A\n"
6649             "/* */ someCall(\n"
6650             "    parameter);",
6651             format("#define A\n"
6652                    "/* */someCall(parameter);",
6653                    getLLVMStyleWithColumns(15)));
6654   EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
6655   EXPECT_EQ("/*\n"
6656             "*\n"
6657             " * aaaaaa\n"
6658             " * aaaaaa\n"
6659             "*/",
6660             format("/*\n"
6661                    "*\n"
6662                    " * aaaaaa aaaaaa\n"
6663                    "*/",
6664                    getLLVMStyleWithColumns(10)));
6665   EXPECT_EQ("/*\n"
6666             "**\n"
6667             "* aaaaaa\n"
6668             "*aaaaaa\n"
6669             "*/",
6670             format("/*\n"
6671                    "**\n"
6672                    "* aaaaaa aaaaaa\n"
6673                    "*/",
6674                    getLLVMStyleWithColumns(10)));
6675 
6676   FormatStyle NoBinPacking = getLLVMStyle();
6677   NoBinPacking.BinPackParameters = false;
6678   EXPECT_EQ("someFunction(1, /* comment 1 */\n"
6679             "             2, /* comment 2 */\n"
6680             "             3, /* comment 3 */\n"
6681             "             aaaa,\n"
6682             "             bbbb);",
6683             format("someFunction (1,   /* comment 1 */\n"
6684                    "                2, /* comment 2 */  \n"
6685                    "               3,   /* comment 3 */\n"
6686                    "aaaa, bbbb );",
6687                    NoBinPacking));
6688   verifyFormat(
6689       "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6690       "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6691   EXPECT_EQ(
6692       "bool aaaaaaaaaaaaa = /* trailing comment */\n"
6693       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6694       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
6695       format(
6696           "bool       aaaaaaaaaaaaa =       /* trailing comment */\n"
6697           "    aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa    ||\n"
6698           "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa   || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
6699   EXPECT_EQ(
6700       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
6701       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   /* comment */\n"
6702       "int cccccccccccccccccccccccccccccc;       /* comment */\n",
6703       format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
6704              "int      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
6705              "int    cccccccccccccccccccccccccccccc;  /* comment */\n"));
6706 
6707   verifyFormat("void f(int * /* unused */) {}");
6708 
6709   EXPECT_EQ("/*\n"
6710             " **\n"
6711             " */",
6712             format("/*\n"
6713                    " **\n"
6714                    " */"));
6715   EXPECT_EQ("/*\n"
6716             " *q\n"
6717             " */",
6718             format("/*\n"
6719                    " *q\n"
6720                    " */"));
6721   EXPECT_EQ("/*\n"
6722             " * q\n"
6723             " */",
6724             format("/*\n"
6725                    " * q\n"
6726                    " */"));
6727   EXPECT_EQ("/*\n"
6728             " **/",
6729             format("/*\n"
6730                    " **/"));
6731   EXPECT_EQ("/*\n"
6732             " ***/",
6733             format("/*\n"
6734                    " ***/"));
6735 }
6736 
6737 TEST_F(FormatTest, BlockCommentsInMacros) {
6738   EXPECT_EQ("#define A          \\\n"
6739             "  {                \\\n"
6740             "    /* one line */ \\\n"
6741             "    someCall();",
6742             format("#define A {        \\\n"
6743                    "  /* one line */   \\\n"
6744                    "  someCall();",
6745                    getLLVMStyleWithColumns(20)));
6746   EXPECT_EQ("#define A          \\\n"
6747             "  {                \\\n"
6748             "    /* previous */ \\\n"
6749             "    /* one line */ \\\n"
6750             "    someCall();",
6751             format("#define A {        \\\n"
6752                    "  /* previous */   \\\n"
6753                    "  /* one line */   \\\n"
6754                    "  someCall();",
6755                    getLLVMStyleWithColumns(20)));
6756 }
6757 
6758 TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
6759   EXPECT_EQ("a = {\n"
6760             "    1111 /*    */\n"
6761             "};",
6762             format("a = {1111 /*    */\n"
6763                    "};",
6764                    getLLVMStyleWithColumns(15)));
6765   EXPECT_EQ("a = {\n"
6766             "    1111 /*      */\n"
6767             "};",
6768             format("a = {1111 /*      */\n"
6769                    "};",
6770                    getLLVMStyleWithColumns(15)));
6771 
6772   // FIXME: The formatting is still wrong here.
6773   EXPECT_EQ("a = {\n"
6774             "    1111 /*      a\n"
6775             "            */\n"
6776             "};",
6777             format("a = {1111 /*      a */\n"
6778                    "};",
6779                    getLLVMStyleWithColumns(15)));
6780 }
6781 
6782 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
6783   // FIXME: This is not what we want...
6784   verifyFormat("{\n"
6785                "// a"
6786                "// b");
6787 }
6788 
6789 TEST_F(FormatTest, FormatStarDependingOnContext) {
6790   verifyFormat("void f(int *a);");
6791   verifyFormat("void f() { f(fint * b); }");
6792   verifyFormat("class A {\n  void f(int *a);\n};");
6793   verifyFormat("class A {\n  int *a;\n};");
6794   verifyFormat("namespace a {\n"
6795                "namespace b {\n"
6796                "class A {\n"
6797                "  void f() {}\n"
6798                "  int *a;\n"
6799                "};\n"
6800                "}\n"
6801                "}");
6802 }
6803 
6804 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
6805   verifyFormat("while");
6806   verifyFormat("operator");
6807 }
6808 
6809 //===----------------------------------------------------------------------===//
6810 // Objective-C tests.
6811 //===----------------------------------------------------------------------===//
6812 
6813 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
6814   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
6815   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
6816             format("-(NSUInteger)indexOfObject:(id)anObject;"));
6817   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
6818   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
6819   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
6820             format("-(NSInteger)Method3:(id)anObject;"));
6821   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
6822             format("-(NSInteger)Method4:(id)anObject;"));
6823   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
6824             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
6825   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
6826             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
6827   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
6828             "forAllCells:(BOOL)flag;",
6829             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
6830                    "forAllCells:(BOOL)flag;"));
6831 
6832   // Very long objectiveC method declaration.
6833   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
6834                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
6835   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
6836                "                    inRange:(NSRange)range\n"
6837                "                   outRange:(NSRange)out_range\n"
6838                "                  outRange1:(NSRange)out_range1\n"
6839                "                  outRange2:(NSRange)out_range2\n"
6840                "                  outRange3:(NSRange)out_range3\n"
6841                "                  outRange4:(NSRange)out_range4\n"
6842                "                  outRange5:(NSRange)out_range5\n"
6843                "                  outRange6:(NSRange)out_range6\n"
6844                "                  outRange7:(NSRange)out_range7\n"
6845                "                  outRange8:(NSRange)out_range8\n"
6846                "                  outRange9:(NSRange)out_range9;");
6847 
6848   // When the function name has to be wrapped.
6849   FormatStyle Style = getLLVMStyle();
6850   Style.IndentWrappedFunctionNames = false;
6851   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
6852                "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
6853                "           anotherName:(NSString)bbbbbbbbbbbbbb {\n"
6854                "}",
6855                Style);
6856   Style.IndentWrappedFunctionNames = true;
6857   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
6858                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
6859                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
6860                "}",
6861                Style);
6862 
6863   verifyFormat("- (int)sum:(vector<int>)numbers;");
6864   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
6865   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
6866   // protocol lists (but not for template classes):
6867   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
6868 
6869   verifyFormat("- (int (*)())foo:(int (*)())f;");
6870   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
6871 
6872   // If there's no return type (very rare in practice!), LLVM and Google style
6873   // agree.
6874   verifyFormat("- foo;");
6875   verifyFormat("- foo:(int)f;");
6876   verifyGoogleFormat("- foo:(int)foo;");
6877 }
6878 
6879 TEST_F(FormatTest, FormatObjCInterface) {
6880   verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
6881                "@public\n"
6882                "  int field1;\n"
6883                "@protected\n"
6884                "  int field2;\n"
6885                "@private\n"
6886                "  int field3;\n"
6887                "@package\n"
6888                "  int field4;\n"
6889                "}\n"
6890                "+ (id)init;\n"
6891                "@end");
6892 
6893   verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
6894                      " @public\n"
6895                      "  int field1;\n"
6896                      " @protected\n"
6897                      "  int field2;\n"
6898                      " @private\n"
6899                      "  int field3;\n"
6900                      " @package\n"
6901                      "  int field4;\n"
6902                      "}\n"
6903                      "+ (id)init;\n"
6904                      "@end");
6905 
6906   verifyFormat("@interface /* wait for it */ Foo\n"
6907                "+ (id)init;\n"
6908                "// Look, a comment!\n"
6909                "- (int)answerWith:(int)i;\n"
6910                "@end");
6911 
6912   verifyFormat("@interface Foo\n"
6913                "@end\n"
6914                "@interface Bar\n"
6915                "@end");
6916 
6917   verifyFormat("@interface Foo : Bar\n"
6918                "+ (id)init;\n"
6919                "@end");
6920 
6921   verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
6922                "+ (id)init;\n"
6923                "@end");
6924 
6925   verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
6926                      "+ (id)init;\n"
6927                      "@end");
6928 
6929   verifyFormat("@interface Foo (HackStuff)\n"
6930                "+ (id)init;\n"
6931                "@end");
6932 
6933   verifyFormat("@interface Foo ()\n"
6934                "+ (id)init;\n"
6935                "@end");
6936 
6937   verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
6938                "+ (id)init;\n"
6939                "@end");
6940 
6941   verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
6942                      "+ (id)init;\n"
6943                      "@end");
6944 
6945   verifyFormat("@interface Foo {\n"
6946                "  int _i;\n"
6947                "}\n"
6948                "+ (id)init;\n"
6949                "@end");
6950 
6951   verifyFormat("@interface Foo : Bar {\n"
6952                "  int _i;\n"
6953                "}\n"
6954                "+ (id)init;\n"
6955                "@end");
6956 
6957   verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
6958                "  int _i;\n"
6959                "}\n"
6960                "+ (id)init;\n"
6961                "@end");
6962 
6963   verifyFormat("@interface Foo (HackStuff) {\n"
6964                "  int _i;\n"
6965                "}\n"
6966                "+ (id)init;\n"
6967                "@end");
6968 
6969   verifyFormat("@interface Foo () {\n"
6970                "  int _i;\n"
6971                "}\n"
6972                "+ (id)init;\n"
6973                "@end");
6974 
6975   verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
6976                "  int _i;\n"
6977                "}\n"
6978                "+ (id)init;\n"
6979                "@end");
6980 
6981   FormatStyle OnePerLine = getGoogleStyle();
6982   OnePerLine.BinPackParameters = false;
6983   verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n"
6984                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6985                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6986                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6987                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
6988                "}",
6989                OnePerLine);
6990 }
6991 
6992 TEST_F(FormatTest, FormatObjCImplementation) {
6993   verifyFormat("@implementation Foo : NSObject {\n"
6994                "@public\n"
6995                "  int field1;\n"
6996                "@protected\n"
6997                "  int field2;\n"
6998                "@private\n"
6999                "  int field3;\n"
7000                "@package\n"
7001                "  int field4;\n"
7002                "}\n"
7003                "+ (id)init {\n}\n"
7004                "@end");
7005 
7006   verifyGoogleFormat("@implementation Foo : NSObject {\n"
7007                      " @public\n"
7008                      "  int field1;\n"
7009                      " @protected\n"
7010                      "  int field2;\n"
7011                      " @private\n"
7012                      "  int field3;\n"
7013                      " @package\n"
7014                      "  int field4;\n"
7015                      "}\n"
7016                      "+ (id)init {\n}\n"
7017                      "@end");
7018 
7019   verifyFormat("@implementation Foo\n"
7020                "+ (id)init {\n"
7021                "  if (true)\n"
7022                "    return nil;\n"
7023                "}\n"
7024                "// Look, a comment!\n"
7025                "- (int)answerWith:(int)i {\n"
7026                "  return i;\n"
7027                "}\n"
7028                "+ (int)answerWith:(int)i {\n"
7029                "  return i;\n"
7030                "}\n"
7031                "@end");
7032 
7033   verifyFormat("@implementation Foo\n"
7034                "@end\n"
7035                "@implementation Bar\n"
7036                "@end");
7037 
7038   EXPECT_EQ("@implementation Foo : Bar\n"
7039             "+ (id)init {\n}\n"
7040             "- (void)foo {\n}\n"
7041             "@end",
7042             format("@implementation Foo : Bar\n"
7043                    "+(id)init{}\n"
7044                    "-(void)foo{}\n"
7045                    "@end"));
7046 
7047   verifyFormat("@implementation Foo {\n"
7048                "  int _i;\n"
7049                "}\n"
7050                "+ (id)init {\n}\n"
7051                "@end");
7052 
7053   verifyFormat("@implementation Foo : Bar {\n"
7054                "  int _i;\n"
7055                "}\n"
7056                "+ (id)init {\n}\n"
7057                "@end");
7058 
7059   verifyFormat("@implementation Foo (HackStuff)\n"
7060                "+ (id)init {\n}\n"
7061                "@end");
7062   verifyFormat("@implementation ObjcClass\n"
7063                "- (void)method;\n"
7064                "{}\n"
7065                "@end");
7066 }
7067 
7068 TEST_F(FormatTest, FormatObjCProtocol) {
7069   verifyFormat("@protocol Foo\n"
7070                "@property(weak) id delegate;\n"
7071                "- (NSUInteger)numberOfThings;\n"
7072                "@end");
7073 
7074   verifyFormat("@protocol MyProtocol <NSObject>\n"
7075                "- (NSUInteger)numberOfThings;\n"
7076                "@end");
7077 
7078   verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
7079                      "- (NSUInteger)numberOfThings;\n"
7080                      "@end");
7081 
7082   verifyFormat("@protocol Foo;\n"
7083                "@protocol Bar;\n");
7084 
7085   verifyFormat("@protocol Foo\n"
7086                "@end\n"
7087                "@protocol Bar\n"
7088                "@end");
7089 
7090   verifyFormat("@protocol myProtocol\n"
7091                "- (void)mandatoryWithInt:(int)i;\n"
7092                "@optional\n"
7093                "- (void)optional;\n"
7094                "@required\n"
7095                "- (void)required;\n"
7096                "@optional\n"
7097                "@property(assign) int madProp;\n"
7098                "@end\n");
7099 
7100   verifyFormat("@property(nonatomic, assign, readonly)\n"
7101                "    int *looooooooooooooooooooooooooooongNumber;\n"
7102                "@property(nonatomic, assign, readonly)\n"
7103                "    NSString *looooooooooooooooooooooooooooongName;");
7104 
7105   verifyFormat("@implementation PR18406\n"
7106                "}\n"
7107                "@end");
7108 }
7109 
7110 TEST_F(FormatTest, FormatObjCMethodDeclarations) {
7111   verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
7112                "                   rect:(NSRect)theRect\n"
7113                "               interval:(float)theInterval {\n"
7114                "}");
7115   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7116                "          longKeyword:(NSRect)theRect\n"
7117                "    evenLongerKeyword:(float)theInterval\n"
7118                "                error:(NSError **)theError {\n"
7119                "}");
7120   verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
7121                "                         y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
7122                "    NS_DESIGNATED_INITIALIZER;",
7123                getLLVMStyleWithColumns(60));
7124 
7125   // Continuation indent width should win over aligning colons if the function
7126   // name is long.
7127   FormatStyle continuationStyle = getGoogleStyle();
7128   continuationStyle.ColumnLimit = 40;
7129   continuationStyle.IndentWrappedFunctionNames = true;
7130   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7131                "    dontAlignNamef:(NSRect)theRect {\n"
7132                "}",
7133                continuationStyle);
7134 
7135   // Make sure we don't break aligning for short parameter names.
7136   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7137                "       aShortf:(NSRect)theRect {\n"
7138                "}",
7139                continuationStyle);
7140 }
7141 
7142 TEST_F(FormatTest, FormatObjCMethodExpr) {
7143   verifyFormat("[foo bar:baz];");
7144   verifyFormat("return [foo bar:baz];");
7145   verifyFormat("return (a)[foo bar:baz];");
7146   verifyFormat("f([foo bar:baz]);");
7147   verifyFormat("f(2, [foo bar:baz]);");
7148   verifyFormat("f(2, a ? b : c);");
7149   verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
7150 
7151   // Unary operators.
7152   verifyFormat("int a = +[foo bar:baz];");
7153   verifyFormat("int a = -[foo bar:baz];");
7154   verifyFormat("int a = ![foo bar:baz];");
7155   verifyFormat("int a = ~[foo bar:baz];");
7156   verifyFormat("int a = ++[foo bar:baz];");
7157   verifyFormat("int a = --[foo bar:baz];");
7158   verifyFormat("int a = sizeof [foo bar:baz];");
7159   verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
7160   verifyFormat("int a = &[foo bar:baz];");
7161   verifyFormat("int a = *[foo bar:baz];");
7162   // FIXME: Make casts work, without breaking f()[4].
7163   // verifyFormat("int a = (int)[foo bar:baz];");
7164   // verifyFormat("return (int)[foo bar:baz];");
7165   // verifyFormat("(void)[foo bar:baz];");
7166   verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
7167 
7168   // Binary operators.
7169   verifyFormat("[foo bar:baz], [foo bar:baz];");
7170   verifyFormat("[foo bar:baz] = [foo bar:baz];");
7171   verifyFormat("[foo bar:baz] *= [foo bar:baz];");
7172   verifyFormat("[foo bar:baz] /= [foo bar:baz];");
7173   verifyFormat("[foo bar:baz] %= [foo bar:baz];");
7174   verifyFormat("[foo bar:baz] += [foo bar:baz];");
7175   verifyFormat("[foo bar:baz] -= [foo bar:baz];");
7176   verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
7177   verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
7178   verifyFormat("[foo bar:baz] &= [foo bar:baz];");
7179   verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
7180   verifyFormat("[foo bar:baz] |= [foo bar:baz];");
7181   verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
7182   verifyFormat("[foo bar:baz] || [foo bar:baz];");
7183   verifyFormat("[foo bar:baz] && [foo bar:baz];");
7184   verifyFormat("[foo bar:baz] | [foo bar:baz];");
7185   verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
7186   verifyFormat("[foo bar:baz] & [foo bar:baz];");
7187   verifyFormat("[foo bar:baz] == [foo bar:baz];");
7188   verifyFormat("[foo bar:baz] != [foo bar:baz];");
7189   verifyFormat("[foo bar:baz] >= [foo bar:baz];");
7190   verifyFormat("[foo bar:baz] <= [foo bar:baz];");
7191   verifyFormat("[foo bar:baz] > [foo bar:baz];");
7192   verifyFormat("[foo bar:baz] < [foo bar:baz];");
7193   verifyFormat("[foo bar:baz] >> [foo bar:baz];");
7194   verifyFormat("[foo bar:baz] << [foo bar:baz];");
7195   verifyFormat("[foo bar:baz] - [foo bar:baz];");
7196   verifyFormat("[foo bar:baz] + [foo bar:baz];");
7197   verifyFormat("[foo bar:baz] * [foo bar:baz];");
7198   verifyFormat("[foo bar:baz] / [foo bar:baz];");
7199   verifyFormat("[foo bar:baz] % [foo bar:baz];");
7200   // Whew!
7201 
7202   verifyFormat("return in[42];");
7203   verifyFormat("for (auto v : in[1]) {\n}");
7204   verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
7205   verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
7206   verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
7207   verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
7208   verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
7209   verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
7210                "}");
7211   verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
7212   verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
7213   verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
7214 
7215   verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
7216   verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
7217   verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
7218   verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
7219   verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
7220   verifyFormat("[button setAction:@selector(zoomOut:)];");
7221   verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
7222 
7223   verifyFormat("arr[[self indexForFoo:a]];");
7224   verifyFormat("throw [self errorFor:a];");
7225   verifyFormat("@throw [self errorFor:a];");
7226 
7227   verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
7228   verifyFormat("[(id)foo bar:(id) ? baz : quux];");
7229   verifyFormat("4 > 4 ? (id)a : (id)baz;");
7230 
7231   // This tests that the formatter doesn't break after "backing" but before ":",
7232   // which would be at 80 columns.
7233   verifyFormat(
7234       "void f() {\n"
7235       "  if ((self = [super initWithContentRect:contentRect\n"
7236       "                               styleMask:styleMask ?: otherMask\n"
7237       "                                 backing:NSBackingStoreBuffered\n"
7238       "                                   defer:YES]))");
7239 
7240   verifyFormat(
7241       "[foo checkThatBreakingAfterColonWorksOk:\n"
7242       "         [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
7243 
7244   verifyFormat("[myObj short:arg1 // Force line break\n"
7245                "          longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
7246                "    evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
7247                "                error:arg4];");
7248   verifyFormat(
7249       "void f() {\n"
7250       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
7251       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
7252       "                                     pos.width(), pos.height())\n"
7253       "                styleMask:NSBorderlessWindowMask\n"
7254       "                  backing:NSBackingStoreBuffered\n"
7255       "                    defer:NO]);\n"
7256       "}");
7257   verifyFormat(
7258       "void f() {\n"
7259       "  popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
7260       "      iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
7261       "                                 pos.width(), pos.height())\n"
7262       "                syeMask:NSBorderlessWindowMask\n"
7263       "                  bking:NSBackingStoreBuffered\n"
7264       "                    der:NO]);\n"
7265       "}",
7266       getLLVMStyleWithColumns(70));
7267   verifyFormat(
7268       "void f() {\n"
7269       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
7270       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
7271       "                                     pos.width(), pos.height())\n"
7272       "                styleMask:NSBorderlessWindowMask\n"
7273       "                  backing:NSBackingStoreBuffered\n"
7274       "                    defer:NO]);\n"
7275       "}",
7276       getChromiumStyle(FormatStyle::LK_Cpp));
7277   verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
7278                "                             with:contentsNativeView];");
7279 
7280   verifyFormat(
7281       "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
7282       "           owner:nillllll];");
7283 
7284   verifyFormat(
7285       "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
7286       "        forType:kBookmarkButtonDragType];");
7287 
7288   verifyFormat("[defaultCenter addObserver:self\n"
7289                "                  selector:@selector(willEnterFullscreen)\n"
7290                "                      name:kWillEnterFullscreenNotification\n"
7291                "                    object:nil];");
7292   verifyFormat("[image_rep drawInRect:drawRect\n"
7293                "             fromRect:NSZeroRect\n"
7294                "            operation:NSCompositeCopy\n"
7295                "             fraction:1.0\n"
7296                "       respectFlipped:NO\n"
7297                "                hints:nil];");
7298   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7299                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
7300   verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
7301                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
7302   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
7303                "    aaaaaaaaaaaaaaaaaaaaaa];");
7304   verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
7305                "        .aaaaaaaa];", // FIXME: Indentation seems off.
7306                getLLVMStyleWithColumns(60));
7307 
7308   verifyFormat(
7309       "scoped_nsobject<NSTextField> message(\n"
7310       "    // The frame will be fixed up when |-setMessageText:| is called.\n"
7311       "    [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
7312   verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
7313                "    aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
7314                "         aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
7315                "          aaaa:bbb];");
7316   verifyFormat("[self param:function( //\n"
7317                "                parameter)]");
7318   verifyFormat(
7319       "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7320       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7321       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
7322 
7323   // FIXME: This violates the column limit.
7324   verifyFormat(
7325       "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
7326       "    aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
7327       "                  aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];",
7328       getLLVMStyleWithColumns(60));
7329 
7330   // Variadic parameters.
7331   verifyFormat(
7332       "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
7333   verifyFormat(
7334       "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7335       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7336       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
7337   verifyFormat("[self // break\n"
7338                "      a:a\n"
7339                "    aaa:aaa];");
7340   verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
7341                "          [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
7342 }
7343 
7344 TEST_F(FormatTest, ObjCAt) {
7345   verifyFormat("@autoreleasepool");
7346   verifyFormat("@catch");
7347   verifyFormat("@class");
7348   verifyFormat("@compatibility_alias");
7349   verifyFormat("@defs");
7350   verifyFormat("@dynamic");
7351   verifyFormat("@encode");
7352   verifyFormat("@end");
7353   verifyFormat("@finally");
7354   verifyFormat("@implementation");
7355   verifyFormat("@import");
7356   verifyFormat("@interface");
7357   verifyFormat("@optional");
7358   verifyFormat("@package");
7359   verifyFormat("@private");
7360   verifyFormat("@property");
7361   verifyFormat("@protected");
7362   verifyFormat("@protocol");
7363   verifyFormat("@public");
7364   verifyFormat("@required");
7365   verifyFormat("@selector");
7366   verifyFormat("@synchronized");
7367   verifyFormat("@synthesize");
7368   verifyFormat("@throw");
7369   verifyFormat("@try");
7370 
7371   EXPECT_EQ("@interface", format("@ interface"));
7372 
7373   // The precise formatting of this doesn't matter, nobody writes code like
7374   // this.
7375   verifyFormat("@ /*foo*/ interface");
7376 }
7377 
7378 TEST_F(FormatTest, ObjCSnippets) {
7379   verifyFormat("@autoreleasepool {\n"
7380                "  foo();\n"
7381                "}");
7382   verifyFormat("@class Foo, Bar;");
7383   verifyFormat("@compatibility_alias AliasName ExistingClass;");
7384   verifyFormat("@dynamic textColor;");
7385   verifyFormat("char *buf1 = @encode(int *);");
7386   verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
7387   verifyFormat("char *buf1 = @encode(int **);");
7388   verifyFormat("Protocol *proto = @protocol(p1);");
7389   verifyFormat("SEL s = @selector(foo:);");
7390   verifyFormat("@synchronized(self) {\n"
7391                "  f();\n"
7392                "}");
7393 
7394   verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7395   verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7396 
7397   verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
7398   verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
7399   verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
7400   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7401                getMozillaStyle());
7402   verifyFormat("@property BOOL editable;", getMozillaStyle());
7403   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7404                getWebKitStyle());
7405   verifyFormat("@property BOOL editable;", getWebKitStyle());
7406 
7407   verifyFormat("@import foo.bar;\n"
7408                "@import baz;");
7409 }
7410 
7411 TEST_F(FormatTest, ObjCLiterals) {
7412   verifyFormat("@\"String\"");
7413   verifyFormat("@1");
7414   verifyFormat("@+4.8");
7415   verifyFormat("@-4");
7416   verifyFormat("@1LL");
7417   verifyFormat("@.5");
7418   verifyFormat("@'c'");
7419   verifyFormat("@true");
7420 
7421   verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
7422   verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
7423   verifyFormat("NSNumber *favoriteColor = @(Green);");
7424   verifyFormat("NSString *path = @(getenv(\"PATH\"));");
7425 
7426   verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
7427 }
7428 
7429 TEST_F(FormatTest, ObjCDictLiterals) {
7430   verifyFormat("@{");
7431   verifyFormat("@{}");
7432   verifyFormat("@{@\"one\" : @1}");
7433   verifyFormat("return @{@\"one\" : @1;");
7434   verifyFormat("@{@\"one\" : @1}");
7435 
7436   verifyFormat("@{@\"one\" : @{@2 : @1}}");
7437   verifyFormat("@{\n"
7438                "  @\"one\" : @{@2 : @1},\n"
7439                "}");
7440 
7441   verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
7442   verifyIncompleteFormat("[self setDict:@{}");
7443   verifyIncompleteFormat("[self setDict:@{@1 : @2}");
7444   verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
7445   verifyFormat(
7446       "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
7447   verifyFormat(
7448       "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
7449 
7450   verifyFormat("NSDictionary *d = @{\n"
7451                "  @\"nam\" : NSUserNam(),\n"
7452                "  @\"dte\" : [NSDate date],\n"
7453                "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
7454                "};");
7455   verifyFormat(
7456       "@{\n"
7457       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7458       "regularFont,\n"
7459       "};");
7460   verifyGoogleFormat(
7461       "@{\n"
7462       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7463       "regularFont,\n"
7464       "};");
7465   verifyFormat(
7466       "@{\n"
7467       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
7468       "      reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
7469       "};");
7470 
7471   // We should try to be robust in case someone forgets the "@".
7472   verifyFormat("NSDictionary *d = {\n"
7473                "  @\"nam\" : NSUserNam(),\n"
7474                "  @\"dte\" : [NSDate date],\n"
7475                "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
7476                "};");
7477   verifyFormat("NSMutableDictionary *dictionary =\n"
7478                "    [NSMutableDictionary dictionaryWithDictionary:@{\n"
7479                "      aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
7480                "      bbbbbbbbbbbbbbbbbb : bbbbb,\n"
7481                "      cccccccccccccccc : ccccccccccccccc\n"
7482                "    }];");
7483 }
7484 
7485 TEST_F(FormatTest, ObjCArrayLiterals) {
7486   verifyIncompleteFormat("@[");
7487   verifyFormat("@[]");
7488   verifyFormat(
7489       "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
7490   verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
7491   verifyFormat("NSArray *array = @[ [foo description] ];");
7492 
7493   verifyFormat(
7494       "NSArray *some_variable = @[\n"
7495       "  aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
7496       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7497       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7498       "  @\"aaaaaaaaaaaaaaaaa\"\n"
7499       "];");
7500   verifyFormat("NSArray *some_variable = @[\n"
7501                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7502                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7503                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7504                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7505                "];");
7506   verifyGoogleFormat("NSArray *some_variable = @[\n"
7507                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7508                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7509                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7510                      "  @\"aaaaaaaaaaaaaaaaa\"\n"
7511                      "];");
7512   verifyFormat("NSArray *array = @[\n"
7513                "  @\"a\",\n"
7514                "  @\"a\",\n" // Trailing comma -> one per line.
7515                "];");
7516 
7517   // We should try to be robust in case someone forgets the "@".
7518   verifyFormat("NSArray *some_variable = [\n"
7519                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7520                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7521                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7522                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7523                "];");
7524   verifyFormat(
7525       "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
7526       "                                             index:(NSUInteger)index\n"
7527       "                                nonDigitAttributes:\n"
7528       "                                    (NSDictionary *)noDigitAttributes;");
7529   verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
7530                "  NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
7531                "]];");
7532 }
7533 
7534 TEST_F(FormatTest, BreaksStringLiterals) {
7535   EXPECT_EQ("\"some text \"\n"
7536             "\"other\";",
7537             format("\"some text other\";", getLLVMStyleWithColumns(12)));
7538   EXPECT_EQ("\"some text \"\n"
7539             "\"other\";",
7540             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
7541   EXPECT_EQ(
7542       "#define A  \\\n"
7543       "  \"some \"  \\\n"
7544       "  \"text \"  \\\n"
7545       "  \"other\";",
7546       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
7547   EXPECT_EQ(
7548       "#define A  \\\n"
7549       "  \"so \"    \\\n"
7550       "  \"text \"  \\\n"
7551       "  \"other\";",
7552       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
7553 
7554   EXPECT_EQ("\"some text\"",
7555             format("\"some text\"", getLLVMStyleWithColumns(1)));
7556   EXPECT_EQ("\"some text\"",
7557             format("\"some text\"", getLLVMStyleWithColumns(11)));
7558   EXPECT_EQ("\"some \"\n"
7559             "\"text\"",
7560             format("\"some text\"", getLLVMStyleWithColumns(10)));
7561   EXPECT_EQ("\"some \"\n"
7562             "\"text\"",
7563             format("\"some text\"", getLLVMStyleWithColumns(7)));
7564   EXPECT_EQ("\"some\"\n"
7565             "\" tex\"\n"
7566             "\"t\"",
7567             format("\"some text\"", getLLVMStyleWithColumns(6)));
7568   EXPECT_EQ("\"some\"\n"
7569             "\" tex\"\n"
7570             "\" and\"",
7571             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
7572   EXPECT_EQ("\"some\"\n"
7573             "\"/tex\"\n"
7574             "\"/and\"",
7575             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
7576 
7577   EXPECT_EQ("variable =\n"
7578             "    \"long string \"\n"
7579             "    \"literal\";",
7580             format("variable = \"long string literal\";",
7581                    getLLVMStyleWithColumns(20)));
7582 
7583   EXPECT_EQ("variable = f(\n"
7584             "    \"long string \"\n"
7585             "    \"literal\",\n"
7586             "    short,\n"
7587             "    loooooooooooooooooooong);",
7588             format("variable = f(\"long string literal\", short, "
7589                    "loooooooooooooooooooong);",
7590                    getLLVMStyleWithColumns(20)));
7591 
7592   EXPECT_EQ(
7593       "f(g(\"long string \"\n"
7594       "    \"literal\"),\n"
7595       "  b);",
7596       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
7597   EXPECT_EQ("f(g(\"long string \"\n"
7598             "    \"literal\",\n"
7599             "    a),\n"
7600             "  b);",
7601             format("f(g(\"long string literal\", a), b);",
7602                    getLLVMStyleWithColumns(20)));
7603   EXPECT_EQ(
7604       "f(\"one two\".split(\n"
7605       "    variable));",
7606       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
7607   EXPECT_EQ("f(\"one two three four five six \"\n"
7608             "  \"seven\".split(\n"
7609             "      really_looooong_variable));",
7610             format("f(\"one two three four five six seven\"."
7611                    "split(really_looooong_variable));",
7612                    getLLVMStyleWithColumns(33)));
7613 
7614   EXPECT_EQ("f(\"some \"\n"
7615             "  \"text\",\n"
7616             "  other);",
7617             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
7618 
7619   // Only break as a last resort.
7620   verifyFormat(
7621       "aaaaaaaaaaaaaaaaaaaa(\n"
7622       "    aaaaaaaaaaaaaaaaaaaa,\n"
7623       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
7624 
7625   EXPECT_EQ("\"splitmea\"\n"
7626             "\"trandomp\"\n"
7627             "\"oint\"",
7628             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
7629 
7630   EXPECT_EQ("\"split/\"\n"
7631             "\"pathat/\"\n"
7632             "\"slashes\"",
7633             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7634 
7635   EXPECT_EQ("\"split/\"\n"
7636             "\"pathat/\"\n"
7637             "\"slashes\"",
7638             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7639   EXPECT_EQ("\"split at \"\n"
7640             "\"spaces/at/\"\n"
7641             "\"slashes.at.any$\"\n"
7642             "\"non-alphanumeric%\"\n"
7643             "\"1111111111characte\"\n"
7644             "\"rs\"",
7645             format("\"split at "
7646                    "spaces/at/"
7647                    "slashes.at."
7648                    "any$non-"
7649                    "alphanumeric%"
7650                    "1111111111characte"
7651                    "rs\"",
7652                    getLLVMStyleWithColumns(20)));
7653 
7654   // Verify that splitting the strings understands
7655   // Style::AlwaysBreakBeforeMultilineStrings.
7656   EXPECT_EQ(
7657       "aaaaaaaaaaaa(\n"
7658       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
7659       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
7660       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
7661              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7662              "aaaaaaaaaaaaaaaaaaaaaa\");",
7663              getGoogleStyle()));
7664   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7665             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
7666             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
7667                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7668                    "aaaaaaaaaaaaaaaaaaaaaa\";",
7669                    getGoogleStyle()));
7670   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7671             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7672             format("llvm::outs() << "
7673                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
7674                    "aaaaaaaaaaaaaaaaaaa\";"));
7675   EXPECT_EQ("ffff(\n"
7676             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7677             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7678             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
7679                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7680                    getGoogleStyle()));
7681 
7682   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
7683   AlignLeft.AlignEscapedNewlinesLeft = true;
7684   EXPECT_EQ("#define A \\\n"
7685             "  \"some \" \\\n"
7686             "  \"text \" \\\n"
7687             "  \"other\";",
7688             format("#define A \"some text other\";", AlignLeft));
7689 }
7690 
7691 TEST_F(FormatTest, FullyRemoveEmptyLines) {
7692   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
7693   NoEmptyLines.MaxEmptyLinesToKeep = 0;
7694   EXPECT_EQ("int i = a(b());",
7695             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
7696 }
7697 
7698 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
7699   EXPECT_EQ(
7700       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7701       "(\n"
7702       "    \"x\t\");",
7703       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7704              "aaaaaaa("
7705              "\"x\t\");"));
7706 }
7707 
7708 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
7709   EXPECT_EQ(
7710       "u8\"utf8 string \"\n"
7711       "u8\"literal\";",
7712       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
7713   EXPECT_EQ(
7714       "u\"utf16 string \"\n"
7715       "u\"literal\";",
7716       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
7717   EXPECT_EQ(
7718       "U\"utf32 string \"\n"
7719       "U\"literal\";",
7720       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
7721   EXPECT_EQ("L\"wide string \"\n"
7722             "L\"literal\";",
7723             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
7724   EXPECT_EQ("@\"NSString \"\n"
7725             "@\"literal\";",
7726             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
7727 
7728   // This input makes clang-format try to split the incomplete unicode escape
7729   // sequence, which used to lead to a crasher.
7730   verifyNoCrash(
7731       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
7732       getLLVMStyleWithColumns(60));
7733 }
7734 
7735 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
7736   FormatStyle Style = getGoogleStyleWithColumns(15);
7737   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
7738   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
7739   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
7740   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
7741   EXPECT_EQ("u8R\"x(raw literal)x\";",
7742             format("u8R\"x(raw literal)x\";", Style));
7743 }
7744 
7745 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
7746   FormatStyle Style = getLLVMStyleWithColumns(20);
7747   EXPECT_EQ(
7748       "_T(\"aaaaaaaaaaaaaa\")\n"
7749       "_T(\"aaaaaaaaaaaaaa\")\n"
7750       "_T(\"aaaaaaaaaaaa\")",
7751       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
7752   EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
7753             "     _T(\"aaaaaa\"),\n"
7754             "  z);",
7755             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
7756 
7757   // FIXME: Handle embedded spaces in one iteration.
7758   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
7759   //            "_T(\"aaaaaaaaaaaaa\")\n"
7760   //            "_T(\"aaaaaaaaaaaaa\")\n"
7761   //            "_T(\"a\")",
7762   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7763   //                   getLLVMStyleWithColumns(20)));
7764   EXPECT_EQ(
7765       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7766       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
7767   EXPECT_EQ("f(\n"
7768             "#if !TEST\n"
7769             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7770             "#endif\n"
7771             "    );",
7772             format("f(\n"
7773                    "#if !TEST\n"
7774                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7775                    "#endif\n"
7776                    ");"));
7777   EXPECT_EQ("f(\n"
7778             "\n"
7779             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
7780             format("f(\n"
7781                    "\n"
7782                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
7783 }
7784 
7785 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
7786   EXPECT_EQ(
7787       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7788       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7789       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7790       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7791              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7792              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
7793 }
7794 
7795 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
7796   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
7797             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
7798   EXPECT_EQ("fffffffffff(g(R\"x(\n"
7799             "multiline raw string literal xxxxxxxxxxxxxx\n"
7800             ")x\",\n"
7801             "              a),\n"
7802             "            b);",
7803             format("fffffffffff(g(R\"x(\n"
7804                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7805                    ")x\", a), b);",
7806                    getGoogleStyleWithColumns(20)));
7807   EXPECT_EQ("fffffffffff(\n"
7808             "    g(R\"x(qqq\n"
7809             "multiline raw string literal xxxxxxxxxxxxxx\n"
7810             ")x\",\n"
7811             "      a),\n"
7812             "    b);",
7813             format("fffffffffff(g(R\"x(qqq\n"
7814                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7815                    ")x\", a), b);",
7816                    getGoogleStyleWithColumns(20)));
7817 
7818   EXPECT_EQ("fffffffffff(R\"x(\n"
7819             "multiline raw string literal xxxxxxxxxxxxxx\n"
7820             ")x\");",
7821             format("fffffffffff(R\"x(\n"
7822                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7823                    ")x\");",
7824                    getGoogleStyleWithColumns(20)));
7825   EXPECT_EQ("fffffffffff(R\"x(\n"
7826             "multiline raw string literal xxxxxxxxxxxxxx\n"
7827             ")x\" + bbbbbb);",
7828             format("fffffffffff(R\"x(\n"
7829                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7830                    ")x\" +   bbbbbb);",
7831                    getGoogleStyleWithColumns(20)));
7832   EXPECT_EQ("fffffffffff(\n"
7833             "    R\"x(\n"
7834             "multiline raw string literal xxxxxxxxxxxxxx\n"
7835             ")x\" +\n"
7836             "    bbbbbb);",
7837             format("fffffffffff(\n"
7838                    " R\"x(\n"
7839                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7840                    ")x\" + bbbbbb);",
7841                    getGoogleStyleWithColumns(20)));
7842 }
7843 
7844 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
7845   verifyFormat("string a = \"unterminated;");
7846   EXPECT_EQ("function(\"unterminated,\n"
7847             "         OtherParameter);",
7848             format("function(  \"unterminated,\n"
7849                    "    OtherParameter);"));
7850 }
7851 
7852 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
7853   FormatStyle Style = getLLVMStyle();
7854   Style.Standard = FormatStyle::LS_Cpp03;
7855   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
7856             format("#define x(_a) printf(\"foo\"_a);", Style));
7857 }
7858 
7859 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
7860 
7861 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
7862   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
7863             "             \"ddeeefff\");",
7864             format("someFunction(\"aaabbbcccdddeeefff\");",
7865                    getLLVMStyleWithColumns(25)));
7866   EXPECT_EQ("someFunction1234567890(\n"
7867             "    \"aaabbbcccdddeeefff\");",
7868             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7869                    getLLVMStyleWithColumns(26)));
7870   EXPECT_EQ("someFunction1234567890(\n"
7871             "    \"aaabbbcccdddeeeff\"\n"
7872             "    \"f\");",
7873             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7874                    getLLVMStyleWithColumns(25)));
7875   EXPECT_EQ("someFunction1234567890(\n"
7876             "    \"aaabbbcccdddeeeff\"\n"
7877             "    \"f\");",
7878             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7879                    getLLVMStyleWithColumns(24)));
7880   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7881             "             \"ddde \"\n"
7882             "             \"efff\");",
7883             format("someFunction(\"aaabbbcc ddde efff\");",
7884                    getLLVMStyleWithColumns(25)));
7885   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
7886             "             \"ddeeefff\");",
7887             format("someFunction(\"aaabbbccc ddeeefff\");",
7888                    getLLVMStyleWithColumns(25)));
7889   EXPECT_EQ("someFunction1234567890(\n"
7890             "    \"aaabb \"\n"
7891             "    \"cccdddeeefff\");",
7892             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
7893                    getLLVMStyleWithColumns(25)));
7894   EXPECT_EQ("#define A          \\\n"
7895             "  string s =       \\\n"
7896             "      \"123456789\"  \\\n"
7897             "      \"0\";         \\\n"
7898             "  int i;",
7899             format("#define A string s = \"1234567890\"; int i;",
7900                    getLLVMStyleWithColumns(20)));
7901   // FIXME: Put additional penalties on breaking at non-whitespace locations.
7902   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7903             "             \"dddeeeff\"\n"
7904             "             \"f\");",
7905             format("someFunction(\"aaabbbcc dddeeefff\");",
7906                    getLLVMStyleWithColumns(25)));
7907 }
7908 
7909 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
7910   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
7911   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
7912   EXPECT_EQ("\"test\"\n"
7913             "\"\\n\"",
7914             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
7915   EXPECT_EQ("\"tes\\\\\"\n"
7916             "\"n\"",
7917             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
7918   EXPECT_EQ("\"\\\\\\\\\"\n"
7919             "\"\\n\"",
7920             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
7921   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
7922   EXPECT_EQ("\"\\uff01\"\n"
7923             "\"test\"",
7924             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
7925   EXPECT_EQ("\"\\Uff01ff02\"",
7926             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
7927   EXPECT_EQ("\"\\x000000000001\"\n"
7928             "\"next\"",
7929             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
7930   EXPECT_EQ("\"\\x000000000001next\"",
7931             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
7932   EXPECT_EQ("\"\\x000000000001\"",
7933             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
7934   EXPECT_EQ("\"test\"\n"
7935             "\"\\000000\"\n"
7936             "\"000001\"",
7937             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
7938   EXPECT_EQ("\"test\\000\"\n"
7939             "\"00000000\"\n"
7940             "\"1\"",
7941             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
7942 }
7943 
7944 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
7945   verifyFormat("void f() {\n"
7946                "  return g() {}\n"
7947                "  void h() {}");
7948   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
7949                "g();\n"
7950                "}");
7951 }
7952 
7953 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
7954   verifyFormat(
7955       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
7956 }
7957 
7958 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
7959   verifyFormat("class X {\n"
7960                "  void f() {\n"
7961                "  }\n"
7962                "};",
7963                getLLVMStyleWithColumns(12));
7964 }
7965 
7966 TEST_F(FormatTest, ConfigurableIndentWidth) {
7967   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
7968   EightIndent.IndentWidth = 8;
7969   EightIndent.ContinuationIndentWidth = 8;
7970   verifyFormat("void f() {\n"
7971                "        someFunction();\n"
7972                "        if (true) {\n"
7973                "                f();\n"
7974                "        }\n"
7975                "}",
7976                EightIndent);
7977   verifyFormat("class X {\n"
7978                "        void f() {\n"
7979                "        }\n"
7980                "};",
7981                EightIndent);
7982   verifyFormat("int x[] = {\n"
7983                "        call(),\n"
7984                "        call()};",
7985                EightIndent);
7986 }
7987 
7988 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
7989   verifyFormat("double\n"
7990                "f();",
7991                getLLVMStyleWithColumns(8));
7992 }
7993 
7994 TEST_F(FormatTest, ConfigurableUseOfTab) {
7995   FormatStyle Tab = getLLVMStyleWithColumns(42);
7996   Tab.IndentWidth = 8;
7997   Tab.UseTab = FormatStyle::UT_Always;
7998   Tab.AlignEscapedNewlinesLeft = true;
7999 
8000   EXPECT_EQ("if (aaaaaaaa && // q\n"
8001             "    bb)\t\t// w\n"
8002             "\t;",
8003             format("if (aaaaaaaa &&// q\n"
8004                    "bb)// w\n"
8005                    ";",
8006                    Tab));
8007   EXPECT_EQ("if (aaa && bbb) // w\n"
8008             "\t;",
8009             format("if(aaa&&bbb)// w\n"
8010                    ";",
8011                    Tab));
8012 
8013   verifyFormat("class X {\n"
8014                "\tvoid f() {\n"
8015                "\t\tsomeFunction(parameter1,\n"
8016                "\t\t\t     parameter2);\n"
8017                "\t}\n"
8018                "};",
8019                Tab);
8020   verifyFormat("#define A                        \\\n"
8021                "\tvoid f() {               \\\n"
8022                "\t\tsomeFunction(    \\\n"
8023                "\t\t    parameter1,  \\\n"
8024                "\t\t    parameter2); \\\n"
8025                "\t}",
8026                Tab);
8027 
8028   Tab.TabWidth = 4;
8029   Tab.IndentWidth = 8;
8030   verifyFormat("class TabWidth4Indent8 {\n"
8031                "\t\tvoid f() {\n"
8032                "\t\t\t\tsomeFunction(parameter1,\n"
8033                "\t\t\t\t\t\t\t parameter2);\n"
8034                "\t\t}\n"
8035                "};",
8036                Tab);
8037 
8038   Tab.TabWidth = 4;
8039   Tab.IndentWidth = 4;
8040   verifyFormat("class TabWidth4Indent4 {\n"
8041                "\tvoid f() {\n"
8042                "\t\tsomeFunction(parameter1,\n"
8043                "\t\t\t\t\t parameter2);\n"
8044                "\t}\n"
8045                "};",
8046                Tab);
8047 
8048   Tab.TabWidth = 8;
8049   Tab.IndentWidth = 4;
8050   verifyFormat("class TabWidth8Indent4 {\n"
8051                "    void f() {\n"
8052                "\tsomeFunction(parameter1,\n"
8053                "\t\t     parameter2);\n"
8054                "    }\n"
8055                "};",
8056                Tab);
8057 
8058   Tab.TabWidth = 8;
8059   Tab.IndentWidth = 8;
8060   EXPECT_EQ("/*\n"
8061             "\t      a\t\tcomment\n"
8062             "\t      in multiple lines\n"
8063             "       */",
8064             format("   /*\t \t \n"
8065                    " \t \t a\t\tcomment\t \t\n"
8066                    " \t \t in multiple lines\t\n"
8067                    " \t  */",
8068                    Tab));
8069 
8070   Tab.UseTab = FormatStyle::UT_ForIndentation;
8071   verifyFormat("{\n"
8072                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8073                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8074                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8075                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8076                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8077                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8078                "};",
8079                Tab);
8080   verifyFormat("enum A {\n"
8081                "\ta1, // Force multiple lines\n"
8082                "\ta2,\n"
8083                "\ta3\n"
8084                "};",
8085                Tab);
8086   EXPECT_EQ("if (aaaaaaaa && // q\n"
8087             "    bb)         // w\n"
8088             "\t;",
8089             format("if (aaaaaaaa &&// q\n"
8090                    "bb)// w\n"
8091                    ";",
8092                    Tab));
8093   verifyFormat("class X {\n"
8094                "\tvoid f() {\n"
8095                "\t\tsomeFunction(parameter1,\n"
8096                "\t\t             parameter2);\n"
8097                "\t}\n"
8098                "};",
8099                Tab);
8100   verifyFormat("{\n"
8101                "\tQ({\n"
8102                "\t\tint a;\n"
8103                "\t\tsomeFunction(aaaaaaaa,\n"
8104                "\t\t             bbbbbbb);\n"
8105                "\t}, p);\n"
8106                "}",
8107                Tab);
8108   EXPECT_EQ("{\n"
8109             "\t/* aaaa\n"
8110             "\t   bbbb */\n"
8111             "}",
8112             format("{\n"
8113                    "/* aaaa\n"
8114                    "   bbbb */\n"
8115                    "}",
8116                    Tab));
8117   EXPECT_EQ("{\n"
8118             "\t/*\n"
8119             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8120             "\t  bbbbbbbbbbbbb\n"
8121             "\t*/\n"
8122             "}",
8123             format("{\n"
8124                    "/*\n"
8125                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8126                    "*/\n"
8127                    "}",
8128                    Tab));
8129   EXPECT_EQ("{\n"
8130             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8131             "\t// bbbbbbbbbbbbb\n"
8132             "}",
8133             format("{\n"
8134                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8135                    "}",
8136                    Tab));
8137   EXPECT_EQ("{\n"
8138             "\t/*\n"
8139             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8140             "\t  bbbbbbbbbbbbb\n"
8141             "\t*/\n"
8142             "}",
8143             format("{\n"
8144                    "\t/*\n"
8145                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8146                    "\t*/\n"
8147                    "}",
8148                    Tab));
8149   EXPECT_EQ("{\n"
8150             "\t/*\n"
8151             "\n"
8152             "\t*/\n"
8153             "}",
8154             format("{\n"
8155                    "\t/*\n"
8156                    "\n"
8157                    "\t*/\n"
8158                    "}",
8159                    Tab));
8160   EXPECT_EQ("{\n"
8161             "\t/*\n"
8162             " asdf\n"
8163             "\t*/\n"
8164             "}",
8165             format("{\n"
8166                    "\t/*\n"
8167                    " asdf\n"
8168                    "\t*/\n"
8169                    "}",
8170                    Tab));
8171 
8172   Tab.UseTab = FormatStyle::UT_Never;
8173   EXPECT_EQ("/*\n"
8174             "              a\t\tcomment\n"
8175             "              in multiple lines\n"
8176             "       */",
8177             format("   /*\t \t \n"
8178                    " \t \t a\t\tcomment\t \t\n"
8179                    " \t \t in multiple lines\t\n"
8180                    " \t  */",
8181                    Tab));
8182   EXPECT_EQ("/* some\n"
8183             "   comment */",
8184             format(" \t \t /* some\n"
8185                    " \t \t    comment */",
8186                    Tab));
8187   EXPECT_EQ("int a; /* some\n"
8188             "   comment */",
8189             format(" \t \t int a; /* some\n"
8190                    " \t \t    comment */",
8191                    Tab));
8192 
8193   EXPECT_EQ("int a; /* some\n"
8194             "comment */",
8195             format(" \t \t int\ta; /* some\n"
8196                    " \t \t    comment */",
8197                    Tab));
8198   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8199             "    comment */",
8200             format(" \t \t f(\"\t\t\"); /* some\n"
8201                    " \t \t    comment */",
8202                    Tab));
8203   EXPECT_EQ("{\n"
8204             "  /*\n"
8205             "   * Comment\n"
8206             "   */\n"
8207             "  int i;\n"
8208             "}",
8209             format("{\n"
8210                    "\t/*\n"
8211                    "\t * Comment\n"
8212                    "\t */\n"
8213                    "\t int i;\n"
8214                    "}"));
8215 }
8216 
8217 TEST_F(FormatTest, CalculatesOriginalColumn) {
8218   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8219             "q\"; /* some\n"
8220             "       comment */",
8221             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8222                    "q\"; /* some\n"
8223                    "       comment */",
8224                    getLLVMStyle()));
8225   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8226             "/* some\n"
8227             "   comment */",
8228             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8229                    " /* some\n"
8230                    "    comment */",
8231                    getLLVMStyle()));
8232   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8233             "qqq\n"
8234             "/* some\n"
8235             "   comment */",
8236             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8237                    "qqq\n"
8238                    " /* some\n"
8239                    "    comment */",
8240                    getLLVMStyle()));
8241   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8242             "wwww; /* some\n"
8243             "         comment */",
8244             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8245                    "wwww; /* some\n"
8246                    "         comment */",
8247                    getLLVMStyle()));
8248 }
8249 
8250 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
8251   FormatStyle NoSpace = getLLVMStyle();
8252   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
8253 
8254   verifyFormat("while(true)\n"
8255                "  continue;",
8256                NoSpace);
8257   verifyFormat("for(;;)\n"
8258                "  continue;",
8259                NoSpace);
8260   verifyFormat("if(true)\n"
8261                "  f();\n"
8262                "else if(true)\n"
8263                "  f();",
8264                NoSpace);
8265   verifyFormat("do {\n"
8266                "  do_something();\n"
8267                "} while(something());",
8268                NoSpace);
8269   verifyFormat("switch(x) {\n"
8270                "default:\n"
8271                "  break;\n"
8272                "}",
8273                NoSpace);
8274   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
8275   verifyFormat("size_t x = sizeof(x);", NoSpace);
8276   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
8277   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
8278   verifyFormat("alignas(128) char a[128];", NoSpace);
8279   verifyFormat("size_t x = alignof(MyType);", NoSpace);
8280   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
8281   verifyFormat("int f() throw(Deprecated);", NoSpace);
8282   verifyFormat("typedef void (*cb)(int);", NoSpace);
8283   verifyFormat("T A::operator()();", NoSpace);
8284   verifyFormat("X A::operator++(T);", NoSpace);
8285 
8286   FormatStyle Space = getLLVMStyle();
8287   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
8288 
8289   verifyFormat("int f ();", Space);
8290   verifyFormat("void f (int a, T b) {\n"
8291                "  while (true)\n"
8292                "    continue;\n"
8293                "}",
8294                Space);
8295   verifyFormat("if (true)\n"
8296                "  f ();\n"
8297                "else if (true)\n"
8298                "  f ();",
8299                Space);
8300   verifyFormat("do {\n"
8301                "  do_something ();\n"
8302                "} while (something ());",
8303                Space);
8304   verifyFormat("switch (x) {\n"
8305                "default:\n"
8306                "  break;\n"
8307                "}",
8308                Space);
8309   verifyFormat("A::A () : a (1) {}", Space);
8310   verifyFormat("void f () __attribute__ ((asdf));", Space);
8311   verifyFormat("*(&a + 1);\n"
8312                "&((&a)[1]);\n"
8313                "a[(b + c) * d];\n"
8314                "(((a + 1) * 2) + 3) * 4;",
8315                Space);
8316   verifyFormat("#define A(x) x", Space);
8317   verifyFormat("#define A (x) x", Space);
8318   verifyFormat("#if defined(x)\n"
8319                "#endif",
8320                Space);
8321   verifyFormat("auto i = std::make_unique<int> (5);", Space);
8322   verifyFormat("size_t x = sizeof (x);", Space);
8323   verifyFormat("auto f (int x) -> decltype (x);", Space);
8324   verifyFormat("int f (T x) noexcept (x.create ());", Space);
8325   verifyFormat("alignas (128) char a[128];", Space);
8326   verifyFormat("size_t x = alignof (MyType);", Space);
8327   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
8328   verifyFormat("int f () throw (Deprecated);", Space);
8329   verifyFormat("typedef void (*cb) (int);", Space);
8330   verifyFormat("T A::operator() ();", Space);
8331   verifyFormat("X A::operator++ (T);", Space);
8332 }
8333 
8334 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
8335   FormatStyle Spaces = getLLVMStyle();
8336 
8337   Spaces.SpacesInParentheses = true;
8338   verifyFormat("call( x, y, z );", Spaces);
8339   verifyFormat("call();", Spaces);
8340   verifyFormat("std::function<void( int, int )> callback;", Spaces);
8341   verifyFormat("while ( (bool)1 )\n"
8342                "  continue;",
8343                Spaces);
8344   verifyFormat("for ( ;; )\n"
8345                "  continue;",
8346                Spaces);
8347   verifyFormat("if ( true )\n"
8348                "  f();\n"
8349                "else if ( true )\n"
8350                "  f();",
8351                Spaces);
8352   verifyFormat("do {\n"
8353                "  do_something( (int)i );\n"
8354                "} while ( something() );",
8355                Spaces);
8356   verifyFormat("switch ( x ) {\n"
8357                "default:\n"
8358                "  break;\n"
8359                "}",
8360                Spaces);
8361 
8362   Spaces.SpacesInParentheses = false;
8363   Spaces.SpacesInCStyleCastParentheses = true;
8364   verifyFormat("Type *A = ( Type * )P;", Spaces);
8365   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
8366   verifyFormat("x = ( int32 )y;", Spaces);
8367   verifyFormat("int a = ( int )(2.0f);", Spaces);
8368   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
8369   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
8370   verifyFormat("#define x (( int )-1)", Spaces);
8371 
8372   // Run the first set of tests again with:
8373   Spaces.SpacesInParentheses = false, Spaces.SpaceInEmptyParentheses = true;
8374   Spaces.SpacesInCStyleCastParentheses = true;
8375   verifyFormat("call(x, y, z);", Spaces);
8376   verifyFormat("call( );", Spaces);
8377   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8378   verifyFormat("while (( bool )1)\n"
8379                "  continue;",
8380                Spaces);
8381   verifyFormat("for (;;)\n"
8382                "  continue;",
8383                Spaces);
8384   verifyFormat("if (true)\n"
8385                "  f( );\n"
8386                "else if (true)\n"
8387                "  f( );",
8388                Spaces);
8389   verifyFormat("do {\n"
8390                "  do_something(( int )i);\n"
8391                "} while (something( ));",
8392                Spaces);
8393   verifyFormat("switch (x) {\n"
8394                "default:\n"
8395                "  break;\n"
8396                "}",
8397                Spaces);
8398 
8399   // Run the first set of tests again with:
8400   Spaces.SpaceAfterCStyleCast = true;
8401   verifyFormat("call(x, y, z);", Spaces);
8402   verifyFormat("call( );", Spaces);
8403   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8404   verifyFormat("while (( bool ) 1)\n"
8405                "  continue;",
8406                Spaces);
8407   verifyFormat("for (;;)\n"
8408                "  continue;",
8409                Spaces);
8410   verifyFormat("if (true)\n"
8411                "  f( );\n"
8412                "else if (true)\n"
8413                "  f( );",
8414                Spaces);
8415   verifyFormat("do {\n"
8416                "  do_something(( int ) i);\n"
8417                "} while (something( ));",
8418                Spaces);
8419   verifyFormat("switch (x) {\n"
8420                "default:\n"
8421                "  break;\n"
8422                "}",
8423                Spaces);
8424 
8425   // Run subset of tests again with:
8426   Spaces.SpacesInCStyleCastParentheses = false;
8427   Spaces.SpaceAfterCStyleCast = true;
8428   verifyFormat("while ((bool) 1)\n"
8429                "  continue;",
8430                Spaces);
8431   verifyFormat("do {\n"
8432                "  do_something((int) i);\n"
8433                "} while (something( ));",
8434                Spaces);
8435 }
8436 
8437 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
8438   verifyFormat("int a[5];");
8439   verifyFormat("a[3] += 42;");
8440 
8441   FormatStyle Spaces = getLLVMStyle();
8442   Spaces.SpacesInSquareBrackets = true;
8443   // Lambdas unchanged.
8444   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
8445   verifyFormat("return [i, args...] {};", Spaces);
8446 
8447   // Not lambdas.
8448   verifyFormat("int a[ 5 ];", Spaces);
8449   verifyFormat("a[ 3 ] += 42;", Spaces);
8450   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
8451   verifyFormat("double &operator[](int i) { return 0; }\n"
8452                "int i;",
8453                Spaces);
8454   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
8455   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
8456   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
8457 }
8458 
8459 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
8460   verifyFormat("int a = 5;");
8461   verifyFormat("a += 42;");
8462   verifyFormat("a or_eq 8;");
8463 
8464   FormatStyle Spaces = getLLVMStyle();
8465   Spaces.SpaceBeforeAssignmentOperators = false;
8466   verifyFormat("int a= 5;", Spaces);
8467   verifyFormat("a+= 42;", Spaces);
8468   verifyFormat("a or_eq 8;", Spaces);
8469 }
8470 
8471 TEST_F(FormatTest, AlignConsecutiveAssignments) {
8472   FormatStyle Alignment = getLLVMStyle();
8473   Alignment.AlignConsecutiveAssignments = false;
8474   verifyFormat("int a = 5;\n"
8475                "int oneTwoThree = 123;",
8476                Alignment);
8477   verifyFormat("int a = 5;\n"
8478                "int oneTwoThree = 123;",
8479                Alignment);
8480 
8481   Alignment.AlignConsecutiveAssignments = true;
8482   verifyFormat("int a           = 5;\n"
8483                "int oneTwoThree = 123;",
8484                Alignment);
8485   verifyFormat("int a           = method();\n"
8486                "int oneTwoThree = 133;",
8487                Alignment);
8488   verifyFormat("a &= 5;\n"
8489                "bcd *= 5;\n"
8490                "ghtyf += 5;\n"
8491                "dvfvdb -= 5;\n"
8492                "a /= 5;\n"
8493                "vdsvsv %= 5;\n"
8494                "sfdbddfbdfbb ^= 5;\n"
8495                "dvsdsv |= 5;\n"
8496                "int dsvvdvsdvvv = 123;",
8497                Alignment);
8498   verifyFormat("int i = 1, j = 10;\n"
8499                "something = 2000;",
8500                Alignment);
8501   verifyFormat("something = 2000;\n"
8502                "int i = 1, j = 10;\n",
8503                Alignment);
8504   verifyFormat("something = 2000;\n"
8505                "another   = 911;\n"
8506                "int i = 1, j = 10;\n"
8507                "oneMore = 1;\n"
8508                "i       = 2;",
8509                Alignment);
8510   verifyFormat("int a   = 5;\n"
8511                "int one = 1;\n"
8512                "method();\n"
8513                "int oneTwoThree = 123;\n"
8514                "int oneTwo      = 12;",
8515                Alignment);
8516   verifyFormat("int oneTwoThree = 123; // comment\n"
8517                "int oneTwo      = 12;  // comment",
8518                Alignment);
8519   EXPECT_EQ("int a = 5;\n"
8520             "\n"
8521             "int oneTwoThree = 123;",
8522             format("int a       = 5;\n"
8523                    "\n"
8524                    "int oneTwoThree= 123;",
8525                    Alignment));
8526   EXPECT_EQ("int a   = 5;\n"
8527             "int one = 1;\n"
8528             "\n"
8529             "int oneTwoThree = 123;",
8530             format("int a = 5;\n"
8531                    "int one = 1;\n"
8532                    "\n"
8533                    "int oneTwoThree = 123;",
8534                    Alignment));
8535   EXPECT_EQ("int a   = 5;\n"
8536             "int one = 1;\n"
8537             "\n"
8538             "int oneTwoThree = 123;\n"
8539             "int oneTwo      = 12;",
8540             format("int a = 5;\n"
8541                    "int one = 1;\n"
8542                    "\n"
8543                    "int oneTwoThree = 123;\n"
8544                    "int oneTwo = 12;",
8545                    Alignment));
8546   Alignment.AlignEscapedNewlinesLeft = true;
8547   verifyFormat("#define A               \\\n"
8548                "  int aaaa       = 12;  \\\n"
8549                "  int b          = 23;  \\\n"
8550                "  int ccc        = 234; \\\n"
8551                "  int dddddddddd = 2345;",
8552                Alignment);
8553   Alignment.AlignEscapedNewlinesLeft = false;
8554   verifyFormat("#define A                                                      "
8555                "                \\\n"
8556                "  int aaaa       = 12;                                         "
8557                "                \\\n"
8558                "  int b          = 23;                                         "
8559                "                \\\n"
8560                "  int ccc        = 234;                                        "
8561                "                \\\n"
8562                "  int dddddddddd = 2345;",
8563                Alignment);
8564   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
8565                "k = 4, int l = 5,\n"
8566                "                  int m = 6) {\n"
8567                "  int j      = 10;\n"
8568                "  otherThing = 1;\n"
8569                "}",
8570                Alignment);
8571   verifyFormat("void SomeFunction(int parameter = 0) {\n"
8572                "  int i   = 1;\n"
8573                "  int j   = 2;\n"
8574                "  int big = 10000;\n"
8575                "}",
8576                Alignment);
8577   verifyFormat("class C {\n"
8578                "public:\n"
8579                "  int i = 1;\n"
8580                "  virtual void f() = 0;\n"
8581                "};",
8582                Alignment);
8583   verifyFormat("int i = 1;\n"
8584                "if (SomeType t = getSomething()) {\n"
8585                "}\n"
8586                "int j   = 2;\n"
8587                "int big = 10000;",
8588                Alignment);
8589   verifyFormat("int j = 7;\n"
8590                "for (int k = 0; k < N; ++k) {\n"
8591                "}\n"
8592                "int j   = 2;\n"
8593                "int big = 10000;\n"
8594                "}",
8595                Alignment);
8596   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8597   verifyFormat("int i = 1;\n"
8598                "LooooooooooongType loooooooooooooooooooooongVariable\n"
8599                "    = someLooooooooooooooooongFunction();\n"
8600                "int j = 2;",
8601                Alignment);
8602   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8603   verifyFormat("int i = 1;\n"
8604                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
8605                "    someLooooooooooooooooongFunction();\n"
8606                "int j = 2;",
8607                Alignment);
8608   // FIXME: Should align all three assignments
8609   verifyFormat(
8610       "int i      = 1;\n"
8611       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
8612       "                          loooooooooooooooooooooongParameterB);\n"
8613       "int j = 2;",
8614       Alignment);
8615 }
8616 
8617 TEST_F(FormatTest, LinuxBraceBreaking) {
8618   FormatStyle LinuxBraceStyle = getLLVMStyle();
8619   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
8620   verifyFormat("namespace a\n"
8621                "{\n"
8622                "class A\n"
8623                "{\n"
8624                "  void f()\n"
8625                "  {\n"
8626                "    if (true) {\n"
8627                "      a();\n"
8628                "      b();\n"
8629                "    }\n"
8630                "  }\n"
8631                "  void g() { return; }\n"
8632                "};\n"
8633                "struct B {\n"
8634                "  int x;\n"
8635                "};\n"
8636                "}\n",
8637                LinuxBraceStyle);
8638   verifyFormat("enum X {\n"
8639                "  Y = 0,\n"
8640                "}\n",
8641                LinuxBraceStyle);
8642   verifyFormat("struct S {\n"
8643                "  int Type;\n"
8644                "  union {\n"
8645                "    int x;\n"
8646                "    double y;\n"
8647                "  } Value;\n"
8648                "  class C\n"
8649                "  {\n"
8650                "    MyFavoriteType Value;\n"
8651                "  } Class;\n"
8652                "}\n",
8653                LinuxBraceStyle);
8654 }
8655 
8656 TEST_F(FormatTest, MozillaBraceBreaking) {
8657   FormatStyle MozillaBraceStyle = getLLVMStyle();
8658   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
8659   verifyFormat("namespace a {\n"
8660                "class A\n"
8661                "{\n"
8662                "  void f()\n"
8663                "  {\n"
8664                "    if (true) {\n"
8665                "      a();\n"
8666                "      b();\n"
8667                "    }\n"
8668                "  }\n"
8669                "  void g() { return; }\n"
8670                "};\n"
8671                "enum E\n"
8672                "{\n"
8673                "  A,\n"
8674                "  // foo\n"
8675                "  B,\n"
8676                "  C\n"
8677                "};\n"
8678                "struct B\n"
8679                "{\n"
8680                "  int x;\n"
8681                "};\n"
8682                "}\n",
8683                MozillaBraceStyle);
8684   verifyFormat("struct S\n"
8685                "{\n"
8686                "  int Type;\n"
8687                "  union\n"
8688                "  {\n"
8689                "    int x;\n"
8690                "    double y;\n"
8691                "  } Value;\n"
8692                "  class C\n"
8693                "  {\n"
8694                "    MyFavoriteType Value;\n"
8695                "  } Class;\n"
8696                "}\n",
8697                MozillaBraceStyle);
8698 }
8699 
8700 TEST_F(FormatTest, StroustrupBraceBreaking) {
8701   FormatStyle StroustrupBraceStyle = getLLVMStyle();
8702   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8703   verifyFormat("namespace a {\n"
8704                "class A {\n"
8705                "  void f()\n"
8706                "  {\n"
8707                "    if (true) {\n"
8708                "      a();\n"
8709                "      b();\n"
8710                "    }\n"
8711                "  }\n"
8712                "  void g() { return; }\n"
8713                "};\n"
8714                "struct B {\n"
8715                "  int x;\n"
8716                "};\n"
8717                "}\n",
8718                StroustrupBraceStyle);
8719 
8720   verifyFormat("void foo()\n"
8721                "{\n"
8722                "  if (a) {\n"
8723                "    a();\n"
8724                "  }\n"
8725                "  else {\n"
8726                "    b();\n"
8727                "  }\n"
8728                "}\n",
8729                StroustrupBraceStyle);
8730 
8731   verifyFormat("#ifdef _DEBUG\n"
8732                "int foo(int i = 0)\n"
8733                "#else\n"
8734                "int foo(int i = 5)\n"
8735                "#endif\n"
8736                "{\n"
8737                "  return i;\n"
8738                "}",
8739                StroustrupBraceStyle);
8740 
8741   verifyFormat("void foo() {}\n"
8742                "void bar()\n"
8743                "#ifdef _DEBUG\n"
8744                "{\n"
8745                "  foo();\n"
8746                "}\n"
8747                "#else\n"
8748                "{\n"
8749                "}\n"
8750                "#endif",
8751                StroustrupBraceStyle);
8752 
8753   verifyFormat("void foobar() { int i = 5; }\n"
8754                "#ifdef _DEBUG\n"
8755                "void bar() {}\n"
8756                "#else\n"
8757                "void bar() { foobar(); }\n"
8758                "#endif",
8759                StroustrupBraceStyle);
8760 }
8761 
8762 TEST_F(FormatTest, AllmanBraceBreaking) {
8763   FormatStyle AllmanBraceStyle = getLLVMStyle();
8764   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
8765   verifyFormat("namespace a\n"
8766                "{\n"
8767                "class A\n"
8768                "{\n"
8769                "  void f()\n"
8770                "  {\n"
8771                "    if (true)\n"
8772                "    {\n"
8773                "      a();\n"
8774                "      b();\n"
8775                "    }\n"
8776                "  }\n"
8777                "  void g() { return; }\n"
8778                "};\n"
8779                "struct B\n"
8780                "{\n"
8781                "  int x;\n"
8782                "};\n"
8783                "}",
8784                AllmanBraceStyle);
8785 
8786   verifyFormat("void f()\n"
8787                "{\n"
8788                "  if (true)\n"
8789                "  {\n"
8790                "    a();\n"
8791                "  }\n"
8792                "  else if (false)\n"
8793                "  {\n"
8794                "    b();\n"
8795                "  }\n"
8796                "  else\n"
8797                "  {\n"
8798                "    c();\n"
8799                "  }\n"
8800                "}\n",
8801                AllmanBraceStyle);
8802 
8803   verifyFormat("void f()\n"
8804                "{\n"
8805                "  for (int i = 0; i < 10; ++i)\n"
8806                "  {\n"
8807                "    a();\n"
8808                "  }\n"
8809                "  while (false)\n"
8810                "  {\n"
8811                "    b();\n"
8812                "  }\n"
8813                "  do\n"
8814                "  {\n"
8815                "    c();\n"
8816                "  } while (false)\n"
8817                "}\n",
8818                AllmanBraceStyle);
8819 
8820   verifyFormat("void f(int a)\n"
8821                "{\n"
8822                "  switch (a)\n"
8823                "  {\n"
8824                "  case 0:\n"
8825                "    break;\n"
8826                "  case 1:\n"
8827                "  {\n"
8828                "    break;\n"
8829                "  }\n"
8830                "  case 2:\n"
8831                "  {\n"
8832                "  }\n"
8833                "  break;\n"
8834                "  default:\n"
8835                "    break;\n"
8836                "  }\n"
8837                "}\n",
8838                AllmanBraceStyle);
8839 
8840   verifyFormat("enum X\n"
8841                "{\n"
8842                "  Y = 0,\n"
8843                "}\n",
8844                AllmanBraceStyle);
8845   verifyFormat("enum X\n"
8846                "{\n"
8847                "  Y = 0\n"
8848                "}\n",
8849                AllmanBraceStyle);
8850 
8851   verifyFormat("@interface BSApplicationController ()\n"
8852                "{\n"
8853                "@private\n"
8854                "  id _extraIvar;\n"
8855                "}\n"
8856                "@end\n",
8857                AllmanBraceStyle);
8858 
8859   verifyFormat("#ifdef _DEBUG\n"
8860                "int foo(int i = 0)\n"
8861                "#else\n"
8862                "int foo(int i = 5)\n"
8863                "#endif\n"
8864                "{\n"
8865                "  return i;\n"
8866                "}",
8867                AllmanBraceStyle);
8868 
8869   verifyFormat("void foo() {}\n"
8870                "void bar()\n"
8871                "#ifdef _DEBUG\n"
8872                "{\n"
8873                "  foo();\n"
8874                "}\n"
8875                "#else\n"
8876                "{\n"
8877                "}\n"
8878                "#endif",
8879                AllmanBraceStyle);
8880 
8881   verifyFormat("void foobar() { int i = 5; }\n"
8882                "#ifdef _DEBUG\n"
8883                "void bar() {}\n"
8884                "#else\n"
8885                "void bar() { foobar(); }\n"
8886                "#endif",
8887                AllmanBraceStyle);
8888 
8889   // This shouldn't affect ObjC blocks..
8890   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
8891                "  // ...\n"
8892                "  int i;\n"
8893                "}];",
8894                AllmanBraceStyle);
8895   verifyFormat("void (^block)(void) = ^{\n"
8896                "  // ...\n"
8897                "  int i;\n"
8898                "};",
8899                AllmanBraceStyle);
8900   // .. or dict literals.
8901   verifyFormat("void f()\n"
8902                "{\n"
8903                "  [object someMethod:@{ @\"a\" : @\"b\" }];\n"
8904                "}",
8905                AllmanBraceStyle);
8906   verifyFormat("int f()\n"
8907                "{ // comment\n"
8908                "  return 42;\n"
8909                "}",
8910                AllmanBraceStyle);
8911 
8912   AllmanBraceStyle.ColumnLimit = 19;
8913   verifyFormat("void f() { int i; }", AllmanBraceStyle);
8914   AllmanBraceStyle.ColumnLimit = 18;
8915   verifyFormat("void f()\n"
8916                "{\n"
8917                "  int i;\n"
8918                "}",
8919                AllmanBraceStyle);
8920   AllmanBraceStyle.ColumnLimit = 80;
8921 
8922   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
8923   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
8924   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
8925   verifyFormat("void f(bool b)\n"
8926                "{\n"
8927                "  if (b)\n"
8928                "  {\n"
8929                "    return;\n"
8930                "  }\n"
8931                "}\n",
8932                BreakBeforeBraceShortIfs);
8933   verifyFormat("void f(bool b)\n"
8934                "{\n"
8935                "  if (b) return;\n"
8936                "}\n",
8937                BreakBeforeBraceShortIfs);
8938   verifyFormat("void f(bool b)\n"
8939                "{\n"
8940                "  while (b)\n"
8941                "  {\n"
8942                "    return;\n"
8943                "  }\n"
8944                "}\n",
8945                BreakBeforeBraceShortIfs);
8946 }
8947 
8948 TEST_F(FormatTest, GNUBraceBreaking) {
8949   FormatStyle GNUBraceStyle = getLLVMStyle();
8950   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
8951   verifyFormat("namespace a\n"
8952                "{\n"
8953                "class A\n"
8954                "{\n"
8955                "  void f()\n"
8956                "  {\n"
8957                "    int a;\n"
8958                "    {\n"
8959                "      int b;\n"
8960                "    }\n"
8961                "    if (true)\n"
8962                "      {\n"
8963                "        a();\n"
8964                "        b();\n"
8965                "      }\n"
8966                "  }\n"
8967                "  void g() { return; }\n"
8968                "}\n"
8969                "}",
8970                GNUBraceStyle);
8971 
8972   verifyFormat("void f()\n"
8973                "{\n"
8974                "  if (true)\n"
8975                "    {\n"
8976                "      a();\n"
8977                "    }\n"
8978                "  else if (false)\n"
8979                "    {\n"
8980                "      b();\n"
8981                "    }\n"
8982                "  else\n"
8983                "    {\n"
8984                "      c();\n"
8985                "    }\n"
8986                "}\n",
8987                GNUBraceStyle);
8988 
8989   verifyFormat("void f()\n"
8990                "{\n"
8991                "  for (int i = 0; i < 10; ++i)\n"
8992                "    {\n"
8993                "      a();\n"
8994                "    }\n"
8995                "  while (false)\n"
8996                "    {\n"
8997                "      b();\n"
8998                "    }\n"
8999                "  do\n"
9000                "    {\n"
9001                "      c();\n"
9002                "    }\n"
9003                "  while (false);\n"
9004                "}\n",
9005                GNUBraceStyle);
9006 
9007   verifyFormat("void f(int a)\n"
9008                "{\n"
9009                "  switch (a)\n"
9010                "    {\n"
9011                "    case 0:\n"
9012                "      break;\n"
9013                "    case 1:\n"
9014                "      {\n"
9015                "        break;\n"
9016                "      }\n"
9017                "    case 2:\n"
9018                "      {\n"
9019                "      }\n"
9020                "      break;\n"
9021                "    default:\n"
9022                "      break;\n"
9023                "    }\n"
9024                "}\n",
9025                GNUBraceStyle);
9026 
9027   verifyFormat("enum X\n"
9028                "{\n"
9029                "  Y = 0,\n"
9030                "}\n",
9031                GNUBraceStyle);
9032 
9033   verifyFormat("@interface BSApplicationController ()\n"
9034                "{\n"
9035                "@private\n"
9036                "  id _extraIvar;\n"
9037                "}\n"
9038                "@end\n",
9039                GNUBraceStyle);
9040 
9041   verifyFormat("#ifdef _DEBUG\n"
9042                "int foo(int i = 0)\n"
9043                "#else\n"
9044                "int foo(int i = 5)\n"
9045                "#endif\n"
9046                "{\n"
9047                "  return i;\n"
9048                "}",
9049                GNUBraceStyle);
9050 
9051   verifyFormat("void foo() {}\n"
9052                "void bar()\n"
9053                "#ifdef _DEBUG\n"
9054                "{\n"
9055                "  foo();\n"
9056                "}\n"
9057                "#else\n"
9058                "{\n"
9059                "}\n"
9060                "#endif",
9061                GNUBraceStyle);
9062 
9063   verifyFormat("void foobar() { int i = 5; }\n"
9064                "#ifdef _DEBUG\n"
9065                "void bar() {}\n"
9066                "#else\n"
9067                "void bar() { foobar(); }\n"
9068                "#endif",
9069                GNUBraceStyle);
9070 }
9071 
9072 TEST_F(FormatTest, WebKitBraceBreaking) {
9073   FormatStyle WebKitBraceStyle = getLLVMStyle();
9074   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
9075   verifyFormat("namespace a {\n"
9076                "class A {\n"
9077                "  void f()\n"
9078                "  {\n"
9079                "    if (true) {\n"
9080                "      a();\n"
9081                "      b();\n"
9082                "    }\n"
9083                "  }\n"
9084                "  void g() { return; }\n"
9085                "};\n"
9086                "enum E {\n"
9087                "  A,\n"
9088                "  // foo\n"
9089                "  B,\n"
9090                "  C\n"
9091                "};\n"
9092                "struct B {\n"
9093                "  int x;\n"
9094                "};\n"
9095                "}\n",
9096                WebKitBraceStyle);
9097   verifyFormat("struct S {\n"
9098                "  int Type;\n"
9099                "  union {\n"
9100                "    int x;\n"
9101                "    double y;\n"
9102                "  } Value;\n"
9103                "  class C {\n"
9104                "    MyFavoriteType Value;\n"
9105                "  } Class;\n"
9106                "};\n",
9107                WebKitBraceStyle);
9108 }
9109 
9110 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
9111   verifyFormat("void f() {\n"
9112                "  try {\n"
9113                "  } catch (const Exception &e) {\n"
9114                "  }\n"
9115                "}\n",
9116                getLLVMStyle());
9117 }
9118 
9119 TEST_F(FormatTest, UnderstandsPragmas) {
9120   verifyFormat("#pragma omp reduction(| : var)");
9121   verifyFormat("#pragma omp reduction(+ : var)");
9122 
9123   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
9124             "(including parentheses).",
9125             format("#pragma    mark   Any non-hyphenated or hyphenated string "
9126                    "(including parentheses)."));
9127 }
9128 
9129 TEST_F(FormatTest, UnderstandPragmaOption) {
9130   verifyFormat("#pragma option -C -A");
9131 
9132   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
9133 }
9134 
9135 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
9136   for (size_t i = 1; i < Styles.size(); ++i)                                   \
9137   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
9138                                   << " differs from Style #0"
9139 
9140 TEST_F(FormatTest, GetsPredefinedStyleByName) {
9141   SmallVector<FormatStyle, 3> Styles;
9142   Styles.resize(3);
9143 
9144   Styles[0] = getLLVMStyle();
9145   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
9146   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
9147   EXPECT_ALL_STYLES_EQUAL(Styles);
9148 
9149   Styles[0] = getGoogleStyle();
9150   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
9151   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
9152   EXPECT_ALL_STYLES_EQUAL(Styles);
9153 
9154   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9155   EXPECT_TRUE(
9156       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
9157   EXPECT_TRUE(
9158       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
9159   EXPECT_ALL_STYLES_EQUAL(Styles);
9160 
9161   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
9162   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
9163   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
9164   EXPECT_ALL_STYLES_EQUAL(Styles);
9165 
9166   Styles[0] = getMozillaStyle();
9167   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
9168   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
9169   EXPECT_ALL_STYLES_EQUAL(Styles);
9170 
9171   Styles[0] = getWebKitStyle();
9172   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
9173   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
9174   EXPECT_ALL_STYLES_EQUAL(Styles);
9175 
9176   Styles[0] = getGNUStyle();
9177   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
9178   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
9179   EXPECT_ALL_STYLES_EQUAL(Styles);
9180 
9181   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
9182 }
9183 
9184 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
9185   SmallVector<FormatStyle, 8> Styles;
9186   Styles.resize(2);
9187 
9188   Styles[0] = getGoogleStyle();
9189   Styles[1] = getLLVMStyle();
9190   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9191   EXPECT_ALL_STYLES_EQUAL(Styles);
9192 
9193   Styles.resize(5);
9194   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9195   Styles[1] = getLLVMStyle();
9196   Styles[1].Language = FormatStyle::LK_JavaScript;
9197   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9198 
9199   Styles[2] = getLLVMStyle();
9200   Styles[2].Language = FormatStyle::LK_JavaScript;
9201   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
9202                                   "BasedOnStyle: Google",
9203                                   &Styles[2])
9204                    .value());
9205 
9206   Styles[3] = getLLVMStyle();
9207   Styles[3].Language = FormatStyle::LK_JavaScript;
9208   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
9209                                   "Language: JavaScript",
9210                                   &Styles[3])
9211                    .value());
9212 
9213   Styles[4] = getLLVMStyle();
9214   Styles[4].Language = FormatStyle::LK_JavaScript;
9215   EXPECT_EQ(0, parseConfiguration("---\n"
9216                                   "BasedOnStyle: LLVM\n"
9217                                   "IndentWidth: 123\n"
9218                                   "---\n"
9219                                   "BasedOnStyle: Google\n"
9220                                   "Language: JavaScript",
9221                                   &Styles[4])
9222                    .value());
9223   EXPECT_ALL_STYLES_EQUAL(Styles);
9224 }
9225 
9226 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
9227   Style.FIELD = false;                                                         \
9228   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
9229   EXPECT_TRUE(Style.FIELD);                                                    \
9230   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
9231   EXPECT_FALSE(Style.FIELD);
9232 
9233 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
9234 
9235 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
9236   EXPECT_NE(VALUE, Style.FIELD);                                               \
9237   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
9238   EXPECT_EQ(VALUE, Style.FIELD)
9239 
9240 TEST_F(FormatTest, ParsesConfigurationBools) {
9241   FormatStyle Style = {};
9242   Style.Language = FormatStyle::LK_Cpp;
9243   CHECK_PARSE_BOOL(AlignAfterOpenBracket);
9244   CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
9245   CHECK_PARSE_BOOL(AlignOperands);
9246   CHECK_PARSE_BOOL(AlignTrailingComments);
9247   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
9248   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
9249   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
9250   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
9251   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
9252   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
9253   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
9254   CHECK_PARSE_BOOL(BinPackParameters);
9255   CHECK_PARSE_BOOL(BinPackArguments);
9256   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
9257   CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
9258   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
9259   CHECK_PARSE_BOOL(DerivePointerAlignment);
9260   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
9261   CHECK_PARSE_BOOL(IndentCaseLabels);
9262   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
9263   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
9264   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
9265   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
9266   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
9267   CHECK_PARSE_BOOL(SpacesInParentheses);
9268   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
9269   CHECK_PARSE_BOOL(SpacesInAngles);
9270   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
9271   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
9272   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
9273   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
9274   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
9275 }
9276 
9277 #undef CHECK_PARSE_BOOL
9278 
9279 TEST_F(FormatTest, ParsesConfiguration) {
9280   FormatStyle Style = {};
9281   Style.Language = FormatStyle::LK_Cpp;
9282   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
9283   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
9284               ConstructorInitializerIndentWidth, 1234u);
9285   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
9286   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
9287   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
9288   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
9289               PenaltyBreakBeforeFirstCallParameter, 1234u);
9290   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
9291   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
9292               PenaltyReturnTypeOnItsOwnLine, 1234u);
9293   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
9294               SpacesBeforeTrailingComments, 1234u);
9295   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
9296   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
9297 
9298   Style.PointerAlignment = FormatStyle::PAS_Middle;
9299   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
9300               FormatStyle::PAS_Left);
9301   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
9302               FormatStyle::PAS_Right);
9303   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
9304               FormatStyle::PAS_Middle);
9305   // For backward compatibility:
9306   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
9307               FormatStyle::PAS_Left);
9308   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
9309               FormatStyle::PAS_Right);
9310   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
9311               FormatStyle::PAS_Middle);
9312 
9313   Style.Standard = FormatStyle::LS_Auto;
9314   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
9315   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
9316   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
9317   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
9318   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
9319 
9320   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9321   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
9322               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
9323   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
9324               FormatStyle::BOS_None);
9325   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
9326               FormatStyle::BOS_All);
9327   // For backward compatibility:
9328   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
9329               FormatStyle::BOS_None);
9330   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
9331               FormatStyle::BOS_All);
9332 
9333   Style.UseTab = FormatStyle::UT_ForIndentation;
9334   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
9335   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
9336   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
9337   // For backward compatibility:
9338   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
9339   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
9340 
9341   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
9342   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
9343               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9344   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
9345               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
9346   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
9347               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
9348   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
9349               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9350   // For backward compatibility:
9351   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
9352               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9353   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
9354               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9355 
9356   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
9357   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
9358               FormatStyle::SBPO_Never);
9359   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
9360               FormatStyle::SBPO_Always);
9361   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
9362               FormatStyle::SBPO_ControlStatements);
9363   // For backward compatibility:
9364   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
9365               FormatStyle::SBPO_Never);
9366   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
9367               FormatStyle::SBPO_ControlStatements);
9368 
9369   Style.ColumnLimit = 123;
9370   FormatStyle BaseStyle = getLLVMStyle();
9371   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
9372   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
9373 
9374   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9375   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
9376               FormatStyle::BS_Attach);
9377   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
9378               FormatStyle::BS_Linux);
9379   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
9380               FormatStyle::BS_Mozilla);
9381   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
9382               FormatStyle::BS_Stroustrup);
9383   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
9384               FormatStyle::BS_Allman);
9385   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
9386   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, FormatStyle::BS_WebKit);
9387 
9388   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
9389   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
9390               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
9391   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
9392               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
9393   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
9394               AlwaysBreakAfterDefinitionReturnType,
9395               FormatStyle::DRTBS_TopLevel);
9396 
9397   Style.NamespaceIndentation = FormatStyle::NI_All;
9398   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
9399               FormatStyle::NI_None);
9400   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
9401               FormatStyle::NI_Inner);
9402   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
9403               FormatStyle::NI_All);
9404 
9405   Style.ForEachMacros.clear();
9406   std::vector<std::string> BoostForeach;
9407   BoostForeach.push_back("BOOST_FOREACH");
9408   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
9409   std::vector<std::string> BoostAndQForeach;
9410   BoostAndQForeach.push_back("BOOST_FOREACH");
9411   BoostAndQForeach.push_back("Q_FOREACH");
9412   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
9413               BoostAndQForeach);
9414 }
9415 
9416 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
9417   FormatStyle Style = {};
9418   Style.Language = FormatStyle::LK_Cpp;
9419   CHECK_PARSE("Language: Cpp\n"
9420               "IndentWidth: 12",
9421               IndentWidth, 12u);
9422   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
9423                                "IndentWidth: 34",
9424                                &Style),
9425             ParseError::Unsuitable);
9426   EXPECT_EQ(12u, Style.IndentWidth);
9427   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
9428   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9429 
9430   Style.Language = FormatStyle::LK_JavaScript;
9431   CHECK_PARSE("Language: JavaScript\n"
9432               "IndentWidth: 12",
9433               IndentWidth, 12u);
9434   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
9435   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
9436                                "IndentWidth: 34",
9437                                &Style),
9438             ParseError::Unsuitable);
9439   EXPECT_EQ(23u, Style.IndentWidth);
9440   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
9441   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9442 
9443   CHECK_PARSE("BasedOnStyle: LLVM\n"
9444               "IndentWidth: 67",
9445               IndentWidth, 67u);
9446 
9447   CHECK_PARSE("---\n"
9448               "Language: JavaScript\n"
9449               "IndentWidth: 12\n"
9450               "---\n"
9451               "Language: Cpp\n"
9452               "IndentWidth: 34\n"
9453               "...\n",
9454               IndentWidth, 12u);
9455 
9456   Style.Language = FormatStyle::LK_Cpp;
9457   CHECK_PARSE("---\n"
9458               "Language: JavaScript\n"
9459               "IndentWidth: 12\n"
9460               "---\n"
9461               "Language: Cpp\n"
9462               "IndentWidth: 34\n"
9463               "...\n",
9464               IndentWidth, 34u);
9465   CHECK_PARSE("---\n"
9466               "IndentWidth: 78\n"
9467               "---\n"
9468               "Language: JavaScript\n"
9469               "IndentWidth: 56\n"
9470               "...\n",
9471               IndentWidth, 78u);
9472 
9473   Style.ColumnLimit = 123;
9474   Style.IndentWidth = 234;
9475   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
9476   Style.TabWidth = 345;
9477   EXPECT_FALSE(parseConfiguration("---\n"
9478                                   "IndentWidth: 456\n"
9479                                   "BreakBeforeBraces: Allman\n"
9480                                   "---\n"
9481                                   "Language: JavaScript\n"
9482                                   "IndentWidth: 111\n"
9483                                   "TabWidth: 111\n"
9484                                   "---\n"
9485                                   "Language: Cpp\n"
9486                                   "BreakBeforeBraces: Stroustrup\n"
9487                                   "TabWidth: 789\n"
9488                                   "...\n",
9489                                   &Style));
9490   EXPECT_EQ(123u, Style.ColumnLimit);
9491   EXPECT_EQ(456u, Style.IndentWidth);
9492   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
9493   EXPECT_EQ(789u, Style.TabWidth);
9494 
9495   EXPECT_EQ(parseConfiguration("---\n"
9496                                "Language: JavaScript\n"
9497                                "IndentWidth: 56\n"
9498                                "---\n"
9499                                "IndentWidth: 78\n"
9500                                "...\n",
9501                                &Style),
9502             ParseError::Error);
9503   EXPECT_EQ(parseConfiguration("---\n"
9504                                "Language: JavaScript\n"
9505                                "IndentWidth: 56\n"
9506                                "---\n"
9507                                "Language: JavaScript\n"
9508                                "IndentWidth: 78\n"
9509                                "...\n",
9510                                &Style),
9511             ParseError::Error);
9512 
9513   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9514 }
9515 
9516 #undef CHECK_PARSE
9517 
9518 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
9519   FormatStyle Style = {};
9520   Style.Language = FormatStyle::LK_JavaScript;
9521   Style.BreakBeforeTernaryOperators = true;
9522   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
9523   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9524 
9525   Style.BreakBeforeTernaryOperators = true;
9526   EXPECT_EQ(0, parseConfiguration("---\n"
9527                                   "BasedOnStyle: Google\n"
9528                                   "---\n"
9529                                   "Language: JavaScript\n"
9530                                   "IndentWidth: 76\n"
9531                                   "...\n",
9532                                   &Style)
9533                    .value());
9534   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9535   EXPECT_EQ(76u, Style.IndentWidth);
9536   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9537 }
9538 
9539 TEST_F(FormatTest, ConfigurationRoundTripTest) {
9540   FormatStyle Style = getLLVMStyle();
9541   std::string YAML = configurationAsText(Style);
9542   FormatStyle ParsedStyle = {};
9543   ParsedStyle.Language = FormatStyle::LK_Cpp;
9544   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
9545   EXPECT_EQ(Style, ParsedStyle);
9546 }
9547 
9548 TEST_F(FormatTest, WorksFor8bitEncodings) {
9549   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
9550             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
9551             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
9552             "\"\xef\xee\xf0\xf3...\"",
9553             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
9554                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
9555                    "\xef\xee\xf0\xf3...\"",
9556                    getLLVMStyleWithColumns(12)));
9557 }
9558 
9559 TEST_F(FormatTest, HandlesUTF8BOM) {
9560   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
9561   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
9562             format("\xef\xbb\xbf#include <iostream>"));
9563   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
9564             format("\xef\xbb\xbf\n#include <iostream>"));
9565 }
9566 
9567 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
9568 #if !defined(_MSC_VER)
9569 
9570 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
9571   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
9572                getLLVMStyleWithColumns(35));
9573   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
9574                getLLVMStyleWithColumns(31));
9575   verifyFormat("// Однажды в студёную зимнюю пору...",
9576                getLLVMStyleWithColumns(36));
9577   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
9578   verifyFormat("/* Однажды в студёную зимнюю пору... */",
9579                getLLVMStyleWithColumns(39));
9580   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
9581                getLLVMStyleWithColumns(35));
9582 }
9583 
9584 TEST_F(FormatTest, SplitsUTF8Strings) {
9585   // Non-printable characters' width is currently considered to be the length in
9586   // bytes in UTF8. The characters can be displayed in very different manner
9587   // (zero-width, single width with a substitution glyph, expanded to their code
9588   // (e.g. "<8d>"), so there's no single correct way to handle them.
9589   EXPECT_EQ("\"aaaaÄ\"\n"
9590             "\"\xc2\x8d\";",
9591             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9592   EXPECT_EQ("\"aaaaaaaÄ\"\n"
9593             "\"\xc2\x8d\";",
9594             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9595   EXPECT_EQ("\"Однажды, в \"\n"
9596             "\"студёную \"\n"
9597             "\"зимнюю \"\n"
9598             "\"пору,\"",
9599             format("\"Однажды, в студёную зимнюю пору,\"",
9600                    getLLVMStyleWithColumns(13)));
9601   EXPECT_EQ(
9602       "\"一 二 三 \"\n"
9603       "\"四 五六 \"\n"
9604       "\"七 八 九 \"\n"
9605       "\"十\"",
9606       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
9607   EXPECT_EQ("\"一\t二 \"\n"
9608             "\"\t三 \"\n"
9609             "\"四 五\t六 \"\n"
9610             "\"\t七 \"\n"
9611             "\"八九十\tqq\"",
9612             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
9613                    getLLVMStyleWithColumns(11)));
9614 
9615   // UTF8 character in an escape sequence.
9616   EXPECT_EQ("\"aaaaaa\"\n"
9617             "\"\\\xC2\x8D\"",
9618             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
9619 }
9620 
9621 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
9622   EXPECT_EQ("const char *sssss =\n"
9623             "    \"一二三四五六七八\\\n"
9624             " 九 十\";",
9625             format("const char *sssss = \"一二三四五六七八\\\n"
9626                    " 九 十\";",
9627                    getLLVMStyleWithColumns(30)));
9628 }
9629 
9630 TEST_F(FormatTest, SplitsUTF8LineComments) {
9631   EXPECT_EQ("// aaaaÄ\xc2\x8d",
9632             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
9633   EXPECT_EQ("// Я из лесу\n"
9634             "// вышел; был\n"
9635             "// сильный\n"
9636             "// мороз.",
9637             format("// Я из лесу вышел; был сильный мороз.",
9638                    getLLVMStyleWithColumns(13)));
9639   EXPECT_EQ("// 一二三\n"
9640             "// 四五六七\n"
9641             "// 八  九\n"
9642             "// 十",
9643             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
9644 }
9645 
9646 TEST_F(FormatTest, SplitsUTF8BlockComments) {
9647   EXPECT_EQ("/* Гляжу,\n"
9648             " * поднимается\n"
9649             " * медленно в\n"
9650             " * гору\n"
9651             " * Лошадка,\n"
9652             " * везущая\n"
9653             " * хворосту\n"
9654             " * воз. */",
9655             format("/* Гляжу, поднимается медленно в гору\n"
9656                    " * Лошадка, везущая хворосту воз. */",
9657                    getLLVMStyleWithColumns(13)));
9658   EXPECT_EQ(
9659       "/* 一二三\n"
9660       " * 四五六七\n"
9661       " * 八  九\n"
9662       " * 十  */",
9663       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
9664   EXPECT_EQ("/* �������� ��������\n"
9665             " * ��������\n"
9666             " * ������-�� */",
9667             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
9668 }
9669 
9670 #endif // _MSC_VER
9671 
9672 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
9673   FormatStyle Style = getLLVMStyle();
9674 
9675   Style.ConstructorInitializerIndentWidth = 4;
9676   verifyFormat(
9677       "SomeClass::Constructor()\n"
9678       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9679       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9680       Style);
9681 
9682   Style.ConstructorInitializerIndentWidth = 2;
9683   verifyFormat(
9684       "SomeClass::Constructor()\n"
9685       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9686       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9687       Style);
9688 
9689   Style.ConstructorInitializerIndentWidth = 0;
9690   verifyFormat(
9691       "SomeClass::Constructor()\n"
9692       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9693       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9694       Style);
9695 }
9696 
9697 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
9698   FormatStyle Style = getLLVMStyle();
9699   Style.BreakConstructorInitializersBeforeComma = true;
9700   Style.ConstructorInitializerIndentWidth = 4;
9701   verifyFormat("SomeClass::Constructor()\n"
9702                "    : a(a)\n"
9703                "    , b(b)\n"
9704                "    , c(c) {}",
9705                Style);
9706   verifyFormat("SomeClass::Constructor()\n"
9707                "    : a(a) {}",
9708                Style);
9709 
9710   Style.ColumnLimit = 0;
9711   verifyFormat("SomeClass::Constructor()\n"
9712                "    : a(a) {}",
9713                Style);
9714   verifyFormat("SomeClass::Constructor()\n"
9715                "    : a(a)\n"
9716                "    , b(b)\n"
9717                "    , c(c) {}",
9718                Style);
9719   verifyFormat("SomeClass::Constructor()\n"
9720                "    : a(a) {\n"
9721                "  foo();\n"
9722                "  bar();\n"
9723                "}",
9724                Style);
9725 
9726   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9727   verifyFormat("SomeClass::Constructor()\n"
9728                "    : a(a)\n"
9729                "    , b(b)\n"
9730                "    , c(c) {\n}",
9731                Style);
9732   verifyFormat("SomeClass::Constructor()\n"
9733                "    : a(a) {\n}",
9734                Style);
9735 
9736   Style.ColumnLimit = 80;
9737   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
9738   Style.ConstructorInitializerIndentWidth = 2;
9739   verifyFormat("SomeClass::Constructor()\n"
9740                "  : a(a)\n"
9741                "  , b(b)\n"
9742                "  , c(c) {}",
9743                Style);
9744 
9745   Style.ConstructorInitializerIndentWidth = 0;
9746   verifyFormat("SomeClass::Constructor()\n"
9747                ": a(a)\n"
9748                ", b(b)\n"
9749                ", c(c) {}",
9750                Style);
9751 
9752   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
9753   Style.ConstructorInitializerIndentWidth = 4;
9754   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
9755   verifyFormat(
9756       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
9757       Style);
9758   verifyFormat(
9759       "SomeClass::Constructor()\n"
9760       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
9761       Style);
9762   Style.ConstructorInitializerIndentWidth = 4;
9763   Style.ColumnLimit = 60;
9764   verifyFormat("SomeClass::Constructor()\n"
9765                "    : aaaaaaaa(aaaaaaaa)\n"
9766                "    , aaaaaaaa(aaaaaaaa)\n"
9767                "    , aaaaaaaa(aaaaaaaa) {}",
9768                Style);
9769 }
9770 
9771 TEST_F(FormatTest, Destructors) {
9772   verifyFormat("void F(int &i) { i.~int(); }");
9773   verifyFormat("void F(int &i) { i->~int(); }");
9774 }
9775 
9776 TEST_F(FormatTest, FormatsWithWebKitStyle) {
9777   FormatStyle Style = getWebKitStyle();
9778 
9779   // Don't indent in outer namespaces.
9780   verifyFormat("namespace outer {\n"
9781                "int i;\n"
9782                "namespace inner {\n"
9783                "    int i;\n"
9784                "} // namespace inner\n"
9785                "} // namespace outer\n"
9786                "namespace other_outer {\n"
9787                "int i;\n"
9788                "}",
9789                Style);
9790 
9791   // Don't indent case labels.
9792   verifyFormat("switch (variable) {\n"
9793                "case 1:\n"
9794                "case 2:\n"
9795                "    doSomething();\n"
9796                "    break;\n"
9797                "default:\n"
9798                "    ++variable;\n"
9799                "}",
9800                Style);
9801 
9802   // Wrap before binary operators.
9803   EXPECT_EQ("void f()\n"
9804             "{\n"
9805             "    if (aaaaaaaaaaaaaaaa\n"
9806             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
9807             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
9808             "        return;\n"
9809             "}",
9810             format("void f() {\n"
9811                    "if (aaaaaaaaaaaaaaaa\n"
9812                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
9813                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
9814                    "return;\n"
9815                    "}",
9816                    Style));
9817 
9818   // Allow functions on a single line.
9819   verifyFormat("void f() { return; }", Style);
9820 
9821   // Constructor initializers are formatted one per line with the "," on the
9822   // new line.
9823   verifyFormat("Constructor()\n"
9824                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9825                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
9826                "          aaaaaaaaaaaaaa)\n"
9827                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
9828                "{\n"
9829                "}",
9830                Style);
9831   verifyFormat("SomeClass::Constructor()\n"
9832                "    : a(a)\n"
9833                "{\n"
9834                "}",
9835                Style);
9836   EXPECT_EQ("SomeClass::Constructor()\n"
9837             "    : a(a)\n"
9838             "{\n"
9839             "}",
9840             format("SomeClass::Constructor():a(a){}", Style));
9841   verifyFormat("SomeClass::Constructor()\n"
9842                "    : a(a)\n"
9843                "    , b(b)\n"
9844                "    , c(c)\n"
9845                "{\n"
9846                "}",
9847                Style);
9848   verifyFormat("SomeClass::Constructor()\n"
9849                "    : a(a)\n"
9850                "{\n"
9851                "    foo();\n"
9852                "    bar();\n"
9853                "}",
9854                Style);
9855 
9856   // Access specifiers should be aligned left.
9857   verifyFormat("class C {\n"
9858                "public:\n"
9859                "    int i;\n"
9860                "};",
9861                Style);
9862 
9863   // Do not align comments.
9864   verifyFormat("int a; // Do not\n"
9865                "double b; // align comments.",
9866                Style);
9867 
9868   // Do not align operands.
9869   EXPECT_EQ("ASSERT(aaaa\n"
9870             "    || bbbb);",
9871             format("ASSERT ( aaaa\n||bbbb);", Style));
9872 
9873   // Accept input's line breaks.
9874   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
9875             "    || bbbbbbbbbbbbbbb) {\n"
9876             "    i++;\n"
9877             "}",
9878             format("if (aaaaaaaaaaaaaaa\n"
9879                    "|| bbbbbbbbbbbbbbb) { i++; }",
9880                    Style));
9881   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
9882             "    i++;\n"
9883             "}",
9884             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
9885 
9886   // Don't automatically break all macro definitions (llvm.org/PR17842).
9887   verifyFormat("#define aNumber 10", Style);
9888   // However, generally keep the line breaks that the user authored.
9889   EXPECT_EQ("#define aNumber \\\n"
9890             "    10",
9891             format("#define aNumber \\\n"
9892                    " 10",
9893                    Style));
9894 
9895   // Keep empty and one-element array literals on a single line.
9896   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
9897             "                                  copyItems:YES];",
9898             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
9899                    "copyItems:YES];",
9900                    Style));
9901   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
9902             "                                  copyItems:YES];",
9903             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
9904                    "             copyItems:YES];",
9905                    Style));
9906   // FIXME: This does not seem right, there should be more indentation before
9907   // the array literal's entries. Nested blocks have the same problem.
9908   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
9909             "    @\"a\",\n"
9910             "    @\"a\"\n"
9911             "]\n"
9912             "                                  copyItems:YES];",
9913             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
9914                    "     @\"a\",\n"
9915                    "     @\"a\"\n"
9916                    "     ]\n"
9917                    "       copyItems:YES];",
9918                    Style));
9919   EXPECT_EQ(
9920       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
9921       "                                  copyItems:YES];",
9922       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
9923              "   copyItems:YES];",
9924              Style));
9925 
9926   verifyFormat("[self.a b:c c:d];", Style);
9927   EXPECT_EQ("[self.a b:c\n"
9928             "        c:d];",
9929             format("[self.a b:c\n"
9930                    "c:d];",
9931                    Style));
9932 }
9933 
9934 TEST_F(FormatTest, FormatsLambdas) {
9935   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
9936   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
9937   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
9938   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
9939   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
9940   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
9941   verifyFormat("void f() {\n"
9942                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
9943                "}\n");
9944   verifyFormat("void f() {\n"
9945                "  other(x.begin(), //\n"
9946                "        x.end(),   //\n"
9947                "        [&](int, int) { return 1; });\n"
9948                "}\n");
9949   verifyFormat("SomeFunction([]() { // A cool function...\n"
9950                "  return 43;\n"
9951                "});");
9952   EXPECT_EQ("SomeFunction([]() {\n"
9953             "#define A a\n"
9954             "  return 43;\n"
9955             "});",
9956             format("SomeFunction([](){\n"
9957                    "#define A a\n"
9958                    "return 43;\n"
9959                    "});"));
9960   verifyFormat("void f() {\n"
9961                "  SomeFunction([](decltype(x), A *a) {});\n"
9962                "}");
9963   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9964                "    [](const aaaaaaaaaa &a) { return a; });");
9965   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
9966                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
9967                "});");
9968   verifyFormat("Constructor()\n"
9969                "    : Field([] { // comment\n"
9970                "        int i;\n"
9971                "      }) {}");
9972   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
9973                "  return some_parameter.size();\n"
9974                "};");
9975   verifyFormat("int i = aaaaaa ? 1 //\n"
9976                "               : [] {\n"
9977                "                   return 2; //\n"
9978                "                 }();");
9979   verifyFormat("llvm::errs() << \"number of twos is \"\n"
9980                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
9981                "                  return x == 2; // force break\n"
9982                "                });");
9983   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n"
9984                "    int iiiiiiiiiiii) {\n"
9985                "  return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n"
9986                "});",
9987                getLLVMStyleWithColumns(60));
9988   verifyFormat("SomeFunction({[&] {\n"
9989                "                // comment\n"
9990                "              },\n"
9991                "              [&] {\n"
9992                "                // comment\n"
9993                "              }});");
9994   verifyFormat("SomeFunction({[&] {\n"
9995                "  // comment\n"
9996                "}});");
9997 
9998   // Lambdas with return types.
9999   verifyFormat("int c = []() -> int { return 2; }();\n");
10000   verifyFormat("int c = []() -> int * { return 2; }();\n");
10001   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
10002   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
10003   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
10004   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
10005   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
10006   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
10007   verifyFormat("[a, a]() -> a<1> {};");
10008   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
10009                "                   int j) -> int {\n"
10010                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
10011                "};");
10012   verifyFormat(
10013       "aaaaaaaaaaaaaaaaaaaaaa(\n"
10014       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
10015       "      return aaaaaaaaaaaaaaaaa;\n"
10016       "    });",
10017       getLLVMStyleWithColumns(70));
10018 
10019   // Multiple lambdas in the same parentheses change indentation rules.
10020   verifyFormat("SomeFunction(\n"
10021                "    []() {\n"
10022                "      int i = 42;\n"
10023                "      return i;\n"
10024                "    },\n"
10025                "    []() {\n"
10026                "      int j = 43;\n"
10027                "      return j;\n"
10028                "    });");
10029 
10030   // More complex introducers.
10031   verifyFormat("return [i, args...] {};");
10032 
10033   // Not lambdas.
10034   verifyFormat("constexpr char hello[]{\"hello\"};");
10035   verifyFormat("double &operator[](int i) { return 0; }\n"
10036                "int i;");
10037   verifyFormat("std::unique_ptr<int[]> foo() {}");
10038   verifyFormat("int i = a[a][a]->f();");
10039   verifyFormat("int i = (*b)[a]->f();");
10040 
10041   // Other corner cases.
10042   verifyFormat("void f() {\n"
10043                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
10044                "      );\n"
10045                "}");
10046 
10047   // Lambdas created through weird macros.
10048   verifyFormat("void f() {\n"
10049                "  MACRO((const AA &a) { return 1; });\n"
10050                "}");
10051 
10052   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
10053                "      doo_dah();\n"
10054                "      doo_dah();\n"
10055                "    })) {\n"
10056                "}");
10057   verifyFormat("auto lambda = []() {\n"
10058                "  int a = 2\n"
10059                "#if A\n"
10060                "          + 2\n"
10061                "#endif\n"
10062                "      ;\n"
10063                "};");
10064 }
10065 
10066 TEST_F(FormatTest, FormatsBlocks) {
10067   FormatStyle ShortBlocks = getLLVMStyle();
10068   ShortBlocks.AllowShortBlocksOnASingleLine = true;
10069   verifyFormat("int (^Block)(int, int);", ShortBlocks);
10070   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
10071   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
10072   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
10073   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
10074   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
10075 
10076   verifyFormat("foo(^{ bar(); });", ShortBlocks);
10077   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
10078   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
10079 
10080   verifyFormat("[operation setCompletionBlock:^{\n"
10081                "  [self onOperationDone];\n"
10082                "}];");
10083   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
10084                "  [self onOperationDone];\n"
10085                "}]};");
10086   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
10087                "  f();\n"
10088                "}];");
10089   verifyFormat("int a = [operation block:^int(int *i) {\n"
10090                "  return 1;\n"
10091                "}];");
10092   verifyFormat("[myObject doSomethingWith:arg1\n"
10093                "                      aaa:^int(int *a) {\n"
10094                "                        return 1;\n"
10095                "                      }\n"
10096                "                      bbb:f(a * bbbbbbbb)];");
10097 
10098   verifyFormat("[operation setCompletionBlock:^{\n"
10099                "  [self.delegate newDataAvailable];\n"
10100                "}];",
10101                getLLVMStyleWithColumns(60));
10102   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
10103                "  NSString *path = [self sessionFilePath];\n"
10104                "  if (path) {\n"
10105                "    // ...\n"
10106                "  }\n"
10107                "});");
10108   verifyFormat("[[SessionService sharedService]\n"
10109                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10110                "      if (window) {\n"
10111                "        [self windowDidLoad:window];\n"
10112                "      } else {\n"
10113                "        [self errorLoadingWindow];\n"
10114                "      }\n"
10115                "    }];");
10116   verifyFormat("void (^largeBlock)(void) = ^{\n"
10117                "  // ...\n"
10118                "};\n",
10119                getLLVMStyleWithColumns(40));
10120   verifyFormat("[[SessionService sharedService]\n"
10121                "    loadWindowWithCompletionBlock: //\n"
10122                "        ^(SessionWindow *window) {\n"
10123                "          if (window) {\n"
10124                "            [self windowDidLoad:window];\n"
10125                "          } else {\n"
10126                "            [self errorLoadingWindow];\n"
10127                "          }\n"
10128                "        }];",
10129                getLLVMStyleWithColumns(60));
10130   verifyFormat("[myObject doSomethingWith:arg1\n"
10131                "    firstBlock:^(Foo *a) {\n"
10132                "      // ...\n"
10133                "      int i;\n"
10134                "    }\n"
10135                "    secondBlock:^(Bar *b) {\n"
10136                "      // ...\n"
10137                "      int i;\n"
10138                "    }\n"
10139                "    thirdBlock:^Foo(Bar *b) {\n"
10140                "      // ...\n"
10141                "      int i;\n"
10142                "    }];");
10143   verifyFormat("[myObject doSomethingWith:arg1\n"
10144                "               firstBlock:-1\n"
10145                "              secondBlock:^(Bar *b) {\n"
10146                "                // ...\n"
10147                "                int i;\n"
10148                "              }];");
10149 
10150   verifyFormat("f(^{\n"
10151                "  @autoreleasepool {\n"
10152                "    if (a) {\n"
10153                "      g();\n"
10154                "    }\n"
10155                "  }\n"
10156                "});");
10157   verifyFormat("Block b = ^int *(A *a, B *b) {}");
10158 
10159   FormatStyle FourIndent = getLLVMStyle();
10160   FourIndent.ObjCBlockIndentWidth = 4;
10161   verifyFormat("[operation setCompletionBlock:^{\n"
10162                "    [self onOperationDone];\n"
10163                "}];",
10164                FourIndent);
10165 }
10166 
10167 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
10168   FormatStyle ZeroColumn = getLLVMStyle();
10169   ZeroColumn.ColumnLimit = 0;
10170 
10171   verifyFormat("[[SessionService sharedService] "
10172                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10173                "  if (window) {\n"
10174                "    [self windowDidLoad:window];\n"
10175                "  } else {\n"
10176                "    [self errorLoadingWindow];\n"
10177                "  }\n"
10178                "}];",
10179                ZeroColumn);
10180   EXPECT_EQ("[[SessionService sharedService]\n"
10181             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10182             "      if (window) {\n"
10183             "        [self windowDidLoad:window];\n"
10184             "      } else {\n"
10185             "        [self errorLoadingWindow];\n"
10186             "      }\n"
10187             "    }];",
10188             format("[[SessionService sharedService]\n"
10189                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10190                    "                if (window) {\n"
10191                    "    [self windowDidLoad:window];\n"
10192                    "  } else {\n"
10193                    "    [self errorLoadingWindow];\n"
10194                    "  }\n"
10195                    "}];",
10196                    ZeroColumn));
10197   verifyFormat("[myObject doSomethingWith:arg1\n"
10198                "    firstBlock:^(Foo *a) {\n"
10199                "      // ...\n"
10200                "      int i;\n"
10201                "    }\n"
10202                "    secondBlock:^(Bar *b) {\n"
10203                "      // ...\n"
10204                "      int i;\n"
10205                "    }\n"
10206                "    thirdBlock:^Foo(Bar *b) {\n"
10207                "      // ...\n"
10208                "      int i;\n"
10209                "    }];",
10210                ZeroColumn);
10211   verifyFormat("f(^{\n"
10212                "  @autoreleasepool {\n"
10213                "    if (a) {\n"
10214                "      g();\n"
10215                "    }\n"
10216                "  }\n"
10217                "});",
10218                ZeroColumn);
10219   verifyFormat("void (^largeBlock)(void) = ^{\n"
10220                "  // ...\n"
10221                "};",
10222                ZeroColumn);
10223 
10224   ZeroColumn.AllowShortBlocksOnASingleLine = true;
10225   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
10226             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
10227   ZeroColumn.AllowShortBlocksOnASingleLine = false;
10228   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
10229             "  int i;\n"
10230             "};",
10231             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
10232 }
10233 
10234 TEST_F(FormatTest, SupportsCRLF) {
10235   EXPECT_EQ("int a;\r\n"
10236             "int b;\r\n"
10237             "int c;\r\n",
10238             format("int a;\r\n"
10239                    "  int b;\r\n"
10240                    "    int c;\r\n",
10241                    getLLVMStyle()));
10242   EXPECT_EQ("int a;\r\n"
10243             "int b;\r\n"
10244             "int c;\r\n",
10245             format("int a;\r\n"
10246                    "  int b;\n"
10247                    "    int c;\r\n",
10248                    getLLVMStyle()));
10249   EXPECT_EQ("int a;\n"
10250             "int b;\n"
10251             "int c;\n",
10252             format("int a;\r\n"
10253                    "  int b;\n"
10254                    "    int c;\n",
10255                    getLLVMStyle()));
10256   EXPECT_EQ("\"aaaaaaa \"\r\n"
10257             "\"bbbbbbb\";\r\n",
10258             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
10259   EXPECT_EQ("#define A \\\r\n"
10260             "  b;      \\\r\n"
10261             "  c;      \\\r\n"
10262             "  d;\r\n",
10263             format("#define A \\\r\n"
10264                    "  b; \\\r\n"
10265                    "  c; d; \r\n",
10266                    getGoogleStyle()));
10267 
10268   EXPECT_EQ("/*\r\n"
10269             "multi line block comments\r\n"
10270             "should not introduce\r\n"
10271             "an extra carriage return\r\n"
10272             "*/\r\n",
10273             format("/*\r\n"
10274                    "multi line block comments\r\n"
10275                    "should not introduce\r\n"
10276                    "an extra carriage return\r\n"
10277                    "*/\r\n"));
10278 }
10279 
10280 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
10281   verifyFormat("MY_CLASS(C) {\n"
10282                "  int i;\n"
10283                "  int j;\n"
10284                "};");
10285 }
10286 
10287 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
10288   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
10289   TwoIndent.ContinuationIndentWidth = 2;
10290 
10291   EXPECT_EQ("int i =\n"
10292             "  longFunction(\n"
10293             "    arg);",
10294             format("int i = longFunction(arg);", TwoIndent));
10295 
10296   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
10297   SixIndent.ContinuationIndentWidth = 6;
10298 
10299   EXPECT_EQ("int i =\n"
10300             "      longFunction(\n"
10301             "            arg);",
10302             format("int i = longFunction(arg);", SixIndent));
10303 }
10304 
10305 TEST_F(FormatTest, SpacesInAngles) {
10306   FormatStyle Spaces = getLLVMStyle();
10307   Spaces.SpacesInAngles = true;
10308 
10309   verifyFormat("static_cast< int >(arg);", Spaces);
10310   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
10311   verifyFormat("f< int, float >();", Spaces);
10312   verifyFormat("template <> g() {}", Spaces);
10313   verifyFormat("template < std::vector< int > > f() {}", Spaces);
10314 
10315   Spaces.Standard = FormatStyle::LS_Cpp03;
10316   Spaces.SpacesInAngles = true;
10317   verifyFormat("A< A< int > >();", Spaces);
10318 
10319   Spaces.SpacesInAngles = false;
10320   verifyFormat("A<A<int> >();", Spaces);
10321 
10322   Spaces.Standard = FormatStyle::LS_Cpp11;
10323   Spaces.SpacesInAngles = true;
10324   verifyFormat("A< A< int > >();", Spaces);
10325 
10326   Spaces.SpacesInAngles = false;
10327   verifyFormat("A<A<int>>();", Spaces);
10328 }
10329 
10330 TEST_F(FormatTest, TripleAngleBrackets) {
10331   verifyFormat("f<<<1, 1>>>();");
10332   verifyFormat("f<<<1, 1, 1, s>>>();");
10333   verifyFormat("f<<<a, b, c, d>>>();");
10334   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
10335   verifyFormat("f<param><<<1, 1>>>();");
10336   verifyFormat("f<1><<<1, 1>>>();");
10337   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
10338   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10339                "aaaaaaaaaaa<<<\n    1, 1>>>();");
10340 }
10341 
10342 TEST_F(FormatTest, MergeLessLessAtEnd) {
10343   verifyFormat("<<");
10344   EXPECT_EQ("< < <", format("\\\n<<<"));
10345   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10346                "aaallvm::outs() <<");
10347   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10348                "aaaallvm::outs()\n    <<");
10349 }
10350 
10351 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
10352   std::string code = "#if A\n"
10353                      "#if B\n"
10354                      "a.\n"
10355                      "#endif\n"
10356                      "    a = 1;\n"
10357                      "#else\n"
10358                      "#endif\n"
10359                      "#if C\n"
10360                      "#else\n"
10361                      "#endif\n";
10362   EXPECT_EQ(code, format(code));
10363 }
10364 
10365 TEST_F(FormatTest, HandleConflictMarkers) {
10366   // Git/SVN conflict markers.
10367   EXPECT_EQ("int a;\n"
10368             "void f() {\n"
10369             "  callme(some(parameter1,\n"
10370             "<<<<<<< text by the vcs\n"
10371             "              parameter2),\n"
10372             "||||||| text by the vcs\n"
10373             "              parameter2),\n"
10374             "         parameter3,\n"
10375             "======= text by the vcs\n"
10376             "              parameter2, parameter3),\n"
10377             ">>>>>>> text by the vcs\n"
10378             "         otherparameter);\n",
10379             format("int a;\n"
10380                    "void f() {\n"
10381                    "  callme(some(parameter1,\n"
10382                    "<<<<<<< text by the vcs\n"
10383                    "  parameter2),\n"
10384                    "||||||| text by the vcs\n"
10385                    "  parameter2),\n"
10386                    "  parameter3,\n"
10387                    "======= text by the vcs\n"
10388                    "  parameter2,\n"
10389                    "  parameter3),\n"
10390                    ">>>>>>> text by the vcs\n"
10391                    "  otherparameter);\n"));
10392 
10393   // Perforce markers.
10394   EXPECT_EQ("void f() {\n"
10395             "  function(\n"
10396             ">>>> text by the vcs\n"
10397             "      parameter,\n"
10398             "==== text by the vcs\n"
10399             "      parameter,\n"
10400             "==== text by the vcs\n"
10401             "      parameter,\n"
10402             "<<<< text by the vcs\n"
10403             "      parameter);\n",
10404             format("void f() {\n"
10405                    "  function(\n"
10406                    ">>>> text by the vcs\n"
10407                    "  parameter,\n"
10408                    "==== text by the vcs\n"
10409                    "  parameter,\n"
10410                    "==== text by the vcs\n"
10411                    "  parameter,\n"
10412                    "<<<< text by the vcs\n"
10413                    "  parameter);\n"));
10414 
10415   EXPECT_EQ("<<<<<<<\n"
10416             "|||||||\n"
10417             "=======\n"
10418             ">>>>>>>",
10419             format("<<<<<<<\n"
10420                    "|||||||\n"
10421                    "=======\n"
10422                    ">>>>>>>"));
10423 
10424   EXPECT_EQ("<<<<<<<\n"
10425             "|||||||\n"
10426             "int i;\n"
10427             "=======\n"
10428             ">>>>>>>",
10429             format("<<<<<<<\n"
10430                    "|||||||\n"
10431                    "int i;\n"
10432                    "=======\n"
10433                    ">>>>>>>"));
10434 
10435   // FIXME: Handle parsing of macros around conflict markers correctly:
10436   EXPECT_EQ("#define Macro \\\n"
10437             "<<<<<<<\n"
10438             "Something \\\n"
10439             "|||||||\n"
10440             "Else \\\n"
10441             "=======\n"
10442             "Other \\\n"
10443             ">>>>>>>\n"
10444             "    End int i;\n",
10445             format("#define Macro \\\n"
10446                    "<<<<<<<\n"
10447                    "  Something \\\n"
10448                    "|||||||\n"
10449                    "  Else \\\n"
10450                    "=======\n"
10451                    "  Other \\\n"
10452                    ">>>>>>>\n"
10453                    "  End\n"
10454                    "int i;\n"));
10455 }
10456 
10457 TEST_F(FormatTest, DisableRegions) {
10458   EXPECT_EQ("int i;\n"
10459             "// clang-format off\n"
10460             "  int j;\n"
10461             "// clang-format on\n"
10462             "int k;",
10463             format(" int  i;\n"
10464                    "   // clang-format off\n"
10465                    "  int j;\n"
10466                    " // clang-format on\n"
10467                    "   int   k;"));
10468   EXPECT_EQ("int i;\n"
10469             "/* clang-format off */\n"
10470             "  int j;\n"
10471             "/* clang-format on */\n"
10472             "int k;",
10473             format(" int  i;\n"
10474                    "   /* clang-format off */\n"
10475                    "  int j;\n"
10476                    " /* clang-format on */\n"
10477                    "   int   k;"));
10478 }
10479 
10480 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
10481   format("? ) =");
10482   verifyNoCrash("#define a\\\n /**/}");
10483 }
10484 
10485 } // end namespace
10486 } // end namespace format
10487 } // end namespace clang
10488