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                "case 8:\n"
760                "  x = 8; // comment\n"
761                "  break;\n"
762                "default: y = 1; break;\n"
763                "}",
764                Style);
765   verifyFormat("switch (a) {\n"
766                "#if FOO\n"
767                "case 0: return 0;\n"
768                "#endif\n"
769                "}",
770                Style);
771   verifyFormat("switch (a) {\n"
772                "case 1: {\n"
773                "}\n"
774                "case 2: {\n"
775                "  return;\n"
776                "}\n"
777                "case 3: {\n"
778                "  x = 1;\n"
779                "  return;\n"
780                "}\n"
781                "case 4:\n"
782                "  if (x)\n"
783                "    return;\n"
784                "}",
785                Style);
786   Style.ColumnLimit = 21;
787   verifyFormat("switch (a) {\n"
788                "case 1: x = 1; break;\n"
789                "case 2: return;\n"
790                "case 3:\n"
791                "case 4:\n"
792                "case 5: return;\n"
793                "default:\n"
794                "  y = 1;\n"
795                "  break;\n"
796                "}",
797                Style);
798 }
799 
800 TEST_F(FormatTest, FormatsLabels) {
801   verifyFormat("void f() {\n"
802                "  some_code();\n"
803                "test_label:\n"
804                "  some_other_code();\n"
805                "  {\n"
806                "    some_more_code();\n"
807                "  another_label:\n"
808                "    some_more_code();\n"
809                "  }\n"
810                "}");
811   verifyFormat("{\n"
812                "  some_code();\n"
813                "test_label:\n"
814                "  some_other_code();\n"
815                "}");
816   verifyFormat("{\n"
817                "  some_code();\n"
818                "test_label:;\n"
819                "  int i = 0;\n"
820                "}");
821 }
822 
823 //===----------------------------------------------------------------------===//
824 // Tests for comments.
825 //===----------------------------------------------------------------------===//
826 
827 TEST_F(FormatTest, UnderstandsSingleLineComments) {
828   verifyFormat("//* */");
829   verifyFormat("// line 1\n"
830                "// line 2\n"
831                "void f() {}\n");
832 
833   verifyFormat("void f() {\n"
834                "  // Doesn't do anything\n"
835                "}");
836   verifyFormat("SomeObject\n"
837                "    // Calling someFunction on SomeObject\n"
838                "    .someFunction();");
839   verifyFormat("auto result = SomeObject\n"
840                "                  // Calling someFunction on SomeObject\n"
841                "                  .someFunction();");
842   verifyFormat("void f(int i,  // some comment (probably for i)\n"
843                "       int j,  // some comment (probably for j)\n"
844                "       int k); // some comment (probably for k)");
845   verifyFormat("void f(int i,\n"
846                "       // some comment (probably for j)\n"
847                "       int j,\n"
848                "       // some comment (probably for k)\n"
849                "       int k);");
850 
851   verifyFormat("int i    // This is a fancy variable\n"
852                "    = 5; // with nicely aligned comment.");
853 
854   verifyFormat("// Leading comment.\n"
855                "int a; // Trailing comment.");
856   verifyFormat("int a; // Trailing comment\n"
857                "       // on 2\n"
858                "       // or 3 lines.\n"
859                "int b;");
860   verifyFormat("int a; // Trailing comment\n"
861                "\n"
862                "// Leading comment.\n"
863                "int b;");
864   verifyFormat("int a;    // Comment.\n"
865                "          // More details.\n"
866                "int bbbb; // Another comment.");
867   verifyFormat(
868       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
869       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   // comment\n"
870       "int cccccccccccccccccccccccccccccc;       // comment\n"
871       "int ddd;                     // looooooooooooooooooooooooong comment\n"
872       "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
873       "int bbbbbbbbbbbbbbbbbbbbb;   // comment\n"
874       "int ccccccccccccccccccc;     // comment");
875 
876   verifyFormat("#include \"a\"     // comment\n"
877                "#include \"a/b/c\" // comment");
878   verifyFormat("#include <a>     // comment\n"
879                "#include <a/b/c> // comment");
880   EXPECT_EQ("#include \"a\"     // comment\n"
881             "#include \"a/b/c\" // comment",
882             format("#include \\\n"
883                    "  \"a\" // comment\n"
884                    "#include \"a/b/c\" // comment"));
885 
886   verifyFormat("enum E {\n"
887                "  // comment\n"
888                "  VAL_A, // comment\n"
889                "  VAL_B\n"
890                "};");
891 
892   verifyFormat(
893       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
894       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
895   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
896                "    // Comment inside a statement.\n"
897                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
898   verifyFormat("SomeFunction(a,\n"
899                "             // comment\n"
900                "             b + x);");
901   verifyFormat("SomeFunction(a, a,\n"
902                "             // comment\n"
903                "             b + x);");
904   verifyFormat(
905       "bool aaaaaaaaaaaaa = // comment\n"
906       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
907       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
908 
909   verifyFormat("int aaaa; // aaaaa\n"
910                "int aa;   // aaaaaaa",
911                getLLVMStyleWithColumns(20));
912 
913   EXPECT_EQ("void f() { // This does something ..\n"
914             "}\n"
915             "int a; // This is unrelated",
916             format("void f()    {     // This does something ..\n"
917                    "  }\n"
918                    "int   a;     // This is unrelated"));
919   EXPECT_EQ("class C {\n"
920             "  void f() { // This does something ..\n"
921             "  }          // awesome..\n"
922             "\n"
923             "  int a; // This is unrelated\n"
924             "};",
925             format("class C{void f()    { // This does something ..\n"
926                    "      } // awesome..\n"
927                    " \n"
928                    "int a;    // This is unrelated\n"
929                    "};"));
930 
931   EXPECT_EQ("int i; // single line trailing comment",
932             format("int i;\\\n// single line trailing comment"));
933 
934   verifyGoogleFormat("int a;  // Trailing comment.");
935 
936   verifyFormat("someFunction(anotherFunction( // Force break.\n"
937                "    parameter));");
938 
939   verifyGoogleFormat("#endif  // HEADER_GUARD");
940 
941   verifyFormat("const char *test[] = {\n"
942                "    // A\n"
943                "    \"aaaa\",\n"
944                "    // B\n"
945                "    \"aaaaa\"};");
946   verifyGoogleFormat(
947       "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
948       "    aaaaaaaaaaaaaaaaaaaaaa);  // 81_cols_with_this_comment");
949   EXPECT_EQ("D(a, {\n"
950             "  // test\n"
951             "  int a;\n"
952             "});",
953             format("D(a, {\n"
954                    "// test\n"
955                    "int a;\n"
956                    "});"));
957 
958   EXPECT_EQ("lineWith(); // comment\n"
959             "// at start\n"
960             "otherLine();",
961             format("lineWith();   // comment\n"
962                    "// at start\n"
963                    "otherLine();"));
964   EXPECT_EQ("lineWith(); // comment\n"
965             "            // at start\n"
966             "otherLine();",
967             format("lineWith();   // comment\n"
968                    " // at start\n"
969                    "otherLine();"));
970 
971   EXPECT_EQ("lineWith(); // comment\n"
972             "// at start\n"
973             "otherLine(); // comment",
974             format("lineWith();   // comment\n"
975                    "// at start\n"
976                    "otherLine();   // comment"));
977   EXPECT_EQ("lineWith();\n"
978             "// at start\n"
979             "otherLine(); // comment",
980             format("lineWith();\n"
981                    " // at start\n"
982                    "otherLine();   // comment"));
983   EXPECT_EQ("// first\n"
984             "// at start\n"
985             "otherLine(); // comment",
986             format("// first\n"
987                    " // at start\n"
988                    "otherLine();   // comment"));
989   EXPECT_EQ("f();\n"
990             "// first\n"
991             "// at start\n"
992             "otherLine(); // comment",
993             format("f();\n"
994                    "// first\n"
995                    " // at start\n"
996                    "otherLine();   // comment"));
997   verifyFormat("f(); // comment\n"
998                "// first\n"
999                "// at start\n"
1000                "otherLine();");
1001   EXPECT_EQ("f(); // comment\n"
1002             "// first\n"
1003             "// at start\n"
1004             "otherLine();",
1005             format("f();   // comment\n"
1006                    "// first\n"
1007                    " // at start\n"
1008                    "otherLine();"));
1009   EXPECT_EQ("f(); // comment\n"
1010             "     // first\n"
1011             "// at start\n"
1012             "otherLine();",
1013             format("f();   // comment\n"
1014                    " // first\n"
1015                    "// at start\n"
1016                    "otherLine();"));
1017   EXPECT_EQ("void f() {\n"
1018             "  lineWith(); // comment\n"
1019             "  // at start\n"
1020             "}",
1021             format("void              f() {\n"
1022                    "  lineWith(); // comment\n"
1023                    "  // at start\n"
1024                    "}"));
1025 
1026   verifyFormat("#define A                                                  \\\n"
1027                "  int i; /* iiiiiiiiiiiiiiiiiiiii */                       \\\n"
1028                "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
1029                getLLVMStyleWithColumns(60));
1030   verifyFormat(
1031       "#define A                                                   \\\n"
1032       "  int i;                        /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
1033       "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
1034       getLLVMStyleWithColumns(61));
1035 
1036   verifyFormat("if ( // This is some comment\n"
1037                "    x + 3) {\n"
1038                "}");
1039   EXPECT_EQ("if ( // This is some comment\n"
1040             "     // spanning two lines\n"
1041             "    x + 3) {\n"
1042             "}",
1043             format("if( // This is some comment\n"
1044                    "     // spanning two lines\n"
1045                    " x + 3) {\n"
1046                    "}"));
1047 
1048   verifyNoCrash("/\\\n/");
1049   verifyNoCrash("/\\\n* */");
1050   // The 0-character somehow makes the lexer return a proper comment.
1051   verifyNoCrash(StringRef("/*\\\0\n/", 6));
1052 }
1053 
1054 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
1055   EXPECT_EQ("SomeFunction(a,\n"
1056             "             b, // comment\n"
1057             "             c);",
1058             format("SomeFunction(a,\n"
1059                    "          b, // comment\n"
1060                    "      c);"));
1061   EXPECT_EQ("SomeFunction(a, b,\n"
1062             "             // comment\n"
1063             "             c);",
1064             format("SomeFunction(a,\n"
1065                    "          b,\n"
1066                    "  // comment\n"
1067                    "      c);"));
1068   EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
1069             "             c);",
1070             format("SomeFunction(a, b, // comment (unclear relation)\n"
1071                    "      c);"));
1072   EXPECT_EQ("SomeFunction(a, // comment\n"
1073             "             b,\n"
1074             "             c); // comment",
1075             format("SomeFunction(a,     // comment\n"
1076                    "          b,\n"
1077                    "      c); // comment"));
1078 }
1079 
1080 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
1081   EXPECT_EQ("// comment", format("// comment  "));
1082   EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
1083             format("int aaaaaaa, bbbbbbb; // comment                   ",
1084                    getLLVMStyleWithColumns(33)));
1085   EXPECT_EQ("// comment\\\n", format("// comment\\\n  \t \v   \f   "));
1086   EXPECT_EQ("// comment    \\\n", format("// comment    \\\n  \t \v   \f   "));
1087 }
1088 
1089 TEST_F(FormatTest, UnderstandsBlockComments) {
1090   verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
1091   verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
1092   EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
1093             "  bbbbbbbbbbbbbbbbbbbbbbbbb);",
1094             format("f(aaaaaaaaaaaaaaaaaaaaaaaaa ,   \\\n"
1095                    "/* Trailing comment for aa... */\n"
1096                    "  bbbbbbbbbbbbbbbbbbbbbbbbb);"));
1097   EXPECT_EQ(
1098       "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1099       "  /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
1100       format("f(aaaaaaaaaaaaaaaaaaaaaaaaa    ,   \n"
1101              "/* Leading comment for bb... */   bbbbbbbbbbbbbbbbbbbbbbbbb);"));
1102   EXPECT_EQ(
1103       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1104       "    aaaaaaaaaaaaaaaaaa,\n"
1105       "    aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
1106       "}",
1107       format("void      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1108              "                      aaaaaaaaaaaaaaaaaa  ,\n"
1109              "    aaaaaaaaaaaaaaaaaa) {   /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
1110              "}"));
1111 
1112   FormatStyle NoBinPacking = getLLVMStyle();
1113   NoBinPacking.BinPackParameters = false;
1114   verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
1115                "         /* parameter 2 */ aaaaaa,\n"
1116                "         /* parameter 3 */ aaaaaa,\n"
1117                "         /* parameter 4 */ aaaaaa);",
1118                NoBinPacking);
1119 
1120   // Aligning block comments in macros.
1121   verifyGoogleFormat("#define A        \\\n"
1122                      "  int i;   /*a*/ \\\n"
1123                      "  int jjj; /*b*/");
1124 }
1125 
1126 TEST_F(FormatTest, AlignsBlockComments) {
1127   EXPECT_EQ("/*\n"
1128             " * Really multi-line\n"
1129             " * comment.\n"
1130             " */\n"
1131             "void f() {}",
1132             format("  /*\n"
1133                    "   * Really multi-line\n"
1134                    "   * comment.\n"
1135                    "   */\n"
1136                    "  void f() {}"));
1137   EXPECT_EQ("class C {\n"
1138             "  /*\n"
1139             "   * Another multi-line\n"
1140             "   * comment.\n"
1141             "   */\n"
1142             "  void f() {}\n"
1143             "};",
1144             format("class C {\n"
1145                    "/*\n"
1146                    " * Another multi-line\n"
1147                    " * comment.\n"
1148                    " */\n"
1149                    "void f() {}\n"
1150                    "};"));
1151   EXPECT_EQ("/*\n"
1152             "  1. This is a comment with non-trivial formatting.\n"
1153             "     1.1. We have to indent/outdent all lines equally\n"
1154             "         1.1.1. to keep the formatting.\n"
1155             " */",
1156             format("  /*\n"
1157                    "    1. This is a comment with non-trivial formatting.\n"
1158                    "       1.1. We have to indent/outdent all lines equally\n"
1159                    "           1.1.1. to keep the formatting.\n"
1160                    "   */"));
1161   EXPECT_EQ("/*\n"
1162             "Don't try to outdent if there's not enough indentation.\n"
1163             "*/",
1164             format("  /*\n"
1165                    " Don't try to outdent if there's not enough indentation.\n"
1166                    " */"));
1167 
1168   EXPECT_EQ("int i; /* Comment with empty...\n"
1169             "        *\n"
1170             "        * line. */",
1171             format("int i; /* Comment with empty...\n"
1172                    "        *\n"
1173                    "        * line. */"));
1174   EXPECT_EQ("int foobar = 0; /* comment */\n"
1175             "int bar = 0;    /* multiline\n"
1176             "                   comment 1 */\n"
1177             "int baz = 0;    /* multiline\n"
1178             "                   comment 2 */\n"
1179             "int bzz = 0;    /* multiline\n"
1180             "                   comment 3 */",
1181             format("int foobar = 0; /* comment */\n"
1182                    "int bar = 0;    /* multiline\n"
1183                    "                   comment 1 */\n"
1184                    "int baz = 0; /* multiline\n"
1185                    "                comment 2 */\n"
1186                    "int bzz = 0;         /* multiline\n"
1187                    "                        comment 3 */"));
1188   EXPECT_EQ("int foobar = 0; /* comment */\n"
1189             "int bar = 0;    /* multiline\n"
1190             "   comment */\n"
1191             "int baz = 0;    /* multiline\n"
1192             "comment */",
1193             format("int foobar = 0; /* comment */\n"
1194                    "int bar = 0; /* multiline\n"
1195                    "comment */\n"
1196                    "int baz = 0;        /* multiline\n"
1197                    "comment */"));
1198 }
1199 
1200 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
1201   EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1202             "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
1203             format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1204                    "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
1205   EXPECT_EQ(
1206       "void ffffffffffff(\n"
1207       "    int aaaaaaaa, int bbbbbbbb,\n"
1208       "    int cccccccccccc) { /*\n"
1209       "                           aaaaaaaaaa\n"
1210       "                           aaaaaaaaaaaaa\n"
1211       "                           bbbbbbbbbbbbbb\n"
1212       "                           bbbbbbbbbb\n"
1213       "                         */\n"
1214       "}",
1215       format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
1216              "{ /*\n"
1217              "     aaaaaaaaaa aaaaaaaaaaaaa\n"
1218              "     bbbbbbbbbbbbbb bbbbbbbbbb\n"
1219              "   */\n"
1220              "}",
1221              getLLVMStyleWithColumns(40)));
1222 }
1223 
1224 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
1225   EXPECT_EQ("void ffffffffff(\n"
1226             "    int aaaaa /* test */);",
1227             format("void ffffffffff(int aaaaa /* test */);",
1228                    getLLVMStyleWithColumns(35)));
1229 }
1230 
1231 TEST_F(FormatTest, SplitsLongCxxComments) {
1232   EXPECT_EQ("// A comment that\n"
1233             "// doesn't fit on\n"
1234             "// one line",
1235             format("// A comment that doesn't fit on one line",
1236                    getLLVMStyleWithColumns(20)));
1237   EXPECT_EQ("/// A comment that\n"
1238             "/// doesn't fit on\n"
1239             "/// one line",
1240             format("/// A comment that doesn't fit on one line",
1241                    getLLVMStyleWithColumns(20)));
1242   EXPECT_EQ("//! A comment that\n"
1243             "//! doesn't fit on\n"
1244             "//! one line",
1245             format("//! A comment that doesn't fit on one line",
1246                    getLLVMStyleWithColumns(20)));
1247   EXPECT_EQ("// a b c d\n"
1248             "// e f  g\n"
1249             "// h i j k",
1250             format("// a b c d e f  g h i j k", getLLVMStyleWithColumns(10)));
1251   EXPECT_EQ(
1252       "// a b c d\n"
1253       "// e f  g\n"
1254       "// h i j k",
1255       format("\\\n// a b c d e f  g h i j k", getLLVMStyleWithColumns(10)));
1256   EXPECT_EQ("if (true) // A comment that\n"
1257             "          // doesn't fit on\n"
1258             "          // one line",
1259             format("if (true) // A comment that doesn't fit on one line   ",
1260                    getLLVMStyleWithColumns(30)));
1261   EXPECT_EQ("//    Don't_touch_leading_whitespace",
1262             format("//    Don't_touch_leading_whitespace",
1263                    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("//! Add leading\n"
1271             "//! whitespace",
1272             format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
1273   EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
1274   EXPECT_EQ("// Even if it makes the line exceed the column\n"
1275             "// limit",
1276             format("//Even if it makes the line exceed the column limit",
1277                    getLLVMStyleWithColumns(51)));
1278   EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
1279 
1280   EXPECT_EQ("// aa bb cc dd",
1281             format("// aa bb             cc dd                   ",
1282                    getLLVMStyleWithColumns(15)));
1283 
1284   EXPECT_EQ("// A comment before\n"
1285             "// a macro\n"
1286             "// definition\n"
1287             "#define a b",
1288             format("// A comment before a macro definition\n"
1289                    "#define a b",
1290                    getLLVMStyleWithColumns(20)));
1291   EXPECT_EQ("void ffffff(\n"
1292             "    int aaaaaaaaa,  // wwww\n"
1293             "    int bbbbbbbbbb, // xxxxxxx\n"
1294             "                    // yyyyyyyyyy\n"
1295             "    int c, int d, int e) {}",
1296             format("void ffffff(\n"
1297                    "    int aaaaaaaaa, // wwww\n"
1298                    "    int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
1299                    "    int c, int d, int e) {}",
1300                    getLLVMStyleWithColumns(40)));
1301   EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1302             format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1303                    getLLVMStyleWithColumns(20)));
1304   EXPECT_EQ(
1305       "#define XXX // a b c d\n"
1306       "            // e f g h",
1307       format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
1308   EXPECT_EQ(
1309       "#define XXX // q w e r\n"
1310       "            // t y u i",
1311       format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
1312 }
1313 
1314 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
1315   EXPECT_EQ("//     A comment\n"
1316             "//     that doesn't\n"
1317             "//     fit on one\n"
1318             "//     line",
1319             format("//     A comment that doesn't fit on one line",
1320                    getLLVMStyleWithColumns(20)));
1321   EXPECT_EQ("///     A comment\n"
1322             "///     that doesn't\n"
1323             "///     fit on one\n"
1324             "///     line",
1325             format("///     A comment that doesn't fit on one line",
1326                    getLLVMStyleWithColumns(20)));
1327 }
1328 
1329 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
1330   EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1331             "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1332             "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1333             format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1334                    "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1335                    "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
1336   EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1337             "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1338             "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1339             format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1340                    "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1341                    "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1342                    getLLVMStyleWithColumns(50)));
1343   // FIXME: One day we might want to implement adjustment of leading whitespace
1344   // of the consecutive lines in this kind of comment:
1345   EXPECT_EQ("double\n"
1346             "    a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1347             "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1348             "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1349             format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1350                    "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1351                    "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1352                    getLLVMStyleWithColumns(49)));
1353 }
1354 
1355 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
1356   FormatStyle Pragmas = getLLVMStyleWithColumns(30);
1357   Pragmas.CommentPragmas = "^ IWYU pragma:";
1358   EXPECT_EQ(
1359       "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
1360       format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
1361   EXPECT_EQ(
1362       "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
1363       format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
1364 }
1365 
1366 TEST_F(FormatTest, PriorityOfCommentBreaking) {
1367   EXPECT_EQ("if (xxx ==\n"
1368             "        yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
1369             "    zzz)\n"
1370             "  q();",
1371             format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
1372                    "    zzz) q();",
1373                    getLLVMStyleWithColumns(40)));
1374   EXPECT_EQ("if (xxxxxxxxxx ==\n"
1375             "        yyy && // aaaaaa bbbbbbbb cccc\n"
1376             "    zzz)\n"
1377             "  q();",
1378             format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
1379                    "    zzz) q();",
1380                    getLLVMStyleWithColumns(40)));
1381   EXPECT_EQ("if (xxxxxxxxxx &&\n"
1382             "        yyy || // aaaaaa bbbbbbbb cccc\n"
1383             "    zzz)\n"
1384             "  q();",
1385             format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
1386                    "    zzz) q();",
1387                    getLLVMStyleWithColumns(40)));
1388   EXPECT_EQ("fffffffff(\n"
1389             "    &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
1390             "    zzz);",
1391             format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
1392                    " zzz);",
1393                    getLLVMStyleWithColumns(40)));
1394 }
1395 
1396 TEST_F(FormatTest, MultiLineCommentsInDefines) {
1397   EXPECT_EQ("#define A(x) /* \\\n"
1398             "  a comment     \\\n"
1399             "  inside */     \\\n"
1400             "  f();",
1401             format("#define A(x) /* \\\n"
1402                    "  a comment     \\\n"
1403                    "  inside */     \\\n"
1404                    "  f();",
1405                    getLLVMStyleWithColumns(17)));
1406   EXPECT_EQ("#define A(      \\\n"
1407             "    x) /*       \\\n"
1408             "  a comment     \\\n"
1409             "  inside */     \\\n"
1410             "  f();",
1411             format("#define A(      \\\n"
1412                    "    x) /*       \\\n"
1413                    "  a comment     \\\n"
1414                    "  inside */     \\\n"
1415                    "  f();",
1416                    getLLVMStyleWithColumns(17)));
1417 }
1418 
1419 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
1420   EXPECT_EQ("namespace {}\n// Test\n#define A",
1421             format("namespace {}\n   // Test\n#define A"));
1422   EXPECT_EQ("namespace {}\n/* Test */\n#define A",
1423             format("namespace {}\n   /* Test */\n#define A"));
1424   EXPECT_EQ("namespace {}\n/* Test */ #define A",
1425             format("namespace {}\n   /* Test */    #define A"));
1426 }
1427 
1428 TEST_F(FormatTest, SplitsLongLinesInComments) {
1429   EXPECT_EQ("/* This is a long\n"
1430             " * comment that\n"
1431             " * doesn't\n"
1432             " * fit on one line.\n"
1433             " */",
1434             format("/* "
1435                    "This is a long                                         "
1436                    "comment that "
1437                    "doesn't                                    "
1438                    "fit on one line.  */",
1439                    getLLVMStyleWithColumns(20)));
1440   EXPECT_EQ(
1441       "/* a b c d\n"
1442       " * e f  g\n"
1443       " * h i j k\n"
1444       " */",
1445       format("/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
1446   EXPECT_EQ(
1447       "/* a b c d\n"
1448       " * e f  g\n"
1449       " * h i j k\n"
1450       " */",
1451       format("\\\n/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
1452   EXPECT_EQ("/*\n"
1453             "This is a long\n"
1454             "comment that doesn't\n"
1455             "fit on one line.\n"
1456             "*/",
1457             format("/*\n"
1458                    "This is a long                                         "
1459                    "comment that doesn't                                    "
1460                    "fit on one line.                                      \n"
1461                    "*/",
1462                    getLLVMStyleWithColumns(20)));
1463   EXPECT_EQ("/*\n"
1464             " * This is a long\n"
1465             " * comment that\n"
1466             " * doesn't fit on\n"
1467             " * one line.\n"
1468             " */",
1469             format("/*      \n"
1470                    " * This is a long "
1471                    "   comment that     "
1472                    "   doesn't fit on   "
1473                    "   one line.                                            \n"
1474                    " */",
1475                    getLLVMStyleWithColumns(20)));
1476   EXPECT_EQ("/*\n"
1477             " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
1478             " * so_it_should_be_broken\n"
1479             " * wherever_a_space_occurs\n"
1480             " */",
1481             format("/*\n"
1482                    " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
1483                    "   so_it_should_be_broken "
1484                    "   wherever_a_space_occurs                             \n"
1485                    " */",
1486                    getLLVMStyleWithColumns(20)));
1487   EXPECT_EQ("/*\n"
1488             " *    This_comment_can_not_be_broken_into_lines\n"
1489             " */",
1490             format("/*\n"
1491                    " *    This_comment_can_not_be_broken_into_lines\n"
1492                    " */",
1493                    getLLVMStyleWithColumns(20)));
1494   EXPECT_EQ("{\n"
1495             "  /*\n"
1496             "  This is another\n"
1497             "  long comment that\n"
1498             "  doesn't fit on one\n"
1499             "  line    1234567890\n"
1500             "  */\n"
1501             "}",
1502             format("{\n"
1503                    "/*\n"
1504                    "This is another     "
1505                    "  long comment that "
1506                    "  doesn't fit on one"
1507                    "  line    1234567890\n"
1508                    "*/\n"
1509                    "}",
1510                    getLLVMStyleWithColumns(20)));
1511   EXPECT_EQ("{\n"
1512             "  /*\n"
1513             "   * This        i s\n"
1514             "   * another comment\n"
1515             "   * t hat  doesn' t\n"
1516             "   * fit on one l i\n"
1517             "   * n e\n"
1518             "   */\n"
1519             "}",
1520             format("{\n"
1521                    "/*\n"
1522                    " * This        i s"
1523                    "   another comment"
1524                    "   t hat  doesn' t"
1525                    "   fit on one l i"
1526                    "   n e\n"
1527                    " */\n"
1528                    "}",
1529                    getLLVMStyleWithColumns(20)));
1530   EXPECT_EQ("/*\n"
1531             " * This is a long\n"
1532             " * comment that\n"
1533             " * doesn't fit on\n"
1534             " * one line\n"
1535             " */",
1536             format("   /*\n"
1537                    "    * This is a long comment that doesn't fit on one line\n"
1538                    "    */",
1539                    getLLVMStyleWithColumns(20)));
1540   EXPECT_EQ("{\n"
1541             "  if (something) /* This is a\n"
1542             "                    long\n"
1543             "                    comment */\n"
1544             "    ;\n"
1545             "}",
1546             format("{\n"
1547                    "  if (something) /* This is a long comment */\n"
1548                    "    ;\n"
1549                    "}",
1550                    getLLVMStyleWithColumns(30)));
1551 
1552   EXPECT_EQ("/* A comment before\n"
1553             " * a macro\n"
1554             " * definition */\n"
1555             "#define a b",
1556             format("/* A comment before a macro definition */\n"
1557                    "#define a b",
1558                    getLLVMStyleWithColumns(20)));
1559 
1560   EXPECT_EQ("/* some comment\n"
1561             "     *   a comment\n"
1562             "* that we break\n"
1563             " * another comment\n"
1564             "* we have to break\n"
1565             "* a left comment\n"
1566             " */",
1567             format("  /* some comment\n"
1568                    "       *   a comment that we break\n"
1569                    "   * another comment we have to break\n"
1570                    "* a left comment\n"
1571                    "   */",
1572                    getLLVMStyleWithColumns(20)));
1573 
1574   EXPECT_EQ("/**\n"
1575             " * multiline block\n"
1576             " * comment\n"
1577             " *\n"
1578             " */",
1579             format("/**\n"
1580                    " * multiline block comment\n"
1581                    " *\n"
1582                    " */",
1583                    getLLVMStyleWithColumns(20)));
1584 
1585   EXPECT_EQ("/*\n"
1586             "\n"
1587             "\n"
1588             "    */\n",
1589             format("  /*       \n"
1590                    "      \n"
1591                    "               \n"
1592                    "      */\n"));
1593 
1594   EXPECT_EQ("/* a a */",
1595             format("/* a a            */", getLLVMStyleWithColumns(15)));
1596   EXPECT_EQ("/* a a bc  */",
1597             format("/* a a            bc  */", getLLVMStyleWithColumns(15)));
1598   EXPECT_EQ("/* aaa aaa\n"
1599             " * aaaaa */",
1600             format("/* aaa aaa aaaaa       */", getLLVMStyleWithColumns(15)));
1601   EXPECT_EQ("/* aaa aaa\n"
1602             " * aaaaa     */",
1603             format("/* aaa aaa aaaaa     */", getLLVMStyleWithColumns(15)));
1604 }
1605 
1606 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
1607   EXPECT_EQ("#define X          \\\n"
1608             "  /*               \\\n"
1609             "   Test            \\\n"
1610             "   Macro comment   \\\n"
1611             "   with a long     \\\n"
1612             "   line            \\\n"
1613             "   */              \\\n"
1614             "  A + B",
1615             format("#define X \\\n"
1616                    "  /*\n"
1617                    "   Test\n"
1618                    "   Macro comment with a long  line\n"
1619                    "   */ \\\n"
1620                    "  A + B",
1621                    getLLVMStyleWithColumns(20)));
1622   EXPECT_EQ("#define X          \\\n"
1623             "  /* Macro comment \\\n"
1624             "     with a long   \\\n"
1625             "     line */       \\\n"
1626             "  A + B",
1627             format("#define X \\\n"
1628                    "  /* Macro comment with a long\n"
1629                    "     line */ \\\n"
1630                    "  A + B",
1631                    getLLVMStyleWithColumns(20)));
1632   EXPECT_EQ("#define X          \\\n"
1633             "  /* Macro comment \\\n"
1634             "   * with a long   \\\n"
1635             "   * line */       \\\n"
1636             "  A + B",
1637             format("#define X \\\n"
1638                    "  /* Macro comment with a long  line */ \\\n"
1639                    "  A + B",
1640                    getLLVMStyleWithColumns(20)));
1641 }
1642 
1643 TEST_F(FormatTest, CommentsInStaticInitializers) {
1644   EXPECT_EQ(
1645       "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
1646       "                        aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
1647       "                        /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
1648       "                        aaaaaaaaaaaaaaaaaaaa, // comment\n"
1649       "                        aaaaaaaaaaaaaaaaaaaa};",
1650       format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa  ,  /* comment */\n"
1651              "                   aaaaaaaaaaaaaaaaaaaa   /* comment */ ,\n"
1652              "                     /* comment */   aaaaaaaaaaaaaaaaaaaa ,\n"
1653              "              aaaaaaaaaaaaaaaaaaaa ,   // comment\n"
1654              "                  aaaaaaaaaaaaaaaaaaaa };"));
1655   verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1656                "                        bbbbbbbbbbb, ccccccccccc};");
1657   verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
1658                "                        // comment for bb....\n"
1659                "                        bbbbbbbbbbb, ccccccccccc};");
1660   verifyGoogleFormat(
1661       "static SomeType type = {aaaaaaaaaaa,  // comment for aa...\n"
1662       "                        bbbbbbbbbbb, ccccccccccc};");
1663   verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
1664                      "                        // comment for bb....\n"
1665                      "                        bbbbbbbbbbb, ccccccccccc};");
1666 
1667   verifyFormat("S s = {{a, b, c},  // Group #1\n"
1668                "       {d, e, f},  // Group #2\n"
1669                "       {g, h, i}}; // Group #3");
1670   verifyFormat("S s = {{// Group #1\n"
1671                "        a, b, c},\n"
1672                "       {// Group #2\n"
1673                "        d, e, f},\n"
1674                "       {// Group #3\n"
1675                "        g, h, i}};");
1676 
1677   EXPECT_EQ("S s = {\n"
1678             "    // Some comment\n"
1679             "    a,\n"
1680             "\n"
1681             "    // Comment after empty line\n"
1682             "    b}",
1683             format("S s =    {\n"
1684                    "      // Some comment\n"
1685                    "  a,\n"
1686                    "  \n"
1687                    "     // Comment after empty line\n"
1688                    "      b\n"
1689                    "}"));
1690   EXPECT_EQ("S s = {\n"
1691             "    /* Some comment */\n"
1692             "    a,\n"
1693             "\n"
1694             "    /* Comment after empty line */\n"
1695             "    b}",
1696             format("S s =    {\n"
1697                    "      /* Some comment */\n"
1698                    "  a,\n"
1699                    "  \n"
1700                    "     /* Comment after empty line */\n"
1701                    "      b\n"
1702                    "}"));
1703   verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
1704                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1705                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1706                "    0x00, 0x00, 0x00, 0x00};            // comment\n");
1707 }
1708 
1709 TEST_F(FormatTest, IgnoresIf0Contents) {
1710   EXPECT_EQ("#if 0\n"
1711             "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1712             "#endif\n"
1713             "void f() {}",
1714             format("#if 0\n"
1715                    "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1716                    "#endif\n"
1717                    "void f(  ) {  }"));
1718   EXPECT_EQ("#if false\n"
1719             "void f(  ) {  }\n"
1720             "#endif\n"
1721             "void g() {}\n",
1722             format("#if false\n"
1723                    "void f(  ) {  }\n"
1724                    "#endif\n"
1725                    "void g(  ) {  }\n"));
1726   EXPECT_EQ("enum E {\n"
1727             "  One,\n"
1728             "  Two,\n"
1729             "#if 0\n"
1730             "Three,\n"
1731             "      Four,\n"
1732             "#endif\n"
1733             "  Five\n"
1734             "};",
1735             format("enum E {\n"
1736                    "  One,Two,\n"
1737                    "#if 0\n"
1738                    "Three,\n"
1739                    "      Four,\n"
1740                    "#endif\n"
1741                    "  Five};"));
1742   EXPECT_EQ("enum F {\n"
1743             "  One,\n"
1744             "#if 1\n"
1745             "  Two,\n"
1746             "#if 0\n"
1747             "Three,\n"
1748             "      Four,\n"
1749             "#endif\n"
1750             "  Five\n"
1751             "#endif\n"
1752             "};",
1753             format("enum F {\n"
1754                    "One,\n"
1755                    "#if 1\n"
1756                    "Two,\n"
1757                    "#if 0\n"
1758                    "Three,\n"
1759                    "      Four,\n"
1760                    "#endif\n"
1761                    "Five\n"
1762                    "#endif\n"
1763                    "};"));
1764   EXPECT_EQ("enum G {\n"
1765             "  One,\n"
1766             "#if 0\n"
1767             "Two,\n"
1768             "#else\n"
1769             "  Three,\n"
1770             "#endif\n"
1771             "  Four\n"
1772             "};",
1773             format("enum G {\n"
1774                    "One,\n"
1775                    "#if 0\n"
1776                    "Two,\n"
1777                    "#else\n"
1778                    "Three,\n"
1779                    "#endif\n"
1780                    "Four\n"
1781                    "};"));
1782   EXPECT_EQ("enum H {\n"
1783             "  One,\n"
1784             "#if 0\n"
1785             "#ifdef Q\n"
1786             "Two,\n"
1787             "#else\n"
1788             "Three,\n"
1789             "#endif\n"
1790             "#endif\n"
1791             "  Four\n"
1792             "};",
1793             format("enum H {\n"
1794                    "One,\n"
1795                    "#if 0\n"
1796                    "#ifdef Q\n"
1797                    "Two,\n"
1798                    "#else\n"
1799                    "Three,\n"
1800                    "#endif\n"
1801                    "#endif\n"
1802                    "Four\n"
1803                    "};"));
1804   EXPECT_EQ("enum I {\n"
1805             "  One,\n"
1806             "#if /* test */ 0 || 1\n"
1807             "Two,\n"
1808             "Three,\n"
1809             "#endif\n"
1810             "  Four\n"
1811             "};",
1812             format("enum I {\n"
1813                    "One,\n"
1814                    "#if /* test */ 0 || 1\n"
1815                    "Two,\n"
1816                    "Three,\n"
1817                    "#endif\n"
1818                    "Four\n"
1819                    "};"));
1820   EXPECT_EQ("enum J {\n"
1821             "  One,\n"
1822             "#if 0\n"
1823             "#if 0\n"
1824             "Two,\n"
1825             "#else\n"
1826             "Three,\n"
1827             "#endif\n"
1828             "Four,\n"
1829             "#endif\n"
1830             "  Five\n"
1831             "};",
1832             format("enum J {\n"
1833                    "One,\n"
1834                    "#if 0\n"
1835                    "#if 0\n"
1836                    "Two,\n"
1837                    "#else\n"
1838                    "Three,\n"
1839                    "#endif\n"
1840                    "Four,\n"
1841                    "#endif\n"
1842                    "Five\n"
1843                    "};"));
1844 }
1845 
1846 //===----------------------------------------------------------------------===//
1847 // Tests for classes, namespaces, etc.
1848 //===----------------------------------------------------------------------===//
1849 
1850 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1851   verifyFormat("class A {};");
1852 }
1853 
1854 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1855   verifyFormat("class A {\n"
1856                "public:\n"
1857                "public: // comment\n"
1858                "protected:\n"
1859                "private:\n"
1860                "  void f() {}\n"
1861                "};");
1862   verifyGoogleFormat("class A {\n"
1863                      " public:\n"
1864                      " protected:\n"
1865                      " private:\n"
1866                      "  void f() {}\n"
1867                      "};");
1868   verifyFormat("class A {\n"
1869                "public slots:\n"
1870                "  void f() {}\n"
1871                "public Q_SLOTS:\n"
1872                "  void f() {}\n"
1873                "signals:\n"
1874                "  void g();\n"
1875                "};");
1876 
1877   // Don't interpret 'signals' the wrong way.
1878   verifyFormat("signals.set();");
1879   verifyFormat("for (Signals signals : f()) {\n}");
1880   verifyFormat("{\n"
1881                "  signals.set(); // This needs indentation.\n"
1882                "}");
1883 }
1884 
1885 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1886   EXPECT_EQ("class A {\n"
1887             "public:\n"
1888             "  void f();\n"
1889             "\n"
1890             "private:\n"
1891             "  void g() {}\n"
1892             "  // test\n"
1893             "protected:\n"
1894             "  int h;\n"
1895             "};",
1896             format("class A {\n"
1897                    "public:\n"
1898                    "void f();\n"
1899                    "private:\n"
1900                    "void g() {}\n"
1901                    "// test\n"
1902                    "protected:\n"
1903                    "int h;\n"
1904                    "};"));
1905   EXPECT_EQ("class A {\n"
1906             "protected:\n"
1907             "public:\n"
1908             "  void f();\n"
1909             "};",
1910             format("class A {\n"
1911                    "protected:\n"
1912                    "\n"
1913                    "public:\n"
1914                    "\n"
1915                    "  void f();\n"
1916                    "};"));
1917 
1918   // Even ensure proper spacing inside macros.
1919   EXPECT_EQ("#define B     \\\n"
1920             "  class A {   \\\n"
1921             "   protected: \\\n"
1922             "   public:    \\\n"
1923             "    void f(); \\\n"
1924             "  };",
1925             format("#define B     \\\n"
1926                    "  class A {   \\\n"
1927                    "   protected: \\\n"
1928                    "              \\\n"
1929                    "   public:    \\\n"
1930                    "              \\\n"
1931                    "    void f(); \\\n"
1932                    "  };",
1933                    getGoogleStyle()));
1934   // But don't remove empty lines after macros ending in access specifiers.
1935   EXPECT_EQ("#define A private:\n"
1936             "\n"
1937             "int i;",
1938             format("#define A         private:\n"
1939                    "\n"
1940                    "int              i;"));
1941 }
1942 
1943 TEST_F(FormatTest, FormatsClasses) {
1944   verifyFormat("class A : public B {};");
1945   verifyFormat("class A : public ::B {};");
1946 
1947   verifyFormat(
1948       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1949       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1950   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1951                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1952                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1953   verifyFormat(
1954       "class A : public B, public C, public D, public E, public F {};");
1955   verifyFormat("class AAAAAAAAAAAA : public B,\n"
1956                "                     public C,\n"
1957                "                     public D,\n"
1958                "                     public E,\n"
1959                "                     public F,\n"
1960                "                     public G {};");
1961 
1962   verifyFormat("class\n"
1963                "    ReallyReallyLongClassName {\n"
1964                "  int i;\n"
1965                "};",
1966                getLLVMStyleWithColumns(32));
1967   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
1968                "                           aaaaaaaaaaaaaaaa> {};");
1969   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
1970                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
1971                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
1972   verifyFormat("template <class R, class C>\n"
1973                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
1974                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
1975   verifyFormat("class ::A::B {};");
1976 }
1977 
1978 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1979   verifyFormat("class A {\n} a, b;");
1980   verifyFormat("struct A {\n} a, b;");
1981   verifyFormat("union A {\n} a;");
1982 }
1983 
1984 TEST_F(FormatTest, FormatsEnum) {
1985   verifyFormat("enum {\n"
1986                "  Zero,\n"
1987                "  One = 1,\n"
1988                "  Two = One + 1,\n"
1989                "  Three = (One + Two),\n"
1990                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1991                "  Five = (One, Two, Three, Four, 5)\n"
1992                "};");
1993   verifyGoogleFormat("enum {\n"
1994                      "  Zero,\n"
1995                      "  One = 1,\n"
1996                      "  Two = One + 1,\n"
1997                      "  Three = (One + Two),\n"
1998                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1999                      "  Five = (One, Two, Three, Four, 5)\n"
2000                      "};");
2001   verifyFormat("enum Enum {};");
2002   verifyFormat("enum {};");
2003   verifyFormat("enum X E {} d;");
2004   verifyFormat("enum __attribute__((...)) E {} d;");
2005   verifyFormat("enum __declspec__((...)) E {} d;");
2006   verifyFormat("enum {\n"
2007                "  Bar = Foo<int, int>::value\n"
2008                "};",
2009                getLLVMStyleWithColumns(30));
2010 
2011   verifyFormat("enum ShortEnum { A, B, C };");
2012   verifyGoogleFormat("enum ShortEnum { A, B, C };");
2013 
2014   EXPECT_EQ("enum KeepEmptyLines {\n"
2015             "  ONE,\n"
2016             "\n"
2017             "  TWO,\n"
2018             "\n"
2019             "  THREE\n"
2020             "}",
2021             format("enum KeepEmptyLines {\n"
2022                    "  ONE,\n"
2023                    "\n"
2024                    "  TWO,\n"
2025                    "\n"
2026                    "\n"
2027                    "  THREE\n"
2028                    "}"));
2029   verifyFormat("enum E { // comment\n"
2030                "  ONE,\n"
2031                "  TWO\n"
2032                "};\n"
2033                "int i;");
2034   // Not enums.
2035   verifyFormat("enum X f() {\n"
2036                "  a();\n"
2037                "  return 42;\n"
2038                "}");
2039   verifyFormat("enum X Type::f() {\n"
2040                "  a();\n"
2041                "  return 42;\n"
2042                "}");
2043   verifyFormat("enum ::X f() {\n"
2044                "  a();\n"
2045                "  return 42;\n"
2046                "}");
2047   verifyFormat("enum ns::X f() {\n"
2048                "  a();\n"
2049                "  return 42;\n"
2050                "}");
2051 }
2052 
2053 TEST_F(FormatTest, FormatsEnumsWithErrors) {
2054   verifyFormat("enum Type {\n"
2055                "  One = 0; // These semicolons should be commas.\n"
2056                "  Two = 1;\n"
2057                "};");
2058   verifyFormat("namespace n {\n"
2059                "enum Type {\n"
2060                "  One,\n"
2061                "  Two, // missing };\n"
2062                "  int i;\n"
2063                "}\n"
2064                "void g() {}");
2065 }
2066 
2067 TEST_F(FormatTest, FormatsEnumStruct) {
2068   verifyFormat("enum struct {\n"
2069                "  Zero,\n"
2070                "  One = 1,\n"
2071                "  Two = One + 1,\n"
2072                "  Three = (One + Two),\n"
2073                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2074                "  Five = (One, Two, Three, Four, 5)\n"
2075                "};");
2076   verifyFormat("enum struct Enum {};");
2077   verifyFormat("enum struct {};");
2078   verifyFormat("enum struct X E {} d;");
2079   verifyFormat("enum struct __attribute__((...)) E {} d;");
2080   verifyFormat("enum struct __declspec__((...)) E {} d;");
2081   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
2082 }
2083 
2084 TEST_F(FormatTest, FormatsEnumClass) {
2085   verifyFormat("enum class {\n"
2086                "  Zero,\n"
2087                "  One = 1,\n"
2088                "  Two = One + 1,\n"
2089                "  Three = (One + Two),\n"
2090                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2091                "  Five = (One, Two, Three, Four, 5)\n"
2092                "};");
2093   verifyFormat("enum class Enum {};");
2094   verifyFormat("enum class {};");
2095   verifyFormat("enum class X E {} d;");
2096   verifyFormat("enum class __attribute__((...)) E {} d;");
2097   verifyFormat("enum class __declspec__((...)) E {} d;");
2098   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
2099 }
2100 
2101 TEST_F(FormatTest, FormatsEnumTypes) {
2102   verifyFormat("enum X : int {\n"
2103                "  A, // Force multiple lines.\n"
2104                "  B\n"
2105                "};");
2106   verifyFormat("enum X : int { A, B };");
2107   verifyFormat("enum X : std::uint32_t { A, B };");
2108 }
2109 
2110 TEST_F(FormatTest, FormatsNSEnums) {
2111   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
2112   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
2113                      "  // Information about someDecentlyLongValue.\n"
2114                      "  someDecentlyLongValue,\n"
2115                      "  // Information about anotherDecentlyLongValue.\n"
2116                      "  anotherDecentlyLongValue,\n"
2117                      "  // Information about aThirdDecentlyLongValue.\n"
2118                      "  aThirdDecentlyLongValue\n"
2119                      "};");
2120   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
2121                      "  a = 1,\n"
2122                      "  b = 2,\n"
2123                      "  c = 3,\n"
2124                      "};");
2125   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
2126                      "  a = 1,\n"
2127                      "  b = 2,\n"
2128                      "  c = 3,\n"
2129                      "};");
2130   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
2131                      "  a = 1,\n"
2132                      "  b = 2,\n"
2133                      "  c = 3,\n"
2134                      "};");
2135 }
2136 
2137 TEST_F(FormatTest, FormatsBitfields) {
2138   verifyFormat("struct Bitfields {\n"
2139                "  unsigned sClass : 8;\n"
2140                "  unsigned ValueKind : 2;\n"
2141                "};");
2142   verifyFormat("struct A {\n"
2143                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
2144                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
2145                "};");
2146   verifyFormat("struct MyStruct {\n"
2147                "  uchar data;\n"
2148                "  uchar : 8;\n"
2149                "  uchar : 8;\n"
2150                "  uchar other;\n"
2151                "};");
2152 }
2153 
2154 TEST_F(FormatTest, FormatsNamespaces) {
2155   verifyFormat("namespace some_namespace {\n"
2156                "class A {};\n"
2157                "void f() { f(); }\n"
2158                "}");
2159   verifyFormat("namespace {\n"
2160                "class A {};\n"
2161                "void f() { f(); }\n"
2162                "}");
2163   verifyFormat("inline namespace X {\n"
2164                "class A {};\n"
2165                "void f() { f(); }\n"
2166                "}");
2167   verifyFormat("using namespace some_namespace;\n"
2168                "class A {};\n"
2169                "void f() { f(); }");
2170 
2171   // This code is more common than we thought; if we
2172   // layout this correctly the semicolon will go into
2173   // its own line, which is undesirable.
2174   verifyFormat("namespace {};");
2175   verifyFormat("namespace {\n"
2176                "class A {};\n"
2177                "};");
2178 
2179   verifyFormat("namespace {\n"
2180                "int SomeVariable = 0; // comment\n"
2181                "} // namespace");
2182   EXPECT_EQ("#ifndef HEADER_GUARD\n"
2183             "#define HEADER_GUARD\n"
2184             "namespace my_namespace {\n"
2185             "int i;\n"
2186             "} // my_namespace\n"
2187             "#endif // HEADER_GUARD",
2188             format("#ifndef HEADER_GUARD\n"
2189                    " #define HEADER_GUARD\n"
2190                    "   namespace my_namespace {\n"
2191                    "int i;\n"
2192                    "}    // my_namespace\n"
2193                    "#endif    // HEADER_GUARD"));
2194 
2195   FormatStyle Style = getLLVMStyle();
2196   Style.NamespaceIndentation = FormatStyle::NI_All;
2197   EXPECT_EQ("namespace out {\n"
2198             "  int i;\n"
2199             "  namespace in {\n"
2200             "    int i;\n"
2201             "  } // namespace\n"
2202             "} // namespace",
2203             format("namespace out {\n"
2204                    "int i;\n"
2205                    "namespace in {\n"
2206                    "int i;\n"
2207                    "} // namespace\n"
2208                    "} // namespace",
2209                    Style));
2210 
2211   Style.NamespaceIndentation = FormatStyle::NI_Inner;
2212   EXPECT_EQ("namespace out {\n"
2213             "int i;\n"
2214             "namespace in {\n"
2215             "  int i;\n"
2216             "} // namespace\n"
2217             "} // namespace",
2218             format("namespace out {\n"
2219                    "int i;\n"
2220                    "namespace in {\n"
2221                    "int i;\n"
2222                    "} // namespace\n"
2223                    "} // namespace",
2224                    Style));
2225 }
2226 
2227 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
2228 
2229 TEST_F(FormatTest, FormatsInlineASM) {
2230   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
2231   verifyFormat("asm(\"nop\" ::: \"memory\");");
2232   verifyFormat(
2233       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
2234       "    \"cpuid\\n\\t\"\n"
2235       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
2236       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
2237       "    : \"a\"(value));");
2238   EXPECT_EQ(
2239       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
2240       "  __asm {\n"
2241       "        mov     edx,[that] // vtable in edx\n"
2242       "        mov     eax,methodIndex\n"
2243       "        call    [edx][eax*4] // stdcall\n"
2244       "  }\n"
2245       "}",
2246       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
2247              "    __asm {\n"
2248              "        mov     edx,[that] // vtable in edx\n"
2249              "        mov     eax,methodIndex\n"
2250              "        call    [edx][eax*4] // stdcall\n"
2251              "    }\n"
2252              "}"));
2253   EXPECT_EQ("_asm {\n"
2254             "  xor eax, eax;\n"
2255             "  cpuid;\n"
2256             "}",
2257             format("_asm {\n"
2258                    "  xor eax, eax;\n"
2259                    "  cpuid;\n"
2260                    "}"));
2261   verifyFormat("void function() {\n"
2262                "  // comment\n"
2263                "  asm(\"\");\n"
2264                "}");
2265   EXPECT_EQ("__asm {\n"
2266             "}\n"
2267             "int i;",
2268             format("__asm   {\n"
2269                    "}\n"
2270                    "int   i;"));
2271 }
2272 
2273 TEST_F(FormatTest, FormatTryCatch) {
2274   verifyFormat("try {\n"
2275                "  throw a * b;\n"
2276                "} catch (int a) {\n"
2277                "  // Do nothing.\n"
2278                "} catch (...) {\n"
2279                "  exit(42);\n"
2280                "}");
2281 
2282   // Function-level try statements.
2283   verifyFormat("int f() try { return 4; } catch (...) {\n"
2284                "  return 5;\n"
2285                "}");
2286   verifyFormat("class A {\n"
2287                "  int a;\n"
2288                "  A() try : a(0) {\n"
2289                "  } catch (...) {\n"
2290                "    throw;\n"
2291                "  }\n"
2292                "};\n");
2293 
2294   // Incomplete try-catch blocks.
2295   verifyIncompleteFormat("try {} catch (");
2296 }
2297 
2298 TEST_F(FormatTest, FormatSEHTryCatch) {
2299   verifyFormat("__try {\n"
2300                "  int a = b * c;\n"
2301                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
2302                "  // Do nothing.\n"
2303                "}");
2304 
2305   verifyFormat("__try {\n"
2306                "  int a = b * c;\n"
2307                "} __finally {\n"
2308                "  // Do nothing.\n"
2309                "}");
2310 
2311   verifyFormat("DEBUG({\n"
2312                "  __try {\n"
2313                "  } __finally {\n"
2314                "  }\n"
2315                "});\n");
2316 }
2317 
2318 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
2319   verifyFormat("try {\n"
2320                "  f();\n"
2321                "} catch {\n"
2322                "  g();\n"
2323                "}");
2324   verifyFormat("try {\n"
2325                "  f();\n"
2326                "} catch (A a) MACRO(x) {\n"
2327                "  g();\n"
2328                "} catch (B b) MACRO(x) {\n"
2329                "  g();\n"
2330                "}");
2331 }
2332 
2333 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
2334   FormatStyle Style = getLLVMStyle();
2335   for (auto BraceStyle :
2336        {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, FormatStyle::BS_WebKit}) {
2337     Style.BreakBeforeBraces = BraceStyle;
2338     verifyFormat("try {\n"
2339                  "  // something\n"
2340                  "} catch (...) {\n"
2341                  "  // something\n"
2342                  "}",
2343                  Style);
2344   }
2345   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
2346   verifyFormat("try {\n"
2347                "  // something\n"
2348                "}\n"
2349                "catch (...) {\n"
2350                "  // something\n"
2351                "}",
2352                Style);
2353   verifyFormat("__try {\n"
2354                "  // something\n"
2355                "}\n"
2356                "__finally {\n"
2357                "  // something\n"
2358                "}",
2359                Style);
2360   verifyFormat("@try {\n"
2361                "  // something\n"
2362                "}\n"
2363                "@finally {\n"
2364                "  // something\n"
2365                "}",
2366                Style);
2367   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2368   verifyFormat("try\n"
2369                "{\n"
2370                "  // something\n"
2371                "}\n"
2372                "catch (...)\n"
2373                "{\n"
2374                "  // something\n"
2375                "}",
2376                Style);
2377   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
2378   verifyFormat("try\n"
2379                "  {\n"
2380                "    // something\n"
2381                "  }\n"
2382                "catch (...)\n"
2383                "  {\n"
2384                "    // something\n"
2385                "  }",
2386                Style);
2387 }
2388 
2389 TEST_F(FormatTest, FormatObjCTryCatch) {
2390   verifyFormat("@try {\n"
2391                "  f();\n"
2392                "} @catch (NSException e) {\n"
2393                "  @throw;\n"
2394                "} @finally {\n"
2395                "  exit(42);\n"
2396                "}");
2397   verifyFormat("DEBUG({\n"
2398                "  @try {\n"
2399                "  } @finally {\n"
2400                "  }\n"
2401                "});\n");
2402 }
2403 
2404 TEST_F(FormatTest, FormatObjCAutoreleasepool) {
2405   FormatStyle Style = getLLVMStyle();
2406   verifyFormat("@autoreleasepool {\n"
2407                "  f();\n"
2408                "}\n"
2409                "@autoreleasepool {\n"
2410                "  f();\n"
2411                "}\n",
2412                Style);
2413   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2414   verifyFormat("@autoreleasepool\n"
2415                "{\n"
2416                "  f();\n"
2417                "}\n"
2418                "@autoreleasepool\n"
2419                "{\n"
2420                "  f();\n"
2421                "}\n",
2422                Style);
2423 }
2424 
2425 TEST_F(FormatTest, StaticInitializers) {
2426   verifyFormat("static SomeClass SC = {1, 'a'};");
2427 
2428   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
2429                "    100000000, "
2430                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
2431 
2432   // Here, everything other than the "}" would fit on a line.
2433   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
2434                "    10000000000000000000000000};");
2435   EXPECT_EQ("S s = {a,\n"
2436             "\n"
2437             "       b};",
2438             format("S s = {\n"
2439                    "  a,\n"
2440                    "\n"
2441                    "  b\n"
2442                    "};"));
2443 
2444   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
2445   // line. However, the formatting looks a bit off and this probably doesn't
2446   // happen often in practice.
2447   verifyFormat("static int Variable[1] = {\n"
2448                "    {1000000000000000000000000000000000000}};",
2449                getLLVMStyleWithColumns(40));
2450 }
2451 
2452 TEST_F(FormatTest, DesignatedInitializers) {
2453   verifyFormat("const struct A a = {.a = 1, .b = 2};");
2454   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
2455                "                    .bbbbbbbbbb = 2,\n"
2456                "                    .cccccccccc = 3,\n"
2457                "                    .dddddddddd = 4,\n"
2458                "                    .eeeeeeeeee = 5};");
2459   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2460                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
2461                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
2462                "    .ccccccccccccccccccccccccccc = 3,\n"
2463                "    .ddddddddddddddddddddddddddd = 4,\n"
2464                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
2465 
2466   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
2467 }
2468 
2469 TEST_F(FormatTest, NestedStaticInitializers) {
2470   verifyFormat("static A x = {{{}}};\n");
2471   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
2472                "               {init1, init2, init3, init4}}};",
2473                getLLVMStyleWithColumns(50));
2474 
2475   verifyFormat("somes Status::global_reps[3] = {\n"
2476                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2477                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2478                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
2479                getLLVMStyleWithColumns(60));
2480   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
2481                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2482                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2483                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
2484   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
2485                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
2486                "rect.fTop}};");
2487 
2488   verifyFormat(
2489       "SomeArrayOfSomeType a = {\n"
2490       "    {{1, 2, 3},\n"
2491       "     {1, 2, 3},\n"
2492       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
2493       "      333333333333333333333333333333},\n"
2494       "     {1, 2, 3},\n"
2495       "     {1, 2, 3}}};");
2496   verifyFormat(
2497       "SomeArrayOfSomeType a = {\n"
2498       "    {{1, 2, 3}},\n"
2499       "    {{1, 2, 3}},\n"
2500       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
2501       "      333333333333333333333333333333}},\n"
2502       "    {{1, 2, 3}},\n"
2503       "    {{1, 2, 3}}};");
2504 
2505   verifyFormat("struct {\n"
2506                "  unsigned bit;\n"
2507                "  const char *const name;\n"
2508                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
2509                "                 {kOsWin, \"Windows\"},\n"
2510                "                 {kOsLinux, \"Linux\"},\n"
2511                "                 {kOsCrOS, \"Chrome OS\"}};");
2512   verifyFormat("struct {\n"
2513                "  unsigned bit;\n"
2514                "  const char *const name;\n"
2515                "} kBitsToOs[] = {\n"
2516                "    {kOsMac, \"Mac\"},\n"
2517                "    {kOsWin, \"Windows\"},\n"
2518                "    {kOsLinux, \"Linux\"},\n"
2519                "    {kOsCrOS, \"Chrome OS\"},\n"
2520                "};");
2521 }
2522 
2523 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
2524   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2525                "                      \\\n"
2526                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
2527 }
2528 
2529 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
2530   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
2531                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
2532 
2533   // Do break defaulted and deleted functions.
2534   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2535                "    default;",
2536                getLLVMStyleWithColumns(40));
2537   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2538                "    delete;",
2539                getLLVMStyleWithColumns(40));
2540 }
2541 
2542 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
2543   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
2544                getLLVMStyleWithColumns(40));
2545   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2546                getLLVMStyleWithColumns(40));
2547   EXPECT_EQ("#define Q                              \\\n"
2548             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
2549             "  \"aaaaaaaa.cpp\"",
2550             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2551                    getLLVMStyleWithColumns(40)));
2552 }
2553 
2554 TEST_F(FormatTest, UnderstandsLinePPDirective) {
2555   EXPECT_EQ("# 123 \"A string literal\"",
2556             format("   #     123    \"A string literal\""));
2557 }
2558 
2559 TEST_F(FormatTest, LayoutUnknownPPDirective) {
2560   EXPECT_EQ("#;", format("#;"));
2561   verifyFormat("#\n;\n;\n;");
2562 }
2563 
2564 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
2565   EXPECT_EQ("#line 42 \"test\"\n",
2566             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
2567   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
2568                                     getLLVMStyleWithColumns(12)));
2569 }
2570 
2571 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
2572   EXPECT_EQ("#line 42 \"test\"",
2573             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
2574   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
2575 }
2576 
2577 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
2578   verifyFormat("#define A \\x20");
2579   verifyFormat("#define A \\ x20");
2580   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
2581   verifyFormat("#define A ''");
2582   verifyFormat("#define A ''qqq");
2583   verifyFormat("#define A `qqq");
2584   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
2585   EXPECT_EQ("const char *c = STRINGIFY(\n"
2586             "\\na : b);",
2587             format("const char * c = STRINGIFY(\n"
2588                    "\\na : b);"));
2589 
2590   verifyFormat("a\r\\");
2591   verifyFormat("a\v\\");
2592   verifyFormat("a\f\\");
2593 }
2594 
2595 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
2596   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
2597   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
2598   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
2599   // FIXME: We never break before the macro name.
2600   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
2601 
2602   verifyFormat("#define A A\n#define A A");
2603   verifyFormat("#define A(X) A\n#define A A");
2604 
2605   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
2606   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
2607 }
2608 
2609 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
2610   EXPECT_EQ("// somecomment\n"
2611             "#include \"a.h\"\n"
2612             "#define A(  \\\n"
2613             "    A, B)\n"
2614             "#include \"b.h\"\n"
2615             "// somecomment\n",
2616             format("  // somecomment\n"
2617                    "  #include \"a.h\"\n"
2618                    "#define A(A,\\\n"
2619                    "    B)\n"
2620                    "    #include \"b.h\"\n"
2621                    " // somecomment\n",
2622                    getLLVMStyleWithColumns(13)));
2623 }
2624 
2625 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
2626 
2627 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
2628   EXPECT_EQ("#define A    \\\n"
2629             "  c;         \\\n"
2630             "  e;\n"
2631             "f;",
2632             format("#define A c; e;\n"
2633                    "f;",
2634                    getLLVMStyleWithColumns(14)));
2635 }
2636 
2637 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
2638 
2639 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
2640   EXPECT_EQ("int x,\n"
2641             "#define A\n"
2642             "    y;",
2643             format("int x,\n#define A\ny;"));
2644 }
2645 
2646 TEST_F(FormatTest, HashInMacroDefinition) {
2647   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
2648   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
2649   verifyFormat("#define A  \\\n"
2650                "  {        \\\n"
2651                "    f(#c); \\\n"
2652                "  }",
2653                getLLVMStyleWithColumns(11));
2654 
2655   verifyFormat("#define A(X)         \\\n"
2656                "  void function##X()",
2657                getLLVMStyleWithColumns(22));
2658 
2659   verifyFormat("#define A(a, b, c)   \\\n"
2660                "  void a##b##c()",
2661                getLLVMStyleWithColumns(22));
2662 
2663   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
2664 }
2665 
2666 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
2667   EXPECT_EQ("#define A (x)", format("#define A (x)"));
2668   EXPECT_EQ("#define A(x)", format("#define A(x)"));
2669 }
2670 
2671 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
2672   EXPECT_EQ("#define A b;", format("#define A \\\n"
2673                                    "          \\\n"
2674                                    "  b;",
2675                                    getLLVMStyleWithColumns(25)));
2676   EXPECT_EQ("#define A \\\n"
2677             "          \\\n"
2678             "  a;      \\\n"
2679             "  b;",
2680             format("#define A \\\n"
2681                    "          \\\n"
2682                    "  a;      \\\n"
2683                    "  b;",
2684                    getLLVMStyleWithColumns(11)));
2685   EXPECT_EQ("#define A \\\n"
2686             "  a;      \\\n"
2687             "          \\\n"
2688             "  b;",
2689             format("#define A \\\n"
2690                    "  a;      \\\n"
2691                    "          \\\n"
2692                    "  b;",
2693                    getLLVMStyleWithColumns(11)));
2694 }
2695 
2696 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
2697   verifyIncompleteFormat("#define A :");
2698   verifyFormat("#define SOMECASES  \\\n"
2699                "  case 1:          \\\n"
2700                "  case 2\n",
2701                getLLVMStyleWithColumns(20));
2702   verifyFormat("#define A template <typename T>");
2703   verifyIncompleteFormat("#define STR(x) #x\n"
2704                          "f(STR(this_is_a_string_literal{));");
2705   verifyFormat("#pragma omp threadprivate( \\\n"
2706                "    y)), // expected-warning",
2707                getLLVMStyleWithColumns(28));
2708   verifyFormat("#d, = };");
2709   verifyFormat("#if \"a");
2710   verifyIncompleteFormat("({\n"
2711                          "#define b     \\\n"
2712                          "  }           \\\n"
2713                          "  a\n"
2714                          "a",
2715                          getLLVMStyleWithColumns(15));
2716   verifyFormat("#define A     \\\n"
2717                "  {           \\\n"
2718                "    {\n"
2719                "#define B     \\\n"
2720                "  }           \\\n"
2721                "  }",
2722                getLLVMStyleWithColumns(15));
2723   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2724   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2725   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2726   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
2727 }
2728 
2729 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2730   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2731   EXPECT_EQ("class A : public QObject {\n"
2732             "  Q_OBJECT\n"
2733             "\n"
2734             "  A() {}\n"
2735             "};",
2736             format("class A  :  public QObject {\n"
2737                    "     Q_OBJECT\n"
2738                    "\n"
2739                    "  A() {\n}\n"
2740                    "}  ;"));
2741   EXPECT_EQ("MACRO\n"
2742             "/*static*/ int i;",
2743             format("MACRO\n"
2744                    " /*static*/ int   i;"));
2745   EXPECT_EQ("SOME_MACRO\n"
2746             "namespace {\n"
2747             "void f();\n"
2748             "}",
2749             format("SOME_MACRO\n"
2750                    "  namespace    {\n"
2751                    "void   f(  );\n"
2752                    "}"));
2753   // Only if the identifier contains at least 5 characters.
2754   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
2755   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
2756   // Only if everything is upper case.
2757   EXPECT_EQ("class A : public QObject {\n"
2758             "  Q_Object A() {}\n"
2759             "};",
2760             format("class A  :  public QObject {\n"
2761                    "     Q_Object\n"
2762                    "  A() {\n}\n"
2763                    "}  ;"));
2764 
2765   // Only if the next line can actually start an unwrapped line.
2766   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2767             format("SOME_WEIRD_LOG_MACRO\n"
2768                    "<< SomeThing;"));
2769 
2770   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2771                "(n, buffers))\n",
2772                getChromiumStyle(FormatStyle::LK_Cpp));
2773 }
2774 
2775 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2776   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2777             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2778             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2779             "class X {};\n"
2780             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2781             "int *createScopDetectionPass() { return 0; }",
2782             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2783                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2784                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2785                    "  class X {};\n"
2786                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2787                    "  int *createScopDetectionPass() { return 0; }"));
2788   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2789   // braces, so that inner block is indented one level more.
2790   EXPECT_EQ("int q() {\n"
2791             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2792             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2793             "  IPC_END_MESSAGE_MAP()\n"
2794             "}",
2795             format("int q() {\n"
2796                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2797                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2798                    "  IPC_END_MESSAGE_MAP()\n"
2799                    "}"));
2800 
2801   // Same inside macros.
2802   EXPECT_EQ("#define LIST(L) \\\n"
2803             "  L(A)          \\\n"
2804             "  L(B)          \\\n"
2805             "  L(C)",
2806             format("#define LIST(L) \\\n"
2807                    "  L(A) \\\n"
2808                    "  L(B) \\\n"
2809                    "  L(C)",
2810                    getGoogleStyle()));
2811 
2812   // These must not be recognized as macros.
2813   EXPECT_EQ("int q() {\n"
2814             "  f(x);\n"
2815             "  f(x) {}\n"
2816             "  f(x)->g();\n"
2817             "  f(x)->*g();\n"
2818             "  f(x).g();\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) ^= x;\n"
2828             "  f(x) >>= x;\n"
2829             "  f(x) <<= x;\n"
2830             "  f(x)[y].z();\n"
2831             "  LOG(INFO) << x;\n"
2832             "  ifstream(x) >> x;\n"
2833             "}\n",
2834             format("int q() {\n"
2835                    "  f(x)\n;\n"
2836                    "  f(x)\n {}\n"
2837                    "  f(x)\n->g();\n"
2838                    "  f(x)\n->*g();\n"
2839                    "  f(x)\n.g();\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 ^= x;\n"
2849                    "  f(x)\n >>= x;\n"
2850                    "  f(x)\n <<= x;\n"
2851                    "  f(x)\n[y].z();\n"
2852                    "  LOG(INFO)\n << x;\n"
2853                    "  ifstream(x)\n >> x;\n"
2854                    "}\n"));
2855   EXPECT_EQ("int q() {\n"
2856             "  F(x)\n"
2857             "  if (1) {\n"
2858             "  }\n"
2859             "  F(x)\n"
2860             "  while (1) {\n"
2861             "  }\n"
2862             "  F(x)\n"
2863             "  G(x);\n"
2864             "  F(x)\n"
2865             "  try {\n"
2866             "    Q();\n"
2867             "  } catch (...) {\n"
2868             "  }\n"
2869             "}\n",
2870             format("int q() {\n"
2871                    "F(x)\n"
2872                    "if (1) {}\n"
2873                    "F(x)\n"
2874                    "while (1) {}\n"
2875                    "F(x)\n"
2876                    "G(x);\n"
2877                    "F(x)\n"
2878                    "try { Q(); } catch (...) {}\n"
2879                    "}\n"));
2880   EXPECT_EQ("class A {\n"
2881             "  A() : t(0) {}\n"
2882             "  A(int i) noexcept() : {}\n"
2883             "  A(X x)\n" // FIXME: function-level try blocks are broken.
2884             "  try : t(0) {\n"
2885             "  } catch (...) {\n"
2886             "  }\n"
2887             "};",
2888             format("class A {\n"
2889                    "  A()\n : t(0) {}\n"
2890                    "  A(int i)\n noexcept() : {}\n"
2891                    "  A(X x)\n"
2892                    "  try : t(0) {} catch (...) {}\n"
2893                    "};"));
2894   EXPECT_EQ("class SomeClass {\n"
2895             "public:\n"
2896             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2897             "};",
2898             format("class SomeClass {\n"
2899                    "public:\n"
2900                    "  SomeClass()\n"
2901                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2902                    "};"));
2903   EXPECT_EQ("class SomeClass {\n"
2904             "public:\n"
2905             "  SomeClass()\n"
2906             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2907             "};",
2908             format("class SomeClass {\n"
2909                    "public:\n"
2910                    "  SomeClass()\n"
2911                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2912                    "};",
2913                    getLLVMStyleWithColumns(40)));
2914 }
2915 
2916 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2917   verifyFormat("#define A \\\n"
2918                "  f({     \\\n"
2919                "    g();  \\\n"
2920                "  });",
2921                getLLVMStyleWithColumns(11));
2922 }
2923 
2924 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
2925   EXPECT_EQ("{\n  {\n#define A\n  }\n}", format("{{\n#define A\n}}"));
2926 }
2927 
2928 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
2929   verifyFormat("{\n  { a #c; }\n}");
2930 }
2931 
2932 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
2933   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
2934             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
2935   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
2936             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
2937 }
2938 
2939 TEST_F(FormatTest, EscapedNewlines) {
2940   EXPECT_EQ(
2941       "#define A \\\n  int i;  \\\n  int j;",
2942       format("#define A \\\nint i;\\\n  int j;", getLLVMStyleWithColumns(11)));
2943   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
2944   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2945   EXPECT_EQ("/* \\  \\  \\\n*/", format("\\\n/* \\  \\  \\\n*/"));
2946   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
2947 }
2948 
2949 TEST_F(FormatTest, DontCrashOnBlockComments) {
2950   EXPECT_EQ(
2951       "int xxxxxxxxx; /* "
2952       "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
2953       "zzzzzz\n"
2954       "0*/",
2955       format("int xxxxxxxxx;                          /* "
2956              "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
2957              "0*/"));
2958 }
2959 
2960 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
2961   verifyFormat("#define A \\\n"
2962                "  int v(  \\\n"
2963                "      a); \\\n"
2964                "  int i;",
2965                getLLVMStyleWithColumns(11));
2966 }
2967 
2968 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
2969   EXPECT_EQ(
2970       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2971       "                      \\\n"
2972       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2973       "\n"
2974       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2975       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
2976       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
2977              "\\\n"
2978              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2979              "  \n"
2980              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2981              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
2982 }
2983 
2984 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
2985   EXPECT_EQ("int\n"
2986             "#define A\n"
2987             "    a;",
2988             format("int\n#define A\na;"));
2989   verifyFormat("functionCallTo(\n"
2990                "    someOtherFunction(\n"
2991                "        withSomeParameters, whichInSequence,\n"
2992                "        areLongerThanALine(andAnotherCall,\n"
2993                "#define A B\n"
2994                "                           withMoreParamters,\n"
2995                "                           whichStronglyInfluenceTheLayout),\n"
2996                "        andMoreParameters),\n"
2997                "    trailing);",
2998                getLLVMStyleWithColumns(69));
2999   verifyFormat("Foo::Foo()\n"
3000                "#ifdef BAR\n"
3001                "    : baz(0)\n"
3002                "#endif\n"
3003                "{\n"
3004                "}");
3005   verifyFormat("void f() {\n"
3006                "  if (true)\n"
3007                "#ifdef A\n"
3008                "    f(42);\n"
3009                "  x();\n"
3010                "#else\n"
3011                "    g();\n"
3012                "  x();\n"
3013                "#endif\n"
3014                "}");
3015   verifyFormat("void f(param1, param2,\n"
3016                "       param3,\n"
3017                "#ifdef A\n"
3018                "       param4(param5,\n"
3019                "#ifdef A1\n"
3020                "              param6,\n"
3021                "#ifdef A2\n"
3022                "              param7),\n"
3023                "#else\n"
3024                "              param8),\n"
3025                "       param9,\n"
3026                "#endif\n"
3027                "       param10,\n"
3028                "#endif\n"
3029                "       param11)\n"
3030                "#else\n"
3031                "       param12)\n"
3032                "#endif\n"
3033                "{\n"
3034                "  x();\n"
3035                "}",
3036                getLLVMStyleWithColumns(28));
3037   verifyFormat("#if 1\n"
3038                "int i;");
3039   verifyFormat("#if 1\n"
3040                "#endif\n"
3041                "#if 1\n"
3042                "#else\n"
3043                "#endif\n");
3044   verifyFormat("DEBUG({\n"
3045                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3046                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
3047                "});\n"
3048                "#if a\n"
3049                "#else\n"
3050                "#endif");
3051 
3052   verifyIncompleteFormat("void f(\n"
3053                          "#if A\n"
3054                          "    );\n"
3055                          "#else\n"
3056                          "#endif");
3057 }
3058 
3059 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
3060   verifyFormat("#endif\n"
3061                "#if B");
3062 }
3063 
3064 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
3065   FormatStyle SingleLine = getLLVMStyle();
3066   SingleLine.AllowShortIfStatementsOnASingleLine = true;
3067   verifyFormat("#if 0\n"
3068                "#elif 1\n"
3069                "#endif\n"
3070                "void foo() {\n"
3071                "  if (test) foo2();\n"
3072                "}",
3073                SingleLine);
3074 }
3075 
3076 TEST_F(FormatTest, LayoutBlockInsideParens) {
3077   verifyFormat("functionCall({ int i; });");
3078   verifyFormat("functionCall({\n"
3079                "  int i;\n"
3080                "  int j;\n"
3081                "});");
3082   verifyFormat("functionCall(\n"
3083                "    {\n"
3084                "      int i;\n"
3085                "      int j;\n"
3086                "    },\n"
3087                "    aaaa, bbbb, cccc);");
3088   verifyFormat("functionA(functionB({\n"
3089                "            int i;\n"
3090                "            int j;\n"
3091                "          }),\n"
3092                "          aaaa, bbbb, cccc);");
3093   verifyFormat("functionCall(\n"
3094                "    {\n"
3095                "      int i;\n"
3096                "      int j;\n"
3097                "    },\n"
3098                "    aaaa, bbbb, // comment\n"
3099                "    cccc);");
3100   verifyFormat("functionA(functionB({\n"
3101                "            int i;\n"
3102                "            int j;\n"
3103                "          }),\n"
3104                "          aaaa, bbbb, // comment\n"
3105                "          cccc);");
3106   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
3107   verifyFormat("functionCall(aaaa, bbbb, {\n"
3108                "  int i;\n"
3109                "  int j;\n"
3110                "});");
3111   verifyFormat(
3112       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
3113       "    {\n"
3114       "      int i; // break\n"
3115       "    },\n"
3116       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
3117       "                                     ccccccccccccccccc));");
3118   verifyFormat("DEBUG({\n"
3119                "  if (a)\n"
3120                "    f();\n"
3121                "});");
3122 }
3123 
3124 TEST_F(FormatTest, LayoutBlockInsideStatement) {
3125   EXPECT_EQ("SOME_MACRO { int i; }\n"
3126             "int i;",
3127             format("  SOME_MACRO  {int i;}  int i;"));
3128 }
3129 
3130 TEST_F(FormatTest, LayoutNestedBlocks) {
3131   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
3132                "  struct s {\n"
3133                "    int i;\n"
3134                "  };\n"
3135                "  s kBitsToOs[] = {{10}};\n"
3136                "  for (int i = 0; i < 10; ++i)\n"
3137                "    return;\n"
3138                "}");
3139   verifyFormat("call(parameter, {\n"
3140                "  something();\n"
3141                "  // Comment using all columns.\n"
3142                "  somethingelse();\n"
3143                "});",
3144                getLLVMStyleWithColumns(40));
3145   verifyFormat("DEBUG( //\n"
3146                "    { f(); }, a);");
3147   verifyFormat("DEBUG( //\n"
3148                "    {\n"
3149                "      f(); //\n"
3150                "    },\n"
3151                "    a);");
3152 
3153   EXPECT_EQ("call(parameter, {\n"
3154             "  something();\n"
3155             "  // Comment too\n"
3156             "  // looooooooooong.\n"
3157             "  somethingElse();\n"
3158             "});",
3159             format("call(parameter, {\n"
3160                    "  something();\n"
3161                    "  // Comment too looooooooooong.\n"
3162                    "  somethingElse();\n"
3163                    "});",
3164                    getLLVMStyleWithColumns(29)));
3165   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
3166   EXPECT_EQ("DEBUG({ // comment\n"
3167             "  int i;\n"
3168             "});",
3169             format("DEBUG({ // comment\n"
3170                    "int  i;\n"
3171                    "});"));
3172   EXPECT_EQ("DEBUG({\n"
3173             "  int i;\n"
3174             "\n"
3175             "  // comment\n"
3176             "  int j;\n"
3177             "});",
3178             format("DEBUG({\n"
3179                    "  int  i;\n"
3180                    "\n"
3181                    "  // comment\n"
3182                    "  int  j;\n"
3183                    "});"));
3184 
3185   verifyFormat("DEBUG({\n"
3186                "  if (a)\n"
3187                "    return;\n"
3188                "});");
3189   verifyGoogleFormat("DEBUG({\n"
3190                      "  if (a) return;\n"
3191                      "});");
3192   FormatStyle Style = getGoogleStyle();
3193   Style.ColumnLimit = 45;
3194   verifyFormat("Debug(aaaaa,\n"
3195                "      {\n"
3196                "        if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
3197                "      },\n"
3198                "      a);",
3199                Style);
3200 
3201   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
3202 
3203   verifyNoCrash("^{v^{a}}");
3204 }
3205 
3206 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
3207   EXPECT_EQ("#define MACRO()                     \\\n"
3208             "  Debug(aaa, /* force line break */ \\\n"
3209             "        {                           \\\n"
3210             "          int i;                    \\\n"
3211             "          int j;                    \\\n"
3212             "        })",
3213             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
3214                    "          {  int   i;  int  j;   })",
3215                    getGoogleStyle()));
3216 
3217   EXPECT_EQ("#define A                                       \\\n"
3218             "  [] {                                          \\\n"
3219             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
3220             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
3221             "  }",
3222             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
3223                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
3224                    getGoogleStyle()));
3225 }
3226 
3227 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
3228   EXPECT_EQ("{}", format("{}"));
3229   verifyFormat("enum E {};");
3230   verifyFormat("enum E {}");
3231 }
3232 
3233 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
3234   FormatStyle Style = getLLVMStyle();
3235   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
3236   Style.MacroBlockEnd = "^[A-Z_]+_END$";
3237   verifyFormat("FOO_BEGIN\n"
3238                "  FOO_ENTRY\n"
3239                "FOO_END", Style);
3240   verifyFormat("FOO_BEGIN\n"
3241                "  NESTED_FOO_BEGIN\n"
3242                "    NESTED_FOO_ENTRY\n"
3243                "  NESTED_FOO_END\n"
3244                "FOO_END", Style);
3245   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
3246                "  int x;\n"
3247                "  x = 1;\n"
3248                "FOO_END(Baz)", Style);
3249 }
3250 
3251 //===----------------------------------------------------------------------===//
3252 // Line break tests.
3253 //===----------------------------------------------------------------------===//
3254 
3255 TEST_F(FormatTest, PreventConfusingIndents) {
3256   verifyFormat(
3257       "void f() {\n"
3258       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
3259       "                         parameter, parameter, parameter)),\n"
3260       "                     SecondLongCall(parameter));\n"
3261       "}");
3262   verifyFormat(
3263       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3264       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3265       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3266       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
3267   verifyFormat(
3268       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3269       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
3270       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
3271       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
3272   verifyFormat(
3273       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3274       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
3275       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
3276       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
3277   verifyFormat("int a = bbbb && ccc && fffff(\n"
3278                "#define A Just forcing a new line\n"
3279                "                           ddd);");
3280 }
3281 
3282 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
3283   verifyFormat(
3284       "bool aaaaaaa =\n"
3285       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
3286       "    bbbbbbbb();");
3287   verifyFormat(
3288       "bool aaaaaaa =\n"
3289       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
3290       "    bbbbbbbb();");
3291 
3292   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3293                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
3294                "    ccccccccc == ddddddddddd;");
3295   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3296                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
3297                "    ccccccccc == ddddddddddd;");
3298   verifyFormat(
3299       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3300       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
3301       "    ccccccccc == ddddddddddd;");
3302 
3303   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3304                "                 aaaaaa) &&\n"
3305                "         bbbbbb && cccccc;");
3306   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3307                "                 aaaaaa) >>\n"
3308                "         bbbbbb;");
3309   verifyFormat("Whitespaces.addUntouchableComment(\n"
3310                "    SourceMgr.getSpellingColumnNumber(\n"
3311                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
3312                "    1);");
3313 
3314   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3315                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
3316                "    cccccc) {\n}");
3317   verifyFormat("b = a &&\n"
3318                "    // Comment\n"
3319                "    b.c && d;");
3320 
3321   // If the LHS of a comparison is not a binary expression itself, the
3322   // additional linebreak confuses many people.
3323   verifyFormat(
3324       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3325       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
3326       "}");
3327   verifyFormat(
3328       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3329       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3330       "}");
3331   verifyFormat(
3332       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
3333       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3334       "}");
3335   // Even explicit parentheses stress the precedence enough to make the
3336   // additional break unnecessary.
3337   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3338                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3339                "}");
3340   // This cases is borderline, but with the indentation it is still readable.
3341   verifyFormat(
3342       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3343       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3344       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
3345       "}",
3346       getLLVMStyleWithColumns(75));
3347 
3348   // If the LHS is a binary expression, we should still use the additional break
3349   // as otherwise the formatting hides the operator precedence.
3350   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3351                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3352                "    5) {\n"
3353                "}");
3354 
3355   FormatStyle OnePerLine = getLLVMStyle();
3356   OnePerLine.BinPackParameters = false;
3357   verifyFormat(
3358       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3359       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3360       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
3361       OnePerLine);
3362 }
3363 
3364 TEST_F(FormatTest, ExpressionIndentation) {
3365   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3366                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3367                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3368                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3369                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3370                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
3371                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3372                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
3373                "                 ccccccccccccccccccccccccccccccccccccccccc;");
3374   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3375                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3376                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3377                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3378   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3379                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3380                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3381                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3382   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3383                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3384                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3385                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3386   verifyFormat("if () {\n"
3387                "} else if (aaaaa &&\n"
3388                "           bbbbb > // break\n"
3389                "               ccccc) {\n"
3390                "}");
3391 
3392   // Presence of a trailing comment used to change indentation of b.
3393   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
3394                "       b;\n"
3395                "return aaaaaaaaaaaaaaaaaaa +\n"
3396                "       b; //",
3397                getLLVMStyleWithColumns(30));
3398 }
3399 
3400 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
3401   // Not sure what the best system is here. Like this, the LHS can be found
3402   // immediately above an operator (everything with the same or a higher
3403   // indent). The RHS is aligned right of the operator and so compasses
3404   // everything until something with the same indent as the operator is found.
3405   // FIXME: Is this a good system?
3406   FormatStyle Style = getLLVMStyle();
3407   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
3408   verifyFormat(
3409       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3410       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3411       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3412       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3413       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3414       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3415       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3416       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3417       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
3418       Style);
3419   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3420                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3421                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3422                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3423                Style);
3424   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3425                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3426                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3427                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3428                Style);
3429   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3430                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3431                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3432                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3433                Style);
3434   verifyFormat("if () {\n"
3435                "} else if (aaaaa\n"
3436                "           && bbbbb // break\n"
3437                "                  > ccccc) {\n"
3438                "}",
3439                Style);
3440   verifyFormat("return (a)\n"
3441                "       // comment\n"
3442                "       + b;",
3443                Style);
3444   verifyFormat(
3445       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3446       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3447       "             + cc;",
3448       Style);
3449 
3450   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3451                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3452                Style);
3453 
3454   // Forced by comments.
3455   verifyFormat(
3456       "unsigned ContentSize =\n"
3457       "    sizeof(int16_t)   // DWARF ARange version number\n"
3458       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
3459       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
3460       "    + sizeof(int8_t); // Segment Size (in bytes)");
3461 
3462   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
3463                "       == boost::fusion::at_c<1>(iiii).second;",
3464                Style);
3465 
3466   Style.ColumnLimit = 60;
3467   verifyFormat("zzzzzzzzzz\n"
3468                "    = bbbbbbbbbbbbbbbbb\n"
3469                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
3470                Style);
3471 }
3472 
3473 TEST_F(FormatTest, NoOperandAlignment) {
3474   FormatStyle Style = getLLVMStyle();
3475   Style.AlignOperands = false;
3476   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3477   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3478                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3479                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3480                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3481                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3482                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3483                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3484                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3485                "        > ccccccccccccccccccccccccccccccccccccccccc;",
3486                Style);
3487 
3488   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3489                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3490                "    + cc;",
3491                Style);
3492   verifyFormat("int a = aa\n"
3493                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3494                "        * cccccccccccccccccccccccccccccccccccc;",
3495                Style);
3496 
3497   Style.AlignAfterOpenBracket = false;
3498   verifyFormat("return (a > b\n"
3499                "    // comment1\n"
3500                "    // comment2\n"
3501                "    || c);",
3502                Style);
3503 }
3504 
3505 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
3506   FormatStyle Style = getLLVMStyle();
3507   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3508   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3509                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3510                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
3511                Style);
3512 }
3513 
3514 TEST_F(FormatTest, ConstructorInitializers) {
3515   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3516   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
3517                getLLVMStyleWithColumns(45));
3518   verifyFormat("Constructor()\n"
3519                "    : Inttializer(FitsOnTheLine) {}",
3520                getLLVMStyleWithColumns(44));
3521   verifyFormat("Constructor()\n"
3522                "    : Inttializer(FitsOnTheLine) {}",
3523                getLLVMStyleWithColumns(43));
3524 
3525   verifyFormat("template <typename T>\n"
3526                "Constructor() : Initializer(FitsOnTheLine) {}",
3527                getLLVMStyleWithColumns(45));
3528 
3529   verifyFormat(
3530       "SomeClass::Constructor()\n"
3531       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3532 
3533   verifyFormat(
3534       "SomeClass::Constructor()\n"
3535       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3536       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
3537   verifyFormat(
3538       "SomeClass::Constructor()\n"
3539       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3540       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3541   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3542                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3543                "    : aaaaaaaaaa(aaaaaa) {}");
3544 
3545   verifyFormat("Constructor()\n"
3546                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3547                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3548                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3549                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
3550 
3551   verifyFormat("Constructor()\n"
3552                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3553                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3554 
3555   verifyFormat("Constructor(int Parameter = 0)\n"
3556                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3557                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
3558   verifyFormat("Constructor()\n"
3559                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3560                "}",
3561                getLLVMStyleWithColumns(60));
3562   verifyFormat("Constructor()\n"
3563                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3564                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
3565 
3566   // Here a line could be saved by splitting the second initializer onto two
3567   // lines, but that is not desirable.
3568   verifyFormat("Constructor()\n"
3569                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3570                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
3571                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3572 
3573   FormatStyle OnePerLine = getLLVMStyle();
3574   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3575   verifyFormat("SomeClass::Constructor()\n"
3576                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3577                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3578                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3579                OnePerLine);
3580   verifyFormat("SomeClass::Constructor()\n"
3581                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3582                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3583                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3584                OnePerLine);
3585   verifyFormat("MyClass::MyClass(int var)\n"
3586                "    : some_var_(var),            // 4 space indent\n"
3587                "      some_other_var_(var + 1) { // lined up\n"
3588                "}",
3589                OnePerLine);
3590   verifyFormat("Constructor()\n"
3591                "    : aaaaa(aaaaaa),\n"
3592                "      aaaaa(aaaaaa),\n"
3593                "      aaaaa(aaaaaa),\n"
3594                "      aaaaa(aaaaaa),\n"
3595                "      aaaaa(aaaaaa) {}",
3596                OnePerLine);
3597   verifyFormat("Constructor()\n"
3598                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3599                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
3600                OnePerLine);
3601 
3602   EXPECT_EQ("Constructor()\n"
3603             "    : // Comment forcing unwanted break.\n"
3604             "      aaaa(aaaa) {}",
3605             format("Constructor() :\n"
3606                    "    // Comment forcing unwanted break.\n"
3607                    "    aaaa(aaaa) {}"));
3608 }
3609 
3610 TEST_F(FormatTest, MemoizationTests) {
3611   // This breaks if the memoization lookup does not take \c Indent and
3612   // \c LastSpace into account.
3613   verifyFormat(
3614       "extern CFRunLoopTimerRef\n"
3615       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
3616       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
3617       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
3618       "                     CFRunLoopTimerContext *context) {}");
3619 
3620   // Deep nesting somewhat works around our memoization.
3621   verifyFormat(
3622       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3623       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3624       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3625       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3626       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
3627       getLLVMStyleWithColumns(65));
3628   verifyFormat(
3629       "aaaaa(\n"
3630       "    aaaaa,\n"
3631       "    aaaaa(\n"
3632       "        aaaaa,\n"
3633       "        aaaaa(\n"
3634       "            aaaaa,\n"
3635       "            aaaaa(\n"
3636       "                aaaaa,\n"
3637       "                aaaaa(\n"
3638       "                    aaaaa,\n"
3639       "                    aaaaa(\n"
3640       "                        aaaaa,\n"
3641       "                        aaaaa(\n"
3642       "                            aaaaa,\n"
3643       "                            aaaaa(\n"
3644       "                                aaaaa,\n"
3645       "                                aaaaa(\n"
3646       "                                    aaaaa,\n"
3647       "                                    aaaaa(\n"
3648       "                                        aaaaa,\n"
3649       "                                        aaaaa(\n"
3650       "                                            aaaaa,\n"
3651       "                                            aaaaa(\n"
3652       "                                                aaaaa,\n"
3653       "                                                aaaaa))))))))))));",
3654       getLLVMStyleWithColumns(65));
3655   verifyFormat(
3656       "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"
3657       "                                  a),\n"
3658       "                                a),\n"
3659       "                              a),\n"
3660       "                            a),\n"
3661       "                          a),\n"
3662       "                        a),\n"
3663       "                      a),\n"
3664       "                    a),\n"
3665       "                  a),\n"
3666       "                a),\n"
3667       "              a),\n"
3668       "            a),\n"
3669       "          a),\n"
3670       "        a),\n"
3671       "      a),\n"
3672       "    a),\n"
3673       "  a)",
3674       getLLVMStyleWithColumns(65));
3675 
3676   // This test takes VERY long when memoization is broken.
3677   FormatStyle OnePerLine = getLLVMStyle();
3678   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3679   OnePerLine.BinPackParameters = false;
3680   std::string input = "Constructor()\n"
3681                       "    : aaaa(a,\n";
3682   for (unsigned i = 0, e = 80; i != e; ++i) {
3683     input += "           a,\n";
3684   }
3685   input += "           a) {}";
3686   verifyFormat(input, OnePerLine);
3687 }
3688 
3689 TEST_F(FormatTest, BreaksAsHighAsPossible) {
3690   verifyFormat(
3691       "void f() {\n"
3692       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
3693       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
3694       "    f();\n"
3695       "}");
3696   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
3697                "    Intervals[i - 1].getRange().getLast()) {\n}");
3698 }
3699 
3700 TEST_F(FormatTest, BreaksFunctionDeclarations) {
3701   // Principially, we break function declarations in a certain order:
3702   // 1) break amongst arguments.
3703   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
3704                "                              Cccccccccccccc cccccccccccccc);");
3705   verifyFormat("template <class TemplateIt>\n"
3706                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
3707                "                            TemplateIt *stop) {}");
3708 
3709   // 2) break after return type.
3710   verifyFormat(
3711       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3712       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
3713       getGoogleStyle());
3714 
3715   // 3) break after (.
3716   verifyFormat(
3717       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
3718       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
3719       getGoogleStyle());
3720 
3721   // 4) break before after nested name specifiers.
3722   verifyFormat(
3723       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3724       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
3725       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
3726       getGoogleStyle());
3727 
3728   // However, there are exceptions, if a sufficient amount of lines can be
3729   // saved.
3730   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
3731   // more adjusting.
3732   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3733                "                                  Cccccccccccccc cccccccccc,\n"
3734                "                                  Cccccccccccccc cccccccccc,\n"
3735                "                                  Cccccccccccccc cccccccccc,\n"
3736                "                                  Cccccccccccccc cccccccccc);");
3737   verifyFormat(
3738       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3739       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3740       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3741       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
3742       getGoogleStyle());
3743   verifyFormat(
3744       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3745       "                                          Cccccccccccccc cccccccccc,\n"
3746       "                                          Cccccccccccccc cccccccccc,\n"
3747       "                                          Cccccccccccccc cccccccccc,\n"
3748       "                                          Cccccccccccccc cccccccccc,\n"
3749       "                                          Cccccccccccccc cccccccccc,\n"
3750       "                                          Cccccccccccccc cccccccccc);");
3751   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3752                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3753                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3754                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3755                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
3756 
3757   // Break after multi-line parameters.
3758   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3759                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3760                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3761                "    bbbb bbbb);");
3762   verifyFormat("void SomeLoooooooooooongFunction(\n"
3763                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
3764                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3765                "    int bbbbbbbbbbbbb);");
3766 
3767   // Treat overloaded operators like other functions.
3768   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3769                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
3770   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3771                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
3772   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3773                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
3774   verifyGoogleFormat(
3775       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
3776       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3777   verifyGoogleFormat(
3778       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
3779       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3780   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3781                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3782   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
3783                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3784   verifyGoogleFormat(
3785       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
3786       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3787       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
3788 
3789   FormatStyle Style = getLLVMStyle();
3790   Style.PointerAlignment = FormatStyle::PAS_Left;
3791   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3792                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
3793                Style);
3794   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
3795                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3796                Style);
3797 }
3798 
3799 TEST_F(FormatTest, TrailingReturnType) {
3800   verifyFormat("auto foo() -> int;\n");
3801   verifyFormat("struct S {\n"
3802                "  auto bar() const -> int;\n"
3803                "};");
3804   verifyFormat("template <size_t Order, typename T>\n"
3805                "auto load_img(const std::string &filename)\n"
3806                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
3807   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
3808                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
3809   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
3810   verifyFormat("template <typename T>\n"
3811                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
3812                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
3813 
3814   // Not trailing return types.
3815   verifyFormat("void f() { auto a = b->c(); }");
3816 }
3817 
3818 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
3819   // Avoid breaking before trailing 'const' or other trailing annotations, if
3820   // they are not function-like.
3821   FormatStyle Style = getGoogleStyle();
3822   Style.ColumnLimit = 47;
3823   verifyFormat("void someLongFunction(\n"
3824                "    int someLoooooooooooooongParameter) const {\n}",
3825                getLLVMStyleWithColumns(47));
3826   verifyFormat("LoooooongReturnType\n"
3827                "someLoooooooongFunction() const {}",
3828                getLLVMStyleWithColumns(47));
3829   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
3830                "    const {}",
3831                Style);
3832   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3833                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
3834   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3835                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
3836   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3837                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
3838   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
3839                "                   aaaaaaaaaaa aaaaa) const override;");
3840   verifyGoogleFormat(
3841       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3842       "    const override;");
3843 
3844   // Even if the first parameter has to be wrapped.
3845   verifyFormat("void someLongFunction(\n"
3846                "    int someLongParameter) const {}",
3847                getLLVMStyleWithColumns(46));
3848   verifyFormat("void someLongFunction(\n"
3849                "    int someLongParameter) const {}",
3850                Style);
3851   verifyFormat("void someLongFunction(\n"
3852                "    int someLongParameter) override {}",
3853                Style);
3854   verifyFormat("void someLongFunction(\n"
3855                "    int someLongParameter) OVERRIDE {}",
3856                Style);
3857   verifyFormat("void someLongFunction(\n"
3858                "    int someLongParameter) final {}",
3859                Style);
3860   verifyFormat("void someLongFunction(\n"
3861                "    int someLongParameter) FINAL {}",
3862                Style);
3863   verifyFormat("void someLongFunction(\n"
3864                "    int parameter) const override {}",
3865                Style);
3866 
3867   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
3868   verifyFormat("void someLongFunction(\n"
3869                "    int someLongParameter) const\n"
3870                "{\n"
3871                "}",
3872                Style);
3873 
3874   // Unless these are unknown annotations.
3875   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
3876                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3877                "    LONG_AND_UGLY_ANNOTATION;");
3878 
3879   // Breaking before function-like trailing annotations is fine to keep them
3880   // close to their arguments.
3881   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3882                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3883   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3884                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3885   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3886                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
3887   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
3888                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
3889   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
3890 
3891   verifyFormat(
3892       "void aaaaaaaaaaaaaaaaaa()\n"
3893       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
3894       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
3895   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3896                "    __attribute__((unused));");
3897   verifyGoogleFormat(
3898       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3899       "    GUARDED_BY(aaaaaaaaaaaa);");
3900   verifyGoogleFormat(
3901       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3902       "    GUARDED_BY(aaaaaaaaaaaa);");
3903   verifyGoogleFormat(
3904       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3905       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3906   verifyGoogleFormat(
3907       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3908       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
3909 }
3910 
3911 TEST_F(FormatTest, FunctionAnnotations) {
3912   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3913                "int OldFunction(const string &parameter) {}");
3914   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3915                "string OldFunction(const string &parameter) {}");
3916   verifyFormat("template <typename T>\n"
3917                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3918                "string OldFunction(const string &parameter) {}");
3919 
3920   // Not function annotations.
3921   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3922                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
3923   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
3924                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
3925 }
3926 
3927 TEST_F(FormatTest, BreaksDesireably) {
3928   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3929                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3930                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
3931   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3932                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
3933                "}");
3934 
3935   verifyFormat(
3936       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3937       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3938 
3939   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3940                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3941                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3942 
3943   verifyFormat(
3944       "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3945       "                            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
3946       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3947       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
3948 
3949   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3950                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3951 
3952   verifyFormat(
3953       "void f() {\n"
3954       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
3955       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3956       "}");
3957   verifyFormat(
3958       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3959       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3960   verifyFormat(
3961       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3962       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3963   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3964                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3965                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3966 
3967   // Indent consistently independent of call expression and unary operator.
3968   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3969                "    dddddddddddddddddddddddddddddd));");
3970   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3971                "    dddddddddddddddddddddddddddddd));");
3972   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
3973                "    dddddddddddddddddddddddddddddd));");
3974 
3975   // This test case breaks on an incorrect memoization, i.e. an optimization not
3976   // taking into account the StopAt value.
3977   verifyFormat(
3978       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3979       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3980       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3981       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3982 
3983   verifyFormat("{\n  {\n    {\n"
3984                "      Annotation.SpaceRequiredBefore =\n"
3985                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
3986                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
3987                "    }\n  }\n}");
3988 
3989   // Break on an outer level if there was a break on an inner level.
3990   EXPECT_EQ("f(g(h(a, // comment\n"
3991             "      b, c),\n"
3992             "    d, e),\n"
3993             "  x, y);",
3994             format("f(g(h(a, // comment\n"
3995                    "    b, c), d, e), x, y);"));
3996 
3997   // Prefer breaking similar line breaks.
3998   verifyFormat(
3999       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
4000       "                             NSTrackingMouseEnteredAndExited |\n"
4001       "                             NSTrackingActiveAlways;");
4002 }
4003 
4004 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
4005   FormatStyle NoBinPacking = getGoogleStyle();
4006   NoBinPacking.BinPackParameters = false;
4007   NoBinPacking.BinPackArguments = true;
4008   verifyFormat("void f() {\n"
4009                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
4010                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4011                "}",
4012                NoBinPacking);
4013   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
4014                "       int aaaaaaaaaaaaaaaaaaaa,\n"
4015                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4016                NoBinPacking);
4017 }
4018 
4019 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
4020   FormatStyle NoBinPacking = getGoogleStyle();
4021   NoBinPacking.BinPackParameters = false;
4022   NoBinPacking.BinPackArguments = false;
4023   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
4024                "  aaaaaaaaaaaaaaaaaaaa,\n"
4025                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
4026                NoBinPacking);
4027   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
4028                "        aaaaaaaaaaaaa,\n"
4029                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
4030                NoBinPacking);
4031   verifyFormat(
4032       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4033       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4034       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4035       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4036       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
4037       NoBinPacking);
4038   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4039                "    .aaaaaaaaaaaaaaaaaa();",
4040                NoBinPacking);
4041   verifyFormat("void f() {\n"
4042                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4043                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
4044                "}",
4045                NoBinPacking);
4046 
4047   verifyFormat(
4048       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4049       "             aaaaaaaaaaaa,\n"
4050       "             aaaaaaaaaaaa);",
4051       NoBinPacking);
4052   verifyFormat(
4053       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
4054       "                               ddddddddddddddddddddddddddddd),\n"
4055       "             test);",
4056       NoBinPacking);
4057 
4058   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4059                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
4060                "            aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;",
4061                NoBinPacking);
4062   verifyFormat("a(\"a\"\n"
4063                "  \"a\",\n"
4064                "  a);");
4065 
4066   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4067   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
4068                "                aaaaaaaaa,\n"
4069                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4070                NoBinPacking);
4071   verifyFormat(
4072       "void f() {\n"
4073       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4074       "      .aaaaaaa();\n"
4075       "}",
4076       NoBinPacking);
4077   verifyFormat(
4078       "template <class SomeType, class SomeOtherType>\n"
4079       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
4080       NoBinPacking);
4081 }
4082 
4083 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
4084   FormatStyle Style = getLLVMStyleWithColumns(15);
4085   Style.ExperimentalAutoDetectBinPacking = true;
4086   EXPECT_EQ("aaa(aaaa,\n"
4087             "    aaaa,\n"
4088             "    aaaa);\n"
4089             "aaa(aaaa,\n"
4090             "    aaaa,\n"
4091             "    aaaa);",
4092             format("aaa(aaaa,\n" // one-per-line
4093                    "  aaaa,\n"
4094                    "    aaaa  );\n"
4095                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4096                    Style));
4097   EXPECT_EQ("aaa(aaaa, aaaa,\n"
4098             "    aaaa);\n"
4099             "aaa(aaaa, aaaa,\n"
4100             "    aaaa);",
4101             format("aaa(aaaa,  aaaa,\n" // bin-packed
4102                    "    aaaa  );\n"
4103                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4104                    Style));
4105 }
4106 
4107 TEST_F(FormatTest, FormatsBuilderPattern) {
4108   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
4109                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
4110                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
4111                "    .StartsWith(\".init\", ORDER_INIT)\n"
4112                "    .StartsWith(\".fini\", ORDER_FINI)\n"
4113                "    .StartsWith(\".hash\", ORDER_HASH)\n"
4114                "    .Default(ORDER_TEXT);\n");
4115 
4116   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
4117                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
4118   verifyFormat(
4119       "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n"
4120       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4121       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4122   verifyFormat(
4123       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
4124       "    aaaaaaaaaaaaaa);");
4125   verifyFormat(
4126       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
4127       "    aaaaaa->aaaaaaaaaaaa()\n"
4128       "        ->aaaaaaaaaaaaaaaa(\n"
4129       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4130       "        ->aaaaaaaaaaaaaaaaa();");
4131   verifyGoogleFormat(
4132       "void f() {\n"
4133       "  someo->Add((new util::filetools::Handler(dir))\n"
4134       "                 ->OnEvent1(NewPermanentCallback(\n"
4135       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
4136       "                 ->OnEvent2(NewPermanentCallback(\n"
4137       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
4138       "                 ->OnEvent3(NewPermanentCallback(\n"
4139       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
4140       "                 ->OnEvent5(NewPermanentCallback(\n"
4141       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
4142       "                 ->OnEvent6(NewPermanentCallback(\n"
4143       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
4144       "}");
4145 
4146   verifyFormat(
4147       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
4148   verifyFormat("aaaaaaaaaaaaaaa()\n"
4149                "    .aaaaaaaaaaaaaaa()\n"
4150                "    .aaaaaaaaaaaaaaa()\n"
4151                "    .aaaaaaaaaaaaaaa()\n"
4152                "    .aaaaaaaaaaaaaaa();");
4153   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4154                "    .aaaaaaaaaaaaaaa()\n"
4155                "    .aaaaaaaaaaaaaaa()\n"
4156                "    .aaaaaaaaaaaaaaa();");
4157   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4158                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4159                "    .aaaaaaaaaaaaaaa();");
4160   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
4161                "    ->aaaaaaaaaaaaaae(0)\n"
4162                "    ->aaaaaaaaaaaaaaa();");
4163 
4164   // Don't linewrap after very short segments.
4165   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4166                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4167                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4168   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4169                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4170                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4171   verifyFormat("aaa()\n"
4172                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4173                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4174                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4175 
4176   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4177                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4178                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
4179   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4180                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
4181                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
4182 
4183   // Prefer not to break after empty parentheses.
4184   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
4185                "    First->LastNewlineOffset);");
4186 }
4187 
4188 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
4189   verifyFormat(
4190       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4191       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
4192   verifyFormat(
4193       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
4194       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
4195 
4196   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4197                "    ccccccccccccccccccccccccc) {\n}");
4198   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
4199                "    ccccccccccccccccccccccccc) {\n}");
4200 
4201   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4202                "    ccccccccccccccccccccccccc) {\n}");
4203   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
4204                "    ccccccccccccccccccccccccc) {\n}");
4205 
4206   verifyFormat(
4207       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
4208       "    ccccccccccccccccccccccccc) {\n}");
4209   verifyFormat(
4210       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
4211       "    ccccccccccccccccccccccccc) {\n}");
4212 
4213   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
4214                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
4215                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
4216                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4217   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
4218                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
4219                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
4220                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4221 
4222   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
4223                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
4224                "    aaaaaaaaaaaaaaa != aa) {\n}");
4225   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
4226                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
4227                "    aaaaaaaaaaaaaaa != aa) {\n}");
4228 }
4229 
4230 TEST_F(FormatTest, BreaksAfterAssignments) {
4231   verifyFormat(
4232       "unsigned Cost =\n"
4233       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
4234       "                        SI->getPointerAddressSpaceee());\n");
4235   verifyFormat(
4236       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
4237       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
4238 
4239   verifyFormat(
4240       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
4241       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
4242   verifyFormat("unsigned OriginalStartColumn =\n"
4243                "    SourceMgr.getSpellingColumnNumber(\n"
4244                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
4245                "    1;");
4246 }
4247 
4248 TEST_F(FormatTest, AlignsAfterAssignments) {
4249   verifyFormat(
4250       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4251       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
4252   verifyFormat(
4253       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4254       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
4255   verifyFormat(
4256       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4257       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
4258   verifyFormat(
4259       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4260       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
4261   verifyFormat(
4262       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4263       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4264       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
4265 }
4266 
4267 TEST_F(FormatTest, AlignsAfterReturn) {
4268   verifyFormat(
4269       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4270       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
4271   verifyFormat(
4272       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4273       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
4274   verifyFormat(
4275       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4276       "       aaaaaaaaaaaaaaaaaaaaaa();");
4277   verifyFormat(
4278       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4279       "        aaaaaaaaaaaaaaaaaaaaaa());");
4280   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4281                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4282   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4283                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
4284                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4285   verifyFormat("return\n"
4286                "    // true if code is one of a or b.\n"
4287                "    code == a || code == b;");
4288 }
4289 
4290 TEST_F(FormatTest, AlignsAfterOpenBracket) {
4291   verifyFormat(
4292       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4293       "                                                aaaaaaaaa aaaaaaa) {}");
4294   verifyFormat(
4295       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4296       "                                               aaaaaaaaaaa aaaaaaaaa);");
4297   verifyFormat(
4298       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4299       "                                             aaaaaaaaaaaaaaaaaaaaa));");
4300   FormatStyle Style = getLLVMStyle();
4301   Style.AlignAfterOpenBracket = false;
4302   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4303                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
4304                Style);
4305   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4306                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
4307                Style);
4308   verifyFormat("SomeLongVariableName->someFunction(\n"
4309                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
4310                Style);
4311   verifyFormat(
4312       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4313       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4314       Style);
4315   verifyFormat(
4316       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4317       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4318       Style);
4319   verifyFormat(
4320       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4321       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4322       Style);
4323 }
4324 
4325 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
4326   FormatStyle Style = getLLVMStyleWithColumns(40);
4327   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4328                "          bbbbbbbbbbbbbbbbbbbbbb);",
4329                Style);
4330   Style.AlignAfterOpenBracket = true;
4331   Style.AlignOperands = false;
4332   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4333                "          bbbbbbbbbbbbbbbbbbbbbb);",
4334                Style);
4335   Style.AlignAfterOpenBracket = false;
4336   Style.AlignOperands = true;
4337   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4338                "          bbbbbbbbbbbbbbbbbbbbbb);",
4339                Style);
4340   Style.AlignAfterOpenBracket = false;
4341   Style.AlignOperands = false;
4342   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4343                "    bbbbbbbbbbbbbbbbbbbbbb);",
4344                Style);
4345 }
4346 
4347 TEST_F(FormatTest, BreaksConditionalExpressions) {
4348   verifyFormat(
4349       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4350       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4351       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4352   verifyFormat(
4353       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4354       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4355   verifyFormat(
4356       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
4357       "                                                    : aaaaaaaaaaaaa);");
4358   verifyFormat(
4359       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4360       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4361       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4362       "                   aaaaaaaaaaaaa);");
4363   verifyFormat(
4364       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4365       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4366       "                   aaaaaaaaaaaaa);");
4367   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4368                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4369                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4370                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4371                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4372   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4373                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4374                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4375                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4376                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4377                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4378                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4379   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4380                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4381                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4382                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4383                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4384   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4385                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4386                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4387   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4388                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4389                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4390                "        : aaaaaaaaaaaaaaaa;");
4391   verifyFormat(
4392       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4393       "    ? aaaaaaaaaaaaaaa\n"
4394       "    : aaaaaaaaaaaaaaa;");
4395   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4396                "          aaaaaaaaa\n"
4397                "      ? b\n"
4398                "      : c);");
4399   verifyFormat("return aaaa == bbbb\n"
4400                "           // comment\n"
4401                "           ? aaaa\n"
4402                "           : bbbb;");
4403   verifyFormat("unsigned Indent =\n"
4404                "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
4405                "                              ? IndentForLevel[TheLine.Level]\n"
4406                "                              : TheLine * 2,\n"
4407                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4408                getLLVMStyleWithColumns(70));
4409   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4410                "                  ? aaaaaaaaaaaaaaa\n"
4411                "                  : bbbbbbbbbbbbbbb //\n"
4412                "                        ? ccccccccccccccc\n"
4413                "                        : ddddddddddddddd;");
4414   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4415                "                  ? aaaaaaaaaaaaaaa\n"
4416                "                  : (bbbbbbbbbbbbbbb //\n"
4417                "                         ? ccccccccccccccc\n"
4418                "                         : ddddddddddddddd);");
4419   verifyFormat(
4420       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4421       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4422       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
4423       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
4424       "                                      : aaaaaaaaaa;");
4425   verifyFormat(
4426       "aaaaaa = aaaaaaaaaaaa\n"
4427       "             ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4428       "                          : aaaaaaaaaaaaaaaaaaaaaa\n"
4429       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4430 
4431   FormatStyle NoBinPacking = getLLVMStyle();
4432   NoBinPacking.BinPackArguments = false;
4433   verifyFormat(
4434       "void f() {\n"
4435       "  g(aaa,\n"
4436       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4437       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4438       "        ? aaaaaaaaaaaaaaa\n"
4439       "        : aaaaaaaaaaaaaaa);\n"
4440       "}",
4441       NoBinPacking);
4442   verifyFormat(
4443       "void f() {\n"
4444       "  g(aaa,\n"
4445       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4446       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4447       "        ?: aaaaaaaaaaaaaaa);\n"
4448       "}",
4449       NoBinPacking);
4450 
4451   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
4452                "             // comment.\n"
4453                "             ccccccccccccccccccccccccccccccccccccccc\n"
4454                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4455                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
4456 
4457   // Assignments in conditional expressions. Apparently not uncommon :-(.
4458   verifyFormat("return a != b\n"
4459                "           // comment\n"
4460                "           ? a = b\n"
4461                "           : a = b;");
4462   verifyFormat("return a != b\n"
4463                "           // comment\n"
4464                "           ? a = a != b\n"
4465                "                     // comment\n"
4466                "                     ? a = b\n"
4467                "                     : a\n"
4468                "           : a;\n");
4469   verifyFormat("return a != b\n"
4470                "           // comment\n"
4471                "           ? a\n"
4472                "           : a = a != b\n"
4473                "                     // comment\n"
4474                "                     ? a = b\n"
4475                "                     : a;");
4476 }
4477 
4478 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
4479   FormatStyle Style = getLLVMStyle();
4480   Style.BreakBeforeTernaryOperators = false;
4481   Style.ColumnLimit = 70;
4482   verifyFormat(
4483       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4484       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4485       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4486       Style);
4487   verifyFormat(
4488       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4489       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4490       Style);
4491   verifyFormat(
4492       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
4493       "                                                      aaaaaaaaaaaaa);",
4494       Style);
4495   verifyFormat(
4496       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4497       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4498       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4499       "                   aaaaaaaaaaaaa);",
4500       Style);
4501   verifyFormat(
4502       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4503       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4504       "                   aaaaaaaaaaaaa);",
4505       Style);
4506   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4507                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4508                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4509                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4510                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4511                Style);
4512   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4513                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4514                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4515                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4516                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4517                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4518                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4519                Style);
4520   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4521                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
4522                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4523                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4524                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4525                Style);
4526   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4527                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4528                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4529                Style);
4530   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4531                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4532                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4533                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4534                Style);
4535   verifyFormat(
4536       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4537       "    aaaaaaaaaaaaaaa :\n"
4538       "    aaaaaaaaaaaaaaa;",
4539       Style);
4540   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4541                "          aaaaaaaaa ?\n"
4542                "      b :\n"
4543                "      c);",
4544                Style);
4545   verifyFormat(
4546       "unsigned Indent =\n"
4547       "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
4548       "                              IndentForLevel[TheLine.Level] :\n"
4549       "                              TheLine * 2,\n"
4550       "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4551       Style);
4552   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4553                "                  aaaaaaaaaaaaaaa :\n"
4554                "                  bbbbbbbbbbbbbbb ? //\n"
4555                "                      ccccccccccccccc :\n"
4556                "                      ddddddddddddddd;",
4557                Style);
4558   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4559                "                  aaaaaaaaaaaaaaa :\n"
4560                "                  (bbbbbbbbbbbbbbb ? //\n"
4561                "                       ccccccccccccccc :\n"
4562                "                       ddddddddddddddd);",
4563                Style);
4564 }
4565 
4566 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
4567   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
4568                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
4569   verifyFormat("bool a = true, b = false;");
4570 
4571   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4572                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
4573                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
4574                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
4575   verifyFormat(
4576       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
4577       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
4578       "     d = e && f;");
4579   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
4580                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
4581   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4582                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
4583   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
4584                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
4585 
4586   FormatStyle Style = getGoogleStyle();
4587   Style.PointerAlignment = FormatStyle::PAS_Left;
4588   Style.DerivePointerAlignment = false;
4589   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4590                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
4591                "    *b = bbbbbbbbbbbbbbbbbbb;",
4592                Style);
4593   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4594                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
4595                Style);
4596 }
4597 
4598 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
4599   verifyFormat("arr[foo ? bar : baz];");
4600   verifyFormat("f()[foo ? bar : baz];");
4601   verifyFormat("(a + b)[foo ? bar : baz];");
4602   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
4603 }
4604 
4605 TEST_F(FormatTest, AlignsStringLiterals) {
4606   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
4607                "                                      \"short literal\");");
4608   verifyFormat(
4609       "looooooooooooooooooooooooongFunction(\n"
4610       "    \"short literal\"\n"
4611       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
4612   verifyFormat("someFunction(\"Always break between multi-line\"\n"
4613                "             \" string literals\",\n"
4614                "             and, other, parameters);");
4615   EXPECT_EQ("fun + \"1243\" /* comment */\n"
4616             "      \"5678\";",
4617             format("fun + \"1243\" /* comment */\n"
4618                    "      \"5678\";",
4619                    getLLVMStyleWithColumns(28)));
4620   EXPECT_EQ(
4621       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
4622       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
4623       "         \"aaaaaaaaaaaaaaaa\";",
4624       format("aaaaaa ="
4625              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
4626              "aaaaaaaaaaaaaaaaaaaaa\" "
4627              "\"aaaaaaaaaaaaaaaa\";"));
4628   verifyFormat("a = a + \"a\"\n"
4629                "        \"a\"\n"
4630                "        \"a\";");
4631   verifyFormat("f(\"a\", \"b\"\n"
4632                "       \"c\");");
4633 
4634   verifyFormat(
4635       "#define LL_FORMAT \"ll\"\n"
4636       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
4637       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
4638 
4639   verifyFormat("#define A(X)          \\\n"
4640                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
4641                "  \"ccccc\"",
4642                getLLVMStyleWithColumns(23));
4643   verifyFormat("#define A \"def\"\n"
4644                "f(\"abc\" A \"ghi\"\n"
4645                "  \"jkl\");");
4646 
4647   verifyFormat("f(L\"a\"\n"
4648                "  L\"b\");");
4649   verifyFormat("#define A(X)            \\\n"
4650                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
4651                "  L\"ccccc\"",
4652                getLLVMStyleWithColumns(25));
4653 
4654   verifyFormat("f(@\"a\"\n"
4655                "  @\"b\");");
4656   verifyFormat("NSString s = @\"a\"\n"
4657                "             @\"b\"\n"
4658                "             @\"c\";");
4659   verifyFormat("NSString s = @\"a\"\n"
4660                "              \"b\"\n"
4661                "              \"c\";");
4662 }
4663 
4664 TEST_F(FormatTest, DefinitionReturnTypeBreakingStyle) {
4665   FormatStyle Style = getLLVMStyle();
4666   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_TopLevel;
4667   verifyFormat("class C {\n"
4668                "  int f() { return 1; }\n"
4669                "};\n"
4670                "int\n"
4671                "f() {\n"
4672                "  return 1;\n"
4673                "}",
4674                Style);
4675   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
4676   verifyFormat("class C {\n"
4677                "  int\n"
4678                "  f() {\n"
4679                "    return 1;\n"
4680                "  }\n"
4681                "};\n"
4682                "int\n"
4683                "f() {\n"
4684                "  return 1;\n"
4685                "}",
4686                Style);
4687   verifyFormat("const char *\n"
4688                "f(void) {\n" // Break here.
4689                "  return \"\";\n"
4690                "}\n"
4691                "const char *bar(void);\n", // No break here.
4692                Style);
4693   verifyFormat("template <class T>\n"
4694                "T *\n"
4695                "f(T &c) {\n" // Break here.
4696                "  return NULL;\n"
4697                "}\n"
4698                "template <class T> T *f(T &c);\n", // No break here.
4699                Style);
4700   verifyFormat("class C {\n"
4701                "  int\n"
4702                "  operator+() {\n"
4703                "    return 1;\n"
4704                "  }\n"
4705                "  int\n"
4706                "  operator()() {\n"
4707                "    return 1;\n"
4708                "  }\n"
4709                "};\n",
4710                Style);
4711   verifyFormat("void\n"
4712                "A::operator()() {}\n"
4713                "void\n"
4714                "A::operator>>() {}\n"
4715                "void\n"
4716                "A::operator+() {}\n",
4717                Style);
4718   verifyFormat("void *operator new(std::size_t s);", // No break here.
4719                Style);
4720   verifyFormat("void *\n"
4721                "operator new(std::size_t s) {}",
4722                Style);
4723   verifyFormat("void *\n"
4724                "operator delete[](void *ptr) {}",
4725                Style);
4726   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4727   verifyFormat("const char *\n"
4728                "f(void)\n" // Break here.
4729                "{\n"
4730                "  return \"\";\n"
4731                "}\n"
4732                "const char *bar(void);\n", // No break here.
4733                Style);
4734   verifyFormat("template <class T>\n"
4735                "T *\n"     // Problem here: no line break
4736                "f(T &c)\n" // Break here.
4737                "{\n"
4738                "  return NULL;\n"
4739                "}\n"
4740                "template <class T> T *f(T &c);\n", // No break here.
4741                Style);
4742 }
4743 
4744 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
4745   FormatStyle NoBreak = getLLVMStyle();
4746   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
4747   FormatStyle Break = getLLVMStyle();
4748   Break.AlwaysBreakBeforeMultilineStrings = true;
4749   verifyFormat("aaaa = \"bbbb\"\n"
4750                "       \"cccc\";",
4751                NoBreak);
4752   verifyFormat("aaaa =\n"
4753                "    \"bbbb\"\n"
4754                "    \"cccc\";",
4755                Break);
4756   verifyFormat("aaaa(\"bbbb\"\n"
4757                "     \"cccc\");",
4758                NoBreak);
4759   verifyFormat("aaaa(\n"
4760                "    \"bbbb\"\n"
4761                "    \"cccc\");",
4762                Break);
4763   verifyFormat("aaaa(qqq, \"bbbb\"\n"
4764                "          \"cccc\");",
4765                NoBreak);
4766   verifyFormat("aaaa(qqq,\n"
4767                "     \"bbbb\"\n"
4768                "     \"cccc\");",
4769                Break);
4770   verifyFormat("aaaa(qqq,\n"
4771                "     L\"bbbb\"\n"
4772                "     L\"cccc\");",
4773                Break);
4774   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
4775                "                      \"bbbb\"));",
4776                Break);
4777   verifyFormat("string s = someFunction(\n"
4778                "    \"abc\"\n"
4779                "    \"abc\");",
4780                Break);
4781 
4782   // As we break before unary operators, breaking right after them is bad.
4783   verifyFormat("string foo = abc ? \"x\"\n"
4784                "                   \"blah blah blah blah blah blah\"\n"
4785                "                 : \"y\";",
4786                Break);
4787 
4788   // Don't break if there is no column gain.
4789   verifyFormat("f(\"aaaa\"\n"
4790                "  \"bbbb\");",
4791                Break);
4792 
4793   // Treat literals with escaped newlines like multi-line string literals.
4794   EXPECT_EQ("x = \"a\\\n"
4795             "b\\\n"
4796             "c\";",
4797             format("x = \"a\\\n"
4798                    "b\\\n"
4799                    "c\";",
4800                    NoBreak));
4801   EXPECT_EQ("xxxx =\n"
4802             "    \"a\\\n"
4803             "b\\\n"
4804             "c\";",
4805             format("xxxx = \"a\\\n"
4806                    "b\\\n"
4807                    "c\";",
4808                    Break));
4809 
4810   // Exempt ObjC strings for now.
4811   EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
4812             "                          @\"bbbb\";",
4813             format("NSString *const kString = @\"aaaa\"\n"
4814                    "@\"bbbb\";",
4815                    Break));
4816 
4817   Break.ColumnLimit = 0;
4818   verifyFormat("const char *hello = \"hello llvm\";", Break);
4819 }
4820 
4821 TEST_F(FormatTest, AlignsPipes) {
4822   verifyFormat(
4823       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4824       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4825       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4826   verifyFormat(
4827       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
4828       "                     << aaaaaaaaaaaaaaaaaaaa;");
4829   verifyFormat(
4830       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4831       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4832   verifyFormat(
4833       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
4834       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
4835       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
4836   verifyFormat(
4837       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4838       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4839       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4840   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4841                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4842                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4843                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4844   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
4845                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
4846   verifyFormat(
4847       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4848       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4849 
4850   verifyFormat("return out << \"somepacket = {\\n\"\n"
4851                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
4852                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
4853                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
4854                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
4855                "           << \"}\";");
4856 
4857   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4858                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4859                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
4860   verifyFormat(
4861       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
4862       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
4863       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
4864       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
4865       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
4866   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
4867                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4868   verifyFormat(
4869       "void f() {\n"
4870       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
4871       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4872       "}");
4873   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
4874                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
4875   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4876                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4877                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
4878                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
4879   verifyFormat("LOG_IF(aaa == //\n"
4880                "       bbb)\n"
4881                "    << a << b;");
4882 
4883   // Breaking before the first "<<" is generally not desirable.
4884   verifyFormat(
4885       "llvm::errs()\n"
4886       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4887       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4888       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4889       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4890       getLLVMStyleWithColumns(70));
4891   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4892                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4893                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4894                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4895                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4896                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4897                getLLVMStyleWithColumns(70));
4898 
4899   // But sometimes, breaking before the first "<<" is desirable.
4900   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4901                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
4902   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
4903                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4904                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4905   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
4906                "    << BEF << IsTemplate << Description << E->getType();");
4907 
4908   verifyFormat(
4909       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4910       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4911 
4912   // Incomplete string literal.
4913   EXPECT_EQ("llvm::errs() << \"\n"
4914             "             << a;",
4915             format("llvm::errs() << \"\n<<a;"));
4916 
4917   verifyFormat("void f() {\n"
4918                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
4919                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
4920                "}");
4921 
4922   // Handle 'endl'.
4923   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
4924                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4925   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4926 }
4927 
4928 TEST_F(FormatTest, UnderstandsEquals) {
4929   verifyFormat(
4930       "aaaaaaaaaaaaaaaaa =\n"
4931       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4932   verifyFormat(
4933       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4934       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4935   verifyFormat(
4936       "if (a) {\n"
4937       "  f();\n"
4938       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4939       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
4940       "}");
4941 
4942   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4943                "        100000000 + 10000000) {\n}");
4944 }
4945 
4946 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
4947   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4948                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
4949 
4950   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4951                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
4952 
4953   verifyFormat(
4954       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
4955       "                                                          Parameter2);");
4956 
4957   verifyFormat(
4958       "ShortObject->shortFunction(\n"
4959       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
4960       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
4961 
4962   verifyFormat("loooooooooooooongFunction(\n"
4963                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
4964 
4965   verifyFormat(
4966       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
4967       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
4968 
4969   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
4970                "    .WillRepeatedly(Return(SomeValue));");
4971   verifyFormat("void f() {\n"
4972                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
4973                "      .Times(2)\n"
4974                "      .WillRepeatedly(Return(SomeValue));\n"
4975                "}");
4976   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
4977                "    ccccccccccccccccccccccc);");
4978   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4979                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4980                "          .aaaaa(aaaaa),\n"
4981                "      aaaaaaaaaaaaaaaaaaaaa);");
4982   verifyFormat("void f() {\n"
4983                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4984                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
4985                "}");
4986   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4987                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4988                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4989                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4990                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4991   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4992                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4993                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4994                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
4995                "}");
4996 
4997   // Here, it is not necessary to wrap at "." or "->".
4998   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
4999                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5000   verifyFormat(
5001       "aaaaaaaaaaa->aaaaaaaaa(\n"
5002       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5003       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
5004 
5005   verifyFormat(
5006       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5007       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
5008   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
5009                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5010   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
5011                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5012 
5013   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5014                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5015                "    .a();");
5016 
5017   FormatStyle NoBinPacking = getLLVMStyle();
5018   NoBinPacking.BinPackParameters = false;
5019   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5020                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5021                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
5022                "                         aaaaaaaaaaaaaaaaaaa,\n"
5023                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5024                NoBinPacking);
5025 
5026   // If there is a subsequent call, change to hanging indentation.
5027   verifyFormat(
5028       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5029       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
5030       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5031   verifyFormat(
5032       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5033       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
5034   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5035                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5036                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5037   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5038                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5039                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5040 }
5041 
5042 TEST_F(FormatTest, WrapsTemplateDeclarations) {
5043   verifyFormat("template <typename T>\n"
5044                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5045   verifyFormat("template <typename T>\n"
5046                "// T should be one of {A, B}.\n"
5047                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5048   verifyFormat(
5049       "template <typename T>\n"
5050       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
5051   verifyFormat("template <typename T>\n"
5052                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
5053                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
5054   verifyFormat(
5055       "template <typename T>\n"
5056       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
5057       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
5058   verifyFormat(
5059       "template <typename T>\n"
5060       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
5061       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
5062       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5063   verifyFormat("template <typename T>\n"
5064                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5065                "    int aaaaaaaaaaaaaaaaaaaaaa);");
5066   verifyFormat(
5067       "template <typename T1, typename T2 = char, typename T3 = char,\n"
5068       "          typename T4 = char>\n"
5069       "void f();");
5070   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
5071                "          template <typename> class cccccccccccccccccccccc,\n"
5072                "          typename ddddddddddddd>\n"
5073                "class C {};");
5074   verifyFormat(
5075       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
5076       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5077 
5078   verifyFormat("void f() {\n"
5079                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
5080                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
5081                "}");
5082 
5083   verifyFormat("template <typename T> class C {};");
5084   verifyFormat("template <typename T> void f();");
5085   verifyFormat("template <typename T> void f() {}");
5086   verifyFormat(
5087       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5088       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5089       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
5090       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5091       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5092       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
5093       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
5094       getLLVMStyleWithColumns(72));
5095   EXPECT_EQ("static_cast<A< //\n"
5096             "    B> *>(\n"
5097             "\n"
5098             "    );",
5099             format("static_cast<A<//\n"
5100                    "    B>*>(\n"
5101                    "\n"
5102                    "    );"));
5103   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5104                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
5105 
5106   FormatStyle AlwaysBreak = getLLVMStyle();
5107   AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
5108   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
5109   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
5110   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
5111   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5112                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5113                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
5114   verifyFormat("template <template <typename> class Fooooooo,\n"
5115                "          template <typename> class Baaaaaaar>\n"
5116                "struct C {};",
5117                AlwaysBreak);
5118   verifyFormat("template <typename T> // T can be A, B or C.\n"
5119                "struct C {};",
5120                AlwaysBreak);
5121 }
5122 
5123 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
5124   verifyFormat(
5125       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5126       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5127   verifyFormat(
5128       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5129       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5130       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5131 
5132   // FIXME: Should we have the extra indent after the second break?
5133   verifyFormat(
5134       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5135       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5136       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5137 
5138   verifyFormat(
5139       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
5140       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
5141 
5142   // Breaking at nested name specifiers is generally not desirable.
5143   verifyFormat(
5144       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5145       "    aaaaaaaaaaaaaaaaaaaaaaa);");
5146 
5147   verifyFormat(
5148       "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5149       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5150       "                   aaaaaaaaaaaaaaaaaaaaa);",
5151       getLLVMStyleWithColumns(74));
5152 
5153   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5154                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5155                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5156 }
5157 
5158 TEST_F(FormatTest, UnderstandsTemplateParameters) {
5159   verifyFormat("A<int> a;");
5160   verifyFormat("A<A<A<int>>> a;");
5161   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
5162   verifyFormat("bool x = a < 1 || 2 > a;");
5163   verifyFormat("bool x = 5 < f<int>();");
5164   verifyFormat("bool x = f<int>() > 5;");
5165   verifyFormat("bool x = 5 < a<int>::x;");
5166   verifyFormat("bool x = a < 4 ? a > 2 : false;");
5167   verifyFormat("bool x = f() ? a < 2 : a > 2;");
5168 
5169   verifyGoogleFormat("A<A<int>> a;");
5170   verifyGoogleFormat("A<A<A<int>>> a;");
5171   verifyGoogleFormat("A<A<A<A<int>>>> a;");
5172   verifyGoogleFormat("A<A<int> > a;");
5173   verifyGoogleFormat("A<A<A<int> > > a;");
5174   verifyGoogleFormat("A<A<A<A<int> > > > a;");
5175   verifyGoogleFormat("A<::A<int>> a;");
5176   verifyGoogleFormat("A<::A> a;");
5177   verifyGoogleFormat("A< ::A> a;");
5178   verifyGoogleFormat("A< ::A<int> > a;");
5179   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
5180   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
5181   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
5182   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
5183   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
5184             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
5185 
5186   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
5187 
5188   verifyFormat("test >> a >> b;");
5189   verifyFormat("test << a >> b;");
5190 
5191   verifyFormat("f<int>();");
5192   verifyFormat("template <typename T> void f() {}");
5193   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
5194   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
5195                "sizeof(char)>::type>;");
5196   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
5197 
5198   // Not template parameters.
5199   verifyFormat("return a < b && c > d;");
5200   verifyFormat("void f() {\n"
5201                "  while (a < b && c > d) {\n"
5202                "  }\n"
5203                "}");
5204   verifyFormat("template <typename... Types>\n"
5205                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
5206 
5207   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5208                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
5209                getLLVMStyleWithColumns(60));
5210   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
5211   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
5212   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
5213 }
5214 
5215 TEST_F(FormatTest, UnderstandsBinaryOperators) {
5216   verifyFormat("COMPARE(a, ==, b);");
5217 }
5218 
5219 TEST_F(FormatTest, UnderstandsPointersToMembers) {
5220   verifyFormat("int A::*x;");
5221   verifyFormat("int (S::*func)(void *);");
5222   verifyFormat("void f() { int (S::*func)(void *); }");
5223   verifyFormat("typedef bool *(Class::*Member)() const;");
5224   verifyFormat("void f() {\n"
5225                "  (a->*f)();\n"
5226                "  a->*x;\n"
5227                "  (a.*f)();\n"
5228                "  ((*a).*f)();\n"
5229                "  a.*x;\n"
5230                "}");
5231   verifyFormat("void f() {\n"
5232                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5233                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
5234                "}");
5235   verifyFormat(
5236       "(aaaaaaaaaa->*bbbbbbb)(\n"
5237       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5238   FormatStyle Style = getLLVMStyle();
5239   Style.PointerAlignment = FormatStyle::PAS_Left;
5240   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
5241 }
5242 
5243 TEST_F(FormatTest, UnderstandsUnaryOperators) {
5244   verifyFormat("int a = -2;");
5245   verifyFormat("f(-1, -2, -3);");
5246   verifyFormat("a[-1] = 5;");
5247   verifyFormat("int a = 5 + -2;");
5248   verifyFormat("if (i == -1) {\n}");
5249   verifyFormat("if (i != -1) {\n}");
5250   verifyFormat("if (i > -1) {\n}");
5251   verifyFormat("if (i < -1) {\n}");
5252   verifyFormat("++(a->f());");
5253   verifyFormat("--(a->f());");
5254   verifyFormat("(a->f())++;");
5255   verifyFormat("a[42]++;");
5256   verifyFormat("if (!(a->f())) {\n}");
5257 
5258   verifyFormat("a-- > b;");
5259   verifyFormat("b ? -a : c;");
5260   verifyFormat("n * sizeof char16;");
5261   verifyFormat("n * alignof char16;", getGoogleStyle());
5262   verifyFormat("sizeof(char);");
5263   verifyFormat("alignof(char);", getGoogleStyle());
5264 
5265   verifyFormat("return -1;");
5266   verifyFormat("switch (a) {\n"
5267                "case -1:\n"
5268                "  break;\n"
5269                "}");
5270   verifyFormat("#define X -1");
5271   verifyFormat("#define X -kConstant");
5272 
5273   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
5274   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
5275 
5276   verifyFormat("int a = /* confusing comment */ -1;");
5277   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
5278   verifyFormat("int a = i /* confusing comment */++;");
5279 }
5280 
5281 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
5282   verifyFormat("if (!aaaaaaaaaa( // break\n"
5283                "        aaaaa)) {\n"
5284                "}");
5285   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
5286                "    aaaaa));");
5287   verifyFormat("*aaa = aaaaaaa( // break\n"
5288                "    bbbbbb);");
5289 }
5290 
5291 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
5292   verifyFormat("bool operator<();");
5293   verifyFormat("bool operator>();");
5294   verifyFormat("bool operator=();");
5295   verifyFormat("bool operator==();");
5296   verifyFormat("bool operator!=();");
5297   verifyFormat("int operator+();");
5298   verifyFormat("int operator++();");
5299   verifyFormat("bool operator();");
5300   verifyFormat("bool operator()();");
5301   verifyFormat("bool operator[]();");
5302   verifyFormat("operator bool();");
5303   verifyFormat("operator int();");
5304   verifyFormat("operator void *();");
5305   verifyFormat("operator SomeType<int>();");
5306   verifyFormat("operator SomeType<int, int>();");
5307   verifyFormat("operator SomeType<SomeType<int>>();");
5308   verifyFormat("void *operator new(std::size_t size);");
5309   verifyFormat("void *operator new[](std::size_t size);");
5310   verifyFormat("void operator delete(void *ptr);");
5311   verifyFormat("void operator delete[](void *ptr);");
5312   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
5313                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
5314 
5315   verifyFormat(
5316       "ostream &operator<<(ostream &OutputStream,\n"
5317       "                    SomeReallyLongType WithSomeReallyLongValue);");
5318   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
5319                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
5320                "  return left.group < right.group;\n"
5321                "}");
5322   verifyFormat("SomeType &operator=(const SomeType &S);");
5323   verifyFormat("f.template operator()<int>();");
5324 
5325   verifyGoogleFormat("operator void*();");
5326   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
5327   verifyGoogleFormat("operator ::A();");
5328 
5329   verifyFormat("using A::operator+;");
5330 }
5331 
5332 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
5333   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
5334   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
5335   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
5336   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
5337   verifyFormat("Deleted &operator=(const Deleted &) &;");
5338   verifyFormat("Deleted &operator=(const Deleted &) &&;");
5339   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
5340   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
5341   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
5342   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
5343   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
5344 
5345   FormatStyle AlignLeft = getLLVMStyle();
5346   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
5347   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
5348   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
5349                AlignLeft);
5350   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
5351   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
5352 
5353   FormatStyle Spaces = getLLVMStyle();
5354   Spaces.SpacesInCStyleCastParentheses = true;
5355   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
5356   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
5357   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
5358   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
5359 
5360   Spaces.SpacesInCStyleCastParentheses = false;
5361   Spaces.SpacesInParentheses = true;
5362   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
5363   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
5364   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
5365   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
5366 }
5367 
5368 TEST_F(FormatTest, UnderstandsNewAndDelete) {
5369   verifyFormat("void f() {\n"
5370                "  A *a = new A;\n"
5371                "  A *a = new (placement) A;\n"
5372                "  delete a;\n"
5373                "  delete (A *)a;\n"
5374                "}");
5375   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5376                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5377   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5378                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5379                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5380   verifyFormat("delete[] h->p;");
5381 }
5382 
5383 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
5384   verifyFormat("int *f(int *a) {}");
5385   verifyFormat("int main(int argc, char **argv) {}");
5386   verifyFormat("Test::Test(int b) : a(b * b) {}");
5387   verifyIndependentOfContext("f(a, *a);");
5388   verifyFormat("void g() { f(*a); }");
5389   verifyIndependentOfContext("int a = b * 10;");
5390   verifyIndependentOfContext("int a = 10 * b;");
5391   verifyIndependentOfContext("int a = b * c;");
5392   verifyIndependentOfContext("int a += b * c;");
5393   verifyIndependentOfContext("int a -= b * c;");
5394   verifyIndependentOfContext("int a *= b * c;");
5395   verifyIndependentOfContext("int a /= b * c;");
5396   verifyIndependentOfContext("int a = *b;");
5397   verifyIndependentOfContext("int a = *b * c;");
5398   verifyIndependentOfContext("int a = b * *c;");
5399   verifyIndependentOfContext("int a = b * (10);");
5400   verifyIndependentOfContext("S << b * (10);");
5401   verifyIndependentOfContext("return 10 * b;");
5402   verifyIndependentOfContext("return *b * *c;");
5403   verifyIndependentOfContext("return a & ~b;");
5404   verifyIndependentOfContext("f(b ? *c : *d);");
5405   verifyIndependentOfContext("int a = b ? *c : *d;");
5406   verifyIndependentOfContext("*b = a;");
5407   verifyIndependentOfContext("a * ~b;");
5408   verifyIndependentOfContext("a * !b;");
5409   verifyIndependentOfContext("a * +b;");
5410   verifyIndependentOfContext("a * -b;");
5411   verifyIndependentOfContext("a * ++b;");
5412   verifyIndependentOfContext("a * --b;");
5413   verifyIndependentOfContext("a[4] * b;");
5414   verifyIndependentOfContext("a[a * a] = 1;");
5415   verifyIndependentOfContext("f() * b;");
5416   verifyIndependentOfContext("a * [self dostuff];");
5417   verifyIndependentOfContext("int x = a * (a + b);");
5418   verifyIndependentOfContext("(a *)(a + b);");
5419   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
5420   verifyIndependentOfContext("int *pa = (int *)&a;");
5421   verifyIndependentOfContext("return sizeof(int **);");
5422   verifyIndependentOfContext("return sizeof(int ******);");
5423   verifyIndependentOfContext("return (int **&)a;");
5424   verifyIndependentOfContext("f((*PointerToArray)[10]);");
5425   verifyFormat("void f(Type (*parameter)[10]) {}");
5426   verifyGoogleFormat("return sizeof(int**);");
5427   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
5428   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
5429   verifyFormat("auto a = [](int **&, int ***) {};");
5430   verifyFormat("auto PointerBinding = [](const char *S) {};");
5431   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
5432   verifyFormat("[](const decltype(*a) &value) {}");
5433   verifyFormat("decltype(a * b) F();");
5434   verifyFormat("#define MACRO() [](A *a) { return 1; }");
5435   verifyIndependentOfContext("typedef void (*f)(int *a);");
5436   verifyIndependentOfContext("int i{a * b};");
5437   verifyIndependentOfContext("aaa && aaa->f();");
5438   verifyIndependentOfContext("int x = ~*p;");
5439   verifyFormat("Constructor() : a(a), area(width * height) {}");
5440   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
5441   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
5442   verifyFormat("void f() { f(a, c * d); }");
5443   verifyFormat("void f() { f(new a(), c * d); }");
5444 
5445   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
5446 
5447   verifyIndependentOfContext("A<int *> a;");
5448   verifyIndependentOfContext("A<int **> a;");
5449   verifyIndependentOfContext("A<int *, int *> a;");
5450   verifyIndependentOfContext("A<int *[]> a;");
5451   verifyIndependentOfContext(
5452       "const char *const p = reinterpret_cast<const char *const>(q);");
5453   verifyIndependentOfContext("A<int **, int **> a;");
5454   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
5455   verifyFormat("for (char **a = b; *a; ++a) {\n}");
5456   verifyFormat("for (; a && b;) {\n}");
5457   verifyFormat("bool foo = true && [] { return false; }();");
5458 
5459   verifyFormat(
5460       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5461       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5462 
5463   verifyGoogleFormat("**outparam = 1;");
5464   verifyGoogleFormat("*outparam = a * b;");
5465   verifyGoogleFormat("int main(int argc, char** argv) {}");
5466   verifyGoogleFormat("A<int*> a;");
5467   verifyGoogleFormat("A<int**> a;");
5468   verifyGoogleFormat("A<int*, int*> a;");
5469   verifyGoogleFormat("A<int**, int**> a;");
5470   verifyGoogleFormat("f(b ? *c : *d);");
5471   verifyGoogleFormat("int a = b ? *c : *d;");
5472   verifyGoogleFormat("Type* t = **x;");
5473   verifyGoogleFormat("Type* t = *++*x;");
5474   verifyGoogleFormat("*++*x;");
5475   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
5476   verifyGoogleFormat("Type* t = x++ * y;");
5477   verifyGoogleFormat(
5478       "const char* const p = reinterpret_cast<const char* const>(q);");
5479   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
5480   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
5481   verifyGoogleFormat("template <typename T>\n"
5482                      "void f(int i = 0, SomeType** temps = NULL);");
5483 
5484   FormatStyle Left = getLLVMStyle();
5485   Left.PointerAlignment = FormatStyle::PAS_Left;
5486   verifyFormat("x = *a(x) = *a(y);", Left);
5487   verifyFormat("for (;; * = b) {\n}", Left);
5488 
5489   verifyIndependentOfContext("a = *(x + y);");
5490   verifyIndependentOfContext("a = &(x + y);");
5491   verifyIndependentOfContext("*(x + y).call();");
5492   verifyIndependentOfContext("&(x + y)->call();");
5493   verifyFormat("void f() { &(*I).first; }");
5494 
5495   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
5496   verifyFormat(
5497       "int *MyValues = {\n"
5498       "    *A, // Operator detection might be confused by the '{'\n"
5499       "    *BB // Operator detection might be confused by previous comment\n"
5500       "};");
5501 
5502   verifyIndependentOfContext("if (int *a = &b)");
5503   verifyIndependentOfContext("if (int &a = *b)");
5504   verifyIndependentOfContext("if (a & b[i])");
5505   verifyIndependentOfContext("if (a::b::c::d & b[i])");
5506   verifyIndependentOfContext("if (*b[i])");
5507   verifyIndependentOfContext("if (int *a = (&b))");
5508   verifyIndependentOfContext("while (int *a = &b)");
5509   verifyIndependentOfContext("size = sizeof *a;");
5510   verifyIndependentOfContext("if (a && (b = c))");
5511   verifyFormat("void f() {\n"
5512                "  for (const int &v : Values) {\n"
5513                "  }\n"
5514                "}");
5515   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
5516   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
5517   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
5518 
5519   verifyFormat("#define A (!a * b)");
5520   verifyFormat("#define MACRO     \\\n"
5521                "  int *i = a * b; \\\n"
5522                "  void f(a *b);",
5523                getLLVMStyleWithColumns(19));
5524 
5525   verifyIndependentOfContext("A = new SomeType *[Length];");
5526   verifyIndependentOfContext("A = new SomeType *[Length]();");
5527   verifyIndependentOfContext("T **t = new T *;");
5528   verifyIndependentOfContext("T **t = new T *();");
5529   verifyGoogleFormat("A = new SomeType*[Length]();");
5530   verifyGoogleFormat("A = new SomeType*[Length];");
5531   verifyGoogleFormat("T** t = new T*;");
5532   verifyGoogleFormat("T** t = new T*();");
5533 
5534   FormatStyle PointerLeft = getLLVMStyle();
5535   PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
5536   verifyFormat("delete *x;", PointerLeft);
5537   verifyFormat("STATIC_ASSERT((a & b) == 0);");
5538   verifyFormat("STATIC_ASSERT(0 == (a & b));");
5539   verifyFormat("template <bool a, bool b> "
5540                "typename t::if<x && y>::type f() {}");
5541   verifyFormat("template <int *y> f() {}");
5542   verifyFormat("vector<int *> v;");
5543   verifyFormat("vector<int *const> v;");
5544   verifyFormat("vector<int *const **const *> v;");
5545   verifyFormat("vector<int *volatile> v;");
5546   verifyFormat("vector<a * b> v;");
5547   verifyFormat("foo<b && false>();");
5548   verifyFormat("foo<b & 1>();");
5549   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
5550   verifyFormat(
5551       "template <class T, class = typename std::enable_if<\n"
5552       "                       std::is_integral<T>::value &&\n"
5553       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
5554       "void F();",
5555       getLLVMStyleWithColumns(76));
5556   verifyFormat(
5557       "template <class T,\n"
5558       "          class = typename ::std::enable_if<\n"
5559       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
5560       "void F();",
5561       getGoogleStyleWithColumns(68));
5562 
5563   verifyIndependentOfContext("MACRO(int *i);");
5564   verifyIndependentOfContext("MACRO(auto *a);");
5565   verifyIndependentOfContext("MACRO(const A *a);");
5566   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
5567   // FIXME: Is there a way to make this work?
5568   // verifyIndependentOfContext("MACRO(A *a);");
5569 
5570   verifyFormat("DatumHandle const *operator->() const { return input_; }");
5571 
5572   EXPECT_EQ("#define OP(x)                                    \\\n"
5573             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
5574             "    return s << a.DebugString();                 \\\n"
5575             "  }",
5576             format("#define OP(x) \\\n"
5577                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
5578                    "    return s << a.DebugString(); \\\n"
5579                    "  }",
5580                    getLLVMStyleWithColumns(50)));
5581 
5582   // FIXME: We cannot handle this case yet; we might be able to figure out that
5583   // foo<x> d > v; doesn't make sense.
5584   verifyFormat("foo<a<b && c> d> v;");
5585 
5586   FormatStyle PointerMiddle = getLLVMStyle();
5587   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
5588   verifyFormat("delete *x;", PointerMiddle);
5589   verifyFormat("int * x;", PointerMiddle);
5590   verifyFormat("template <int * y> f() {}", PointerMiddle);
5591   verifyFormat("int * f(int * a) {}", PointerMiddle);
5592   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
5593   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
5594   verifyFormat("A<int *> a;", PointerMiddle);
5595   verifyFormat("A<int **> a;", PointerMiddle);
5596   verifyFormat("A<int *, int *> a;", PointerMiddle);
5597   verifyFormat("A<int * []> a;", PointerMiddle);
5598   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
5599   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
5600   verifyFormat("T ** t = new T *;", PointerMiddle);
5601 
5602   // Member function reference qualifiers aren't binary operators.
5603   verifyFormat("string // break\n"
5604                "operator()() & {}");
5605   verifyFormat("string // break\n"
5606                "operator()() && {}");
5607   verifyGoogleFormat("template <typename T>\n"
5608                      "auto x() & -> int {}");
5609 }
5610 
5611 TEST_F(FormatTest, UnderstandsAttributes) {
5612   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
5613   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
5614                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
5615   FormatStyle AfterType = getLLVMStyle();
5616   AfterType.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
5617   verifyFormat("__attribute__((nodebug)) void\n"
5618                "foo() {}\n",
5619                AfterType);
5620 }
5621 
5622 TEST_F(FormatTest, UnderstandsEllipsis) {
5623   verifyFormat("int printf(const char *fmt, ...);");
5624   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
5625   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
5626 
5627   FormatStyle PointersLeft = getLLVMStyle();
5628   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
5629   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
5630 }
5631 
5632 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
5633   EXPECT_EQ("int *a;\n"
5634             "int *a;\n"
5635             "int *a;",
5636             format("int *a;\n"
5637                    "int* a;\n"
5638                    "int *a;",
5639                    getGoogleStyle()));
5640   EXPECT_EQ("int* a;\n"
5641             "int* a;\n"
5642             "int* a;",
5643             format("int* a;\n"
5644                    "int* a;\n"
5645                    "int *a;",
5646                    getGoogleStyle()));
5647   EXPECT_EQ("int *a;\n"
5648             "int *a;\n"
5649             "int *a;",
5650             format("int *a;\n"
5651                    "int * a;\n"
5652                    "int *  a;",
5653                    getGoogleStyle()));
5654   EXPECT_EQ("auto x = [] {\n"
5655             "  int *a;\n"
5656             "  int *a;\n"
5657             "  int *a;\n"
5658             "};",
5659             format("auto x=[]{int *a;\n"
5660                    "int * a;\n"
5661                    "int *  a;};",
5662                    getGoogleStyle()));
5663 }
5664 
5665 TEST_F(FormatTest, UnderstandsRvalueReferences) {
5666   verifyFormat("int f(int &&a) {}");
5667   verifyFormat("int f(int a, char &&b) {}");
5668   verifyFormat("void f() { int &&a = b; }");
5669   verifyGoogleFormat("int f(int a, char&& b) {}");
5670   verifyGoogleFormat("void f() { int&& a = b; }");
5671 
5672   verifyIndependentOfContext("A<int &&> a;");
5673   verifyIndependentOfContext("A<int &&, int &&> a;");
5674   verifyGoogleFormat("A<int&&> a;");
5675   verifyGoogleFormat("A<int&&, int&&> a;");
5676 
5677   // Not rvalue references:
5678   verifyFormat("template <bool B, bool C> class A {\n"
5679                "  static_assert(B && C, \"Something is wrong\");\n"
5680                "};");
5681   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
5682   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
5683   verifyFormat("#define A(a, b) (a && b)");
5684 }
5685 
5686 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
5687   verifyFormat("void f() {\n"
5688                "  x[aaaaaaaaa -\n"
5689                "    b] = 23;\n"
5690                "}",
5691                getLLVMStyleWithColumns(15));
5692 }
5693 
5694 TEST_F(FormatTest, FormatsCasts) {
5695   verifyFormat("Type *A = static_cast<Type *>(P);");
5696   verifyFormat("Type *A = (Type *)P;");
5697   verifyFormat("Type *A = (vector<Type *, int *>)P;");
5698   verifyFormat("int a = (int)(2.0f);");
5699   verifyFormat("int a = (int)2.0f;");
5700   verifyFormat("x[(int32)y];");
5701   verifyFormat("x = (int32)y;");
5702   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
5703   verifyFormat("int a = (int)*b;");
5704   verifyFormat("int a = (int)2.0f;");
5705   verifyFormat("int a = (int)~0;");
5706   verifyFormat("int a = (int)++a;");
5707   verifyFormat("int a = (int)sizeof(int);");
5708   verifyFormat("int a = (int)+2;");
5709   verifyFormat("my_int a = (my_int)2.0f;");
5710   verifyFormat("my_int a = (my_int)sizeof(int);");
5711   verifyFormat("return (my_int)aaa;");
5712   verifyFormat("#define x ((int)-1)");
5713   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
5714   verifyFormat("#define p(q) ((int *)&q)");
5715   verifyFormat("fn(a)(b) + 1;");
5716 
5717   verifyFormat("void f() { my_int a = (my_int)*b; }");
5718   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
5719   verifyFormat("my_int a = (my_int)~0;");
5720   verifyFormat("my_int a = (my_int)++a;");
5721   verifyFormat("my_int a = (my_int)-2;");
5722   verifyFormat("my_int a = (my_int)1;");
5723   verifyFormat("my_int a = (my_int *)1;");
5724   verifyFormat("my_int a = (const my_int)-1;");
5725   verifyFormat("my_int a = (const my_int *)-1;");
5726   verifyFormat("my_int a = (my_int)(my_int)-1;");
5727   verifyFormat("my_int a = (ns::my_int)-2;");
5728   verifyFormat("case (my_int)ONE:");
5729 
5730   // FIXME: single value wrapped with paren will be treated as cast.
5731   verifyFormat("void f(int i = (kValue)*kMask) {}");
5732 
5733   verifyFormat("{ (void)F; }");
5734 
5735   // Don't break after a cast's
5736   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5737                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
5738                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
5739 
5740   // These are not casts.
5741   verifyFormat("void f(int *) {}");
5742   verifyFormat("f(foo)->b;");
5743   verifyFormat("f(foo).b;");
5744   verifyFormat("f(foo)(b);");
5745   verifyFormat("f(foo)[b];");
5746   verifyFormat("[](foo) { return 4; }(bar);");
5747   verifyFormat("(*funptr)(foo)[4];");
5748   verifyFormat("funptrs[4](foo)[4];");
5749   verifyFormat("void f(int *);");
5750   verifyFormat("void f(int *) = 0;");
5751   verifyFormat("void f(SmallVector<int>) {}");
5752   verifyFormat("void f(SmallVector<int>);");
5753   verifyFormat("void f(SmallVector<int>) = 0;");
5754   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
5755   verifyFormat("int a = sizeof(int) * b;");
5756   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
5757   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
5758   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
5759   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
5760 
5761   // These are not casts, but at some point were confused with casts.
5762   verifyFormat("virtual void foo(int *) override;");
5763   verifyFormat("virtual void foo(char &) const;");
5764   verifyFormat("virtual void foo(int *a, char *) const;");
5765   verifyFormat("int a = sizeof(int *) + b;");
5766   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
5767 
5768   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
5769                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5770   // FIXME: The indentation here is not ideal.
5771   verifyFormat(
5772       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5773       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
5774       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
5775 }
5776 
5777 TEST_F(FormatTest, FormatsFunctionTypes) {
5778   verifyFormat("A<bool()> a;");
5779   verifyFormat("A<SomeType()> a;");
5780   verifyFormat("A<void (*)(int, std::string)> a;");
5781   verifyFormat("A<void *(int)>;");
5782   verifyFormat("void *(*a)(int *, SomeType *);");
5783   verifyFormat("int (*func)(void *);");
5784   verifyFormat("void f() { int (*func)(void *); }");
5785   verifyFormat("template <class CallbackClass>\n"
5786                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
5787 
5788   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
5789   verifyGoogleFormat("void* (*a)(int);");
5790   verifyGoogleFormat(
5791       "template <class CallbackClass>\n"
5792       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
5793 
5794   // Other constructs can look somewhat like function types:
5795   verifyFormat("A<sizeof(*x)> a;");
5796   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
5797   verifyFormat("some_var = function(*some_pointer_var)[0];");
5798   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
5799 }
5800 
5801 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
5802   verifyFormat("A (*foo_)[6];");
5803   verifyFormat("vector<int> (*foo_)[6];");
5804 }
5805 
5806 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
5807   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5808                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5809   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
5810                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5811   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5812                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
5813 
5814   // Different ways of ()-initializiation.
5815   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5816                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
5817   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5818                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
5819   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5820                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
5821   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5822                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
5823 }
5824 
5825 TEST_F(FormatTest, BreaksLongDeclarations) {
5826   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
5827                "    AnotherNameForTheLongType;");
5828   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
5829                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5830   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5831                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5832   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
5833                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5834   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5835                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5836   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
5837                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5838   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5839                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5840   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5841                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5842   FormatStyle Indented = getLLVMStyle();
5843   Indented.IndentWrappedFunctionNames = true;
5844   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5845                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
5846                Indented);
5847   verifyFormat(
5848       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5849       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5850       Indented);
5851   verifyFormat(
5852       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5853       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5854       Indented);
5855   verifyFormat(
5856       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5857       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5858       Indented);
5859 
5860   // FIXME: Without the comment, this breaks after "(".
5861   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
5862                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
5863                getGoogleStyle());
5864 
5865   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
5866                "                  int LoooooooooooooooooooongParam2) {}");
5867   verifyFormat(
5868       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
5869       "                                   SourceLocation L, IdentifierIn *II,\n"
5870       "                                   Type *T) {}");
5871   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
5872                "ReallyReaaallyLongFunctionName(\n"
5873                "    const std::string &SomeParameter,\n"
5874                "    const SomeType<string, SomeOtherTemplateParameter>\n"
5875                "        &ReallyReallyLongParameterName,\n"
5876                "    const SomeType<string, SomeOtherTemplateParameter>\n"
5877                "        &AnotherLongParameterName) {}");
5878   verifyFormat("template <typename A>\n"
5879                "SomeLoooooooooooooooooooooongType<\n"
5880                "    typename some_namespace::SomeOtherType<A>::Type>\n"
5881                "Function() {}");
5882 
5883   verifyGoogleFormat(
5884       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
5885       "    aaaaaaaaaaaaaaaaaaaaaaa;");
5886   verifyGoogleFormat(
5887       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
5888       "                                   SourceLocation L) {}");
5889   verifyGoogleFormat(
5890       "some_namespace::LongReturnType\n"
5891       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
5892       "    int first_long_parameter, int second_parameter) {}");
5893 
5894   verifyGoogleFormat("template <typename T>\n"
5895                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5896                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
5897   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5898                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
5899 
5900   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5901                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5902                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5903   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5904                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5905                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
5906   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5907                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5908                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
5909                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5910 }
5911 
5912 TEST_F(FormatTest, FormatsArrays) {
5913   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5914                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
5915   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5916                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5917   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5918                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
5919   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5920                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5921                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5922   verifyFormat(
5923       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
5924       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5925       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
5926 
5927   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
5928                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
5929   verifyFormat(
5930       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
5931       "                                  .aaaaaaa[0]\n"
5932       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
5933 
5934   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
5935 }
5936 
5937 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
5938   verifyFormat("(a)->b();");
5939   verifyFormat("--a;");
5940 }
5941 
5942 TEST_F(FormatTest, HandlesIncludeDirectives) {
5943   verifyFormat("#include <string>\n"
5944                "#include <a/b/c.h>\n"
5945                "#include \"a/b/string\"\n"
5946                "#include \"string.h\"\n"
5947                "#include \"string.h\"\n"
5948                "#include <a-a>\n"
5949                "#include < path with space >\n"
5950                "#include_next <test.h>"
5951                "#include \"abc.h\" // this is included for ABC\n"
5952                "#include \"some long include\" // with a comment\n"
5953                "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
5954                getLLVMStyleWithColumns(35));
5955   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
5956   EXPECT_EQ("#include <a>", format("#include<a>"));
5957 
5958   verifyFormat("#import <string>");
5959   verifyFormat("#import <a/b/c.h>");
5960   verifyFormat("#import \"a/b/string\"");
5961   verifyFormat("#import \"string.h\"");
5962   verifyFormat("#import \"string.h\"");
5963   verifyFormat("#if __has_include(<strstream>)\n"
5964                "#include <strstream>\n"
5965                "#endif");
5966 
5967   verifyFormat("#define MY_IMPORT <a/b>");
5968 
5969   // Protocol buffer definition or missing "#".
5970   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
5971                getLLVMStyleWithColumns(30));
5972 
5973   FormatStyle Style = getLLVMStyle();
5974   Style.AlwaysBreakBeforeMultilineStrings = true;
5975   Style.ColumnLimit = 0;
5976   verifyFormat("#import \"abc.h\"", Style);
5977 
5978   // But 'import' might also be a regular C++ namespace.
5979   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5980                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5981 }
5982 
5983 //===----------------------------------------------------------------------===//
5984 // Error recovery tests.
5985 //===----------------------------------------------------------------------===//
5986 
5987 TEST_F(FormatTest, IncompleteParameterLists) {
5988   FormatStyle NoBinPacking = getLLVMStyle();
5989   NoBinPacking.BinPackParameters = false;
5990   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
5991                "                        double *min_x,\n"
5992                "                        double *max_x,\n"
5993                "                        double *min_y,\n"
5994                "                        double *max_y,\n"
5995                "                        double *min_z,\n"
5996                "                        double *max_z, ) {}",
5997                NoBinPacking);
5998 }
5999 
6000 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
6001   verifyFormat("void f() { return; }\n42");
6002   verifyFormat("void f() {\n"
6003                "  if (0)\n"
6004                "    return;\n"
6005                "}\n"
6006                "42");
6007   verifyFormat("void f() { return }\n42");
6008   verifyFormat("void f() {\n"
6009                "  if (0)\n"
6010                "    return\n"
6011                "}\n"
6012                "42");
6013 }
6014 
6015 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
6016   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
6017   EXPECT_EQ("void f() {\n"
6018             "  if (a)\n"
6019             "    return\n"
6020             "}",
6021             format("void  f  (  )  {  if  ( a )  return  }"));
6022   EXPECT_EQ("namespace N {\n"
6023             "void f()\n"
6024             "}",
6025             format("namespace  N  {  void f()  }"));
6026   EXPECT_EQ("namespace N {\n"
6027             "void f() {}\n"
6028             "void g()\n"
6029             "}",
6030             format("namespace N  { void f( ) { } void g( ) }"));
6031 }
6032 
6033 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
6034   verifyFormat("int aaaaaaaa =\n"
6035                "    // Overlylongcomment\n"
6036                "    b;",
6037                getLLVMStyleWithColumns(20));
6038   verifyFormat("function(\n"
6039                "    ShortArgument,\n"
6040                "    LoooooooooooongArgument);\n",
6041                getLLVMStyleWithColumns(20));
6042 }
6043 
6044 TEST_F(FormatTest, IncorrectAccessSpecifier) {
6045   verifyFormat("public:");
6046   verifyFormat("class A {\n"
6047                "public\n"
6048                "  void f() {}\n"
6049                "};");
6050   verifyFormat("public\n"
6051                "int qwerty;");
6052   verifyFormat("public\n"
6053                "B {}");
6054   verifyFormat("public\n"
6055                "{}");
6056   verifyFormat("public\n"
6057                "B { int x; }");
6058 }
6059 
6060 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
6061   verifyFormat("{");
6062   verifyFormat("#})");
6063   verifyNoCrash("(/**/[:!] ?[).");
6064 }
6065 
6066 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
6067   verifyFormat("do {\n}");
6068   verifyFormat("do {\n}\n"
6069                "f();");
6070   verifyFormat("do {\n}\n"
6071                "wheeee(fun);");
6072   verifyFormat("do {\n"
6073                "  f();\n"
6074                "}");
6075 }
6076 
6077 TEST_F(FormatTest, IncorrectCodeMissingParens) {
6078   verifyFormat("if {\n  foo;\n  foo();\n}");
6079   verifyFormat("switch {\n  foo;\n  foo();\n}");
6080   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
6081   verifyFormat("while {\n  foo;\n  foo();\n}");
6082   verifyFormat("do {\n  foo;\n  foo();\n} while;");
6083 }
6084 
6085 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
6086   verifyIncompleteFormat("namespace {\n"
6087                          "class Foo { Foo (\n"
6088                          "};\n"
6089                          "} // comment");
6090 }
6091 
6092 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
6093   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
6094   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
6095   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
6096   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
6097 
6098   EXPECT_EQ("{\n"
6099             "  {\n"
6100             "    breakme(\n"
6101             "        qwe);\n"
6102             "  }\n",
6103             format("{\n"
6104                    "    {\n"
6105                    " breakme(qwe);\n"
6106                    "}\n",
6107                    getLLVMStyleWithColumns(10)));
6108 }
6109 
6110 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
6111   verifyFormat("int x = {\n"
6112                "    avariable,\n"
6113                "    b(alongervariable)};",
6114                getLLVMStyleWithColumns(25));
6115 }
6116 
6117 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
6118   verifyFormat("return (a)(b){1, 2, 3};");
6119 }
6120 
6121 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
6122   verifyFormat("vector<int> x{1, 2, 3, 4};");
6123   verifyFormat("vector<int> x{\n"
6124                "    1, 2, 3, 4,\n"
6125                "};");
6126   verifyFormat("vector<T> x{{}, {}, {}, {}};");
6127   verifyFormat("f({1, 2});");
6128   verifyFormat("auto v = Foo{-1};");
6129   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
6130   verifyFormat("Class::Class : member{1, 2, 3} {}");
6131   verifyFormat("new vector<int>{1, 2, 3};");
6132   verifyFormat("new int[3]{1, 2, 3};");
6133   verifyFormat("new int{1};");
6134   verifyFormat("return {arg1, arg2};");
6135   verifyFormat("return {arg1, SomeType{parameter}};");
6136   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
6137   verifyFormat("new T{arg1, arg2};");
6138   verifyFormat("f(MyMap[{composite, key}]);");
6139   verifyFormat("class Class {\n"
6140                "  T member = {arg1, arg2};\n"
6141                "};");
6142   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
6143   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
6144   verifyFormat("int a = std::is_integral<int>{} + 0;");
6145 
6146   verifyFormat("int foo(int i) { return fo1{}(i); }");
6147   verifyFormat("int foo(int i) { return fo1{}(i); }");
6148   verifyFormat("auto i = decltype(x){};");
6149   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
6150   verifyFormat("Node n{1, Node{1000}, //\n"
6151                "       2};");
6152   verifyFormat("Aaaa aaaaaaa{\n"
6153                "    {\n"
6154                "        aaaa,\n"
6155                "    },\n"
6156                "};");
6157   verifyFormat("class C : public D {\n"
6158                "  SomeClass SC{2};\n"
6159                "};");
6160   verifyFormat("class C : public A {\n"
6161                "  class D : public B {\n"
6162                "    void f() { int i{2}; }\n"
6163                "  };\n"
6164                "};");
6165   verifyFormat("#define A {a, a},");
6166 
6167   // In combination with BinPackArguments = false.
6168   FormatStyle NoBinPacking = getLLVMStyle();
6169   NoBinPacking.BinPackArguments = false;
6170   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
6171                "                      bbbbb,\n"
6172                "                      ccccc,\n"
6173                "                      ddddd,\n"
6174                "                      eeeee,\n"
6175                "                      ffffff,\n"
6176                "                      ggggg,\n"
6177                "                      hhhhhh,\n"
6178                "                      iiiiii,\n"
6179                "                      jjjjjj,\n"
6180                "                      kkkkkk};",
6181                NoBinPacking);
6182   verifyFormat("const Aaaaaa aaaaa = {\n"
6183                "    aaaaa,\n"
6184                "    bbbbb,\n"
6185                "    ccccc,\n"
6186                "    ddddd,\n"
6187                "    eeeee,\n"
6188                "    ffffff,\n"
6189                "    ggggg,\n"
6190                "    hhhhhh,\n"
6191                "    iiiiii,\n"
6192                "    jjjjjj,\n"
6193                "    kkkkkk,\n"
6194                "};",
6195                NoBinPacking);
6196   verifyFormat(
6197       "const Aaaaaa aaaaa = {\n"
6198       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
6199       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
6200       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
6201       "};",
6202       NoBinPacking);
6203 
6204   // FIXME: The alignment of these trailing comments might be bad. Then again,
6205   // this might be utterly useless in real code.
6206   verifyFormat("Constructor::Constructor()\n"
6207                "    : some_value{         //\n"
6208                "                 aaaaaaa, //\n"
6209                "                 bbbbbbb} {}");
6210 
6211   // In braced lists, the first comment is always assumed to belong to the
6212   // first element. Thus, it can be moved to the next or previous line as
6213   // appropriate.
6214   EXPECT_EQ("function({// First element:\n"
6215             "          1,\n"
6216             "          // Second element:\n"
6217             "          2});",
6218             format("function({\n"
6219                    "    // First element:\n"
6220                    "    1,\n"
6221                    "    // Second element:\n"
6222                    "    2});"));
6223   EXPECT_EQ("std::vector<int> MyNumbers{\n"
6224             "    // First element:\n"
6225             "    1,\n"
6226             "    // Second element:\n"
6227             "    2};",
6228             format("std::vector<int> MyNumbers{// First element:\n"
6229                    "                           1,\n"
6230                    "                           // Second element:\n"
6231                    "                           2};",
6232                    getLLVMStyleWithColumns(30)));
6233   // A trailing comma should still lead to an enforced line break.
6234   EXPECT_EQ("vector<int> SomeVector = {\n"
6235             "    // aaa\n"
6236             "    1, 2,\n"
6237             "};",
6238             format("vector<int> SomeVector = { // aaa\n"
6239                    "    1, 2, };"));
6240 
6241   FormatStyle ExtraSpaces = getLLVMStyle();
6242   ExtraSpaces.Cpp11BracedListStyle = false;
6243   ExtraSpaces.ColumnLimit = 75;
6244   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
6245   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
6246   verifyFormat("f({ 1, 2 });", ExtraSpaces);
6247   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
6248   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
6249   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
6250   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
6251   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
6252   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
6253   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
6254   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
6255   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
6256   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
6257   verifyFormat("class Class {\n"
6258                "  T member = { arg1, arg2 };\n"
6259                "};",
6260                ExtraSpaces);
6261   verifyFormat(
6262       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6263       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
6264       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6265       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
6266       ExtraSpaces);
6267   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
6268   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
6269                ExtraSpaces);
6270   verifyFormat(
6271       "someFunction(OtherParam,\n"
6272       "             BracedList{ // comment 1 (Forcing interesting break)\n"
6273       "                         param1, param2,\n"
6274       "                         // comment 2\n"
6275       "                         param3, param4 });",
6276       ExtraSpaces);
6277   verifyFormat(
6278       "std::this_thread::sleep_for(\n"
6279       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
6280       ExtraSpaces);
6281   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
6282                "    aaaaaaa,\n"
6283                "    aaaaaaaaaa,\n"
6284                "    aaaaa,\n"
6285                "    aaaaaaaaaaaaaaa,\n"
6286                "    aaa,\n"
6287                "    aaaaaaaaaa,\n"
6288                "    a,\n"
6289                "    aaaaaaaaaaaaaaaaaaaaa,\n"
6290                "    aaaaaaaaaaaa,\n"
6291                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
6292                "    aaaaaaa,\n"
6293                "    a};");
6294   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
6295 }
6296 
6297 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
6298   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6299                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6300                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6301                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6302                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6303                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6304   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
6305                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6306                "                 1, 22, 333, 4444, 55555, //\n"
6307                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6308                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6309   verifyFormat(
6310       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6311       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6312       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
6313       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6314       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6315       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6316       "                 7777777};");
6317   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6318                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6319                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6320   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6321                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6322                "    // Separating comment.\n"
6323                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
6324   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6325                "    // Leading comment\n"
6326                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6327                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6328   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6329                "                 1, 1, 1, 1};",
6330                getLLVMStyleWithColumns(39));
6331   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6332                "                 1, 1, 1, 1};",
6333                getLLVMStyleWithColumns(38));
6334   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
6335                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
6336                getLLVMStyleWithColumns(43));
6337   verifyFormat(
6338       "static unsigned SomeValues[10][3] = {\n"
6339       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
6340       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
6341   verifyFormat("static auto fields = new vector<string>{\n"
6342                "    \"aaaaaaaaaaaaa\",\n"
6343                "    \"aaaaaaaaaaaaa\",\n"
6344                "    \"aaaaaaaaaaaa\",\n"
6345                "    \"aaaaaaaaaaaaaa\",\n"
6346                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6347                "    \"aaaaaaaaaaaa\",\n"
6348                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6349                "};");
6350   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
6351   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
6352                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
6353                "                 3, cccccccccccccccccccccc};",
6354                getLLVMStyleWithColumns(60));
6355 
6356   // Trailing commas.
6357   verifyFormat("vector<int> x = {\n"
6358                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
6359                "};",
6360                getLLVMStyleWithColumns(39));
6361   verifyFormat("vector<int> x = {\n"
6362                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
6363                "};",
6364                getLLVMStyleWithColumns(39));
6365   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6366                "                 1, 1, 1, 1,\n"
6367                "                 /**/ /**/};",
6368                getLLVMStyleWithColumns(39));
6369 
6370   // Trailing comment in the first line.
6371   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
6372                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
6373                "    111111111,  222222222,  3333333333,  444444444,  //\n"
6374                "    11111111,   22222222,   333333333,   44444444};");
6375   // Trailing comment in the last line.
6376   verifyFormat("int aaaaa[] = {\n"
6377                "    1, 2, 3, // comment\n"
6378                "    4, 5, 6  // comment\n"
6379                "};");
6380 
6381   // With nested lists, we should either format one item per line or all nested
6382   // lists one on line.
6383   // FIXME: For some nested lists, we can do better.
6384   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
6385                "        {aaaaaaaaaaaaaaaaaaa},\n"
6386                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
6387                "        {aaaaaaaaaaaaaaaaa}};",
6388                getLLVMStyleWithColumns(60));
6389   verifyFormat(
6390       "SomeStruct my_struct_array = {\n"
6391       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
6392       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
6393       "    {aaa, aaa},\n"
6394       "    {aaa, aaa},\n"
6395       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
6396       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6397       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
6398 
6399   // No column layout should be used here.
6400   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
6401                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
6402 
6403   verifyNoCrash("a<,");
6404 }
6405 
6406 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
6407   FormatStyle DoNotMerge = getLLVMStyle();
6408   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6409 
6410   verifyFormat("void f() { return 42; }");
6411   verifyFormat("void f() {\n"
6412                "  return 42;\n"
6413                "}",
6414                DoNotMerge);
6415   verifyFormat("void f() {\n"
6416                "  // Comment\n"
6417                "}");
6418   verifyFormat("{\n"
6419                "#error {\n"
6420                "  int a;\n"
6421                "}");
6422   verifyFormat("{\n"
6423                "  int a;\n"
6424                "#error {\n"
6425                "}");
6426   verifyFormat("void f() {} // comment");
6427   verifyFormat("void f() { int a; } // comment");
6428   verifyFormat("void f() {\n"
6429                "} // comment",
6430                DoNotMerge);
6431   verifyFormat("void f() {\n"
6432                "  int a;\n"
6433                "} // comment",
6434                DoNotMerge);
6435   verifyFormat("void f() {\n"
6436                "} // comment",
6437                getLLVMStyleWithColumns(15));
6438 
6439   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
6440   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
6441 
6442   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
6443   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
6444   verifyFormat("class C {\n"
6445                "  C()\n"
6446                "      : iiiiiiii(nullptr),\n"
6447                "        kkkkkkk(nullptr),\n"
6448                "        mmmmmmm(nullptr),\n"
6449                "        nnnnnnn(nullptr) {}\n"
6450                "};",
6451                getGoogleStyle());
6452 
6453   FormatStyle NoColumnLimit = getLLVMStyle();
6454   NoColumnLimit.ColumnLimit = 0;
6455   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
6456   EXPECT_EQ("class C {\n"
6457             "  A() : b(0) {}\n"
6458             "};",
6459             format("class C{A():b(0){}};", NoColumnLimit));
6460   EXPECT_EQ("A()\n"
6461             "    : b(0) {\n"
6462             "}",
6463             format("A()\n:b(0)\n{\n}", NoColumnLimit));
6464 
6465   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
6466   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
6467       FormatStyle::SFS_None;
6468   EXPECT_EQ("A()\n"
6469             "    : b(0) {\n"
6470             "}",
6471             format("A():b(0){}", DoNotMergeNoColumnLimit));
6472   EXPECT_EQ("A()\n"
6473             "    : b(0) {\n"
6474             "}",
6475             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
6476 
6477   verifyFormat("#define A          \\\n"
6478                "  void f() {       \\\n"
6479                "    int i;         \\\n"
6480                "  }",
6481                getLLVMStyleWithColumns(20));
6482   verifyFormat("#define A           \\\n"
6483                "  void f() { int i; }",
6484                getLLVMStyleWithColumns(21));
6485   verifyFormat("#define A            \\\n"
6486                "  void f() {         \\\n"
6487                "    int i;           \\\n"
6488                "  }                  \\\n"
6489                "  int j;",
6490                getLLVMStyleWithColumns(22));
6491   verifyFormat("#define A             \\\n"
6492                "  void f() { int i; } \\\n"
6493                "  int j;",
6494                getLLVMStyleWithColumns(23));
6495 }
6496 
6497 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
6498   FormatStyle MergeInlineOnly = getLLVMStyle();
6499   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
6500   verifyFormat("class C {\n"
6501                "  int f() { return 42; }\n"
6502                "};",
6503                MergeInlineOnly);
6504   verifyFormat("int f() {\n"
6505                "  return 42;\n"
6506                "}",
6507                MergeInlineOnly);
6508 }
6509 
6510 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
6511   // Elaborate type variable declarations.
6512   verifyFormat("struct foo a = {bar};\nint n;");
6513   verifyFormat("class foo a = {bar};\nint n;");
6514   verifyFormat("union foo a = {bar};\nint n;");
6515 
6516   // Elaborate types inside function definitions.
6517   verifyFormat("struct foo f() {}\nint n;");
6518   verifyFormat("class foo f() {}\nint n;");
6519   verifyFormat("union foo f() {}\nint n;");
6520 
6521   // Templates.
6522   verifyFormat("template <class X> void f() {}\nint n;");
6523   verifyFormat("template <struct X> void f() {}\nint n;");
6524   verifyFormat("template <union X> void f() {}\nint n;");
6525 
6526   // Actual definitions...
6527   verifyFormat("struct {\n} n;");
6528   verifyFormat(
6529       "template <template <class T, class Y>, class Z> class X {\n} n;");
6530   verifyFormat("union Z {\n  int n;\n} x;");
6531   verifyFormat("class MACRO Z {\n} n;");
6532   verifyFormat("class MACRO(X) Z {\n} n;");
6533   verifyFormat("class __attribute__(X) Z {\n} n;");
6534   verifyFormat("class __declspec(X) Z {\n} n;");
6535   verifyFormat("class A##B##C {\n} n;");
6536   verifyFormat("class alignas(16) Z {\n} n;");
6537   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
6538   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
6539 
6540   // Redefinition from nested context:
6541   verifyFormat("class A::B::C {\n} n;");
6542 
6543   // Template definitions.
6544   verifyFormat(
6545       "template <typename F>\n"
6546       "Matcher(const Matcher<F> &Other,\n"
6547       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
6548       "                             !is_same<F, T>::value>::type * = 0)\n"
6549       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
6550 
6551   // FIXME: This is still incorrectly handled at the formatter side.
6552   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
6553   verifyFormat("int i = SomeFunction(a<b, a> b);");
6554 
6555   // FIXME:
6556   // This now gets parsed incorrectly as class definition.
6557   // verifyFormat("class A<int> f() {\n}\nint n;");
6558 
6559   // Elaborate types where incorrectly parsing the structural element would
6560   // break the indent.
6561   verifyFormat("if (true)\n"
6562                "  class X x;\n"
6563                "else\n"
6564                "  f();\n");
6565 
6566   // This is simply incomplete. Formatting is not important, but must not crash.
6567   verifyFormat("class A:");
6568 }
6569 
6570 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
6571   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
6572             format("#error Leave     all         white!!!!! space* alone!\n"));
6573   EXPECT_EQ(
6574       "#warning Leave     all         white!!!!! space* alone!\n",
6575       format("#warning Leave     all         white!!!!! space* alone!\n"));
6576   EXPECT_EQ("#error 1", format("  #  error   1"));
6577   EXPECT_EQ("#warning 1", format("  #  warning 1"));
6578 }
6579 
6580 TEST_F(FormatTest, FormatHashIfExpressions) {
6581   verifyFormat("#if AAAA && BBBB");
6582   verifyFormat("#if (AAAA && BBBB)");
6583   verifyFormat("#elif (AAAA && BBBB)");
6584   // FIXME: Come up with a better indentation for #elif.
6585   verifyFormat(
6586       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
6587       "    defined(BBBBBBBB)\n"
6588       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
6589       "    defined(BBBBBBBB)\n"
6590       "#endif",
6591       getLLVMStyleWithColumns(65));
6592 }
6593 
6594 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
6595   FormatStyle AllowsMergedIf = getGoogleStyle();
6596   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
6597   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
6598   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
6599   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
6600   EXPECT_EQ("if (true) return 42;",
6601             format("if (true)\nreturn 42;", AllowsMergedIf));
6602   FormatStyle ShortMergedIf = AllowsMergedIf;
6603   ShortMergedIf.ColumnLimit = 25;
6604   verifyFormat("#define A \\\n"
6605                "  if (true) return 42;",
6606                ShortMergedIf);
6607   verifyFormat("#define A \\\n"
6608                "  f();    \\\n"
6609                "  if (true)\n"
6610                "#define B",
6611                ShortMergedIf);
6612   verifyFormat("#define A \\\n"
6613                "  f();    \\\n"
6614                "  if (true)\n"
6615                "g();",
6616                ShortMergedIf);
6617   verifyFormat("{\n"
6618                "#ifdef A\n"
6619                "  // Comment\n"
6620                "  if (true) continue;\n"
6621                "#endif\n"
6622                "  // Comment\n"
6623                "  if (true) continue;\n"
6624                "}",
6625                ShortMergedIf);
6626   ShortMergedIf.ColumnLimit = 29;
6627   verifyFormat("#define A                   \\\n"
6628                "  if (aaaaaaaaaa) return 1; \\\n"
6629                "  return 2;",
6630                ShortMergedIf);
6631   ShortMergedIf.ColumnLimit = 28;
6632   verifyFormat("#define A         \\\n"
6633                "  if (aaaaaaaaaa) \\\n"
6634                "    return 1;     \\\n"
6635                "  return 2;",
6636                ShortMergedIf);
6637 }
6638 
6639 TEST_F(FormatTest, BlockCommentsInControlLoops) {
6640   verifyFormat("if (0) /* a comment in a strange place */ {\n"
6641                "  f();\n"
6642                "}");
6643   verifyFormat("if (0) /* a comment in a strange place */ {\n"
6644                "  f();\n"
6645                "} /* another comment */ else /* comment #3 */ {\n"
6646                "  g();\n"
6647                "}");
6648   verifyFormat("while (0) /* a comment in a strange place */ {\n"
6649                "  f();\n"
6650                "}");
6651   verifyFormat("for (;;) /* a comment in a strange place */ {\n"
6652                "  f();\n"
6653                "}");
6654   verifyFormat("do /* a comment in a strange place */ {\n"
6655                "  f();\n"
6656                "} /* another comment */ while (0);");
6657 }
6658 
6659 TEST_F(FormatTest, BlockComments) {
6660   EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
6661             format("/* *//* */  /* */\n/* *//* */  /* */"));
6662   EXPECT_EQ("/* */ a /* */ b;", format("  /* */  a/* */  b;"));
6663   EXPECT_EQ("#define A /*123*/ \\\n"
6664             "  b\n"
6665             "/* */\n"
6666             "someCall(\n"
6667             "    parameter);",
6668             format("#define A /*123*/ b\n"
6669                    "/* */\n"
6670                    "someCall(parameter);",
6671                    getLLVMStyleWithColumns(15)));
6672 
6673   EXPECT_EQ("#define A\n"
6674             "/* */ someCall(\n"
6675             "    parameter);",
6676             format("#define A\n"
6677                    "/* */someCall(parameter);",
6678                    getLLVMStyleWithColumns(15)));
6679   EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
6680   EXPECT_EQ("/*\n"
6681             "*\n"
6682             " * aaaaaa\n"
6683             " * aaaaaa\n"
6684             "*/",
6685             format("/*\n"
6686                    "*\n"
6687                    " * aaaaaa aaaaaa\n"
6688                    "*/",
6689                    getLLVMStyleWithColumns(10)));
6690   EXPECT_EQ("/*\n"
6691             "**\n"
6692             "* aaaaaa\n"
6693             "*aaaaaa\n"
6694             "*/",
6695             format("/*\n"
6696                    "**\n"
6697                    "* aaaaaa aaaaaa\n"
6698                    "*/",
6699                    getLLVMStyleWithColumns(10)));
6700 
6701   FormatStyle NoBinPacking = getLLVMStyle();
6702   NoBinPacking.BinPackParameters = false;
6703   EXPECT_EQ("someFunction(1, /* comment 1 */\n"
6704             "             2, /* comment 2 */\n"
6705             "             3, /* comment 3 */\n"
6706             "             aaaa,\n"
6707             "             bbbb);",
6708             format("someFunction (1,   /* comment 1 */\n"
6709                    "                2, /* comment 2 */  \n"
6710                    "               3,   /* comment 3 */\n"
6711                    "aaaa, bbbb );",
6712                    NoBinPacking));
6713   verifyFormat(
6714       "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6715       "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6716   EXPECT_EQ(
6717       "bool aaaaaaaaaaaaa = /* trailing comment */\n"
6718       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6719       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
6720       format(
6721           "bool       aaaaaaaaaaaaa =       /* trailing comment */\n"
6722           "    aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa    ||\n"
6723           "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa   || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
6724   EXPECT_EQ(
6725       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
6726       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   /* comment */\n"
6727       "int cccccccccccccccccccccccccccccc;       /* comment */\n",
6728       format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
6729              "int      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
6730              "int    cccccccccccccccccccccccccccccc;  /* comment */\n"));
6731 
6732   verifyFormat("void f(int * /* unused */) {}");
6733 
6734   EXPECT_EQ("/*\n"
6735             " **\n"
6736             " */",
6737             format("/*\n"
6738                    " **\n"
6739                    " */"));
6740   EXPECT_EQ("/*\n"
6741             " *q\n"
6742             " */",
6743             format("/*\n"
6744                    " *q\n"
6745                    " */"));
6746   EXPECT_EQ("/*\n"
6747             " * q\n"
6748             " */",
6749             format("/*\n"
6750                    " * q\n"
6751                    " */"));
6752   EXPECT_EQ("/*\n"
6753             " **/",
6754             format("/*\n"
6755                    " **/"));
6756   EXPECT_EQ("/*\n"
6757             " ***/",
6758             format("/*\n"
6759                    " ***/"));
6760 }
6761 
6762 TEST_F(FormatTest, BlockCommentsInMacros) {
6763   EXPECT_EQ("#define A          \\\n"
6764             "  {                \\\n"
6765             "    /* one line */ \\\n"
6766             "    someCall();",
6767             format("#define A {        \\\n"
6768                    "  /* one line */   \\\n"
6769                    "  someCall();",
6770                    getLLVMStyleWithColumns(20)));
6771   EXPECT_EQ("#define A          \\\n"
6772             "  {                \\\n"
6773             "    /* previous */ \\\n"
6774             "    /* one line */ \\\n"
6775             "    someCall();",
6776             format("#define A {        \\\n"
6777                    "  /* previous */   \\\n"
6778                    "  /* one line */   \\\n"
6779                    "  someCall();",
6780                    getLLVMStyleWithColumns(20)));
6781 }
6782 
6783 TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
6784   EXPECT_EQ("a = {\n"
6785             "    1111 /*    */\n"
6786             "};",
6787             format("a = {1111 /*    */\n"
6788                    "};",
6789                    getLLVMStyleWithColumns(15)));
6790   EXPECT_EQ("a = {\n"
6791             "    1111 /*      */\n"
6792             "};",
6793             format("a = {1111 /*      */\n"
6794                    "};",
6795                    getLLVMStyleWithColumns(15)));
6796 
6797   // FIXME: The formatting is still wrong here.
6798   EXPECT_EQ("a = {\n"
6799             "    1111 /*      a\n"
6800             "            */\n"
6801             "};",
6802             format("a = {1111 /*      a */\n"
6803                    "};",
6804                    getLLVMStyleWithColumns(15)));
6805 }
6806 
6807 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
6808   // FIXME: This is not what we want...
6809   verifyFormat("{\n"
6810                "// a"
6811                "// b");
6812 }
6813 
6814 TEST_F(FormatTest, FormatStarDependingOnContext) {
6815   verifyFormat("void f(int *a);");
6816   verifyFormat("void f() { f(fint * b); }");
6817   verifyFormat("class A {\n  void f(int *a);\n};");
6818   verifyFormat("class A {\n  int *a;\n};");
6819   verifyFormat("namespace a {\n"
6820                "namespace b {\n"
6821                "class A {\n"
6822                "  void f() {}\n"
6823                "  int *a;\n"
6824                "};\n"
6825                "}\n"
6826                "}");
6827 }
6828 
6829 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
6830   verifyFormat("while");
6831   verifyFormat("operator");
6832 }
6833 
6834 //===----------------------------------------------------------------------===//
6835 // Objective-C tests.
6836 //===----------------------------------------------------------------------===//
6837 
6838 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
6839   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
6840   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
6841             format("-(NSUInteger)indexOfObject:(id)anObject;"));
6842   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
6843   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
6844   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
6845             format("-(NSInteger)Method3:(id)anObject;"));
6846   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
6847             format("-(NSInteger)Method4:(id)anObject;"));
6848   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
6849             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
6850   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
6851             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
6852   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
6853             "forAllCells:(BOOL)flag;",
6854             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
6855                    "forAllCells:(BOOL)flag;"));
6856 
6857   // Very long objectiveC method declaration.
6858   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
6859                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
6860   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
6861                "                    inRange:(NSRange)range\n"
6862                "                   outRange:(NSRange)out_range\n"
6863                "                  outRange1:(NSRange)out_range1\n"
6864                "                  outRange2:(NSRange)out_range2\n"
6865                "                  outRange3:(NSRange)out_range3\n"
6866                "                  outRange4:(NSRange)out_range4\n"
6867                "                  outRange5:(NSRange)out_range5\n"
6868                "                  outRange6:(NSRange)out_range6\n"
6869                "                  outRange7:(NSRange)out_range7\n"
6870                "                  outRange8:(NSRange)out_range8\n"
6871                "                  outRange9:(NSRange)out_range9;");
6872 
6873   // When the function name has to be wrapped.
6874   FormatStyle Style = getLLVMStyle();
6875   Style.IndentWrappedFunctionNames = false;
6876   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
6877                "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
6878                "           anotherName:(NSString)bbbbbbbbbbbbbb {\n"
6879                "}",
6880                Style);
6881   Style.IndentWrappedFunctionNames = true;
6882   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
6883                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
6884                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
6885                "}",
6886                Style);
6887 
6888   verifyFormat("- (int)sum:(vector<int>)numbers;");
6889   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
6890   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
6891   // protocol lists (but not for template classes):
6892   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
6893 
6894   verifyFormat("- (int (*)())foo:(int (*)())f;");
6895   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
6896 
6897   // If there's no return type (very rare in practice!), LLVM and Google style
6898   // agree.
6899   verifyFormat("- foo;");
6900   verifyFormat("- foo:(int)f;");
6901   verifyGoogleFormat("- foo:(int)foo;");
6902 }
6903 
6904 TEST_F(FormatTest, FormatObjCInterface) {
6905   verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
6906                "@public\n"
6907                "  int field1;\n"
6908                "@protected\n"
6909                "  int field2;\n"
6910                "@private\n"
6911                "  int field3;\n"
6912                "@package\n"
6913                "  int field4;\n"
6914                "}\n"
6915                "+ (id)init;\n"
6916                "@end");
6917 
6918   verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
6919                      " @public\n"
6920                      "  int field1;\n"
6921                      " @protected\n"
6922                      "  int field2;\n"
6923                      " @private\n"
6924                      "  int field3;\n"
6925                      " @package\n"
6926                      "  int field4;\n"
6927                      "}\n"
6928                      "+ (id)init;\n"
6929                      "@end");
6930 
6931   verifyFormat("@interface /* wait for it */ Foo\n"
6932                "+ (id)init;\n"
6933                "// Look, a comment!\n"
6934                "- (int)answerWith:(int)i;\n"
6935                "@end");
6936 
6937   verifyFormat("@interface Foo\n"
6938                "@end\n"
6939                "@interface Bar\n"
6940                "@end");
6941 
6942   verifyFormat("@interface Foo : Bar\n"
6943                "+ (id)init;\n"
6944                "@end");
6945 
6946   verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
6947                "+ (id)init;\n"
6948                "@end");
6949 
6950   verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
6951                      "+ (id)init;\n"
6952                      "@end");
6953 
6954   verifyFormat("@interface Foo (HackStuff)\n"
6955                "+ (id)init;\n"
6956                "@end");
6957 
6958   verifyFormat("@interface Foo ()\n"
6959                "+ (id)init;\n"
6960                "@end");
6961 
6962   verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
6963                "+ (id)init;\n"
6964                "@end");
6965 
6966   verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
6967                      "+ (id)init;\n"
6968                      "@end");
6969 
6970   verifyFormat("@interface Foo {\n"
6971                "  int _i;\n"
6972                "}\n"
6973                "+ (id)init;\n"
6974                "@end");
6975 
6976   verifyFormat("@interface Foo : Bar {\n"
6977                "  int _i;\n"
6978                "}\n"
6979                "+ (id)init;\n"
6980                "@end");
6981 
6982   verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
6983                "  int _i;\n"
6984                "}\n"
6985                "+ (id)init;\n"
6986                "@end");
6987 
6988   verifyFormat("@interface Foo (HackStuff) {\n"
6989                "  int _i;\n"
6990                "}\n"
6991                "+ (id)init;\n"
6992                "@end");
6993 
6994   verifyFormat("@interface Foo () {\n"
6995                "  int _i;\n"
6996                "}\n"
6997                "+ (id)init;\n"
6998                "@end");
6999 
7000   verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
7001                "  int _i;\n"
7002                "}\n"
7003                "+ (id)init;\n"
7004                "@end");
7005 
7006   FormatStyle OnePerLine = getGoogleStyle();
7007   OnePerLine.BinPackParameters = false;
7008   verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n"
7009                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7010                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7011                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7012                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
7013                "}",
7014                OnePerLine);
7015 }
7016 
7017 TEST_F(FormatTest, FormatObjCImplementation) {
7018   verifyFormat("@implementation Foo : NSObject {\n"
7019                "@public\n"
7020                "  int field1;\n"
7021                "@protected\n"
7022                "  int field2;\n"
7023                "@private\n"
7024                "  int field3;\n"
7025                "@package\n"
7026                "  int field4;\n"
7027                "}\n"
7028                "+ (id)init {\n}\n"
7029                "@end");
7030 
7031   verifyGoogleFormat("@implementation Foo : NSObject {\n"
7032                      " @public\n"
7033                      "  int field1;\n"
7034                      " @protected\n"
7035                      "  int field2;\n"
7036                      " @private\n"
7037                      "  int field3;\n"
7038                      " @package\n"
7039                      "  int field4;\n"
7040                      "}\n"
7041                      "+ (id)init {\n}\n"
7042                      "@end");
7043 
7044   verifyFormat("@implementation Foo\n"
7045                "+ (id)init {\n"
7046                "  if (true)\n"
7047                "    return nil;\n"
7048                "}\n"
7049                "// Look, a comment!\n"
7050                "- (int)answerWith:(int)i {\n"
7051                "  return i;\n"
7052                "}\n"
7053                "+ (int)answerWith:(int)i {\n"
7054                "  return i;\n"
7055                "}\n"
7056                "@end");
7057 
7058   verifyFormat("@implementation Foo\n"
7059                "@end\n"
7060                "@implementation Bar\n"
7061                "@end");
7062 
7063   EXPECT_EQ("@implementation Foo : Bar\n"
7064             "+ (id)init {\n}\n"
7065             "- (void)foo {\n}\n"
7066             "@end",
7067             format("@implementation Foo : Bar\n"
7068                    "+(id)init{}\n"
7069                    "-(void)foo{}\n"
7070                    "@end"));
7071 
7072   verifyFormat("@implementation Foo {\n"
7073                "  int _i;\n"
7074                "}\n"
7075                "+ (id)init {\n}\n"
7076                "@end");
7077 
7078   verifyFormat("@implementation Foo : Bar {\n"
7079                "  int _i;\n"
7080                "}\n"
7081                "+ (id)init {\n}\n"
7082                "@end");
7083 
7084   verifyFormat("@implementation Foo (HackStuff)\n"
7085                "+ (id)init {\n}\n"
7086                "@end");
7087   verifyFormat("@implementation ObjcClass\n"
7088                "- (void)method;\n"
7089                "{}\n"
7090                "@end");
7091 }
7092 
7093 TEST_F(FormatTest, FormatObjCProtocol) {
7094   verifyFormat("@protocol Foo\n"
7095                "@property(weak) id delegate;\n"
7096                "- (NSUInteger)numberOfThings;\n"
7097                "@end");
7098 
7099   verifyFormat("@protocol MyProtocol <NSObject>\n"
7100                "- (NSUInteger)numberOfThings;\n"
7101                "@end");
7102 
7103   verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
7104                      "- (NSUInteger)numberOfThings;\n"
7105                      "@end");
7106 
7107   verifyFormat("@protocol Foo;\n"
7108                "@protocol Bar;\n");
7109 
7110   verifyFormat("@protocol Foo\n"
7111                "@end\n"
7112                "@protocol Bar\n"
7113                "@end");
7114 
7115   verifyFormat("@protocol myProtocol\n"
7116                "- (void)mandatoryWithInt:(int)i;\n"
7117                "@optional\n"
7118                "- (void)optional;\n"
7119                "@required\n"
7120                "- (void)required;\n"
7121                "@optional\n"
7122                "@property(assign) int madProp;\n"
7123                "@end\n");
7124 
7125   verifyFormat("@property(nonatomic, assign, readonly)\n"
7126                "    int *looooooooooooooooooooooooooooongNumber;\n"
7127                "@property(nonatomic, assign, readonly)\n"
7128                "    NSString *looooooooooooooooooooooooooooongName;");
7129 
7130   verifyFormat("@implementation PR18406\n"
7131                "}\n"
7132                "@end");
7133 }
7134 
7135 TEST_F(FormatTest, FormatObjCMethodDeclarations) {
7136   verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
7137                "                   rect:(NSRect)theRect\n"
7138                "               interval:(float)theInterval {\n"
7139                "}");
7140   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7141                "          longKeyword:(NSRect)theRect\n"
7142                "    evenLongerKeyword:(float)theInterval\n"
7143                "                error:(NSError **)theError {\n"
7144                "}");
7145   verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
7146                "                         y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
7147                "    NS_DESIGNATED_INITIALIZER;",
7148                getLLVMStyleWithColumns(60));
7149 
7150   // Continuation indent width should win over aligning colons if the function
7151   // name is long.
7152   FormatStyle continuationStyle = getGoogleStyle();
7153   continuationStyle.ColumnLimit = 40;
7154   continuationStyle.IndentWrappedFunctionNames = true;
7155   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7156                "    dontAlignNamef:(NSRect)theRect {\n"
7157                "}",
7158                continuationStyle);
7159 
7160   // Make sure we don't break aligning for short parameter names.
7161   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7162                "       aShortf:(NSRect)theRect {\n"
7163                "}",
7164                continuationStyle);
7165 }
7166 
7167 TEST_F(FormatTest, FormatObjCMethodExpr) {
7168   verifyFormat("[foo bar:baz];");
7169   verifyFormat("return [foo bar:baz];");
7170   verifyFormat("return (a)[foo bar:baz];");
7171   verifyFormat("f([foo bar:baz]);");
7172   verifyFormat("f(2, [foo bar:baz]);");
7173   verifyFormat("f(2, a ? b : c);");
7174   verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
7175 
7176   // Unary operators.
7177   verifyFormat("int a = +[foo bar:baz];");
7178   verifyFormat("int a = -[foo bar:baz];");
7179   verifyFormat("int a = ![foo bar:baz];");
7180   verifyFormat("int a = ~[foo bar:baz];");
7181   verifyFormat("int a = ++[foo bar:baz];");
7182   verifyFormat("int a = --[foo bar:baz];");
7183   verifyFormat("int a = sizeof [foo bar:baz];");
7184   verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
7185   verifyFormat("int a = &[foo bar:baz];");
7186   verifyFormat("int a = *[foo bar:baz];");
7187   // FIXME: Make casts work, without breaking f()[4].
7188   // verifyFormat("int a = (int)[foo bar:baz];");
7189   // verifyFormat("return (int)[foo bar:baz];");
7190   // verifyFormat("(void)[foo bar:baz];");
7191   verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
7192 
7193   // Binary operators.
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   verifyFormat("[foo bar:baz] -= [foo bar:baz];");
7201   verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
7202   verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
7203   verifyFormat("[foo bar:baz] &= [foo bar:baz];");
7204   verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
7205   verifyFormat("[foo bar:baz] |= [foo bar:baz];");
7206   verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
7207   verifyFormat("[foo bar:baz] || [foo bar:baz];");
7208   verifyFormat("[foo bar:baz] && [foo bar:baz];");
7209   verifyFormat("[foo bar:baz] | [foo bar:baz];");
7210   verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
7211   verifyFormat("[foo bar:baz] & [foo bar:baz];");
7212   verifyFormat("[foo bar:baz] == [foo bar:baz];");
7213   verifyFormat("[foo bar:baz] != [foo bar:baz];");
7214   verifyFormat("[foo bar:baz] >= [foo bar:baz];");
7215   verifyFormat("[foo bar:baz] <= [foo bar:baz];");
7216   verifyFormat("[foo bar:baz] > [foo bar:baz];");
7217   verifyFormat("[foo bar:baz] < [foo bar:baz];");
7218   verifyFormat("[foo bar:baz] >> [foo bar:baz];");
7219   verifyFormat("[foo bar:baz] << [foo bar:baz];");
7220   verifyFormat("[foo bar:baz] - [foo bar:baz];");
7221   verifyFormat("[foo bar:baz] + [foo bar:baz];");
7222   verifyFormat("[foo bar:baz] * [foo bar:baz];");
7223   verifyFormat("[foo bar:baz] / [foo bar:baz];");
7224   verifyFormat("[foo bar:baz] % [foo bar:baz];");
7225   // Whew!
7226 
7227   verifyFormat("return in[42];");
7228   verifyFormat("for (auto v : in[1]) {\n}");
7229   verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
7230   verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
7231   verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
7232   verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
7233   verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
7234   verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
7235                "}");
7236   verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
7237   verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
7238   verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
7239 
7240   verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
7241   verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
7242   verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
7243   verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
7244   verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
7245   verifyFormat("[button setAction:@selector(zoomOut:)];");
7246   verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
7247 
7248   verifyFormat("arr[[self indexForFoo:a]];");
7249   verifyFormat("throw [self errorFor:a];");
7250   verifyFormat("@throw [self errorFor:a];");
7251 
7252   verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
7253   verifyFormat("[(id)foo bar:(id) ? baz : quux];");
7254   verifyFormat("4 > 4 ? (id)a : (id)baz;");
7255 
7256   // This tests that the formatter doesn't break after "backing" but before ":",
7257   // which would be at 80 columns.
7258   verifyFormat(
7259       "void f() {\n"
7260       "  if ((self = [super initWithContentRect:contentRect\n"
7261       "                               styleMask:styleMask ?: otherMask\n"
7262       "                                 backing:NSBackingStoreBuffered\n"
7263       "                                   defer:YES]))");
7264 
7265   verifyFormat(
7266       "[foo checkThatBreakingAfterColonWorksOk:\n"
7267       "         [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
7268 
7269   verifyFormat("[myObj short:arg1 // Force line break\n"
7270                "          longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
7271                "    evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
7272                "                error:arg4];");
7273   verifyFormat(
7274       "void f() {\n"
7275       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
7276       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
7277       "                                     pos.width(), pos.height())\n"
7278       "                styleMask:NSBorderlessWindowMask\n"
7279       "                  backing:NSBackingStoreBuffered\n"
7280       "                    defer:NO]);\n"
7281       "}");
7282   verifyFormat(
7283       "void f() {\n"
7284       "  popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
7285       "      iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
7286       "                                 pos.width(), pos.height())\n"
7287       "                syeMask:NSBorderlessWindowMask\n"
7288       "                  bking:NSBackingStoreBuffered\n"
7289       "                    der:NO]);\n"
7290       "}",
7291       getLLVMStyleWithColumns(70));
7292   verifyFormat(
7293       "void f() {\n"
7294       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
7295       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
7296       "                                     pos.width(), pos.height())\n"
7297       "                styleMask:NSBorderlessWindowMask\n"
7298       "                  backing:NSBackingStoreBuffered\n"
7299       "                    defer:NO]);\n"
7300       "}",
7301       getChromiumStyle(FormatStyle::LK_Cpp));
7302   verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
7303                "                             with:contentsNativeView];");
7304 
7305   verifyFormat(
7306       "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
7307       "           owner:nillllll];");
7308 
7309   verifyFormat(
7310       "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
7311       "        forType:kBookmarkButtonDragType];");
7312 
7313   verifyFormat("[defaultCenter addObserver:self\n"
7314                "                  selector:@selector(willEnterFullscreen)\n"
7315                "                      name:kWillEnterFullscreenNotification\n"
7316                "                    object:nil];");
7317   verifyFormat("[image_rep drawInRect:drawRect\n"
7318                "             fromRect:NSZeroRect\n"
7319                "            operation:NSCompositeCopy\n"
7320                "             fraction:1.0\n"
7321                "       respectFlipped:NO\n"
7322                "                hints:nil];");
7323   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7324                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
7325   verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
7326                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
7327   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
7328                "    aaaaaaaaaaaaaaaaaaaaaa];");
7329   verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
7330                "        .aaaaaaaa];", // FIXME: Indentation seems off.
7331                getLLVMStyleWithColumns(60));
7332 
7333   verifyFormat(
7334       "scoped_nsobject<NSTextField> message(\n"
7335       "    // The frame will be fixed up when |-setMessageText:| is called.\n"
7336       "    [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
7337   verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
7338                "    aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
7339                "         aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
7340                "          aaaa:bbb];");
7341   verifyFormat("[self param:function( //\n"
7342                "                parameter)]");
7343   verifyFormat(
7344       "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7345       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7346       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
7347 
7348   // FIXME: This violates the column limit.
7349   verifyFormat(
7350       "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
7351       "    aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
7352       "                  aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];",
7353       getLLVMStyleWithColumns(60));
7354 
7355   // Variadic parameters.
7356   verifyFormat(
7357       "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
7358   verifyFormat(
7359       "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7360       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7361       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
7362   verifyFormat("[self // break\n"
7363                "      a:a\n"
7364                "    aaa:aaa];");
7365   verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
7366                "          [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
7367 }
7368 
7369 TEST_F(FormatTest, ObjCAt) {
7370   verifyFormat("@autoreleasepool");
7371   verifyFormat("@catch");
7372   verifyFormat("@class");
7373   verifyFormat("@compatibility_alias");
7374   verifyFormat("@defs");
7375   verifyFormat("@dynamic");
7376   verifyFormat("@encode");
7377   verifyFormat("@end");
7378   verifyFormat("@finally");
7379   verifyFormat("@implementation");
7380   verifyFormat("@import");
7381   verifyFormat("@interface");
7382   verifyFormat("@optional");
7383   verifyFormat("@package");
7384   verifyFormat("@private");
7385   verifyFormat("@property");
7386   verifyFormat("@protected");
7387   verifyFormat("@protocol");
7388   verifyFormat("@public");
7389   verifyFormat("@required");
7390   verifyFormat("@selector");
7391   verifyFormat("@synchronized");
7392   verifyFormat("@synthesize");
7393   verifyFormat("@throw");
7394   verifyFormat("@try");
7395 
7396   EXPECT_EQ("@interface", format("@ interface"));
7397 
7398   // The precise formatting of this doesn't matter, nobody writes code like
7399   // this.
7400   verifyFormat("@ /*foo*/ interface");
7401 }
7402 
7403 TEST_F(FormatTest, ObjCSnippets) {
7404   verifyFormat("@autoreleasepool {\n"
7405                "  foo();\n"
7406                "}");
7407   verifyFormat("@class Foo, Bar;");
7408   verifyFormat("@compatibility_alias AliasName ExistingClass;");
7409   verifyFormat("@dynamic textColor;");
7410   verifyFormat("char *buf1 = @encode(int *);");
7411   verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
7412   verifyFormat("char *buf1 = @encode(int **);");
7413   verifyFormat("Protocol *proto = @protocol(p1);");
7414   verifyFormat("SEL s = @selector(foo:);");
7415   verifyFormat("@synchronized(self) {\n"
7416                "  f();\n"
7417                "}");
7418 
7419   verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7420   verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7421 
7422   verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
7423   verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
7424   verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
7425   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7426                getMozillaStyle());
7427   verifyFormat("@property BOOL editable;", getMozillaStyle());
7428   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7429                getWebKitStyle());
7430   verifyFormat("@property BOOL editable;", getWebKitStyle());
7431 
7432   verifyFormat("@import foo.bar;\n"
7433                "@import baz;");
7434 }
7435 
7436 TEST_F(FormatTest, ObjCLiterals) {
7437   verifyFormat("@\"String\"");
7438   verifyFormat("@1");
7439   verifyFormat("@+4.8");
7440   verifyFormat("@-4");
7441   verifyFormat("@1LL");
7442   verifyFormat("@.5");
7443   verifyFormat("@'c'");
7444   verifyFormat("@true");
7445 
7446   verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
7447   verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
7448   verifyFormat("NSNumber *favoriteColor = @(Green);");
7449   verifyFormat("NSString *path = @(getenv(\"PATH\"));");
7450 
7451   verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
7452 }
7453 
7454 TEST_F(FormatTest, ObjCDictLiterals) {
7455   verifyFormat("@{");
7456   verifyFormat("@{}");
7457   verifyFormat("@{@\"one\" : @1}");
7458   verifyFormat("return @{@\"one\" : @1;");
7459   verifyFormat("@{@\"one\" : @1}");
7460 
7461   verifyFormat("@{@\"one\" : @{@2 : @1}}");
7462   verifyFormat("@{\n"
7463                "  @\"one\" : @{@2 : @1},\n"
7464                "}");
7465 
7466   verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
7467   verifyIncompleteFormat("[self setDict:@{}");
7468   verifyIncompleteFormat("[self setDict:@{@1 : @2}");
7469   verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
7470   verifyFormat(
7471       "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
7472   verifyFormat(
7473       "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
7474 
7475   verifyFormat("NSDictionary *d = @{\n"
7476                "  @\"nam\" : NSUserNam(),\n"
7477                "  @\"dte\" : [NSDate date],\n"
7478                "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
7479                "};");
7480   verifyFormat(
7481       "@{\n"
7482       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7483       "regularFont,\n"
7484       "};");
7485   verifyGoogleFormat(
7486       "@{\n"
7487       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7488       "regularFont,\n"
7489       "};");
7490   verifyFormat(
7491       "@{\n"
7492       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
7493       "      reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
7494       "};");
7495 
7496   // We should try to be robust in case someone forgets the "@".
7497   verifyFormat("NSDictionary *d = {\n"
7498                "  @\"nam\" : NSUserNam(),\n"
7499                "  @\"dte\" : [NSDate date],\n"
7500                "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
7501                "};");
7502   verifyFormat("NSMutableDictionary *dictionary =\n"
7503                "    [NSMutableDictionary dictionaryWithDictionary:@{\n"
7504                "      aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
7505                "      bbbbbbbbbbbbbbbbbb : bbbbb,\n"
7506                "      cccccccccccccccc : ccccccccccccccc\n"
7507                "    }];");
7508 }
7509 
7510 TEST_F(FormatTest, ObjCArrayLiterals) {
7511   verifyIncompleteFormat("@[");
7512   verifyFormat("@[]");
7513   verifyFormat(
7514       "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
7515   verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
7516   verifyFormat("NSArray *array = @[ [foo description] ];");
7517 
7518   verifyFormat(
7519       "NSArray *some_variable = @[\n"
7520       "  aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
7521       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7522       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7523       "  @\"aaaaaaaaaaaaaaaaa\"\n"
7524       "];");
7525   verifyFormat("NSArray *some_variable = @[\n"
7526                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7527                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7528                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7529                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7530                "];");
7531   verifyGoogleFormat("NSArray *some_variable = @[\n"
7532                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7533                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7534                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7535                      "  @\"aaaaaaaaaaaaaaaaa\"\n"
7536                      "];");
7537   verifyFormat("NSArray *array = @[\n"
7538                "  @\"a\",\n"
7539                "  @\"a\",\n" // Trailing comma -> one per line.
7540                "];");
7541 
7542   // We should try to be robust in case someone forgets the "@".
7543   verifyFormat("NSArray *some_variable = [\n"
7544                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7545                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7546                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7547                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7548                "];");
7549   verifyFormat(
7550       "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
7551       "                                             index:(NSUInteger)index\n"
7552       "                                nonDigitAttributes:\n"
7553       "                                    (NSDictionary *)noDigitAttributes;");
7554   verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
7555                "  NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
7556                "]];");
7557 }
7558 
7559 TEST_F(FormatTest, BreaksStringLiterals) {
7560   EXPECT_EQ("\"some text \"\n"
7561             "\"other\";",
7562             format("\"some text other\";", getLLVMStyleWithColumns(12)));
7563   EXPECT_EQ("\"some text \"\n"
7564             "\"other\";",
7565             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
7566   EXPECT_EQ(
7567       "#define A  \\\n"
7568       "  \"some \"  \\\n"
7569       "  \"text \"  \\\n"
7570       "  \"other\";",
7571       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
7572   EXPECT_EQ(
7573       "#define A  \\\n"
7574       "  \"so \"    \\\n"
7575       "  \"text \"  \\\n"
7576       "  \"other\";",
7577       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
7578 
7579   EXPECT_EQ("\"some text\"",
7580             format("\"some text\"", getLLVMStyleWithColumns(1)));
7581   EXPECT_EQ("\"some text\"",
7582             format("\"some text\"", getLLVMStyleWithColumns(11)));
7583   EXPECT_EQ("\"some \"\n"
7584             "\"text\"",
7585             format("\"some text\"", getLLVMStyleWithColumns(10)));
7586   EXPECT_EQ("\"some \"\n"
7587             "\"text\"",
7588             format("\"some text\"", getLLVMStyleWithColumns(7)));
7589   EXPECT_EQ("\"some\"\n"
7590             "\" tex\"\n"
7591             "\"t\"",
7592             format("\"some text\"", getLLVMStyleWithColumns(6)));
7593   EXPECT_EQ("\"some\"\n"
7594             "\" tex\"\n"
7595             "\" and\"",
7596             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
7597   EXPECT_EQ("\"some\"\n"
7598             "\"/tex\"\n"
7599             "\"/and\"",
7600             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
7601 
7602   EXPECT_EQ("variable =\n"
7603             "    \"long string \"\n"
7604             "    \"literal\";",
7605             format("variable = \"long string literal\";",
7606                    getLLVMStyleWithColumns(20)));
7607 
7608   EXPECT_EQ("variable = f(\n"
7609             "    \"long string \"\n"
7610             "    \"literal\",\n"
7611             "    short,\n"
7612             "    loooooooooooooooooooong);",
7613             format("variable = f(\"long string literal\", short, "
7614                    "loooooooooooooooooooong);",
7615                    getLLVMStyleWithColumns(20)));
7616 
7617   EXPECT_EQ(
7618       "f(g(\"long string \"\n"
7619       "    \"literal\"),\n"
7620       "  b);",
7621       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
7622   EXPECT_EQ("f(g(\"long string \"\n"
7623             "    \"literal\",\n"
7624             "    a),\n"
7625             "  b);",
7626             format("f(g(\"long string literal\", a), b);",
7627                    getLLVMStyleWithColumns(20)));
7628   EXPECT_EQ(
7629       "f(\"one two\".split(\n"
7630       "    variable));",
7631       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
7632   EXPECT_EQ("f(\"one two three four five six \"\n"
7633             "  \"seven\".split(\n"
7634             "      really_looooong_variable));",
7635             format("f(\"one two three four five six seven\"."
7636                    "split(really_looooong_variable));",
7637                    getLLVMStyleWithColumns(33)));
7638 
7639   EXPECT_EQ("f(\"some \"\n"
7640             "  \"text\",\n"
7641             "  other);",
7642             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
7643 
7644   // Only break as a last resort.
7645   verifyFormat(
7646       "aaaaaaaaaaaaaaaaaaaa(\n"
7647       "    aaaaaaaaaaaaaaaaaaaa,\n"
7648       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
7649 
7650   EXPECT_EQ("\"splitmea\"\n"
7651             "\"trandomp\"\n"
7652             "\"oint\"",
7653             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
7654 
7655   EXPECT_EQ("\"split/\"\n"
7656             "\"pathat/\"\n"
7657             "\"slashes\"",
7658             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7659 
7660   EXPECT_EQ("\"split/\"\n"
7661             "\"pathat/\"\n"
7662             "\"slashes\"",
7663             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7664   EXPECT_EQ("\"split at \"\n"
7665             "\"spaces/at/\"\n"
7666             "\"slashes.at.any$\"\n"
7667             "\"non-alphanumeric%\"\n"
7668             "\"1111111111characte\"\n"
7669             "\"rs\"",
7670             format("\"split at "
7671                    "spaces/at/"
7672                    "slashes.at."
7673                    "any$non-"
7674                    "alphanumeric%"
7675                    "1111111111characte"
7676                    "rs\"",
7677                    getLLVMStyleWithColumns(20)));
7678 
7679   // Verify that splitting the strings understands
7680   // Style::AlwaysBreakBeforeMultilineStrings.
7681   EXPECT_EQ(
7682       "aaaaaaaaaaaa(\n"
7683       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
7684       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
7685       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
7686              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7687              "aaaaaaaaaaaaaaaaaaaaaa\");",
7688              getGoogleStyle()));
7689   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7690             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
7691             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
7692                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7693                    "aaaaaaaaaaaaaaaaaaaaaa\";",
7694                    getGoogleStyle()));
7695   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7696             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7697             format("llvm::outs() << "
7698                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
7699                    "aaaaaaaaaaaaaaaaaaa\";"));
7700   EXPECT_EQ("ffff(\n"
7701             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7702             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7703             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
7704                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7705                    getGoogleStyle()));
7706 
7707   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
7708   AlignLeft.AlignEscapedNewlinesLeft = true;
7709   EXPECT_EQ("#define A \\\n"
7710             "  \"some \" \\\n"
7711             "  \"text \" \\\n"
7712             "  \"other\";",
7713             format("#define A \"some text other\";", AlignLeft));
7714 }
7715 
7716 TEST_F(FormatTest, FullyRemoveEmptyLines) {
7717   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
7718   NoEmptyLines.MaxEmptyLinesToKeep = 0;
7719   EXPECT_EQ("int i = a(b());",
7720             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
7721 }
7722 
7723 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
7724   EXPECT_EQ(
7725       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7726       "(\n"
7727       "    \"x\t\");",
7728       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7729              "aaaaaaa("
7730              "\"x\t\");"));
7731 }
7732 
7733 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
7734   EXPECT_EQ(
7735       "u8\"utf8 string \"\n"
7736       "u8\"literal\";",
7737       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
7738   EXPECT_EQ(
7739       "u\"utf16 string \"\n"
7740       "u\"literal\";",
7741       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
7742   EXPECT_EQ(
7743       "U\"utf32 string \"\n"
7744       "U\"literal\";",
7745       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
7746   EXPECT_EQ("L\"wide string \"\n"
7747             "L\"literal\";",
7748             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
7749   EXPECT_EQ("@\"NSString \"\n"
7750             "@\"literal\";",
7751             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
7752 
7753   // This input makes clang-format try to split the incomplete unicode escape
7754   // sequence, which used to lead to a crasher.
7755   verifyNoCrash(
7756       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
7757       getLLVMStyleWithColumns(60));
7758 }
7759 
7760 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
7761   FormatStyle Style = getGoogleStyleWithColumns(15);
7762   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
7763   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
7764   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
7765   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
7766   EXPECT_EQ("u8R\"x(raw literal)x\";",
7767             format("u8R\"x(raw literal)x\";", Style));
7768 }
7769 
7770 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
7771   FormatStyle Style = getLLVMStyleWithColumns(20);
7772   EXPECT_EQ(
7773       "_T(\"aaaaaaaaaaaaaa\")\n"
7774       "_T(\"aaaaaaaaaaaaaa\")\n"
7775       "_T(\"aaaaaaaaaaaa\")",
7776       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
7777   EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
7778             "     _T(\"aaaaaa\"),\n"
7779             "  z);",
7780             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
7781 
7782   // FIXME: Handle embedded spaces in one iteration.
7783   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
7784   //            "_T(\"aaaaaaaaaaaaa\")\n"
7785   //            "_T(\"aaaaaaaaaaaaa\")\n"
7786   //            "_T(\"a\")",
7787   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7788   //                   getLLVMStyleWithColumns(20)));
7789   EXPECT_EQ(
7790       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7791       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
7792   EXPECT_EQ("f(\n"
7793             "#if !TEST\n"
7794             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7795             "#endif\n"
7796             "    );",
7797             format("f(\n"
7798                    "#if !TEST\n"
7799                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7800                    "#endif\n"
7801                    ");"));
7802   EXPECT_EQ("f(\n"
7803             "\n"
7804             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
7805             format("f(\n"
7806                    "\n"
7807                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
7808 }
7809 
7810 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
7811   EXPECT_EQ(
7812       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7813       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7814       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7815       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7816              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7817              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
7818 }
7819 
7820 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
7821   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
7822             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
7823   EXPECT_EQ("fffffffffff(g(R\"x(\n"
7824             "multiline raw string literal xxxxxxxxxxxxxx\n"
7825             ")x\",\n"
7826             "              a),\n"
7827             "            b);",
7828             format("fffffffffff(g(R\"x(\n"
7829                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7830                    ")x\", a), b);",
7831                    getGoogleStyleWithColumns(20)));
7832   EXPECT_EQ("fffffffffff(\n"
7833             "    g(R\"x(qqq\n"
7834             "multiline raw string literal xxxxxxxxxxxxxx\n"
7835             ")x\",\n"
7836             "      a),\n"
7837             "    b);",
7838             format("fffffffffff(g(R\"x(qqq\n"
7839                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7840                    ")x\", a), b);",
7841                    getGoogleStyleWithColumns(20)));
7842 
7843   EXPECT_EQ("fffffffffff(R\"x(\n"
7844             "multiline raw string literal xxxxxxxxxxxxxx\n"
7845             ")x\");",
7846             format("fffffffffff(R\"x(\n"
7847                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7848                    ")x\");",
7849                    getGoogleStyleWithColumns(20)));
7850   EXPECT_EQ("fffffffffff(R\"x(\n"
7851             "multiline raw string literal xxxxxxxxxxxxxx\n"
7852             ")x\" + bbbbbb);",
7853             format("fffffffffff(R\"x(\n"
7854                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7855                    ")x\" +   bbbbbb);",
7856                    getGoogleStyleWithColumns(20)));
7857   EXPECT_EQ("fffffffffff(\n"
7858             "    R\"x(\n"
7859             "multiline raw string literal xxxxxxxxxxxxxx\n"
7860             ")x\" +\n"
7861             "    bbbbbb);",
7862             format("fffffffffff(\n"
7863                    " R\"x(\n"
7864                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7865                    ")x\" + bbbbbb);",
7866                    getGoogleStyleWithColumns(20)));
7867 }
7868 
7869 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
7870   verifyFormat("string a = \"unterminated;");
7871   EXPECT_EQ("function(\"unterminated,\n"
7872             "         OtherParameter);",
7873             format("function(  \"unterminated,\n"
7874                    "    OtherParameter);"));
7875 }
7876 
7877 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
7878   FormatStyle Style = getLLVMStyle();
7879   Style.Standard = FormatStyle::LS_Cpp03;
7880   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
7881             format("#define x(_a) printf(\"foo\"_a);", Style));
7882 }
7883 
7884 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
7885 
7886 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
7887   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
7888             "             \"ddeeefff\");",
7889             format("someFunction(\"aaabbbcccdddeeefff\");",
7890                    getLLVMStyleWithColumns(25)));
7891   EXPECT_EQ("someFunction1234567890(\n"
7892             "    \"aaabbbcccdddeeefff\");",
7893             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7894                    getLLVMStyleWithColumns(26)));
7895   EXPECT_EQ("someFunction1234567890(\n"
7896             "    \"aaabbbcccdddeeeff\"\n"
7897             "    \"f\");",
7898             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7899                    getLLVMStyleWithColumns(25)));
7900   EXPECT_EQ("someFunction1234567890(\n"
7901             "    \"aaabbbcccdddeeeff\"\n"
7902             "    \"f\");",
7903             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7904                    getLLVMStyleWithColumns(24)));
7905   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7906             "             \"ddde \"\n"
7907             "             \"efff\");",
7908             format("someFunction(\"aaabbbcc ddde efff\");",
7909                    getLLVMStyleWithColumns(25)));
7910   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
7911             "             \"ddeeefff\");",
7912             format("someFunction(\"aaabbbccc ddeeefff\");",
7913                    getLLVMStyleWithColumns(25)));
7914   EXPECT_EQ("someFunction1234567890(\n"
7915             "    \"aaabb \"\n"
7916             "    \"cccdddeeefff\");",
7917             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
7918                    getLLVMStyleWithColumns(25)));
7919   EXPECT_EQ("#define A          \\\n"
7920             "  string s =       \\\n"
7921             "      \"123456789\"  \\\n"
7922             "      \"0\";         \\\n"
7923             "  int i;",
7924             format("#define A string s = \"1234567890\"; int i;",
7925                    getLLVMStyleWithColumns(20)));
7926   // FIXME: Put additional penalties on breaking at non-whitespace locations.
7927   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7928             "             \"dddeeeff\"\n"
7929             "             \"f\");",
7930             format("someFunction(\"aaabbbcc dddeeefff\");",
7931                    getLLVMStyleWithColumns(25)));
7932 }
7933 
7934 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
7935   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
7936   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
7937   EXPECT_EQ("\"test\"\n"
7938             "\"\\n\"",
7939             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
7940   EXPECT_EQ("\"tes\\\\\"\n"
7941             "\"n\"",
7942             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
7943   EXPECT_EQ("\"\\\\\\\\\"\n"
7944             "\"\\n\"",
7945             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
7946   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
7947   EXPECT_EQ("\"\\uff01\"\n"
7948             "\"test\"",
7949             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
7950   EXPECT_EQ("\"\\Uff01ff02\"",
7951             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
7952   EXPECT_EQ("\"\\x000000000001\"\n"
7953             "\"next\"",
7954             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
7955   EXPECT_EQ("\"\\x000000000001next\"",
7956             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
7957   EXPECT_EQ("\"\\x000000000001\"",
7958             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
7959   EXPECT_EQ("\"test\"\n"
7960             "\"\\000000\"\n"
7961             "\"000001\"",
7962             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
7963   EXPECT_EQ("\"test\\000\"\n"
7964             "\"00000000\"\n"
7965             "\"1\"",
7966             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
7967 }
7968 
7969 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
7970   verifyFormat("void f() {\n"
7971                "  return g() {}\n"
7972                "  void h() {}");
7973   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
7974                "g();\n"
7975                "}");
7976 }
7977 
7978 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
7979   verifyFormat(
7980       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
7981 }
7982 
7983 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
7984   verifyFormat("class X {\n"
7985                "  void f() {\n"
7986                "  }\n"
7987                "};",
7988                getLLVMStyleWithColumns(12));
7989 }
7990 
7991 TEST_F(FormatTest, ConfigurableIndentWidth) {
7992   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
7993   EightIndent.IndentWidth = 8;
7994   EightIndent.ContinuationIndentWidth = 8;
7995   verifyFormat("void f() {\n"
7996                "        someFunction();\n"
7997                "        if (true) {\n"
7998                "                f();\n"
7999                "        }\n"
8000                "}",
8001                EightIndent);
8002   verifyFormat("class X {\n"
8003                "        void f() {\n"
8004                "        }\n"
8005                "};",
8006                EightIndent);
8007   verifyFormat("int x[] = {\n"
8008                "        call(),\n"
8009                "        call()};",
8010                EightIndent);
8011 }
8012 
8013 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
8014   verifyFormat("double\n"
8015                "f();",
8016                getLLVMStyleWithColumns(8));
8017 }
8018 
8019 TEST_F(FormatTest, ConfigurableUseOfTab) {
8020   FormatStyle Tab = getLLVMStyleWithColumns(42);
8021   Tab.IndentWidth = 8;
8022   Tab.UseTab = FormatStyle::UT_Always;
8023   Tab.AlignEscapedNewlinesLeft = true;
8024 
8025   EXPECT_EQ("if (aaaaaaaa && // q\n"
8026             "    bb)\t\t// w\n"
8027             "\t;",
8028             format("if (aaaaaaaa &&// q\n"
8029                    "bb)// w\n"
8030                    ";",
8031                    Tab));
8032   EXPECT_EQ("if (aaa && bbb) // w\n"
8033             "\t;",
8034             format("if(aaa&&bbb)// w\n"
8035                    ";",
8036                    Tab));
8037 
8038   verifyFormat("class X {\n"
8039                "\tvoid f() {\n"
8040                "\t\tsomeFunction(parameter1,\n"
8041                "\t\t\t     parameter2);\n"
8042                "\t}\n"
8043                "};",
8044                Tab);
8045   verifyFormat("#define A                        \\\n"
8046                "\tvoid f() {               \\\n"
8047                "\t\tsomeFunction(    \\\n"
8048                "\t\t    parameter1,  \\\n"
8049                "\t\t    parameter2); \\\n"
8050                "\t}",
8051                Tab);
8052 
8053   Tab.TabWidth = 4;
8054   Tab.IndentWidth = 8;
8055   verifyFormat("class TabWidth4Indent8 {\n"
8056                "\t\tvoid f() {\n"
8057                "\t\t\t\tsomeFunction(parameter1,\n"
8058                "\t\t\t\t\t\t\t parameter2);\n"
8059                "\t\t}\n"
8060                "};",
8061                Tab);
8062 
8063   Tab.TabWidth = 4;
8064   Tab.IndentWidth = 4;
8065   verifyFormat("class TabWidth4Indent4 {\n"
8066                "\tvoid f() {\n"
8067                "\t\tsomeFunction(parameter1,\n"
8068                "\t\t\t\t\t parameter2);\n"
8069                "\t}\n"
8070                "};",
8071                Tab);
8072 
8073   Tab.TabWidth = 8;
8074   Tab.IndentWidth = 4;
8075   verifyFormat("class TabWidth8Indent4 {\n"
8076                "    void f() {\n"
8077                "\tsomeFunction(parameter1,\n"
8078                "\t\t     parameter2);\n"
8079                "    }\n"
8080                "};",
8081                Tab);
8082 
8083   Tab.TabWidth = 8;
8084   Tab.IndentWidth = 8;
8085   EXPECT_EQ("/*\n"
8086             "\t      a\t\tcomment\n"
8087             "\t      in multiple lines\n"
8088             "       */",
8089             format("   /*\t \t \n"
8090                    " \t \t a\t\tcomment\t \t\n"
8091                    " \t \t in multiple lines\t\n"
8092                    " \t  */",
8093                    Tab));
8094 
8095   Tab.UseTab = FormatStyle::UT_ForIndentation;
8096   verifyFormat("{\n"
8097                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8098                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8099                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8100                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8101                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8102                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8103                "};",
8104                Tab);
8105   verifyFormat("enum A {\n"
8106                "\ta1, // Force multiple lines\n"
8107                "\ta2,\n"
8108                "\ta3\n"
8109                "};",
8110                Tab);
8111   EXPECT_EQ("if (aaaaaaaa && // q\n"
8112             "    bb)         // w\n"
8113             "\t;",
8114             format("if (aaaaaaaa &&// q\n"
8115                    "bb)// w\n"
8116                    ";",
8117                    Tab));
8118   verifyFormat("class X {\n"
8119                "\tvoid f() {\n"
8120                "\t\tsomeFunction(parameter1,\n"
8121                "\t\t             parameter2);\n"
8122                "\t}\n"
8123                "};",
8124                Tab);
8125   verifyFormat("{\n"
8126                "\tQ(\n"
8127                "\t    {\n"
8128                "\t\t    int a;\n"
8129                "\t\t    someFunction(aaaaaaaa,\n"
8130                "\t\t                 bbbbbbb);\n"
8131                "\t    },\n"
8132                "\t    p);\n"
8133                "}",
8134                Tab);
8135   EXPECT_EQ("{\n"
8136             "\t/* aaaa\n"
8137             "\t   bbbb */\n"
8138             "}",
8139             format("{\n"
8140                    "/* aaaa\n"
8141                    "   bbbb */\n"
8142                    "}",
8143                    Tab));
8144   EXPECT_EQ("{\n"
8145             "\t/*\n"
8146             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8147             "\t  bbbbbbbbbbbbb\n"
8148             "\t*/\n"
8149             "}",
8150             format("{\n"
8151                    "/*\n"
8152                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8153                    "*/\n"
8154                    "}",
8155                    Tab));
8156   EXPECT_EQ("{\n"
8157             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8158             "\t// bbbbbbbbbbbbb\n"
8159             "}",
8160             format("{\n"
8161                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8162                    "}",
8163                    Tab));
8164   EXPECT_EQ("{\n"
8165             "\t/*\n"
8166             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8167             "\t  bbbbbbbbbbbbb\n"
8168             "\t*/\n"
8169             "}",
8170             format("{\n"
8171                    "\t/*\n"
8172                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8173                    "\t*/\n"
8174                    "}",
8175                    Tab));
8176   EXPECT_EQ("{\n"
8177             "\t/*\n"
8178             "\n"
8179             "\t*/\n"
8180             "}",
8181             format("{\n"
8182                    "\t/*\n"
8183                    "\n"
8184                    "\t*/\n"
8185                    "}",
8186                    Tab));
8187   EXPECT_EQ("{\n"
8188             "\t/*\n"
8189             " asdf\n"
8190             "\t*/\n"
8191             "}",
8192             format("{\n"
8193                    "\t/*\n"
8194                    " asdf\n"
8195                    "\t*/\n"
8196                    "}",
8197                    Tab));
8198 
8199   Tab.UseTab = FormatStyle::UT_Never;
8200   EXPECT_EQ("/*\n"
8201             "              a\t\tcomment\n"
8202             "              in multiple lines\n"
8203             "       */",
8204             format("   /*\t \t \n"
8205                    " \t \t a\t\tcomment\t \t\n"
8206                    " \t \t in multiple lines\t\n"
8207                    " \t  */",
8208                    Tab));
8209   EXPECT_EQ("/* some\n"
8210             "   comment */",
8211             format(" \t \t /* some\n"
8212                    " \t \t    comment */",
8213                    Tab));
8214   EXPECT_EQ("int a; /* some\n"
8215             "   comment */",
8216             format(" \t \t int a; /* some\n"
8217                    " \t \t    comment */",
8218                    Tab));
8219 
8220   EXPECT_EQ("int a; /* some\n"
8221             "comment */",
8222             format(" \t \t int\ta; /* some\n"
8223                    " \t \t    comment */",
8224                    Tab));
8225   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8226             "    comment */",
8227             format(" \t \t f(\"\t\t\"); /* some\n"
8228                    " \t \t    comment */",
8229                    Tab));
8230   EXPECT_EQ("{\n"
8231             "  /*\n"
8232             "   * Comment\n"
8233             "   */\n"
8234             "  int i;\n"
8235             "}",
8236             format("{\n"
8237                    "\t/*\n"
8238                    "\t * Comment\n"
8239                    "\t */\n"
8240                    "\t int i;\n"
8241                    "}"));
8242 }
8243 
8244 TEST_F(FormatTest, CalculatesOriginalColumn) {
8245   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8246             "q\"; /* some\n"
8247             "       comment */",
8248             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8249                    "q\"; /* some\n"
8250                    "       comment */",
8251                    getLLVMStyle()));
8252   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8253             "/* some\n"
8254             "   comment */",
8255             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8256                    " /* some\n"
8257                    "    comment */",
8258                    getLLVMStyle()));
8259   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8260             "qqq\n"
8261             "/* some\n"
8262             "   comment */",
8263             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8264                    "qqq\n"
8265                    " /* some\n"
8266                    "    comment */",
8267                    getLLVMStyle()));
8268   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8269             "wwww; /* some\n"
8270             "         comment */",
8271             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8272                    "wwww; /* some\n"
8273                    "         comment */",
8274                    getLLVMStyle()));
8275 }
8276 
8277 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
8278   FormatStyle NoSpace = getLLVMStyle();
8279   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
8280 
8281   verifyFormat("while(true)\n"
8282                "  continue;",
8283                NoSpace);
8284   verifyFormat("for(;;)\n"
8285                "  continue;",
8286                NoSpace);
8287   verifyFormat("if(true)\n"
8288                "  f();\n"
8289                "else if(true)\n"
8290                "  f();",
8291                NoSpace);
8292   verifyFormat("do {\n"
8293                "  do_something();\n"
8294                "} while(something());",
8295                NoSpace);
8296   verifyFormat("switch(x) {\n"
8297                "default:\n"
8298                "  break;\n"
8299                "}",
8300                NoSpace);
8301   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
8302   verifyFormat("size_t x = sizeof(x);", NoSpace);
8303   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
8304   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
8305   verifyFormat("alignas(128) char a[128];", NoSpace);
8306   verifyFormat("size_t x = alignof(MyType);", NoSpace);
8307   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
8308   verifyFormat("int f() throw(Deprecated);", NoSpace);
8309   verifyFormat("typedef void (*cb)(int);", NoSpace);
8310   verifyFormat("T A::operator()();", NoSpace);
8311   verifyFormat("X A::operator++(T);", NoSpace);
8312 
8313   FormatStyle Space = getLLVMStyle();
8314   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
8315 
8316   verifyFormat("int f ();", Space);
8317   verifyFormat("void f (int a, T b) {\n"
8318                "  while (true)\n"
8319                "    continue;\n"
8320                "}",
8321                Space);
8322   verifyFormat("if (true)\n"
8323                "  f ();\n"
8324                "else if (true)\n"
8325                "  f ();",
8326                Space);
8327   verifyFormat("do {\n"
8328                "  do_something ();\n"
8329                "} while (something ());",
8330                Space);
8331   verifyFormat("switch (x) {\n"
8332                "default:\n"
8333                "  break;\n"
8334                "}",
8335                Space);
8336   verifyFormat("A::A () : a (1) {}", Space);
8337   verifyFormat("void f () __attribute__ ((asdf));", Space);
8338   verifyFormat("*(&a + 1);\n"
8339                "&((&a)[1]);\n"
8340                "a[(b + c) * d];\n"
8341                "(((a + 1) * 2) + 3) * 4;",
8342                Space);
8343   verifyFormat("#define A(x) x", Space);
8344   verifyFormat("#define A (x) x", Space);
8345   verifyFormat("#if defined(x)\n"
8346                "#endif",
8347                Space);
8348   verifyFormat("auto i = std::make_unique<int> (5);", Space);
8349   verifyFormat("size_t x = sizeof (x);", Space);
8350   verifyFormat("auto f (int x) -> decltype (x);", Space);
8351   verifyFormat("int f (T x) noexcept (x.create ());", Space);
8352   verifyFormat("alignas (128) char a[128];", Space);
8353   verifyFormat("size_t x = alignof (MyType);", Space);
8354   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
8355   verifyFormat("int f () throw (Deprecated);", Space);
8356   verifyFormat("typedef void (*cb) (int);", Space);
8357   verifyFormat("T A::operator() ();", Space);
8358   verifyFormat("X A::operator++ (T);", Space);
8359 }
8360 
8361 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
8362   FormatStyle Spaces = getLLVMStyle();
8363 
8364   Spaces.SpacesInParentheses = true;
8365   verifyFormat("call( x, y, z );", Spaces);
8366   verifyFormat("call();", Spaces);
8367   verifyFormat("std::function<void( int, int )> callback;", Spaces);
8368   verifyFormat("while ( (bool)1 )\n"
8369                "  continue;",
8370                Spaces);
8371   verifyFormat("for ( ;; )\n"
8372                "  continue;",
8373                Spaces);
8374   verifyFormat("if ( true )\n"
8375                "  f();\n"
8376                "else if ( true )\n"
8377                "  f();",
8378                Spaces);
8379   verifyFormat("do {\n"
8380                "  do_something( (int)i );\n"
8381                "} while ( something() );",
8382                Spaces);
8383   verifyFormat("switch ( x ) {\n"
8384                "default:\n"
8385                "  break;\n"
8386                "}",
8387                Spaces);
8388 
8389   Spaces.SpacesInParentheses = false;
8390   Spaces.SpacesInCStyleCastParentheses = true;
8391   verifyFormat("Type *A = ( Type * )P;", Spaces);
8392   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
8393   verifyFormat("x = ( int32 )y;", Spaces);
8394   verifyFormat("int a = ( int )(2.0f);", Spaces);
8395   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
8396   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
8397   verifyFormat("#define x (( int )-1)", Spaces);
8398 
8399   // Run the first set of tests again with:
8400   Spaces.SpacesInParentheses = false, Spaces.SpaceInEmptyParentheses = true;
8401   Spaces.SpacesInCStyleCastParentheses = true;
8402   verifyFormat("call(x, y, z);", Spaces);
8403   verifyFormat("call( );", Spaces);
8404   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8405   verifyFormat("while (( bool )1)\n"
8406                "  continue;",
8407                Spaces);
8408   verifyFormat("for (;;)\n"
8409                "  continue;",
8410                Spaces);
8411   verifyFormat("if (true)\n"
8412                "  f( );\n"
8413                "else if (true)\n"
8414                "  f( );",
8415                Spaces);
8416   verifyFormat("do {\n"
8417                "  do_something(( int )i);\n"
8418                "} while (something( ));",
8419                Spaces);
8420   verifyFormat("switch (x) {\n"
8421                "default:\n"
8422                "  break;\n"
8423                "}",
8424                Spaces);
8425 
8426   // Run the first set of tests again with:
8427   Spaces.SpaceAfterCStyleCast = true;
8428   verifyFormat("call(x, y, z);", Spaces);
8429   verifyFormat("call( );", Spaces);
8430   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8431   verifyFormat("while (( bool ) 1)\n"
8432                "  continue;",
8433                Spaces);
8434   verifyFormat("for (;;)\n"
8435                "  continue;",
8436                Spaces);
8437   verifyFormat("if (true)\n"
8438                "  f( );\n"
8439                "else if (true)\n"
8440                "  f( );",
8441                Spaces);
8442   verifyFormat("do {\n"
8443                "  do_something(( int ) i);\n"
8444                "} while (something( ));",
8445                Spaces);
8446   verifyFormat("switch (x) {\n"
8447                "default:\n"
8448                "  break;\n"
8449                "}",
8450                Spaces);
8451 
8452   // Run subset of tests again with:
8453   Spaces.SpacesInCStyleCastParentheses = false;
8454   Spaces.SpaceAfterCStyleCast = true;
8455   verifyFormat("while ((bool) 1)\n"
8456                "  continue;",
8457                Spaces);
8458   verifyFormat("do {\n"
8459                "  do_something((int) i);\n"
8460                "} while (something( ));",
8461                Spaces);
8462 }
8463 
8464 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
8465   verifyFormat("int a[5];");
8466   verifyFormat("a[3] += 42;");
8467 
8468   FormatStyle Spaces = getLLVMStyle();
8469   Spaces.SpacesInSquareBrackets = true;
8470   // Lambdas unchanged.
8471   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
8472   verifyFormat("return [i, args...] {};", Spaces);
8473 
8474   // Not lambdas.
8475   verifyFormat("int a[ 5 ];", Spaces);
8476   verifyFormat("a[ 3 ] += 42;", Spaces);
8477   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
8478   verifyFormat("double &operator[](int i) { return 0; }\n"
8479                "int i;",
8480                Spaces);
8481   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
8482   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
8483   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
8484 }
8485 
8486 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
8487   verifyFormat("int a = 5;");
8488   verifyFormat("a += 42;");
8489   verifyFormat("a or_eq 8;");
8490 
8491   FormatStyle Spaces = getLLVMStyle();
8492   Spaces.SpaceBeforeAssignmentOperators = false;
8493   verifyFormat("int a= 5;", Spaces);
8494   verifyFormat("a+= 42;", Spaces);
8495   verifyFormat("a or_eq 8;", Spaces);
8496 }
8497 
8498 TEST_F(FormatTest, AlignConsecutiveAssignments) {
8499   FormatStyle Alignment = getLLVMStyle();
8500   Alignment.AlignConsecutiveAssignments = false;
8501   verifyFormat("int a = 5;\n"
8502                "int oneTwoThree = 123;",
8503                Alignment);
8504   verifyFormat("int a = 5;\n"
8505                "int oneTwoThree = 123;",
8506                Alignment);
8507 
8508   Alignment.AlignConsecutiveAssignments = true;
8509   verifyFormat("int a           = 5;\n"
8510                "int oneTwoThree = 123;",
8511                Alignment);
8512   verifyFormat("int a           = method();\n"
8513                "int oneTwoThree = 133;",
8514                Alignment);
8515   verifyFormat("a &= 5;\n"
8516                "bcd *= 5;\n"
8517                "ghtyf += 5;\n"
8518                "dvfvdb -= 5;\n"
8519                "a /= 5;\n"
8520                "vdsvsv %= 5;\n"
8521                "sfdbddfbdfbb ^= 5;\n"
8522                "dvsdsv |= 5;\n"
8523                "int dsvvdvsdvvv = 123;",
8524                Alignment);
8525   verifyFormat("int i = 1, j = 10;\n"
8526                "something = 2000;",
8527                Alignment);
8528   verifyFormat("something = 2000;\n"
8529                "int i = 1, j = 10;\n",
8530                Alignment);
8531   verifyFormat("something = 2000;\n"
8532                "another   = 911;\n"
8533                "int i = 1, j = 10;\n"
8534                "oneMore = 1;\n"
8535                "i       = 2;",
8536                Alignment);
8537   verifyFormat("int a   = 5;\n"
8538                "int one = 1;\n"
8539                "method();\n"
8540                "int oneTwoThree = 123;\n"
8541                "int oneTwo      = 12;",
8542                Alignment);
8543   verifyFormat("int oneTwoThree = 123;\n"
8544                "int oneTwo      = 12;\n"
8545                "method();\n",
8546                Alignment);
8547   verifyFormat("int oneTwoThree = 123; // comment\n"
8548                "int oneTwo      = 12;  // comment",
8549                Alignment);
8550   EXPECT_EQ("int a = 5;\n"
8551             "\n"
8552             "int oneTwoThree = 123;",
8553             format("int a       = 5;\n"
8554                    "\n"
8555                    "int oneTwoThree= 123;",
8556                    Alignment));
8557   EXPECT_EQ("int a   = 5;\n"
8558             "int one = 1;\n"
8559             "\n"
8560             "int oneTwoThree = 123;",
8561             format("int a = 5;\n"
8562                    "int one = 1;\n"
8563                    "\n"
8564                    "int oneTwoThree = 123;",
8565                    Alignment));
8566   EXPECT_EQ("int a   = 5;\n"
8567             "int one = 1;\n"
8568             "\n"
8569             "int oneTwoThree = 123;\n"
8570             "int oneTwo      = 12;",
8571             format("int a = 5;\n"
8572                    "int one = 1;\n"
8573                    "\n"
8574                    "int oneTwoThree = 123;\n"
8575                    "int oneTwo = 12;",
8576                    Alignment));
8577   Alignment.AlignEscapedNewlinesLeft = true;
8578   verifyFormat("#define A               \\\n"
8579                "  int aaaa       = 12;  \\\n"
8580                "  int b          = 23;  \\\n"
8581                "  int ccc        = 234; \\\n"
8582                "  int dddddddddd = 2345;",
8583                Alignment);
8584   Alignment.AlignEscapedNewlinesLeft = false;
8585   verifyFormat("#define A                                                      "
8586                "                \\\n"
8587                "  int aaaa       = 12;                                         "
8588                "                \\\n"
8589                "  int b          = 23;                                         "
8590                "                \\\n"
8591                "  int ccc        = 234;                                        "
8592                "                \\\n"
8593                "  int dddddddddd = 2345;",
8594                Alignment);
8595   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
8596                "k = 4, int l = 5,\n"
8597                "                  int m = 6) {\n"
8598                "  int j      = 10;\n"
8599                "  otherThing = 1;\n"
8600                "}",
8601                Alignment);
8602   verifyFormat("void SomeFunction(int parameter = 0) {\n"
8603                "  int i   = 1;\n"
8604                "  int j   = 2;\n"
8605                "  int big = 10000;\n"
8606                "}",
8607                Alignment);
8608   verifyFormat("class C {\n"
8609                "public:\n"
8610                "  int i = 1;\n"
8611                "  virtual void f() = 0;\n"
8612                "};",
8613                Alignment);
8614   verifyFormat("int i = 1;\n"
8615                "if (SomeType t = getSomething()) {\n"
8616                "}\n"
8617                "int j   = 2;\n"
8618                "int big = 10000;",
8619                Alignment);
8620   verifyFormat("int j = 7;\n"
8621                "for (int k = 0; k < N; ++k) {\n"
8622                "}\n"
8623                "int j   = 2;\n"
8624                "int big = 10000;\n"
8625                "}",
8626                Alignment);
8627   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8628   verifyFormat("int i = 1;\n"
8629                "LooooooooooongType loooooooooooooooooooooongVariable\n"
8630                "    = someLooooooooooooooooongFunction();\n"
8631                "int j = 2;",
8632                Alignment);
8633   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8634   verifyFormat("int i = 1;\n"
8635                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
8636                "    someLooooooooooooooooongFunction();\n"
8637                "int j = 2;",
8638                Alignment);
8639   // FIXME: Should align all three assignments
8640   verifyFormat(
8641       "int i      = 1;\n"
8642       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
8643       "                          loooooooooooooooooooooongParameterB);\n"
8644       "int j = 2;",
8645       Alignment);
8646 }
8647 
8648 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
8649   FormatStyle Alignment = getLLVMStyle();
8650   Alignment.AlignConsecutiveDeclarations = false;
8651   verifyFormat("float const a = 5;\n"
8652                "int oneTwoThree = 123;",
8653                Alignment);
8654   verifyFormat("int a = 5;\n"
8655                "float const oneTwoThree = 123;",
8656                Alignment);
8657 
8658   Alignment.AlignConsecutiveDeclarations = true;
8659   verifyFormat("float const a = 5;\n"
8660                "int         oneTwoThree = 123;",
8661                Alignment);
8662   verifyFormat("int         a = method();\n"
8663                "float const oneTwoThree = 133;",
8664                Alignment);
8665   verifyFormat("int i = 1, j = 10;\n"
8666                "something = 2000;",
8667                Alignment);
8668   verifyFormat("something = 2000;\n"
8669                "int i = 1, j = 10;\n",
8670                Alignment);
8671   verifyFormat("float      something = 2000;\n"
8672                "double     another = 911;\n"
8673                "int        i = 1, j = 10;\n"
8674                "const int *oneMore = 1;\n"
8675                "unsigned   i = 2;",
8676                Alignment);
8677   verifyFormat("float a = 5;\n"
8678                "int   one = 1;\n"
8679                "method();\n"
8680                "const double       oneTwoThree = 123;\n"
8681                "const unsigned int oneTwo = 12;",
8682                Alignment);
8683   verifyFormat("int      oneTwoThree{0}; // comment\n"
8684                "unsigned oneTwo;         // comment",
8685                Alignment);
8686   EXPECT_EQ("float const a = 5;\n"
8687             "\n"
8688             "int oneTwoThree = 123;",
8689             format("float const   a = 5;\n"
8690                    "\n"
8691                    "int           oneTwoThree= 123;",
8692                    Alignment));
8693   EXPECT_EQ("float a = 5;\n"
8694             "int   one = 1;\n"
8695             "\n"
8696             "unsigned oneTwoThree = 123;",
8697             format("float    a = 5;\n"
8698                    "int      one = 1;\n"
8699                    "\n"
8700                    "unsigned oneTwoThree = 123;",
8701                    Alignment));
8702   EXPECT_EQ("float a = 5;\n"
8703             "int   one = 1;\n"
8704             "\n"
8705             "unsigned oneTwoThree = 123;\n"
8706             "int      oneTwo = 12;",
8707             format("float    a = 5;\n"
8708                    "int one = 1;\n"
8709                    "\n"
8710                    "unsigned oneTwoThree = 123;\n"
8711                    "int oneTwo = 12;",
8712                    Alignment));
8713   Alignment.AlignConsecutiveAssignments = true;
8714   verifyFormat("float      something = 2000;\n"
8715                "double     another   = 911;\n"
8716                "int        i = 1, j = 10;\n"
8717                "const int *oneMore = 1;\n"
8718                "unsigned   i       = 2;",
8719                Alignment);
8720   verifyFormat("int      oneTwoThree = {0}; // comment\n"
8721                "unsigned oneTwo      = 0;   // comment",
8722                Alignment);
8723   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
8724             "  int const i   = 1;\n"
8725             "  int *     j   = 2;\n"
8726             "  int       big = 10000;\n"
8727             "\n"
8728             "  unsigned oneTwoThree = 123;\n"
8729             "  int      oneTwo      = 12;\n"
8730             "  method();\n"
8731             "  float k  = 2;\n"
8732             "  int   ll = 10000;\n"
8733             "}",
8734             format("void SomeFunction(int parameter= 0) {\n"
8735                    " int const  i= 1;\n"
8736                    "  int *j=2;\n"
8737                    " int big  =  10000;\n"
8738                    "\n"
8739                    "unsigned oneTwoThree  =123;\n"
8740                    "int oneTwo = 12;\n"
8741                    "  method();\n"
8742                    "float k= 2;\n"
8743                    "int ll=10000;\n"
8744                    "}",
8745                    Alignment));
8746   Alignment.AlignConsecutiveAssignments = false;
8747   Alignment.AlignEscapedNewlinesLeft = true;
8748   verifyFormat("#define A              \\\n"
8749                "  int       aaaa = 12; \\\n"
8750                "  float     b = 23;    \\\n"
8751                "  const int ccc = 234; \\\n"
8752                "  unsigned  dddddddddd = 2345;",
8753                Alignment);
8754   Alignment.AlignEscapedNewlinesLeft = false;
8755   Alignment.ColumnLimit = 30;
8756   verifyFormat("#define A                    \\\n"
8757                "  int       aaaa = 12;       \\\n"
8758                "  float     b = 23;          \\\n"
8759                "  const int ccc = 234;       \\\n"
8760                "  int       dddddddddd = 2345;",
8761                Alignment);
8762   Alignment.ColumnLimit = 80;
8763   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
8764                "k = 4, int l = 5,\n"
8765                "                  int m = 6) {\n"
8766                "  const int j = 10;\n"
8767                "  otherThing = 1;\n"
8768                "}",
8769                Alignment);
8770   verifyFormat("void SomeFunction(int parameter = 0) {\n"
8771                "  int const i = 1;\n"
8772                "  int *     j = 2;\n"
8773                "  int       big = 10000;\n"
8774                "}",
8775                Alignment);
8776   verifyFormat("class C {\n"
8777                "public:\n"
8778                "  int          i = 1;\n"
8779                "  virtual void f() = 0;\n"
8780                "};",
8781                Alignment);
8782   verifyFormat("float i = 1;\n"
8783                "if (SomeType t = getSomething()) {\n"
8784                "}\n"
8785                "const unsigned j = 2;\n"
8786                "int            big = 10000;",
8787                Alignment);
8788   verifyFormat("float j = 7;\n"
8789                "for (int k = 0; k < N; ++k) {\n"
8790                "}\n"
8791                "unsigned j = 2;\n"
8792                "int      big = 10000;\n"
8793                "}",
8794                Alignment);
8795   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8796   verifyFormat("float              i = 1;\n"
8797                "LooooooooooongType loooooooooooooooooooooongVariable\n"
8798                "    = someLooooooooooooooooongFunction();\n"
8799                "int j = 2;",
8800                Alignment);
8801   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8802   verifyFormat("int                i = 1;\n"
8803                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
8804                "    someLooooooooooooooooongFunction();\n"
8805                "int j = 2;",
8806                Alignment);
8807   // FIXME: Should align all three declarations
8808   verifyFormat(
8809       "int      i = 1;\n"
8810       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
8811       "                          loooooooooooooooooooooongParameterB);\n"
8812       "int j = 2;",
8813       Alignment);
8814 
8815   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
8816   // We expect declarations and assignments to align, as long as it doesn't
8817   // exceed the column limit, starting a new alignemnt sequence whenever it
8818   // happens.
8819   Alignment.AlignConsecutiveAssignments = true;
8820   Alignment.ColumnLimit = 30;
8821   verifyFormat("float    ii              = 1;\n"
8822                "unsigned j               = 2;\n"
8823                "int someVerylongVariable = 1;\n"
8824                "AnotherLongType  ll = 123456;\n"
8825                "VeryVeryLongType k  = 2;\n"
8826                "int              myvar = 1;",
8827                Alignment);
8828   Alignment.ColumnLimit = 80;
8829 }
8830 
8831 TEST_F(FormatTest, LinuxBraceBreaking) {
8832   FormatStyle LinuxBraceStyle = getLLVMStyle();
8833   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
8834   verifyFormat("namespace a\n"
8835                "{\n"
8836                "class A\n"
8837                "{\n"
8838                "  void f()\n"
8839                "  {\n"
8840                "    if (true) {\n"
8841                "      a();\n"
8842                "      b();\n"
8843                "    }\n"
8844                "  }\n"
8845                "  void g() { return; }\n"
8846                "};\n"
8847                "struct B {\n"
8848                "  int x;\n"
8849                "};\n"
8850                "}\n",
8851                LinuxBraceStyle);
8852   verifyFormat("enum X {\n"
8853                "  Y = 0,\n"
8854                "}\n",
8855                LinuxBraceStyle);
8856   verifyFormat("struct S {\n"
8857                "  int Type;\n"
8858                "  union {\n"
8859                "    int x;\n"
8860                "    double y;\n"
8861                "  } Value;\n"
8862                "  class C\n"
8863                "  {\n"
8864                "    MyFavoriteType Value;\n"
8865                "  } Class;\n"
8866                "}\n",
8867                LinuxBraceStyle);
8868 }
8869 
8870 TEST_F(FormatTest, MozillaBraceBreaking) {
8871   FormatStyle MozillaBraceStyle = getLLVMStyle();
8872   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
8873   verifyFormat("namespace a {\n"
8874                "class A\n"
8875                "{\n"
8876                "  void f()\n"
8877                "  {\n"
8878                "    if (true) {\n"
8879                "      a();\n"
8880                "      b();\n"
8881                "    }\n"
8882                "  }\n"
8883                "  void g() { return; }\n"
8884                "};\n"
8885                "enum E\n"
8886                "{\n"
8887                "  A,\n"
8888                "  // foo\n"
8889                "  B,\n"
8890                "  C\n"
8891                "};\n"
8892                "struct B\n"
8893                "{\n"
8894                "  int x;\n"
8895                "};\n"
8896                "}\n",
8897                MozillaBraceStyle);
8898   verifyFormat("struct S\n"
8899                "{\n"
8900                "  int Type;\n"
8901                "  union\n"
8902                "  {\n"
8903                "    int x;\n"
8904                "    double y;\n"
8905                "  } Value;\n"
8906                "  class C\n"
8907                "  {\n"
8908                "    MyFavoriteType Value;\n"
8909                "  } Class;\n"
8910                "}\n",
8911                MozillaBraceStyle);
8912 }
8913 
8914 TEST_F(FormatTest, StroustrupBraceBreaking) {
8915   FormatStyle StroustrupBraceStyle = getLLVMStyle();
8916   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8917   verifyFormat("namespace a {\n"
8918                "class A {\n"
8919                "  void f()\n"
8920                "  {\n"
8921                "    if (true) {\n"
8922                "      a();\n"
8923                "      b();\n"
8924                "    }\n"
8925                "  }\n"
8926                "  void g() { return; }\n"
8927                "};\n"
8928                "struct B {\n"
8929                "  int x;\n"
8930                "};\n"
8931                "}\n",
8932                StroustrupBraceStyle);
8933 
8934   verifyFormat("void foo()\n"
8935                "{\n"
8936                "  if (a) {\n"
8937                "    a();\n"
8938                "  }\n"
8939                "  else {\n"
8940                "    b();\n"
8941                "  }\n"
8942                "}\n",
8943                StroustrupBraceStyle);
8944 
8945   verifyFormat("#ifdef _DEBUG\n"
8946                "int foo(int i = 0)\n"
8947                "#else\n"
8948                "int foo(int i = 5)\n"
8949                "#endif\n"
8950                "{\n"
8951                "  return i;\n"
8952                "}",
8953                StroustrupBraceStyle);
8954 
8955   verifyFormat("void foo() {}\n"
8956                "void bar()\n"
8957                "#ifdef _DEBUG\n"
8958                "{\n"
8959                "  foo();\n"
8960                "}\n"
8961                "#else\n"
8962                "{\n"
8963                "}\n"
8964                "#endif",
8965                StroustrupBraceStyle);
8966 
8967   verifyFormat("void foobar() { int i = 5; }\n"
8968                "#ifdef _DEBUG\n"
8969                "void bar() {}\n"
8970                "#else\n"
8971                "void bar() { foobar(); }\n"
8972                "#endif",
8973                StroustrupBraceStyle);
8974 }
8975 
8976 TEST_F(FormatTest, AllmanBraceBreaking) {
8977   FormatStyle AllmanBraceStyle = getLLVMStyle();
8978   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
8979   verifyFormat("namespace a\n"
8980                "{\n"
8981                "class A\n"
8982                "{\n"
8983                "  void f()\n"
8984                "  {\n"
8985                "    if (true)\n"
8986                "    {\n"
8987                "      a();\n"
8988                "      b();\n"
8989                "    }\n"
8990                "  }\n"
8991                "  void g() { return; }\n"
8992                "};\n"
8993                "struct B\n"
8994                "{\n"
8995                "  int x;\n"
8996                "};\n"
8997                "}",
8998                AllmanBraceStyle);
8999 
9000   verifyFormat("void f()\n"
9001                "{\n"
9002                "  if (true)\n"
9003                "  {\n"
9004                "    a();\n"
9005                "  }\n"
9006                "  else if (false)\n"
9007                "  {\n"
9008                "    b();\n"
9009                "  }\n"
9010                "  else\n"
9011                "  {\n"
9012                "    c();\n"
9013                "  }\n"
9014                "}\n",
9015                AllmanBraceStyle);
9016 
9017   verifyFormat("void f()\n"
9018                "{\n"
9019                "  for (int i = 0; i < 10; ++i)\n"
9020                "  {\n"
9021                "    a();\n"
9022                "  }\n"
9023                "  while (false)\n"
9024                "  {\n"
9025                "    b();\n"
9026                "  }\n"
9027                "  do\n"
9028                "  {\n"
9029                "    c();\n"
9030                "  } while (false)\n"
9031                "}\n",
9032                AllmanBraceStyle);
9033 
9034   verifyFormat("void f(int a)\n"
9035                "{\n"
9036                "  switch (a)\n"
9037                "  {\n"
9038                "  case 0:\n"
9039                "    break;\n"
9040                "  case 1:\n"
9041                "  {\n"
9042                "    break;\n"
9043                "  }\n"
9044                "  case 2:\n"
9045                "  {\n"
9046                "  }\n"
9047                "  break;\n"
9048                "  default:\n"
9049                "    break;\n"
9050                "  }\n"
9051                "}\n",
9052                AllmanBraceStyle);
9053 
9054   verifyFormat("enum X\n"
9055                "{\n"
9056                "  Y = 0,\n"
9057                "}\n",
9058                AllmanBraceStyle);
9059   verifyFormat("enum X\n"
9060                "{\n"
9061                "  Y = 0\n"
9062                "}\n",
9063                AllmanBraceStyle);
9064 
9065   verifyFormat("@interface BSApplicationController ()\n"
9066                "{\n"
9067                "@private\n"
9068                "  id _extraIvar;\n"
9069                "}\n"
9070                "@end\n",
9071                AllmanBraceStyle);
9072 
9073   verifyFormat("#ifdef _DEBUG\n"
9074                "int foo(int i = 0)\n"
9075                "#else\n"
9076                "int foo(int i = 5)\n"
9077                "#endif\n"
9078                "{\n"
9079                "  return i;\n"
9080                "}",
9081                AllmanBraceStyle);
9082 
9083   verifyFormat("void foo() {}\n"
9084                "void bar()\n"
9085                "#ifdef _DEBUG\n"
9086                "{\n"
9087                "  foo();\n"
9088                "}\n"
9089                "#else\n"
9090                "{\n"
9091                "}\n"
9092                "#endif",
9093                AllmanBraceStyle);
9094 
9095   verifyFormat("void foobar() { int i = 5; }\n"
9096                "#ifdef _DEBUG\n"
9097                "void bar() {}\n"
9098                "#else\n"
9099                "void bar() { foobar(); }\n"
9100                "#endif",
9101                AllmanBraceStyle);
9102 
9103   // This shouldn't affect ObjC blocks..
9104   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
9105                "  // ...\n"
9106                "  int i;\n"
9107                "}];",
9108                AllmanBraceStyle);
9109   verifyFormat("void (^block)(void) = ^{\n"
9110                "  // ...\n"
9111                "  int i;\n"
9112                "};",
9113                AllmanBraceStyle);
9114   // .. or dict literals.
9115   verifyFormat("void f()\n"
9116                "{\n"
9117                "  [object someMethod:@{ @\"a\" : @\"b\" }];\n"
9118                "}",
9119                AllmanBraceStyle);
9120   verifyFormat("int f()\n"
9121                "{ // comment\n"
9122                "  return 42;\n"
9123                "}",
9124                AllmanBraceStyle);
9125 
9126   AllmanBraceStyle.ColumnLimit = 19;
9127   verifyFormat("void f() { int i; }", AllmanBraceStyle);
9128   AllmanBraceStyle.ColumnLimit = 18;
9129   verifyFormat("void f()\n"
9130                "{\n"
9131                "  int i;\n"
9132                "}",
9133                AllmanBraceStyle);
9134   AllmanBraceStyle.ColumnLimit = 80;
9135 
9136   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
9137   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
9138   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
9139   verifyFormat("void f(bool b)\n"
9140                "{\n"
9141                "  if (b)\n"
9142                "  {\n"
9143                "    return;\n"
9144                "  }\n"
9145                "}\n",
9146                BreakBeforeBraceShortIfs);
9147   verifyFormat("void f(bool b)\n"
9148                "{\n"
9149                "  if (b) return;\n"
9150                "}\n",
9151                BreakBeforeBraceShortIfs);
9152   verifyFormat("void f(bool b)\n"
9153                "{\n"
9154                "  while (b)\n"
9155                "  {\n"
9156                "    return;\n"
9157                "  }\n"
9158                "}\n",
9159                BreakBeforeBraceShortIfs);
9160 }
9161 
9162 TEST_F(FormatTest, GNUBraceBreaking) {
9163   FormatStyle GNUBraceStyle = getLLVMStyle();
9164   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
9165   verifyFormat("namespace a\n"
9166                "{\n"
9167                "class A\n"
9168                "{\n"
9169                "  void f()\n"
9170                "  {\n"
9171                "    int a;\n"
9172                "    {\n"
9173                "      int b;\n"
9174                "    }\n"
9175                "    if (true)\n"
9176                "      {\n"
9177                "        a();\n"
9178                "        b();\n"
9179                "      }\n"
9180                "  }\n"
9181                "  void g() { return; }\n"
9182                "}\n"
9183                "}",
9184                GNUBraceStyle);
9185 
9186   verifyFormat("void f()\n"
9187                "{\n"
9188                "  if (true)\n"
9189                "    {\n"
9190                "      a();\n"
9191                "    }\n"
9192                "  else if (false)\n"
9193                "    {\n"
9194                "      b();\n"
9195                "    }\n"
9196                "  else\n"
9197                "    {\n"
9198                "      c();\n"
9199                "    }\n"
9200                "}\n",
9201                GNUBraceStyle);
9202 
9203   verifyFormat("void f()\n"
9204                "{\n"
9205                "  for (int i = 0; i < 10; ++i)\n"
9206                "    {\n"
9207                "      a();\n"
9208                "    }\n"
9209                "  while (false)\n"
9210                "    {\n"
9211                "      b();\n"
9212                "    }\n"
9213                "  do\n"
9214                "    {\n"
9215                "      c();\n"
9216                "    }\n"
9217                "  while (false);\n"
9218                "}\n",
9219                GNUBraceStyle);
9220 
9221   verifyFormat("void f(int a)\n"
9222                "{\n"
9223                "  switch (a)\n"
9224                "    {\n"
9225                "    case 0:\n"
9226                "      break;\n"
9227                "    case 1:\n"
9228                "      {\n"
9229                "        break;\n"
9230                "      }\n"
9231                "    case 2:\n"
9232                "      {\n"
9233                "      }\n"
9234                "      break;\n"
9235                "    default:\n"
9236                "      break;\n"
9237                "    }\n"
9238                "}\n",
9239                GNUBraceStyle);
9240 
9241   verifyFormat("enum X\n"
9242                "{\n"
9243                "  Y = 0,\n"
9244                "}\n",
9245                GNUBraceStyle);
9246 
9247   verifyFormat("@interface BSApplicationController ()\n"
9248                "{\n"
9249                "@private\n"
9250                "  id _extraIvar;\n"
9251                "}\n"
9252                "@end\n",
9253                GNUBraceStyle);
9254 
9255   verifyFormat("#ifdef _DEBUG\n"
9256                "int foo(int i = 0)\n"
9257                "#else\n"
9258                "int foo(int i = 5)\n"
9259                "#endif\n"
9260                "{\n"
9261                "  return i;\n"
9262                "}",
9263                GNUBraceStyle);
9264 
9265   verifyFormat("void foo() {}\n"
9266                "void bar()\n"
9267                "#ifdef _DEBUG\n"
9268                "{\n"
9269                "  foo();\n"
9270                "}\n"
9271                "#else\n"
9272                "{\n"
9273                "}\n"
9274                "#endif",
9275                GNUBraceStyle);
9276 
9277   verifyFormat("void foobar() { int i = 5; }\n"
9278                "#ifdef _DEBUG\n"
9279                "void bar() {}\n"
9280                "#else\n"
9281                "void bar() { foobar(); }\n"
9282                "#endif",
9283                GNUBraceStyle);
9284 }
9285 
9286 TEST_F(FormatTest, WebKitBraceBreaking) {
9287   FormatStyle WebKitBraceStyle = getLLVMStyle();
9288   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
9289   verifyFormat("namespace a {\n"
9290                "class A {\n"
9291                "  void f()\n"
9292                "  {\n"
9293                "    if (true) {\n"
9294                "      a();\n"
9295                "      b();\n"
9296                "    }\n"
9297                "  }\n"
9298                "  void g() { return; }\n"
9299                "};\n"
9300                "enum E {\n"
9301                "  A,\n"
9302                "  // foo\n"
9303                "  B,\n"
9304                "  C\n"
9305                "};\n"
9306                "struct B {\n"
9307                "  int x;\n"
9308                "};\n"
9309                "}\n",
9310                WebKitBraceStyle);
9311   verifyFormat("struct S {\n"
9312                "  int Type;\n"
9313                "  union {\n"
9314                "    int x;\n"
9315                "    double y;\n"
9316                "  } Value;\n"
9317                "  class C {\n"
9318                "    MyFavoriteType Value;\n"
9319                "  } Class;\n"
9320                "};\n",
9321                WebKitBraceStyle);
9322 }
9323 
9324 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
9325   verifyFormat("void f() {\n"
9326                "  try {\n"
9327                "  } catch (const Exception &e) {\n"
9328                "  }\n"
9329                "}\n",
9330                getLLVMStyle());
9331 }
9332 
9333 TEST_F(FormatTest, UnderstandsPragmas) {
9334   verifyFormat("#pragma omp reduction(| : var)");
9335   verifyFormat("#pragma omp reduction(+ : var)");
9336 
9337   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
9338             "(including parentheses).",
9339             format("#pragma    mark   Any non-hyphenated or hyphenated string "
9340                    "(including parentheses)."));
9341 }
9342 
9343 TEST_F(FormatTest, UnderstandPragmaOption) {
9344   verifyFormat("#pragma option -C -A");
9345 
9346   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
9347 }
9348 
9349 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
9350   for (size_t i = 1; i < Styles.size(); ++i)                                   \
9351   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
9352                                   << " differs from Style #0"
9353 
9354 TEST_F(FormatTest, GetsPredefinedStyleByName) {
9355   SmallVector<FormatStyle, 3> Styles;
9356   Styles.resize(3);
9357 
9358   Styles[0] = getLLVMStyle();
9359   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
9360   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
9361   EXPECT_ALL_STYLES_EQUAL(Styles);
9362 
9363   Styles[0] = getGoogleStyle();
9364   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
9365   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
9366   EXPECT_ALL_STYLES_EQUAL(Styles);
9367 
9368   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9369   EXPECT_TRUE(
9370       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
9371   EXPECT_TRUE(
9372       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
9373   EXPECT_ALL_STYLES_EQUAL(Styles);
9374 
9375   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
9376   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
9377   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
9378   EXPECT_ALL_STYLES_EQUAL(Styles);
9379 
9380   Styles[0] = getMozillaStyle();
9381   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
9382   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
9383   EXPECT_ALL_STYLES_EQUAL(Styles);
9384 
9385   Styles[0] = getWebKitStyle();
9386   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
9387   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
9388   EXPECT_ALL_STYLES_EQUAL(Styles);
9389 
9390   Styles[0] = getGNUStyle();
9391   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
9392   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
9393   EXPECT_ALL_STYLES_EQUAL(Styles);
9394 
9395   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
9396 }
9397 
9398 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
9399   SmallVector<FormatStyle, 8> Styles;
9400   Styles.resize(2);
9401 
9402   Styles[0] = getGoogleStyle();
9403   Styles[1] = getLLVMStyle();
9404   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9405   EXPECT_ALL_STYLES_EQUAL(Styles);
9406 
9407   Styles.resize(5);
9408   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9409   Styles[1] = getLLVMStyle();
9410   Styles[1].Language = FormatStyle::LK_JavaScript;
9411   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9412 
9413   Styles[2] = getLLVMStyle();
9414   Styles[2].Language = FormatStyle::LK_JavaScript;
9415   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
9416                                   "BasedOnStyle: Google",
9417                                   &Styles[2])
9418                    .value());
9419 
9420   Styles[3] = getLLVMStyle();
9421   Styles[3].Language = FormatStyle::LK_JavaScript;
9422   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
9423                                   "Language: JavaScript",
9424                                   &Styles[3])
9425                    .value());
9426 
9427   Styles[4] = getLLVMStyle();
9428   Styles[4].Language = FormatStyle::LK_JavaScript;
9429   EXPECT_EQ(0, parseConfiguration("---\n"
9430                                   "BasedOnStyle: LLVM\n"
9431                                   "IndentWidth: 123\n"
9432                                   "---\n"
9433                                   "BasedOnStyle: Google\n"
9434                                   "Language: JavaScript",
9435                                   &Styles[4])
9436                    .value());
9437   EXPECT_ALL_STYLES_EQUAL(Styles);
9438 }
9439 
9440 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
9441   Style.FIELD = false;                                                         \
9442   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
9443   EXPECT_TRUE(Style.FIELD);                                                    \
9444   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
9445   EXPECT_FALSE(Style.FIELD);
9446 
9447 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
9448 
9449 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
9450   Style.STRUCT.FIELD = false;                                                  \
9451   EXPECT_EQ(0,                                                                 \
9452             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
9453                 .value());                                                     \
9454   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
9455   EXPECT_EQ(0,                                                                 \
9456             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
9457                 .value());                                                     \
9458   EXPECT_FALSE(Style.STRUCT.FIELD);
9459 
9460 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
9461   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
9462 
9463 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
9464   EXPECT_NE(VALUE, Style.FIELD);                                               \
9465   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
9466   EXPECT_EQ(VALUE, Style.FIELD)
9467 
9468 TEST_F(FormatTest, ParsesConfigurationBools) {
9469   FormatStyle Style = {};
9470   Style.Language = FormatStyle::LK_Cpp;
9471   CHECK_PARSE_BOOL(AlignAfterOpenBracket);
9472   CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
9473   CHECK_PARSE_BOOL(AlignOperands);
9474   CHECK_PARSE_BOOL(AlignTrailingComments);
9475   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
9476   CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
9477   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
9478   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
9479   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
9480   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
9481   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
9482   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
9483   CHECK_PARSE_BOOL(BinPackParameters);
9484   CHECK_PARSE_BOOL(BinPackArguments);
9485   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
9486   CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
9487   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
9488   CHECK_PARSE_BOOL(DerivePointerAlignment);
9489   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
9490   CHECK_PARSE_BOOL(IndentCaseLabels);
9491   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
9492   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
9493   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
9494   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
9495   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
9496   CHECK_PARSE_BOOL(SpacesInParentheses);
9497   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
9498   CHECK_PARSE_BOOL(SpacesInAngles);
9499   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
9500   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
9501   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
9502   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
9503   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
9504 
9505   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
9506   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
9507   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
9508   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
9509   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
9510   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
9511   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
9512   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
9513   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
9514   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
9515   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
9516 }
9517 
9518 #undef CHECK_PARSE_BOOL
9519 
9520 TEST_F(FormatTest, ParsesConfiguration) {
9521   FormatStyle Style = {};
9522   Style.Language = FormatStyle::LK_Cpp;
9523   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
9524   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
9525               ConstructorInitializerIndentWidth, 1234u);
9526   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
9527   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
9528   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
9529   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
9530               PenaltyBreakBeforeFirstCallParameter, 1234u);
9531   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
9532   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
9533               PenaltyReturnTypeOnItsOwnLine, 1234u);
9534   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
9535               SpacesBeforeTrailingComments, 1234u);
9536   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
9537   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
9538 
9539   Style.PointerAlignment = FormatStyle::PAS_Middle;
9540   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
9541               FormatStyle::PAS_Left);
9542   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
9543               FormatStyle::PAS_Right);
9544   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
9545               FormatStyle::PAS_Middle);
9546   // For backward compatibility:
9547   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
9548               FormatStyle::PAS_Left);
9549   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
9550               FormatStyle::PAS_Right);
9551   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
9552               FormatStyle::PAS_Middle);
9553 
9554   Style.Standard = FormatStyle::LS_Auto;
9555   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
9556   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
9557   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
9558   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
9559   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
9560 
9561   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9562   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
9563               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
9564   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
9565               FormatStyle::BOS_None);
9566   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
9567               FormatStyle::BOS_All);
9568   // For backward compatibility:
9569   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
9570               FormatStyle::BOS_None);
9571   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
9572               FormatStyle::BOS_All);
9573 
9574   Style.UseTab = FormatStyle::UT_ForIndentation;
9575   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
9576   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
9577   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
9578   // For backward compatibility:
9579   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
9580   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
9581 
9582   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
9583   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
9584               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9585   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
9586               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
9587   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
9588               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
9589   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
9590               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9591   // For backward compatibility:
9592   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
9593               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9594   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
9595               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9596 
9597   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
9598   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
9599               FormatStyle::SBPO_Never);
9600   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
9601               FormatStyle::SBPO_Always);
9602   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
9603               FormatStyle::SBPO_ControlStatements);
9604   // For backward compatibility:
9605   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
9606               FormatStyle::SBPO_Never);
9607   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
9608               FormatStyle::SBPO_ControlStatements);
9609 
9610   Style.ColumnLimit = 123;
9611   FormatStyle BaseStyle = getLLVMStyle();
9612   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
9613   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
9614 
9615   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9616   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
9617               FormatStyle::BS_Attach);
9618   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
9619               FormatStyle::BS_Linux);
9620   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
9621               FormatStyle::BS_Mozilla);
9622   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
9623               FormatStyle::BS_Stroustrup);
9624   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
9625               FormatStyle::BS_Allman);
9626   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
9627   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
9628               FormatStyle::BS_WebKit);
9629   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
9630               FormatStyle::BS_Custom);
9631 
9632   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
9633   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
9634               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
9635   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
9636               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
9637   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
9638               AlwaysBreakAfterDefinitionReturnType,
9639               FormatStyle::DRTBS_TopLevel);
9640 
9641   Style.NamespaceIndentation = FormatStyle::NI_All;
9642   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
9643               FormatStyle::NI_None);
9644   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
9645               FormatStyle::NI_Inner);
9646   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
9647               FormatStyle::NI_All);
9648 
9649   Style.ForEachMacros.clear();
9650   std::vector<std::string> BoostForeach;
9651   BoostForeach.push_back("BOOST_FOREACH");
9652   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
9653   std::vector<std::string> BoostAndQForeach;
9654   BoostAndQForeach.push_back("BOOST_FOREACH");
9655   BoostAndQForeach.push_back("Q_FOREACH");
9656   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
9657               BoostAndQForeach);
9658 }
9659 
9660 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
9661   FormatStyle Style = {};
9662   Style.Language = FormatStyle::LK_Cpp;
9663   CHECK_PARSE("Language: Cpp\n"
9664               "IndentWidth: 12",
9665               IndentWidth, 12u);
9666   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
9667                                "IndentWidth: 34",
9668                                &Style),
9669             ParseError::Unsuitable);
9670   EXPECT_EQ(12u, Style.IndentWidth);
9671   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
9672   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9673 
9674   Style.Language = FormatStyle::LK_JavaScript;
9675   CHECK_PARSE("Language: JavaScript\n"
9676               "IndentWidth: 12",
9677               IndentWidth, 12u);
9678   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
9679   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
9680                                "IndentWidth: 34",
9681                                &Style),
9682             ParseError::Unsuitable);
9683   EXPECT_EQ(23u, Style.IndentWidth);
9684   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
9685   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9686 
9687   CHECK_PARSE("BasedOnStyle: LLVM\n"
9688               "IndentWidth: 67",
9689               IndentWidth, 67u);
9690 
9691   CHECK_PARSE("---\n"
9692               "Language: JavaScript\n"
9693               "IndentWidth: 12\n"
9694               "---\n"
9695               "Language: Cpp\n"
9696               "IndentWidth: 34\n"
9697               "...\n",
9698               IndentWidth, 12u);
9699 
9700   Style.Language = FormatStyle::LK_Cpp;
9701   CHECK_PARSE("---\n"
9702               "Language: JavaScript\n"
9703               "IndentWidth: 12\n"
9704               "---\n"
9705               "Language: Cpp\n"
9706               "IndentWidth: 34\n"
9707               "...\n",
9708               IndentWidth, 34u);
9709   CHECK_PARSE("---\n"
9710               "IndentWidth: 78\n"
9711               "---\n"
9712               "Language: JavaScript\n"
9713               "IndentWidth: 56\n"
9714               "...\n",
9715               IndentWidth, 78u);
9716 
9717   Style.ColumnLimit = 123;
9718   Style.IndentWidth = 234;
9719   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
9720   Style.TabWidth = 345;
9721   EXPECT_FALSE(parseConfiguration("---\n"
9722                                   "IndentWidth: 456\n"
9723                                   "BreakBeforeBraces: Allman\n"
9724                                   "---\n"
9725                                   "Language: JavaScript\n"
9726                                   "IndentWidth: 111\n"
9727                                   "TabWidth: 111\n"
9728                                   "---\n"
9729                                   "Language: Cpp\n"
9730                                   "BreakBeforeBraces: Stroustrup\n"
9731                                   "TabWidth: 789\n"
9732                                   "...\n",
9733                                   &Style));
9734   EXPECT_EQ(123u, Style.ColumnLimit);
9735   EXPECT_EQ(456u, Style.IndentWidth);
9736   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
9737   EXPECT_EQ(789u, Style.TabWidth);
9738 
9739   EXPECT_EQ(parseConfiguration("---\n"
9740                                "Language: JavaScript\n"
9741                                "IndentWidth: 56\n"
9742                                "---\n"
9743                                "IndentWidth: 78\n"
9744                                "...\n",
9745                                &Style),
9746             ParseError::Error);
9747   EXPECT_EQ(parseConfiguration("---\n"
9748                                "Language: JavaScript\n"
9749                                "IndentWidth: 56\n"
9750                                "---\n"
9751                                "Language: JavaScript\n"
9752                                "IndentWidth: 78\n"
9753                                "...\n",
9754                                &Style),
9755             ParseError::Error);
9756 
9757   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9758 }
9759 
9760 #undef CHECK_PARSE
9761 
9762 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
9763   FormatStyle Style = {};
9764   Style.Language = FormatStyle::LK_JavaScript;
9765   Style.BreakBeforeTernaryOperators = true;
9766   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
9767   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9768 
9769   Style.BreakBeforeTernaryOperators = true;
9770   EXPECT_EQ(0, parseConfiguration("---\n"
9771                                   "BasedOnStyle: Google\n"
9772                                   "---\n"
9773                                   "Language: JavaScript\n"
9774                                   "IndentWidth: 76\n"
9775                                   "...\n",
9776                                   &Style)
9777                    .value());
9778   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9779   EXPECT_EQ(76u, Style.IndentWidth);
9780   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9781 }
9782 
9783 TEST_F(FormatTest, ConfigurationRoundTripTest) {
9784   FormatStyle Style = getLLVMStyle();
9785   std::string YAML = configurationAsText(Style);
9786   FormatStyle ParsedStyle = {};
9787   ParsedStyle.Language = FormatStyle::LK_Cpp;
9788   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
9789   EXPECT_EQ(Style, ParsedStyle);
9790 }
9791 
9792 TEST_F(FormatTest, WorksFor8bitEncodings) {
9793   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
9794             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
9795             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
9796             "\"\xef\xee\xf0\xf3...\"",
9797             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
9798                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
9799                    "\xef\xee\xf0\xf3...\"",
9800                    getLLVMStyleWithColumns(12)));
9801 }
9802 
9803 TEST_F(FormatTest, HandlesUTF8BOM) {
9804   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
9805   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
9806             format("\xef\xbb\xbf#include <iostream>"));
9807   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
9808             format("\xef\xbb\xbf\n#include <iostream>"));
9809 }
9810 
9811 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
9812 #if !defined(_MSC_VER)
9813 
9814 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
9815   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
9816                getLLVMStyleWithColumns(35));
9817   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
9818                getLLVMStyleWithColumns(31));
9819   verifyFormat("// Однажды в студёную зимнюю пору...",
9820                getLLVMStyleWithColumns(36));
9821   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
9822   verifyFormat("/* Однажды в студёную зимнюю пору... */",
9823                getLLVMStyleWithColumns(39));
9824   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
9825                getLLVMStyleWithColumns(35));
9826 }
9827 
9828 TEST_F(FormatTest, SplitsUTF8Strings) {
9829   // Non-printable characters' width is currently considered to be the length in
9830   // bytes in UTF8. The characters can be displayed in very different manner
9831   // (zero-width, single width with a substitution glyph, expanded to their code
9832   // (e.g. "<8d>"), so there's no single correct way to handle them.
9833   EXPECT_EQ("\"aaaaÄ\"\n"
9834             "\"\xc2\x8d\";",
9835             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9836   EXPECT_EQ("\"aaaaaaaÄ\"\n"
9837             "\"\xc2\x8d\";",
9838             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9839   EXPECT_EQ("\"Однажды, в \"\n"
9840             "\"студёную \"\n"
9841             "\"зимнюю \"\n"
9842             "\"пору,\"",
9843             format("\"Однажды, в студёную зимнюю пору,\"",
9844                    getLLVMStyleWithColumns(13)));
9845   EXPECT_EQ(
9846       "\"一 二 三 \"\n"
9847       "\"四 五六 \"\n"
9848       "\"七 八 九 \"\n"
9849       "\"十\"",
9850       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
9851   EXPECT_EQ("\"一\t二 \"\n"
9852             "\"\t三 \"\n"
9853             "\"四 五\t六 \"\n"
9854             "\"\t七 \"\n"
9855             "\"八九十\tqq\"",
9856             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
9857                    getLLVMStyleWithColumns(11)));
9858 
9859   // UTF8 character in an escape sequence.
9860   EXPECT_EQ("\"aaaaaa\"\n"
9861             "\"\\\xC2\x8D\"",
9862             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
9863 }
9864 
9865 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
9866   EXPECT_EQ("const char *sssss =\n"
9867             "    \"一二三四五六七八\\\n"
9868             " 九 十\";",
9869             format("const char *sssss = \"一二三四五六七八\\\n"
9870                    " 九 十\";",
9871                    getLLVMStyleWithColumns(30)));
9872 }
9873 
9874 TEST_F(FormatTest, SplitsUTF8LineComments) {
9875   EXPECT_EQ("// aaaaÄ\xc2\x8d",
9876             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
9877   EXPECT_EQ("// Я из лесу\n"
9878             "// вышел; был\n"
9879             "// сильный\n"
9880             "// мороз.",
9881             format("// Я из лесу вышел; был сильный мороз.",
9882                    getLLVMStyleWithColumns(13)));
9883   EXPECT_EQ("// 一二三\n"
9884             "// 四五六七\n"
9885             "// 八  九\n"
9886             "// 十",
9887             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
9888 }
9889 
9890 TEST_F(FormatTest, SplitsUTF8BlockComments) {
9891   EXPECT_EQ("/* Гляжу,\n"
9892             " * поднимается\n"
9893             " * медленно в\n"
9894             " * гору\n"
9895             " * Лошадка,\n"
9896             " * везущая\n"
9897             " * хворосту\n"
9898             " * воз. */",
9899             format("/* Гляжу, поднимается медленно в гору\n"
9900                    " * Лошадка, везущая хворосту воз. */",
9901                    getLLVMStyleWithColumns(13)));
9902   EXPECT_EQ(
9903       "/* 一二三\n"
9904       " * 四五六七\n"
9905       " * 八  九\n"
9906       " * 十  */",
9907       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
9908   EXPECT_EQ("/* �������� ��������\n"
9909             " * ��������\n"
9910             " * ������-�� */",
9911             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
9912 }
9913 
9914 #endif // _MSC_VER
9915 
9916 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
9917   FormatStyle Style = getLLVMStyle();
9918 
9919   Style.ConstructorInitializerIndentWidth = 4;
9920   verifyFormat(
9921       "SomeClass::Constructor()\n"
9922       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9923       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9924       Style);
9925 
9926   Style.ConstructorInitializerIndentWidth = 2;
9927   verifyFormat(
9928       "SomeClass::Constructor()\n"
9929       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9930       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9931       Style);
9932 
9933   Style.ConstructorInitializerIndentWidth = 0;
9934   verifyFormat(
9935       "SomeClass::Constructor()\n"
9936       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9937       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9938       Style);
9939 }
9940 
9941 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
9942   FormatStyle Style = getLLVMStyle();
9943   Style.BreakConstructorInitializersBeforeComma = true;
9944   Style.ConstructorInitializerIndentWidth = 4;
9945   verifyFormat("SomeClass::Constructor()\n"
9946                "    : a(a)\n"
9947                "    , b(b)\n"
9948                "    , c(c) {}",
9949                Style);
9950   verifyFormat("SomeClass::Constructor()\n"
9951                "    : a(a) {}",
9952                Style);
9953 
9954   Style.ColumnLimit = 0;
9955   verifyFormat("SomeClass::Constructor()\n"
9956                "    : a(a) {}",
9957                Style);
9958   verifyFormat("SomeClass::Constructor()\n"
9959                "    : a(a)\n"
9960                "    , b(b)\n"
9961                "    , c(c) {}",
9962                Style);
9963   verifyFormat("SomeClass::Constructor()\n"
9964                "    : a(a) {\n"
9965                "  foo();\n"
9966                "  bar();\n"
9967                "}",
9968                Style);
9969 
9970   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9971   verifyFormat("SomeClass::Constructor()\n"
9972                "    : a(a)\n"
9973                "    , b(b)\n"
9974                "    , c(c) {\n}",
9975                Style);
9976   verifyFormat("SomeClass::Constructor()\n"
9977                "    : a(a) {\n}",
9978                Style);
9979 
9980   Style.ColumnLimit = 80;
9981   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
9982   Style.ConstructorInitializerIndentWidth = 2;
9983   verifyFormat("SomeClass::Constructor()\n"
9984                "  : a(a)\n"
9985                "  , b(b)\n"
9986                "  , c(c) {}",
9987                Style);
9988 
9989   Style.ConstructorInitializerIndentWidth = 0;
9990   verifyFormat("SomeClass::Constructor()\n"
9991                ": a(a)\n"
9992                ", b(b)\n"
9993                ", c(c) {}",
9994                Style);
9995 
9996   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
9997   Style.ConstructorInitializerIndentWidth = 4;
9998   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
9999   verifyFormat(
10000       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
10001       Style);
10002   verifyFormat(
10003       "SomeClass::Constructor()\n"
10004       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
10005       Style);
10006   Style.ConstructorInitializerIndentWidth = 4;
10007   Style.ColumnLimit = 60;
10008   verifyFormat("SomeClass::Constructor()\n"
10009                "    : aaaaaaaa(aaaaaaaa)\n"
10010                "    , aaaaaaaa(aaaaaaaa)\n"
10011                "    , aaaaaaaa(aaaaaaaa) {}",
10012                Style);
10013 }
10014 
10015 TEST_F(FormatTest, Destructors) {
10016   verifyFormat("void F(int &i) { i.~int(); }");
10017   verifyFormat("void F(int &i) { i->~int(); }");
10018 }
10019 
10020 TEST_F(FormatTest, FormatsWithWebKitStyle) {
10021   FormatStyle Style = getWebKitStyle();
10022 
10023   // Don't indent in outer namespaces.
10024   verifyFormat("namespace outer {\n"
10025                "int i;\n"
10026                "namespace inner {\n"
10027                "    int i;\n"
10028                "} // namespace inner\n"
10029                "} // namespace outer\n"
10030                "namespace other_outer {\n"
10031                "int i;\n"
10032                "}",
10033                Style);
10034 
10035   // Don't indent case labels.
10036   verifyFormat("switch (variable) {\n"
10037                "case 1:\n"
10038                "case 2:\n"
10039                "    doSomething();\n"
10040                "    break;\n"
10041                "default:\n"
10042                "    ++variable;\n"
10043                "}",
10044                Style);
10045 
10046   // Wrap before binary operators.
10047   EXPECT_EQ("void f()\n"
10048             "{\n"
10049             "    if (aaaaaaaaaaaaaaaa\n"
10050             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
10051             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10052             "        return;\n"
10053             "}",
10054             format("void f() {\n"
10055                    "if (aaaaaaaaaaaaaaaa\n"
10056                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
10057                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10058                    "return;\n"
10059                    "}",
10060                    Style));
10061 
10062   // Allow functions on a single line.
10063   verifyFormat("void f() { return; }", Style);
10064 
10065   // Constructor initializers are formatted one per line with the "," on the
10066   // new line.
10067   verifyFormat("Constructor()\n"
10068                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10069                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
10070                "          aaaaaaaaaaaaaa)\n"
10071                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
10072                "{\n"
10073                "}",
10074                Style);
10075   verifyFormat("SomeClass::Constructor()\n"
10076                "    : a(a)\n"
10077                "{\n"
10078                "}",
10079                Style);
10080   EXPECT_EQ("SomeClass::Constructor()\n"
10081             "    : a(a)\n"
10082             "{\n"
10083             "}",
10084             format("SomeClass::Constructor():a(a){}", Style));
10085   verifyFormat("SomeClass::Constructor()\n"
10086                "    : a(a)\n"
10087                "    , b(b)\n"
10088                "    , c(c)\n"
10089                "{\n"
10090                "}",
10091                Style);
10092   verifyFormat("SomeClass::Constructor()\n"
10093                "    : a(a)\n"
10094                "{\n"
10095                "    foo();\n"
10096                "    bar();\n"
10097                "}",
10098                Style);
10099 
10100   // Access specifiers should be aligned left.
10101   verifyFormat("class C {\n"
10102                "public:\n"
10103                "    int i;\n"
10104                "};",
10105                Style);
10106 
10107   // Do not align comments.
10108   verifyFormat("int a; // Do not\n"
10109                "double b; // align comments.",
10110                Style);
10111 
10112   // Do not align operands.
10113   EXPECT_EQ("ASSERT(aaaa\n"
10114             "    || bbbb);",
10115             format("ASSERT ( aaaa\n||bbbb);", Style));
10116 
10117   // Accept input's line breaks.
10118   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
10119             "    || bbbbbbbbbbbbbbb) {\n"
10120             "    i++;\n"
10121             "}",
10122             format("if (aaaaaaaaaaaaaaa\n"
10123                    "|| bbbbbbbbbbbbbbb) { i++; }",
10124                    Style));
10125   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
10126             "    i++;\n"
10127             "}",
10128             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
10129 
10130   // Don't automatically break all macro definitions (llvm.org/PR17842).
10131   verifyFormat("#define aNumber 10", Style);
10132   // However, generally keep the line breaks that the user authored.
10133   EXPECT_EQ("#define aNumber \\\n"
10134             "    10",
10135             format("#define aNumber \\\n"
10136                    " 10",
10137                    Style));
10138 
10139   // Keep empty and one-element array literals on a single line.
10140   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
10141             "                                  copyItems:YES];",
10142             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
10143                    "copyItems:YES];",
10144                    Style));
10145   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
10146             "                                  copyItems:YES];",
10147             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
10148                    "             copyItems:YES];",
10149                    Style));
10150   // FIXME: This does not seem right, there should be more indentation before
10151   // the array literal's entries. Nested blocks have the same problem.
10152   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10153             "    @\"a\",\n"
10154             "    @\"a\"\n"
10155             "]\n"
10156             "                                  copyItems:YES];",
10157             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10158                    "     @\"a\",\n"
10159                    "     @\"a\"\n"
10160                    "     ]\n"
10161                    "       copyItems:YES];",
10162                    Style));
10163   EXPECT_EQ(
10164       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10165       "                                  copyItems:YES];",
10166       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10167              "   copyItems:YES];",
10168              Style));
10169 
10170   verifyFormat("[self.a b:c c:d];", Style);
10171   EXPECT_EQ("[self.a b:c\n"
10172             "        c:d];",
10173             format("[self.a b:c\n"
10174                    "c:d];",
10175                    Style));
10176 }
10177 
10178 TEST_F(FormatTest, FormatsLambdas) {
10179   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
10180   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
10181   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
10182   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
10183   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
10184   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
10185   verifyFormat("void f() {\n"
10186                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
10187                "}\n");
10188   verifyFormat("void f() {\n"
10189                "  other(x.begin(), //\n"
10190                "        x.end(),   //\n"
10191                "        [&](int, int) { return 1; });\n"
10192                "}\n");
10193   verifyFormat("SomeFunction([]() { // A cool function...\n"
10194                "  return 43;\n"
10195                "});");
10196   EXPECT_EQ("SomeFunction([]() {\n"
10197             "#define A a\n"
10198             "  return 43;\n"
10199             "});",
10200             format("SomeFunction([](){\n"
10201                    "#define A a\n"
10202                    "return 43;\n"
10203                    "});"));
10204   verifyFormat("void f() {\n"
10205                "  SomeFunction([](decltype(x), A *a) {});\n"
10206                "}");
10207   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10208                "    [](const aaaaaaaaaa &a) { return a; });");
10209   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
10210                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
10211                "});");
10212   verifyFormat("Constructor()\n"
10213                "    : Field([] { // comment\n"
10214                "        int i;\n"
10215                "      }) {}");
10216   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
10217                "  return some_parameter.size();\n"
10218                "};");
10219   verifyFormat("int i = aaaaaa ? 1 //\n"
10220                "               : [] {\n"
10221                "                   return 2; //\n"
10222                "                 }();");
10223   verifyFormat("llvm::errs() << \"number of twos is \"\n"
10224                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
10225                "                  return x == 2; // force break\n"
10226                "                });");
10227   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n"
10228                "    int iiiiiiiiiiii) {\n"
10229                "  return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n"
10230                "});",
10231                getLLVMStyleWithColumns(60));
10232   verifyFormat("SomeFunction({[&] {\n"
10233                "                // comment\n"
10234                "              },\n"
10235                "              [&] {\n"
10236                "                // comment\n"
10237                "              }});");
10238   verifyFormat("SomeFunction({[&] {\n"
10239                "  // comment\n"
10240                "}});");
10241   verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
10242                "                             [&]() { return true; },\n"
10243                "                         aaaaa aaaaaaaaa);");
10244 
10245   // Lambdas with return types.
10246   verifyFormat("int c = []() -> int { return 2; }();\n");
10247   verifyFormat("int c = []() -> int * { return 2; }();\n");
10248   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
10249   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
10250   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
10251   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
10252   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
10253   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
10254   verifyFormat("[a, a]() -> a<1> {};");
10255   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
10256                "                   int j) -> int {\n"
10257                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
10258                "};");
10259   verifyFormat(
10260       "aaaaaaaaaaaaaaaaaaaaaa(\n"
10261       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
10262       "      return aaaaaaaaaaaaaaaaa;\n"
10263       "    });",
10264       getLLVMStyleWithColumns(70));
10265 
10266   // Multiple lambdas in the same parentheses change indentation rules.
10267   verifyFormat("SomeFunction(\n"
10268                "    []() {\n"
10269                "      int i = 42;\n"
10270                "      return i;\n"
10271                "    },\n"
10272                "    []() {\n"
10273                "      int j = 43;\n"
10274                "      return j;\n"
10275                "    });");
10276 
10277   // More complex introducers.
10278   verifyFormat("return [i, args...] {};");
10279 
10280   // Not lambdas.
10281   verifyFormat("constexpr char hello[]{\"hello\"};");
10282   verifyFormat("double &operator[](int i) { return 0; }\n"
10283                "int i;");
10284   verifyFormat("std::unique_ptr<int[]> foo() {}");
10285   verifyFormat("int i = a[a][a]->f();");
10286   verifyFormat("int i = (*b)[a]->f();");
10287 
10288   // Other corner cases.
10289   verifyFormat("void f() {\n"
10290                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
10291                "      );\n"
10292                "}");
10293 
10294   // Lambdas created through weird macros.
10295   verifyFormat("void f() {\n"
10296                "  MACRO((const AA &a) { return 1; });\n"
10297                "}");
10298 
10299   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
10300                "      doo_dah();\n"
10301                "      doo_dah();\n"
10302                "    })) {\n"
10303                "}");
10304   verifyFormat("auto lambda = []() {\n"
10305                "  int a = 2\n"
10306                "#if A\n"
10307                "          + 2\n"
10308                "#endif\n"
10309                "      ;\n"
10310                "};");
10311 }
10312 
10313 TEST_F(FormatTest, FormatsBlocks) {
10314   FormatStyle ShortBlocks = getLLVMStyle();
10315   ShortBlocks.AllowShortBlocksOnASingleLine = true;
10316   verifyFormat("int (^Block)(int, int);", ShortBlocks);
10317   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
10318   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
10319   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
10320   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
10321   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
10322 
10323   verifyFormat("foo(^{ bar(); });", ShortBlocks);
10324   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
10325   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
10326 
10327   verifyFormat("[operation setCompletionBlock:^{\n"
10328                "  [self onOperationDone];\n"
10329                "}];");
10330   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
10331                "  [self onOperationDone];\n"
10332                "}]};");
10333   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
10334                "  f();\n"
10335                "}];");
10336   verifyFormat("int a = [operation block:^int(int *i) {\n"
10337                "  return 1;\n"
10338                "}];");
10339   verifyFormat("[myObject doSomethingWith:arg1\n"
10340                "                      aaa:^int(int *a) {\n"
10341                "                        return 1;\n"
10342                "                      }\n"
10343                "                      bbb:f(a * bbbbbbbb)];");
10344 
10345   verifyFormat("[operation setCompletionBlock:^{\n"
10346                "  [self.delegate newDataAvailable];\n"
10347                "}];",
10348                getLLVMStyleWithColumns(60));
10349   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
10350                "  NSString *path = [self sessionFilePath];\n"
10351                "  if (path) {\n"
10352                "    // ...\n"
10353                "  }\n"
10354                "});");
10355   verifyFormat("[[SessionService sharedService]\n"
10356                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10357                "      if (window) {\n"
10358                "        [self windowDidLoad:window];\n"
10359                "      } else {\n"
10360                "        [self errorLoadingWindow];\n"
10361                "      }\n"
10362                "    }];");
10363   verifyFormat("void (^largeBlock)(void) = ^{\n"
10364                "  // ...\n"
10365                "};\n",
10366                getLLVMStyleWithColumns(40));
10367   verifyFormat("[[SessionService sharedService]\n"
10368                "    loadWindowWithCompletionBlock: //\n"
10369                "        ^(SessionWindow *window) {\n"
10370                "          if (window) {\n"
10371                "            [self windowDidLoad:window];\n"
10372                "          } else {\n"
10373                "            [self errorLoadingWindow];\n"
10374                "          }\n"
10375                "        }];",
10376                getLLVMStyleWithColumns(60));
10377   verifyFormat("[myObject doSomethingWith:arg1\n"
10378                "    firstBlock:^(Foo *a) {\n"
10379                "      // ...\n"
10380                "      int i;\n"
10381                "    }\n"
10382                "    secondBlock:^(Bar *b) {\n"
10383                "      // ...\n"
10384                "      int i;\n"
10385                "    }\n"
10386                "    thirdBlock:^Foo(Bar *b) {\n"
10387                "      // ...\n"
10388                "      int i;\n"
10389                "    }];");
10390   verifyFormat("[myObject doSomethingWith:arg1\n"
10391                "               firstBlock:-1\n"
10392                "              secondBlock:^(Bar *b) {\n"
10393                "                // ...\n"
10394                "                int i;\n"
10395                "              }];");
10396 
10397   verifyFormat("f(^{\n"
10398                "  @autoreleasepool {\n"
10399                "    if (a) {\n"
10400                "      g();\n"
10401                "    }\n"
10402                "  }\n"
10403                "});");
10404   verifyFormat("Block b = ^int *(A *a, B *b) {}");
10405 
10406   FormatStyle FourIndent = getLLVMStyle();
10407   FourIndent.ObjCBlockIndentWidth = 4;
10408   verifyFormat("[operation setCompletionBlock:^{\n"
10409                "    [self onOperationDone];\n"
10410                "}];",
10411                FourIndent);
10412 }
10413 
10414 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
10415   FormatStyle ZeroColumn = getLLVMStyle();
10416   ZeroColumn.ColumnLimit = 0;
10417 
10418   verifyFormat("[[SessionService sharedService] "
10419                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10420                "  if (window) {\n"
10421                "    [self windowDidLoad:window];\n"
10422                "  } else {\n"
10423                "    [self errorLoadingWindow];\n"
10424                "  }\n"
10425                "}];",
10426                ZeroColumn);
10427   EXPECT_EQ("[[SessionService sharedService]\n"
10428             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10429             "      if (window) {\n"
10430             "        [self windowDidLoad:window];\n"
10431             "      } else {\n"
10432             "        [self errorLoadingWindow];\n"
10433             "      }\n"
10434             "    }];",
10435             format("[[SessionService sharedService]\n"
10436                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10437                    "                if (window) {\n"
10438                    "    [self windowDidLoad:window];\n"
10439                    "  } else {\n"
10440                    "    [self errorLoadingWindow];\n"
10441                    "  }\n"
10442                    "}];",
10443                    ZeroColumn));
10444   verifyFormat("[myObject doSomethingWith:arg1\n"
10445                "    firstBlock:^(Foo *a) {\n"
10446                "      // ...\n"
10447                "      int i;\n"
10448                "    }\n"
10449                "    secondBlock:^(Bar *b) {\n"
10450                "      // ...\n"
10451                "      int i;\n"
10452                "    }\n"
10453                "    thirdBlock:^Foo(Bar *b) {\n"
10454                "      // ...\n"
10455                "      int i;\n"
10456                "    }];",
10457                ZeroColumn);
10458   verifyFormat("f(^{\n"
10459                "  @autoreleasepool {\n"
10460                "    if (a) {\n"
10461                "      g();\n"
10462                "    }\n"
10463                "  }\n"
10464                "});",
10465                ZeroColumn);
10466   verifyFormat("void (^largeBlock)(void) = ^{\n"
10467                "  // ...\n"
10468                "};",
10469                ZeroColumn);
10470 
10471   ZeroColumn.AllowShortBlocksOnASingleLine = true;
10472   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
10473             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
10474   ZeroColumn.AllowShortBlocksOnASingleLine = false;
10475   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
10476             "  int i;\n"
10477             "};",
10478             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
10479 }
10480 
10481 TEST_F(FormatTest, SupportsCRLF) {
10482   EXPECT_EQ("int a;\r\n"
10483             "int b;\r\n"
10484             "int c;\r\n",
10485             format("int a;\r\n"
10486                    "  int b;\r\n"
10487                    "    int c;\r\n",
10488                    getLLVMStyle()));
10489   EXPECT_EQ("int a;\r\n"
10490             "int b;\r\n"
10491             "int c;\r\n",
10492             format("int a;\r\n"
10493                    "  int b;\n"
10494                    "    int c;\r\n",
10495                    getLLVMStyle()));
10496   EXPECT_EQ("int a;\n"
10497             "int b;\n"
10498             "int c;\n",
10499             format("int a;\r\n"
10500                    "  int b;\n"
10501                    "    int c;\n",
10502                    getLLVMStyle()));
10503   EXPECT_EQ("\"aaaaaaa \"\r\n"
10504             "\"bbbbbbb\";\r\n",
10505             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
10506   EXPECT_EQ("#define A \\\r\n"
10507             "  b;      \\\r\n"
10508             "  c;      \\\r\n"
10509             "  d;\r\n",
10510             format("#define A \\\r\n"
10511                    "  b; \\\r\n"
10512                    "  c; d; \r\n",
10513                    getGoogleStyle()));
10514 
10515   EXPECT_EQ("/*\r\n"
10516             "multi line block comments\r\n"
10517             "should not introduce\r\n"
10518             "an extra carriage return\r\n"
10519             "*/\r\n",
10520             format("/*\r\n"
10521                    "multi line block comments\r\n"
10522                    "should not introduce\r\n"
10523                    "an extra carriage return\r\n"
10524                    "*/\r\n"));
10525 }
10526 
10527 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
10528   verifyFormat("MY_CLASS(C) {\n"
10529                "  int i;\n"
10530                "  int j;\n"
10531                "};");
10532 }
10533 
10534 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
10535   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
10536   TwoIndent.ContinuationIndentWidth = 2;
10537 
10538   EXPECT_EQ("int i =\n"
10539             "  longFunction(\n"
10540             "    arg);",
10541             format("int i = longFunction(arg);", TwoIndent));
10542 
10543   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
10544   SixIndent.ContinuationIndentWidth = 6;
10545 
10546   EXPECT_EQ("int i =\n"
10547             "      longFunction(\n"
10548             "            arg);",
10549             format("int i = longFunction(arg);", SixIndent));
10550 }
10551 
10552 TEST_F(FormatTest, SpacesInAngles) {
10553   FormatStyle Spaces = getLLVMStyle();
10554   Spaces.SpacesInAngles = true;
10555 
10556   verifyFormat("static_cast< int >(arg);", Spaces);
10557   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
10558   verifyFormat("f< int, float >();", Spaces);
10559   verifyFormat("template <> g() {}", Spaces);
10560   verifyFormat("template < std::vector< int > > f() {}", Spaces);
10561 
10562   Spaces.Standard = FormatStyle::LS_Cpp03;
10563   Spaces.SpacesInAngles = true;
10564   verifyFormat("A< A< int > >();", Spaces);
10565 
10566   Spaces.SpacesInAngles = false;
10567   verifyFormat("A<A<int> >();", Spaces);
10568 
10569   Spaces.Standard = FormatStyle::LS_Cpp11;
10570   Spaces.SpacesInAngles = true;
10571   verifyFormat("A< A< int > >();", Spaces);
10572 
10573   Spaces.SpacesInAngles = false;
10574   verifyFormat("A<A<int>>();", Spaces);
10575 }
10576 
10577 TEST_F(FormatTest, TripleAngleBrackets) {
10578   verifyFormat("f<<<1, 1>>>();");
10579   verifyFormat("f<<<1, 1, 1, s>>>();");
10580   verifyFormat("f<<<a, b, c, d>>>();");
10581   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
10582   verifyFormat("f<param><<<1, 1>>>();");
10583   verifyFormat("f<1><<<1, 1>>>();");
10584   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
10585   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10586                "aaaaaaaaaaa<<<\n    1, 1>>>();");
10587 }
10588 
10589 TEST_F(FormatTest, MergeLessLessAtEnd) {
10590   verifyFormat("<<");
10591   EXPECT_EQ("< < <", format("\\\n<<<"));
10592   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10593                "aaallvm::outs() <<");
10594   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10595                "aaaallvm::outs()\n    <<");
10596 }
10597 
10598 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
10599   std::string code = "#if A\n"
10600                      "#if B\n"
10601                      "a.\n"
10602                      "#endif\n"
10603                      "    a = 1;\n"
10604                      "#else\n"
10605                      "#endif\n"
10606                      "#if C\n"
10607                      "#else\n"
10608                      "#endif\n";
10609   EXPECT_EQ(code, format(code));
10610 }
10611 
10612 TEST_F(FormatTest, HandleConflictMarkers) {
10613   // Git/SVN conflict markers.
10614   EXPECT_EQ("int a;\n"
10615             "void f() {\n"
10616             "  callme(some(parameter1,\n"
10617             "<<<<<<< text by the vcs\n"
10618             "              parameter2),\n"
10619             "||||||| text by the vcs\n"
10620             "              parameter2),\n"
10621             "         parameter3,\n"
10622             "======= text by the vcs\n"
10623             "              parameter2, parameter3),\n"
10624             ">>>>>>> text by the vcs\n"
10625             "         otherparameter);\n",
10626             format("int a;\n"
10627                    "void f() {\n"
10628                    "  callme(some(parameter1,\n"
10629                    "<<<<<<< text by the vcs\n"
10630                    "  parameter2),\n"
10631                    "||||||| text by the vcs\n"
10632                    "  parameter2),\n"
10633                    "  parameter3,\n"
10634                    "======= text by the vcs\n"
10635                    "  parameter2,\n"
10636                    "  parameter3),\n"
10637                    ">>>>>>> text by the vcs\n"
10638                    "  otherparameter);\n"));
10639 
10640   // Perforce markers.
10641   EXPECT_EQ("void f() {\n"
10642             "  function(\n"
10643             ">>>> text by the vcs\n"
10644             "      parameter,\n"
10645             "==== text by the vcs\n"
10646             "      parameter,\n"
10647             "==== text by the vcs\n"
10648             "      parameter,\n"
10649             "<<<< text by the vcs\n"
10650             "      parameter);\n",
10651             format("void f() {\n"
10652                    "  function(\n"
10653                    ">>>> text by the vcs\n"
10654                    "  parameter,\n"
10655                    "==== text by the vcs\n"
10656                    "  parameter,\n"
10657                    "==== text by the vcs\n"
10658                    "  parameter,\n"
10659                    "<<<< text by the vcs\n"
10660                    "  parameter);\n"));
10661 
10662   EXPECT_EQ("<<<<<<<\n"
10663             "|||||||\n"
10664             "=======\n"
10665             ">>>>>>>",
10666             format("<<<<<<<\n"
10667                    "|||||||\n"
10668                    "=======\n"
10669                    ">>>>>>>"));
10670 
10671   EXPECT_EQ("<<<<<<<\n"
10672             "|||||||\n"
10673             "int i;\n"
10674             "=======\n"
10675             ">>>>>>>",
10676             format("<<<<<<<\n"
10677                    "|||||||\n"
10678                    "int i;\n"
10679                    "=======\n"
10680                    ">>>>>>>"));
10681 
10682   // FIXME: Handle parsing of macros around conflict markers correctly:
10683   EXPECT_EQ("#define Macro \\\n"
10684             "<<<<<<<\n"
10685             "Something \\\n"
10686             "|||||||\n"
10687             "Else \\\n"
10688             "=======\n"
10689             "Other \\\n"
10690             ">>>>>>>\n"
10691             "    End int i;\n",
10692             format("#define Macro \\\n"
10693                    "<<<<<<<\n"
10694                    "  Something \\\n"
10695                    "|||||||\n"
10696                    "  Else \\\n"
10697                    "=======\n"
10698                    "  Other \\\n"
10699                    ">>>>>>>\n"
10700                    "  End\n"
10701                    "int i;\n"));
10702 }
10703 
10704 TEST_F(FormatTest, DisableRegions) {
10705   EXPECT_EQ("int i;\n"
10706             "// clang-format off\n"
10707             "  int j;\n"
10708             "// clang-format on\n"
10709             "int k;",
10710             format(" int  i;\n"
10711                    "   // clang-format off\n"
10712                    "  int j;\n"
10713                    " // clang-format on\n"
10714                    "   int   k;"));
10715   EXPECT_EQ("int i;\n"
10716             "/* clang-format off */\n"
10717             "  int j;\n"
10718             "/* clang-format on */\n"
10719             "int k;",
10720             format(" int  i;\n"
10721                    "   /* clang-format off */\n"
10722                    "  int j;\n"
10723                    " /* clang-format on */\n"
10724                    "   int   k;"));
10725 }
10726 
10727 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
10728   format("? ) =");
10729   verifyNoCrash("#define a\\\n /**/}");
10730 }
10731 
10732 } // end namespace
10733 } // end namespace format
10734 } // end namespace clang
10735