1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "FormatTestUtils.h"
11 #include "clang/Format/Format.h"
12 #include "llvm/Support/Debug.h"
13 #include "gtest/gtest.h"
14 
15 #define DEBUG_TYPE "format-test"
16 
17 namespace clang {
18 namespace format {
19 namespace {
20 
21 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
22 
23 class FormatTest : public ::testing::Test {
24 protected:
25   enum IncompleteCheck {
26     IC_ExpectComplete,
27     IC_ExpectIncomplete,
28     IC_DoNotCheck
29   };
30 
31   std::string format(llvm::StringRef Code,
32                      const FormatStyle &Style = getLLVMStyle(),
33                      IncompleteCheck CheckIncomplete = IC_ExpectComplete) {
34     DEBUG(llvm::errs() << "---\n");
35     DEBUG(llvm::errs() << Code << "\n\n");
36     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
37     bool IncompleteFormat = false;
38     tooling::Replacements Replaces =
39         reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
40     if (CheckIncomplete != IC_DoNotCheck) {
41       bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete;
42       EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n";
43     }
44     ReplacementCount = Replaces.size();
45     std::string Result = applyAllReplacements(Code, Replaces);
46     EXPECT_NE("", Result);
47     DEBUG(llvm::errs() << "\n" << Result << "\n\n");
48     return Result;
49   }
50 
51   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
52     FormatStyle Style = getLLVMStyle();
53     Style.ColumnLimit = ColumnLimit;
54     return Style;
55   }
56 
57   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
58     FormatStyle Style = getGoogleStyle();
59     Style.ColumnLimit = ColumnLimit;
60     return Style;
61   }
62 
63   void verifyFormat(llvm::StringRef Code,
64                     const FormatStyle &Style = getLLVMStyle()) {
65     EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
66   }
67 
68   void verifyIncompleteFormat(llvm::StringRef Code,
69                               const FormatStyle &Style = getLLVMStyle()) {
70     EXPECT_EQ(Code.str(),
71               format(test::messUp(Code), Style, IC_ExpectIncomplete));
72   }
73 
74   void verifyGoogleFormat(llvm::StringRef Code) {
75     verifyFormat(Code, getGoogleStyle());
76   }
77 
78   void verifyIndependentOfContext(llvm::StringRef text) {
79     verifyFormat(text);
80     verifyFormat(llvm::Twine("void f() { " + text + " }").str());
81   }
82 
83   /// \brief Verify that clang-format does not crash on the given input.
84   void verifyNoCrash(llvm::StringRef Code,
85                      const FormatStyle &Style = getLLVMStyle()) {
86     format(Code, Style, IC_DoNotCheck);
87   }
88 
89   int ReplacementCount;
90 };
91 
92 TEST_F(FormatTest, MessUp) {
93   EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
94   EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
95   EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
96   EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
97   EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
98 }
99 
100 //===----------------------------------------------------------------------===//
101 // Basic function tests.
102 //===----------------------------------------------------------------------===//
103 
104 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
105   EXPECT_EQ(";", format(";"));
106 }
107 
108 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
109   EXPECT_EQ("int i;", format("  int i;"));
110   EXPECT_EQ("\nint i;", format(" \n\t \v \f  int i;"));
111   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
112   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
113 }
114 
115 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
116   EXPECT_EQ("int i;", format("int\ni;"));
117 }
118 
119 TEST_F(FormatTest, FormatsNestedBlockStatements) {
120   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
121 }
122 
123 TEST_F(FormatTest, FormatsNestedCall) {
124   verifyFormat("Method(f1, f2(f3));");
125   verifyFormat("Method(f1(f2, f3()));");
126   verifyFormat("Method(f1(f2, (f3())));");
127 }
128 
129 TEST_F(FormatTest, NestedNameSpecifiers) {
130   verifyFormat("vector<::Type> v;");
131   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
132   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
133   verifyFormat("bool a = 2 < ::SomeFunction();");
134 }
135 
136 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
137   EXPECT_EQ("if (a) {\n"
138             "  f();\n"
139             "}",
140             format("if(a){f();}"));
141   EXPECT_EQ(4, ReplacementCount);
142   EXPECT_EQ("if (a) {\n"
143             "  f();\n"
144             "}",
145             format("if (a) {\n"
146                    "  f();\n"
147                    "}"));
148   EXPECT_EQ(0, ReplacementCount);
149   EXPECT_EQ("/*\r\n"
150             "\r\n"
151             "*/\r\n",
152             format("/*\r\n"
153                    "\r\n"
154                    "*/\r\n"));
155   EXPECT_EQ(0, ReplacementCount);
156 }
157 
158 TEST_F(FormatTest, RemovesEmptyLines) {
159   EXPECT_EQ("class C {\n"
160             "  int i;\n"
161             "};",
162             format("class C {\n"
163                    " int i;\n"
164                    "\n"
165                    "};"));
166 
167   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
168   EXPECT_EQ("namespace N {\n"
169             "\n"
170             "int i;\n"
171             "}",
172             format("namespace N {\n"
173                    "\n"
174                    "int    i;\n"
175                    "}",
176                    getGoogleStyle()));
177   EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
178             "\n"
179             "int i;\n"
180             "}",
181             format("extern /**/ \"C\" /**/ {\n"
182                    "\n"
183                    "int    i;\n"
184                    "}",
185                    getGoogleStyle()));
186 
187   // ...but do keep inlining and removing empty lines for non-block extern "C"
188   // functions.
189   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
190   EXPECT_EQ("extern \"C\" int f() {\n"
191             "  int i = 42;\n"
192             "  return i;\n"
193             "}",
194             format("extern \"C\" int f() {\n"
195                    "\n"
196                    "  int i = 42;\n"
197                    "  return i;\n"
198                    "}",
199                    getGoogleStyle()));
200 
201   // Remove empty lines at the beginning and end of blocks.
202   EXPECT_EQ("void f() {\n"
203             "\n"
204             "  if (a) {\n"
205             "\n"
206             "    f();\n"
207             "  }\n"
208             "}",
209             format("void f() {\n"
210                    "\n"
211                    "  if (a) {\n"
212                    "\n"
213                    "    f();\n"
214                    "\n"
215                    "  }\n"
216                    "\n"
217                    "}",
218                    getLLVMStyle()));
219   EXPECT_EQ("void f() {\n"
220             "  if (a) {\n"
221             "    f();\n"
222             "  }\n"
223             "}",
224             format("void f() {\n"
225                    "\n"
226                    "  if (a) {\n"
227                    "\n"
228                    "    f();\n"
229                    "\n"
230                    "  }\n"
231                    "\n"
232                    "}",
233                    getGoogleStyle()));
234 
235   // Don't remove empty lines in more complex control statements.
236   EXPECT_EQ("void f() {\n"
237             "  if (a) {\n"
238             "    f();\n"
239             "\n"
240             "  } else if (b) {\n"
241             "    f();\n"
242             "  }\n"
243             "}",
244             format("void f() {\n"
245                    "  if (a) {\n"
246                    "    f();\n"
247                    "\n"
248                    "  } else if (b) {\n"
249                    "    f();\n"
250                    "\n"
251                    "  }\n"
252                    "\n"
253                    "}"));
254 
255   // FIXME: This is slightly inconsistent.
256   EXPECT_EQ("namespace {\n"
257             "int i;\n"
258             "}",
259             format("namespace {\n"
260                    "int i;\n"
261                    "\n"
262                    "}"));
263   EXPECT_EQ("namespace {\n"
264             "int i;\n"
265             "\n"
266             "} // namespace",
267             format("namespace {\n"
268                    "int i;\n"
269                    "\n"
270                    "}  // namespace"));
271 }
272 
273 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
274   verifyFormat("x = (a) and (b);");
275   verifyFormat("x = (a) or (b);");
276   verifyFormat("x = (a) bitand (b);");
277   verifyFormat("x = (a) bitor (b);");
278   verifyFormat("x = (a) not_eq (b);");
279   verifyFormat("x = (a) and_eq (b);");
280   verifyFormat("x = (a) or_eq (b);");
281   verifyFormat("x = (a) xor (b);");
282 }
283 
284 //===----------------------------------------------------------------------===//
285 // Tests for control statements.
286 //===----------------------------------------------------------------------===//
287 
288 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
289   verifyFormat("if (true)\n  f();\ng();");
290   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
291   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
292 
293   FormatStyle AllowsMergedIf = getLLVMStyle();
294   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
295   verifyFormat("if (a)\n"
296                "  // comment\n"
297                "  f();",
298                AllowsMergedIf);
299   verifyFormat("if (a)\n"
300                "  ;",
301                AllowsMergedIf);
302   verifyFormat("if (a)\n"
303                "  if (b) return;",
304                AllowsMergedIf);
305 
306   verifyFormat("if (a) // Can't merge this\n"
307                "  f();\n",
308                AllowsMergedIf);
309   verifyFormat("if (a) /* still don't merge */\n"
310                "  f();",
311                AllowsMergedIf);
312   verifyFormat("if (a) { // Never merge this\n"
313                "  f();\n"
314                "}",
315                AllowsMergedIf);
316   verifyFormat("if (a) { /* Never merge this */\n"
317                "  f();\n"
318                "}",
319                AllowsMergedIf);
320 
321   AllowsMergedIf.ColumnLimit = 14;
322   verifyFormat("if (a) return;", AllowsMergedIf);
323   verifyFormat("if (aaaaaaaaa)\n"
324                "  return;",
325                AllowsMergedIf);
326 
327   AllowsMergedIf.ColumnLimit = 13;
328   verifyFormat("if (a)\n  return;", AllowsMergedIf);
329 }
330 
331 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
332   FormatStyle AllowsMergedLoops = getLLVMStyle();
333   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
334   verifyFormat("while (true) continue;", AllowsMergedLoops);
335   verifyFormat("for (;;) continue;", AllowsMergedLoops);
336   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
337   verifyFormat("while (true)\n"
338                "  ;",
339                AllowsMergedLoops);
340   verifyFormat("for (;;)\n"
341                "  ;",
342                AllowsMergedLoops);
343   verifyFormat("for (;;)\n"
344                "  for (;;) continue;",
345                AllowsMergedLoops);
346   verifyFormat("for (;;) // Can't merge this\n"
347                "  continue;",
348                AllowsMergedLoops);
349   verifyFormat("for (;;) /* still don't merge */\n"
350                "  continue;",
351                AllowsMergedLoops);
352 }
353 
354 TEST_F(FormatTest, FormatShortBracedStatements) {
355   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
356   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
357 
358   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
359   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
360 
361   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
362   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
363   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
364   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
365   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
366   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
367   verifyFormat("if (true) { //\n"
368                "  f();\n"
369                "}",
370                AllowSimpleBracedStatements);
371   verifyFormat("if (true) {\n"
372                "  f();\n"
373                "  f();\n"
374                "}",
375                AllowSimpleBracedStatements);
376   verifyFormat("if (true) {\n"
377                "  f();\n"
378                "} else {\n"
379                "  f();\n"
380                "}",
381                AllowSimpleBracedStatements);
382 
383   verifyFormat("template <int> struct A2 {\n"
384                "  struct B {};\n"
385                "};",
386                AllowSimpleBracedStatements);
387 
388   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
389   verifyFormat("if (true) {\n"
390                "  f();\n"
391                "}",
392                AllowSimpleBracedStatements);
393   verifyFormat("if (true) {\n"
394                "  f();\n"
395                "} else {\n"
396                "  f();\n"
397                "}",
398                AllowSimpleBracedStatements);
399 
400   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
401   verifyFormat("while (true) {\n"
402                "  f();\n"
403                "}",
404                AllowSimpleBracedStatements);
405   verifyFormat("for (;;) {\n"
406                "  f();\n"
407                "}",
408                AllowSimpleBracedStatements);
409 }
410 
411 TEST_F(FormatTest, ParseIfElse) {
412   verifyFormat("if (true)\n"
413                "  if (true)\n"
414                "    if (true)\n"
415                "      f();\n"
416                "    else\n"
417                "      g();\n"
418                "  else\n"
419                "    h();\n"
420                "else\n"
421                "  i();");
422   verifyFormat("if (true)\n"
423                "  if (true)\n"
424                "    if (true) {\n"
425                "      if (true)\n"
426                "        f();\n"
427                "    } else {\n"
428                "      g();\n"
429                "    }\n"
430                "  else\n"
431                "    h();\n"
432                "else {\n"
433                "  i();\n"
434                "}");
435   verifyFormat("void f() {\n"
436                "  if (a) {\n"
437                "  } else {\n"
438                "  }\n"
439                "}");
440 }
441 
442 TEST_F(FormatTest, ElseIf) {
443   verifyFormat("if (a) {\n} else if (b) {\n}");
444   verifyFormat("if (a)\n"
445                "  f();\n"
446                "else if (b)\n"
447                "  g();\n"
448                "else\n"
449                "  h();");
450   verifyFormat("if (a) {\n"
451                "  f();\n"
452                "}\n"
453                "// or else ..\n"
454                "else {\n"
455                "  g()\n"
456                "}");
457 
458   verifyFormat("if (a) {\n"
459                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
460                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
461                "}");
462   verifyFormat("if (a) {\n"
463                "} else if (\n"
464                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
465                "}",
466                getLLVMStyleWithColumns(62));
467 }
468 
469 TEST_F(FormatTest, FormatsForLoop) {
470   verifyFormat(
471       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
472       "     ++VeryVeryLongLoopVariable)\n"
473       "  ;");
474   verifyFormat("for (;;)\n"
475                "  f();");
476   verifyFormat("for (;;) {\n}");
477   verifyFormat("for (;;) {\n"
478                "  f();\n"
479                "}");
480   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
481 
482   verifyFormat(
483       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
484       "                                          E = UnwrappedLines.end();\n"
485       "     I != E; ++I) {\n}");
486 
487   verifyFormat(
488       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
489       "     ++IIIII) {\n}");
490   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
491                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
492                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
493   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
494                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
495                "         E = FD->getDeclsInPrototypeScope().end();\n"
496                "     I != E; ++I) {\n}");
497   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
498                "         I = Container.begin(),\n"
499                "         E = Container.end();\n"
500                "     I != E; ++I) {\n}",
501                getLLVMStyleWithColumns(76));
502 
503   verifyFormat(
504       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
505       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
506       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
507       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
508       "     ++aaaaaaaaaaa) {\n}");
509   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
510                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
511                "     ++i) {\n}");
512   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
513                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
514                "}");
515   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
516                "         aaaaaaaaaa);\n"
517                "     iter; ++iter) {\n"
518                "}");
519   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
520                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
521                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
522                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
523 
524   FormatStyle NoBinPacking = getLLVMStyle();
525   NoBinPacking.BinPackParameters = false;
526   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
527                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
528                "                                           aaaaaaaaaaaaaaaa,\n"
529                "                                           aaaaaaaaaaaaaaaa,\n"
530                "                                           aaaaaaaaaaaaaaaa);\n"
531                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
532                "}",
533                NoBinPacking);
534   verifyFormat(
535       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
536       "                                          E = UnwrappedLines.end();\n"
537       "     I != E;\n"
538       "     ++I) {\n}",
539       NoBinPacking);
540 }
541 
542 TEST_F(FormatTest, RangeBasedForLoops) {
543   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
544                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
545   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
546                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
547   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
548                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
549   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
550                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
551 }
552 
553 TEST_F(FormatTest, ForEachLoops) {
554   verifyFormat("void f() {\n"
555                "  foreach (Item *item, itemlist) {}\n"
556                "  Q_FOREACH (Item *item, itemlist) {}\n"
557                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
558                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
559                "}");
560 
561   // As function-like macros.
562   verifyFormat("#define foreach(x, y)\n"
563                "#define Q_FOREACH(x, y)\n"
564                "#define BOOST_FOREACH(x, y)\n"
565                "#define UNKNOWN_FOREACH(x, y)\n");
566 
567   // Not as function-like macros.
568   verifyFormat("#define foreach (x, y)\n"
569                "#define Q_FOREACH (x, y)\n"
570                "#define BOOST_FOREACH (x, y)\n"
571                "#define UNKNOWN_FOREACH (x, y)\n");
572 }
573 
574 TEST_F(FormatTest, FormatsWhileLoop) {
575   verifyFormat("while (true) {\n}");
576   verifyFormat("while (true)\n"
577                "  f();");
578   verifyFormat("while () {\n}");
579   verifyFormat("while () {\n"
580                "  f();\n"
581                "}");
582 }
583 
584 TEST_F(FormatTest, FormatsDoWhile) {
585   verifyFormat("do {\n"
586                "  do_something();\n"
587                "} while (something());");
588   verifyFormat("do\n"
589                "  do_something();\n"
590                "while (something());");
591 }
592 
593 TEST_F(FormatTest, FormatsSwitchStatement) {
594   verifyFormat("switch (x) {\n"
595                "case 1:\n"
596                "  f();\n"
597                "  break;\n"
598                "case kFoo:\n"
599                "case ns::kBar:\n"
600                "case kBaz:\n"
601                "  break;\n"
602                "default:\n"
603                "  g();\n"
604                "  break;\n"
605                "}");
606   verifyFormat("switch (x) {\n"
607                "case 1: {\n"
608                "  f();\n"
609                "  break;\n"
610                "}\n"
611                "case 2: {\n"
612                "  break;\n"
613                "}\n"
614                "}");
615   verifyFormat("switch (x) {\n"
616                "case 1: {\n"
617                "  f();\n"
618                "  {\n"
619                "    g();\n"
620                "    h();\n"
621                "  }\n"
622                "  break;\n"
623                "}\n"
624                "}");
625   verifyFormat("switch (x) {\n"
626                "case 1: {\n"
627                "  f();\n"
628                "  if (foo) {\n"
629                "    g();\n"
630                "    h();\n"
631                "  }\n"
632                "  break;\n"
633                "}\n"
634                "}");
635   verifyFormat("switch (x) {\n"
636                "case 1: {\n"
637                "  f();\n"
638                "  g();\n"
639                "} break;\n"
640                "}");
641   verifyFormat("switch (test)\n"
642                "  ;");
643   verifyFormat("switch (x) {\n"
644                "default: {\n"
645                "  // Do nothing.\n"
646                "}\n"
647                "}");
648   verifyFormat("switch (x) {\n"
649                "// comment\n"
650                "// if 1, do f()\n"
651                "case 1:\n"
652                "  f();\n"
653                "}");
654   verifyFormat("switch (x) {\n"
655                "case 1:\n"
656                "  // Do amazing stuff\n"
657                "  {\n"
658                "    f();\n"
659                "    g();\n"
660                "  }\n"
661                "  break;\n"
662                "}");
663   verifyFormat("#define A          \\\n"
664                "  switch (x) {     \\\n"
665                "  case a:          \\\n"
666                "    foo = b;       \\\n"
667                "  }",
668                getLLVMStyleWithColumns(20));
669   verifyFormat("#define OPERATION_CASE(name)           \\\n"
670                "  case OP_name:                        \\\n"
671                "    return operations::Operation##name\n",
672                getLLVMStyleWithColumns(40));
673   verifyFormat("switch (x) {\n"
674                "case 1:;\n"
675                "default:;\n"
676                "  int i;\n"
677                "}");
678 
679   verifyGoogleFormat("switch (x) {\n"
680                      "  case 1:\n"
681                      "    f();\n"
682                      "    break;\n"
683                      "  case kFoo:\n"
684                      "  case ns::kBar:\n"
685                      "  case kBaz:\n"
686                      "    break;\n"
687                      "  default:\n"
688                      "    g();\n"
689                      "    break;\n"
690                      "}");
691   verifyGoogleFormat("switch (x) {\n"
692                      "  case 1: {\n"
693                      "    f();\n"
694                      "    break;\n"
695                      "  }\n"
696                      "}");
697   verifyGoogleFormat("switch (test)\n"
698                      "  ;");
699 
700   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
701                      "  case OP_name:              \\\n"
702                      "    return operations::Operation##name\n");
703   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
704                      "  // Get the correction operation class.\n"
705                      "  switch (OpCode) {\n"
706                      "    CASE(Add);\n"
707                      "    CASE(Subtract);\n"
708                      "    default:\n"
709                      "      return operations::Unknown;\n"
710                      "  }\n"
711                      "#undef OPERATION_CASE\n"
712                      "}");
713   verifyFormat("DEBUG({\n"
714                "  switch (x) {\n"
715                "  case A:\n"
716                "    f();\n"
717                "    break;\n"
718                "  // On B:\n"
719                "  case B:\n"
720                "    g();\n"
721                "    break;\n"
722                "  }\n"
723                "});");
724   verifyFormat("switch (a) {\n"
725                "case (b):\n"
726                "  return;\n"
727                "}");
728 
729   verifyFormat("switch (a) {\n"
730                "case some_namespace::\n"
731                "    some_constant:\n"
732                "  return;\n"
733                "}",
734                getLLVMStyleWithColumns(34));
735 }
736 
737 TEST_F(FormatTest, CaseRanges) {
738   verifyFormat("switch (x) {\n"
739                "case 'A' ... 'Z':\n"
740                "case 1 ... 5:\n"
741                "  break;\n"
742                "}");
743 }
744 
745 TEST_F(FormatTest, ShortCaseLabels) {
746   FormatStyle Style = getLLVMStyle();
747   Style.AllowShortCaseLabelsOnASingleLine = true;
748   verifyFormat("switch (a) {\n"
749                "case 1: x = 1; break;\n"
750                "case 2: return;\n"
751                "case 3:\n"
752                "case 4:\n"
753                "case 5: return;\n"
754                "case 6: // comment\n"
755                "  return;\n"
756                "case 7:\n"
757                "  // comment\n"
758                "  return;\n"
759                "case 8:\n"
760                "  x = 8; // comment\n"
761                "  break;\n"
762                "default: y = 1; break;\n"
763                "}",
764                Style);
765   verifyFormat("switch (a) {\n"
766                "#if FOO\n"
767                "case 0: return 0;\n"
768                "#endif\n"
769                "}",
770                Style);
771   verifyFormat("switch (a) {\n"
772                "case 1: {\n"
773                "}\n"
774                "case 2: {\n"
775                "  return;\n"
776                "}\n"
777                "case 3: {\n"
778                "  x = 1;\n"
779                "  return;\n"
780                "}\n"
781                "case 4:\n"
782                "  if (x)\n"
783                "    return;\n"
784                "}",
785                Style);
786   Style.ColumnLimit = 21;
787   verifyFormat("switch (a) {\n"
788                "case 1: x = 1; break;\n"
789                "case 2: return;\n"
790                "case 3:\n"
791                "case 4:\n"
792                "case 5: return;\n"
793                "default:\n"
794                "  y = 1;\n"
795                "  break;\n"
796                "}",
797                Style);
798 }
799 
800 TEST_F(FormatTest, FormatsLabels) {
801   verifyFormat("void f() {\n"
802                "  some_code();\n"
803                "test_label:\n"
804                "  some_other_code();\n"
805                "  {\n"
806                "    some_more_code();\n"
807                "  another_label:\n"
808                "    some_more_code();\n"
809                "  }\n"
810                "}");
811   verifyFormat("{\n"
812                "  some_code();\n"
813                "test_label:\n"
814                "  some_other_code();\n"
815                "}");
816   verifyFormat("{\n"
817                "  some_code();\n"
818                "test_label:;\n"
819                "  int i = 0;\n"
820                "}");
821 }
822 
823 //===----------------------------------------------------------------------===//
824 // Tests for comments.
825 //===----------------------------------------------------------------------===//
826 
827 TEST_F(FormatTest, UnderstandsSingleLineComments) {
828   verifyFormat("//* */");
829   verifyFormat("// line 1\n"
830                "// line 2\n"
831                "void f() {}\n");
832 
833   verifyFormat("void f() {\n"
834                "  // Doesn't do anything\n"
835                "}");
836   verifyFormat("SomeObject\n"
837                "    // Calling someFunction on SomeObject\n"
838                "    .someFunction();");
839   verifyFormat("auto result = SomeObject\n"
840                "                  // Calling someFunction on SomeObject\n"
841                "                  .someFunction();");
842   verifyFormat("void f(int i,  // some comment (probably for i)\n"
843                "       int j,  // some comment (probably for j)\n"
844                "       int k); // some comment (probably for k)");
845   verifyFormat("void f(int i,\n"
846                "       // some comment (probably for j)\n"
847                "       int j,\n"
848                "       // some comment (probably for k)\n"
849                "       int k);");
850 
851   verifyFormat("int i    // This is a fancy variable\n"
852                "    = 5; // with nicely aligned comment.");
853 
854   verifyFormat("// Leading comment.\n"
855                "int a; // Trailing comment.");
856   verifyFormat("int a; // Trailing comment\n"
857                "       // on 2\n"
858                "       // or 3 lines.\n"
859                "int b;");
860   verifyFormat("int a; // Trailing comment\n"
861                "\n"
862                "// Leading comment.\n"
863                "int b;");
864   verifyFormat("int a;    // Comment.\n"
865                "          // More details.\n"
866                "int bbbb; // Another comment.");
867   verifyFormat(
868       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
869       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   // comment\n"
870       "int cccccccccccccccccccccccccccccc;       // comment\n"
871       "int ddd;                     // looooooooooooooooooooooooong comment\n"
872       "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
873       "int bbbbbbbbbbbbbbbbbbbbb;   // comment\n"
874       "int ccccccccccccccccccc;     // comment");
875 
876   verifyFormat("#include \"a\"     // comment\n"
877                "#include \"a/b/c\" // comment");
878   verifyFormat("#include <a>     // comment\n"
879                "#include <a/b/c> // comment");
880   EXPECT_EQ("#include \"a\"     // comment\n"
881             "#include \"a/b/c\" // comment",
882             format("#include \\\n"
883                    "  \"a\" // comment\n"
884                    "#include \"a/b/c\" // comment"));
885 
886   verifyFormat("enum E {\n"
887                "  // comment\n"
888                "  VAL_A, // comment\n"
889                "  VAL_B\n"
890                "};");
891 
892   verifyFormat(
893       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
894       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
895   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
896                "    // Comment inside a statement.\n"
897                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
898   verifyFormat("SomeFunction(a,\n"
899                "             // comment\n"
900                "             b + x);");
901   verifyFormat("SomeFunction(a, a,\n"
902                "             // comment\n"
903                "             b + x);");
904   verifyFormat(
905       "bool aaaaaaaaaaaaa = // comment\n"
906       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
907       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
908 
909   verifyFormat("int aaaa; // aaaaa\n"
910                "int aa;   // aaaaaaa",
911                getLLVMStyleWithColumns(20));
912 
913   EXPECT_EQ("void f() { // This does something ..\n"
914             "}\n"
915             "int a; // This is unrelated",
916             format("void f()    {     // This does something ..\n"
917                    "  }\n"
918                    "int   a;     // This is unrelated"));
919   EXPECT_EQ("class C {\n"
920             "  void f() { // This does something ..\n"
921             "  }          // awesome..\n"
922             "\n"
923             "  int a; // This is unrelated\n"
924             "};",
925             format("class C{void f()    { // This does something ..\n"
926                    "      } // awesome..\n"
927                    " \n"
928                    "int a;    // This is unrelated\n"
929                    "};"));
930 
931   EXPECT_EQ("int i; // single line trailing comment",
932             format("int i;\\\n// single line trailing comment"));
933 
934   verifyGoogleFormat("int a;  // Trailing comment.");
935 
936   verifyFormat("someFunction(anotherFunction( // Force break.\n"
937                "    parameter));");
938 
939   verifyGoogleFormat("#endif  // HEADER_GUARD");
940 
941   verifyFormat("const char *test[] = {\n"
942                "    // A\n"
943                "    \"aaaa\",\n"
944                "    // B\n"
945                "    \"aaaaa\"};");
946   verifyGoogleFormat(
947       "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
948       "    aaaaaaaaaaaaaaaaaaaaaa);  // 81_cols_with_this_comment");
949   EXPECT_EQ("D(a, {\n"
950             "  // test\n"
951             "  int a;\n"
952             "});",
953             format("D(a, {\n"
954                    "// test\n"
955                    "int a;\n"
956                    "});"));
957 
958   EXPECT_EQ("lineWith(); // comment\n"
959             "// at start\n"
960             "otherLine();",
961             format("lineWith();   // comment\n"
962                    "// at start\n"
963                    "otherLine();"));
964   EXPECT_EQ("lineWith(); // comment\n"
965             "            // at start\n"
966             "otherLine();",
967             format("lineWith();   // comment\n"
968                    " // at start\n"
969                    "otherLine();"));
970 
971   EXPECT_EQ("lineWith(); // comment\n"
972             "// at start\n"
973             "otherLine(); // comment",
974             format("lineWith();   // comment\n"
975                    "// at start\n"
976                    "otherLine();   // comment"));
977   EXPECT_EQ("lineWith();\n"
978             "// at start\n"
979             "otherLine(); // comment",
980             format("lineWith();\n"
981                    " // at start\n"
982                    "otherLine();   // comment"));
983   EXPECT_EQ("// first\n"
984             "// at start\n"
985             "otherLine(); // comment",
986             format("// first\n"
987                    " // at start\n"
988                    "otherLine();   // comment"));
989   EXPECT_EQ("f();\n"
990             "// first\n"
991             "// at start\n"
992             "otherLine(); // comment",
993             format("f();\n"
994                    "// first\n"
995                    " // at start\n"
996                    "otherLine();   // comment"));
997   verifyFormat("f(); // comment\n"
998                "// first\n"
999                "// at start\n"
1000                "otherLine();");
1001   EXPECT_EQ("f(); // comment\n"
1002             "// first\n"
1003             "// at start\n"
1004             "otherLine();",
1005             format("f();   // comment\n"
1006                    "// first\n"
1007                    " // at start\n"
1008                    "otherLine();"));
1009   EXPECT_EQ("f(); // comment\n"
1010             "     // first\n"
1011             "// at start\n"
1012             "otherLine();",
1013             format("f();   // comment\n"
1014                    " // first\n"
1015                    "// at start\n"
1016                    "otherLine();"));
1017   EXPECT_EQ("void f() {\n"
1018             "  lineWith(); // comment\n"
1019             "  // at start\n"
1020             "}",
1021             format("void              f() {\n"
1022                    "  lineWith(); // comment\n"
1023                    "  // at start\n"
1024                    "}"));
1025   EXPECT_EQ("int xy; // a\n"
1026             "int z;  // b",
1027             format("int xy;    // a\n"
1028                    "int z;    //b"));
1029   EXPECT_EQ("int xy; // a\n"
1030             "int z; // bb",
1031             format("int xy;    // a\n"
1032                    "int z;    //bb",
1033                    getLLVMStyleWithColumns(12)));
1034 
1035   verifyFormat("#define A                                                  \\\n"
1036                "  int i; /* iiiiiiiiiiiiiiiiiiiii */                       \\\n"
1037                "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
1038                getLLVMStyleWithColumns(60));
1039   verifyFormat(
1040       "#define A                                                   \\\n"
1041       "  int i;                        /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
1042       "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
1043       getLLVMStyleWithColumns(61));
1044 
1045   verifyFormat("if ( // This is some comment\n"
1046                "    x + 3) {\n"
1047                "}");
1048   EXPECT_EQ("if ( // This is some comment\n"
1049             "     // spanning two lines\n"
1050             "    x + 3) {\n"
1051             "}",
1052             format("if( // This is some comment\n"
1053                    "     // spanning two lines\n"
1054                    " x + 3) {\n"
1055                    "}"));
1056 
1057   verifyNoCrash("/\\\n/");
1058   verifyNoCrash("/\\\n* */");
1059   // The 0-character somehow makes the lexer return a proper comment.
1060   verifyNoCrash(StringRef("/*\\\0\n/", 6));
1061 }
1062 
1063 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
1064   EXPECT_EQ("SomeFunction(a,\n"
1065             "             b, // comment\n"
1066             "             c);",
1067             format("SomeFunction(a,\n"
1068                    "          b, // comment\n"
1069                    "      c);"));
1070   EXPECT_EQ("SomeFunction(a, b,\n"
1071             "             // comment\n"
1072             "             c);",
1073             format("SomeFunction(a,\n"
1074                    "          b,\n"
1075                    "  // comment\n"
1076                    "      c);"));
1077   EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
1078             "             c);",
1079             format("SomeFunction(a, b, // comment (unclear relation)\n"
1080                    "      c);"));
1081   EXPECT_EQ("SomeFunction(a, // comment\n"
1082             "             b,\n"
1083             "             c); // comment",
1084             format("SomeFunction(a,     // comment\n"
1085                    "          b,\n"
1086                    "      c); // comment"));
1087 }
1088 
1089 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
1090   EXPECT_EQ("// comment", format("// comment  "));
1091   EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
1092             format("int aaaaaaa, bbbbbbb; // comment                   ",
1093                    getLLVMStyleWithColumns(33)));
1094   EXPECT_EQ("// comment\\\n", format("// comment\\\n  \t \v   \f   "));
1095   EXPECT_EQ("// comment    \\\n", format("// comment    \\\n  \t \v   \f   "));
1096 }
1097 
1098 TEST_F(FormatTest, UnderstandsBlockComments) {
1099   verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
1100   verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
1101   EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
1102             "  bbbbbbbbbbbbbbbbbbbbbbbbb);",
1103             format("f(aaaaaaaaaaaaaaaaaaaaaaaaa ,   \\\n"
1104                    "/* Trailing comment for aa... */\n"
1105                    "  bbbbbbbbbbbbbbbbbbbbbbbbb);"));
1106   EXPECT_EQ(
1107       "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1108       "  /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
1109       format("f(aaaaaaaaaaaaaaaaaaaaaaaaa    ,   \n"
1110              "/* Leading comment for bb... */   bbbbbbbbbbbbbbbbbbbbbbbbb);"));
1111   EXPECT_EQ(
1112       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1113       "    aaaaaaaaaaaaaaaaaa,\n"
1114       "    aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
1115       "}",
1116       format("void      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1117              "                      aaaaaaaaaaaaaaaaaa  ,\n"
1118              "    aaaaaaaaaaaaaaaaaa) {   /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
1119              "}"));
1120 
1121   FormatStyle NoBinPacking = getLLVMStyle();
1122   NoBinPacking.BinPackParameters = false;
1123   verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
1124                "         /* parameter 2 */ aaaaaa,\n"
1125                "         /* parameter 3 */ aaaaaa,\n"
1126                "         /* parameter 4 */ aaaaaa);",
1127                NoBinPacking);
1128 
1129   // Aligning block comments in macros.
1130   verifyGoogleFormat("#define A        \\\n"
1131                      "  int i;   /*a*/ \\\n"
1132                      "  int jjj; /*b*/");
1133 }
1134 
1135 TEST_F(FormatTest, AlignsBlockComments) {
1136   EXPECT_EQ("/*\n"
1137             " * Really multi-line\n"
1138             " * comment.\n"
1139             " */\n"
1140             "void f() {}",
1141             format("  /*\n"
1142                    "   * Really multi-line\n"
1143                    "   * comment.\n"
1144                    "   */\n"
1145                    "  void f() {}"));
1146   EXPECT_EQ("class C {\n"
1147             "  /*\n"
1148             "   * Another multi-line\n"
1149             "   * comment.\n"
1150             "   */\n"
1151             "  void f() {}\n"
1152             "};",
1153             format("class C {\n"
1154                    "/*\n"
1155                    " * Another multi-line\n"
1156                    " * comment.\n"
1157                    " */\n"
1158                    "void f() {}\n"
1159                    "};"));
1160   EXPECT_EQ("/*\n"
1161             "  1. This is a comment with non-trivial formatting.\n"
1162             "     1.1. We have to indent/outdent all lines equally\n"
1163             "         1.1.1. to keep the formatting.\n"
1164             " */",
1165             format("  /*\n"
1166                    "    1. This is a comment with non-trivial formatting.\n"
1167                    "       1.1. We have to indent/outdent all lines equally\n"
1168                    "           1.1.1. to keep the formatting.\n"
1169                    "   */"));
1170   EXPECT_EQ("/*\n"
1171             "Don't try to outdent if there's not enough indentation.\n"
1172             "*/",
1173             format("  /*\n"
1174                    " Don't try to outdent if there's not enough indentation.\n"
1175                    " */"));
1176 
1177   EXPECT_EQ("int i; /* Comment with empty...\n"
1178             "        *\n"
1179             "        * line. */",
1180             format("int i; /* Comment with empty...\n"
1181                    "        *\n"
1182                    "        * line. */"));
1183   EXPECT_EQ("int foobar = 0; /* comment */\n"
1184             "int bar = 0;    /* multiline\n"
1185             "                   comment 1 */\n"
1186             "int baz = 0;    /* multiline\n"
1187             "                   comment 2 */\n"
1188             "int bzz = 0;    /* multiline\n"
1189             "                   comment 3 */",
1190             format("int foobar = 0; /* comment */\n"
1191                    "int bar = 0;    /* multiline\n"
1192                    "                   comment 1 */\n"
1193                    "int baz = 0; /* multiline\n"
1194                    "                comment 2 */\n"
1195                    "int bzz = 0;         /* multiline\n"
1196                    "                        comment 3 */"));
1197   EXPECT_EQ("int foobar = 0; /* comment */\n"
1198             "int bar = 0;    /* multiline\n"
1199             "   comment */\n"
1200             "int baz = 0;    /* multiline\n"
1201             "comment */",
1202             format("int foobar = 0; /* comment */\n"
1203                    "int bar = 0; /* multiline\n"
1204                    "comment */\n"
1205                    "int baz = 0;        /* multiline\n"
1206                    "comment */"));
1207 }
1208 
1209 TEST_F(FormatTest, CommentReflowingCanBeTurnedOff) {
1210   FormatStyle Style = getLLVMStyleWithColumns(20);
1211   Style.ReflowComments = false;
1212   verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style);
1213   verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style);
1214 }
1215 
1216 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
1217   EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1218             "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
1219             format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1220                    "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
1221   EXPECT_EQ(
1222       "void ffffffffffff(\n"
1223       "    int aaaaaaaa, int bbbbbbbb,\n"
1224       "    int cccccccccccc) { /*\n"
1225       "                           aaaaaaaaaa\n"
1226       "                           aaaaaaaaaaaaa\n"
1227       "                           bbbbbbbbbbbbbb\n"
1228       "                           bbbbbbbbbb\n"
1229       "                         */\n"
1230       "}",
1231       format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
1232              "{ /*\n"
1233              "     aaaaaaaaaa aaaaaaaaaaaaa\n"
1234              "     bbbbbbbbbbbbbb bbbbbbbbbb\n"
1235              "   */\n"
1236              "}",
1237              getLLVMStyleWithColumns(40)));
1238 }
1239 
1240 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
1241   EXPECT_EQ("void ffffffffff(\n"
1242             "    int aaaaa /* test */);",
1243             format("void ffffffffff(int aaaaa /* test */);",
1244                    getLLVMStyleWithColumns(35)));
1245 }
1246 
1247 TEST_F(FormatTest, SplitsLongCxxComments) {
1248   EXPECT_EQ("// A comment that\n"
1249             "// doesn't fit on\n"
1250             "// one line",
1251             format("// A comment that doesn't fit on one line",
1252                    getLLVMStyleWithColumns(20)));
1253   EXPECT_EQ("/// A comment that\n"
1254             "/// doesn't fit on\n"
1255             "/// one line",
1256             format("/// A comment that doesn't fit on one line",
1257                    getLLVMStyleWithColumns(20)));
1258   EXPECT_EQ("//! A comment that\n"
1259             "//! doesn't fit on\n"
1260             "//! one line",
1261             format("//! A comment that doesn't fit on one line",
1262                    getLLVMStyleWithColumns(20)));
1263   EXPECT_EQ("// a b c d\n"
1264             "// e f  g\n"
1265             "// h i j k",
1266             format("// a b c d e f  g h i j k", getLLVMStyleWithColumns(10)));
1267   EXPECT_EQ(
1268       "// a b c d\n"
1269       "// e f  g\n"
1270       "// h i j k",
1271       format("\\\n// a b c d e f  g h i j k", getLLVMStyleWithColumns(10)));
1272   EXPECT_EQ("if (true) // A comment that\n"
1273             "          // doesn't fit on\n"
1274             "          // one line",
1275             format("if (true) // A comment that doesn't fit on one line   ",
1276                    getLLVMStyleWithColumns(30)));
1277   EXPECT_EQ("//    Don't_touch_leading_whitespace",
1278             format("//    Don't_touch_leading_whitespace",
1279                    getLLVMStyleWithColumns(20)));
1280   EXPECT_EQ("// Add leading\n"
1281             "// whitespace",
1282             format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
1283   EXPECT_EQ("/// Add leading\n"
1284             "/// whitespace",
1285             format("///Add leading whitespace", getLLVMStyleWithColumns(20)));
1286   EXPECT_EQ("//! Add leading\n"
1287             "//! whitespace",
1288             format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
1289   EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
1290   EXPECT_EQ("// Even if it makes the line exceed the column\n"
1291             "// limit",
1292             format("//Even if it makes the line exceed the column limit",
1293                    getLLVMStyleWithColumns(51)));
1294   EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
1295 
1296   EXPECT_EQ("// aa bb cc dd",
1297             format("// aa bb             cc dd                   ",
1298                    getLLVMStyleWithColumns(15)));
1299 
1300   EXPECT_EQ("// A comment before\n"
1301             "// a macro\n"
1302             "// definition\n"
1303             "#define a b",
1304             format("// A comment before a macro definition\n"
1305                    "#define a b",
1306                    getLLVMStyleWithColumns(20)));
1307   EXPECT_EQ("void ffffff(\n"
1308             "    int aaaaaaaaa,  // wwww\n"
1309             "    int bbbbbbbbbb, // xxxxxxx\n"
1310             "                    // yyyyyyyyyy\n"
1311             "    int c, int d, int e) {}",
1312             format("void ffffff(\n"
1313                    "    int aaaaaaaaa, // wwww\n"
1314                    "    int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
1315                    "    int c, int d, int e) {}",
1316                    getLLVMStyleWithColumns(40)));
1317   EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1318             format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1319                    getLLVMStyleWithColumns(20)));
1320   EXPECT_EQ(
1321       "#define XXX // a b c d\n"
1322       "            // e f g h",
1323       format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
1324   EXPECT_EQ(
1325       "#define XXX // q w e r\n"
1326       "            // t y u i",
1327       format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
1328 }
1329 
1330 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
1331   EXPECT_EQ("//     A comment\n"
1332             "//     that doesn't\n"
1333             "//     fit on one\n"
1334             "//     line",
1335             format("//     A comment that doesn't fit on one line",
1336                    getLLVMStyleWithColumns(20)));
1337   EXPECT_EQ("///     A comment\n"
1338             "///     that doesn't\n"
1339             "///     fit on one\n"
1340             "///     line",
1341             format("///     A comment that doesn't fit on one line",
1342                    getLLVMStyleWithColumns(20)));
1343 }
1344 
1345 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
1346   EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1347             "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1348             "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1349             format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1350                    "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1351                    "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
1352   EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1353             "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1354             "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1355             format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1356                    "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1357                    "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1358                    getLLVMStyleWithColumns(50)));
1359   // FIXME: One day we might want to implement adjustment of leading whitespace
1360   // of the consecutive lines in this kind of comment:
1361   EXPECT_EQ("double\n"
1362             "    a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1363             "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1364             "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1365             format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1366                    "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1367                    "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1368                    getLLVMStyleWithColumns(49)));
1369 }
1370 
1371 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
1372   FormatStyle Pragmas = getLLVMStyleWithColumns(30);
1373   Pragmas.CommentPragmas = "^ IWYU pragma:";
1374   EXPECT_EQ(
1375       "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
1376       format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
1377   EXPECT_EQ(
1378       "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
1379       format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
1380 }
1381 
1382 TEST_F(FormatTest, PriorityOfCommentBreaking) {
1383   EXPECT_EQ("if (xxx ==\n"
1384             "        yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
1385             "    zzz)\n"
1386             "  q();",
1387             format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
1388                    "    zzz) q();",
1389                    getLLVMStyleWithColumns(40)));
1390   EXPECT_EQ("if (xxxxxxxxxx ==\n"
1391             "        yyy && // aaaaaa bbbbbbbb cccc\n"
1392             "    zzz)\n"
1393             "  q();",
1394             format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
1395                    "    zzz) q();",
1396                    getLLVMStyleWithColumns(40)));
1397   EXPECT_EQ("if (xxxxxxxxxx &&\n"
1398             "        yyy || // aaaaaa bbbbbbbb cccc\n"
1399             "    zzz)\n"
1400             "  q();",
1401             format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
1402                    "    zzz) q();",
1403                    getLLVMStyleWithColumns(40)));
1404   EXPECT_EQ("fffffffff(\n"
1405             "    &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
1406             "    zzz);",
1407             format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
1408                    " zzz);",
1409                    getLLVMStyleWithColumns(40)));
1410 }
1411 
1412 TEST_F(FormatTest, MultiLineCommentsInDefines) {
1413   EXPECT_EQ("#define A(x) /* \\\n"
1414             "  a comment     \\\n"
1415             "  inside */     \\\n"
1416             "  f();",
1417             format("#define A(x) /* \\\n"
1418                    "  a comment     \\\n"
1419                    "  inside */     \\\n"
1420                    "  f();",
1421                    getLLVMStyleWithColumns(17)));
1422   EXPECT_EQ("#define A(      \\\n"
1423             "    x) /*       \\\n"
1424             "  a comment     \\\n"
1425             "  inside */     \\\n"
1426             "  f();",
1427             format("#define A(      \\\n"
1428                    "    x) /*       \\\n"
1429                    "  a comment     \\\n"
1430                    "  inside */     \\\n"
1431                    "  f();",
1432                    getLLVMStyleWithColumns(17)));
1433 }
1434 
1435 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
1436   EXPECT_EQ("namespace {}\n// Test\n#define A",
1437             format("namespace {}\n   // Test\n#define A"));
1438   EXPECT_EQ("namespace {}\n/* Test */\n#define A",
1439             format("namespace {}\n   /* Test */\n#define A"));
1440   EXPECT_EQ("namespace {}\n/* Test */ #define A",
1441             format("namespace {}\n   /* Test */    #define A"));
1442 }
1443 
1444 TEST_F(FormatTest, SplitsLongLinesInComments) {
1445   EXPECT_EQ("/* This is a long\n"
1446             " * comment that\n"
1447             " * doesn't\n"
1448             " * fit on one line.\n"
1449             " */",
1450             format("/* "
1451                    "This is a long                                         "
1452                    "comment that "
1453                    "doesn't                                    "
1454                    "fit on one line.  */",
1455                    getLLVMStyleWithColumns(20)));
1456   EXPECT_EQ(
1457       "/* a b c d\n"
1458       " * e f  g\n"
1459       " * h i j k\n"
1460       " */",
1461       format("/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
1462   EXPECT_EQ(
1463       "/* a b c d\n"
1464       " * e f  g\n"
1465       " * h i j k\n"
1466       " */",
1467       format("\\\n/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
1468   EXPECT_EQ("/*\n"
1469             "This is a long\n"
1470             "comment that doesn't\n"
1471             "fit on one line.\n"
1472             "*/",
1473             format("/*\n"
1474                    "This is a long                                         "
1475                    "comment that doesn't                                    "
1476                    "fit on one line.                                      \n"
1477                    "*/",
1478                    getLLVMStyleWithColumns(20)));
1479   EXPECT_EQ("/*\n"
1480             " * This is a long\n"
1481             " * comment that\n"
1482             " * doesn't fit on\n"
1483             " * one line.\n"
1484             " */",
1485             format("/*      \n"
1486                    " * This is a long "
1487                    "   comment that     "
1488                    "   doesn't fit on   "
1489                    "   one line.                                            \n"
1490                    " */",
1491                    getLLVMStyleWithColumns(20)));
1492   EXPECT_EQ("/*\n"
1493             " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
1494             " * so_it_should_be_broken\n"
1495             " * wherever_a_space_occurs\n"
1496             " */",
1497             format("/*\n"
1498                    " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
1499                    "   so_it_should_be_broken "
1500                    "   wherever_a_space_occurs                             \n"
1501                    " */",
1502                    getLLVMStyleWithColumns(20)));
1503   EXPECT_EQ("/*\n"
1504             " *    This_comment_can_not_be_broken_into_lines\n"
1505             " */",
1506             format("/*\n"
1507                    " *    This_comment_can_not_be_broken_into_lines\n"
1508                    " */",
1509                    getLLVMStyleWithColumns(20)));
1510   EXPECT_EQ("{\n"
1511             "  /*\n"
1512             "  This is another\n"
1513             "  long comment that\n"
1514             "  doesn't fit on one\n"
1515             "  line    1234567890\n"
1516             "  */\n"
1517             "}",
1518             format("{\n"
1519                    "/*\n"
1520                    "This is another     "
1521                    "  long comment that "
1522                    "  doesn't fit on one"
1523                    "  line    1234567890\n"
1524                    "*/\n"
1525                    "}",
1526                    getLLVMStyleWithColumns(20)));
1527   EXPECT_EQ("{\n"
1528             "  /*\n"
1529             "   * This        i s\n"
1530             "   * another comment\n"
1531             "   * t hat  doesn' t\n"
1532             "   * fit on one l i\n"
1533             "   * n e\n"
1534             "   */\n"
1535             "}",
1536             format("{\n"
1537                    "/*\n"
1538                    " * This        i s"
1539                    "   another comment"
1540                    "   t hat  doesn' t"
1541                    "   fit on one l i"
1542                    "   n e\n"
1543                    " */\n"
1544                    "}",
1545                    getLLVMStyleWithColumns(20)));
1546   EXPECT_EQ("/*\n"
1547             " * This is a long\n"
1548             " * comment that\n"
1549             " * doesn't fit on\n"
1550             " * one line\n"
1551             " */",
1552             format("   /*\n"
1553                    "    * This is a long comment that doesn't fit on one line\n"
1554                    "    */",
1555                    getLLVMStyleWithColumns(20)));
1556   EXPECT_EQ("{\n"
1557             "  if (something) /* This is a\n"
1558             "                    long\n"
1559             "                    comment */\n"
1560             "    ;\n"
1561             "}",
1562             format("{\n"
1563                    "  if (something) /* This is a long comment */\n"
1564                    "    ;\n"
1565                    "}",
1566                    getLLVMStyleWithColumns(30)));
1567 
1568   EXPECT_EQ("/* A comment before\n"
1569             " * a macro\n"
1570             " * definition */\n"
1571             "#define a b",
1572             format("/* A comment before a macro definition */\n"
1573                    "#define a b",
1574                    getLLVMStyleWithColumns(20)));
1575 
1576   EXPECT_EQ("/* some comment\n"
1577             "     *   a comment\n"
1578             "* that we break\n"
1579             " * another comment\n"
1580             "* we have to break\n"
1581             "* a left comment\n"
1582             " */",
1583             format("  /* some comment\n"
1584                    "       *   a comment that we break\n"
1585                    "   * another comment we have to break\n"
1586                    "* a left comment\n"
1587                    "   */",
1588                    getLLVMStyleWithColumns(20)));
1589 
1590   EXPECT_EQ("/**\n"
1591             " * multiline block\n"
1592             " * comment\n"
1593             " *\n"
1594             " */",
1595             format("/**\n"
1596                    " * multiline block comment\n"
1597                    " *\n"
1598                    " */",
1599                    getLLVMStyleWithColumns(20)));
1600 
1601   EXPECT_EQ("/*\n"
1602             "\n"
1603             "\n"
1604             "    */\n",
1605             format("  /*       \n"
1606                    "      \n"
1607                    "               \n"
1608                    "      */\n"));
1609 
1610   EXPECT_EQ("/* a a */",
1611             format("/* a a            */", getLLVMStyleWithColumns(15)));
1612   EXPECT_EQ("/* a a bc  */",
1613             format("/* a a            bc  */", getLLVMStyleWithColumns(15)));
1614   EXPECT_EQ("/* aaa aaa\n"
1615             " * aaaaa */",
1616             format("/* aaa aaa aaaaa       */", getLLVMStyleWithColumns(15)));
1617   EXPECT_EQ("/* aaa aaa\n"
1618             " * aaaaa     */",
1619             format("/* aaa aaa aaaaa     */", getLLVMStyleWithColumns(15)));
1620 }
1621 
1622 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
1623   EXPECT_EQ("#define X          \\\n"
1624             "  /*               \\\n"
1625             "   Test            \\\n"
1626             "   Macro comment   \\\n"
1627             "   with a long     \\\n"
1628             "   line            \\\n"
1629             "   */              \\\n"
1630             "  A + B",
1631             format("#define X \\\n"
1632                    "  /*\n"
1633                    "   Test\n"
1634                    "   Macro comment with a long  line\n"
1635                    "   */ \\\n"
1636                    "  A + B",
1637                    getLLVMStyleWithColumns(20)));
1638   EXPECT_EQ("#define X          \\\n"
1639             "  /* Macro comment \\\n"
1640             "     with a long   \\\n"
1641             "     line */       \\\n"
1642             "  A + B",
1643             format("#define X \\\n"
1644                    "  /* Macro comment with a long\n"
1645                    "     line */ \\\n"
1646                    "  A + B",
1647                    getLLVMStyleWithColumns(20)));
1648   EXPECT_EQ("#define X          \\\n"
1649             "  /* Macro comment \\\n"
1650             "   * with a long   \\\n"
1651             "   * line */       \\\n"
1652             "  A + B",
1653             format("#define X \\\n"
1654                    "  /* Macro comment with a long  line */ \\\n"
1655                    "  A + B",
1656                    getLLVMStyleWithColumns(20)));
1657 }
1658 
1659 TEST_F(FormatTest, CommentsInStaticInitializers) {
1660   EXPECT_EQ(
1661       "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
1662       "                        aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
1663       "                        /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
1664       "                        aaaaaaaaaaaaaaaaaaaa, // comment\n"
1665       "                        aaaaaaaaaaaaaaaaaaaa};",
1666       format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa  ,  /* comment */\n"
1667              "                   aaaaaaaaaaaaaaaaaaaa   /* comment */ ,\n"
1668              "                     /* comment */   aaaaaaaaaaaaaaaaaaaa ,\n"
1669              "              aaaaaaaaaaaaaaaaaaaa ,   // comment\n"
1670              "                  aaaaaaaaaaaaaaaaaaaa };"));
1671   verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1672                "                        bbbbbbbbbbb, ccccccccccc};");
1673   verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
1674                "                        // comment for bb....\n"
1675                "                        bbbbbbbbbbb, ccccccccccc};");
1676   verifyGoogleFormat(
1677       "static SomeType type = {aaaaaaaaaaa,  // comment for aa...\n"
1678       "                        bbbbbbbbbbb, ccccccccccc};");
1679   verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
1680                      "                        // comment for bb....\n"
1681                      "                        bbbbbbbbbbb, ccccccccccc};");
1682 
1683   verifyFormat("S s = {{a, b, c},  // Group #1\n"
1684                "       {d, e, f},  // Group #2\n"
1685                "       {g, h, i}}; // Group #3");
1686   verifyFormat("S s = {{// Group #1\n"
1687                "        a, b, c},\n"
1688                "       {// Group #2\n"
1689                "        d, e, f},\n"
1690                "       {// Group #3\n"
1691                "        g, h, i}};");
1692 
1693   EXPECT_EQ("S s = {\n"
1694             "    // Some comment\n"
1695             "    a,\n"
1696             "\n"
1697             "    // Comment after empty line\n"
1698             "    b}",
1699             format("S s =    {\n"
1700                    "      // Some comment\n"
1701                    "  a,\n"
1702                    "  \n"
1703                    "     // Comment after empty line\n"
1704                    "      b\n"
1705                    "}"));
1706   EXPECT_EQ("S s = {\n"
1707             "    /* Some comment */\n"
1708             "    a,\n"
1709             "\n"
1710             "    /* Comment after empty line */\n"
1711             "    b}",
1712             format("S s =    {\n"
1713                    "      /* Some comment */\n"
1714                    "  a,\n"
1715                    "  \n"
1716                    "     /* Comment after empty line */\n"
1717                    "      b\n"
1718                    "}"));
1719   verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
1720                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1721                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1722                "    0x00, 0x00, 0x00, 0x00};            // comment\n");
1723 }
1724 
1725 TEST_F(FormatTest, IgnoresIf0Contents) {
1726   EXPECT_EQ("#if 0\n"
1727             "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1728             "#endif\n"
1729             "void f() {}",
1730             format("#if 0\n"
1731                    "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1732                    "#endif\n"
1733                    "void f(  ) {  }"));
1734   EXPECT_EQ("#if false\n"
1735             "void f(  ) {  }\n"
1736             "#endif\n"
1737             "void g() {}\n",
1738             format("#if false\n"
1739                    "void f(  ) {  }\n"
1740                    "#endif\n"
1741                    "void g(  ) {  }\n"));
1742   EXPECT_EQ("enum E {\n"
1743             "  One,\n"
1744             "  Two,\n"
1745             "#if 0\n"
1746             "Three,\n"
1747             "      Four,\n"
1748             "#endif\n"
1749             "  Five\n"
1750             "};",
1751             format("enum E {\n"
1752                    "  One,Two,\n"
1753                    "#if 0\n"
1754                    "Three,\n"
1755                    "      Four,\n"
1756                    "#endif\n"
1757                    "  Five};"));
1758   EXPECT_EQ("enum F {\n"
1759             "  One,\n"
1760             "#if 1\n"
1761             "  Two,\n"
1762             "#if 0\n"
1763             "Three,\n"
1764             "      Four,\n"
1765             "#endif\n"
1766             "  Five\n"
1767             "#endif\n"
1768             "};",
1769             format("enum F {\n"
1770                    "One,\n"
1771                    "#if 1\n"
1772                    "Two,\n"
1773                    "#if 0\n"
1774                    "Three,\n"
1775                    "      Four,\n"
1776                    "#endif\n"
1777                    "Five\n"
1778                    "#endif\n"
1779                    "};"));
1780   EXPECT_EQ("enum G {\n"
1781             "  One,\n"
1782             "#if 0\n"
1783             "Two,\n"
1784             "#else\n"
1785             "  Three,\n"
1786             "#endif\n"
1787             "  Four\n"
1788             "};",
1789             format("enum G {\n"
1790                    "One,\n"
1791                    "#if 0\n"
1792                    "Two,\n"
1793                    "#else\n"
1794                    "Three,\n"
1795                    "#endif\n"
1796                    "Four\n"
1797                    "};"));
1798   EXPECT_EQ("enum H {\n"
1799             "  One,\n"
1800             "#if 0\n"
1801             "#ifdef Q\n"
1802             "Two,\n"
1803             "#else\n"
1804             "Three,\n"
1805             "#endif\n"
1806             "#endif\n"
1807             "  Four\n"
1808             "};",
1809             format("enum H {\n"
1810                    "One,\n"
1811                    "#if 0\n"
1812                    "#ifdef Q\n"
1813                    "Two,\n"
1814                    "#else\n"
1815                    "Three,\n"
1816                    "#endif\n"
1817                    "#endif\n"
1818                    "Four\n"
1819                    "};"));
1820   EXPECT_EQ("enum I {\n"
1821             "  One,\n"
1822             "#if /* test */ 0 || 1\n"
1823             "Two,\n"
1824             "Three,\n"
1825             "#endif\n"
1826             "  Four\n"
1827             "};",
1828             format("enum I {\n"
1829                    "One,\n"
1830                    "#if /* test */ 0 || 1\n"
1831                    "Two,\n"
1832                    "Three,\n"
1833                    "#endif\n"
1834                    "Four\n"
1835                    "};"));
1836   EXPECT_EQ("enum J {\n"
1837             "  One,\n"
1838             "#if 0\n"
1839             "#if 0\n"
1840             "Two,\n"
1841             "#else\n"
1842             "Three,\n"
1843             "#endif\n"
1844             "Four,\n"
1845             "#endif\n"
1846             "  Five\n"
1847             "};",
1848             format("enum J {\n"
1849                    "One,\n"
1850                    "#if 0\n"
1851                    "#if 0\n"
1852                    "Two,\n"
1853                    "#else\n"
1854                    "Three,\n"
1855                    "#endif\n"
1856                    "Four,\n"
1857                    "#endif\n"
1858                    "Five\n"
1859                    "};"));
1860 }
1861 
1862 //===----------------------------------------------------------------------===//
1863 // Tests for classes, namespaces, etc.
1864 //===----------------------------------------------------------------------===//
1865 
1866 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1867   verifyFormat("class A {};");
1868 }
1869 
1870 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1871   verifyFormat("class A {\n"
1872                "public:\n"
1873                "public: // comment\n"
1874                "protected:\n"
1875                "private:\n"
1876                "  void f() {}\n"
1877                "};");
1878   verifyGoogleFormat("class A {\n"
1879                      " public:\n"
1880                      " protected:\n"
1881                      " private:\n"
1882                      "  void f() {}\n"
1883                      "};");
1884   verifyFormat("class A {\n"
1885                "public slots:\n"
1886                "  void f1() {}\n"
1887                "public Q_SLOTS:\n"
1888                "  void f2() {}\n"
1889                "protected slots:\n"
1890                "  void f3() {}\n"
1891                "protected Q_SLOTS:\n"
1892                "  void f4() {}\n"
1893                "private slots:\n"
1894                "  void f5() {}\n"
1895                "private Q_SLOTS:\n"
1896                "  void f6() {}\n"
1897                "signals:\n"
1898                "  void g1();\n"
1899                "Q_SIGNALS:\n"
1900                "  void g2();\n"
1901                "};");
1902 
1903   // Don't interpret 'signals' the wrong way.
1904   verifyFormat("signals.set();");
1905   verifyFormat("for (Signals signals : f()) {\n}");
1906   verifyFormat("{\n"
1907                "  signals.set(); // This needs indentation.\n"
1908                "}");
1909 }
1910 
1911 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1912   EXPECT_EQ("class A {\n"
1913             "public:\n"
1914             "  void f();\n"
1915             "\n"
1916             "private:\n"
1917             "  void g() {}\n"
1918             "  // test\n"
1919             "protected:\n"
1920             "  int h;\n"
1921             "};",
1922             format("class A {\n"
1923                    "public:\n"
1924                    "void f();\n"
1925                    "private:\n"
1926                    "void g() {}\n"
1927                    "// test\n"
1928                    "protected:\n"
1929                    "int h;\n"
1930                    "};"));
1931   EXPECT_EQ("class A {\n"
1932             "protected:\n"
1933             "public:\n"
1934             "  void f();\n"
1935             "};",
1936             format("class A {\n"
1937                    "protected:\n"
1938                    "\n"
1939                    "public:\n"
1940                    "\n"
1941                    "  void f();\n"
1942                    "};"));
1943 
1944   // Even ensure proper spacing inside macros.
1945   EXPECT_EQ("#define B     \\\n"
1946             "  class A {   \\\n"
1947             "   protected: \\\n"
1948             "   public:    \\\n"
1949             "    void f(); \\\n"
1950             "  };",
1951             format("#define B     \\\n"
1952                    "  class A {   \\\n"
1953                    "   protected: \\\n"
1954                    "              \\\n"
1955                    "   public:    \\\n"
1956                    "              \\\n"
1957                    "    void f(); \\\n"
1958                    "  };",
1959                    getGoogleStyle()));
1960   // But don't remove empty lines after macros ending in access specifiers.
1961   EXPECT_EQ("#define A private:\n"
1962             "\n"
1963             "int i;",
1964             format("#define A         private:\n"
1965                    "\n"
1966                    "int              i;"));
1967 }
1968 
1969 TEST_F(FormatTest, FormatsClasses) {
1970   verifyFormat("class A : public B {};");
1971   verifyFormat("class A : public ::B {};");
1972 
1973   verifyFormat(
1974       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1975       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1976   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1977                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1978                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1979   verifyFormat(
1980       "class A : public B, public C, public D, public E, public F {};");
1981   verifyFormat("class AAAAAAAAAAAA : public B,\n"
1982                "                     public C,\n"
1983                "                     public D,\n"
1984                "                     public E,\n"
1985                "                     public F,\n"
1986                "                     public G {};");
1987 
1988   verifyFormat("class\n"
1989                "    ReallyReallyLongClassName {\n"
1990                "  int i;\n"
1991                "};",
1992                getLLVMStyleWithColumns(32));
1993   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
1994                "                           aaaaaaaaaaaaaaaa> {};");
1995   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
1996                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
1997                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
1998   verifyFormat("template <class R, class C>\n"
1999                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
2000                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
2001   verifyFormat("class ::A::B {};");
2002 }
2003 
2004 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
2005   verifyFormat("class A {\n} a, b;");
2006   verifyFormat("struct A {\n} a, b;");
2007   verifyFormat("union A {\n} a;");
2008 }
2009 
2010 TEST_F(FormatTest, FormatsEnum) {
2011   verifyFormat("enum {\n"
2012                "  Zero,\n"
2013                "  One = 1,\n"
2014                "  Two = One + 1,\n"
2015                "  Three = (One + Two),\n"
2016                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2017                "  Five = (One, Two, Three, Four, 5)\n"
2018                "};");
2019   verifyGoogleFormat("enum {\n"
2020                      "  Zero,\n"
2021                      "  One = 1,\n"
2022                      "  Two = One + 1,\n"
2023                      "  Three = (One + Two),\n"
2024                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2025                      "  Five = (One, Two, Three, Four, 5)\n"
2026                      "};");
2027   verifyFormat("enum Enum {};");
2028   verifyFormat("enum {};");
2029   verifyFormat("enum X E {} d;");
2030   verifyFormat("enum __attribute__((...)) E {} d;");
2031   verifyFormat("enum __declspec__((...)) E {} d;");
2032   verifyFormat("enum {\n"
2033                "  Bar = Foo<int, int>::value\n"
2034                "};",
2035                getLLVMStyleWithColumns(30));
2036 
2037   verifyFormat("enum ShortEnum { A, B, C };");
2038   verifyGoogleFormat("enum ShortEnum { A, B, C };");
2039 
2040   EXPECT_EQ("enum KeepEmptyLines {\n"
2041             "  ONE,\n"
2042             "\n"
2043             "  TWO,\n"
2044             "\n"
2045             "  THREE\n"
2046             "}",
2047             format("enum KeepEmptyLines {\n"
2048                    "  ONE,\n"
2049                    "\n"
2050                    "  TWO,\n"
2051                    "\n"
2052                    "\n"
2053                    "  THREE\n"
2054                    "}"));
2055   verifyFormat("enum E { // comment\n"
2056                "  ONE,\n"
2057                "  TWO\n"
2058                "};\n"
2059                "int i;");
2060   // Not enums.
2061   verifyFormat("enum X f() {\n"
2062                "  a();\n"
2063                "  return 42;\n"
2064                "}");
2065   verifyFormat("enum X Type::f() {\n"
2066                "  a();\n"
2067                "  return 42;\n"
2068                "}");
2069   verifyFormat("enum ::X f() {\n"
2070                "  a();\n"
2071                "  return 42;\n"
2072                "}");
2073   verifyFormat("enum ns::X f() {\n"
2074                "  a();\n"
2075                "  return 42;\n"
2076                "}");
2077 }
2078 
2079 TEST_F(FormatTest, FormatsEnumsWithErrors) {
2080   verifyFormat("enum Type {\n"
2081                "  One = 0; // These semicolons should be commas.\n"
2082                "  Two = 1;\n"
2083                "};");
2084   verifyFormat("namespace n {\n"
2085                "enum Type {\n"
2086                "  One,\n"
2087                "  Two, // missing };\n"
2088                "  int i;\n"
2089                "}\n"
2090                "void g() {}");
2091 }
2092 
2093 TEST_F(FormatTest, FormatsEnumStruct) {
2094   verifyFormat("enum struct {\n"
2095                "  Zero,\n"
2096                "  One = 1,\n"
2097                "  Two = One + 1,\n"
2098                "  Three = (One + Two),\n"
2099                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2100                "  Five = (One, Two, Three, Four, 5)\n"
2101                "};");
2102   verifyFormat("enum struct Enum {};");
2103   verifyFormat("enum struct {};");
2104   verifyFormat("enum struct X E {} d;");
2105   verifyFormat("enum struct __attribute__((...)) E {} d;");
2106   verifyFormat("enum struct __declspec__((...)) E {} d;");
2107   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
2108 }
2109 
2110 TEST_F(FormatTest, FormatsEnumClass) {
2111   verifyFormat("enum class {\n"
2112                "  Zero,\n"
2113                "  One = 1,\n"
2114                "  Two = One + 1,\n"
2115                "  Three = (One + Two),\n"
2116                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2117                "  Five = (One, Two, Three, Four, 5)\n"
2118                "};");
2119   verifyFormat("enum class Enum {};");
2120   verifyFormat("enum class {};");
2121   verifyFormat("enum class X E {} d;");
2122   verifyFormat("enum class __attribute__((...)) E {} d;");
2123   verifyFormat("enum class __declspec__((...)) E {} d;");
2124   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
2125 }
2126 
2127 TEST_F(FormatTest, FormatsEnumTypes) {
2128   verifyFormat("enum X : int {\n"
2129                "  A, // Force multiple lines.\n"
2130                "  B\n"
2131                "};");
2132   verifyFormat("enum X : int { A, B };");
2133   verifyFormat("enum X : std::uint32_t { A, B };");
2134 }
2135 
2136 TEST_F(FormatTest, FormatsNSEnums) {
2137   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
2138   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
2139                      "  // Information about someDecentlyLongValue.\n"
2140                      "  someDecentlyLongValue,\n"
2141                      "  // Information about anotherDecentlyLongValue.\n"
2142                      "  anotherDecentlyLongValue,\n"
2143                      "  // Information about aThirdDecentlyLongValue.\n"
2144                      "  aThirdDecentlyLongValue\n"
2145                      "};");
2146   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
2147                      "  a = 1,\n"
2148                      "  b = 2,\n"
2149                      "  c = 3,\n"
2150                      "};");
2151   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
2152                      "  a = 1,\n"
2153                      "  b = 2,\n"
2154                      "  c = 3,\n"
2155                      "};");
2156   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
2157                      "  a = 1,\n"
2158                      "  b = 2,\n"
2159                      "  c = 3,\n"
2160                      "};");
2161 }
2162 
2163 TEST_F(FormatTest, FormatsBitfields) {
2164   verifyFormat("struct Bitfields {\n"
2165                "  unsigned sClass : 8;\n"
2166                "  unsigned ValueKind : 2;\n"
2167                "};");
2168   verifyFormat("struct A {\n"
2169                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
2170                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
2171                "};");
2172   verifyFormat("struct MyStruct {\n"
2173                "  uchar data;\n"
2174                "  uchar : 8;\n"
2175                "  uchar : 8;\n"
2176                "  uchar other;\n"
2177                "};");
2178 }
2179 
2180 TEST_F(FormatTest, FormatsNamespaces) {
2181   verifyFormat("namespace some_namespace {\n"
2182                "class A {};\n"
2183                "void f() { f(); }\n"
2184                "}");
2185   verifyFormat("namespace {\n"
2186                "class A {};\n"
2187                "void f() { f(); }\n"
2188                "}");
2189   verifyFormat("inline namespace X {\n"
2190                "class A {};\n"
2191                "void f() { f(); }\n"
2192                "}");
2193   verifyFormat("using namespace some_namespace;\n"
2194                "class A {};\n"
2195                "void f() { f(); }");
2196 
2197   // This code is more common than we thought; if we
2198   // layout this correctly the semicolon will go into
2199   // its own line, which is undesirable.
2200   verifyFormat("namespace {};");
2201   verifyFormat("namespace {\n"
2202                "class A {};\n"
2203                "};");
2204 
2205   verifyFormat("namespace {\n"
2206                "int SomeVariable = 0; // comment\n"
2207                "} // namespace");
2208   EXPECT_EQ("#ifndef HEADER_GUARD\n"
2209             "#define HEADER_GUARD\n"
2210             "namespace my_namespace {\n"
2211             "int i;\n"
2212             "} // my_namespace\n"
2213             "#endif // HEADER_GUARD",
2214             format("#ifndef HEADER_GUARD\n"
2215                    " #define HEADER_GUARD\n"
2216                    "   namespace my_namespace {\n"
2217                    "int i;\n"
2218                    "}    // my_namespace\n"
2219                    "#endif    // HEADER_GUARD"));
2220 
2221   EXPECT_EQ("namespace A::B {\n"
2222             "class C {};\n"
2223             "}",
2224             format("namespace A::B {\n"
2225                    "class C {};\n"
2226                    "}"));
2227 
2228   FormatStyle Style = getLLVMStyle();
2229   Style.NamespaceIndentation = FormatStyle::NI_All;
2230   EXPECT_EQ("namespace out {\n"
2231             "  int i;\n"
2232             "  namespace in {\n"
2233             "    int i;\n"
2234             "  } // namespace\n"
2235             "} // namespace",
2236             format("namespace out {\n"
2237                    "int i;\n"
2238                    "namespace in {\n"
2239                    "int i;\n"
2240                    "} // namespace\n"
2241                    "} // namespace",
2242                    Style));
2243 
2244   Style.NamespaceIndentation = FormatStyle::NI_Inner;
2245   EXPECT_EQ("namespace out {\n"
2246             "int i;\n"
2247             "namespace in {\n"
2248             "  int i;\n"
2249             "} // namespace\n"
2250             "} // namespace",
2251             format("namespace out {\n"
2252                    "int i;\n"
2253                    "namespace in {\n"
2254                    "int i;\n"
2255                    "} // namespace\n"
2256                    "} // namespace",
2257                    Style));
2258 }
2259 
2260 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
2261 
2262 TEST_F(FormatTest, FormatsInlineASM) {
2263   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
2264   verifyFormat("asm(\"nop\" ::: \"memory\");");
2265   verifyFormat(
2266       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
2267       "    \"cpuid\\n\\t\"\n"
2268       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
2269       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
2270       "    : \"a\"(value));");
2271   EXPECT_EQ(
2272       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
2273       "  __asm {\n"
2274       "        mov     edx,[that] // vtable in edx\n"
2275       "        mov     eax,methodIndex\n"
2276       "        call    [edx][eax*4] // stdcall\n"
2277       "  }\n"
2278       "}",
2279       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
2280              "    __asm {\n"
2281              "        mov     edx,[that] // vtable in edx\n"
2282              "        mov     eax,methodIndex\n"
2283              "        call    [edx][eax*4] // stdcall\n"
2284              "    }\n"
2285              "}"));
2286   EXPECT_EQ("_asm {\n"
2287             "  xor eax, eax;\n"
2288             "  cpuid;\n"
2289             "}",
2290             format("_asm {\n"
2291                    "  xor eax, eax;\n"
2292                    "  cpuid;\n"
2293                    "}"));
2294   verifyFormat("void function() {\n"
2295                "  // comment\n"
2296                "  asm(\"\");\n"
2297                "}");
2298   EXPECT_EQ("__asm {\n"
2299             "}\n"
2300             "int i;",
2301             format("__asm   {\n"
2302                    "}\n"
2303                    "int   i;"));
2304 }
2305 
2306 TEST_F(FormatTest, FormatTryCatch) {
2307   verifyFormat("try {\n"
2308                "  throw a * b;\n"
2309                "} catch (int a) {\n"
2310                "  // Do nothing.\n"
2311                "} catch (...) {\n"
2312                "  exit(42);\n"
2313                "}");
2314 
2315   // Function-level try statements.
2316   verifyFormat("int f() try { return 4; } catch (...) {\n"
2317                "  return 5;\n"
2318                "}");
2319   verifyFormat("class A {\n"
2320                "  int a;\n"
2321                "  A() try : a(0) {\n"
2322                "  } catch (...) {\n"
2323                "    throw;\n"
2324                "  }\n"
2325                "};\n");
2326 
2327   // Incomplete try-catch blocks.
2328   verifyIncompleteFormat("try {} catch (");
2329 }
2330 
2331 TEST_F(FormatTest, FormatSEHTryCatch) {
2332   verifyFormat("__try {\n"
2333                "  int a = b * c;\n"
2334                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
2335                "  // Do nothing.\n"
2336                "}");
2337 
2338   verifyFormat("__try {\n"
2339                "  int a = b * c;\n"
2340                "} __finally {\n"
2341                "  // Do nothing.\n"
2342                "}");
2343 
2344   verifyFormat("DEBUG({\n"
2345                "  __try {\n"
2346                "  } __finally {\n"
2347                "  }\n"
2348                "});\n");
2349 }
2350 
2351 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
2352   verifyFormat("try {\n"
2353                "  f();\n"
2354                "} catch {\n"
2355                "  g();\n"
2356                "}");
2357   verifyFormat("try {\n"
2358                "  f();\n"
2359                "} catch (A a) MACRO(x) {\n"
2360                "  g();\n"
2361                "} catch (B b) MACRO(x) {\n"
2362                "  g();\n"
2363                "}");
2364 }
2365 
2366 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
2367   FormatStyle Style = getLLVMStyle();
2368   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
2369                           FormatStyle::BS_WebKit}) {
2370     Style.BreakBeforeBraces = BraceStyle;
2371     verifyFormat("try {\n"
2372                  "  // something\n"
2373                  "} catch (...) {\n"
2374                  "  // something\n"
2375                  "}",
2376                  Style);
2377   }
2378   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
2379   verifyFormat("try {\n"
2380                "  // something\n"
2381                "}\n"
2382                "catch (...) {\n"
2383                "  // something\n"
2384                "}",
2385                Style);
2386   verifyFormat("__try {\n"
2387                "  // something\n"
2388                "}\n"
2389                "__finally {\n"
2390                "  // something\n"
2391                "}",
2392                Style);
2393   verifyFormat("@try {\n"
2394                "  // something\n"
2395                "}\n"
2396                "@finally {\n"
2397                "  // something\n"
2398                "}",
2399                Style);
2400   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2401   verifyFormat("try\n"
2402                "{\n"
2403                "  // something\n"
2404                "}\n"
2405                "catch (...)\n"
2406                "{\n"
2407                "  // something\n"
2408                "}",
2409                Style);
2410   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
2411   verifyFormat("try\n"
2412                "  {\n"
2413                "    // something\n"
2414                "  }\n"
2415                "catch (...)\n"
2416                "  {\n"
2417                "    // something\n"
2418                "  }",
2419                Style);
2420   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2421   Style.BraceWrapping.BeforeCatch = true;
2422   verifyFormat("try {\n"
2423                "  // something\n"
2424                "}\n"
2425                "catch (...) {\n"
2426                "  // something\n"
2427                "}",
2428                Style);
2429 }
2430 
2431 TEST_F(FormatTest, FormatObjCTryCatch) {
2432   verifyFormat("@try {\n"
2433                "  f();\n"
2434                "} @catch (NSException e) {\n"
2435                "  @throw;\n"
2436                "} @finally {\n"
2437                "  exit(42);\n"
2438                "}");
2439   verifyFormat("DEBUG({\n"
2440                "  @try {\n"
2441                "  } @finally {\n"
2442                "  }\n"
2443                "});\n");
2444 }
2445 
2446 TEST_F(FormatTest, FormatObjCAutoreleasepool) {
2447   FormatStyle Style = getLLVMStyle();
2448   verifyFormat("@autoreleasepool {\n"
2449                "  f();\n"
2450                "}\n"
2451                "@autoreleasepool {\n"
2452                "  f();\n"
2453                "}\n",
2454                Style);
2455   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2456   verifyFormat("@autoreleasepool\n"
2457                "{\n"
2458                "  f();\n"
2459                "}\n"
2460                "@autoreleasepool\n"
2461                "{\n"
2462                "  f();\n"
2463                "}\n",
2464                Style);
2465 }
2466 
2467 TEST_F(FormatTest, StaticInitializers) {
2468   verifyFormat("static SomeClass SC = {1, 'a'};");
2469 
2470   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
2471                "    100000000, "
2472                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
2473 
2474   // Here, everything other than the "}" would fit on a line.
2475   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
2476                "    10000000000000000000000000};");
2477   EXPECT_EQ("S s = {a,\n"
2478             "\n"
2479             "       b};",
2480             format("S s = {\n"
2481                    "  a,\n"
2482                    "\n"
2483                    "  b\n"
2484                    "};"));
2485 
2486   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
2487   // line. However, the formatting looks a bit off and this probably doesn't
2488   // happen often in practice.
2489   verifyFormat("static int Variable[1] = {\n"
2490                "    {1000000000000000000000000000000000000}};",
2491                getLLVMStyleWithColumns(40));
2492 }
2493 
2494 TEST_F(FormatTest, DesignatedInitializers) {
2495   verifyFormat("const struct A a = {.a = 1, .b = 2};");
2496   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
2497                "                    .bbbbbbbbbb = 2,\n"
2498                "                    .cccccccccc = 3,\n"
2499                "                    .dddddddddd = 4,\n"
2500                "                    .eeeeeeeeee = 5};");
2501   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2502                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
2503                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
2504                "    .ccccccccccccccccccccccccccc = 3,\n"
2505                "    .ddddddddddddddddddddddddddd = 4,\n"
2506                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
2507 
2508   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
2509 }
2510 
2511 TEST_F(FormatTest, NestedStaticInitializers) {
2512   verifyFormat("static A x = {{{}}};\n");
2513   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
2514                "               {init1, init2, init3, init4}}};",
2515                getLLVMStyleWithColumns(50));
2516 
2517   verifyFormat("somes Status::global_reps[3] = {\n"
2518                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2519                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2520                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
2521                getLLVMStyleWithColumns(60));
2522   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
2523                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2524                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2525                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
2526   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
2527                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
2528                "rect.fTop}};");
2529 
2530   verifyFormat(
2531       "SomeArrayOfSomeType a = {\n"
2532       "    {{1, 2, 3},\n"
2533       "     {1, 2, 3},\n"
2534       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
2535       "      333333333333333333333333333333},\n"
2536       "     {1, 2, 3},\n"
2537       "     {1, 2, 3}}};");
2538   verifyFormat(
2539       "SomeArrayOfSomeType a = {\n"
2540       "    {{1, 2, 3}},\n"
2541       "    {{1, 2, 3}},\n"
2542       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
2543       "      333333333333333333333333333333}},\n"
2544       "    {{1, 2, 3}},\n"
2545       "    {{1, 2, 3}}};");
2546 
2547   verifyFormat("struct {\n"
2548                "  unsigned bit;\n"
2549                "  const char *const name;\n"
2550                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
2551                "                 {kOsWin, \"Windows\"},\n"
2552                "                 {kOsLinux, \"Linux\"},\n"
2553                "                 {kOsCrOS, \"Chrome OS\"}};");
2554   verifyFormat("struct {\n"
2555                "  unsigned bit;\n"
2556                "  const char *const name;\n"
2557                "} kBitsToOs[] = {\n"
2558                "    {kOsMac, \"Mac\"},\n"
2559                "    {kOsWin, \"Windows\"},\n"
2560                "    {kOsLinux, \"Linux\"},\n"
2561                "    {kOsCrOS, \"Chrome OS\"},\n"
2562                "};");
2563 }
2564 
2565 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
2566   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2567                "                      \\\n"
2568                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
2569 }
2570 
2571 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
2572   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
2573                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
2574 
2575   // Do break defaulted and deleted functions.
2576   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2577                "    default;",
2578                getLLVMStyleWithColumns(40));
2579   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2580                "    delete;",
2581                getLLVMStyleWithColumns(40));
2582 }
2583 
2584 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
2585   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
2586                getLLVMStyleWithColumns(40));
2587   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2588                getLLVMStyleWithColumns(40));
2589   EXPECT_EQ("#define Q                              \\\n"
2590             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
2591             "  \"aaaaaaaa.cpp\"",
2592             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2593                    getLLVMStyleWithColumns(40)));
2594 }
2595 
2596 TEST_F(FormatTest, UnderstandsLinePPDirective) {
2597   EXPECT_EQ("# 123 \"A string literal\"",
2598             format("   #     123    \"A string literal\""));
2599 }
2600 
2601 TEST_F(FormatTest, LayoutUnknownPPDirective) {
2602   EXPECT_EQ("#;", format("#;"));
2603   verifyFormat("#\n;\n;\n;");
2604 }
2605 
2606 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
2607   EXPECT_EQ("#line 42 \"test\"\n",
2608             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
2609   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
2610                                     getLLVMStyleWithColumns(12)));
2611 }
2612 
2613 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
2614   EXPECT_EQ("#line 42 \"test\"",
2615             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
2616   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
2617 }
2618 
2619 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
2620   verifyFormat("#define A \\x20");
2621   verifyFormat("#define A \\ x20");
2622   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
2623   verifyFormat("#define A ''");
2624   verifyFormat("#define A ''qqq");
2625   verifyFormat("#define A `qqq");
2626   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
2627   EXPECT_EQ("const char *c = STRINGIFY(\n"
2628             "\\na : b);",
2629             format("const char * c = STRINGIFY(\n"
2630                    "\\na : b);"));
2631 
2632   verifyFormat("a\r\\");
2633   verifyFormat("a\v\\");
2634   verifyFormat("a\f\\");
2635 }
2636 
2637 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
2638   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
2639   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
2640   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
2641   // FIXME: We never break before the macro name.
2642   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
2643 
2644   verifyFormat("#define A A\n#define A A");
2645   verifyFormat("#define A(X) A\n#define A A");
2646 
2647   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
2648   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
2649 }
2650 
2651 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
2652   EXPECT_EQ("// somecomment\n"
2653             "#include \"a.h\"\n"
2654             "#define A(  \\\n"
2655             "    A, B)\n"
2656             "#include \"b.h\"\n"
2657             "// somecomment\n",
2658             format("  // somecomment\n"
2659                    "  #include \"a.h\"\n"
2660                    "#define A(A,\\\n"
2661                    "    B)\n"
2662                    "    #include \"b.h\"\n"
2663                    " // somecomment\n",
2664                    getLLVMStyleWithColumns(13)));
2665 }
2666 
2667 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
2668 
2669 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
2670   EXPECT_EQ("#define A    \\\n"
2671             "  c;         \\\n"
2672             "  e;\n"
2673             "f;",
2674             format("#define A c; e;\n"
2675                    "f;",
2676                    getLLVMStyleWithColumns(14)));
2677 }
2678 
2679 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
2680 
2681 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
2682   EXPECT_EQ("int x,\n"
2683             "#define A\n"
2684             "    y;",
2685             format("int x,\n#define A\ny;"));
2686 }
2687 
2688 TEST_F(FormatTest, HashInMacroDefinition) {
2689   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
2690   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
2691   verifyFormat("#define A  \\\n"
2692                "  {        \\\n"
2693                "    f(#c); \\\n"
2694                "  }",
2695                getLLVMStyleWithColumns(11));
2696 
2697   verifyFormat("#define A(X)         \\\n"
2698                "  void function##X()",
2699                getLLVMStyleWithColumns(22));
2700 
2701   verifyFormat("#define A(a, b, c)   \\\n"
2702                "  void a##b##c()",
2703                getLLVMStyleWithColumns(22));
2704 
2705   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
2706 }
2707 
2708 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
2709   EXPECT_EQ("#define A (x)", format("#define A (x)"));
2710   EXPECT_EQ("#define A(x)", format("#define A(x)"));
2711 }
2712 
2713 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
2714   EXPECT_EQ("#define A b;", format("#define A \\\n"
2715                                    "          \\\n"
2716                                    "  b;",
2717                                    getLLVMStyleWithColumns(25)));
2718   EXPECT_EQ("#define A \\\n"
2719             "          \\\n"
2720             "  a;      \\\n"
2721             "  b;",
2722             format("#define A \\\n"
2723                    "          \\\n"
2724                    "  a;      \\\n"
2725                    "  b;",
2726                    getLLVMStyleWithColumns(11)));
2727   EXPECT_EQ("#define A \\\n"
2728             "  a;      \\\n"
2729             "          \\\n"
2730             "  b;",
2731             format("#define A \\\n"
2732                    "  a;      \\\n"
2733                    "          \\\n"
2734                    "  b;",
2735                    getLLVMStyleWithColumns(11)));
2736 }
2737 
2738 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
2739   verifyIncompleteFormat("#define A :");
2740   verifyFormat("#define SOMECASES  \\\n"
2741                "  case 1:          \\\n"
2742                "  case 2\n",
2743                getLLVMStyleWithColumns(20));
2744   verifyFormat("#define A template <typename T>");
2745   verifyIncompleteFormat("#define STR(x) #x\n"
2746                          "f(STR(this_is_a_string_literal{));");
2747   verifyFormat("#pragma omp threadprivate( \\\n"
2748                "    y)), // expected-warning",
2749                getLLVMStyleWithColumns(28));
2750   verifyFormat("#d, = };");
2751   verifyFormat("#if \"a");
2752   verifyIncompleteFormat("({\n"
2753                          "#define b     \\\n"
2754                          "  }           \\\n"
2755                          "  a\n"
2756                          "a",
2757                          getLLVMStyleWithColumns(15));
2758   verifyFormat("#define A     \\\n"
2759                "  {           \\\n"
2760                "    {\n"
2761                "#define B     \\\n"
2762                "  }           \\\n"
2763                "  }",
2764                getLLVMStyleWithColumns(15));
2765   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2766   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2767   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2768   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
2769 }
2770 
2771 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2772   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2773   EXPECT_EQ("class A : public QObject {\n"
2774             "  Q_OBJECT\n"
2775             "\n"
2776             "  A() {}\n"
2777             "};",
2778             format("class A  :  public QObject {\n"
2779                    "     Q_OBJECT\n"
2780                    "\n"
2781                    "  A() {\n}\n"
2782                    "}  ;"));
2783   EXPECT_EQ("MACRO\n"
2784             "/*static*/ int i;",
2785             format("MACRO\n"
2786                    " /*static*/ int   i;"));
2787   EXPECT_EQ("SOME_MACRO\n"
2788             "namespace {\n"
2789             "void f();\n"
2790             "}",
2791             format("SOME_MACRO\n"
2792                    "  namespace    {\n"
2793                    "void   f(  );\n"
2794                    "}"));
2795   // Only if the identifier contains at least 5 characters.
2796   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
2797   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
2798   // Only if everything is upper case.
2799   EXPECT_EQ("class A : public QObject {\n"
2800             "  Q_Object A() {}\n"
2801             "};",
2802             format("class A  :  public QObject {\n"
2803                    "     Q_Object\n"
2804                    "  A() {\n}\n"
2805                    "}  ;"));
2806 
2807   // Only if the next line can actually start an unwrapped line.
2808   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2809             format("SOME_WEIRD_LOG_MACRO\n"
2810                    "<< SomeThing;"));
2811 
2812   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2813                "(n, buffers))\n",
2814                getChromiumStyle(FormatStyle::LK_Cpp));
2815 }
2816 
2817 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2818   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2819             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2820             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2821             "class X {};\n"
2822             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2823             "int *createScopDetectionPass() { return 0; }",
2824             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2825                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2826                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2827                    "  class X {};\n"
2828                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2829                    "  int *createScopDetectionPass() { return 0; }"));
2830   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2831   // braces, so that inner block is indented one level more.
2832   EXPECT_EQ("int q() {\n"
2833             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2834             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2835             "  IPC_END_MESSAGE_MAP()\n"
2836             "}",
2837             format("int q() {\n"
2838                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2839                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2840                    "  IPC_END_MESSAGE_MAP()\n"
2841                    "}"));
2842 
2843   // Same inside macros.
2844   EXPECT_EQ("#define LIST(L) \\\n"
2845             "  L(A)          \\\n"
2846             "  L(B)          \\\n"
2847             "  L(C)",
2848             format("#define LIST(L) \\\n"
2849                    "  L(A) \\\n"
2850                    "  L(B) \\\n"
2851                    "  L(C)",
2852                    getGoogleStyle()));
2853 
2854   // These must not be recognized as macros.
2855   EXPECT_EQ("int q() {\n"
2856             "  f(x);\n"
2857             "  f(x) {}\n"
2858             "  f(x)->g();\n"
2859             "  f(x)->*g();\n"
2860             "  f(x).g();\n"
2861             "  f(x) = x;\n"
2862             "  f(x) += x;\n"
2863             "  f(x) -= x;\n"
2864             "  f(x) *= x;\n"
2865             "  f(x) /= x;\n"
2866             "  f(x) %= x;\n"
2867             "  f(x) &= x;\n"
2868             "  f(x) |= x;\n"
2869             "  f(x) ^= x;\n"
2870             "  f(x) >>= x;\n"
2871             "  f(x) <<= x;\n"
2872             "  f(x)[y].z();\n"
2873             "  LOG(INFO) << x;\n"
2874             "  ifstream(x) >> x;\n"
2875             "}\n",
2876             format("int q() {\n"
2877                    "  f(x)\n;\n"
2878                    "  f(x)\n {}\n"
2879                    "  f(x)\n->g();\n"
2880                    "  f(x)\n->*g();\n"
2881                    "  f(x)\n.g();\n"
2882                    "  f(x)\n = x;\n"
2883                    "  f(x)\n += x;\n"
2884                    "  f(x)\n -= x;\n"
2885                    "  f(x)\n *= x;\n"
2886                    "  f(x)\n /= x;\n"
2887                    "  f(x)\n %= x;\n"
2888                    "  f(x)\n &= x;\n"
2889                    "  f(x)\n |= x;\n"
2890                    "  f(x)\n ^= x;\n"
2891                    "  f(x)\n >>= x;\n"
2892                    "  f(x)\n <<= x;\n"
2893                    "  f(x)\n[y].z();\n"
2894                    "  LOG(INFO)\n << x;\n"
2895                    "  ifstream(x)\n >> x;\n"
2896                    "}\n"));
2897   EXPECT_EQ("int q() {\n"
2898             "  F(x)\n"
2899             "  if (1) {\n"
2900             "  }\n"
2901             "  F(x)\n"
2902             "  while (1) {\n"
2903             "  }\n"
2904             "  F(x)\n"
2905             "  G(x);\n"
2906             "  F(x)\n"
2907             "  try {\n"
2908             "    Q();\n"
2909             "  } catch (...) {\n"
2910             "  }\n"
2911             "}\n",
2912             format("int q() {\n"
2913                    "F(x)\n"
2914                    "if (1) {}\n"
2915                    "F(x)\n"
2916                    "while (1) {}\n"
2917                    "F(x)\n"
2918                    "G(x);\n"
2919                    "F(x)\n"
2920                    "try { Q(); } catch (...) {}\n"
2921                    "}\n"));
2922   EXPECT_EQ("class A {\n"
2923             "  A() : t(0) {}\n"
2924             "  A(int i) noexcept() : {}\n"
2925             "  A(X x)\n" // FIXME: function-level try blocks are broken.
2926             "  try : t(0) {\n"
2927             "  } catch (...) {\n"
2928             "  }\n"
2929             "};",
2930             format("class A {\n"
2931                    "  A()\n : t(0) {}\n"
2932                    "  A(int i)\n noexcept() : {}\n"
2933                    "  A(X x)\n"
2934                    "  try : t(0) {} catch (...) {}\n"
2935                    "};"));
2936   EXPECT_EQ("class SomeClass {\n"
2937             "public:\n"
2938             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2939             "};",
2940             format("class SomeClass {\n"
2941                    "public:\n"
2942                    "  SomeClass()\n"
2943                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2944                    "};"));
2945   EXPECT_EQ("class SomeClass {\n"
2946             "public:\n"
2947             "  SomeClass()\n"
2948             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2949             "};",
2950             format("class SomeClass {\n"
2951                    "public:\n"
2952                    "  SomeClass()\n"
2953                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2954                    "};",
2955                    getLLVMStyleWithColumns(40)));
2956 
2957   verifyFormat("MACRO(>)");
2958 }
2959 
2960 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2961   verifyFormat("#define A \\\n"
2962                "  f({     \\\n"
2963                "    g();  \\\n"
2964                "  });",
2965                getLLVMStyleWithColumns(11));
2966 }
2967 
2968 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
2969   EXPECT_EQ("{\n  {\n#define A\n  }\n}", format("{{\n#define A\n}}"));
2970 }
2971 
2972 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
2973   verifyFormat("{\n  { a #c; }\n}");
2974 }
2975 
2976 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
2977   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
2978             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
2979   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
2980             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
2981 }
2982 
2983 TEST_F(FormatTest, EscapedNewlines) {
2984   EXPECT_EQ(
2985       "#define A \\\n  int i;  \\\n  int j;",
2986       format("#define A \\\nint i;\\\n  int j;", getLLVMStyleWithColumns(11)));
2987   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
2988   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2989   EXPECT_EQ("/* \\  \\  \\\n*/", format("\\\n/* \\  \\  \\\n*/"));
2990   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
2991 }
2992 
2993 TEST_F(FormatTest, DontCrashOnBlockComments) {
2994   EXPECT_EQ(
2995       "int xxxxxxxxx; /* "
2996       "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
2997       "zzzzzz\n"
2998       "0*/",
2999       format("int xxxxxxxxx;                          /* "
3000              "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
3001              "0*/"));
3002 }
3003 
3004 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
3005   verifyFormat("#define A \\\n"
3006                "  int v(  \\\n"
3007                "      a); \\\n"
3008                "  int i;",
3009                getLLVMStyleWithColumns(11));
3010 }
3011 
3012 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
3013   EXPECT_EQ(
3014       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
3015       "                      \\\n"
3016       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
3017       "\n"
3018       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
3019       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
3020       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
3021              "\\\n"
3022              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
3023              "  \n"
3024              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
3025              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
3026 }
3027 
3028 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
3029   EXPECT_EQ("int\n"
3030             "#define A\n"
3031             "    a;",
3032             format("int\n#define A\na;"));
3033   verifyFormat("functionCallTo(\n"
3034                "    someOtherFunction(\n"
3035                "        withSomeParameters, whichInSequence,\n"
3036                "        areLongerThanALine(andAnotherCall,\n"
3037                "#define A B\n"
3038                "                           withMoreParamters,\n"
3039                "                           whichStronglyInfluenceTheLayout),\n"
3040                "        andMoreParameters),\n"
3041                "    trailing);",
3042                getLLVMStyleWithColumns(69));
3043   verifyFormat("Foo::Foo()\n"
3044                "#ifdef BAR\n"
3045                "    : baz(0)\n"
3046                "#endif\n"
3047                "{\n"
3048                "}");
3049   verifyFormat("void f() {\n"
3050                "  if (true)\n"
3051                "#ifdef A\n"
3052                "    f(42);\n"
3053                "  x();\n"
3054                "#else\n"
3055                "    g();\n"
3056                "  x();\n"
3057                "#endif\n"
3058                "}");
3059   verifyFormat("void f(param1, param2,\n"
3060                "       param3,\n"
3061                "#ifdef A\n"
3062                "       param4(param5,\n"
3063                "#ifdef A1\n"
3064                "              param6,\n"
3065                "#ifdef A2\n"
3066                "              param7),\n"
3067                "#else\n"
3068                "              param8),\n"
3069                "       param9,\n"
3070                "#endif\n"
3071                "       param10,\n"
3072                "#endif\n"
3073                "       param11)\n"
3074                "#else\n"
3075                "       param12)\n"
3076                "#endif\n"
3077                "{\n"
3078                "  x();\n"
3079                "}",
3080                getLLVMStyleWithColumns(28));
3081   verifyFormat("#if 1\n"
3082                "int i;");
3083   verifyFormat("#if 1\n"
3084                "#endif\n"
3085                "#if 1\n"
3086                "#else\n"
3087                "#endif\n");
3088   verifyFormat("DEBUG({\n"
3089                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3090                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
3091                "});\n"
3092                "#if a\n"
3093                "#else\n"
3094                "#endif");
3095 
3096   verifyIncompleteFormat("void f(\n"
3097                          "#if A\n"
3098                          "    );\n"
3099                          "#else\n"
3100                          "#endif");
3101 }
3102 
3103 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
3104   verifyFormat("#endif\n"
3105                "#if B");
3106 }
3107 
3108 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
3109   FormatStyle SingleLine = getLLVMStyle();
3110   SingleLine.AllowShortIfStatementsOnASingleLine = true;
3111   verifyFormat("#if 0\n"
3112                "#elif 1\n"
3113                "#endif\n"
3114                "void foo() {\n"
3115                "  if (test) foo2();\n"
3116                "}",
3117                SingleLine);
3118 }
3119 
3120 TEST_F(FormatTest, LayoutBlockInsideParens) {
3121   verifyFormat("functionCall({ int i; });");
3122   verifyFormat("functionCall({\n"
3123                "  int i;\n"
3124                "  int j;\n"
3125                "});");
3126   verifyFormat("functionCall(\n"
3127                "    {\n"
3128                "      int i;\n"
3129                "      int j;\n"
3130                "    },\n"
3131                "    aaaa, bbbb, cccc);");
3132   verifyFormat("functionA(functionB({\n"
3133                "            int i;\n"
3134                "            int j;\n"
3135                "          }),\n"
3136                "          aaaa, bbbb, cccc);");
3137   verifyFormat("functionCall(\n"
3138                "    {\n"
3139                "      int i;\n"
3140                "      int j;\n"
3141                "    },\n"
3142                "    aaaa, bbbb, // comment\n"
3143                "    cccc);");
3144   verifyFormat("functionA(functionB({\n"
3145                "            int i;\n"
3146                "            int j;\n"
3147                "          }),\n"
3148                "          aaaa, bbbb, // comment\n"
3149                "          cccc);");
3150   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
3151   verifyFormat("functionCall(aaaa, bbbb, {\n"
3152                "  int i;\n"
3153                "  int j;\n"
3154                "});");
3155   verifyFormat(
3156       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
3157       "    {\n"
3158       "      int i; // break\n"
3159       "    },\n"
3160       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
3161       "                                     ccccccccccccccccc));");
3162   verifyFormat("DEBUG({\n"
3163                "  if (a)\n"
3164                "    f();\n"
3165                "});");
3166 }
3167 
3168 TEST_F(FormatTest, LayoutBlockInsideStatement) {
3169   EXPECT_EQ("SOME_MACRO { int i; }\n"
3170             "int i;",
3171             format("  SOME_MACRO  {int i;}  int i;"));
3172 }
3173 
3174 TEST_F(FormatTest, LayoutNestedBlocks) {
3175   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
3176                "  struct s {\n"
3177                "    int i;\n"
3178                "  };\n"
3179                "  s kBitsToOs[] = {{10}};\n"
3180                "  for (int i = 0; i < 10; ++i)\n"
3181                "    return;\n"
3182                "}");
3183   verifyFormat("call(parameter, {\n"
3184                "  something();\n"
3185                "  // Comment using all columns.\n"
3186                "  somethingelse();\n"
3187                "});",
3188                getLLVMStyleWithColumns(40));
3189   verifyFormat("DEBUG( //\n"
3190                "    { f(); }, a);");
3191   verifyFormat("DEBUG( //\n"
3192                "    {\n"
3193                "      f(); //\n"
3194                "    },\n"
3195                "    a);");
3196 
3197   EXPECT_EQ("call(parameter, {\n"
3198             "  something();\n"
3199             "  // Comment too\n"
3200             "  // looooooooooong.\n"
3201             "  somethingElse();\n"
3202             "});",
3203             format("call(parameter, {\n"
3204                    "  something();\n"
3205                    "  // Comment too looooooooooong.\n"
3206                    "  somethingElse();\n"
3207                    "});",
3208                    getLLVMStyleWithColumns(29)));
3209   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
3210   EXPECT_EQ("DEBUG({ // comment\n"
3211             "  int i;\n"
3212             "});",
3213             format("DEBUG({ // comment\n"
3214                    "int  i;\n"
3215                    "});"));
3216   EXPECT_EQ("DEBUG({\n"
3217             "  int i;\n"
3218             "\n"
3219             "  // comment\n"
3220             "  int j;\n"
3221             "});",
3222             format("DEBUG({\n"
3223                    "  int  i;\n"
3224                    "\n"
3225                    "  // comment\n"
3226                    "  int  j;\n"
3227                    "});"));
3228 
3229   verifyFormat("DEBUG({\n"
3230                "  if (a)\n"
3231                "    return;\n"
3232                "});");
3233   verifyGoogleFormat("DEBUG({\n"
3234                      "  if (a) return;\n"
3235                      "});");
3236   FormatStyle Style = getGoogleStyle();
3237   Style.ColumnLimit = 45;
3238   verifyFormat("Debug(aaaaa,\n"
3239                "      {\n"
3240                "        if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
3241                "      },\n"
3242                "      a);",
3243                Style);
3244 
3245   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
3246 
3247   verifyNoCrash("^{v^{a}}");
3248 }
3249 
3250 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
3251   EXPECT_EQ("#define MACRO()                     \\\n"
3252             "  Debug(aaa, /* force line break */ \\\n"
3253             "        {                           \\\n"
3254             "          int i;                    \\\n"
3255             "          int j;                    \\\n"
3256             "        })",
3257             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
3258                    "          {  int   i;  int  j;   })",
3259                    getGoogleStyle()));
3260 
3261   EXPECT_EQ("#define A                                       \\\n"
3262             "  [] {                                          \\\n"
3263             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
3264             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
3265             "  }",
3266             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
3267                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
3268                    getGoogleStyle()));
3269 }
3270 
3271 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
3272   EXPECT_EQ("{}", format("{}"));
3273   verifyFormat("enum E {};");
3274   verifyFormat("enum E {}");
3275 }
3276 
3277 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
3278   FormatStyle Style = getLLVMStyle();
3279   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
3280   Style.MacroBlockEnd = "^[A-Z_]+_END$";
3281   verifyFormat("FOO_BEGIN\n"
3282                "  FOO_ENTRY\n"
3283                "FOO_END", Style);
3284   verifyFormat("FOO_BEGIN\n"
3285                "  NESTED_FOO_BEGIN\n"
3286                "    NESTED_FOO_ENTRY\n"
3287                "  NESTED_FOO_END\n"
3288                "FOO_END", Style);
3289   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
3290                "  int x;\n"
3291                "  x = 1;\n"
3292                "FOO_END(Baz)", Style);
3293 }
3294 
3295 //===----------------------------------------------------------------------===//
3296 // Line break tests.
3297 //===----------------------------------------------------------------------===//
3298 
3299 TEST_F(FormatTest, PreventConfusingIndents) {
3300   verifyFormat(
3301       "void f() {\n"
3302       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
3303       "                         parameter, parameter, parameter)),\n"
3304       "                     SecondLongCall(parameter));\n"
3305       "}");
3306   verifyFormat(
3307       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3308       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3309       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3310       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
3311   verifyFormat(
3312       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3313       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
3314       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
3315       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
3316   verifyFormat(
3317       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3318       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
3319       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
3320       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
3321   verifyFormat("int a = bbbb && ccc && fffff(\n"
3322                "#define A Just forcing a new line\n"
3323                "                           ddd);");
3324 }
3325 
3326 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
3327   verifyFormat(
3328       "bool aaaaaaa =\n"
3329       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
3330       "    bbbbbbbb();");
3331   verifyFormat(
3332       "bool aaaaaaa =\n"
3333       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
3334       "    bbbbbbbb();");
3335 
3336   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3337                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
3338                "    ccccccccc == ddddddddddd;");
3339   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3340                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
3341                "    ccccccccc == ddddddddddd;");
3342   verifyFormat(
3343       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3344       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
3345       "    ccccccccc == ddddddddddd;");
3346 
3347   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3348                "                 aaaaaa) &&\n"
3349                "         bbbbbb && cccccc;");
3350   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3351                "                 aaaaaa) >>\n"
3352                "         bbbbbb;");
3353   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
3354                "    SourceMgr.getSpellingColumnNumber(\n"
3355                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
3356                "    1);");
3357 
3358   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3359                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
3360                "    cccccc) {\n}");
3361   verifyFormat("b = a &&\n"
3362                "    // Comment\n"
3363                "    b.c && d;");
3364 
3365   // If the LHS of a comparison is not a binary expression itself, the
3366   // additional linebreak confuses many people.
3367   verifyFormat(
3368       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3369       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
3370       "}");
3371   verifyFormat(
3372       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3373       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3374       "}");
3375   verifyFormat(
3376       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
3377       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3378       "}");
3379   // Even explicit parentheses stress the precedence enough to make the
3380   // additional break unnecessary.
3381   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3382                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3383                "}");
3384   // This cases is borderline, but with the indentation it is still readable.
3385   verifyFormat(
3386       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3387       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3388       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
3389       "}",
3390       getLLVMStyleWithColumns(75));
3391 
3392   // If the LHS is a binary expression, we should still use the additional break
3393   // as otherwise the formatting hides the operator precedence.
3394   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3395                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3396                "    5) {\n"
3397                "}");
3398 
3399   FormatStyle OnePerLine = getLLVMStyle();
3400   OnePerLine.BinPackParameters = false;
3401   verifyFormat(
3402       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3403       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3404       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
3405       OnePerLine);
3406 }
3407 
3408 TEST_F(FormatTest, ExpressionIndentation) {
3409   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3410                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3411                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3412                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3413                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3414                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
3415                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3416                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
3417                "                 ccccccccccccccccccccccccccccccccccccccccc;");
3418   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3419                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3420                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3421                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3422   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3423                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3424                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3425                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3426   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3427                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3428                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3429                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3430   verifyFormat("if () {\n"
3431                "} else if (aaaaa &&\n"
3432                "           bbbbb > // break\n"
3433                "               ccccc) {\n"
3434                "}");
3435 
3436   // Presence of a trailing comment used to change indentation of b.
3437   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
3438                "       b;\n"
3439                "return aaaaaaaaaaaaaaaaaaa +\n"
3440                "       b; //",
3441                getLLVMStyleWithColumns(30));
3442 }
3443 
3444 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
3445   // Not sure what the best system is here. Like this, the LHS can be found
3446   // immediately above an operator (everything with the same or a higher
3447   // indent). The RHS is aligned right of the operator and so compasses
3448   // everything until something with the same indent as the operator is found.
3449   // FIXME: Is this a good system?
3450   FormatStyle Style = getLLVMStyle();
3451   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
3452   verifyFormat(
3453       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3454       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3455       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3456       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3457       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3458       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3459       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3460       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3461       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
3462       Style);
3463   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3464                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3465                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3466                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3467                Style);
3468   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3469                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3470                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3471                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3472                Style);
3473   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3474                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3475                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3476                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3477                Style);
3478   verifyFormat("if () {\n"
3479                "} else if (aaaaa\n"
3480                "           && bbbbb // break\n"
3481                "                  > ccccc) {\n"
3482                "}",
3483                Style);
3484   verifyFormat("return (a)\n"
3485                "       // comment\n"
3486                "       + b;",
3487                Style);
3488   verifyFormat(
3489       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3490       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3491       "             + cc;",
3492       Style);
3493 
3494   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3495                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3496                Style);
3497 
3498   // Forced by comments.
3499   verifyFormat(
3500       "unsigned ContentSize =\n"
3501       "    sizeof(int16_t)   // DWARF ARange version number\n"
3502       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
3503       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
3504       "    + sizeof(int8_t); // Segment Size (in bytes)");
3505 
3506   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
3507                "       == boost::fusion::at_c<1>(iiii).second;",
3508                Style);
3509 
3510   Style.ColumnLimit = 60;
3511   verifyFormat("zzzzzzzzzz\n"
3512                "    = bbbbbbbbbbbbbbbbb\n"
3513                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
3514                Style);
3515 }
3516 
3517 TEST_F(FormatTest, NoOperandAlignment) {
3518   FormatStyle Style = getLLVMStyle();
3519   Style.AlignOperands = false;
3520   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3521   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3522                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3523                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3524                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3525                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3526                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3527                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3528                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3529                "        > ccccccccccccccccccccccccccccccccccccccccc;",
3530                Style);
3531 
3532   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3533                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3534                "    + cc;",
3535                Style);
3536   verifyFormat("int a = aa\n"
3537                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3538                "        * cccccccccccccccccccccccccccccccccccc;",
3539                Style);
3540 
3541   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3542   verifyFormat("return (a > b\n"
3543                "    // comment1\n"
3544                "    // comment2\n"
3545                "    || c);",
3546                Style);
3547 }
3548 
3549 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
3550   FormatStyle Style = getLLVMStyle();
3551   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3552   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3553                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3554                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
3555                Style);
3556 }
3557 
3558 TEST_F(FormatTest, ConstructorInitializers) {
3559   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3560   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
3561                getLLVMStyleWithColumns(45));
3562   verifyFormat("Constructor()\n"
3563                "    : Inttializer(FitsOnTheLine) {}",
3564                getLLVMStyleWithColumns(44));
3565   verifyFormat("Constructor()\n"
3566                "    : Inttializer(FitsOnTheLine) {}",
3567                getLLVMStyleWithColumns(43));
3568 
3569   verifyFormat("template <typename T>\n"
3570                "Constructor() : Initializer(FitsOnTheLine) {}",
3571                getLLVMStyleWithColumns(45));
3572 
3573   verifyFormat(
3574       "SomeClass::Constructor()\n"
3575       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3576 
3577   verifyFormat(
3578       "SomeClass::Constructor()\n"
3579       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3580       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
3581   verifyFormat(
3582       "SomeClass::Constructor()\n"
3583       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3584       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3585   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3586                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3587                "    : aaaaaaaaaa(aaaaaa) {}");
3588 
3589   verifyFormat("Constructor()\n"
3590                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3591                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3592                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3593                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
3594 
3595   verifyFormat("Constructor()\n"
3596                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3597                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3598 
3599   verifyFormat("Constructor(int Parameter = 0)\n"
3600                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3601                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
3602   verifyFormat("Constructor()\n"
3603                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3604                "}",
3605                getLLVMStyleWithColumns(60));
3606   verifyFormat("Constructor()\n"
3607                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3608                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
3609 
3610   // Here a line could be saved by splitting the second initializer onto two
3611   // lines, but that is not desirable.
3612   verifyFormat("Constructor()\n"
3613                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3614                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
3615                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3616 
3617   FormatStyle OnePerLine = getLLVMStyle();
3618   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3619   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3620   verifyFormat("SomeClass::Constructor()\n"
3621                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3622                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3623                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3624                OnePerLine);
3625   verifyFormat("SomeClass::Constructor()\n"
3626                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3627                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3628                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3629                OnePerLine);
3630   verifyFormat("MyClass::MyClass(int var)\n"
3631                "    : some_var_(var),            // 4 space indent\n"
3632                "      some_other_var_(var + 1) { // lined up\n"
3633                "}",
3634                OnePerLine);
3635   verifyFormat("Constructor()\n"
3636                "    : aaaaa(aaaaaa),\n"
3637                "      aaaaa(aaaaaa),\n"
3638                "      aaaaa(aaaaaa),\n"
3639                "      aaaaa(aaaaaa),\n"
3640                "      aaaaa(aaaaaa) {}",
3641                OnePerLine);
3642   verifyFormat("Constructor()\n"
3643                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3644                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
3645                OnePerLine);
3646   OnePerLine.BinPackParameters = false;
3647   verifyFormat(
3648       "Constructor()\n"
3649       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3650       "          aaaaaaaaaaa().aaa(),\n"
3651       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3652       OnePerLine);
3653   OnePerLine.ColumnLimit = 60;
3654   verifyFormat("Constructor()\n"
3655                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
3656                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3657                OnePerLine);
3658 
3659   EXPECT_EQ("Constructor()\n"
3660             "    : // Comment forcing unwanted break.\n"
3661             "      aaaa(aaaa) {}",
3662             format("Constructor() :\n"
3663                    "    // Comment forcing unwanted break.\n"
3664                    "    aaaa(aaaa) {}"));
3665 }
3666 
3667 TEST_F(FormatTest, MemoizationTests) {
3668   // This breaks if the memoization lookup does not take \c Indent and
3669   // \c LastSpace into account.
3670   verifyFormat(
3671       "extern CFRunLoopTimerRef\n"
3672       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
3673       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
3674       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
3675       "                     CFRunLoopTimerContext *context) {}");
3676 
3677   // Deep nesting somewhat works around our memoization.
3678   verifyFormat(
3679       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3680       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3681       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3682       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3683       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
3684       getLLVMStyleWithColumns(65));
3685   verifyFormat(
3686       "aaaaa(\n"
3687       "    aaaaa,\n"
3688       "    aaaaa(\n"
3689       "        aaaaa,\n"
3690       "        aaaaa(\n"
3691       "            aaaaa,\n"
3692       "            aaaaa(\n"
3693       "                aaaaa,\n"
3694       "                aaaaa(\n"
3695       "                    aaaaa,\n"
3696       "                    aaaaa(\n"
3697       "                        aaaaa,\n"
3698       "                        aaaaa(\n"
3699       "                            aaaaa,\n"
3700       "                            aaaaa(\n"
3701       "                                aaaaa,\n"
3702       "                                aaaaa(\n"
3703       "                                    aaaaa,\n"
3704       "                                    aaaaa(\n"
3705       "                                        aaaaa,\n"
3706       "                                        aaaaa(\n"
3707       "                                            aaaaa,\n"
3708       "                                            aaaaa(\n"
3709       "                                                aaaaa,\n"
3710       "                                                aaaaa))))))))))));",
3711       getLLVMStyleWithColumns(65));
3712   verifyFormat(
3713       "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"
3714       "                                  a),\n"
3715       "                                a),\n"
3716       "                              a),\n"
3717       "                            a),\n"
3718       "                          a),\n"
3719       "                        a),\n"
3720       "                      a),\n"
3721       "                    a),\n"
3722       "                  a),\n"
3723       "                a),\n"
3724       "              a),\n"
3725       "            a),\n"
3726       "          a),\n"
3727       "        a),\n"
3728       "      a),\n"
3729       "    a),\n"
3730       "  a)",
3731       getLLVMStyleWithColumns(65));
3732 
3733   // This test takes VERY long when memoization is broken.
3734   FormatStyle OnePerLine = getLLVMStyle();
3735   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3736   OnePerLine.BinPackParameters = false;
3737   std::string input = "Constructor()\n"
3738                       "    : aaaa(a,\n";
3739   for (unsigned i = 0, e = 80; i != e; ++i) {
3740     input += "           a,\n";
3741   }
3742   input += "           a) {}";
3743   verifyFormat(input, OnePerLine);
3744 }
3745 
3746 TEST_F(FormatTest, BreaksAsHighAsPossible) {
3747   verifyFormat(
3748       "void f() {\n"
3749       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
3750       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
3751       "    f();\n"
3752       "}");
3753   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
3754                "    Intervals[i - 1].getRange().getLast()) {\n}");
3755 }
3756 
3757 TEST_F(FormatTest, BreaksFunctionDeclarations) {
3758   // Principially, we break function declarations in a certain order:
3759   // 1) break amongst arguments.
3760   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
3761                "                              Cccccccccccccc cccccccccccccc);");
3762   verifyFormat("template <class TemplateIt>\n"
3763                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
3764                "                            TemplateIt *stop) {}");
3765 
3766   // 2) break after return type.
3767   verifyFormat(
3768       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3769       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
3770       getGoogleStyle());
3771 
3772   // 3) break after (.
3773   verifyFormat(
3774       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
3775       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
3776       getGoogleStyle());
3777 
3778   // 4) break before after nested name specifiers.
3779   verifyFormat(
3780       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3781       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
3782       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
3783       getGoogleStyle());
3784 
3785   // However, there are exceptions, if a sufficient amount of lines can be
3786   // saved.
3787   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
3788   // more adjusting.
3789   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3790                "                                  Cccccccccccccc cccccccccc,\n"
3791                "                                  Cccccccccccccc cccccccccc,\n"
3792                "                                  Cccccccccccccc cccccccccc,\n"
3793                "                                  Cccccccccccccc cccccccccc);");
3794   verifyFormat(
3795       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3796       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3797       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3798       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
3799       getGoogleStyle());
3800   verifyFormat(
3801       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3802       "                                          Cccccccccccccc cccccccccc,\n"
3803       "                                          Cccccccccccccc cccccccccc,\n"
3804       "                                          Cccccccccccccc cccccccccc,\n"
3805       "                                          Cccccccccccccc cccccccccc,\n"
3806       "                                          Cccccccccccccc cccccccccc,\n"
3807       "                                          Cccccccccccccc cccccccccc);");
3808   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3809                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3810                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3811                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3812                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
3813 
3814   // Break after multi-line parameters.
3815   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3816                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3817                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3818                "    bbbb bbbb);");
3819   verifyFormat("void SomeLoooooooooooongFunction(\n"
3820                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
3821                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3822                "    int bbbbbbbbbbbbb);");
3823 
3824   // Treat overloaded operators like other functions.
3825   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3826                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
3827   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3828                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
3829   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3830                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
3831   verifyGoogleFormat(
3832       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
3833       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3834   verifyGoogleFormat(
3835       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
3836       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3837   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3838                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3839   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
3840                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3841   verifyGoogleFormat(
3842       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
3843       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3844       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
3845 
3846   FormatStyle Style = getLLVMStyle();
3847   Style.PointerAlignment = FormatStyle::PAS_Left;
3848   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3849                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
3850                Style);
3851   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
3852                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3853                Style);
3854 }
3855 
3856 TEST_F(FormatTest, TrailingReturnType) {
3857   verifyFormat("auto foo() -> int;\n");
3858   verifyFormat("struct S {\n"
3859                "  auto bar() const -> int;\n"
3860                "};");
3861   verifyFormat("template <size_t Order, typename T>\n"
3862                "auto load_img(const std::string &filename)\n"
3863                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
3864   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
3865                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
3866   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
3867   verifyFormat("template <typename T>\n"
3868                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
3869                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
3870 
3871   // Not trailing return types.
3872   verifyFormat("void f() { auto a = b->c(); }");
3873 }
3874 
3875 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
3876   // Avoid breaking before trailing 'const' or other trailing annotations, if
3877   // they are not function-like.
3878   FormatStyle Style = getGoogleStyle();
3879   Style.ColumnLimit = 47;
3880   verifyFormat("void someLongFunction(\n"
3881                "    int someLoooooooooooooongParameter) const {\n}",
3882                getLLVMStyleWithColumns(47));
3883   verifyFormat("LoooooongReturnType\n"
3884                "someLoooooooongFunction() const {}",
3885                getLLVMStyleWithColumns(47));
3886   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
3887                "    const {}",
3888                Style);
3889   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3890                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
3891   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3892                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
3893   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3894                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
3895   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
3896                "                   aaaaaaaaaaa aaaaa) const override;");
3897   verifyGoogleFormat(
3898       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3899       "    const override;");
3900 
3901   // Even if the first parameter has to be wrapped.
3902   verifyFormat("void someLongFunction(\n"
3903                "    int someLongParameter) const {}",
3904                getLLVMStyleWithColumns(46));
3905   verifyFormat("void someLongFunction(\n"
3906                "    int someLongParameter) const {}",
3907                Style);
3908   verifyFormat("void someLongFunction(\n"
3909                "    int someLongParameter) override {}",
3910                Style);
3911   verifyFormat("void someLongFunction(\n"
3912                "    int someLongParameter) OVERRIDE {}",
3913                Style);
3914   verifyFormat("void someLongFunction(\n"
3915                "    int someLongParameter) final {}",
3916                Style);
3917   verifyFormat("void someLongFunction(\n"
3918                "    int someLongParameter) FINAL {}",
3919                Style);
3920   verifyFormat("void someLongFunction(\n"
3921                "    int parameter) const override {}",
3922                Style);
3923 
3924   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
3925   verifyFormat("void someLongFunction(\n"
3926                "    int someLongParameter) const\n"
3927                "{\n"
3928                "}",
3929                Style);
3930 
3931   // Unless these are unknown annotations.
3932   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
3933                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3934                "    LONG_AND_UGLY_ANNOTATION;");
3935 
3936   // Breaking before function-like trailing annotations is fine to keep them
3937   // close to their arguments.
3938   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3939                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3940   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3941                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3942   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3943                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
3944   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
3945                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
3946   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
3947 
3948   verifyFormat(
3949       "void aaaaaaaaaaaaaaaaaa()\n"
3950       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
3951       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
3952   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3953                "    __attribute__((unused));");
3954   verifyGoogleFormat(
3955       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3956       "    GUARDED_BY(aaaaaaaaaaaa);");
3957   verifyGoogleFormat(
3958       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3959       "    GUARDED_BY(aaaaaaaaaaaa);");
3960   verifyGoogleFormat(
3961       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3962       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3963   verifyGoogleFormat(
3964       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3965       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
3966 }
3967 
3968 TEST_F(FormatTest, FunctionAnnotations) {
3969   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3970                "int OldFunction(const string &parameter) {}");
3971   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3972                "string OldFunction(const string &parameter) {}");
3973   verifyFormat("template <typename T>\n"
3974                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3975                "string OldFunction(const string &parameter) {}");
3976 
3977   // Not function annotations.
3978   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3979                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
3980   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
3981                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
3982 }
3983 
3984 TEST_F(FormatTest, BreaksDesireably) {
3985   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3986                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3987                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
3988   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3989                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
3990                "}");
3991 
3992   verifyFormat(
3993       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3994       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3995 
3996   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3997                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3998                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3999 
4000   verifyFormat(
4001       "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4002       "                            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4003       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4004       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
4005 
4006   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4007                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4008 
4009   verifyFormat(
4010       "void f() {\n"
4011       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
4012       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4013       "}");
4014   verifyFormat(
4015       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4016       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4017   verifyFormat(
4018       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4019       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4020   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4021                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4022                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4023 
4024   // Indent consistently independent of call expression and unary operator.
4025   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4026                "    dddddddddddddddddddddddddddddd));");
4027   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4028                "    dddddddddddddddddddddddddddddd));");
4029   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
4030                "    dddddddddddddddddddddddddddddd));");
4031 
4032   // This test case breaks on an incorrect memoization, i.e. an optimization not
4033   // taking into account the StopAt value.
4034   verifyFormat(
4035       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4036       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4037       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4038       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4039 
4040   verifyFormat("{\n  {\n    {\n"
4041                "      Annotation.SpaceRequiredBefore =\n"
4042                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
4043                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
4044                "    }\n  }\n}");
4045 
4046   // Break on an outer level if there was a break on an inner level.
4047   EXPECT_EQ("f(g(h(a, // comment\n"
4048             "      b, c),\n"
4049             "    d, e),\n"
4050             "  x, y);",
4051             format("f(g(h(a, // comment\n"
4052                    "    b, c), d, e), x, y);"));
4053 
4054   // Prefer breaking similar line breaks.
4055   verifyFormat(
4056       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
4057       "                             NSTrackingMouseEnteredAndExited |\n"
4058       "                             NSTrackingActiveAlways;");
4059 }
4060 
4061 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
4062   FormatStyle NoBinPacking = getGoogleStyle();
4063   NoBinPacking.BinPackParameters = false;
4064   NoBinPacking.BinPackArguments = true;
4065   verifyFormat("void f() {\n"
4066                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
4067                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4068                "}",
4069                NoBinPacking);
4070   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
4071                "       int aaaaaaaaaaaaaaaaaaaa,\n"
4072                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4073                NoBinPacking);
4074 
4075   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4076   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4077                "                        vector<int> bbbbbbbbbbbbbbb);",
4078                NoBinPacking);
4079   // FIXME: This behavior difference is probably not wanted. However, currently
4080   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
4081   // template arguments from BreakBeforeParameter being set because of the
4082   // one-per-line formatting.
4083   verifyFormat(
4084       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4085       "                                             aaaaaaaaaa> aaaaaaaaaa);",
4086       NoBinPacking);
4087   verifyFormat(
4088       "void fffffffffff(\n"
4089       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
4090       "        aaaaaaaaaa);");
4091 }
4092 
4093 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
4094   FormatStyle NoBinPacking = getGoogleStyle();
4095   NoBinPacking.BinPackParameters = false;
4096   NoBinPacking.BinPackArguments = false;
4097   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
4098                "  aaaaaaaaaaaaaaaaaaaa,\n"
4099                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
4100                NoBinPacking);
4101   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
4102                "        aaaaaaaaaaaaa,\n"
4103                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
4104                NoBinPacking);
4105   verifyFormat(
4106       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4107       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4108       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4109       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4110       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
4111       NoBinPacking);
4112   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4113                "    .aaaaaaaaaaaaaaaaaa();",
4114                NoBinPacking);
4115   verifyFormat("void f() {\n"
4116                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4117                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
4118                "}",
4119                NoBinPacking);
4120 
4121   verifyFormat(
4122       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4123       "             aaaaaaaaaaaa,\n"
4124       "             aaaaaaaaaaaa);",
4125       NoBinPacking);
4126   verifyFormat(
4127       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
4128       "                               ddddddddddddddddddddddddddddd),\n"
4129       "             test);",
4130       NoBinPacking);
4131 
4132   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4133                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
4134                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
4135                "    aaaaaaaaaaaaaaaaaa;",
4136                NoBinPacking);
4137   verifyFormat("a(\"a\"\n"
4138                "  \"a\",\n"
4139                "  a);");
4140 
4141   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4142   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
4143                "                aaaaaaaaa,\n"
4144                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4145                NoBinPacking);
4146   verifyFormat(
4147       "void f() {\n"
4148       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4149       "      .aaaaaaa();\n"
4150       "}",
4151       NoBinPacking);
4152   verifyFormat(
4153       "template <class SomeType, class SomeOtherType>\n"
4154       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
4155       NoBinPacking);
4156 }
4157 
4158 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
4159   FormatStyle Style = getLLVMStyleWithColumns(15);
4160   Style.ExperimentalAutoDetectBinPacking = true;
4161   EXPECT_EQ("aaa(aaaa,\n"
4162             "    aaaa,\n"
4163             "    aaaa);\n"
4164             "aaa(aaaa,\n"
4165             "    aaaa,\n"
4166             "    aaaa);",
4167             format("aaa(aaaa,\n" // one-per-line
4168                    "  aaaa,\n"
4169                    "    aaaa  );\n"
4170                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4171                    Style));
4172   EXPECT_EQ("aaa(aaaa, aaaa,\n"
4173             "    aaaa);\n"
4174             "aaa(aaaa, aaaa,\n"
4175             "    aaaa);",
4176             format("aaa(aaaa,  aaaa,\n" // bin-packed
4177                    "    aaaa  );\n"
4178                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4179                    Style));
4180 }
4181 
4182 TEST_F(FormatTest, FormatsBuilderPattern) {
4183   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
4184                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
4185                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
4186                "    .StartsWith(\".init\", ORDER_INIT)\n"
4187                "    .StartsWith(\".fini\", ORDER_FINI)\n"
4188                "    .StartsWith(\".hash\", ORDER_HASH)\n"
4189                "    .Default(ORDER_TEXT);\n");
4190 
4191   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
4192                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
4193   verifyFormat(
4194       "aaaaaaa->aaaaaaa\n"
4195       "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4196       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4197       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4198   verifyFormat(
4199       "aaaaaaa->aaaaaaa\n"
4200       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4201       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4202   verifyFormat(
4203       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
4204       "    aaaaaaaaaaaaaa);");
4205   verifyFormat(
4206       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
4207       "    aaaaaa->aaaaaaaaaaaa()\n"
4208       "        ->aaaaaaaaaaaaaaaa(\n"
4209       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4210       "        ->aaaaaaaaaaaaaaaaa();");
4211   verifyGoogleFormat(
4212       "void f() {\n"
4213       "  someo->Add((new util::filetools::Handler(dir))\n"
4214       "                 ->OnEvent1(NewPermanentCallback(\n"
4215       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
4216       "                 ->OnEvent2(NewPermanentCallback(\n"
4217       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
4218       "                 ->OnEvent3(NewPermanentCallback(\n"
4219       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
4220       "                 ->OnEvent5(NewPermanentCallback(\n"
4221       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
4222       "                 ->OnEvent6(NewPermanentCallback(\n"
4223       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
4224       "}");
4225 
4226   verifyFormat(
4227       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
4228   verifyFormat("aaaaaaaaaaaaaaa()\n"
4229                "    .aaaaaaaaaaaaaaa()\n"
4230                "    .aaaaaaaaaaaaaaa()\n"
4231                "    .aaaaaaaaaaaaaaa()\n"
4232                "    .aaaaaaaaaaaaaaa();");
4233   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4234                "    .aaaaaaaaaaaaaaa()\n"
4235                "    .aaaaaaaaaaaaaaa()\n"
4236                "    .aaaaaaaaaaaaaaa();");
4237   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4238                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4239                "    .aaaaaaaaaaaaaaa();");
4240   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
4241                "    ->aaaaaaaaaaaaaae(0)\n"
4242                "    ->aaaaaaaaaaaaaaa();");
4243 
4244   // Don't linewrap after very short segments.
4245   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4246                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4247                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4248   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4249                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4250                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4251   verifyFormat("aaa()\n"
4252                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4253                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4254                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4255 
4256   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4257                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4258                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
4259   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4260                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
4261                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
4262 
4263   // Prefer not to break after empty parentheses.
4264   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
4265                "    First->LastNewlineOffset);");
4266 
4267   // Prefer not to create "hanging" indents.
4268   verifyFormat(
4269       "return !soooooooooooooome_map\n"
4270       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4271       "            .second;");
4272   verifyFormat(
4273       "return aaaaaaaaaaaaaaaa\n"
4274       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
4275       "    .aaaa(aaaaaaaaaaaaaa);");
4276   // No hanging indent here.
4277   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
4278                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4279   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
4280                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4281   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4282                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4283                getLLVMStyleWithColumns(60));
4284   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
4285                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4286                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4287                getLLVMStyleWithColumns(59));
4288   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4289                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4290                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4291 }
4292 
4293 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
4294   verifyFormat(
4295       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4296       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
4297   verifyFormat(
4298       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
4299       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
4300 
4301   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4302                "    ccccccccccccccccccccccccc) {\n}");
4303   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
4304                "    ccccccccccccccccccccccccc) {\n}");
4305 
4306   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4307                "    ccccccccccccccccccccccccc) {\n}");
4308   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
4309                "    ccccccccccccccccccccccccc) {\n}");
4310 
4311   verifyFormat(
4312       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
4313       "    ccccccccccccccccccccccccc) {\n}");
4314   verifyFormat(
4315       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
4316       "    ccccccccccccccccccccccccc) {\n}");
4317 
4318   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
4319                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
4320                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
4321                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4322   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
4323                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
4324                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
4325                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4326 
4327   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
4328                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
4329                "    aaaaaaaaaaaaaaa != aa) {\n}");
4330   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
4331                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
4332                "    aaaaaaaaaaaaaaa != aa) {\n}");
4333 }
4334 
4335 TEST_F(FormatTest, BreaksAfterAssignments) {
4336   verifyFormat(
4337       "unsigned Cost =\n"
4338       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
4339       "                        SI->getPointerAddressSpaceee());\n");
4340   verifyFormat(
4341       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
4342       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
4343 
4344   verifyFormat(
4345       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
4346       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
4347   verifyFormat("unsigned OriginalStartColumn =\n"
4348                "    SourceMgr.getSpellingColumnNumber(\n"
4349                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
4350                "    1;");
4351 }
4352 
4353 TEST_F(FormatTest, AlignsAfterAssignments) {
4354   verifyFormat(
4355       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4356       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
4357   verifyFormat(
4358       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4359       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
4360   verifyFormat(
4361       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4362       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
4363   verifyFormat(
4364       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4365       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
4366   verifyFormat(
4367       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4368       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4369       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
4370 }
4371 
4372 TEST_F(FormatTest, AlignsAfterReturn) {
4373   verifyFormat(
4374       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4375       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
4376   verifyFormat(
4377       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4378       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
4379   verifyFormat(
4380       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4381       "       aaaaaaaaaaaaaaaaaaaaaa();");
4382   verifyFormat(
4383       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4384       "        aaaaaaaaaaaaaaaaaaaaaa());");
4385   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4386                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4387   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4388                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
4389                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4390   verifyFormat("return\n"
4391                "    // true if code is one of a or b.\n"
4392                "    code == a || code == b;");
4393 }
4394 
4395 TEST_F(FormatTest, AlignsAfterOpenBracket) {
4396   verifyFormat(
4397       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4398       "                                                aaaaaaaaa aaaaaaa) {}");
4399   verifyFormat(
4400       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4401       "                                               aaaaaaaaaaa aaaaaaaaa);");
4402   verifyFormat(
4403       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4404       "                                             aaaaaaaaaaaaaaaaaaaaa));");
4405   FormatStyle Style = getLLVMStyle();
4406   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4407   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4408                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
4409                Style);
4410   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4411                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
4412                Style);
4413   verifyFormat("SomeLongVariableName->someFunction(\n"
4414                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
4415                Style);
4416   verifyFormat(
4417       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4418       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4419       Style);
4420   verifyFormat(
4421       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4422       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4423       Style);
4424   verifyFormat(
4425       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4426       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4427       Style);
4428 
4429   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4430   Style.BinPackArguments = false;
4431   Style.BinPackParameters = false;
4432   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4433                "    aaaaaaaaaaa aaaaaaaa,\n"
4434                "    aaaaaaaaa aaaaaaa,\n"
4435                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4436                Style);
4437   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4438                "    aaaaaaaaaaa aaaaaaaaa,\n"
4439                "    aaaaaaaaaaa aaaaaaaaa,\n"
4440                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4441                Style);
4442   verifyFormat("SomeLongVariableName->someFunction(\n"
4443                "    foooooooo(\n"
4444                "        aaaaaaaaaaaaaaa,\n"
4445                "        aaaaaaaaaaaaaaaaaaaaa,\n"
4446                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4447                Style);
4448 }
4449 
4450 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
4451   FormatStyle Style = getLLVMStyleWithColumns(40);
4452   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4453                "          bbbbbbbbbbbbbbbbbbbbbb);",
4454                Style);
4455   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
4456   Style.AlignOperands = false;
4457   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4458                "          bbbbbbbbbbbbbbbbbbbbbb);",
4459                Style);
4460   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4461   Style.AlignOperands = true;
4462   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4463                "          bbbbbbbbbbbbbbbbbbbbbb);",
4464                Style);
4465   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4466   Style.AlignOperands = false;
4467   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4468                "    bbbbbbbbbbbbbbbbbbbbbb);",
4469                Style);
4470 }
4471 
4472 TEST_F(FormatTest, BreaksConditionalExpressions) {
4473   verifyFormat(
4474       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4475       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4476       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4477   verifyFormat(
4478       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4479       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4480   verifyFormat(
4481       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
4482       "                                                    : aaaaaaaaaaaaa);");
4483   verifyFormat(
4484       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4485       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4486       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4487       "                   aaaaaaaaaaaaa);");
4488   verifyFormat(
4489       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4490       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4491       "                   aaaaaaaaaaaaa);");
4492   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4493                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4494                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4495                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4496                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4497   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4498                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4499                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4500                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4501                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4502                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4503                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4504   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4505                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4506                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4507                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4508                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4509   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4510                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4511                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4512   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4513                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4514                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4515                "        : aaaaaaaaaaaaaaaa;");
4516   verifyFormat(
4517       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4518       "    ? aaaaaaaaaaaaaaa\n"
4519       "    : aaaaaaaaaaaaaaa;");
4520   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4521                "          aaaaaaaaa\n"
4522                "      ? b\n"
4523                "      : c);");
4524   verifyFormat("return aaaa == bbbb\n"
4525                "           // comment\n"
4526                "           ? aaaa\n"
4527                "           : bbbb;");
4528   verifyFormat("unsigned Indent =\n"
4529                "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
4530                "                              ? IndentForLevel[TheLine.Level]\n"
4531                "                              : TheLine * 2,\n"
4532                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4533                getLLVMStyleWithColumns(70));
4534   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4535                "                  ? aaaaaaaaaaaaaaa\n"
4536                "                  : bbbbbbbbbbbbbbb //\n"
4537                "                        ? ccccccccccccccc\n"
4538                "                        : ddddddddddddddd;");
4539   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4540                "                  ? aaaaaaaaaaaaaaa\n"
4541                "                  : (bbbbbbbbbbbbbbb //\n"
4542                "                         ? ccccccccccccccc\n"
4543                "                         : ddddddddddddddd);");
4544   verifyFormat(
4545       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4546       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4547       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
4548       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
4549       "                                      : aaaaaaaaaa;");
4550   verifyFormat(
4551       "aaaaaa = aaaaaaaaaaaa\n"
4552       "             ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4553       "                          : aaaaaaaaaaaaaaaaaaaaaa\n"
4554       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4555 
4556   FormatStyle NoBinPacking = getLLVMStyle();
4557   NoBinPacking.BinPackArguments = false;
4558   verifyFormat(
4559       "void f() {\n"
4560       "  g(aaa,\n"
4561       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4562       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4563       "        ? aaaaaaaaaaaaaaa\n"
4564       "        : aaaaaaaaaaaaaaa);\n"
4565       "}",
4566       NoBinPacking);
4567   verifyFormat(
4568       "void f() {\n"
4569       "  g(aaa,\n"
4570       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4571       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4572       "        ?: aaaaaaaaaaaaaaa);\n"
4573       "}",
4574       NoBinPacking);
4575 
4576   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
4577                "             // comment.\n"
4578                "             ccccccccccccccccccccccccccccccccccccccc\n"
4579                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4580                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
4581 
4582   // Assignments in conditional expressions. Apparently not uncommon :-(.
4583   verifyFormat("return a != b\n"
4584                "           // comment\n"
4585                "           ? a = b\n"
4586                "           : a = b;");
4587   verifyFormat("return a != b\n"
4588                "           // comment\n"
4589                "           ? a = a != b\n"
4590                "                     // comment\n"
4591                "                     ? a = b\n"
4592                "                     : a\n"
4593                "           : a;\n");
4594   verifyFormat("return a != b\n"
4595                "           // comment\n"
4596                "           ? a\n"
4597                "           : a = a != b\n"
4598                "                     // comment\n"
4599                "                     ? a = b\n"
4600                "                     : a;");
4601 }
4602 
4603 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
4604   FormatStyle Style = getLLVMStyle();
4605   Style.BreakBeforeTernaryOperators = false;
4606   Style.ColumnLimit = 70;
4607   verifyFormat(
4608       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4609       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4610       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4611       Style);
4612   verifyFormat(
4613       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4614       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4615       Style);
4616   verifyFormat(
4617       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
4618       "                                                      aaaaaaaaaaaaa);",
4619       Style);
4620   verifyFormat(
4621       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4622       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4623       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4624       "                   aaaaaaaaaaaaa);",
4625       Style);
4626   verifyFormat(
4627       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4628       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4629       "                   aaaaaaaaaaaaa);",
4630       Style);
4631   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4632                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4633                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4634                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4635                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4636                Style);
4637   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4638                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4639                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4640                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4641                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4642                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4643                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4644                Style);
4645   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4646                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
4647                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4648                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4649                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4650                Style);
4651   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4652                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4653                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4654                Style);
4655   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4656                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4657                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4658                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4659                Style);
4660   verifyFormat(
4661       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4662       "    aaaaaaaaaaaaaaa :\n"
4663       "    aaaaaaaaaaaaaaa;",
4664       Style);
4665   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4666                "          aaaaaaaaa ?\n"
4667                "      b :\n"
4668                "      c);",
4669                Style);
4670   verifyFormat(
4671       "unsigned Indent =\n"
4672       "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
4673       "                              IndentForLevel[TheLine.Level] :\n"
4674       "                              TheLine * 2,\n"
4675       "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4676       Style);
4677   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4678                "                  aaaaaaaaaaaaaaa :\n"
4679                "                  bbbbbbbbbbbbbbb ? //\n"
4680                "                      ccccccccccccccc :\n"
4681                "                      ddddddddddddddd;",
4682                Style);
4683   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4684                "                  aaaaaaaaaaaaaaa :\n"
4685                "                  (bbbbbbbbbbbbbbb ? //\n"
4686                "                       ccccccccccccccc :\n"
4687                "                       ddddddddddddddd);",
4688                Style);
4689 }
4690 
4691 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
4692   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
4693                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
4694   verifyFormat("bool a = true, b = false;");
4695 
4696   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4697                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
4698                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
4699                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
4700   verifyFormat(
4701       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
4702       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
4703       "     d = e && f;");
4704   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
4705                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
4706   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4707                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
4708   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
4709                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
4710 
4711   FormatStyle Style = getGoogleStyle();
4712   Style.PointerAlignment = FormatStyle::PAS_Left;
4713   Style.DerivePointerAlignment = false;
4714   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4715                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
4716                "    *b = bbbbbbbbbbbbbbbbbbb;",
4717                Style);
4718   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4719                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
4720                Style);
4721 }
4722 
4723 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
4724   verifyFormat("arr[foo ? bar : baz];");
4725   verifyFormat("f()[foo ? bar : baz];");
4726   verifyFormat("(a + b)[foo ? bar : baz];");
4727   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
4728 }
4729 
4730 TEST_F(FormatTest, AlignsStringLiterals) {
4731   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
4732                "                                      \"short literal\");");
4733   verifyFormat(
4734       "looooooooooooooooooooooooongFunction(\n"
4735       "    \"short literal\"\n"
4736       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
4737   verifyFormat("someFunction(\"Always break between multi-line\"\n"
4738                "             \" string literals\",\n"
4739                "             and, other, parameters);");
4740   EXPECT_EQ("fun + \"1243\" /* comment */\n"
4741             "      \"5678\";",
4742             format("fun + \"1243\" /* comment */\n"
4743                    "      \"5678\";",
4744                    getLLVMStyleWithColumns(28)));
4745   EXPECT_EQ(
4746       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
4747       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
4748       "         \"aaaaaaaaaaaaaaaa\";",
4749       format("aaaaaa ="
4750              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
4751              "aaaaaaaaaaaaaaaaaaaaa\" "
4752              "\"aaaaaaaaaaaaaaaa\";"));
4753   verifyFormat("a = a + \"a\"\n"
4754                "        \"a\"\n"
4755                "        \"a\";");
4756   verifyFormat("f(\"a\", \"b\"\n"
4757                "       \"c\");");
4758 
4759   verifyFormat(
4760       "#define LL_FORMAT \"ll\"\n"
4761       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
4762       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
4763 
4764   verifyFormat("#define A(X)          \\\n"
4765                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
4766                "  \"ccccc\"",
4767                getLLVMStyleWithColumns(23));
4768   verifyFormat("#define A \"def\"\n"
4769                "f(\"abc\" A \"ghi\"\n"
4770                "  \"jkl\");");
4771 
4772   verifyFormat("f(L\"a\"\n"
4773                "  L\"b\");");
4774   verifyFormat("#define A(X)            \\\n"
4775                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
4776                "  L\"ccccc\"",
4777                getLLVMStyleWithColumns(25));
4778 
4779   verifyFormat("f(@\"a\"\n"
4780                "  @\"b\");");
4781   verifyFormat("NSString s = @\"a\"\n"
4782                "             @\"b\"\n"
4783                "             @\"c\";");
4784   verifyFormat("NSString s = @\"a\"\n"
4785                "              \"b\"\n"
4786                "              \"c\";");
4787 }
4788 
4789 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
4790   FormatStyle Style = getLLVMStyle();
4791   // No declarations or definitions should be moved to own line.
4792   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
4793   verifyFormat("class A {\n"
4794                "  int f() { return 1; }\n"
4795                "  int g();\n"
4796                "};\n"
4797                "int f() { return 1; }\n"
4798                "int g();\n",
4799                Style);
4800 
4801   // All declarations and definitions should have the return type moved to its
4802   // own
4803   // line.
4804   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
4805   verifyFormat("class E {\n"
4806                "  int\n"
4807                "  f() {\n"
4808                "    return 1;\n"
4809                "  }\n"
4810                "  int\n"
4811                "  g();\n"
4812                "};\n"
4813                "int\n"
4814                "f() {\n"
4815                "  return 1;\n"
4816                "}\n"
4817                "int\n"
4818                "g();\n",
4819                Style);
4820 
4821   // Top-level definitions, and no kinds of declarations should have the
4822   // return type moved to its own line.
4823   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
4824   verifyFormat("class B {\n"
4825                "  int f() { return 1; }\n"
4826                "  int g();\n"
4827                "};\n"
4828                "int\n"
4829                "f() {\n"
4830                "  return 1;\n"
4831                "}\n"
4832                "int g();\n",
4833                Style);
4834 
4835   // Top-level definitions and declarations should have the return type moved
4836   // to its own line.
4837   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
4838   verifyFormat("class C {\n"
4839                "  int f() { return 1; }\n"
4840                "  int g();\n"
4841                "};\n"
4842                "int\n"
4843                "f() {\n"
4844                "  return 1;\n"
4845                "}\n"
4846                "int\n"
4847                "g();\n",
4848                Style);
4849 
4850   // All definitions should have the return type moved to its own line, but no
4851   // kinds of declarations.
4852   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
4853   verifyFormat("class D {\n"
4854                "  int\n"
4855                "  f() {\n"
4856                "    return 1;\n"
4857                "  }\n"
4858                "  int g();\n"
4859                "};\n"
4860                "int\n"
4861                "f() {\n"
4862                "  return 1;\n"
4863                "}\n"
4864                "int g();\n",
4865                Style);
4866   verifyFormat("const char *\n"
4867                "f(void) {\n" // Break here.
4868                "  return \"\";\n"
4869                "}\n"
4870                "const char *bar(void);\n", // No break here.
4871                Style);
4872   verifyFormat("template <class T>\n"
4873                "T *\n"
4874                "f(T &c) {\n" // Break here.
4875                "  return NULL;\n"
4876                "}\n"
4877                "template <class T> T *f(T &c);\n", // No break here.
4878                Style);
4879   verifyFormat("class C {\n"
4880                "  int\n"
4881                "  operator+() {\n"
4882                "    return 1;\n"
4883                "  }\n"
4884                "  int\n"
4885                "  operator()() {\n"
4886                "    return 1;\n"
4887                "  }\n"
4888                "};\n",
4889                Style);
4890   verifyFormat("void\n"
4891                "A::operator()() {}\n"
4892                "void\n"
4893                "A::operator>>() {}\n"
4894                "void\n"
4895                "A::operator+() {}\n",
4896                Style);
4897   verifyFormat("void *operator new(std::size_t s);", // No break here.
4898                Style);
4899   verifyFormat("void *\n"
4900                "operator new(std::size_t s) {}",
4901                Style);
4902   verifyFormat("void *\n"
4903                "operator delete[](void *ptr) {}",
4904                Style);
4905   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4906   verifyFormat("const char *\n"
4907                "f(void)\n" // Break here.
4908                "{\n"
4909                "  return \"\";\n"
4910                "}\n"
4911                "const char *bar(void);\n", // No break here.
4912                Style);
4913   verifyFormat("template <class T>\n"
4914                "T *\n"     // Problem here: no line break
4915                "f(T &c)\n" // Break here.
4916                "{\n"
4917                "  return NULL;\n"
4918                "}\n"
4919                "template <class T> T *f(T &c);\n", // No break here.
4920                Style);
4921 }
4922 
4923 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
4924   FormatStyle NoBreak = getLLVMStyle();
4925   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
4926   FormatStyle Break = getLLVMStyle();
4927   Break.AlwaysBreakBeforeMultilineStrings = true;
4928   verifyFormat("aaaa = \"bbbb\"\n"
4929                "       \"cccc\";",
4930                NoBreak);
4931   verifyFormat("aaaa =\n"
4932                "    \"bbbb\"\n"
4933                "    \"cccc\";",
4934                Break);
4935   verifyFormat("aaaa(\"bbbb\"\n"
4936                "     \"cccc\");",
4937                NoBreak);
4938   verifyFormat("aaaa(\n"
4939                "    \"bbbb\"\n"
4940                "    \"cccc\");",
4941                Break);
4942   verifyFormat("aaaa(qqq, \"bbbb\"\n"
4943                "          \"cccc\");",
4944                NoBreak);
4945   verifyFormat("aaaa(qqq,\n"
4946                "     \"bbbb\"\n"
4947                "     \"cccc\");",
4948                Break);
4949   verifyFormat("aaaa(qqq,\n"
4950                "     L\"bbbb\"\n"
4951                "     L\"cccc\");",
4952                Break);
4953   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
4954                "                      \"bbbb\"));",
4955                Break);
4956   verifyFormat("string s = someFunction(\n"
4957                "    \"abc\"\n"
4958                "    \"abc\");",
4959                Break);
4960 
4961   // As we break before unary operators, breaking right after them is bad.
4962   verifyFormat("string foo = abc ? \"x\"\n"
4963                "                   \"blah blah blah blah blah blah\"\n"
4964                "                 : \"y\";",
4965                Break);
4966 
4967   // Don't break if there is no column gain.
4968   verifyFormat("f(\"aaaa\"\n"
4969                "  \"bbbb\");",
4970                Break);
4971 
4972   // Treat literals with escaped newlines like multi-line string literals.
4973   EXPECT_EQ("x = \"a\\\n"
4974             "b\\\n"
4975             "c\";",
4976             format("x = \"a\\\n"
4977                    "b\\\n"
4978                    "c\";",
4979                    NoBreak));
4980   EXPECT_EQ("xxxx =\n"
4981             "    \"a\\\n"
4982             "b\\\n"
4983             "c\";",
4984             format("xxxx = \"a\\\n"
4985                    "b\\\n"
4986                    "c\";",
4987                    Break));
4988 
4989   // Exempt ObjC strings for now.
4990   EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
4991             "                          @\"bbbb\";",
4992             format("NSString *const kString = @\"aaaa\"\n"
4993                    "@\"bbbb\";",
4994                    Break));
4995 
4996   Break.ColumnLimit = 0;
4997   verifyFormat("const char *hello = \"hello llvm\";", Break);
4998 }
4999 
5000 TEST_F(FormatTest, AlignsPipes) {
5001   verifyFormat(
5002       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5003       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5004       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5005   verifyFormat(
5006       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
5007       "                     << aaaaaaaaaaaaaaaaaaaa;");
5008   verifyFormat(
5009       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5010       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5011   verifyFormat(
5012       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
5013       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
5014       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
5015   verifyFormat(
5016       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5017       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5018       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5019   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5020                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5021                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5022                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5023   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
5024                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
5025   verifyFormat(
5026       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5027       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5028 
5029   verifyFormat("return out << \"somepacket = {\\n\"\n"
5030                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
5031                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
5032                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
5033                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
5034                "           << \"}\";");
5035 
5036   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5037                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5038                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
5039   verifyFormat(
5040       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
5041       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
5042       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
5043       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
5044       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
5045   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
5046                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5047   verifyFormat(
5048       "void f() {\n"
5049       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
5050       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
5051       "}");
5052   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
5053                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
5054   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5055                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5056                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
5057                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
5058   verifyFormat("LOG_IF(aaa == //\n"
5059                "       bbb)\n"
5060                "    << a << b;");
5061 
5062   // Breaking before the first "<<" is generally not desirable.
5063   verifyFormat(
5064       "llvm::errs()\n"
5065       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5066       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5067       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5068       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5069       getLLVMStyleWithColumns(70));
5070   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5071                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5072                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5073                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5074                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5075                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5076                getLLVMStyleWithColumns(70));
5077 
5078   // But sometimes, breaking before the first "<<" is desirable.
5079   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5080                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
5081   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
5082                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5083                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5084   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
5085                "    << BEF << IsTemplate << Description << E->getType();");
5086 
5087   verifyFormat(
5088       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5089       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5090 
5091   // Incomplete string literal.
5092   EXPECT_EQ("llvm::errs() << \"\n"
5093             "             << a;",
5094             format("llvm::errs() << \"\n<<a;"));
5095 
5096   verifyFormat("void f() {\n"
5097                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
5098                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
5099                "}");
5100 
5101   // Handle 'endl'.
5102   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
5103                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5104   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5105 
5106   // Handle '\n'.
5107   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
5108                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5109   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
5110                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
5111   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
5112                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
5113   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5114 }
5115 
5116 TEST_F(FormatTest, UnderstandsEquals) {
5117   verifyFormat(
5118       "aaaaaaaaaaaaaaaaa =\n"
5119       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5120   verifyFormat(
5121       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5122       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5123   verifyFormat(
5124       "if (a) {\n"
5125       "  f();\n"
5126       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5127       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5128       "}");
5129 
5130   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5131                "        100000000 + 10000000) {\n}");
5132 }
5133 
5134 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
5135   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5136                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
5137 
5138   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5139                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
5140 
5141   verifyFormat(
5142       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
5143       "                                                          Parameter2);");
5144 
5145   verifyFormat(
5146       "ShortObject->shortFunction(\n"
5147       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
5148       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
5149 
5150   verifyFormat("loooooooooooooongFunction(\n"
5151                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
5152 
5153   verifyFormat(
5154       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
5155       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
5156 
5157   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5158                "    .WillRepeatedly(Return(SomeValue));");
5159   verifyFormat("void f() {\n"
5160                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5161                "      .Times(2)\n"
5162                "      .WillRepeatedly(Return(SomeValue));\n"
5163                "}");
5164   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
5165                "    ccccccccccccccccccccccc);");
5166   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5167                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5168                "          .aaaaa(aaaaa),\n"
5169                "      aaaaaaaaaaaaaaaaaaaaa);");
5170   verifyFormat("void f() {\n"
5171                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5172                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
5173                "}");
5174   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5175                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5176                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5177                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5178                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5179   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5180                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5181                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5182                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
5183                "}");
5184 
5185   // Here, it is not necessary to wrap at "." or "->".
5186   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
5187                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5188   verifyFormat(
5189       "aaaaaaaaaaa->aaaaaaaaa(\n"
5190       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5191       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
5192 
5193   verifyFormat(
5194       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5195       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
5196   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
5197                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5198   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
5199                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5200 
5201   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5202                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5203                "    .a();");
5204 
5205   FormatStyle NoBinPacking = getLLVMStyle();
5206   NoBinPacking.BinPackParameters = false;
5207   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5208                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5209                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
5210                "                         aaaaaaaaaaaaaaaaaaa,\n"
5211                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5212                NoBinPacking);
5213 
5214   // If there is a subsequent call, change to hanging indentation.
5215   verifyFormat(
5216       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5217       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
5218       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5219   verifyFormat(
5220       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5221       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
5222   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5223                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5224                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5225   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5226                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5227                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5228 }
5229 
5230 TEST_F(FormatTest, WrapsTemplateDeclarations) {
5231   verifyFormat("template <typename T>\n"
5232                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5233   verifyFormat("template <typename T>\n"
5234                "// T should be one of {A, B}.\n"
5235                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5236   verifyFormat(
5237       "template <typename T>\n"
5238       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
5239   verifyFormat("template <typename T>\n"
5240                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
5241                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
5242   verifyFormat(
5243       "template <typename T>\n"
5244       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
5245       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
5246   verifyFormat(
5247       "template <typename T>\n"
5248       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
5249       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
5250       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5251   verifyFormat("template <typename T>\n"
5252                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5253                "    int aaaaaaaaaaaaaaaaaaaaaa);");
5254   verifyFormat(
5255       "template <typename T1, typename T2 = char, typename T3 = char,\n"
5256       "          typename T4 = char>\n"
5257       "void f();");
5258   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
5259                "          template <typename> class cccccccccccccccccccccc,\n"
5260                "          typename ddddddddddddd>\n"
5261                "class C {};");
5262   verifyFormat(
5263       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
5264       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5265 
5266   verifyFormat("void f() {\n"
5267                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
5268                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
5269                "}");
5270 
5271   verifyFormat("template <typename T> class C {};");
5272   verifyFormat("template <typename T> void f();");
5273   verifyFormat("template <typename T> void f() {}");
5274   verifyFormat(
5275       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5276       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5277       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
5278       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5279       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5280       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
5281       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
5282       getLLVMStyleWithColumns(72));
5283   EXPECT_EQ("static_cast<A< //\n"
5284             "    B> *>(\n"
5285             "\n"
5286             "    );",
5287             format("static_cast<A<//\n"
5288                    "    B>*>(\n"
5289                    "\n"
5290                    "    );"));
5291   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5292                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
5293 
5294   FormatStyle AlwaysBreak = getLLVMStyle();
5295   AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
5296   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
5297   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
5298   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
5299   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5300                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5301                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
5302   verifyFormat("template <template <typename> class Fooooooo,\n"
5303                "          template <typename> class Baaaaaaar>\n"
5304                "struct C {};",
5305                AlwaysBreak);
5306   verifyFormat("template <typename T> // T can be A, B or C.\n"
5307                "struct C {};",
5308                AlwaysBreak);
5309 }
5310 
5311 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
5312   verifyFormat(
5313       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5314       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5315   verifyFormat(
5316       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5317       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5318       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5319 
5320   // FIXME: Should we have the extra indent after the second break?
5321   verifyFormat(
5322       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5323       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5324       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5325 
5326   verifyFormat(
5327       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
5328       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
5329 
5330   // Breaking at nested name specifiers is generally not desirable.
5331   verifyFormat(
5332       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5333       "    aaaaaaaaaaaaaaaaaaaaaaa);");
5334 
5335   verifyFormat(
5336       "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5337       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5338       "                   aaaaaaaaaaaaaaaaaaaaa);",
5339       getLLVMStyleWithColumns(74));
5340 
5341   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5342                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5343                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5344 }
5345 
5346 TEST_F(FormatTest, UnderstandsTemplateParameters) {
5347   verifyFormat("A<int> a;");
5348   verifyFormat("A<A<A<int>>> a;");
5349   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
5350   verifyFormat("bool x = a < 1 || 2 > a;");
5351   verifyFormat("bool x = 5 < f<int>();");
5352   verifyFormat("bool x = f<int>() > 5;");
5353   verifyFormat("bool x = 5 < a<int>::x;");
5354   verifyFormat("bool x = a < 4 ? a > 2 : false;");
5355   verifyFormat("bool x = f() ? a < 2 : a > 2;");
5356 
5357   verifyGoogleFormat("A<A<int>> a;");
5358   verifyGoogleFormat("A<A<A<int>>> a;");
5359   verifyGoogleFormat("A<A<A<A<int>>>> a;");
5360   verifyGoogleFormat("A<A<int> > a;");
5361   verifyGoogleFormat("A<A<A<int> > > a;");
5362   verifyGoogleFormat("A<A<A<A<int> > > > a;");
5363   verifyGoogleFormat("A<::A<int>> a;");
5364   verifyGoogleFormat("A<::A> a;");
5365   verifyGoogleFormat("A< ::A> a;");
5366   verifyGoogleFormat("A< ::A<int> > a;");
5367   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
5368   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
5369   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
5370   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
5371   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
5372             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
5373 
5374   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
5375 
5376   verifyFormat("test >> a >> b;");
5377   verifyFormat("test << a >> b;");
5378 
5379   verifyFormat("f<int>();");
5380   verifyFormat("template <typename T> void f() {}");
5381   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
5382   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
5383                "sizeof(char)>::type>;");
5384   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
5385 
5386   // Not template parameters.
5387   verifyFormat("return a < b && c > d;");
5388   verifyFormat("void f() {\n"
5389                "  while (a < b && c > d) {\n"
5390                "  }\n"
5391                "}");
5392   verifyFormat("template <typename... Types>\n"
5393                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
5394 
5395   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5396                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
5397                getLLVMStyleWithColumns(60));
5398   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
5399   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
5400   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
5401 }
5402 
5403 TEST_F(FormatTest, UnderstandsBinaryOperators) {
5404   verifyFormat("COMPARE(a, ==, b);");
5405 }
5406 
5407 TEST_F(FormatTest, UnderstandsPointersToMembers) {
5408   verifyFormat("int A::*x;");
5409   verifyFormat("int (S::*func)(void *);");
5410   verifyFormat("void f() { int (S::*func)(void *); }");
5411   verifyFormat("typedef bool *(Class::*Member)() const;");
5412   verifyFormat("void f() {\n"
5413                "  (a->*f)();\n"
5414                "  a->*x;\n"
5415                "  (a.*f)();\n"
5416                "  ((*a).*f)();\n"
5417                "  a.*x;\n"
5418                "}");
5419   verifyFormat("void f() {\n"
5420                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5421                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
5422                "}");
5423   verifyFormat(
5424       "(aaaaaaaaaa->*bbbbbbb)(\n"
5425       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5426   FormatStyle Style = getLLVMStyle();
5427   Style.PointerAlignment = FormatStyle::PAS_Left;
5428   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
5429 }
5430 
5431 TEST_F(FormatTest, UnderstandsUnaryOperators) {
5432   verifyFormat("int a = -2;");
5433   verifyFormat("f(-1, -2, -3);");
5434   verifyFormat("a[-1] = 5;");
5435   verifyFormat("int a = 5 + -2;");
5436   verifyFormat("if (i == -1) {\n}");
5437   verifyFormat("if (i != -1) {\n}");
5438   verifyFormat("if (i > -1) {\n}");
5439   verifyFormat("if (i < -1) {\n}");
5440   verifyFormat("++(a->f());");
5441   verifyFormat("--(a->f());");
5442   verifyFormat("(a->f())++;");
5443   verifyFormat("a[42]++;");
5444   verifyFormat("if (!(a->f())) {\n}");
5445 
5446   verifyFormat("a-- > b;");
5447   verifyFormat("b ? -a : c;");
5448   verifyFormat("n * sizeof char16;");
5449   verifyFormat("n * alignof char16;", getGoogleStyle());
5450   verifyFormat("sizeof(char);");
5451   verifyFormat("alignof(char);", getGoogleStyle());
5452 
5453   verifyFormat("return -1;");
5454   verifyFormat("switch (a) {\n"
5455                "case -1:\n"
5456                "  break;\n"
5457                "}");
5458   verifyFormat("#define X -1");
5459   verifyFormat("#define X -kConstant");
5460 
5461   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
5462   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
5463 
5464   verifyFormat("int a = /* confusing comment */ -1;");
5465   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
5466   verifyFormat("int a = i /* confusing comment */++;");
5467 }
5468 
5469 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
5470   verifyFormat("if (!aaaaaaaaaa( // break\n"
5471                "        aaaaa)) {\n"
5472                "}");
5473   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
5474                "    aaaaa));");
5475   verifyFormat("*aaa = aaaaaaa( // break\n"
5476                "    bbbbbb);");
5477 }
5478 
5479 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
5480   verifyFormat("bool operator<();");
5481   verifyFormat("bool operator>();");
5482   verifyFormat("bool operator=();");
5483   verifyFormat("bool operator==();");
5484   verifyFormat("bool operator!=();");
5485   verifyFormat("int operator+();");
5486   verifyFormat("int operator++();");
5487   verifyFormat("bool operator,();");
5488   verifyFormat("bool operator();");
5489   verifyFormat("bool operator()();");
5490   verifyFormat("bool operator[]();");
5491   verifyFormat("operator bool();");
5492   verifyFormat("operator int();");
5493   verifyFormat("operator void *();");
5494   verifyFormat("operator SomeType<int>();");
5495   verifyFormat("operator SomeType<int, int>();");
5496   verifyFormat("operator SomeType<SomeType<int>>();");
5497   verifyFormat("void *operator new(std::size_t size);");
5498   verifyFormat("void *operator new[](std::size_t size);");
5499   verifyFormat("void operator delete(void *ptr);");
5500   verifyFormat("void operator delete[](void *ptr);");
5501   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
5502                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
5503   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
5504                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
5505 
5506   verifyFormat(
5507       "ostream &operator<<(ostream &OutputStream,\n"
5508       "                    SomeReallyLongType WithSomeReallyLongValue);");
5509   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
5510                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
5511                "  return left.group < right.group;\n"
5512                "}");
5513   verifyFormat("SomeType &operator=(const SomeType &S);");
5514   verifyFormat("f.template operator()<int>();");
5515 
5516   verifyGoogleFormat("operator void*();");
5517   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
5518   verifyGoogleFormat("operator ::A();");
5519 
5520   verifyFormat("using A::operator+;");
5521   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
5522                "int i;");
5523 }
5524 
5525 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
5526   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
5527   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
5528   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
5529   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
5530   verifyFormat("Deleted &operator=(const Deleted &) &;");
5531   verifyFormat("Deleted &operator=(const Deleted &) &&;");
5532   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
5533   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
5534   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
5535   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
5536   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
5537 
5538   FormatStyle AlignLeft = getLLVMStyle();
5539   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
5540   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
5541   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
5542                AlignLeft);
5543   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
5544   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
5545 
5546   FormatStyle Spaces = getLLVMStyle();
5547   Spaces.SpacesInCStyleCastParentheses = true;
5548   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
5549   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
5550   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
5551   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
5552 
5553   Spaces.SpacesInCStyleCastParentheses = false;
5554   Spaces.SpacesInParentheses = true;
5555   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
5556   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
5557   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
5558   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
5559 }
5560 
5561 TEST_F(FormatTest, UnderstandsNewAndDelete) {
5562   verifyFormat("void f() {\n"
5563                "  A *a = new A;\n"
5564                "  A *a = new (placement) A;\n"
5565                "  delete a;\n"
5566                "  delete (A *)a;\n"
5567                "}");
5568   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5569                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5570   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5571                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5572                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5573   verifyFormat("delete[] h->p;");
5574 }
5575 
5576 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
5577   verifyFormat("int *f(int *a) {}");
5578   verifyFormat("int main(int argc, char **argv) {}");
5579   verifyFormat("Test::Test(int b) : a(b * b) {}");
5580   verifyIndependentOfContext("f(a, *a);");
5581   verifyFormat("void g() { f(*a); }");
5582   verifyIndependentOfContext("int a = b * 10;");
5583   verifyIndependentOfContext("int a = 10 * b;");
5584   verifyIndependentOfContext("int a = b * c;");
5585   verifyIndependentOfContext("int a += b * c;");
5586   verifyIndependentOfContext("int a -= b * c;");
5587   verifyIndependentOfContext("int a *= b * c;");
5588   verifyIndependentOfContext("int a /= b * c;");
5589   verifyIndependentOfContext("int a = *b;");
5590   verifyIndependentOfContext("int a = *b * c;");
5591   verifyIndependentOfContext("int a = b * *c;");
5592   verifyIndependentOfContext("int a = b * (10);");
5593   verifyIndependentOfContext("S << b * (10);");
5594   verifyIndependentOfContext("return 10 * b;");
5595   verifyIndependentOfContext("return *b * *c;");
5596   verifyIndependentOfContext("return a & ~b;");
5597   verifyIndependentOfContext("f(b ? *c : *d);");
5598   verifyIndependentOfContext("int a = b ? *c : *d;");
5599   verifyIndependentOfContext("*b = a;");
5600   verifyIndependentOfContext("a * ~b;");
5601   verifyIndependentOfContext("a * !b;");
5602   verifyIndependentOfContext("a * +b;");
5603   verifyIndependentOfContext("a * -b;");
5604   verifyIndependentOfContext("a * ++b;");
5605   verifyIndependentOfContext("a * --b;");
5606   verifyIndependentOfContext("a[4] * b;");
5607   verifyIndependentOfContext("a[a * a] = 1;");
5608   verifyIndependentOfContext("f() * b;");
5609   verifyIndependentOfContext("a * [self dostuff];");
5610   verifyIndependentOfContext("int x = a * (a + b);");
5611   verifyIndependentOfContext("(a *)(a + b);");
5612   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
5613   verifyIndependentOfContext("int *pa = (int *)&a;");
5614   verifyIndependentOfContext("return sizeof(int **);");
5615   verifyIndependentOfContext("return sizeof(int ******);");
5616   verifyIndependentOfContext("return (int **&)a;");
5617   verifyIndependentOfContext("f((*PointerToArray)[10]);");
5618   verifyFormat("void f(Type (*parameter)[10]) {}");
5619   verifyFormat("void f(Type (&parameter)[10]) {}");
5620   verifyGoogleFormat("return sizeof(int**);");
5621   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
5622   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
5623   verifyFormat("auto a = [](int **&, int ***) {};");
5624   verifyFormat("auto PointerBinding = [](const char *S) {};");
5625   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
5626   verifyFormat("[](const decltype(*a) &value) {}");
5627   verifyFormat("decltype(a * b) F();");
5628   verifyFormat("#define MACRO() [](A *a) { return 1; }");
5629   verifyIndependentOfContext("typedef void (*f)(int *a);");
5630   verifyIndependentOfContext("int i{a * b};");
5631   verifyIndependentOfContext("aaa && aaa->f();");
5632   verifyIndependentOfContext("int x = ~*p;");
5633   verifyFormat("Constructor() : a(a), area(width * height) {}");
5634   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
5635   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
5636   verifyFormat("void f() { f(a, c * d); }");
5637   verifyFormat("void f() { f(new a(), c * d); }");
5638 
5639   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
5640 
5641   verifyIndependentOfContext("A<int *> a;");
5642   verifyIndependentOfContext("A<int **> a;");
5643   verifyIndependentOfContext("A<int *, int *> a;");
5644   verifyIndependentOfContext("A<int *[]> a;");
5645   verifyIndependentOfContext(
5646       "const char *const p = reinterpret_cast<const char *const>(q);");
5647   verifyIndependentOfContext("A<int **, int **> a;");
5648   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
5649   verifyFormat("for (char **a = b; *a; ++a) {\n}");
5650   verifyFormat("for (; a && b;) {\n}");
5651   verifyFormat("bool foo = true && [] { return false; }();");
5652 
5653   verifyFormat(
5654       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5655       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5656 
5657   verifyGoogleFormat("**outparam = 1;");
5658   verifyGoogleFormat("*outparam = a * b;");
5659   verifyGoogleFormat("int main(int argc, char** argv) {}");
5660   verifyGoogleFormat("A<int*> a;");
5661   verifyGoogleFormat("A<int**> a;");
5662   verifyGoogleFormat("A<int*, int*> a;");
5663   verifyGoogleFormat("A<int**, int**> a;");
5664   verifyGoogleFormat("f(b ? *c : *d);");
5665   verifyGoogleFormat("int a = b ? *c : *d;");
5666   verifyGoogleFormat("Type* t = **x;");
5667   verifyGoogleFormat("Type* t = *++*x;");
5668   verifyGoogleFormat("*++*x;");
5669   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
5670   verifyGoogleFormat("Type* t = x++ * y;");
5671   verifyGoogleFormat(
5672       "const char* const p = reinterpret_cast<const char* const>(q);");
5673   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
5674   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
5675   verifyGoogleFormat("template <typename T>\n"
5676                      "void f(int i = 0, SomeType** temps = NULL);");
5677 
5678   FormatStyle Left = getLLVMStyle();
5679   Left.PointerAlignment = FormatStyle::PAS_Left;
5680   verifyFormat("x = *a(x) = *a(y);", Left);
5681   verifyFormat("for (;; * = b) {\n}", Left);
5682   verifyFormat("return *this += 1;", Left);
5683 
5684   verifyIndependentOfContext("a = *(x + y);");
5685   verifyIndependentOfContext("a = &(x + y);");
5686   verifyIndependentOfContext("*(x + y).call();");
5687   verifyIndependentOfContext("&(x + y)->call();");
5688   verifyFormat("void f() { &(*I).first; }");
5689 
5690   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
5691   verifyFormat(
5692       "int *MyValues = {\n"
5693       "    *A, // Operator detection might be confused by the '{'\n"
5694       "    *BB // Operator detection might be confused by previous comment\n"
5695       "};");
5696 
5697   verifyIndependentOfContext("if (int *a = &b)");
5698   verifyIndependentOfContext("if (int &a = *b)");
5699   verifyIndependentOfContext("if (a & b[i])");
5700   verifyIndependentOfContext("if (a::b::c::d & b[i])");
5701   verifyIndependentOfContext("if (*b[i])");
5702   verifyIndependentOfContext("if (int *a = (&b))");
5703   verifyIndependentOfContext("while (int *a = &b)");
5704   verifyIndependentOfContext("size = sizeof *a;");
5705   verifyIndependentOfContext("if (a && (b = c))");
5706   verifyFormat("void f() {\n"
5707                "  for (const int &v : Values) {\n"
5708                "  }\n"
5709                "}");
5710   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
5711   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
5712   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
5713 
5714   verifyFormat("#define A (!a * b)");
5715   verifyFormat("#define MACRO     \\\n"
5716                "  int *i = a * b; \\\n"
5717                "  void f(a *b);",
5718                getLLVMStyleWithColumns(19));
5719 
5720   verifyIndependentOfContext("A = new SomeType *[Length];");
5721   verifyIndependentOfContext("A = new SomeType *[Length]();");
5722   verifyIndependentOfContext("T **t = new T *;");
5723   verifyIndependentOfContext("T **t = new T *();");
5724   verifyGoogleFormat("A = new SomeType*[Length]();");
5725   verifyGoogleFormat("A = new SomeType*[Length];");
5726   verifyGoogleFormat("T** t = new T*;");
5727   verifyGoogleFormat("T** t = new T*();");
5728 
5729   FormatStyle PointerLeft = getLLVMStyle();
5730   PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
5731   verifyFormat("delete *x;", PointerLeft);
5732   verifyFormat("STATIC_ASSERT((a & b) == 0);");
5733   verifyFormat("STATIC_ASSERT(0 == (a & b));");
5734   verifyFormat("template <bool a, bool b> "
5735                "typename t::if<x && y>::type f() {}");
5736   verifyFormat("template <int *y> f() {}");
5737   verifyFormat("vector<int *> v;");
5738   verifyFormat("vector<int *const> v;");
5739   verifyFormat("vector<int *const **const *> v;");
5740   verifyFormat("vector<int *volatile> v;");
5741   verifyFormat("vector<a * b> v;");
5742   verifyFormat("foo<b && false>();");
5743   verifyFormat("foo<b & 1>();");
5744   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
5745   verifyFormat(
5746       "template <class T, class = typename std::enable_if<\n"
5747       "                       std::is_integral<T>::value &&\n"
5748       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
5749       "void F();",
5750       getLLVMStyleWithColumns(76));
5751   verifyFormat(
5752       "template <class T,\n"
5753       "          class = typename ::std::enable_if<\n"
5754       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
5755       "void F();",
5756       getGoogleStyleWithColumns(68));
5757 
5758   verifyIndependentOfContext("MACRO(int *i);");
5759   verifyIndependentOfContext("MACRO(auto *a);");
5760   verifyIndependentOfContext("MACRO(const A *a);");
5761   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
5762   // FIXME: Is there a way to make this work?
5763   // verifyIndependentOfContext("MACRO(A *a);");
5764 
5765   verifyFormat("DatumHandle const *operator->() const { return input_; }");
5766   verifyFormat("return options != nullptr && operator==(*options);");
5767 
5768   EXPECT_EQ("#define OP(x)                                    \\\n"
5769             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
5770             "    return s << a.DebugString();                 \\\n"
5771             "  }",
5772             format("#define OP(x) \\\n"
5773                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
5774                    "    return s << a.DebugString(); \\\n"
5775                    "  }",
5776                    getLLVMStyleWithColumns(50)));
5777 
5778   // FIXME: We cannot handle this case yet; we might be able to figure out that
5779   // foo<x> d > v; doesn't make sense.
5780   verifyFormat("foo<a<b && c> d> v;");
5781 
5782   FormatStyle PointerMiddle = getLLVMStyle();
5783   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
5784   verifyFormat("delete *x;", PointerMiddle);
5785   verifyFormat("int * x;", PointerMiddle);
5786   verifyFormat("template <int * y> f() {}", PointerMiddle);
5787   verifyFormat("int * f(int * a) {}", PointerMiddle);
5788   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
5789   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
5790   verifyFormat("A<int *> a;", PointerMiddle);
5791   verifyFormat("A<int **> a;", PointerMiddle);
5792   verifyFormat("A<int *, int *> a;", PointerMiddle);
5793   verifyFormat("A<int * []> a;", PointerMiddle);
5794   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
5795   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
5796   verifyFormat("T ** t = new T *;", PointerMiddle);
5797 
5798   // Member function reference qualifiers aren't binary operators.
5799   verifyFormat("string // break\n"
5800                "operator()() & {}");
5801   verifyFormat("string // break\n"
5802                "operator()() && {}");
5803   verifyGoogleFormat("template <typename T>\n"
5804                      "auto x() & -> int {}");
5805 }
5806 
5807 TEST_F(FormatTest, UnderstandsAttributes) {
5808   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
5809   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
5810                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
5811   FormatStyle AfterType = getLLVMStyle();
5812   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
5813   verifyFormat("__attribute__((nodebug)) void\n"
5814                "foo() {}\n",
5815                AfterType);
5816 }
5817 
5818 TEST_F(FormatTest, UnderstandsEllipsis) {
5819   verifyFormat("int printf(const char *fmt, ...);");
5820   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
5821   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
5822 
5823   FormatStyle PointersLeft = getLLVMStyle();
5824   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
5825   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
5826 }
5827 
5828 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
5829   EXPECT_EQ("int *a;\n"
5830             "int *a;\n"
5831             "int *a;",
5832             format("int *a;\n"
5833                    "int* a;\n"
5834                    "int *a;",
5835                    getGoogleStyle()));
5836   EXPECT_EQ("int* a;\n"
5837             "int* a;\n"
5838             "int* a;",
5839             format("int* a;\n"
5840                    "int* a;\n"
5841                    "int *a;",
5842                    getGoogleStyle()));
5843   EXPECT_EQ("int *a;\n"
5844             "int *a;\n"
5845             "int *a;",
5846             format("int *a;\n"
5847                    "int * a;\n"
5848                    "int *  a;",
5849                    getGoogleStyle()));
5850   EXPECT_EQ("auto x = [] {\n"
5851             "  int *a;\n"
5852             "  int *a;\n"
5853             "  int *a;\n"
5854             "};",
5855             format("auto x=[]{int *a;\n"
5856                    "int * a;\n"
5857                    "int *  a;};",
5858                    getGoogleStyle()));
5859 }
5860 
5861 TEST_F(FormatTest, UnderstandsRvalueReferences) {
5862   verifyFormat("int f(int &&a) {}");
5863   verifyFormat("int f(int a, char &&b) {}");
5864   verifyFormat("void f() { int &&a = b; }");
5865   verifyGoogleFormat("int f(int a, char&& b) {}");
5866   verifyGoogleFormat("void f() { int&& a = b; }");
5867 
5868   verifyIndependentOfContext("A<int &&> a;");
5869   verifyIndependentOfContext("A<int &&, int &&> a;");
5870   verifyGoogleFormat("A<int&&> a;");
5871   verifyGoogleFormat("A<int&&, int&&> a;");
5872 
5873   // Not rvalue references:
5874   verifyFormat("template <bool B, bool C> class A {\n"
5875                "  static_assert(B && C, \"Something is wrong\");\n"
5876                "};");
5877   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
5878   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
5879   verifyFormat("#define A(a, b) (a && b)");
5880 }
5881 
5882 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
5883   verifyFormat("void f() {\n"
5884                "  x[aaaaaaaaa -\n"
5885                "    b] = 23;\n"
5886                "}",
5887                getLLVMStyleWithColumns(15));
5888 }
5889 
5890 TEST_F(FormatTest, FormatsCasts) {
5891   verifyFormat("Type *A = static_cast<Type *>(P);");
5892   verifyFormat("Type *A = (Type *)P;");
5893   verifyFormat("Type *A = (vector<Type *, int *>)P;");
5894   verifyFormat("int a = (int)(2.0f);");
5895   verifyFormat("int a = (int)2.0f;");
5896   verifyFormat("x[(int32)y];");
5897   verifyFormat("x = (int32)y;");
5898   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
5899   verifyFormat("int a = (int)*b;");
5900   verifyFormat("int a = (int)2.0f;");
5901   verifyFormat("int a = (int)~0;");
5902   verifyFormat("int a = (int)++a;");
5903   verifyFormat("int a = (int)sizeof(int);");
5904   verifyFormat("int a = (int)+2;");
5905   verifyFormat("my_int a = (my_int)2.0f;");
5906   verifyFormat("my_int a = (my_int)sizeof(int);");
5907   verifyFormat("return (my_int)aaa;");
5908   verifyFormat("#define x ((int)-1)");
5909   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
5910   verifyFormat("#define p(q) ((int *)&q)");
5911   verifyFormat("fn(a)(b) + 1;");
5912 
5913   verifyFormat("void f() { my_int a = (my_int)*b; }");
5914   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
5915   verifyFormat("my_int a = (my_int)~0;");
5916   verifyFormat("my_int a = (my_int)++a;");
5917   verifyFormat("my_int a = (my_int)-2;");
5918   verifyFormat("my_int a = (my_int)1;");
5919   verifyFormat("my_int a = (my_int *)1;");
5920   verifyFormat("my_int a = (const my_int)-1;");
5921   verifyFormat("my_int a = (const my_int *)-1;");
5922   verifyFormat("my_int a = (my_int)(my_int)-1;");
5923   verifyFormat("my_int a = (ns::my_int)-2;");
5924   verifyFormat("case (my_int)ONE:");
5925 
5926   // FIXME: single value wrapped with paren will be treated as cast.
5927   verifyFormat("void f(int i = (kValue)*kMask) {}");
5928 
5929   verifyFormat("{ (void)F; }");
5930 
5931   // Don't break after a cast's
5932   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5933                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
5934                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
5935 
5936   // These are not casts.
5937   verifyFormat("void f(int *) {}");
5938   verifyFormat("f(foo)->b;");
5939   verifyFormat("f(foo).b;");
5940   verifyFormat("f(foo)(b);");
5941   verifyFormat("f(foo)[b];");
5942   verifyFormat("[](foo) { return 4; }(bar);");
5943   verifyFormat("(*funptr)(foo)[4];");
5944   verifyFormat("funptrs[4](foo)[4];");
5945   verifyFormat("void f(int *);");
5946   verifyFormat("void f(int *) = 0;");
5947   verifyFormat("void f(SmallVector<int>) {}");
5948   verifyFormat("void f(SmallVector<int>);");
5949   verifyFormat("void f(SmallVector<int>) = 0;");
5950   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
5951   verifyFormat("int a = sizeof(int) * b;");
5952   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
5953   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
5954   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
5955   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
5956 
5957   // These are not casts, but at some point were confused with casts.
5958   verifyFormat("virtual void foo(int *) override;");
5959   verifyFormat("virtual void foo(char &) const;");
5960   verifyFormat("virtual void foo(int *a, char *) const;");
5961   verifyFormat("int a = sizeof(int *) + b;");
5962   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
5963   verifyFormat("bool b = f(g<int>) && c;");
5964   verifyFormat("typedef void (*f)(int i) func;");
5965 
5966   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
5967                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5968   // FIXME: The indentation here is not ideal.
5969   verifyFormat(
5970       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5971       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
5972       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
5973 }
5974 
5975 TEST_F(FormatTest, FormatsFunctionTypes) {
5976   verifyFormat("A<bool()> a;");
5977   verifyFormat("A<SomeType()> a;");
5978   verifyFormat("A<void (*)(int, std::string)> a;");
5979   verifyFormat("A<void *(int)>;");
5980   verifyFormat("void *(*a)(int *, SomeType *);");
5981   verifyFormat("int (*func)(void *);");
5982   verifyFormat("void f() { int (*func)(void *); }");
5983   verifyFormat("template <class CallbackClass>\n"
5984                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
5985 
5986   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
5987   verifyGoogleFormat("void* (*a)(int);");
5988   verifyGoogleFormat(
5989       "template <class CallbackClass>\n"
5990       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
5991 
5992   // Other constructs can look somewhat like function types:
5993   verifyFormat("A<sizeof(*x)> a;");
5994   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
5995   verifyFormat("some_var = function(*some_pointer_var)[0];");
5996   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
5997   verifyFormat("int x = f(&h)();");
5998 }
5999 
6000 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
6001   verifyFormat("A (*foo_)[6];");
6002   verifyFormat("vector<int> (*foo_)[6];");
6003 }
6004 
6005 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
6006   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6007                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6008   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
6009                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6010   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6011                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
6012 
6013   // Different ways of ()-initializiation.
6014   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6015                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
6016   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6017                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
6018   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6019                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
6020   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6021                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
6022 }
6023 
6024 TEST_F(FormatTest, BreaksLongDeclarations) {
6025   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
6026                "    AnotherNameForTheLongType;");
6027   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
6028                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6029   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6030                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6031   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
6032                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6033   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6034                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6035   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
6036                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6037   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6038                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6039   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6040                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6041   FormatStyle Indented = getLLVMStyle();
6042   Indented.IndentWrappedFunctionNames = true;
6043   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6044                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
6045                Indented);
6046   verifyFormat(
6047       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6048       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6049       Indented);
6050   verifyFormat(
6051       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6052       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6053       Indented);
6054   verifyFormat(
6055       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6056       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6057       Indented);
6058 
6059   // FIXME: Without the comment, this breaks after "(".
6060   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
6061                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
6062                getGoogleStyle());
6063 
6064   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
6065                "                  int LoooooooooooooooooooongParam2) {}");
6066   verifyFormat(
6067       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
6068       "                                   SourceLocation L, IdentifierIn *II,\n"
6069       "                                   Type *T) {}");
6070   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
6071                "ReallyReaaallyLongFunctionName(\n"
6072                "    const std::string &SomeParameter,\n"
6073                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6074                "        &ReallyReallyLongParameterName,\n"
6075                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6076                "        &AnotherLongParameterName) {}");
6077   verifyFormat("template <typename A>\n"
6078                "SomeLoooooooooooooooooooooongType<\n"
6079                "    typename some_namespace::SomeOtherType<A>::Type>\n"
6080                "Function() {}");
6081 
6082   verifyGoogleFormat(
6083       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
6084       "    aaaaaaaaaaaaaaaaaaaaaaa;");
6085   verifyGoogleFormat(
6086       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
6087       "                                   SourceLocation L) {}");
6088   verifyGoogleFormat(
6089       "some_namespace::LongReturnType\n"
6090       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
6091       "    int first_long_parameter, int second_parameter) {}");
6092 
6093   verifyGoogleFormat("template <typename T>\n"
6094                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6095                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
6096   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6097                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
6098 
6099   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
6100                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6101                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6102   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6103                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6104                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
6105   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6106                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
6107                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
6108                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6109 }
6110 
6111 TEST_F(FormatTest, FormatsArrays) {
6112   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6113                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
6114   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
6115                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
6116   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
6117                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
6118   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6119                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6120   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6121                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
6122   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6123                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6124                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6125   verifyFormat(
6126       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
6127       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6128       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
6129 
6130   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
6131                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
6132   verifyFormat(
6133       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
6134       "                                  .aaaaaaa[0]\n"
6135       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
6136 
6137   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
6138 }
6139 
6140 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
6141   verifyFormat("(a)->b();");
6142   verifyFormat("--a;");
6143 }
6144 
6145 TEST_F(FormatTest, HandlesIncludeDirectives) {
6146   verifyFormat("#include <string>\n"
6147                "#include <a/b/c.h>\n"
6148                "#include \"a/b/string\"\n"
6149                "#include \"string.h\"\n"
6150                "#include \"string.h\"\n"
6151                "#include <a-a>\n"
6152                "#include < path with space >\n"
6153                "#include_next <test.h>"
6154                "#include \"abc.h\" // this is included for ABC\n"
6155                "#include \"some long include\" // with a comment\n"
6156                "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
6157                getLLVMStyleWithColumns(35));
6158   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
6159   EXPECT_EQ("#include <a>", format("#include<a>"));
6160 
6161   verifyFormat("#import <string>");
6162   verifyFormat("#import <a/b/c.h>");
6163   verifyFormat("#import \"a/b/string\"");
6164   verifyFormat("#import \"string.h\"");
6165   verifyFormat("#import \"string.h\"");
6166   verifyFormat("#if __has_include(<strstream>)\n"
6167                "#include <strstream>\n"
6168                "#endif");
6169 
6170   verifyFormat("#define MY_IMPORT <a/b>");
6171 
6172   // Protocol buffer definition or missing "#".
6173   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
6174                getLLVMStyleWithColumns(30));
6175 
6176   FormatStyle Style = getLLVMStyle();
6177   Style.AlwaysBreakBeforeMultilineStrings = true;
6178   Style.ColumnLimit = 0;
6179   verifyFormat("#import \"abc.h\"", Style);
6180 
6181   // But 'import' might also be a regular C++ namespace.
6182   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6183                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6184 }
6185 
6186 //===----------------------------------------------------------------------===//
6187 // Error recovery tests.
6188 //===----------------------------------------------------------------------===//
6189 
6190 TEST_F(FormatTest, IncompleteParameterLists) {
6191   FormatStyle NoBinPacking = getLLVMStyle();
6192   NoBinPacking.BinPackParameters = false;
6193   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
6194                "                        double *min_x,\n"
6195                "                        double *max_x,\n"
6196                "                        double *min_y,\n"
6197                "                        double *max_y,\n"
6198                "                        double *min_z,\n"
6199                "                        double *max_z, ) {}",
6200                NoBinPacking);
6201 }
6202 
6203 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
6204   verifyFormat("void f() { return; }\n42");
6205   verifyFormat("void f() {\n"
6206                "  if (0)\n"
6207                "    return;\n"
6208                "}\n"
6209                "42");
6210   verifyFormat("void f() { return }\n42");
6211   verifyFormat("void f() {\n"
6212                "  if (0)\n"
6213                "    return\n"
6214                "}\n"
6215                "42");
6216 }
6217 
6218 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
6219   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
6220   EXPECT_EQ("void f() {\n"
6221             "  if (a)\n"
6222             "    return\n"
6223             "}",
6224             format("void  f  (  )  {  if  ( a )  return  }"));
6225   EXPECT_EQ("namespace N {\n"
6226             "void f()\n"
6227             "}",
6228             format("namespace  N  {  void f()  }"));
6229   EXPECT_EQ("namespace N {\n"
6230             "void f() {}\n"
6231             "void g()\n"
6232             "}",
6233             format("namespace N  { void f( ) { } void g( ) }"));
6234 }
6235 
6236 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
6237   verifyFormat("int aaaaaaaa =\n"
6238                "    // Overlylongcomment\n"
6239                "    b;",
6240                getLLVMStyleWithColumns(20));
6241   verifyFormat("function(\n"
6242                "    ShortArgument,\n"
6243                "    LoooooooooooongArgument);\n",
6244                getLLVMStyleWithColumns(20));
6245 }
6246 
6247 TEST_F(FormatTest, IncorrectAccessSpecifier) {
6248   verifyFormat("public:");
6249   verifyFormat("class A {\n"
6250                "public\n"
6251                "  void f() {}\n"
6252                "};");
6253   verifyFormat("public\n"
6254                "int qwerty;");
6255   verifyFormat("public\n"
6256                "B {}");
6257   verifyFormat("public\n"
6258                "{}");
6259   verifyFormat("public\n"
6260                "B { int x; }");
6261 }
6262 
6263 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
6264   verifyFormat("{");
6265   verifyFormat("#})");
6266   verifyNoCrash("(/**/[:!] ?[).");
6267 }
6268 
6269 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
6270   verifyFormat("do {\n}");
6271   verifyFormat("do {\n}\n"
6272                "f();");
6273   verifyFormat("do {\n}\n"
6274                "wheeee(fun);");
6275   verifyFormat("do {\n"
6276                "  f();\n"
6277                "}");
6278 }
6279 
6280 TEST_F(FormatTest, IncorrectCodeMissingParens) {
6281   verifyFormat("if {\n  foo;\n  foo();\n}");
6282   verifyFormat("switch {\n  foo;\n  foo();\n}");
6283   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
6284   verifyFormat("while {\n  foo;\n  foo();\n}");
6285   verifyFormat("do {\n  foo;\n  foo();\n} while;");
6286 }
6287 
6288 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
6289   verifyIncompleteFormat("namespace {\n"
6290                          "class Foo { Foo (\n"
6291                          "};\n"
6292                          "} // comment");
6293 }
6294 
6295 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
6296   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
6297   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
6298   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
6299   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
6300 
6301   EXPECT_EQ("{\n"
6302             "  {\n"
6303             "    breakme(\n"
6304             "        qwe);\n"
6305             "  }\n",
6306             format("{\n"
6307                    "    {\n"
6308                    " breakme(qwe);\n"
6309                    "}\n",
6310                    getLLVMStyleWithColumns(10)));
6311 }
6312 
6313 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
6314   verifyFormat("int x = {\n"
6315                "    avariable,\n"
6316                "    b(alongervariable)};",
6317                getLLVMStyleWithColumns(25));
6318 }
6319 
6320 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
6321   verifyFormat("return (a)(b){1, 2, 3};");
6322 }
6323 
6324 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
6325   verifyFormat("vector<int> x{1, 2, 3, 4};");
6326   verifyFormat("vector<int> x{\n"
6327                "    1, 2, 3, 4,\n"
6328                "};");
6329   verifyFormat("vector<T> x{{}, {}, {}, {}};");
6330   verifyFormat("f({1, 2});");
6331   verifyFormat("auto v = Foo{-1};");
6332   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
6333   verifyFormat("Class::Class : member{1, 2, 3} {}");
6334   verifyFormat("new vector<int>{1, 2, 3};");
6335   verifyFormat("new int[3]{1, 2, 3};");
6336   verifyFormat("new int{1};");
6337   verifyFormat("return {arg1, arg2};");
6338   verifyFormat("return {arg1, SomeType{parameter}};");
6339   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
6340   verifyFormat("new T{arg1, arg2};");
6341   verifyFormat("f(MyMap[{composite, key}]);");
6342   verifyFormat("class Class {\n"
6343                "  T member = {arg1, arg2};\n"
6344                "};");
6345   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
6346   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
6347   verifyFormat("int a = std::is_integral<int>{} + 0;");
6348 
6349   verifyFormat("int foo(int i) { return fo1{}(i); }");
6350   verifyFormat("int foo(int i) { return fo1{}(i); }");
6351   verifyFormat("auto i = decltype(x){};");
6352   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
6353   verifyFormat("Node n{1, Node{1000}, //\n"
6354                "       2};");
6355   verifyFormat("Aaaa aaaaaaa{\n"
6356                "    {\n"
6357                "        aaaa,\n"
6358                "    },\n"
6359                "};");
6360   verifyFormat("class C : public D {\n"
6361                "  SomeClass SC{2};\n"
6362                "};");
6363   verifyFormat("class C : public A {\n"
6364                "  class D : public B {\n"
6365                "    void f() { int i{2}; }\n"
6366                "  };\n"
6367                "};");
6368   verifyFormat("#define A {a, a},");
6369 
6370   // In combination with BinPackArguments = false.
6371   FormatStyle NoBinPacking = getLLVMStyle();
6372   NoBinPacking.BinPackArguments = false;
6373   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
6374                "                      bbbbb,\n"
6375                "                      ccccc,\n"
6376                "                      ddddd,\n"
6377                "                      eeeee,\n"
6378                "                      ffffff,\n"
6379                "                      ggggg,\n"
6380                "                      hhhhhh,\n"
6381                "                      iiiiii,\n"
6382                "                      jjjjjj,\n"
6383                "                      kkkkkk};",
6384                NoBinPacking);
6385   verifyFormat("const Aaaaaa aaaaa = {\n"
6386                "    aaaaa,\n"
6387                "    bbbbb,\n"
6388                "    ccccc,\n"
6389                "    ddddd,\n"
6390                "    eeeee,\n"
6391                "    ffffff,\n"
6392                "    ggggg,\n"
6393                "    hhhhhh,\n"
6394                "    iiiiii,\n"
6395                "    jjjjjj,\n"
6396                "    kkkkkk,\n"
6397                "};",
6398                NoBinPacking);
6399   verifyFormat(
6400       "const Aaaaaa aaaaa = {\n"
6401       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
6402       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
6403       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
6404       "};",
6405       NoBinPacking);
6406 
6407   // FIXME: The alignment of these trailing comments might be bad. Then again,
6408   // this might be utterly useless in real code.
6409   verifyFormat("Constructor::Constructor()\n"
6410                "    : some_value{         //\n"
6411                "                 aaaaaaa, //\n"
6412                "                 bbbbbbb} {}");
6413 
6414   // In braced lists, the first comment is always assumed to belong to the
6415   // first element. Thus, it can be moved to the next or previous line as
6416   // appropriate.
6417   EXPECT_EQ("function({// First element:\n"
6418             "          1,\n"
6419             "          // Second element:\n"
6420             "          2});",
6421             format("function({\n"
6422                    "    // First element:\n"
6423                    "    1,\n"
6424                    "    // Second element:\n"
6425                    "    2});"));
6426   EXPECT_EQ("std::vector<int> MyNumbers{\n"
6427             "    // First element:\n"
6428             "    1,\n"
6429             "    // Second element:\n"
6430             "    2};",
6431             format("std::vector<int> MyNumbers{// First element:\n"
6432                    "                           1,\n"
6433                    "                           // Second element:\n"
6434                    "                           2};",
6435                    getLLVMStyleWithColumns(30)));
6436   // A trailing comma should still lead to an enforced line break.
6437   EXPECT_EQ("vector<int> SomeVector = {\n"
6438             "    // aaa\n"
6439             "    1, 2,\n"
6440             "};",
6441             format("vector<int> SomeVector = { // aaa\n"
6442                    "    1, 2, };"));
6443 
6444   FormatStyle ExtraSpaces = getLLVMStyle();
6445   ExtraSpaces.Cpp11BracedListStyle = false;
6446   ExtraSpaces.ColumnLimit = 75;
6447   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
6448   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
6449   verifyFormat("f({ 1, 2 });", ExtraSpaces);
6450   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
6451   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
6452   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
6453   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
6454   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
6455   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
6456   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
6457   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
6458   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
6459   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
6460   verifyFormat("class Class {\n"
6461                "  T member = { arg1, arg2 };\n"
6462                "};",
6463                ExtraSpaces);
6464   verifyFormat(
6465       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6466       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
6467       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6468       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
6469       ExtraSpaces);
6470   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
6471   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
6472                ExtraSpaces);
6473   verifyFormat(
6474       "someFunction(OtherParam,\n"
6475       "             BracedList{ // comment 1 (Forcing interesting break)\n"
6476       "                         param1, param2,\n"
6477       "                         // comment 2\n"
6478       "                         param3, param4 });",
6479       ExtraSpaces);
6480   verifyFormat(
6481       "std::this_thread::sleep_for(\n"
6482       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
6483       ExtraSpaces);
6484   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
6485                "    aaaaaaa,\n"
6486                "    aaaaaaaaaa,\n"
6487                "    aaaaa,\n"
6488                "    aaaaaaaaaaaaaaa,\n"
6489                "    aaa,\n"
6490                "    aaaaaaaaaa,\n"
6491                "    a,\n"
6492                "    aaaaaaaaaaaaaaaaaaaaa,\n"
6493                "    aaaaaaaaaaaa,\n"
6494                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
6495                "    aaaaaaa,\n"
6496                "    a};");
6497   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
6498 }
6499 
6500 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
6501   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6502                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6503                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6504                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6505                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6506                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6507   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
6508                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6509                "                 1, 22, 333, 4444, 55555, //\n"
6510                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6511                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6512   verifyFormat(
6513       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6514       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6515       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
6516       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6517       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6518       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6519       "                 7777777};");
6520   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6521                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6522                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6523   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6524                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6525                "    // Separating comment.\n"
6526                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
6527   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6528                "    // Leading comment\n"
6529                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6530                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6531   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6532                "                 1, 1, 1, 1};",
6533                getLLVMStyleWithColumns(39));
6534   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6535                "                 1, 1, 1, 1};",
6536                getLLVMStyleWithColumns(38));
6537   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
6538                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
6539                getLLVMStyleWithColumns(43));
6540   verifyFormat(
6541       "static unsigned SomeValues[10][3] = {\n"
6542       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
6543       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
6544   verifyFormat("static auto fields = new vector<string>{\n"
6545                "    \"aaaaaaaaaaaaa\",\n"
6546                "    \"aaaaaaaaaaaaa\",\n"
6547                "    \"aaaaaaaaaaaa\",\n"
6548                "    \"aaaaaaaaaaaaaa\",\n"
6549                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6550                "    \"aaaaaaaaaaaa\",\n"
6551                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6552                "};");
6553   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
6554   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
6555                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
6556                "                 3, cccccccccccccccccccccc};",
6557                getLLVMStyleWithColumns(60));
6558 
6559   // Trailing commas.
6560   verifyFormat("vector<int> x = {\n"
6561                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
6562                "};",
6563                getLLVMStyleWithColumns(39));
6564   verifyFormat("vector<int> x = {\n"
6565                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
6566                "};",
6567                getLLVMStyleWithColumns(39));
6568   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6569                "                 1, 1, 1, 1,\n"
6570                "                 /**/ /**/};",
6571                getLLVMStyleWithColumns(39));
6572 
6573   // Trailing comment in the first line.
6574   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
6575                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
6576                "    111111111,  222222222,  3333333333,  444444444,  //\n"
6577                "    11111111,   22222222,   333333333,   44444444};");
6578   // Trailing comment in the last line.
6579   verifyFormat("int aaaaa[] = {\n"
6580                "    1, 2, 3, // comment\n"
6581                "    4, 5, 6  // comment\n"
6582                "};");
6583 
6584   // With nested lists, we should either format one item per line or all nested
6585   // lists one on line.
6586   // FIXME: For some nested lists, we can do better.
6587   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
6588                "        {aaaaaaaaaaaaaaaaaaa},\n"
6589                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
6590                "        {aaaaaaaaaaaaaaaaa}};",
6591                getLLVMStyleWithColumns(60));
6592   verifyFormat(
6593       "SomeStruct my_struct_array = {\n"
6594       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
6595       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
6596       "    {aaa, aaa},\n"
6597       "    {aaa, aaa},\n"
6598       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
6599       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6600       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
6601 
6602   // No column layout should be used here.
6603   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
6604                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
6605 
6606   verifyNoCrash("a<,");
6607 
6608   // No braced initializer here.
6609   verifyFormat("void f() {\n"
6610                "  struct Dummy {};\n"
6611                "  f(v);\n"
6612                "}");
6613 
6614   // Long lists should be formatted in columns even if they are nested.
6615   verifyFormat(
6616       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6617       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6618       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6619       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6620       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6621       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
6622 }
6623 
6624 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
6625   FormatStyle DoNotMerge = getLLVMStyle();
6626   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6627 
6628   verifyFormat("void f() { return 42; }");
6629   verifyFormat("void f() {\n"
6630                "  return 42;\n"
6631                "}",
6632                DoNotMerge);
6633   verifyFormat("void f() {\n"
6634                "  // Comment\n"
6635                "}");
6636   verifyFormat("{\n"
6637                "#error {\n"
6638                "  int a;\n"
6639                "}");
6640   verifyFormat("{\n"
6641                "  int a;\n"
6642                "#error {\n"
6643                "}");
6644   verifyFormat("void f() {} // comment");
6645   verifyFormat("void f() { int a; } // comment");
6646   verifyFormat("void f() {\n"
6647                "} // comment",
6648                DoNotMerge);
6649   verifyFormat("void f() {\n"
6650                "  int a;\n"
6651                "} // comment",
6652                DoNotMerge);
6653   verifyFormat("void f() {\n"
6654                "} // comment",
6655                getLLVMStyleWithColumns(15));
6656 
6657   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
6658   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
6659 
6660   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
6661   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
6662   verifyFormat("class C {\n"
6663                "  C()\n"
6664                "      : iiiiiiii(nullptr),\n"
6665                "        kkkkkkk(nullptr),\n"
6666                "        mmmmmmm(nullptr),\n"
6667                "        nnnnnnn(nullptr) {}\n"
6668                "};",
6669                getGoogleStyle());
6670 
6671   FormatStyle NoColumnLimit = getLLVMStyle();
6672   NoColumnLimit.ColumnLimit = 0;
6673   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
6674   EXPECT_EQ("class C {\n"
6675             "  A() : b(0) {}\n"
6676             "};",
6677             format("class C{A():b(0){}};", NoColumnLimit));
6678   EXPECT_EQ("A()\n"
6679             "    : b(0) {\n"
6680             "}",
6681             format("A()\n:b(0)\n{\n}", NoColumnLimit));
6682 
6683   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
6684   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
6685       FormatStyle::SFS_None;
6686   EXPECT_EQ("A()\n"
6687             "    : b(0) {\n"
6688             "}",
6689             format("A():b(0){}", DoNotMergeNoColumnLimit));
6690   EXPECT_EQ("A()\n"
6691             "    : b(0) {\n"
6692             "}",
6693             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
6694 
6695   verifyFormat("#define A          \\\n"
6696                "  void f() {       \\\n"
6697                "    int i;         \\\n"
6698                "  }",
6699                getLLVMStyleWithColumns(20));
6700   verifyFormat("#define A           \\\n"
6701                "  void f() { int i; }",
6702                getLLVMStyleWithColumns(21));
6703   verifyFormat("#define A            \\\n"
6704                "  void f() {         \\\n"
6705                "    int i;           \\\n"
6706                "  }                  \\\n"
6707                "  int j;",
6708                getLLVMStyleWithColumns(22));
6709   verifyFormat("#define A             \\\n"
6710                "  void f() { int i; } \\\n"
6711                "  int j;",
6712                getLLVMStyleWithColumns(23));
6713 }
6714 
6715 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
6716   FormatStyle MergeInlineOnly = getLLVMStyle();
6717   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
6718   verifyFormat("class C {\n"
6719                "  int f() { return 42; }\n"
6720                "};",
6721                MergeInlineOnly);
6722   verifyFormat("int f() {\n"
6723                "  return 42;\n"
6724                "}",
6725                MergeInlineOnly);
6726 }
6727 
6728 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
6729   // Elaborate type variable declarations.
6730   verifyFormat("struct foo a = {bar};\nint n;");
6731   verifyFormat("class foo a = {bar};\nint n;");
6732   verifyFormat("union foo a = {bar};\nint n;");
6733 
6734   // Elaborate types inside function definitions.
6735   verifyFormat("struct foo f() {}\nint n;");
6736   verifyFormat("class foo f() {}\nint n;");
6737   verifyFormat("union foo f() {}\nint n;");
6738 
6739   // Templates.
6740   verifyFormat("template <class X> void f() {}\nint n;");
6741   verifyFormat("template <struct X> void f() {}\nint n;");
6742   verifyFormat("template <union X> void f() {}\nint n;");
6743 
6744   // Actual definitions...
6745   verifyFormat("struct {\n} n;");
6746   verifyFormat(
6747       "template <template <class T, class Y>, class Z> class X {\n} n;");
6748   verifyFormat("union Z {\n  int n;\n} x;");
6749   verifyFormat("class MACRO Z {\n} n;");
6750   verifyFormat("class MACRO(X) Z {\n} n;");
6751   verifyFormat("class __attribute__(X) Z {\n} n;");
6752   verifyFormat("class __declspec(X) Z {\n} n;");
6753   verifyFormat("class A##B##C {\n} n;");
6754   verifyFormat("class alignas(16) Z {\n} n;");
6755   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
6756   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
6757 
6758   // Redefinition from nested context:
6759   verifyFormat("class A::B::C {\n} n;");
6760 
6761   // Template definitions.
6762   verifyFormat(
6763       "template <typename F>\n"
6764       "Matcher(const Matcher<F> &Other,\n"
6765       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
6766       "                             !is_same<F, T>::value>::type * = 0)\n"
6767       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
6768 
6769   // FIXME: This is still incorrectly handled at the formatter side.
6770   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
6771   verifyFormat("int i = SomeFunction(a<b, a> b);");
6772 
6773   // FIXME:
6774   // This now gets parsed incorrectly as class definition.
6775   // verifyFormat("class A<int> f() {\n}\nint n;");
6776 
6777   // Elaborate types where incorrectly parsing the structural element would
6778   // break the indent.
6779   verifyFormat("if (true)\n"
6780                "  class X x;\n"
6781                "else\n"
6782                "  f();\n");
6783 
6784   // This is simply incomplete. Formatting is not important, but must not crash.
6785   verifyFormat("class A:");
6786 }
6787 
6788 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
6789   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
6790             format("#error Leave     all         white!!!!! space* alone!\n"));
6791   EXPECT_EQ(
6792       "#warning Leave     all         white!!!!! space* alone!\n",
6793       format("#warning Leave     all         white!!!!! space* alone!\n"));
6794   EXPECT_EQ("#error 1", format("  #  error   1"));
6795   EXPECT_EQ("#warning 1", format("  #  warning 1"));
6796 }
6797 
6798 TEST_F(FormatTest, FormatHashIfExpressions) {
6799   verifyFormat("#if AAAA && BBBB");
6800   verifyFormat("#if (AAAA && BBBB)");
6801   verifyFormat("#elif (AAAA && BBBB)");
6802   // FIXME: Come up with a better indentation for #elif.
6803   verifyFormat(
6804       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
6805       "    defined(BBBBBBBB)\n"
6806       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
6807       "    defined(BBBBBBBB)\n"
6808       "#endif",
6809       getLLVMStyleWithColumns(65));
6810 }
6811 
6812 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
6813   FormatStyle AllowsMergedIf = getGoogleStyle();
6814   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
6815   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
6816   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
6817   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
6818   EXPECT_EQ("if (true) return 42;",
6819             format("if (true)\nreturn 42;", AllowsMergedIf));
6820   FormatStyle ShortMergedIf = AllowsMergedIf;
6821   ShortMergedIf.ColumnLimit = 25;
6822   verifyFormat("#define A \\\n"
6823                "  if (true) return 42;",
6824                ShortMergedIf);
6825   verifyFormat("#define A \\\n"
6826                "  f();    \\\n"
6827                "  if (true)\n"
6828                "#define B",
6829                ShortMergedIf);
6830   verifyFormat("#define A \\\n"
6831                "  f();    \\\n"
6832                "  if (true)\n"
6833                "g();",
6834                ShortMergedIf);
6835   verifyFormat("{\n"
6836                "#ifdef A\n"
6837                "  // Comment\n"
6838                "  if (true) continue;\n"
6839                "#endif\n"
6840                "  // Comment\n"
6841                "  if (true) continue;\n"
6842                "}",
6843                ShortMergedIf);
6844   ShortMergedIf.ColumnLimit = 29;
6845   verifyFormat("#define A                   \\\n"
6846                "  if (aaaaaaaaaa) return 1; \\\n"
6847                "  return 2;",
6848                ShortMergedIf);
6849   ShortMergedIf.ColumnLimit = 28;
6850   verifyFormat("#define A         \\\n"
6851                "  if (aaaaaaaaaa) \\\n"
6852                "    return 1;     \\\n"
6853                "  return 2;",
6854                ShortMergedIf);
6855 }
6856 
6857 TEST_F(FormatTest, BlockCommentsInControlLoops) {
6858   verifyFormat("if (0) /* a comment in a strange place */ {\n"
6859                "  f();\n"
6860                "}");
6861   verifyFormat("if (0) /* a comment in a strange place */ {\n"
6862                "  f();\n"
6863                "} /* another comment */ else /* comment #3 */ {\n"
6864                "  g();\n"
6865                "}");
6866   verifyFormat("while (0) /* a comment in a strange place */ {\n"
6867                "  f();\n"
6868                "}");
6869   verifyFormat("for (;;) /* a comment in a strange place */ {\n"
6870                "  f();\n"
6871                "}");
6872   verifyFormat("do /* a comment in a strange place */ {\n"
6873                "  f();\n"
6874                "} /* another comment */ while (0);");
6875 }
6876 
6877 TEST_F(FormatTest, BlockComments) {
6878   EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
6879             format("/* *//* */  /* */\n/* *//* */  /* */"));
6880   EXPECT_EQ("/* */ a /* */ b;", format("  /* */  a/* */  b;"));
6881   EXPECT_EQ("#define A /*123*/ \\\n"
6882             "  b\n"
6883             "/* */\n"
6884             "someCall(\n"
6885             "    parameter);",
6886             format("#define A /*123*/ b\n"
6887                    "/* */\n"
6888                    "someCall(parameter);",
6889                    getLLVMStyleWithColumns(15)));
6890 
6891   EXPECT_EQ("#define A\n"
6892             "/* */ someCall(\n"
6893             "    parameter);",
6894             format("#define A\n"
6895                    "/* */someCall(parameter);",
6896                    getLLVMStyleWithColumns(15)));
6897   EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
6898   EXPECT_EQ("/*\n"
6899             "*\n"
6900             " * aaaaaa\n"
6901             " * aaaaaa\n"
6902             "*/",
6903             format("/*\n"
6904                    "*\n"
6905                    " * aaaaaa aaaaaa\n"
6906                    "*/",
6907                    getLLVMStyleWithColumns(10)));
6908   EXPECT_EQ("/*\n"
6909             "**\n"
6910             "* aaaaaa\n"
6911             "*aaaaaa\n"
6912             "*/",
6913             format("/*\n"
6914                    "**\n"
6915                    "* aaaaaa aaaaaa\n"
6916                    "*/",
6917                    getLLVMStyleWithColumns(10)));
6918 
6919   FormatStyle NoBinPacking = getLLVMStyle();
6920   NoBinPacking.BinPackParameters = false;
6921   EXPECT_EQ("someFunction(1, /* comment 1 */\n"
6922             "             2, /* comment 2 */\n"
6923             "             3, /* comment 3 */\n"
6924             "             aaaa,\n"
6925             "             bbbb);",
6926             format("someFunction (1,   /* comment 1 */\n"
6927                    "                2, /* comment 2 */  \n"
6928                    "               3,   /* comment 3 */\n"
6929                    "aaaa, bbbb );",
6930                    NoBinPacking));
6931   verifyFormat(
6932       "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6933       "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6934   EXPECT_EQ(
6935       "bool aaaaaaaaaaaaa = /* trailing comment */\n"
6936       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6937       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
6938       format(
6939           "bool       aaaaaaaaaaaaa =       /* trailing comment */\n"
6940           "    aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa    ||\n"
6941           "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa   || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
6942   EXPECT_EQ(
6943       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
6944       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   /* comment */\n"
6945       "int cccccccccccccccccccccccccccccc;       /* comment */\n",
6946       format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
6947              "int      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
6948              "int    cccccccccccccccccccccccccccccc;  /* comment */\n"));
6949 
6950   verifyFormat("void f(int * /* unused */) {}");
6951 
6952   EXPECT_EQ("/*\n"
6953             " **\n"
6954             " */",
6955             format("/*\n"
6956                    " **\n"
6957                    " */"));
6958   EXPECT_EQ("/*\n"
6959             " *q\n"
6960             " */",
6961             format("/*\n"
6962                    " *q\n"
6963                    " */"));
6964   EXPECT_EQ("/*\n"
6965             " * q\n"
6966             " */",
6967             format("/*\n"
6968                    " * q\n"
6969                    " */"));
6970   EXPECT_EQ("/*\n"
6971             " **/",
6972             format("/*\n"
6973                    " **/"));
6974   EXPECT_EQ("/*\n"
6975             " ***/",
6976             format("/*\n"
6977                    " ***/"));
6978 }
6979 
6980 TEST_F(FormatTest, BlockCommentsInMacros) {
6981   EXPECT_EQ("#define A          \\\n"
6982             "  {                \\\n"
6983             "    /* one line */ \\\n"
6984             "    someCall();",
6985             format("#define A {        \\\n"
6986                    "  /* one line */   \\\n"
6987                    "  someCall();",
6988                    getLLVMStyleWithColumns(20)));
6989   EXPECT_EQ("#define A          \\\n"
6990             "  {                \\\n"
6991             "    /* previous */ \\\n"
6992             "    /* one line */ \\\n"
6993             "    someCall();",
6994             format("#define A {        \\\n"
6995                    "  /* previous */   \\\n"
6996                    "  /* one line */   \\\n"
6997                    "  someCall();",
6998                    getLLVMStyleWithColumns(20)));
6999 }
7000 
7001 TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
7002   EXPECT_EQ("a = {\n"
7003             "    1111 /*    */\n"
7004             "};",
7005             format("a = {1111 /*    */\n"
7006                    "};",
7007                    getLLVMStyleWithColumns(15)));
7008   EXPECT_EQ("a = {\n"
7009             "    1111 /*      */\n"
7010             "};",
7011             format("a = {1111 /*      */\n"
7012                    "};",
7013                    getLLVMStyleWithColumns(15)));
7014 
7015   // FIXME: The formatting is still wrong here.
7016   EXPECT_EQ("a = {\n"
7017             "    1111 /*      a\n"
7018             "            */\n"
7019             "};",
7020             format("a = {1111 /*      a */\n"
7021                    "};",
7022                    getLLVMStyleWithColumns(15)));
7023 }
7024 
7025 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
7026   // FIXME: This is not what we want...
7027   verifyFormat("{\n"
7028                "// a"
7029                "// b");
7030 }
7031 
7032 TEST_F(FormatTest, FormatStarDependingOnContext) {
7033   verifyFormat("void f(int *a);");
7034   verifyFormat("void f() { f(fint * b); }");
7035   verifyFormat("class A {\n  void f(int *a);\n};");
7036   verifyFormat("class A {\n  int *a;\n};");
7037   verifyFormat("namespace a {\n"
7038                "namespace b {\n"
7039                "class A {\n"
7040                "  void f() {}\n"
7041                "  int *a;\n"
7042                "};\n"
7043                "}\n"
7044                "}");
7045 }
7046 
7047 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
7048   verifyFormat("while");
7049   verifyFormat("operator");
7050 }
7051 
7052 //===----------------------------------------------------------------------===//
7053 // Objective-C tests.
7054 //===----------------------------------------------------------------------===//
7055 
7056 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
7057   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
7058   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
7059             format("-(NSUInteger)indexOfObject:(id)anObject;"));
7060   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
7061   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
7062   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
7063             format("-(NSInteger)Method3:(id)anObject;"));
7064   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
7065             format("-(NSInteger)Method4:(id)anObject;"));
7066   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
7067             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
7068   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
7069             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
7070   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7071             "forAllCells:(BOOL)flag;",
7072             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7073                    "forAllCells:(BOOL)flag;"));
7074 
7075   // Very long objectiveC method declaration.
7076   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
7077                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
7078   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
7079                "                    inRange:(NSRange)range\n"
7080                "                   outRange:(NSRange)out_range\n"
7081                "                  outRange1:(NSRange)out_range1\n"
7082                "                  outRange2:(NSRange)out_range2\n"
7083                "                  outRange3:(NSRange)out_range3\n"
7084                "                  outRange4:(NSRange)out_range4\n"
7085                "                  outRange5:(NSRange)out_range5\n"
7086                "                  outRange6:(NSRange)out_range6\n"
7087                "                  outRange7:(NSRange)out_range7\n"
7088                "                  outRange8:(NSRange)out_range8\n"
7089                "                  outRange9:(NSRange)out_range9;");
7090 
7091   // When the function name has to be wrapped.
7092   FormatStyle Style = getLLVMStyle();
7093   Style.IndentWrappedFunctionNames = false;
7094   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7095                "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7096                "           anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7097                "}",
7098                Style);
7099   Style.IndentWrappedFunctionNames = true;
7100   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7101                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7102                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7103                "}",
7104                Style);
7105 
7106   verifyFormat("- (int)sum:(vector<int>)numbers;");
7107   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
7108   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
7109   // protocol lists (but not for template classes):
7110   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
7111 
7112   verifyFormat("- (int (*)())foo:(int (*)())f;");
7113   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
7114 
7115   // If there's no return type (very rare in practice!), LLVM and Google style
7116   // agree.
7117   verifyFormat("- foo;");
7118   verifyFormat("- foo:(int)f;");
7119   verifyGoogleFormat("- foo:(int)foo;");
7120 }
7121 
7122 TEST_F(FormatTest, FormatObjCInterface) {
7123   verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
7124                "@public\n"
7125                "  int field1;\n"
7126                "@protected\n"
7127                "  int field2;\n"
7128                "@private\n"
7129                "  int field3;\n"
7130                "@package\n"
7131                "  int field4;\n"
7132                "}\n"
7133                "+ (id)init;\n"
7134                "@end");
7135 
7136   verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
7137                      " @public\n"
7138                      "  int field1;\n"
7139                      " @protected\n"
7140                      "  int field2;\n"
7141                      " @private\n"
7142                      "  int field3;\n"
7143                      " @package\n"
7144                      "  int field4;\n"
7145                      "}\n"
7146                      "+ (id)init;\n"
7147                      "@end");
7148 
7149   verifyFormat("@interface /* wait for it */ Foo\n"
7150                "+ (id)init;\n"
7151                "// Look, a comment!\n"
7152                "- (int)answerWith:(int)i;\n"
7153                "@end");
7154 
7155   verifyFormat("@interface Foo\n"
7156                "@end\n"
7157                "@interface Bar\n"
7158                "@end");
7159 
7160   verifyFormat("@interface Foo : Bar\n"
7161                "+ (id)init;\n"
7162                "@end");
7163 
7164   verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
7165                "+ (id)init;\n"
7166                "@end");
7167 
7168   verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
7169                      "+ (id)init;\n"
7170                      "@end");
7171 
7172   verifyFormat("@interface Foo (HackStuff)\n"
7173                "+ (id)init;\n"
7174                "@end");
7175 
7176   verifyFormat("@interface Foo ()\n"
7177                "+ (id)init;\n"
7178                "@end");
7179 
7180   verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
7181                "+ (id)init;\n"
7182                "@end");
7183 
7184   verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
7185                      "+ (id)init;\n"
7186                      "@end");
7187 
7188   verifyFormat("@interface Foo {\n"
7189                "  int _i;\n"
7190                "}\n"
7191                "+ (id)init;\n"
7192                "@end");
7193 
7194   verifyFormat("@interface Foo : Bar {\n"
7195                "  int _i;\n"
7196                "}\n"
7197                "+ (id)init;\n"
7198                "@end");
7199 
7200   verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
7201                "  int _i;\n"
7202                "}\n"
7203                "+ (id)init;\n"
7204                "@end");
7205 
7206   verifyFormat("@interface Foo (HackStuff) {\n"
7207                "  int _i;\n"
7208                "}\n"
7209                "+ (id)init;\n"
7210                "@end");
7211 
7212   verifyFormat("@interface Foo () {\n"
7213                "  int _i;\n"
7214                "}\n"
7215                "+ (id)init;\n"
7216                "@end");
7217 
7218   verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
7219                "  int _i;\n"
7220                "}\n"
7221                "+ (id)init;\n"
7222                "@end");
7223 
7224   FormatStyle OnePerLine = getGoogleStyle();
7225   OnePerLine.BinPackParameters = false;
7226   verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n"
7227                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7228                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7229                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7230                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
7231                "}",
7232                OnePerLine);
7233 }
7234 
7235 TEST_F(FormatTest, FormatObjCImplementation) {
7236   verifyFormat("@implementation Foo : NSObject {\n"
7237                "@public\n"
7238                "  int field1;\n"
7239                "@protected\n"
7240                "  int field2;\n"
7241                "@private\n"
7242                "  int field3;\n"
7243                "@package\n"
7244                "  int field4;\n"
7245                "}\n"
7246                "+ (id)init {\n}\n"
7247                "@end");
7248 
7249   verifyGoogleFormat("@implementation Foo : NSObject {\n"
7250                      " @public\n"
7251                      "  int field1;\n"
7252                      " @protected\n"
7253                      "  int field2;\n"
7254                      " @private\n"
7255                      "  int field3;\n"
7256                      " @package\n"
7257                      "  int field4;\n"
7258                      "}\n"
7259                      "+ (id)init {\n}\n"
7260                      "@end");
7261 
7262   verifyFormat("@implementation Foo\n"
7263                "+ (id)init {\n"
7264                "  if (true)\n"
7265                "    return nil;\n"
7266                "}\n"
7267                "// Look, a comment!\n"
7268                "- (int)answerWith:(int)i {\n"
7269                "  return i;\n"
7270                "}\n"
7271                "+ (int)answerWith:(int)i {\n"
7272                "  return i;\n"
7273                "}\n"
7274                "@end");
7275 
7276   verifyFormat("@implementation Foo\n"
7277                "@end\n"
7278                "@implementation Bar\n"
7279                "@end");
7280 
7281   EXPECT_EQ("@implementation Foo : Bar\n"
7282             "+ (id)init {\n}\n"
7283             "- (void)foo {\n}\n"
7284             "@end",
7285             format("@implementation Foo : Bar\n"
7286                    "+(id)init{}\n"
7287                    "-(void)foo{}\n"
7288                    "@end"));
7289 
7290   verifyFormat("@implementation Foo {\n"
7291                "  int _i;\n"
7292                "}\n"
7293                "+ (id)init {\n}\n"
7294                "@end");
7295 
7296   verifyFormat("@implementation Foo : Bar {\n"
7297                "  int _i;\n"
7298                "}\n"
7299                "+ (id)init {\n}\n"
7300                "@end");
7301 
7302   verifyFormat("@implementation Foo (HackStuff)\n"
7303                "+ (id)init {\n}\n"
7304                "@end");
7305   verifyFormat("@implementation ObjcClass\n"
7306                "- (void)method;\n"
7307                "{}\n"
7308                "@end");
7309 }
7310 
7311 TEST_F(FormatTest, FormatObjCProtocol) {
7312   verifyFormat("@protocol Foo\n"
7313                "@property(weak) id delegate;\n"
7314                "- (NSUInteger)numberOfThings;\n"
7315                "@end");
7316 
7317   verifyFormat("@protocol MyProtocol <NSObject>\n"
7318                "- (NSUInteger)numberOfThings;\n"
7319                "@end");
7320 
7321   verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
7322                      "- (NSUInteger)numberOfThings;\n"
7323                      "@end");
7324 
7325   verifyFormat("@protocol Foo;\n"
7326                "@protocol Bar;\n");
7327 
7328   verifyFormat("@protocol Foo\n"
7329                "@end\n"
7330                "@protocol Bar\n"
7331                "@end");
7332 
7333   verifyFormat("@protocol myProtocol\n"
7334                "- (void)mandatoryWithInt:(int)i;\n"
7335                "@optional\n"
7336                "- (void)optional;\n"
7337                "@required\n"
7338                "- (void)required;\n"
7339                "@optional\n"
7340                "@property(assign) int madProp;\n"
7341                "@end\n");
7342 
7343   verifyFormat("@property(nonatomic, assign, readonly)\n"
7344                "    int *looooooooooooooooooooooooooooongNumber;\n"
7345                "@property(nonatomic, assign, readonly)\n"
7346                "    NSString *looooooooooooooooooooooooooooongName;");
7347 
7348   verifyFormat("@implementation PR18406\n"
7349                "}\n"
7350                "@end");
7351 }
7352 
7353 TEST_F(FormatTest, FormatObjCMethodDeclarations) {
7354   verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
7355                "                   rect:(NSRect)theRect\n"
7356                "               interval:(float)theInterval {\n"
7357                "}");
7358   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7359                "      longKeyword:(NSRect)theRect\n"
7360                "    longerKeyword:(float)theInterval\n"
7361                "            error:(NSError **)theError {\n"
7362                "}");
7363   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7364                "          longKeyword:(NSRect)theRect\n"
7365                "    evenLongerKeyword:(float)theInterval\n"
7366                "                error:(NSError **)theError {\n"
7367                "}");
7368   verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
7369                "                         y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
7370                "    NS_DESIGNATED_INITIALIZER;",
7371                getLLVMStyleWithColumns(60));
7372 
7373   // Continuation indent width should win over aligning colons if the function
7374   // name is long.
7375   FormatStyle continuationStyle = getGoogleStyle();
7376   continuationStyle.ColumnLimit = 40;
7377   continuationStyle.IndentWrappedFunctionNames = true;
7378   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7379                "    dontAlignNamef:(NSRect)theRect {\n"
7380                "}",
7381                continuationStyle);
7382 
7383   // Make sure we don't break aligning for short parameter names.
7384   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7385                "       aShortf:(NSRect)theRect {\n"
7386                "}",
7387                continuationStyle);
7388 }
7389 
7390 TEST_F(FormatTest, FormatObjCMethodExpr) {
7391   verifyFormat("[foo bar:baz];");
7392   verifyFormat("return [foo bar:baz];");
7393   verifyFormat("return (a)[foo bar:baz];");
7394   verifyFormat("f([foo bar:baz]);");
7395   verifyFormat("f(2, [foo bar:baz]);");
7396   verifyFormat("f(2, a ? b : c);");
7397   verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
7398 
7399   // Unary operators.
7400   verifyFormat("int a = +[foo bar:baz];");
7401   verifyFormat("int a = -[foo bar:baz];");
7402   verifyFormat("int a = ![foo bar:baz];");
7403   verifyFormat("int a = ~[foo bar:baz];");
7404   verifyFormat("int a = ++[foo bar:baz];");
7405   verifyFormat("int a = --[foo bar:baz];");
7406   verifyFormat("int a = sizeof [foo bar:baz];");
7407   verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
7408   verifyFormat("int a = &[foo bar:baz];");
7409   verifyFormat("int a = *[foo bar:baz];");
7410   // FIXME: Make casts work, without breaking f()[4].
7411   // verifyFormat("int a = (int)[foo bar:baz];");
7412   // verifyFormat("return (int)[foo bar:baz];");
7413   // verifyFormat("(void)[foo bar:baz];");
7414   verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
7415 
7416   // Binary operators.
7417   verifyFormat("[foo bar:baz], [foo bar:baz];");
7418   verifyFormat("[foo bar:baz] = [foo bar:baz];");
7419   verifyFormat("[foo bar:baz] *= [foo bar:baz];");
7420   verifyFormat("[foo bar:baz] /= [foo bar:baz];");
7421   verifyFormat("[foo bar:baz] %= [foo bar:baz];");
7422   verifyFormat("[foo bar:baz] += [foo bar:baz];");
7423   verifyFormat("[foo bar:baz] -= [foo bar:baz];");
7424   verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
7425   verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
7426   verifyFormat("[foo bar:baz] &= [foo bar:baz];");
7427   verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
7428   verifyFormat("[foo bar:baz] |= [foo bar:baz];");
7429   verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
7430   verifyFormat("[foo bar:baz] || [foo bar:baz];");
7431   verifyFormat("[foo bar:baz] && [foo bar:baz];");
7432   verifyFormat("[foo bar:baz] | [foo bar:baz];");
7433   verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
7434   verifyFormat("[foo bar:baz] & [foo bar:baz];");
7435   verifyFormat("[foo bar:baz] == [foo bar:baz];");
7436   verifyFormat("[foo bar:baz] != [foo bar:baz];");
7437   verifyFormat("[foo bar:baz] >= [foo bar:baz];");
7438   verifyFormat("[foo bar:baz] <= [foo bar:baz];");
7439   verifyFormat("[foo bar:baz] > [foo bar:baz];");
7440   verifyFormat("[foo bar:baz] < [foo bar:baz];");
7441   verifyFormat("[foo bar:baz] >> [foo bar:baz];");
7442   verifyFormat("[foo bar:baz] << [foo bar:baz];");
7443   verifyFormat("[foo bar:baz] - [foo bar:baz];");
7444   verifyFormat("[foo bar:baz] + [foo bar:baz];");
7445   verifyFormat("[foo bar:baz] * [foo bar:baz];");
7446   verifyFormat("[foo bar:baz] / [foo bar:baz];");
7447   verifyFormat("[foo bar:baz] % [foo bar:baz];");
7448   // Whew!
7449 
7450   verifyFormat("return in[42];");
7451   verifyFormat("for (auto v : in[1]) {\n}");
7452   verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
7453   verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
7454   verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
7455   verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
7456   verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
7457   verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
7458                "}");
7459   verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
7460   verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
7461   verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
7462 
7463   verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
7464   verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
7465   verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
7466   verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
7467   verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
7468   verifyFormat("[button setAction:@selector(zoomOut:)];");
7469   verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
7470 
7471   verifyFormat("arr[[self indexForFoo:a]];");
7472   verifyFormat("throw [self errorFor:a];");
7473   verifyFormat("@throw [self errorFor:a];");
7474 
7475   verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
7476   verifyFormat("[(id)foo bar:(id) ? baz : quux];");
7477   verifyFormat("4 > 4 ? (id)a : (id)baz;");
7478 
7479   // This tests that the formatter doesn't break after "backing" but before ":",
7480   // which would be at 80 columns.
7481   verifyFormat(
7482       "void f() {\n"
7483       "  if ((self = [super initWithContentRect:contentRect\n"
7484       "                               styleMask:styleMask ?: otherMask\n"
7485       "                                 backing:NSBackingStoreBuffered\n"
7486       "                                   defer:YES]))");
7487 
7488   verifyFormat(
7489       "[foo checkThatBreakingAfterColonWorksOk:\n"
7490       "         [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
7491 
7492   verifyFormat("[myObj short:arg1 // Force line break\n"
7493                "          longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
7494                "    evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
7495                "                error:arg4];");
7496   verifyFormat(
7497       "void f() {\n"
7498       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
7499       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
7500       "                                     pos.width(), pos.height())\n"
7501       "                styleMask:NSBorderlessWindowMask\n"
7502       "                  backing:NSBackingStoreBuffered\n"
7503       "                    defer:NO]);\n"
7504       "}");
7505   verifyFormat(
7506       "void f() {\n"
7507       "  popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
7508       "      iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
7509       "                                 pos.width(), pos.height())\n"
7510       "                syeMask:NSBorderlessWindowMask\n"
7511       "                  bking:NSBackingStoreBuffered\n"
7512       "                    der:NO]);\n"
7513       "}",
7514       getLLVMStyleWithColumns(70));
7515   verifyFormat(
7516       "void f() {\n"
7517       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
7518       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
7519       "                                     pos.width(), pos.height())\n"
7520       "                styleMask:NSBorderlessWindowMask\n"
7521       "                  backing:NSBackingStoreBuffered\n"
7522       "                    defer:NO]);\n"
7523       "}",
7524       getChromiumStyle(FormatStyle::LK_Cpp));
7525   verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
7526                "                             with:contentsNativeView];");
7527 
7528   verifyFormat(
7529       "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
7530       "           owner:nillllll];");
7531 
7532   verifyFormat(
7533       "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
7534       "        forType:kBookmarkButtonDragType];");
7535 
7536   verifyFormat("[defaultCenter addObserver:self\n"
7537                "                  selector:@selector(willEnterFullscreen)\n"
7538                "                      name:kWillEnterFullscreenNotification\n"
7539                "                    object:nil];");
7540   verifyFormat("[image_rep drawInRect:drawRect\n"
7541                "             fromRect:NSZeroRect\n"
7542                "            operation:NSCompositeCopy\n"
7543                "             fraction:1.0\n"
7544                "       respectFlipped:NO\n"
7545                "                hints:nil];");
7546   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7547                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
7548   verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
7549                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
7550   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
7551                "    aaaaaaaaaaaaaaaaaaaaaa];");
7552   verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
7553                "        .aaaaaaaa];", // FIXME: Indentation seems off.
7554                getLLVMStyleWithColumns(60));
7555 
7556   verifyFormat(
7557       "scoped_nsobject<NSTextField> message(\n"
7558       "    // The frame will be fixed up when |-setMessageText:| is called.\n"
7559       "    [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
7560   verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
7561                "    aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
7562                "         aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
7563                "          aaaa:bbb];");
7564   verifyFormat("[self param:function( //\n"
7565                "                parameter)]");
7566   verifyFormat(
7567       "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7568       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7569       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
7570 
7571   // FIXME: This violates the column limit.
7572   verifyFormat(
7573       "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
7574       "    aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
7575       "                  aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];",
7576       getLLVMStyleWithColumns(60));
7577 
7578   // Variadic parameters.
7579   verifyFormat(
7580       "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
7581   verifyFormat(
7582       "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7583       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7584       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
7585   verifyFormat("[self // break\n"
7586                "      a:a\n"
7587                "    aaa:aaa];");
7588   verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
7589                "          [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
7590 }
7591 
7592 TEST_F(FormatTest, ObjCAt) {
7593   verifyFormat("@autoreleasepool");
7594   verifyFormat("@catch");
7595   verifyFormat("@class");
7596   verifyFormat("@compatibility_alias");
7597   verifyFormat("@defs");
7598   verifyFormat("@dynamic");
7599   verifyFormat("@encode");
7600   verifyFormat("@end");
7601   verifyFormat("@finally");
7602   verifyFormat("@implementation");
7603   verifyFormat("@import");
7604   verifyFormat("@interface");
7605   verifyFormat("@optional");
7606   verifyFormat("@package");
7607   verifyFormat("@private");
7608   verifyFormat("@property");
7609   verifyFormat("@protected");
7610   verifyFormat("@protocol");
7611   verifyFormat("@public");
7612   verifyFormat("@required");
7613   verifyFormat("@selector");
7614   verifyFormat("@synchronized");
7615   verifyFormat("@synthesize");
7616   verifyFormat("@throw");
7617   verifyFormat("@try");
7618 
7619   EXPECT_EQ("@interface", format("@ interface"));
7620 
7621   // The precise formatting of this doesn't matter, nobody writes code like
7622   // this.
7623   verifyFormat("@ /*foo*/ interface");
7624 }
7625 
7626 TEST_F(FormatTest, ObjCSnippets) {
7627   verifyFormat("@autoreleasepool {\n"
7628                "  foo();\n"
7629                "}");
7630   verifyFormat("@class Foo, Bar;");
7631   verifyFormat("@compatibility_alias AliasName ExistingClass;");
7632   verifyFormat("@dynamic textColor;");
7633   verifyFormat("char *buf1 = @encode(int *);");
7634   verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
7635   verifyFormat("char *buf1 = @encode(int **);");
7636   verifyFormat("Protocol *proto = @protocol(p1);");
7637   verifyFormat("SEL s = @selector(foo:);");
7638   verifyFormat("@synchronized(self) {\n"
7639                "  f();\n"
7640                "}");
7641 
7642   verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7643   verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7644 
7645   verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
7646   verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
7647   verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
7648   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7649                getMozillaStyle());
7650   verifyFormat("@property BOOL editable;", getMozillaStyle());
7651   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7652                getWebKitStyle());
7653   verifyFormat("@property BOOL editable;", getWebKitStyle());
7654 
7655   verifyFormat("@import foo.bar;\n"
7656                "@import baz;");
7657 }
7658 
7659 TEST_F(FormatTest, ObjCForIn) {
7660   verifyFormat("- (void)test {\n"
7661                "  for (NSString *n in arrayOfStrings) {\n"
7662                "    foo(n);\n"
7663                "  }\n"
7664                "}");
7665   verifyFormat("- (void)test {\n"
7666                "  for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
7667                "    foo(n);\n"
7668                "  }\n"
7669                "}");
7670 }
7671 
7672 TEST_F(FormatTest, ObjCLiterals) {
7673   verifyFormat("@\"String\"");
7674   verifyFormat("@1");
7675   verifyFormat("@+4.8");
7676   verifyFormat("@-4");
7677   verifyFormat("@1LL");
7678   verifyFormat("@.5");
7679   verifyFormat("@'c'");
7680   verifyFormat("@true");
7681 
7682   verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
7683   verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
7684   verifyFormat("NSNumber *favoriteColor = @(Green);");
7685   verifyFormat("NSString *path = @(getenv(\"PATH\"));");
7686 
7687   verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
7688 }
7689 
7690 TEST_F(FormatTest, ObjCDictLiterals) {
7691   verifyFormat("@{");
7692   verifyFormat("@{}");
7693   verifyFormat("@{@\"one\" : @1}");
7694   verifyFormat("return @{@\"one\" : @1;");
7695   verifyFormat("@{@\"one\" : @1}");
7696 
7697   verifyFormat("@{@\"one\" : @{@2 : @1}}");
7698   verifyFormat("@{\n"
7699                "  @\"one\" : @{@2 : @1},\n"
7700                "}");
7701 
7702   verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
7703   verifyIncompleteFormat("[self setDict:@{}");
7704   verifyIncompleteFormat("[self setDict:@{@1 : @2}");
7705   verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
7706   verifyFormat(
7707       "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
7708   verifyFormat(
7709       "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
7710 
7711   verifyFormat("NSDictionary *d = @{\n"
7712                "  @\"nam\" : NSUserNam(),\n"
7713                "  @\"dte\" : [NSDate date],\n"
7714                "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
7715                "};");
7716   verifyFormat(
7717       "@{\n"
7718       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7719       "regularFont,\n"
7720       "};");
7721   verifyGoogleFormat(
7722       "@{\n"
7723       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7724       "regularFont,\n"
7725       "};");
7726   verifyFormat(
7727       "@{\n"
7728       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
7729       "      reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
7730       "};");
7731 
7732   // We should try to be robust in case someone forgets the "@".
7733   verifyFormat("NSDictionary *d = {\n"
7734                "  @\"nam\" : NSUserNam(),\n"
7735                "  @\"dte\" : [NSDate date],\n"
7736                "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
7737                "};");
7738   verifyFormat("NSMutableDictionary *dictionary =\n"
7739                "    [NSMutableDictionary dictionaryWithDictionary:@{\n"
7740                "      aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
7741                "      bbbbbbbbbbbbbbbbbb : bbbbb,\n"
7742                "      cccccccccccccccc : ccccccccccccccc\n"
7743                "    }];");
7744 
7745   // Ensure that casts before the key are kept on the same line as the key.
7746   verifyFormat(
7747       "NSDictionary *d = @{\n"
7748       "  (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7749       "  (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
7750       "};");
7751 }
7752 
7753 TEST_F(FormatTest, ObjCArrayLiterals) {
7754   verifyIncompleteFormat("@[");
7755   verifyFormat("@[]");
7756   verifyFormat(
7757       "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
7758   verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
7759   verifyFormat("NSArray *array = @[ [foo description] ];");
7760 
7761   verifyFormat(
7762       "NSArray *some_variable = @[\n"
7763       "  aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
7764       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7765       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7766       "  @\"aaaaaaaaaaaaaaaaa\"\n"
7767       "];");
7768   verifyFormat("NSArray *some_variable = @[\n"
7769                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7770                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7771                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7772                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7773                "];");
7774   verifyGoogleFormat("NSArray *some_variable = @[\n"
7775                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7776                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7777                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7778                      "  @\"aaaaaaaaaaaaaaaaa\"\n"
7779                      "];");
7780   verifyFormat("NSArray *array = @[\n"
7781                "  @\"a\",\n"
7782                "  @\"a\",\n" // Trailing comma -> one per line.
7783                "];");
7784 
7785   // We should try to be robust in case someone forgets the "@".
7786   verifyFormat("NSArray *some_variable = [\n"
7787                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7788                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7789                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7790                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7791                "];");
7792   verifyFormat(
7793       "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
7794       "                                             index:(NSUInteger)index\n"
7795       "                                nonDigitAttributes:\n"
7796       "                                    (NSDictionary *)noDigitAttributes;");
7797   verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
7798                "  NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
7799                "]];");
7800 }
7801 
7802 TEST_F(FormatTest, BreaksStringLiterals) {
7803   EXPECT_EQ("\"some text \"\n"
7804             "\"other\";",
7805             format("\"some text other\";", getLLVMStyleWithColumns(12)));
7806   EXPECT_EQ("\"some text \"\n"
7807             "\"other\";",
7808             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
7809   EXPECT_EQ(
7810       "#define A  \\\n"
7811       "  \"some \"  \\\n"
7812       "  \"text \"  \\\n"
7813       "  \"other\";",
7814       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
7815   EXPECT_EQ(
7816       "#define A  \\\n"
7817       "  \"so \"    \\\n"
7818       "  \"text \"  \\\n"
7819       "  \"other\";",
7820       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
7821 
7822   EXPECT_EQ("\"some text\"",
7823             format("\"some text\"", getLLVMStyleWithColumns(1)));
7824   EXPECT_EQ("\"some text\"",
7825             format("\"some text\"", getLLVMStyleWithColumns(11)));
7826   EXPECT_EQ("\"some \"\n"
7827             "\"text\"",
7828             format("\"some text\"", getLLVMStyleWithColumns(10)));
7829   EXPECT_EQ("\"some \"\n"
7830             "\"text\"",
7831             format("\"some text\"", getLLVMStyleWithColumns(7)));
7832   EXPECT_EQ("\"some\"\n"
7833             "\" tex\"\n"
7834             "\"t\"",
7835             format("\"some text\"", getLLVMStyleWithColumns(6)));
7836   EXPECT_EQ("\"some\"\n"
7837             "\" tex\"\n"
7838             "\" and\"",
7839             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
7840   EXPECT_EQ("\"some\"\n"
7841             "\"/tex\"\n"
7842             "\"/and\"",
7843             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
7844 
7845   EXPECT_EQ("variable =\n"
7846             "    \"long string \"\n"
7847             "    \"literal\";",
7848             format("variable = \"long string literal\";",
7849                    getLLVMStyleWithColumns(20)));
7850 
7851   EXPECT_EQ("variable = f(\n"
7852             "    \"long string \"\n"
7853             "    \"literal\",\n"
7854             "    short,\n"
7855             "    loooooooooooooooooooong);",
7856             format("variable = f(\"long string literal\", short, "
7857                    "loooooooooooooooooooong);",
7858                    getLLVMStyleWithColumns(20)));
7859 
7860   EXPECT_EQ(
7861       "f(g(\"long string \"\n"
7862       "    \"literal\"),\n"
7863       "  b);",
7864       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
7865   EXPECT_EQ("f(g(\"long string \"\n"
7866             "    \"literal\",\n"
7867             "    a),\n"
7868             "  b);",
7869             format("f(g(\"long string literal\", a), b);",
7870                    getLLVMStyleWithColumns(20)));
7871   EXPECT_EQ(
7872       "f(\"one two\".split(\n"
7873       "    variable));",
7874       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
7875   EXPECT_EQ("f(\"one two three four five six \"\n"
7876             "  \"seven\".split(\n"
7877             "      really_looooong_variable));",
7878             format("f(\"one two three four five six seven\"."
7879                    "split(really_looooong_variable));",
7880                    getLLVMStyleWithColumns(33)));
7881 
7882   EXPECT_EQ("f(\"some \"\n"
7883             "  \"text\",\n"
7884             "  other);",
7885             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
7886 
7887   // Only break as a last resort.
7888   verifyFormat(
7889       "aaaaaaaaaaaaaaaaaaaa(\n"
7890       "    aaaaaaaaaaaaaaaaaaaa,\n"
7891       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
7892 
7893   EXPECT_EQ("\"splitmea\"\n"
7894             "\"trandomp\"\n"
7895             "\"oint\"",
7896             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
7897 
7898   EXPECT_EQ("\"split/\"\n"
7899             "\"pathat/\"\n"
7900             "\"slashes\"",
7901             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7902 
7903   EXPECT_EQ("\"split/\"\n"
7904             "\"pathat/\"\n"
7905             "\"slashes\"",
7906             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7907   EXPECT_EQ("\"split at \"\n"
7908             "\"spaces/at/\"\n"
7909             "\"slashes.at.any$\"\n"
7910             "\"non-alphanumeric%\"\n"
7911             "\"1111111111characte\"\n"
7912             "\"rs\"",
7913             format("\"split at "
7914                    "spaces/at/"
7915                    "slashes.at."
7916                    "any$non-"
7917                    "alphanumeric%"
7918                    "1111111111characte"
7919                    "rs\"",
7920                    getLLVMStyleWithColumns(20)));
7921 
7922   // Verify that splitting the strings understands
7923   // Style::AlwaysBreakBeforeMultilineStrings.
7924   EXPECT_EQ(
7925       "aaaaaaaaaaaa(\n"
7926       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
7927       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
7928       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
7929              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7930              "aaaaaaaaaaaaaaaaaaaaaa\");",
7931              getGoogleStyle()));
7932   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7933             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
7934             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
7935                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7936                    "aaaaaaaaaaaaaaaaaaaaaa\";",
7937                    getGoogleStyle()));
7938   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7939             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7940             format("llvm::outs() << "
7941                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
7942                    "aaaaaaaaaaaaaaaaaaa\";"));
7943   EXPECT_EQ("ffff(\n"
7944             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7945             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7946             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
7947                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7948                    getGoogleStyle()));
7949 
7950   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
7951   AlignLeft.AlignEscapedNewlinesLeft = true;
7952   EXPECT_EQ("#define A \\\n"
7953             "  \"some \" \\\n"
7954             "  \"text \" \\\n"
7955             "  \"other\";",
7956             format("#define A \"some text other\";", AlignLeft));
7957 }
7958 
7959 TEST_F(FormatTest, FullyRemoveEmptyLines) {
7960   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
7961   NoEmptyLines.MaxEmptyLinesToKeep = 0;
7962   EXPECT_EQ("int i = a(b());",
7963             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
7964 }
7965 
7966 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
7967   EXPECT_EQ(
7968       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7969       "(\n"
7970       "    \"x\t\");",
7971       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7972              "aaaaaaa("
7973              "\"x\t\");"));
7974 }
7975 
7976 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
7977   EXPECT_EQ(
7978       "u8\"utf8 string \"\n"
7979       "u8\"literal\";",
7980       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
7981   EXPECT_EQ(
7982       "u\"utf16 string \"\n"
7983       "u\"literal\";",
7984       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
7985   EXPECT_EQ(
7986       "U\"utf32 string \"\n"
7987       "U\"literal\";",
7988       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
7989   EXPECT_EQ("L\"wide string \"\n"
7990             "L\"literal\";",
7991             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
7992   EXPECT_EQ("@\"NSString \"\n"
7993             "@\"literal\";",
7994             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
7995 
7996   // This input makes clang-format try to split the incomplete unicode escape
7997   // sequence, which used to lead to a crasher.
7998   verifyNoCrash(
7999       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
8000       getLLVMStyleWithColumns(60));
8001 }
8002 
8003 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
8004   FormatStyle Style = getGoogleStyleWithColumns(15);
8005   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
8006   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
8007   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
8008   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
8009   EXPECT_EQ("u8R\"x(raw literal)x\";",
8010             format("u8R\"x(raw literal)x\";", Style));
8011 }
8012 
8013 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
8014   FormatStyle Style = getLLVMStyleWithColumns(20);
8015   EXPECT_EQ(
8016       "_T(\"aaaaaaaaaaaaaa\")\n"
8017       "_T(\"aaaaaaaaaaaaaa\")\n"
8018       "_T(\"aaaaaaaaaaaa\")",
8019       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
8020   EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
8021             "     _T(\"aaaaaa\"),\n"
8022             "  z);",
8023             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
8024 
8025   // FIXME: Handle embedded spaces in one iteration.
8026   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
8027   //            "_T(\"aaaaaaaaaaaaa\")\n"
8028   //            "_T(\"aaaaaaaaaaaaa\")\n"
8029   //            "_T(\"a\")",
8030   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
8031   //                   getLLVMStyleWithColumns(20)));
8032   EXPECT_EQ(
8033       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
8034       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
8035   EXPECT_EQ("f(\n"
8036             "#if !TEST\n"
8037             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
8038             "#endif\n"
8039             "    );",
8040             format("f(\n"
8041                    "#if !TEST\n"
8042                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
8043                    "#endif\n"
8044                    ");"));
8045   EXPECT_EQ("f(\n"
8046             "\n"
8047             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
8048             format("f(\n"
8049                    "\n"
8050                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
8051 }
8052 
8053 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
8054   EXPECT_EQ(
8055       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8056       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8057       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
8058       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8059              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8060              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
8061 }
8062 
8063 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
8064   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
8065             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
8066   EXPECT_EQ("fffffffffff(g(R\"x(\n"
8067             "multiline raw string literal xxxxxxxxxxxxxx\n"
8068             ")x\",\n"
8069             "              a),\n"
8070             "            b);",
8071             format("fffffffffff(g(R\"x(\n"
8072                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8073                    ")x\", a), b);",
8074                    getGoogleStyleWithColumns(20)));
8075   EXPECT_EQ("fffffffffff(\n"
8076             "    g(R\"x(qqq\n"
8077             "multiline raw string literal xxxxxxxxxxxxxx\n"
8078             ")x\",\n"
8079             "      a),\n"
8080             "    b);",
8081             format("fffffffffff(g(R\"x(qqq\n"
8082                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8083                    ")x\", a), b);",
8084                    getGoogleStyleWithColumns(20)));
8085 
8086   EXPECT_EQ("fffffffffff(R\"x(\n"
8087             "multiline raw string literal xxxxxxxxxxxxxx\n"
8088             ")x\");",
8089             format("fffffffffff(R\"x(\n"
8090                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8091                    ")x\");",
8092                    getGoogleStyleWithColumns(20)));
8093   EXPECT_EQ("fffffffffff(R\"x(\n"
8094             "multiline raw string literal xxxxxxxxxxxxxx\n"
8095             ")x\" + bbbbbb);",
8096             format("fffffffffff(R\"x(\n"
8097                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8098                    ")x\" +   bbbbbb);",
8099                    getGoogleStyleWithColumns(20)));
8100   EXPECT_EQ("fffffffffff(\n"
8101             "    R\"x(\n"
8102             "multiline raw string literal xxxxxxxxxxxxxx\n"
8103             ")x\" +\n"
8104             "    bbbbbb);",
8105             format("fffffffffff(\n"
8106                    " R\"x(\n"
8107                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8108                    ")x\" + bbbbbb);",
8109                    getGoogleStyleWithColumns(20)));
8110 }
8111 
8112 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
8113   verifyFormat("string a = \"unterminated;");
8114   EXPECT_EQ("function(\"unterminated,\n"
8115             "         OtherParameter);",
8116             format("function(  \"unterminated,\n"
8117                    "    OtherParameter);"));
8118 }
8119 
8120 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
8121   FormatStyle Style = getLLVMStyle();
8122   Style.Standard = FormatStyle::LS_Cpp03;
8123   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
8124             format("#define x(_a) printf(\"foo\"_a);", Style));
8125 }
8126 
8127 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
8128 
8129 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
8130   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
8131             "             \"ddeeefff\");",
8132             format("someFunction(\"aaabbbcccdddeeefff\");",
8133                    getLLVMStyleWithColumns(25)));
8134   EXPECT_EQ("someFunction1234567890(\n"
8135             "    \"aaabbbcccdddeeefff\");",
8136             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8137                    getLLVMStyleWithColumns(26)));
8138   EXPECT_EQ("someFunction1234567890(\n"
8139             "    \"aaabbbcccdddeeeff\"\n"
8140             "    \"f\");",
8141             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8142                    getLLVMStyleWithColumns(25)));
8143   EXPECT_EQ("someFunction1234567890(\n"
8144             "    \"aaabbbcccdddeeeff\"\n"
8145             "    \"f\");",
8146             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8147                    getLLVMStyleWithColumns(24)));
8148   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
8149             "             \"ddde \"\n"
8150             "             \"efff\");",
8151             format("someFunction(\"aaabbbcc ddde efff\");",
8152                    getLLVMStyleWithColumns(25)));
8153   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
8154             "             \"ddeeefff\");",
8155             format("someFunction(\"aaabbbccc ddeeefff\");",
8156                    getLLVMStyleWithColumns(25)));
8157   EXPECT_EQ("someFunction1234567890(\n"
8158             "    \"aaabb \"\n"
8159             "    \"cccdddeeefff\");",
8160             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
8161                    getLLVMStyleWithColumns(25)));
8162   EXPECT_EQ("#define A          \\\n"
8163             "  string s =       \\\n"
8164             "      \"123456789\"  \\\n"
8165             "      \"0\";         \\\n"
8166             "  int i;",
8167             format("#define A string s = \"1234567890\"; int i;",
8168                    getLLVMStyleWithColumns(20)));
8169   // FIXME: Put additional penalties on breaking at non-whitespace locations.
8170   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
8171             "             \"dddeeeff\"\n"
8172             "             \"f\");",
8173             format("someFunction(\"aaabbbcc dddeeefff\");",
8174                    getLLVMStyleWithColumns(25)));
8175 }
8176 
8177 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
8178   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
8179   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
8180   EXPECT_EQ("\"test\"\n"
8181             "\"\\n\"",
8182             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
8183   EXPECT_EQ("\"tes\\\\\"\n"
8184             "\"n\"",
8185             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
8186   EXPECT_EQ("\"\\\\\\\\\"\n"
8187             "\"\\n\"",
8188             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
8189   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
8190   EXPECT_EQ("\"\\uff01\"\n"
8191             "\"test\"",
8192             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
8193   EXPECT_EQ("\"\\Uff01ff02\"",
8194             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
8195   EXPECT_EQ("\"\\x000000000001\"\n"
8196             "\"next\"",
8197             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
8198   EXPECT_EQ("\"\\x000000000001next\"",
8199             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
8200   EXPECT_EQ("\"\\x000000000001\"",
8201             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
8202   EXPECT_EQ("\"test\"\n"
8203             "\"\\000000\"\n"
8204             "\"000001\"",
8205             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
8206   EXPECT_EQ("\"test\\000\"\n"
8207             "\"00000000\"\n"
8208             "\"1\"",
8209             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
8210 }
8211 
8212 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
8213   verifyFormat("void f() {\n"
8214                "  return g() {}\n"
8215                "  void h() {}");
8216   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
8217                "g();\n"
8218                "}");
8219 }
8220 
8221 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
8222   verifyFormat(
8223       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
8224 }
8225 
8226 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
8227   verifyFormat("class X {\n"
8228                "  void f() {\n"
8229                "  }\n"
8230                "};",
8231                getLLVMStyleWithColumns(12));
8232 }
8233 
8234 TEST_F(FormatTest, ConfigurableIndentWidth) {
8235   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
8236   EightIndent.IndentWidth = 8;
8237   EightIndent.ContinuationIndentWidth = 8;
8238   verifyFormat("void f() {\n"
8239                "        someFunction();\n"
8240                "        if (true) {\n"
8241                "                f();\n"
8242                "        }\n"
8243                "}",
8244                EightIndent);
8245   verifyFormat("class X {\n"
8246                "        void f() {\n"
8247                "        }\n"
8248                "};",
8249                EightIndent);
8250   verifyFormat("int x[] = {\n"
8251                "        call(),\n"
8252                "        call()};",
8253                EightIndent);
8254 }
8255 
8256 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
8257   verifyFormat("double\n"
8258                "f();",
8259                getLLVMStyleWithColumns(8));
8260 }
8261 
8262 TEST_F(FormatTest, ConfigurableUseOfTab) {
8263   FormatStyle Tab = getLLVMStyleWithColumns(42);
8264   Tab.IndentWidth = 8;
8265   Tab.UseTab = FormatStyle::UT_Always;
8266   Tab.AlignEscapedNewlinesLeft = true;
8267 
8268   EXPECT_EQ("if (aaaaaaaa && // q\n"
8269             "    bb)\t\t// w\n"
8270             "\t;",
8271             format("if (aaaaaaaa &&// q\n"
8272                    "bb)// w\n"
8273                    ";",
8274                    Tab));
8275   EXPECT_EQ("if (aaa && bbb) // w\n"
8276             "\t;",
8277             format("if(aaa&&bbb)// w\n"
8278                    ";",
8279                    Tab));
8280 
8281   verifyFormat("class X {\n"
8282                "\tvoid f() {\n"
8283                "\t\tsomeFunction(parameter1,\n"
8284                "\t\t\t     parameter2);\n"
8285                "\t}\n"
8286                "};",
8287                Tab);
8288   verifyFormat("#define A                        \\\n"
8289                "\tvoid f() {               \\\n"
8290                "\t\tsomeFunction(    \\\n"
8291                "\t\t    parameter1,  \\\n"
8292                "\t\t    parameter2); \\\n"
8293                "\t}",
8294                Tab);
8295 
8296   Tab.TabWidth = 4;
8297   Tab.IndentWidth = 8;
8298   verifyFormat("class TabWidth4Indent8 {\n"
8299                "\t\tvoid f() {\n"
8300                "\t\t\t\tsomeFunction(parameter1,\n"
8301                "\t\t\t\t\t\t\t parameter2);\n"
8302                "\t\t}\n"
8303                "};",
8304                Tab);
8305 
8306   Tab.TabWidth = 4;
8307   Tab.IndentWidth = 4;
8308   verifyFormat("class TabWidth4Indent4 {\n"
8309                "\tvoid f() {\n"
8310                "\t\tsomeFunction(parameter1,\n"
8311                "\t\t\t\t\t parameter2);\n"
8312                "\t}\n"
8313                "};",
8314                Tab);
8315 
8316   Tab.TabWidth = 8;
8317   Tab.IndentWidth = 4;
8318   verifyFormat("class TabWidth8Indent4 {\n"
8319                "    void f() {\n"
8320                "\tsomeFunction(parameter1,\n"
8321                "\t\t     parameter2);\n"
8322                "    }\n"
8323                "};",
8324                Tab);
8325 
8326   Tab.TabWidth = 8;
8327   Tab.IndentWidth = 8;
8328   EXPECT_EQ("/*\n"
8329             "\t      a\t\tcomment\n"
8330             "\t      in multiple lines\n"
8331             "       */",
8332             format("   /*\t \t \n"
8333                    " \t \t a\t\tcomment\t \t\n"
8334                    " \t \t in multiple lines\t\n"
8335                    " \t  */",
8336                    Tab));
8337 
8338   Tab.UseTab = FormatStyle::UT_ForIndentation;
8339   verifyFormat("{\n"
8340                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8341                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8342                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8343                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8344                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8345                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8346                "};",
8347                Tab);
8348   verifyFormat("enum AA {\n"
8349                "\ta1, // Force multiple lines\n"
8350                "\ta2,\n"
8351                "\ta3\n"
8352                "};",
8353                Tab);
8354   EXPECT_EQ("if (aaaaaaaa && // q\n"
8355             "    bb)         // w\n"
8356             "\t;",
8357             format("if (aaaaaaaa &&// q\n"
8358                    "bb)// w\n"
8359                    ";",
8360                    Tab));
8361   verifyFormat("class X {\n"
8362                "\tvoid f() {\n"
8363                "\t\tsomeFunction(parameter1,\n"
8364                "\t\t             parameter2);\n"
8365                "\t}\n"
8366                "};",
8367                Tab);
8368   verifyFormat("{\n"
8369                "\tQ(\n"
8370                "\t    {\n"
8371                "\t\t    int a;\n"
8372                "\t\t    someFunction(aaaaaaaa,\n"
8373                "\t\t                 bbbbbbb);\n"
8374                "\t    },\n"
8375                "\t    p);\n"
8376                "}",
8377                Tab);
8378   EXPECT_EQ("{\n"
8379             "\t/* aaaa\n"
8380             "\t   bbbb */\n"
8381             "}",
8382             format("{\n"
8383                    "/* aaaa\n"
8384                    "   bbbb */\n"
8385                    "}",
8386                    Tab));
8387   EXPECT_EQ("{\n"
8388             "\t/*\n"
8389             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8390             "\t  bbbbbbbbbbbbb\n"
8391             "\t*/\n"
8392             "}",
8393             format("{\n"
8394                    "/*\n"
8395                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8396                    "*/\n"
8397                    "}",
8398                    Tab));
8399   EXPECT_EQ("{\n"
8400             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8401             "\t// bbbbbbbbbbbbb\n"
8402             "}",
8403             format("{\n"
8404                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8405                    "}",
8406                    Tab));
8407   EXPECT_EQ("{\n"
8408             "\t/*\n"
8409             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8410             "\t  bbbbbbbbbbbbb\n"
8411             "\t*/\n"
8412             "}",
8413             format("{\n"
8414                    "\t/*\n"
8415                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8416                    "\t*/\n"
8417                    "}",
8418                    Tab));
8419   EXPECT_EQ("{\n"
8420             "\t/*\n"
8421             "\n"
8422             "\t*/\n"
8423             "}",
8424             format("{\n"
8425                    "\t/*\n"
8426                    "\n"
8427                    "\t*/\n"
8428                    "}",
8429                    Tab));
8430   EXPECT_EQ("{\n"
8431             "\t/*\n"
8432             " asdf\n"
8433             "\t*/\n"
8434             "}",
8435             format("{\n"
8436                    "\t/*\n"
8437                    " asdf\n"
8438                    "\t*/\n"
8439                    "}",
8440                    Tab));
8441 
8442   Tab.UseTab = FormatStyle::UT_Never;
8443   EXPECT_EQ("/*\n"
8444             "              a\t\tcomment\n"
8445             "              in multiple lines\n"
8446             "       */",
8447             format("   /*\t \t \n"
8448                    " \t \t a\t\tcomment\t \t\n"
8449                    " \t \t in multiple lines\t\n"
8450                    " \t  */",
8451                    Tab));
8452   EXPECT_EQ("/* some\n"
8453             "   comment */",
8454             format(" \t \t /* some\n"
8455                    " \t \t    comment */",
8456                    Tab));
8457   EXPECT_EQ("int a; /* some\n"
8458             "   comment */",
8459             format(" \t \t int a; /* some\n"
8460                    " \t \t    comment */",
8461                    Tab));
8462 
8463   EXPECT_EQ("int a; /* some\n"
8464             "comment */",
8465             format(" \t \t int\ta; /* some\n"
8466                    " \t \t    comment */",
8467                    Tab));
8468   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8469             "    comment */",
8470             format(" \t \t f(\"\t\t\"); /* some\n"
8471                    " \t \t    comment */",
8472                    Tab));
8473   EXPECT_EQ("{\n"
8474             "  /*\n"
8475             "   * Comment\n"
8476             "   */\n"
8477             "  int i;\n"
8478             "}",
8479             format("{\n"
8480                    "\t/*\n"
8481                    "\t * Comment\n"
8482                    "\t */\n"
8483                    "\t int i;\n"
8484                    "}"));
8485 }
8486 
8487 TEST_F(FormatTest, CalculatesOriginalColumn) {
8488   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8489             "q\"; /* some\n"
8490             "       comment */",
8491             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8492                    "q\"; /* some\n"
8493                    "       comment */",
8494                    getLLVMStyle()));
8495   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8496             "/* some\n"
8497             "   comment */",
8498             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8499                    " /* some\n"
8500                    "    comment */",
8501                    getLLVMStyle()));
8502   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8503             "qqq\n"
8504             "/* some\n"
8505             "   comment */",
8506             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8507                    "qqq\n"
8508                    " /* some\n"
8509                    "    comment */",
8510                    getLLVMStyle()));
8511   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8512             "wwww; /* some\n"
8513             "         comment */",
8514             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8515                    "wwww; /* some\n"
8516                    "         comment */",
8517                    getLLVMStyle()));
8518 }
8519 
8520 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
8521   FormatStyle NoSpace = getLLVMStyle();
8522   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
8523 
8524   verifyFormat("while(true)\n"
8525                "  continue;",
8526                NoSpace);
8527   verifyFormat("for(;;)\n"
8528                "  continue;",
8529                NoSpace);
8530   verifyFormat("if(true)\n"
8531                "  f();\n"
8532                "else if(true)\n"
8533                "  f();",
8534                NoSpace);
8535   verifyFormat("do {\n"
8536                "  do_something();\n"
8537                "} while(something());",
8538                NoSpace);
8539   verifyFormat("switch(x) {\n"
8540                "default:\n"
8541                "  break;\n"
8542                "}",
8543                NoSpace);
8544   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
8545   verifyFormat("size_t x = sizeof(x);", NoSpace);
8546   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
8547   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
8548   verifyFormat("alignas(128) char a[128];", NoSpace);
8549   verifyFormat("size_t x = alignof(MyType);", NoSpace);
8550   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
8551   verifyFormat("int f() throw(Deprecated);", NoSpace);
8552   verifyFormat("typedef void (*cb)(int);", NoSpace);
8553   verifyFormat("T A::operator()();", NoSpace);
8554   verifyFormat("X A::operator++(T);", NoSpace);
8555 
8556   FormatStyle Space = getLLVMStyle();
8557   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
8558 
8559   verifyFormat("int f ();", Space);
8560   verifyFormat("void f (int a, T b) {\n"
8561                "  while (true)\n"
8562                "    continue;\n"
8563                "}",
8564                Space);
8565   verifyFormat("if (true)\n"
8566                "  f ();\n"
8567                "else if (true)\n"
8568                "  f ();",
8569                Space);
8570   verifyFormat("do {\n"
8571                "  do_something ();\n"
8572                "} while (something ());",
8573                Space);
8574   verifyFormat("switch (x) {\n"
8575                "default:\n"
8576                "  break;\n"
8577                "}",
8578                Space);
8579   verifyFormat("A::A () : a (1) {}", Space);
8580   verifyFormat("void f () __attribute__ ((asdf));", Space);
8581   verifyFormat("*(&a + 1);\n"
8582                "&((&a)[1]);\n"
8583                "a[(b + c) * d];\n"
8584                "(((a + 1) * 2) + 3) * 4;",
8585                Space);
8586   verifyFormat("#define A(x) x", Space);
8587   verifyFormat("#define A (x) x", Space);
8588   verifyFormat("#if defined(x)\n"
8589                "#endif",
8590                Space);
8591   verifyFormat("auto i = std::make_unique<int> (5);", Space);
8592   verifyFormat("size_t x = sizeof (x);", Space);
8593   verifyFormat("auto f (int x) -> decltype (x);", Space);
8594   verifyFormat("int f (T x) noexcept (x.create ());", Space);
8595   verifyFormat("alignas (128) char a[128];", Space);
8596   verifyFormat("size_t x = alignof (MyType);", Space);
8597   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
8598   verifyFormat("int f () throw (Deprecated);", Space);
8599   verifyFormat("typedef void (*cb) (int);", Space);
8600   verifyFormat("T A::operator() ();", Space);
8601   verifyFormat("X A::operator++ (T);", Space);
8602 }
8603 
8604 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
8605   FormatStyle Spaces = getLLVMStyle();
8606 
8607   Spaces.SpacesInParentheses = true;
8608   verifyFormat("call( x, y, z );", Spaces);
8609   verifyFormat("call();", Spaces);
8610   verifyFormat("std::function<void( int, int )> callback;", Spaces);
8611   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
8612                Spaces);
8613   verifyFormat("while ( (bool)1 )\n"
8614                "  continue;",
8615                Spaces);
8616   verifyFormat("for ( ;; )\n"
8617                "  continue;",
8618                Spaces);
8619   verifyFormat("if ( true )\n"
8620                "  f();\n"
8621                "else if ( true )\n"
8622                "  f();",
8623                Spaces);
8624   verifyFormat("do {\n"
8625                "  do_something( (int)i );\n"
8626                "} while ( something() );",
8627                Spaces);
8628   verifyFormat("switch ( x ) {\n"
8629                "default:\n"
8630                "  break;\n"
8631                "}",
8632                Spaces);
8633 
8634   Spaces.SpacesInParentheses = false;
8635   Spaces.SpacesInCStyleCastParentheses = true;
8636   verifyFormat("Type *A = ( Type * )P;", Spaces);
8637   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
8638   verifyFormat("x = ( int32 )y;", Spaces);
8639   verifyFormat("int a = ( int )(2.0f);", Spaces);
8640   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
8641   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
8642   verifyFormat("#define x (( int )-1)", Spaces);
8643 
8644   // Run the first set of tests again with:
8645   Spaces.SpacesInParentheses = false, Spaces.SpaceInEmptyParentheses = true;
8646   Spaces.SpacesInCStyleCastParentheses = true;
8647   verifyFormat("call(x, y, z);", Spaces);
8648   verifyFormat("call( );", Spaces);
8649   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8650   verifyFormat("while (( bool )1)\n"
8651                "  continue;",
8652                Spaces);
8653   verifyFormat("for (;;)\n"
8654                "  continue;",
8655                Spaces);
8656   verifyFormat("if (true)\n"
8657                "  f( );\n"
8658                "else if (true)\n"
8659                "  f( );",
8660                Spaces);
8661   verifyFormat("do {\n"
8662                "  do_something(( int )i);\n"
8663                "} while (something( ));",
8664                Spaces);
8665   verifyFormat("switch (x) {\n"
8666                "default:\n"
8667                "  break;\n"
8668                "}",
8669                Spaces);
8670 
8671   // Run the first set of tests again with:
8672   Spaces.SpaceAfterCStyleCast = true;
8673   verifyFormat("call(x, y, z);", Spaces);
8674   verifyFormat("call( );", Spaces);
8675   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8676   verifyFormat("while (( bool ) 1)\n"
8677                "  continue;",
8678                Spaces);
8679   verifyFormat("for (;;)\n"
8680                "  continue;",
8681                Spaces);
8682   verifyFormat("if (true)\n"
8683                "  f( );\n"
8684                "else if (true)\n"
8685                "  f( );",
8686                Spaces);
8687   verifyFormat("do {\n"
8688                "  do_something(( int ) i);\n"
8689                "} while (something( ));",
8690                Spaces);
8691   verifyFormat("switch (x) {\n"
8692                "default:\n"
8693                "  break;\n"
8694                "}",
8695                Spaces);
8696 
8697   // Run subset of tests again with:
8698   Spaces.SpacesInCStyleCastParentheses = false;
8699   Spaces.SpaceAfterCStyleCast = true;
8700   verifyFormat("while ((bool) 1)\n"
8701                "  continue;",
8702                Spaces);
8703   verifyFormat("do {\n"
8704                "  do_something((int) i);\n"
8705                "} while (something( ));",
8706                Spaces);
8707 }
8708 
8709 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
8710   verifyFormat("int a[5];");
8711   verifyFormat("a[3] += 42;");
8712 
8713   FormatStyle Spaces = getLLVMStyle();
8714   Spaces.SpacesInSquareBrackets = true;
8715   // Lambdas unchanged.
8716   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
8717   verifyFormat("return [i, args...] {};", Spaces);
8718 
8719   // Not lambdas.
8720   verifyFormat("int a[ 5 ];", Spaces);
8721   verifyFormat("a[ 3 ] += 42;", Spaces);
8722   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
8723   verifyFormat("double &operator[](int i) { return 0; }\n"
8724                "int i;",
8725                Spaces);
8726   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
8727   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
8728   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
8729 }
8730 
8731 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
8732   verifyFormat("int a = 5;");
8733   verifyFormat("a += 42;");
8734   verifyFormat("a or_eq 8;");
8735 
8736   FormatStyle Spaces = getLLVMStyle();
8737   Spaces.SpaceBeforeAssignmentOperators = false;
8738   verifyFormat("int a= 5;", Spaces);
8739   verifyFormat("a+= 42;", Spaces);
8740   verifyFormat("a or_eq 8;", Spaces);
8741 }
8742 
8743 TEST_F(FormatTest, AlignConsecutiveAssignments) {
8744   FormatStyle Alignment = getLLVMStyle();
8745   Alignment.AlignConsecutiveAssignments = false;
8746   verifyFormat("int a = 5;\n"
8747                "int oneTwoThree = 123;",
8748                Alignment);
8749   verifyFormat("int a = 5;\n"
8750                "int oneTwoThree = 123;",
8751                Alignment);
8752 
8753   Alignment.AlignConsecutiveAssignments = true;
8754   verifyFormat("int a           = 5;\n"
8755                "int oneTwoThree = 123;",
8756                Alignment);
8757   verifyFormat("int a           = method();\n"
8758                "int oneTwoThree = 133;",
8759                Alignment);
8760   verifyFormat("a &= 5;\n"
8761                "bcd *= 5;\n"
8762                "ghtyf += 5;\n"
8763                "dvfvdb -= 5;\n"
8764                "a /= 5;\n"
8765                "vdsvsv %= 5;\n"
8766                "sfdbddfbdfbb ^= 5;\n"
8767                "dvsdsv |= 5;\n"
8768                "int dsvvdvsdvvv = 123;",
8769                Alignment);
8770   verifyFormat("int i = 1, j = 10;\n"
8771                "something = 2000;",
8772                Alignment);
8773   verifyFormat("something = 2000;\n"
8774                "int i = 1, j = 10;\n",
8775                Alignment);
8776   verifyFormat("something = 2000;\n"
8777                "another   = 911;\n"
8778                "int i = 1, j = 10;\n"
8779                "oneMore = 1;\n"
8780                "i       = 2;",
8781                Alignment);
8782   verifyFormat("int a   = 5;\n"
8783                "int one = 1;\n"
8784                "method();\n"
8785                "int oneTwoThree = 123;\n"
8786                "int oneTwo      = 12;",
8787                Alignment);
8788   verifyFormat("int oneTwoThree = 123;\n"
8789                "int oneTwo      = 12;\n"
8790                "method();\n",
8791                Alignment);
8792   verifyFormat("int oneTwoThree = 123; // comment\n"
8793                "int oneTwo      = 12;  // comment",
8794                Alignment);
8795   EXPECT_EQ("int a = 5;\n"
8796             "\n"
8797             "int oneTwoThree = 123;",
8798             format("int a       = 5;\n"
8799                    "\n"
8800                    "int oneTwoThree= 123;",
8801                    Alignment));
8802   EXPECT_EQ("int a   = 5;\n"
8803             "int one = 1;\n"
8804             "\n"
8805             "int oneTwoThree = 123;",
8806             format("int a = 5;\n"
8807                    "int one = 1;\n"
8808                    "\n"
8809                    "int oneTwoThree = 123;",
8810                    Alignment));
8811   EXPECT_EQ("int a   = 5;\n"
8812             "int one = 1;\n"
8813             "\n"
8814             "int oneTwoThree = 123;\n"
8815             "int oneTwo      = 12;",
8816             format("int a = 5;\n"
8817                    "int one = 1;\n"
8818                    "\n"
8819                    "int oneTwoThree = 123;\n"
8820                    "int oneTwo = 12;",
8821                    Alignment));
8822   Alignment.AlignEscapedNewlinesLeft = true;
8823   verifyFormat("#define A               \\\n"
8824                "  int aaaa       = 12;  \\\n"
8825                "  int b          = 23;  \\\n"
8826                "  int ccc        = 234; \\\n"
8827                "  int dddddddddd = 2345;",
8828                Alignment);
8829   Alignment.AlignEscapedNewlinesLeft = false;
8830   verifyFormat("#define A                                                      "
8831                "                \\\n"
8832                "  int aaaa       = 12;                                         "
8833                "                \\\n"
8834                "  int b          = 23;                                         "
8835                "                \\\n"
8836                "  int ccc        = 234;                                        "
8837                "                \\\n"
8838                "  int dddddddddd = 2345;",
8839                Alignment);
8840   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
8841                "k = 4, int l = 5,\n"
8842                "                  int m = 6) {\n"
8843                "  int j      = 10;\n"
8844                "  otherThing = 1;\n"
8845                "}",
8846                Alignment);
8847   verifyFormat("void SomeFunction(int parameter = 0) {\n"
8848                "  int i   = 1;\n"
8849                "  int j   = 2;\n"
8850                "  int big = 10000;\n"
8851                "}",
8852                Alignment);
8853   verifyFormat("class C {\n"
8854                "public:\n"
8855                "  int i            = 1;\n"
8856                "  virtual void f() = 0;\n"
8857                "};",
8858                Alignment);
8859   verifyFormat("int i = 1;\n"
8860                "if (SomeType t = getSomething()) {\n"
8861                "}\n"
8862                "int j   = 2;\n"
8863                "int big = 10000;",
8864                Alignment);
8865   verifyFormat("int j = 7;\n"
8866                "for (int k = 0; k < N; ++k) {\n"
8867                "}\n"
8868                "int j   = 2;\n"
8869                "int big = 10000;\n"
8870                "}",
8871                Alignment);
8872   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8873   verifyFormat("int i = 1;\n"
8874                "LooooooooooongType loooooooooooooooooooooongVariable\n"
8875                "    = someLooooooooooooooooongFunction();\n"
8876                "int j = 2;",
8877                Alignment);
8878   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8879   verifyFormat("int i = 1;\n"
8880                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
8881                "    someLooooooooooooooooongFunction();\n"
8882                "int j = 2;",
8883                Alignment);
8884 
8885   verifyFormat("auto lambda = []() {\n"
8886                "  auto i = 0;\n"
8887                "  return 0;\n"
8888                "};\n"
8889                "int i  = 0;\n"
8890                "auto v = type{\n"
8891                "    i = 1,   //\n"
8892                "    (i = 2), //\n"
8893                "    i = 3    //\n"
8894                "};",
8895                Alignment);
8896 
8897   // FIXME: Should align all three assignments
8898   verifyFormat(
8899       "int i      = 1;\n"
8900       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
8901       "                          loooooooooooooooooooooongParameterB);\n"
8902       "int j = 2;",
8903       Alignment);
8904 
8905   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
8906                "          typename B   = very_long_type_name_1,\n"
8907                "          typename T_2 = very_long_type_name_2>\n"
8908                "auto foo() {}\n",
8909                Alignment);
8910   verifyFormat("int a, b = 1;\n"
8911                "int c  = 2;\n"
8912                "int dd = 3;\n",
8913                Alignment);
8914   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
8915                "float b[1][] = {{3.f}};\n",
8916                Alignment);
8917 }
8918 
8919 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
8920   FormatStyle Alignment = getLLVMStyle();
8921   Alignment.AlignConsecutiveDeclarations = false;
8922   verifyFormat("float const a = 5;\n"
8923                "int oneTwoThree = 123;",
8924                Alignment);
8925   verifyFormat("int a = 5;\n"
8926                "float const oneTwoThree = 123;",
8927                Alignment);
8928 
8929   Alignment.AlignConsecutiveDeclarations = true;
8930   verifyFormat("float const a = 5;\n"
8931                "int         oneTwoThree = 123;",
8932                Alignment);
8933   verifyFormat("int         a = method();\n"
8934                "float const oneTwoThree = 133;",
8935                Alignment);
8936   verifyFormat("int i = 1, j = 10;\n"
8937                "something = 2000;",
8938                Alignment);
8939   verifyFormat("something = 2000;\n"
8940                "int i = 1, j = 10;\n",
8941                Alignment);
8942   verifyFormat("float      something = 2000;\n"
8943                "double     another = 911;\n"
8944                "int        i = 1, j = 10;\n"
8945                "const int *oneMore = 1;\n"
8946                "unsigned   i = 2;",
8947                Alignment);
8948   verifyFormat("float a = 5;\n"
8949                "int   one = 1;\n"
8950                "method();\n"
8951                "const double       oneTwoThree = 123;\n"
8952                "const unsigned int oneTwo = 12;",
8953                Alignment);
8954   verifyFormat("int      oneTwoThree{0}; // comment\n"
8955                "unsigned oneTwo;         // comment",
8956                Alignment);
8957   EXPECT_EQ("float const a = 5;\n"
8958             "\n"
8959             "int oneTwoThree = 123;",
8960             format("float const   a = 5;\n"
8961                    "\n"
8962                    "int           oneTwoThree= 123;",
8963                    Alignment));
8964   EXPECT_EQ("float a = 5;\n"
8965             "int   one = 1;\n"
8966             "\n"
8967             "unsigned oneTwoThree = 123;",
8968             format("float    a = 5;\n"
8969                    "int      one = 1;\n"
8970                    "\n"
8971                    "unsigned oneTwoThree = 123;",
8972                    Alignment));
8973   EXPECT_EQ("float a = 5;\n"
8974             "int   one = 1;\n"
8975             "\n"
8976             "unsigned oneTwoThree = 123;\n"
8977             "int      oneTwo = 12;",
8978             format("float    a = 5;\n"
8979                    "int one = 1;\n"
8980                    "\n"
8981                    "unsigned oneTwoThree = 123;\n"
8982                    "int oneTwo = 12;",
8983                    Alignment));
8984   Alignment.AlignConsecutiveAssignments = true;
8985   verifyFormat("float      something = 2000;\n"
8986                "double     another   = 911;\n"
8987                "int        i = 1, j = 10;\n"
8988                "const int *oneMore = 1;\n"
8989                "unsigned   i       = 2;",
8990                Alignment);
8991   verifyFormat("int      oneTwoThree = {0}; // comment\n"
8992                "unsigned oneTwo      = 0;   // comment",
8993                Alignment);
8994   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
8995             "  int const i   = 1;\n"
8996             "  int *     j   = 2;\n"
8997             "  int       big = 10000;\n"
8998             "\n"
8999             "  unsigned oneTwoThree = 123;\n"
9000             "  int      oneTwo      = 12;\n"
9001             "  method();\n"
9002             "  float k  = 2;\n"
9003             "  int   ll = 10000;\n"
9004             "}",
9005             format("void SomeFunction(int parameter= 0) {\n"
9006                    " int const  i= 1;\n"
9007                    "  int *j=2;\n"
9008                    " int big  =  10000;\n"
9009                    "\n"
9010                    "unsigned oneTwoThree  =123;\n"
9011                    "int oneTwo = 12;\n"
9012                    "  method();\n"
9013                    "float k= 2;\n"
9014                    "int ll=10000;\n"
9015                    "}",
9016                    Alignment));
9017   Alignment.AlignConsecutiveAssignments = false;
9018   Alignment.AlignEscapedNewlinesLeft = true;
9019   verifyFormat("#define A              \\\n"
9020                "  int       aaaa = 12; \\\n"
9021                "  float     b = 23;    \\\n"
9022                "  const int ccc = 234; \\\n"
9023                "  unsigned  dddddddddd = 2345;",
9024                Alignment);
9025   Alignment.AlignEscapedNewlinesLeft = false;
9026   Alignment.ColumnLimit = 30;
9027   verifyFormat("#define A                    \\\n"
9028                "  int       aaaa = 12;       \\\n"
9029                "  float     b = 23;          \\\n"
9030                "  const int ccc = 234;       \\\n"
9031                "  int       dddddddddd = 2345;",
9032                Alignment);
9033   Alignment.ColumnLimit = 80;
9034   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
9035                "k = 4, int l = 5,\n"
9036                "                  int m = 6) {\n"
9037                "  const int j = 10;\n"
9038                "  otherThing = 1;\n"
9039                "}",
9040                Alignment);
9041   verifyFormat("void SomeFunction(int parameter = 0) {\n"
9042                "  int const i = 1;\n"
9043                "  int *     j = 2;\n"
9044                "  int       big = 10000;\n"
9045                "}",
9046                Alignment);
9047   verifyFormat("class C {\n"
9048                "public:\n"
9049                "  int          i = 1;\n"
9050                "  virtual void f() = 0;\n"
9051                "};",
9052                Alignment);
9053   verifyFormat("float i = 1;\n"
9054                "if (SomeType t = getSomething()) {\n"
9055                "}\n"
9056                "const unsigned j = 2;\n"
9057                "int            big = 10000;",
9058                Alignment);
9059   verifyFormat("float j = 7;\n"
9060                "for (int k = 0; k < N; ++k) {\n"
9061                "}\n"
9062                "unsigned j = 2;\n"
9063                "int      big = 10000;\n"
9064                "}",
9065                Alignment);
9066   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9067   verifyFormat("float              i = 1;\n"
9068                "LooooooooooongType loooooooooooooooooooooongVariable\n"
9069                "    = someLooooooooooooooooongFunction();\n"
9070                "int j = 2;",
9071                Alignment);
9072   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9073   verifyFormat("int                i = 1;\n"
9074                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
9075                "    someLooooooooooooooooongFunction();\n"
9076                "int j = 2;",
9077                Alignment);
9078 
9079   Alignment.AlignConsecutiveAssignments = true;
9080   verifyFormat("auto lambda = []() {\n"
9081                "  auto  ii = 0;\n"
9082                "  float j  = 0;\n"
9083                "  return 0;\n"
9084                "};\n"
9085                "int   i  = 0;\n"
9086                "float i2 = 0;\n"
9087                "auto  v  = type{\n"
9088                "    i = 1,   //\n"
9089                "    (i = 2), //\n"
9090                "    i = 3    //\n"
9091                "};",
9092                Alignment);
9093   Alignment.AlignConsecutiveAssignments = false;
9094 
9095   // FIXME: Should align all three declarations
9096   verifyFormat(
9097       "int      i = 1;\n"
9098       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
9099       "                          loooooooooooooooooooooongParameterB);\n"
9100       "int j = 2;",
9101       Alignment);
9102 
9103   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
9104   // We expect declarations and assignments to align, as long as it doesn't
9105   // exceed the column limit, starting a new alignemnt sequence whenever it
9106   // happens.
9107   Alignment.AlignConsecutiveAssignments = true;
9108   Alignment.ColumnLimit = 30;
9109   verifyFormat("float    ii              = 1;\n"
9110                "unsigned j               = 2;\n"
9111                "int someVerylongVariable = 1;\n"
9112                "AnotherLongType  ll = 123456;\n"
9113                "VeryVeryLongType k  = 2;\n"
9114                "int              myvar = 1;",
9115                Alignment);
9116   Alignment.ColumnLimit = 80;
9117   Alignment.AlignConsecutiveAssignments = false;
9118 
9119   verifyFormat(
9120       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
9121       "          typename LongType, typename B>\n"
9122       "auto foo() {}\n",
9123       Alignment);
9124   verifyFormat("float a, b = 1;\n"
9125                "int   c = 2;\n"
9126                "int   dd = 3;\n",
9127                Alignment);
9128   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
9129                "float b[1][] = {{3.f}};\n",
9130                Alignment);
9131   Alignment.AlignConsecutiveAssignments = true;
9132   verifyFormat("float a, b = 1;\n"
9133                "int   c  = 2;\n"
9134                "int   dd = 3;\n",
9135                Alignment);
9136   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
9137                "float b[1][] = {{3.f}};\n",
9138                Alignment);
9139   Alignment.AlignConsecutiveAssignments = false;
9140 
9141   Alignment.ColumnLimit = 30;
9142   Alignment.BinPackParameters = false;
9143   verifyFormat("void foo(float     a,\n"
9144                "         float     b,\n"
9145                "         int       c,\n"
9146                "         uint32_t *d) {\n"
9147                "  int *  e = 0;\n"
9148                "  float  f = 0;\n"
9149                "  double g = 0;\n"
9150                "}\n"
9151                "void bar(ino_t     a,\n"
9152                "         int       b,\n"
9153                "         uint32_t *c,\n"
9154                "         bool      d) {}\n",
9155                Alignment);
9156   Alignment.BinPackParameters = true;
9157   Alignment.ColumnLimit = 80;
9158 }
9159 
9160 TEST_F(FormatTest, LinuxBraceBreaking) {
9161   FormatStyle LinuxBraceStyle = getLLVMStyle();
9162   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
9163   verifyFormat("namespace a\n"
9164                "{\n"
9165                "class A\n"
9166                "{\n"
9167                "  void f()\n"
9168                "  {\n"
9169                "    if (true) {\n"
9170                "      a();\n"
9171                "      b();\n"
9172                "    } else {\n"
9173                "      a();\n"
9174                "    }\n"
9175                "  }\n"
9176                "  void g() { return; }\n"
9177                "};\n"
9178                "struct B {\n"
9179                "  int x;\n"
9180                "};\n"
9181                "}\n",
9182                LinuxBraceStyle);
9183   verifyFormat("enum X {\n"
9184                "  Y = 0,\n"
9185                "}\n",
9186                LinuxBraceStyle);
9187   verifyFormat("struct S {\n"
9188                "  int Type;\n"
9189                "  union {\n"
9190                "    int x;\n"
9191                "    double y;\n"
9192                "  } Value;\n"
9193                "  class C\n"
9194                "  {\n"
9195                "    MyFavoriteType Value;\n"
9196                "  } Class;\n"
9197                "}\n",
9198                LinuxBraceStyle);
9199 }
9200 
9201 TEST_F(FormatTest, MozillaBraceBreaking) {
9202   FormatStyle MozillaBraceStyle = getLLVMStyle();
9203   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
9204   verifyFormat("namespace a {\n"
9205                "class A\n"
9206                "{\n"
9207                "  void f()\n"
9208                "  {\n"
9209                "    if (true) {\n"
9210                "      a();\n"
9211                "      b();\n"
9212                "    }\n"
9213                "  }\n"
9214                "  void g() { return; }\n"
9215                "};\n"
9216                "enum E\n"
9217                "{\n"
9218                "  A,\n"
9219                "  // foo\n"
9220                "  B,\n"
9221                "  C\n"
9222                "};\n"
9223                "struct B\n"
9224                "{\n"
9225                "  int x;\n"
9226                "};\n"
9227                "}\n",
9228                MozillaBraceStyle);
9229   verifyFormat("struct S\n"
9230                "{\n"
9231                "  int Type;\n"
9232                "  union\n"
9233                "  {\n"
9234                "    int x;\n"
9235                "    double y;\n"
9236                "  } Value;\n"
9237                "  class C\n"
9238                "  {\n"
9239                "    MyFavoriteType Value;\n"
9240                "  } Class;\n"
9241                "}\n",
9242                MozillaBraceStyle);
9243 }
9244 
9245 TEST_F(FormatTest, StroustrupBraceBreaking) {
9246   FormatStyle StroustrupBraceStyle = getLLVMStyle();
9247   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9248   verifyFormat("namespace a {\n"
9249                "class A {\n"
9250                "  void f()\n"
9251                "  {\n"
9252                "    if (true) {\n"
9253                "      a();\n"
9254                "      b();\n"
9255                "    }\n"
9256                "  }\n"
9257                "  void g() { return; }\n"
9258                "};\n"
9259                "struct B {\n"
9260                "  int x;\n"
9261                "};\n"
9262                "}\n",
9263                StroustrupBraceStyle);
9264 
9265   verifyFormat("void foo()\n"
9266                "{\n"
9267                "  if (a) {\n"
9268                "    a();\n"
9269                "  }\n"
9270                "  else {\n"
9271                "    b();\n"
9272                "  }\n"
9273                "}\n",
9274                StroustrupBraceStyle);
9275 
9276   verifyFormat("#ifdef _DEBUG\n"
9277                "int foo(int i = 0)\n"
9278                "#else\n"
9279                "int foo(int i = 5)\n"
9280                "#endif\n"
9281                "{\n"
9282                "  return i;\n"
9283                "}",
9284                StroustrupBraceStyle);
9285 
9286   verifyFormat("void foo() {}\n"
9287                "void bar()\n"
9288                "#ifdef _DEBUG\n"
9289                "{\n"
9290                "  foo();\n"
9291                "}\n"
9292                "#else\n"
9293                "{\n"
9294                "}\n"
9295                "#endif",
9296                StroustrupBraceStyle);
9297 
9298   verifyFormat("void foobar() { int i = 5; }\n"
9299                "#ifdef _DEBUG\n"
9300                "void bar() {}\n"
9301                "#else\n"
9302                "void bar() { foobar(); }\n"
9303                "#endif",
9304                StroustrupBraceStyle);
9305 }
9306 
9307 TEST_F(FormatTest, AllmanBraceBreaking) {
9308   FormatStyle AllmanBraceStyle = getLLVMStyle();
9309   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
9310   verifyFormat("namespace a\n"
9311                "{\n"
9312                "class A\n"
9313                "{\n"
9314                "  void f()\n"
9315                "  {\n"
9316                "    if (true)\n"
9317                "    {\n"
9318                "      a();\n"
9319                "      b();\n"
9320                "    }\n"
9321                "  }\n"
9322                "  void g() { return; }\n"
9323                "};\n"
9324                "struct B\n"
9325                "{\n"
9326                "  int x;\n"
9327                "};\n"
9328                "}",
9329                AllmanBraceStyle);
9330 
9331   verifyFormat("void f()\n"
9332                "{\n"
9333                "  if (true)\n"
9334                "  {\n"
9335                "    a();\n"
9336                "  }\n"
9337                "  else if (false)\n"
9338                "  {\n"
9339                "    b();\n"
9340                "  }\n"
9341                "  else\n"
9342                "  {\n"
9343                "    c();\n"
9344                "  }\n"
9345                "}\n",
9346                AllmanBraceStyle);
9347 
9348   verifyFormat("void f()\n"
9349                "{\n"
9350                "  for (int i = 0; i < 10; ++i)\n"
9351                "  {\n"
9352                "    a();\n"
9353                "  }\n"
9354                "  while (false)\n"
9355                "  {\n"
9356                "    b();\n"
9357                "  }\n"
9358                "  do\n"
9359                "  {\n"
9360                "    c();\n"
9361                "  } while (false)\n"
9362                "}\n",
9363                AllmanBraceStyle);
9364 
9365   verifyFormat("void f(int a)\n"
9366                "{\n"
9367                "  switch (a)\n"
9368                "  {\n"
9369                "  case 0:\n"
9370                "    break;\n"
9371                "  case 1:\n"
9372                "  {\n"
9373                "    break;\n"
9374                "  }\n"
9375                "  case 2:\n"
9376                "  {\n"
9377                "  }\n"
9378                "  break;\n"
9379                "  default:\n"
9380                "    break;\n"
9381                "  }\n"
9382                "}\n",
9383                AllmanBraceStyle);
9384 
9385   verifyFormat("enum X\n"
9386                "{\n"
9387                "  Y = 0,\n"
9388                "}\n",
9389                AllmanBraceStyle);
9390   verifyFormat("enum X\n"
9391                "{\n"
9392                "  Y = 0\n"
9393                "}\n",
9394                AllmanBraceStyle);
9395 
9396   verifyFormat("@interface BSApplicationController ()\n"
9397                "{\n"
9398                "@private\n"
9399                "  id _extraIvar;\n"
9400                "}\n"
9401                "@end\n",
9402                AllmanBraceStyle);
9403 
9404   verifyFormat("#ifdef _DEBUG\n"
9405                "int foo(int i = 0)\n"
9406                "#else\n"
9407                "int foo(int i = 5)\n"
9408                "#endif\n"
9409                "{\n"
9410                "  return i;\n"
9411                "}",
9412                AllmanBraceStyle);
9413 
9414   verifyFormat("void foo() {}\n"
9415                "void bar()\n"
9416                "#ifdef _DEBUG\n"
9417                "{\n"
9418                "  foo();\n"
9419                "}\n"
9420                "#else\n"
9421                "{\n"
9422                "}\n"
9423                "#endif",
9424                AllmanBraceStyle);
9425 
9426   verifyFormat("void foobar() { int i = 5; }\n"
9427                "#ifdef _DEBUG\n"
9428                "void bar() {}\n"
9429                "#else\n"
9430                "void bar() { foobar(); }\n"
9431                "#endif",
9432                AllmanBraceStyle);
9433 
9434   // This shouldn't affect ObjC blocks..
9435   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
9436                "  // ...\n"
9437                "  int i;\n"
9438                "}];",
9439                AllmanBraceStyle);
9440   verifyFormat("void (^block)(void) = ^{\n"
9441                "  // ...\n"
9442                "  int i;\n"
9443                "};",
9444                AllmanBraceStyle);
9445   // .. or dict literals.
9446   verifyFormat("void f()\n"
9447                "{\n"
9448                "  [object someMethod:@{ @\"a\" : @\"b\" }];\n"
9449                "}",
9450                AllmanBraceStyle);
9451   verifyFormat("int f()\n"
9452                "{ // comment\n"
9453                "  return 42;\n"
9454                "}",
9455                AllmanBraceStyle);
9456 
9457   AllmanBraceStyle.ColumnLimit = 19;
9458   verifyFormat("void f() { int i; }", AllmanBraceStyle);
9459   AllmanBraceStyle.ColumnLimit = 18;
9460   verifyFormat("void f()\n"
9461                "{\n"
9462                "  int i;\n"
9463                "}",
9464                AllmanBraceStyle);
9465   AllmanBraceStyle.ColumnLimit = 80;
9466 
9467   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
9468   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
9469   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
9470   verifyFormat("void f(bool b)\n"
9471                "{\n"
9472                "  if (b)\n"
9473                "  {\n"
9474                "    return;\n"
9475                "  }\n"
9476                "}\n",
9477                BreakBeforeBraceShortIfs);
9478   verifyFormat("void f(bool b)\n"
9479                "{\n"
9480                "  if (b) return;\n"
9481                "}\n",
9482                BreakBeforeBraceShortIfs);
9483   verifyFormat("void f(bool b)\n"
9484                "{\n"
9485                "  while (b)\n"
9486                "  {\n"
9487                "    return;\n"
9488                "  }\n"
9489                "}\n",
9490                BreakBeforeBraceShortIfs);
9491 }
9492 
9493 TEST_F(FormatTest, GNUBraceBreaking) {
9494   FormatStyle GNUBraceStyle = getLLVMStyle();
9495   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
9496   verifyFormat("namespace a\n"
9497                "{\n"
9498                "class A\n"
9499                "{\n"
9500                "  void f()\n"
9501                "  {\n"
9502                "    int a;\n"
9503                "    {\n"
9504                "      int b;\n"
9505                "    }\n"
9506                "    if (true)\n"
9507                "      {\n"
9508                "        a();\n"
9509                "        b();\n"
9510                "      }\n"
9511                "  }\n"
9512                "  void g() { return; }\n"
9513                "}\n"
9514                "}",
9515                GNUBraceStyle);
9516 
9517   verifyFormat("void f()\n"
9518                "{\n"
9519                "  if (true)\n"
9520                "    {\n"
9521                "      a();\n"
9522                "    }\n"
9523                "  else if (false)\n"
9524                "    {\n"
9525                "      b();\n"
9526                "    }\n"
9527                "  else\n"
9528                "    {\n"
9529                "      c();\n"
9530                "    }\n"
9531                "}\n",
9532                GNUBraceStyle);
9533 
9534   verifyFormat("void f()\n"
9535                "{\n"
9536                "  for (int i = 0; i < 10; ++i)\n"
9537                "    {\n"
9538                "      a();\n"
9539                "    }\n"
9540                "  while (false)\n"
9541                "    {\n"
9542                "      b();\n"
9543                "    }\n"
9544                "  do\n"
9545                "    {\n"
9546                "      c();\n"
9547                "    }\n"
9548                "  while (false);\n"
9549                "}\n",
9550                GNUBraceStyle);
9551 
9552   verifyFormat("void f(int a)\n"
9553                "{\n"
9554                "  switch (a)\n"
9555                "    {\n"
9556                "    case 0:\n"
9557                "      break;\n"
9558                "    case 1:\n"
9559                "      {\n"
9560                "        break;\n"
9561                "      }\n"
9562                "    case 2:\n"
9563                "      {\n"
9564                "      }\n"
9565                "      break;\n"
9566                "    default:\n"
9567                "      break;\n"
9568                "    }\n"
9569                "}\n",
9570                GNUBraceStyle);
9571 
9572   verifyFormat("enum X\n"
9573                "{\n"
9574                "  Y = 0,\n"
9575                "}\n",
9576                GNUBraceStyle);
9577 
9578   verifyFormat("@interface BSApplicationController ()\n"
9579                "{\n"
9580                "@private\n"
9581                "  id _extraIvar;\n"
9582                "}\n"
9583                "@end\n",
9584                GNUBraceStyle);
9585 
9586   verifyFormat("#ifdef _DEBUG\n"
9587                "int foo(int i = 0)\n"
9588                "#else\n"
9589                "int foo(int i = 5)\n"
9590                "#endif\n"
9591                "{\n"
9592                "  return i;\n"
9593                "}",
9594                GNUBraceStyle);
9595 
9596   verifyFormat("void foo() {}\n"
9597                "void bar()\n"
9598                "#ifdef _DEBUG\n"
9599                "{\n"
9600                "  foo();\n"
9601                "}\n"
9602                "#else\n"
9603                "{\n"
9604                "}\n"
9605                "#endif",
9606                GNUBraceStyle);
9607 
9608   verifyFormat("void foobar() { int i = 5; }\n"
9609                "#ifdef _DEBUG\n"
9610                "void bar() {}\n"
9611                "#else\n"
9612                "void bar() { foobar(); }\n"
9613                "#endif",
9614                GNUBraceStyle);
9615 }
9616 
9617 TEST_F(FormatTest, WebKitBraceBreaking) {
9618   FormatStyle WebKitBraceStyle = getLLVMStyle();
9619   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
9620   verifyFormat("namespace a {\n"
9621                "class A {\n"
9622                "  void f()\n"
9623                "  {\n"
9624                "    if (true) {\n"
9625                "      a();\n"
9626                "      b();\n"
9627                "    }\n"
9628                "  }\n"
9629                "  void g() { return; }\n"
9630                "};\n"
9631                "enum E {\n"
9632                "  A,\n"
9633                "  // foo\n"
9634                "  B,\n"
9635                "  C\n"
9636                "};\n"
9637                "struct B {\n"
9638                "  int x;\n"
9639                "};\n"
9640                "}\n",
9641                WebKitBraceStyle);
9642   verifyFormat("struct S {\n"
9643                "  int Type;\n"
9644                "  union {\n"
9645                "    int x;\n"
9646                "    double y;\n"
9647                "  } Value;\n"
9648                "  class C {\n"
9649                "    MyFavoriteType Value;\n"
9650                "  } Class;\n"
9651                "};\n",
9652                WebKitBraceStyle);
9653 }
9654 
9655 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
9656   verifyFormat("void f() {\n"
9657                "  try {\n"
9658                "  } catch (const Exception &e) {\n"
9659                "  }\n"
9660                "}\n",
9661                getLLVMStyle());
9662 }
9663 
9664 TEST_F(FormatTest, UnderstandsPragmas) {
9665   verifyFormat("#pragma omp reduction(| : var)");
9666   verifyFormat("#pragma omp reduction(+ : var)");
9667 
9668   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
9669             "(including parentheses).",
9670             format("#pragma    mark   Any non-hyphenated or hyphenated string "
9671                    "(including parentheses)."));
9672 }
9673 
9674 TEST_F(FormatTest, UnderstandPragmaOption) {
9675   verifyFormat("#pragma option -C -A");
9676 
9677   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
9678 }
9679 
9680 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
9681   for (size_t i = 1; i < Styles.size(); ++i)                                   \
9682   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
9683                                   << " differs from Style #0"
9684 
9685 TEST_F(FormatTest, GetsPredefinedStyleByName) {
9686   SmallVector<FormatStyle, 3> Styles;
9687   Styles.resize(3);
9688 
9689   Styles[0] = getLLVMStyle();
9690   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
9691   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
9692   EXPECT_ALL_STYLES_EQUAL(Styles);
9693 
9694   Styles[0] = getGoogleStyle();
9695   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
9696   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
9697   EXPECT_ALL_STYLES_EQUAL(Styles);
9698 
9699   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9700   EXPECT_TRUE(
9701       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
9702   EXPECT_TRUE(
9703       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
9704   EXPECT_ALL_STYLES_EQUAL(Styles);
9705 
9706   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
9707   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
9708   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
9709   EXPECT_ALL_STYLES_EQUAL(Styles);
9710 
9711   Styles[0] = getMozillaStyle();
9712   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
9713   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
9714   EXPECT_ALL_STYLES_EQUAL(Styles);
9715 
9716   Styles[0] = getWebKitStyle();
9717   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
9718   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
9719   EXPECT_ALL_STYLES_EQUAL(Styles);
9720 
9721   Styles[0] = getGNUStyle();
9722   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
9723   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
9724   EXPECT_ALL_STYLES_EQUAL(Styles);
9725 
9726   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
9727 }
9728 
9729 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
9730   SmallVector<FormatStyle, 8> Styles;
9731   Styles.resize(2);
9732 
9733   Styles[0] = getGoogleStyle();
9734   Styles[1] = getLLVMStyle();
9735   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9736   EXPECT_ALL_STYLES_EQUAL(Styles);
9737 
9738   Styles.resize(5);
9739   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9740   Styles[1] = getLLVMStyle();
9741   Styles[1].Language = FormatStyle::LK_JavaScript;
9742   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9743 
9744   Styles[2] = getLLVMStyle();
9745   Styles[2].Language = FormatStyle::LK_JavaScript;
9746   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
9747                                   "BasedOnStyle: Google",
9748                                   &Styles[2])
9749                    .value());
9750 
9751   Styles[3] = getLLVMStyle();
9752   Styles[3].Language = FormatStyle::LK_JavaScript;
9753   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
9754                                   "Language: JavaScript",
9755                                   &Styles[3])
9756                    .value());
9757 
9758   Styles[4] = getLLVMStyle();
9759   Styles[4].Language = FormatStyle::LK_JavaScript;
9760   EXPECT_EQ(0, parseConfiguration("---\n"
9761                                   "BasedOnStyle: LLVM\n"
9762                                   "IndentWidth: 123\n"
9763                                   "---\n"
9764                                   "BasedOnStyle: Google\n"
9765                                   "Language: JavaScript",
9766                                   &Styles[4])
9767                    .value());
9768   EXPECT_ALL_STYLES_EQUAL(Styles);
9769 }
9770 
9771 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
9772   Style.FIELD = false;                                                         \
9773   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
9774   EXPECT_TRUE(Style.FIELD);                                                    \
9775   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
9776   EXPECT_FALSE(Style.FIELD);
9777 
9778 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
9779 
9780 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
9781   Style.STRUCT.FIELD = false;                                                  \
9782   EXPECT_EQ(0,                                                                 \
9783             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
9784                 .value());                                                     \
9785   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
9786   EXPECT_EQ(0,                                                                 \
9787             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
9788                 .value());                                                     \
9789   EXPECT_FALSE(Style.STRUCT.FIELD);
9790 
9791 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
9792   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
9793 
9794 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
9795   EXPECT_NE(VALUE, Style.FIELD);                                               \
9796   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
9797   EXPECT_EQ(VALUE, Style.FIELD)
9798 
9799 TEST_F(FormatTest, ParsesConfigurationBools) {
9800   FormatStyle Style = {};
9801   Style.Language = FormatStyle::LK_Cpp;
9802   CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
9803   CHECK_PARSE_BOOL(AlignOperands);
9804   CHECK_PARSE_BOOL(AlignTrailingComments);
9805   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
9806   CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
9807   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
9808   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
9809   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
9810   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
9811   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
9812   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
9813   CHECK_PARSE_BOOL(BinPackArguments);
9814   CHECK_PARSE_BOOL(BinPackParameters);
9815   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
9816   CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
9817   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
9818   CHECK_PARSE_BOOL(DerivePointerAlignment);
9819   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
9820   CHECK_PARSE_BOOL(DisableFormat);
9821   CHECK_PARSE_BOOL(IndentCaseLabels);
9822   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
9823   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
9824   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
9825   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
9826   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
9827   CHECK_PARSE_BOOL(ReflowComments);
9828   CHECK_PARSE_BOOL(SortIncludes);
9829   CHECK_PARSE_BOOL(SpacesInParentheses);
9830   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
9831   CHECK_PARSE_BOOL(SpacesInAngles);
9832   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
9833   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
9834   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
9835   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
9836   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
9837 
9838   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
9839   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
9840   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
9841   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
9842   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
9843   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
9844   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
9845   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
9846   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
9847   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
9848   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
9849 }
9850 
9851 #undef CHECK_PARSE_BOOL
9852 
9853 TEST_F(FormatTest, ParsesConfiguration) {
9854   FormatStyle Style = {};
9855   Style.Language = FormatStyle::LK_Cpp;
9856   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
9857   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
9858               ConstructorInitializerIndentWidth, 1234u);
9859   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
9860   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
9861   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
9862   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
9863               PenaltyBreakBeforeFirstCallParameter, 1234u);
9864   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
9865   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
9866               PenaltyReturnTypeOnItsOwnLine, 1234u);
9867   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
9868               SpacesBeforeTrailingComments, 1234u);
9869   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
9870   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
9871 
9872   Style.PointerAlignment = FormatStyle::PAS_Middle;
9873   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
9874               FormatStyle::PAS_Left);
9875   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
9876               FormatStyle::PAS_Right);
9877   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
9878               FormatStyle::PAS_Middle);
9879   // For backward compatibility:
9880   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
9881               FormatStyle::PAS_Left);
9882   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
9883               FormatStyle::PAS_Right);
9884   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
9885               FormatStyle::PAS_Middle);
9886 
9887   Style.Standard = FormatStyle::LS_Auto;
9888   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
9889   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
9890   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
9891   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
9892   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
9893 
9894   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9895   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
9896               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
9897   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
9898               FormatStyle::BOS_None);
9899   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
9900               FormatStyle::BOS_All);
9901   // For backward compatibility:
9902   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
9903               FormatStyle::BOS_None);
9904   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
9905               FormatStyle::BOS_All);
9906 
9907   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9908   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
9909               FormatStyle::BAS_Align);
9910   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
9911               FormatStyle::BAS_DontAlign);
9912   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
9913               FormatStyle::BAS_AlwaysBreak);
9914   // For backward compatibility:
9915   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
9916               FormatStyle::BAS_DontAlign);
9917   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
9918               FormatStyle::BAS_Align);
9919 
9920   Style.UseTab = FormatStyle::UT_ForIndentation;
9921   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
9922   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
9923   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
9924   // For backward compatibility:
9925   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
9926   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
9927 
9928   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
9929   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
9930               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9931   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
9932               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
9933   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
9934               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
9935   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
9936               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9937   // For backward compatibility:
9938   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
9939               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9940   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
9941               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9942 
9943   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
9944   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
9945               FormatStyle::SBPO_Never);
9946   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
9947               FormatStyle::SBPO_Always);
9948   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
9949               FormatStyle::SBPO_ControlStatements);
9950   // For backward compatibility:
9951   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
9952               FormatStyle::SBPO_Never);
9953   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
9954               FormatStyle::SBPO_ControlStatements);
9955 
9956   Style.ColumnLimit = 123;
9957   FormatStyle BaseStyle = getLLVMStyle();
9958   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
9959   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
9960 
9961   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9962   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
9963               FormatStyle::BS_Attach);
9964   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
9965               FormatStyle::BS_Linux);
9966   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
9967               FormatStyle::BS_Mozilla);
9968   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
9969               FormatStyle::BS_Stroustrup);
9970   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
9971               FormatStyle::BS_Allman);
9972   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
9973   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
9974               FormatStyle::BS_WebKit);
9975   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
9976               FormatStyle::BS_Custom);
9977 
9978   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
9979   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
9980               FormatStyle::RTBS_None);
9981   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
9982               FormatStyle::RTBS_All);
9983   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
9984               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
9985   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
9986               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
9987   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
9988               AlwaysBreakAfterReturnType,
9989               FormatStyle::RTBS_TopLevelDefinitions);
9990 
9991   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
9992   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
9993               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
9994   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
9995               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
9996   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
9997               AlwaysBreakAfterDefinitionReturnType,
9998               FormatStyle::DRTBS_TopLevel);
9999 
10000   Style.NamespaceIndentation = FormatStyle::NI_All;
10001   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
10002               FormatStyle::NI_None);
10003   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
10004               FormatStyle::NI_Inner);
10005   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
10006               FormatStyle::NI_All);
10007 
10008   // FIXME: This is required because parsing a configuration simply overwrites
10009   // the first N elements of the list instead of resetting it.
10010   Style.ForEachMacros.clear();
10011   std::vector<std::string> BoostForeach;
10012   BoostForeach.push_back("BOOST_FOREACH");
10013   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
10014   std::vector<std::string> BoostAndQForeach;
10015   BoostAndQForeach.push_back("BOOST_FOREACH");
10016   BoostAndQForeach.push_back("Q_FOREACH");
10017   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
10018               BoostAndQForeach);
10019 
10020   Style.IncludeCategories.clear();
10021   std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
10022                                                                   {".*", 1}};
10023   CHECK_PARSE("IncludeCategories:\n"
10024               "  - Regex: abc/.*\n"
10025               "    Priority: 2\n"
10026               "  - Regex: .*\n"
10027               "    Priority: 1",
10028               IncludeCategories, ExpectedCategories);
10029 }
10030 
10031 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
10032   FormatStyle Style = {};
10033   Style.Language = FormatStyle::LK_Cpp;
10034   CHECK_PARSE("Language: Cpp\n"
10035               "IndentWidth: 12",
10036               IndentWidth, 12u);
10037   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
10038                                "IndentWidth: 34",
10039                                &Style),
10040             ParseError::Unsuitable);
10041   EXPECT_EQ(12u, Style.IndentWidth);
10042   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
10043   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
10044 
10045   Style.Language = FormatStyle::LK_JavaScript;
10046   CHECK_PARSE("Language: JavaScript\n"
10047               "IndentWidth: 12",
10048               IndentWidth, 12u);
10049   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
10050   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
10051                                "IndentWidth: 34",
10052                                &Style),
10053             ParseError::Unsuitable);
10054   EXPECT_EQ(23u, Style.IndentWidth);
10055   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
10056   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
10057 
10058   CHECK_PARSE("BasedOnStyle: LLVM\n"
10059               "IndentWidth: 67",
10060               IndentWidth, 67u);
10061 
10062   CHECK_PARSE("---\n"
10063               "Language: JavaScript\n"
10064               "IndentWidth: 12\n"
10065               "---\n"
10066               "Language: Cpp\n"
10067               "IndentWidth: 34\n"
10068               "...\n",
10069               IndentWidth, 12u);
10070 
10071   Style.Language = FormatStyle::LK_Cpp;
10072   CHECK_PARSE("---\n"
10073               "Language: JavaScript\n"
10074               "IndentWidth: 12\n"
10075               "---\n"
10076               "Language: Cpp\n"
10077               "IndentWidth: 34\n"
10078               "...\n",
10079               IndentWidth, 34u);
10080   CHECK_PARSE("---\n"
10081               "IndentWidth: 78\n"
10082               "---\n"
10083               "Language: JavaScript\n"
10084               "IndentWidth: 56\n"
10085               "...\n",
10086               IndentWidth, 78u);
10087 
10088   Style.ColumnLimit = 123;
10089   Style.IndentWidth = 234;
10090   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
10091   Style.TabWidth = 345;
10092   EXPECT_FALSE(parseConfiguration("---\n"
10093                                   "IndentWidth: 456\n"
10094                                   "BreakBeforeBraces: Allman\n"
10095                                   "---\n"
10096                                   "Language: JavaScript\n"
10097                                   "IndentWidth: 111\n"
10098                                   "TabWidth: 111\n"
10099                                   "---\n"
10100                                   "Language: Cpp\n"
10101                                   "BreakBeforeBraces: Stroustrup\n"
10102                                   "TabWidth: 789\n"
10103                                   "...\n",
10104                                   &Style));
10105   EXPECT_EQ(123u, Style.ColumnLimit);
10106   EXPECT_EQ(456u, Style.IndentWidth);
10107   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
10108   EXPECT_EQ(789u, Style.TabWidth);
10109 
10110   EXPECT_EQ(parseConfiguration("---\n"
10111                                "Language: JavaScript\n"
10112                                "IndentWidth: 56\n"
10113                                "---\n"
10114                                "IndentWidth: 78\n"
10115                                "...\n",
10116                                &Style),
10117             ParseError::Error);
10118   EXPECT_EQ(parseConfiguration("---\n"
10119                                "Language: JavaScript\n"
10120                                "IndentWidth: 56\n"
10121                                "---\n"
10122                                "Language: JavaScript\n"
10123                                "IndentWidth: 78\n"
10124                                "...\n",
10125                                &Style),
10126             ParseError::Error);
10127 
10128   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
10129 }
10130 
10131 #undef CHECK_PARSE
10132 
10133 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
10134   FormatStyle Style = {};
10135   Style.Language = FormatStyle::LK_JavaScript;
10136   Style.BreakBeforeTernaryOperators = true;
10137   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
10138   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
10139 
10140   Style.BreakBeforeTernaryOperators = true;
10141   EXPECT_EQ(0, parseConfiguration("---\n"
10142                                   "BasedOnStyle: Google\n"
10143                                   "---\n"
10144                                   "Language: JavaScript\n"
10145                                   "IndentWidth: 76\n"
10146                                   "...\n",
10147                                   &Style)
10148                    .value());
10149   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
10150   EXPECT_EQ(76u, Style.IndentWidth);
10151   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
10152 }
10153 
10154 TEST_F(FormatTest, ConfigurationRoundTripTest) {
10155   FormatStyle Style = getLLVMStyle();
10156   std::string YAML = configurationAsText(Style);
10157   FormatStyle ParsedStyle = {};
10158   ParsedStyle.Language = FormatStyle::LK_Cpp;
10159   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
10160   EXPECT_EQ(Style, ParsedStyle);
10161 }
10162 
10163 TEST_F(FormatTest, WorksFor8bitEncodings) {
10164   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
10165             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
10166             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
10167             "\"\xef\xee\xf0\xf3...\"",
10168             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
10169                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
10170                    "\xef\xee\xf0\xf3...\"",
10171                    getLLVMStyleWithColumns(12)));
10172 }
10173 
10174 TEST_F(FormatTest, HandlesUTF8BOM) {
10175   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
10176   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
10177             format("\xef\xbb\xbf#include <iostream>"));
10178   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
10179             format("\xef\xbb\xbf\n#include <iostream>"));
10180 }
10181 
10182 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
10183 #if !defined(_MSC_VER)
10184 
10185 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
10186   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
10187                getLLVMStyleWithColumns(35));
10188   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
10189                getLLVMStyleWithColumns(31));
10190   verifyFormat("// Однажды в студёную зимнюю пору...",
10191                getLLVMStyleWithColumns(36));
10192   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
10193   verifyFormat("/* Однажды в студёную зимнюю пору... */",
10194                getLLVMStyleWithColumns(39));
10195   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
10196                getLLVMStyleWithColumns(35));
10197 }
10198 
10199 TEST_F(FormatTest, SplitsUTF8Strings) {
10200   // Non-printable characters' width is currently considered to be the length in
10201   // bytes in UTF8. The characters can be displayed in very different manner
10202   // (zero-width, single width with a substitution glyph, expanded to their code
10203   // (e.g. "<8d>"), so there's no single correct way to handle them.
10204   EXPECT_EQ("\"aaaaÄ\"\n"
10205             "\"\xc2\x8d\";",
10206             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
10207   EXPECT_EQ("\"aaaaaaaÄ\"\n"
10208             "\"\xc2\x8d\";",
10209             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
10210   EXPECT_EQ("\"Однажды, в \"\n"
10211             "\"студёную \"\n"
10212             "\"зимнюю \"\n"
10213             "\"пору,\"",
10214             format("\"Однажды, в студёную зимнюю пору,\"",
10215                    getLLVMStyleWithColumns(13)));
10216   EXPECT_EQ(
10217       "\"一 二 三 \"\n"
10218       "\"四 五六 \"\n"
10219       "\"七 八 九 \"\n"
10220       "\"十\"",
10221       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
10222   EXPECT_EQ("\"一\t二 \"\n"
10223             "\"\t三 \"\n"
10224             "\"四 五\t六 \"\n"
10225             "\"\t七 \"\n"
10226             "\"八九十\tqq\"",
10227             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
10228                    getLLVMStyleWithColumns(11)));
10229 
10230   // UTF8 character in an escape sequence.
10231   EXPECT_EQ("\"aaaaaa\"\n"
10232             "\"\\\xC2\x8D\"",
10233             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
10234 }
10235 
10236 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
10237   EXPECT_EQ("const char *sssss =\n"
10238             "    \"一二三四五六七八\\\n"
10239             " 九 十\";",
10240             format("const char *sssss = \"一二三四五六七八\\\n"
10241                    " 九 十\";",
10242                    getLLVMStyleWithColumns(30)));
10243 }
10244 
10245 TEST_F(FormatTest, SplitsUTF8LineComments) {
10246   EXPECT_EQ("// aaaaÄ\xc2\x8d",
10247             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
10248   EXPECT_EQ("// Я из лесу\n"
10249             "// вышел; был\n"
10250             "// сильный\n"
10251             "// мороз.",
10252             format("// Я из лесу вышел; был сильный мороз.",
10253                    getLLVMStyleWithColumns(13)));
10254   EXPECT_EQ("// 一二三\n"
10255             "// 四五六七\n"
10256             "// 八  九\n"
10257             "// 十",
10258             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
10259 }
10260 
10261 TEST_F(FormatTest, SplitsUTF8BlockComments) {
10262   EXPECT_EQ("/* Гляжу,\n"
10263             " * поднимается\n"
10264             " * медленно в\n"
10265             " * гору\n"
10266             " * Лошадка,\n"
10267             " * везущая\n"
10268             " * хворосту\n"
10269             " * воз. */",
10270             format("/* Гляжу, поднимается медленно в гору\n"
10271                    " * Лошадка, везущая хворосту воз. */",
10272                    getLLVMStyleWithColumns(13)));
10273   EXPECT_EQ(
10274       "/* 一二三\n"
10275       " * 四五六七\n"
10276       " * 八  九\n"
10277       " * 十  */",
10278       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
10279   EXPECT_EQ("/* �������� ��������\n"
10280             " * ��������\n"
10281             " * ������-�� */",
10282             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
10283 }
10284 
10285 #endif // _MSC_VER
10286 
10287 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
10288   FormatStyle Style = getLLVMStyle();
10289 
10290   Style.ConstructorInitializerIndentWidth = 4;
10291   verifyFormat(
10292       "SomeClass::Constructor()\n"
10293       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10294       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10295       Style);
10296 
10297   Style.ConstructorInitializerIndentWidth = 2;
10298   verifyFormat(
10299       "SomeClass::Constructor()\n"
10300       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10301       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10302       Style);
10303 
10304   Style.ConstructorInitializerIndentWidth = 0;
10305   verifyFormat(
10306       "SomeClass::Constructor()\n"
10307       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10308       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10309       Style);
10310 }
10311 
10312 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
10313   FormatStyle Style = getLLVMStyle();
10314   Style.BreakConstructorInitializersBeforeComma = true;
10315   Style.ConstructorInitializerIndentWidth = 4;
10316   verifyFormat("SomeClass::Constructor()\n"
10317                "    : a(a)\n"
10318                "    , b(b)\n"
10319                "    , c(c) {}",
10320                Style);
10321   verifyFormat("SomeClass::Constructor()\n"
10322                "    : a(a) {}",
10323                Style);
10324 
10325   Style.ColumnLimit = 0;
10326   verifyFormat("SomeClass::Constructor()\n"
10327                "    : a(a) {}",
10328                Style);
10329   verifyFormat("SomeClass::Constructor()\n"
10330                "    : a(a)\n"
10331                "    , b(b)\n"
10332                "    , c(c) {}",
10333                Style);
10334   verifyFormat("SomeClass::Constructor()\n"
10335                "    : a(a) {\n"
10336                "  foo();\n"
10337                "  bar();\n"
10338                "}",
10339                Style);
10340 
10341   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10342   verifyFormat("SomeClass::Constructor()\n"
10343                "    : a(a)\n"
10344                "    , b(b)\n"
10345                "    , c(c) {\n}",
10346                Style);
10347   verifyFormat("SomeClass::Constructor()\n"
10348                "    : a(a) {\n}",
10349                Style);
10350 
10351   Style.ColumnLimit = 80;
10352   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
10353   Style.ConstructorInitializerIndentWidth = 2;
10354   verifyFormat("SomeClass::Constructor()\n"
10355                "  : a(a)\n"
10356                "  , b(b)\n"
10357                "  , c(c) {}",
10358                Style);
10359 
10360   Style.ConstructorInitializerIndentWidth = 0;
10361   verifyFormat("SomeClass::Constructor()\n"
10362                ": a(a)\n"
10363                ", b(b)\n"
10364                ", c(c) {}",
10365                Style);
10366 
10367   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
10368   Style.ConstructorInitializerIndentWidth = 4;
10369   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
10370   verifyFormat(
10371       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
10372       Style);
10373   verifyFormat(
10374       "SomeClass::Constructor()\n"
10375       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
10376       Style);
10377   Style.ConstructorInitializerIndentWidth = 4;
10378   Style.ColumnLimit = 60;
10379   verifyFormat("SomeClass::Constructor()\n"
10380                "    : aaaaaaaa(aaaaaaaa)\n"
10381                "    , aaaaaaaa(aaaaaaaa)\n"
10382                "    , aaaaaaaa(aaaaaaaa) {}",
10383                Style);
10384 }
10385 
10386 TEST_F(FormatTest, Destructors) {
10387   verifyFormat("void F(int &i) { i.~int(); }");
10388   verifyFormat("void F(int &i) { i->~int(); }");
10389 }
10390 
10391 TEST_F(FormatTest, FormatsWithWebKitStyle) {
10392   FormatStyle Style = getWebKitStyle();
10393 
10394   // Don't indent in outer namespaces.
10395   verifyFormat("namespace outer {\n"
10396                "int i;\n"
10397                "namespace inner {\n"
10398                "    int i;\n"
10399                "} // namespace inner\n"
10400                "} // namespace outer\n"
10401                "namespace other_outer {\n"
10402                "int i;\n"
10403                "}",
10404                Style);
10405 
10406   // Don't indent case labels.
10407   verifyFormat("switch (variable) {\n"
10408                "case 1:\n"
10409                "case 2:\n"
10410                "    doSomething();\n"
10411                "    break;\n"
10412                "default:\n"
10413                "    ++variable;\n"
10414                "}",
10415                Style);
10416 
10417   // Wrap before binary operators.
10418   EXPECT_EQ("void f()\n"
10419             "{\n"
10420             "    if (aaaaaaaaaaaaaaaa\n"
10421             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
10422             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10423             "        return;\n"
10424             "}",
10425             format("void f() {\n"
10426                    "if (aaaaaaaaaaaaaaaa\n"
10427                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
10428                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10429                    "return;\n"
10430                    "}",
10431                    Style));
10432 
10433   // Allow functions on a single line.
10434   verifyFormat("void f() { return; }", Style);
10435 
10436   // Constructor initializers are formatted one per line with the "," on the
10437   // new line.
10438   verifyFormat("Constructor()\n"
10439                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10440                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
10441                "          aaaaaaaaaaaaaa)\n"
10442                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
10443                "{\n"
10444                "}",
10445                Style);
10446   verifyFormat("SomeClass::Constructor()\n"
10447                "    : a(a)\n"
10448                "{\n"
10449                "}",
10450                Style);
10451   EXPECT_EQ("SomeClass::Constructor()\n"
10452             "    : a(a)\n"
10453             "{\n"
10454             "}",
10455             format("SomeClass::Constructor():a(a){}", Style));
10456   verifyFormat("SomeClass::Constructor()\n"
10457                "    : a(a)\n"
10458                "    , b(b)\n"
10459                "    , c(c)\n"
10460                "{\n"
10461                "}",
10462                Style);
10463   verifyFormat("SomeClass::Constructor()\n"
10464                "    : a(a)\n"
10465                "{\n"
10466                "    foo();\n"
10467                "    bar();\n"
10468                "}",
10469                Style);
10470 
10471   // Access specifiers should be aligned left.
10472   verifyFormat("class C {\n"
10473                "public:\n"
10474                "    int i;\n"
10475                "};",
10476                Style);
10477 
10478   // Do not align comments.
10479   verifyFormat("int a; // Do not\n"
10480                "double b; // align comments.",
10481                Style);
10482 
10483   // Do not align operands.
10484   EXPECT_EQ("ASSERT(aaaa\n"
10485             "    || bbbb);",
10486             format("ASSERT ( aaaa\n||bbbb);", Style));
10487 
10488   // Accept input's line breaks.
10489   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
10490             "    || bbbbbbbbbbbbbbb) {\n"
10491             "    i++;\n"
10492             "}",
10493             format("if (aaaaaaaaaaaaaaa\n"
10494                    "|| bbbbbbbbbbbbbbb) { i++; }",
10495                    Style));
10496   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
10497             "    i++;\n"
10498             "}",
10499             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
10500 
10501   // Don't automatically break all macro definitions (llvm.org/PR17842).
10502   verifyFormat("#define aNumber 10", Style);
10503   // However, generally keep the line breaks that the user authored.
10504   EXPECT_EQ("#define aNumber \\\n"
10505             "    10",
10506             format("#define aNumber \\\n"
10507                    " 10",
10508                    Style));
10509 
10510   // Keep empty and one-element array literals on a single line.
10511   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
10512             "                                  copyItems:YES];",
10513             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
10514                    "copyItems:YES];",
10515                    Style));
10516   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
10517             "                                  copyItems:YES];",
10518             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
10519                    "             copyItems:YES];",
10520                    Style));
10521   // FIXME: This does not seem right, there should be more indentation before
10522   // the array literal's entries. Nested blocks have the same problem.
10523   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10524             "    @\"a\",\n"
10525             "    @\"a\"\n"
10526             "]\n"
10527             "                                  copyItems:YES];",
10528             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10529                    "     @\"a\",\n"
10530                    "     @\"a\"\n"
10531                    "     ]\n"
10532                    "       copyItems:YES];",
10533                    Style));
10534   EXPECT_EQ(
10535       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10536       "                                  copyItems:YES];",
10537       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10538              "   copyItems:YES];",
10539              Style));
10540 
10541   verifyFormat("[self.a b:c c:d];", Style);
10542   EXPECT_EQ("[self.a b:c\n"
10543             "        c:d];",
10544             format("[self.a b:c\n"
10545                    "c:d];",
10546                    Style));
10547 }
10548 
10549 TEST_F(FormatTest, FormatsLambdas) {
10550   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
10551   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
10552   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
10553   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
10554   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
10555   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
10556   verifyFormat("void f() {\n"
10557                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
10558                "}\n");
10559   verifyFormat("void f() {\n"
10560                "  other(x.begin(), //\n"
10561                "        x.end(),   //\n"
10562                "        [&](int, int) { return 1; });\n"
10563                "}\n");
10564   verifyFormat("SomeFunction([]() { // A cool function...\n"
10565                "  return 43;\n"
10566                "});");
10567   EXPECT_EQ("SomeFunction([]() {\n"
10568             "#define A a\n"
10569             "  return 43;\n"
10570             "});",
10571             format("SomeFunction([](){\n"
10572                    "#define A a\n"
10573                    "return 43;\n"
10574                    "});"));
10575   verifyFormat("void f() {\n"
10576                "  SomeFunction([](decltype(x), A *a) {});\n"
10577                "}");
10578   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10579                "    [](const aaaaaaaaaa &a) { return a; });");
10580   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
10581                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
10582                "});");
10583   verifyFormat("Constructor()\n"
10584                "    : Field([] { // comment\n"
10585                "        int i;\n"
10586                "      }) {}");
10587   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
10588                "  return some_parameter.size();\n"
10589                "};");
10590   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
10591                "    [](const string &s) { return s; };");
10592   verifyFormat("int i = aaaaaa ? 1 //\n"
10593                "               : [] {\n"
10594                "                   return 2; //\n"
10595                "                 }();");
10596   verifyFormat("llvm::errs() << \"number of twos is \"\n"
10597                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
10598                "                  return x == 2; // force break\n"
10599                "                });");
10600   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n"
10601                "    int iiiiiiiiiiii) {\n"
10602                "  return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n"
10603                "});",
10604                getLLVMStyleWithColumns(60));
10605   verifyFormat("SomeFunction({[&] {\n"
10606                "                // comment\n"
10607                "              },\n"
10608                "              [&] {\n"
10609                "                // comment\n"
10610                "              }});");
10611   verifyFormat("SomeFunction({[&] {\n"
10612                "  // comment\n"
10613                "}});");
10614   verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
10615                "                             [&]() { return true; },\n"
10616                "                         aaaaa aaaaaaaaa);");
10617 
10618   // Lambdas with return types.
10619   verifyFormat("int c = []() -> int { return 2; }();\n");
10620   verifyFormat("int c = []() -> int * { return 2; }();\n");
10621   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
10622   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
10623   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
10624   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
10625   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
10626   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
10627   verifyFormat("[a, a]() -> a<1> {};");
10628   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
10629                "                   int j) -> int {\n"
10630                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
10631                "};");
10632   verifyFormat(
10633       "aaaaaaaaaaaaaaaaaaaaaa(\n"
10634       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
10635       "      return aaaaaaaaaaaaaaaaa;\n"
10636       "    });",
10637       getLLVMStyleWithColumns(70));
10638 
10639   // Multiple lambdas in the same parentheses change indentation rules.
10640   verifyFormat("SomeFunction(\n"
10641                "    []() {\n"
10642                "      int i = 42;\n"
10643                "      return i;\n"
10644                "    },\n"
10645                "    []() {\n"
10646                "      int j = 43;\n"
10647                "      return j;\n"
10648                "    });");
10649 
10650   // More complex introducers.
10651   verifyFormat("return [i, args...] {};");
10652 
10653   // Not lambdas.
10654   verifyFormat("constexpr char hello[]{\"hello\"};");
10655   verifyFormat("double &operator[](int i) { return 0; }\n"
10656                "int i;");
10657   verifyFormat("std::unique_ptr<int[]> foo() {}");
10658   verifyFormat("int i = a[a][a]->f();");
10659   verifyFormat("int i = (*b)[a]->f();");
10660 
10661   // Other corner cases.
10662   verifyFormat("void f() {\n"
10663                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
10664                "      );\n"
10665                "}");
10666 
10667   // Lambdas created through weird macros.
10668   verifyFormat("void f() {\n"
10669                "  MACRO((const AA &a) { return 1; });\n"
10670                "  MACRO((AA &a) { return 1; });\n"
10671                "}");
10672 
10673   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
10674                "      doo_dah();\n"
10675                "      doo_dah();\n"
10676                "    })) {\n"
10677                "}");
10678   verifyFormat("auto lambda = []() {\n"
10679                "  int a = 2\n"
10680                "#if A\n"
10681                "          + 2\n"
10682                "#endif\n"
10683                "      ;\n"
10684                "};");
10685 }
10686 
10687 TEST_F(FormatTest, FormatsBlocks) {
10688   FormatStyle ShortBlocks = getLLVMStyle();
10689   ShortBlocks.AllowShortBlocksOnASingleLine = true;
10690   verifyFormat("int (^Block)(int, int);", ShortBlocks);
10691   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
10692   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
10693   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
10694   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
10695   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
10696 
10697   verifyFormat("foo(^{ bar(); });", ShortBlocks);
10698   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
10699   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
10700 
10701   verifyFormat("[operation setCompletionBlock:^{\n"
10702                "  [self onOperationDone];\n"
10703                "}];");
10704   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
10705                "  [self onOperationDone];\n"
10706                "}]};");
10707   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
10708                "  f();\n"
10709                "}];");
10710   verifyFormat("int a = [operation block:^int(int *i) {\n"
10711                "  return 1;\n"
10712                "}];");
10713   verifyFormat("[myObject doSomethingWith:arg1\n"
10714                "                      aaa:^int(int *a) {\n"
10715                "                        return 1;\n"
10716                "                      }\n"
10717                "                      bbb:f(a * bbbbbbbb)];");
10718 
10719   verifyFormat("[operation setCompletionBlock:^{\n"
10720                "  [self.delegate newDataAvailable];\n"
10721                "}];",
10722                getLLVMStyleWithColumns(60));
10723   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
10724                "  NSString *path = [self sessionFilePath];\n"
10725                "  if (path) {\n"
10726                "    // ...\n"
10727                "  }\n"
10728                "});");
10729   verifyFormat("[[SessionService sharedService]\n"
10730                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10731                "      if (window) {\n"
10732                "        [self windowDidLoad:window];\n"
10733                "      } else {\n"
10734                "        [self errorLoadingWindow];\n"
10735                "      }\n"
10736                "    }];");
10737   verifyFormat("void (^largeBlock)(void) = ^{\n"
10738                "  // ...\n"
10739                "};\n",
10740                getLLVMStyleWithColumns(40));
10741   verifyFormat("[[SessionService sharedService]\n"
10742                "    loadWindowWithCompletionBlock: //\n"
10743                "        ^(SessionWindow *window) {\n"
10744                "          if (window) {\n"
10745                "            [self windowDidLoad:window];\n"
10746                "          } else {\n"
10747                "            [self errorLoadingWindow];\n"
10748                "          }\n"
10749                "        }];",
10750                getLLVMStyleWithColumns(60));
10751   verifyFormat("[myObject doSomethingWith:arg1\n"
10752                "    firstBlock:^(Foo *a) {\n"
10753                "      // ...\n"
10754                "      int i;\n"
10755                "    }\n"
10756                "    secondBlock:^(Bar *b) {\n"
10757                "      // ...\n"
10758                "      int i;\n"
10759                "    }\n"
10760                "    thirdBlock:^Foo(Bar *b) {\n"
10761                "      // ...\n"
10762                "      int i;\n"
10763                "    }];");
10764   verifyFormat("[myObject doSomethingWith:arg1\n"
10765                "               firstBlock:-1\n"
10766                "              secondBlock:^(Bar *b) {\n"
10767                "                // ...\n"
10768                "                int i;\n"
10769                "              }];");
10770 
10771   verifyFormat("f(^{\n"
10772                "  @autoreleasepool {\n"
10773                "    if (a) {\n"
10774                "      g();\n"
10775                "    }\n"
10776                "  }\n"
10777                "});");
10778   verifyFormat("Block b = ^int *(A *a, B *b) {}");
10779 
10780   FormatStyle FourIndent = getLLVMStyle();
10781   FourIndent.ObjCBlockIndentWidth = 4;
10782   verifyFormat("[operation setCompletionBlock:^{\n"
10783                "    [self onOperationDone];\n"
10784                "}];",
10785                FourIndent);
10786 }
10787 
10788 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
10789   FormatStyle ZeroColumn = getLLVMStyle();
10790   ZeroColumn.ColumnLimit = 0;
10791 
10792   verifyFormat("[[SessionService sharedService] "
10793                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10794                "  if (window) {\n"
10795                "    [self windowDidLoad:window];\n"
10796                "  } else {\n"
10797                "    [self errorLoadingWindow];\n"
10798                "  }\n"
10799                "}];",
10800                ZeroColumn);
10801   EXPECT_EQ("[[SessionService sharedService]\n"
10802             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10803             "      if (window) {\n"
10804             "        [self windowDidLoad:window];\n"
10805             "      } else {\n"
10806             "        [self errorLoadingWindow];\n"
10807             "      }\n"
10808             "    }];",
10809             format("[[SessionService sharedService]\n"
10810                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10811                    "                if (window) {\n"
10812                    "    [self windowDidLoad:window];\n"
10813                    "  } else {\n"
10814                    "    [self errorLoadingWindow];\n"
10815                    "  }\n"
10816                    "}];",
10817                    ZeroColumn));
10818   verifyFormat("[myObject doSomethingWith:arg1\n"
10819                "    firstBlock:^(Foo *a) {\n"
10820                "      // ...\n"
10821                "      int i;\n"
10822                "    }\n"
10823                "    secondBlock:^(Bar *b) {\n"
10824                "      // ...\n"
10825                "      int i;\n"
10826                "    }\n"
10827                "    thirdBlock:^Foo(Bar *b) {\n"
10828                "      // ...\n"
10829                "      int i;\n"
10830                "    }];",
10831                ZeroColumn);
10832   verifyFormat("f(^{\n"
10833                "  @autoreleasepool {\n"
10834                "    if (a) {\n"
10835                "      g();\n"
10836                "    }\n"
10837                "  }\n"
10838                "});",
10839                ZeroColumn);
10840   verifyFormat("void (^largeBlock)(void) = ^{\n"
10841                "  // ...\n"
10842                "};",
10843                ZeroColumn);
10844 
10845   ZeroColumn.AllowShortBlocksOnASingleLine = true;
10846   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
10847             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
10848   ZeroColumn.AllowShortBlocksOnASingleLine = false;
10849   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
10850             "  int i;\n"
10851             "};",
10852             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
10853 }
10854 
10855 TEST_F(FormatTest, SupportsCRLF) {
10856   EXPECT_EQ("int a;\r\n"
10857             "int b;\r\n"
10858             "int c;\r\n",
10859             format("int a;\r\n"
10860                    "  int b;\r\n"
10861                    "    int c;\r\n",
10862                    getLLVMStyle()));
10863   EXPECT_EQ("int a;\r\n"
10864             "int b;\r\n"
10865             "int c;\r\n",
10866             format("int a;\r\n"
10867                    "  int b;\n"
10868                    "    int c;\r\n",
10869                    getLLVMStyle()));
10870   EXPECT_EQ("int a;\n"
10871             "int b;\n"
10872             "int c;\n",
10873             format("int a;\r\n"
10874                    "  int b;\n"
10875                    "    int c;\n",
10876                    getLLVMStyle()));
10877   EXPECT_EQ("\"aaaaaaa \"\r\n"
10878             "\"bbbbbbb\";\r\n",
10879             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
10880   EXPECT_EQ("#define A \\\r\n"
10881             "  b;      \\\r\n"
10882             "  c;      \\\r\n"
10883             "  d;\r\n",
10884             format("#define A \\\r\n"
10885                    "  b; \\\r\n"
10886                    "  c; d; \r\n",
10887                    getGoogleStyle()));
10888 
10889   EXPECT_EQ("/*\r\n"
10890             "multi line block comments\r\n"
10891             "should not introduce\r\n"
10892             "an extra carriage return\r\n"
10893             "*/\r\n",
10894             format("/*\r\n"
10895                    "multi line block comments\r\n"
10896                    "should not introduce\r\n"
10897                    "an extra carriage return\r\n"
10898                    "*/\r\n"));
10899 }
10900 
10901 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
10902   verifyFormat("MY_CLASS(C) {\n"
10903                "  int i;\n"
10904                "  int j;\n"
10905                "};");
10906 }
10907 
10908 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
10909   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
10910   TwoIndent.ContinuationIndentWidth = 2;
10911 
10912   EXPECT_EQ("int i =\n"
10913             "  longFunction(\n"
10914             "    arg);",
10915             format("int i = longFunction(arg);", TwoIndent));
10916 
10917   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
10918   SixIndent.ContinuationIndentWidth = 6;
10919 
10920   EXPECT_EQ("int i =\n"
10921             "      longFunction(\n"
10922             "            arg);",
10923             format("int i = longFunction(arg);", SixIndent));
10924 }
10925 
10926 TEST_F(FormatTest, SpacesInAngles) {
10927   FormatStyle Spaces = getLLVMStyle();
10928   Spaces.SpacesInAngles = true;
10929 
10930   verifyFormat("static_cast< int >(arg);", Spaces);
10931   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
10932   verifyFormat("f< int, float >();", Spaces);
10933   verifyFormat("template <> g() {}", Spaces);
10934   verifyFormat("template < std::vector< int > > f() {}", Spaces);
10935   verifyFormat("std::function< void(int, int) > fct;", Spaces);
10936   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
10937                Spaces);
10938 
10939   Spaces.Standard = FormatStyle::LS_Cpp03;
10940   Spaces.SpacesInAngles = true;
10941   verifyFormat("A< A< int > >();", Spaces);
10942 
10943   Spaces.SpacesInAngles = false;
10944   verifyFormat("A<A<int> >();", Spaces);
10945 
10946   Spaces.Standard = FormatStyle::LS_Cpp11;
10947   Spaces.SpacesInAngles = true;
10948   verifyFormat("A< A< int > >();", Spaces);
10949 
10950   Spaces.SpacesInAngles = false;
10951   verifyFormat("A<A<int>>();", Spaces);
10952 }
10953 
10954 TEST_F(FormatTest, TripleAngleBrackets) {
10955   verifyFormat("f<<<1, 1>>>();");
10956   verifyFormat("f<<<1, 1, 1, s>>>();");
10957   verifyFormat("f<<<a, b, c, d>>>();");
10958   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
10959   verifyFormat("f<param><<<1, 1>>>();");
10960   verifyFormat("f<1><<<1, 1>>>();");
10961   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
10962   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10963                "aaaaaaaaaaa<<<\n    1, 1>>>();");
10964 }
10965 
10966 TEST_F(FormatTest, MergeLessLessAtEnd) {
10967   verifyFormat("<<");
10968   EXPECT_EQ("< < <", format("\\\n<<<"));
10969   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10970                "aaallvm::outs() <<");
10971   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10972                "aaaallvm::outs()\n    <<");
10973 }
10974 
10975 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
10976   std::string code = "#if A\n"
10977                      "#if B\n"
10978                      "a.\n"
10979                      "#endif\n"
10980                      "    a = 1;\n"
10981                      "#else\n"
10982                      "#endif\n"
10983                      "#if C\n"
10984                      "#else\n"
10985                      "#endif\n";
10986   EXPECT_EQ(code, format(code));
10987 }
10988 
10989 TEST_F(FormatTest, HandleConflictMarkers) {
10990   // Git/SVN conflict markers.
10991   EXPECT_EQ("int a;\n"
10992             "void f() {\n"
10993             "  callme(some(parameter1,\n"
10994             "<<<<<<< text by the vcs\n"
10995             "              parameter2),\n"
10996             "||||||| text by the vcs\n"
10997             "              parameter2),\n"
10998             "         parameter3,\n"
10999             "======= text by the vcs\n"
11000             "              parameter2, parameter3),\n"
11001             ">>>>>>> text by the vcs\n"
11002             "         otherparameter);\n",
11003             format("int a;\n"
11004                    "void f() {\n"
11005                    "  callme(some(parameter1,\n"
11006                    "<<<<<<< text by the vcs\n"
11007                    "  parameter2),\n"
11008                    "||||||| text by the vcs\n"
11009                    "  parameter2),\n"
11010                    "  parameter3,\n"
11011                    "======= text by the vcs\n"
11012                    "  parameter2,\n"
11013                    "  parameter3),\n"
11014                    ">>>>>>> text by the vcs\n"
11015                    "  otherparameter);\n"));
11016 
11017   // Perforce markers.
11018   EXPECT_EQ("void f() {\n"
11019             "  function(\n"
11020             ">>>> text by the vcs\n"
11021             "      parameter,\n"
11022             "==== text by the vcs\n"
11023             "      parameter,\n"
11024             "==== text by the vcs\n"
11025             "      parameter,\n"
11026             "<<<< text by the vcs\n"
11027             "      parameter);\n",
11028             format("void f() {\n"
11029                    "  function(\n"
11030                    ">>>> text by the vcs\n"
11031                    "  parameter,\n"
11032                    "==== text by the vcs\n"
11033                    "  parameter,\n"
11034                    "==== text by the vcs\n"
11035                    "  parameter,\n"
11036                    "<<<< text by the vcs\n"
11037                    "  parameter);\n"));
11038 
11039   EXPECT_EQ("<<<<<<<\n"
11040             "|||||||\n"
11041             "=======\n"
11042             ">>>>>>>",
11043             format("<<<<<<<\n"
11044                    "|||||||\n"
11045                    "=======\n"
11046                    ">>>>>>>"));
11047 
11048   EXPECT_EQ("<<<<<<<\n"
11049             "|||||||\n"
11050             "int i;\n"
11051             "=======\n"
11052             ">>>>>>>",
11053             format("<<<<<<<\n"
11054                    "|||||||\n"
11055                    "int i;\n"
11056                    "=======\n"
11057                    ">>>>>>>"));
11058 
11059   // FIXME: Handle parsing of macros around conflict markers correctly:
11060   EXPECT_EQ("#define Macro \\\n"
11061             "<<<<<<<\n"
11062             "Something \\\n"
11063             "|||||||\n"
11064             "Else \\\n"
11065             "=======\n"
11066             "Other \\\n"
11067             ">>>>>>>\n"
11068             "    End int i;\n",
11069             format("#define Macro \\\n"
11070                    "<<<<<<<\n"
11071                    "  Something \\\n"
11072                    "|||||||\n"
11073                    "  Else \\\n"
11074                    "=======\n"
11075                    "  Other \\\n"
11076                    ">>>>>>>\n"
11077                    "  End\n"
11078                    "int i;\n"));
11079 }
11080 
11081 TEST_F(FormatTest, DisableRegions) {
11082   EXPECT_EQ("int i;\n"
11083             "// clang-format off\n"
11084             "  int j;\n"
11085             "// clang-format on\n"
11086             "int k;",
11087             format(" int  i;\n"
11088                    "   // clang-format off\n"
11089                    "  int j;\n"
11090                    " // clang-format on\n"
11091                    "   int   k;"));
11092   EXPECT_EQ("int i;\n"
11093             "/* clang-format off */\n"
11094             "  int j;\n"
11095             "/* clang-format on */\n"
11096             "int k;",
11097             format(" int  i;\n"
11098                    "   /* clang-format off */\n"
11099                    "  int j;\n"
11100                    " /* clang-format on */\n"
11101                    "   int   k;"));
11102 }
11103 
11104 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
11105   format("? ) =");
11106   verifyNoCrash("#define a\\\n /**/}");
11107 }
11108 
11109 TEST_F(FormatTest, FormatsTableGenCode) {
11110   FormatStyle Style = getLLVMStyle();
11111   Style.Language = FormatStyle::LK_TableGen;
11112   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
11113 }
11114 
11115 } // end namespace
11116 } // end namespace format
11117 } // end namespace clang
11118