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   verifyFormat("class C {\n"
4682                "  int\n"
4683                "  operator+() {\n"
4684                "    return 1;\n"
4685                "  }\n"
4686                "  int\n"
4687                "  operator()() {\n"
4688                "    return 1;\n"
4689                "  }\n"
4690                "};\n",
4691                Style);
4692   verifyFormat("void\n"
4693                "A::operator()() {}\n"
4694                "void\n"
4695                "A::operator>>() {}\n"
4696                "void\n"
4697                "A::operator+() {}\n",
4698                Style);
4699   verifyFormat("void *operator new(std::size_t s);", // No break here.
4700                Style);
4701   verifyFormat("void *\n"
4702                "operator new(std::size_t s) {}",
4703                Style);
4704   verifyFormat("void *\n"
4705                "operator delete[](void *ptr) {}",
4706                Style);
4707   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4708   verifyFormat("const char *\n"
4709                "f(void)\n" // Break here.
4710                "{\n"
4711                "  return \"\";\n"
4712                "}\n"
4713                "const char *bar(void);\n", // No break here.
4714                Style);
4715   verifyFormat("template <class T>\n"
4716                "T *\n"     // Problem here: no line break
4717                "f(T &c)\n" // Break here.
4718                "{\n"
4719                "  return NULL;\n"
4720                "}\n"
4721                "template <class T> T *f(T &c);\n", // No break here.
4722                Style);
4723 }
4724 
4725 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
4726   FormatStyle NoBreak = getLLVMStyle();
4727   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
4728   FormatStyle Break = getLLVMStyle();
4729   Break.AlwaysBreakBeforeMultilineStrings = true;
4730   verifyFormat("aaaa = \"bbbb\"\n"
4731                "       \"cccc\";",
4732                NoBreak);
4733   verifyFormat("aaaa =\n"
4734                "    \"bbbb\"\n"
4735                "    \"cccc\";",
4736                Break);
4737   verifyFormat("aaaa(\"bbbb\"\n"
4738                "     \"cccc\");",
4739                NoBreak);
4740   verifyFormat("aaaa(\n"
4741                "    \"bbbb\"\n"
4742                "    \"cccc\");",
4743                Break);
4744   verifyFormat("aaaa(qqq, \"bbbb\"\n"
4745                "          \"cccc\");",
4746                NoBreak);
4747   verifyFormat("aaaa(qqq,\n"
4748                "     \"bbbb\"\n"
4749                "     \"cccc\");",
4750                Break);
4751   verifyFormat("aaaa(qqq,\n"
4752                "     L\"bbbb\"\n"
4753                "     L\"cccc\");",
4754                Break);
4755   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
4756                "                      \"bbbb\"));",
4757                Break);
4758   verifyFormat("string s = someFunction(\n"
4759                "    \"abc\"\n"
4760                "    \"abc\");",
4761                Break);
4762 
4763   // As we break before unary operators, breaking right after them is bad.
4764   verifyFormat("string foo = abc ? \"x\"\n"
4765                "                   \"blah blah blah blah blah blah\"\n"
4766                "                 : \"y\";",
4767                Break);
4768 
4769   // Don't break if there is no column gain.
4770   verifyFormat("f(\"aaaa\"\n"
4771                "  \"bbbb\");",
4772                Break);
4773 
4774   // Treat literals with escaped newlines like multi-line string literals.
4775   EXPECT_EQ("x = \"a\\\n"
4776             "b\\\n"
4777             "c\";",
4778             format("x = \"a\\\n"
4779                    "b\\\n"
4780                    "c\";",
4781                    NoBreak));
4782   EXPECT_EQ("xxxx =\n"
4783             "    \"a\\\n"
4784             "b\\\n"
4785             "c\";",
4786             format("xxxx = \"a\\\n"
4787                    "b\\\n"
4788                    "c\";",
4789                    Break));
4790 
4791   // Exempt ObjC strings for now.
4792   EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
4793             "                          @\"bbbb\";",
4794             format("NSString *const kString = @\"aaaa\"\n"
4795                    "@\"bbbb\";",
4796                    Break));
4797 
4798   Break.ColumnLimit = 0;
4799   verifyFormat("const char *hello = \"hello llvm\";", Break);
4800 }
4801 
4802 TEST_F(FormatTest, AlignsPipes) {
4803   verifyFormat(
4804       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4805       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4806       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4807   verifyFormat(
4808       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
4809       "                     << aaaaaaaaaaaaaaaaaaaa;");
4810   verifyFormat(
4811       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4812       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4813   verifyFormat(
4814       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
4815       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
4816       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
4817   verifyFormat(
4818       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4819       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4820       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4821   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4822                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4823                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4824                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4825   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
4826                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
4827   verifyFormat(
4828       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4829       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4830 
4831   verifyFormat("return out << \"somepacket = {\\n\"\n"
4832                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
4833                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
4834                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
4835                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
4836                "           << \"}\";");
4837 
4838   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4839                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4840                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
4841   verifyFormat(
4842       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
4843       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
4844       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
4845       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
4846       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
4847   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
4848                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4849   verifyFormat(
4850       "void f() {\n"
4851       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
4852       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4853       "}");
4854   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
4855                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
4856   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4857                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4858                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
4859                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
4860   verifyFormat("LOG_IF(aaa == //\n"
4861                "       bbb)\n"
4862                "    << a << b;");
4863 
4864   // Breaking before the first "<<" is generally not desirable.
4865   verifyFormat(
4866       "llvm::errs()\n"
4867       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4868       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4869       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4870       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4871       getLLVMStyleWithColumns(70));
4872   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4873                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4874                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4875                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4876                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4877                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4878                getLLVMStyleWithColumns(70));
4879 
4880   // But sometimes, breaking before the first "<<" is desirable.
4881   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4882                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
4883   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
4884                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4885                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4886   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
4887                "    << BEF << IsTemplate << Description << E->getType();");
4888 
4889   verifyFormat(
4890       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4891       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4892 
4893   // Incomplete string literal.
4894   EXPECT_EQ("llvm::errs() << \"\n"
4895             "             << a;",
4896             format("llvm::errs() << \"\n<<a;"));
4897 
4898   verifyFormat("void f() {\n"
4899                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
4900                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
4901                "}");
4902 
4903   // Handle 'endl'.
4904   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
4905                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4906   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4907 }
4908 
4909 TEST_F(FormatTest, UnderstandsEquals) {
4910   verifyFormat(
4911       "aaaaaaaaaaaaaaaaa =\n"
4912       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4913   verifyFormat(
4914       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4915       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4916   verifyFormat(
4917       "if (a) {\n"
4918       "  f();\n"
4919       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4920       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
4921       "}");
4922 
4923   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4924                "        100000000 + 10000000) {\n}");
4925 }
4926 
4927 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
4928   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4929                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
4930 
4931   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4932                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
4933 
4934   verifyFormat(
4935       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
4936       "                                                          Parameter2);");
4937 
4938   verifyFormat(
4939       "ShortObject->shortFunction(\n"
4940       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
4941       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
4942 
4943   verifyFormat("loooooooooooooongFunction(\n"
4944                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
4945 
4946   verifyFormat(
4947       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
4948       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
4949 
4950   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
4951                "    .WillRepeatedly(Return(SomeValue));");
4952   verifyFormat("void f() {\n"
4953                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
4954                "      .Times(2)\n"
4955                "      .WillRepeatedly(Return(SomeValue));\n"
4956                "}");
4957   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
4958                "    ccccccccccccccccccccccc);");
4959   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4960                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4961                "          .aaaaa(aaaaa),\n"
4962                "      aaaaaaaaaaaaaaaaaaaaa);");
4963   verifyFormat("void f() {\n"
4964                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4965                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
4966                "}");
4967   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4968                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4969                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4970                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4971                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4972   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4973                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4974                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4975                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
4976                "}");
4977 
4978   // Here, it is not necessary to wrap at "." or "->".
4979   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
4980                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4981   verifyFormat(
4982       "aaaaaaaaaaa->aaaaaaaaa(\n"
4983       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4984       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
4985 
4986   verifyFormat(
4987       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4988       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
4989   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
4990                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
4991   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
4992                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
4993 
4994   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4995                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4996                "    .a();");
4997 
4998   FormatStyle NoBinPacking = getLLVMStyle();
4999   NoBinPacking.BinPackParameters = false;
5000   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5001                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5002                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
5003                "                         aaaaaaaaaaaaaaaaaaa,\n"
5004                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5005                NoBinPacking);
5006 
5007   // If there is a subsequent call, change to hanging indentation.
5008   verifyFormat(
5009       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5010       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
5011       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5012   verifyFormat(
5013       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5014       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
5015   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5016                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5017                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5018   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5019                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5020                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5021 }
5022 
5023 TEST_F(FormatTest, WrapsTemplateDeclarations) {
5024   verifyFormat("template <typename T>\n"
5025                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5026   verifyFormat("template <typename T>\n"
5027                "// T should be one of {A, B}.\n"
5028                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5029   verifyFormat(
5030       "template <typename T>\n"
5031       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
5032   verifyFormat("template <typename T>\n"
5033                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
5034                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
5035   verifyFormat(
5036       "template <typename T>\n"
5037       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
5038       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
5039   verifyFormat(
5040       "template <typename T>\n"
5041       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
5042       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
5043       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5044   verifyFormat("template <typename T>\n"
5045                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5046                "    int aaaaaaaaaaaaaaaaaaaaaa);");
5047   verifyFormat(
5048       "template <typename T1, typename T2 = char, typename T3 = char,\n"
5049       "          typename T4 = char>\n"
5050       "void f();");
5051   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
5052                "          template <typename> class cccccccccccccccccccccc,\n"
5053                "          typename ddddddddddddd>\n"
5054                "class C {};");
5055   verifyFormat(
5056       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
5057       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5058 
5059   verifyFormat("void f() {\n"
5060                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
5061                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
5062                "}");
5063 
5064   verifyFormat("template <typename T> class C {};");
5065   verifyFormat("template <typename T> void f();");
5066   verifyFormat("template <typename T> void f() {}");
5067   verifyFormat(
5068       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5069       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5070       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
5071       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5072       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5073       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
5074       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
5075       getLLVMStyleWithColumns(72));
5076   EXPECT_EQ("static_cast<A< //\n"
5077             "    B> *>(\n"
5078             "\n"
5079             "    );",
5080             format("static_cast<A<//\n"
5081                    "    B>*>(\n"
5082                    "\n"
5083                    "    );"));
5084   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5085                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
5086 
5087   FormatStyle AlwaysBreak = getLLVMStyle();
5088   AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
5089   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
5090   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
5091   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
5092   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5093                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5094                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
5095   verifyFormat("template <template <typename> class Fooooooo,\n"
5096                "          template <typename> class Baaaaaaar>\n"
5097                "struct C {};",
5098                AlwaysBreak);
5099   verifyFormat("template <typename T> // T can be A, B or C.\n"
5100                "struct C {};",
5101                AlwaysBreak);
5102 }
5103 
5104 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
5105   verifyFormat(
5106       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5107       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5108   verifyFormat(
5109       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5110       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5111       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5112 
5113   // FIXME: Should we have the extra indent after the second break?
5114   verifyFormat(
5115       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5116       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5117       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5118 
5119   verifyFormat(
5120       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
5121       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
5122 
5123   // Breaking at nested name specifiers is generally not desirable.
5124   verifyFormat(
5125       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5126       "    aaaaaaaaaaaaaaaaaaaaaaa);");
5127 
5128   verifyFormat(
5129       "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5130       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5131       "                   aaaaaaaaaaaaaaaaaaaaa);",
5132       getLLVMStyleWithColumns(74));
5133 
5134   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5135                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5136                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5137 }
5138 
5139 TEST_F(FormatTest, UnderstandsTemplateParameters) {
5140   verifyFormat("A<int> a;");
5141   verifyFormat("A<A<A<int>>> a;");
5142   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
5143   verifyFormat("bool x = a < 1 || 2 > a;");
5144   verifyFormat("bool x = 5 < f<int>();");
5145   verifyFormat("bool x = f<int>() > 5;");
5146   verifyFormat("bool x = 5 < a<int>::x;");
5147   verifyFormat("bool x = a < 4 ? a > 2 : false;");
5148   verifyFormat("bool x = f() ? a < 2 : a > 2;");
5149 
5150   verifyGoogleFormat("A<A<int>> a;");
5151   verifyGoogleFormat("A<A<A<int>>> a;");
5152   verifyGoogleFormat("A<A<A<A<int>>>> a;");
5153   verifyGoogleFormat("A<A<int> > a;");
5154   verifyGoogleFormat("A<A<A<int> > > a;");
5155   verifyGoogleFormat("A<A<A<A<int> > > > a;");
5156   verifyGoogleFormat("A<::A<int>> a;");
5157   verifyGoogleFormat("A<::A> a;");
5158   verifyGoogleFormat("A< ::A> a;");
5159   verifyGoogleFormat("A< ::A<int> > a;");
5160   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
5161   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
5162   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
5163   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
5164   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
5165             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
5166 
5167   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
5168 
5169   verifyFormat("test >> a >> b;");
5170   verifyFormat("test << a >> b;");
5171 
5172   verifyFormat("f<int>();");
5173   verifyFormat("template <typename T> void f() {}");
5174   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
5175   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
5176                "sizeof(char)>::type>;");
5177   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
5178 
5179   // Not template parameters.
5180   verifyFormat("return a < b && c > d;");
5181   verifyFormat("void f() {\n"
5182                "  while (a < b && c > d) {\n"
5183                "  }\n"
5184                "}");
5185   verifyFormat("template <typename... Types>\n"
5186                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
5187 
5188   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5189                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
5190                getLLVMStyleWithColumns(60));
5191   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
5192   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
5193   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
5194 }
5195 
5196 TEST_F(FormatTest, UnderstandsBinaryOperators) {
5197   verifyFormat("COMPARE(a, ==, b);");
5198 }
5199 
5200 TEST_F(FormatTest, UnderstandsPointersToMembers) {
5201   verifyFormat("int A::*x;");
5202   verifyFormat("int (S::*func)(void *);");
5203   verifyFormat("void f() { int (S::*func)(void *); }");
5204   verifyFormat("typedef bool *(Class::*Member)() const;");
5205   verifyFormat("void f() {\n"
5206                "  (a->*f)();\n"
5207                "  a->*x;\n"
5208                "  (a.*f)();\n"
5209                "  ((*a).*f)();\n"
5210                "  a.*x;\n"
5211                "}");
5212   verifyFormat("void f() {\n"
5213                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5214                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
5215                "}");
5216   verifyFormat(
5217       "(aaaaaaaaaa->*bbbbbbb)(\n"
5218       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5219   FormatStyle Style = getLLVMStyle();
5220   Style.PointerAlignment = FormatStyle::PAS_Left;
5221   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
5222 }
5223 
5224 TEST_F(FormatTest, UnderstandsUnaryOperators) {
5225   verifyFormat("int a = -2;");
5226   verifyFormat("f(-1, -2, -3);");
5227   verifyFormat("a[-1] = 5;");
5228   verifyFormat("int a = 5 + -2;");
5229   verifyFormat("if (i == -1) {\n}");
5230   verifyFormat("if (i != -1) {\n}");
5231   verifyFormat("if (i > -1) {\n}");
5232   verifyFormat("if (i < -1) {\n}");
5233   verifyFormat("++(a->f());");
5234   verifyFormat("--(a->f());");
5235   verifyFormat("(a->f())++;");
5236   verifyFormat("a[42]++;");
5237   verifyFormat("if (!(a->f())) {\n}");
5238 
5239   verifyFormat("a-- > b;");
5240   verifyFormat("b ? -a : c;");
5241   verifyFormat("n * sizeof char16;");
5242   verifyFormat("n * alignof char16;", getGoogleStyle());
5243   verifyFormat("sizeof(char);");
5244   verifyFormat("alignof(char);", getGoogleStyle());
5245 
5246   verifyFormat("return -1;");
5247   verifyFormat("switch (a) {\n"
5248                "case -1:\n"
5249                "  break;\n"
5250                "}");
5251   verifyFormat("#define X -1");
5252   verifyFormat("#define X -kConstant");
5253 
5254   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
5255   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
5256 
5257   verifyFormat("int a = /* confusing comment */ -1;");
5258   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
5259   verifyFormat("int a = i /* confusing comment */++;");
5260 }
5261 
5262 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
5263   verifyFormat("if (!aaaaaaaaaa( // break\n"
5264                "        aaaaa)) {\n"
5265                "}");
5266   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
5267                "    aaaaa));");
5268   verifyFormat("*aaa = aaaaaaa( // break\n"
5269                "    bbbbbb);");
5270 }
5271 
5272 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
5273   verifyFormat("bool operator<();");
5274   verifyFormat("bool operator>();");
5275   verifyFormat("bool operator=();");
5276   verifyFormat("bool operator==();");
5277   verifyFormat("bool operator!=();");
5278   verifyFormat("int operator+();");
5279   verifyFormat("int operator++();");
5280   verifyFormat("bool operator();");
5281   verifyFormat("bool operator()();");
5282   verifyFormat("bool operator[]();");
5283   verifyFormat("operator bool();");
5284   verifyFormat("operator int();");
5285   verifyFormat("operator void *();");
5286   verifyFormat("operator SomeType<int>();");
5287   verifyFormat("operator SomeType<int, int>();");
5288   verifyFormat("operator SomeType<SomeType<int>>();");
5289   verifyFormat("void *operator new(std::size_t size);");
5290   verifyFormat("void *operator new[](std::size_t size);");
5291   verifyFormat("void operator delete(void *ptr);");
5292   verifyFormat("void operator delete[](void *ptr);");
5293   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
5294                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
5295 
5296   verifyFormat(
5297       "ostream &operator<<(ostream &OutputStream,\n"
5298       "                    SomeReallyLongType WithSomeReallyLongValue);");
5299   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
5300                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
5301                "  return left.group < right.group;\n"
5302                "}");
5303   verifyFormat("SomeType &operator=(const SomeType &S);");
5304   verifyFormat("f.template operator()<int>();");
5305 
5306   verifyGoogleFormat("operator void*();");
5307   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
5308   verifyGoogleFormat("operator ::A();");
5309 
5310   verifyFormat("using A::operator+;");
5311 }
5312 
5313 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
5314   verifyFormat("Deleted &operator=(const Deleted &)& = default;");
5315   verifyFormat("Deleted &operator=(const Deleted &)&& = delete;");
5316   verifyFormat("SomeType MemberFunction(const Deleted &)& = delete;");
5317   verifyFormat("SomeType MemberFunction(const Deleted &)&& = delete;");
5318   verifyFormat("Deleted &operator=(const Deleted &)&;");
5319   verifyFormat("Deleted &operator=(const Deleted &)&&;");
5320   verifyFormat("SomeType MemberFunction(const Deleted &)&;");
5321   verifyFormat("SomeType MemberFunction(const Deleted &)&&;");
5322 
5323   verifyGoogleFormat("Deleted& operator=(const Deleted&)& = default;");
5324   verifyGoogleFormat("SomeType MemberFunction(const Deleted&)& = delete;");
5325   verifyGoogleFormat("Deleted& operator=(const Deleted&)&;");
5326   verifyGoogleFormat("SomeType MemberFunction(const Deleted&)&;");
5327 
5328   FormatStyle Spaces = getLLVMStyle();
5329   Spaces.SpacesInCStyleCastParentheses = true;
5330   verifyFormat("Deleted &operator=(const Deleted &)& = default;", Spaces);
5331   verifyFormat("SomeType MemberFunction(const Deleted &)& = delete;", Spaces);
5332   verifyFormat("Deleted &operator=(const Deleted &)&;", Spaces);
5333   verifyFormat("SomeType MemberFunction(const Deleted &)&;", Spaces);
5334 
5335   Spaces.SpacesInCStyleCastParentheses = false;
5336   Spaces.SpacesInParentheses = true;
5337   verifyFormat("Deleted &operator=( const Deleted & )& = default;", Spaces);
5338   verifyFormat("SomeType MemberFunction( const Deleted & )& = delete;", Spaces);
5339   verifyFormat("Deleted &operator=( const Deleted & )&;", Spaces);
5340   verifyFormat("SomeType MemberFunction( const Deleted & )&;", Spaces);
5341 }
5342 
5343 TEST_F(FormatTest, UnderstandsNewAndDelete) {
5344   verifyFormat("void f() {\n"
5345                "  A *a = new A;\n"
5346                "  A *a = new (placement) A;\n"
5347                "  delete a;\n"
5348                "  delete (A *)a;\n"
5349                "}");
5350   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5351                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5352   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5353                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5354                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5355   verifyFormat("delete[] h->p;");
5356 }
5357 
5358 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
5359   verifyFormat("int *f(int *a) {}");
5360   verifyFormat("int main(int argc, char **argv) {}");
5361   verifyFormat("Test::Test(int b) : a(b * b) {}");
5362   verifyIndependentOfContext("f(a, *a);");
5363   verifyFormat("void g() { f(*a); }");
5364   verifyIndependentOfContext("int a = b * 10;");
5365   verifyIndependentOfContext("int a = 10 * b;");
5366   verifyIndependentOfContext("int a = b * c;");
5367   verifyIndependentOfContext("int a += b * c;");
5368   verifyIndependentOfContext("int a -= b * c;");
5369   verifyIndependentOfContext("int a *= b * c;");
5370   verifyIndependentOfContext("int a /= b * c;");
5371   verifyIndependentOfContext("int a = *b;");
5372   verifyIndependentOfContext("int a = *b * c;");
5373   verifyIndependentOfContext("int a = b * *c;");
5374   verifyIndependentOfContext("int a = b * (10);");
5375   verifyIndependentOfContext("S << b * (10);");
5376   verifyIndependentOfContext("return 10 * b;");
5377   verifyIndependentOfContext("return *b * *c;");
5378   verifyIndependentOfContext("return a & ~b;");
5379   verifyIndependentOfContext("f(b ? *c : *d);");
5380   verifyIndependentOfContext("int a = b ? *c : *d;");
5381   verifyIndependentOfContext("*b = a;");
5382   verifyIndependentOfContext("a * ~b;");
5383   verifyIndependentOfContext("a * !b;");
5384   verifyIndependentOfContext("a * +b;");
5385   verifyIndependentOfContext("a * -b;");
5386   verifyIndependentOfContext("a * ++b;");
5387   verifyIndependentOfContext("a * --b;");
5388   verifyIndependentOfContext("a[4] * b;");
5389   verifyIndependentOfContext("a[a * a] = 1;");
5390   verifyIndependentOfContext("f() * b;");
5391   verifyIndependentOfContext("a * [self dostuff];");
5392   verifyIndependentOfContext("int x = a * (a + b);");
5393   verifyIndependentOfContext("(a *)(a + b);");
5394   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
5395   verifyIndependentOfContext("int *pa = (int *)&a;");
5396   verifyIndependentOfContext("return sizeof(int **);");
5397   verifyIndependentOfContext("return sizeof(int ******);");
5398   verifyIndependentOfContext("return (int **&)a;");
5399   verifyIndependentOfContext("f((*PointerToArray)[10]);");
5400   verifyFormat("void f(Type (*parameter)[10]) {}");
5401   verifyGoogleFormat("return sizeof(int**);");
5402   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
5403   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
5404   verifyFormat("auto a = [](int **&, int ***) {};");
5405   verifyFormat("auto PointerBinding = [](const char *S) {};");
5406   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
5407   verifyFormat("[](const decltype(*a) &value) {}");
5408   verifyFormat("#define MACRO() [](A *a) { return 1; }");
5409   verifyIndependentOfContext("typedef void (*f)(int *a);");
5410   verifyIndependentOfContext("int i{a * b};");
5411   verifyIndependentOfContext("aaa && aaa->f();");
5412   verifyIndependentOfContext("int x = ~*p;");
5413   verifyFormat("Constructor() : a(a), area(width * height) {}");
5414   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
5415   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
5416   verifyFormat("void f() { f(a, c * d); }");
5417   verifyFormat("void f() { f(new a(), c * d); }");
5418 
5419   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
5420 
5421   verifyIndependentOfContext("A<int *> a;");
5422   verifyIndependentOfContext("A<int **> a;");
5423   verifyIndependentOfContext("A<int *, int *> a;");
5424   verifyIndependentOfContext("A<int *[]> a;");
5425   verifyIndependentOfContext(
5426       "const char *const p = reinterpret_cast<const char *const>(q);");
5427   verifyIndependentOfContext("A<int **, int **> a;");
5428   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
5429   verifyFormat("for (char **a = b; *a; ++a) {\n}");
5430   verifyFormat("for (; a && b;) {\n}");
5431   verifyFormat("bool foo = true && [] { return false; }();");
5432 
5433   verifyFormat(
5434       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5435       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5436 
5437   verifyGoogleFormat("**outparam = 1;");
5438   verifyGoogleFormat("*outparam = a * b;");
5439   verifyGoogleFormat("int main(int argc, char** argv) {}");
5440   verifyGoogleFormat("A<int*> a;");
5441   verifyGoogleFormat("A<int**> a;");
5442   verifyGoogleFormat("A<int*, int*> a;");
5443   verifyGoogleFormat("A<int**, int**> a;");
5444   verifyGoogleFormat("f(b ? *c : *d);");
5445   verifyGoogleFormat("int a = b ? *c : *d;");
5446   verifyGoogleFormat("Type* t = **x;");
5447   verifyGoogleFormat("Type* t = *++*x;");
5448   verifyGoogleFormat("*++*x;");
5449   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
5450   verifyGoogleFormat("Type* t = x++ * y;");
5451   verifyGoogleFormat(
5452       "const char* const p = reinterpret_cast<const char* const>(q);");
5453   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
5454   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
5455   verifyGoogleFormat("template <typename T>\n"
5456                      "void f(int i = 0, SomeType** temps = NULL);");
5457 
5458   FormatStyle Left = getLLVMStyle();
5459   Left.PointerAlignment = FormatStyle::PAS_Left;
5460   verifyFormat("x = *a(x) = *a(y);", Left);
5461   verifyFormat("for (;; * = b) {\n}", Left);
5462 
5463   verifyIndependentOfContext("a = *(x + y);");
5464   verifyIndependentOfContext("a = &(x + y);");
5465   verifyIndependentOfContext("*(x + y).call();");
5466   verifyIndependentOfContext("&(x + y)->call();");
5467   verifyFormat("void f() { &(*I).first; }");
5468 
5469   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
5470   verifyFormat(
5471       "int *MyValues = {\n"
5472       "    *A, // Operator detection might be confused by the '{'\n"
5473       "    *BB // Operator detection might be confused by previous comment\n"
5474       "};");
5475 
5476   verifyIndependentOfContext("if (int *a = &b)");
5477   verifyIndependentOfContext("if (int &a = *b)");
5478   verifyIndependentOfContext("if (a & b[i])");
5479   verifyIndependentOfContext("if (a::b::c::d & b[i])");
5480   verifyIndependentOfContext("if (*b[i])");
5481   verifyIndependentOfContext("if (int *a = (&b))");
5482   verifyIndependentOfContext("while (int *a = &b)");
5483   verifyIndependentOfContext("size = sizeof *a;");
5484   verifyIndependentOfContext("if (a && (b = c))");
5485   verifyFormat("void f() {\n"
5486                "  for (const int &v : Values) {\n"
5487                "  }\n"
5488                "}");
5489   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
5490   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
5491   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
5492 
5493   verifyFormat("#define A (!a * b)");
5494   verifyFormat("#define MACRO     \\\n"
5495                "  int *i = a * b; \\\n"
5496                "  void f(a *b);",
5497                getLLVMStyleWithColumns(19));
5498 
5499   verifyIndependentOfContext("A = new SomeType *[Length];");
5500   verifyIndependentOfContext("A = new SomeType *[Length]();");
5501   verifyIndependentOfContext("T **t = new T *;");
5502   verifyIndependentOfContext("T **t = new T *();");
5503   verifyGoogleFormat("A = new SomeType*[Length]();");
5504   verifyGoogleFormat("A = new SomeType*[Length];");
5505   verifyGoogleFormat("T** t = new T*;");
5506   verifyGoogleFormat("T** t = new T*();");
5507 
5508   FormatStyle PointerLeft = getLLVMStyle();
5509   PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
5510   verifyFormat("delete *x;", PointerLeft);
5511   verifyFormat("STATIC_ASSERT((a & b) == 0);");
5512   verifyFormat("STATIC_ASSERT(0 == (a & b));");
5513   verifyFormat("template <bool a, bool b> "
5514                "typename t::if<x && y>::type f() {}");
5515   verifyFormat("template <int *y> f() {}");
5516   verifyFormat("vector<int *> v;");
5517   verifyFormat("vector<int *const> v;");
5518   verifyFormat("vector<int *const **const *> v;");
5519   verifyFormat("vector<int *volatile> v;");
5520   verifyFormat("vector<a * b> v;");
5521   verifyFormat("foo<b && false>();");
5522   verifyFormat("foo<b & 1>();");
5523   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
5524   verifyFormat(
5525       "template <class T, class = typename std::enable_if<\n"
5526       "                       std::is_integral<T>::value &&\n"
5527       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
5528       "void F();",
5529       getLLVMStyleWithColumns(76));
5530   verifyFormat(
5531       "template <class T,\n"
5532       "          class = typename ::std::enable_if<\n"
5533       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
5534       "void F();",
5535       getGoogleStyleWithColumns(68));
5536 
5537   verifyIndependentOfContext("MACRO(int *i);");
5538   verifyIndependentOfContext("MACRO(auto *a);");
5539   verifyIndependentOfContext("MACRO(const A *a);");
5540   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
5541   // FIXME: Is there a way to make this work?
5542   // verifyIndependentOfContext("MACRO(A *a);");
5543 
5544   verifyFormat("DatumHandle const *operator->() const { return input_; }");
5545 
5546   EXPECT_EQ("#define OP(x)                                    \\\n"
5547             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
5548             "    return s << a.DebugString();                 \\\n"
5549             "  }",
5550             format("#define OP(x) \\\n"
5551                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
5552                    "    return s << a.DebugString(); \\\n"
5553                    "  }",
5554                    getLLVMStyleWithColumns(50)));
5555 
5556   // FIXME: We cannot handle this case yet; we might be able to figure out that
5557   // foo<x> d > v; doesn't make sense.
5558   verifyFormat("foo<a<b && c> d> v;");
5559 
5560   FormatStyle PointerMiddle = getLLVMStyle();
5561   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
5562   verifyFormat("delete *x;", PointerMiddle);
5563   verifyFormat("int * x;", PointerMiddle);
5564   verifyFormat("template <int * y> f() {}", PointerMiddle);
5565   verifyFormat("int * f(int * a) {}", PointerMiddle);
5566   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
5567   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
5568   verifyFormat("A<int *> a;", PointerMiddle);
5569   verifyFormat("A<int **> a;", PointerMiddle);
5570   verifyFormat("A<int *, int *> a;", PointerMiddle);
5571   verifyFormat("A<int * []> a;", PointerMiddle);
5572   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
5573   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
5574   verifyFormat("T ** t = new T *;", PointerMiddle);
5575 
5576   // Member function reference qualifiers aren't binary operators.
5577   verifyFormat("string // break\n"
5578                "operator()() & {}");
5579   verifyFormat("string // break\n"
5580                "operator()() && {}");
5581   verifyGoogleFormat("template <typename T>\n"
5582                      "auto x() & -> int {}");
5583 }
5584 
5585 TEST_F(FormatTest, UnderstandsAttributes) {
5586   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
5587   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
5588                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
5589   FormatStyle AfterType = getLLVMStyle();
5590   AfterType.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
5591   verifyFormat("__attribute__((nodebug)) void\n"
5592                "foo() {}\n",
5593                AfterType);
5594 }
5595 
5596 TEST_F(FormatTest, UnderstandsEllipsis) {
5597   verifyFormat("int printf(const char *fmt, ...);");
5598   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
5599   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
5600 
5601   FormatStyle PointersLeft = getLLVMStyle();
5602   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
5603   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
5604 }
5605 
5606 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
5607   EXPECT_EQ("int *a;\n"
5608             "int *a;\n"
5609             "int *a;",
5610             format("int *a;\n"
5611                    "int* a;\n"
5612                    "int *a;",
5613                    getGoogleStyle()));
5614   EXPECT_EQ("int* a;\n"
5615             "int* a;\n"
5616             "int* a;",
5617             format("int* a;\n"
5618                    "int* a;\n"
5619                    "int *a;",
5620                    getGoogleStyle()));
5621   EXPECT_EQ("int *a;\n"
5622             "int *a;\n"
5623             "int *a;",
5624             format("int *a;\n"
5625                    "int * a;\n"
5626                    "int *  a;",
5627                    getGoogleStyle()));
5628   EXPECT_EQ("auto x = [] {\n"
5629             "  int *a;\n"
5630             "  int *a;\n"
5631             "  int *a;\n"
5632             "};",
5633             format("auto x=[]{int *a;\n"
5634                    "int * a;\n"
5635                    "int *  a;};",
5636                    getGoogleStyle()));
5637 }
5638 
5639 TEST_F(FormatTest, UnderstandsRvalueReferences) {
5640   verifyFormat("int f(int &&a) {}");
5641   verifyFormat("int f(int a, char &&b) {}");
5642   verifyFormat("void f() { int &&a = b; }");
5643   verifyGoogleFormat("int f(int a, char&& b) {}");
5644   verifyGoogleFormat("void f() { int&& a = b; }");
5645 
5646   verifyIndependentOfContext("A<int &&> a;");
5647   verifyIndependentOfContext("A<int &&, int &&> a;");
5648   verifyGoogleFormat("A<int&&> a;");
5649   verifyGoogleFormat("A<int&&, int&&> a;");
5650 
5651   // Not rvalue references:
5652   verifyFormat("template <bool B, bool C> class A {\n"
5653                "  static_assert(B && C, \"Something is wrong\");\n"
5654                "};");
5655   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
5656   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
5657   verifyFormat("#define A(a, b) (a && b)");
5658 }
5659 
5660 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
5661   verifyFormat("void f() {\n"
5662                "  x[aaaaaaaaa -\n"
5663                "    b] = 23;\n"
5664                "}",
5665                getLLVMStyleWithColumns(15));
5666 }
5667 
5668 TEST_F(FormatTest, FormatsCasts) {
5669   verifyFormat("Type *A = static_cast<Type *>(P);");
5670   verifyFormat("Type *A = (Type *)P;");
5671   verifyFormat("Type *A = (vector<Type *, int *>)P;");
5672   verifyFormat("int a = (int)(2.0f);");
5673   verifyFormat("int a = (int)2.0f;");
5674   verifyFormat("x[(int32)y];");
5675   verifyFormat("x = (int32)y;");
5676   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
5677   verifyFormat("int a = (int)*b;");
5678   verifyFormat("int a = (int)2.0f;");
5679   verifyFormat("int a = (int)~0;");
5680   verifyFormat("int a = (int)++a;");
5681   verifyFormat("int a = (int)sizeof(int);");
5682   verifyFormat("int a = (int)+2;");
5683   verifyFormat("my_int a = (my_int)2.0f;");
5684   verifyFormat("my_int a = (my_int)sizeof(int);");
5685   verifyFormat("return (my_int)aaa;");
5686   verifyFormat("#define x ((int)-1)");
5687   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
5688   verifyFormat("#define p(q) ((int *)&q)");
5689   verifyFormat("fn(a)(b) + 1;");
5690 
5691   verifyFormat("void f() { my_int a = (my_int)*b; }");
5692   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
5693   verifyFormat("my_int a = (my_int)~0;");
5694   verifyFormat("my_int a = (my_int)++a;");
5695   verifyFormat("my_int a = (my_int)-2;");
5696   verifyFormat("my_int a = (my_int)1;");
5697   verifyFormat("my_int a = (my_int *)1;");
5698   verifyFormat("my_int a = (const my_int)-1;");
5699   verifyFormat("my_int a = (const my_int *)-1;");
5700   verifyFormat("my_int a = (my_int)(my_int)-1;");
5701   verifyFormat("my_int a = (ns::my_int)-2;");
5702   verifyFormat("case (my_int)ONE:");
5703 
5704   // FIXME: single value wrapped with paren will be treated as cast.
5705   verifyFormat("void f(int i = (kValue)*kMask) {}");
5706 
5707   verifyFormat("{ (void)F; }");
5708 
5709   // Don't break after a cast's
5710   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5711                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
5712                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
5713 
5714   // These are not casts.
5715   verifyFormat("void f(int *) {}");
5716   verifyFormat("f(foo)->b;");
5717   verifyFormat("f(foo).b;");
5718   verifyFormat("f(foo)(b);");
5719   verifyFormat("f(foo)[b];");
5720   verifyFormat("[](foo) { return 4; }(bar);");
5721   verifyFormat("(*funptr)(foo)[4];");
5722   verifyFormat("funptrs[4](foo)[4];");
5723   verifyFormat("void f(int *);");
5724   verifyFormat("void f(int *) = 0;");
5725   verifyFormat("void f(SmallVector<int>) {}");
5726   verifyFormat("void f(SmallVector<int>);");
5727   verifyFormat("void f(SmallVector<int>) = 0;");
5728   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
5729   verifyFormat("int a = sizeof(int) * b;");
5730   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
5731   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
5732   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
5733   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
5734 
5735   // These are not casts, but at some point were confused with casts.
5736   verifyFormat("virtual void foo(int *) override;");
5737   verifyFormat("virtual void foo(char &) const;");
5738   verifyFormat("virtual void foo(int *a, char *) const;");
5739   verifyFormat("int a = sizeof(int *) + b;");
5740   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
5741 
5742   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
5743                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5744   // FIXME: The indentation here is not ideal.
5745   verifyFormat(
5746       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5747       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
5748       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
5749 }
5750 
5751 TEST_F(FormatTest, FormatsFunctionTypes) {
5752   verifyFormat("A<bool()> a;");
5753   verifyFormat("A<SomeType()> a;");
5754   verifyFormat("A<void (*)(int, std::string)> a;");
5755   verifyFormat("A<void *(int)>;");
5756   verifyFormat("void *(*a)(int *, SomeType *);");
5757   verifyFormat("int (*func)(void *);");
5758   verifyFormat("void f() { int (*func)(void *); }");
5759   verifyFormat("template <class CallbackClass>\n"
5760                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
5761 
5762   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
5763   verifyGoogleFormat("void* (*a)(int);");
5764   verifyGoogleFormat(
5765       "template <class CallbackClass>\n"
5766       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
5767 
5768   // Other constructs can look somewhat like function types:
5769   verifyFormat("A<sizeof(*x)> a;");
5770   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
5771   verifyFormat("some_var = function(*some_pointer_var)[0];");
5772   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
5773 }
5774 
5775 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
5776   verifyFormat("A (*foo_)[6];");
5777   verifyFormat("vector<int> (*foo_)[6];");
5778 }
5779 
5780 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
5781   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5782                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5783   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
5784                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5785   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5786                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
5787 
5788   // Different ways of ()-initializiation.
5789   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5790                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
5791   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5792                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
5793   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5794                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
5795   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5796                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
5797 }
5798 
5799 TEST_F(FormatTest, BreaksLongDeclarations) {
5800   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
5801                "    AnotherNameForTheLongType;");
5802   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
5803                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5804   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5805                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5806   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
5807                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5808   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5809                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5810   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
5811                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5812   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5813                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5814   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5815                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5816   FormatStyle Indented = getLLVMStyle();
5817   Indented.IndentWrappedFunctionNames = true;
5818   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5819                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
5820                Indented);
5821   verifyFormat(
5822       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5823       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5824       Indented);
5825   verifyFormat(
5826       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5827       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5828       Indented);
5829   verifyFormat(
5830       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5831       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5832       Indented);
5833 
5834   // FIXME: Without the comment, this breaks after "(".
5835   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
5836                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
5837                getGoogleStyle());
5838 
5839   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
5840                "                  int LoooooooooooooooooooongParam2) {}");
5841   verifyFormat(
5842       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
5843       "                                   SourceLocation L, IdentifierIn *II,\n"
5844       "                                   Type *T) {}");
5845   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
5846                "ReallyReaaallyLongFunctionName(\n"
5847                "    const std::string &SomeParameter,\n"
5848                "    const SomeType<string, SomeOtherTemplateParameter>\n"
5849                "        &ReallyReallyLongParameterName,\n"
5850                "    const SomeType<string, SomeOtherTemplateParameter>\n"
5851                "        &AnotherLongParameterName) {}");
5852   verifyFormat("template <typename A>\n"
5853                "SomeLoooooooooooooooooooooongType<\n"
5854                "    typename some_namespace::SomeOtherType<A>::Type>\n"
5855                "Function() {}");
5856 
5857   verifyGoogleFormat(
5858       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
5859       "    aaaaaaaaaaaaaaaaaaaaaaa;");
5860   verifyGoogleFormat(
5861       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
5862       "                                   SourceLocation L) {}");
5863   verifyGoogleFormat(
5864       "some_namespace::LongReturnType\n"
5865       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
5866       "    int first_long_parameter, int second_parameter) {}");
5867 
5868   verifyGoogleFormat("template <typename T>\n"
5869                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5870                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
5871   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5872                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
5873 
5874   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5875                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5876                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5877   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5878                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5879                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
5880   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5881                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5882                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
5883                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5884 }
5885 
5886 TEST_F(FormatTest, FormatsArrays) {
5887   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5888                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
5889   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5890                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5891   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5892                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
5893   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5894                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5895                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5896   verifyFormat(
5897       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
5898       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5899       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
5900 
5901   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
5902                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
5903   verifyFormat(
5904       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
5905       "                                  .aaaaaaa[0]\n"
5906       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
5907 
5908   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
5909 }
5910 
5911 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
5912   verifyFormat("(a)->b();");
5913   verifyFormat("--a;");
5914 }
5915 
5916 TEST_F(FormatTest, HandlesIncludeDirectives) {
5917   verifyFormat("#include <string>\n"
5918                "#include <a/b/c.h>\n"
5919                "#include \"a/b/string\"\n"
5920                "#include \"string.h\"\n"
5921                "#include \"string.h\"\n"
5922                "#include <a-a>\n"
5923                "#include < path with space >\n"
5924                "#include_next <test.h>"
5925                "#include \"abc.h\" // this is included for ABC\n"
5926                "#include \"some long include\" // with a comment\n"
5927                "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
5928                getLLVMStyleWithColumns(35));
5929   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
5930   EXPECT_EQ("#include <a>", format("#include<a>"));
5931 
5932   verifyFormat("#import <string>");
5933   verifyFormat("#import <a/b/c.h>");
5934   verifyFormat("#import \"a/b/string\"");
5935   verifyFormat("#import \"string.h\"");
5936   verifyFormat("#import \"string.h\"");
5937   verifyFormat("#if __has_include(<strstream>)\n"
5938                "#include <strstream>\n"
5939                "#endif");
5940 
5941   verifyFormat("#define MY_IMPORT <a/b>");
5942 
5943   // Protocol buffer definition or missing "#".
5944   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
5945                getLLVMStyleWithColumns(30));
5946 
5947   FormatStyle Style = getLLVMStyle();
5948   Style.AlwaysBreakBeforeMultilineStrings = true;
5949   Style.ColumnLimit = 0;
5950   verifyFormat("#import \"abc.h\"", Style);
5951 
5952   // But 'import' might also be a regular C++ namespace.
5953   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5954                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5955 }
5956 
5957 //===----------------------------------------------------------------------===//
5958 // Error recovery tests.
5959 //===----------------------------------------------------------------------===//
5960 
5961 TEST_F(FormatTest, IncompleteParameterLists) {
5962   FormatStyle NoBinPacking = getLLVMStyle();
5963   NoBinPacking.BinPackParameters = false;
5964   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
5965                "                        double *min_x,\n"
5966                "                        double *max_x,\n"
5967                "                        double *min_y,\n"
5968                "                        double *max_y,\n"
5969                "                        double *min_z,\n"
5970                "                        double *max_z, ) {}",
5971                NoBinPacking);
5972 }
5973 
5974 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
5975   verifyFormat("void f() { return; }\n42");
5976   verifyFormat("void f() {\n"
5977                "  if (0)\n"
5978                "    return;\n"
5979                "}\n"
5980                "42");
5981   verifyFormat("void f() { return }\n42");
5982   verifyFormat("void f() {\n"
5983                "  if (0)\n"
5984                "    return\n"
5985                "}\n"
5986                "42");
5987 }
5988 
5989 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
5990   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
5991   EXPECT_EQ("void f() {\n"
5992             "  if (a)\n"
5993             "    return\n"
5994             "}",
5995             format("void  f  (  )  {  if  ( a )  return  }"));
5996   EXPECT_EQ("namespace N {\n"
5997             "void f()\n"
5998             "}",
5999             format("namespace  N  {  void f()  }"));
6000   EXPECT_EQ("namespace N {\n"
6001             "void f() {}\n"
6002             "void g()\n"
6003             "}",
6004             format("namespace N  { void f( ) { } void g( ) }"));
6005 }
6006 
6007 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
6008   verifyFormat("int aaaaaaaa =\n"
6009                "    // Overlylongcomment\n"
6010                "    b;",
6011                getLLVMStyleWithColumns(20));
6012   verifyFormat("function(\n"
6013                "    ShortArgument,\n"
6014                "    LoooooooooooongArgument);\n",
6015                getLLVMStyleWithColumns(20));
6016 }
6017 
6018 TEST_F(FormatTest, IncorrectAccessSpecifier) {
6019   verifyFormat("public:");
6020   verifyFormat("class A {\n"
6021                "public\n"
6022                "  void f() {}\n"
6023                "};");
6024   verifyFormat("public\n"
6025                "int qwerty;");
6026   verifyFormat("public\n"
6027                "B {}");
6028   verifyFormat("public\n"
6029                "{}");
6030   verifyFormat("public\n"
6031                "B { int x; }");
6032 }
6033 
6034 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
6035   verifyFormat("{");
6036   verifyFormat("#})");
6037   verifyNoCrash("(/**/[:!] ?[).");
6038 }
6039 
6040 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
6041   verifyFormat("do {\n}");
6042   verifyFormat("do {\n}\n"
6043                "f();");
6044   verifyFormat("do {\n}\n"
6045                "wheeee(fun);");
6046   verifyFormat("do {\n"
6047                "  f();\n"
6048                "}");
6049 }
6050 
6051 TEST_F(FormatTest, IncorrectCodeMissingParens) {
6052   verifyFormat("if {\n  foo;\n  foo();\n}");
6053   verifyFormat("switch {\n  foo;\n  foo();\n}");
6054   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
6055   verifyFormat("while {\n  foo;\n  foo();\n}");
6056   verifyFormat("do {\n  foo;\n  foo();\n} while;");
6057 }
6058 
6059 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
6060   verifyIncompleteFormat("namespace {\n"
6061                          "class Foo { Foo (\n"
6062                          "};\n"
6063                          "} // comment");
6064 }
6065 
6066 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
6067   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
6068   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
6069   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
6070   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
6071 
6072   EXPECT_EQ("{\n"
6073             "  {\n"
6074             "    breakme(\n"
6075             "        qwe);\n"
6076             "  }\n",
6077             format("{\n"
6078                    "    {\n"
6079                    " breakme(qwe);\n"
6080                    "}\n",
6081                    getLLVMStyleWithColumns(10)));
6082 }
6083 
6084 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
6085   verifyFormat("int x = {\n"
6086                "    avariable,\n"
6087                "    b(alongervariable)};",
6088                getLLVMStyleWithColumns(25));
6089 }
6090 
6091 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
6092   verifyFormat("return (a)(b){1, 2, 3};");
6093 }
6094 
6095 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
6096   verifyFormat("vector<int> x{1, 2, 3, 4};");
6097   verifyFormat("vector<int> x{\n"
6098                "    1, 2, 3, 4,\n"
6099                "};");
6100   verifyFormat("vector<T> x{{}, {}, {}, {}};");
6101   verifyFormat("f({1, 2});");
6102   verifyFormat("auto v = Foo{-1};");
6103   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
6104   verifyFormat("Class::Class : member{1, 2, 3} {}");
6105   verifyFormat("new vector<int>{1, 2, 3};");
6106   verifyFormat("new int[3]{1, 2, 3};");
6107   verifyFormat("new int{1};");
6108   verifyFormat("return {arg1, arg2};");
6109   verifyFormat("return {arg1, SomeType{parameter}};");
6110   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
6111   verifyFormat("new T{arg1, arg2};");
6112   verifyFormat("f(MyMap[{composite, key}]);");
6113   verifyFormat("class Class {\n"
6114                "  T member = {arg1, arg2};\n"
6115                "};");
6116   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
6117   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
6118   verifyFormat("int a = std::is_integral<int>{} + 0;");
6119 
6120   verifyFormat("int foo(int i) { return fo1{}(i); }");
6121   verifyFormat("int foo(int i) { return fo1{}(i); }");
6122   verifyFormat("auto i = decltype(x){};");
6123   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
6124   verifyFormat("Node n{1, Node{1000}, //\n"
6125                "       2};");
6126   verifyFormat("Aaaa aaaaaaa{\n"
6127                "    {\n"
6128                "        aaaa,\n"
6129                "    },\n"
6130                "};");
6131   verifyFormat("class C : public D {\n"
6132                "  SomeClass SC{2};\n"
6133                "};");
6134   verifyFormat("class C : public A {\n"
6135                "  class D : public B {\n"
6136                "    void f() { int i{2}; }\n"
6137                "  };\n"
6138                "};");
6139 
6140   // In combination with BinPackArguments = false.
6141   FormatStyle NoBinPacking = getLLVMStyle();
6142   NoBinPacking.BinPackArguments = false;
6143   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
6144                "                      bbbbb,\n"
6145                "                      ccccc,\n"
6146                "                      ddddd,\n"
6147                "                      eeeee,\n"
6148                "                      ffffff,\n"
6149                "                      ggggg,\n"
6150                "                      hhhhhh,\n"
6151                "                      iiiiii,\n"
6152                "                      jjjjjj,\n"
6153                "                      kkkkkk};",
6154                NoBinPacking);
6155   verifyFormat("const Aaaaaa aaaaa = {\n"
6156                "    aaaaa,\n"
6157                "    bbbbb,\n"
6158                "    ccccc,\n"
6159                "    ddddd,\n"
6160                "    eeeee,\n"
6161                "    ffffff,\n"
6162                "    ggggg,\n"
6163                "    hhhhhh,\n"
6164                "    iiiiii,\n"
6165                "    jjjjjj,\n"
6166                "    kkkkkk,\n"
6167                "};",
6168                NoBinPacking);
6169   verifyFormat(
6170       "const Aaaaaa aaaaa = {\n"
6171       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
6172       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
6173       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
6174       "};",
6175       NoBinPacking);
6176 
6177   // FIXME: The alignment of these trailing comments might be bad. Then again,
6178   // this might be utterly useless in real code.
6179   verifyFormat("Constructor::Constructor()\n"
6180                "    : some_value{         //\n"
6181                "                 aaaaaaa, //\n"
6182                "                 bbbbbbb} {}");
6183 
6184   // In braced lists, the first comment is always assumed to belong to the
6185   // first element. Thus, it can be moved to the next or previous line as
6186   // appropriate.
6187   EXPECT_EQ("function({// First element:\n"
6188             "          1,\n"
6189             "          // Second element:\n"
6190             "          2});",
6191             format("function({\n"
6192                    "    // First element:\n"
6193                    "    1,\n"
6194                    "    // Second element:\n"
6195                    "    2});"));
6196   EXPECT_EQ("std::vector<int> MyNumbers{\n"
6197             "    // First element:\n"
6198             "    1,\n"
6199             "    // Second element:\n"
6200             "    2};",
6201             format("std::vector<int> MyNumbers{// First element:\n"
6202                    "                           1,\n"
6203                    "                           // Second element:\n"
6204                    "                           2};",
6205                    getLLVMStyleWithColumns(30)));
6206   // A trailing comma should still lead to an enforced line break.
6207   EXPECT_EQ("vector<int> SomeVector = {\n"
6208             "    // aaa\n"
6209             "    1, 2,\n"
6210             "};",
6211             format("vector<int> SomeVector = { // aaa\n"
6212                    "    1, 2, };"));
6213 
6214   FormatStyle ExtraSpaces = getLLVMStyle();
6215   ExtraSpaces.Cpp11BracedListStyle = false;
6216   ExtraSpaces.ColumnLimit = 75;
6217   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
6218   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
6219   verifyFormat("f({ 1, 2 });", ExtraSpaces);
6220   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
6221   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
6222   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
6223   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
6224   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
6225   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
6226   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
6227   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
6228   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
6229   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
6230   verifyFormat("class Class {\n"
6231                "  T member = { arg1, arg2 };\n"
6232                "};",
6233                ExtraSpaces);
6234   verifyFormat(
6235       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6236       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
6237       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6238       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
6239       ExtraSpaces);
6240   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
6241   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
6242                ExtraSpaces);
6243   verifyFormat(
6244       "someFunction(OtherParam,\n"
6245       "             BracedList{ // comment 1 (Forcing interesting break)\n"
6246       "                         param1, param2,\n"
6247       "                         // comment 2\n"
6248       "                         param3, param4 });",
6249       ExtraSpaces);
6250   verifyFormat(
6251       "std::this_thread::sleep_for(\n"
6252       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
6253       ExtraSpaces);
6254   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
6255                "    aaaaaaa,\n"
6256                "    aaaaaaaaaa,\n"
6257                "    aaaaa,\n"
6258                "    aaaaaaaaaaaaaaa,\n"
6259                "    aaa,\n"
6260                "    aaaaaaaaaa,\n"
6261                "    a,\n"
6262                "    aaaaaaaaaaaaaaaaaaaaa,\n"
6263                "    aaaaaaaaaaaa,\n"
6264                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
6265                "    aaaaaaa,\n"
6266                "    a};");
6267   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
6268 }
6269 
6270 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
6271   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6272                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6273                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6274                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6275                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6276                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6277   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
6278                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6279                "                 1, 22, 333, 4444, 55555, //\n"
6280                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6281                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6282   verifyFormat(
6283       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6284       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6285       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
6286       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6287       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6288       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6289       "                 7777777};");
6290   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6291                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6292                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6293   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6294                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6295                "    // Separating comment.\n"
6296                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
6297   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6298                "    // Leading comment\n"
6299                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6300                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6301   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6302                "                 1, 1, 1, 1};",
6303                getLLVMStyleWithColumns(39));
6304   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6305                "                 1, 1, 1, 1};",
6306                getLLVMStyleWithColumns(38));
6307   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
6308                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
6309                getLLVMStyleWithColumns(43));
6310   verifyFormat(
6311       "static unsigned SomeValues[10][3] = {\n"
6312       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
6313       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
6314   verifyFormat("static auto fields = new vector<string>{\n"
6315                "    \"aaaaaaaaaaaaa\",\n"
6316                "    \"aaaaaaaaaaaaa\",\n"
6317                "    \"aaaaaaaaaaaa\",\n"
6318                "    \"aaaaaaaaaaaaaa\",\n"
6319                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6320                "    \"aaaaaaaaaaaa\",\n"
6321                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6322                "};");
6323   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
6324   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
6325                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
6326                "                 3, cccccccccccccccccccccc};",
6327                getLLVMStyleWithColumns(60));
6328 
6329   // Trailing commas.
6330   verifyFormat("vector<int> x = {\n"
6331                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
6332                "};",
6333                getLLVMStyleWithColumns(39));
6334   verifyFormat("vector<int> x = {\n"
6335                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
6336                "};",
6337                getLLVMStyleWithColumns(39));
6338   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6339                "                 1, 1, 1, 1,\n"
6340                "                 /**/ /**/};",
6341                getLLVMStyleWithColumns(39));
6342 
6343   // Trailing comment in the first line.
6344   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
6345                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
6346                "    111111111,  222222222,  3333333333,  444444444,  //\n"
6347                "    11111111,   22222222,   333333333,   44444444};");
6348   // Trailing comment in the last line.
6349   verifyFormat("int aaaaa[] = {\n"
6350                "    1, 2, 3, // comment\n"
6351                "    4, 5, 6  // comment\n"
6352                "};");
6353 
6354   // With nested lists, we should either format one item per line or all nested
6355   // lists one on line.
6356   // FIXME: For some nested lists, we can do better.
6357   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
6358                "        {aaaaaaaaaaaaaaaaaaa},\n"
6359                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
6360                "        {aaaaaaaaaaaaaaaaa}};",
6361                getLLVMStyleWithColumns(60));
6362   verifyFormat(
6363       "SomeStruct my_struct_array = {\n"
6364       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
6365       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
6366       "    {aaa, aaa},\n"
6367       "    {aaa, aaa},\n"
6368       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
6369       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6370       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
6371 
6372   // No column layout should be used here.
6373   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
6374                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
6375 
6376   verifyNoCrash("a<,");
6377 }
6378 
6379 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
6380   FormatStyle DoNotMerge = getLLVMStyle();
6381   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6382 
6383   verifyFormat("void f() { return 42; }");
6384   verifyFormat("void f() {\n"
6385                "  return 42;\n"
6386                "}",
6387                DoNotMerge);
6388   verifyFormat("void f() {\n"
6389                "  // Comment\n"
6390                "}");
6391   verifyFormat("{\n"
6392                "#error {\n"
6393                "  int a;\n"
6394                "}");
6395   verifyFormat("{\n"
6396                "  int a;\n"
6397                "#error {\n"
6398                "}");
6399   verifyFormat("void f() {} // comment");
6400   verifyFormat("void f() { int a; } // comment");
6401   verifyFormat("void f() {\n"
6402                "} // comment",
6403                DoNotMerge);
6404   verifyFormat("void f() {\n"
6405                "  int a;\n"
6406                "} // comment",
6407                DoNotMerge);
6408   verifyFormat("void f() {\n"
6409                "} // comment",
6410                getLLVMStyleWithColumns(15));
6411 
6412   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
6413   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
6414 
6415   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
6416   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
6417   verifyFormat("class C {\n"
6418                "  C()\n"
6419                "      : iiiiiiii(nullptr),\n"
6420                "        kkkkkkk(nullptr),\n"
6421                "        mmmmmmm(nullptr),\n"
6422                "        nnnnnnn(nullptr) {}\n"
6423                "};",
6424                getGoogleStyle());
6425 
6426   FormatStyle NoColumnLimit = getLLVMStyle();
6427   NoColumnLimit.ColumnLimit = 0;
6428   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
6429   EXPECT_EQ("class C {\n"
6430             "  A() : b(0) {}\n"
6431             "};",
6432             format("class C{A():b(0){}};", NoColumnLimit));
6433   EXPECT_EQ("A()\n"
6434             "    : b(0) {\n"
6435             "}",
6436             format("A()\n:b(0)\n{\n}", NoColumnLimit));
6437 
6438   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
6439   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
6440       FormatStyle::SFS_None;
6441   EXPECT_EQ("A()\n"
6442             "    : b(0) {\n"
6443             "}",
6444             format("A():b(0){}", DoNotMergeNoColumnLimit));
6445   EXPECT_EQ("A()\n"
6446             "    : b(0) {\n"
6447             "}",
6448             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
6449 
6450   verifyFormat("#define A          \\\n"
6451                "  void f() {       \\\n"
6452                "    int i;         \\\n"
6453                "  }",
6454                getLLVMStyleWithColumns(20));
6455   verifyFormat("#define A           \\\n"
6456                "  void f() { int i; }",
6457                getLLVMStyleWithColumns(21));
6458   verifyFormat("#define A            \\\n"
6459                "  void f() {         \\\n"
6460                "    int i;           \\\n"
6461                "  }                  \\\n"
6462                "  int j;",
6463                getLLVMStyleWithColumns(22));
6464   verifyFormat("#define A             \\\n"
6465                "  void f() { int i; } \\\n"
6466                "  int j;",
6467                getLLVMStyleWithColumns(23));
6468 }
6469 
6470 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
6471   FormatStyle MergeInlineOnly = getLLVMStyle();
6472   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
6473   verifyFormat("class C {\n"
6474                "  int f() { return 42; }\n"
6475                "};",
6476                MergeInlineOnly);
6477   verifyFormat("int f() {\n"
6478                "  return 42;\n"
6479                "}",
6480                MergeInlineOnly);
6481 }
6482 
6483 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
6484   // Elaborate type variable declarations.
6485   verifyFormat("struct foo a = {bar};\nint n;");
6486   verifyFormat("class foo a = {bar};\nint n;");
6487   verifyFormat("union foo a = {bar};\nint n;");
6488 
6489   // Elaborate types inside function definitions.
6490   verifyFormat("struct foo f() {}\nint n;");
6491   verifyFormat("class foo f() {}\nint n;");
6492   verifyFormat("union foo f() {}\nint n;");
6493 
6494   // Templates.
6495   verifyFormat("template <class X> void f() {}\nint n;");
6496   verifyFormat("template <struct X> void f() {}\nint n;");
6497   verifyFormat("template <union X> void f() {}\nint n;");
6498 
6499   // Actual definitions...
6500   verifyFormat("struct {\n} n;");
6501   verifyFormat(
6502       "template <template <class T, class Y>, class Z> class X {\n} n;");
6503   verifyFormat("union Z {\n  int n;\n} x;");
6504   verifyFormat("class MACRO Z {\n} n;");
6505   verifyFormat("class MACRO(X) Z {\n} n;");
6506   verifyFormat("class __attribute__(X) Z {\n} n;");
6507   verifyFormat("class __declspec(X) Z {\n} n;");
6508   verifyFormat("class A##B##C {\n} n;");
6509   verifyFormat("class alignas(16) Z {\n} n;");
6510   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
6511   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
6512 
6513   // Redefinition from nested context:
6514   verifyFormat("class A::B::C {\n} n;");
6515 
6516   // Template definitions.
6517   verifyFormat(
6518       "template <typename F>\n"
6519       "Matcher(const Matcher<F> &Other,\n"
6520       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
6521       "                             !is_same<F, T>::value>::type * = 0)\n"
6522       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
6523 
6524   // FIXME: This is still incorrectly handled at the formatter side.
6525   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
6526   verifyFormat("int i = SomeFunction(a<b, a> b);");
6527 
6528   // FIXME:
6529   // This now gets parsed incorrectly as class definition.
6530   // verifyFormat("class A<int> f() {\n}\nint n;");
6531 
6532   // Elaborate types where incorrectly parsing the structural element would
6533   // break the indent.
6534   verifyFormat("if (true)\n"
6535                "  class X x;\n"
6536                "else\n"
6537                "  f();\n");
6538 
6539   // This is simply incomplete. Formatting is not important, but must not crash.
6540   verifyFormat("class A:");
6541 }
6542 
6543 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
6544   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
6545             format("#error Leave     all         white!!!!! space* alone!\n"));
6546   EXPECT_EQ(
6547       "#warning Leave     all         white!!!!! space* alone!\n",
6548       format("#warning Leave     all         white!!!!! space* alone!\n"));
6549   EXPECT_EQ("#error 1", format("  #  error   1"));
6550   EXPECT_EQ("#warning 1", format("  #  warning 1"));
6551 }
6552 
6553 TEST_F(FormatTest, FormatHashIfExpressions) {
6554   verifyFormat("#if AAAA && BBBB");
6555   // FIXME: Come up with a better indentation for #elif.
6556   verifyFormat(
6557       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
6558       "    defined(BBBBBBBB)\n"
6559       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
6560       "    defined(BBBBBBBB)\n"
6561       "#endif",
6562       getLLVMStyleWithColumns(65));
6563 }
6564 
6565 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
6566   FormatStyle AllowsMergedIf = getGoogleStyle();
6567   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
6568   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
6569   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
6570   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
6571   EXPECT_EQ("if (true) return 42;",
6572             format("if (true)\nreturn 42;", AllowsMergedIf));
6573   FormatStyle ShortMergedIf = AllowsMergedIf;
6574   ShortMergedIf.ColumnLimit = 25;
6575   verifyFormat("#define A \\\n"
6576                "  if (true) return 42;",
6577                ShortMergedIf);
6578   verifyFormat("#define A \\\n"
6579                "  f();    \\\n"
6580                "  if (true)\n"
6581                "#define B",
6582                ShortMergedIf);
6583   verifyFormat("#define A \\\n"
6584                "  f();    \\\n"
6585                "  if (true)\n"
6586                "g();",
6587                ShortMergedIf);
6588   verifyFormat("{\n"
6589                "#ifdef A\n"
6590                "  // Comment\n"
6591                "  if (true) continue;\n"
6592                "#endif\n"
6593                "  // Comment\n"
6594                "  if (true) continue;\n"
6595                "}",
6596                ShortMergedIf);
6597   ShortMergedIf.ColumnLimit = 29;
6598   verifyFormat("#define A                   \\\n"
6599                "  if (aaaaaaaaaa) return 1; \\\n"
6600                "  return 2;",
6601                ShortMergedIf);
6602   ShortMergedIf.ColumnLimit = 28;
6603   verifyFormat("#define A         \\\n"
6604                "  if (aaaaaaaaaa) \\\n"
6605                "    return 1;     \\\n"
6606                "  return 2;",
6607                ShortMergedIf);
6608 }
6609 
6610 TEST_F(FormatTest, BlockCommentsInControlLoops) {
6611   verifyFormat("if (0) /* a comment in a strange place */ {\n"
6612                "  f();\n"
6613                "}");
6614   verifyFormat("if (0) /* a comment in a strange place */ {\n"
6615                "  f();\n"
6616                "} /* another comment */ else /* comment #3 */ {\n"
6617                "  g();\n"
6618                "}");
6619   verifyFormat("while (0) /* a comment in a strange place */ {\n"
6620                "  f();\n"
6621                "}");
6622   verifyFormat("for (;;) /* a comment in a strange place */ {\n"
6623                "  f();\n"
6624                "}");
6625   verifyFormat("do /* a comment in a strange place */ {\n"
6626                "  f();\n"
6627                "} /* another comment */ while (0);");
6628 }
6629 
6630 TEST_F(FormatTest, BlockComments) {
6631   EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
6632             format("/* *//* */  /* */\n/* *//* */  /* */"));
6633   EXPECT_EQ("/* */ a /* */ b;", format("  /* */  a/* */  b;"));
6634   EXPECT_EQ("#define A /*123*/ \\\n"
6635             "  b\n"
6636             "/* */\n"
6637             "someCall(\n"
6638             "    parameter);",
6639             format("#define A /*123*/ b\n"
6640                    "/* */\n"
6641                    "someCall(parameter);",
6642                    getLLVMStyleWithColumns(15)));
6643 
6644   EXPECT_EQ("#define A\n"
6645             "/* */ someCall(\n"
6646             "    parameter);",
6647             format("#define A\n"
6648                    "/* */someCall(parameter);",
6649                    getLLVMStyleWithColumns(15)));
6650   EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
6651   EXPECT_EQ("/*\n"
6652             "*\n"
6653             " * aaaaaa\n"
6654             " * aaaaaa\n"
6655             "*/",
6656             format("/*\n"
6657                    "*\n"
6658                    " * aaaaaa aaaaaa\n"
6659                    "*/",
6660                    getLLVMStyleWithColumns(10)));
6661   EXPECT_EQ("/*\n"
6662             "**\n"
6663             "* aaaaaa\n"
6664             "*aaaaaa\n"
6665             "*/",
6666             format("/*\n"
6667                    "**\n"
6668                    "* aaaaaa aaaaaa\n"
6669                    "*/",
6670                    getLLVMStyleWithColumns(10)));
6671 
6672   FormatStyle NoBinPacking = getLLVMStyle();
6673   NoBinPacking.BinPackParameters = false;
6674   EXPECT_EQ("someFunction(1, /* comment 1 */\n"
6675             "             2, /* comment 2 */\n"
6676             "             3, /* comment 3 */\n"
6677             "             aaaa,\n"
6678             "             bbbb);",
6679             format("someFunction (1,   /* comment 1 */\n"
6680                    "                2, /* comment 2 */  \n"
6681                    "               3,   /* comment 3 */\n"
6682                    "aaaa, bbbb );",
6683                    NoBinPacking));
6684   verifyFormat(
6685       "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6686       "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6687   EXPECT_EQ(
6688       "bool aaaaaaaaaaaaa = /* trailing comment */\n"
6689       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6690       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
6691       format(
6692           "bool       aaaaaaaaaaaaa =       /* trailing comment */\n"
6693           "    aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa    ||\n"
6694           "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa   || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
6695   EXPECT_EQ(
6696       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
6697       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   /* comment */\n"
6698       "int cccccccccccccccccccccccccccccc;       /* comment */\n",
6699       format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
6700              "int      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
6701              "int    cccccccccccccccccccccccccccccc;  /* comment */\n"));
6702 
6703   verifyFormat("void f(int * /* unused */) {}");
6704 
6705   EXPECT_EQ("/*\n"
6706             " **\n"
6707             " */",
6708             format("/*\n"
6709                    " **\n"
6710                    " */"));
6711   EXPECT_EQ("/*\n"
6712             " *q\n"
6713             " */",
6714             format("/*\n"
6715                    " *q\n"
6716                    " */"));
6717   EXPECT_EQ("/*\n"
6718             " * q\n"
6719             " */",
6720             format("/*\n"
6721                    " * q\n"
6722                    " */"));
6723   EXPECT_EQ("/*\n"
6724             " **/",
6725             format("/*\n"
6726                    " **/"));
6727   EXPECT_EQ("/*\n"
6728             " ***/",
6729             format("/*\n"
6730                    " ***/"));
6731 }
6732 
6733 TEST_F(FormatTest, BlockCommentsInMacros) {
6734   EXPECT_EQ("#define A          \\\n"
6735             "  {                \\\n"
6736             "    /* one line */ \\\n"
6737             "    someCall();",
6738             format("#define A {        \\\n"
6739                    "  /* one line */   \\\n"
6740                    "  someCall();",
6741                    getLLVMStyleWithColumns(20)));
6742   EXPECT_EQ("#define A          \\\n"
6743             "  {                \\\n"
6744             "    /* previous */ \\\n"
6745             "    /* one line */ \\\n"
6746             "    someCall();",
6747             format("#define A {        \\\n"
6748                    "  /* previous */   \\\n"
6749                    "  /* one line */   \\\n"
6750                    "  someCall();",
6751                    getLLVMStyleWithColumns(20)));
6752 }
6753 
6754 TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
6755   EXPECT_EQ("a = {\n"
6756             "    1111 /*    */\n"
6757             "};",
6758             format("a = {1111 /*    */\n"
6759                    "};",
6760                    getLLVMStyleWithColumns(15)));
6761   EXPECT_EQ("a = {\n"
6762             "    1111 /*      */\n"
6763             "};",
6764             format("a = {1111 /*      */\n"
6765                    "};",
6766                    getLLVMStyleWithColumns(15)));
6767 
6768   // FIXME: The formatting is still wrong here.
6769   EXPECT_EQ("a = {\n"
6770             "    1111 /*      a\n"
6771             "            */\n"
6772             "};",
6773             format("a = {1111 /*      a */\n"
6774                    "};",
6775                    getLLVMStyleWithColumns(15)));
6776 }
6777 
6778 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
6779   // FIXME: This is not what we want...
6780   verifyFormat("{\n"
6781                "// a"
6782                "// b");
6783 }
6784 
6785 TEST_F(FormatTest, FormatStarDependingOnContext) {
6786   verifyFormat("void f(int *a);");
6787   verifyFormat("void f() { f(fint * b); }");
6788   verifyFormat("class A {\n  void f(int *a);\n};");
6789   verifyFormat("class A {\n  int *a;\n};");
6790   verifyFormat("namespace a {\n"
6791                "namespace b {\n"
6792                "class A {\n"
6793                "  void f() {}\n"
6794                "  int *a;\n"
6795                "};\n"
6796                "}\n"
6797                "}");
6798 }
6799 
6800 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
6801   verifyFormat("while");
6802   verifyFormat("operator");
6803 }
6804 
6805 //===----------------------------------------------------------------------===//
6806 // Objective-C tests.
6807 //===----------------------------------------------------------------------===//
6808 
6809 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
6810   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
6811   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
6812             format("-(NSUInteger)indexOfObject:(id)anObject;"));
6813   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
6814   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
6815   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
6816             format("-(NSInteger)Method3:(id)anObject;"));
6817   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
6818             format("-(NSInteger)Method4:(id)anObject;"));
6819   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
6820             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
6821   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
6822             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
6823   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
6824             "forAllCells:(BOOL)flag;",
6825             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
6826                    "forAllCells:(BOOL)flag;"));
6827 
6828   // Very long objectiveC method declaration.
6829   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
6830                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
6831   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
6832                "                    inRange:(NSRange)range\n"
6833                "                   outRange:(NSRange)out_range\n"
6834                "                  outRange1:(NSRange)out_range1\n"
6835                "                  outRange2:(NSRange)out_range2\n"
6836                "                  outRange3:(NSRange)out_range3\n"
6837                "                  outRange4:(NSRange)out_range4\n"
6838                "                  outRange5:(NSRange)out_range5\n"
6839                "                  outRange6:(NSRange)out_range6\n"
6840                "                  outRange7:(NSRange)out_range7\n"
6841                "                  outRange8:(NSRange)out_range8\n"
6842                "                  outRange9:(NSRange)out_range9;");
6843 
6844   // When the function name has to be wrapped.
6845   FormatStyle Style = getLLVMStyle();
6846   Style.IndentWrappedFunctionNames = false;
6847   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
6848                "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
6849                "           anotherName:(NSString)bbbbbbbbbbbbbb {\n"
6850                "}",
6851                Style);
6852   Style.IndentWrappedFunctionNames = true;
6853   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
6854                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
6855                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
6856                "}",
6857                Style);
6858 
6859   verifyFormat("- (int)sum:(vector<int>)numbers;");
6860   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
6861   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
6862   // protocol lists (but not for template classes):
6863   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
6864 
6865   verifyFormat("- (int (*)())foo:(int (*)())f;");
6866   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
6867 
6868   // If there's no return type (very rare in practice!), LLVM and Google style
6869   // agree.
6870   verifyFormat("- foo;");
6871   verifyFormat("- foo:(int)f;");
6872   verifyGoogleFormat("- foo:(int)foo;");
6873 }
6874 
6875 TEST_F(FormatTest, FormatObjCInterface) {
6876   verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
6877                "@public\n"
6878                "  int field1;\n"
6879                "@protected\n"
6880                "  int field2;\n"
6881                "@private\n"
6882                "  int field3;\n"
6883                "@package\n"
6884                "  int field4;\n"
6885                "}\n"
6886                "+ (id)init;\n"
6887                "@end");
6888 
6889   verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
6890                      " @public\n"
6891                      "  int field1;\n"
6892                      " @protected\n"
6893                      "  int field2;\n"
6894                      " @private\n"
6895                      "  int field3;\n"
6896                      " @package\n"
6897                      "  int field4;\n"
6898                      "}\n"
6899                      "+ (id)init;\n"
6900                      "@end");
6901 
6902   verifyFormat("@interface /* wait for it */ Foo\n"
6903                "+ (id)init;\n"
6904                "// Look, a comment!\n"
6905                "- (int)answerWith:(int)i;\n"
6906                "@end");
6907 
6908   verifyFormat("@interface Foo\n"
6909                "@end\n"
6910                "@interface Bar\n"
6911                "@end");
6912 
6913   verifyFormat("@interface Foo : Bar\n"
6914                "+ (id)init;\n"
6915                "@end");
6916 
6917   verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
6918                "+ (id)init;\n"
6919                "@end");
6920 
6921   verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
6922                      "+ (id)init;\n"
6923                      "@end");
6924 
6925   verifyFormat("@interface Foo (HackStuff)\n"
6926                "+ (id)init;\n"
6927                "@end");
6928 
6929   verifyFormat("@interface Foo ()\n"
6930                "+ (id)init;\n"
6931                "@end");
6932 
6933   verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
6934                "+ (id)init;\n"
6935                "@end");
6936 
6937   verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
6938                      "+ (id)init;\n"
6939                      "@end");
6940 
6941   verifyFormat("@interface Foo {\n"
6942                "  int _i;\n"
6943                "}\n"
6944                "+ (id)init;\n"
6945                "@end");
6946 
6947   verifyFormat("@interface Foo : Bar {\n"
6948                "  int _i;\n"
6949                "}\n"
6950                "+ (id)init;\n"
6951                "@end");
6952 
6953   verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
6954                "  int _i;\n"
6955                "}\n"
6956                "+ (id)init;\n"
6957                "@end");
6958 
6959   verifyFormat("@interface Foo (HackStuff) {\n"
6960                "  int _i;\n"
6961                "}\n"
6962                "+ (id)init;\n"
6963                "@end");
6964 
6965   verifyFormat("@interface Foo () {\n"
6966                "  int _i;\n"
6967                "}\n"
6968                "+ (id)init;\n"
6969                "@end");
6970 
6971   verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
6972                "  int _i;\n"
6973                "}\n"
6974                "+ (id)init;\n"
6975                "@end");
6976 
6977   FormatStyle OnePerLine = getGoogleStyle();
6978   OnePerLine.BinPackParameters = false;
6979   verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n"
6980                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6981                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6982                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6983                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
6984                "}",
6985                OnePerLine);
6986 }
6987 
6988 TEST_F(FormatTest, FormatObjCImplementation) {
6989   verifyFormat("@implementation Foo : NSObject {\n"
6990                "@public\n"
6991                "  int field1;\n"
6992                "@protected\n"
6993                "  int field2;\n"
6994                "@private\n"
6995                "  int field3;\n"
6996                "@package\n"
6997                "  int field4;\n"
6998                "}\n"
6999                "+ (id)init {\n}\n"
7000                "@end");
7001 
7002   verifyGoogleFormat("@implementation Foo : NSObject {\n"
7003                      " @public\n"
7004                      "  int field1;\n"
7005                      " @protected\n"
7006                      "  int field2;\n"
7007                      " @private\n"
7008                      "  int field3;\n"
7009                      " @package\n"
7010                      "  int field4;\n"
7011                      "}\n"
7012                      "+ (id)init {\n}\n"
7013                      "@end");
7014 
7015   verifyFormat("@implementation Foo\n"
7016                "+ (id)init {\n"
7017                "  if (true)\n"
7018                "    return nil;\n"
7019                "}\n"
7020                "// Look, a comment!\n"
7021                "- (int)answerWith:(int)i {\n"
7022                "  return i;\n"
7023                "}\n"
7024                "+ (int)answerWith:(int)i {\n"
7025                "  return i;\n"
7026                "}\n"
7027                "@end");
7028 
7029   verifyFormat("@implementation Foo\n"
7030                "@end\n"
7031                "@implementation Bar\n"
7032                "@end");
7033 
7034   EXPECT_EQ("@implementation Foo : Bar\n"
7035             "+ (id)init {\n}\n"
7036             "- (void)foo {\n}\n"
7037             "@end",
7038             format("@implementation Foo : Bar\n"
7039                    "+(id)init{}\n"
7040                    "-(void)foo{}\n"
7041                    "@end"));
7042 
7043   verifyFormat("@implementation Foo {\n"
7044                "  int _i;\n"
7045                "}\n"
7046                "+ (id)init {\n}\n"
7047                "@end");
7048 
7049   verifyFormat("@implementation Foo : Bar {\n"
7050                "  int _i;\n"
7051                "}\n"
7052                "+ (id)init {\n}\n"
7053                "@end");
7054 
7055   verifyFormat("@implementation Foo (HackStuff)\n"
7056                "+ (id)init {\n}\n"
7057                "@end");
7058   verifyFormat("@implementation ObjcClass\n"
7059                "- (void)method;\n"
7060                "{}\n"
7061                "@end");
7062 }
7063 
7064 TEST_F(FormatTest, FormatObjCProtocol) {
7065   verifyFormat("@protocol Foo\n"
7066                "@property(weak) id delegate;\n"
7067                "- (NSUInteger)numberOfThings;\n"
7068                "@end");
7069 
7070   verifyFormat("@protocol MyProtocol <NSObject>\n"
7071                "- (NSUInteger)numberOfThings;\n"
7072                "@end");
7073 
7074   verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
7075                      "- (NSUInteger)numberOfThings;\n"
7076                      "@end");
7077 
7078   verifyFormat("@protocol Foo;\n"
7079                "@protocol Bar;\n");
7080 
7081   verifyFormat("@protocol Foo\n"
7082                "@end\n"
7083                "@protocol Bar\n"
7084                "@end");
7085 
7086   verifyFormat("@protocol myProtocol\n"
7087                "- (void)mandatoryWithInt:(int)i;\n"
7088                "@optional\n"
7089                "- (void)optional;\n"
7090                "@required\n"
7091                "- (void)required;\n"
7092                "@optional\n"
7093                "@property(assign) int madProp;\n"
7094                "@end\n");
7095 
7096   verifyFormat("@property(nonatomic, assign, readonly)\n"
7097                "    int *looooooooooooooooooooooooooooongNumber;\n"
7098                "@property(nonatomic, assign, readonly)\n"
7099                "    NSString *looooooooooooooooooooooooooooongName;");
7100 
7101   verifyFormat("@implementation PR18406\n"
7102                "}\n"
7103                "@end");
7104 }
7105 
7106 TEST_F(FormatTest, FormatObjCMethodDeclarations) {
7107   verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
7108                "                   rect:(NSRect)theRect\n"
7109                "               interval:(float)theInterval {\n"
7110                "}");
7111   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7112                "          longKeyword:(NSRect)theRect\n"
7113                "    evenLongerKeyword:(float)theInterval\n"
7114                "                error:(NSError **)theError {\n"
7115                "}");
7116   verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
7117                "                         y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
7118                "    NS_DESIGNATED_INITIALIZER;",
7119                getLLVMStyleWithColumns(60));
7120 
7121   // Continuation indent width should win over aligning colons if the function
7122   // name is long.
7123   FormatStyle continuationStyle = getGoogleStyle();
7124   continuationStyle.ColumnLimit = 40;
7125   continuationStyle.IndentWrappedFunctionNames = true;
7126   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7127                "    dontAlignNamef:(NSRect)theRect {\n"
7128                "}",
7129                continuationStyle);
7130 
7131   // Make sure we don't break aligning for short parameter names.
7132   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7133                "       aShortf:(NSRect)theRect {\n"
7134                "}",
7135                continuationStyle);
7136 }
7137 
7138 TEST_F(FormatTest, FormatObjCMethodExpr) {
7139   verifyFormat("[foo bar:baz];");
7140   verifyFormat("return [foo bar:baz];");
7141   verifyFormat("return (a)[foo bar:baz];");
7142   verifyFormat("f([foo bar:baz]);");
7143   verifyFormat("f(2, [foo bar:baz]);");
7144   verifyFormat("f(2, a ? b : c);");
7145   verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
7146 
7147   // Unary operators.
7148   verifyFormat("int a = +[foo bar:baz];");
7149   verifyFormat("int a = -[foo bar:baz];");
7150   verifyFormat("int a = ![foo bar:baz];");
7151   verifyFormat("int a = ~[foo bar:baz];");
7152   verifyFormat("int a = ++[foo bar:baz];");
7153   verifyFormat("int a = --[foo bar:baz];");
7154   verifyFormat("int a = sizeof [foo bar:baz];");
7155   verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
7156   verifyFormat("int a = &[foo bar:baz];");
7157   verifyFormat("int a = *[foo bar:baz];");
7158   // FIXME: Make casts work, without breaking f()[4].
7159   // verifyFormat("int a = (int)[foo bar:baz];");
7160   // verifyFormat("return (int)[foo bar:baz];");
7161   // verifyFormat("(void)[foo bar:baz];");
7162   verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
7163 
7164   // Binary operators.
7165   verifyFormat("[foo bar:baz], [foo bar:baz];");
7166   verifyFormat("[foo bar:baz] = [foo bar:baz];");
7167   verifyFormat("[foo bar:baz] *= [foo bar:baz];");
7168   verifyFormat("[foo bar:baz] /= [foo bar:baz];");
7169   verifyFormat("[foo bar:baz] %= [foo bar:baz];");
7170   verifyFormat("[foo bar:baz] += [foo bar:baz];");
7171   verifyFormat("[foo bar:baz] -= [foo bar:baz];");
7172   verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
7173   verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
7174   verifyFormat("[foo bar:baz] &= [foo bar:baz];");
7175   verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
7176   verifyFormat("[foo bar:baz] |= [foo bar:baz];");
7177   verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
7178   verifyFormat("[foo bar:baz] || [foo bar:baz];");
7179   verifyFormat("[foo bar:baz] && [foo bar:baz];");
7180   verifyFormat("[foo bar:baz] | [foo bar:baz];");
7181   verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
7182   verifyFormat("[foo bar:baz] & [foo bar:baz];");
7183   verifyFormat("[foo bar:baz] == [foo bar:baz];");
7184   verifyFormat("[foo bar:baz] != [foo bar:baz];");
7185   verifyFormat("[foo bar:baz] >= [foo bar:baz];");
7186   verifyFormat("[foo bar:baz] <= [foo bar:baz];");
7187   verifyFormat("[foo bar:baz] > [foo bar:baz];");
7188   verifyFormat("[foo bar:baz] < [foo bar:baz];");
7189   verifyFormat("[foo bar:baz] >> [foo bar:baz];");
7190   verifyFormat("[foo bar:baz] << [foo bar:baz];");
7191   verifyFormat("[foo bar:baz] - [foo bar:baz];");
7192   verifyFormat("[foo bar:baz] + [foo bar:baz];");
7193   verifyFormat("[foo bar:baz] * [foo bar:baz];");
7194   verifyFormat("[foo bar:baz] / [foo bar:baz];");
7195   verifyFormat("[foo bar:baz] % [foo bar:baz];");
7196   // Whew!
7197 
7198   verifyFormat("return in[42];");
7199   verifyFormat("for (auto v : in[1]) {\n}");
7200   verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
7201   verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
7202   verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
7203   verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
7204   verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
7205   verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
7206                "}");
7207   verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
7208   verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
7209   verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
7210 
7211   verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
7212   verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
7213   verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
7214   verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
7215   verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
7216   verifyFormat("[button setAction:@selector(zoomOut:)];");
7217   verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
7218 
7219   verifyFormat("arr[[self indexForFoo:a]];");
7220   verifyFormat("throw [self errorFor:a];");
7221   verifyFormat("@throw [self errorFor:a];");
7222 
7223   verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
7224   verifyFormat("[(id)foo bar:(id) ? baz : quux];");
7225   verifyFormat("4 > 4 ? (id)a : (id)baz;");
7226 
7227   // This tests that the formatter doesn't break after "backing" but before ":",
7228   // which would be at 80 columns.
7229   verifyFormat(
7230       "void f() {\n"
7231       "  if ((self = [super initWithContentRect:contentRect\n"
7232       "                               styleMask:styleMask ?: otherMask\n"
7233       "                                 backing:NSBackingStoreBuffered\n"
7234       "                                   defer:YES]))");
7235 
7236   verifyFormat(
7237       "[foo checkThatBreakingAfterColonWorksOk:\n"
7238       "         [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
7239 
7240   verifyFormat("[myObj short:arg1 // Force line break\n"
7241                "          longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
7242                "    evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
7243                "                error:arg4];");
7244   verifyFormat(
7245       "void f() {\n"
7246       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
7247       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
7248       "                                     pos.width(), pos.height())\n"
7249       "                styleMask:NSBorderlessWindowMask\n"
7250       "                  backing:NSBackingStoreBuffered\n"
7251       "                    defer:NO]);\n"
7252       "}");
7253   verifyFormat(
7254       "void f() {\n"
7255       "  popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
7256       "      iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
7257       "                                 pos.width(), pos.height())\n"
7258       "                syeMask:NSBorderlessWindowMask\n"
7259       "                  bking:NSBackingStoreBuffered\n"
7260       "                    der:NO]);\n"
7261       "}",
7262       getLLVMStyleWithColumns(70));
7263   verifyFormat(
7264       "void f() {\n"
7265       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
7266       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
7267       "                                     pos.width(), pos.height())\n"
7268       "                styleMask:NSBorderlessWindowMask\n"
7269       "                  backing:NSBackingStoreBuffered\n"
7270       "                    defer:NO]);\n"
7271       "}",
7272       getChromiumStyle(FormatStyle::LK_Cpp));
7273   verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
7274                "                             with:contentsNativeView];");
7275 
7276   verifyFormat(
7277       "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
7278       "           owner:nillllll];");
7279 
7280   verifyFormat(
7281       "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
7282       "        forType:kBookmarkButtonDragType];");
7283 
7284   verifyFormat("[defaultCenter addObserver:self\n"
7285                "                  selector:@selector(willEnterFullscreen)\n"
7286                "                      name:kWillEnterFullscreenNotification\n"
7287                "                    object:nil];");
7288   verifyFormat("[image_rep drawInRect:drawRect\n"
7289                "             fromRect:NSZeroRect\n"
7290                "            operation:NSCompositeCopy\n"
7291                "             fraction:1.0\n"
7292                "       respectFlipped:NO\n"
7293                "                hints:nil];");
7294   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7295                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
7296   verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
7297                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
7298   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
7299                "    aaaaaaaaaaaaaaaaaaaaaa];");
7300   verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
7301                "        .aaaaaaaa];", // FIXME: Indentation seems off.
7302                getLLVMStyleWithColumns(60));
7303 
7304   verifyFormat(
7305       "scoped_nsobject<NSTextField> message(\n"
7306       "    // The frame will be fixed up when |-setMessageText:| is called.\n"
7307       "    [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
7308   verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
7309                "    aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
7310                "         aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
7311                "          aaaa:bbb];");
7312   verifyFormat("[self param:function( //\n"
7313                "                parameter)]");
7314   verifyFormat(
7315       "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7316       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7317       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
7318 
7319   // FIXME: This violates the column limit.
7320   verifyFormat(
7321       "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
7322       "    aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
7323       "                  aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];",
7324       getLLVMStyleWithColumns(60));
7325 
7326   // Variadic parameters.
7327   verifyFormat(
7328       "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
7329   verifyFormat(
7330       "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7331       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7332       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
7333   verifyFormat("[self // break\n"
7334                "      a:a\n"
7335                "    aaa:aaa];");
7336   verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
7337                "          [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
7338 }
7339 
7340 TEST_F(FormatTest, ObjCAt) {
7341   verifyFormat("@autoreleasepool");
7342   verifyFormat("@catch");
7343   verifyFormat("@class");
7344   verifyFormat("@compatibility_alias");
7345   verifyFormat("@defs");
7346   verifyFormat("@dynamic");
7347   verifyFormat("@encode");
7348   verifyFormat("@end");
7349   verifyFormat("@finally");
7350   verifyFormat("@implementation");
7351   verifyFormat("@import");
7352   verifyFormat("@interface");
7353   verifyFormat("@optional");
7354   verifyFormat("@package");
7355   verifyFormat("@private");
7356   verifyFormat("@property");
7357   verifyFormat("@protected");
7358   verifyFormat("@protocol");
7359   verifyFormat("@public");
7360   verifyFormat("@required");
7361   verifyFormat("@selector");
7362   verifyFormat("@synchronized");
7363   verifyFormat("@synthesize");
7364   verifyFormat("@throw");
7365   verifyFormat("@try");
7366 
7367   EXPECT_EQ("@interface", format("@ interface"));
7368 
7369   // The precise formatting of this doesn't matter, nobody writes code like
7370   // this.
7371   verifyFormat("@ /*foo*/ interface");
7372 }
7373 
7374 TEST_F(FormatTest, ObjCSnippets) {
7375   verifyFormat("@autoreleasepool {\n"
7376                "  foo();\n"
7377                "}");
7378   verifyFormat("@class Foo, Bar;");
7379   verifyFormat("@compatibility_alias AliasName ExistingClass;");
7380   verifyFormat("@dynamic textColor;");
7381   verifyFormat("char *buf1 = @encode(int *);");
7382   verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
7383   verifyFormat("char *buf1 = @encode(int **);");
7384   verifyFormat("Protocol *proto = @protocol(p1);");
7385   verifyFormat("SEL s = @selector(foo:);");
7386   verifyFormat("@synchronized(self) {\n"
7387                "  f();\n"
7388                "}");
7389 
7390   verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7391   verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7392 
7393   verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
7394   verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
7395   verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
7396   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7397                getMozillaStyle());
7398   verifyFormat("@property BOOL editable;", getMozillaStyle());
7399   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7400                getWebKitStyle());
7401   verifyFormat("@property BOOL editable;", getWebKitStyle());
7402 
7403   verifyFormat("@import foo.bar;\n"
7404                "@import baz;");
7405 }
7406 
7407 TEST_F(FormatTest, ObjCLiterals) {
7408   verifyFormat("@\"String\"");
7409   verifyFormat("@1");
7410   verifyFormat("@+4.8");
7411   verifyFormat("@-4");
7412   verifyFormat("@1LL");
7413   verifyFormat("@.5");
7414   verifyFormat("@'c'");
7415   verifyFormat("@true");
7416 
7417   verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
7418   verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
7419   verifyFormat("NSNumber *favoriteColor = @(Green);");
7420   verifyFormat("NSString *path = @(getenv(\"PATH\"));");
7421 
7422   verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
7423 }
7424 
7425 TEST_F(FormatTest, ObjCDictLiterals) {
7426   verifyFormat("@{");
7427   verifyFormat("@{}");
7428   verifyFormat("@{@\"one\" : @1}");
7429   verifyFormat("return @{@\"one\" : @1;");
7430   verifyFormat("@{@\"one\" : @1}");
7431 
7432   verifyFormat("@{@\"one\" : @{@2 : @1}}");
7433   verifyFormat("@{\n"
7434                "  @\"one\" : @{@2 : @1},\n"
7435                "}");
7436 
7437   verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
7438   verifyIncompleteFormat("[self setDict:@{}");
7439   verifyIncompleteFormat("[self setDict:@{@1 : @2}");
7440   verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
7441   verifyFormat(
7442       "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
7443   verifyFormat(
7444       "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
7445 
7446   verifyFormat("NSDictionary *d = @{\n"
7447                "  @\"nam\" : NSUserNam(),\n"
7448                "  @\"dte\" : [NSDate date],\n"
7449                "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
7450                "};");
7451   verifyFormat(
7452       "@{\n"
7453       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7454       "regularFont,\n"
7455       "};");
7456   verifyGoogleFormat(
7457       "@{\n"
7458       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7459       "regularFont,\n"
7460       "};");
7461   verifyFormat(
7462       "@{\n"
7463       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
7464       "      reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
7465       "};");
7466 
7467   // We should try to be robust in case someone forgets the "@".
7468   verifyFormat("NSDictionary *d = {\n"
7469                "  @\"nam\" : NSUserNam(),\n"
7470                "  @\"dte\" : [NSDate date],\n"
7471                "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
7472                "};");
7473   verifyFormat("NSMutableDictionary *dictionary =\n"
7474                "    [NSMutableDictionary dictionaryWithDictionary:@{\n"
7475                "      aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
7476                "      bbbbbbbbbbbbbbbbbb : bbbbb,\n"
7477                "      cccccccccccccccc : ccccccccccccccc\n"
7478                "    }];");
7479 }
7480 
7481 TEST_F(FormatTest, ObjCArrayLiterals) {
7482   verifyIncompleteFormat("@[");
7483   verifyFormat("@[]");
7484   verifyFormat(
7485       "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
7486   verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
7487   verifyFormat("NSArray *array = @[ [foo description] ];");
7488 
7489   verifyFormat(
7490       "NSArray *some_variable = @[\n"
7491       "  aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
7492       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7493       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7494       "  @\"aaaaaaaaaaaaaaaaa\"\n"
7495       "];");
7496   verifyFormat("NSArray *some_variable = @[\n"
7497                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7498                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7499                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7500                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7501                "];");
7502   verifyGoogleFormat("NSArray *some_variable = @[\n"
7503                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7504                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7505                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7506                      "  @\"aaaaaaaaaaaaaaaaa\"\n"
7507                      "];");
7508   verifyFormat("NSArray *array = @[\n"
7509                "  @\"a\",\n"
7510                "  @\"a\",\n" // Trailing comma -> one per line.
7511                "];");
7512 
7513   // We should try to be robust in case someone forgets the "@".
7514   verifyFormat("NSArray *some_variable = [\n"
7515                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7516                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7517                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7518                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7519                "];");
7520   verifyFormat(
7521       "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
7522       "                                             index:(NSUInteger)index\n"
7523       "                                nonDigitAttributes:\n"
7524       "                                    (NSDictionary *)noDigitAttributes;");
7525   verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
7526                "  NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
7527                "]];");
7528 }
7529 
7530 TEST_F(FormatTest, BreaksStringLiterals) {
7531   EXPECT_EQ("\"some text \"\n"
7532             "\"other\";",
7533             format("\"some text other\";", getLLVMStyleWithColumns(12)));
7534   EXPECT_EQ("\"some text \"\n"
7535             "\"other\";",
7536             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
7537   EXPECT_EQ(
7538       "#define A  \\\n"
7539       "  \"some \"  \\\n"
7540       "  \"text \"  \\\n"
7541       "  \"other\";",
7542       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
7543   EXPECT_EQ(
7544       "#define A  \\\n"
7545       "  \"so \"    \\\n"
7546       "  \"text \"  \\\n"
7547       "  \"other\";",
7548       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
7549 
7550   EXPECT_EQ("\"some text\"",
7551             format("\"some text\"", getLLVMStyleWithColumns(1)));
7552   EXPECT_EQ("\"some text\"",
7553             format("\"some text\"", getLLVMStyleWithColumns(11)));
7554   EXPECT_EQ("\"some \"\n"
7555             "\"text\"",
7556             format("\"some text\"", getLLVMStyleWithColumns(10)));
7557   EXPECT_EQ("\"some \"\n"
7558             "\"text\"",
7559             format("\"some text\"", getLLVMStyleWithColumns(7)));
7560   EXPECT_EQ("\"some\"\n"
7561             "\" tex\"\n"
7562             "\"t\"",
7563             format("\"some text\"", getLLVMStyleWithColumns(6)));
7564   EXPECT_EQ("\"some\"\n"
7565             "\" tex\"\n"
7566             "\" and\"",
7567             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
7568   EXPECT_EQ("\"some\"\n"
7569             "\"/tex\"\n"
7570             "\"/and\"",
7571             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
7572 
7573   EXPECT_EQ("variable =\n"
7574             "    \"long string \"\n"
7575             "    \"literal\";",
7576             format("variable = \"long string literal\";",
7577                    getLLVMStyleWithColumns(20)));
7578 
7579   EXPECT_EQ("variable = f(\n"
7580             "    \"long string \"\n"
7581             "    \"literal\",\n"
7582             "    short,\n"
7583             "    loooooooooooooooooooong);",
7584             format("variable = f(\"long string literal\", short, "
7585                    "loooooooooooooooooooong);",
7586                    getLLVMStyleWithColumns(20)));
7587 
7588   EXPECT_EQ(
7589       "f(g(\"long string \"\n"
7590       "    \"literal\"),\n"
7591       "  b);",
7592       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
7593   EXPECT_EQ("f(g(\"long string \"\n"
7594             "    \"literal\",\n"
7595             "    a),\n"
7596             "  b);",
7597             format("f(g(\"long string literal\", a), b);",
7598                    getLLVMStyleWithColumns(20)));
7599   EXPECT_EQ(
7600       "f(\"one two\".split(\n"
7601       "    variable));",
7602       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
7603   EXPECT_EQ("f(\"one two three four five six \"\n"
7604             "  \"seven\".split(\n"
7605             "      really_looooong_variable));",
7606             format("f(\"one two three four five six seven\"."
7607                    "split(really_looooong_variable));",
7608                    getLLVMStyleWithColumns(33)));
7609 
7610   EXPECT_EQ("f(\"some \"\n"
7611             "  \"text\",\n"
7612             "  other);",
7613             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
7614 
7615   // Only break as a last resort.
7616   verifyFormat(
7617       "aaaaaaaaaaaaaaaaaaaa(\n"
7618       "    aaaaaaaaaaaaaaaaaaaa,\n"
7619       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
7620 
7621   EXPECT_EQ("\"splitmea\"\n"
7622             "\"trandomp\"\n"
7623             "\"oint\"",
7624             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
7625 
7626   EXPECT_EQ("\"split/\"\n"
7627             "\"pathat/\"\n"
7628             "\"slashes\"",
7629             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7630 
7631   EXPECT_EQ("\"split/\"\n"
7632             "\"pathat/\"\n"
7633             "\"slashes\"",
7634             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7635   EXPECT_EQ("\"split at \"\n"
7636             "\"spaces/at/\"\n"
7637             "\"slashes.at.any$\"\n"
7638             "\"non-alphanumeric%\"\n"
7639             "\"1111111111characte\"\n"
7640             "\"rs\"",
7641             format("\"split at "
7642                    "spaces/at/"
7643                    "slashes.at."
7644                    "any$non-"
7645                    "alphanumeric%"
7646                    "1111111111characte"
7647                    "rs\"",
7648                    getLLVMStyleWithColumns(20)));
7649 
7650   // Verify that splitting the strings understands
7651   // Style::AlwaysBreakBeforeMultilineStrings.
7652   EXPECT_EQ(
7653       "aaaaaaaaaaaa(\n"
7654       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
7655       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
7656       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
7657              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7658              "aaaaaaaaaaaaaaaaaaaaaa\");",
7659              getGoogleStyle()));
7660   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7661             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
7662             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
7663                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7664                    "aaaaaaaaaaaaaaaaaaaaaa\";",
7665                    getGoogleStyle()));
7666   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7667             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7668             format("llvm::outs() << "
7669                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
7670                    "aaaaaaaaaaaaaaaaaaa\";"));
7671   EXPECT_EQ("ffff(\n"
7672             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7673             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7674             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
7675                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7676                    getGoogleStyle()));
7677 
7678   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
7679   AlignLeft.AlignEscapedNewlinesLeft = true;
7680   EXPECT_EQ("#define A \\\n"
7681             "  \"some \" \\\n"
7682             "  \"text \" \\\n"
7683             "  \"other\";",
7684             format("#define A \"some text other\";", AlignLeft));
7685 }
7686 
7687 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
7688   EXPECT_EQ(
7689       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7690       "(\n"
7691       "    \"x\t\");",
7692       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7693              "aaaaaaa("
7694              "\"x\t\");"));
7695 }
7696 
7697 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
7698   EXPECT_EQ(
7699       "u8\"utf8 string \"\n"
7700       "u8\"literal\";",
7701       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
7702   EXPECT_EQ(
7703       "u\"utf16 string \"\n"
7704       "u\"literal\";",
7705       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
7706   EXPECT_EQ(
7707       "U\"utf32 string \"\n"
7708       "U\"literal\";",
7709       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
7710   EXPECT_EQ("L\"wide string \"\n"
7711             "L\"literal\";",
7712             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
7713   EXPECT_EQ("@\"NSString \"\n"
7714             "@\"literal\";",
7715             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
7716 
7717   // This input makes clang-format try to split the incomplete unicode escape
7718   // sequence, which used to lead to a crasher.
7719   verifyNoCrash(
7720       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
7721       getLLVMStyleWithColumns(60));
7722 }
7723 
7724 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
7725   FormatStyle Style = getGoogleStyleWithColumns(15);
7726   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
7727   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
7728   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
7729   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
7730   EXPECT_EQ("u8R\"x(raw literal)x\";",
7731             format("u8R\"x(raw literal)x\";", Style));
7732 }
7733 
7734 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
7735   FormatStyle Style = getLLVMStyleWithColumns(20);
7736   EXPECT_EQ(
7737       "_T(\"aaaaaaaaaaaaaa\")\n"
7738       "_T(\"aaaaaaaaaaaaaa\")\n"
7739       "_T(\"aaaaaaaaaaaa\")",
7740       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
7741   EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
7742             "     _T(\"aaaaaa\"),\n"
7743             "  z);",
7744             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
7745 
7746   // FIXME: Handle embedded spaces in one iteration.
7747   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
7748   //            "_T(\"aaaaaaaaaaaaa\")\n"
7749   //            "_T(\"aaaaaaaaaaaaa\")\n"
7750   //            "_T(\"a\")",
7751   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7752   //                   getLLVMStyleWithColumns(20)));
7753   EXPECT_EQ(
7754       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7755       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
7756   EXPECT_EQ("f(\n"
7757             "#if !TEST\n"
7758             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7759             "#endif\n"
7760             "    );",
7761             format("f(\n"
7762                    "#if !TEST\n"
7763                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7764                    "#endif\n"
7765                    ");"));
7766   EXPECT_EQ("f(\n"
7767             "\n"
7768             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
7769             format("f(\n"
7770                    "\n"
7771                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
7772 }
7773 
7774 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
7775   EXPECT_EQ(
7776       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7777       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7778       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7779       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7780              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7781              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
7782 }
7783 
7784 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
7785   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
7786             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
7787   EXPECT_EQ("fffffffffff(g(R\"x(\n"
7788             "multiline raw string literal xxxxxxxxxxxxxx\n"
7789             ")x\",\n"
7790             "              a),\n"
7791             "            b);",
7792             format("fffffffffff(g(R\"x(\n"
7793                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7794                    ")x\", a), b);",
7795                    getGoogleStyleWithColumns(20)));
7796   EXPECT_EQ("fffffffffff(\n"
7797             "    g(R\"x(qqq\n"
7798             "multiline raw string literal xxxxxxxxxxxxxx\n"
7799             ")x\",\n"
7800             "      a),\n"
7801             "    b);",
7802             format("fffffffffff(g(R\"x(qqq\n"
7803                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7804                    ")x\", a), b);",
7805                    getGoogleStyleWithColumns(20)));
7806 
7807   EXPECT_EQ("fffffffffff(R\"x(\n"
7808             "multiline raw string literal xxxxxxxxxxxxxx\n"
7809             ")x\");",
7810             format("fffffffffff(R\"x(\n"
7811                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7812                    ")x\");",
7813                    getGoogleStyleWithColumns(20)));
7814   EXPECT_EQ("fffffffffff(R\"x(\n"
7815             "multiline raw string literal xxxxxxxxxxxxxx\n"
7816             ")x\" + bbbbbb);",
7817             format("fffffffffff(R\"x(\n"
7818                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7819                    ")x\" +   bbbbbb);",
7820                    getGoogleStyleWithColumns(20)));
7821   EXPECT_EQ("fffffffffff(\n"
7822             "    R\"x(\n"
7823             "multiline raw string literal xxxxxxxxxxxxxx\n"
7824             ")x\" +\n"
7825             "    bbbbbb);",
7826             format("fffffffffff(\n"
7827                    " R\"x(\n"
7828                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7829                    ")x\" + bbbbbb);",
7830                    getGoogleStyleWithColumns(20)));
7831 }
7832 
7833 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
7834   verifyFormat("string a = \"unterminated;");
7835   EXPECT_EQ("function(\"unterminated,\n"
7836             "         OtherParameter);",
7837             format("function(  \"unterminated,\n"
7838                    "    OtherParameter);"));
7839 }
7840 
7841 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
7842   FormatStyle Style = getLLVMStyle();
7843   Style.Standard = FormatStyle::LS_Cpp03;
7844   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
7845             format("#define x(_a) printf(\"foo\"_a);", Style));
7846 }
7847 
7848 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
7849 
7850 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
7851   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
7852             "             \"ddeeefff\");",
7853             format("someFunction(\"aaabbbcccdddeeefff\");",
7854                    getLLVMStyleWithColumns(25)));
7855   EXPECT_EQ("someFunction1234567890(\n"
7856             "    \"aaabbbcccdddeeefff\");",
7857             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7858                    getLLVMStyleWithColumns(26)));
7859   EXPECT_EQ("someFunction1234567890(\n"
7860             "    \"aaabbbcccdddeeeff\"\n"
7861             "    \"f\");",
7862             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7863                    getLLVMStyleWithColumns(25)));
7864   EXPECT_EQ("someFunction1234567890(\n"
7865             "    \"aaabbbcccdddeeeff\"\n"
7866             "    \"f\");",
7867             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7868                    getLLVMStyleWithColumns(24)));
7869   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7870             "             \"ddde \"\n"
7871             "             \"efff\");",
7872             format("someFunction(\"aaabbbcc ddde efff\");",
7873                    getLLVMStyleWithColumns(25)));
7874   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
7875             "             \"ddeeefff\");",
7876             format("someFunction(\"aaabbbccc ddeeefff\");",
7877                    getLLVMStyleWithColumns(25)));
7878   EXPECT_EQ("someFunction1234567890(\n"
7879             "    \"aaabb \"\n"
7880             "    \"cccdddeeefff\");",
7881             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
7882                    getLLVMStyleWithColumns(25)));
7883   EXPECT_EQ("#define A          \\\n"
7884             "  string s =       \\\n"
7885             "      \"123456789\"  \\\n"
7886             "      \"0\";         \\\n"
7887             "  int i;",
7888             format("#define A string s = \"1234567890\"; int i;",
7889                    getLLVMStyleWithColumns(20)));
7890   // FIXME: Put additional penalties on breaking at non-whitespace locations.
7891   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7892             "             \"dddeeeff\"\n"
7893             "             \"f\");",
7894             format("someFunction(\"aaabbbcc dddeeefff\");",
7895                    getLLVMStyleWithColumns(25)));
7896 }
7897 
7898 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
7899   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
7900   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
7901   EXPECT_EQ("\"test\"\n"
7902             "\"\\n\"",
7903             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
7904   EXPECT_EQ("\"tes\\\\\"\n"
7905             "\"n\"",
7906             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
7907   EXPECT_EQ("\"\\\\\\\\\"\n"
7908             "\"\\n\"",
7909             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
7910   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
7911   EXPECT_EQ("\"\\uff01\"\n"
7912             "\"test\"",
7913             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
7914   EXPECT_EQ("\"\\Uff01ff02\"",
7915             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
7916   EXPECT_EQ("\"\\x000000000001\"\n"
7917             "\"next\"",
7918             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
7919   EXPECT_EQ("\"\\x000000000001next\"",
7920             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
7921   EXPECT_EQ("\"\\x000000000001\"",
7922             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
7923   EXPECT_EQ("\"test\"\n"
7924             "\"\\000000\"\n"
7925             "\"000001\"",
7926             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
7927   EXPECT_EQ("\"test\\000\"\n"
7928             "\"00000000\"\n"
7929             "\"1\"",
7930             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
7931 }
7932 
7933 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
7934   verifyFormat("void f() {\n"
7935                "  return g() {}\n"
7936                "  void h() {}");
7937   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
7938                "g();\n"
7939                "}");
7940 }
7941 
7942 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
7943   verifyFormat(
7944       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
7945 }
7946 
7947 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
7948   verifyFormat("class X {\n"
7949                "  void f() {\n"
7950                "  }\n"
7951                "};",
7952                getLLVMStyleWithColumns(12));
7953 }
7954 
7955 TEST_F(FormatTest, ConfigurableIndentWidth) {
7956   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
7957   EightIndent.IndentWidth = 8;
7958   EightIndent.ContinuationIndentWidth = 8;
7959   verifyFormat("void f() {\n"
7960                "        someFunction();\n"
7961                "        if (true) {\n"
7962                "                f();\n"
7963                "        }\n"
7964                "}",
7965                EightIndent);
7966   verifyFormat("class X {\n"
7967                "        void f() {\n"
7968                "        }\n"
7969                "};",
7970                EightIndent);
7971   verifyFormat("int x[] = {\n"
7972                "        call(),\n"
7973                "        call()};",
7974                EightIndent);
7975 }
7976 
7977 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
7978   verifyFormat("double\n"
7979                "f();",
7980                getLLVMStyleWithColumns(8));
7981 }
7982 
7983 TEST_F(FormatTest, ConfigurableUseOfTab) {
7984   FormatStyle Tab = getLLVMStyleWithColumns(42);
7985   Tab.IndentWidth = 8;
7986   Tab.UseTab = FormatStyle::UT_Always;
7987   Tab.AlignEscapedNewlinesLeft = true;
7988 
7989   EXPECT_EQ("if (aaaaaaaa && // q\n"
7990             "    bb)\t\t// w\n"
7991             "\t;",
7992             format("if (aaaaaaaa &&// q\n"
7993                    "bb)// w\n"
7994                    ";",
7995                    Tab));
7996   EXPECT_EQ("if (aaa && bbb) // w\n"
7997             "\t;",
7998             format("if(aaa&&bbb)// w\n"
7999                    ";",
8000                    Tab));
8001 
8002   verifyFormat("class X {\n"
8003                "\tvoid f() {\n"
8004                "\t\tsomeFunction(parameter1,\n"
8005                "\t\t\t     parameter2);\n"
8006                "\t}\n"
8007                "};",
8008                Tab);
8009   verifyFormat("#define A                        \\\n"
8010                "\tvoid f() {               \\\n"
8011                "\t\tsomeFunction(    \\\n"
8012                "\t\t    parameter1,  \\\n"
8013                "\t\t    parameter2); \\\n"
8014                "\t}",
8015                Tab);
8016 
8017   Tab.TabWidth = 4;
8018   Tab.IndentWidth = 8;
8019   verifyFormat("class TabWidth4Indent8 {\n"
8020                "\t\tvoid f() {\n"
8021                "\t\t\t\tsomeFunction(parameter1,\n"
8022                "\t\t\t\t\t\t\t parameter2);\n"
8023                "\t\t}\n"
8024                "};",
8025                Tab);
8026 
8027   Tab.TabWidth = 4;
8028   Tab.IndentWidth = 4;
8029   verifyFormat("class TabWidth4Indent4 {\n"
8030                "\tvoid f() {\n"
8031                "\t\tsomeFunction(parameter1,\n"
8032                "\t\t\t\t\t parameter2);\n"
8033                "\t}\n"
8034                "};",
8035                Tab);
8036 
8037   Tab.TabWidth = 8;
8038   Tab.IndentWidth = 4;
8039   verifyFormat("class TabWidth8Indent4 {\n"
8040                "    void f() {\n"
8041                "\tsomeFunction(parameter1,\n"
8042                "\t\t     parameter2);\n"
8043                "    }\n"
8044                "};",
8045                Tab);
8046 
8047   Tab.TabWidth = 8;
8048   Tab.IndentWidth = 8;
8049   EXPECT_EQ("/*\n"
8050             "\t      a\t\tcomment\n"
8051             "\t      in multiple lines\n"
8052             "       */",
8053             format("   /*\t \t \n"
8054                    " \t \t a\t\tcomment\t \t\n"
8055                    " \t \t in multiple lines\t\n"
8056                    " \t  */",
8057                    Tab));
8058 
8059   Tab.UseTab = FormatStyle::UT_ForIndentation;
8060   verifyFormat("{\n"
8061                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8062                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8063                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8064                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8065                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8066                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8067                "};",
8068                Tab);
8069   verifyFormat("enum A {\n"
8070                "\ta1, // Force multiple lines\n"
8071                "\ta2,\n"
8072                "\ta3\n"
8073                "};",
8074                Tab);
8075   EXPECT_EQ("if (aaaaaaaa && // q\n"
8076             "    bb)         // w\n"
8077             "\t;",
8078             format("if (aaaaaaaa &&// q\n"
8079                    "bb)// w\n"
8080                    ";",
8081                    Tab));
8082   verifyFormat("class X {\n"
8083                "\tvoid f() {\n"
8084                "\t\tsomeFunction(parameter1,\n"
8085                "\t\t             parameter2);\n"
8086                "\t}\n"
8087                "};",
8088                Tab);
8089   verifyFormat("{\n"
8090                "\tQ({\n"
8091                "\t\tint a;\n"
8092                "\t\tsomeFunction(aaaaaaaa,\n"
8093                "\t\t             bbbbbbb);\n"
8094                "\t}, p);\n"
8095                "}",
8096                Tab);
8097   EXPECT_EQ("{\n"
8098             "\t/* aaaa\n"
8099             "\t   bbbb */\n"
8100             "}",
8101             format("{\n"
8102                    "/* aaaa\n"
8103                    "   bbbb */\n"
8104                    "}",
8105                    Tab));
8106   EXPECT_EQ("{\n"
8107             "\t/*\n"
8108             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8109             "\t  bbbbbbbbbbbbb\n"
8110             "\t*/\n"
8111             "}",
8112             format("{\n"
8113                    "/*\n"
8114                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8115                    "*/\n"
8116                    "}",
8117                    Tab));
8118   EXPECT_EQ("{\n"
8119             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8120             "\t// bbbbbbbbbbbbb\n"
8121             "}",
8122             format("{\n"
8123                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8124                    "}",
8125                    Tab));
8126   EXPECT_EQ("{\n"
8127             "\t/*\n"
8128             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8129             "\t  bbbbbbbbbbbbb\n"
8130             "\t*/\n"
8131             "}",
8132             format("{\n"
8133                    "\t/*\n"
8134                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8135                    "\t*/\n"
8136                    "}",
8137                    Tab));
8138   EXPECT_EQ("{\n"
8139             "\t/*\n"
8140             "\n"
8141             "\t*/\n"
8142             "}",
8143             format("{\n"
8144                    "\t/*\n"
8145                    "\n"
8146                    "\t*/\n"
8147                    "}",
8148                    Tab));
8149   EXPECT_EQ("{\n"
8150             "\t/*\n"
8151             " asdf\n"
8152             "\t*/\n"
8153             "}",
8154             format("{\n"
8155                    "\t/*\n"
8156                    " asdf\n"
8157                    "\t*/\n"
8158                    "}",
8159                    Tab));
8160 
8161   Tab.UseTab = FormatStyle::UT_Never;
8162   EXPECT_EQ("/*\n"
8163             "              a\t\tcomment\n"
8164             "              in multiple lines\n"
8165             "       */",
8166             format("   /*\t \t \n"
8167                    " \t \t a\t\tcomment\t \t\n"
8168                    " \t \t in multiple lines\t\n"
8169                    " \t  */",
8170                    Tab));
8171   EXPECT_EQ("/* some\n"
8172             "   comment */",
8173             format(" \t \t /* some\n"
8174                    " \t \t    comment */",
8175                    Tab));
8176   EXPECT_EQ("int a; /* some\n"
8177             "   comment */",
8178             format(" \t \t int a; /* some\n"
8179                    " \t \t    comment */",
8180                    Tab));
8181 
8182   EXPECT_EQ("int a; /* some\n"
8183             "comment */",
8184             format(" \t \t int\ta; /* some\n"
8185                    " \t \t    comment */",
8186                    Tab));
8187   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8188             "    comment */",
8189             format(" \t \t f(\"\t\t\"); /* some\n"
8190                    " \t \t    comment */",
8191                    Tab));
8192   EXPECT_EQ("{\n"
8193             "  /*\n"
8194             "   * Comment\n"
8195             "   */\n"
8196             "  int i;\n"
8197             "}",
8198             format("{\n"
8199                    "\t/*\n"
8200                    "\t * Comment\n"
8201                    "\t */\n"
8202                    "\t int i;\n"
8203                    "}"));
8204 }
8205 
8206 TEST_F(FormatTest, CalculatesOriginalColumn) {
8207   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8208             "q\"; /* some\n"
8209             "       comment */",
8210             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8211                    "q\"; /* some\n"
8212                    "       comment */",
8213                    getLLVMStyle()));
8214   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8215             "/* some\n"
8216             "   comment */",
8217             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8218                    " /* some\n"
8219                    "    comment */",
8220                    getLLVMStyle()));
8221   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8222             "qqq\n"
8223             "/* some\n"
8224             "   comment */",
8225             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8226                    "qqq\n"
8227                    " /* some\n"
8228                    "    comment */",
8229                    getLLVMStyle()));
8230   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8231             "wwww; /* some\n"
8232             "         comment */",
8233             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8234                    "wwww; /* some\n"
8235                    "         comment */",
8236                    getLLVMStyle()));
8237 }
8238 
8239 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
8240   FormatStyle NoSpace = getLLVMStyle();
8241   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
8242 
8243   verifyFormat("while(true)\n"
8244                "  continue;",
8245                NoSpace);
8246   verifyFormat("for(;;)\n"
8247                "  continue;",
8248                NoSpace);
8249   verifyFormat("if(true)\n"
8250                "  f();\n"
8251                "else if(true)\n"
8252                "  f();",
8253                NoSpace);
8254   verifyFormat("do {\n"
8255                "  do_something();\n"
8256                "} while(something());",
8257                NoSpace);
8258   verifyFormat("switch(x) {\n"
8259                "default:\n"
8260                "  break;\n"
8261                "}",
8262                NoSpace);
8263   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
8264   verifyFormat("size_t x = sizeof(x);", NoSpace);
8265   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
8266   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
8267   verifyFormat("alignas(128) char a[128];", NoSpace);
8268   verifyFormat("size_t x = alignof(MyType);", NoSpace);
8269   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
8270   verifyFormat("int f() throw(Deprecated);", NoSpace);
8271   verifyFormat("typedef void (*cb)(int);", NoSpace);
8272 
8273   FormatStyle Space = getLLVMStyle();
8274   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
8275 
8276   verifyFormat("int f ();", Space);
8277   verifyFormat("void f (int a, T b) {\n"
8278                "  while (true)\n"
8279                "    continue;\n"
8280                "}",
8281                Space);
8282   verifyFormat("if (true)\n"
8283                "  f ();\n"
8284                "else if (true)\n"
8285                "  f ();",
8286                Space);
8287   verifyFormat("do {\n"
8288                "  do_something ();\n"
8289                "} while (something ());",
8290                Space);
8291   verifyFormat("switch (x) {\n"
8292                "default:\n"
8293                "  break;\n"
8294                "}",
8295                Space);
8296   verifyFormat("A::A () : a (1) {}", Space);
8297   verifyFormat("void f () __attribute__ ((asdf));", Space);
8298   verifyFormat("*(&a + 1);\n"
8299                "&((&a)[1]);\n"
8300                "a[(b + c) * d];\n"
8301                "(((a + 1) * 2) + 3) * 4;",
8302                Space);
8303   verifyFormat("#define A(x) x", Space);
8304   verifyFormat("#define A (x) x", Space);
8305   verifyFormat("#if defined(x)\n"
8306                "#endif",
8307                Space);
8308   verifyFormat("auto i = std::make_unique<int> (5);", Space);
8309   verifyFormat("size_t x = sizeof (x);", Space);
8310   verifyFormat("auto f (int x) -> decltype (x);", Space);
8311   verifyFormat("int f (T x) noexcept (x.create ());", Space);
8312   verifyFormat("alignas (128) char a[128];", Space);
8313   verifyFormat("size_t x = alignof (MyType);", Space);
8314   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
8315   verifyFormat("int f () throw (Deprecated);", Space);
8316   verifyFormat("typedef void (*cb) (int);", Space);
8317 }
8318 
8319 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
8320   FormatStyle Spaces = getLLVMStyle();
8321 
8322   Spaces.SpacesInParentheses = true;
8323   verifyFormat("call( x, y, z );", Spaces);
8324   verifyFormat("call();", Spaces);
8325   verifyFormat("std::function<void( int, int )> callback;", Spaces);
8326   verifyFormat("while ( (bool)1 )\n"
8327                "  continue;",
8328                Spaces);
8329   verifyFormat("for ( ;; )\n"
8330                "  continue;",
8331                Spaces);
8332   verifyFormat("if ( true )\n"
8333                "  f();\n"
8334                "else if ( true )\n"
8335                "  f();",
8336                Spaces);
8337   verifyFormat("do {\n"
8338                "  do_something( (int)i );\n"
8339                "} while ( something() );",
8340                Spaces);
8341   verifyFormat("switch ( x ) {\n"
8342                "default:\n"
8343                "  break;\n"
8344                "}",
8345                Spaces);
8346 
8347   Spaces.SpacesInParentheses = false;
8348   Spaces.SpacesInCStyleCastParentheses = true;
8349   verifyFormat("Type *A = ( Type * )P;", Spaces);
8350   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
8351   verifyFormat("x = ( int32 )y;", Spaces);
8352   verifyFormat("int a = ( int )(2.0f);", Spaces);
8353   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
8354   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
8355   verifyFormat("#define x (( int )-1)", Spaces);
8356 
8357   // Run the first set of tests again with:
8358   Spaces.SpacesInParentheses = false, Spaces.SpaceInEmptyParentheses = true;
8359   Spaces.SpacesInCStyleCastParentheses = true;
8360   verifyFormat("call(x, y, z);", Spaces);
8361   verifyFormat("call( );", Spaces);
8362   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8363   verifyFormat("while (( bool )1)\n"
8364                "  continue;",
8365                Spaces);
8366   verifyFormat("for (;;)\n"
8367                "  continue;",
8368                Spaces);
8369   verifyFormat("if (true)\n"
8370                "  f( );\n"
8371                "else if (true)\n"
8372                "  f( );",
8373                Spaces);
8374   verifyFormat("do {\n"
8375                "  do_something(( int )i);\n"
8376                "} while (something( ));",
8377                Spaces);
8378   verifyFormat("switch (x) {\n"
8379                "default:\n"
8380                "  break;\n"
8381                "}",
8382                Spaces);
8383 
8384   // Run the first set of tests again with:
8385   Spaces.SpaceAfterCStyleCast = true;
8386   verifyFormat("call(x, y, z);", Spaces);
8387   verifyFormat("call( );", Spaces);
8388   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8389   verifyFormat("while (( bool ) 1)\n"
8390                "  continue;",
8391                Spaces);
8392   verifyFormat("for (;;)\n"
8393                "  continue;",
8394                Spaces);
8395   verifyFormat("if (true)\n"
8396                "  f( );\n"
8397                "else if (true)\n"
8398                "  f( );",
8399                Spaces);
8400   verifyFormat("do {\n"
8401                "  do_something(( int ) i);\n"
8402                "} while (something( ));",
8403                Spaces);
8404   verifyFormat("switch (x) {\n"
8405                "default:\n"
8406                "  break;\n"
8407                "}",
8408                Spaces);
8409 
8410   // Run subset of tests again with:
8411   Spaces.SpacesInCStyleCastParentheses = false;
8412   Spaces.SpaceAfterCStyleCast = true;
8413   verifyFormat("while ((bool) 1)\n"
8414                "  continue;",
8415                Spaces);
8416   verifyFormat("do {\n"
8417                "  do_something((int) i);\n"
8418                "} while (something( ));",
8419                Spaces);
8420 }
8421 
8422 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
8423   verifyFormat("int a[5];");
8424   verifyFormat("a[3] += 42;");
8425 
8426   FormatStyle Spaces = getLLVMStyle();
8427   Spaces.SpacesInSquareBrackets = true;
8428   // Lambdas unchanged.
8429   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
8430   verifyFormat("return [i, args...] {};", Spaces);
8431 
8432   // Not lambdas.
8433   verifyFormat("int a[ 5 ];", Spaces);
8434   verifyFormat("a[ 3 ] += 42;", Spaces);
8435   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
8436   verifyFormat("double &operator[](int i) { return 0; }\n"
8437                "int i;",
8438                Spaces);
8439   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
8440   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
8441   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
8442 }
8443 
8444 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
8445   verifyFormat("int a = 5;");
8446   verifyFormat("a += 42;");
8447   verifyFormat("a or_eq 8;");
8448 
8449   FormatStyle Spaces = getLLVMStyle();
8450   Spaces.SpaceBeforeAssignmentOperators = false;
8451   verifyFormat("int a= 5;", Spaces);
8452   verifyFormat("a+= 42;", Spaces);
8453   verifyFormat("a or_eq 8;", Spaces);
8454 }
8455 
8456 TEST_F(FormatTest, AlignConsecutiveAssignments) {
8457   FormatStyle Alignment = getLLVMStyle();
8458   Alignment.AlignConsecutiveAssignments = false;
8459   verifyFormat("int a = 5;\n"
8460                "int oneTwoThree = 123;",
8461                Alignment);
8462   verifyFormat("int a = 5;\n"
8463                "int oneTwoThree = 123;",
8464                Alignment);
8465 
8466   Alignment.AlignConsecutiveAssignments = true;
8467   verifyFormat("int a           = 5;\n"
8468                "int oneTwoThree = 123;",
8469                Alignment);
8470   verifyFormat("int a           = method();\n"
8471                "int oneTwoThree = 133;",
8472                Alignment);
8473   verifyFormat("a &= 5;\n"
8474                "bcd *= 5;\n"
8475                "ghtyf += 5;\n"
8476                "dvfvdb -= 5;\n"
8477                "a /= 5;\n"
8478                "vdsvsv %= 5;\n"
8479                "sfdbddfbdfbb ^= 5;\n"
8480                "dvsdsv |= 5;\n"
8481                "int dsvvdvsdvvv = 123;",
8482                Alignment);
8483   verifyFormat("int i = 1, j = 10;\n"
8484                "something = 2000;",
8485                Alignment);
8486   verifyFormat("something = 2000;\n"
8487                "int i = 1, j = 10;\n",
8488                Alignment);
8489   verifyFormat("something = 2000;\n"
8490                "another   = 911;\n"
8491                "int i = 1, j = 10;\n"
8492                "oneMore = 1;\n"
8493                "i       = 2;",
8494                Alignment);
8495   verifyFormat("int a   = 5;\n"
8496                "int one = 1;\n"
8497                "method();\n"
8498                "int oneTwoThree = 123;\n"
8499                "int oneTwo      = 12;",
8500                Alignment);
8501   verifyFormat("int oneTwoThree = 123; // comment\n"
8502                "int oneTwo      = 12;  // comment",
8503                Alignment);
8504   EXPECT_EQ("int a = 5;\n"
8505             "\n"
8506             "int oneTwoThree = 123;",
8507             format("int a       = 5;\n"
8508                    "\n"
8509                    "int oneTwoThree= 123;",
8510                    Alignment));
8511   EXPECT_EQ("int a   = 5;\n"
8512             "int one = 1;\n"
8513             "\n"
8514             "int oneTwoThree = 123;",
8515             format("int a = 5;\n"
8516                    "int one = 1;\n"
8517                    "\n"
8518                    "int oneTwoThree = 123;",
8519                    Alignment));
8520   EXPECT_EQ("int a   = 5;\n"
8521             "int one = 1;\n"
8522             "\n"
8523             "int oneTwoThree = 123;\n"
8524             "int oneTwo      = 12;",
8525             format("int a = 5;\n"
8526                    "int one = 1;\n"
8527                    "\n"
8528                    "int oneTwoThree = 123;\n"
8529                    "int oneTwo = 12;",
8530                    Alignment));
8531   Alignment.AlignEscapedNewlinesLeft = true;
8532   verifyFormat("#define A               \\\n"
8533                "  int aaaa       = 12;  \\\n"
8534                "  int b          = 23;  \\\n"
8535                "  int ccc        = 234; \\\n"
8536                "  int dddddddddd = 2345;",
8537                Alignment);
8538   Alignment.AlignEscapedNewlinesLeft = false;
8539   verifyFormat("#define A                                                      "
8540                "                \\\n"
8541                "  int aaaa       = 12;                                         "
8542                "                \\\n"
8543                "  int b          = 23;                                         "
8544                "                \\\n"
8545                "  int ccc        = 234;                                        "
8546                "                \\\n"
8547                "  int dddddddddd = 2345;",
8548                Alignment);
8549   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
8550                "k = 4, int l = 5,\n"
8551                "                  int m = 6) {\n"
8552                "  int j      = 10;\n"
8553                "  otherThing = 1;\n"
8554                "}",
8555                Alignment);
8556   verifyFormat("void SomeFunction(int parameter = 0) {\n"
8557                "  int i   = 1;\n"
8558                "  int j   = 2;\n"
8559                "  int big = 10000;\n"
8560                "}",
8561                Alignment);
8562   verifyFormat("class C {\n"
8563                "public:\n"
8564                "  int i = 1;\n"
8565                "  virtual void f() = 0;\n"
8566                "};",
8567                Alignment);
8568   verifyFormat("int i = 1;\n"
8569                "if (SomeType t = getSomething()) {\n"
8570                "}\n"
8571                "int j   = 2;\n"
8572                "int big = 10000;",
8573                Alignment);
8574   verifyFormat("int j = 7;\n"
8575                "for (int k = 0; k < N; ++k) {\n"
8576                "}\n"
8577                "int j   = 2;\n"
8578                "int big = 10000;\n"
8579                "}",
8580                Alignment);
8581   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8582   verifyFormat("int i = 1;\n"
8583                "LooooooooooongType loooooooooooooooooooooongVariable\n"
8584                "    = someLooooooooooooooooongFunction();\n"
8585                "int j = 2;",
8586                Alignment);
8587   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8588   verifyFormat("int i = 1;\n"
8589                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
8590                "    someLooooooooooooooooongFunction();\n"
8591                "int j = 2;",
8592                Alignment);
8593   // FIXME: Should align all three assignments
8594   verifyFormat(
8595       "int i      = 1;\n"
8596       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
8597       "                          loooooooooooooooooooooongParameterB);\n"
8598       "int j = 2;",
8599       Alignment);
8600 }
8601 
8602 TEST_F(FormatTest, LinuxBraceBreaking) {
8603   FormatStyle LinuxBraceStyle = getLLVMStyle();
8604   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
8605   verifyFormat("namespace a\n"
8606                "{\n"
8607                "class A\n"
8608                "{\n"
8609                "  void f()\n"
8610                "  {\n"
8611                "    if (true) {\n"
8612                "      a();\n"
8613                "      b();\n"
8614                "    }\n"
8615                "  }\n"
8616                "  void g() { return; }\n"
8617                "};\n"
8618                "struct B {\n"
8619                "  int x;\n"
8620                "};\n"
8621                "}\n",
8622                LinuxBraceStyle);
8623   verifyFormat("enum X {\n"
8624                "  Y = 0,\n"
8625                "}\n",
8626                LinuxBraceStyle);
8627   verifyFormat("struct S {\n"
8628                "  int Type;\n"
8629                "  union {\n"
8630                "    int x;\n"
8631                "    double y;\n"
8632                "  } Value;\n"
8633                "  class C\n"
8634                "  {\n"
8635                "    MyFavoriteType Value;\n"
8636                "  } Class;\n"
8637                "}\n",
8638                LinuxBraceStyle);
8639 }
8640 
8641 TEST_F(FormatTest, MozillaBraceBreaking) {
8642   FormatStyle MozillaBraceStyle = getLLVMStyle();
8643   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
8644   verifyFormat("namespace a {\n"
8645                "class A\n"
8646                "{\n"
8647                "  void f()\n"
8648                "  {\n"
8649                "    if (true) {\n"
8650                "      a();\n"
8651                "      b();\n"
8652                "    }\n"
8653                "  }\n"
8654                "  void g() { return; }\n"
8655                "};\n"
8656                "enum E\n"
8657                "{\n"
8658                "  A,\n"
8659                "  // foo\n"
8660                "  B,\n"
8661                "  C\n"
8662                "};\n"
8663                "struct B\n"
8664                "{\n"
8665                "  int x;\n"
8666                "};\n"
8667                "}\n",
8668                MozillaBraceStyle);
8669   verifyFormat("struct S\n"
8670                "{\n"
8671                "  int Type;\n"
8672                "  union\n"
8673                "  {\n"
8674                "    int x;\n"
8675                "    double y;\n"
8676                "  } Value;\n"
8677                "  class C\n"
8678                "  {\n"
8679                "    MyFavoriteType Value;\n"
8680                "  } Class;\n"
8681                "}\n",
8682                MozillaBraceStyle);
8683 }
8684 
8685 TEST_F(FormatTest, StroustrupBraceBreaking) {
8686   FormatStyle StroustrupBraceStyle = getLLVMStyle();
8687   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8688   verifyFormat("namespace a {\n"
8689                "class A {\n"
8690                "  void f()\n"
8691                "  {\n"
8692                "    if (true) {\n"
8693                "      a();\n"
8694                "      b();\n"
8695                "    }\n"
8696                "  }\n"
8697                "  void g() { return; }\n"
8698                "};\n"
8699                "struct B {\n"
8700                "  int x;\n"
8701                "};\n"
8702                "}\n",
8703                StroustrupBraceStyle);
8704 
8705   verifyFormat("void foo()\n"
8706                "{\n"
8707                "  if (a) {\n"
8708                "    a();\n"
8709                "  }\n"
8710                "  else {\n"
8711                "    b();\n"
8712                "  }\n"
8713                "}\n",
8714                StroustrupBraceStyle);
8715 
8716   verifyFormat("#ifdef _DEBUG\n"
8717                "int foo(int i = 0)\n"
8718                "#else\n"
8719                "int foo(int i = 5)\n"
8720                "#endif\n"
8721                "{\n"
8722                "  return i;\n"
8723                "}",
8724                StroustrupBraceStyle);
8725 
8726   verifyFormat("void foo() {}\n"
8727                "void bar()\n"
8728                "#ifdef _DEBUG\n"
8729                "{\n"
8730                "  foo();\n"
8731                "}\n"
8732                "#else\n"
8733                "{\n"
8734                "}\n"
8735                "#endif",
8736                StroustrupBraceStyle);
8737 
8738   verifyFormat("void foobar() { int i = 5; }\n"
8739                "#ifdef _DEBUG\n"
8740                "void bar() {}\n"
8741                "#else\n"
8742                "void bar() { foobar(); }\n"
8743                "#endif",
8744                StroustrupBraceStyle);
8745 }
8746 
8747 TEST_F(FormatTest, AllmanBraceBreaking) {
8748   FormatStyle AllmanBraceStyle = getLLVMStyle();
8749   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
8750   verifyFormat("namespace a\n"
8751                "{\n"
8752                "class A\n"
8753                "{\n"
8754                "  void f()\n"
8755                "  {\n"
8756                "    if (true)\n"
8757                "    {\n"
8758                "      a();\n"
8759                "      b();\n"
8760                "    }\n"
8761                "  }\n"
8762                "  void g() { return; }\n"
8763                "};\n"
8764                "struct B\n"
8765                "{\n"
8766                "  int x;\n"
8767                "};\n"
8768                "}",
8769                AllmanBraceStyle);
8770 
8771   verifyFormat("void f()\n"
8772                "{\n"
8773                "  if (true)\n"
8774                "  {\n"
8775                "    a();\n"
8776                "  }\n"
8777                "  else if (false)\n"
8778                "  {\n"
8779                "    b();\n"
8780                "  }\n"
8781                "  else\n"
8782                "  {\n"
8783                "    c();\n"
8784                "  }\n"
8785                "}\n",
8786                AllmanBraceStyle);
8787 
8788   verifyFormat("void f()\n"
8789                "{\n"
8790                "  for (int i = 0; i < 10; ++i)\n"
8791                "  {\n"
8792                "    a();\n"
8793                "  }\n"
8794                "  while (false)\n"
8795                "  {\n"
8796                "    b();\n"
8797                "  }\n"
8798                "  do\n"
8799                "  {\n"
8800                "    c();\n"
8801                "  } while (false)\n"
8802                "}\n",
8803                AllmanBraceStyle);
8804 
8805   verifyFormat("void f(int a)\n"
8806                "{\n"
8807                "  switch (a)\n"
8808                "  {\n"
8809                "  case 0:\n"
8810                "    break;\n"
8811                "  case 1:\n"
8812                "  {\n"
8813                "    break;\n"
8814                "  }\n"
8815                "  case 2:\n"
8816                "  {\n"
8817                "  }\n"
8818                "  break;\n"
8819                "  default:\n"
8820                "    break;\n"
8821                "  }\n"
8822                "}\n",
8823                AllmanBraceStyle);
8824 
8825   verifyFormat("enum X\n"
8826                "{\n"
8827                "  Y = 0,\n"
8828                "}\n",
8829                AllmanBraceStyle);
8830   verifyFormat("enum X\n"
8831                "{\n"
8832                "  Y = 0\n"
8833                "}\n",
8834                AllmanBraceStyle);
8835 
8836   verifyFormat("@interface BSApplicationController ()\n"
8837                "{\n"
8838                "@private\n"
8839                "  id _extraIvar;\n"
8840                "}\n"
8841                "@end\n",
8842                AllmanBraceStyle);
8843 
8844   verifyFormat("#ifdef _DEBUG\n"
8845                "int foo(int i = 0)\n"
8846                "#else\n"
8847                "int foo(int i = 5)\n"
8848                "#endif\n"
8849                "{\n"
8850                "  return i;\n"
8851                "}",
8852                AllmanBraceStyle);
8853 
8854   verifyFormat("void foo() {}\n"
8855                "void bar()\n"
8856                "#ifdef _DEBUG\n"
8857                "{\n"
8858                "  foo();\n"
8859                "}\n"
8860                "#else\n"
8861                "{\n"
8862                "}\n"
8863                "#endif",
8864                AllmanBraceStyle);
8865 
8866   verifyFormat("void foobar() { int i = 5; }\n"
8867                "#ifdef _DEBUG\n"
8868                "void bar() {}\n"
8869                "#else\n"
8870                "void bar() { foobar(); }\n"
8871                "#endif",
8872                AllmanBraceStyle);
8873 
8874   // This shouldn't affect ObjC blocks..
8875   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
8876                "  // ...\n"
8877                "  int i;\n"
8878                "}];",
8879                AllmanBraceStyle);
8880   verifyFormat("void (^block)(void) = ^{\n"
8881                "  // ...\n"
8882                "  int i;\n"
8883                "};",
8884                AllmanBraceStyle);
8885   // .. or dict literals.
8886   verifyFormat("void f()\n"
8887                "{\n"
8888                "  [object someMethod:@{ @\"a\" : @\"b\" }];\n"
8889                "}",
8890                AllmanBraceStyle);
8891   verifyFormat("int f()\n"
8892                "{ // comment\n"
8893                "  return 42;\n"
8894                "}",
8895                AllmanBraceStyle);
8896 
8897   AllmanBraceStyle.ColumnLimit = 19;
8898   verifyFormat("void f() { int i; }", AllmanBraceStyle);
8899   AllmanBraceStyle.ColumnLimit = 18;
8900   verifyFormat("void f()\n"
8901                "{\n"
8902                "  int i;\n"
8903                "}",
8904                AllmanBraceStyle);
8905   AllmanBraceStyle.ColumnLimit = 80;
8906 
8907   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
8908   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
8909   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
8910   verifyFormat("void f(bool b)\n"
8911                "{\n"
8912                "  if (b)\n"
8913                "  {\n"
8914                "    return;\n"
8915                "  }\n"
8916                "}\n",
8917                BreakBeforeBraceShortIfs);
8918   verifyFormat("void f(bool b)\n"
8919                "{\n"
8920                "  if (b) return;\n"
8921                "}\n",
8922                BreakBeforeBraceShortIfs);
8923   verifyFormat("void f(bool b)\n"
8924                "{\n"
8925                "  while (b)\n"
8926                "  {\n"
8927                "    return;\n"
8928                "  }\n"
8929                "}\n",
8930                BreakBeforeBraceShortIfs);
8931 }
8932 
8933 TEST_F(FormatTest, GNUBraceBreaking) {
8934   FormatStyle GNUBraceStyle = getLLVMStyle();
8935   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
8936   verifyFormat("namespace a\n"
8937                "{\n"
8938                "class A\n"
8939                "{\n"
8940                "  void f()\n"
8941                "  {\n"
8942                "    int a;\n"
8943                "    {\n"
8944                "      int b;\n"
8945                "    }\n"
8946                "    if (true)\n"
8947                "      {\n"
8948                "        a();\n"
8949                "        b();\n"
8950                "      }\n"
8951                "  }\n"
8952                "  void g() { return; }\n"
8953                "}\n"
8954                "}",
8955                GNUBraceStyle);
8956 
8957   verifyFormat("void f()\n"
8958                "{\n"
8959                "  if (true)\n"
8960                "    {\n"
8961                "      a();\n"
8962                "    }\n"
8963                "  else if (false)\n"
8964                "    {\n"
8965                "      b();\n"
8966                "    }\n"
8967                "  else\n"
8968                "    {\n"
8969                "      c();\n"
8970                "    }\n"
8971                "}\n",
8972                GNUBraceStyle);
8973 
8974   verifyFormat("void f()\n"
8975                "{\n"
8976                "  for (int i = 0; i < 10; ++i)\n"
8977                "    {\n"
8978                "      a();\n"
8979                "    }\n"
8980                "  while (false)\n"
8981                "    {\n"
8982                "      b();\n"
8983                "    }\n"
8984                "  do\n"
8985                "    {\n"
8986                "      c();\n"
8987                "    }\n"
8988                "  while (false);\n"
8989                "}\n",
8990                GNUBraceStyle);
8991 
8992   verifyFormat("void f(int a)\n"
8993                "{\n"
8994                "  switch (a)\n"
8995                "    {\n"
8996                "    case 0:\n"
8997                "      break;\n"
8998                "    case 1:\n"
8999                "      {\n"
9000                "        break;\n"
9001                "      }\n"
9002                "    case 2:\n"
9003                "      {\n"
9004                "      }\n"
9005                "      break;\n"
9006                "    default:\n"
9007                "      break;\n"
9008                "    }\n"
9009                "}\n",
9010                GNUBraceStyle);
9011 
9012   verifyFormat("enum X\n"
9013                "{\n"
9014                "  Y = 0,\n"
9015                "}\n",
9016                GNUBraceStyle);
9017 
9018   verifyFormat("@interface BSApplicationController ()\n"
9019                "{\n"
9020                "@private\n"
9021                "  id _extraIvar;\n"
9022                "}\n"
9023                "@end\n",
9024                GNUBraceStyle);
9025 
9026   verifyFormat("#ifdef _DEBUG\n"
9027                "int foo(int i = 0)\n"
9028                "#else\n"
9029                "int foo(int i = 5)\n"
9030                "#endif\n"
9031                "{\n"
9032                "  return i;\n"
9033                "}",
9034                GNUBraceStyle);
9035 
9036   verifyFormat("void foo() {}\n"
9037                "void bar()\n"
9038                "#ifdef _DEBUG\n"
9039                "{\n"
9040                "  foo();\n"
9041                "}\n"
9042                "#else\n"
9043                "{\n"
9044                "}\n"
9045                "#endif",
9046                GNUBraceStyle);
9047 
9048   verifyFormat("void foobar() { int i = 5; }\n"
9049                "#ifdef _DEBUG\n"
9050                "void bar() {}\n"
9051                "#else\n"
9052                "void bar() { foobar(); }\n"
9053                "#endif",
9054                GNUBraceStyle);
9055 }
9056 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
9057   verifyFormat("void f() {\n"
9058                "  try {\n"
9059                "  } catch (const Exception &e) {\n"
9060                "  }\n"
9061                "}\n",
9062                getLLVMStyle());
9063 }
9064 
9065 TEST_F(FormatTest, UnderstandsPragmas) {
9066   verifyFormat("#pragma omp reduction(| : var)");
9067   verifyFormat("#pragma omp reduction(+ : var)");
9068 
9069   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
9070             "(including parentheses).",
9071             format("#pragma    mark   Any non-hyphenated or hyphenated string "
9072                    "(including parentheses)."));
9073 }
9074 
9075 TEST_F(FormatTest, UnderstandPragmaOption) {
9076   verifyFormat("#pragma option -C -A");
9077 
9078   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
9079 }
9080 
9081 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
9082   for (size_t i = 1; i < Styles.size(); ++i)                                   \
9083   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
9084                                   << " differs from Style #0"
9085 
9086 TEST_F(FormatTest, GetsPredefinedStyleByName) {
9087   SmallVector<FormatStyle, 3> Styles;
9088   Styles.resize(3);
9089 
9090   Styles[0] = getLLVMStyle();
9091   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
9092   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
9093   EXPECT_ALL_STYLES_EQUAL(Styles);
9094 
9095   Styles[0] = getGoogleStyle();
9096   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
9097   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
9098   EXPECT_ALL_STYLES_EQUAL(Styles);
9099 
9100   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9101   EXPECT_TRUE(
9102       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
9103   EXPECT_TRUE(
9104       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
9105   EXPECT_ALL_STYLES_EQUAL(Styles);
9106 
9107   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
9108   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
9109   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
9110   EXPECT_ALL_STYLES_EQUAL(Styles);
9111 
9112   Styles[0] = getMozillaStyle();
9113   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
9114   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
9115   EXPECT_ALL_STYLES_EQUAL(Styles);
9116 
9117   Styles[0] = getWebKitStyle();
9118   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
9119   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
9120   EXPECT_ALL_STYLES_EQUAL(Styles);
9121 
9122   Styles[0] = getGNUStyle();
9123   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
9124   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
9125   EXPECT_ALL_STYLES_EQUAL(Styles);
9126 
9127   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
9128 }
9129 
9130 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
9131   SmallVector<FormatStyle, 8> Styles;
9132   Styles.resize(2);
9133 
9134   Styles[0] = getGoogleStyle();
9135   Styles[1] = getLLVMStyle();
9136   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9137   EXPECT_ALL_STYLES_EQUAL(Styles);
9138 
9139   Styles.resize(5);
9140   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9141   Styles[1] = getLLVMStyle();
9142   Styles[1].Language = FormatStyle::LK_JavaScript;
9143   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9144 
9145   Styles[2] = getLLVMStyle();
9146   Styles[2].Language = FormatStyle::LK_JavaScript;
9147   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
9148                                   "BasedOnStyle: Google",
9149                                   &Styles[2])
9150                    .value());
9151 
9152   Styles[3] = getLLVMStyle();
9153   Styles[3].Language = FormatStyle::LK_JavaScript;
9154   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
9155                                   "Language: JavaScript",
9156                                   &Styles[3])
9157                    .value());
9158 
9159   Styles[4] = getLLVMStyle();
9160   Styles[4].Language = FormatStyle::LK_JavaScript;
9161   EXPECT_EQ(0, parseConfiguration("---\n"
9162                                   "BasedOnStyle: LLVM\n"
9163                                   "IndentWidth: 123\n"
9164                                   "---\n"
9165                                   "BasedOnStyle: Google\n"
9166                                   "Language: JavaScript",
9167                                   &Styles[4])
9168                    .value());
9169   EXPECT_ALL_STYLES_EQUAL(Styles);
9170 }
9171 
9172 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
9173   Style.FIELD = false;                                                         \
9174   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
9175   EXPECT_TRUE(Style.FIELD);                                                    \
9176   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
9177   EXPECT_FALSE(Style.FIELD);
9178 
9179 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
9180 
9181 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
9182   EXPECT_NE(VALUE, Style.FIELD);                                               \
9183   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
9184   EXPECT_EQ(VALUE, Style.FIELD)
9185 
9186 TEST_F(FormatTest, ParsesConfigurationBools) {
9187   FormatStyle Style = {};
9188   Style.Language = FormatStyle::LK_Cpp;
9189   CHECK_PARSE_BOOL(AlignAfterOpenBracket);
9190   CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
9191   CHECK_PARSE_BOOL(AlignOperands);
9192   CHECK_PARSE_BOOL(AlignTrailingComments);
9193   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
9194   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
9195   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
9196   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
9197   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
9198   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
9199   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
9200   CHECK_PARSE_BOOL(BinPackParameters);
9201   CHECK_PARSE_BOOL(BinPackArguments);
9202   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
9203   CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
9204   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
9205   CHECK_PARSE_BOOL(DerivePointerAlignment);
9206   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
9207   CHECK_PARSE_BOOL(IndentCaseLabels);
9208   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
9209   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
9210   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
9211   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
9212   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
9213   CHECK_PARSE_BOOL(SpacesInParentheses);
9214   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
9215   CHECK_PARSE_BOOL(SpacesInAngles);
9216   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
9217   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
9218   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
9219   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
9220   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
9221 }
9222 
9223 #undef CHECK_PARSE_BOOL
9224 
9225 TEST_F(FormatTest, ParsesConfiguration) {
9226   FormatStyle Style = {};
9227   Style.Language = FormatStyle::LK_Cpp;
9228   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
9229   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
9230               ConstructorInitializerIndentWidth, 1234u);
9231   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
9232   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
9233   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
9234   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
9235               PenaltyBreakBeforeFirstCallParameter, 1234u);
9236   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
9237   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
9238               PenaltyReturnTypeOnItsOwnLine, 1234u);
9239   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
9240               SpacesBeforeTrailingComments, 1234u);
9241   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
9242   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
9243 
9244   Style.PointerAlignment = FormatStyle::PAS_Middle;
9245   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
9246               FormatStyle::PAS_Left);
9247   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
9248               FormatStyle::PAS_Right);
9249   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
9250               FormatStyle::PAS_Middle);
9251   // For backward compatibility:
9252   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
9253               FormatStyle::PAS_Left);
9254   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
9255               FormatStyle::PAS_Right);
9256   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
9257               FormatStyle::PAS_Middle);
9258 
9259   Style.Standard = FormatStyle::LS_Auto;
9260   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
9261   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
9262   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
9263   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
9264   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
9265 
9266   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9267   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
9268               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
9269   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
9270               FormatStyle::BOS_None);
9271   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
9272               FormatStyle::BOS_All);
9273   // For backward compatibility:
9274   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
9275               FormatStyle::BOS_None);
9276   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
9277               FormatStyle::BOS_All);
9278 
9279   Style.UseTab = FormatStyle::UT_ForIndentation;
9280   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
9281   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
9282   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
9283   // For backward compatibility:
9284   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
9285   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
9286 
9287   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
9288   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
9289               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9290   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
9291               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
9292   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
9293               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
9294   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
9295               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9296   // For backward compatibility:
9297   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
9298               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9299   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
9300               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9301 
9302   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
9303   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
9304               FormatStyle::SBPO_Never);
9305   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
9306               FormatStyle::SBPO_Always);
9307   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
9308               FormatStyle::SBPO_ControlStatements);
9309   // For backward compatibility:
9310   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
9311               FormatStyle::SBPO_Never);
9312   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
9313               FormatStyle::SBPO_ControlStatements);
9314 
9315   Style.ColumnLimit = 123;
9316   FormatStyle BaseStyle = getLLVMStyle();
9317   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
9318   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
9319 
9320   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9321   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
9322               FormatStyle::BS_Attach);
9323   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
9324               FormatStyle::BS_Linux);
9325   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
9326               FormatStyle::BS_Mozilla);
9327   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
9328               FormatStyle::BS_Stroustrup);
9329   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
9330               FormatStyle::BS_Allman);
9331   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
9332 
9333   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
9334   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
9335               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
9336   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
9337               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
9338   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
9339               AlwaysBreakAfterDefinitionReturnType,
9340               FormatStyle::DRTBS_TopLevel);
9341 
9342   Style.NamespaceIndentation = FormatStyle::NI_All;
9343   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
9344               FormatStyle::NI_None);
9345   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
9346               FormatStyle::NI_Inner);
9347   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
9348               FormatStyle::NI_All);
9349 
9350   Style.ForEachMacros.clear();
9351   std::vector<std::string> BoostForeach;
9352   BoostForeach.push_back("BOOST_FOREACH");
9353   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
9354   std::vector<std::string> BoostAndQForeach;
9355   BoostAndQForeach.push_back("BOOST_FOREACH");
9356   BoostAndQForeach.push_back("Q_FOREACH");
9357   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
9358               BoostAndQForeach);
9359 }
9360 
9361 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
9362   FormatStyle Style = {};
9363   Style.Language = FormatStyle::LK_Cpp;
9364   CHECK_PARSE("Language: Cpp\n"
9365               "IndentWidth: 12",
9366               IndentWidth, 12u);
9367   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
9368                                "IndentWidth: 34",
9369                                &Style),
9370             ParseError::Unsuitable);
9371   EXPECT_EQ(12u, Style.IndentWidth);
9372   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
9373   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9374 
9375   Style.Language = FormatStyle::LK_JavaScript;
9376   CHECK_PARSE("Language: JavaScript\n"
9377               "IndentWidth: 12",
9378               IndentWidth, 12u);
9379   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
9380   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
9381                                "IndentWidth: 34",
9382                                &Style),
9383             ParseError::Unsuitable);
9384   EXPECT_EQ(23u, Style.IndentWidth);
9385   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
9386   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9387 
9388   CHECK_PARSE("BasedOnStyle: LLVM\n"
9389               "IndentWidth: 67",
9390               IndentWidth, 67u);
9391 
9392   CHECK_PARSE("---\n"
9393               "Language: JavaScript\n"
9394               "IndentWidth: 12\n"
9395               "---\n"
9396               "Language: Cpp\n"
9397               "IndentWidth: 34\n"
9398               "...\n",
9399               IndentWidth, 12u);
9400 
9401   Style.Language = FormatStyle::LK_Cpp;
9402   CHECK_PARSE("---\n"
9403               "Language: JavaScript\n"
9404               "IndentWidth: 12\n"
9405               "---\n"
9406               "Language: Cpp\n"
9407               "IndentWidth: 34\n"
9408               "...\n",
9409               IndentWidth, 34u);
9410   CHECK_PARSE("---\n"
9411               "IndentWidth: 78\n"
9412               "---\n"
9413               "Language: JavaScript\n"
9414               "IndentWidth: 56\n"
9415               "...\n",
9416               IndentWidth, 78u);
9417 
9418   Style.ColumnLimit = 123;
9419   Style.IndentWidth = 234;
9420   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
9421   Style.TabWidth = 345;
9422   EXPECT_FALSE(parseConfiguration("---\n"
9423                                   "IndentWidth: 456\n"
9424                                   "BreakBeforeBraces: Allman\n"
9425                                   "---\n"
9426                                   "Language: JavaScript\n"
9427                                   "IndentWidth: 111\n"
9428                                   "TabWidth: 111\n"
9429                                   "---\n"
9430                                   "Language: Cpp\n"
9431                                   "BreakBeforeBraces: Stroustrup\n"
9432                                   "TabWidth: 789\n"
9433                                   "...\n",
9434                                   &Style));
9435   EXPECT_EQ(123u, Style.ColumnLimit);
9436   EXPECT_EQ(456u, Style.IndentWidth);
9437   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
9438   EXPECT_EQ(789u, Style.TabWidth);
9439 
9440   EXPECT_EQ(parseConfiguration("---\n"
9441                                "Language: JavaScript\n"
9442                                "IndentWidth: 56\n"
9443                                "---\n"
9444                                "IndentWidth: 78\n"
9445                                "...\n",
9446                                &Style),
9447             ParseError::Error);
9448   EXPECT_EQ(parseConfiguration("---\n"
9449                                "Language: JavaScript\n"
9450                                "IndentWidth: 56\n"
9451                                "---\n"
9452                                "Language: JavaScript\n"
9453                                "IndentWidth: 78\n"
9454                                "...\n",
9455                                &Style),
9456             ParseError::Error);
9457 
9458   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9459 }
9460 
9461 #undef CHECK_PARSE
9462 
9463 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
9464   FormatStyle Style = {};
9465   Style.Language = FormatStyle::LK_JavaScript;
9466   Style.BreakBeforeTernaryOperators = true;
9467   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
9468   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9469 
9470   Style.BreakBeforeTernaryOperators = true;
9471   EXPECT_EQ(0, parseConfiguration("---\n"
9472                                   "BasedOnStyle: Google\n"
9473                                   "---\n"
9474                                   "Language: JavaScript\n"
9475                                   "IndentWidth: 76\n"
9476                                   "...\n",
9477                                   &Style)
9478                    .value());
9479   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9480   EXPECT_EQ(76u, Style.IndentWidth);
9481   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9482 }
9483 
9484 TEST_F(FormatTest, ConfigurationRoundTripTest) {
9485   FormatStyle Style = getLLVMStyle();
9486   std::string YAML = configurationAsText(Style);
9487   FormatStyle ParsedStyle = {};
9488   ParsedStyle.Language = FormatStyle::LK_Cpp;
9489   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
9490   EXPECT_EQ(Style, ParsedStyle);
9491 }
9492 
9493 TEST_F(FormatTest, WorksFor8bitEncodings) {
9494   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
9495             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
9496             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
9497             "\"\xef\xee\xf0\xf3...\"",
9498             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
9499                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
9500                    "\xef\xee\xf0\xf3...\"",
9501                    getLLVMStyleWithColumns(12)));
9502 }
9503 
9504 TEST_F(FormatTest, HandlesUTF8BOM) {
9505   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
9506   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
9507             format("\xef\xbb\xbf#include <iostream>"));
9508   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
9509             format("\xef\xbb\xbf\n#include <iostream>"));
9510 }
9511 
9512 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
9513 #if !defined(_MSC_VER)
9514 
9515 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
9516   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
9517                getLLVMStyleWithColumns(35));
9518   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
9519                getLLVMStyleWithColumns(31));
9520   verifyFormat("// Однажды в студёную зимнюю пору...",
9521                getLLVMStyleWithColumns(36));
9522   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
9523   verifyFormat("/* Однажды в студёную зимнюю пору... */",
9524                getLLVMStyleWithColumns(39));
9525   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
9526                getLLVMStyleWithColumns(35));
9527 }
9528 
9529 TEST_F(FormatTest, SplitsUTF8Strings) {
9530   // Non-printable characters' width is currently considered to be the length in
9531   // bytes in UTF8. The characters can be displayed in very different manner
9532   // (zero-width, single width with a substitution glyph, expanded to their code
9533   // (e.g. "<8d>"), so there's no single correct way to handle them.
9534   EXPECT_EQ("\"aaaaÄ\"\n"
9535             "\"\xc2\x8d\";",
9536             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9537   EXPECT_EQ("\"aaaaaaaÄ\"\n"
9538             "\"\xc2\x8d\";",
9539             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9540   EXPECT_EQ("\"Однажды, в \"\n"
9541             "\"студёную \"\n"
9542             "\"зимнюю \"\n"
9543             "\"пору,\"",
9544             format("\"Однажды, в студёную зимнюю пору,\"",
9545                    getLLVMStyleWithColumns(13)));
9546   EXPECT_EQ(
9547       "\"一 二 三 \"\n"
9548       "\"四 五六 \"\n"
9549       "\"七 八 九 \"\n"
9550       "\"十\"",
9551       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
9552   EXPECT_EQ("\"一\t二 \"\n"
9553             "\"\t三 \"\n"
9554             "\"四 五\t六 \"\n"
9555             "\"\t七 \"\n"
9556             "\"八九十\tqq\"",
9557             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
9558                    getLLVMStyleWithColumns(11)));
9559 
9560   // UTF8 character in an escape sequence.
9561   EXPECT_EQ("\"aaaaaa\"\n"
9562             "\"\\\xC2\x8D\"",
9563             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
9564 }
9565 
9566 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
9567   EXPECT_EQ("const char *sssss =\n"
9568             "    \"一二三四五六七八\\\n"
9569             " 九 十\";",
9570             format("const char *sssss = \"一二三四五六七八\\\n"
9571                    " 九 十\";",
9572                    getLLVMStyleWithColumns(30)));
9573 }
9574 
9575 TEST_F(FormatTest, SplitsUTF8LineComments) {
9576   EXPECT_EQ("// aaaaÄ\xc2\x8d",
9577             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
9578   EXPECT_EQ("// Я из лесу\n"
9579             "// вышел; был\n"
9580             "// сильный\n"
9581             "// мороз.",
9582             format("// Я из лесу вышел; был сильный мороз.",
9583                    getLLVMStyleWithColumns(13)));
9584   EXPECT_EQ("// 一二三\n"
9585             "// 四五六七\n"
9586             "// 八  九\n"
9587             "// 十",
9588             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
9589 }
9590 
9591 TEST_F(FormatTest, SplitsUTF8BlockComments) {
9592   EXPECT_EQ("/* Гляжу,\n"
9593             " * поднимается\n"
9594             " * медленно в\n"
9595             " * гору\n"
9596             " * Лошадка,\n"
9597             " * везущая\n"
9598             " * хворосту\n"
9599             " * воз. */",
9600             format("/* Гляжу, поднимается медленно в гору\n"
9601                    " * Лошадка, везущая хворосту воз. */",
9602                    getLLVMStyleWithColumns(13)));
9603   EXPECT_EQ(
9604       "/* 一二三\n"
9605       " * 四五六七\n"
9606       " * 八  九\n"
9607       " * 十  */",
9608       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
9609   EXPECT_EQ("/* �������� ��������\n"
9610             " * ��������\n"
9611             " * ������-�� */",
9612             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
9613 }
9614 
9615 #endif // _MSC_VER
9616 
9617 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
9618   FormatStyle Style = getLLVMStyle();
9619 
9620   Style.ConstructorInitializerIndentWidth = 4;
9621   verifyFormat(
9622       "SomeClass::Constructor()\n"
9623       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9624       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9625       Style);
9626 
9627   Style.ConstructorInitializerIndentWidth = 2;
9628   verifyFormat(
9629       "SomeClass::Constructor()\n"
9630       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9631       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9632       Style);
9633 
9634   Style.ConstructorInitializerIndentWidth = 0;
9635   verifyFormat(
9636       "SomeClass::Constructor()\n"
9637       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9638       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9639       Style);
9640 }
9641 
9642 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
9643   FormatStyle Style = getLLVMStyle();
9644   Style.BreakConstructorInitializersBeforeComma = true;
9645   Style.ConstructorInitializerIndentWidth = 4;
9646   verifyFormat("SomeClass::Constructor()\n"
9647                "    : a(a)\n"
9648                "    , b(b)\n"
9649                "    , c(c) {}",
9650                Style);
9651   verifyFormat("SomeClass::Constructor()\n"
9652                "    : a(a) {}",
9653                Style);
9654 
9655   Style.ColumnLimit = 0;
9656   verifyFormat("SomeClass::Constructor()\n"
9657                "    : a(a) {}",
9658                Style);
9659   verifyFormat("SomeClass::Constructor()\n"
9660                "    : a(a)\n"
9661                "    , b(b)\n"
9662                "    , c(c) {}",
9663                Style);
9664   verifyFormat("SomeClass::Constructor()\n"
9665                "    : a(a) {\n"
9666                "  foo();\n"
9667                "  bar();\n"
9668                "}",
9669                Style);
9670 
9671   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9672   verifyFormat("SomeClass::Constructor()\n"
9673                "    : a(a)\n"
9674                "    , b(b)\n"
9675                "    , c(c) {\n}",
9676                Style);
9677   verifyFormat("SomeClass::Constructor()\n"
9678                "    : a(a) {\n}",
9679                Style);
9680 
9681   Style.ColumnLimit = 80;
9682   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
9683   Style.ConstructorInitializerIndentWidth = 2;
9684   verifyFormat("SomeClass::Constructor()\n"
9685                "  : a(a)\n"
9686                "  , b(b)\n"
9687                "  , c(c) {}",
9688                Style);
9689 
9690   Style.ConstructorInitializerIndentWidth = 0;
9691   verifyFormat("SomeClass::Constructor()\n"
9692                ": a(a)\n"
9693                ", b(b)\n"
9694                ", c(c) {}",
9695                Style);
9696 
9697   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
9698   Style.ConstructorInitializerIndentWidth = 4;
9699   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
9700   verifyFormat(
9701       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
9702       Style);
9703   verifyFormat(
9704       "SomeClass::Constructor()\n"
9705       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
9706       Style);
9707   Style.ConstructorInitializerIndentWidth = 4;
9708   Style.ColumnLimit = 60;
9709   verifyFormat("SomeClass::Constructor()\n"
9710                "    : aaaaaaaa(aaaaaaaa)\n"
9711                "    , aaaaaaaa(aaaaaaaa)\n"
9712                "    , aaaaaaaa(aaaaaaaa) {}",
9713                Style);
9714 }
9715 
9716 TEST_F(FormatTest, Destructors) {
9717   verifyFormat("void F(int &i) { i.~int(); }");
9718   verifyFormat("void F(int &i) { i->~int(); }");
9719 }
9720 
9721 TEST_F(FormatTest, FormatsWithWebKitStyle) {
9722   FormatStyle Style = getWebKitStyle();
9723 
9724   // Don't indent in outer namespaces.
9725   verifyFormat("namespace outer {\n"
9726                "int i;\n"
9727                "namespace inner {\n"
9728                "    int i;\n"
9729                "} // namespace inner\n"
9730                "} // namespace outer\n"
9731                "namespace other_outer {\n"
9732                "int i;\n"
9733                "}",
9734                Style);
9735 
9736   // Don't indent case labels.
9737   verifyFormat("switch (variable) {\n"
9738                "case 1:\n"
9739                "case 2:\n"
9740                "    doSomething();\n"
9741                "    break;\n"
9742                "default:\n"
9743                "    ++variable;\n"
9744                "}",
9745                Style);
9746 
9747   // Wrap before binary operators.
9748   EXPECT_EQ("void f()\n"
9749             "{\n"
9750             "    if (aaaaaaaaaaaaaaaa\n"
9751             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
9752             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
9753             "        return;\n"
9754             "}",
9755             format("void f() {\n"
9756                    "if (aaaaaaaaaaaaaaaa\n"
9757                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
9758                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
9759                    "return;\n"
9760                    "}",
9761                    Style));
9762 
9763   // Allow functions on a single line.
9764   verifyFormat("void f() { return; }", Style);
9765 
9766   // Constructor initializers are formatted one per line with the "," on the
9767   // new line.
9768   verifyFormat("Constructor()\n"
9769                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9770                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
9771                "          aaaaaaaaaaaaaa)\n"
9772                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
9773                "{\n"
9774                "}",
9775                Style);
9776   verifyFormat("SomeClass::Constructor()\n"
9777                "    : a(a)\n"
9778                "{\n"
9779                "}",
9780                Style);
9781   EXPECT_EQ("SomeClass::Constructor()\n"
9782             "    : a(a)\n"
9783             "{\n"
9784             "}",
9785             format("SomeClass::Constructor():a(a){}", Style));
9786   verifyFormat("SomeClass::Constructor()\n"
9787                "    : a(a)\n"
9788                "    , b(b)\n"
9789                "    , c(c)\n"
9790                "{\n"
9791                "}",
9792                Style);
9793   verifyFormat("SomeClass::Constructor()\n"
9794                "    : a(a)\n"
9795                "{\n"
9796                "    foo();\n"
9797                "    bar();\n"
9798                "}",
9799                Style);
9800 
9801   // Access specifiers should be aligned left.
9802   verifyFormat("class C {\n"
9803                "public:\n"
9804                "    int i;\n"
9805                "};",
9806                Style);
9807 
9808   // Do not align comments.
9809   verifyFormat("int a; // Do not\n"
9810                "double b; // align comments.",
9811                Style);
9812 
9813   // Do not align operands.
9814   EXPECT_EQ("ASSERT(aaaa\n"
9815             "    || bbbb);",
9816             format("ASSERT ( aaaa\n||bbbb);", Style));
9817 
9818   // Accept input's line breaks.
9819   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
9820             "    || bbbbbbbbbbbbbbb) {\n"
9821             "    i++;\n"
9822             "}",
9823             format("if (aaaaaaaaaaaaaaa\n"
9824                    "|| bbbbbbbbbbbbbbb) { i++; }",
9825                    Style));
9826   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
9827             "    i++;\n"
9828             "}",
9829             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
9830 
9831   // Don't automatically break all macro definitions (llvm.org/PR17842).
9832   verifyFormat("#define aNumber 10", Style);
9833   // However, generally keep the line breaks that the user authored.
9834   EXPECT_EQ("#define aNumber \\\n"
9835             "    10",
9836             format("#define aNumber \\\n"
9837                    " 10",
9838                    Style));
9839 
9840   // Keep empty and one-element array literals on a single line.
9841   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
9842             "                                  copyItems:YES];",
9843             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
9844                    "copyItems:YES];",
9845                    Style));
9846   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
9847             "                                  copyItems:YES];",
9848             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
9849                    "             copyItems:YES];",
9850                    Style));
9851   // FIXME: This does not seem right, there should be more indentation before
9852   // the array literal's entries. Nested blocks have the same problem.
9853   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
9854             "    @\"a\",\n"
9855             "    @\"a\"\n"
9856             "]\n"
9857             "                                  copyItems:YES];",
9858             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
9859                    "     @\"a\",\n"
9860                    "     @\"a\"\n"
9861                    "     ]\n"
9862                    "       copyItems:YES];",
9863                    Style));
9864   EXPECT_EQ(
9865       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
9866       "                                  copyItems:YES];",
9867       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
9868              "   copyItems:YES];",
9869              Style));
9870 
9871   verifyFormat("[self.a b:c c:d];", Style);
9872   EXPECT_EQ("[self.a b:c\n"
9873             "        c:d];",
9874             format("[self.a b:c\n"
9875                    "c:d];",
9876                    Style));
9877 }
9878 
9879 TEST_F(FormatTest, FormatsLambdas) {
9880   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
9881   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
9882   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
9883   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
9884   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
9885   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
9886   verifyFormat("void f() {\n"
9887                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
9888                "}\n");
9889   verifyFormat("void f() {\n"
9890                "  other(x.begin(), //\n"
9891                "        x.end(),   //\n"
9892                "        [&](int, int) { return 1; });\n"
9893                "}\n");
9894   verifyFormat("SomeFunction([]() { // A cool function...\n"
9895                "  return 43;\n"
9896                "});");
9897   EXPECT_EQ("SomeFunction([]() {\n"
9898             "#define A a\n"
9899             "  return 43;\n"
9900             "});",
9901             format("SomeFunction([](){\n"
9902                    "#define A a\n"
9903                    "return 43;\n"
9904                    "});"));
9905   verifyFormat("void f() {\n"
9906                "  SomeFunction([](decltype(x), A *a) {});\n"
9907                "}");
9908   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9909                "    [](const aaaaaaaaaa &a) { return a; });");
9910   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
9911                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
9912                "});");
9913   verifyFormat("Constructor()\n"
9914                "    : Field([] { // comment\n"
9915                "        int i;\n"
9916                "      }) {}");
9917   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
9918                "  return some_parameter.size();\n"
9919                "};");
9920   verifyFormat("int i = aaaaaa ? 1 //\n"
9921                "               : [] {\n"
9922                "                   return 2; //\n"
9923                "                 }();");
9924   verifyFormat("llvm::errs() << \"number of twos is \"\n"
9925                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
9926                "                  return x == 2; // force break\n"
9927                "                });");
9928   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n"
9929                "    int iiiiiiiiiiii) {\n"
9930                "  return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n"
9931                "});",
9932                getLLVMStyleWithColumns(60));
9933   verifyFormat("SomeFunction({[&] {\n"
9934                "                // comment\n"
9935                "              },\n"
9936                "              [&] {\n"
9937                "                // comment\n"
9938                "              }});");
9939   verifyFormat("SomeFunction({[&] {\n"
9940                "  // comment\n"
9941                "}});");
9942 
9943   // Lambdas with return types.
9944   verifyFormat("int c = []() -> int { return 2; }();\n");
9945   verifyFormat("int c = []() -> int * { return 2; }();\n");
9946   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
9947   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
9948   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
9949   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
9950   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
9951   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
9952   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
9953                "                   int j) -> int {\n"
9954                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
9955                "};");
9956   verifyFormat(
9957       "aaaaaaaaaaaaaaaaaaaaaa(\n"
9958       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
9959       "      return aaaaaaaaaaaaaaaaa;\n"
9960       "    });",
9961       getLLVMStyleWithColumns(70));
9962 
9963   // Multiple lambdas in the same parentheses change indentation rules.
9964   verifyFormat("SomeFunction(\n"
9965                "    []() {\n"
9966                "      int i = 42;\n"
9967                "      return i;\n"
9968                "    },\n"
9969                "    []() {\n"
9970                "      int j = 43;\n"
9971                "      return j;\n"
9972                "    });");
9973 
9974   // More complex introducers.
9975   verifyFormat("return [i, args...] {};");
9976 
9977   // Not lambdas.
9978   verifyFormat("constexpr char hello[]{\"hello\"};");
9979   verifyFormat("double &operator[](int i) { return 0; }\n"
9980                "int i;");
9981   verifyFormat("std::unique_ptr<int[]> foo() {}");
9982   verifyFormat("int i = a[a][a]->f();");
9983   verifyFormat("int i = (*b)[a]->f();");
9984 
9985   // Other corner cases.
9986   verifyFormat("void f() {\n"
9987                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
9988                "      );\n"
9989                "}");
9990 
9991   // Lambdas created through weird macros.
9992   verifyFormat("void f() {\n"
9993                "  MACRO((const AA &a) { return 1; });\n"
9994                "}");
9995 
9996   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
9997                "      doo_dah();\n"
9998                "      doo_dah();\n"
9999                "    })) {\n"
10000                "}");
10001   verifyFormat("auto lambda = []() {\n"
10002                "  int a = 2\n"
10003                "#if A\n"
10004                "          + 2\n"
10005                "#endif\n"
10006                "      ;\n"
10007                "};");
10008 }
10009 
10010 TEST_F(FormatTest, FormatsBlocks) {
10011   FormatStyle ShortBlocks = getLLVMStyle();
10012   ShortBlocks.AllowShortBlocksOnASingleLine = true;
10013   verifyFormat("int (^Block)(int, int);", ShortBlocks);
10014   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
10015   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
10016   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
10017   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
10018   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
10019 
10020   verifyFormat("foo(^{ bar(); });", ShortBlocks);
10021   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
10022   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
10023 
10024   verifyFormat("[operation setCompletionBlock:^{\n"
10025                "  [self onOperationDone];\n"
10026                "}];");
10027   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
10028                "  [self onOperationDone];\n"
10029                "}]};");
10030   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
10031                "  f();\n"
10032                "}];");
10033   verifyFormat("int a = [operation block:^int(int *i) {\n"
10034                "  return 1;\n"
10035                "}];");
10036   verifyFormat("[myObject doSomethingWith:arg1\n"
10037                "                      aaa:^int(int *a) {\n"
10038                "                        return 1;\n"
10039                "                      }\n"
10040                "                      bbb:f(a * bbbbbbbb)];");
10041 
10042   verifyFormat("[operation setCompletionBlock:^{\n"
10043                "  [self.delegate newDataAvailable];\n"
10044                "}];",
10045                getLLVMStyleWithColumns(60));
10046   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
10047                "  NSString *path = [self sessionFilePath];\n"
10048                "  if (path) {\n"
10049                "    // ...\n"
10050                "  }\n"
10051                "});");
10052   verifyFormat("[[SessionService sharedService]\n"
10053                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10054                "      if (window) {\n"
10055                "        [self windowDidLoad:window];\n"
10056                "      } else {\n"
10057                "        [self errorLoadingWindow];\n"
10058                "      }\n"
10059                "    }];");
10060   verifyFormat("void (^largeBlock)(void) = ^{\n"
10061                "  // ...\n"
10062                "};\n",
10063                getLLVMStyleWithColumns(40));
10064   verifyFormat("[[SessionService sharedService]\n"
10065                "    loadWindowWithCompletionBlock: //\n"
10066                "        ^(SessionWindow *window) {\n"
10067                "          if (window) {\n"
10068                "            [self windowDidLoad:window];\n"
10069                "          } else {\n"
10070                "            [self errorLoadingWindow];\n"
10071                "          }\n"
10072                "        }];",
10073                getLLVMStyleWithColumns(60));
10074   verifyFormat("[myObject doSomethingWith:arg1\n"
10075                "    firstBlock:^(Foo *a) {\n"
10076                "      // ...\n"
10077                "      int i;\n"
10078                "    }\n"
10079                "    secondBlock:^(Bar *b) {\n"
10080                "      // ...\n"
10081                "      int i;\n"
10082                "    }\n"
10083                "    thirdBlock:^Foo(Bar *b) {\n"
10084                "      // ...\n"
10085                "      int i;\n"
10086                "    }];");
10087   verifyFormat("[myObject doSomethingWith:arg1\n"
10088                "               firstBlock:-1\n"
10089                "              secondBlock:^(Bar *b) {\n"
10090                "                // ...\n"
10091                "                int i;\n"
10092                "              }];");
10093 
10094   verifyFormat("f(^{\n"
10095                "  @autoreleasepool {\n"
10096                "    if (a) {\n"
10097                "      g();\n"
10098                "    }\n"
10099                "  }\n"
10100                "});");
10101   verifyFormat("Block b = ^int *(A *a, B *b) {}");
10102 
10103   FormatStyle FourIndent = getLLVMStyle();
10104   FourIndent.ObjCBlockIndentWidth = 4;
10105   verifyFormat("[operation setCompletionBlock:^{\n"
10106                "    [self onOperationDone];\n"
10107                "}];",
10108                FourIndent);
10109 }
10110 
10111 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
10112   FormatStyle ZeroColumn = getLLVMStyle();
10113   ZeroColumn.ColumnLimit = 0;
10114 
10115   verifyFormat("[[SessionService sharedService] "
10116                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10117                "  if (window) {\n"
10118                "    [self windowDidLoad:window];\n"
10119                "  } else {\n"
10120                "    [self errorLoadingWindow];\n"
10121                "  }\n"
10122                "}];",
10123                ZeroColumn);
10124   EXPECT_EQ("[[SessionService sharedService]\n"
10125             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10126             "      if (window) {\n"
10127             "        [self windowDidLoad:window];\n"
10128             "      } else {\n"
10129             "        [self errorLoadingWindow];\n"
10130             "      }\n"
10131             "    }];",
10132             format("[[SessionService sharedService]\n"
10133                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10134                    "                if (window) {\n"
10135                    "    [self windowDidLoad:window];\n"
10136                    "  } else {\n"
10137                    "    [self errorLoadingWindow];\n"
10138                    "  }\n"
10139                    "}];",
10140                    ZeroColumn));
10141   verifyFormat("[myObject doSomethingWith:arg1\n"
10142                "    firstBlock:^(Foo *a) {\n"
10143                "      // ...\n"
10144                "      int i;\n"
10145                "    }\n"
10146                "    secondBlock:^(Bar *b) {\n"
10147                "      // ...\n"
10148                "      int i;\n"
10149                "    }\n"
10150                "    thirdBlock:^Foo(Bar *b) {\n"
10151                "      // ...\n"
10152                "      int i;\n"
10153                "    }];",
10154                ZeroColumn);
10155   verifyFormat("f(^{\n"
10156                "  @autoreleasepool {\n"
10157                "    if (a) {\n"
10158                "      g();\n"
10159                "    }\n"
10160                "  }\n"
10161                "});",
10162                ZeroColumn);
10163   verifyFormat("void (^largeBlock)(void) = ^{\n"
10164                "  // ...\n"
10165                "};",
10166                ZeroColumn);
10167 
10168   ZeroColumn.AllowShortBlocksOnASingleLine = true;
10169   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
10170             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
10171   ZeroColumn.AllowShortBlocksOnASingleLine = false;
10172   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
10173             "  int i;\n"
10174             "};",
10175             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
10176 }
10177 
10178 TEST_F(FormatTest, SupportsCRLF) {
10179   EXPECT_EQ("int a;\r\n"
10180             "int b;\r\n"
10181             "int c;\r\n",
10182             format("int a;\r\n"
10183                    "  int b;\r\n"
10184                    "    int c;\r\n",
10185                    getLLVMStyle()));
10186   EXPECT_EQ("int a;\r\n"
10187             "int b;\r\n"
10188             "int c;\r\n",
10189             format("int a;\r\n"
10190                    "  int b;\n"
10191                    "    int c;\r\n",
10192                    getLLVMStyle()));
10193   EXPECT_EQ("int a;\n"
10194             "int b;\n"
10195             "int c;\n",
10196             format("int a;\r\n"
10197                    "  int b;\n"
10198                    "    int c;\n",
10199                    getLLVMStyle()));
10200   EXPECT_EQ("\"aaaaaaa \"\r\n"
10201             "\"bbbbbbb\";\r\n",
10202             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
10203   EXPECT_EQ("#define A \\\r\n"
10204             "  b;      \\\r\n"
10205             "  c;      \\\r\n"
10206             "  d;\r\n",
10207             format("#define A \\\r\n"
10208                    "  b; \\\r\n"
10209                    "  c; d; \r\n",
10210                    getGoogleStyle()));
10211 
10212   EXPECT_EQ("/*\r\n"
10213             "multi line block comments\r\n"
10214             "should not introduce\r\n"
10215             "an extra carriage return\r\n"
10216             "*/\r\n",
10217             format("/*\r\n"
10218                    "multi line block comments\r\n"
10219                    "should not introduce\r\n"
10220                    "an extra carriage return\r\n"
10221                    "*/\r\n"));
10222 }
10223 
10224 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
10225   verifyFormat("MY_CLASS(C) {\n"
10226                "  int i;\n"
10227                "  int j;\n"
10228                "};");
10229 }
10230 
10231 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
10232   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
10233   TwoIndent.ContinuationIndentWidth = 2;
10234 
10235   EXPECT_EQ("int i =\n"
10236             "  longFunction(\n"
10237             "    arg);",
10238             format("int i = longFunction(arg);", TwoIndent));
10239 
10240   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
10241   SixIndent.ContinuationIndentWidth = 6;
10242 
10243   EXPECT_EQ("int i =\n"
10244             "      longFunction(\n"
10245             "            arg);",
10246             format("int i = longFunction(arg);", SixIndent));
10247 }
10248 
10249 TEST_F(FormatTest, SpacesInAngles) {
10250   FormatStyle Spaces = getLLVMStyle();
10251   Spaces.SpacesInAngles = true;
10252 
10253   verifyFormat("static_cast< int >(arg);", Spaces);
10254   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
10255   verifyFormat("f< int, float >();", Spaces);
10256   verifyFormat("template <> g() {}", Spaces);
10257   verifyFormat("template < std::vector< int > > f() {}", Spaces);
10258 
10259   Spaces.Standard = FormatStyle::LS_Cpp03;
10260   Spaces.SpacesInAngles = true;
10261   verifyFormat("A< A< int > >();", Spaces);
10262 
10263   Spaces.SpacesInAngles = false;
10264   verifyFormat("A<A<int> >();", Spaces);
10265 
10266   Spaces.Standard = FormatStyle::LS_Cpp11;
10267   Spaces.SpacesInAngles = true;
10268   verifyFormat("A< A< int > >();", Spaces);
10269 
10270   Spaces.SpacesInAngles = false;
10271   verifyFormat("A<A<int>>();", Spaces);
10272 }
10273 
10274 TEST_F(FormatTest, TripleAngleBrackets) {
10275   verifyFormat("f<<<1, 1>>>();");
10276   verifyFormat("f<<<1, 1, 1, s>>>();");
10277   verifyFormat("f<<<a, b, c, d>>>();");
10278   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
10279   verifyFormat("f<param><<<1, 1>>>();");
10280   verifyFormat("f<1><<<1, 1>>>();");
10281   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
10282   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10283                "aaaaaaaaaaa<<<\n    1, 1>>>();");
10284 }
10285 
10286 TEST_F(FormatTest, MergeLessLessAtEnd) {
10287   verifyFormat("<<");
10288   EXPECT_EQ("< < <", format("\\\n<<<"));
10289   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10290                "aaallvm::outs() <<");
10291   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10292                "aaaallvm::outs()\n    <<");
10293 }
10294 
10295 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
10296   std::string code = "#if A\n"
10297                      "#if B\n"
10298                      "a.\n"
10299                      "#endif\n"
10300                      "    a = 1;\n"
10301                      "#else\n"
10302                      "#endif\n"
10303                      "#if C\n"
10304                      "#else\n"
10305                      "#endif\n";
10306   EXPECT_EQ(code, format(code));
10307 }
10308 
10309 TEST_F(FormatTest, HandleConflictMarkers) {
10310   // Git/SVN conflict markers.
10311   EXPECT_EQ("int a;\n"
10312             "void f() {\n"
10313             "  callme(some(parameter1,\n"
10314             "<<<<<<< text by the vcs\n"
10315             "              parameter2),\n"
10316             "||||||| text by the vcs\n"
10317             "              parameter2),\n"
10318             "         parameter3,\n"
10319             "======= text by the vcs\n"
10320             "              parameter2, parameter3),\n"
10321             ">>>>>>> text by the vcs\n"
10322             "         otherparameter);\n",
10323             format("int a;\n"
10324                    "void f() {\n"
10325                    "  callme(some(parameter1,\n"
10326                    "<<<<<<< text by the vcs\n"
10327                    "  parameter2),\n"
10328                    "||||||| text by the vcs\n"
10329                    "  parameter2),\n"
10330                    "  parameter3,\n"
10331                    "======= text by the vcs\n"
10332                    "  parameter2,\n"
10333                    "  parameter3),\n"
10334                    ">>>>>>> text by the vcs\n"
10335                    "  otherparameter);\n"));
10336 
10337   // Perforce markers.
10338   EXPECT_EQ("void f() {\n"
10339             "  function(\n"
10340             ">>>> text by the vcs\n"
10341             "      parameter,\n"
10342             "==== text by the vcs\n"
10343             "      parameter,\n"
10344             "==== text by the vcs\n"
10345             "      parameter,\n"
10346             "<<<< text by the vcs\n"
10347             "      parameter);\n",
10348             format("void f() {\n"
10349                    "  function(\n"
10350                    ">>>> text by the vcs\n"
10351                    "  parameter,\n"
10352                    "==== text by the vcs\n"
10353                    "  parameter,\n"
10354                    "==== text by the vcs\n"
10355                    "  parameter,\n"
10356                    "<<<< text by the vcs\n"
10357                    "  parameter);\n"));
10358 
10359   EXPECT_EQ("<<<<<<<\n"
10360             "|||||||\n"
10361             "=======\n"
10362             ">>>>>>>",
10363             format("<<<<<<<\n"
10364                    "|||||||\n"
10365                    "=======\n"
10366                    ">>>>>>>"));
10367 
10368   EXPECT_EQ("<<<<<<<\n"
10369             "|||||||\n"
10370             "int i;\n"
10371             "=======\n"
10372             ">>>>>>>",
10373             format("<<<<<<<\n"
10374                    "|||||||\n"
10375                    "int i;\n"
10376                    "=======\n"
10377                    ">>>>>>>"));
10378 
10379   // FIXME: Handle parsing of macros around conflict markers correctly:
10380   EXPECT_EQ("#define Macro \\\n"
10381             "<<<<<<<\n"
10382             "Something \\\n"
10383             "|||||||\n"
10384             "Else \\\n"
10385             "=======\n"
10386             "Other \\\n"
10387             ">>>>>>>\n"
10388             "    End int i;\n",
10389             format("#define Macro \\\n"
10390                    "<<<<<<<\n"
10391                    "  Something \\\n"
10392                    "|||||||\n"
10393                    "  Else \\\n"
10394                    "=======\n"
10395                    "  Other \\\n"
10396                    ">>>>>>>\n"
10397                    "  End\n"
10398                    "int i;\n"));
10399 }
10400 
10401 TEST_F(FormatTest, DisableRegions) {
10402   EXPECT_EQ("int i;\n"
10403             "// clang-format off\n"
10404             "  int j;\n"
10405             "// clang-format on\n"
10406             "int k;",
10407             format(" int  i;\n"
10408                    "   // clang-format off\n"
10409                    "  int j;\n"
10410                    " // clang-format on\n"
10411                    "   int   k;"));
10412   EXPECT_EQ("int i;\n"
10413             "/* clang-format off */\n"
10414             "  int j;\n"
10415             "/* clang-format on */\n"
10416             "int k;",
10417             format(" int  i;\n"
10418                    "   /* clang-format off */\n"
10419                    "  int j;\n"
10420                    " /* clang-format on */\n"
10421                    "   int   k;"));
10422 }
10423 
10424 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
10425   format("? ) =");
10426   verifyNoCrash("#define a\\\n /**/}");
10427 }
10428 
10429 } // end namespace
10430 } // end namespace format
10431 } // end namespace clang
10432