1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "FormatTestUtils.h"
11 #include "clang/Format/Format.h"
12 #include "llvm/Support/Debug.h"
13 #include "gtest/gtest.h"
14 
15 #define DEBUG_TYPE "format-test"
16 
17 namespace clang {
18 namespace format {
19 namespace {
20 
21 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
22 
23 class FormatTest : public ::testing::Test {
24 protected:
25   enum IncompleteCheck {
26     IC_ExpectComplete,
27     IC_ExpectIncomplete,
28     IC_DoNotCheck
29   };
30 
31   std::string format(llvm::StringRef Code,
32                      const FormatStyle &Style = getLLVMStyle(),
33                      IncompleteCheck CheckIncomplete = IC_ExpectComplete) {
34     DEBUG(llvm::errs() << "---\n");
35     DEBUG(llvm::errs() << Code << "\n\n");
36     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
37     bool IncompleteFormat = false;
38     tooling::Replacements Replaces =
39         reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
40     if (CheckIncomplete != IC_DoNotCheck) {
41       bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete;
42       EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n";
43     }
44     ReplacementCount = Replaces.size();
45     std::string Result = applyAllReplacements(Code, Replaces);
46     EXPECT_NE("", Result);
47     DEBUG(llvm::errs() << "\n" << Result << "\n\n");
48     return Result;
49   }
50 
51   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
52     FormatStyle Style = getLLVMStyle();
53     Style.ColumnLimit = ColumnLimit;
54     return Style;
55   }
56 
57   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
58     FormatStyle Style = getGoogleStyle();
59     Style.ColumnLimit = ColumnLimit;
60     return Style;
61   }
62 
63   void verifyFormat(llvm::StringRef Code,
64                     const FormatStyle &Style = getLLVMStyle()) {
65     EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
66   }
67 
68   void verifyIncompleteFormat(llvm::StringRef Code,
69                               const FormatStyle &Style = getLLVMStyle()) {
70     EXPECT_EQ(Code.str(),
71               format(test::messUp(Code), Style, IC_ExpectIncomplete));
72   }
73 
74   void verifyGoogleFormat(llvm::StringRef Code) {
75     verifyFormat(Code, getGoogleStyle());
76   }
77 
78   void verifyIndependentOfContext(llvm::StringRef text) {
79     verifyFormat(text);
80     verifyFormat(llvm::Twine("void f() { " + text + " }").str());
81   }
82 
83   /// \brief Verify that clang-format does not crash on the given input.
84   void verifyNoCrash(llvm::StringRef Code,
85                      const FormatStyle &Style = getLLVMStyle()) {
86     format(Code, Style, IC_DoNotCheck);
87   }
88 
89   int ReplacementCount;
90 };
91 
92 TEST_F(FormatTest, MessUp) {
93   EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
94   EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
95   EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
96   EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
97   EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
98 }
99 
100 //===----------------------------------------------------------------------===//
101 // Basic function tests.
102 //===----------------------------------------------------------------------===//
103 
104 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
105   EXPECT_EQ(";", format(";"));
106 }
107 
108 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
109   EXPECT_EQ("int i;", format("  int i;"));
110   EXPECT_EQ("\nint i;", format(" \n\t \v \f  int i;"));
111   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
112   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
113 }
114 
115 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
116   EXPECT_EQ("int i;", format("int\ni;"));
117 }
118 
119 TEST_F(FormatTest, FormatsNestedBlockStatements) {
120   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
121 }
122 
123 TEST_F(FormatTest, FormatsNestedCall) {
124   verifyFormat("Method(f1, f2(f3));");
125   verifyFormat("Method(f1(f2, f3()));");
126   verifyFormat("Method(f1(f2, (f3())));");
127 }
128 
129 TEST_F(FormatTest, NestedNameSpecifiers) {
130   verifyFormat("vector<::Type> v;");
131   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
132   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
133   verifyFormat("bool a = 2 < ::SomeFunction();");
134 }
135 
136 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
137   EXPECT_EQ("if (a) {\n"
138             "  f();\n"
139             "}",
140             format("if(a){f();}"));
141   EXPECT_EQ(4, ReplacementCount);
142   EXPECT_EQ("if (a) {\n"
143             "  f();\n"
144             "}",
145             format("if (a) {\n"
146                    "  f();\n"
147                    "}"));
148   EXPECT_EQ(0, ReplacementCount);
149   EXPECT_EQ("/*\r\n"
150             "\r\n"
151             "*/\r\n",
152             format("/*\r\n"
153                    "\r\n"
154                    "*/\r\n"));
155   EXPECT_EQ(0, ReplacementCount);
156 }
157 
158 TEST_F(FormatTest, RemovesEmptyLines) {
159   EXPECT_EQ("class C {\n"
160             "  int i;\n"
161             "};",
162             format("class C {\n"
163                    " int i;\n"
164                    "\n"
165                    "};"));
166 
167   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
168   EXPECT_EQ("namespace N {\n"
169             "\n"
170             "int i;\n"
171             "}",
172             format("namespace N {\n"
173                    "\n"
174                    "int    i;\n"
175                    "}",
176                    getGoogleStyle()));
177   EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
178             "\n"
179             "int i;\n"
180             "}",
181             format("extern /**/ \"C\" /**/ {\n"
182                    "\n"
183                    "int    i;\n"
184                    "}",
185                    getGoogleStyle()));
186 
187   // ...but do keep inlining and removing empty lines for non-block extern "C"
188   // functions.
189   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
190   EXPECT_EQ("extern \"C\" int f() {\n"
191             "  int i = 42;\n"
192             "  return i;\n"
193             "}",
194             format("extern \"C\" int f() {\n"
195                    "\n"
196                    "  int i = 42;\n"
197                    "  return i;\n"
198                    "}",
199                    getGoogleStyle()));
200 
201   // Remove empty lines at the beginning and end of blocks.
202   EXPECT_EQ("void f() {\n"
203             "\n"
204             "  if (a) {\n"
205             "\n"
206             "    f();\n"
207             "  }\n"
208             "}",
209             format("void f() {\n"
210                    "\n"
211                    "  if (a) {\n"
212                    "\n"
213                    "    f();\n"
214                    "\n"
215                    "  }\n"
216                    "\n"
217                    "}",
218                    getLLVMStyle()));
219   EXPECT_EQ("void f() {\n"
220             "  if (a) {\n"
221             "    f();\n"
222             "  }\n"
223             "}",
224             format("void f() {\n"
225                    "\n"
226                    "  if (a) {\n"
227                    "\n"
228                    "    f();\n"
229                    "\n"
230                    "  }\n"
231                    "\n"
232                    "}",
233                    getGoogleStyle()));
234 
235   // Don't remove empty lines in more complex control statements.
236   EXPECT_EQ("void f() {\n"
237             "  if (a) {\n"
238             "    f();\n"
239             "\n"
240             "  } else if (b) {\n"
241             "    f();\n"
242             "  }\n"
243             "}",
244             format("void f() {\n"
245                    "  if (a) {\n"
246                    "    f();\n"
247                    "\n"
248                    "  } else if (b) {\n"
249                    "    f();\n"
250                    "\n"
251                    "  }\n"
252                    "\n"
253                    "}"));
254 
255   // FIXME: This is slightly inconsistent.
256   EXPECT_EQ("namespace {\n"
257             "int i;\n"
258             "}",
259             format("namespace {\n"
260                    "int i;\n"
261                    "\n"
262                    "}"));
263   EXPECT_EQ("namespace {\n"
264             "int i;\n"
265             "\n"
266             "} // namespace",
267             format("namespace {\n"
268                    "int i;\n"
269                    "\n"
270                    "}  // namespace"));
271 }
272 
273 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
274   verifyFormat("x = (a) and (b);");
275   verifyFormat("x = (a) or (b);");
276   verifyFormat("x = (a) bitand (b);");
277   verifyFormat("x = (a) bitor (b);");
278   verifyFormat("x = (a) not_eq (b);");
279   verifyFormat("x = (a) and_eq (b);");
280   verifyFormat("x = (a) or_eq (b);");
281   verifyFormat("x = (a) xor (b);");
282 }
283 
284 //===----------------------------------------------------------------------===//
285 // Tests for control statements.
286 //===----------------------------------------------------------------------===//
287 
288 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
289   verifyFormat("if (true)\n  f();\ng();");
290   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
291   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
292 
293   FormatStyle AllowsMergedIf = getLLVMStyle();
294   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
295   verifyFormat("if (a)\n"
296                "  // comment\n"
297                "  f();",
298                AllowsMergedIf);
299   verifyFormat("if (a)\n"
300                "  ;",
301                AllowsMergedIf);
302   verifyFormat("if (a)\n"
303                "  if (b) return;",
304                AllowsMergedIf);
305 
306   verifyFormat("if (a) // Can't merge this\n"
307                "  f();\n",
308                AllowsMergedIf);
309   verifyFormat("if (a) /* still don't merge */\n"
310                "  f();",
311                AllowsMergedIf);
312   verifyFormat("if (a) { // Never merge this\n"
313                "  f();\n"
314                "}",
315                AllowsMergedIf);
316   verifyFormat("if (a) {/* Never merge this */\n"
317                "  f();\n"
318                "}",
319                AllowsMergedIf);
320 
321   AllowsMergedIf.ColumnLimit = 14;
322   verifyFormat("if (a) return;", AllowsMergedIf);
323   verifyFormat("if (aaaaaaaaa)\n"
324                "  return;",
325                AllowsMergedIf);
326 
327   AllowsMergedIf.ColumnLimit = 13;
328   verifyFormat("if (a)\n  return;", AllowsMergedIf);
329 }
330 
331 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
332   FormatStyle AllowsMergedLoops = getLLVMStyle();
333   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
334   verifyFormat("while (true) continue;", AllowsMergedLoops);
335   verifyFormat("for (;;) continue;", AllowsMergedLoops);
336   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
337   verifyFormat("while (true)\n"
338                "  ;",
339                AllowsMergedLoops);
340   verifyFormat("for (;;)\n"
341                "  ;",
342                AllowsMergedLoops);
343   verifyFormat("for (;;)\n"
344                "  for (;;) continue;",
345                AllowsMergedLoops);
346   verifyFormat("for (;;) // Can't merge this\n"
347                "  continue;",
348                AllowsMergedLoops);
349   verifyFormat("for (;;) /* still don't merge */\n"
350                "  continue;",
351                AllowsMergedLoops);
352 }
353 
354 TEST_F(FormatTest, FormatShortBracedStatements) {
355   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
356   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
357 
358   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
359   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
360 
361   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
362   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
363   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
364   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
365   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
366   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
367   verifyFormat("if (true) { //\n"
368                "  f();\n"
369                "}",
370                AllowSimpleBracedStatements);
371   verifyFormat("if (true) {\n"
372                "  f();\n"
373                "  f();\n"
374                "}",
375                AllowSimpleBracedStatements);
376   verifyFormat("if (true) {\n"
377                "  f();\n"
378                "} else {\n"
379                "  f();\n"
380                "}",
381                AllowSimpleBracedStatements);
382 
383   verifyFormat("template <int> struct A2 {\n"
384                "  struct B {};\n"
385                "};",
386                AllowSimpleBracedStatements);
387 
388   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
389   verifyFormat("if (true) {\n"
390                "  f();\n"
391                "}",
392                AllowSimpleBracedStatements);
393   verifyFormat("if (true) {\n"
394                "  f();\n"
395                "} else {\n"
396                "  f();\n"
397                "}",
398                AllowSimpleBracedStatements);
399 
400   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
401   verifyFormat("while (true) {\n"
402                "  f();\n"
403                "}",
404                AllowSimpleBracedStatements);
405   verifyFormat("for (;;) {\n"
406                "  f();\n"
407                "}",
408                AllowSimpleBracedStatements);
409 }
410 
411 TEST_F(FormatTest, ParseIfElse) {
412   verifyFormat("if (true)\n"
413                "  if (true)\n"
414                "    if (true)\n"
415                "      f();\n"
416                "    else\n"
417                "      g();\n"
418                "  else\n"
419                "    h();\n"
420                "else\n"
421                "  i();");
422   verifyFormat("if (true)\n"
423                "  if (true)\n"
424                "    if (true) {\n"
425                "      if (true)\n"
426                "        f();\n"
427                "    } else {\n"
428                "      g();\n"
429                "    }\n"
430                "  else\n"
431                "    h();\n"
432                "else {\n"
433                "  i();\n"
434                "}");
435   verifyFormat("void f() {\n"
436                "  if (a) {\n"
437                "  } else {\n"
438                "  }\n"
439                "}");
440 }
441 
442 TEST_F(FormatTest, ElseIf) {
443   verifyFormat("if (a) {\n} else if (b) {\n}");
444   verifyFormat("if (a)\n"
445                "  f();\n"
446                "else if (b)\n"
447                "  g();\n"
448                "else\n"
449                "  h();");
450   verifyFormat("if (a) {\n"
451                "  f();\n"
452                "}\n"
453                "// or else ..\n"
454                "else {\n"
455                "  g()\n"
456                "}");
457 
458   verifyFormat("if (a) {\n"
459                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
460                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
461                "}");
462   verifyFormat("if (a) {\n"
463                "} else if (\n"
464                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
465                "}",
466                getLLVMStyleWithColumns(62));
467 }
468 
469 TEST_F(FormatTest, FormatsForLoop) {
470   verifyFormat(
471       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
472       "     ++VeryVeryLongLoopVariable)\n"
473       "  ;");
474   verifyFormat("for (;;)\n"
475                "  f();");
476   verifyFormat("for (;;) {\n}");
477   verifyFormat("for (;;) {\n"
478                "  f();\n"
479                "}");
480   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
481 
482   verifyFormat(
483       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
484       "                                          E = UnwrappedLines.end();\n"
485       "     I != E; ++I) {\n}");
486 
487   verifyFormat(
488       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
489       "     ++IIIII) {\n}");
490   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
491                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
492                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
493   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
494                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
495                "         E = FD->getDeclsInPrototypeScope().end();\n"
496                "     I != E; ++I) {\n}");
497   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
498                "         I = Container.begin(),\n"
499                "         E = Container.end();\n"
500                "     I != E; ++I) {\n}",
501                getLLVMStyleWithColumns(76));
502 
503   verifyFormat(
504       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
505       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
506       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
507       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
508       "     ++aaaaaaaaaaa) {\n}");
509   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
510                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
511                "     ++i) {\n}");
512   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
513                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
514                "}");
515   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
516                "         aaaaaaaaaa);\n"
517                "     iter; ++iter) {\n"
518                "}");
519   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
520                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
521                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
522                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
523 
524   FormatStyle NoBinPacking = getLLVMStyle();
525   NoBinPacking.BinPackParameters = false;
526   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
527                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
528                "                                           aaaaaaaaaaaaaaaa,\n"
529                "                                           aaaaaaaaaaaaaaaa,\n"
530                "                                           aaaaaaaaaaaaaaaa);\n"
531                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
532                "}",
533                NoBinPacking);
534   verifyFormat(
535       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
536       "                                          E = UnwrappedLines.end();\n"
537       "     I != E;\n"
538       "     ++I) {\n}",
539       NoBinPacking);
540 }
541 
542 TEST_F(FormatTest, RangeBasedForLoops) {
543   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
544                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
545   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
546                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
547   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
548                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
549   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
550                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
551 }
552 
553 TEST_F(FormatTest, ForEachLoops) {
554   verifyFormat("void f() {\n"
555                "  foreach (Item *item, itemlist) {}\n"
556                "  Q_FOREACH (Item *item, itemlist) {}\n"
557                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
558                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
559                "}");
560 
561   // As function-like macros.
562   verifyFormat("#define foreach(x, y)\n"
563                "#define Q_FOREACH(x, y)\n"
564                "#define BOOST_FOREACH(x, y)\n"
565                "#define UNKNOWN_FOREACH(x, y)\n");
566 
567   // Not as function-like macros.
568   verifyFormat("#define foreach (x, y)\n"
569                "#define Q_FOREACH (x, y)\n"
570                "#define BOOST_FOREACH (x, y)\n"
571                "#define UNKNOWN_FOREACH (x, y)\n");
572 }
573 
574 TEST_F(FormatTest, FormatsWhileLoop) {
575   verifyFormat("while (true) {\n}");
576   verifyFormat("while (true)\n"
577                "  f();");
578   verifyFormat("while () {\n}");
579   verifyFormat("while () {\n"
580                "  f();\n"
581                "}");
582 }
583 
584 TEST_F(FormatTest, FormatsDoWhile) {
585   verifyFormat("do {\n"
586                "  do_something();\n"
587                "} while (something());");
588   verifyFormat("do\n"
589                "  do_something();\n"
590                "while (something());");
591 }
592 
593 TEST_F(FormatTest, FormatsSwitchStatement) {
594   verifyFormat("switch (x) {\n"
595                "case 1:\n"
596                "  f();\n"
597                "  break;\n"
598                "case kFoo:\n"
599                "case ns::kBar:\n"
600                "case kBaz:\n"
601                "  break;\n"
602                "default:\n"
603                "  g();\n"
604                "  break;\n"
605                "}");
606   verifyFormat("switch (x) {\n"
607                "case 1: {\n"
608                "  f();\n"
609                "  break;\n"
610                "}\n"
611                "case 2: {\n"
612                "  break;\n"
613                "}\n"
614                "}");
615   verifyFormat("switch (x) {\n"
616                "case 1: {\n"
617                "  f();\n"
618                "  {\n"
619                "    g();\n"
620                "    h();\n"
621                "  }\n"
622                "  break;\n"
623                "}\n"
624                "}");
625   verifyFormat("switch (x) {\n"
626                "case 1: {\n"
627                "  f();\n"
628                "  if (foo) {\n"
629                "    g();\n"
630                "    h();\n"
631                "  }\n"
632                "  break;\n"
633                "}\n"
634                "}");
635   verifyFormat("switch (x) {\n"
636                "case 1: {\n"
637                "  f();\n"
638                "  g();\n"
639                "} break;\n"
640                "}");
641   verifyFormat("switch (test)\n"
642                "  ;");
643   verifyFormat("switch (x) {\n"
644                "default: {\n"
645                "  // Do nothing.\n"
646                "}\n"
647                "}");
648   verifyFormat("switch (x) {\n"
649                "// comment\n"
650                "// if 1, do f()\n"
651                "case 1:\n"
652                "  f();\n"
653                "}");
654   verifyFormat("switch (x) {\n"
655                "case 1:\n"
656                "  // Do amazing stuff\n"
657                "  {\n"
658                "    f();\n"
659                "    g();\n"
660                "  }\n"
661                "  break;\n"
662                "}");
663   verifyFormat("#define A          \\\n"
664                "  switch (x) {     \\\n"
665                "  case a:          \\\n"
666                "    foo = b;       \\\n"
667                "  }",
668                getLLVMStyleWithColumns(20));
669   verifyFormat("#define OPERATION_CASE(name)           \\\n"
670                "  case OP_name:                        \\\n"
671                "    return operations::Operation##name\n",
672                getLLVMStyleWithColumns(40));
673   verifyFormat("switch (x) {\n"
674                "case 1:;\n"
675                "default:;\n"
676                "  int i;\n"
677                "}");
678 
679   verifyGoogleFormat("switch (x) {\n"
680                      "  case 1:\n"
681                      "    f();\n"
682                      "    break;\n"
683                      "  case kFoo:\n"
684                      "  case ns::kBar:\n"
685                      "  case kBaz:\n"
686                      "    break;\n"
687                      "  default:\n"
688                      "    g();\n"
689                      "    break;\n"
690                      "}");
691   verifyGoogleFormat("switch (x) {\n"
692                      "  case 1: {\n"
693                      "    f();\n"
694                      "    break;\n"
695                      "  }\n"
696                      "}");
697   verifyGoogleFormat("switch (test)\n"
698                      "  ;");
699 
700   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
701                      "  case OP_name:              \\\n"
702                      "    return operations::Operation##name\n");
703   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
704                      "  // Get the correction operation class.\n"
705                      "  switch (OpCode) {\n"
706                      "    CASE(Add);\n"
707                      "    CASE(Subtract);\n"
708                      "    default:\n"
709                      "      return operations::Unknown;\n"
710                      "  }\n"
711                      "#undef OPERATION_CASE\n"
712                      "}");
713   verifyFormat("DEBUG({\n"
714                "  switch (x) {\n"
715                "  case A:\n"
716                "    f();\n"
717                "    break;\n"
718                "  // On B:\n"
719                "  case B:\n"
720                "    g();\n"
721                "    break;\n"
722                "  }\n"
723                "});");
724   verifyFormat("switch (a) {\n"
725                "case (b):\n"
726                "  return;\n"
727                "}");
728 
729   verifyFormat("switch (a) {\n"
730                "case some_namespace::\n"
731                "    some_constant:\n"
732                "  return;\n"
733                "}",
734                getLLVMStyleWithColumns(34));
735 }
736 
737 TEST_F(FormatTest, CaseRanges) {
738   verifyFormat("switch (x) {\n"
739                "case 'A' ... 'Z':\n"
740                "case 1 ... 5:\n"
741                "  break;\n"
742                "}");
743 }
744 
745 TEST_F(FormatTest, ShortCaseLabels) {
746   FormatStyle Style = getLLVMStyle();
747   Style.AllowShortCaseLabelsOnASingleLine = true;
748   verifyFormat("switch (a) {\n"
749                "case 1: x = 1; break;\n"
750                "case 2: return;\n"
751                "case 3:\n"
752                "case 4:\n"
753                "case 5: return;\n"
754                "case 6: // comment\n"
755                "  return;\n"
756                "case 7:\n"
757                "  // comment\n"
758                "  return;\n"
759                "default: y = 1; break;\n"
760                "}",
761                Style);
762   verifyFormat("switch (a) {\n"
763                "#if FOO\n"
764                "case 0: return 0;\n"
765                "#endif\n"
766                "}",
767                Style);
768   verifyFormat("switch (a) {\n"
769                "case 1: {\n"
770                "}\n"
771                "case 2: {\n"
772                "  return;\n"
773                "}\n"
774                "case 3: {\n"
775                "  x = 1;\n"
776                "  return;\n"
777                "}\n"
778                "case 4:\n"
779                "  if (x)\n"
780                "    return;\n"
781                "}",
782                Style);
783   Style.ColumnLimit = 21;
784   verifyFormat("switch (a) {\n"
785                "case 1: x = 1; break;\n"
786                "case 2: return;\n"
787                "case 3:\n"
788                "case 4:\n"
789                "case 5: return;\n"
790                "default:\n"
791                "  y = 1;\n"
792                "  break;\n"
793                "}",
794                Style);
795 }
796 
797 TEST_F(FormatTest, FormatsLabels) {
798   verifyFormat("void f() {\n"
799                "  some_code();\n"
800                "test_label:\n"
801                "  some_other_code();\n"
802                "  {\n"
803                "    some_more_code();\n"
804                "  another_label:\n"
805                "    some_more_code();\n"
806                "  }\n"
807                "}");
808   verifyFormat("{\n"
809                "  some_code();\n"
810                "test_label:\n"
811                "  some_other_code();\n"
812                "}");
813   verifyFormat("{\n"
814                "  some_code();\n"
815                "test_label:;\n"
816                "  int i = 0;\n"
817                "}");
818 }
819 
820 //===----------------------------------------------------------------------===//
821 // Tests for comments.
822 //===----------------------------------------------------------------------===//
823 
824 TEST_F(FormatTest, UnderstandsSingleLineComments) {
825   verifyFormat("//* */");
826   verifyFormat("// line 1\n"
827                "// line 2\n"
828                "void f() {}\n");
829 
830   verifyFormat("void f() {\n"
831                "  // Doesn't do anything\n"
832                "}");
833   verifyFormat("SomeObject\n"
834                "    // Calling someFunction on SomeObject\n"
835                "    .someFunction();");
836   verifyFormat("auto result = SomeObject\n"
837                "                  // Calling someFunction on SomeObject\n"
838                "                  .someFunction();");
839   verifyFormat("void f(int i,  // some comment (probably for i)\n"
840                "       int j,  // some comment (probably for j)\n"
841                "       int k); // some comment (probably for k)");
842   verifyFormat("void f(int i,\n"
843                "       // some comment (probably for j)\n"
844                "       int j,\n"
845                "       // some comment (probably for k)\n"
846                "       int k);");
847 
848   verifyFormat("int i    // This is a fancy variable\n"
849                "    = 5; // with nicely aligned comment.");
850 
851   verifyFormat("// Leading comment.\n"
852                "int a; // Trailing comment.");
853   verifyFormat("int a; // Trailing comment\n"
854                "       // on 2\n"
855                "       // or 3 lines.\n"
856                "int b;");
857   verifyFormat("int a; // Trailing comment\n"
858                "\n"
859                "// Leading comment.\n"
860                "int b;");
861   verifyFormat("int a;    // Comment.\n"
862                "          // More details.\n"
863                "int bbbb; // Another comment.");
864   verifyFormat(
865       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
866       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   // comment\n"
867       "int cccccccccccccccccccccccccccccc;       // comment\n"
868       "int ddd;                     // looooooooooooooooooooooooong comment\n"
869       "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
870       "int bbbbbbbbbbbbbbbbbbbbb;   // comment\n"
871       "int ccccccccccccccccccc;     // comment");
872 
873   verifyFormat("#include \"a\"     // comment\n"
874                "#include \"a/b/c\" // comment");
875   verifyFormat("#include <a>     // comment\n"
876                "#include <a/b/c> // comment");
877   EXPECT_EQ("#include \"a\"     // comment\n"
878             "#include \"a/b/c\" // comment",
879             format("#include \\\n"
880                    "  \"a\" // comment\n"
881                    "#include \"a/b/c\" // comment"));
882 
883   verifyFormat("enum E {\n"
884                "  // comment\n"
885                "  VAL_A, // comment\n"
886                "  VAL_B\n"
887                "};");
888 
889   verifyFormat(
890       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
891       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
892   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
893                "    // Comment inside a statement.\n"
894                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
895   verifyFormat("SomeFunction(a,\n"
896                "             // comment\n"
897                "             b + x);");
898   verifyFormat("SomeFunction(a, a,\n"
899                "             // comment\n"
900                "             b + x);");
901   verifyFormat(
902       "bool aaaaaaaaaaaaa = // comment\n"
903       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
904       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
905 
906   verifyFormat("int aaaa; // aaaaa\n"
907                "int aa;   // aaaaaaa",
908                getLLVMStyleWithColumns(20));
909 
910   EXPECT_EQ("void f() { // This does something ..\n"
911             "}\n"
912             "int a; // This is unrelated",
913             format("void f()    {     // This does something ..\n"
914                    "  }\n"
915                    "int   a;     // This is unrelated"));
916   EXPECT_EQ("class C {\n"
917             "  void f() { // This does something ..\n"
918             "  }          // awesome..\n"
919             "\n"
920             "  int a; // This is unrelated\n"
921             "};",
922             format("class C{void f()    { // This does something ..\n"
923                    "      } // awesome..\n"
924                    " \n"
925                    "int a;    // This is unrelated\n"
926                    "};"));
927 
928   EXPECT_EQ("int i; // single line trailing comment",
929             format("int i;\\\n// single line trailing comment"));
930 
931   verifyGoogleFormat("int a;  // Trailing comment.");
932 
933   verifyFormat("someFunction(anotherFunction( // Force break.\n"
934                "    parameter));");
935 
936   verifyGoogleFormat("#endif  // HEADER_GUARD");
937 
938   verifyFormat("const char *test[] = {\n"
939                "    // A\n"
940                "    \"aaaa\",\n"
941                "    // B\n"
942                "    \"aaaaa\"};");
943   verifyGoogleFormat(
944       "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
945       "    aaaaaaaaaaaaaaaaaaaaaa);  // 81_cols_with_this_comment");
946   EXPECT_EQ("D(a, {\n"
947             "  // test\n"
948             "  int a;\n"
949             "});",
950             format("D(a, {\n"
951                    "// test\n"
952                    "int a;\n"
953                    "});"));
954 
955   EXPECT_EQ("lineWith(); // comment\n"
956             "// at start\n"
957             "otherLine();",
958             format("lineWith();   // comment\n"
959                    "// at start\n"
960                    "otherLine();"));
961   EXPECT_EQ("lineWith(); // comment\n"
962             "            // at start\n"
963             "otherLine();",
964             format("lineWith();   // comment\n"
965                    " // at start\n"
966                    "otherLine();"));
967 
968   EXPECT_EQ("lineWith(); // comment\n"
969             "// at start\n"
970             "otherLine(); // comment",
971             format("lineWith();   // comment\n"
972                    "// at start\n"
973                    "otherLine();   // comment"));
974   EXPECT_EQ("lineWith();\n"
975             "// at start\n"
976             "otherLine(); // comment",
977             format("lineWith();\n"
978                    " // at start\n"
979                    "otherLine();   // comment"));
980   EXPECT_EQ("// first\n"
981             "// at start\n"
982             "otherLine(); // comment",
983             format("// first\n"
984                    " // at start\n"
985                    "otherLine();   // comment"));
986   EXPECT_EQ("f();\n"
987             "// first\n"
988             "// at start\n"
989             "otherLine(); // comment",
990             format("f();\n"
991                    "// first\n"
992                    " // at start\n"
993                    "otherLine();   // comment"));
994   verifyFormat("f(); // comment\n"
995                "// first\n"
996                "// at start\n"
997                "otherLine();");
998   EXPECT_EQ("f(); // comment\n"
999             "// first\n"
1000             "// at start\n"
1001             "otherLine();",
1002             format("f();   // comment\n"
1003                    "// first\n"
1004                    " // at start\n"
1005                    "otherLine();"));
1006   EXPECT_EQ("f(); // comment\n"
1007             "     // first\n"
1008             "// at start\n"
1009             "otherLine();",
1010             format("f();   // comment\n"
1011                    " // first\n"
1012                    "// at start\n"
1013                    "otherLine();"));
1014   EXPECT_EQ("void f() {\n"
1015             "  lineWith(); // comment\n"
1016             "  // at start\n"
1017             "}",
1018             format("void              f() {\n"
1019                    "  lineWith(); // comment\n"
1020                    "  // at start\n"
1021                    "}"));
1022 
1023   verifyFormat("#define A                                                  \\\n"
1024                "  int i; /* iiiiiiiiiiiiiiiiiiiii */                       \\\n"
1025                "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
1026                getLLVMStyleWithColumns(60));
1027   verifyFormat(
1028       "#define A                                                   \\\n"
1029       "  int i;                        /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
1030       "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
1031       getLLVMStyleWithColumns(61));
1032 
1033   verifyFormat("if ( // This is some comment\n"
1034                "    x + 3) {\n"
1035                "}");
1036   EXPECT_EQ("if ( // This is some comment\n"
1037             "     // spanning two lines\n"
1038             "    x + 3) {\n"
1039             "}",
1040             format("if( // This is some comment\n"
1041                    "     // spanning two lines\n"
1042                    " x + 3) {\n"
1043                    "}"));
1044 
1045   verifyNoCrash("/\\\n/");
1046   verifyNoCrash("/\\\n* */");
1047   // The 0-character somehow makes the lexer return a proper comment.
1048   verifyNoCrash(StringRef("/*\\\0\n/", 6));
1049 }
1050 
1051 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
1052   EXPECT_EQ("SomeFunction(a,\n"
1053             "             b, // comment\n"
1054             "             c);",
1055             format("SomeFunction(a,\n"
1056                    "          b, // comment\n"
1057                    "      c);"));
1058   EXPECT_EQ("SomeFunction(a, b,\n"
1059             "             // comment\n"
1060             "             c);",
1061             format("SomeFunction(a,\n"
1062                    "          b,\n"
1063                    "  // comment\n"
1064                    "      c);"));
1065   EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
1066             "             c);",
1067             format("SomeFunction(a, b, // comment (unclear relation)\n"
1068                    "      c);"));
1069   EXPECT_EQ("SomeFunction(a, // comment\n"
1070             "             b,\n"
1071             "             c); // comment",
1072             format("SomeFunction(a,     // comment\n"
1073                    "          b,\n"
1074                    "      c); // comment"));
1075 }
1076 
1077 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
1078   EXPECT_EQ("// comment", format("// comment  "));
1079   EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
1080             format("int aaaaaaa, bbbbbbb; // comment                   ",
1081                    getLLVMStyleWithColumns(33)));
1082   EXPECT_EQ("// comment\\\n", format("// comment\\\n  \t \v   \f   "));
1083   EXPECT_EQ("// comment    \\\n", format("// comment    \\\n  \t \v   \f   "));
1084 }
1085 
1086 TEST_F(FormatTest, UnderstandsBlockComments) {
1087   verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
1088   verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
1089   EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
1090             "  bbbbbbbbbbbbbbbbbbbbbbbbb);",
1091             format("f(aaaaaaaaaaaaaaaaaaaaaaaaa ,   \\\n"
1092                    "/* Trailing comment for aa... */\n"
1093                    "  bbbbbbbbbbbbbbbbbbbbbbbbb);"));
1094   EXPECT_EQ(
1095       "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1096       "  /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
1097       format("f(aaaaaaaaaaaaaaaaaaaaaaaaa    ,   \n"
1098              "/* Leading comment for bb... */   bbbbbbbbbbbbbbbbbbbbbbbbb);"));
1099   EXPECT_EQ(
1100       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1101       "    aaaaaaaaaaaaaaaaaa,\n"
1102       "    aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
1103       "}",
1104       format("void      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1105              "                      aaaaaaaaaaaaaaaaaa  ,\n"
1106              "    aaaaaaaaaaaaaaaaaa) {   /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
1107              "}"));
1108 
1109   FormatStyle NoBinPacking = getLLVMStyle();
1110   NoBinPacking.BinPackParameters = false;
1111   verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
1112                "         /* parameter 2 */ aaaaaa,\n"
1113                "         /* parameter 3 */ aaaaaa,\n"
1114                "         /* parameter 4 */ aaaaaa);",
1115                NoBinPacking);
1116 
1117   // Aligning block comments in macros.
1118   verifyGoogleFormat("#define A        \\\n"
1119                      "  int i;   /*a*/ \\\n"
1120                      "  int jjj; /*b*/");
1121 }
1122 
1123 TEST_F(FormatTest, AlignsBlockComments) {
1124   EXPECT_EQ("/*\n"
1125             " * Really multi-line\n"
1126             " * comment.\n"
1127             " */\n"
1128             "void f() {}",
1129             format("  /*\n"
1130                    "   * Really multi-line\n"
1131                    "   * comment.\n"
1132                    "   */\n"
1133                    "  void f() {}"));
1134   EXPECT_EQ("class C {\n"
1135             "  /*\n"
1136             "   * Another multi-line\n"
1137             "   * comment.\n"
1138             "   */\n"
1139             "  void f() {}\n"
1140             "};",
1141             format("class C {\n"
1142                    "/*\n"
1143                    " * Another multi-line\n"
1144                    " * comment.\n"
1145                    " */\n"
1146                    "void f() {}\n"
1147                    "};"));
1148   EXPECT_EQ("/*\n"
1149             "  1. This is a comment with non-trivial formatting.\n"
1150             "     1.1. We have to indent/outdent all lines equally\n"
1151             "         1.1.1. to keep the formatting.\n"
1152             " */",
1153             format("  /*\n"
1154                    "    1. This is a comment with non-trivial formatting.\n"
1155                    "       1.1. We have to indent/outdent all lines equally\n"
1156                    "           1.1.1. to keep the formatting.\n"
1157                    "   */"));
1158   EXPECT_EQ("/*\n"
1159             "Don't try to outdent if there's not enough indentation.\n"
1160             "*/",
1161             format("  /*\n"
1162                    " Don't try to outdent if there's not enough indentation.\n"
1163                    " */"));
1164 
1165   EXPECT_EQ("int i; /* Comment with empty...\n"
1166             "        *\n"
1167             "        * line. */",
1168             format("int i; /* Comment with empty...\n"
1169                    "        *\n"
1170                    "        * line. */"));
1171   EXPECT_EQ("int foobar = 0; /* comment */\n"
1172             "int bar = 0;    /* multiline\n"
1173             "                   comment 1 */\n"
1174             "int baz = 0;    /* multiline\n"
1175             "                   comment 2 */\n"
1176             "int bzz = 0;    /* multiline\n"
1177             "                   comment 3 */",
1178             format("int foobar = 0; /* comment */\n"
1179                    "int bar = 0;    /* multiline\n"
1180                    "                   comment 1 */\n"
1181                    "int baz = 0; /* multiline\n"
1182                    "                comment 2 */\n"
1183                    "int bzz = 0;         /* multiline\n"
1184                    "                        comment 3 */"));
1185   EXPECT_EQ("int foobar = 0; /* comment */\n"
1186             "int bar = 0;    /* multiline\n"
1187             "   comment */\n"
1188             "int baz = 0;    /* multiline\n"
1189             "comment */",
1190             format("int foobar = 0; /* comment */\n"
1191                    "int bar = 0; /* multiline\n"
1192                    "comment */\n"
1193                    "int baz = 0;        /* multiline\n"
1194                    "comment */"));
1195 }
1196 
1197 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
1198   EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1199             "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
1200             format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1201                    "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
1202   EXPECT_EQ(
1203       "void ffffffffffff(\n"
1204       "    int aaaaaaaa, int bbbbbbbb,\n"
1205       "    int cccccccccccc) { /*\n"
1206       "                           aaaaaaaaaa\n"
1207       "                           aaaaaaaaaaaaa\n"
1208       "                           bbbbbbbbbbbbbb\n"
1209       "                           bbbbbbbbbb\n"
1210       "                         */\n"
1211       "}",
1212       format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
1213              "{ /*\n"
1214              "     aaaaaaaaaa aaaaaaaaaaaaa\n"
1215              "     bbbbbbbbbbbbbb bbbbbbbbbb\n"
1216              "   */\n"
1217              "}",
1218              getLLVMStyleWithColumns(40)));
1219 }
1220 
1221 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
1222   EXPECT_EQ("void ffffffffff(\n"
1223             "    int aaaaa /* test */);",
1224             format("void ffffffffff(int aaaaa /* test */);",
1225                    getLLVMStyleWithColumns(35)));
1226 }
1227 
1228 TEST_F(FormatTest, SplitsLongCxxComments) {
1229   EXPECT_EQ("// A comment that\n"
1230             "// doesn't fit on\n"
1231             "// one line",
1232             format("// A comment that doesn't fit on one line",
1233                    getLLVMStyleWithColumns(20)));
1234   EXPECT_EQ("/// A comment that\n"
1235             "/// doesn't fit on\n"
1236             "/// one line",
1237             format("/// A comment that doesn't fit on one line",
1238                    getLLVMStyleWithColumns(20)));
1239   EXPECT_EQ("//! A comment that\n"
1240             "//! doesn't fit on\n"
1241             "//! one line",
1242             format("//! A comment that doesn't fit on one line",
1243                    getLLVMStyleWithColumns(20)));
1244   EXPECT_EQ("// a b c d\n"
1245             "// e f  g\n"
1246             "// h i j k",
1247             format("// a b c d e f  g h i j k", getLLVMStyleWithColumns(10)));
1248   EXPECT_EQ(
1249       "// a b c d\n"
1250       "// e f  g\n"
1251       "// h i j k",
1252       format("\\\n// a b c d e f  g h i j k", getLLVMStyleWithColumns(10)));
1253   EXPECT_EQ("if (true) // A comment that\n"
1254             "          // doesn't fit on\n"
1255             "          // one line",
1256             format("if (true) // A comment that doesn't fit on one line   ",
1257                    getLLVMStyleWithColumns(30)));
1258   EXPECT_EQ("//    Don't_touch_leading_whitespace",
1259             format("//    Don't_touch_leading_whitespace",
1260                    getLLVMStyleWithColumns(20)));
1261   EXPECT_EQ("// Add leading\n"
1262             "// whitespace",
1263             format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
1264   EXPECT_EQ("/// Add leading\n"
1265             "/// whitespace",
1266             format("///Add leading whitespace", getLLVMStyleWithColumns(20)));
1267   EXPECT_EQ("//! Add leading\n"
1268             "//! whitespace",
1269             format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
1270   EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
1271   EXPECT_EQ("// Even if it makes the line exceed the column\n"
1272             "// limit",
1273             format("//Even if it makes the line exceed the column limit",
1274                    getLLVMStyleWithColumns(51)));
1275   EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
1276 
1277   EXPECT_EQ("// aa bb cc dd",
1278             format("// aa bb             cc dd                   ",
1279                    getLLVMStyleWithColumns(15)));
1280 
1281   EXPECT_EQ("// A comment before\n"
1282             "// a macro\n"
1283             "// definition\n"
1284             "#define a b",
1285             format("// A comment before a macro definition\n"
1286                    "#define a b",
1287                    getLLVMStyleWithColumns(20)));
1288   EXPECT_EQ("void ffffff(\n"
1289             "    int aaaaaaaaa,  // wwww\n"
1290             "    int bbbbbbbbbb, // xxxxxxx\n"
1291             "                    // yyyyyyyyyy\n"
1292             "    int c, int d, int e) {}",
1293             format("void ffffff(\n"
1294                    "    int aaaaaaaaa, // wwww\n"
1295                    "    int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
1296                    "    int c, int d, int e) {}",
1297                    getLLVMStyleWithColumns(40)));
1298   EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1299             format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1300                    getLLVMStyleWithColumns(20)));
1301   EXPECT_EQ(
1302       "#define XXX // a b c d\n"
1303       "            // e f g h",
1304       format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
1305   EXPECT_EQ(
1306       "#define XXX // q w e r\n"
1307       "            // t y u i",
1308       format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
1309 }
1310 
1311 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
1312   EXPECT_EQ("//     A comment\n"
1313             "//     that doesn't\n"
1314             "//     fit on one\n"
1315             "//     line",
1316             format("//     A comment that doesn't fit on one line",
1317                    getLLVMStyleWithColumns(20)));
1318   EXPECT_EQ("///     A comment\n"
1319             "///     that doesn't\n"
1320             "///     fit on one\n"
1321             "///     line",
1322             format("///     A comment that doesn't fit on one line",
1323                    getLLVMStyleWithColumns(20)));
1324 }
1325 
1326 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
1327   EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1328             "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1329             "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1330             format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1331                    "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1332                    "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
1333   EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1334             "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1335             "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1336             format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1337                    "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1338                    "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1339                    getLLVMStyleWithColumns(50)));
1340   // FIXME: One day we might want to implement adjustment of leading whitespace
1341   // of the consecutive lines in this kind of comment:
1342   EXPECT_EQ("double\n"
1343             "    a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1344             "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1345             "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1346             format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1347                    "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1348                    "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1349                    getLLVMStyleWithColumns(49)));
1350 }
1351 
1352 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
1353   FormatStyle Pragmas = getLLVMStyleWithColumns(30);
1354   Pragmas.CommentPragmas = "^ IWYU pragma:";
1355   EXPECT_EQ(
1356       "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
1357       format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
1358   EXPECT_EQ(
1359       "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
1360       format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
1361 }
1362 
1363 TEST_F(FormatTest, PriorityOfCommentBreaking) {
1364   EXPECT_EQ("if (xxx ==\n"
1365             "        yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
1366             "    zzz)\n"
1367             "  q();",
1368             format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
1369                    "    zzz) q();",
1370                    getLLVMStyleWithColumns(40)));
1371   EXPECT_EQ("if (xxxxxxxxxx ==\n"
1372             "        yyy && // aaaaaa bbbbbbbb cccc\n"
1373             "    zzz)\n"
1374             "  q();",
1375             format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
1376                    "    zzz) q();",
1377                    getLLVMStyleWithColumns(40)));
1378   EXPECT_EQ("if (xxxxxxxxxx &&\n"
1379             "        yyy || // aaaaaa bbbbbbbb cccc\n"
1380             "    zzz)\n"
1381             "  q();",
1382             format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
1383                    "    zzz) q();",
1384                    getLLVMStyleWithColumns(40)));
1385   EXPECT_EQ("fffffffff(\n"
1386             "    &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
1387             "    zzz);",
1388             format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
1389                    " zzz);",
1390                    getLLVMStyleWithColumns(40)));
1391 }
1392 
1393 TEST_F(FormatTest, MultiLineCommentsInDefines) {
1394   EXPECT_EQ("#define A(x) /* \\\n"
1395             "  a comment     \\\n"
1396             "  inside */     \\\n"
1397             "  f();",
1398             format("#define A(x) /* \\\n"
1399                    "  a comment     \\\n"
1400                    "  inside */     \\\n"
1401                    "  f();",
1402                    getLLVMStyleWithColumns(17)));
1403   EXPECT_EQ("#define A(      \\\n"
1404             "    x) /*       \\\n"
1405             "  a comment     \\\n"
1406             "  inside */     \\\n"
1407             "  f();",
1408             format("#define A(      \\\n"
1409                    "    x) /*       \\\n"
1410                    "  a comment     \\\n"
1411                    "  inside */     \\\n"
1412                    "  f();",
1413                    getLLVMStyleWithColumns(17)));
1414 }
1415 
1416 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
1417   EXPECT_EQ("namespace {}\n// Test\n#define A",
1418             format("namespace {}\n   // Test\n#define A"));
1419   EXPECT_EQ("namespace {}\n/* Test */\n#define A",
1420             format("namespace {}\n   /* Test */\n#define A"));
1421   EXPECT_EQ("namespace {}\n/* Test */ #define A",
1422             format("namespace {}\n   /* Test */    #define A"));
1423 }
1424 
1425 TEST_F(FormatTest, SplitsLongLinesInComments) {
1426   EXPECT_EQ("/* This is a long\n"
1427             " * comment that\n"
1428             " * doesn't\n"
1429             " * fit on one line.\n"
1430             " */",
1431             format("/* "
1432                    "This is a long                                         "
1433                    "comment that "
1434                    "doesn't                                    "
1435                    "fit on one line.  */",
1436                    getLLVMStyleWithColumns(20)));
1437   EXPECT_EQ(
1438       "/* a b c d\n"
1439       " * e f  g\n"
1440       " * h i j k\n"
1441       " */",
1442       format("/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
1443   EXPECT_EQ(
1444       "/* a b c d\n"
1445       " * e f  g\n"
1446       " * h i j k\n"
1447       " */",
1448       format("\\\n/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
1449   EXPECT_EQ("/*\n"
1450             "This is a long\n"
1451             "comment that doesn't\n"
1452             "fit on one line.\n"
1453             "*/",
1454             format("/*\n"
1455                    "This is a long                                         "
1456                    "comment that doesn't                                    "
1457                    "fit on one line.                                      \n"
1458                    "*/",
1459                    getLLVMStyleWithColumns(20)));
1460   EXPECT_EQ("/*\n"
1461             " * This is a long\n"
1462             " * comment that\n"
1463             " * doesn't fit on\n"
1464             " * one line.\n"
1465             " */",
1466             format("/*      \n"
1467                    " * This is a long "
1468                    "   comment that     "
1469                    "   doesn't fit on   "
1470                    "   one line.                                            \n"
1471                    " */",
1472                    getLLVMStyleWithColumns(20)));
1473   EXPECT_EQ("/*\n"
1474             " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
1475             " * so_it_should_be_broken\n"
1476             " * wherever_a_space_occurs\n"
1477             " */",
1478             format("/*\n"
1479                    " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
1480                    "   so_it_should_be_broken "
1481                    "   wherever_a_space_occurs                             \n"
1482                    " */",
1483                    getLLVMStyleWithColumns(20)));
1484   EXPECT_EQ("/*\n"
1485             " *    This_comment_can_not_be_broken_into_lines\n"
1486             " */",
1487             format("/*\n"
1488                    " *    This_comment_can_not_be_broken_into_lines\n"
1489                    " */",
1490                    getLLVMStyleWithColumns(20)));
1491   EXPECT_EQ("{\n"
1492             "  /*\n"
1493             "  This is another\n"
1494             "  long comment that\n"
1495             "  doesn't fit on one\n"
1496             "  line    1234567890\n"
1497             "  */\n"
1498             "}",
1499             format("{\n"
1500                    "/*\n"
1501                    "This is another     "
1502                    "  long comment that "
1503                    "  doesn't fit on one"
1504                    "  line    1234567890\n"
1505                    "*/\n"
1506                    "}",
1507                    getLLVMStyleWithColumns(20)));
1508   EXPECT_EQ("{\n"
1509             "  /*\n"
1510             "   * This        i s\n"
1511             "   * another comment\n"
1512             "   * t hat  doesn' t\n"
1513             "   * fit on one l i\n"
1514             "   * n e\n"
1515             "   */\n"
1516             "}",
1517             format("{\n"
1518                    "/*\n"
1519                    " * This        i s"
1520                    "   another comment"
1521                    "   t hat  doesn' t"
1522                    "   fit on one l i"
1523                    "   n e\n"
1524                    " */\n"
1525                    "}",
1526                    getLLVMStyleWithColumns(20)));
1527   EXPECT_EQ("/*\n"
1528             " * This is a long\n"
1529             " * comment that\n"
1530             " * doesn't fit on\n"
1531             " * one line\n"
1532             " */",
1533             format("   /*\n"
1534                    "    * This is a long comment that doesn't fit on one line\n"
1535                    "    */",
1536                    getLLVMStyleWithColumns(20)));
1537   EXPECT_EQ("{\n"
1538             "  if (something) /* This is a\n"
1539             "                    long\n"
1540             "                    comment */\n"
1541             "    ;\n"
1542             "}",
1543             format("{\n"
1544                    "  if (something) /* This is a long comment */\n"
1545                    "    ;\n"
1546                    "}",
1547                    getLLVMStyleWithColumns(30)));
1548 
1549   EXPECT_EQ("/* A comment before\n"
1550             " * a macro\n"
1551             " * definition */\n"
1552             "#define a b",
1553             format("/* A comment before a macro definition */\n"
1554                    "#define a b",
1555                    getLLVMStyleWithColumns(20)));
1556 
1557   EXPECT_EQ("/* some comment\n"
1558             "     *   a comment\n"
1559             "* that we break\n"
1560             " * another comment\n"
1561             "* we have to break\n"
1562             "* a left comment\n"
1563             " */",
1564             format("  /* some comment\n"
1565                    "       *   a comment that we break\n"
1566                    "   * another comment we have to break\n"
1567                    "* a left comment\n"
1568                    "   */",
1569                    getLLVMStyleWithColumns(20)));
1570 
1571   EXPECT_EQ("/**\n"
1572             " * multiline block\n"
1573             " * comment\n"
1574             " *\n"
1575             " */",
1576             format("/**\n"
1577                    " * multiline block comment\n"
1578                    " *\n"
1579                    " */",
1580                    getLLVMStyleWithColumns(20)));
1581 
1582   EXPECT_EQ("/*\n"
1583             "\n"
1584             "\n"
1585             "    */\n",
1586             format("  /*       \n"
1587                    "      \n"
1588                    "               \n"
1589                    "      */\n"));
1590 
1591   EXPECT_EQ("/* a a */",
1592             format("/* a a            */", getLLVMStyleWithColumns(15)));
1593   EXPECT_EQ("/* a a bc  */",
1594             format("/* a a            bc  */", getLLVMStyleWithColumns(15)));
1595   EXPECT_EQ("/* aaa aaa\n"
1596             " * aaaaa */",
1597             format("/* aaa aaa aaaaa       */", getLLVMStyleWithColumns(15)));
1598   EXPECT_EQ("/* aaa aaa\n"
1599             " * aaaaa     */",
1600             format("/* aaa aaa aaaaa     */", getLLVMStyleWithColumns(15)));
1601 }
1602 
1603 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
1604   EXPECT_EQ("#define X          \\\n"
1605             "  /*               \\\n"
1606             "   Test            \\\n"
1607             "   Macro comment   \\\n"
1608             "   with a long     \\\n"
1609             "   line            \\\n"
1610             "   */              \\\n"
1611             "  A + B",
1612             format("#define X \\\n"
1613                    "  /*\n"
1614                    "   Test\n"
1615                    "   Macro comment with a long  line\n"
1616                    "   */ \\\n"
1617                    "  A + B",
1618                    getLLVMStyleWithColumns(20)));
1619   EXPECT_EQ("#define X          \\\n"
1620             "  /* Macro comment \\\n"
1621             "     with a long   \\\n"
1622             "     line */       \\\n"
1623             "  A + B",
1624             format("#define X \\\n"
1625                    "  /* Macro comment with a long\n"
1626                    "     line */ \\\n"
1627                    "  A + B",
1628                    getLLVMStyleWithColumns(20)));
1629   EXPECT_EQ("#define X          \\\n"
1630             "  /* Macro comment \\\n"
1631             "   * with a long   \\\n"
1632             "   * line */       \\\n"
1633             "  A + B",
1634             format("#define X \\\n"
1635                    "  /* Macro comment with a long  line */ \\\n"
1636                    "  A + B",
1637                    getLLVMStyleWithColumns(20)));
1638 }
1639 
1640 TEST_F(FormatTest, CommentsInStaticInitializers) {
1641   EXPECT_EQ(
1642       "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
1643       "                        aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
1644       "                        /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
1645       "                        aaaaaaaaaaaaaaaaaaaa, // comment\n"
1646       "                        aaaaaaaaaaaaaaaaaaaa};",
1647       format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa  ,  /* comment */\n"
1648              "                   aaaaaaaaaaaaaaaaaaaa   /* comment */ ,\n"
1649              "                     /* comment */   aaaaaaaaaaaaaaaaaaaa ,\n"
1650              "              aaaaaaaaaaaaaaaaaaaa ,   // comment\n"
1651              "                  aaaaaaaaaaaaaaaaaaaa };"));
1652   verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1653                "                        bbbbbbbbbbb, ccccccccccc};");
1654   verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
1655                "                        // comment for bb....\n"
1656                "                        bbbbbbbbbbb, ccccccccccc};");
1657   verifyGoogleFormat(
1658       "static SomeType type = {aaaaaaaaaaa,  // comment for aa...\n"
1659       "                        bbbbbbbbbbb, ccccccccccc};");
1660   verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
1661                      "                        // comment for bb....\n"
1662                      "                        bbbbbbbbbbb, ccccccccccc};");
1663 
1664   verifyFormat("S s = {{a, b, c},  // Group #1\n"
1665                "       {d, e, f},  // Group #2\n"
1666                "       {g, h, i}}; // Group #3");
1667   verifyFormat("S s = {{// Group #1\n"
1668                "        a, b, c},\n"
1669                "       {// Group #2\n"
1670                "        d, e, f},\n"
1671                "       {// Group #3\n"
1672                "        g, h, i}};");
1673 
1674   EXPECT_EQ("S s = {\n"
1675             "    // Some comment\n"
1676             "    a,\n"
1677             "\n"
1678             "    // Comment after empty line\n"
1679             "    b}",
1680             format("S s =    {\n"
1681                    "      // Some comment\n"
1682                    "  a,\n"
1683                    "  \n"
1684                    "     // Comment after empty line\n"
1685                    "      b\n"
1686                    "}"));
1687   EXPECT_EQ("S s = {\n"
1688             "    /* Some comment */\n"
1689             "    a,\n"
1690             "\n"
1691             "    /* Comment after empty line */\n"
1692             "    b}",
1693             format("S s =    {\n"
1694                    "      /* Some comment */\n"
1695                    "  a,\n"
1696                    "  \n"
1697                    "     /* Comment after empty line */\n"
1698                    "      b\n"
1699                    "}"));
1700   verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
1701                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1702                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1703                "    0x00, 0x00, 0x00, 0x00};            // comment\n");
1704 }
1705 
1706 TEST_F(FormatTest, IgnoresIf0Contents) {
1707   EXPECT_EQ("#if 0\n"
1708             "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1709             "#endif\n"
1710             "void f() {}",
1711             format("#if 0\n"
1712                    "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1713                    "#endif\n"
1714                    "void f(  ) {  }"));
1715   EXPECT_EQ("#if false\n"
1716             "void f(  ) {  }\n"
1717             "#endif\n"
1718             "void g() {}\n",
1719             format("#if false\n"
1720                    "void f(  ) {  }\n"
1721                    "#endif\n"
1722                    "void g(  ) {  }\n"));
1723   EXPECT_EQ("enum E {\n"
1724             "  One,\n"
1725             "  Two,\n"
1726             "#if 0\n"
1727             "Three,\n"
1728             "      Four,\n"
1729             "#endif\n"
1730             "  Five\n"
1731             "};",
1732             format("enum E {\n"
1733                    "  One,Two,\n"
1734                    "#if 0\n"
1735                    "Three,\n"
1736                    "      Four,\n"
1737                    "#endif\n"
1738                    "  Five};"));
1739   EXPECT_EQ("enum F {\n"
1740             "  One,\n"
1741             "#if 1\n"
1742             "  Two,\n"
1743             "#if 0\n"
1744             "Three,\n"
1745             "      Four,\n"
1746             "#endif\n"
1747             "  Five\n"
1748             "#endif\n"
1749             "};",
1750             format("enum F {\n"
1751                    "One,\n"
1752                    "#if 1\n"
1753                    "Two,\n"
1754                    "#if 0\n"
1755                    "Three,\n"
1756                    "      Four,\n"
1757                    "#endif\n"
1758                    "Five\n"
1759                    "#endif\n"
1760                    "};"));
1761   EXPECT_EQ("enum G {\n"
1762             "  One,\n"
1763             "#if 0\n"
1764             "Two,\n"
1765             "#else\n"
1766             "  Three,\n"
1767             "#endif\n"
1768             "  Four\n"
1769             "};",
1770             format("enum G {\n"
1771                    "One,\n"
1772                    "#if 0\n"
1773                    "Two,\n"
1774                    "#else\n"
1775                    "Three,\n"
1776                    "#endif\n"
1777                    "Four\n"
1778                    "};"));
1779   EXPECT_EQ("enum H {\n"
1780             "  One,\n"
1781             "#if 0\n"
1782             "#ifdef Q\n"
1783             "Two,\n"
1784             "#else\n"
1785             "Three,\n"
1786             "#endif\n"
1787             "#endif\n"
1788             "  Four\n"
1789             "};",
1790             format("enum H {\n"
1791                    "One,\n"
1792                    "#if 0\n"
1793                    "#ifdef Q\n"
1794                    "Two,\n"
1795                    "#else\n"
1796                    "Three,\n"
1797                    "#endif\n"
1798                    "#endif\n"
1799                    "Four\n"
1800                    "};"));
1801   EXPECT_EQ("enum I {\n"
1802             "  One,\n"
1803             "#if /* test */ 0 || 1\n"
1804             "Two,\n"
1805             "Three,\n"
1806             "#endif\n"
1807             "  Four\n"
1808             "};",
1809             format("enum I {\n"
1810                    "One,\n"
1811                    "#if /* test */ 0 || 1\n"
1812                    "Two,\n"
1813                    "Three,\n"
1814                    "#endif\n"
1815                    "Four\n"
1816                    "};"));
1817   EXPECT_EQ("enum J {\n"
1818             "  One,\n"
1819             "#if 0\n"
1820             "#if 0\n"
1821             "Two,\n"
1822             "#else\n"
1823             "Three,\n"
1824             "#endif\n"
1825             "Four,\n"
1826             "#endif\n"
1827             "  Five\n"
1828             "};",
1829             format("enum J {\n"
1830                    "One,\n"
1831                    "#if 0\n"
1832                    "#if 0\n"
1833                    "Two,\n"
1834                    "#else\n"
1835                    "Three,\n"
1836                    "#endif\n"
1837                    "Four,\n"
1838                    "#endif\n"
1839                    "Five\n"
1840                    "};"));
1841 }
1842 
1843 //===----------------------------------------------------------------------===//
1844 // Tests for classes, namespaces, etc.
1845 //===----------------------------------------------------------------------===//
1846 
1847 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1848   verifyFormat("class A {};");
1849 }
1850 
1851 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1852   verifyFormat("class A {\n"
1853                "public:\n"
1854                "public: // comment\n"
1855                "protected:\n"
1856                "private:\n"
1857                "  void f() {}\n"
1858                "};");
1859   verifyGoogleFormat("class A {\n"
1860                      " public:\n"
1861                      " protected:\n"
1862                      " private:\n"
1863                      "  void f() {}\n"
1864                      "};");
1865   verifyFormat("class A {\n"
1866                "public slots:\n"
1867                "  void f() {}\n"
1868                "public Q_SLOTS:\n"
1869                "  void f() {}\n"
1870                "signals:\n"
1871                "  void g();\n"
1872                "};");
1873 
1874   // Don't interpret 'signals' the wrong way.
1875   verifyFormat("signals.set();");
1876   verifyFormat("for (Signals signals : f()) {\n}");
1877   verifyFormat("{\n"
1878                "  signals.set(); // This needs indentation.\n"
1879                "}");
1880 }
1881 
1882 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1883   EXPECT_EQ("class A {\n"
1884             "public:\n"
1885             "  void f();\n"
1886             "\n"
1887             "private:\n"
1888             "  void g() {}\n"
1889             "  // test\n"
1890             "protected:\n"
1891             "  int h;\n"
1892             "};",
1893             format("class A {\n"
1894                    "public:\n"
1895                    "void f();\n"
1896                    "private:\n"
1897                    "void g() {}\n"
1898                    "// test\n"
1899                    "protected:\n"
1900                    "int h;\n"
1901                    "};"));
1902   EXPECT_EQ("class A {\n"
1903             "protected:\n"
1904             "public:\n"
1905             "  void f();\n"
1906             "};",
1907             format("class A {\n"
1908                    "protected:\n"
1909                    "\n"
1910                    "public:\n"
1911                    "\n"
1912                    "  void f();\n"
1913                    "};"));
1914 
1915   // Even ensure proper spacing inside macros.
1916   EXPECT_EQ("#define B     \\\n"
1917             "  class A {   \\\n"
1918             "   protected: \\\n"
1919             "   public:    \\\n"
1920             "    void f(); \\\n"
1921             "  };",
1922             format("#define B     \\\n"
1923                    "  class A {   \\\n"
1924                    "   protected: \\\n"
1925                    "              \\\n"
1926                    "   public:    \\\n"
1927                    "              \\\n"
1928                    "    void f(); \\\n"
1929                    "  };",
1930                    getGoogleStyle()));
1931   // But don't remove empty lines after macros ending in access specifiers.
1932   EXPECT_EQ("#define A private:\n"
1933             "\n"
1934             "int i;",
1935             format("#define A         private:\n"
1936                    "\n"
1937                    "int              i;"));
1938 }
1939 
1940 TEST_F(FormatTest, FormatsClasses) {
1941   verifyFormat("class A : public B {};");
1942   verifyFormat("class A : public ::B {};");
1943 
1944   verifyFormat(
1945       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1946       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1947   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1948                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1949                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1950   verifyFormat(
1951       "class A : public B, public C, public D, public E, public F {};");
1952   verifyFormat("class AAAAAAAAAAAA : public B,\n"
1953                "                     public C,\n"
1954                "                     public D,\n"
1955                "                     public E,\n"
1956                "                     public F,\n"
1957                "                     public G {};");
1958 
1959   verifyFormat("class\n"
1960                "    ReallyReallyLongClassName {\n"
1961                "  int i;\n"
1962                "};",
1963                getLLVMStyleWithColumns(32));
1964   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
1965                "                           aaaaaaaaaaaaaaaa> {};");
1966   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
1967                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
1968                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
1969   verifyFormat("template <class R, class C>\n"
1970                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
1971                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
1972   verifyFormat("class ::A::B {};");
1973 }
1974 
1975 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1976   verifyFormat("class A {\n} a, b;");
1977   verifyFormat("struct A {\n} a, b;");
1978   verifyFormat("union A {\n} a;");
1979 }
1980 
1981 TEST_F(FormatTest, FormatsEnum) {
1982   verifyFormat("enum {\n"
1983                "  Zero,\n"
1984                "  One = 1,\n"
1985                "  Two = One + 1,\n"
1986                "  Three = (One + Two),\n"
1987                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1988                "  Five = (One, Two, Three, Four, 5)\n"
1989                "};");
1990   verifyGoogleFormat("enum {\n"
1991                      "  Zero,\n"
1992                      "  One = 1,\n"
1993                      "  Two = One + 1,\n"
1994                      "  Three = (One + Two),\n"
1995                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1996                      "  Five = (One, Two, Three, Four, 5)\n"
1997                      "};");
1998   verifyFormat("enum Enum {};");
1999   verifyFormat("enum {};");
2000   verifyFormat("enum X E {} d;");
2001   verifyFormat("enum __attribute__((...)) E {} d;");
2002   verifyFormat("enum __declspec__((...)) E {} d;");
2003   verifyFormat("enum {\n"
2004                "  Bar = Foo<int, int>::value\n"
2005                "};",
2006                getLLVMStyleWithColumns(30));
2007 
2008   verifyFormat("enum ShortEnum { A, B, C };");
2009   verifyGoogleFormat("enum ShortEnum { A, B, C };");
2010 
2011   EXPECT_EQ("enum KeepEmptyLines {\n"
2012             "  ONE,\n"
2013             "\n"
2014             "  TWO,\n"
2015             "\n"
2016             "  THREE\n"
2017             "}",
2018             format("enum KeepEmptyLines {\n"
2019                    "  ONE,\n"
2020                    "\n"
2021                    "  TWO,\n"
2022                    "\n"
2023                    "\n"
2024                    "  THREE\n"
2025                    "}"));
2026   verifyFormat("enum E { // comment\n"
2027                "  ONE,\n"
2028                "  TWO\n"
2029                "};\n"
2030                "int i;");
2031   // Not enums.
2032   verifyFormat("enum X f() {\n"
2033                "  a();\n"
2034                "  return 42;\n"
2035                "}");
2036   verifyFormat("enum X Type::f() {\n"
2037                "  a();\n"
2038                "  return 42;\n"
2039                "}");
2040   verifyFormat("enum ::X f() {\n"
2041                "  a();\n"
2042                "  return 42;\n"
2043                "}");
2044   verifyFormat("enum ns::X f() {\n"
2045                "  a();\n"
2046                "  return 42;\n"
2047                "}");
2048 }
2049 
2050 TEST_F(FormatTest, FormatsEnumsWithErrors) {
2051   verifyFormat("enum Type {\n"
2052                "  One = 0; // These semicolons should be commas.\n"
2053                "  Two = 1;\n"
2054                "};");
2055   verifyFormat("namespace n {\n"
2056                "enum Type {\n"
2057                "  One,\n"
2058                "  Two, // missing };\n"
2059                "  int i;\n"
2060                "}\n"
2061                "void g() {}");
2062 }
2063 
2064 TEST_F(FormatTest, FormatsEnumStruct) {
2065   verifyFormat("enum struct {\n"
2066                "  Zero,\n"
2067                "  One = 1,\n"
2068                "  Two = One + 1,\n"
2069                "  Three = (One + Two),\n"
2070                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2071                "  Five = (One, Two, Three, Four, 5)\n"
2072                "};");
2073   verifyFormat("enum struct Enum {};");
2074   verifyFormat("enum struct {};");
2075   verifyFormat("enum struct X E {} d;");
2076   verifyFormat("enum struct __attribute__((...)) E {} d;");
2077   verifyFormat("enum struct __declspec__((...)) E {} d;");
2078   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
2079 }
2080 
2081 TEST_F(FormatTest, FormatsEnumClass) {
2082   verifyFormat("enum class {\n"
2083                "  Zero,\n"
2084                "  One = 1,\n"
2085                "  Two = One + 1,\n"
2086                "  Three = (One + Two),\n"
2087                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2088                "  Five = (One, Two, Three, Four, 5)\n"
2089                "};");
2090   verifyFormat("enum class Enum {};");
2091   verifyFormat("enum class {};");
2092   verifyFormat("enum class X E {} d;");
2093   verifyFormat("enum class __attribute__((...)) E {} d;");
2094   verifyFormat("enum class __declspec__((...)) E {} d;");
2095   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
2096 }
2097 
2098 TEST_F(FormatTest, FormatsEnumTypes) {
2099   verifyFormat("enum X : int {\n"
2100                "  A, // Force multiple lines.\n"
2101                "  B\n"
2102                "};");
2103   verifyFormat("enum X : int { A, B };");
2104   verifyFormat("enum X : std::uint32_t { A, B };");
2105 }
2106 
2107 TEST_F(FormatTest, FormatsNSEnums) {
2108   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
2109   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
2110                      "  // Information about someDecentlyLongValue.\n"
2111                      "  someDecentlyLongValue,\n"
2112                      "  // Information about anotherDecentlyLongValue.\n"
2113                      "  anotherDecentlyLongValue,\n"
2114                      "  // Information about aThirdDecentlyLongValue.\n"
2115                      "  aThirdDecentlyLongValue\n"
2116                      "};");
2117   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
2118                      "  a = 1,\n"
2119                      "  b = 2,\n"
2120                      "  c = 3,\n"
2121                      "};");
2122   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
2123                      "  a = 1,\n"
2124                      "  b = 2,\n"
2125                      "  c = 3,\n"
2126                      "};");
2127   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
2128                      "  a = 1,\n"
2129                      "  b = 2,\n"
2130                      "  c = 3,\n"
2131                      "};");
2132 }
2133 
2134 TEST_F(FormatTest, FormatsBitfields) {
2135   verifyFormat("struct Bitfields {\n"
2136                "  unsigned sClass : 8;\n"
2137                "  unsigned ValueKind : 2;\n"
2138                "};");
2139   verifyFormat("struct A {\n"
2140                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
2141                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
2142                "};");
2143   verifyFormat("struct MyStruct {\n"
2144                "  uchar data;\n"
2145                "  uchar : 8;\n"
2146                "  uchar : 8;\n"
2147                "  uchar other;\n"
2148                "};");
2149 }
2150 
2151 TEST_F(FormatTest, FormatsNamespaces) {
2152   verifyFormat("namespace some_namespace {\n"
2153                "class A {};\n"
2154                "void f() { f(); }\n"
2155                "}");
2156   verifyFormat("namespace {\n"
2157                "class A {};\n"
2158                "void f() { f(); }\n"
2159                "}");
2160   verifyFormat("inline namespace X {\n"
2161                "class A {};\n"
2162                "void f() { f(); }\n"
2163                "}");
2164   verifyFormat("using namespace some_namespace;\n"
2165                "class A {};\n"
2166                "void f() { f(); }");
2167 
2168   // This code is more common than we thought; if we
2169   // layout this correctly the semicolon will go into
2170   // its own line, which is undesirable.
2171   verifyFormat("namespace {};");
2172   verifyFormat("namespace {\n"
2173                "class A {};\n"
2174                "};");
2175 
2176   verifyFormat("namespace {\n"
2177                "int SomeVariable = 0; // comment\n"
2178                "} // namespace");
2179   EXPECT_EQ("#ifndef HEADER_GUARD\n"
2180             "#define HEADER_GUARD\n"
2181             "namespace my_namespace {\n"
2182             "int i;\n"
2183             "} // my_namespace\n"
2184             "#endif // HEADER_GUARD",
2185             format("#ifndef HEADER_GUARD\n"
2186                    " #define HEADER_GUARD\n"
2187                    "   namespace my_namespace {\n"
2188                    "int i;\n"
2189                    "}    // my_namespace\n"
2190                    "#endif    // HEADER_GUARD"));
2191 
2192   FormatStyle Style = getLLVMStyle();
2193   Style.NamespaceIndentation = FormatStyle::NI_All;
2194   EXPECT_EQ("namespace out {\n"
2195             "  int i;\n"
2196             "  namespace in {\n"
2197             "    int i;\n"
2198             "  } // namespace\n"
2199             "} // namespace",
2200             format("namespace out {\n"
2201                    "int i;\n"
2202                    "namespace in {\n"
2203                    "int i;\n"
2204                    "} // namespace\n"
2205                    "} // namespace",
2206                    Style));
2207 
2208   Style.NamespaceIndentation = FormatStyle::NI_Inner;
2209   EXPECT_EQ("namespace out {\n"
2210             "int i;\n"
2211             "namespace in {\n"
2212             "  int i;\n"
2213             "} // namespace\n"
2214             "} // namespace",
2215             format("namespace out {\n"
2216                    "int i;\n"
2217                    "namespace in {\n"
2218                    "int i;\n"
2219                    "} // namespace\n"
2220                    "} // namespace",
2221                    Style));
2222 }
2223 
2224 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
2225 
2226 TEST_F(FormatTest, FormatsInlineASM) {
2227   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
2228   verifyFormat("asm(\"nop\" ::: \"memory\");");
2229   verifyFormat(
2230       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
2231       "    \"cpuid\\n\\t\"\n"
2232       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
2233       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
2234       "    : \"a\"(value));");
2235   EXPECT_EQ(
2236       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
2237       "  __asm {\n"
2238       "        mov     edx,[that] // vtable in edx\n"
2239       "        mov     eax,methodIndex\n"
2240       "        call    [edx][eax*4] // stdcall\n"
2241       "  }\n"
2242       "}",
2243       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
2244              "    __asm {\n"
2245              "        mov     edx,[that] // vtable in edx\n"
2246              "        mov     eax,methodIndex\n"
2247              "        call    [edx][eax*4] // stdcall\n"
2248              "    }\n"
2249              "}"));
2250   EXPECT_EQ("_asm {\n"
2251             "  xor eax, eax;\n"
2252             "  cpuid;\n"
2253             "}",
2254             format("_asm {\n"
2255                    "  xor eax, eax;\n"
2256                    "  cpuid;\n"
2257                    "}"));
2258   verifyFormat("void function() {\n"
2259                "  // comment\n"
2260                "  asm(\"\");\n"
2261                "}");
2262   EXPECT_EQ("__asm {\n"
2263             "}\n"
2264             "int i;",
2265             format("__asm   {\n"
2266                    "}\n"
2267                    "int   i;"));
2268 }
2269 
2270 TEST_F(FormatTest, FormatTryCatch) {
2271   verifyFormat("try {\n"
2272                "  throw a * b;\n"
2273                "} catch (int a) {\n"
2274                "  // Do nothing.\n"
2275                "} catch (...) {\n"
2276                "  exit(42);\n"
2277                "}");
2278 
2279   // Function-level try statements.
2280   verifyFormat("int f() try { return 4; } catch (...) {\n"
2281                "  return 5;\n"
2282                "}");
2283   verifyFormat("class A {\n"
2284                "  int a;\n"
2285                "  A() try : a(0) {\n"
2286                "  } catch (...) {\n"
2287                "    throw;\n"
2288                "  }\n"
2289                "};\n");
2290 
2291   // Incomplete try-catch blocks.
2292   verifyIncompleteFormat("try {} catch (");
2293 }
2294 
2295 TEST_F(FormatTest, FormatSEHTryCatch) {
2296   verifyFormat("__try {\n"
2297                "  int a = b * c;\n"
2298                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
2299                "  // Do nothing.\n"
2300                "}");
2301 
2302   verifyFormat("__try {\n"
2303                "  int a = b * c;\n"
2304                "} __finally {\n"
2305                "  // Do nothing.\n"
2306                "}");
2307 
2308   verifyFormat("DEBUG({\n"
2309                "  __try {\n"
2310                "  } __finally {\n"
2311                "  }\n"
2312                "});\n");
2313 }
2314 
2315 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
2316   verifyFormat("try {\n"
2317                "  f();\n"
2318                "} catch {\n"
2319                "  g();\n"
2320                "}");
2321   verifyFormat("try {\n"
2322                "  f();\n"
2323                "} catch (A a) MACRO(x) {\n"
2324                "  g();\n"
2325                "} catch (B b) MACRO(x) {\n"
2326                "  g();\n"
2327                "}");
2328 }
2329 
2330 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
2331   FormatStyle Style = getLLVMStyle();
2332   Style.BreakBeforeBraces = FormatStyle::BS_Attach;
2333   verifyFormat("try {\n"
2334                "  // something\n"
2335                "} catch (...) {\n"
2336                "  // something\n"
2337                "}",
2338                Style);
2339   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
2340   verifyFormat("try {\n"
2341                "  // something\n"
2342                "}\n"
2343                "catch (...) {\n"
2344                "  // something\n"
2345                "}",
2346                Style);
2347   verifyFormat("__try {\n"
2348                "  // something\n"
2349                "}\n"
2350                "__finally {\n"
2351                "  // something\n"
2352                "}",
2353                Style);
2354   verifyFormat("@try {\n"
2355                "  // something\n"
2356                "}\n"
2357                "@finally {\n"
2358                "  // something\n"
2359                "}",
2360                Style);
2361   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2362   verifyFormat("try\n"
2363                "{\n"
2364                "  // something\n"
2365                "}\n"
2366                "catch (...)\n"
2367                "{\n"
2368                "  // something\n"
2369                "}",
2370                Style);
2371   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
2372   verifyFormat("try\n"
2373                "  {\n"
2374                "    // something\n"
2375                "  }\n"
2376                "catch (...)\n"
2377                "  {\n"
2378                "    // something\n"
2379                "  }",
2380                Style);
2381 }
2382 
2383 TEST_F(FormatTest, FormatObjCTryCatch) {
2384   verifyFormat("@try {\n"
2385                "  f();\n"
2386                "} @catch (NSException e) {\n"
2387                "  @throw;\n"
2388                "} @finally {\n"
2389                "  exit(42);\n"
2390                "}");
2391   verifyFormat("DEBUG({\n"
2392                "  @try {\n"
2393                "  } @finally {\n"
2394                "  }\n"
2395                "});\n");
2396 }
2397 
2398 TEST_F(FormatTest, FormatObjCAutoreleasepool) {
2399   FormatStyle Style = getLLVMStyle();
2400   verifyFormat("@autoreleasepool {\n"
2401                "  f();\n"
2402                "}\n"
2403                "@autoreleasepool {\n"
2404                "  f();\n"
2405                "}\n",
2406                Style);
2407   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2408   verifyFormat("@autoreleasepool\n"
2409                "{\n"
2410                "  f();\n"
2411                "}\n"
2412                "@autoreleasepool\n"
2413                "{\n"
2414                "  f();\n"
2415                "}\n",
2416                Style);
2417 }
2418 
2419 TEST_F(FormatTest, StaticInitializers) {
2420   verifyFormat("static SomeClass SC = {1, 'a'};");
2421 
2422   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
2423                "    100000000, "
2424                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
2425 
2426   // Here, everything other than the "}" would fit on a line.
2427   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
2428                "    10000000000000000000000000};");
2429   EXPECT_EQ("S s = {a,\n"
2430             "\n"
2431             "       b};",
2432             format("S s = {\n"
2433                    "  a,\n"
2434                    "\n"
2435                    "  b\n"
2436                    "};"));
2437 
2438   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
2439   // line. However, the formatting looks a bit off and this probably doesn't
2440   // happen often in practice.
2441   verifyFormat("static int Variable[1] = {\n"
2442                "    {1000000000000000000000000000000000000}};",
2443                getLLVMStyleWithColumns(40));
2444 }
2445 
2446 TEST_F(FormatTest, DesignatedInitializers) {
2447   verifyFormat("const struct A a = {.a = 1, .b = 2};");
2448   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
2449                "                    .bbbbbbbbbb = 2,\n"
2450                "                    .cccccccccc = 3,\n"
2451                "                    .dddddddddd = 4,\n"
2452                "                    .eeeeeeeeee = 5};");
2453   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2454                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
2455                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
2456                "    .ccccccccccccccccccccccccccc = 3,\n"
2457                "    .ddddddddddddddddddddddddddd = 4,\n"
2458                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
2459 
2460   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
2461 }
2462 
2463 TEST_F(FormatTest, NestedStaticInitializers) {
2464   verifyFormat("static A x = {{{}}};\n");
2465   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
2466                "               {init1, init2, init3, init4}}};",
2467                getLLVMStyleWithColumns(50));
2468 
2469   verifyFormat("somes Status::global_reps[3] = {\n"
2470                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2471                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2472                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
2473                getLLVMStyleWithColumns(60));
2474   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
2475                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2476                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2477                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
2478   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
2479                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
2480                "rect.fTop}};");
2481 
2482   verifyFormat(
2483       "SomeArrayOfSomeType a = {\n"
2484       "    {{1, 2, 3},\n"
2485       "     {1, 2, 3},\n"
2486       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
2487       "      333333333333333333333333333333},\n"
2488       "     {1, 2, 3},\n"
2489       "     {1, 2, 3}}};");
2490   verifyFormat(
2491       "SomeArrayOfSomeType a = {\n"
2492       "    {{1, 2, 3}},\n"
2493       "    {{1, 2, 3}},\n"
2494       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
2495       "      333333333333333333333333333333}},\n"
2496       "    {{1, 2, 3}},\n"
2497       "    {{1, 2, 3}}};");
2498 
2499   verifyFormat("struct {\n"
2500                "  unsigned bit;\n"
2501                "  const char *const name;\n"
2502                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
2503                "                 {kOsWin, \"Windows\"},\n"
2504                "                 {kOsLinux, \"Linux\"},\n"
2505                "                 {kOsCrOS, \"Chrome OS\"}};");
2506   verifyFormat("struct {\n"
2507                "  unsigned bit;\n"
2508                "  const char *const name;\n"
2509                "} kBitsToOs[] = {\n"
2510                "    {kOsMac, \"Mac\"},\n"
2511                "    {kOsWin, \"Windows\"},\n"
2512                "    {kOsLinux, \"Linux\"},\n"
2513                "    {kOsCrOS, \"Chrome OS\"},\n"
2514                "};");
2515 }
2516 
2517 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
2518   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2519                "                      \\\n"
2520                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
2521 }
2522 
2523 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
2524   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
2525                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
2526 
2527   // Do break defaulted and deleted functions.
2528   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2529                "    default;",
2530                getLLVMStyleWithColumns(40));
2531   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2532                "    delete;",
2533                getLLVMStyleWithColumns(40));
2534 }
2535 
2536 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
2537   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
2538                getLLVMStyleWithColumns(40));
2539   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2540                getLLVMStyleWithColumns(40));
2541   EXPECT_EQ("#define Q                              \\\n"
2542             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
2543             "  \"aaaaaaaa.cpp\"",
2544             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2545                    getLLVMStyleWithColumns(40)));
2546 }
2547 
2548 TEST_F(FormatTest, UnderstandsLinePPDirective) {
2549   EXPECT_EQ("# 123 \"A string literal\"",
2550             format("   #     123    \"A string literal\""));
2551 }
2552 
2553 TEST_F(FormatTest, LayoutUnknownPPDirective) {
2554   EXPECT_EQ("#;", format("#;"));
2555   verifyFormat("#\n;\n;\n;");
2556 }
2557 
2558 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
2559   EXPECT_EQ("#line 42 \"test\"\n",
2560             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
2561   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
2562                                     getLLVMStyleWithColumns(12)));
2563 }
2564 
2565 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
2566   EXPECT_EQ("#line 42 \"test\"",
2567             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
2568   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
2569 }
2570 
2571 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
2572   verifyFormat("#define A \\x20");
2573   verifyFormat("#define A \\ x20");
2574   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
2575   verifyFormat("#define A ''");
2576   verifyFormat("#define A ''qqq");
2577   verifyFormat("#define A `qqq");
2578   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
2579   EXPECT_EQ("const char *c = STRINGIFY(\n"
2580             "\\na : b);",
2581             format("const char * c = STRINGIFY(\n"
2582                    "\\na : b);"));
2583 
2584   verifyFormat("a\r\\");
2585   verifyFormat("a\v\\");
2586   verifyFormat("a\f\\");
2587 }
2588 
2589 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
2590   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
2591   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
2592   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
2593   // FIXME: We never break before the macro name.
2594   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
2595 
2596   verifyFormat("#define A A\n#define A A");
2597   verifyFormat("#define A(X) A\n#define A A");
2598 
2599   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
2600   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
2601 }
2602 
2603 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
2604   EXPECT_EQ("// somecomment\n"
2605             "#include \"a.h\"\n"
2606             "#define A(  \\\n"
2607             "    A, B)\n"
2608             "#include \"b.h\"\n"
2609             "// somecomment\n",
2610             format("  // somecomment\n"
2611                    "  #include \"a.h\"\n"
2612                    "#define A(A,\\\n"
2613                    "    B)\n"
2614                    "    #include \"b.h\"\n"
2615                    " // somecomment\n",
2616                    getLLVMStyleWithColumns(13)));
2617 }
2618 
2619 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
2620 
2621 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
2622   EXPECT_EQ("#define A    \\\n"
2623             "  c;         \\\n"
2624             "  e;\n"
2625             "f;",
2626             format("#define A c; e;\n"
2627                    "f;",
2628                    getLLVMStyleWithColumns(14)));
2629 }
2630 
2631 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
2632 
2633 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
2634   EXPECT_EQ("int x,\n"
2635             "#define A\n"
2636             "    y;",
2637             format("int x,\n#define A\ny;"));
2638 }
2639 
2640 TEST_F(FormatTest, HashInMacroDefinition) {
2641   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
2642   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
2643   verifyFormat("#define A  \\\n"
2644                "  {        \\\n"
2645                "    f(#c); \\\n"
2646                "  }",
2647                getLLVMStyleWithColumns(11));
2648 
2649   verifyFormat("#define A(X)         \\\n"
2650                "  void function##X()",
2651                getLLVMStyleWithColumns(22));
2652 
2653   verifyFormat("#define A(a, b, c)   \\\n"
2654                "  void a##b##c()",
2655                getLLVMStyleWithColumns(22));
2656 
2657   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
2658 }
2659 
2660 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
2661   EXPECT_EQ("#define A (x)", format("#define A (x)"));
2662   EXPECT_EQ("#define A(x)", format("#define A(x)"));
2663 }
2664 
2665 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
2666   EXPECT_EQ("#define A b;", format("#define A \\\n"
2667                                    "          \\\n"
2668                                    "  b;",
2669                                    getLLVMStyleWithColumns(25)));
2670   EXPECT_EQ("#define A \\\n"
2671             "          \\\n"
2672             "  a;      \\\n"
2673             "  b;",
2674             format("#define A \\\n"
2675                    "          \\\n"
2676                    "  a;      \\\n"
2677                    "  b;",
2678                    getLLVMStyleWithColumns(11)));
2679   EXPECT_EQ("#define A \\\n"
2680             "  a;      \\\n"
2681             "          \\\n"
2682             "  b;",
2683             format("#define A \\\n"
2684                    "  a;      \\\n"
2685                    "          \\\n"
2686                    "  b;",
2687                    getLLVMStyleWithColumns(11)));
2688 }
2689 
2690 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
2691   verifyIncompleteFormat("#define A :");
2692   verifyFormat("#define SOMECASES  \\\n"
2693                "  case 1:          \\\n"
2694                "  case 2\n",
2695                getLLVMStyleWithColumns(20));
2696   verifyFormat("#define A template <typename T>");
2697   verifyIncompleteFormat("#define STR(x) #x\n"
2698                          "f(STR(this_is_a_string_literal{));");
2699   verifyFormat("#pragma omp threadprivate( \\\n"
2700                "    y)), // expected-warning",
2701                getLLVMStyleWithColumns(28));
2702   verifyFormat("#d, = };");
2703   verifyFormat("#if \"a");
2704   verifyIncompleteFormat("({\n"
2705                          "#define b     \\\n"
2706                          "  }           \\\n"
2707                          "  a\n"
2708                          "a",
2709                          getLLVMStyleWithColumns(15));
2710   verifyFormat("#define A     \\\n"
2711                "  {           \\\n"
2712                "    {\n"
2713                "#define B     \\\n"
2714                "  }           \\\n"
2715                "  }",
2716                getLLVMStyleWithColumns(15));
2717   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2718   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2719   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2720   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
2721 }
2722 
2723 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2724   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2725   EXPECT_EQ("class A : public QObject {\n"
2726             "  Q_OBJECT\n"
2727             "\n"
2728             "  A() {}\n"
2729             "};",
2730             format("class A  :  public QObject {\n"
2731                    "     Q_OBJECT\n"
2732                    "\n"
2733                    "  A() {\n}\n"
2734                    "}  ;"));
2735   EXPECT_EQ("MACRO\n"
2736             "/*static*/ int i;",
2737             format("MACRO\n"
2738                    " /*static*/ int   i;"));
2739   EXPECT_EQ("SOME_MACRO\n"
2740             "namespace {\n"
2741             "void f();\n"
2742             "}",
2743             format("SOME_MACRO\n"
2744                    "  namespace    {\n"
2745                    "void   f(  );\n"
2746                    "}"));
2747   // Only if the identifier contains at least 5 characters.
2748   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
2749   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
2750   // Only if everything is upper case.
2751   EXPECT_EQ("class A : public QObject {\n"
2752             "  Q_Object A() {}\n"
2753             "};",
2754             format("class A  :  public QObject {\n"
2755                    "     Q_Object\n"
2756                    "  A() {\n}\n"
2757                    "}  ;"));
2758 
2759   // Only if the next line can actually start an unwrapped line.
2760   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2761             format("SOME_WEIRD_LOG_MACRO\n"
2762                    "<< SomeThing;"));
2763 
2764   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2765                "(n, buffers))\n",
2766                getChromiumStyle(FormatStyle::LK_Cpp));
2767 }
2768 
2769 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2770   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2771             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2772             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2773             "class X {};\n"
2774             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2775             "int *createScopDetectionPass() { return 0; }",
2776             format("  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   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2783   // braces, so that inner block is indented one level more.
2784   EXPECT_EQ("int q() {\n"
2785             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2786             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2787             "  IPC_END_MESSAGE_MAP()\n"
2788             "}",
2789             format("int q() {\n"
2790                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2791                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2792                    "  IPC_END_MESSAGE_MAP()\n"
2793                    "}"));
2794 
2795   // Same inside macros.
2796   EXPECT_EQ("#define LIST(L) \\\n"
2797             "  L(A)          \\\n"
2798             "  L(B)          \\\n"
2799             "  L(C)",
2800             format("#define LIST(L) \\\n"
2801                    "  L(A) \\\n"
2802                    "  L(B) \\\n"
2803                    "  L(C)",
2804                    getGoogleStyle()));
2805 
2806   // These must not be recognized as macros.
2807   EXPECT_EQ("int q() {\n"
2808             "  f(x);\n"
2809             "  f(x) {}\n"
2810             "  f(x)->g();\n"
2811             "  f(x)->*g();\n"
2812             "  f(x).g();\n"
2813             "  f(x) = x;\n"
2814             "  f(x) += x;\n"
2815             "  f(x) -= x;\n"
2816             "  f(x) *= x;\n"
2817             "  f(x) /= x;\n"
2818             "  f(x) %= x;\n"
2819             "  f(x) &= x;\n"
2820             "  f(x) |= x;\n"
2821             "  f(x) ^= x;\n"
2822             "  f(x) >>= x;\n"
2823             "  f(x) <<= x;\n"
2824             "  f(x)[y].z();\n"
2825             "  LOG(INFO) << x;\n"
2826             "  ifstream(x) >> x;\n"
2827             "}\n",
2828             format("int q() {\n"
2829                    "  f(x)\n;\n"
2830                    "  f(x)\n {}\n"
2831                    "  f(x)\n->g();\n"
2832                    "  f(x)\n->*g();\n"
2833                    "  f(x)\n.g();\n"
2834                    "  f(x)\n = x;\n"
2835                    "  f(x)\n += x;\n"
2836                    "  f(x)\n -= x;\n"
2837                    "  f(x)\n *= x;\n"
2838                    "  f(x)\n /= x;\n"
2839                    "  f(x)\n %= x;\n"
2840                    "  f(x)\n &= x;\n"
2841                    "  f(x)\n |= x;\n"
2842                    "  f(x)\n ^= x;\n"
2843                    "  f(x)\n >>= x;\n"
2844                    "  f(x)\n <<= x;\n"
2845                    "  f(x)\n[y].z();\n"
2846                    "  LOG(INFO)\n << x;\n"
2847                    "  ifstream(x)\n >> x;\n"
2848                    "}\n"));
2849   EXPECT_EQ("int q() {\n"
2850             "  F(x)\n"
2851             "  if (1) {\n"
2852             "  }\n"
2853             "  F(x)\n"
2854             "  while (1) {\n"
2855             "  }\n"
2856             "  F(x)\n"
2857             "  G(x);\n"
2858             "  F(x)\n"
2859             "  try {\n"
2860             "    Q();\n"
2861             "  } catch (...) {\n"
2862             "  }\n"
2863             "}\n",
2864             format("int q() {\n"
2865                    "F(x)\n"
2866                    "if (1) {}\n"
2867                    "F(x)\n"
2868                    "while (1) {}\n"
2869                    "F(x)\n"
2870                    "G(x);\n"
2871                    "F(x)\n"
2872                    "try { Q(); } catch (...) {}\n"
2873                    "}\n"));
2874   EXPECT_EQ("class A {\n"
2875             "  A() : t(0) {}\n"
2876             "  A(int i) noexcept() : {}\n"
2877             "  A(X x)\n" // FIXME: function-level try blocks are broken.
2878             "  try : t(0) {\n"
2879             "  } catch (...) {\n"
2880             "  }\n"
2881             "};",
2882             format("class A {\n"
2883                    "  A()\n : t(0) {}\n"
2884                    "  A(int i)\n noexcept() : {}\n"
2885                    "  A(X x)\n"
2886                    "  try : t(0) {} catch (...) {}\n"
2887                    "};"));
2888   EXPECT_EQ("class SomeClass {\n"
2889             "public:\n"
2890             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2891             "};",
2892             format("class SomeClass {\n"
2893                    "public:\n"
2894                    "  SomeClass()\n"
2895                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2896                    "};"));
2897   EXPECT_EQ("class SomeClass {\n"
2898             "public:\n"
2899             "  SomeClass()\n"
2900             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2901             "};",
2902             format("class SomeClass {\n"
2903                    "public:\n"
2904                    "  SomeClass()\n"
2905                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2906                    "};",
2907                    getLLVMStyleWithColumns(40)));
2908 }
2909 
2910 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2911   verifyFormat("#define A \\\n"
2912                "  f({     \\\n"
2913                "    g();  \\\n"
2914                "  });",
2915                getLLVMStyleWithColumns(11));
2916 }
2917 
2918 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
2919   EXPECT_EQ("{\n  {\n#define A\n  }\n}", format("{{\n#define A\n}}"));
2920 }
2921 
2922 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
2923   verifyFormat("{\n  { a #c; }\n}");
2924 }
2925 
2926 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
2927   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
2928             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
2929   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
2930             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
2931 }
2932 
2933 TEST_F(FormatTest, EscapedNewlines) {
2934   EXPECT_EQ(
2935       "#define A \\\n  int i;  \\\n  int j;",
2936       format("#define A \\\nint i;\\\n  int j;", getLLVMStyleWithColumns(11)));
2937   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
2938   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2939   EXPECT_EQ("/* \\  \\  \\\n*/", format("\\\n/* \\  \\  \\\n*/"));
2940   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
2941 }
2942 
2943 TEST_F(FormatTest, DontCrashOnBlockComments) {
2944   EXPECT_EQ(
2945       "int xxxxxxxxx; /* "
2946       "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
2947       "zzzzzz\n"
2948       "0*/",
2949       format("int xxxxxxxxx;                          /* "
2950              "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
2951              "0*/"));
2952 }
2953 
2954 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
2955   verifyFormat("#define A \\\n"
2956                "  int v(  \\\n"
2957                "      a); \\\n"
2958                "  int i;",
2959                getLLVMStyleWithColumns(11));
2960 }
2961 
2962 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
2963   EXPECT_EQ(
2964       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2965       "                      \\\n"
2966       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2967       "\n"
2968       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2969       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
2970       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
2971              "\\\n"
2972              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2973              "  \n"
2974              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2975              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
2976 }
2977 
2978 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
2979   EXPECT_EQ("int\n"
2980             "#define A\n"
2981             "    a;",
2982             format("int\n#define A\na;"));
2983   verifyFormat("functionCallTo(\n"
2984                "    someOtherFunction(\n"
2985                "        withSomeParameters, whichInSequence,\n"
2986                "        areLongerThanALine(andAnotherCall,\n"
2987                "#define A B\n"
2988                "                           withMoreParamters,\n"
2989                "                           whichStronglyInfluenceTheLayout),\n"
2990                "        andMoreParameters),\n"
2991                "    trailing);",
2992                getLLVMStyleWithColumns(69));
2993   verifyFormat("Foo::Foo()\n"
2994                "#ifdef BAR\n"
2995                "    : baz(0)\n"
2996                "#endif\n"
2997                "{\n"
2998                "}");
2999   verifyFormat("void f() {\n"
3000                "  if (true)\n"
3001                "#ifdef A\n"
3002                "    f(42);\n"
3003                "  x();\n"
3004                "#else\n"
3005                "    g();\n"
3006                "  x();\n"
3007                "#endif\n"
3008                "}");
3009   verifyFormat("void f(param1, param2,\n"
3010                "       param3,\n"
3011                "#ifdef A\n"
3012                "       param4(param5,\n"
3013                "#ifdef A1\n"
3014                "              param6,\n"
3015                "#ifdef A2\n"
3016                "              param7),\n"
3017                "#else\n"
3018                "              param8),\n"
3019                "       param9,\n"
3020                "#endif\n"
3021                "       param10,\n"
3022                "#endif\n"
3023                "       param11)\n"
3024                "#else\n"
3025                "       param12)\n"
3026                "#endif\n"
3027                "{\n"
3028                "  x();\n"
3029                "}",
3030                getLLVMStyleWithColumns(28));
3031   verifyFormat("#if 1\n"
3032                "int i;");
3033   verifyFormat("#if 1\n"
3034                "#endif\n"
3035                "#if 1\n"
3036                "#else\n"
3037                "#endif\n");
3038   verifyFormat("DEBUG({\n"
3039                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3040                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
3041                "});\n"
3042                "#if a\n"
3043                "#else\n"
3044                "#endif");
3045 
3046   verifyIncompleteFormat("void f(\n"
3047                          "#if A\n"
3048                          "    );\n"
3049                          "#else\n"
3050                          "#endif");
3051 }
3052 
3053 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
3054   verifyFormat("#endif\n"
3055                "#if B");
3056 }
3057 
3058 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
3059   FormatStyle SingleLine = getLLVMStyle();
3060   SingleLine.AllowShortIfStatementsOnASingleLine = true;
3061   verifyFormat("#if 0\n"
3062                "#elif 1\n"
3063                "#endif\n"
3064                "void foo() {\n"
3065                "  if (test) foo2();\n"
3066                "}",
3067                SingleLine);
3068 }
3069 
3070 TEST_F(FormatTest, LayoutBlockInsideParens) {
3071   verifyFormat("functionCall({ int i; });");
3072   verifyFormat("functionCall({\n"
3073                "  int i;\n"
3074                "  int j;\n"
3075                "});");
3076   verifyFormat("functionCall({\n"
3077                "  int i;\n"
3078                "  int j;\n"
3079                "}, aaaa, bbbb, cccc);");
3080   verifyFormat("functionA(functionB({\n"
3081                "            int i;\n"
3082                "            int j;\n"
3083                "          }),\n"
3084                "          aaaa, bbbb, cccc);");
3085   verifyFormat("functionCall(\n"
3086                "    {\n"
3087                "      int i;\n"
3088                "      int j;\n"
3089                "    },\n"
3090                "    aaaa, bbbb, // comment\n"
3091                "    cccc);");
3092   verifyFormat("functionA(functionB({\n"
3093                "            int i;\n"
3094                "            int j;\n"
3095                "          }),\n"
3096                "          aaaa, bbbb, // comment\n"
3097                "          cccc);");
3098   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
3099   verifyFormat("functionCall(aaaa, bbbb, {\n"
3100                "  int i;\n"
3101                "  int j;\n"
3102                "});");
3103   verifyFormat(
3104       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
3105       "    {\n"
3106       "      int i; // break\n"
3107       "    },\n"
3108       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
3109       "                                     ccccccccccccccccc));");
3110   verifyFormat("DEBUG({\n"
3111                "  if (a)\n"
3112                "    f();\n"
3113                "});");
3114 }
3115 
3116 TEST_F(FormatTest, LayoutBlockInsideStatement) {
3117   EXPECT_EQ("SOME_MACRO { int i; }\n"
3118             "int i;",
3119             format("  SOME_MACRO  {int i;}  int i;"));
3120 }
3121 
3122 TEST_F(FormatTest, LayoutNestedBlocks) {
3123   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
3124                "  struct s {\n"
3125                "    int i;\n"
3126                "  };\n"
3127                "  s kBitsToOs[] = {{10}};\n"
3128                "  for (int i = 0; i < 10; ++i)\n"
3129                "    return;\n"
3130                "}");
3131   verifyFormat("call(parameter, {\n"
3132                "  something();\n"
3133                "  // Comment using all columns.\n"
3134                "  somethingelse();\n"
3135                "});",
3136                getLLVMStyleWithColumns(40));
3137   verifyFormat("DEBUG( //\n"
3138                "    { f(); }, a);");
3139   verifyFormat("DEBUG( //\n"
3140                "    {\n"
3141                "      f(); //\n"
3142                "    },\n"
3143                "    a);");
3144 
3145   EXPECT_EQ("call(parameter, {\n"
3146             "  something();\n"
3147             "  // Comment too\n"
3148             "  // looooooooooong.\n"
3149             "  somethingElse();\n"
3150             "});",
3151             format("call(parameter, {\n"
3152                    "  something();\n"
3153                    "  // Comment too looooooooooong.\n"
3154                    "  somethingElse();\n"
3155                    "});",
3156                    getLLVMStyleWithColumns(29)));
3157   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
3158   EXPECT_EQ("DEBUG({ // comment\n"
3159             "  int i;\n"
3160             "});",
3161             format("DEBUG({ // comment\n"
3162                    "int  i;\n"
3163                    "});"));
3164   EXPECT_EQ("DEBUG({\n"
3165             "  int i;\n"
3166             "\n"
3167             "  // comment\n"
3168             "  int j;\n"
3169             "});",
3170             format("DEBUG({\n"
3171                    "  int  i;\n"
3172                    "\n"
3173                    "  // comment\n"
3174                    "  int  j;\n"
3175                    "});"));
3176 
3177   verifyFormat("DEBUG({\n"
3178                "  if (a)\n"
3179                "    return;\n"
3180                "});");
3181   verifyGoogleFormat("DEBUG({\n"
3182                      "  if (a) return;\n"
3183                      "});");
3184   FormatStyle Style = getGoogleStyle();
3185   Style.ColumnLimit = 45;
3186   verifyFormat("Debug(aaaaa, {\n"
3187                "  if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
3188                "}, a);",
3189                Style);
3190 
3191   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
3192 
3193   verifyNoCrash("^{v^{a}}");
3194 }
3195 
3196 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
3197   EXPECT_EQ("#define MACRO()                     \\\n"
3198             "  Debug(aaa, /* force line break */ \\\n"
3199             "        {                           \\\n"
3200             "          int i;                    \\\n"
3201             "          int j;                    \\\n"
3202             "        })",
3203             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
3204                    "          {  int   i;  int  j;   })",
3205                    getGoogleStyle()));
3206 
3207   EXPECT_EQ("#define A                                       \\\n"
3208             "  [] {                                          \\\n"
3209             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
3210             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
3211             "  }",
3212             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
3213                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
3214                    getGoogleStyle()));
3215 }
3216 
3217 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
3218   EXPECT_EQ("{}", format("{}"));
3219   verifyFormat("enum E {};");
3220   verifyFormat("enum E {}");
3221 }
3222 
3223 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
3224   FormatStyle Style = getLLVMStyle();
3225   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
3226   Style.MacroBlockEnd = "^[A-Z_]+_END$";
3227   verifyFormat("FOO_BEGIN\n"
3228                "  FOO_ENTRY\n"
3229                "FOO_END", Style);
3230   verifyFormat("FOO_BEGIN\n"
3231                "  NESTED_FOO_BEGIN\n"
3232                "    NESTED_FOO_ENTRY\n"
3233                "  NESTED_FOO_END\n"
3234                "FOO_END", Style);
3235   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
3236                "  int x;\n"
3237                "  x = 1;\n"
3238                "FOO_END(Baz)", Style);
3239 }
3240 
3241 //===----------------------------------------------------------------------===//
3242 // Line break tests.
3243 //===----------------------------------------------------------------------===//
3244 
3245 TEST_F(FormatTest, PreventConfusingIndents) {
3246   verifyFormat(
3247       "void f() {\n"
3248       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
3249       "                         parameter, parameter, parameter)),\n"
3250       "                     SecondLongCall(parameter));\n"
3251       "}");
3252   verifyFormat(
3253       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3254       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3255       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3256       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
3257   verifyFormat(
3258       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3259       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
3260       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
3261       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
3262   verifyFormat(
3263       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3264       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
3265       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
3266       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
3267   verifyFormat("int a = bbbb && ccc && fffff(\n"
3268                "#define A Just forcing a new line\n"
3269                "                           ddd);");
3270 }
3271 
3272 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
3273   verifyFormat(
3274       "bool aaaaaaa =\n"
3275       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
3276       "    bbbbbbbb();");
3277   verifyFormat(
3278       "bool aaaaaaa =\n"
3279       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
3280       "    bbbbbbbb();");
3281 
3282   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3283                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
3284                "    ccccccccc == ddddddddddd;");
3285   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3286                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
3287                "    ccccccccc == ddddddddddd;");
3288   verifyFormat(
3289       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3290       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
3291       "    ccccccccc == ddddddddddd;");
3292 
3293   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3294                "                 aaaaaa) &&\n"
3295                "         bbbbbb && cccccc;");
3296   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3297                "                 aaaaaa) >>\n"
3298                "         bbbbbb;");
3299   verifyFormat("Whitespaces.addUntouchableComment(\n"
3300                "    SourceMgr.getSpellingColumnNumber(\n"
3301                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
3302                "    1);");
3303 
3304   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3305                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
3306                "    cccccc) {\n}");
3307   verifyFormat("b = a &&\n"
3308                "    // Comment\n"
3309                "    b.c && d;");
3310 
3311   // If the LHS of a comparison is not a binary expression itself, the
3312   // additional linebreak confuses many people.
3313   verifyFormat(
3314       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3315       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
3316       "}");
3317   verifyFormat(
3318       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3319       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3320       "}");
3321   verifyFormat(
3322       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
3323       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3324       "}");
3325   // Even explicit parentheses stress the precedence enough to make the
3326   // additional break unnecessary.
3327   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3328                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3329                "}");
3330   // This cases is borderline, but with the indentation it is still readable.
3331   verifyFormat(
3332       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3333       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3334       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
3335       "}",
3336       getLLVMStyleWithColumns(75));
3337 
3338   // If the LHS is a binary expression, we should still use the additional break
3339   // as otherwise the formatting hides the operator precedence.
3340   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3341                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3342                "    5) {\n"
3343                "}");
3344 
3345   FormatStyle OnePerLine = getLLVMStyle();
3346   OnePerLine.BinPackParameters = false;
3347   verifyFormat(
3348       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3349       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3350       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
3351       OnePerLine);
3352 }
3353 
3354 TEST_F(FormatTest, ExpressionIndentation) {
3355   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3356                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3357                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3358                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3359                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3360                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
3361                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3362                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
3363                "                 ccccccccccccccccccccccccccccccccccccccccc;");
3364   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3365                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3366                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3367                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3368   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3369                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3370                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3371                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3372   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3373                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3374                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3375                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3376   verifyFormat("if () {\n"
3377                "} else if (aaaaa &&\n"
3378                "           bbbbb > // break\n"
3379                "               ccccc) {\n"
3380                "}");
3381 
3382   // Presence of a trailing comment used to change indentation of b.
3383   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
3384                "       b;\n"
3385                "return aaaaaaaaaaaaaaaaaaa +\n"
3386                "       b; //",
3387                getLLVMStyleWithColumns(30));
3388 }
3389 
3390 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
3391   // Not sure what the best system is here. Like this, the LHS can be found
3392   // immediately above an operator (everything with the same or a higher
3393   // indent). The RHS is aligned right of the operator and so compasses
3394   // everything until something with the same indent as the operator is found.
3395   // FIXME: Is this a good system?
3396   FormatStyle Style = getLLVMStyle();
3397   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
3398   verifyFormat(
3399       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3400       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3401       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3402       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3403       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3404       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3405       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3406       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3407       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
3408       Style);
3409   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3410                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3411                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3412                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3413                Style);
3414   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3415                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3416                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3417                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3418                Style);
3419   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3420                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3421                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3422                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3423                Style);
3424   verifyFormat("if () {\n"
3425                "} else if (aaaaa\n"
3426                "           && bbbbb // break\n"
3427                "                  > ccccc) {\n"
3428                "}",
3429                Style);
3430   verifyFormat("return (a)\n"
3431                "       // comment\n"
3432                "       + b;",
3433                Style);
3434   verifyFormat(
3435       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3436       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3437       "             + cc;",
3438       Style);
3439 
3440   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3441                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3442                Style);
3443 
3444   // Forced by comments.
3445   verifyFormat(
3446       "unsigned ContentSize =\n"
3447       "    sizeof(int16_t)   // DWARF ARange version number\n"
3448       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
3449       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
3450       "    + sizeof(int8_t); // Segment Size (in bytes)");
3451 
3452   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
3453                "       == boost::fusion::at_c<1>(iiii).second;",
3454                Style);
3455 
3456   Style.ColumnLimit = 60;
3457   verifyFormat("zzzzzzzzzz\n"
3458                "    = bbbbbbbbbbbbbbbbb\n"
3459                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
3460                Style);
3461 }
3462 
3463 TEST_F(FormatTest, NoOperandAlignment) {
3464   FormatStyle Style = getLLVMStyle();
3465   Style.AlignOperands = false;
3466   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3467   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3468                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3469                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3470                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3471                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3472                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3473                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3474                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3475                "        > ccccccccccccccccccccccccccccccccccccccccc;",
3476                Style);
3477 
3478   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3479                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3480                "    + cc;",
3481                Style);
3482   verifyFormat("int a = aa\n"
3483                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3484                "        * cccccccccccccccccccccccccccccccccccc;",
3485                Style);
3486 
3487   Style.AlignAfterOpenBracket = false;
3488   verifyFormat("return (a > b\n"
3489                "    // comment1\n"
3490                "    // comment2\n"
3491                "    || c);",
3492                Style);
3493 }
3494 
3495 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
3496   FormatStyle Style = getLLVMStyle();
3497   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3498   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3499                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3500                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
3501                Style);
3502 }
3503 
3504 TEST_F(FormatTest, ConstructorInitializers) {
3505   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3506   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
3507                getLLVMStyleWithColumns(45));
3508   verifyFormat("Constructor()\n"
3509                "    : Inttializer(FitsOnTheLine) {}",
3510                getLLVMStyleWithColumns(44));
3511   verifyFormat("Constructor()\n"
3512                "    : Inttializer(FitsOnTheLine) {}",
3513                getLLVMStyleWithColumns(43));
3514 
3515   verifyFormat(
3516       "SomeClass::Constructor()\n"
3517       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3518 
3519   verifyFormat(
3520       "SomeClass::Constructor()\n"
3521       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3522       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
3523   verifyFormat(
3524       "SomeClass::Constructor()\n"
3525       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3526       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3527 
3528   verifyFormat("Constructor()\n"
3529                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3530                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3531                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3532                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
3533 
3534   verifyFormat("Constructor()\n"
3535                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3536                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3537 
3538   verifyFormat("Constructor(int Parameter = 0)\n"
3539                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3540                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
3541   verifyFormat("Constructor()\n"
3542                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3543                "}",
3544                getLLVMStyleWithColumns(60));
3545   verifyFormat("Constructor()\n"
3546                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3547                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
3548 
3549   // Here a line could be saved by splitting the second initializer onto two
3550   // lines, but that is not desirable.
3551   verifyFormat("Constructor()\n"
3552                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3553                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
3554                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3555 
3556   FormatStyle OnePerLine = getLLVMStyle();
3557   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3558   verifyFormat("SomeClass::Constructor()\n"
3559                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3560                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3561                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3562                OnePerLine);
3563   verifyFormat("SomeClass::Constructor()\n"
3564                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3565                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3566                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3567                OnePerLine);
3568   verifyFormat("MyClass::MyClass(int var)\n"
3569                "    : some_var_(var),            // 4 space indent\n"
3570                "      some_other_var_(var + 1) { // lined up\n"
3571                "}",
3572                OnePerLine);
3573   verifyFormat("Constructor()\n"
3574                "    : aaaaa(aaaaaa),\n"
3575                "      aaaaa(aaaaaa),\n"
3576                "      aaaaa(aaaaaa),\n"
3577                "      aaaaa(aaaaaa),\n"
3578                "      aaaaa(aaaaaa) {}",
3579                OnePerLine);
3580   verifyFormat("Constructor()\n"
3581                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3582                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
3583                OnePerLine);
3584 
3585   EXPECT_EQ("Constructor()\n"
3586             "    : // Comment forcing unwanted break.\n"
3587             "      aaaa(aaaa) {}",
3588             format("Constructor() :\n"
3589                    "    // Comment forcing unwanted break.\n"
3590                    "    aaaa(aaaa) {}"));
3591 }
3592 
3593 TEST_F(FormatTest, MemoizationTests) {
3594   // This breaks if the memoization lookup does not take \c Indent and
3595   // \c LastSpace into account.
3596   verifyFormat(
3597       "extern CFRunLoopTimerRef\n"
3598       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
3599       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
3600       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
3601       "                     CFRunLoopTimerContext *context) {}");
3602 
3603   // Deep nesting somewhat works around our memoization.
3604   verifyFormat(
3605       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3606       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3607       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3608       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3609       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
3610       getLLVMStyleWithColumns(65));
3611   verifyFormat(
3612       "aaaaa(\n"
3613       "    aaaaa,\n"
3614       "    aaaaa(\n"
3615       "        aaaaa,\n"
3616       "        aaaaa(\n"
3617       "            aaaaa,\n"
3618       "            aaaaa(\n"
3619       "                aaaaa,\n"
3620       "                aaaaa(\n"
3621       "                    aaaaa,\n"
3622       "                    aaaaa(\n"
3623       "                        aaaaa,\n"
3624       "                        aaaaa(\n"
3625       "                            aaaaa,\n"
3626       "                            aaaaa(\n"
3627       "                                aaaaa,\n"
3628       "                                aaaaa(\n"
3629       "                                    aaaaa,\n"
3630       "                                    aaaaa(\n"
3631       "                                        aaaaa,\n"
3632       "                                        aaaaa(\n"
3633       "                                            aaaaa,\n"
3634       "                                            aaaaa(\n"
3635       "                                                aaaaa,\n"
3636       "                                                aaaaa))))))))))));",
3637       getLLVMStyleWithColumns(65));
3638   verifyFormat(
3639       "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"
3640       "                                  a),\n"
3641       "                                a),\n"
3642       "                              a),\n"
3643       "                            a),\n"
3644       "                          a),\n"
3645       "                        a),\n"
3646       "                      a),\n"
3647       "                    a),\n"
3648       "                  a),\n"
3649       "                a),\n"
3650       "              a),\n"
3651       "            a),\n"
3652       "          a),\n"
3653       "        a),\n"
3654       "      a),\n"
3655       "    a),\n"
3656       "  a)",
3657       getLLVMStyleWithColumns(65));
3658 
3659   // This test takes VERY long when memoization is broken.
3660   FormatStyle OnePerLine = getLLVMStyle();
3661   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3662   OnePerLine.BinPackParameters = false;
3663   std::string input = "Constructor()\n"
3664                       "    : aaaa(a,\n";
3665   for (unsigned i = 0, e = 80; i != e; ++i) {
3666     input += "           a,\n";
3667   }
3668   input += "           a) {}";
3669   verifyFormat(input, OnePerLine);
3670 }
3671 
3672 TEST_F(FormatTest, BreaksAsHighAsPossible) {
3673   verifyFormat(
3674       "void f() {\n"
3675       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
3676       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
3677       "    f();\n"
3678       "}");
3679   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
3680                "    Intervals[i - 1].getRange().getLast()) {\n}");
3681 }
3682 
3683 TEST_F(FormatTest, BreaksFunctionDeclarations) {
3684   // Principially, we break function declarations in a certain order:
3685   // 1) break amongst arguments.
3686   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
3687                "                              Cccccccccccccc cccccccccccccc);");
3688   verifyFormat("template <class TemplateIt>\n"
3689                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
3690                "                            TemplateIt *stop) {}");
3691 
3692   // 2) break after return type.
3693   verifyFormat(
3694       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3695       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
3696       getGoogleStyle());
3697 
3698   // 3) break after (.
3699   verifyFormat(
3700       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
3701       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
3702       getGoogleStyle());
3703 
3704   // 4) break before after nested name specifiers.
3705   verifyFormat(
3706       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3707       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
3708       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
3709       getGoogleStyle());
3710 
3711   // However, there are exceptions, if a sufficient amount of lines can be
3712   // saved.
3713   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
3714   // more adjusting.
3715   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3716                "                                  Cccccccccccccc cccccccccc,\n"
3717                "                                  Cccccccccccccc cccccccccc,\n"
3718                "                                  Cccccccccccccc cccccccccc,\n"
3719                "                                  Cccccccccccccc cccccccccc);");
3720   verifyFormat(
3721       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3722       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3723       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3724       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
3725       getGoogleStyle());
3726   verifyFormat(
3727       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3728       "                                          Cccccccccccccc cccccccccc,\n"
3729       "                                          Cccccccccccccc cccccccccc,\n"
3730       "                                          Cccccccccccccc cccccccccc,\n"
3731       "                                          Cccccccccccccc cccccccccc,\n"
3732       "                                          Cccccccccccccc cccccccccc,\n"
3733       "                                          Cccccccccccccc cccccccccc);");
3734   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3735                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3736                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3737                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3738                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
3739 
3740   // Break after multi-line parameters.
3741   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3742                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3743                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3744                "    bbbb bbbb);");
3745   verifyFormat("void SomeLoooooooooooongFunction(\n"
3746                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
3747                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3748                "    int bbbbbbbbbbbbb);");
3749 
3750   // Treat overloaded operators like other functions.
3751   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3752                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
3753   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3754                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
3755   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3756                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
3757   verifyGoogleFormat(
3758       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
3759       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3760   verifyGoogleFormat(
3761       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
3762       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3763   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3764                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3765   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
3766                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3767   verifyGoogleFormat(
3768       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
3769       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3770       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
3771 
3772   FormatStyle Style = getLLVMStyle();
3773   Style.PointerAlignment = FormatStyle::PAS_Left;
3774   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3775                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
3776                Style);
3777   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
3778                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3779                Style);
3780 }
3781 
3782 TEST_F(FormatTest, TrailingReturnType) {
3783   verifyFormat("auto foo() -> int;\n");
3784   verifyFormat("struct S {\n"
3785                "  auto bar() const -> int;\n"
3786                "};");
3787   verifyFormat("template <size_t Order, typename T>\n"
3788                "auto load_img(const std::string &filename)\n"
3789                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
3790   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
3791                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
3792   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
3793   verifyFormat("template <typename T>\n"
3794                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
3795                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
3796 
3797   // Not trailing return types.
3798   verifyFormat("void f() { auto a = b->c(); }");
3799 }
3800 
3801 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
3802   // Avoid breaking before trailing 'const' or other trailing annotations, if
3803   // they are not function-like.
3804   FormatStyle Style = getGoogleStyle();
3805   Style.ColumnLimit = 47;
3806   verifyFormat("void someLongFunction(\n"
3807                "    int someLoooooooooooooongParameter) const {\n}",
3808                getLLVMStyleWithColumns(47));
3809   verifyFormat("LoooooongReturnType\n"
3810                "someLoooooooongFunction() const {}",
3811                getLLVMStyleWithColumns(47));
3812   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
3813                "    const {}",
3814                Style);
3815   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3816                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
3817   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3818                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
3819   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3820                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
3821   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
3822                "                   aaaaaaaaaaa aaaaa) const override;");
3823   verifyGoogleFormat(
3824       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3825       "    const override;");
3826 
3827   // Even if the first parameter has to be wrapped.
3828   verifyFormat("void someLongFunction(\n"
3829                "    int someLongParameter) const {}",
3830                getLLVMStyleWithColumns(46));
3831   verifyFormat("void someLongFunction(\n"
3832                "    int someLongParameter) const {}",
3833                Style);
3834   verifyFormat("void someLongFunction(\n"
3835                "    int someLongParameter) override {}",
3836                Style);
3837   verifyFormat("void someLongFunction(\n"
3838                "    int someLongParameter) OVERRIDE {}",
3839                Style);
3840   verifyFormat("void someLongFunction(\n"
3841                "    int someLongParameter) final {}",
3842                Style);
3843   verifyFormat("void someLongFunction(\n"
3844                "    int someLongParameter) FINAL {}",
3845                Style);
3846   verifyFormat("void someLongFunction(\n"
3847                "    int parameter) const override {}",
3848                Style);
3849 
3850   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
3851   verifyFormat("void someLongFunction(\n"
3852                "    int someLongParameter) const\n"
3853                "{\n"
3854                "}",
3855                Style);
3856 
3857   // Unless these are unknown annotations.
3858   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
3859                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3860                "    LONG_AND_UGLY_ANNOTATION;");
3861 
3862   // Breaking before function-like trailing annotations is fine to keep them
3863   // close to their arguments.
3864   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3865                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3866   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3867                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3868   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3869                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
3870   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
3871                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
3872   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
3873 
3874   verifyFormat(
3875       "void aaaaaaaaaaaaaaaaaa()\n"
3876       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
3877       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
3878   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3879                "    __attribute__((unused));");
3880   verifyGoogleFormat(
3881       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3882       "    GUARDED_BY(aaaaaaaaaaaa);");
3883   verifyGoogleFormat(
3884       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3885       "    GUARDED_BY(aaaaaaaaaaaa);");
3886   verifyGoogleFormat(
3887       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3888       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3889   verifyGoogleFormat(
3890       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3891       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
3892 }
3893 
3894 TEST_F(FormatTest, FunctionAnnotations) {
3895   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3896                "string OldFunction(const string &parameter) {}");
3897   verifyFormat("template <typename T>\n"
3898                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3899                "string OldFunction(const string &parameter) {}");
3900 
3901   // Not function annotations.
3902   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3903                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
3904   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
3905                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
3906 }
3907 
3908 TEST_F(FormatTest, BreaksDesireably) {
3909   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3910                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3911                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
3912   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3913                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
3914                "}");
3915 
3916   verifyFormat(
3917       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3918       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3919 
3920   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3921                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3922                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3923 
3924   verifyFormat(
3925       "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3926       "                            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
3927       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3928       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
3929 
3930   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3931                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3932 
3933   verifyFormat(
3934       "void f() {\n"
3935       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
3936       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3937       "}");
3938   verifyFormat(
3939       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3940       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3941   verifyFormat(
3942       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3943       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3944   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3945                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3946                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3947 
3948   // Indent consistently independent of call expression and unary operator.
3949   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3950                "    dddddddddddddddddddddddddddddd));");
3951   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3952                "    dddddddddddddddddddddddddddddd));");
3953   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
3954                "    dddddddddddddddddddddddddddddd));");
3955 
3956   // This test case breaks on an incorrect memoization, i.e. an optimization not
3957   // taking into account the StopAt value.
3958   verifyFormat(
3959       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3960       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3961       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3962       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3963 
3964   verifyFormat("{\n  {\n    {\n"
3965                "      Annotation.SpaceRequiredBefore =\n"
3966                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
3967                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
3968                "    }\n  }\n}");
3969 
3970   // Break on an outer level if there was a break on an inner level.
3971   EXPECT_EQ("f(g(h(a, // comment\n"
3972             "      b, c),\n"
3973             "    d, e),\n"
3974             "  x, y);",
3975             format("f(g(h(a, // comment\n"
3976                    "    b, c), d, e), x, y);"));
3977 
3978   // Prefer breaking similar line breaks.
3979   verifyFormat(
3980       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
3981       "                             NSTrackingMouseEnteredAndExited |\n"
3982       "                             NSTrackingActiveAlways;");
3983 }
3984 
3985 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
3986   FormatStyle NoBinPacking = getGoogleStyle();
3987   NoBinPacking.BinPackParameters = false;
3988   NoBinPacking.BinPackArguments = true;
3989   verifyFormat("void f() {\n"
3990                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
3991                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3992                "}",
3993                NoBinPacking);
3994   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
3995                "       int aaaaaaaaaaaaaaaaaaaa,\n"
3996                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3997                NoBinPacking);
3998 }
3999 
4000 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
4001   FormatStyle NoBinPacking = getGoogleStyle();
4002   NoBinPacking.BinPackParameters = false;
4003   NoBinPacking.BinPackArguments = false;
4004   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
4005                "  aaaaaaaaaaaaaaaaaaaa,\n"
4006                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
4007                NoBinPacking);
4008   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
4009                "        aaaaaaaaaaaaa,\n"
4010                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
4011                NoBinPacking);
4012   verifyFormat(
4013       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4014       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4015       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4016       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4017       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
4018       NoBinPacking);
4019   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4020                "    .aaaaaaaaaaaaaaaaaa();",
4021                NoBinPacking);
4022   verifyFormat("void f() {\n"
4023                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4024                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
4025                "}",
4026                NoBinPacking);
4027 
4028   verifyFormat(
4029       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4030       "             aaaaaaaaaaaa,\n"
4031       "             aaaaaaaaaaaa);",
4032       NoBinPacking);
4033   verifyFormat(
4034       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
4035       "                               ddddddddddddddddddddddddddddd),\n"
4036       "             test);",
4037       NoBinPacking);
4038 
4039   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4040                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
4041                "            aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;",
4042                NoBinPacking);
4043   verifyFormat("a(\"a\"\n"
4044                "  \"a\",\n"
4045                "  a);");
4046 
4047   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4048   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
4049                "                aaaaaaaaa,\n"
4050                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4051                NoBinPacking);
4052   verifyFormat(
4053       "void f() {\n"
4054       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4055       "      .aaaaaaa();\n"
4056       "}",
4057       NoBinPacking);
4058   verifyFormat(
4059       "template <class SomeType, class SomeOtherType>\n"
4060       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
4061       NoBinPacking);
4062 }
4063 
4064 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
4065   FormatStyle Style = getLLVMStyleWithColumns(15);
4066   Style.ExperimentalAutoDetectBinPacking = true;
4067   EXPECT_EQ("aaa(aaaa,\n"
4068             "    aaaa,\n"
4069             "    aaaa);\n"
4070             "aaa(aaaa,\n"
4071             "    aaaa,\n"
4072             "    aaaa);",
4073             format("aaa(aaaa,\n" // one-per-line
4074                    "  aaaa,\n"
4075                    "    aaaa  );\n"
4076                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4077                    Style));
4078   EXPECT_EQ("aaa(aaaa, aaaa,\n"
4079             "    aaaa);\n"
4080             "aaa(aaaa, aaaa,\n"
4081             "    aaaa);",
4082             format("aaa(aaaa,  aaaa,\n" // bin-packed
4083                    "    aaaa  );\n"
4084                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4085                    Style));
4086 }
4087 
4088 TEST_F(FormatTest, FormatsBuilderPattern) {
4089   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
4090                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
4091                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
4092                "    .StartsWith(\".init\", ORDER_INIT)\n"
4093                "    .StartsWith(\".fini\", ORDER_FINI)\n"
4094                "    .StartsWith(\".hash\", ORDER_HASH)\n"
4095                "    .Default(ORDER_TEXT);\n");
4096 
4097   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
4098                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
4099   verifyFormat(
4100       "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n"
4101       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4102       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4103   verifyFormat(
4104       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
4105       "    aaaaaaaaaaaaaa);");
4106   verifyFormat(
4107       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
4108       "    aaaaaa->aaaaaaaaaaaa()\n"
4109       "        ->aaaaaaaaaaaaaaaa(\n"
4110       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4111       "        ->aaaaaaaaaaaaaaaaa();");
4112   verifyGoogleFormat(
4113       "void f() {\n"
4114       "  someo->Add((new util::filetools::Handler(dir))\n"
4115       "                 ->OnEvent1(NewPermanentCallback(\n"
4116       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
4117       "                 ->OnEvent2(NewPermanentCallback(\n"
4118       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
4119       "                 ->OnEvent3(NewPermanentCallback(\n"
4120       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
4121       "                 ->OnEvent5(NewPermanentCallback(\n"
4122       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
4123       "                 ->OnEvent6(NewPermanentCallback(\n"
4124       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
4125       "}");
4126 
4127   verifyFormat(
4128       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
4129   verifyFormat("aaaaaaaaaaaaaaa()\n"
4130                "    .aaaaaaaaaaaaaaa()\n"
4131                "    .aaaaaaaaaaaaaaa()\n"
4132                "    .aaaaaaaaaaaaaaa()\n"
4133                "    .aaaaaaaaaaaaaaa();");
4134   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4135                "    .aaaaaaaaaaaaaaa()\n"
4136                "    .aaaaaaaaaaaaaaa()\n"
4137                "    .aaaaaaaaaaaaaaa();");
4138   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4139                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4140                "    .aaaaaaaaaaaaaaa();");
4141   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
4142                "    ->aaaaaaaaaaaaaae(0)\n"
4143                "    ->aaaaaaaaaaaaaaa();");
4144 
4145   // Don't linewrap after very short segments.
4146   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4147                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4148                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4149   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4150                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4151                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4152   verifyFormat("aaa()\n"
4153                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4154                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4155                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4156 
4157   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4158                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4159                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
4160   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4161                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
4162                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
4163 
4164   // Prefer not to break after empty parentheses.
4165   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
4166                "    First->LastNewlineOffset);");
4167 }
4168 
4169 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
4170   verifyFormat(
4171       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4172       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
4173   verifyFormat(
4174       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
4175       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
4176 
4177   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4178                "    ccccccccccccccccccccccccc) {\n}");
4179   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
4180                "    ccccccccccccccccccccccccc) {\n}");
4181 
4182   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4183                "    ccccccccccccccccccccccccc) {\n}");
4184   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
4185                "    ccccccccccccccccccccccccc) {\n}");
4186 
4187   verifyFormat(
4188       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
4189       "    ccccccccccccccccccccccccc) {\n}");
4190   verifyFormat(
4191       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
4192       "    ccccccccccccccccccccccccc) {\n}");
4193 
4194   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
4195                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
4196                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
4197                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4198   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
4199                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
4200                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
4201                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4202 
4203   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
4204                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
4205                "    aaaaaaaaaaaaaaa != aa) {\n}");
4206   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
4207                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
4208                "    aaaaaaaaaaaaaaa != aa) {\n}");
4209 }
4210 
4211 TEST_F(FormatTest, BreaksAfterAssignments) {
4212   verifyFormat(
4213       "unsigned Cost =\n"
4214       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
4215       "                        SI->getPointerAddressSpaceee());\n");
4216   verifyFormat(
4217       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
4218       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
4219 
4220   verifyFormat(
4221       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
4222       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
4223   verifyFormat("unsigned OriginalStartColumn =\n"
4224                "    SourceMgr.getSpellingColumnNumber(\n"
4225                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
4226                "    1;");
4227 }
4228 
4229 TEST_F(FormatTest, AlignsAfterAssignments) {
4230   verifyFormat(
4231       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4232       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
4233   verifyFormat(
4234       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4235       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
4236   verifyFormat(
4237       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4238       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
4239   verifyFormat(
4240       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4241       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
4242   verifyFormat(
4243       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4244       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4245       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
4246 }
4247 
4248 TEST_F(FormatTest, AlignsAfterReturn) {
4249   verifyFormat(
4250       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4251       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
4252   verifyFormat(
4253       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4254       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
4255   verifyFormat(
4256       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4257       "       aaaaaaaaaaaaaaaaaaaaaa();");
4258   verifyFormat(
4259       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4260       "        aaaaaaaaaaaaaaaaaaaaaa());");
4261   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4262                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4263   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4264                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
4265                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4266   verifyFormat("return\n"
4267                "    // true if code is one of a or b.\n"
4268                "    code == a || code == b;");
4269 }
4270 
4271 TEST_F(FormatTest, AlignsAfterOpenBracket) {
4272   verifyFormat(
4273       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4274       "                                                aaaaaaaaa aaaaaaa) {}");
4275   verifyFormat(
4276       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4277       "                                               aaaaaaaaaaa aaaaaaaaa);");
4278   verifyFormat(
4279       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4280       "                                             aaaaaaaaaaaaaaaaaaaaa));");
4281   FormatStyle Style = getLLVMStyle();
4282   Style.AlignAfterOpenBracket = false;
4283   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4284                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
4285                Style);
4286   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4287                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
4288                Style);
4289   verifyFormat("SomeLongVariableName->someFunction(\n"
4290                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
4291                Style);
4292   verifyFormat(
4293       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4294       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4295       Style);
4296   verifyFormat(
4297       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4298       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4299       Style);
4300   verifyFormat(
4301       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4302       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4303       Style);
4304 }
4305 
4306 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
4307   FormatStyle Style = getLLVMStyleWithColumns(40);
4308   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4309                "          bbbbbbbbbbbbbbbbbbbbbb);",
4310                Style);
4311   Style.AlignAfterOpenBracket = true;
4312   Style.AlignOperands = false;
4313   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4314                "          bbbbbbbbbbbbbbbbbbbbbb);",
4315                Style);
4316   Style.AlignAfterOpenBracket = false;
4317   Style.AlignOperands = true;
4318   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4319                "          bbbbbbbbbbbbbbbbbbbbbb);",
4320                Style);
4321   Style.AlignAfterOpenBracket = false;
4322   Style.AlignOperands = false;
4323   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4324                "    bbbbbbbbbbbbbbbbbbbbbb);",
4325                Style);
4326 }
4327 
4328 TEST_F(FormatTest, BreaksConditionalExpressions) {
4329   verifyFormat(
4330       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4331       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4332       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4333   verifyFormat(
4334       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4335       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4336   verifyFormat(
4337       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
4338       "                                                    : aaaaaaaaaaaaa);");
4339   verifyFormat(
4340       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4341       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4342       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4343       "                   aaaaaaaaaaaaa);");
4344   verifyFormat(
4345       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4346       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4347       "                   aaaaaaaaaaaaa);");
4348   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4349                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4350                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4351                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4352                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4353   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4354                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4355                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4356                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4357                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4358                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4359                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4360   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4361                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4362                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4363                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4364                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4365   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4366                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4367                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4368   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4369                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4370                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4371                "        : aaaaaaaaaaaaaaaa;");
4372   verifyFormat(
4373       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4374       "    ? aaaaaaaaaaaaaaa\n"
4375       "    : aaaaaaaaaaaaaaa;");
4376   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4377                "          aaaaaaaaa\n"
4378                "      ? b\n"
4379                "      : c);");
4380   verifyFormat("return aaaa == bbbb\n"
4381                "           // comment\n"
4382                "           ? aaaa\n"
4383                "           : bbbb;");
4384   verifyFormat("unsigned Indent =\n"
4385                "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
4386                "                              ? IndentForLevel[TheLine.Level]\n"
4387                "                              : TheLine * 2,\n"
4388                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4389                getLLVMStyleWithColumns(70));
4390   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4391                "                  ? aaaaaaaaaaaaaaa\n"
4392                "                  : bbbbbbbbbbbbbbb //\n"
4393                "                        ? ccccccccccccccc\n"
4394                "                        : ddddddddddddddd;");
4395   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4396                "                  ? aaaaaaaaaaaaaaa\n"
4397                "                  : (bbbbbbbbbbbbbbb //\n"
4398                "                         ? ccccccccccccccc\n"
4399                "                         : ddddddddddddddd);");
4400   verifyFormat(
4401       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4402       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4403       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
4404       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
4405       "                                      : aaaaaaaaaa;");
4406   verifyFormat(
4407       "aaaaaa = aaaaaaaaaaaa\n"
4408       "             ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4409       "                          : aaaaaaaaaaaaaaaaaaaaaa\n"
4410       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4411 
4412   FormatStyle NoBinPacking = getLLVMStyle();
4413   NoBinPacking.BinPackArguments = false;
4414   verifyFormat(
4415       "void f() {\n"
4416       "  g(aaa,\n"
4417       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4418       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4419       "        ? aaaaaaaaaaaaaaa\n"
4420       "        : aaaaaaaaaaaaaaa);\n"
4421       "}",
4422       NoBinPacking);
4423   verifyFormat(
4424       "void f() {\n"
4425       "  g(aaa,\n"
4426       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4427       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4428       "        ?: aaaaaaaaaaaaaaa);\n"
4429       "}",
4430       NoBinPacking);
4431 
4432   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
4433                "             // comment.\n"
4434                "             ccccccccccccccccccccccccccccccccccccccc\n"
4435                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4436                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
4437 
4438   // Assignments in conditional expressions. Apparently not uncommon :-(.
4439   verifyFormat("return a != b\n"
4440                "           // comment\n"
4441                "           ? a = b\n"
4442                "           : a = b;");
4443   verifyFormat("return a != b\n"
4444                "           // comment\n"
4445                "           ? a = a != b\n"
4446                "                     // comment\n"
4447                "                     ? a = b\n"
4448                "                     : a\n"
4449                "           : a;\n");
4450   verifyFormat("return a != b\n"
4451                "           // comment\n"
4452                "           ? a\n"
4453                "           : a = a != b\n"
4454                "                     // comment\n"
4455                "                     ? a = b\n"
4456                "                     : a;");
4457 }
4458 
4459 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
4460   FormatStyle Style = getLLVMStyle();
4461   Style.BreakBeforeTernaryOperators = false;
4462   Style.ColumnLimit = 70;
4463   verifyFormat(
4464       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4465       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4466       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4467       Style);
4468   verifyFormat(
4469       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4470       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4471       Style);
4472   verifyFormat(
4473       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
4474       "                                                      aaaaaaaaaaaaa);",
4475       Style);
4476   verifyFormat(
4477       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4478       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4479       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4480       "                   aaaaaaaaaaaaa);",
4481       Style);
4482   verifyFormat(
4483       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4484       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4485       "                   aaaaaaaaaaaaa);",
4486       Style);
4487   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4488                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4489                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4490                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4491                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4492                Style);
4493   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4494                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4495                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4496                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4497                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4498                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4499                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4500                Style);
4501   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4502                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
4503                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4504                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4505                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4506                Style);
4507   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4508                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4509                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4510                Style);
4511   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4512                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4513                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4514                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4515                Style);
4516   verifyFormat(
4517       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4518       "    aaaaaaaaaaaaaaa :\n"
4519       "    aaaaaaaaaaaaaaa;",
4520       Style);
4521   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4522                "          aaaaaaaaa ?\n"
4523                "      b :\n"
4524                "      c);",
4525                Style);
4526   verifyFormat(
4527       "unsigned Indent =\n"
4528       "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
4529       "                              IndentForLevel[TheLine.Level] :\n"
4530       "                              TheLine * 2,\n"
4531       "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4532       Style);
4533   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4534                "                  aaaaaaaaaaaaaaa :\n"
4535                "                  bbbbbbbbbbbbbbb ? //\n"
4536                "                      ccccccccccccccc :\n"
4537                "                      ddddddddddddddd;",
4538                Style);
4539   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4540                "                  aaaaaaaaaaaaaaa :\n"
4541                "                  (bbbbbbbbbbbbbbb ? //\n"
4542                "                       ccccccccccccccc :\n"
4543                "                       ddddddddddddddd);",
4544                Style);
4545 }
4546 
4547 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
4548   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
4549                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
4550   verifyFormat("bool a = true, b = false;");
4551 
4552   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4553                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
4554                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
4555                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
4556   verifyFormat(
4557       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
4558       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
4559       "     d = e && f;");
4560   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
4561                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
4562   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4563                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
4564   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
4565                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
4566 
4567   FormatStyle Style = getGoogleStyle();
4568   Style.PointerAlignment = FormatStyle::PAS_Left;
4569   Style.DerivePointerAlignment = false;
4570   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4571                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
4572                "    *b = bbbbbbbbbbbbbbbbbbb;",
4573                Style);
4574   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4575                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
4576                Style);
4577 }
4578 
4579 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
4580   verifyFormat("arr[foo ? bar : baz];");
4581   verifyFormat("f()[foo ? bar : baz];");
4582   verifyFormat("(a + b)[foo ? bar : baz];");
4583   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
4584 }
4585 
4586 TEST_F(FormatTest, AlignsStringLiterals) {
4587   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
4588                "                                      \"short literal\");");
4589   verifyFormat(
4590       "looooooooooooooooooooooooongFunction(\n"
4591       "    \"short literal\"\n"
4592       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
4593   verifyFormat("someFunction(\"Always break between multi-line\"\n"
4594                "             \" string literals\",\n"
4595                "             and, other, parameters);");
4596   EXPECT_EQ("fun + \"1243\" /* comment */\n"
4597             "      \"5678\";",
4598             format("fun + \"1243\" /* comment */\n"
4599                    "      \"5678\";",
4600                    getLLVMStyleWithColumns(28)));
4601   EXPECT_EQ(
4602       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
4603       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
4604       "         \"aaaaaaaaaaaaaaaa\";",
4605       format("aaaaaa ="
4606              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
4607              "aaaaaaaaaaaaaaaaaaaaa\" "
4608              "\"aaaaaaaaaaaaaaaa\";"));
4609   verifyFormat("a = a + \"a\"\n"
4610                "        \"a\"\n"
4611                "        \"a\";");
4612   verifyFormat("f(\"a\", \"b\"\n"
4613                "       \"c\");");
4614 
4615   verifyFormat(
4616       "#define LL_FORMAT \"ll\"\n"
4617       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
4618       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
4619 
4620   verifyFormat("#define A(X)          \\\n"
4621                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
4622                "  \"ccccc\"",
4623                getLLVMStyleWithColumns(23));
4624   verifyFormat("#define A \"def\"\n"
4625                "f(\"abc\" A \"ghi\"\n"
4626                "  \"jkl\");");
4627 
4628   verifyFormat("f(L\"a\"\n"
4629                "  L\"b\");");
4630   verifyFormat("#define A(X)            \\\n"
4631                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
4632                "  L\"ccccc\"",
4633                getLLVMStyleWithColumns(25));
4634 
4635   verifyFormat("f(@\"a\"\n"
4636                "  @\"b\");");
4637   verifyFormat("NSString s = @\"a\"\n"
4638                "             @\"b\"\n"
4639                "             @\"c\";");
4640   verifyFormat("NSString s = @\"a\"\n"
4641                "              \"b\"\n"
4642                "              \"c\";");
4643 }
4644 
4645 TEST_F(FormatTest, DefinitionReturnTypeBreakingStyle) {
4646   FormatStyle Style = getLLVMStyle();
4647   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_TopLevel;
4648   verifyFormat("class C {\n"
4649                "  int f() { return 1; }\n"
4650                "};\n"
4651                "int\n"
4652                "f() {\n"
4653                "  return 1;\n"
4654                "}",
4655                Style);
4656   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
4657   verifyFormat("class C {\n"
4658                "  int\n"
4659                "  f() {\n"
4660                "    return 1;\n"
4661                "  }\n"
4662                "};\n"
4663                "int\n"
4664                "f() {\n"
4665                "  return 1;\n"
4666                "}",
4667                Style);
4668   verifyFormat("const char *\n"
4669                "f(void) {\n" // Break here.
4670                "  return \"\";\n"
4671                "}\n"
4672                "const char *bar(void);\n", // No break here.
4673                Style);
4674   verifyFormat("template <class T>\n"
4675                "T *\n"
4676                "f(T &c) {\n" // Break here.
4677                "  return NULL;\n"
4678                "}\n"
4679                "template <class T> T *f(T &c);\n", // No break here.
4680                Style);
4681   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4682   verifyFormat("const char *\n"
4683                "f(void)\n" // Break here.
4684                "{\n"
4685                "  return \"\";\n"
4686                "}\n"
4687                "const char *bar(void);\n", // No break here.
4688                Style);
4689   verifyFormat("template <class T>\n"
4690                "T *\n"     // Problem here: no line break
4691                "f(T &c)\n" // Break here.
4692                "{\n"
4693                "  return NULL;\n"
4694                "}\n"
4695                "template <class T> T *f(T &c);\n", // No break here.
4696                Style);
4697 }
4698 
4699 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
4700   FormatStyle NoBreak = getLLVMStyle();
4701   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
4702   FormatStyle Break = getLLVMStyle();
4703   Break.AlwaysBreakBeforeMultilineStrings = true;
4704   verifyFormat("aaaa = \"bbbb\"\n"
4705                "       \"cccc\";",
4706                NoBreak);
4707   verifyFormat("aaaa =\n"
4708                "    \"bbbb\"\n"
4709                "    \"cccc\";",
4710                Break);
4711   verifyFormat("aaaa(\"bbbb\"\n"
4712                "     \"cccc\");",
4713                NoBreak);
4714   verifyFormat("aaaa(\n"
4715                "    \"bbbb\"\n"
4716                "    \"cccc\");",
4717                Break);
4718   verifyFormat("aaaa(qqq, \"bbbb\"\n"
4719                "          \"cccc\");",
4720                NoBreak);
4721   verifyFormat("aaaa(qqq,\n"
4722                "     \"bbbb\"\n"
4723                "     \"cccc\");",
4724                Break);
4725   verifyFormat("aaaa(qqq,\n"
4726                "     L\"bbbb\"\n"
4727                "     L\"cccc\");",
4728                Break);
4729   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
4730                "                      \"bbbb\"));",
4731                Break);
4732   verifyFormat("string s = someFunction(\n"
4733                "    \"abc\"\n"
4734                "    \"abc\");",
4735                Break);
4736 
4737   // As we break before unary operators, breaking right after them is bad.
4738   verifyFormat("string foo = abc ? \"x\"\n"
4739                "                   \"blah blah blah blah blah blah\"\n"
4740                "                 : \"y\";",
4741                Break);
4742 
4743   // Don't break if there is no column gain.
4744   verifyFormat("f(\"aaaa\"\n"
4745                "  \"bbbb\");",
4746                Break);
4747 
4748   // Treat literals with escaped newlines like multi-line string literals.
4749   EXPECT_EQ("x = \"a\\\n"
4750             "b\\\n"
4751             "c\";",
4752             format("x = \"a\\\n"
4753                    "b\\\n"
4754                    "c\";",
4755                    NoBreak));
4756   EXPECT_EQ("xxxx =\n"
4757             "    \"a\\\n"
4758             "b\\\n"
4759             "c\";",
4760             format("xxxx = \"a\\\n"
4761                    "b\\\n"
4762                    "c\";",
4763                    Break));
4764 
4765   // Exempt ObjC strings for now.
4766   EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
4767             "                          @\"bbbb\";",
4768             format("NSString *const kString = @\"aaaa\"\n"
4769                    "@\"bbbb\";",
4770                    Break));
4771 
4772   Break.ColumnLimit = 0;
4773   verifyFormat("const char *hello = \"hello llvm\";", Break);
4774 }
4775 
4776 TEST_F(FormatTest, AlignsPipes) {
4777   verifyFormat(
4778       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4779       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4780       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4781   verifyFormat(
4782       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
4783       "                     << aaaaaaaaaaaaaaaaaaaa;");
4784   verifyFormat(
4785       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4786       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4787   verifyFormat(
4788       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
4789       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
4790       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
4791   verifyFormat(
4792       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4793       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4794       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4795   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4796                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4797                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4798                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4799   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
4800                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
4801   verifyFormat(
4802       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4803       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4804 
4805   verifyFormat("return out << \"somepacket = {\\n\"\n"
4806                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
4807                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
4808                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
4809                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
4810                "           << \"}\";");
4811 
4812   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4813                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4814                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
4815   verifyFormat(
4816       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
4817       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
4818       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
4819       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
4820       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
4821   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
4822                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4823   verifyFormat(
4824       "void f() {\n"
4825       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
4826       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4827       "}");
4828   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
4829                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
4830   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4831                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4832                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
4833                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
4834   verifyFormat("LOG_IF(aaa == //\n"
4835                "       bbb)\n"
4836                "    << a << b;");
4837 
4838   // Breaking before the first "<<" is generally not desirable.
4839   verifyFormat(
4840       "llvm::errs()\n"
4841       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4842       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4843       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4844       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4845       getLLVMStyleWithColumns(70));
4846   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4847                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4848                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4849                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4850                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4851                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4852                getLLVMStyleWithColumns(70));
4853 
4854   // But sometimes, breaking before the first "<<" is desirable.
4855   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4856                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
4857   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
4858                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4859                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4860   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
4861                "    << BEF << IsTemplate << Description << E->getType();");
4862 
4863   verifyFormat(
4864       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4865       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4866 
4867   // Incomplete string literal.
4868   EXPECT_EQ("llvm::errs() << \"\n"
4869             "             << a;",
4870             format("llvm::errs() << \"\n<<a;"));
4871 
4872   verifyFormat("void f() {\n"
4873                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
4874                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
4875                "}");
4876 
4877   // Handle 'endl'.
4878   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
4879                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4880   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4881 }
4882 
4883 TEST_F(FormatTest, UnderstandsEquals) {
4884   verifyFormat(
4885       "aaaaaaaaaaaaaaaaa =\n"
4886       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4887   verifyFormat(
4888       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4889       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4890   verifyFormat(
4891       "if (a) {\n"
4892       "  f();\n"
4893       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4894       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
4895       "}");
4896 
4897   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4898                "        100000000 + 10000000) {\n}");
4899 }
4900 
4901 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
4902   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4903                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
4904 
4905   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4906                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
4907 
4908   verifyFormat(
4909       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
4910       "                                                          Parameter2);");
4911 
4912   verifyFormat(
4913       "ShortObject->shortFunction(\n"
4914       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
4915       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
4916 
4917   verifyFormat("loooooooooooooongFunction(\n"
4918                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
4919 
4920   verifyFormat(
4921       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
4922       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
4923 
4924   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
4925                "    .WillRepeatedly(Return(SomeValue));");
4926   verifyFormat("void f() {\n"
4927                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
4928                "      .Times(2)\n"
4929                "      .WillRepeatedly(Return(SomeValue));\n"
4930                "}");
4931   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
4932                "    ccccccccccccccccccccccc);");
4933   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4934                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4935                "          .aaaaa(aaaaa),\n"
4936                "      aaaaaaaaaaaaaaaaaaaaa);");
4937   verifyFormat("void f() {\n"
4938                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4939                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
4940                "}");
4941   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4942                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4943                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4944                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4945                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4946   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4947                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4948                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4949                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
4950                "}");
4951 
4952   // Here, it is not necessary to wrap at "." or "->".
4953   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
4954                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4955   verifyFormat(
4956       "aaaaaaaaaaa->aaaaaaaaa(\n"
4957       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4958       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
4959 
4960   verifyFormat(
4961       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4962       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
4963   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
4964                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
4965   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
4966                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
4967 
4968   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4969                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4970                "    .a();");
4971 
4972   FormatStyle NoBinPacking = getLLVMStyle();
4973   NoBinPacking.BinPackParameters = false;
4974   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
4975                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
4976                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
4977                "                         aaaaaaaaaaaaaaaaaaa,\n"
4978                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4979                NoBinPacking);
4980 
4981   // If there is a subsequent call, change to hanging indentation.
4982   verifyFormat(
4983       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4984       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
4985       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4986   verifyFormat(
4987       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4988       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
4989   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4990                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4991                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4992   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4993                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4994                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
4995 }
4996 
4997 TEST_F(FormatTest, WrapsTemplateDeclarations) {
4998   verifyFormat("template <typename T>\n"
4999                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5000   verifyFormat("template <typename T>\n"
5001                "// T should be one of {A, B}.\n"
5002                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5003   verifyFormat(
5004       "template <typename T>\n"
5005       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
5006   verifyFormat("template <typename T>\n"
5007                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
5008                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
5009   verifyFormat(
5010       "template <typename T>\n"
5011       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
5012       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
5013   verifyFormat(
5014       "template <typename T>\n"
5015       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
5016       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
5017       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5018   verifyFormat("template <typename T>\n"
5019                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5020                "    int aaaaaaaaaaaaaaaaaaaaaa);");
5021   verifyFormat(
5022       "template <typename T1, typename T2 = char, typename T3 = char,\n"
5023       "          typename T4 = char>\n"
5024       "void f();");
5025   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
5026                "          template <typename> class cccccccccccccccccccccc,\n"
5027                "          typename ddddddddddddd>\n"
5028                "class C {};");
5029   verifyFormat(
5030       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
5031       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5032 
5033   verifyFormat("void f() {\n"
5034                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
5035                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
5036                "}");
5037 
5038   verifyFormat("template <typename T> class C {};");
5039   verifyFormat("template <typename T> void f();");
5040   verifyFormat("template <typename T> void f() {}");
5041   verifyFormat(
5042       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5043       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5044       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
5045       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5046       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5047       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
5048       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
5049       getLLVMStyleWithColumns(72));
5050   EXPECT_EQ("static_cast<A< //\n"
5051             "    B> *>(\n"
5052             "\n"
5053             "    );",
5054             format("static_cast<A<//\n"
5055                    "    B>*>(\n"
5056                    "\n"
5057                    "    );"));
5058   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5059                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
5060 
5061   FormatStyle AlwaysBreak = getLLVMStyle();
5062   AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
5063   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
5064   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
5065   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
5066   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5067                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5068                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
5069   verifyFormat("template <template <typename> class Fooooooo,\n"
5070                "          template <typename> class Baaaaaaar>\n"
5071                "struct C {};",
5072                AlwaysBreak);
5073   verifyFormat("template <typename T> // T can be A, B or C.\n"
5074                "struct C {};",
5075                AlwaysBreak);
5076 }
5077 
5078 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
5079   verifyFormat(
5080       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5081       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5082   verifyFormat(
5083       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5084       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5085       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5086 
5087   // FIXME: Should we have the extra indent after the second break?
5088   verifyFormat(
5089       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5090       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5091       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5092 
5093   verifyFormat(
5094       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
5095       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
5096 
5097   // Breaking at nested name specifiers is generally not desirable.
5098   verifyFormat(
5099       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5100       "    aaaaaaaaaaaaaaaaaaaaaaa);");
5101 
5102   verifyFormat(
5103       "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5104       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5105       "                   aaaaaaaaaaaaaaaaaaaaa);",
5106       getLLVMStyleWithColumns(74));
5107 
5108   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5109                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5110                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5111 }
5112 
5113 TEST_F(FormatTest, UnderstandsTemplateParameters) {
5114   verifyFormat("A<int> a;");
5115   verifyFormat("A<A<A<int>>> a;");
5116   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
5117   verifyFormat("bool x = a < 1 || 2 > a;");
5118   verifyFormat("bool x = 5 < f<int>();");
5119   verifyFormat("bool x = f<int>() > 5;");
5120   verifyFormat("bool x = 5 < a<int>::x;");
5121   verifyFormat("bool x = a < 4 ? a > 2 : false;");
5122   verifyFormat("bool x = f() ? a < 2 : a > 2;");
5123 
5124   verifyGoogleFormat("A<A<int>> a;");
5125   verifyGoogleFormat("A<A<A<int>>> a;");
5126   verifyGoogleFormat("A<A<A<A<int>>>> a;");
5127   verifyGoogleFormat("A<A<int> > a;");
5128   verifyGoogleFormat("A<A<A<int> > > a;");
5129   verifyGoogleFormat("A<A<A<A<int> > > > a;");
5130   verifyGoogleFormat("A<::A<int>> a;");
5131   verifyGoogleFormat("A<::A> a;");
5132   verifyGoogleFormat("A< ::A> a;");
5133   verifyGoogleFormat("A< ::A<int> > a;");
5134   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
5135   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
5136   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
5137   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
5138 
5139   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
5140 
5141   verifyFormat("test >> a >> b;");
5142   verifyFormat("test << a >> b;");
5143 
5144   verifyFormat("f<int>();");
5145   verifyFormat("template <typename T> void f() {}");
5146   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
5147   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
5148                "sizeof(char)>::type>;");
5149   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
5150 
5151   // Not template parameters.
5152   verifyFormat("return a < b && c > d;");
5153   verifyFormat("void f() {\n"
5154                "  while (a < b && c > d) {\n"
5155                "  }\n"
5156                "}");
5157   verifyFormat("template <typename... Types>\n"
5158                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
5159 
5160   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5161                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
5162                getLLVMStyleWithColumns(60));
5163   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
5164   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
5165   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
5166 }
5167 
5168 TEST_F(FormatTest, UnderstandsBinaryOperators) {
5169   verifyFormat("COMPARE(a, ==, b);");
5170 }
5171 
5172 TEST_F(FormatTest, UnderstandsPointersToMembers) {
5173   verifyFormat("int A::*x;");
5174   verifyFormat("int (S::*func)(void *);");
5175   verifyFormat("void f() { int (S::*func)(void *); }");
5176   verifyFormat("typedef bool *(Class::*Member)() const;");
5177   verifyFormat("void f() {\n"
5178                "  (a->*f)();\n"
5179                "  a->*x;\n"
5180                "  (a.*f)();\n"
5181                "  ((*a).*f)();\n"
5182                "  a.*x;\n"
5183                "}");
5184   verifyFormat("void f() {\n"
5185                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5186                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
5187                "}");
5188   verifyFormat(
5189       "(aaaaaaaaaa->*bbbbbbb)(\n"
5190       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5191   FormatStyle Style = getLLVMStyle();
5192   Style.PointerAlignment = FormatStyle::PAS_Left;
5193   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
5194 }
5195 
5196 TEST_F(FormatTest, UnderstandsUnaryOperators) {
5197   verifyFormat("int a = -2;");
5198   verifyFormat("f(-1, -2, -3);");
5199   verifyFormat("a[-1] = 5;");
5200   verifyFormat("int a = 5 + -2;");
5201   verifyFormat("if (i == -1) {\n}");
5202   verifyFormat("if (i != -1) {\n}");
5203   verifyFormat("if (i > -1) {\n}");
5204   verifyFormat("if (i < -1) {\n}");
5205   verifyFormat("++(a->f());");
5206   verifyFormat("--(a->f());");
5207   verifyFormat("(a->f())++;");
5208   verifyFormat("a[42]++;");
5209   verifyFormat("if (!(a->f())) {\n}");
5210 
5211   verifyFormat("a-- > b;");
5212   verifyFormat("b ? -a : c;");
5213   verifyFormat("n * sizeof char16;");
5214   verifyFormat("n * alignof char16;", getGoogleStyle());
5215   verifyFormat("sizeof(char);");
5216   verifyFormat("alignof(char);", getGoogleStyle());
5217 
5218   verifyFormat("return -1;");
5219   verifyFormat("switch (a) {\n"
5220                "case -1:\n"
5221                "  break;\n"
5222                "}");
5223   verifyFormat("#define X -1");
5224   verifyFormat("#define X -kConstant");
5225 
5226   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
5227   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
5228 
5229   verifyFormat("int a = /* confusing comment */ -1;");
5230   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
5231   verifyFormat("int a = i /* confusing comment */++;");
5232 }
5233 
5234 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
5235   verifyFormat("if (!aaaaaaaaaa( // break\n"
5236                "        aaaaa)) {\n"
5237                "}");
5238   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
5239                "    aaaaa));");
5240   verifyFormat("*aaa = aaaaaaa( // break\n"
5241                "    bbbbbb);");
5242 }
5243 
5244 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
5245   verifyFormat("bool operator<();");
5246   verifyFormat("bool operator>();");
5247   verifyFormat("bool operator=();");
5248   verifyFormat("bool operator==();");
5249   verifyFormat("bool operator!=();");
5250   verifyFormat("int operator+();");
5251   verifyFormat("int operator++();");
5252   verifyFormat("bool operator();");
5253   verifyFormat("bool operator()();");
5254   verifyFormat("bool operator[]();");
5255   verifyFormat("operator bool();");
5256   verifyFormat("operator int();");
5257   verifyFormat("operator void *();");
5258   verifyFormat("operator SomeType<int>();");
5259   verifyFormat("operator SomeType<int, int>();");
5260   verifyFormat("operator SomeType<SomeType<int>>();");
5261   verifyFormat("void *operator new(std::size_t size);");
5262   verifyFormat("void *operator new[](std::size_t size);");
5263   verifyFormat("void operator delete(void *ptr);");
5264   verifyFormat("void operator delete[](void *ptr);");
5265   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
5266                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
5267 
5268   verifyFormat(
5269       "ostream &operator<<(ostream &OutputStream,\n"
5270       "                    SomeReallyLongType WithSomeReallyLongValue);");
5271   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
5272                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
5273                "  return left.group < right.group;\n"
5274                "}");
5275   verifyFormat("SomeType &operator=(const SomeType &S);");
5276   verifyFormat("f.template operator()<int>();");
5277 
5278   verifyGoogleFormat("operator void*();");
5279   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
5280   verifyGoogleFormat("operator ::A();");
5281 
5282   verifyFormat("using A::operator+;");
5283 }
5284 
5285 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
5286   verifyFormat("Deleted &operator=(const Deleted &)& = default;");
5287   verifyFormat("Deleted &operator=(const Deleted &)&& = delete;");
5288   verifyFormat("SomeType MemberFunction(const Deleted &)& = delete;");
5289   verifyFormat("SomeType MemberFunction(const Deleted &)&& = delete;");
5290   verifyFormat("Deleted &operator=(const Deleted &)&;");
5291   verifyFormat("Deleted &operator=(const Deleted &)&&;");
5292   verifyFormat("SomeType MemberFunction(const Deleted &)&;");
5293   verifyFormat("SomeType MemberFunction(const Deleted &)&&;");
5294 
5295   verifyGoogleFormat("Deleted& operator=(const Deleted&)& = default;");
5296   verifyGoogleFormat("SomeType MemberFunction(const Deleted&)& = delete;");
5297   verifyGoogleFormat("Deleted& operator=(const Deleted&)&;");
5298   verifyGoogleFormat("SomeType MemberFunction(const Deleted&)&;");
5299 
5300   FormatStyle Spaces = getLLVMStyle();
5301   Spaces.SpacesInCStyleCastParentheses = true;
5302   verifyFormat("Deleted &operator=(const Deleted &)& = default;", Spaces);
5303   verifyFormat("SomeType MemberFunction(const Deleted &)& = delete;", Spaces);
5304   verifyFormat("Deleted &operator=(const Deleted &)&;", Spaces);
5305   verifyFormat("SomeType MemberFunction(const Deleted &)&;", Spaces);
5306 
5307   Spaces.SpacesInCStyleCastParentheses = false;
5308   Spaces.SpacesInParentheses = true;
5309   verifyFormat("Deleted &operator=( const Deleted & )& = default;", Spaces);
5310   verifyFormat("SomeType MemberFunction( const Deleted & )& = delete;", Spaces);
5311   verifyFormat("Deleted &operator=( const Deleted & )&;", Spaces);
5312   verifyFormat("SomeType MemberFunction( const Deleted & )&;", Spaces);
5313 }
5314 
5315 TEST_F(FormatTest, UnderstandsNewAndDelete) {
5316   verifyFormat("void f() {\n"
5317                "  A *a = new A;\n"
5318                "  A *a = new (placement) A;\n"
5319                "  delete a;\n"
5320                "  delete (A *)a;\n"
5321                "}");
5322   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5323                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5324   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5325                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5326                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5327   verifyFormat("delete[] h->p;");
5328 }
5329 
5330 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
5331   verifyFormat("int *f(int *a) {}");
5332   verifyFormat("int main(int argc, char **argv) {}");
5333   verifyFormat("Test::Test(int b) : a(b * b) {}");
5334   verifyIndependentOfContext("f(a, *a);");
5335   verifyFormat("void g() { f(*a); }");
5336   verifyIndependentOfContext("int a = b * 10;");
5337   verifyIndependentOfContext("int a = 10 * b;");
5338   verifyIndependentOfContext("int a = b * c;");
5339   verifyIndependentOfContext("int a += b * c;");
5340   verifyIndependentOfContext("int a -= b * c;");
5341   verifyIndependentOfContext("int a *= b * c;");
5342   verifyIndependentOfContext("int a /= b * c;");
5343   verifyIndependentOfContext("int a = *b;");
5344   verifyIndependentOfContext("int a = *b * c;");
5345   verifyIndependentOfContext("int a = b * *c;");
5346   verifyIndependentOfContext("int a = b * (10);");
5347   verifyIndependentOfContext("S << b * (10);");
5348   verifyIndependentOfContext("return 10 * b;");
5349   verifyIndependentOfContext("return *b * *c;");
5350   verifyIndependentOfContext("return a & ~b;");
5351   verifyIndependentOfContext("f(b ? *c : *d);");
5352   verifyIndependentOfContext("int a = b ? *c : *d;");
5353   verifyIndependentOfContext("*b = a;");
5354   verifyIndependentOfContext("a * ~b;");
5355   verifyIndependentOfContext("a * !b;");
5356   verifyIndependentOfContext("a * +b;");
5357   verifyIndependentOfContext("a * -b;");
5358   verifyIndependentOfContext("a * ++b;");
5359   verifyIndependentOfContext("a * --b;");
5360   verifyIndependentOfContext("a[4] * b;");
5361   verifyIndependentOfContext("a[a * a] = 1;");
5362   verifyIndependentOfContext("f() * b;");
5363   verifyIndependentOfContext("a * [self dostuff];");
5364   verifyIndependentOfContext("int x = a * (a + b);");
5365   verifyIndependentOfContext("(a *)(a + b);");
5366   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
5367   verifyIndependentOfContext("int *pa = (int *)&a;");
5368   verifyIndependentOfContext("return sizeof(int **);");
5369   verifyIndependentOfContext("return sizeof(int ******);");
5370   verifyIndependentOfContext("return (int **&)a;");
5371   verifyIndependentOfContext("f((*PointerToArray)[10]);");
5372   verifyFormat("void f(Type (*parameter)[10]) {}");
5373   verifyGoogleFormat("return sizeof(int**);");
5374   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
5375   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
5376   verifyFormat("auto a = [](int **&, int ***) {};");
5377   verifyFormat("auto PointerBinding = [](const char *S) {};");
5378   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
5379   verifyFormat("[](const decltype(*a) &value) {}");
5380   verifyFormat("#define MACRO() [](A *a) { return 1; }");
5381   verifyIndependentOfContext("typedef void (*f)(int *a);");
5382   verifyIndependentOfContext("int i{a * b};");
5383   verifyIndependentOfContext("aaa && aaa->f();");
5384   verifyIndependentOfContext("int x = ~*p;");
5385   verifyFormat("Constructor() : a(a), area(width * height) {}");
5386   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
5387   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
5388   verifyFormat("void f() { f(a, c * d); }");
5389   verifyFormat("void f() { f(new a(), c * d); }");
5390 
5391   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
5392 
5393   verifyIndependentOfContext("A<int *> a;");
5394   verifyIndependentOfContext("A<int **> a;");
5395   verifyIndependentOfContext("A<int *, int *> a;");
5396   verifyIndependentOfContext("A<int *[]> a;");
5397   verifyIndependentOfContext(
5398       "const char *const p = reinterpret_cast<const char *const>(q);");
5399   verifyIndependentOfContext("A<int **, int **> a;");
5400   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
5401   verifyFormat("for (char **a = b; *a; ++a) {\n}");
5402   verifyFormat("for (; a && b;) {\n}");
5403   verifyFormat("bool foo = true && [] { return false; }();");
5404 
5405   verifyFormat(
5406       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5407       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5408 
5409   verifyGoogleFormat("**outparam = 1;");
5410   verifyGoogleFormat("*outparam = a * b;");
5411   verifyGoogleFormat("int main(int argc, char** argv) {}");
5412   verifyGoogleFormat("A<int*> a;");
5413   verifyGoogleFormat("A<int**> a;");
5414   verifyGoogleFormat("A<int*, int*> a;");
5415   verifyGoogleFormat("A<int**, int**> a;");
5416   verifyGoogleFormat("f(b ? *c : *d);");
5417   verifyGoogleFormat("int a = b ? *c : *d;");
5418   verifyGoogleFormat("Type* t = **x;");
5419   verifyGoogleFormat("Type* t = *++*x;");
5420   verifyGoogleFormat("*++*x;");
5421   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
5422   verifyGoogleFormat("Type* t = x++ * y;");
5423   verifyGoogleFormat(
5424       "const char* const p = reinterpret_cast<const char* const>(q);");
5425   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
5426   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
5427   verifyGoogleFormat("template <typename T>\n"
5428                      "void f(int i = 0, SomeType** temps = NULL);");
5429 
5430   FormatStyle Left = getLLVMStyle();
5431   Left.PointerAlignment = FormatStyle::PAS_Left;
5432   verifyFormat("x = *a(x) = *a(y);", Left);
5433 
5434   verifyIndependentOfContext("a = *(x + y);");
5435   verifyIndependentOfContext("a = &(x + y);");
5436   verifyIndependentOfContext("*(x + y).call();");
5437   verifyIndependentOfContext("&(x + y)->call();");
5438   verifyFormat("void f() { &(*I).first; }");
5439 
5440   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
5441   verifyFormat(
5442       "int *MyValues = {\n"
5443       "    *A, // Operator detection might be confused by the '{'\n"
5444       "    *BB // Operator detection might be confused by previous comment\n"
5445       "};");
5446 
5447   verifyIndependentOfContext("if (int *a = &b)");
5448   verifyIndependentOfContext("if (int &a = *b)");
5449   verifyIndependentOfContext("if (a & b[i])");
5450   verifyIndependentOfContext("if (a::b::c::d & b[i])");
5451   verifyIndependentOfContext("if (*b[i])");
5452   verifyIndependentOfContext("if (int *a = (&b))");
5453   verifyIndependentOfContext("while (int *a = &b)");
5454   verifyIndependentOfContext("size = sizeof *a;");
5455   verifyIndependentOfContext("if (a && (b = c))");
5456   verifyFormat("void f() {\n"
5457                "  for (const int &v : Values) {\n"
5458                "  }\n"
5459                "}");
5460   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
5461   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
5462   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
5463 
5464   verifyFormat("#define A (!a * b)");
5465   verifyFormat("#define MACRO     \\\n"
5466                "  int *i = a * b; \\\n"
5467                "  void f(a *b);",
5468                getLLVMStyleWithColumns(19));
5469 
5470   verifyIndependentOfContext("A = new SomeType *[Length];");
5471   verifyIndependentOfContext("A = new SomeType *[Length]();");
5472   verifyIndependentOfContext("T **t = new T *;");
5473   verifyIndependentOfContext("T **t = new T *();");
5474   verifyGoogleFormat("A = new SomeType*[Length]();");
5475   verifyGoogleFormat("A = new SomeType*[Length];");
5476   verifyGoogleFormat("T** t = new T*;");
5477   verifyGoogleFormat("T** t = new T*();");
5478 
5479   FormatStyle PointerLeft = getLLVMStyle();
5480   PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
5481   verifyFormat("delete *x;", PointerLeft);
5482   verifyFormat("STATIC_ASSERT((a & b) == 0);");
5483   verifyFormat("STATIC_ASSERT(0 == (a & b));");
5484   verifyFormat("template <bool a, bool b> "
5485                "typename t::if<x && y>::type f() {}");
5486   verifyFormat("template <int *y> f() {}");
5487   verifyFormat("vector<int *> v;");
5488   verifyFormat("vector<int *const> v;");
5489   verifyFormat("vector<int *const **const *> v;");
5490   verifyFormat("vector<int *volatile> v;");
5491   verifyFormat("vector<a * b> v;");
5492   verifyFormat("foo<b && false>();");
5493   verifyFormat("foo<b & 1>();");
5494   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
5495   verifyFormat(
5496       "template <class T, class = typename std::enable_if<\n"
5497       "                       std::is_integral<T>::value &&\n"
5498       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
5499       "void F();",
5500       getLLVMStyleWithColumns(76));
5501   verifyFormat(
5502       "template <class T,\n"
5503       "          class = typename ::std::enable_if<\n"
5504       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
5505       "void F();",
5506       getGoogleStyleWithColumns(68));
5507 
5508   verifyIndependentOfContext("MACRO(int *i);");
5509   verifyIndependentOfContext("MACRO(auto *a);");
5510   verifyIndependentOfContext("MACRO(const A *a);");
5511   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
5512   // FIXME: Is there a way to make this work?
5513   // verifyIndependentOfContext("MACRO(A *a);");
5514 
5515   verifyFormat("DatumHandle const *operator->() const { return input_; }");
5516 
5517   EXPECT_EQ("#define OP(x)                                    \\\n"
5518             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
5519             "    return s << a.DebugString();                 \\\n"
5520             "  }",
5521             format("#define OP(x) \\\n"
5522                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
5523                    "    return s << a.DebugString(); \\\n"
5524                    "  }",
5525                    getLLVMStyleWithColumns(50)));
5526 
5527   // FIXME: We cannot handle this case yet; we might be able to figure out that
5528   // foo<x> d > v; doesn't make sense.
5529   verifyFormat("foo<a<b && c> d> v;");
5530 
5531   FormatStyle PointerMiddle = getLLVMStyle();
5532   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
5533   verifyFormat("delete *x;", PointerMiddle);
5534   verifyFormat("int * x;", PointerMiddle);
5535   verifyFormat("template <int * y> f() {}", PointerMiddle);
5536   verifyFormat("int * f(int * a) {}", PointerMiddle);
5537   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
5538   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
5539   verifyFormat("A<int *> a;", PointerMiddle);
5540   verifyFormat("A<int **> a;", PointerMiddle);
5541   verifyFormat("A<int *, int *> a;", PointerMiddle);
5542   verifyFormat("A<int * []> a;", PointerMiddle);
5543   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
5544   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
5545   verifyFormat("T ** t = new T *;", PointerMiddle);
5546 
5547   // Member function reference qualifiers aren't binary operators.
5548   verifyFormat("string // break\n"
5549                "operator()() & {}");
5550   verifyFormat("string // break\n"
5551                "operator()() && {}");
5552   verifyGoogleFormat("template <typename T>\n"
5553                      "auto x() & -> int {}");
5554 }
5555 
5556 TEST_F(FormatTest, UnderstandsAttributes) {
5557   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
5558   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
5559                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
5560   FormatStyle AfterType = getLLVMStyle();
5561   AfterType.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
5562   verifyFormat("__attribute__((nodebug)) void\n"
5563                "foo() {}\n",
5564                AfterType);
5565 }
5566 
5567 TEST_F(FormatTest, UnderstandsEllipsis) {
5568   verifyFormat("int printf(const char *fmt, ...);");
5569   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
5570   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
5571 
5572   FormatStyle PointersLeft = getLLVMStyle();
5573   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
5574   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
5575 }
5576 
5577 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
5578   EXPECT_EQ("int *a;\n"
5579             "int *a;\n"
5580             "int *a;",
5581             format("int *a;\n"
5582                    "int* a;\n"
5583                    "int *a;",
5584                    getGoogleStyle()));
5585   EXPECT_EQ("int* a;\n"
5586             "int* a;\n"
5587             "int* a;",
5588             format("int* a;\n"
5589                    "int* a;\n"
5590                    "int *a;",
5591                    getGoogleStyle()));
5592   EXPECT_EQ("int *a;\n"
5593             "int *a;\n"
5594             "int *a;",
5595             format("int *a;\n"
5596                    "int * a;\n"
5597                    "int *  a;",
5598                    getGoogleStyle()));
5599 }
5600 
5601 TEST_F(FormatTest, UnderstandsRvalueReferences) {
5602   verifyFormat("int f(int &&a) {}");
5603   verifyFormat("int f(int a, char &&b) {}");
5604   verifyFormat("void f() { int &&a = b; }");
5605   verifyGoogleFormat("int f(int a, char&& b) {}");
5606   verifyGoogleFormat("void f() { int&& a = b; }");
5607 
5608   verifyIndependentOfContext("A<int &&> a;");
5609   verifyIndependentOfContext("A<int &&, int &&> a;");
5610   verifyGoogleFormat("A<int&&> a;");
5611   verifyGoogleFormat("A<int&&, int&&> a;");
5612 
5613   // Not rvalue references:
5614   verifyFormat("template <bool B, bool C> class A {\n"
5615                "  static_assert(B && C, \"Something is wrong\");\n"
5616                "};");
5617   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
5618   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
5619   verifyFormat("#define A(a, b) (a && b)");
5620 }
5621 
5622 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
5623   verifyFormat("void f() {\n"
5624                "  x[aaaaaaaaa -\n"
5625                "    b] = 23;\n"
5626                "}",
5627                getLLVMStyleWithColumns(15));
5628 }
5629 
5630 TEST_F(FormatTest, FormatsCasts) {
5631   verifyFormat("Type *A = static_cast<Type *>(P);");
5632   verifyFormat("Type *A = (Type *)P;");
5633   verifyFormat("Type *A = (vector<Type *, int *>)P;");
5634   verifyFormat("int a = (int)(2.0f);");
5635   verifyFormat("int a = (int)2.0f;");
5636   verifyFormat("x[(int32)y];");
5637   verifyFormat("x = (int32)y;");
5638   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
5639   verifyFormat("int a = (int)*b;");
5640   verifyFormat("int a = (int)2.0f;");
5641   verifyFormat("int a = (int)~0;");
5642   verifyFormat("int a = (int)++a;");
5643   verifyFormat("int a = (int)sizeof(int);");
5644   verifyFormat("int a = (int)+2;");
5645   verifyFormat("my_int a = (my_int)2.0f;");
5646   verifyFormat("my_int a = (my_int)sizeof(int);");
5647   verifyFormat("return (my_int)aaa;");
5648   verifyFormat("#define x ((int)-1)");
5649   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
5650   verifyFormat("#define p(q) ((int *)&q)");
5651   verifyFormat("fn(a)(b) + 1;");
5652 
5653   verifyFormat("void f() { my_int a = (my_int)*b; }");
5654   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
5655   verifyFormat("my_int a = (my_int)~0;");
5656   verifyFormat("my_int a = (my_int)++a;");
5657   verifyFormat("my_int a = (my_int)-2;");
5658   verifyFormat("my_int a = (my_int)1;");
5659   verifyFormat("my_int a = (my_int *)1;");
5660   verifyFormat("my_int a = (const my_int)-1;");
5661   verifyFormat("my_int a = (const my_int *)-1;");
5662   verifyFormat("my_int a = (my_int)(my_int)-1;");
5663   verifyFormat("my_int a = (ns::my_int)-2;");
5664   verifyFormat("case (my_int)ONE:");
5665 
5666   // FIXME: single value wrapped with paren will be treated as cast.
5667   verifyFormat("void f(int i = (kValue)*kMask) {}");
5668 
5669   verifyFormat("{ (void)F; }");
5670 
5671   // Don't break after a cast's
5672   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5673                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
5674                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
5675 
5676   // These are not casts.
5677   verifyFormat("void f(int *) {}");
5678   verifyFormat("f(foo)->b;");
5679   verifyFormat("f(foo).b;");
5680   verifyFormat("f(foo)(b);");
5681   verifyFormat("f(foo)[b];");
5682   verifyFormat("[](foo) { return 4; }(bar);");
5683   verifyFormat("(*funptr)(foo)[4];");
5684   verifyFormat("funptrs[4](foo)[4];");
5685   verifyFormat("void f(int *);");
5686   verifyFormat("void f(int *) = 0;");
5687   verifyFormat("void f(SmallVector<int>) {}");
5688   verifyFormat("void f(SmallVector<int>);");
5689   verifyFormat("void f(SmallVector<int>) = 0;");
5690   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
5691   verifyFormat("int a = sizeof(int) * b;");
5692   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
5693   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
5694   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
5695   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
5696 
5697   // These are not casts, but at some point were confused with casts.
5698   verifyFormat("virtual void foo(int *) override;");
5699   verifyFormat("virtual void foo(char &) const;");
5700   verifyFormat("virtual void foo(int *a, char *) const;");
5701   verifyFormat("int a = sizeof(int *) + b;");
5702   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
5703 
5704   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
5705                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5706   // FIXME: The indentation here is not ideal.
5707   verifyFormat(
5708       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5709       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
5710       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
5711 }
5712 
5713 TEST_F(FormatTest, FormatsFunctionTypes) {
5714   verifyFormat("A<bool()> a;");
5715   verifyFormat("A<SomeType()> a;");
5716   verifyFormat("A<void (*)(int, std::string)> a;");
5717   verifyFormat("A<void *(int)>;");
5718   verifyFormat("void *(*a)(int *, SomeType *);");
5719   verifyFormat("int (*func)(void *);");
5720   verifyFormat("void f() { int (*func)(void *); }");
5721   verifyFormat("template <class CallbackClass>\n"
5722                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
5723 
5724   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
5725   verifyGoogleFormat("void* (*a)(int);");
5726   verifyGoogleFormat(
5727       "template <class CallbackClass>\n"
5728       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
5729 
5730   // Other constructs can look somewhat like function types:
5731   verifyFormat("A<sizeof(*x)> a;");
5732   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
5733   verifyFormat("some_var = function(*some_pointer_var)[0];");
5734   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
5735 }
5736 
5737 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
5738   verifyFormat("A (*foo_)[6];");
5739   verifyFormat("vector<int> (*foo_)[6];");
5740 }
5741 
5742 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
5743   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5744                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5745   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
5746                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5747   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5748                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
5749 
5750   // Different ways of ()-initializiation.
5751   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5752                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
5753   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5754                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
5755   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5756                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
5757   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5758                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
5759 }
5760 
5761 TEST_F(FormatTest, BreaksLongDeclarations) {
5762   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
5763                "    AnotherNameForTheLongType;");
5764   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
5765                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5766   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5767                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5768   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
5769                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5770   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5771                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5772   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
5773                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5774   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5775                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5776   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5777                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5778   FormatStyle Indented = getLLVMStyle();
5779   Indented.IndentWrappedFunctionNames = true;
5780   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5781                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
5782                Indented);
5783   verifyFormat(
5784       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5785       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5786       Indented);
5787   verifyFormat(
5788       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5789       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5790       Indented);
5791   verifyFormat(
5792       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5793       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5794       Indented);
5795 
5796   // FIXME: Without the comment, this breaks after "(".
5797   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
5798                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
5799                getGoogleStyle());
5800 
5801   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
5802                "                  int LoooooooooooooooooooongParam2) {}");
5803   verifyFormat(
5804       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
5805       "                                   SourceLocation L, IdentifierIn *II,\n"
5806       "                                   Type *T) {}");
5807   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
5808                "ReallyReaaallyLongFunctionName(\n"
5809                "    const std::string &SomeParameter,\n"
5810                "    const SomeType<string, SomeOtherTemplateParameter>\n"
5811                "        &ReallyReallyLongParameterName,\n"
5812                "    const SomeType<string, SomeOtherTemplateParameter>\n"
5813                "        &AnotherLongParameterName) {}");
5814   verifyFormat("template <typename A>\n"
5815                "SomeLoooooooooooooooooooooongType<\n"
5816                "    typename some_namespace::SomeOtherType<A>::Type>\n"
5817                "Function() {}");
5818 
5819   verifyGoogleFormat(
5820       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
5821       "    aaaaaaaaaaaaaaaaaaaaaaa;");
5822   verifyGoogleFormat(
5823       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
5824       "                                   SourceLocation L) {}");
5825   verifyGoogleFormat(
5826       "some_namespace::LongReturnType\n"
5827       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
5828       "    int first_long_parameter, int second_parameter) {}");
5829 
5830   verifyGoogleFormat("template <typename T>\n"
5831                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5832                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
5833   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5834                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
5835 
5836   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5837                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5838                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5839   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5840                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5841                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
5842   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5843                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5844                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
5845                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5846 }
5847 
5848 TEST_F(FormatTest, FormatsArrays) {
5849   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5850                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
5851   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5852                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5853   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5854                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
5855   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5856                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5857                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5858   verifyFormat(
5859       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
5860       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5861       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
5862 
5863   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
5864                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
5865   verifyFormat(
5866       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
5867       "                                  .aaaaaaa[0]\n"
5868       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
5869 
5870   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
5871 }
5872 
5873 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
5874   verifyFormat("(a)->b();");
5875   verifyFormat("--a;");
5876 }
5877 
5878 TEST_F(FormatTest, HandlesIncludeDirectives) {
5879   verifyFormat("#include <string>\n"
5880                "#include <a/b/c.h>\n"
5881                "#include \"a/b/string\"\n"
5882                "#include \"string.h\"\n"
5883                "#include \"string.h\"\n"
5884                "#include <a-a>\n"
5885                "#include < path with space >\n"
5886                "#include_next <test.h>"
5887                "#include \"abc.h\" // this is included for ABC\n"
5888                "#include \"some long include\" // with a comment\n"
5889                "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
5890                getLLVMStyleWithColumns(35));
5891   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
5892   EXPECT_EQ("#include <a>", format("#include<a>"));
5893 
5894   verifyFormat("#import <string>");
5895   verifyFormat("#import <a/b/c.h>");
5896   verifyFormat("#import \"a/b/string\"");
5897   verifyFormat("#import \"string.h\"");
5898   verifyFormat("#import \"string.h\"");
5899   verifyFormat("#if __has_include(<strstream>)\n"
5900                "#include <strstream>\n"
5901                "#endif");
5902 
5903   verifyFormat("#define MY_IMPORT <a/b>");
5904 
5905   // Protocol buffer definition or missing "#".
5906   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
5907                getLLVMStyleWithColumns(30));
5908 
5909   FormatStyle Style = getLLVMStyle();
5910   Style.AlwaysBreakBeforeMultilineStrings = true;
5911   Style.ColumnLimit = 0;
5912   verifyFormat("#import \"abc.h\"", Style);
5913 
5914   // But 'import' might also be a regular C++ namespace.
5915   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5916                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5917 }
5918 
5919 //===----------------------------------------------------------------------===//
5920 // Error recovery tests.
5921 //===----------------------------------------------------------------------===//
5922 
5923 TEST_F(FormatTest, IncompleteParameterLists) {
5924   FormatStyle NoBinPacking = getLLVMStyle();
5925   NoBinPacking.BinPackParameters = false;
5926   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
5927                "                        double *min_x,\n"
5928                "                        double *max_x,\n"
5929                "                        double *min_y,\n"
5930                "                        double *max_y,\n"
5931                "                        double *min_z,\n"
5932                "                        double *max_z, ) {}",
5933                NoBinPacking);
5934 }
5935 
5936 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
5937   verifyFormat("void f() { return; }\n42");
5938   verifyFormat("void f() {\n"
5939                "  if (0)\n"
5940                "    return;\n"
5941                "}\n"
5942                "42");
5943   verifyFormat("void f() { return }\n42");
5944   verifyFormat("void f() {\n"
5945                "  if (0)\n"
5946                "    return\n"
5947                "}\n"
5948                "42");
5949 }
5950 
5951 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
5952   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
5953   EXPECT_EQ("void f() {\n"
5954             "  if (a)\n"
5955             "    return\n"
5956             "}",
5957             format("void  f  (  )  {  if  ( a )  return  }"));
5958   EXPECT_EQ("namespace N {\n"
5959             "void f()\n"
5960             "}",
5961             format("namespace  N  {  void f()  }"));
5962   EXPECT_EQ("namespace N {\n"
5963             "void f() {}\n"
5964             "void g()\n"
5965             "}",
5966             format("namespace N  { void f( ) { } void g( ) }"));
5967 }
5968 
5969 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
5970   verifyFormat("int aaaaaaaa =\n"
5971                "    // Overlylongcomment\n"
5972                "    b;",
5973                getLLVMStyleWithColumns(20));
5974   verifyFormat("function(\n"
5975                "    ShortArgument,\n"
5976                "    LoooooooooooongArgument);\n",
5977                getLLVMStyleWithColumns(20));
5978 }
5979 
5980 TEST_F(FormatTest, IncorrectAccessSpecifier) {
5981   verifyFormat("public:");
5982   verifyFormat("class A {\n"
5983                "public\n"
5984                "  void f() {}\n"
5985                "};");
5986   verifyFormat("public\n"
5987                "int qwerty;");
5988   verifyFormat("public\n"
5989                "B {}");
5990   verifyFormat("public\n"
5991                "{}");
5992   verifyFormat("public\n"
5993                "B { int x; }");
5994 }
5995 
5996 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
5997   verifyFormat("{");
5998   verifyFormat("#})");
5999   verifyNoCrash("(/**/[:!] ?[).");
6000 }
6001 
6002 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
6003   verifyFormat("do {\n}");
6004   verifyFormat("do {\n}\n"
6005                "f();");
6006   verifyFormat("do {\n}\n"
6007                "wheeee(fun);");
6008   verifyFormat("do {\n"
6009                "  f();\n"
6010                "}");
6011 }
6012 
6013 TEST_F(FormatTest, IncorrectCodeMissingParens) {
6014   verifyFormat("if {\n  foo;\n  foo();\n}");
6015   verifyFormat("switch {\n  foo;\n  foo();\n}");
6016   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
6017   verifyFormat("while {\n  foo;\n  foo();\n}");
6018   verifyFormat("do {\n  foo;\n  foo();\n} while;");
6019 }
6020 
6021 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
6022   verifyIncompleteFormat("namespace {\n"
6023                          "class Foo { Foo (\n"
6024                          "};\n"
6025                          "} // comment");
6026 }
6027 
6028 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
6029   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
6030   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
6031   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
6032   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
6033 
6034   EXPECT_EQ("{\n"
6035             "  {\n"
6036             "    breakme(\n"
6037             "        qwe);\n"
6038             "  }\n",
6039             format("{\n"
6040                    "    {\n"
6041                    " breakme(qwe);\n"
6042                    "}\n",
6043                    getLLVMStyleWithColumns(10)));
6044 }
6045 
6046 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
6047   verifyFormat("int x = {\n"
6048                "    avariable,\n"
6049                "    b(alongervariable)};",
6050                getLLVMStyleWithColumns(25));
6051 }
6052 
6053 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
6054   verifyFormat("return (a)(b){1, 2, 3};");
6055 }
6056 
6057 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
6058   verifyFormat("vector<int> x{1, 2, 3, 4};");
6059   verifyFormat("vector<int> x{\n"
6060                "    1, 2, 3, 4,\n"
6061                "};");
6062   verifyFormat("vector<T> x{{}, {}, {}, {}};");
6063   verifyFormat("f({1, 2});");
6064   verifyFormat("auto v = Foo{-1};");
6065   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
6066   verifyFormat("Class::Class : member{1, 2, 3} {}");
6067   verifyFormat("new vector<int>{1, 2, 3};");
6068   verifyFormat("new int[3]{1, 2, 3};");
6069   verifyFormat("new int{1};");
6070   verifyFormat("return {arg1, arg2};");
6071   verifyFormat("return {arg1, SomeType{parameter}};");
6072   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
6073   verifyFormat("new T{arg1, arg2};");
6074   verifyFormat("f(MyMap[{composite, key}]);");
6075   verifyFormat("class Class {\n"
6076                "  T member = {arg1, arg2};\n"
6077                "};");
6078   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
6079   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
6080   verifyFormat("int a = std::is_integral<int>{} + 0;");
6081 
6082   verifyFormat("int foo(int i) { return fo1{}(i); }");
6083   verifyFormat("int foo(int i) { return fo1{}(i); }");
6084   verifyFormat("auto i = decltype(x){};");
6085   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
6086   verifyFormat("Node n{1, Node{1000}, //\n"
6087                "       2};");
6088   verifyFormat("Aaaa aaaaaaa{\n"
6089                "    {\n"
6090                "        aaaa,\n"
6091                "    },\n"
6092                "};");
6093   verifyFormat("class C : public D {\n"
6094                "  SomeClass SC{2};\n"
6095                "};");
6096   verifyFormat("class C : public A {\n"
6097                "  class D : public B {\n"
6098                "    void f() { int i{2}; }\n"
6099                "  };\n"
6100                "};");
6101 
6102   // In combination with BinPackArguments = false.
6103   FormatStyle NoBinPacking = getLLVMStyle();
6104   NoBinPacking.BinPackArguments = false;
6105   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
6106                "                      bbbbb,\n"
6107                "                      ccccc,\n"
6108                "                      ddddd,\n"
6109                "                      eeeee,\n"
6110                "                      ffffff,\n"
6111                "                      ggggg,\n"
6112                "                      hhhhhh,\n"
6113                "                      iiiiii,\n"
6114                "                      jjjjjj,\n"
6115                "                      kkkkkk};",
6116                NoBinPacking);
6117   verifyFormat("const Aaaaaa aaaaa = {\n"
6118                "    aaaaa,\n"
6119                "    bbbbb,\n"
6120                "    ccccc,\n"
6121                "    ddddd,\n"
6122                "    eeeee,\n"
6123                "    ffffff,\n"
6124                "    ggggg,\n"
6125                "    hhhhhh,\n"
6126                "    iiiiii,\n"
6127                "    jjjjjj,\n"
6128                "    kkkkkk,\n"
6129                "};",
6130                NoBinPacking);
6131   verifyFormat(
6132       "const Aaaaaa aaaaa = {\n"
6133       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
6134       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
6135       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
6136       "};",
6137       NoBinPacking);
6138 
6139   // FIXME: The alignment of these trailing comments might be bad. Then again,
6140   // this might be utterly useless in real code.
6141   verifyFormat("Constructor::Constructor()\n"
6142                "    : some_value{         //\n"
6143                "                 aaaaaaa, //\n"
6144                "                 bbbbbbb} {}");
6145 
6146   // In braced lists, the first comment is always assumed to belong to the
6147   // first element. Thus, it can be moved to the next or previous line as
6148   // appropriate.
6149   EXPECT_EQ("function({// First element:\n"
6150             "          1,\n"
6151             "          // Second element:\n"
6152             "          2});",
6153             format("function({\n"
6154                    "    // First element:\n"
6155                    "    1,\n"
6156                    "    // Second element:\n"
6157                    "    2});"));
6158   EXPECT_EQ("std::vector<int> MyNumbers{\n"
6159             "    // First element:\n"
6160             "    1,\n"
6161             "    // Second element:\n"
6162             "    2};",
6163             format("std::vector<int> MyNumbers{// First element:\n"
6164                    "                           1,\n"
6165                    "                           // Second element:\n"
6166                    "                           2};",
6167                    getLLVMStyleWithColumns(30)));
6168   // A trailing comma should still lead to an enforced line break.
6169   EXPECT_EQ("vector<int> SomeVector = {\n"
6170             "    // aaa\n"
6171             "    1, 2,\n"
6172             "};",
6173             format("vector<int> SomeVector = { // aaa\n"
6174                    "    1, 2, };"));
6175 
6176   FormatStyle ExtraSpaces = getLLVMStyle();
6177   ExtraSpaces.Cpp11BracedListStyle = false;
6178   ExtraSpaces.ColumnLimit = 75;
6179   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
6180   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
6181   verifyFormat("f({ 1, 2 });", ExtraSpaces);
6182   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
6183   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
6184   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
6185   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
6186   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
6187   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
6188   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
6189   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
6190   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
6191   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
6192   verifyFormat("class Class {\n"
6193                "  T member = { arg1, arg2 };\n"
6194                "};",
6195                ExtraSpaces);
6196   verifyFormat(
6197       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6198       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
6199       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6200       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
6201       ExtraSpaces);
6202   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
6203   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
6204                ExtraSpaces);
6205   verifyFormat(
6206       "someFunction(OtherParam,\n"
6207       "             BracedList{ // comment 1 (Forcing interesting break)\n"
6208       "                         param1, param2,\n"
6209       "                         // comment 2\n"
6210       "                         param3, param4 });",
6211       ExtraSpaces);
6212   verifyFormat(
6213       "std::this_thread::sleep_for(\n"
6214       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
6215       ExtraSpaces);
6216   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
6217                "    aaaaaaa,\n"
6218                "    aaaaaaaaaa,\n"
6219                "    aaaaa,\n"
6220                "    aaaaaaaaaaaaaaa,\n"
6221                "    aaa,\n"
6222                "    aaaaaaaaaa,\n"
6223                "    a,\n"
6224                "    aaaaaaaaaaaaaaaaaaaaa,\n"
6225                "    aaaaaaaaaaaa,\n"
6226                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
6227                "    aaaaaaa,\n"
6228                "    a};");
6229   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
6230 }
6231 
6232 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
6233   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6234                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6235                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6236                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6237                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6238                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6239   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
6240                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6241                "                 1, 22, 333, 4444, 55555, //\n"
6242                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6243                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6244   verifyFormat(
6245       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6246       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6247       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
6248       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6249       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6250       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6251       "                 7777777};");
6252   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6253                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6254                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6255   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6256                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6257                "    // Separating comment.\n"
6258                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
6259   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6260                "    // Leading comment\n"
6261                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6262                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6263   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6264                "                 1, 1, 1, 1};",
6265                getLLVMStyleWithColumns(39));
6266   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6267                "                 1, 1, 1, 1};",
6268                getLLVMStyleWithColumns(38));
6269   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
6270                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
6271                getLLVMStyleWithColumns(43));
6272   verifyFormat(
6273       "static unsigned SomeValues[10][3] = {\n"
6274       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
6275       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
6276   verifyFormat("static auto fields = new vector<string>{\n"
6277                "    \"aaaaaaaaaaaaa\",\n"
6278                "    \"aaaaaaaaaaaaa\",\n"
6279                "    \"aaaaaaaaaaaa\",\n"
6280                "    \"aaaaaaaaaaaaaa\",\n"
6281                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6282                "    \"aaaaaaaaaaaa\",\n"
6283                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6284                "};");
6285   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
6286   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
6287                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
6288                "                 3, cccccccccccccccccccccc};",
6289                getLLVMStyleWithColumns(60));
6290 
6291   // Trailing commas.
6292   verifyFormat("vector<int> x = {\n"
6293                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
6294                "};",
6295                getLLVMStyleWithColumns(39));
6296   verifyFormat("vector<int> x = {\n"
6297                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
6298                "};",
6299                getLLVMStyleWithColumns(39));
6300   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6301                "                 1, 1, 1, 1,\n"
6302                "                 /**/ /**/};",
6303                getLLVMStyleWithColumns(39));
6304 
6305   // Trailing comment in the first line.
6306   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
6307                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
6308                "    111111111,  222222222,  3333333333,  444444444,  //\n"
6309                "    11111111,   22222222,   333333333,   44444444};");
6310 
6311   // With nested lists, we should either format one item per line or all nested
6312   // lists one on line.
6313   // FIXME: For some nested lists, we can do better.
6314   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
6315                "        {aaaaaaaaaaaaaaaaaaa},\n"
6316                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
6317                "        {aaaaaaaaaaaaaaaaa}};",
6318                getLLVMStyleWithColumns(60));
6319   verifyFormat(
6320       "SomeStruct my_struct_array = {\n"
6321       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
6322       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
6323       "    {aaa, aaa},\n"
6324       "    {aaa, aaa},\n"
6325       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
6326       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6327       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
6328 
6329   // No column layout should be used here.
6330   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
6331                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
6332 
6333   verifyNoCrash("a<,");
6334 }
6335 
6336 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
6337   FormatStyle DoNotMerge = getLLVMStyle();
6338   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6339 
6340   verifyFormat("void f() { return 42; }");
6341   verifyFormat("void f() {\n"
6342                "  return 42;\n"
6343                "}",
6344                DoNotMerge);
6345   verifyFormat("void f() {\n"
6346                "  // Comment\n"
6347                "}");
6348   verifyFormat("{\n"
6349                "#error {\n"
6350                "  int a;\n"
6351                "}");
6352   verifyFormat("{\n"
6353                "  int a;\n"
6354                "#error {\n"
6355                "}");
6356   verifyFormat("void f() {} // comment");
6357   verifyFormat("void f() { int a; } // comment");
6358   verifyFormat("void f() {\n"
6359                "} // comment",
6360                DoNotMerge);
6361   verifyFormat("void f() {\n"
6362                "  int a;\n"
6363                "} // comment",
6364                DoNotMerge);
6365   verifyFormat("void f() {\n"
6366                "} // comment",
6367                getLLVMStyleWithColumns(15));
6368 
6369   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
6370   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
6371 
6372   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
6373   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
6374   verifyFormat("class C {\n"
6375                "  C()\n"
6376                "      : iiiiiiii(nullptr),\n"
6377                "        kkkkkkk(nullptr),\n"
6378                "        mmmmmmm(nullptr),\n"
6379                "        nnnnnnn(nullptr) {}\n"
6380                "};",
6381                getGoogleStyle());
6382 
6383   FormatStyle NoColumnLimit = getLLVMStyle();
6384   NoColumnLimit.ColumnLimit = 0;
6385   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
6386   EXPECT_EQ("class C {\n"
6387             "  A() : b(0) {}\n"
6388             "};",
6389             format("class C{A():b(0){}};", NoColumnLimit));
6390   EXPECT_EQ("A()\n"
6391             "    : b(0) {\n"
6392             "}",
6393             format("A()\n:b(0)\n{\n}", NoColumnLimit));
6394 
6395   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
6396   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
6397       FormatStyle::SFS_None;
6398   EXPECT_EQ("A()\n"
6399             "    : b(0) {\n"
6400             "}",
6401             format("A():b(0){}", DoNotMergeNoColumnLimit));
6402   EXPECT_EQ("A()\n"
6403             "    : b(0) {\n"
6404             "}",
6405             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
6406 
6407   verifyFormat("#define A          \\\n"
6408                "  void f() {       \\\n"
6409                "    int i;         \\\n"
6410                "  }",
6411                getLLVMStyleWithColumns(20));
6412   verifyFormat("#define A           \\\n"
6413                "  void f() { int i; }",
6414                getLLVMStyleWithColumns(21));
6415   verifyFormat("#define A            \\\n"
6416                "  void f() {         \\\n"
6417                "    int i;           \\\n"
6418                "  }                  \\\n"
6419                "  int j;",
6420                getLLVMStyleWithColumns(22));
6421   verifyFormat("#define A             \\\n"
6422                "  void f() { int i; } \\\n"
6423                "  int j;",
6424                getLLVMStyleWithColumns(23));
6425 }
6426 
6427 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
6428   FormatStyle MergeInlineOnly = getLLVMStyle();
6429   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
6430   verifyFormat("class C {\n"
6431                "  int f() { return 42; }\n"
6432                "};",
6433                MergeInlineOnly);
6434   verifyFormat("int f() {\n"
6435                "  return 42;\n"
6436                "}",
6437                MergeInlineOnly);
6438 }
6439 
6440 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
6441   // Elaborate type variable declarations.
6442   verifyFormat("struct foo a = {bar};\nint n;");
6443   verifyFormat("class foo a = {bar};\nint n;");
6444   verifyFormat("union foo a = {bar};\nint n;");
6445 
6446   // Elaborate types inside function definitions.
6447   verifyFormat("struct foo f() {}\nint n;");
6448   verifyFormat("class foo f() {}\nint n;");
6449   verifyFormat("union foo f() {}\nint n;");
6450 
6451   // Templates.
6452   verifyFormat("template <class X> void f() {}\nint n;");
6453   verifyFormat("template <struct X> void f() {}\nint n;");
6454   verifyFormat("template <union X> void f() {}\nint n;");
6455 
6456   // Actual definitions...
6457   verifyFormat("struct {\n} n;");
6458   verifyFormat(
6459       "template <template <class T, class Y>, class Z> class X {\n} n;");
6460   verifyFormat("union Z {\n  int n;\n} x;");
6461   verifyFormat("class MACRO Z {\n} n;");
6462   verifyFormat("class MACRO(X) Z {\n} n;");
6463   verifyFormat("class __attribute__(X) Z {\n} n;");
6464   verifyFormat("class __declspec(X) Z {\n} n;");
6465   verifyFormat("class A##B##C {\n} n;");
6466   verifyFormat("class alignas(16) Z {\n} n;");
6467   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
6468   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
6469 
6470   // Redefinition from nested context:
6471   verifyFormat("class A::B::C {\n} n;");
6472 
6473   // Template definitions.
6474   verifyFormat(
6475       "template <typename F>\n"
6476       "Matcher(const Matcher<F> &Other,\n"
6477       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
6478       "                             !is_same<F, T>::value>::type * = 0)\n"
6479       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
6480 
6481   // FIXME: This is still incorrectly handled at the formatter side.
6482   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
6483   verifyFormat("int i = SomeFunction(a<b, a> b);");
6484 
6485   // FIXME:
6486   // This now gets parsed incorrectly as class definition.
6487   // verifyFormat("class A<int> f() {\n}\nint n;");
6488 
6489   // Elaborate types where incorrectly parsing the structural element would
6490   // break the indent.
6491   verifyFormat("if (true)\n"
6492                "  class X x;\n"
6493                "else\n"
6494                "  f();\n");
6495 
6496   // This is simply incomplete. Formatting is not important, but must not crash.
6497   verifyFormat("class A:");
6498 }
6499 
6500 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
6501   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
6502             format("#error Leave     all         white!!!!! space* alone!\n"));
6503   EXPECT_EQ(
6504       "#warning Leave     all         white!!!!! space* alone!\n",
6505       format("#warning Leave     all         white!!!!! space* alone!\n"));
6506   EXPECT_EQ("#error 1", format("  #  error   1"));
6507   EXPECT_EQ("#warning 1", format("  #  warning 1"));
6508 }
6509 
6510 TEST_F(FormatTest, FormatHashIfExpressions) {
6511   verifyFormat("#if AAAA && BBBB");
6512   // FIXME: Come up with a better indentation for #elif.
6513   verifyFormat(
6514       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
6515       "    defined(BBBBBBBB)\n"
6516       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
6517       "    defined(BBBBBBBB)\n"
6518       "#endif",
6519       getLLVMStyleWithColumns(65));
6520 }
6521 
6522 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
6523   FormatStyle AllowsMergedIf = getGoogleStyle();
6524   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
6525   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
6526   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
6527   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
6528   EXPECT_EQ("if (true) return 42;",
6529             format("if (true)\nreturn 42;", AllowsMergedIf));
6530   FormatStyle ShortMergedIf = AllowsMergedIf;
6531   ShortMergedIf.ColumnLimit = 25;
6532   verifyFormat("#define A \\\n"
6533                "  if (true) return 42;",
6534                ShortMergedIf);
6535   verifyFormat("#define A \\\n"
6536                "  f();    \\\n"
6537                "  if (true)\n"
6538                "#define B",
6539                ShortMergedIf);
6540   verifyFormat("#define A \\\n"
6541                "  f();    \\\n"
6542                "  if (true)\n"
6543                "g();",
6544                ShortMergedIf);
6545   verifyFormat("{\n"
6546                "#ifdef A\n"
6547                "  // Comment\n"
6548                "  if (true) continue;\n"
6549                "#endif\n"
6550                "  // Comment\n"
6551                "  if (true) continue;\n"
6552                "}",
6553                ShortMergedIf);
6554   ShortMergedIf.ColumnLimit = 29;
6555   verifyFormat("#define A                   \\\n"
6556                "  if (aaaaaaaaaa) return 1; \\\n"
6557                "  return 2;",
6558                ShortMergedIf);
6559   ShortMergedIf.ColumnLimit = 28;
6560   verifyFormat("#define A         \\\n"
6561                "  if (aaaaaaaaaa) \\\n"
6562                "    return 1;     \\\n"
6563                "  return 2;",
6564                ShortMergedIf);
6565 }
6566 
6567 TEST_F(FormatTest, BlockCommentsInControlLoops) {
6568   verifyFormat("if (0) /* a comment in a strange place */ {\n"
6569                "  f();\n"
6570                "}");
6571   verifyFormat("if (0) /* a comment in a strange place */ {\n"
6572                "  f();\n"
6573                "} /* another comment */ else /* comment #3 */ {\n"
6574                "  g();\n"
6575                "}");
6576   verifyFormat("while (0) /* a comment in a strange place */ {\n"
6577                "  f();\n"
6578                "}");
6579   verifyFormat("for (;;) /* a comment in a strange place */ {\n"
6580                "  f();\n"
6581                "}");
6582   verifyFormat("do /* a comment in a strange place */ {\n"
6583                "  f();\n"
6584                "} /* another comment */ while (0);");
6585 }
6586 
6587 TEST_F(FormatTest, BlockComments) {
6588   EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
6589             format("/* *//* */  /* */\n/* *//* */  /* */"));
6590   EXPECT_EQ("/* */ a /* */ b;", format("  /* */  a/* */  b;"));
6591   EXPECT_EQ("#define A /*123*/ \\\n"
6592             "  b\n"
6593             "/* */\n"
6594             "someCall(\n"
6595             "    parameter);",
6596             format("#define A /*123*/ b\n"
6597                    "/* */\n"
6598                    "someCall(parameter);",
6599                    getLLVMStyleWithColumns(15)));
6600 
6601   EXPECT_EQ("#define A\n"
6602             "/* */ someCall(\n"
6603             "    parameter);",
6604             format("#define A\n"
6605                    "/* */someCall(parameter);",
6606                    getLLVMStyleWithColumns(15)));
6607   EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
6608   EXPECT_EQ("/*\n"
6609             "*\n"
6610             " * aaaaaa\n"
6611             " * aaaaaa\n"
6612             "*/",
6613             format("/*\n"
6614                    "*\n"
6615                    " * aaaaaa aaaaaa\n"
6616                    "*/",
6617                    getLLVMStyleWithColumns(10)));
6618   EXPECT_EQ("/*\n"
6619             "**\n"
6620             "* aaaaaa\n"
6621             "*aaaaaa\n"
6622             "*/",
6623             format("/*\n"
6624                    "**\n"
6625                    "* aaaaaa aaaaaa\n"
6626                    "*/",
6627                    getLLVMStyleWithColumns(10)));
6628 
6629   FormatStyle NoBinPacking = getLLVMStyle();
6630   NoBinPacking.BinPackParameters = false;
6631   EXPECT_EQ("someFunction(1, /* comment 1 */\n"
6632             "             2, /* comment 2 */\n"
6633             "             3, /* comment 3 */\n"
6634             "             aaaa,\n"
6635             "             bbbb);",
6636             format("someFunction (1,   /* comment 1 */\n"
6637                    "                2, /* comment 2 */  \n"
6638                    "               3,   /* comment 3 */\n"
6639                    "aaaa, bbbb );",
6640                    NoBinPacking));
6641   verifyFormat(
6642       "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6643       "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6644   EXPECT_EQ(
6645       "bool aaaaaaaaaaaaa = /* trailing comment */\n"
6646       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6647       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
6648       format(
6649           "bool       aaaaaaaaaaaaa =       /* trailing comment */\n"
6650           "    aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa    ||\n"
6651           "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa   || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
6652   EXPECT_EQ(
6653       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
6654       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   /* comment */\n"
6655       "int cccccccccccccccccccccccccccccc;       /* comment */\n",
6656       format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
6657              "int      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
6658              "int    cccccccccccccccccccccccccccccc;  /* comment */\n"));
6659 
6660   verifyFormat("void f(int * /* unused */) {}");
6661 
6662   EXPECT_EQ("/*\n"
6663             " **\n"
6664             " */",
6665             format("/*\n"
6666                    " **\n"
6667                    " */"));
6668   EXPECT_EQ("/*\n"
6669             " *q\n"
6670             " */",
6671             format("/*\n"
6672                    " *q\n"
6673                    " */"));
6674   EXPECT_EQ("/*\n"
6675             " * q\n"
6676             " */",
6677             format("/*\n"
6678                    " * q\n"
6679                    " */"));
6680   EXPECT_EQ("/*\n"
6681             " **/",
6682             format("/*\n"
6683                    " **/"));
6684   EXPECT_EQ("/*\n"
6685             " ***/",
6686             format("/*\n"
6687                    " ***/"));
6688 }
6689 
6690 TEST_F(FormatTest, BlockCommentsInMacros) {
6691   EXPECT_EQ("#define A          \\\n"
6692             "  {                \\\n"
6693             "    /* one line */ \\\n"
6694             "    someCall();",
6695             format("#define A {        \\\n"
6696                    "  /* one line */   \\\n"
6697                    "  someCall();",
6698                    getLLVMStyleWithColumns(20)));
6699   EXPECT_EQ("#define A          \\\n"
6700             "  {                \\\n"
6701             "    /* previous */ \\\n"
6702             "    /* one line */ \\\n"
6703             "    someCall();",
6704             format("#define A {        \\\n"
6705                    "  /* previous */   \\\n"
6706                    "  /* one line */   \\\n"
6707                    "  someCall();",
6708                    getLLVMStyleWithColumns(20)));
6709 }
6710 
6711 TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
6712   EXPECT_EQ("a = {\n"
6713             "    1111 /*    */\n"
6714             "};",
6715             format("a = {1111 /*    */\n"
6716                    "};",
6717                    getLLVMStyleWithColumns(15)));
6718   EXPECT_EQ("a = {\n"
6719             "    1111 /*      */\n"
6720             "};",
6721             format("a = {1111 /*      */\n"
6722                    "};",
6723                    getLLVMStyleWithColumns(15)));
6724 
6725   // FIXME: The formatting is still wrong here.
6726   EXPECT_EQ("a = {\n"
6727             "    1111 /*      a\n"
6728             "            */\n"
6729             "};",
6730             format("a = {1111 /*      a */\n"
6731                    "};",
6732                    getLLVMStyleWithColumns(15)));
6733 }
6734 
6735 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
6736   // FIXME: This is not what we want...
6737   verifyFormat("{\n"
6738                "// a"
6739                "// b");
6740 }
6741 
6742 TEST_F(FormatTest, FormatStarDependingOnContext) {
6743   verifyFormat("void f(int *a);");
6744   verifyFormat("void f() { f(fint * b); }");
6745   verifyFormat("class A {\n  void f(int *a);\n};");
6746   verifyFormat("class A {\n  int *a;\n};");
6747   verifyFormat("namespace a {\n"
6748                "namespace b {\n"
6749                "class A {\n"
6750                "  void f() {}\n"
6751                "  int *a;\n"
6752                "};\n"
6753                "}\n"
6754                "}");
6755 }
6756 
6757 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
6758   verifyFormat("while");
6759   verifyFormat("operator");
6760 }
6761 
6762 //===----------------------------------------------------------------------===//
6763 // Objective-C tests.
6764 //===----------------------------------------------------------------------===//
6765 
6766 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
6767   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
6768   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
6769             format("-(NSUInteger)indexOfObject:(id)anObject;"));
6770   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
6771   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
6772   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
6773             format("-(NSInteger)Method3:(id)anObject;"));
6774   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
6775             format("-(NSInteger)Method4:(id)anObject;"));
6776   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
6777             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
6778   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
6779             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
6780   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
6781             "forAllCells:(BOOL)flag;",
6782             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
6783                    "forAllCells:(BOOL)flag;"));
6784 
6785   // Very long objectiveC method declaration.
6786   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
6787                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
6788   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
6789                "                    inRange:(NSRange)range\n"
6790                "                   outRange:(NSRange)out_range\n"
6791                "                  outRange1:(NSRange)out_range1\n"
6792                "                  outRange2:(NSRange)out_range2\n"
6793                "                  outRange3:(NSRange)out_range3\n"
6794                "                  outRange4:(NSRange)out_range4\n"
6795                "                  outRange5:(NSRange)out_range5\n"
6796                "                  outRange6:(NSRange)out_range6\n"
6797                "                  outRange7:(NSRange)out_range7\n"
6798                "                  outRange8:(NSRange)out_range8\n"
6799                "                  outRange9:(NSRange)out_range9;");
6800 
6801   // When the function name has to be wrapped.
6802   FormatStyle Style = getLLVMStyle();
6803   Style.IndentWrappedFunctionNames = false;
6804   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
6805                "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
6806                "           anotherName:(NSString)bbbbbbbbbbbbbb {\n"
6807                "}",
6808                Style);
6809   Style.IndentWrappedFunctionNames = true;
6810   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
6811                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
6812                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
6813                "}",
6814                Style);
6815 
6816   verifyFormat("- (int)sum:(vector<int>)numbers;");
6817   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
6818   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
6819   // protocol lists (but not for template classes):
6820   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
6821 
6822   verifyFormat("- (int (*)())foo:(int (*)())f;");
6823   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
6824 
6825   // If there's no return type (very rare in practice!), LLVM and Google style
6826   // agree.
6827   verifyFormat("- foo;");
6828   verifyFormat("- foo:(int)f;");
6829   verifyGoogleFormat("- foo:(int)foo;");
6830 }
6831 
6832 TEST_F(FormatTest, FormatObjCInterface) {
6833   verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
6834                "@public\n"
6835                "  int field1;\n"
6836                "@protected\n"
6837                "  int field2;\n"
6838                "@private\n"
6839                "  int field3;\n"
6840                "@package\n"
6841                "  int field4;\n"
6842                "}\n"
6843                "+ (id)init;\n"
6844                "@end");
6845 
6846   verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
6847                      " @public\n"
6848                      "  int field1;\n"
6849                      " @protected\n"
6850                      "  int field2;\n"
6851                      " @private\n"
6852                      "  int field3;\n"
6853                      " @package\n"
6854                      "  int field4;\n"
6855                      "}\n"
6856                      "+ (id)init;\n"
6857                      "@end");
6858 
6859   verifyFormat("@interface /* wait for it */ Foo\n"
6860                "+ (id)init;\n"
6861                "// Look, a comment!\n"
6862                "- (int)answerWith:(int)i;\n"
6863                "@end");
6864 
6865   verifyFormat("@interface Foo\n"
6866                "@end\n"
6867                "@interface Bar\n"
6868                "@end");
6869 
6870   verifyFormat("@interface Foo : Bar\n"
6871                "+ (id)init;\n"
6872                "@end");
6873 
6874   verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
6875                "+ (id)init;\n"
6876                "@end");
6877 
6878   verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
6879                      "+ (id)init;\n"
6880                      "@end");
6881 
6882   verifyFormat("@interface Foo (HackStuff)\n"
6883                "+ (id)init;\n"
6884                "@end");
6885 
6886   verifyFormat("@interface Foo ()\n"
6887                "+ (id)init;\n"
6888                "@end");
6889 
6890   verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
6891                "+ (id)init;\n"
6892                "@end");
6893 
6894   verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
6895                      "+ (id)init;\n"
6896                      "@end");
6897 
6898   verifyFormat("@interface Foo {\n"
6899                "  int _i;\n"
6900                "}\n"
6901                "+ (id)init;\n"
6902                "@end");
6903 
6904   verifyFormat("@interface Foo : Bar {\n"
6905                "  int _i;\n"
6906                "}\n"
6907                "+ (id)init;\n"
6908                "@end");
6909 
6910   verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
6911                "  int _i;\n"
6912                "}\n"
6913                "+ (id)init;\n"
6914                "@end");
6915 
6916   verifyFormat("@interface Foo (HackStuff) {\n"
6917                "  int _i;\n"
6918                "}\n"
6919                "+ (id)init;\n"
6920                "@end");
6921 
6922   verifyFormat("@interface Foo () {\n"
6923                "  int _i;\n"
6924                "}\n"
6925                "+ (id)init;\n"
6926                "@end");
6927 
6928   verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
6929                "  int _i;\n"
6930                "}\n"
6931                "+ (id)init;\n"
6932                "@end");
6933 
6934   FormatStyle OnePerLine = getGoogleStyle();
6935   OnePerLine.BinPackParameters = false;
6936   verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n"
6937                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6938                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6939                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6940                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
6941                "}",
6942                OnePerLine);
6943 }
6944 
6945 TEST_F(FormatTest, FormatObjCImplementation) {
6946   verifyFormat("@implementation Foo : NSObject {\n"
6947                "@public\n"
6948                "  int field1;\n"
6949                "@protected\n"
6950                "  int field2;\n"
6951                "@private\n"
6952                "  int field3;\n"
6953                "@package\n"
6954                "  int field4;\n"
6955                "}\n"
6956                "+ (id)init {\n}\n"
6957                "@end");
6958 
6959   verifyGoogleFormat("@implementation Foo : NSObject {\n"
6960                      " @public\n"
6961                      "  int field1;\n"
6962                      " @protected\n"
6963                      "  int field2;\n"
6964                      " @private\n"
6965                      "  int field3;\n"
6966                      " @package\n"
6967                      "  int field4;\n"
6968                      "}\n"
6969                      "+ (id)init {\n}\n"
6970                      "@end");
6971 
6972   verifyFormat("@implementation Foo\n"
6973                "+ (id)init {\n"
6974                "  if (true)\n"
6975                "    return nil;\n"
6976                "}\n"
6977                "// Look, a comment!\n"
6978                "- (int)answerWith:(int)i {\n"
6979                "  return i;\n"
6980                "}\n"
6981                "+ (int)answerWith:(int)i {\n"
6982                "  return i;\n"
6983                "}\n"
6984                "@end");
6985 
6986   verifyFormat("@implementation Foo\n"
6987                "@end\n"
6988                "@implementation Bar\n"
6989                "@end");
6990 
6991   EXPECT_EQ("@implementation Foo : Bar\n"
6992             "+ (id)init {\n}\n"
6993             "- (void)foo {\n}\n"
6994             "@end",
6995             format("@implementation Foo : Bar\n"
6996                    "+(id)init{}\n"
6997                    "-(void)foo{}\n"
6998                    "@end"));
6999 
7000   verifyFormat("@implementation Foo {\n"
7001                "  int _i;\n"
7002                "}\n"
7003                "+ (id)init {\n}\n"
7004                "@end");
7005 
7006   verifyFormat("@implementation Foo : Bar {\n"
7007                "  int _i;\n"
7008                "}\n"
7009                "+ (id)init {\n}\n"
7010                "@end");
7011 
7012   verifyFormat("@implementation Foo (HackStuff)\n"
7013                "+ (id)init {\n}\n"
7014                "@end");
7015   verifyFormat("@implementation ObjcClass\n"
7016                "- (void)method;\n"
7017                "{}\n"
7018                "@end");
7019 }
7020 
7021 TEST_F(FormatTest, FormatObjCProtocol) {
7022   verifyFormat("@protocol Foo\n"
7023                "@property(weak) id delegate;\n"
7024                "- (NSUInteger)numberOfThings;\n"
7025                "@end");
7026 
7027   verifyFormat("@protocol MyProtocol <NSObject>\n"
7028                "- (NSUInteger)numberOfThings;\n"
7029                "@end");
7030 
7031   verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
7032                      "- (NSUInteger)numberOfThings;\n"
7033                      "@end");
7034 
7035   verifyFormat("@protocol Foo;\n"
7036                "@protocol Bar;\n");
7037 
7038   verifyFormat("@protocol Foo\n"
7039                "@end\n"
7040                "@protocol Bar\n"
7041                "@end");
7042 
7043   verifyFormat("@protocol myProtocol\n"
7044                "- (void)mandatoryWithInt:(int)i;\n"
7045                "@optional\n"
7046                "- (void)optional;\n"
7047                "@required\n"
7048                "- (void)required;\n"
7049                "@optional\n"
7050                "@property(assign) int madProp;\n"
7051                "@end\n");
7052 
7053   verifyFormat("@property(nonatomic, assign, readonly)\n"
7054                "    int *looooooooooooooooooooooooooooongNumber;\n"
7055                "@property(nonatomic, assign, readonly)\n"
7056                "    NSString *looooooooooooooooooooooooooooongName;");
7057 
7058   verifyFormat("@implementation PR18406\n"
7059                "}\n"
7060                "@end");
7061 }
7062 
7063 TEST_F(FormatTest, FormatObjCMethodDeclarations) {
7064   verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
7065                "                   rect:(NSRect)theRect\n"
7066                "               interval:(float)theInterval {\n"
7067                "}");
7068   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7069                "          longKeyword:(NSRect)theRect\n"
7070                "    evenLongerKeyword:(float)theInterval\n"
7071                "                error:(NSError **)theError {\n"
7072                "}");
7073   verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
7074                "                         y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
7075                "    NS_DESIGNATED_INITIALIZER;",
7076                getLLVMStyleWithColumns(60));
7077 }
7078 
7079 TEST_F(FormatTest, FormatObjCMethodExpr) {
7080   verifyFormat("[foo bar:baz];");
7081   verifyFormat("return [foo bar:baz];");
7082   verifyFormat("return (a)[foo bar:baz];");
7083   verifyFormat("f([foo bar:baz]);");
7084   verifyFormat("f(2, [foo bar:baz]);");
7085   verifyFormat("f(2, a ? b : c);");
7086   verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
7087 
7088   // Unary operators.
7089   verifyFormat("int a = +[foo bar:baz];");
7090   verifyFormat("int a = -[foo bar:baz];");
7091   verifyFormat("int a = ![foo bar:baz];");
7092   verifyFormat("int a = ~[foo bar:baz];");
7093   verifyFormat("int a = ++[foo bar:baz];");
7094   verifyFormat("int a = --[foo bar:baz];");
7095   verifyFormat("int a = sizeof [foo bar:baz];");
7096   verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
7097   verifyFormat("int a = &[foo bar:baz];");
7098   verifyFormat("int a = *[foo bar:baz];");
7099   // FIXME: Make casts work, without breaking f()[4].
7100   // verifyFormat("int a = (int)[foo bar:baz];");
7101   // verifyFormat("return (int)[foo bar:baz];");
7102   // verifyFormat("(void)[foo bar:baz];");
7103   verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
7104 
7105   // Binary operators.
7106   verifyFormat("[foo bar:baz], [foo bar:baz];");
7107   verifyFormat("[foo bar:baz] = [foo bar:baz];");
7108   verifyFormat("[foo bar:baz] *= [foo bar:baz];");
7109   verifyFormat("[foo bar:baz] /= [foo bar:baz];");
7110   verifyFormat("[foo bar:baz] %= [foo bar:baz];");
7111   verifyFormat("[foo bar:baz] += [foo bar:baz];");
7112   verifyFormat("[foo bar:baz] -= [foo bar:baz];");
7113   verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
7114   verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
7115   verifyFormat("[foo bar:baz] &= [foo bar:baz];");
7116   verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
7117   verifyFormat("[foo bar:baz] |= [foo bar:baz];");
7118   verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
7119   verifyFormat("[foo bar:baz] || [foo bar:baz];");
7120   verifyFormat("[foo bar:baz] && [foo bar:baz];");
7121   verifyFormat("[foo bar:baz] | [foo bar:baz];");
7122   verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
7123   verifyFormat("[foo bar:baz] & [foo bar:baz];");
7124   verifyFormat("[foo bar:baz] == [foo bar:baz];");
7125   verifyFormat("[foo bar:baz] != [foo bar:baz];");
7126   verifyFormat("[foo bar:baz] >= [foo bar:baz];");
7127   verifyFormat("[foo bar:baz] <= [foo bar:baz];");
7128   verifyFormat("[foo bar:baz] > [foo bar:baz];");
7129   verifyFormat("[foo bar:baz] < [foo bar:baz];");
7130   verifyFormat("[foo bar:baz] >> [foo bar:baz];");
7131   verifyFormat("[foo bar:baz] << [foo bar:baz];");
7132   verifyFormat("[foo bar:baz] - [foo bar:baz];");
7133   verifyFormat("[foo bar:baz] + [foo bar:baz];");
7134   verifyFormat("[foo bar:baz] * [foo bar:baz];");
7135   verifyFormat("[foo bar:baz] / [foo bar:baz];");
7136   verifyFormat("[foo bar:baz] % [foo bar:baz];");
7137   // Whew!
7138 
7139   verifyFormat("return in[42];");
7140   verifyFormat("for (auto v : in[1]) {\n}");
7141   verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
7142   verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
7143   verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
7144   verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
7145   verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
7146   verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
7147                "}");
7148   verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
7149   verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
7150   verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
7151 
7152   verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
7153   verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
7154   verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
7155   verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
7156   verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
7157   verifyFormat("[button setAction:@selector(zoomOut:)];");
7158   verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
7159 
7160   verifyFormat("arr[[self indexForFoo:a]];");
7161   verifyFormat("throw [self errorFor:a];");
7162   verifyFormat("@throw [self errorFor:a];");
7163 
7164   verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
7165   verifyFormat("[(id)foo bar:(id) ? baz : quux];");
7166   verifyFormat("4 > 4 ? (id)a : (id)baz;");
7167 
7168   // This tests that the formatter doesn't break after "backing" but before ":",
7169   // which would be at 80 columns.
7170   verifyFormat(
7171       "void f() {\n"
7172       "  if ((self = [super initWithContentRect:contentRect\n"
7173       "                               styleMask:styleMask ?: otherMask\n"
7174       "                                 backing:NSBackingStoreBuffered\n"
7175       "                                   defer:YES]))");
7176 
7177   verifyFormat(
7178       "[foo checkThatBreakingAfterColonWorksOk:\n"
7179       "         [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
7180 
7181   verifyFormat("[myObj short:arg1 // Force line break\n"
7182                "          longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
7183                "    evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
7184                "                error:arg4];");
7185   verifyFormat(
7186       "void f() {\n"
7187       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
7188       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
7189       "                                     pos.width(), pos.height())\n"
7190       "                styleMask:NSBorderlessWindowMask\n"
7191       "                  backing:NSBackingStoreBuffered\n"
7192       "                    defer:NO]);\n"
7193       "}");
7194   verifyFormat(
7195       "void f() {\n"
7196       "  popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
7197       "      iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
7198       "                                 pos.width(), pos.height())\n"
7199       "                syeMask:NSBorderlessWindowMask\n"
7200       "                  bking:NSBackingStoreBuffered\n"
7201       "                    der:NO]);\n"
7202       "}",
7203       getLLVMStyleWithColumns(70));
7204   verifyFormat(
7205       "void f() {\n"
7206       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
7207       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
7208       "                                     pos.width(), pos.height())\n"
7209       "                styleMask:NSBorderlessWindowMask\n"
7210       "                  backing:NSBackingStoreBuffered\n"
7211       "                    defer:NO]);\n"
7212       "}",
7213       getChromiumStyle(FormatStyle::LK_Cpp));
7214   verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
7215                "                             with:contentsNativeView];");
7216 
7217   verifyFormat(
7218       "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
7219       "           owner:nillllll];");
7220 
7221   verifyFormat(
7222       "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
7223       "        forType:kBookmarkButtonDragType];");
7224 
7225   verifyFormat("[defaultCenter addObserver:self\n"
7226                "                  selector:@selector(willEnterFullscreen)\n"
7227                "                      name:kWillEnterFullscreenNotification\n"
7228                "                    object:nil];");
7229   verifyFormat("[image_rep drawInRect:drawRect\n"
7230                "             fromRect:NSZeroRect\n"
7231                "            operation:NSCompositeCopy\n"
7232                "             fraction:1.0\n"
7233                "       respectFlipped:NO\n"
7234                "                hints:nil];");
7235   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7236                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
7237   verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
7238                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
7239   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
7240                "    aaaaaaaaaaaaaaaaaaaaaa];");
7241   verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
7242                "        .aaaaaaaa];", // FIXME: Indentation seems off.
7243                getLLVMStyleWithColumns(60));
7244 
7245   verifyFormat(
7246       "scoped_nsobject<NSTextField> message(\n"
7247       "    // The frame will be fixed up when |-setMessageText:| is called.\n"
7248       "    [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
7249   verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
7250                "    aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
7251                "         aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
7252                "          aaaa:bbb];");
7253   verifyFormat("[self param:function( //\n"
7254                "                parameter)]");
7255   verifyFormat(
7256       "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7257       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7258       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
7259 
7260   // FIXME: This violates the column limit.
7261   verifyFormat(
7262       "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
7263       "    aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
7264       "                  aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];",
7265       getLLVMStyleWithColumns(60));
7266 
7267   // Variadic parameters.
7268   verifyFormat(
7269       "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
7270   verifyFormat(
7271       "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7272       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7273       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
7274   verifyFormat("[self // break\n"
7275                "      a:a\n"
7276                "    aaa:aaa];");
7277   verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
7278                "          [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
7279 }
7280 
7281 TEST_F(FormatTest, ObjCAt) {
7282   verifyFormat("@autoreleasepool");
7283   verifyFormat("@catch");
7284   verifyFormat("@class");
7285   verifyFormat("@compatibility_alias");
7286   verifyFormat("@defs");
7287   verifyFormat("@dynamic");
7288   verifyFormat("@encode");
7289   verifyFormat("@end");
7290   verifyFormat("@finally");
7291   verifyFormat("@implementation");
7292   verifyFormat("@import");
7293   verifyFormat("@interface");
7294   verifyFormat("@optional");
7295   verifyFormat("@package");
7296   verifyFormat("@private");
7297   verifyFormat("@property");
7298   verifyFormat("@protected");
7299   verifyFormat("@protocol");
7300   verifyFormat("@public");
7301   verifyFormat("@required");
7302   verifyFormat("@selector");
7303   verifyFormat("@synchronized");
7304   verifyFormat("@synthesize");
7305   verifyFormat("@throw");
7306   verifyFormat("@try");
7307 
7308   EXPECT_EQ("@interface", format("@ interface"));
7309 
7310   // The precise formatting of this doesn't matter, nobody writes code like
7311   // this.
7312   verifyFormat("@ /*foo*/ interface");
7313 }
7314 
7315 TEST_F(FormatTest, ObjCSnippets) {
7316   verifyFormat("@autoreleasepool {\n"
7317                "  foo();\n"
7318                "}");
7319   verifyFormat("@class Foo, Bar;");
7320   verifyFormat("@compatibility_alias AliasName ExistingClass;");
7321   verifyFormat("@dynamic textColor;");
7322   verifyFormat("char *buf1 = @encode(int *);");
7323   verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
7324   verifyFormat("char *buf1 = @encode(int **);");
7325   verifyFormat("Protocol *proto = @protocol(p1);");
7326   verifyFormat("SEL s = @selector(foo:);");
7327   verifyFormat("@synchronized(self) {\n"
7328                "  f();\n"
7329                "}");
7330 
7331   verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7332   verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7333 
7334   verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
7335   verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
7336   verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
7337   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7338                getMozillaStyle());
7339   verifyFormat("@property BOOL editable;", getMozillaStyle());
7340   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7341                getWebKitStyle());
7342   verifyFormat("@property BOOL editable;", getWebKitStyle());
7343 
7344   verifyFormat("@import foo.bar;\n"
7345                "@import baz;");
7346 }
7347 
7348 TEST_F(FormatTest, ObjCLiterals) {
7349   verifyFormat("@\"String\"");
7350   verifyFormat("@1");
7351   verifyFormat("@+4.8");
7352   verifyFormat("@-4");
7353   verifyFormat("@1LL");
7354   verifyFormat("@.5");
7355   verifyFormat("@'c'");
7356   verifyFormat("@true");
7357 
7358   verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
7359   verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
7360   verifyFormat("NSNumber *favoriteColor = @(Green);");
7361   verifyFormat("NSString *path = @(getenv(\"PATH\"));");
7362 
7363   verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
7364 }
7365 
7366 TEST_F(FormatTest, ObjCDictLiterals) {
7367   verifyFormat("@{");
7368   verifyFormat("@{}");
7369   verifyFormat("@{@\"one\" : @1}");
7370   verifyFormat("return @{@\"one\" : @1;");
7371   verifyFormat("@{@\"one\" : @1}");
7372 
7373   verifyFormat("@{@\"one\" : @{@2 : @1}}");
7374   verifyFormat("@{\n"
7375                "  @\"one\" : @{@2 : @1},\n"
7376                "}");
7377 
7378   verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
7379   verifyIncompleteFormat("[self setDict:@{}");
7380   verifyIncompleteFormat("[self setDict:@{@1 : @2}");
7381   verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
7382   verifyFormat(
7383       "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
7384   verifyFormat(
7385       "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
7386 
7387   verifyFormat("NSDictionary *d = @{\n"
7388                "  @\"nam\" : NSUserNam(),\n"
7389                "  @\"dte\" : [NSDate date],\n"
7390                "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
7391                "};");
7392   verifyFormat(
7393       "@{\n"
7394       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7395       "regularFont,\n"
7396       "};");
7397   verifyGoogleFormat(
7398       "@{\n"
7399       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7400       "regularFont,\n"
7401       "};");
7402   verifyFormat(
7403       "@{\n"
7404       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
7405       "      reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
7406       "};");
7407 
7408   // We should try to be robust in case someone forgets the "@".
7409   verifyFormat("NSDictionary *d = {\n"
7410                "  @\"nam\" : NSUserNam(),\n"
7411                "  @\"dte\" : [NSDate date],\n"
7412                "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
7413                "};");
7414   verifyFormat("NSMutableDictionary *dictionary =\n"
7415                "    [NSMutableDictionary dictionaryWithDictionary:@{\n"
7416                "      aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
7417                "      bbbbbbbbbbbbbbbbbb : bbbbb,\n"
7418                "      cccccccccccccccc : ccccccccccccccc\n"
7419                "    }];");
7420 }
7421 
7422 TEST_F(FormatTest, ObjCArrayLiterals) {
7423   verifyIncompleteFormat("@[");
7424   verifyFormat("@[]");
7425   verifyFormat(
7426       "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
7427   verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
7428   verifyFormat("NSArray *array = @[ [foo description] ];");
7429 
7430   verifyFormat(
7431       "NSArray *some_variable = @[\n"
7432       "  aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
7433       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7434       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7435       "  @\"aaaaaaaaaaaaaaaaa\"\n"
7436       "];");
7437   verifyFormat("NSArray *some_variable = @[\n"
7438                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7439                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7440                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7441                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7442                "];");
7443   verifyGoogleFormat("NSArray *some_variable = @[\n"
7444                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7445                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7446                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7447                      "  @\"aaaaaaaaaaaaaaaaa\"\n"
7448                      "];");
7449   verifyFormat("NSArray *array = @[\n"
7450                "  @\"a\",\n"
7451                "  @\"a\",\n" // Trailing comma -> one per line.
7452                "];");
7453 
7454   // We should try to be robust in case someone forgets the "@".
7455   verifyFormat("NSArray *some_variable = [\n"
7456                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7457                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7458                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7459                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7460                "];");
7461   verifyFormat(
7462       "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
7463       "                                             index:(NSUInteger)index\n"
7464       "                                nonDigitAttributes:\n"
7465       "                                    (NSDictionary *)noDigitAttributes;");
7466   verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
7467                "  NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
7468                "]];");
7469 }
7470 
7471 TEST_F(FormatTest, BreaksStringLiterals) {
7472   EXPECT_EQ("\"some text \"\n"
7473             "\"other\";",
7474             format("\"some text other\";", getLLVMStyleWithColumns(12)));
7475   EXPECT_EQ("\"some text \"\n"
7476             "\"other\";",
7477             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
7478   EXPECT_EQ(
7479       "#define A  \\\n"
7480       "  \"some \"  \\\n"
7481       "  \"text \"  \\\n"
7482       "  \"other\";",
7483       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
7484   EXPECT_EQ(
7485       "#define A  \\\n"
7486       "  \"so \"    \\\n"
7487       "  \"text \"  \\\n"
7488       "  \"other\";",
7489       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
7490 
7491   EXPECT_EQ("\"some text\"",
7492             format("\"some text\"", getLLVMStyleWithColumns(1)));
7493   EXPECT_EQ("\"some text\"",
7494             format("\"some text\"", getLLVMStyleWithColumns(11)));
7495   EXPECT_EQ("\"some \"\n"
7496             "\"text\"",
7497             format("\"some text\"", getLLVMStyleWithColumns(10)));
7498   EXPECT_EQ("\"some \"\n"
7499             "\"text\"",
7500             format("\"some text\"", getLLVMStyleWithColumns(7)));
7501   EXPECT_EQ("\"some\"\n"
7502             "\" tex\"\n"
7503             "\"t\"",
7504             format("\"some text\"", getLLVMStyleWithColumns(6)));
7505   EXPECT_EQ("\"some\"\n"
7506             "\" tex\"\n"
7507             "\" and\"",
7508             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
7509   EXPECT_EQ("\"some\"\n"
7510             "\"/tex\"\n"
7511             "\"/and\"",
7512             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
7513 
7514   EXPECT_EQ("variable =\n"
7515             "    \"long string \"\n"
7516             "    \"literal\";",
7517             format("variable = \"long string literal\";",
7518                    getLLVMStyleWithColumns(20)));
7519 
7520   EXPECT_EQ("variable = f(\n"
7521             "    \"long string \"\n"
7522             "    \"literal\",\n"
7523             "    short,\n"
7524             "    loooooooooooooooooooong);",
7525             format("variable = f(\"long string literal\", short, "
7526                    "loooooooooooooooooooong);",
7527                    getLLVMStyleWithColumns(20)));
7528 
7529   EXPECT_EQ(
7530       "f(g(\"long string \"\n"
7531       "    \"literal\"),\n"
7532       "  b);",
7533       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
7534   EXPECT_EQ("f(g(\"long string \"\n"
7535             "    \"literal\",\n"
7536             "    a),\n"
7537             "  b);",
7538             format("f(g(\"long string literal\", a), b);",
7539                    getLLVMStyleWithColumns(20)));
7540   EXPECT_EQ(
7541       "f(\"one two\".split(\n"
7542       "    variable));",
7543       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
7544   EXPECT_EQ("f(\"one two three four five six \"\n"
7545             "  \"seven\".split(\n"
7546             "      really_looooong_variable));",
7547             format("f(\"one two three four five six seven\"."
7548                    "split(really_looooong_variable));",
7549                    getLLVMStyleWithColumns(33)));
7550 
7551   EXPECT_EQ("f(\"some \"\n"
7552             "  \"text\",\n"
7553             "  other);",
7554             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
7555 
7556   // Only break as a last resort.
7557   verifyFormat(
7558       "aaaaaaaaaaaaaaaaaaaa(\n"
7559       "    aaaaaaaaaaaaaaaaaaaa,\n"
7560       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
7561 
7562   EXPECT_EQ("\"splitmea\"\n"
7563             "\"trandomp\"\n"
7564             "\"oint\"",
7565             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
7566 
7567   EXPECT_EQ("\"split/\"\n"
7568             "\"pathat/\"\n"
7569             "\"slashes\"",
7570             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7571 
7572   EXPECT_EQ("\"split/\"\n"
7573             "\"pathat/\"\n"
7574             "\"slashes\"",
7575             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7576   EXPECT_EQ("\"split at \"\n"
7577             "\"spaces/at/\"\n"
7578             "\"slashes.at.any$\"\n"
7579             "\"non-alphanumeric%\"\n"
7580             "\"1111111111characte\"\n"
7581             "\"rs\"",
7582             format("\"split at "
7583                    "spaces/at/"
7584                    "slashes.at."
7585                    "any$non-"
7586                    "alphanumeric%"
7587                    "1111111111characte"
7588                    "rs\"",
7589                    getLLVMStyleWithColumns(20)));
7590 
7591   // Verify that splitting the strings understands
7592   // Style::AlwaysBreakBeforeMultilineStrings.
7593   EXPECT_EQ(
7594       "aaaaaaaaaaaa(\n"
7595       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
7596       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
7597       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
7598              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7599              "aaaaaaaaaaaaaaaaaaaaaa\");",
7600              getGoogleStyle()));
7601   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7602             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
7603             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
7604                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7605                    "aaaaaaaaaaaaaaaaaaaaaa\";",
7606                    getGoogleStyle()));
7607   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7608             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7609             format("llvm::outs() << "
7610                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
7611                    "aaaaaaaaaaaaaaaaaaa\";"));
7612   EXPECT_EQ("ffff(\n"
7613             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7614             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7615             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
7616                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7617                    getGoogleStyle()));
7618 
7619   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
7620   AlignLeft.AlignEscapedNewlinesLeft = true;
7621   EXPECT_EQ("#define A \\\n"
7622             "  \"some \" \\\n"
7623             "  \"text \" \\\n"
7624             "  \"other\";",
7625             format("#define A \"some text other\";", AlignLeft));
7626 }
7627 
7628 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
7629   EXPECT_EQ(
7630       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7631       "(\n"
7632       "    \"x\t\");",
7633       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7634              "aaaaaaa("
7635              "\"x\t\");"));
7636 }
7637 
7638 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
7639   EXPECT_EQ(
7640       "u8\"utf8 string \"\n"
7641       "u8\"literal\";",
7642       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
7643   EXPECT_EQ(
7644       "u\"utf16 string \"\n"
7645       "u\"literal\";",
7646       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
7647   EXPECT_EQ(
7648       "U\"utf32 string \"\n"
7649       "U\"literal\";",
7650       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
7651   EXPECT_EQ("L\"wide string \"\n"
7652             "L\"literal\";",
7653             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
7654   EXPECT_EQ("@\"NSString \"\n"
7655             "@\"literal\";",
7656             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
7657 
7658   // This input makes clang-format try to split the incomplete unicode escape
7659   // sequence, which used to lead to a crasher.
7660   verifyNoCrash(
7661       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
7662       getLLVMStyleWithColumns(60));
7663 }
7664 
7665 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
7666   FormatStyle Style = getGoogleStyleWithColumns(15);
7667   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
7668   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
7669   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
7670   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
7671   EXPECT_EQ("u8R\"x(raw literal)x\";",
7672             format("u8R\"x(raw literal)x\";", Style));
7673 }
7674 
7675 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
7676   FormatStyle Style = getLLVMStyleWithColumns(20);
7677   EXPECT_EQ(
7678       "_T(\"aaaaaaaaaaaaaa\")\n"
7679       "_T(\"aaaaaaaaaaaaaa\")\n"
7680       "_T(\"aaaaaaaaaaaa\")",
7681       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
7682   EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
7683             "     _T(\"aaaaaa\"),\n"
7684             "  z);",
7685             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
7686 
7687   // FIXME: Handle embedded spaces in one iteration.
7688   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
7689   //            "_T(\"aaaaaaaaaaaaa\")\n"
7690   //            "_T(\"aaaaaaaaaaaaa\")\n"
7691   //            "_T(\"a\")",
7692   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7693   //                   getLLVMStyleWithColumns(20)));
7694   EXPECT_EQ(
7695       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7696       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
7697   EXPECT_EQ("f(\n"
7698             "#if !TEST\n"
7699             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7700             "#endif\n"
7701             "    );",
7702             format("f(\n"
7703                    "#if !TEST\n"
7704                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7705                    "#endif\n"
7706                    ");"));
7707   EXPECT_EQ("f(\n"
7708             "\n"
7709             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
7710             format("f(\n"
7711                    "\n"
7712                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
7713 }
7714 
7715 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
7716   EXPECT_EQ(
7717       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7718       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7719       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7720       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7721              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7722              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
7723 }
7724 
7725 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
7726   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
7727             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
7728   EXPECT_EQ("fffffffffff(g(R\"x(\n"
7729             "multiline raw string literal xxxxxxxxxxxxxx\n"
7730             ")x\",\n"
7731             "              a),\n"
7732             "            b);",
7733             format("fffffffffff(g(R\"x(\n"
7734                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7735                    ")x\", a), b);",
7736                    getGoogleStyleWithColumns(20)));
7737   EXPECT_EQ("fffffffffff(\n"
7738             "    g(R\"x(qqq\n"
7739             "multiline raw string literal xxxxxxxxxxxxxx\n"
7740             ")x\",\n"
7741             "      a),\n"
7742             "    b);",
7743             format("fffffffffff(g(R\"x(qqq\n"
7744                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7745                    ")x\", a), b);",
7746                    getGoogleStyleWithColumns(20)));
7747 
7748   EXPECT_EQ("fffffffffff(R\"x(\n"
7749             "multiline raw string literal xxxxxxxxxxxxxx\n"
7750             ")x\");",
7751             format("fffffffffff(R\"x(\n"
7752                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7753                    ")x\");",
7754                    getGoogleStyleWithColumns(20)));
7755   EXPECT_EQ("fffffffffff(R\"x(\n"
7756             "multiline raw string literal xxxxxxxxxxxxxx\n"
7757             ")x\" + bbbbbb);",
7758             format("fffffffffff(R\"x(\n"
7759                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7760                    ")x\" +   bbbbbb);",
7761                    getGoogleStyleWithColumns(20)));
7762   EXPECT_EQ("fffffffffff(\n"
7763             "    R\"x(\n"
7764             "multiline raw string literal xxxxxxxxxxxxxx\n"
7765             ")x\" +\n"
7766             "    bbbbbb);",
7767             format("fffffffffff(\n"
7768                    " R\"x(\n"
7769                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7770                    ")x\" + bbbbbb);",
7771                    getGoogleStyleWithColumns(20)));
7772 }
7773 
7774 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
7775   verifyFormat("string a = \"unterminated;");
7776   EXPECT_EQ("function(\"unterminated,\n"
7777             "         OtherParameter);",
7778             format("function(  \"unterminated,\n"
7779                    "    OtherParameter);"));
7780 }
7781 
7782 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
7783   FormatStyle Style = getLLVMStyle();
7784   Style.Standard = FormatStyle::LS_Cpp03;
7785   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
7786             format("#define x(_a) printf(\"foo\"_a);", Style));
7787 }
7788 
7789 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
7790 
7791 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
7792   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
7793             "             \"ddeeefff\");",
7794             format("someFunction(\"aaabbbcccdddeeefff\");",
7795                    getLLVMStyleWithColumns(25)));
7796   EXPECT_EQ("someFunction1234567890(\n"
7797             "    \"aaabbbcccdddeeefff\");",
7798             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7799                    getLLVMStyleWithColumns(26)));
7800   EXPECT_EQ("someFunction1234567890(\n"
7801             "    \"aaabbbcccdddeeeff\"\n"
7802             "    \"f\");",
7803             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7804                    getLLVMStyleWithColumns(25)));
7805   EXPECT_EQ("someFunction1234567890(\n"
7806             "    \"aaabbbcccdddeeeff\"\n"
7807             "    \"f\");",
7808             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7809                    getLLVMStyleWithColumns(24)));
7810   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7811             "             \"ddde \"\n"
7812             "             \"efff\");",
7813             format("someFunction(\"aaabbbcc ddde efff\");",
7814                    getLLVMStyleWithColumns(25)));
7815   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
7816             "             \"ddeeefff\");",
7817             format("someFunction(\"aaabbbccc ddeeefff\");",
7818                    getLLVMStyleWithColumns(25)));
7819   EXPECT_EQ("someFunction1234567890(\n"
7820             "    \"aaabb \"\n"
7821             "    \"cccdddeeefff\");",
7822             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
7823                    getLLVMStyleWithColumns(25)));
7824   EXPECT_EQ("#define A          \\\n"
7825             "  string s =       \\\n"
7826             "      \"123456789\"  \\\n"
7827             "      \"0\";         \\\n"
7828             "  int i;",
7829             format("#define A string s = \"1234567890\"; int i;",
7830                    getLLVMStyleWithColumns(20)));
7831   // FIXME: Put additional penalties on breaking at non-whitespace locations.
7832   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7833             "             \"dddeeeff\"\n"
7834             "             \"f\");",
7835             format("someFunction(\"aaabbbcc dddeeefff\");",
7836                    getLLVMStyleWithColumns(25)));
7837 }
7838 
7839 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
7840   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
7841   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
7842   EXPECT_EQ("\"test\"\n"
7843             "\"\\n\"",
7844             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
7845   EXPECT_EQ("\"tes\\\\\"\n"
7846             "\"n\"",
7847             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
7848   EXPECT_EQ("\"\\\\\\\\\"\n"
7849             "\"\\n\"",
7850             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
7851   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
7852   EXPECT_EQ("\"\\uff01\"\n"
7853             "\"test\"",
7854             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
7855   EXPECT_EQ("\"\\Uff01ff02\"",
7856             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
7857   EXPECT_EQ("\"\\x000000000001\"\n"
7858             "\"next\"",
7859             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
7860   EXPECT_EQ("\"\\x000000000001next\"",
7861             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
7862   EXPECT_EQ("\"\\x000000000001\"",
7863             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
7864   EXPECT_EQ("\"test\"\n"
7865             "\"\\000000\"\n"
7866             "\"000001\"",
7867             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
7868   EXPECT_EQ("\"test\\000\"\n"
7869             "\"00000000\"\n"
7870             "\"1\"",
7871             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
7872 }
7873 
7874 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
7875   verifyFormat("void f() {\n"
7876                "  return g() {}\n"
7877                "  void h() {}");
7878   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
7879                "g();\n"
7880                "}");
7881 }
7882 
7883 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
7884   verifyFormat(
7885       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
7886 }
7887 
7888 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
7889   verifyFormat("class X {\n"
7890                "  void f() {\n"
7891                "  }\n"
7892                "};",
7893                getLLVMStyleWithColumns(12));
7894 }
7895 
7896 TEST_F(FormatTest, ConfigurableIndentWidth) {
7897   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
7898   EightIndent.IndentWidth = 8;
7899   EightIndent.ContinuationIndentWidth = 8;
7900   verifyFormat("void f() {\n"
7901                "        someFunction();\n"
7902                "        if (true) {\n"
7903                "                f();\n"
7904                "        }\n"
7905                "}",
7906                EightIndent);
7907   verifyFormat("class X {\n"
7908                "        void f() {\n"
7909                "        }\n"
7910                "};",
7911                EightIndent);
7912   verifyFormat("int x[] = {\n"
7913                "        call(),\n"
7914                "        call()};",
7915                EightIndent);
7916 }
7917 
7918 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
7919   verifyFormat("double\n"
7920                "f();",
7921                getLLVMStyleWithColumns(8));
7922 }
7923 
7924 TEST_F(FormatTest, ConfigurableUseOfTab) {
7925   FormatStyle Tab = getLLVMStyleWithColumns(42);
7926   Tab.IndentWidth = 8;
7927   Tab.UseTab = FormatStyle::UT_Always;
7928   Tab.AlignEscapedNewlinesLeft = true;
7929 
7930   EXPECT_EQ("if (aaaaaaaa && // q\n"
7931             "    bb)\t\t// w\n"
7932             "\t;",
7933             format("if (aaaaaaaa &&// q\n"
7934                    "bb)// w\n"
7935                    ";",
7936                    Tab));
7937   EXPECT_EQ("if (aaa && bbb) // w\n"
7938             "\t;",
7939             format("if(aaa&&bbb)// w\n"
7940                    ";",
7941                    Tab));
7942 
7943   verifyFormat("class X {\n"
7944                "\tvoid f() {\n"
7945                "\t\tsomeFunction(parameter1,\n"
7946                "\t\t\t     parameter2);\n"
7947                "\t}\n"
7948                "};",
7949                Tab);
7950   verifyFormat("#define A                        \\\n"
7951                "\tvoid f() {               \\\n"
7952                "\t\tsomeFunction(    \\\n"
7953                "\t\t    parameter1,  \\\n"
7954                "\t\t    parameter2); \\\n"
7955                "\t}",
7956                Tab);
7957 
7958   Tab.TabWidth = 4;
7959   Tab.IndentWidth = 8;
7960   verifyFormat("class TabWidth4Indent8 {\n"
7961                "\t\tvoid f() {\n"
7962                "\t\t\t\tsomeFunction(parameter1,\n"
7963                "\t\t\t\t\t\t\t parameter2);\n"
7964                "\t\t}\n"
7965                "};",
7966                Tab);
7967 
7968   Tab.TabWidth = 4;
7969   Tab.IndentWidth = 4;
7970   verifyFormat("class TabWidth4Indent4 {\n"
7971                "\tvoid f() {\n"
7972                "\t\tsomeFunction(parameter1,\n"
7973                "\t\t\t\t\t parameter2);\n"
7974                "\t}\n"
7975                "};",
7976                Tab);
7977 
7978   Tab.TabWidth = 8;
7979   Tab.IndentWidth = 4;
7980   verifyFormat("class TabWidth8Indent4 {\n"
7981                "    void f() {\n"
7982                "\tsomeFunction(parameter1,\n"
7983                "\t\t     parameter2);\n"
7984                "    }\n"
7985                "};",
7986                Tab);
7987 
7988   Tab.TabWidth = 8;
7989   Tab.IndentWidth = 8;
7990   EXPECT_EQ("/*\n"
7991             "\t      a\t\tcomment\n"
7992             "\t      in multiple lines\n"
7993             "       */",
7994             format("   /*\t \t \n"
7995                    " \t \t a\t\tcomment\t \t\n"
7996                    " \t \t in multiple lines\t\n"
7997                    " \t  */",
7998                    Tab));
7999 
8000   Tab.UseTab = FormatStyle::UT_ForIndentation;
8001   verifyFormat("{\n"
8002                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8003                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8004                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8005                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8006                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8007                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8008                "};",
8009                Tab);
8010   verifyFormat("enum A {\n"
8011                "\ta1, // Force multiple lines\n"
8012                "\ta2,\n"
8013                "\ta3\n"
8014                "};",
8015                Tab);
8016   EXPECT_EQ("if (aaaaaaaa && // q\n"
8017             "    bb)         // w\n"
8018             "\t;",
8019             format("if (aaaaaaaa &&// q\n"
8020                    "bb)// w\n"
8021                    ";",
8022                    Tab));
8023   verifyFormat("class X {\n"
8024                "\tvoid f() {\n"
8025                "\t\tsomeFunction(parameter1,\n"
8026                "\t\t             parameter2);\n"
8027                "\t}\n"
8028                "};",
8029                Tab);
8030   verifyFormat("{\n"
8031                "\tQ({\n"
8032                "\t\tint a;\n"
8033                "\t\tsomeFunction(aaaaaaaa,\n"
8034                "\t\t             bbbbbbb);\n"
8035                "\t}, p);\n"
8036                "}",
8037                Tab);
8038   EXPECT_EQ("{\n"
8039             "\t/* aaaa\n"
8040             "\t   bbbb */\n"
8041             "}",
8042             format("{\n"
8043                    "/* aaaa\n"
8044                    "   bbbb */\n"
8045                    "}",
8046                    Tab));
8047   EXPECT_EQ("{\n"
8048             "\t/*\n"
8049             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8050             "\t  bbbbbbbbbbbbb\n"
8051             "\t*/\n"
8052             "}",
8053             format("{\n"
8054                    "/*\n"
8055                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8056                    "*/\n"
8057                    "}",
8058                    Tab));
8059   EXPECT_EQ("{\n"
8060             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8061             "\t// bbbbbbbbbbbbb\n"
8062             "}",
8063             format("{\n"
8064                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8065                    "}",
8066                    Tab));
8067   EXPECT_EQ("{\n"
8068             "\t/*\n"
8069             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8070             "\t  bbbbbbbbbbbbb\n"
8071             "\t*/\n"
8072             "}",
8073             format("{\n"
8074                    "\t/*\n"
8075                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8076                    "\t*/\n"
8077                    "}",
8078                    Tab));
8079   EXPECT_EQ("{\n"
8080             "\t/*\n"
8081             "\n"
8082             "\t*/\n"
8083             "}",
8084             format("{\n"
8085                    "\t/*\n"
8086                    "\n"
8087                    "\t*/\n"
8088                    "}",
8089                    Tab));
8090   EXPECT_EQ("{\n"
8091             "\t/*\n"
8092             " asdf\n"
8093             "\t*/\n"
8094             "}",
8095             format("{\n"
8096                    "\t/*\n"
8097                    " asdf\n"
8098                    "\t*/\n"
8099                    "}",
8100                    Tab));
8101 
8102   Tab.UseTab = FormatStyle::UT_Never;
8103   EXPECT_EQ("/*\n"
8104             "              a\t\tcomment\n"
8105             "              in multiple lines\n"
8106             "       */",
8107             format("   /*\t \t \n"
8108                    " \t \t a\t\tcomment\t \t\n"
8109                    " \t \t in multiple lines\t\n"
8110                    " \t  */",
8111                    Tab));
8112   EXPECT_EQ("/* some\n"
8113             "   comment */",
8114             format(" \t \t /* some\n"
8115                    " \t \t    comment */",
8116                    Tab));
8117   EXPECT_EQ("int a; /* some\n"
8118             "   comment */",
8119             format(" \t \t int a; /* some\n"
8120                    " \t \t    comment */",
8121                    Tab));
8122 
8123   EXPECT_EQ("int a; /* some\n"
8124             "comment */",
8125             format(" \t \t int\ta; /* some\n"
8126                    " \t \t    comment */",
8127                    Tab));
8128   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8129             "    comment */",
8130             format(" \t \t f(\"\t\t\"); /* some\n"
8131                    " \t \t    comment */",
8132                    Tab));
8133   EXPECT_EQ("{\n"
8134             "  /*\n"
8135             "   * Comment\n"
8136             "   */\n"
8137             "  int i;\n"
8138             "}",
8139             format("{\n"
8140                    "\t/*\n"
8141                    "\t * Comment\n"
8142                    "\t */\n"
8143                    "\t int i;\n"
8144                    "}"));
8145 }
8146 
8147 TEST_F(FormatTest, CalculatesOriginalColumn) {
8148   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8149             "q\"; /* some\n"
8150             "       comment */",
8151             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8152                    "q\"; /* some\n"
8153                    "       comment */",
8154                    getLLVMStyle()));
8155   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8156             "/* some\n"
8157             "   comment */",
8158             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8159                    " /* some\n"
8160                    "    comment */",
8161                    getLLVMStyle()));
8162   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8163             "qqq\n"
8164             "/* some\n"
8165             "   comment */",
8166             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8167                    "qqq\n"
8168                    " /* some\n"
8169                    "    comment */",
8170                    getLLVMStyle()));
8171   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8172             "wwww; /* some\n"
8173             "         comment */",
8174             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8175                    "wwww; /* some\n"
8176                    "         comment */",
8177                    getLLVMStyle()));
8178 }
8179 
8180 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
8181   FormatStyle NoSpace = getLLVMStyle();
8182   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
8183 
8184   verifyFormat("while(true)\n"
8185                "  continue;",
8186                NoSpace);
8187   verifyFormat("for(;;)\n"
8188                "  continue;",
8189                NoSpace);
8190   verifyFormat("if(true)\n"
8191                "  f();\n"
8192                "else if(true)\n"
8193                "  f();",
8194                NoSpace);
8195   verifyFormat("do {\n"
8196                "  do_something();\n"
8197                "} while(something());",
8198                NoSpace);
8199   verifyFormat("switch(x) {\n"
8200                "default:\n"
8201                "  break;\n"
8202                "}",
8203                NoSpace);
8204   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
8205   verifyFormat("size_t x = sizeof(x);", NoSpace);
8206   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
8207   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
8208   verifyFormat("alignas(128) char a[128];", NoSpace);
8209   verifyFormat("size_t x = alignof(MyType);", NoSpace);
8210   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
8211   verifyFormat("int f() throw(Deprecated);", NoSpace);
8212   verifyFormat("typedef void (*cb)(int);", NoSpace);
8213 
8214   FormatStyle Space = getLLVMStyle();
8215   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
8216 
8217   verifyFormat("int f ();", Space);
8218   verifyFormat("void f (int a, T b) {\n"
8219                "  while (true)\n"
8220                "    continue;\n"
8221                "}",
8222                Space);
8223   verifyFormat("if (true)\n"
8224                "  f ();\n"
8225                "else if (true)\n"
8226                "  f ();",
8227                Space);
8228   verifyFormat("do {\n"
8229                "  do_something ();\n"
8230                "} while (something ());",
8231                Space);
8232   verifyFormat("switch (x) {\n"
8233                "default:\n"
8234                "  break;\n"
8235                "}",
8236                Space);
8237   verifyFormat("A::A () : a (1) {}", Space);
8238   verifyFormat("void f () __attribute__ ((asdf));", Space);
8239   verifyFormat("*(&a + 1);\n"
8240                "&((&a)[1]);\n"
8241                "a[(b + c) * d];\n"
8242                "(((a + 1) * 2) + 3) * 4;",
8243                Space);
8244   verifyFormat("#define A(x) x", Space);
8245   verifyFormat("#define A (x) x", Space);
8246   verifyFormat("#if defined(x)\n"
8247                "#endif",
8248                Space);
8249   verifyFormat("auto i = std::make_unique<int> (5);", Space);
8250   verifyFormat("size_t x = sizeof (x);", Space);
8251   verifyFormat("auto f (int x) -> decltype (x);", Space);
8252   verifyFormat("int f (T x) noexcept (x.create ());", Space);
8253   verifyFormat("alignas (128) char a[128];", Space);
8254   verifyFormat("size_t x = alignof (MyType);", Space);
8255   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
8256   verifyFormat("int f () throw (Deprecated);", Space);
8257   verifyFormat("typedef void (*cb) (int);", Space);
8258 }
8259 
8260 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
8261   FormatStyle Spaces = getLLVMStyle();
8262 
8263   Spaces.SpacesInParentheses = true;
8264   verifyFormat("call( x, y, z );", Spaces);
8265   verifyFormat("call();", Spaces);
8266   verifyFormat("std::function<void( int, int )> callback;", Spaces);
8267   verifyFormat("while ( (bool)1 )\n"
8268                "  continue;",
8269                Spaces);
8270   verifyFormat("for ( ;; )\n"
8271                "  continue;",
8272                Spaces);
8273   verifyFormat("if ( true )\n"
8274                "  f();\n"
8275                "else if ( true )\n"
8276                "  f();",
8277                Spaces);
8278   verifyFormat("do {\n"
8279                "  do_something( (int)i );\n"
8280                "} while ( something() );",
8281                Spaces);
8282   verifyFormat("switch ( x ) {\n"
8283                "default:\n"
8284                "  break;\n"
8285                "}",
8286                Spaces);
8287 
8288   Spaces.SpacesInParentheses = false;
8289   Spaces.SpacesInCStyleCastParentheses = true;
8290   verifyFormat("Type *A = ( Type * )P;", Spaces);
8291   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
8292   verifyFormat("x = ( int32 )y;", Spaces);
8293   verifyFormat("int a = ( int )(2.0f);", Spaces);
8294   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
8295   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
8296   verifyFormat("#define x (( int )-1)", Spaces);
8297 
8298   // Run the first set of tests again with:
8299   Spaces.SpacesInParentheses = false, Spaces.SpaceInEmptyParentheses = true;
8300   Spaces.SpacesInCStyleCastParentheses = true;
8301   verifyFormat("call(x, y, z);", Spaces);
8302   verifyFormat("call( );", Spaces);
8303   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8304   verifyFormat("while (( bool )1)\n"
8305                "  continue;",
8306                Spaces);
8307   verifyFormat("for (;;)\n"
8308                "  continue;",
8309                Spaces);
8310   verifyFormat("if (true)\n"
8311                "  f( );\n"
8312                "else if (true)\n"
8313                "  f( );",
8314                Spaces);
8315   verifyFormat("do {\n"
8316                "  do_something(( int )i);\n"
8317                "} while (something( ));",
8318                Spaces);
8319   verifyFormat("switch (x) {\n"
8320                "default:\n"
8321                "  break;\n"
8322                "}",
8323                Spaces);
8324 
8325   // Run the first set of tests again with:
8326   Spaces.SpaceAfterCStyleCast = true;
8327   verifyFormat("call(x, y, z);", Spaces);
8328   verifyFormat("call( );", Spaces);
8329   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8330   verifyFormat("while (( bool ) 1)\n"
8331                "  continue;",
8332                Spaces);
8333   verifyFormat("for (;;)\n"
8334                "  continue;",
8335                Spaces);
8336   verifyFormat("if (true)\n"
8337                "  f( );\n"
8338                "else if (true)\n"
8339                "  f( );",
8340                Spaces);
8341   verifyFormat("do {\n"
8342                "  do_something(( int ) i);\n"
8343                "} while (something( ));",
8344                Spaces);
8345   verifyFormat("switch (x) {\n"
8346                "default:\n"
8347                "  break;\n"
8348                "}",
8349                Spaces);
8350 
8351   // Run subset of tests again with:
8352   Spaces.SpacesInCStyleCastParentheses = false;
8353   Spaces.SpaceAfterCStyleCast = true;
8354   verifyFormat("while ((bool) 1)\n"
8355                "  continue;",
8356                Spaces);
8357   verifyFormat("do {\n"
8358                "  do_something((int) i);\n"
8359                "} while (something( ));",
8360                Spaces);
8361 }
8362 
8363 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
8364   verifyFormat("int a[5];");
8365   verifyFormat("a[3] += 42;");
8366 
8367   FormatStyle Spaces = getLLVMStyle();
8368   Spaces.SpacesInSquareBrackets = true;
8369   // Lambdas unchanged.
8370   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
8371   verifyFormat("return [i, args...] {};", Spaces);
8372 
8373   // Not lambdas.
8374   verifyFormat("int a[ 5 ];", Spaces);
8375   verifyFormat("a[ 3 ] += 42;", Spaces);
8376   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
8377   verifyFormat("double &operator[](int i) { return 0; }\n"
8378                "int i;",
8379                Spaces);
8380   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
8381   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
8382   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
8383 }
8384 
8385 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
8386   verifyFormat("int a = 5;");
8387   verifyFormat("a += 42;");
8388   verifyFormat("a or_eq 8;");
8389 
8390   FormatStyle Spaces = getLLVMStyle();
8391   Spaces.SpaceBeforeAssignmentOperators = false;
8392   verifyFormat("int a= 5;", Spaces);
8393   verifyFormat("a+= 42;", Spaces);
8394   verifyFormat("a or_eq 8;", Spaces);
8395 }
8396 
8397 TEST_F(FormatTest, AlignConsecutiveAssignments) {
8398   FormatStyle Alignment = getLLVMStyle();
8399   Alignment.AlignConsecutiveAssignments = false;
8400   verifyFormat("int a = 5;\n"
8401                "int oneTwoThree = 123;",
8402                Alignment);
8403   verifyFormat("int a = 5;\n"
8404                "int oneTwoThree = 123;",
8405                Alignment);
8406 
8407   Alignment.AlignConsecutiveAssignments = true;
8408   verifyFormat("int a           = 5;\n"
8409                "int oneTwoThree = 123;",
8410                Alignment);
8411   verifyFormat("int a           = method();\n"
8412                "int oneTwoThree = 133;",
8413                Alignment);
8414   verifyFormat("a &= 5;\n"
8415                "bcd *= 5;\n"
8416                "ghtyf += 5;\n"
8417                "dvfvdb -= 5;\n"
8418                "a /= 5;\n"
8419                "vdsvsv %= 5;\n"
8420                "sfdbddfbdfbb ^= 5;\n"
8421                "dvsdsv |= 5;\n"
8422                "int dsvvdvsdvvv = 123;",
8423                Alignment);
8424   verifyFormat("int i = 1, j = 10;\n"
8425                "something = 2000;",
8426                Alignment);
8427   verifyFormat("something = 2000;\n"
8428                "int i = 1, j = 10;\n",
8429                Alignment);
8430   verifyFormat("something = 2000;\n"
8431                "another   = 911;\n"
8432                "int i = 1, j = 10;\n"
8433                "oneMore = 1;\n"
8434                "i       = 2;",
8435                Alignment);
8436   verifyFormat("int a   = 5;\n"
8437                "int one = 1;\n"
8438                "method();\n"
8439                "int oneTwoThree = 123;\n"
8440                "int oneTwo      = 12;",
8441                Alignment);
8442   verifyFormat("int oneTwoThree = 123; // comment\n"
8443                "int oneTwo      = 12;  // comment",
8444                Alignment);
8445   EXPECT_EQ("int a = 5;\n"
8446             "\n"
8447             "int oneTwoThree = 123;",
8448             format("int a       = 5;\n"
8449                    "\n"
8450                    "int oneTwoThree= 123;",
8451                    Alignment));
8452   EXPECT_EQ("int a   = 5;\n"
8453             "int one = 1;\n"
8454             "\n"
8455             "int oneTwoThree = 123;",
8456             format("int a = 5;\n"
8457                    "int one = 1;\n"
8458                    "\n"
8459                    "int oneTwoThree = 123;",
8460                    Alignment));
8461   EXPECT_EQ("int a   = 5;\n"
8462             "int one = 1;\n"
8463             "\n"
8464             "int oneTwoThree = 123;\n"
8465             "int oneTwo      = 12;",
8466             format("int a = 5;\n"
8467                    "int one = 1;\n"
8468                    "\n"
8469                    "int oneTwoThree = 123;\n"
8470                    "int oneTwo = 12;",
8471                    Alignment));
8472   Alignment.AlignEscapedNewlinesLeft = true;
8473   verifyFormat("#define A               \\\n"
8474                "  int aaaa       = 12;  \\\n"
8475                "  int b          = 23;  \\\n"
8476                "  int ccc        = 234; \\\n"
8477                "  int dddddddddd = 2345;",
8478                Alignment);
8479   Alignment.AlignEscapedNewlinesLeft = false;
8480   verifyFormat("#define A                                                      "
8481                "                \\\n"
8482                "  int aaaa       = 12;                                         "
8483                "                \\\n"
8484                "  int b          = 23;                                         "
8485                "                \\\n"
8486                "  int ccc        = 234;                                        "
8487                "                \\\n"
8488                "  int dddddddddd = 2345;",
8489                Alignment);
8490   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
8491                "k = 4, int l = 5,\n"
8492                "                  int m = 6) {\n"
8493                "  int j      = 10;\n"
8494                "  otherThing = 1;\n"
8495                "}",
8496                Alignment);
8497   verifyFormat("void SomeFunction(int parameter = 0) {\n"
8498                "  int i   = 1;\n"
8499                "  int j   = 2;\n"
8500                "  int big = 10000;\n"
8501                "}",
8502                Alignment);
8503   verifyFormat("class C {\n"
8504                "public:\n"
8505                "  int i = 1;\n"
8506                "  virtual void f() = 0;\n"
8507                "};",
8508                Alignment);
8509   verifyFormat("int i = 1;\n"
8510                "if (SomeType t = getSomething()) {\n"
8511                "}\n"
8512                "int j   = 2;\n"
8513                "int big = 10000;",
8514                Alignment);
8515   verifyFormat("int j = 7;\n"
8516                "for (int k = 0; k < N; ++k) {\n"
8517                "}\n"
8518                "int j   = 2;\n"
8519                "int big = 10000;\n"
8520                "}",
8521                Alignment);
8522   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8523   verifyFormat("int i = 1;\n"
8524                "LooooooooooongType loooooooooooooooooooooongVariable\n"
8525                "    = someLooooooooooooooooongFunction();\n"
8526                "int j = 2;",
8527                Alignment);
8528   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8529   verifyFormat("int i = 1;\n"
8530                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
8531                "    someLooooooooooooooooongFunction();\n"
8532                "int j = 2;",
8533                Alignment);
8534   // FIXME: Should align all three assignments
8535   verifyFormat(
8536       "int i      = 1;\n"
8537       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
8538       "                          loooooooooooooooooooooongParameterB);\n"
8539       "int j = 2;",
8540       Alignment);
8541 }
8542 
8543 TEST_F(FormatTest, LinuxBraceBreaking) {
8544   FormatStyle LinuxBraceStyle = getLLVMStyle();
8545   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
8546   verifyFormat("namespace a\n"
8547                "{\n"
8548                "class A\n"
8549                "{\n"
8550                "  void f()\n"
8551                "  {\n"
8552                "    if (true) {\n"
8553                "      a();\n"
8554                "      b();\n"
8555                "    }\n"
8556                "  }\n"
8557                "  void g() { return; }\n"
8558                "};\n"
8559                "struct B {\n"
8560                "  int x;\n"
8561                "};\n"
8562                "}\n",
8563                LinuxBraceStyle);
8564   verifyFormat("enum X {\n"
8565                "  Y = 0,\n"
8566                "}\n",
8567                LinuxBraceStyle);
8568   verifyFormat("struct S {\n"
8569                "  int Type;\n"
8570                "  union {\n"
8571                "    int x;\n"
8572                "    double y;\n"
8573                "  } Value;\n"
8574                "  class C\n"
8575                "  {\n"
8576                "    MyFavoriteType Value;\n"
8577                "  } Class;\n"
8578                "}\n",
8579                LinuxBraceStyle);
8580 }
8581 
8582 TEST_F(FormatTest, MozillaBraceBreaking) {
8583   FormatStyle MozillaBraceStyle = getLLVMStyle();
8584   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
8585   verifyFormat("namespace a {\n"
8586                "class A\n"
8587                "{\n"
8588                "  void f()\n"
8589                "  {\n"
8590                "    if (true) {\n"
8591                "      a();\n"
8592                "      b();\n"
8593                "    }\n"
8594                "  }\n"
8595                "  void g() { return; }\n"
8596                "};\n"
8597                "enum E\n"
8598                "{\n"
8599                "  A,\n"
8600                "  // foo\n"
8601                "  B,\n"
8602                "  C\n"
8603                "};\n"
8604                "struct B\n"
8605                "{\n"
8606                "  int x;\n"
8607                "};\n"
8608                "}\n",
8609                MozillaBraceStyle);
8610   verifyFormat("struct S\n"
8611                "{\n"
8612                "  int Type;\n"
8613                "  union\n"
8614                "  {\n"
8615                "    int x;\n"
8616                "    double y;\n"
8617                "  } Value;\n"
8618                "  class C\n"
8619                "  {\n"
8620                "    MyFavoriteType Value;\n"
8621                "  } Class;\n"
8622                "}\n",
8623                MozillaBraceStyle);
8624 }
8625 
8626 TEST_F(FormatTest, StroustrupBraceBreaking) {
8627   FormatStyle StroustrupBraceStyle = getLLVMStyle();
8628   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8629   verifyFormat("namespace a {\n"
8630                "class A {\n"
8631                "  void f()\n"
8632                "  {\n"
8633                "    if (true) {\n"
8634                "      a();\n"
8635                "      b();\n"
8636                "    }\n"
8637                "  }\n"
8638                "  void g() { return; }\n"
8639                "};\n"
8640                "struct B {\n"
8641                "  int x;\n"
8642                "};\n"
8643                "}\n",
8644                StroustrupBraceStyle);
8645 
8646   verifyFormat("void foo()\n"
8647                "{\n"
8648                "  if (a) {\n"
8649                "    a();\n"
8650                "  }\n"
8651                "  else {\n"
8652                "    b();\n"
8653                "  }\n"
8654                "}\n",
8655                StroustrupBraceStyle);
8656 
8657   verifyFormat("#ifdef _DEBUG\n"
8658                "int foo(int i = 0)\n"
8659                "#else\n"
8660                "int foo(int i = 5)\n"
8661                "#endif\n"
8662                "{\n"
8663                "  return i;\n"
8664                "}",
8665                StroustrupBraceStyle);
8666 
8667   verifyFormat("void foo() {}\n"
8668                "void bar()\n"
8669                "#ifdef _DEBUG\n"
8670                "{\n"
8671                "  foo();\n"
8672                "}\n"
8673                "#else\n"
8674                "{\n"
8675                "}\n"
8676                "#endif",
8677                StroustrupBraceStyle);
8678 
8679   verifyFormat("void foobar() { int i = 5; }\n"
8680                "#ifdef _DEBUG\n"
8681                "void bar() {}\n"
8682                "#else\n"
8683                "void bar() { foobar(); }\n"
8684                "#endif",
8685                StroustrupBraceStyle);
8686 }
8687 
8688 TEST_F(FormatTest, AllmanBraceBreaking) {
8689   FormatStyle AllmanBraceStyle = getLLVMStyle();
8690   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
8691   verifyFormat("namespace a\n"
8692                "{\n"
8693                "class A\n"
8694                "{\n"
8695                "  void f()\n"
8696                "  {\n"
8697                "    if (true)\n"
8698                "    {\n"
8699                "      a();\n"
8700                "      b();\n"
8701                "    }\n"
8702                "  }\n"
8703                "  void g() { return; }\n"
8704                "};\n"
8705                "struct B\n"
8706                "{\n"
8707                "  int x;\n"
8708                "};\n"
8709                "}",
8710                AllmanBraceStyle);
8711 
8712   verifyFormat("void f()\n"
8713                "{\n"
8714                "  if (true)\n"
8715                "  {\n"
8716                "    a();\n"
8717                "  }\n"
8718                "  else if (false)\n"
8719                "  {\n"
8720                "    b();\n"
8721                "  }\n"
8722                "  else\n"
8723                "  {\n"
8724                "    c();\n"
8725                "  }\n"
8726                "}\n",
8727                AllmanBraceStyle);
8728 
8729   verifyFormat("void f()\n"
8730                "{\n"
8731                "  for (int i = 0; i < 10; ++i)\n"
8732                "  {\n"
8733                "    a();\n"
8734                "  }\n"
8735                "  while (false)\n"
8736                "  {\n"
8737                "    b();\n"
8738                "  }\n"
8739                "  do\n"
8740                "  {\n"
8741                "    c();\n"
8742                "  } while (false)\n"
8743                "}\n",
8744                AllmanBraceStyle);
8745 
8746   verifyFormat("void f(int a)\n"
8747                "{\n"
8748                "  switch (a)\n"
8749                "  {\n"
8750                "  case 0:\n"
8751                "    break;\n"
8752                "  case 1:\n"
8753                "  {\n"
8754                "    break;\n"
8755                "  }\n"
8756                "  case 2:\n"
8757                "  {\n"
8758                "  }\n"
8759                "  break;\n"
8760                "  default:\n"
8761                "    break;\n"
8762                "  }\n"
8763                "}\n",
8764                AllmanBraceStyle);
8765 
8766   verifyFormat("enum X\n"
8767                "{\n"
8768                "  Y = 0,\n"
8769                "}\n",
8770                AllmanBraceStyle);
8771   verifyFormat("enum X\n"
8772                "{\n"
8773                "  Y = 0\n"
8774                "}\n",
8775                AllmanBraceStyle);
8776 
8777   verifyFormat("@interface BSApplicationController ()\n"
8778                "{\n"
8779                "@private\n"
8780                "  id _extraIvar;\n"
8781                "}\n"
8782                "@end\n",
8783                AllmanBraceStyle);
8784 
8785   verifyFormat("#ifdef _DEBUG\n"
8786                "int foo(int i = 0)\n"
8787                "#else\n"
8788                "int foo(int i = 5)\n"
8789                "#endif\n"
8790                "{\n"
8791                "  return i;\n"
8792                "}",
8793                AllmanBraceStyle);
8794 
8795   verifyFormat("void foo() {}\n"
8796                "void bar()\n"
8797                "#ifdef _DEBUG\n"
8798                "{\n"
8799                "  foo();\n"
8800                "}\n"
8801                "#else\n"
8802                "{\n"
8803                "}\n"
8804                "#endif",
8805                AllmanBraceStyle);
8806 
8807   verifyFormat("void foobar() { int i = 5; }\n"
8808                "#ifdef _DEBUG\n"
8809                "void bar() {}\n"
8810                "#else\n"
8811                "void bar() { foobar(); }\n"
8812                "#endif",
8813                AllmanBraceStyle);
8814 
8815   // This shouldn't affect ObjC blocks..
8816   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
8817                "  // ...\n"
8818                "  int i;\n"
8819                "}];",
8820                AllmanBraceStyle);
8821   verifyFormat("void (^block)(void) = ^{\n"
8822                "  // ...\n"
8823                "  int i;\n"
8824                "};",
8825                AllmanBraceStyle);
8826   // .. or dict literals.
8827   verifyFormat("void f()\n"
8828                "{\n"
8829                "  [object someMethod:@{ @\"a\" : @\"b\" }];\n"
8830                "}",
8831                AllmanBraceStyle);
8832   verifyFormat("int f()\n"
8833                "{ // comment\n"
8834                "  return 42;\n"
8835                "}",
8836                AllmanBraceStyle);
8837 
8838   AllmanBraceStyle.ColumnLimit = 19;
8839   verifyFormat("void f() { int i; }", AllmanBraceStyle);
8840   AllmanBraceStyle.ColumnLimit = 18;
8841   verifyFormat("void f()\n"
8842                "{\n"
8843                "  int i;\n"
8844                "}",
8845                AllmanBraceStyle);
8846   AllmanBraceStyle.ColumnLimit = 80;
8847 
8848   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
8849   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
8850   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
8851   verifyFormat("void f(bool b)\n"
8852                "{\n"
8853                "  if (b)\n"
8854                "  {\n"
8855                "    return;\n"
8856                "  }\n"
8857                "}\n",
8858                BreakBeforeBraceShortIfs);
8859   verifyFormat("void f(bool b)\n"
8860                "{\n"
8861                "  if (b) return;\n"
8862                "}\n",
8863                BreakBeforeBraceShortIfs);
8864   verifyFormat("void f(bool b)\n"
8865                "{\n"
8866                "  while (b)\n"
8867                "  {\n"
8868                "    return;\n"
8869                "  }\n"
8870                "}\n",
8871                BreakBeforeBraceShortIfs);
8872 }
8873 
8874 TEST_F(FormatTest, GNUBraceBreaking) {
8875   FormatStyle GNUBraceStyle = getLLVMStyle();
8876   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
8877   verifyFormat("namespace a\n"
8878                "{\n"
8879                "class A\n"
8880                "{\n"
8881                "  void f()\n"
8882                "  {\n"
8883                "    int a;\n"
8884                "    {\n"
8885                "      int b;\n"
8886                "    }\n"
8887                "    if (true)\n"
8888                "      {\n"
8889                "        a();\n"
8890                "        b();\n"
8891                "      }\n"
8892                "  }\n"
8893                "  void g() { return; }\n"
8894                "}\n"
8895                "}",
8896                GNUBraceStyle);
8897 
8898   verifyFormat("void f()\n"
8899                "{\n"
8900                "  if (true)\n"
8901                "    {\n"
8902                "      a();\n"
8903                "    }\n"
8904                "  else if (false)\n"
8905                "    {\n"
8906                "      b();\n"
8907                "    }\n"
8908                "  else\n"
8909                "    {\n"
8910                "      c();\n"
8911                "    }\n"
8912                "}\n",
8913                GNUBraceStyle);
8914 
8915   verifyFormat("void f()\n"
8916                "{\n"
8917                "  for (int i = 0; i < 10; ++i)\n"
8918                "    {\n"
8919                "      a();\n"
8920                "    }\n"
8921                "  while (false)\n"
8922                "    {\n"
8923                "      b();\n"
8924                "    }\n"
8925                "  do\n"
8926                "    {\n"
8927                "      c();\n"
8928                "    }\n"
8929                "  while (false);\n"
8930                "}\n",
8931                GNUBraceStyle);
8932 
8933   verifyFormat("void f(int a)\n"
8934                "{\n"
8935                "  switch (a)\n"
8936                "    {\n"
8937                "    case 0:\n"
8938                "      break;\n"
8939                "    case 1:\n"
8940                "      {\n"
8941                "        break;\n"
8942                "      }\n"
8943                "    case 2:\n"
8944                "      {\n"
8945                "      }\n"
8946                "      break;\n"
8947                "    default:\n"
8948                "      break;\n"
8949                "    }\n"
8950                "}\n",
8951                GNUBraceStyle);
8952 
8953   verifyFormat("enum X\n"
8954                "{\n"
8955                "  Y = 0,\n"
8956                "}\n",
8957                GNUBraceStyle);
8958 
8959   verifyFormat("@interface BSApplicationController ()\n"
8960                "{\n"
8961                "@private\n"
8962                "  id _extraIvar;\n"
8963                "}\n"
8964                "@end\n",
8965                GNUBraceStyle);
8966 
8967   verifyFormat("#ifdef _DEBUG\n"
8968                "int foo(int i = 0)\n"
8969                "#else\n"
8970                "int foo(int i = 5)\n"
8971                "#endif\n"
8972                "{\n"
8973                "  return i;\n"
8974                "}",
8975                GNUBraceStyle);
8976 
8977   verifyFormat("void foo() {}\n"
8978                "void bar()\n"
8979                "#ifdef _DEBUG\n"
8980                "{\n"
8981                "  foo();\n"
8982                "}\n"
8983                "#else\n"
8984                "{\n"
8985                "}\n"
8986                "#endif",
8987                GNUBraceStyle);
8988 
8989   verifyFormat("void foobar() { int i = 5; }\n"
8990                "#ifdef _DEBUG\n"
8991                "void bar() {}\n"
8992                "#else\n"
8993                "void bar() { foobar(); }\n"
8994                "#endif",
8995                GNUBraceStyle);
8996 }
8997 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
8998   verifyFormat("void f() {\n"
8999                "  try {\n"
9000                "  } catch (const Exception &e) {\n"
9001                "  }\n"
9002                "}\n",
9003                getLLVMStyle());
9004 }
9005 
9006 TEST_F(FormatTest, UnderstandsPragmas) {
9007   verifyFormat("#pragma omp reduction(| : var)");
9008   verifyFormat("#pragma omp reduction(+ : var)");
9009 
9010   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
9011             "(including parentheses).",
9012             format("#pragma    mark   Any non-hyphenated or hyphenated string "
9013                    "(including parentheses)."));
9014 }
9015 
9016 TEST_F(FormatTest, UnderstandPragmaOption) {
9017   verifyFormat("#pragma option -C -A");
9018 
9019   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
9020 }
9021 
9022 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
9023   for (size_t i = 1; i < Styles.size(); ++i)                                   \
9024   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
9025                                   << " differs from Style #0"
9026 
9027 TEST_F(FormatTest, GetsPredefinedStyleByName) {
9028   SmallVector<FormatStyle, 3> Styles;
9029   Styles.resize(3);
9030 
9031   Styles[0] = getLLVMStyle();
9032   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
9033   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
9034   EXPECT_ALL_STYLES_EQUAL(Styles);
9035 
9036   Styles[0] = getGoogleStyle();
9037   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
9038   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
9039   EXPECT_ALL_STYLES_EQUAL(Styles);
9040 
9041   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9042   EXPECT_TRUE(
9043       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
9044   EXPECT_TRUE(
9045       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
9046   EXPECT_ALL_STYLES_EQUAL(Styles);
9047 
9048   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
9049   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
9050   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
9051   EXPECT_ALL_STYLES_EQUAL(Styles);
9052 
9053   Styles[0] = getMozillaStyle();
9054   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
9055   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
9056   EXPECT_ALL_STYLES_EQUAL(Styles);
9057 
9058   Styles[0] = getWebKitStyle();
9059   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
9060   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
9061   EXPECT_ALL_STYLES_EQUAL(Styles);
9062 
9063   Styles[0] = getGNUStyle();
9064   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
9065   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
9066   EXPECT_ALL_STYLES_EQUAL(Styles);
9067 
9068   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
9069 }
9070 
9071 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
9072   SmallVector<FormatStyle, 8> Styles;
9073   Styles.resize(2);
9074 
9075   Styles[0] = getGoogleStyle();
9076   Styles[1] = getLLVMStyle();
9077   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9078   EXPECT_ALL_STYLES_EQUAL(Styles);
9079 
9080   Styles.resize(5);
9081   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9082   Styles[1] = getLLVMStyle();
9083   Styles[1].Language = FormatStyle::LK_JavaScript;
9084   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9085 
9086   Styles[2] = getLLVMStyle();
9087   Styles[2].Language = FormatStyle::LK_JavaScript;
9088   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
9089                                   "BasedOnStyle: Google",
9090                                   &Styles[2])
9091                    .value());
9092 
9093   Styles[3] = getLLVMStyle();
9094   Styles[3].Language = FormatStyle::LK_JavaScript;
9095   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
9096                                   "Language: JavaScript",
9097                                   &Styles[3])
9098                    .value());
9099 
9100   Styles[4] = getLLVMStyle();
9101   Styles[4].Language = FormatStyle::LK_JavaScript;
9102   EXPECT_EQ(0, parseConfiguration("---\n"
9103                                   "BasedOnStyle: LLVM\n"
9104                                   "IndentWidth: 123\n"
9105                                   "---\n"
9106                                   "BasedOnStyle: Google\n"
9107                                   "Language: JavaScript",
9108                                   &Styles[4])
9109                    .value());
9110   EXPECT_ALL_STYLES_EQUAL(Styles);
9111 }
9112 
9113 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
9114   Style.FIELD = false;                                                         \
9115   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
9116   EXPECT_TRUE(Style.FIELD);                                                    \
9117   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
9118   EXPECT_FALSE(Style.FIELD);
9119 
9120 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
9121 
9122 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
9123   EXPECT_NE(VALUE, Style.FIELD);                                               \
9124   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
9125   EXPECT_EQ(VALUE, Style.FIELD)
9126 
9127 TEST_F(FormatTest, ParsesConfigurationBools) {
9128   FormatStyle Style = {};
9129   Style.Language = FormatStyle::LK_Cpp;
9130   CHECK_PARSE_BOOL(AlignAfterOpenBracket);
9131   CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
9132   CHECK_PARSE_BOOL(AlignOperands);
9133   CHECK_PARSE_BOOL(AlignTrailingComments);
9134   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
9135   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
9136   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
9137   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
9138   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
9139   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
9140   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
9141   CHECK_PARSE_BOOL(BinPackParameters);
9142   CHECK_PARSE_BOOL(BinPackArguments);
9143   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
9144   CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
9145   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
9146   CHECK_PARSE_BOOL(DerivePointerAlignment);
9147   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
9148   CHECK_PARSE_BOOL(IndentCaseLabels);
9149   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
9150   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
9151   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
9152   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
9153   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
9154   CHECK_PARSE_BOOL(SpacesInParentheses);
9155   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
9156   CHECK_PARSE_BOOL(SpacesInAngles);
9157   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
9158   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
9159   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
9160   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
9161   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
9162 }
9163 
9164 #undef CHECK_PARSE_BOOL
9165 
9166 TEST_F(FormatTest, ParsesConfiguration) {
9167   FormatStyle Style = {};
9168   Style.Language = FormatStyle::LK_Cpp;
9169   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
9170   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
9171               ConstructorInitializerIndentWidth, 1234u);
9172   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
9173   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
9174   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
9175   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
9176               PenaltyBreakBeforeFirstCallParameter, 1234u);
9177   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
9178   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
9179               PenaltyReturnTypeOnItsOwnLine, 1234u);
9180   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
9181               SpacesBeforeTrailingComments, 1234u);
9182   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
9183   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
9184 
9185   Style.PointerAlignment = FormatStyle::PAS_Middle;
9186   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
9187               FormatStyle::PAS_Left);
9188   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
9189               FormatStyle::PAS_Right);
9190   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
9191               FormatStyle::PAS_Middle);
9192   // For backward compatibility:
9193   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
9194               FormatStyle::PAS_Left);
9195   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
9196               FormatStyle::PAS_Right);
9197   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
9198               FormatStyle::PAS_Middle);
9199 
9200   Style.Standard = FormatStyle::LS_Auto;
9201   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
9202   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
9203   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
9204   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
9205   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
9206 
9207   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9208   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
9209               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
9210   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
9211               FormatStyle::BOS_None);
9212   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
9213               FormatStyle::BOS_All);
9214   // For backward compatibility:
9215   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
9216               FormatStyle::BOS_None);
9217   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
9218               FormatStyle::BOS_All);
9219 
9220   Style.UseTab = FormatStyle::UT_ForIndentation;
9221   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
9222   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
9223   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
9224   // For backward compatibility:
9225   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
9226   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
9227 
9228   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
9229   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
9230               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9231   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
9232               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
9233   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
9234               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
9235   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
9236               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9237   // For backward compatibility:
9238   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
9239               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9240   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
9241               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9242 
9243   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
9244   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
9245               FormatStyle::SBPO_Never);
9246   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
9247               FormatStyle::SBPO_Always);
9248   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
9249               FormatStyle::SBPO_ControlStatements);
9250   // For backward compatibility:
9251   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
9252               FormatStyle::SBPO_Never);
9253   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
9254               FormatStyle::SBPO_ControlStatements);
9255 
9256   Style.ColumnLimit = 123;
9257   FormatStyle BaseStyle = getLLVMStyle();
9258   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
9259   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
9260 
9261   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9262   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
9263               FormatStyle::BS_Attach);
9264   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
9265               FormatStyle::BS_Linux);
9266   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
9267               FormatStyle::BS_Mozilla);
9268   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
9269               FormatStyle::BS_Stroustrup);
9270   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
9271               FormatStyle::BS_Allman);
9272   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
9273 
9274   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
9275   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
9276               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
9277   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
9278               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
9279   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
9280               AlwaysBreakAfterDefinitionReturnType,
9281               FormatStyle::DRTBS_TopLevel);
9282 
9283   Style.NamespaceIndentation = FormatStyle::NI_All;
9284   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
9285               FormatStyle::NI_None);
9286   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
9287               FormatStyle::NI_Inner);
9288   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
9289               FormatStyle::NI_All);
9290 
9291   Style.ForEachMacros.clear();
9292   std::vector<std::string> BoostForeach;
9293   BoostForeach.push_back("BOOST_FOREACH");
9294   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
9295   std::vector<std::string> BoostAndQForeach;
9296   BoostAndQForeach.push_back("BOOST_FOREACH");
9297   BoostAndQForeach.push_back("Q_FOREACH");
9298   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
9299               BoostAndQForeach);
9300 }
9301 
9302 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
9303   FormatStyle Style = {};
9304   Style.Language = FormatStyle::LK_Cpp;
9305   CHECK_PARSE("Language: Cpp\n"
9306               "IndentWidth: 12",
9307               IndentWidth, 12u);
9308   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
9309                                "IndentWidth: 34",
9310                                &Style),
9311             ParseError::Unsuitable);
9312   EXPECT_EQ(12u, Style.IndentWidth);
9313   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
9314   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9315 
9316   Style.Language = FormatStyle::LK_JavaScript;
9317   CHECK_PARSE("Language: JavaScript\n"
9318               "IndentWidth: 12",
9319               IndentWidth, 12u);
9320   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
9321   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
9322                                "IndentWidth: 34",
9323                                &Style),
9324             ParseError::Unsuitable);
9325   EXPECT_EQ(23u, Style.IndentWidth);
9326   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
9327   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9328 
9329   CHECK_PARSE("BasedOnStyle: LLVM\n"
9330               "IndentWidth: 67",
9331               IndentWidth, 67u);
9332 
9333   CHECK_PARSE("---\n"
9334               "Language: JavaScript\n"
9335               "IndentWidth: 12\n"
9336               "---\n"
9337               "Language: Cpp\n"
9338               "IndentWidth: 34\n"
9339               "...\n",
9340               IndentWidth, 12u);
9341 
9342   Style.Language = FormatStyle::LK_Cpp;
9343   CHECK_PARSE("---\n"
9344               "Language: JavaScript\n"
9345               "IndentWidth: 12\n"
9346               "---\n"
9347               "Language: Cpp\n"
9348               "IndentWidth: 34\n"
9349               "...\n",
9350               IndentWidth, 34u);
9351   CHECK_PARSE("---\n"
9352               "IndentWidth: 78\n"
9353               "---\n"
9354               "Language: JavaScript\n"
9355               "IndentWidth: 56\n"
9356               "...\n",
9357               IndentWidth, 78u);
9358 
9359   Style.ColumnLimit = 123;
9360   Style.IndentWidth = 234;
9361   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
9362   Style.TabWidth = 345;
9363   EXPECT_FALSE(parseConfiguration("---\n"
9364                                   "IndentWidth: 456\n"
9365                                   "BreakBeforeBraces: Allman\n"
9366                                   "---\n"
9367                                   "Language: JavaScript\n"
9368                                   "IndentWidth: 111\n"
9369                                   "TabWidth: 111\n"
9370                                   "---\n"
9371                                   "Language: Cpp\n"
9372                                   "BreakBeforeBraces: Stroustrup\n"
9373                                   "TabWidth: 789\n"
9374                                   "...\n",
9375                                   &Style));
9376   EXPECT_EQ(123u, Style.ColumnLimit);
9377   EXPECT_EQ(456u, Style.IndentWidth);
9378   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
9379   EXPECT_EQ(789u, Style.TabWidth);
9380 
9381   EXPECT_EQ(parseConfiguration("---\n"
9382                                "Language: JavaScript\n"
9383                                "IndentWidth: 56\n"
9384                                "---\n"
9385                                "IndentWidth: 78\n"
9386                                "...\n",
9387                                &Style),
9388             ParseError::Error);
9389   EXPECT_EQ(parseConfiguration("---\n"
9390                                "Language: JavaScript\n"
9391                                "IndentWidth: 56\n"
9392                                "---\n"
9393                                "Language: JavaScript\n"
9394                                "IndentWidth: 78\n"
9395                                "...\n",
9396                                &Style),
9397             ParseError::Error);
9398 
9399   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9400 }
9401 
9402 #undef CHECK_PARSE
9403 
9404 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
9405   FormatStyle Style = {};
9406   Style.Language = FormatStyle::LK_JavaScript;
9407   Style.BreakBeforeTernaryOperators = true;
9408   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
9409   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9410 
9411   Style.BreakBeforeTernaryOperators = true;
9412   EXPECT_EQ(0, parseConfiguration("---\n"
9413                                   "BasedOnStyle: Google\n"
9414                                   "---\n"
9415                                   "Language: JavaScript\n"
9416                                   "IndentWidth: 76\n"
9417                                   "...\n",
9418                                   &Style)
9419                    .value());
9420   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9421   EXPECT_EQ(76u, Style.IndentWidth);
9422   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9423 }
9424 
9425 TEST_F(FormatTest, ConfigurationRoundTripTest) {
9426   FormatStyle Style = getLLVMStyle();
9427   std::string YAML = configurationAsText(Style);
9428   FormatStyle ParsedStyle = {};
9429   ParsedStyle.Language = FormatStyle::LK_Cpp;
9430   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
9431   EXPECT_EQ(Style, ParsedStyle);
9432 }
9433 
9434 TEST_F(FormatTest, WorksFor8bitEncodings) {
9435   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
9436             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
9437             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
9438             "\"\xef\xee\xf0\xf3...\"",
9439             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
9440                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
9441                    "\xef\xee\xf0\xf3...\"",
9442                    getLLVMStyleWithColumns(12)));
9443 }
9444 
9445 TEST_F(FormatTest, HandlesUTF8BOM) {
9446   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
9447   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
9448             format("\xef\xbb\xbf#include <iostream>"));
9449   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
9450             format("\xef\xbb\xbf\n#include <iostream>"));
9451 }
9452 
9453 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
9454 #if !defined(_MSC_VER)
9455 
9456 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
9457   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
9458                getLLVMStyleWithColumns(35));
9459   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
9460                getLLVMStyleWithColumns(31));
9461   verifyFormat("// Однажды в студёную зимнюю пору...",
9462                getLLVMStyleWithColumns(36));
9463   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
9464   verifyFormat("/* Однажды в студёную зимнюю пору... */",
9465                getLLVMStyleWithColumns(39));
9466   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
9467                getLLVMStyleWithColumns(35));
9468 }
9469 
9470 TEST_F(FormatTest, SplitsUTF8Strings) {
9471   // Non-printable characters' width is currently considered to be the length in
9472   // bytes in UTF8. The characters can be displayed in very different manner
9473   // (zero-width, single width with a substitution glyph, expanded to their code
9474   // (e.g. "<8d>"), so there's no single correct way to handle them.
9475   EXPECT_EQ("\"aaaaÄ\"\n"
9476             "\"\xc2\x8d\";",
9477             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9478   EXPECT_EQ("\"aaaaaaaÄ\"\n"
9479             "\"\xc2\x8d\";",
9480             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9481   EXPECT_EQ("\"Однажды, в \"\n"
9482             "\"студёную \"\n"
9483             "\"зимнюю \"\n"
9484             "\"пору,\"",
9485             format("\"Однажды, в студёную зимнюю пору,\"",
9486                    getLLVMStyleWithColumns(13)));
9487   EXPECT_EQ(
9488       "\"一 二 三 \"\n"
9489       "\"四 五六 \"\n"
9490       "\"七 八 九 \"\n"
9491       "\"十\"",
9492       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
9493   EXPECT_EQ("\"一\t二 \"\n"
9494             "\"\t三 \"\n"
9495             "\"四 五\t六 \"\n"
9496             "\"\t七 \"\n"
9497             "\"八九十\tqq\"",
9498             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
9499                    getLLVMStyleWithColumns(11)));
9500 }
9501 
9502 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
9503   EXPECT_EQ("const char *sssss =\n"
9504             "    \"一二三四五六七八\\\n"
9505             " 九 十\";",
9506             format("const char *sssss = \"一二三四五六七八\\\n"
9507                    " 九 十\";",
9508                    getLLVMStyleWithColumns(30)));
9509 }
9510 
9511 TEST_F(FormatTest, SplitsUTF8LineComments) {
9512   EXPECT_EQ("// aaaaÄ\xc2\x8d",
9513             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
9514   EXPECT_EQ("// Я из лесу\n"
9515             "// вышел; был\n"
9516             "// сильный\n"
9517             "// мороз.",
9518             format("// Я из лесу вышел; был сильный мороз.",
9519                    getLLVMStyleWithColumns(13)));
9520   EXPECT_EQ("// 一二三\n"
9521             "// 四五六七\n"
9522             "// 八  九\n"
9523             "// 十",
9524             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
9525 }
9526 
9527 TEST_F(FormatTest, SplitsUTF8BlockComments) {
9528   EXPECT_EQ("/* Гляжу,\n"
9529             " * поднимается\n"
9530             " * медленно в\n"
9531             " * гору\n"
9532             " * Лошадка,\n"
9533             " * везущая\n"
9534             " * хворосту\n"
9535             " * воз. */",
9536             format("/* Гляжу, поднимается медленно в гору\n"
9537                    " * Лошадка, везущая хворосту воз. */",
9538                    getLLVMStyleWithColumns(13)));
9539   EXPECT_EQ(
9540       "/* 一二三\n"
9541       " * 四五六七\n"
9542       " * 八  九\n"
9543       " * 十  */",
9544       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
9545   EXPECT_EQ("/* �������� ��������\n"
9546             " * ��������\n"
9547             " * ������-�� */",
9548             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
9549 }
9550 
9551 #endif // _MSC_VER
9552 
9553 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
9554   FormatStyle Style = getLLVMStyle();
9555 
9556   Style.ConstructorInitializerIndentWidth = 4;
9557   verifyFormat(
9558       "SomeClass::Constructor()\n"
9559       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9560       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9561       Style);
9562 
9563   Style.ConstructorInitializerIndentWidth = 2;
9564   verifyFormat(
9565       "SomeClass::Constructor()\n"
9566       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9567       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9568       Style);
9569 
9570   Style.ConstructorInitializerIndentWidth = 0;
9571   verifyFormat(
9572       "SomeClass::Constructor()\n"
9573       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9574       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9575       Style);
9576 }
9577 
9578 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
9579   FormatStyle Style = getLLVMStyle();
9580   Style.BreakConstructorInitializersBeforeComma = true;
9581   Style.ConstructorInitializerIndentWidth = 4;
9582   verifyFormat("SomeClass::Constructor()\n"
9583                "    : a(a)\n"
9584                "    , b(b)\n"
9585                "    , c(c) {}",
9586                Style);
9587   verifyFormat("SomeClass::Constructor()\n"
9588                "    : a(a) {}",
9589                Style);
9590 
9591   Style.ColumnLimit = 0;
9592   verifyFormat("SomeClass::Constructor()\n"
9593                "    : a(a) {}",
9594                Style);
9595   verifyFormat("SomeClass::Constructor()\n"
9596                "    : a(a)\n"
9597                "    , b(b)\n"
9598                "    , c(c) {}",
9599                Style);
9600   verifyFormat("SomeClass::Constructor()\n"
9601                "    : a(a) {\n"
9602                "  foo();\n"
9603                "  bar();\n"
9604                "}",
9605                Style);
9606 
9607   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9608   verifyFormat("SomeClass::Constructor()\n"
9609                "    : a(a)\n"
9610                "    , b(b)\n"
9611                "    , c(c) {\n}",
9612                Style);
9613   verifyFormat("SomeClass::Constructor()\n"
9614                "    : a(a) {\n}",
9615                Style);
9616 
9617   Style.ColumnLimit = 80;
9618   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
9619   Style.ConstructorInitializerIndentWidth = 2;
9620   verifyFormat("SomeClass::Constructor()\n"
9621                "  : a(a)\n"
9622                "  , b(b)\n"
9623                "  , c(c) {}",
9624                Style);
9625 
9626   Style.ConstructorInitializerIndentWidth = 0;
9627   verifyFormat("SomeClass::Constructor()\n"
9628                ": a(a)\n"
9629                ", b(b)\n"
9630                ", c(c) {}",
9631                Style);
9632 
9633   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
9634   Style.ConstructorInitializerIndentWidth = 4;
9635   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
9636   verifyFormat(
9637       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
9638       Style);
9639   verifyFormat(
9640       "SomeClass::Constructor()\n"
9641       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
9642       Style);
9643   Style.ConstructorInitializerIndentWidth = 4;
9644   Style.ColumnLimit = 60;
9645   verifyFormat("SomeClass::Constructor()\n"
9646                "    : aaaaaaaa(aaaaaaaa)\n"
9647                "    , aaaaaaaa(aaaaaaaa)\n"
9648                "    , aaaaaaaa(aaaaaaaa) {}",
9649                Style);
9650 }
9651 
9652 TEST_F(FormatTest, Destructors) {
9653   verifyFormat("void F(int &i) { i.~int(); }");
9654   verifyFormat("void F(int &i) { i->~int(); }");
9655 }
9656 
9657 TEST_F(FormatTest, FormatsWithWebKitStyle) {
9658   FormatStyle Style = getWebKitStyle();
9659 
9660   // Don't indent in outer namespaces.
9661   verifyFormat("namespace outer {\n"
9662                "int i;\n"
9663                "namespace inner {\n"
9664                "    int i;\n"
9665                "} // namespace inner\n"
9666                "} // namespace outer\n"
9667                "namespace other_outer {\n"
9668                "int i;\n"
9669                "}",
9670                Style);
9671 
9672   // Don't indent case labels.
9673   verifyFormat("switch (variable) {\n"
9674                "case 1:\n"
9675                "case 2:\n"
9676                "    doSomething();\n"
9677                "    break;\n"
9678                "default:\n"
9679                "    ++variable;\n"
9680                "}",
9681                Style);
9682 
9683   // Wrap before binary operators.
9684   EXPECT_EQ("void f()\n"
9685             "{\n"
9686             "    if (aaaaaaaaaaaaaaaa\n"
9687             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
9688             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
9689             "        return;\n"
9690             "}",
9691             format("void f() {\n"
9692                    "if (aaaaaaaaaaaaaaaa\n"
9693                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
9694                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
9695                    "return;\n"
9696                    "}",
9697                    Style));
9698 
9699   // Allow functions on a single line.
9700   verifyFormat("void f() { return; }", Style);
9701 
9702   // Constructor initializers are formatted one per line with the "," on the
9703   // new line.
9704   verifyFormat("Constructor()\n"
9705                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9706                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
9707                "          aaaaaaaaaaaaaa)\n"
9708                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
9709                "{\n"
9710                "}",
9711                Style);
9712   verifyFormat("SomeClass::Constructor()\n"
9713                "    : a(a)\n"
9714                "{\n"
9715                "}",
9716                Style);
9717   EXPECT_EQ("SomeClass::Constructor()\n"
9718             "    : a(a)\n"
9719             "{\n"
9720             "}",
9721             format("SomeClass::Constructor():a(a){}", Style));
9722   verifyFormat("SomeClass::Constructor()\n"
9723                "    : a(a)\n"
9724                "    , b(b)\n"
9725                "    , c(c)\n"
9726                "{\n"
9727                "}",
9728                Style);
9729   verifyFormat("SomeClass::Constructor()\n"
9730                "    : a(a)\n"
9731                "{\n"
9732                "    foo();\n"
9733                "    bar();\n"
9734                "}",
9735                Style);
9736 
9737   // Access specifiers should be aligned left.
9738   verifyFormat("class C {\n"
9739                "public:\n"
9740                "    int i;\n"
9741                "};",
9742                Style);
9743 
9744   // Do not align comments.
9745   verifyFormat("int a; // Do not\n"
9746                "double b; // align comments.",
9747                Style);
9748 
9749   // Do not align operands.
9750   EXPECT_EQ("ASSERT(aaaa\n"
9751             "    || bbbb);",
9752             format("ASSERT ( aaaa\n||bbbb);", Style));
9753 
9754   // Accept input's line breaks.
9755   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
9756             "    || bbbbbbbbbbbbbbb) {\n"
9757             "    i++;\n"
9758             "}",
9759             format("if (aaaaaaaaaaaaaaa\n"
9760                    "|| bbbbbbbbbbbbbbb) { i++; }",
9761                    Style));
9762   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
9763             "    i++;\n"
9764             "}",
9765             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
9766 
9767   // Don't automatically break all macro definitions (llvm.org/PR17842).
9768   verifyFormat("#define aNumber 10", Style);
9769   // However, generally keep the line breaks that the user authored.
9770   EXPECT_EQ("#define aNumber \\\n"
9771             "    10",
9772             format("#define aNumber \\\n"
9773                    " 10",
9774                    Style));
9775 
9776   // Keep empty and one-element array literals on a single line.
9777   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
9778             "                                  copyItems:YES];",
9779             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
9780                    "copyItems:YES];",
9781                    Style));
9782   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
9783             "                                  copyItems:YES];",
9784             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
9785                    "             copyItems:YES];",
9786                    Style));
9787   // FIXME: This does not seem right, there should be more indentation before
9788   // the array literal's entries. Nested blocks have the same problem.
9789   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
9790             "    @\"a\",\n"
9791             "    @\"a\"\n"
9792             "]\n"
9793             "                                  copyItems:YES];",
9794             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
9795                    "     @\"a\",\n"
9796                    "     @\"a\"\n"
9797                    "     ]\n"
9798                    "       copyItems:YES];",
9799                    Style));
9800   EXPECT_EQ(
9801       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
9802       "                                  copyItems:YES];",
9803       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
9804              "   copyItems:YES];",
9805              Style));
9806 
9807   verifyFormat("[self.a b:c c:d];", Style);
9808   EXPECT_EQ("[self.a b:c\n"
9809             "        c:d];",
9810             format("[self.a b:c\n"
9811                    "c:d];",
9812                    Style));
9813 }
9814 
9815 TEST_F(FormatTest, FormatsLambdas) {
9816   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
9817   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
9818   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
9819   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
9820   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
9821   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
9822   verifyFormat("void f() {\n"
9823                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
9824                "}\n");
9825   verifyFormat("void f() {\n"
9826                "  other(x.begin(), //\n"
9827                "        x.end(),   //\n"
9828                "        [&](int, int) { return 1; });\n"
9829                "}\n");
9830   verifyFormat("SomeFunction([]() { // A cool function...\n"
9831                "  return 43;\n"
9832                "});");
9833   EXPECT_EQ("SomeFunction([]() {\n"
9834             "#define A a\n"
9835             "  return 43;\n"
9836             "});",
9837             format("SomeFunction([](){\n"
9838                    "#define A a\n"
9839                    "return 43;\n"
9840                    "});"));
9841   verifyFormat("void f() {\n"
9842                "  SomeFunction([](decltype(x), A *a) {});\n"
9843                "}");
9844   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9845                "    [](const aaaaaaaaaa &a) { return a; });");
9846   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
9847                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
9848                "});");
9849   verifyFormat("Constructor()\n"
9850                "    : Field([] { // comment\n"
9851                "        int i;\n"
9852                "      }) {}");
9853   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
9854                "  return some_parameter.size();\n"
9855                "};");
9856   verifyFormat("int i = aaaaaa ? 1 //\n"
9857                "               : [] {\n"
9858                "                   return 2; //\n"
9859                "                 }();");
9860   verifyFormat("llvm::errs() << \"number of twos is \"\n"
9861                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
9862                "                  return x == 2; // force break\n"
9863                "                });");
9864   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n"
9865                "    int iiiiiiiiiiii) {\n"
9866                "  return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n"
9867                "});",
9868                getLLVMStyleWithColumns(60));
9869   verifyFormat("SomeFunction({[&] {\n"
9870                "                // comment\n"
9871                "              },\n"
9872                "              [&] {\n"
9873                "                // comment\n"
9874                "              }});");
9875   verifyFormat("SomeFunction({[&] {\n"
9876                "  // comment\n"
9877                "}});");
9878 
9879   // Lambdas with return types.
9880   verifyFormat("int c = []() -> int { return 2; }();\n");
9881   verifyFormat("int c = []() -> int * { return 2; }();\n");
9882   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
9883   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
9884   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
9885   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
9886   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
9887   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
9888   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
9889                "                   int j) -> int {\n"
9890                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
9891                "};");
9892   verifyFormat(
9893       "aaaaaaaaaaaaaaaaaaaaaa(\n"
9894       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
9895       "      return aaaaaaaaaaaaaaaaa;\n"
9896       "    });",
9897       getLLVMStyleWithColumns(70));
9898 
9899   // Multiple lambdas in the same parentheses change indentation rules.
9900   verifyFormat("SomeFunction(\n"
9901                "    []() {\n"
9902                "      int i = 42;\n"
9903                "      return i;\n"
9904                "    },\n"
9905                "    []() {\n"
9906                "      int j = 43;\n"
9907                "      return j;\n"
9908                "    });");
9909 
9910   // More complex introducers.
9911   verifyFormat("return [i, args...] {};");
9912 
9913   // Not lambdas.
9914   verifyFormat("constexpr char hello[]{\"hello\"};");
9915   verifyFormat("double &operator[](int i) { return 0; }\n"
9916                "int i;");
9917   verifyFormat("std::unique_ptr<int[]> foo() {}");
9918   verifyFormat("int i = a[a][a]->f();");
9919   verifyFormat("int i = (*b)[a]->f();");
9920 
9921   // Other corner cases.
9922   verifyFormat("void f() {\n"
9923                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
9924                "      );\n"
9925                "}");
9926 
9927   // Lambdas created through weird macros.
9928   verifyFormat("void f() {\n"
9929                "  MACRO((const AA &a) { return 1; });\n"
9930                "}");
9931 
9932   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
9933                "      doo_dah();\n"
9934                "      doo_dah();\n"
9935                "    })) {\n"
9936                "}");
9937   verifyFormat("auto lambda = []() {\n"
9938                "  int a = 2\n"
9939                "#if A\n"
9940                "          + 2\n"
9941                "#endif\n"
9942                "      ;\n"
9943                "};");
9944 }
9945 
9946 TEST_F(FormatTest, FormatsBlocks) {
9947   FormatStyle ShortBlocks = getLLVMStyle();
9948   ShortBlocks.AllowShortBlocksOnASingleLine = true;
9949   verifyFormat("int (^Block)(int, int);", ShortBlocks);
9950   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
9951   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
9952   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
9953   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
9954   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
9955 
9956   verifyFormat("foo(^{ bar(); });", ShortBlocks);
9957   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
9958   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
9959 
9960   verifyFormat("[operation setCompletionBlock:^{\n"
9961                "  [self onOperationDone];\n"
9962                "}];");
9963   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
9964                "  [self onOperationDone];\n"
9965                "}]};");
9966   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
9967                "  f();\n"
9968                "}];");
9969   verifyFormat("int a = [operation block:^int(int *i) {\n"
9970                "  return 1;\n"
9971                "}];");
9972   verifyFormat("[myObject doSomethingWith:arg1\n"
9973                "                      aaa:^int(int *a) {\n"
9974                "                        return 1;\n"
9975                "                      }\n"
9976                "                      bbb:f(a * bbbbbbbb)];");
9977 
9978   verifyFormat("[operation setCompletionBlock:^{\n"
9979                "  [self.delegate newDataAvailable];\n"
9980                "}];",
9981                getLLVMStyleWithColumns(60));
9982   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
9983                "  NSString *path = [self sessionFilePath];\n"
9984                "  if (path) {\n"
9985                "    // ...\n"
9986                "  }\n"
9987                "});");
9988   verifyFormat("[[SessionService sharedService]\n"
9989                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
9990                "      if (window) {\n"
9991                "        [self windowDidLoad:window];\n"
9992                "      } else {\n"
9993                "        [self errorLoadingWindow];\n"
9994                "      }\n"
9995                "    }];");
9996   verifyFormat("void (^largeBlock)(void) = ^{\n"
9997                "  // ...\n"
9998                "};\n",
9999                getLLVMStyleWithColumns(40));
10000   verifyFormat("[[SessionService sharedService]\n"
10001                "    loadWindowWithCompletionBlock: //\n"
10002                "        ^(SessionWindow *window) {\n"
10003                "          if (window) {\n"
10004                "            [self windowDidLoad:window];\n"
10005                "          } else {\n"
10006                "            [self errorLoadingWindow];\n"
10007                "          }\n"
10008                "        }];",
10009                getLLVMStyleWithColumns(60));
10010   verifyFormat("[myObject doSomethingWith:arg1\n"
10011                "    firstBlock:^(Foo *a) {\n"
10012                "      // ...\n"
10013                "      int i;\n"
10014                "    }\n"
10015                "    secondBlock:^(Bar *b) {\n"
10016                "      // ...\n"
10017                "      int i;\n"
10018                "    }\n"
10019                "    thirdBlock:^Foo(Bar *b) {\n"
10020                "      // ...\n"
10021                "      int i;\n"
10022                "    }];");
10023   verifyFormat("[myObject doSomethingWith:arg1\n"
10024                "               firstBlock:-1\n"
10025                "              secondBlock:^(Bar *b) {\n"
10026                "                // ...\n"
10027                "                int i;\n"
10028                "              }];");
10029 
10030   verifyFormat("f(^{\n"
10031                "  @autoreleasepool {\n"
10032                "    if (a) {\n"
10033                "      g();\n"
10034                "    }\n"
10035                "  }\n"
10036                "});");
10037   verifyFormat("Block b = ^int *(A *a, B *b) {}");
10038 
10039   FormatStyle FourIndent = getLLVMStyle();
10040   FourIndent.ObjCBlockIndentWidth = 4;
10041   verifyFormat("[operation setCompletionBlock:^{\n"
10042                "    [self onOperationDone];\n"
10043                "}];",
10044                FourIndent);
10045 }
10046 
10047 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
10048   FormatStyle ZeroColumn = getLLVMStyle();
10049   ZeroColumn.ColumnLimit = 0;
10050 
10051   verifyFormat("[[SessionService sharedService] "
10052                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10053                "  if (window) {\n"
10054                "    [self windowDidLoad:window];\n"
10055                "  } else {\n"
10056                "    [self errorLoadingWindow];\n"
10057                "  }\n"
10058                "}];",
10059                ZeroColumn);
10060   EXPECT_EQ("[[SessionService sharedService]\n"
10061             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10062             "      if (window) {\n"
10063             "        [self windowDidLoad:window];\n"
10064             "      } else {\n"
10065             "        [self errorLoadingWindow];\n"
10066             "      }\n"
10067             "    }];",
10068             format("[[SessionService sharedService]\n"
10069                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10070                    "                if (window) {\n"
10071                    "    [self windowDidLoad:window];\n"
10072                    "  } else {\n"
10073                    "    [self errorLoadingWindow];\n"
10074                    "  }\n"
10075                    "}];",
10076                    ZeroColumn));
10077   verifyFormat("[myObject doSomethingWith:arg1\n"
10078                "    firstBlock:^(Foo *a) {\n"
10079                "      // ...\n"
10080                "      int i;\n"
10081                "    }\n"
10082                "    secondBlock:^(Bar *b) {\n"
10083                "      // ...\n"
10084                "      int i;\n"
10085                "    }\n"
10086                "    thirdBlock:^Foo(Bar *b) {\n"
10087                "      // ...\n"
10088                "      int i;\n"
10089                "    }];",
10090                ZeroColumn);
10091   verifyFormat("f(^{\n"
10092                "  @autoreleasepool {\n"
10093                "    if (a) {\n"
10094                "      g();\n"
10095                "    }\n"
10096                "  }\n"
10097                "});",
10098                ZeroColumn);
10099   verifyFormat("void (^largeBlock)(void) = ^{\n"
10100                "  // ...\n"
10101                "};",
10102                ZeroColumn);
10103 
10104   ZeroColumn.AllowShortBlocksOnASingleLine = true;
10105   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
10106             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
10107   ZeroColumn.AllowShortBlocksOnASingleLine = false;
10108   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
10109             "  int i;\n"
10110             "};",
10111             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
10112 }
10113 
10114 TEST_F(FormatTest, SupportsCRLF) {
10115   EXPECT_EQ("int a;\r\n"
10116             "int b;\r\n"
10117             "int c;\r\n",
10118             format("int a;\r\n"
10119                    "  int b;\r\n"
10120                    "    int c;\r\n",
10121                    getLLVMStyle()));
10122   EXPECT_EQ("int a;\r\n"
10123             "int b;\r\n"
10124             "int c;\r\n",
10125             format("int a;\r\n"
10126                    "  int b;\n"
10127                    "    int c;\r\n",
10128                    getLLVMStyle()));
10129   EXPECT_EQ("int a;\n"
10130             "int b;\n"
10131             "int c;\n",
10132             format("int a;\r\n"
10133                    "  int b;\n"
10134                    "    int c;\n",
10135                    getLLVMStyle()));
10136   EXPECT_EQ("\"aaaaaaa \"\r\n"
10137             "\"bbbbbbb\";\r\n",
10138             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
10139   EXPECT_EQ("#define A \\\r\n"
10140             "  b;      \\\r\n"
10141             "  c;      \\\r\n"
10142             "  d;\r\n",
10143             format("#define A \\\r\n"
10144                    "  b; \\\r\n"
10145                    "  c; d; \r\n",
10146                    getGoogleStyle()));
10147 
10148   EXPECT_EQ("/*\r\n"
10149             "multi line block comments\r\n"
10150             "should not introduce\r\n"
10151             "an extra carriage return\r\n"
10152             "*/\r\n",
10153             format("/*\r\n"
10154                    "multi line block comments\r\n"
10155                    "should not introduce\r\n"
10156                    "an extra carriage return\r\n"
10157                    "*/\r\n"));
10158 }
10159 
10160 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
10161   verifyFormat("MY_CLASS(C) {\n"
10162                "  int i;\n"
10163                "  int j;\n"
10164                "};");
10165 }
10166 
10167 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
10168   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
10169   TwoIndent.ContinuationIndentWidth = 2;
10170 
10171   EXPECT_EQ("int i =\n"
10172             "  longFunction(\n"
10173             "    arg);",
10174             format("int i = longFunction(arg);", TwoIndent));
10175 
10176   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
10177   SixIndent.ContinuationIndentWidth = 6;
10178 
10179   EXPECT_EQ("int i =\n"
10180             "      longFunction(\n"
10181             "            arg);",
10182             format("int i = longFunction(arg);", SixIndent));
10183 }
10184 
10185 TEST_F(FormatTest, SpacesInAngles) {
10186   FormatStyle Spaces = getLLVMStyle();
10187   Spaces.SpacesInAngles = true;
10188 
10189   verifyFormat("static_cast< int >(arg);", Spaces);
10190   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
10191   verifyFormat("f< int, float >();", Spaces);
10192   verifyFormat("template <> g() {}", Spaces);
10193   verifyFormat("template < std::vector< int > > f() {}", Spaces);
10194 
10195   Spaces.Standard = FormatStyle::LS_Cpp03;
10196   Spaces.SpacesInAngles = true;
10197   verifyFormat("A< A< int > >();", Spaces);
10198 
10199   Spaces.SpacesInAngles = false;
10200   verifyFormat("A<A<int> >();", Spaces);
10201 
10202   Spaces.Standard = FormatStyle::LS_Cpp11;
10203   Spaces.SpacesInAngles = true;
10204   verifyFormat("A< A< int > >();", Spaces);
10205 
10206   Spaces.SpacesInAngles = false;
10207   verifyFormat("A<A<int>>();", Spaces);
10208 }
10209 
10210 TEST_F(FormatTest, TripleAngleBrackets) {
10211   verifyFormat("f<<<1, 1>>>();");
10212   verifyFormat("f<<<1, 1, 1, s>>>();");
10213   verifyFormat("f<<<a, b, c, d>>>();");
10214   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
10215   verifyFormat("f<param><<<1, 1>>>();");
10216   verifyFormat("f<1><<<1, 1>>>();");
10217   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
10218   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10219                "aaaaaaaaaaa<<<\n    1, 1>>>();");
10220 }
10221 
10222 TEST_F(FormatTest, MergeLessLessAtEnd) {
10223   verifyFormat("<<");
10224   EXPECT_EQ("< < <", format("\\\n<<<"));
10225   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10226                "aaallvm::outs() <<");
10227   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10228                "aaaallvm::outs()\n    <<");
10229 }
10230 
10231 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
10232   std::string code = "#if A\n"
10233                      "#if B\n"
10234                      "a.\n"
10235                      "#endif\n"
10236                      "    a = 1;\n"
10237                      "#else\n"
10238                      "#endif\n"
10239                      "#if C\n"
10240                      "#else\n"
10241                      "#endif\n";
10242   EXPECT_EQ(code, format(code));
10243 }
10244 
10245 TEST_F(FormatTest, HandleConflictMarkers) {
10246   // Git/SVN conflict markers.
10247   EXPECT_EQ("int a;\n"
10248             "void f() {\n"
10249             "  callme(some(parameter1,\n"
10250             "<<<<<<< text by the vcs\n"
10251             "              parameter2),\n"
10252             "||||||| text by the vcs\n"
10253             "              parameter2),\n"
10254             "         parameter3,\n"
10255             "======= text by the vcs\n"
10256             "              parameter2, parameter3),\n"
10257             ">>>>>>> text by the vcs\n"
10258             "         otherparameter);\n",
10259             format("int a;\n"
10260                    "void f() {\n"
10261                    "  callme(some(parameter1,\n"
10262                    "<<<<<<< text by the vcs\n"
10263                    "  parameter2),\n"
10264                    "||||||| text by the vcs\n"
10265                    "  parameter2),\n"
10266                    "  parameter3,\n"
10267                    "======= text by the vcs\n"
10268                    "  parameter2,\n"
10269                    "  parameter3),\n"
10270                    ">>>>>>> text by the vcs\n"
10271                    "  otherparameter);\n"));
10272 
10273   // Perforce markers.
10274   EXPECT_EQ("void f() {\n"
10275             "  function(\n"
10276             ">>>> text by the vcs\n"
10277             "      parameter,\n"
10278             "==== text by the vcs\n"
10279             "      parameter,\n"
10280             "==== text by the vcs\n"
10281             "      parameter,\n"
10282             "<<<< text by the vcs\n"
10283             "      parameter);\n",
10284             format("void f() {\n"
10285                    "  function(\n"
10286                    ">>>> text by the vcs\n"
10287                    "  parameter,\n"
10288                    "==== text by the vcs\n"
10289                    "  parameter,\n"
10290                    "==== text by the vcs\n"
10291                    "  parameter,\n"
10292                    "<<<< text by the vcs\n"
10293                    "  parameter);\n"));
10294 
10295   EXPECT_EQ("<<<<<<<\n"
10296             "|||||||\n"
10297             "=======\n"
10298             ">>>>>>>",
10299             format("<<<<<<<\n"
10300                    "|||||||\n"
10301                    "=======\n"
10302                    ">>>>>>>"));
10303 
10304   EXPECT_EQ("<<<<<<<\n"
10305             "|||||||\n"
10306             "int i;\n"
10307             "=======\n"
10308             ">>>>>>>",
10309             format("<<<<<<<\n"
10310                    "|||||||\n"
10311                    "int i;\n"
10312                    "=======\n"
10313                    ">>>>>>>"));
10314 
10315   // FIXME: Handle parsing of macros around conflict markers correctly:
10316   EXPECT_EQ("#define Macro \\\n"
10317             "<<<<<<<\n"
10318             "Something \\\n"
10319             "|||||||\n"
10320             "Else \\\n"
10321             "=======\n"
10322             "Other \\\n"
10323             ">>>>>>>\n"
10324             "    End int i;\n",
10325             format("#define Macro \\\n"
10326                    "<<<<<<<\n"
10327                    "  Something \\\n"
10328                    "|||||||\n"
10329                    "  Else \\\n"
10330                    "=======\n"
10331                    "  Other \\\n"
10332                    ">>>>>>>\n"
10333                    "  End\n"
10334                    "int i;\n"));
10335 }
10336 
10337 TEST_F(FormatTest, DisableRegions) {
10338   EXPECT_EQ("int i;\n"
10339             "// clang-format off\n"
10340             "  int j;\n"
10341             "// clang-format on\n"
10342             "int k;",
10343             format(" int  i;\n"
10344                    "   // clang-format off\n"
10345                    "  int j;\n"
10346                    " // clang-format on\n"
10347                    "   int   k;"));
10348   EXPECT_EQ("int i;\n"
10349             "/* clang-format off */\n"
10350             "  int j;\n"
10351             "/* clang-format on */\n"
10352             "int k;",
10353             format(" int  i;\n"
10354                    "   /* clang-format off */\n"
10355                    "  int j;\n"
10356                    " /* clang-format on */\n"
10357                    "   int   k;"));
10358 }
10359 
10360 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
10361   format("? ) =");
10362   verifyNoCrash("#define a\\\n /**/}");
10363 }
10364 
10365 } // end namespace
10366 } // end namespace format
10367 } // end namespace clang
10368